source: src/Actions/FragmentationAction/FragmentationAutomationAction.cpp@ c24071

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since c24071 was 2ae247, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: Clearing FragmentationResultsContainer before adding results in FragmentationAutomation.

  • Property mode set to 100644
File size: 16.5 KB
RevLine 
[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
95using namespace MoleCuilder;
96
97// and construct the stuff
98#include "FragmentationAutomationAction.def"
99#include "Action_impl_pre.hpp"
100/** =========== define the function ====================== */
101
102class controller_AddOn;
103
104// needs to be defined for using the FragmentController
105controller_AddOn *getAddOn()
106{
107 return NULL;
108}
109
[c09f94]110static 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
[f0dea0]116template <class ResultClass>
117std::pair<double, double> getResiduals(const ResultClass &_result)
118{ return std::pair<double, double>(0.,0.); }
119
120std::pair<double, double> getResiduals(const MPQCData &_result)
121{ return std::make_pair(_result.accuracy, _result.desired_accuracy); }
122
123std::pair<double, double> getResiduals(const VMGData &_result)
[7debff]124{
125 // sensibility check on absolute residual
126 if (_result.residual > 1e-5)
127 ELOG(2, "Encountered absolute residual greater than 1e-5: " << _result.residual);
128 // take smaller value of the two as just one of them needs to be less than precision
129 const double residual = std::min(_result.relative_residual, _result.residual);
130 return std::make_pair(residual, _result.precision);
131}
[f0dea0]132
133template <class ResultClass>
134bool checkResults(const typename std::map<JobId_t, ResultClass> &_results)
135{
136 bool result = true;
137 for (typename std::map<JobId_t, ResultClass>::const_iterator iter = _results.begin();
138 iter != _results.end(); ++iter) {
139 std::pair<double, double> residuals = getResiduals(iter->second);
140 if (residuals.second != 0.) {
141 if (residuals.first >= residuals.second) {
142 ELOG(1, "Fragment Job " << iter->first << " converged to " << residuals.first
143 << " instead of " << residuals.second);
144 result = false;
145 }
146 } else {
147 LOG(4, "DEBUG: Not checking accuracy because desired precision not given.");
148 }
149 }
150 return result;
151}
152
[b5b01e]153ActionState::ptr FragmentationFragmentationAutomationAction::performCall() {
[c5324f]154 boost::asio::io_service io_service;
155
156 // TODO: Have io_service run in second thread and merge with current again eventually
157
[a3427f]158 FragmentationResultContainer &container =
159 FragmentationResultContainer::getInstance();
160 const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets();
161 const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets();
162
[4f056e]163 size_t Exitflag = 0;
[a3427f]164 std::map<JobId_t, MPQCData> shortrangedata;
[1dfe00]165#ifdef HAVE_JOBMARKET
[4f056e]166 {
[ac9ca4]167 const size_t NumberJobs = FragmentJobQueue::getInstance().size();
[4f056e]168 MPQCFragmentController mpqccontroller(io_service);
169 mpqccontroller.setHost(params.host.get());
170 mpqccontroller.setPort(params.port.get());
171 // Phase One: obtain ids
[ac9ca4]172 mpqccontroller.requestIds(NumberJobs);
[ca09be]173 if (mpqccontroller.getExitflag() != 0)
174 return Action::failure;
[4f056e]175
[ac9ca4]176 // Phase Two: add MPQCJobs and send
[a61dbb]177 const bool AddJobsStatus = mpqccontroller.addJobsFromQueue(
[6ff62c]178 params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
179 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly
180 );
[a61dbb]181 if (AddJobsStatus)
182 LOG(1, "INFO: Added jobs from FragmentJobsQueue.");
183 else {
184 ELOG(1, "Adding jobs failed.");
185 return Action::failure;
186 }
[ac9ca4]187 mpqccontroller.run();
[4f056e]188
189 // Phase Three: calculate result
[c09f94]190 setMaxSteps(NumberJobs);
191 mpqccontroller.setUpdateHandler(
192 boost::bind(&updateSteps, boost::ref(*this), _1, _2)
193 );
194 start();
195 boost::thread wait_thread(
196 boost::bind(&MPQCFragmentController::waitforResults, boost::ref(mpqccontroller), boost::cref(NumberJobs))
197 );
198 wait_thread.join();
199 stop();
[ca09be]200 if (mpqccontroller.getExitflag() != 0)
201 return Action::failure;
202
[a3427f]203 mpqccontroller.getResults(shortrangedata);
[f0dea0]204 if (!checkResults(shortrangedata)) {
205 ELOG(1, "At least one of the fragments' energy could not be properly minimized.");
[ca09be]206 return Action::failure;
[f0dea0]207 }
208 if (mpqccontroller.getExitflag() != 0) {
209 ELOG(1, "MPQCCommandFragmentController returned non-zero exit flag.");
210 return Action::failure;
211 }
[4f056e]212
213 Exitflag += mpqccontroller.getExitflag();
214 }
[bf0ff3]215#else
216 {
217 const size_t NumberJobs = FragmentJobQueue::getInstance().size();
218 MPQCCommandFragmentController mpqccontroller;
219 // Phase One: obtain ids: not needed, we have infinite pool
220
221 // Phase Two: add MPQCJobs and send
222 const size_t NoJobs = mpqccontroller.addJobsFromQueue(
223 params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
[d6a7b9]224 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
225 params.executable.get().string()
[bf0ff3]226 );
[07ecc5]227 if (NoJobs != NumberJobs) {
228 ELOG(1, "Not all jobs were added succesfully.");
229 return Action::failure;
230 }
[bf0ff3]231 LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue.");
[45cdce6]232
233 // prepare process
234 setMaxSteps(NumberJobs);
235 mpqccontroller.setUpdateHandler(
236 boost::bind(&updateSteps, boost::ref(*this), _1, _2)
237 );
238 start();
[bf0ff3]239 mpqccontroller.run();
[45cdce6]240 stop();
[f0dea0]241 if (mpqccontroller.getExitflag() != 0) {
242 ELOG(1, "MPQCCommandFragmentController returned non-zero exit flag before getting results.");
[ca09be]243 return Action::failure;
[f0dea0]244 }
[bf0ff3]245
246 // get back the results and place them in shortrangedata
247 mpqccontroller.getResults(shortrangedata);
248 ASSERT( shortrangedata.size() == NumberJobs,
249 "FragmentationFragmentationAutomationAction::performCall() - number of converted results "
250 +toString(shortrangedata.size())+" and number of jobs "+toString(NumberJobs)+ " differ.");
[f0dea0]251 if (!checkResults(shortrangedata)) {
252 ELOG(1, "At least one of the fragments' energy could not be properly minimized.");
253 return Action::failure;
254 }
255 if (mpqccontroller.getExitflag() != 0) {
256 ELOG(1, "MPQCCommandFragmentController returned non-zero exit flag after getting results.");
[ca09be]257 return Action::failure;
[f0dea0]258 }
[bf0ff3]259
[4f056e]260 Exitflag += mpqccontroller.getExitflag();
261 }
[1dfe00]262#endif
[c5324f]263
[18ed8c]264#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
[69c733]265 if (params.DoLongrange.get()) {
[a58c16]266 if ( const_cast<const World &>(World::getInstance()).getAllAtoms().size() == 0) {
[26b4d62]267 STATUS("Please load the full molecule into the world before starting this action.");
[db6f7d]268 return Action::failure;
269 }
[06865e]270
[849cd8]271 // obtain combined charge density
[27594e]272 FragmentationChargeDensity summedChargeDensity(
[a3427f]273 shortrangedata,
[bae7bc]274 FragmentJobQueue::getInstance().getKeySets());
[27594e]275 const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
[a3427f]276 LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and "
[4f056e]277 << full_sample.size() << " level-wise long-range jobs.");
[69c733]278
[1e5f84]279 // check boundary conditions
280 const BoundaryConditions::Conditions_t &conditions =
281 World::getInstance().getDomain().getConditions();
282 const bool OpenBoundaryConditions =
283 !((conditions[0] == BoundaryConditions::Wrap) &&
284 (conditions[1] == BoundaryConditions::Wrap) &&
285 (conditions[2] == BoundaryConditions::Wrap));
286 LOG(1, std::string("INFO: Using ")
287 << (OpenBoundaryConditions ? "open" : "periodic")
288 << " boundary conditions.");
289
[4f056e]290 // Phase Four: obtain more ids
[a3427f]291 std::map<JobId_t, VMGData> longrangedata;
[4f056e]292 {
293 VMGFragmentController vmgcontroller(io_service);
294 vmgcontroller.setHost(params.host.get());
295 vmgcontroller.setPort(params.port.get());
[a3427f]296 const size_t NoJobs = shortrangedata.size()+full_sample.size();
[e2925fd]297 vmgcontroller.requestIds(2*NoJobs);
[f0dea0]298 if (vmgcontroller.getExitflag() != 0) {
299 ELOG(1, "VMGFragmentController returned non-zero exit flag on requesting long-range ids.");
[ca09be]300 return Action::failure;
[f0dea0]301 }
[4f056e]302
[e2925fd]303 // Phase Five a: create VMGJobs for electronic charge distribution
[4f056e]304 const size_t near_field_cells = params.near_field_cells.get();
[cd2591]305 const size_t interpolation_degree = params.interpolation_degree.get();
[4f056e]306 if (!vmgcontroller.createLongRangeJobs(
[a3427f]307 shortrangedata,
[4f056e]308 full_sample,
[cd2591]309 near_field_cells,
[6ff62c]310 interpolation_degree,
[e2925fd]311 VMGFragmentController::DontSampleParticles,
312 VMGFragmentController::DoTreatGrid,
[b6b21a]313 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
[1e5f84]314 params.DoPrintDebug.get(),
[17e4fd]315 OpenBoundaryConditions,
[666e9e]316 params.DoSmearCharges.get(),
317 false)) {
[26b4d62]318 STATUS("Could not create long-range jobs for electronic charge distribution.");
[4f056e]319 return Action::failure;
[26b4d62]320 }
[4f056e]321
[e2925fd]322 // Phase Six a: calculate result
[4f056e]323 vmgcontroller.waitforResults(NoJobs);
[f0dea0]324 if (vmgcontroller.getExitflag() != 0) {
325 ELOG(1, "VMGFragmentController returned non-zero exit flag before getting results.");
[ca09be]326 return Action::failure;
[f0dea0]327 }
[a3427f]328 vmgcontroller.getResults(longrangedata);
329 ASSERT( NoJobs == longrangedata.size(),
[4f056e]330 "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
331 +toString(full_sample.size())+"="+toString(NoJobs)
[e2925fd]332 +" and first VMGresults "+toString(longrangedata.size())+" don't match.");
[f0dea0]333 if (!checkResults(longrangedata)) {
334 ELOG(1, "At least one of the fragments' electronic long-range potential could not be properly minimized.");
335 return Action::failure;
336 }
[4f056e]337 Exitflag += vmgcontroller.getExitflag();
[e2925fd]338
339 {
340 std::map<JobId_t, VMGData> longrangedata_both;
341 // Phase Five b: create VMGJobs for nuclei charge distributions
342 const size_t near_field_cells = params.near_field_cells.get();
343 const size_t interpolation_degree = params.interpolation_degree.get();
344 if (!vmgcontroller.createLongRangeJobs(
345 shortrangedata,
346 full_sample,
347 near_field_cells,
348 interpolation_degree,
349 VMGFragmentController::DoSampleParticles,
350 VMGFragmentController::DoTreatGrid,
351 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
[1e5f84]352 params.DoPrintDebug.get(),
[17e4fd]353 OpenBoundaryConditions,
[666e9e]354 params.DoSmearCharges.get(),
355 params.UseImplicitCharges.get())) {
[26b4d62]356 STATUS("Could not create long-range jobs for nuclei charge distribution.");
[e2925fd]357 return Action::failure;
[26b4d62]358 }
[e2925fd]359
360 // Phase Six b: calculate result
361 vmgcontroller.waitforResults(NoJobs);
[ca09be]362 if (vmgcontroller.getExitflag() != 0)
363 return Action::failure;
[e2925fd]364 vmgcontroller.getResults(longrangedata_both);
365 ASSERT( NoJobs == longrangedata_both.size(),
366 "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
367 +toString(full_sample.size())+"="+toString(NoJobs)
368 +" and second VMGresults "+toString(longrangedata_both.size())+" don't match.");
[f0dea0]369 if (!checkResults(longrangedata_both)) {
370 ELOG(1, "At least one of the fragments' nuclei long-range potential could not be properly minimized.");
[ca09be]371 return Action::failure;
[f0dea0]372 }
373 if (vmgcontroller.getExitflag() != 0) {
374 ELOG(1, "VMGFragmentController returned non-zero exit flag after getting results.");
375 return Action::failure;
376 }
[e2925fd]377 Exitflag += vmgcontroller.getExitflag();
378
379 // go through either data and replace nuclei_long with contribution from both
380 ASSERT( longrangedata.size() == longrangedata_both.size(),
381 "FragmentationFragmentationAutomationAction::performCall() - longrange results have different sizes.");
382 std::map<JobId_t, VMGData>::iterator destiter = longrangedata.begin();
[e355b31]383 std::map<JobId_t, VMGData>::const_iterator srciter = longrangedata_both.begin();
384 for (;destiter != longrangedata.end(); ++srciter, ++destiter) {
385 destiter->second.both_sampled_potential = srciter->second.sampled_potential;
[e2925fd]386 destiter->second.nuclei_long = srciter->second.nuclei_long;
[94db13]387 destiter->second.forces = srciter->second.forces;
388 destiter->second.hasForces = srciter->second.hasForces;
[e355b31]389 }
[e2925fd]390 }
[4f056e]391 }
[a2295a]392
[b6b21a]393 if (params.DoPrintDebug.get()) {
394 std::map<JobId_t, std::string> debugData;
395 {
396 if (!full_sample.empty()) {
397 // create debug jobs for each level to print the summed-up potential to vtk files
398 VMGDebugGridFragmentController debugcontroller(io_service);
399 debugcontroller.setHost(params.host.get());
400 debugcontroller.setPort(params.port.get());
401 debugcontroller.requestIds(full_sample.size());
[26b4d62]402 if (!debugcontroller.createDebugJobs(full_sample, OpenBoundaryConditions)) {
403 STATUS("Could not create debug jobs.");
[b6b21a]404 return Action::failure;
[26b4d62]405 }
[b6b21a]406 debugcontroller.waitforResults(full_sample.size());
407 debugcontroller.getResults(debugData);
408 Exitflag += debugcontroller.getExitflag();
409 }
[a2295a]410 }
[2764e0]411 }
[2ae247]412 container.clear();
[a3427f]413 container.addFullResults(keysets, forcekeysets, shortrangedata, longrangedata);
414 } else {
[2ae247]415 container.clear();
[a3427f]416 container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
[69c733]417 }
[a3427f]418#else
[2ae247]419 container.clear();
[a3427f]420 container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
[69c733]421#endif
[edecba]422
[bae7bc]423 // now clear all present jobs as we are done
424 FragmentJobQueue::getInstance().clear();
425
[26b4d62]426 if (Exitflag != 0)
427 STATUS("Controller has returned failure.");
[edecba]428 return (Exitflag == 0) ? Action::success : Action::failure;
429}
430
[b5b01e]431ActionState::ptr FragmentationFragmentationAutomationAction::performUndo(ActionState::ptr _state) {
[edecba]432 return Action::success;
433}
434
[b5b01e]435ActionState::ptr FragmentationFragmentationAutomationAction::performRedo(ActionState::ptr _state){
[edecba]436 return Action::success;
437}
438
439bool FragmentationFragmentationAutomationAction::canUndo() {
440 return false;
441}
442
443bool FragmentationFragmentationAutomationAction::shouldUndo() {
444 return false;
445}
446/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.