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