| [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 |  | 
|---|
| [9eb71b3] | 40 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| [edecba] | 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 | 
|---|
| [4a3df8] | 68 | #include "Fragmentation/Exporters/ExportGraph_ToJobs.hpp" | 
|---|
| [fbf143] | 69 | #include "Fragmentation/Summation/Containers/FragmentationChargeDensity.hpp" | 
|---|
|  | 70 | #include "Fragmentation/Summation/Containers/FragmentationLongRangeResults.hpp" | 
|---|
| [a3427f] | 71 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp" | 
|---|
| [fbf143] | 72 | #include "Fragmentation/Summation/Containers/FragmentationShortRangeResults.hpp" | 
|---|
|  | 73 | #include "Fragmentation/Summation/Containers/MPQCData.hpp" | 
|---|
| [a3427f] | 74 | #include "Fragmentation/KeySetsContainer.hpp" | 
|---|
| [18ed8c] | 75 | #if defined(HAVE_JOBMARKET) && defined(HAVE_VMG) | 
|---|
| [a3427f] | 76 | #include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp" | 
|---|
|  | 77 | #include "Fragmentation/Automation/VMGFragmentController.hpp" | 
|---|
| [11edf0] | 78 | #include "Fragmentation/Summation/Containers/GridDownsampler.hpp" | 
|---|
| [fbf143] | 79 | #include "Fragmentation/Summation/Containers/VMGData.hpp" | 
|---|
|  | 80 | #include "Fragmentation/Summation/Containers/VMGDataFused.hpp" | 
|---|
|  | 81 | #include "Fragmentation/Summation/Containers/VMGDataMap.hpp" | 
|---|
|  | 82 | #include "Fragmentation/Summation/Containers/VMGData_printKeyNames.hpp" | 
|---|
| [69c733] | 83 | #endif | 
|---|
| [edecba] | 84 | #include "World.hpp" | 
|---|
|  | 85 |  | 
|---|
| [c09f94] | 86 | #include <boost/bind.hpp> | 
|---|
|  | 87 | #include <boost/thread.hpp> | 
|---|
| [86cfac5] | 88 | #include <fstream> | 
|---|
| [edecba] | 89 | #include <iostream> | 
|---|
|  | 90 | #include <string> | 
|---|
|  | 91 | #include <vector> | 
|---|
|  | 92 |  | 
|---|
| [8cae4c] | 93 | #include <boost/mpl/for_each.hpp> | 
|---|
|  | 94 |  | 
|---|
| [edecba] | 95 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp" | 
|---|
|  | 96 |  | 
|---|
|  | 97 | using namespace MoleCuilder; | 
|---|
|  | 98 |  | 
|---|
|  | 99 | // and construct the stuff | 
|---|
|  | 100 | #include "FragmentationAutomationAction.def" | 
|---|
|  | 101 | #include "Action_impl_pre.hpp" | 
|---|
|  | 102 | /** =========== define the function ====================== */ | 
|---|
|  | 103 |  | 
|---|
|  | 104 | class controller_AddOn; | 
|---|
|  | 105 |  | 
|---|
|  | 106 | // needs to be defined for using the FragmentController | 
|---|
|  | 107 | controller_AddOn *getAddOn() | 
|---|
|  | 108 | { | 
|---|
|  | 109 | return NULL; | 
|---|
|  | 110 | } | 
|---|
|  | 111 |  | 
|---|
| [c09f94] | 112 | static void updateSteps(Process &p, const size_t step, const size_t total) | 
|---|
|  | 113 | { | 
|---|
|  | 114 | LOG(1, "There are " << step << " steps out of " << total << " done."); | 
|---|
|  | 115 | p.setCurrStep(step); | 
|---|
|  | 116 | } | 
|---|
|  | 117 |  | 
|---|
| [f0dea0] | 118 | template <class ResultClass> | 
|---|
|  | 119 | std::pair<double, double> getResiduals(const ResultClass &_result) | 
|---|
|  | 120 | { return std::pair<double, double>(0.,0.); } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | std::pair<double, double> getResiduals(const MPQCData &_result) | 
|---|
|  | 123 | { return std::make_pair(_result.accuracy, _result.desired_accuracy); } | 
|---|
|  | 124 |  | 
|---|
|  | 125 | std::pair<double, double> getResiduals(const VMGData &_result) | 
|---|
| [7debff] | 126 | { | 
|---|
|  | 127 | // sensibility check on absolute residual | 
|---|
|  | 128 | if (_result.residual > 1e-5) | 
|---|
|  | 129 | ELOG(2, "Encountered absolute residual greater than 1e-5: " << _result.residual); | 
|---|
|  | 130 | // take smaller value of the two as just one of them needs to be less than precision | 
|---|
|  | 131 | const double residual = std::min(_result.relative_residual, _result.residual); | 
|---|
|  | 132 | return std::make_pair(residual, _result.precision); | 
|---|
|  | 133 | } | 
|---|
| [f0dea0] | 134 |  | 
|---|
|  | 135 | template <class ResultClass> | 
|---|
|  | 136 | bool checkResults(const typename std::map<JobId_t, ResultClass> &_results) | 
|---|
|  | 137 | { | 
|---|
|  | 138 | bool result = true; | 
|---|
|  | 139 | for (typename std::map<JobId_t, ResultClass>::const_iterator iter = _results.begin(); | 
|---|
|  | 140 | iter != _results.end(); ++iter) { | 
|---|
|  | 141 | std::pair<double, double> residuals = getResiduals(iter->second); | 
|---|
|  | 142 | if (residuals.second != 0.) { | 
|---|
|  | 143 | if (residuals.first >= residuals.second) { | 
|---|
|  | 144 | ELOG(1, "Fragment Job " << iter->first << " converged to " << residuals.first | 
|---|
|  | 145 | << " instead of " << residuals.second); | 
|---|
|  | 146 | result = false; | 
|---|
|  | 147 | } | 
|---|
|  | 148 | } else { | 
|---|
|  | 149 | LOG(4, "DEBUG: Not checking accuracy because desired precision not given."); | 
|---|
|  | 150 | } | 
|---|
|  | 151 | } | 
|---|
|  | 152 | return result; | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
| [82034f1] | 155 |  | 
|---|
|  | 156 | #ifdef HAVE_JOBMARKET | 
|---|
|  | 157 | /** This performs a direct sum calculation for the nuclei-nuclei interaction energy | 
|---|
|  | 158 | * and forces for each fragment contained in \a shortrangedata. | 
|---|
|  | 159 | * | 
|---|
|  | 160 | * Results are (re)placed in \a _longrangedata. | 
|---|
|  | 161 | * | 
|---|
|  | 162 | * \warning This is only valid for fully open boundary conditions. | 
|---|
|  | 163 | * | 
|---|
|  | 164 | * \param _shortrangedata containing information on position and charges per fragment | 
|---|
|  | 165 | * \param _longrangedata containing energy and forces on return | 
|---|
|  | 166 | */ | 
|---|
| [25f5b66] | 167 | /* Is not used currently, commented out to disable warning message. | 
|---|
| [82034f1] | 168 | static void calculateNucleiNucleiLongRangeContribution( | 
|---|
|  | 169 | const std::map<JobId_t, MPQCData> &_shortrangedata, | 
|---|
|  | 170 | std::map<JobId_t, VMGData>& _longrangedata | 
|---|
|  | 171 | ) | 
|---|
|  | 172 | { | 
|---|
|  | 173 | ASSERT( _shortrangedata.size() == _longrangedata.size(), | 
|---|
|  | 174 | "calculateNucleiNucleiLongRangeContribution() - shortrange and longrange data have inequal size."); | 
|---|
|  | 175 | std::map<JobId_t, VMGData>::iterator longrangeiter = _longrangedata.begin(); | 
|---|
|  | 176 | std::map<JobId_t, MPQCData>::const_iterator iter = _shortrangedata.begin(); | 
|---|
|  | 177 | for (;iter != _shortrangedata.end(); ++iter, ++longrangeiter) { | 
|---|
|  | 178 | const MPQCData &data = iter->second; | 
|---|
|  | 179 | VMGData &longrange_data = longrangeiter->second; | 
|---|
|  | 180 | // set long-range contributions to zero | 
|---|
|  | 181 | longrange_data.nuclei_long = 0.; | 
|---|
|  | 182 | longrange_data.forces.clear(); | 
|---|
|  | 183 | longrange_data.forces.resize(data.positions.size(), FragmentForces::force_t(3,0.)); | 
|---|
|  | 184 | longrange_data.hasForces = true; | 
|---|
|  | 185 | // go through positions and evaluate sum naively | 
|---|
|  | 186 | ASSERT( data.positions.size() == data.charges.size(), | 
|---|
|  | 187 | "calculateNucleiNucleiLongRangeContribution() - positions and charges differ in size."); | 
|---|
|  | 188 | std::vector< std::vector<double> >::const_iterator positer = data.positions.begin(); | 
|---|
|  | 189 | std::vector< double >::const_iterator chargeiter = data.charges.begin(); | 
|---|
|  | 190 | FragmentForces::iterator forceiter = longrange_data.forces.begin(); | 
|---|
|  | 191 | for (; (positer != data.positions.end()) && (chargeiter != data.charges.end()); | 
|---|
|  | 192 | ++positer, ++chargeiter, ++forceiter) { | 
|---|
|  | 193 | ASSERT( positer->size() == NDIM, | 
|---|
|  | 194 | "calculateNucleiNucleiLongRangeContribution() - position " | 
|---|
|  | 195 | +toString(std::distance(data.positions.begin(), positer))+" has not 3 components."); | 
|---|
|  | 196 | const Vector position((*positer)[0], (*positer)[1], (*positer)[2]); | 
|---|
|  | 197 |  | 
|---|
|  | 198 | std::vector< std::vector<double> >::const_iterator otherpositer = data.positions.begin(); | 
|---|
|  | 199 | std::vector< double >::const_iterator otherchargeiter = data.charges.begin(); | 
|---|
|  | 200 | for (; (otherpositer != data.positions.end()) && (otherchargeiter != data.charges.end()); | 
|---|
|  | 201 | ++otherpositer, ++otherchargeiter) { | 
|---|
|  | 202 | if ((positer == otherpositer) || (chargeiter == otherchargeiter)) | 
|---|
|  | 203 | continue; | 
|---|
|  | 204 | ASSERT( otherpositer->size() == NDIM, | 
|---|
|  | 205 | "calculateNucleiNucleiLongRangeContribution() - other position " | 
|---|
|  | 206 | +toString(std::distance(data.positions.begin(), otherpositer))+" has not 3 components."); | 
|---|
|  | 207 | const Vector otherposition((*otherpositer)[0], (*otherpositer)[1], (*otherpositer)[2]); | 
|---|
|  | 208 | const Vector distance = position - otherposition; | 
|---|
|  | 209 | const double invsqrdist = 1./distance.Norm(); | 
|---|
|  | 210 | const double factor = (*otherchargeiter)*invsqrdist*invsqrdist*invsqrdist; | 
|---|
|  | 211 | (*forceiter)[0] = factor*distance[0]; | 
|---|
|  | 212 | (*forceiter)[1] = factor*distance[1]; | 
|---|
|  | 213 | (*forceiter)[2] = factor*distance[2]; | 
|---|
|  | 214 | longrange_data.nuclei_long += 0.5*(*chargeiter)*(*otherchargeiter)*invsqrdist; | 
|---|
|  | 215 | } | 
|---|
|  | 216 | } | 
|---|
|  | 217 | } | 
|---|
|  | 218 | } | 
|---|
| [25f5b66] | 219 | */ | 
|---|
| [82034f1] | 220 | #endif | 
|---|
|  | 221 |  | 
|---|
| [b5b01e] | 222 | ActionState::ptr FragmentationFragmentationAutomationAction::performCall() { | 
|---|
| [c5324f] | 223 | boost::asio::io_service io_service; | 
|---|
|  | 224 |  | 
|---|
|  | 225 | // TODO: Have io_service run in second thread and merge with current again eventually | 
|---|
|  | 226 |  | 
|---|
| [a3427f] | 227 | FragmentationResultContainer &container = | 
|---|
|  | 228 | FragmentationResultContainer::getInstance(); | 
|---|
|  | 229 | const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets(); | 
|---|
|  | 230 | const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets(); | 
|---|
| [ea63cb] | 231 | const FragmentJobQueue::edges_per_fragment_t &edges_per_fragment = | 
|---|
|  | 232 | FragmentJobQueue::getInstance().getEdgesPerFragment(); | 
|---|
| [a3427f] | 233 |  | 
|---|
| [4f056e] | 234 | size_t Exitflag = 0; | 
|---|
| [a3427f] | 235 | std::map<JobId_t, MPQCData> shortrangedata; | 
|---|
| [1dfe00] | 236 | #ifdef HAVE_JOBMARKET | 
|---|
| [4f056e] | 237 | { | 
|---|
| [ac9ca4] | 238 | const size_t NumberJobs = FragmentJobQueue::getInstance().size(); | 
|---|
| [4f056e] | 239 | MPQCFragmentController mpqccontroller(io_service); | 
|---|
|  | 240 | mpqccontroller.setHost(params.host.get()); | 
|---|
|  | 241 | mpqccontroller.setPort(params.port.get()); | 
|---|
|  | 242 | // Phase One: obtain ids | 
|---|
| [ac9ca4] | 243 | mpqccontroller.requestIds(NumberJobs); | 
|---|
| [a6c11a] | 244 | if (mpqccontroller.getExitflag() != 0) { | 
|---|
|  | 245 | ELOG(1, "Could not request all ids from Server."); | 
|---|
| [ca09be] | 246 | return Action::failure; | 
|---|
| [a6c11a] | 247 | } | 
|---|
| [4f056e] | 248 |  | 
|---|
| [ac9ca4] | 249 | // Phase Two: add MPQCJobs and send | 
|---|
| [a61dbb] | 250 | const bool AddJobsStatus = mpqccontroller.addJobsFromQueue( | 
|---|
| [6ff62c] | 251 | params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity, | 
|---|
|  | 252 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly | 
|---|
|  | 253 | ); | 
|---|
| [a61dbb] | 254 | if (AddJobsStatus) | 
|---|
|  | 255 | LOG(1, "INFO: Added jobs from FragmentJobsQueue."); | 
|---|
|  | 256 | else { | 
|---|
|  | 257 | ELOG(1, "Adding jobs failed."); | 
|---|
|  | 258 | return Action::failure; | 
|---|
|  | 259 | } | 
|---|
| [ac9ca4] | 260 | mpqccontroller.run(); | 
|---|
| [4f056e] | 261 |  | 
|---|
|  | 262 | // Phase Three: calculate result | 
|---|
| [c09f94] | 263 | setMaxSteps(NumberJobs); | 
|---|
|  | 264 | mpqccontroller.setUpdateHandler( | 
|---|
|  | 265 | boost::bind(&updateSteps, boost::ref(*this), _1, _2) | 
|---|
|  | 266 | ); | 
|---|
|  | 267 | start(); | 
|---|
|  | 268 | boost::thread wait_thread( | 
|---|
|  | 269 | boost::bind(&MPQCFragmentController::waitforResults, boost::ref(mpqccontroller), boost::cref(NumberJobs)) | 
|---|
|  | 270 | ); | 
|---|
|  | 271 | wait_thread.join(); | 
|---|
|  | 272 | stop(); | 
|---|
| [a6c11a] | 273 | if (mpqccontroller.getExitflag() != 0) { | 
|---|
|  | 274 | ELOG(1, "Could not obtain all results from Server."); | 
|---|
| [ca09be] | 275 | return Action::failure; | 
|---|
| [a6c11a] | 276 | } | 
|---|
| [ca09be] | 277 |  | 
|---|
| [a3427f] | 278 | mpqccontroller.getResults(shortrangedata); | 
|---|
| [f0dea0] | 279 | if (!checkResults(shortrangedata)) { | 
|---|
|  | 280 | ELOG(1, "At least one of the fragments' energy could not be properly minimized."); | 
|---|
| [ca09be] | 281 | return Action::failure; | 
|---|
| [f0dea0] | 282 | } | 
|---|
|  | 283 | if (mpqccontroller.getExitflag() != 0) { | 
|---|
|  | 284 | ELOG(1, "MPQCCommandFragmentController returned non-zero exit flag."); | 
|---|
|  | 285 | return Action::failure; | 
|---|
|  | 286 | } | 
|---|
| [4f056e] | 287 |  | 
|---|
|  | 288 | Exitflag += mpqccontroller.getExitflag(); | 
|---|
|  | 289 | } | 
|---|
| [bf0ff3] | 290 | #else | 
|---|
|  | 291 | { | 
|---|
|  | 292 | const size_t NumberJobs = FragmentJobQueue::getInstance().size(); | 
|---|
|  | 293 | MPQCCommandFragmentController mpqccontroller; | 
|---|
|  | 294 | // Phase One: obtain ids: not needed, we have infinite pool | 
|---|
|  | 295 |  | 
|---|
|  | 296 | // Phase Two: add MPQCJobs and send | 
|---|
|  | 297 | const size_t NoJobs = mpqccontroller.addJobsFromQueue( | 
|---|
|  | 298 | params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity, | 
|---|
| [d6a7b9] | 299 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly, | 
|---|
|  | 300 | params.executable.get().string() | 
|---|
| [bf0ff3] | 301 | ); | 
|---|
| [07ecc5] | 302 | if (NoJobs != NumberJobs) { | 
|---|
|  | 303 | ELOG(1, "Not all jobs were added succesfully."); | 
|---|
|  | 304 | return Action::failure; | 
|---|
|  | 305 | } | 
|---|
| [bf0ff3] | 306 | LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue."); | 
|---|
| [45cdce6] | 307 |  | 
|---|
|  | 308 | // prepare process | 
|---|
|  | 309 | setMaxSteps(NumberJobs); | 
|---|
|  | 310 | mpqccontroller.setUpdateHandler( | 
|---|
|  | 311 | boost::bind(&updateSteps, boost::ref(*this), _1, _2) | 
|---|
|  | 312 | ); | 
|---|
|  | 313 | start(); | 
|---|
| [bf0ff3] | 314 | mpqccontroller.run(); | 
|---|
| [45cdce6] | 315 | stop(); | 
|---|
| [f0dea0] | 316 | if (mpqccontroller.getExitflag() != 0) { | 
|---|
|  | 317 | ELOG(1, "MPQCCommandFragmentController returned non-zero exit flag before getting results."); | 
|---|
| [ca09be] | 318 | return Action::failure; | 
|---|
| [f0dea0] | 319 | } | 
|---|
| [bf0ff3] | 320 |  | 
|---|
|  | 321 | // get back the results and place them in shortrangedata | 
|---|
|  | 322 | mpqccontroller.getResults(shortrangedata); | 
|---|
|  | 323 | ASSERT( shortrangedata.size() == NumberJobs, | 
|---|
|  | 324 | "FragmentationFragmentationAutomationAction::performCall() - number of converted results " | 
|---|
|  | 325 | +toString(shortrangedata.size())+" and number of jobs "+toString(NumberJobs)+ " differ."); | 
|---|
| [f0dea0] | 326 | if (!checkResults(shortrangedata)) { | 
|---|
|  | 327 | ELOG(1, "At least one of the fragments' energy could not be properly minimized."); | 
|---|
|  | 328 | return Action::failure; | 
|---|
|  | 329 | } | 
|---|
|  | 330 | if (mpqccontroller.getExitflag() != 0) { | 
|---|
|  | 331 | ELOG(1, "MPQCCommandFragmentController returned non-zero exit flag after getting results."); | 
|---|
| [ca09be] | 332 | return Action::failure; | 
|---|
| [f0dea0] | 333 | } | 
|---|
| [bf0ff3] | 334 |  | 
|---|
| [4f056e] | 335 | Exitflag += mpqccontroller.getExitflag(); | 
|---|
|  | 336 | } | 
|---|
| [1dfe00] | 337 | #endif | 
|---|
| [c5324f] | 338 |  | 
|---|
| [18ed8c] | 339 | #if defined(HAVE_JOBMARKET) && defined(HAVE_VMG) | 
|---|
| [69c733] | 340 | if (params.DoLongrange.get()) { | 
|---|
| [a58c16] | 341 | if ( const_cast<const World &>(World::getInstance()).getAllAtoms().size() == 0) { | 
|---|
| [26b4d62] | 342 | STATUS("Please load the full molecule into the world before starting this action."); | 
|---|
| [db6f7d] | 343 | return Action::failure; | 
|---|
|  | 344 | } | 
|---|
| [06865e] | 345 |  | 
|---|
| [849cd8] | 346 | // obtain combined charge density | 
|---|
| [4a3df8] | 347 | std::map<JobId_t, MPQCData> shortrangedata_downsampled; | 
|---|
|  | 348 | SamplingGridProperties domain(ExportGraph_ToJobs::getDomainGrid(params.level.get())); | 
|---|
| [27594e] | 349 | FragmentationChargeDensity summedChargeDensity( | 
|---|
| [b5134a] | 350 | shortrangedata); | 
|---|
| [ebadb4] | 351 | { | 
|---|
| [4c648a] | 352 | SamplingGrid zero_globalgrid(domain); | 
|---|
| [ebadb4] | 353 | summedChargeDensity( | 
|---|
|  | 354 | shortrangedata, | 
|---|
|  | 355 | FragmentJobQueue::getInstance().getKeySets(), | 
|---|
|  | 356 | zero_globalgrid); | 
|---|
|  | 357 | } | 
|---|
| [27594e] | 358 | const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid(); | 
|---|
| [a3427f] | 359 | LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and " | 
|---|
| [4f056e] | 360 | << full_sample.size() << " level-wise long-range jobs."); | 
|---|
| [69c733] | 361 |  | 
|---|
| [1e5f84] | 362 | // check boundary conditions | 
|---|
|  | 363 | const BoundaryConditions::Conditions_t &conditions = | 
|---|
|  | 364 | World::getInstance().getDomain().getConditions(); | 
|---|
|  | 365 | const bool OpenBoundaryConditions = | 
|---|
|  | 366 | !((conditions[0] == BoundaryConditions::Wrap) && | 
|---|
|  | 367 | (conditions[1] == BoundaryConditions::Wrap) && | 
|---|
|  | 368 | (conditions[2] == BoundaryConditions::Wrap)); | 
|---|
|  | 369 | LOG(1, std::string("INFO: Using ") | 
|---|
|  | 370 | << (OpenBoundaryConditions ? "open" : "periodic") | 
|---|
|  | 371 | << " boundary conditions."); | 
|---|
|  | 372 |  | 
|---|
| [4f056e] | 373 | // Phase Four: obtain more ids | 
|---|
| [a3427f] | 374 | std::map<JobId_t, VMGData> longrangedata; | 
|---|
| [4f056e] | 375 | { | 
|---|
|  | 376 | VMGFragmentController vmgcontroller(io_service); | 
|---|
|  | 377 | vmgcontroller.setHost(params.host.get()); | 
|---|
|  | 378 | vmgcontroller.setPort(params.port.get()); | 
|---|
| [a3427f] | 379 | const size_t NoJobs = shortrangedata.size()+full_sample.size(); | 
|---|
| [e2925fd] | 380 | vmgcontroller.requestIds(2*NoJobs); | 
|---|
| [f0dea0] | 381 | if (vmgcontroller.getExitflag() != 0) { | 
|---|
|  | 382 | ELOG(1, "VMGFragmentController returned non-zero exit flag on requesting long-range ids."); | 
|---|
| [ca09be] | 383 | return Action::failure; | 
|---|
| [f0dea0] | 384 | } | 
|---|
| [4f056e] | 385 |  | 
|---|
| [e2925fd] | 386 | // Phase Five a: create VMGJobs for electronic charge distribution | 
|---|
| [4f056e] | 387 | const size_t near_field_cells = params.near_field_cells.get(); | 
|---|
| [cd2591] | 388 | const size_t interpolation_degree = params.interpolation_degree.get(); | 
|---|
| [4f056e] | 389 | if (!vmgcontroller.createLongRangeJobs( | 
|---|
| [a3427f] | 390 | shortrangedata, | 
|---|
| [4f056e] | 391 | full_sample, | 
|---|
| [cd2591] | 392 | near_field_cells, | 
|---|
| [6ff62c] | 393 | interpolation_degree, | 
|---|
| [e2925fd] | 394 | VMGFragmentController::DontSampleParticles, | 
|---|
|  | 395 | VMGFragmentController::DoTreatGrid, | 
|---|
| [b6b21a] | 396 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly, | 
|---|
| [1e5f84] | 397 | params.DoPrintDebug.get(), | 
|---|
| [17e4fd] | 398 | OpenBoundaryConditions, | 
|---|
| [666e9e] | 399 | params.DoSmearCharges.get(), | 
|---|
|  | 400 | false)) { | 
|---|
| [26b4d62] | 401 | STATUS("Could not create long-range jobs for electronic charge distribution."); | 
|---|
| [4f056e] | 402 | return Action::failure; | 
|---|
| [26b4d62] | 403 | } | 
|---|
| [4f056e] | 404 |  | 
|---|
| [e2925fd] | 405 | // Phase Six a: calculate result | 
|---|
| [4f056e] | 406 | vmgcontroller.waitforResults(NoJobs); | 
|---|
| [f0dea0] | 407 | if (vmgcontroller.getExitflag() != 0) { | 
|---|
|  | 408 | ELOG(1, "VMGFragmentController returned non-zero exit flag before getting results."); | 
|---|
| [ca09be] | 409 | return Action::failure; | 
|---|
| [f0dea0] | 410 | } | 
|---|
| [a3427f] | 411 | vmgcontroller.getResults(longrangedata); | 
|---|
|  | 412 | ASSERT( NoJobs == longrangedata.size(), | 
|---|
| [4f056e] | 413 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+" | 
|---|
|  | 414 | +toString(full_sample.size())+"="+toString(NoJobs) | 
|---|
| [e2925fd] | 415 | +" and first VMGresults "+toString(longrangedata.size())+" don't match."); | 
|---|
| [f0dea0] | 416 | if (!checkResults(longrangedata)) { | 
|---|
|  | 417 | ELOG(1, "At least one of the fragments' electronic long-range potential could not be properly minimized."); | 
|---|
|  | 418 | return Action::failure; | 
|---|
|  | 419 | } | 
|---|
| [4f056e] | 420 | Exitflag += vmgcontroller.getExitflag(); | 
|---|
| [e2925fd] | 421 |  | 
|---|
|  | 422 | { | 
|---|
|  | 423 | std::map<JobId_t, VMGData> longrangedata_both; | 
|---|
|  | 424 | // Phase Five b: create VMGJobs for nuclei charge distributions | 
|---|
|  | 425 | const size_t near_field_cells = params.near_field_cells.get(); | 
|---|
|  | 426 | const size_t interpolation_degree = params.interpolation_degree.get(); | 
|---|
|  | 427 | if (!vmgcontroller.createLongRangeJobs( | 
|---|
|  | 428 | shortrangedata, | 
|---|
|  | 429 | full_sample, | 
|---|
|  | 430 | near_field_cells, | 
|---|
|  | 431 | interpolation_degree, | 
|---|
|  | 432 | VMGFragmentController::DoSampleParticles, | 
|---|
|  | 433 | VMGFragmentController::DoTreatGrid, | 
|---|
|  | 434 | params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly, | 
|---|
| [1e5f84] | 435 | params.DoPrintDebug.get(), | 
|---|
| [17e4fd] | 436 | OpenBoundaryConditions, | 
|---|
| [666e9e] | 437 | params.DoSmearCharges.get(), | 
|---|
|  | 438 | params.UseImplicitCharges.get())) { | 
|---|
| [26b4d62] | 439 | STATUS("Could not create long-range jobs for nuclei charge distribution."); | 
|---|
| [e2925fd] | 440 | return Action::failure; | 
|---|
| [26b4d62] | 441 | } | 
|---|
| [e2925fd] | 442 |  | 
|---|
|  | 443 | // Phase Six b: calculate result | 
|---|
|  | 444 | vmgcontroller.waitforResults(NoJobs); | 
|---|
| [a6c11a] | 445 | if (vmgcontroller.getExitflag() != 0) { | 
|---|
|  | 446 | ELOG(1, "VMGFragmentController returned non-zero exit flag before getting results."); | 
|---|
| [ca09be] | 447 | return Action::failure; | 
|---|
| [a6c11a] | 448 | } | 
|---|
| [e2925fd] | 449 | vmgcontroller.getResults(longrangedata_both); | 
|---|
|  | 450 | ASSERT( NoJobs == longrangedata_both.size(), | 
|---|
|  | 451 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+" | 
|---|
|  | 452 | +toString(full_sample.size())+"="+toString(NoJobs) | 
|---|
|  | 453 | +" and second VMGresults "+toString(longrangedata_both.size())+" don't match."); | 
|---|
| [f0dea0] | 454 | if (!checkResults(longrangedata_both)) { | 
|---|
|  | 455 | ELOG(1, "At least one of the fragments' nuclei long-range potential could not be properly minimized."); | 
|---|
| [ca09be] | 456 | return Action::failure; | 
|---|
| [f0dea0] | 457 | } | 
|---|
|  | 458 | if (vmgcontroller.getExitflag() != 0) { | 
|---|
|  | 459 | ELOG(1, "VMGFragmentController returned non-zero exit flag after getting results."); | 
|---|
|  | 460 | return Action::failure; | 
|---|
|  | 461 | } | 
|---|
| [e2925fd] | 462 | Exitflag += vmgcontroller.getExitflag(); | 
|---|
|  | 463 |  | 
|---|
| [82034f1] | 464 | // now replace nuclei-nuclei contribution with values from direct summation (is exact for | 
|---|
|  | 465 | // open boundary conditions | 
|---|
|  | 466 | // | 
|---|
|  | 467 | // NOTE: We need to calculate both_sampled_potential for fitting partial charges, | 
|---|
|  | 468 | // nowhere else is this quantity needed. | 
|---|
|  | 469 | //if (OpenBoundaryConditions) | 
|---|
|  | 470 | //  calculateNucleiNucleiLongRangeContribution(shortrangedata, longrangedata_both); | 
|---|
|  | 471 |  | 
|---|
| [e2925fd] | 472 | // go through either data and replace nuclei_long with contribution from both | 
|---|
|  | 473 | ASSERT( longrangedata.size() == longrangedata_both.size(), | 
|---|
|  | 474 | "FragmentationFragmentationAutomationAction::performCall() - longrange results have different sizes."); | 
|---|
|  | 475 | std::map<JobId_t, VMGData>::iterator destiter = longrangedata.begin(); | 
|---|
| [e355b31] | 476 | std::map<JobId_t, VMGData>::const_iterator srciter = longrangedata_both.begin(); | 
|---|
|  | 477 | for (;destiter != longrangedata.end(); ++srciter, ++destiter) { | 
|---|
|  | 478 | destiter->second.both_sampled_potential = srciter->second.sampled_potential; | 
|---|
| [e2925fd] | 479 | destiter->second.nuclei_long = srciter->second.nuclei_long; | 
|---|
| [94db13] | 480 | destiter->second.forces = srciter->second.forces; | 
|---|
|  | 481 | destiter->second.hasForces = srciter->second.hasForces; | 
|---|
| [e355b31] | 482 | } | 
|---|
| [e2925fd] | 483 | } | 
|---|
| [4f056e] | 484 | } | 
|---|
| [a2295a] | 485 |  | 
|---|
| [b6b21a] | 486 | if (params.DoPrintDebug.get()) { | 
|---|
|  | 487 | std::map<JobId_t, std::string> debugData; | 
|---|
|  | 488 | { | 
|---|
|  | 489 | if (!full_sample.empty()) { | 
|---|
|  | 490 | // create debug jobs for each level to print the summed-up potential to vtk files | 
|---|
|  | 491 | VMGDebugGridFragmentController debugcontroller(io_service); | 
|---|
|  | 492 | debugcontroller.setHost(params.host.get()); | 
|---|
|  | 493 | debugcontroller.setPort(params.port.get()); | 
|---|
|  | 494 | debugcontroller.requestIds(full_sample.size()); | 
|---|
| [26b4d62] | 495 | if (!debugcontroller.createDebugJobs(full_sample, OpenBoundaryConditions)) { | 
|---|
|  | 496 | STATUS("Could not create debug jobs."); | 
|---|
| [b6b21a] | 497 | return Action::failure; | 
|---|
| [26b4d62] | 498 | } | 
|---|
| [b6b21a] | 499 | debugcontroller.waitforResults(full_sample.size()); | 
|---|
|  | 500 | debugcontroller.getResults(debugData); | 
|---|
|  | 501 | Exitflag += debugcontroller.getExitflag(); | 
|---|
|  | 502 | } | 
|---|
| [a2295a] | 503 | } | 
|---|
| [2764e0] | 504 | } | 
|---|
| [2ae247] | 505 | container.clear(); | 
|---|
| [ea63cb] | 506 | container.addFullResults(keysets, forcekeysets, edges_per_fragment, shortrangedata, longrangedata); | 
|---|
| [a3427f] | 507 | } else { | 
|---|
| [2ae247] | 508 | container.clear(); | 
|---|
| [ea63cb] | 509 | container.addShortRangeResults(keysets, forcekeysets, edges_per_fragment, shortrangedata); | 
|---|
| [69c733] | 510 | } | 
|---|
| [a3427f] | 511 | #else | 
|---|
| [2ae247] | 512 | container.clear(); | 
|---|
| [ea63cb] | 513 | container.addShortRangeResults(keysets, forcekeysets, edges_per_fragment, shortrangedata); | 
|---|
| [69c733] | 514 | #endif | 
|---|
| [edecba] | 515 |  | 
|---|
| [bae7bc] | 516 | // now clear all present jobs as we are done | 
|---|
|  | 517 | FragmentJobQueue::getInstance().clear(); | 
|---|
|  | 518 |  | 
|---|
| [61cc0f] | 519 | if (Exitflag != 0) { | 
|---|
|  | 520 | STATUS("Controller has returned failure: "+toString(Exitflag)); | 
|---|
|  | 521 | } | 
|---|
| [edecba] | 522 | return (Exitflag == 0) ? Action::success : Action::failure; | 
|---|
|  | 523 | } | 
|---|
|  | 524 |  | 
|---|
| [b5b01e] | 525 | ActionState::ptr FragmentationFragmentationAutomationAction::performUndo(ActionState::ptr _state) { | 
|---|
| [edecba] | 526 | return Action::success; | 
|---|
|  | 527 | } | 
|---|
|  | 528 |  | 
|---|
| [b5b01e] | 529 | ActionState::ptr FragmentationFragmentationAutomationAction::performRedo(ActionState::ptr _state){ | 
|---|
| [edecba] | 530 | return Action::success; | 
|---|
|  | 531 | } | 
|---|
|  | 532 |  | 
|---|
|  | 533 | bool FragmentationFragmentationAutomationAction::canUndo() { | 
|---|
|  | 534 | return false; | 
|---|
|  | 535 | } | 
|---|
|  | 536 |  | 
|---|
|  | 537 | bool FragmentationFragmentationAutomationAction::shouldUndo() { | 
|---|
|  | 538 | return false; | 
|---|
|  | 539 | } | 
|---|
|  | 540 | /** =========== end of function ====================== */ | 
|---|