| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 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 | 
 | 
|---|
| 41 | //// include headers that implement a archive in simple text format
 | 
|---|
| 42 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
| 43 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
| 44 | 
 | 
|---|
| 45 | //
 | 
|---|
| 46 | //#include <boost/mpl/remove.hpp>
 | 
|---|
| 47 | //#include <boost/lambda/lambda.hpp>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | //#include <iostream>
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 52 | #include "CodePatterns/Info.hpp"
 | 
|---|
| 53 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 54 | #include "JobMarket/Jobs/FragmentJob.hpp"
 | 
|---|
| 55 | 
 | 
|---|
| 56 | #include "Fragmentation/Automation/FragmentJobQueue.hpp"
 | 
|---|
| 57 | #include "Fragmentation/Automation/MPQCFragmentController.hpp"
 | 
|---|
| 58 | #include "Fragmentation/Summation/Containers/FragmentationChargeDensity.hpp"
 | 
|---|
| 59 | #include "Fragmentation/Summation/Containers/FragmentationLongRangeResults.hpp"
 | 
|---|
| 60 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
 | 
|---|
| 61 | #include "Fragmentation/Summation/Containers/FragmentationShortRangeResults.hpp"
 | 
|---|
| 62 | #include "Fragmentation/Summation/Containers/MPQCData.hpp"
 | 
|---|
| 63 | #include "Fragmentation/KeySetsContainer.hpp"
 | 
|---|
| 64 | #ifdef HAVE_VMG
 | 
|---|
| 65 | #include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp"
 | 
|---|
| 66 | #include "Fragmentation/Automation/VMGFragmentController.hpp"
 | 
|---|
| 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"
 | 
|---|
| 71 | #endif
 | 
|---|
| 72 | #include "World.hpp"
 | 
|---|
| 73 | 
 | 
|---|
| 74 | #include <fstream>
 | 
|---|
| 75 | #include <iostream>
 | 
|---|
| 76 | #include <string>
 | 
|---|
| 77 | #include <vector>
 | 
|---|
| 78 | 
 | 
|---|
| 79 | #include <boost/mpl/for_each.hpp>
 | 
|---|
| 80 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 103 |   FragmentationResultContainer &container =
 | 
|---|
| 104 |       FragmentationResultContainer::getInstance();
 | 
|---|
| 105 |   const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets();
 | 
|---|
| 106 |   const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets();
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   size_t Exitflag = 0;
 | 
|---|
| 109 |   std::map<JobId_t, MPQCData> shortrangedata;
 | 
|---|
| 110 |   {
 | 
|---|
| 111 |     const size_t NumberJobs = FragmentJobQueue::getInstance().size();
 | 
|---|
| 112 |     MPQCFragmentController mpqccontroller(io_service);
 | 
|---|
| 113 |     mpqccontroller.setHost(params.host.get());
 | 
|---|
| 114 |     mpqccontroller.setPort(params.port.get());
 | 
|---|
| 115 |     // Phase One: obtain ids
 | 
|---|
| 116 |     mpqccontroller.requestIds(NumberJobs);
 | 
|---|
| 117 | 
 | 
|---|
| 118 |     // Phase Two: add MPQCJobs and send
 | 
|---|
| 119 |     const size_t NoJobs = mpqccontroller.addJobsFromQueue(
 | 
|---|
| 120 |         params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity);
 | 
|---|
| 121 |     LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue.");
 | 
|---|
| 122 |     mpqccontroller.run();
 | 
|---|
| 123 | 
 | 
|---|
| 124 |     // Phase Three: calculate result
 | 
|---|
| 125 |     mpqccontroller.waitforResults(NumberJobs);
 | 
|---|
| 126 |     mpqccontroller.getResults(shortrangedata);
 | 
|---|
| 127 | 
 | 
|---|
| 128 |     Exitflag += mpqccontroller.getExitflag();
 | 
|---|
| 129 |   }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | #ifdef HAVE_VMG
 | 
|---|
| 132 |   if (params.DoLongrange.get()) {
 | 
|---|
| 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 |   }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   // obtain combined charge density
 | 
|---|
| 139 |   FragmentationChargeDensity summedChargeDensity(
 | 
|---|
| 140 |       shortrangedata,
 | 
|---|
| 141 |       FragmentJobQueue::getInstance().getKeySets());
 | 
|---|
| 142 |   const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
 | 
|---|
| 143 |   LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and "
 | 
|---|
| 144 |       << full_sample.size() << " level-wise long-range jobs.");
 | 
|---|
| 145 | 
 | 
|---|
| 146 |   // Phase Four: obtain more ids
 | 
|---|
| 147 |   std::map<JobId_t, VMGData> longrangedata;
 | 
|---|
| 148 |   {
 | 
|---|
| 149 |     VMGFragmentController vmgcontroller(io_service);
 | 
|---|
| 150 |     vmgcontroller.setHost(params.host.get());
 | 
|---|
| 151 |     vmgcontroller.setPort(params.port.get());
 | 
|---|
| 152 |     const size_t NoJobs = shortrangedata.size()+full_sample.size();
 | 
|---|
| 153 |     vmgcontroller.requestIds(NoJobs);
 | 
|---|
| 154 | 
 | 
|---|
| 155 |     // Phase Five: create VMGJobs
 | 
|---|
| 156 |     const size_t near_field_cells = params.near_field_cells.get();
 | 
|---|
| 157 |     const size_t interpolation_degree = params.interpolation_degree.get();
 | 
|---|
| 158 |     if (!vmgcontroller.createLongRangeJobs(
 | 
|---|
| 159 |         shortrangedata,
 | 
|---|
| 160 |         full_sample,
 | 
|---|
| 161 |         summedChargeDensity.getFragment(),
 | 
|---|
| 162 |         near_field_cells,
 | 
|---|
| 163 |         interpolation_degree))
 | 
|---|
| 164 |       return Action::failure;
 | 
|---|
| 165 | 
 | 
|---|
| 166 |     // Phase Six: calculate result
 | 
|---|
| 167 |     vmgcontroller.waitforResults(NoJobs);
 | 
|---|
| 168 |     vmgcontroller.getResults(longrangedata);
 | 
|---|
| 169 |     ASSERT( NoJobs == longrangedata.size(),
 | 
|---|
| 170 |         "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
 | 
|---|
| 171 |         +toString(full_sample.size())+"="+toString(NoJobs)
 | 
|---|
| 172 |         +" and VMGresults "+toString(longrangedata.size())+" don't match.");
 | 
|---|
| 173 |     Exitflag += vmgcontroller.getExitflag();
 | 
|---|
| 174 |   }
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   std::map<JobId_t, std::string> debugData;
 | 
|---|
| 177 |   {
 | 
|---|
| 178 |     if (!full_sample.empty()) {
 | 
|---|
| 179 |       // create debug jobs for each level to print the summed-up potential to vtk files
 | 
|---|
| 180 |       VMGDebugGridFragmentController debugcontroller(io_service);
 | 
|---|
| 181 |       debugcontroller.setHost(params.host.get());
 | 
|---|
| 182 |       debugcontroller.setPort(params.port.get());
 | 
|---|
| 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);
 | 
|---|
| 188 |       Exitflag += debugcontroller.getExitflag();
 | 
|---|
| 189 |     }
 | 
|---|
| 190 |   }
 | 
|---|
| 191 |   container.addFullResults(keysets, forcekeysets, shortrangedata, longrangedata);
 | 
|---|
| 192 |   } else {
 | 
|---|
| 193 |     container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
 | 
|---|
| 194 |   }
 | 
|---|
| 195 | #else
 | 
|---|
| 196 |   container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
 | 
|---|
| 197 | #endif
 | 
|---|
| 198 | 
 | 
|---|
| 199 |   // now clear all present jobs as we are done
 | 
|---|
| 200 |   FragmentJobQueue::getInstance().clear();
 | 
|---|
| 201 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 ====================== */
 | 
|---|