[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 |
|
---|
[86cfac5] | 75 | #include <fstream>
|
---|
[edecba] | 76 | #include <iostream>
|
---|
| 77 | #include <string>
|
---|
| 78 | #include <vector>
|
---|
| 79 |
|
---|
[8cae4c] | 80 | #include <boost/mpl/for_each.hpp>
|
---|
| 81 |
|
---|
[edecba] | 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 |
|
---|
[c5324f] | 99 | Action::state_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 |
|
---|
[a3427f] | 104 | FragmentationResultContainer &container =
|
---|
| 105 | FragmentationResultContainer::getInstance();
|
---|
| 106 | const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets();
|
---|
| 107 | const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets();
|
---|
| 108 |
|
---|
[4f056e] | 109 | size_t Exitflag = 0;
|
---|
[a3427f] | 110 | std::map<JobId_t, MPQCData> shortrangedata;
|
---|
[4f056e] | 111 | {
|
---|
[ac9ca4] | 112 | const size_t NumberJobs = FragmentJobQueue::getInstance().size();
|
---|
[4f056e] | 113 | MPQCFragmentController mpqccontroller(io_service);
|
---|
| 114 | mpqccontroller.setHost(params.host.get());
|
---|
| 115 | mpqccontroller.setPort(params.port.get());
|
---|
| 116 | // Phase One: obtain ids
|
---|
[ac9ca4] | 117 | mpqccontroller.requestIds(NumberJobs);
|
---|
[4f056e] | 118 |
|
---|
[ac9ca4] | 119 | // Phase Two: add MPQCJobs and send
|
---|
[503acc1] | 120 | const size_t NoJobs = mpqccontroller.addJobsFromQueue(
|
---|
[6ff62c] | 121 | params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
|
---|
| 122 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly
|
---|
| 123 | );
|
---|
[503acc1] | 124 | LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue.");
|
---|
[ac9ca4] | 125 | mpqccontroller.run();
|
---|
[4f056e] | 126 |
|
---|
| 127 | // Phase Three: calculate result
|
---|
[ac9ca4] | 128 | mpqccontroller.waitforResults(NumberJobs);
|
---|
[a3427f] | 129 | mpqccontroller.getResults(shortrangedata);
|
---|
[4f056e] | 130 |
|
---|
| 131 | Exitflag += mpqccontroller.getExitflag();
|
---|
| 132 | }
|
---|
[c5324f] | 133 |
|
---|
[69c733] | 134 | #ifdef HAVE_VMG
|
---|
| 135 | if (params.DoLongrange.get()) {
|
---|
[db6f7d] | 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 | }
|
---|
[06865e] | 140 |
|
---|
[849cd8] | 141 | // obtain combined charge density
|
---|
[27594e] | 142 | FragmentationChargeDensity summedChargeDensity(
|
---|
[a3427f] | 143 | shortrangedata,
|
---|
[bae7bc] | 144 | FragmentJobQueue::getInstance().getKeySets());
|
---|
[27594e] | 145 | const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
|
---|
[a3427f] | 146 | LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and "
|
---|
[4f056e] | 147 | << full_sample.size() << " level-wise long-range jobs.");
|
---|
[69c733] | 148 |
|
---|
[4f056e] | 149 | // Phase Four: obtain more ids
|
---|
[a3427f] | 150 | std::map<JobId_t, VMGData> longrangedata;
|
---|
[4f056e] | 151 | {
|
---|
| 152 | VMGFragmentController vmgcontroller(io_service);
|
---|
| 153 | vmgcontroller.setHost(params.host.get());
|
---|
| 154 | vmgcontroller.setPort(params.port.get());
|
---|
[a3427f] | 155 | const size_t NoJobs = shortrangedata.size()+full_sample.size();
|
---|
[e2925fd] | 156 | vmgcontroller.requestIds(2*NoJobs);
|
---|
[4f056e] | 157 |
|
---|
[e2925fd] | 158 | // Phase Five a: create VMGJobs for electronic charge distribution
|
---|
[4f056e] | 159 | const size_t near_field_cells = params.near_field_cells.get();
|
---|
[cd2591] | 160 | const size_t interpolation_degree = params.interpolation_degree.get();
|
---|
[4f056e] | 161 | if (!vmgcontroller.createLongRangeJobs(
|
---|
[a3427f] | 162 | shortrangedata,
|
---|
[4f056e] | 163 | full_sample,
|
---|
[cd2591] | 164 | near_field_cells,
|
---|
[6ff62c] | 165 | interpolation_degree,
|
---|
[e2925fd] | 166 | VMGFragmentController::DontSampleParticles,
|
---|
| 167 | VMGFragmentController::DoTreatGrid,
|
---|
[b6b21a] | 168 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
|
---|
| 169 | params.DoPrintDebug.get()))
|
---|
[4f056e] | 170 | return Action::failure;
|
---|
| 171 |
|
---|
[e2925fd] | 172 | // Phase Six a: calculate result
|
---|
[4f056e] | 173 | vmgcontroller.waitforResults(NoJobs);
|
---|
[a3427f] | 174 | vmgcontroller.getResults(longrangedata);
|
---|
| 175 | ASSERT( NoJobs == longrangedata.size(),
|
---|
[4f056e] | 176 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
|
---|
| 177 | +toString(full_sample.size())+"="+toString(NoJobs)
|
---|
[e2925fd] | 178 | +" and first VMGresults "+toString(longrangedata.size())+" don't match.");
|
---|
[4f056e] | 179 | Exitflag += vmgcontroller.getExitflag();
|
---|
[e2925fd] | 180 |
|
---|
| 181 | {
|
---|
| 182 | std::map<JobId_t, VMGData> longrangedata_both;
|
---|
| 183 | // Phase Five b: create VMGJobs for nuclei charge distributions
|
---|
| 184 | const size_t near_field_cells = params.near_field_cells.get();
|
---|
| 185 | const size_t interpolation_degree = params.interpolation_degree.get();
|
---|
| 186 | if (!vmgcontroller.createLongRangeJobs(
|
---|
| 187 | shortrangedata,
|
---|
| 188 | full_sample,
|
---|
| 189 | near_field_cells,
|
---|
| 190 | interpolation_degree,
|
---|
| 191 | VMGFragmentController::DoSampleParticles,
|
---|
| 192 | VMGFragmentController::DoTreatGrid,
|
---|
| 193 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
|
---|
| 194 | params.DoPrintDebug.get()))
|
---|
| 195 | return Action::failure;
|
---|
| 196 |
|
---|
| 197 | // Phase Six b: calculate result
|
---|
| 198 | vmgcontroller.waitforResults(NoJobs);
|
---|
| 199 | vmgcontroller.getResults(longrangedata_both);
|
---|
| 200 | ASSERT( NoJobs == longrangedata_both.size(),
|
---|
| 201 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
|
---|
| 202 | +toString(full_sample.size())+"="+toString(NoJobs)
|
---|
| 203 | +" and second VMGresults "+toString(longrangedata_both.size())+" don't match.");
|
---|
| 204 | Exitflag += vmgcontroller.getExitflag();
|
---|
| 205 |
|
---|
| 206 | // go through either data and replace nuclei_long with contribution from both
|
---|
| 207 | ASSERT( longrangedata.size() == longrangedata_both.size(),
|
---|
| 208 | "FragmentationFragmentationAutomationAction::performCall() - longrange results have different sizes.");
|
---|
| 209 | std::map<JobId_t, VMGData>::iterator destiter = longrangedata.begin();
|
---|
| 210 | std::map<JobId_t, VMGData>::iterator srciter = longrangedata_both.begin();
|
---|
| 211 | for (;destiter != longrangedata.end(); ++srciter, ++destiter)
|
---|
| 212 | destiter->second.nuclei_long = srciter->second.nuclei_long;
|
---|
| 213 | }
|
---|
[4f056e] | 214 | }
|
---|
[a2295a] | 215 |
|
---|
[b6b21a] | 216 | if (params.DoPrintDebug.get()) {
|
---|
| 217 | std::map<JobId_t, std::string> debugData;
|
---|
| 218 | {
|
---|
| 219 | if (!full_sample.empty()) {
|
---|
| 220 | // create debug jobs for each level to print the summed-up potential to vtk files
|
---|
| 221 | VMGDebugGridFragmentController debugcontroller(io_service);
|
---|
| 222 | debugcontroller.setHost(params.host.get());
|
---|
| 223 | debugcontroller.setPort(params.port.get());
|
---|
| 224 | debugcontroller.requestIds(full_sample.size());
|
---|
| 225 | if (!debugcontroller.createDebugJobs(full_sample))
|
---|
| 226 | return Action::failure;
|
---|
| 227 | debugcontroller.waitforResults(full_sample.size());
|
---|
| 228 | debugcontroller.getResults(debugData);
|
---|
| 229 | Exitflag += debugcontroller.getExitflag();
|
---|
| 230 | }
|
---|
[a2295a] | 231 | }
|
---|
[2764e0] | 232 | }
|
---|
[a3427f] | 233 | container.addFullResults(keysets, forcekeysets, shortrangedata, longrangedata);
|
---|
| 234 | } else {
|
---|
| 235 | container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
|
---|
[69c733] | 236 | }
|
---|
[a3427f] | 237 | #else
|
---|
| 238 | container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
|
---|
[69c733] | 239 | #endif
|
---|
[edecba] | 240 |
|
---|
[bae7bc] | 241 | // now clear all present jobs as we are done
|
---|
| 242 | FragmentJobQueue::getInstance().clear();
|
---|
| 243 |
|
---|
[55e1bc] | 244 | // if file is given, advise results container to store to file
|
---|
| 245 | if (!params.resultsfile.get().empty()) {
|
---|
| 246 | boost::filesystem::path resultsfile = params.resultsfile.get();
|
---|
| 247 | std::ofstream returnstream(resultsfile.string().c_str());
|
---|
| 248 | if (returnstream.good()) {
|
---|
| 249 | boost::archive::text_oarchive oa(returnstream);
|
---|
| 250 | oa << container;
|
---|
| 251 | }
|
---|
| 252 | Exitflag += (int)(!returnstream.good());
|
---|
| 253 | returnstream.close();
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[edecba] | 256 | return (Exitflag == 0) ? Action::success : Action::failure;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | Action::state_ptr FragmentationFragmentationAutomationAction::performUndo(Action::state_ptr _state) {
|
---|
| 260 | return Action::success;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | Action::state_ptr FragmentationFragmentationAutomationAction::performRedo(Action::state_ptr _state){
|
---|
| 264 | return Action::success;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | bool FragmentationFragmentationAutomationAction::canUndo() {
|
---|
| 268 | return false;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
|
---|
| 272 | return false;
|
---|
| 273 | }
|
---|
| 274 | /** =========== end of function ====================== */
|
---|