| [edecba] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [5aaa43] | 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| [94d5ac6] | 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/>. | 
|---|
| [edecba] | 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 |  | 
|---|
| [a3427f] | 42 | //// include headers that implement a archive in simple text format | 
|---|
| [55e1bc] | 43 | #include <boost/archive/text_oarchive.hpp> | 
|---|
|  | 44 | #include <boost/archive/text_iarchive.hpp> | 
|---|
| [240c3e] | 45 |  | 
|---|
| [a3427f] | 46 | // | 
|---|
|  | 47 | //#include <boost/mpl/remove.hpp> | 
|---|
|  | 48 | //#include <boost/lambda/lambda.hpp> | 
|---|
| [fb881a] | 49 |  | 
|---|
| [a3427f] | 50 | //#include <iostream> | 
|---|
| [240c3e] | 51 |  | 
|---|
| [6ca578] | 52 | #include "CodePatterns/Assert.hpp" | 
|---|
| [edecba] | 53 | #include "CodePatterns/Info.hpp" | 
|---|
|  | 54 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 55 | #include "JobMarket/Jobs/FragmentJob.hpp" | 
|---|
|  | 56 |  | 
|---|
| [ac9ca4] | 57 | #include "Fragmentation/Automation/FragmentJobQueue.hpp" | 
|---|
| [ffe057] | 58 | #include "Fragmentation/Automation/MPQCFragmentController.hpp" | 
|---|
| [fbf143] | 59 | #include "Fragmentation/Summation/Containers/FragmentationChargeDensity.hpp" | 
|---|
|  | 60 | #include "Fragmentation/Summation/Containers/FragmentationLongRangeResults.hpp" | 
|---|
| [a3427f] | 61 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp" | 
|---|
| [fbf143] | 62 | #include "Fragmentation/Summation/Containers/FragmentationShortRangeResults.hpp" | 
|---|
|  | 63 | #include "Fragmentation/Summation/Containers/MPQCData.hpp" | 
|---|
| [a3427f] | 64 | #include "Fragmentation/KeySetsContainer.hpp" | 
|---|
| [69c733] | 65 | #ifdef HAVE_VMG | 
|---|
| [a3427f] | 66 | #include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp" | 
|---|
|  | 67 | #include "Fragmentation/Automation/VMGFragmentController.hpp" | 
|---|
| [fbf143] | 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" | 
|---|
| [69c733] | 72 | #endif | 
|---|
| [edecba] | 73 | #include "World.hpp" | 
|---|
|  | 74 |  | 
|---|
| [c09f94] | 75 | #include <boost/bind.hpp> | 
|---|
|  | 76 | #include <boost/thread.hpp> | 
|---|
| [86cfac5] | 77 | #include <fstream> | 
|---|
| [edecba] | 78 | #include <iostream> | 
|---|
|  | 79 | #include <string> | 
|---|
|  | 80 | #include <vector> | 
|---|
|  | 81 |  | 
|---|
| [8cae4c] | 82 | #include <boost/mpl/for_each.hpp> | 
|---|
|  | 83 |  | 
|---|
| [edecba] | 84 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp" | 
|---|
|  | 85 |  | 
|---|
|  | 86 | using namespace MoleCuilder; | 
|---|
|  | 87 |  | 
|---|
|  | 88 | // and construct the stuff | 
|---|
|  | 89 | #include "FragmentationAutomationAction.def" | 
|---|
|  | 90 | #include "Action_impl_pre.hpp" | 
|---|
|  | 91 | /** =========== define the function ====================== */ | 
|---|
|  | 92 |  | 
|---|
|  | 93 | class controller_AddOn; | 
|---|
|  | 94 |  | 
|---|
|  | 95 | // needs to be defined for using the FragmentController | 
|---|
|  | 96 | controller_AddOn *getAddOn() | 
|---|
|  | 97 | { | 
|---|
|  | 98 | return NULL; | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
| [c09f94] | 101 | static void updateSteps(Process &p, const size_t step, const size_t total) | 
|---|
|  | 102 | { | 
|---|
|  | 103 | LOG(1, "There are " << step << " steps out of " << total << " done."); | 
|---|
|  | 104 | p.setCurrStep(step); | 
|---|
|  | 105 | } | 
|---|
|  | 106 |  | 
|---|
| [b5b01e] | 107 | ActionState::ptr FragmentationFragmentationAutomationAction::performCall() { | 
|---|
| [c5324f] | 108 | boost::asio::io_service io_service; | 
|---|
|  | 109 |  | 
|---|
|  | 110 | // TODO: Have io_service run in second thread and merge with current again eventually | 
|---|
|  | 111 |  | 
|---|
| [a3427f] | 112 | FragmentationResultContainer &container = | 
|---|
|  | 113 | FragmentationResultContainer::getInstance(); | 
|---|
|  | 114 | const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets(); | 
|---|
|  | 115 | const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets(); | 
|---|
|  | 116 |  | 
|---|
| [4f056e] | 117 | size_t Exitflag = 0; | 
|---|
| [a3427f] | 118 | std::map<JobId_t, MPQCData> shortrangedata; | 
|---|
| [4f056e] | 119 | { | 
|---|
| [ac9ca4] | 120 | const size_t NumberJobs = FragmentJobQueue::getInstance().size(); | 
|---|
| [4f056e] | 121 | MPQCFragmentController mpqccontroller(io_service); | 
|---|
|  | 122 | mpqccontroller.setHost(params.host.get()); | 
|---|
|  | 123 | mpqccontroller.setPort(params.port.get()); | 
|---|
|  | 124 | // Phase One: obtain ids | 
|---|
| [ac9ca4] | 125 | mpqccontroller.requestIds(NumberJobs); | 
|---|
| [4f056e] | 126 |  | 
|---|
| [ac9ca4] | 127 | // Phase Two: add MPQCJobs and send | 
|---|
| [503acc1] | 128 | const size_t NoJobs = mpqccontroller.addJobsFromQueue( | 
|---|
| [6ff62c] | 129 | params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity, | 
|---|
|  | 130 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly | 
|---|
|  | 131 | ); | 
|---|
| [503acc1] | 132 | LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue."); | 
|---|
| [ac9ca4] | 133 | mpqccontroller.run(); | 
|---|
| [4f056e] | 134 |  | 
|---|
|  | 135 | // Phase Three: calculate result | 
|---|
| [c09f94] | 136 | setMaxSteps(NumberJobs); | 
|---|
|  | 137 | mpqccontroller.setUpdateHandler( | 
|---|
|  | 138 | boost::bind(&updateSteps, boost::ref(*this), _1, _2) | 
|---|
|  | 139 | ); | 
|---|
|  | 140 | start(); | 
|---|
|  | 141 | boost::thread wait_thread( | 
|---|
|  | 142 | boost::bind(&MPQCFragmentController::waitforResults, boost::ref(mpqccontroller), boost::cref(NumberJobs)) | 
|---|
|  | 143 | ); | 
|---|
|  | 144 | wait_thread.join(); | 
|---|
|  | 145 | stop(); | 
|---|
| [a3427f] | 146 | mpqccontroller.getResults(shortrangedata); | 
|---|
| [4f056e] | 147 |  | 
|---|
|  | 148 | Exitflag += mpqccontroller.getExitflag(); | 
|---|
|  | 149 | } | 
|---|
| [c5324f] | 150 |  | 
|---|
| [69c733] | 151 | #ifdef HAVE_VMG | 
|---|
|  | 152 | if (params.DoLongrange.get()) { | 
|---|
| [db6f7d] | 153 | if ( World::getInstance().getAllAtoms().size() == 0) { | 
|---|
|  | 154 | ELOG(1, "Please load the full molecule into the world before starting this action."); | 
|---|
|  | 155 | return Action::failure; | 
|---|
|  | 156 | } | 
|---|
| [06865e] | 157 |  | 
|---|
| [849cd8] | 158 | // obtain combined charge density | 
|---|
| [27594e] | 159 | FragmentationChargeDensity summedChargeDensity( | 
|---|
| [a3427f] | 160 | shortrangedata, | 
|---|
| [bae7bc] | 161 | FragmentJobQueue::getInstance().getKeySets()); | 
|---|
| [27594e] | 162 | const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid(); | 
|---|
| [a3427f] | 163 | LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and " | 
|---|
| [4f056e] | 164 | << full_sample.size() << " level-wise long-range jobs."); | 
|---|
| [69c733] | 165 |  | 
|---|
| [1e5f84] | 166 | // check boundary conditions | 
|---|
|  | 167 | const BoundaryConditions::Conditions_t &conditions = | 
|---|
|  | 168 | World::getInstance().getDomain().getConditions(); | 
|---|
|  | 169 | const bool OpenBoundaryConditions = | 
|---|
|  | 170 | !((conditions[0] == BoundaryConditions::Wrap) && | 
|---|
|  | 171 | (conditions[1] == BoundaryConditions::Wrap) && | 
|---|
|  | 172 | (conditions[2] == BoundaryConditions::Wrap)); | 
|---|
|  | 173 | LOG(1, std::string("INFO: Using ") | 
|---|
|  | 174 | << (OpenBoundaryConditions ? "open" : "periodic") | 
|---|
|  | 175 | << " boundary conditions."); | 
|---|
|  | 176 |  | 
|---|
| [4f056e] | 177 | // Phase Four: obtain more ids | 
|---|
| [a3427f] | 178 | std::map<JobId_t, VMGData> longrangedata; | 
|---|
| [4f056e] | 179 | { | 
|---|
|  | 180 | VMGFragmentController vmgcontroller(io_service); | 
|---|
|  | 181 | vmgcontroller.setHost(params.host.get()); | 
|---|
|  | 182 | vmgcontroller.setPort(params.port.get()); | 
|---|
| [a3427f] | 183 | const size_t NoJobs = shortrangedata.size()+full_sample.size(); | 
|---|
| [e2925fd] | 184 | vmgcontroller.requestIds(2*NoJobs); | 
|---|
| [4f056e] | 185 |  | 
|---|
| [e2925fd] | 186 | // Phase Five a: create VMGJobs for electronic charge distribution | 
|---|
| [4f056e] | 187 | const size_t near_field_cells = params.near_field_cells.get(); | 
|---|
| [cd2591] | 188 | const size_t interpolation_degree = params.interpolation_degree.get(); | 
|---|
| [4f056e] | 189 | if (!vmgcontroller.createLongRangeJobs( | 
|---|
| [a3427f] | 190 | shortrangedata, | 
|---|
| [4f056e] | 191 | full_sample, | 
|---|
| [cd2591] | 192 | near_field_cells, | 
|---|
| [6ff62c] | 193 | interpolation_degree, | 
|---|
| [e2925fd] | 194 | VMGFragmentController::DontSampleParticles, | 
|---|
|  | 195 | VMGFragmentController::DoTreatGrid, | 
|---|
| [b6b21a] | 196 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly, | 
|---|
| [1e5f84] | 197 | params.DoPrintDebug.get(), | 
|---|
|  | 198 | OpenBoundaryConditions)) | 
|---|
| [4f056e] | 199 | return Action::failure; | 
|---|
|  | 200 |  | 
|---|
| [e2925fd] | 201 | // Phase Six a: calculate result | 
|---|
| [4f056e] | 202 | vmgcontroller.waitforResults(NoJobs); | 
|---|
| [a3427f] | 203 | vmgcontroller.getResults(longrangedata); | 
|---|
|  | 204 | ASSERT( NoJobs == longrangedata.size(), | 
|---|
| [4f056e] | 205 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+" | 
|---|
|  | 206 | +toString(full_sample.size())+"="+toString(NoJobs) | 
|---|
| [e2925fd] | 207 | +" and first VMGresults "+toString(longrangedata.size())+" don't match."); | 
|---|
| [4f056e] | 208 | Exitflag += vmgcontroller.getExitflag(); | 
|---|
| [e2925fd] | 209 |  | 
|---|
|  | 210 | { | 
|---|
|  | 211 | std::map<JobId_t, VMGData> longrangedata_both; | 
|---|
|  | 212 | // Phase Five b: create VMGJobs for nuclei charge distributions | 
|---|
|  | 213 | const size_t near_field_cells = params.near_field_cells.get(); | 
|---|
|  | 214 | const size_t interpolation_degree = params.interpolation_degree.get(); | 
|---|
|  | 215 | if (!vmgcontroller.createLongRangeJobs( | 
|---|
|  | 216 | shortrangedata, | 
|---|
|  | 217 | full_sample, | 
|---|
|  | 218 | near_field_cells, | 
|---|
|  | 219 | interpolation_degree, | 
|---|
|  | 220 | VMGFragmentController::DoSampleParticles, | 
|---|
|  | 221 | VMGFragmentController::DoTreatGrid, | 
|---|
|  | 222 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly, | 
|---|
| [1e5f84] | 223 | params.DoPrintDebug.get(), | 
|---|
|  | 224 | OpenBoundaryConditions)) | 
|---|
| [e2925fd] | 225 | return Action::failure; | 
|---|
|  | 226 |  | 
|---|
|  | 227 | // Phase Six b: calculate result | 
|---|
|  | 228 | vmgcontroller.waitforResults(NoJobs); | 
|---|
|  | 229 | vmgcontroller.getResults(longrangedata_both); | 
|---|
|  | 230 | ASSERT( NoJobs == longrangedata_both.size(), | 
|---|
|  | 231 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+" | 
|---|
|  | 232 | +toString(full_sample.size())+"="+toString(NoJobs) | 
|---|
|  | 233 | +" and second VMGresults "+toString(longrangedata_both.size())+" don't match."); | 
|---|
|  | 234 | Exitflag += vmgcontroller.getExitflag(); | 
|---|
|  | 235 |  | 
|---|
|  | 236 | // go through either data and replace nuclei_long with contribution from both | 
|---|
|  | 237 | ASSERT( longrangedata.size() == longrangedata_both.size(), | 
|---|
|  | 238 | "FragmentationFragmentationAutomationAction::performCall() - longrange results have different sizes."); | 
|---|
|  | 239 | std::map<JobId_t, VMGData>::iterator destiter = longrangedata.begin(); | 
|---|
| [e355b31] | 240 | std::map<JobId_t, VMGData>::const_iterator srciter = longrangedata_both.begin(); | 
|---|
|  | 241 | for (;destiter != longrangedata.end(); ++srciter, ++destiter) { | 
|---|
|  | 242 | destiter->second.both_sampled_potential = srciter->second.sampled_potential; | 
|---|
| [e2925fd] | 243 | destiter->second.nuclei_long = srciter->second.nuclei_long; | 
|---|
| [e355b31] | 244 | } | 
|---|
| [e2925fd] | 245 | } | 
|---|
| [4f056e] | 246 | } | 
|---|
| [a2295a] | 247 |  | 
|---|
| [b6b21a] | 248 | if (params.DoPrintDebug.get()) { | 
|---|
|  | 249 | std::map<JobId_t, std::string> debugData; | 
|---|
|  | 250 | { | 
|---|
|  | 251 | if (!full_sample.empty()) { | 
|---|
|  | 252 | // create debug jobs for each level to print the summed-up potential to vtk files | 
|---|
|  | 253 | VMGDebugGridFragmentController debugcontroller(io_service); | 
|---|
|  | 254 | debugcontroller.setHost(params.host.get()); | 
|---|
|  | 255 | debugcontroller.setPort(params.port.get()); | 
|---|
|  | 256 | debugcontroller.requestIds(full_sample.size()); | 
|---|
| [1e5f84] | 257 | if (!debugcontroller.createDebugJobs(full_sample, OpenBoundaryConditions)) | 
|---|
| [b6b21a] | 258 | return Action::failure; | 
|---|
|  | 259 | debugcontroller.waitforResults(full_sample.size()); | 
|---|
|  | 260 | debugcontroller.getResults(debugData); | 
|---|
|  | 261 | Exitflag += debugcontroller.getExitflag(); | 
|---|
|  | 262 | } | 
|---|
| [a2295a] | 263 | } | 
|---|
| [2764e0] | 264 | } | 
|---|
| [a3427f] | 265 | container.addFullResults(keysets, forcekeysets, shortrangedata, longrangedata); | 
|---|
|  | 266 | } else { | 
|---|
|  | 267 | container.addShortRangeResults(keysets, forcekeysets, shortrangedata); | 
|---|
| [69c733] | 268 | } | 
|---|
| [a3427f] | 269 | #else | 
|---|
|  | 270 | container.addShortRangeResults(keysets, forcekeysets, shortrangedata); | 
|---|
| [69c733] | 271 | #endif | 
|---|
| [edecba] | 272 |  | 
|---|
| [bae7bc] | 273 | // now clear all present jobs as we are done | 
|---|
|  | 274 | FragmentJobQueue::getInstance().clear(); | 
|---|
|  | 275 |  | 
|---|
| [55e1bc] | 276 | // if file is given, advise results container to store to file | 
|---|
|  | 277 | if (!params.resultsfile.get().empty()) { | 
|---|
|  | 278 | boost::filesystem::path resultsfile = params.resultsfile.get(); | 
|---|
|  | 279 | std::ofstream returnstream(resultsfile.string().c_str()); | 
|---|
|  | 280 | if (returnstream.good()) { | 
|---|
|  | 281 | boost::archive::text_oarchive oa(returnstream); | 
|---|
|  | 282 | oa << container; | 
|---|
|  | 283 | } | 
|---|
|  | 284 | Exitflag += (int)(!returnstream.good()); | 
|---|
|  | 285 | returnstream.close(); | 
|---|
|  | 286 | } | 
|---|
|  | 287 |  | 
|---|
| [edecba] | 288 | return (Exitflag == 0) ? Action::success : Action::failure; | 
|---|
|  | 289 | } | 
|---|
|  | 290 |  | 
|---|
| [b5b01e] | 291 | ActionState::ptr FragmentationFragmentationAutomationAction::performUndo(ActionState::ptr _state) { | 
|---|
| [edecba] | 292 | return Action::success; | 
|---|
|  | 293 | } | 
|---|
|  | 294 |  | 
|---|
| [b5b01e] | 295 | ActionState::ptr FragmentationFragmentationAutomationAction::performRedo(ActionState::ptr _state){ | 
|---|
| [edecba] | 296 | return Action::success; | 
|---|
|  | 297 | } | 
|---|
|  | 298 |  | 
|---|
|  | 299 | bool FragmentationFragmentationAutomationAction::canUndo() { | 
|---|
|  | 300 | return false; | 
|---|
|  | 301 | } | 
|---|
|  | 302 |  | 
|---|
|  | 303 | bool FragmentationFragmentationAutomationAction::shouldUndo() { | 
|---|
|  | 304 | return false; | 
|---|
|  | 305 | } | 
|---|
|  | 306 | /** =========== end of function ====================== */ | 
|---|