[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"
|
---|
[1dfe00] | 55 |
|
---|
| 56 | #ifdef HAVE_JOBMARKET
|
---|
[edecba] | 57 | #include "JobMarket/Jobs/FragmentJob.hpp"
|
---|
[1dfe00] | 58 | #else
|
---|
| 59 | #include "Jobs/JobMarket/FragmentJob.hpp"
|
---|
| 60 | #endif
|
---|
[edecba] | 61 |
|
---|
[ac9ca4] | 62 | #include "Fragmentation/Automation/FragmentJobQueue.hpp"
|
---|
[1dfe00] | 63 | #ifdef HAVE_JOBMARKET
|
---|
[ffe057] | 64 | #include "Fragmentation/Automation/MPQCFragmentController.hpp"
|
---|
[1dfe00] | 65 | #else
|
---|
[bf0ff3] | 66 | #include "Fragmentation/Automation/MPQCCommandFragmentController.hpp"
|
---|
[1dfe00] | 67 | #endif
|
---|
[fbf143] | 68 | #include "Fragmentation/Summation/Containers/FragmentationChargeDensity.hpp"
|
---|
| 69 | #include "Fragmentation/Summation/Containers/FragmentationLongRangeResults.hpp"
|
---|
[a3427f] | 70 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
|
---|
[fbf143] | 71 | #include "Fragmentation/Summation/Containers/FragmentationShortRangeResults.hpp"
|
---|
| 72 | #include "Fragmentation/Summation/Containers/MPQCData.hpp"
|
---|
[a3427f] | 73 | #include "Fragmentation/KeySetsContainer.hpp"
|
---|
[18ed8c] | 74 | #if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
|
---|
[a3427f] | 75 | #include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp"
|
---|
| 76 | #include "Fragmentation/Automation/VMGFragmentController.hpp"
|
---|
[fbf143] | 77 | #include "Fragmentation/Summation/Containers/VMGData.hpp"
|
---|
| 78 | #include "Fragmentation/Summation/Containers/VMGDataFused.hpp"
|
---|
| 79 | #include "Fragmentation/Summation/Containers/VMGDataMap.hpp"
|
---|
| 80 | #include "Fragmentation/Summation/Containers/VMGData_printKeyNames.hpp"
|
---|
[69c733] | 81 | #endif
|
---|
[edecba] | 82 | #include "World.hpp"
|
---|
| 83 |
|
---|
[c09f94] | 84 | #include <boost/bind.hpp>
|
---|
| 85 | #include <boost/thread.hpp>
|
---|
[86cfac5] | 86 | #include <fstream>
|
---|
[edecba] | 87 | #include <iostream>
|
---|
| 88 | #include <string>
|
---|
| 89 | #include <vector>
|
---|
| 90 |
|
---|
[8cae4c] | 91 | #include <boost/mpl/for_each.hpp>
|
---|
| 92 |
|
---|
[edecba] | 93 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
|
---|
| 94 |
|
---|
| 95 | using namespace MoleCuilder;
|
---|
| 96 |
|
---|
| 97 | // and construct the stuff
|
---|
| 98 | #include "FragmentationAutomationAction.def"
|
---|
| 99 | #include "Action_impl_pre.hpp"
|
---|
| 100 | /** =========== define the function ====================== */
|
---|
| 101 |
|
---|
| 102 | class controller_AddOn;
|
---|
| 103 |
|
---|
| 104 | // needs to be defined for using the FragmentController
|
---|
| 105 | controller_AddOn *getAddOn()
|
---|
| 106 | {
|
---|
| 107 | return NULL;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[c09f94] | 110 | static void updateSteps(Process &p, const size_t step, const size_t total)
|
---|
| 111 | {
|
---|
| 112 | LOG(1, "There are " << step << " steps out of " << total << " done.");
|
---|
| 113 | p.setCurrStep(step);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[b5b01e] | 116 | ActionState::ptr FragmentationFragmentationAutomationAction::performCall() {
|
---|
[c5324f] | 117 | boost::asio::io_service io_service;
|
---|
| 118 |
|
---|
| 119 | // TODO: Have io_service run in second thread and merge with current again eventually
|
---|
| 120 |
|
---|
[a3427f] | 121 | FragmentationResultContainer &container =
|
---|
| 122 | FragmentationResultContainer::getInstance();
|
---|
| 123 | const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets();
|
---|
| 124 | const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets();
|
---|
| 125 |
|
---|
[4f056e] | 126 | size_t Exitflag = 0;
|
---|
[a3427f] | 127 | std::map<JobId_t, MPQCData> shortrangedata;
|
---|
[1dfe00] | 128 | #ifdef HAVE_JOBMARKET
|
---|
[4f056e] | 129 | {
|
---|
[ac9ca4] | 130 | const size_t NumberJobs = FragmentJobQueue::getInstance().size();
|
---|
[4f056e] | 131 | MPQCFragmentController mpqccontroller(io_service);
|
---|
| 132 | mpqccontroller.setHost(params.host.get());
|
---|
| 133 | mpqccontroller.setPort(params.port.get());
|
---|
| 134 | // Phase One: obtain ids
|
---|
[ac9ca4] | 135 | mpqccontroller.requestIds(NumberJobs);
|
---|
[4f056e] | 136 |
|
---|
[ac9ca4] | 137 | // Phase Two: add MPQCJobs and send
|
---|
[a61dbb] | 138 | const bool AddJobsStatus = mpqccontroller.addJobsFromQueue(
|
---|
[6ff62c] | 139 | params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
|
---|
| 140 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly
|
---|
| 141 | );
|
---|
[a61dbb] | 142 | if (AddJobsStatus)
|
---|
| 143 | LOG(1, "INFO: Added jobs from FragmentJobsQueue.");
|
---|
| 144 | else {
|
---|
| 145 | ELOG(1, "Adding jobs failed.");
|
---|
| 146 | return Action::failure;
|
---|
| 147 | }
|
---|
[ac9ca4] | 148 | mpqccontroller.run();
|
---|
[4f056e] | 149 |
|
---|
| 150 | // Phase Three: calculate result
|
---|
[c09f94] | 151 | setMaxSteps(NumberJobs);
|
---|
| 152 | mpqccontroller.setUpdateHandler(
|
---|
| 153 | boost::bind(&updateSteps, boost::ref(*this), _1, _2)
|
---|
| 154 | );
|
---|
| 155 | start();
|
---|
| 156 | boost::thread wait_thread(
|
---|
| 157 | boost::bind(&MPQCFragmentController::waitforResults, boost::ref(mpqccontroller), boost::cref(NumberJobs))
|
---|
| 158 | );
|
---|
| 159 | wait_thread.join();
|
---|
| 160 | stop();
|
---|
[a3427f] | 161 | mpqccontroller.getResults(shortrangedata);
|
---|
[4f056e] | 162 |
|
---|
| 163 | Exitflag += mpqccontroller.getExitflag();
|
---|
| 164 | }
|
---|
[bf0ff3] | 165 | #else
|
---|
| 166 | {
|
---|
| 167 | const size_t NumberJobs = FragmentJobQueue::getInstance().size();
|
---|
| 168 | MPQCCommandFragmentController mpqccontroller;
|
---|
| 169 | // Phase One: obtain ids: not needed, we have infinite pool
|
---|
| 170 |
|
---|
| 171 | // Phase Two: add MPQCJobs and send
|
---|
| 172 | const size_t NoJobs = mpqccontroller.addJobsFromQueue(
|
---|
| 173 | params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
|
---|
[d6a7b9] | 174 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
|
---|
| 175 | params.executable.get().string()
|
---|
[bf0ff3] | 176 | );
|
---|
[07ecc5] | 177 | if (NoJobs != NumberJobs) {
|
---|
| 178 | ELOG(1, "Not all jobs were added succesfully.");
|
---|
| 179 | return Action::failure;
|
---|
| 180 | }
|
---|
[bf0ff3] | 181 | LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue.");
|
---|
[45cdce6] | 182 |
|
---|
| 183 | // prepare process
|
---|
| 184 | setMaxSteps(NumberJobs);
|
---|
| 185 | mpqccontroller.setUpdateHandler(
|
---|
| 186 | boost::bind(&updateSteps, boost::ref(*this), _1, _2)
|
---|
| 187 | );
|
---|
| 188 | start();
|
---|
[bf0ff3] | 189 | mpqccontroller.run();
|
---|
[45cdce6] | 190 | stop();
|
---|
[bf0ff3] | 191 |
|
---|
| 192 | // get back the results and place them in shortrangedata
|
---|
| 193 | mpqccontroller.getResults(shortrangedata);
|
---|
| 194 | ASSERT( shortrangedata.size() == NumberJobs,
|
---|
| 195 | "FragmentationFragmentationAutomationAction::performCall() - number of converted results "
|
---|
| 196 | +toString(shortrangedata.size())+" and number of jobs "+toString(NumberJobs)+ " differ.");
|
---|
| 197 |
|
---|
[4f056e] | 198 | Exitflag += mpqccontroller.getExitflag();
|
---|
| 199 | }
|
---|
[1dfe00] | 200 | #endif
|
---|
[c5324f] | 201 |
|
---|
[18ed8c] | 202 | #if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
|
---|
[69c733] | 203 | if (params.DoLongrange.get()) {
|
---|
[db6f7d] | 204 | if ( World::getInstance().getAllAtoms().size() == 0) {
|
---|
[26b4d62] | 205 | STATUS("Please load the full molecule into the world before starting this action.");
|
---|
[db6f7d] | 206 | return Action::failure;
|
---|
| 207 | }
|
---|
[06865e] | 208 |
|
---|
[849cd8] | 209 | // obtain combined charge density
|
---|
[27594e] | 210 | FragmentationChargeDensity summedChargeDensity(
|
---|
[a3427f] | 211 | shortrangedata,
|
---|
[bae7bc] | 212 | FragmentJobQueue::getInstance().getKeySets());
|
---|
[27594e] | 213 | const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
|
---|
[a3427f] | 214 | LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and "
|
---|
[4f056e] | 215 | << full_sample.size() << " level-wise long-range jobs.");
|
---|
[69c733] | 216 |
|
---|
[1e5f84] | 217 | // check boundary conditions
|
---|
| 218 | const BoundaryConditions::Conditions_t &conditions =
|
---|
| 219 | World::getInstance().getDomain().getConditions();
|
---|
| 220 | const bool OpenBoundaryConditions =
|
---|
| 221 | !((conditions[0] == BoundaryConditions::Wrap) &&
|
---|
| 222 | (conditions[1] == BoundaryConditions::Wrap) &&
|
---|
| 223 | (conditions[2] == BoundaryConditions::Wrap));
|
---|
| 224 | LOG(1, std::string("INFO: Using ")
|
---|
| 225 | << (OpenBoundaryConditions ? "open" : "periodic")
|
---|
| 226 | << " boundary conditions.");
|
---|
| 227 |
|
---|
[4f056e] | 228 | // Phase Four: obtain more ids
|
---|
[a3427f] | 229 | std::map<JobId_t, VMGData> longrangedata;
|
---|
[4f056e] | 230 | {
|
---|
| 231 | VMGFragmentController vmgcontroller(io_service);
|
---|
| 232 | vmgcontroller.setHost(params.host.get());
|
---|
| 233 | vmgcontroller.setPort(params.port.get());
|
---|
[a3427f] | 234 | const size_t NoJobs = shortrangedata.size()+full_sample.size();
|
---|
[e2925fd] | 235 | vmgcontroller.requestIds(2*NoJobs);
|
---|
[4f056e] | 236 |
|
---|
[e2925fd] | 237 | // Phase Five a: create VMGJobs for electronic charge distribution
|
---|
[4f056e] | 238 | const size_t near_field_cells = params.near_field_cells.get();
|
---|
[cd2591] | 239 | const size_t interpolation_degree = params.interpolation_degree.get();
|
---|
[4f056e] | 240 | if (!vmgcontroller.createLongRangeJobs(
|
---|
[a3427f] | 241 | shortrangedata,
|
---|
[4f056e] | 242 | full_sample,
|
---|
[cd2591] | 243 | near_field_cells,
|
---|
[6ff62c] | 244 | interpolation_degree,
|
---|
[e2925fd] | 245 | VMGFragmentController::DontSampleParticles,
|
---|
| 246 | VMGFragmentController::DoTreatGrid,
|
---|
[b6b21a] | 247 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
|
---|
[1e5f84] | 248 | params.DoPrintDebug.get(),
|
---|
[26b4d62] | 249 | OpenBoundaryConditions)) {
|
---|
| 250 | STATUS("Could not create long-range jobs for electronic charge distribution.");
|
---|
[4f056e] | 251 | return Action::failure;
|
---|
[26b4d62] | 252 | }
|
---|
[4f056e] | 253 |
|
---|
[e2925fd] | 254 | // Phase Six a: calculate result
|
---|
[4f056e] | 255 | vmgcontroller.waitforResults(NoJobs);
|
---|
[a3427f] | 256 | vmgcontroller.getResults(longrangedata);
|
---|
| 257 | ASSERT( NoJobs == longrangedata.size(),
|
---|
[4f056e] | 258 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
|
---|
| 259 | +toString(full_sample.size())+"="+toString(NoJobs)
|
---|
[e2925fd] | 260 | +" and first VMGresults "+toString(longrangedata.size())+" don't match.");
|
---|
[4f056e] | 261 | Exitflag += vmgcontroller.getExitflag();
|
---|
[e2925fd] | 262 |
|
---|
| 263 | {
|
---|
| 264 | std::map<JobId_t, VMGData> longrangedata_both;
|
---|
| 265 | // Phase Five b: create VMGJobs for nuclei charge distributions
|
---|
| 266 | const size_t near_field_cells = params.near_field_cells.get();
|
---|
| 267 | const size_t interpolation_degree = params.interpolation_degree.get();
|
---|
| 268 | if (!vmgcontroller.createLongRangeJobs(
|
---|
| 269 | shortrangedata,
|
---|
| 270 | full_sample,
|
---|
| 271 | near_field_cells,
|
---|
| 272 | interpolation_degree,
|
---|
| 273 | VMGFragmentController::DoSampleParticles,
|
---|
| 274 | VMGFragmentController::DoTreatGrid,
|
---|
| 275 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
|
---|
[1e5f84] | 276 | params.DoPrintDebug.get(),
|
---|
[26b4d62] | 277 | OpenBoundaryConditions)) {
|
---|
| 278 | STATUS("Could not create long-range jobs for nuclei charge distribution.");
|
---|
[e2925fd] | 279 | return Action::failure;
|
---|
[26b4d62] | 280 | }
|
---|
[e2925fd] | 281 |
|
---|
| 282 | // Phase Six b: calculate result
|
---|
| 283 | vmgcontroller.waitforResults(NoJobs);
|
---|
| 284 | vmgcontroller.getResults(longrangedata_both);
|
---|
| 285 | ASSERT( NoJobs == longrangedata_both.size(),
|
---|
| 286 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
|
---|
| 287 | +toString(full_sample.size())+"="+toString(NoJobs)
|
---|
| 288 | +" and second VMGresults "+toString(longrangedata_both.size())+" don't match.");
|
---|
| 289 | Exitflag += vmgcontroller.getExitflag();
|
---|
| 290 |
|
---|
| 291 | // go through either data and replace nuclei_long with contribution from both
|
---|
| 292 | ASSERT( longrangedata.size() == longrangedata_both.size(),
|
---|
| 293 | "FragmentationFragmentationAutomationAction::performCall() - longrange results have different sizes.");
|
---|
| 294 | std::map<JobId_t, VMGData>::iterator destiter = longrangedata.begin();
|
---|
[e355b31] | 295 | std::map<JobId_t, VMGData>::const_iterator srciter = longrangedata_both.begin();
|
---|
| 296 | for (;destiter != longrangedata.end(); ++srciter, ++destiter) {
|
---|
| 297 | destiter->second.both_sampled_potential = srciter->second.sampled_potential;
|
---|
[e2925fd] | 298 | destiter->second.nuclei_long = srciter->second.nuclei_long;
|
---|
[e355b31] | 299 | }
|
---|
[e2925fd] | 300 | }
|
---|
[4f056e] | 301 | }
|
---|
[a2295a] | 302 |
|
---|
[b6b21a] | 303 | if (params.DoPrintDebug.get()) {
|
---|
| 304 | std::map<JobId_t, std::string> debugData;
|
---|
| 305 | {
|
---|
| 306 | if (!full_sample.empty()) {
|
---|
| 307 | // create debug jobs for each level to print the summed-up potential to vtk files
|
---|
| 308 | VMGDebugGridFragmentController debugcontroller(io_service);
|
---|
| 309 | debugcontroller.setHost(params.host.get());
|
---|
| 310 | debugcontroller.setPort(params.port.get());
|
---|
| 311 | debugcontroller.requestIds(full_sample.size());
|
---|
[26b4d62] | 312 | if (!debugcontroller.createDebugJobs(full_sample, OpenBoundaryConditions)) {
|
---|
| 313 | STATUS("Could not create debug jobs.");
|
---|
[b6b21a] | 314 | return Action::failure;
|
---|
[26b4d62] | 315 | }
|
---|
[b6b21a] | 316 | debugcontroller.waitforResults(full_sample.size());
|
---|
| 317 | debugcontroller.getResults(debugData);
|
---|
| 318 | Exitflag += debugcontroller.getExitflag();
|
---|
| 319 | }
|
---|
[a2295a] | 320 | }
|
---|
[2764e0] | 321 | }
|
---|
[a3427f] | 322 | container.addFullResults(keysets, forcekeysets, shortrangedata, longrangedata);
|
---|
| 323 | } else {
|
---|
| 324 | container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
|
---|
[69c733] | 325 | }
|
---|
[a3427f] | 326 | #else
|
---|
| 327 | container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
|
---|
[69c733] | 328 | #endif
|
---|
[edecba] | 329 |
|
---|
[bae7bc] | 330 | // now clear all present jobs as we are done
|
---|
| 331 | FragmentJobQueue::getInstance().clear();
|
---|
| 332 |
|
---|
[55e1bc] | 333 | // if file is given, advise results container to store to file
|
---|
| 334 | if (!params.resultsfile.get().empty()) {
|
---|
| 335 | boost::filesystem::path resultsfile = params.resultsfile.get();
|
---|
| 336 | std::ofstream returnstream(resultsfile.string().c_str());
|
---|
| 337 | if (returnstream.good()) {
|
---|
| 338 | boost::archive::text_oarchive oa(returnstream);
|
---|
| 339 | oa << container;
|
---|
| 340 | }
|
---|
| 341 | Exitflag += (int)(!returnstream.good());
|
---|
| 342 | returnstream.close();
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[26b4d62] | 345 | if (Exitflag != 0)
|
---|
| 346 | STATUS("Controller has returned failure.");
|
---|
[edecba] | 347 | return (Exitflag == 0) ? Action::success : Action::failure;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[b5b01e] | 350 | ActionState::ptr FragmentationFragmentationAutomationAction::performUndo(ActionState::ptr _state) {
|
---|
[edecba] | 351 | return Action::success;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[b5b01e] | 354 | ActionState::ptr FragmentationFragmentationAutomationAction::performRedo(ActionState::ptr _state){
|
---|
[edecba] | 355 | return Action::success;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | bool FragmentationFragmentationAutomationAction::canUndo() {
|
---|
| 359 | return false;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
|
---|
| 363 | return false;
|
---|
| 364 | }
|
---|
| 365 | /** =========== end of function ====================== */
|
---|