| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 6 |  * 
 | 
|---|
| 7 |  *
 | 
|---|
| 8 |  *   This file is part of MoleCuilder.
 | 
|---|
| 9 |  *
 | 
|---|
| 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 13 |  *    (at your option) any later version.
 | 
|---|
| 14 |  *
 | 
|---|
| 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 18 |  *    GNU General Public License for more details.
 | 
|---|
| 19 |  *
 | 
|---|
| 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 22 |  */
 | 
|---|
| 23 | 
 | 
|---|
| 24 | /*
 | 
|---|
| 25 |  * FragmentationAutomationAction.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: May 18, 2012
 | 
|---|
| 28 |  *      Author: heber
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
| 37 | // boost asio needs specific operator new
 | 
|---|
| 38 | #include <boost/asio.hpp>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | //// include headers that implement a archive in simple text format
 | 
|---|
| 43 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
| 44 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
| 45 | 
 | 
|---|
| 46 | //
 | 
|---|
| 47 | //#include <boost/mpl/remove.hpp>
 | 
|---|
| 48 | //#include <boost/lambda/lambda.hpp>
 | 
|---|
| 49 | 
 | 
|---|
| 50 | //#include <iostream>
 | 
|---|
| 51 | 
 | 
|---|
| 52 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 53 | #include "CodePatterns/Info.hpp"
 | 
|---|
| 54 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 55 | #include "JobMarket/Jobs/FragmentJob.hpp"
 | 
|---|
| 56 | 
 | 
|---|
| 57 | #include "Fragmentation/Automation/FragmentJobQueue.hpp"
 | 
|---|
| 58 | #include "Fragmentation/Automation/MPQCFragmentController.hpp"
 | 
|---|
| 59 | #include "Fragmentation/Summation/Containers/FragmentationChargeDensity.hpp"
 | 
|---|
| 60 | #include "Fragmentation/Summation/Containers/FragmentationLongRangeResults.hpp"
 | 
|---|
| 61 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
 | 
|---|
| 62 | #include "Fragmentation/Summation/Containers/FragmentationShortRangeResults.hpp"
 | 
|---|
| 63 | #include "Fragmentation/Summation/Containers/MPQCData.hpp"
 | 
|---|
| 64 | #include "Fragmentation/KeySetsContainer.hpp"
 | 
|---|
| 65 | #ifdef HAVE_VMG
 | 
|---|
| 66 | #include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp"
 | 
|---|
| 67 | #include "Fragmentation/Automation/VMGFragmentController.hpp"
 | 
|---|
| 68 | #include "Fragmentation/Summation/Containers/VMGData.hpp"
 | 
|---|
| 69 | #include "Fragmentation/Summation/Containers/VMGDataFused.hpp"
 | 
|---|
| 70 | #include "Fragmentation/Summation/Containers/VMGDataMap.hpp"
 | 
|---|
| 71 | #include "Fragmentation/Summation/Containers/VMGData_printKeyNames.hpp"
 | 
|---|
| 72 | #endif
 | 
|---|
| 73 | #include "World.hpp"
 | 
|---|
| 74 | 
 | 
|---|
| 75 | #include <fstream>
 | 
|---|
| 76 | #include <iostream>
 | 
|---|
| 77 | #include <string>
 | 
|---|
| 78 | #include <vector>
 | 
|---|
| 79 | 
 | 
|---|
| 80 | #include <boost/mpl/for_each.hpp>
 | 
|---|
| 81 | 
 | 
|---|
| 82 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
 | 
|---|
| 83 | 
 | 
|---|
| 84 | using namespace MoleCuilder;
 | 
|---|
| 85 | 
 | 
|---|
| 86 | // and construct the stuff
 | 
|---|
| 87 | #include "FragmentationAutomationAction.def"
 | 
|---|
| 88 | #include "Action_impl_pre.hpp"
 | 
|---|
| 89 | /** =========== define the function ====================== */
 | 
|---|
| 90 | 
 | 
|---|
| 91 | class controller_AddOn;
 | 
|---|
| 92 | 
 | 
|---|
| 93 | // needs to be defined for using the FragmentController
 | 
|---|
| 94 | controller_AddOn *getAddOn()
 | 
|---|
| 95 | {
 | 
|---|
| 96 |   return NULL;
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | ActionState::ptr FragmentationFragmentationAutomationAction::performCall() {
 | 
|---|
| 100 |   boost::asio::io_service io_service;
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   // TODO: Have io_service run in second thread and merge with current again eventually
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   FragmentationResultContainer &container =
 | 
|---|
| 105 |       FragmentationResultContainer::getInstance();
 | 
|---|
| 106 |   const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets();
 | 
|---|
| 107 |   const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets();
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   size_t Exitflag = 0;
 | 
|---|
| 110 |   std::map<JobId_t, MPQCData> shortrangedata;
 | 
|---|
| 111 |   {
 | 
|---|
| 112 |     const size_t NumberJobs = FragmentJobQueue::getInstance().size();
 | 
|---|
| 113 |     MPQCFragmentController mpqccontroller(io_service);
 | 
|---|
| 114 |     mpqccontroller.setHost(params.host.get());
 | 
|---|
| 115 |     mpqccontroller.setPort(params.port.get());
 | 
|---|
| 116 |     // Phase One: obtain ids
 | 
|---|
| 117 |     mpqccontroller.requestIds(NumberJobs);
 | 
|---|
| 118 | 
 | 
|---|
| 119 |     // Phase Two: add MPQCJobs and send
 | 
|---|
| 120 |     const size_t NoJobs = mpqccontroller.addJobsFromQueue(
 | 
|---|
| 121 |         params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
 | 
|---|
| 122 |         params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly
 | 
|---|
| 123 |         );
 | 
|---|
| 124 |     LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue.");
 | 
|---|
| 125 |     mpqccontroller.run();
 | 
|---|
| 126 | 
 | 
|---|
| 127 |     // Phase Three: calculate result
 | 
|---|
| 128 |     mpqccontroller.waitforResults(NumberJobs);
 | 
|---|
| 129 |     mpqccontroller.getResults(shortrangedata);
 | 
|---|
| 130 | 
 | 
|---|
| 131 |     Exitflag += mpqccontroller.getExitflag();
 | 
|---|
| 132 |   }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | #ifdef HAVE_VMG
 | 
|---|
| 135 |   if (params.DoLongrange.get()) {
 | 
|---|
| 136 |   if ( World::getInstance().getAllAtoms().size() == 0) {
 | 
|---|
| 137 |     ELOG(1, "Please load the full molecule into the world before starting this action.");
 | 
|---|
| 138 |     return Action::failure;
 | 
|---|
| 139 |   }
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   // obtain combined charge density
 | 
|---|
| 142 |   FragmentationChargeDensity summedChargeDensity(
 | 
|---|
| 143 |       shortrangedata,
 | 
|---|
| 144 |       FragmentJobQueue::getInstance().getKeySets());
 | 
|---|
| 145 |   const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
 | 
|---|
| 146 |   LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and "
 | 
|---|
| 147 |       << full_sample.size() << " level-wise long-range jobs.");
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   // check boundary conditions
 | 
|---|
| 150 |   const BoundaryConditions::Conditions_t &conditions =
 | 
|---|
| 151 |       World::getInstance().getDomain().getConditions();
 | 
|---|
| 152 |   const bool OpenBoundaryConditions =
 | 
|---|
| 153 |       !((conditions[0] == BoundaryConditions::Wrap) &&
 | 
|---|
| 154 |       (conditions[1] == BoundaryConditions::Wrap) &&
 | 
|---|
| 155 |       (conditions[2] == BoundaryConditions::Wrap));
 | 
|---|
| 156 |   LOG(1, std::string("INFO: Using ") 
 | 
|---|
| 157 |       << (OpenBoundaryConditions ? "open" : "periodic")
 | 
|---|
| 158 |       << " boundary conditions.");
 | 
|---|
| 159 | 
 | 
|---|
| 160 |   // Phase Four: obtain more ids
 | 
|---|
| 161 |   std::map<JobId_t, VMGData> longrangedata;
 | 
|---|
| 162 |   {
 | 
|---|
| 163 |     VMGFragmentController vmgcontroller(io_service);
 | 
|---|
| 164 |     vmgcontroller.setHost(params.host.get());
 | 
|---|
| 165 |     vmgcontroller.setPort(params.port.get());
 | 
|---|
| 166 |     const size_t NoJobs = shortrangedata.size()+full_sample.size();
 | 
|---|
| 167 |     vmgcontroller.requestIds(2*NoJobs);
 | 
|---|
| 168 | 
 | 
|---|
| 169 |     // Phase Five a: create VMGJobs for electronic charge distribution
 | 
|---|
| 170 |     const size_t near_field_cells = params.near_field_cells.get();
 | 
|---|
| 171 |     const size_t interpolation_degree = params.interpolation_degree.get();
 | 
|---|
| 172 |     if (!vmgcontroller.createLongRangeJobs(
 | 
|---|
| 173 |         shortrangedata,
 | 
|---|
| 174 |         full_sample,
 | 
|---|
| 175 |         near_field_cells,
 | 
|---|
| 176 |         interpolation_degree,
 | 
|---|
| 177 |         VMGFragmentController::DontSampleParticles,
 | 
|---|
| 178 |         VMGFragmentController::DoTreatGrid,
 | 
|---|
| 179 |         params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
 | 
|---|
| 180 |         params.DoPrintDebug.get(),
 | 
|---|
| 181 |         OpenBoundaryConditions))
 | 
|---|
| 182 |       return Action::failure;
 | 
|---|
| 183 | 
 | 
|---|
| 184 |     // Phase Six a: calculate result
 | 
|---|
| 185 |     vmgcontroller.waitforResults(NoJobs);
 | 
|---|
| 186 |     vmgcontroller.getResults(longrangedata);
 | 
|---|
| 187 |     ASSERT( NoJobs == longrangedata.size(),
 | 
|---|
| 188 |         "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
 | 
|---|
| 189 |         +toString(full_sample.size())+"="+toString(NoJobs)
 | 
|---|
| 190 |         +" and first VMGresults "+toString(longrangedata.size())+" don't match.");
 | 
|---|
| 191 |     Exitflag += vmgcontroller.getExitflag();
 | 
|---|
| 192 | 
 | 
|---|
| 193 |     {
 | 
|---|
| 194 |       std::map<JobId_t, VMGData> longrangedata_both;
 | 
|---|
| 195 |       // Phase Five b: create VMGJobs for nuclei charge distributions
 | 
|---|
| 196 |       const size_t near_field_cells = params.near_field_cells.get();
 | 
|---|
| 197 |       const size_t interpolation_degree = params.interpolation_degree.get();
 | 
|---|
| 198 |       if (!vmgcontroller.createLongRangeJobs(
 | 
|---|
| 199 |           shortrangedata,
 | 
|---|
| 200 |           full_sample,
 | 
|---|
| 201 |           near_field_cells,
 | 
|---|
| 202 |           interpolation_degree,
 | 
|---|
| 203 |           VMGFragmentController::DoSampleParticles,
 | 
|---|
| 204 |           VMGFragmentController::DoTreatGrid,
 | 
|---|
| 205 |           params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
 | 
|---|
| 206 |           params.DoPrintDebug.get(),
 | 
|---|
| 207 |           OpenBoundaryConditions))
 | 
|---|
| 208 |         return Action::failure;
 | 
|---|
| 209 | 
 | 
|---|
| 210 |       // Phase Six b: calculate result
 | 
|---|
| 211 |       vmgcontroller.waitforResults(NoJobs);
 | 
|---|
| 212 |       vmgcontroller.getResults(longrangedata_both);
 | 
|---|
| 213 |       ASSERT( NoJobs == longrangedata_both.size(),
 | 
|---|
| 214 |           "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
 | 
|---|
| 215 |           +toString(full_sample.size())+"="+toString(NoJobs)
 | 
|---|
| 216 |           +" and second VMGresults "+toString(longrangedata_both.size())+" don't match.");
 | 
|---|
| 217 |       Exitflag += vmgcontroller.getExitflag();
 | 
|---|
| 218 | 
 | 
|---|
| 219 |       // go through either data and replace nuclei_long with contribution from both
 | 
|---|
| 220 |       ASSERT( longrangedata.size() == longrangedata_both.size(),
 | 
|---|
| 221 |           "FragmentationFragmentationAutomationAction::performCall() - longrange results have different sizes.");
 | 
|---|
| 222 |       std::map<JobId_t, VMGData>::iterator destiter = longrangedata.begin();
 | 
|---|
| 223 |       std::map<JobId_t, VMGData>::const_iterator srciter = longrangedata_both.begin();
 | 
|---|
| 224 |       for (;destiter != longrangedata.end(); ++srciter, ++destiter) {
 | 
|---|
| 225 |         destiter->second.both_sampled_potential = srciter->second.sampled_potential;
 | 
|---|
| 226 |         destiter->second.nuclei_long = srciter->second.nuclei_long;
 | 
|---|
| 227 |       }
 | 
|---|
| 228 |     }
 | 
|---|
| 229 |   }
 | 
|---|
| 230 | 
 | 
|---|
| 231 |   if (params.DoPrintDebug.get()) {
 | 
|---|
| 232 |     std::map<JobId_t, std::string> debugData;
 | 
|---|
| 233 |     {
 | 
|---|
| 234 |       if (!full_sample.empty()) {
 | 
|---|
| 235 |         // create debug jobs for each level to print the summed-up potential to vtk files
 | 
|---|
| 236 |         VMGDebugGridFragmentController debugcontroller(io_service);
 | 
|---|
| 237 |         debugcontroller.setHost(params.host.get());
 | 
|---|
| 238 |         debugcontroller.setPort(params.port.get());
 | 
|---|
| 239 |         debugcontroller.requestIds(full_sample.size());
 | 
|---|
| 240 |         if (!debugcontroller.createDebugJobs(full_sample, OpenBoundaryConditions))
 | 
|---|
| 241 |           return Action::failure;
 | 
|---|
| 242 |         debugcontroller.waitforResults(full_sample.size());
 | 
|---|
| 243 |         debugcontroller.getResults(debugData);
 | 
|---|
| 244 |         Exitflag += debugcontroller.getExitflag();
 | 
|---|
| 245 |       }
 | 
|---|
| 246 |     }
 | 
|---|
| 247 |   }
 | 
|---|
| 248 |   container.addFullResults(keysets, forcekeysets, shortrangedata, longrangedata);
 | 
|---|
| 249 |   } else {
 | 
|---|
| 250 |     container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
 | 
|---|
| 251 |   }
 | 
|---|
| 252 | #else
 | 
|---|
| 253 |   container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
 | 
|---|
| 254 | #endif
 | 
|---|
| 255 | 
 | 
|---|
| 256 |   // now clear all present jobs as we are done
 | 
|---|
| 257 |   FragmentJobQueue::getInstance().clear();
 | 
|---|
| 258 | 
 | 
|---|
| 259 |   // if file is given, advise results container to store to file
 | 
|---|
| 260 |   if (!params.resultsfile.get().empty()) {
 | 
|---|
| 261 |     boost::filesystem::path resultsfile = params.resultsfile.get();
 | 
|---|
| 262 |     std::ofstream returnstream(resultsfile.string().c_str());
 | 
|---|
| 263 |     if (returnstream.good()) {
 | 
|---|
| 264 |       boost::archive::text_oarchive oa(returnstream);
 | 
|---|
| 265 |       oa << container;
 | 
|---|
| 266 |     }
 | 
|---|
| 267 |     Exitflag += (int)(!returnstream.good());
 | 
|---|
| 268 |     returnstream.close();
 | 
|---|
| 269 |   }
 | 
|---|
| 270 | 
 | 
|---|
| 271 |   return (Exitflag == 0) ? Action::success : Action::failure;
 | 
|---|
| 272 | }
 | 
|---|
| 273 | 
 | 
|---|
| 274 | ActionState::ptr FragmentationFragmentationAutomationAction::performUndo(ActionState::ptr _state) {
 | 
|---|
| 275 |   return Action::success;
 | 
|---|
| 276 | }
 | 
|---|
| 277 | 
 | 
|---|
| 278 | ActionState::ptr FragmentationFragmentationAutomationAction::performRedo(ActionState::ptr _state){
 | 
|---|
| 279 |   return Action::success;
 | 
|---|
| 280 | }
 | 
|---|
| 281 | 
 | 
|---|
| 282 | bool FragmentationFragmentationAutomationAction::canUndo() {
 | 
|---|
| 283 |   return false;
 | 
|---|
| 284 | }
 | 
|---|
| 285 | 
 | 
|---|
| 286 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
 | 
|---|
| 287 |   return false;
 | 
|---|
| 288 | }
 | 
|---|
| 289 | /** =========== end of function ====================== */
 | 
|---|