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

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing 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_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since c73e35 was 26b4d62, checked in by Frederik Heber <heber@…>, 11 years ago

All Actions now give correct failure status via STATUS() macro.

  • Property mode set to 100644
File size: 11.6 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"
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
[c09f94]75#include <boost/bind.hpp>
76#include <boost/thread.hpp>
[86cfac5]77#include <fstream>
[edecba]78#include <iostream>
79#include <string>
80#include <vector>
81
[8cae4c]82#include <boost/mpl/for_each.hpp>
83
[edecba]84#include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
85
86using namespace MoleCuilder;
87
88// and construct the stuff
89#include "FragmentationAutomationAction.def"
90#include "Action_impl_pre.hpp"
91/** =========== define the function ====================== */
92
93class controller_AddOn;
94
95// needs to be defined for using the FragmentController
96controller_AddOn *getAddOn()
97{
98 return NULL;
99}
100
[c09f94]101static void updateSteps(Process &p, const size_t step, const size_t total)
102{
103 LOG(1, "There are " << step << " steps out of " << total << " done.");
104 p.setCurrStep(step);
105}
106
[b5b01e]107ActionState::ptr FragmentationFragmentationAutomationAction::performCall() {
[c5324f]108 boost::asio::io_service io_service;
109
110 // TODO: Have io_service run in second thread and merge with current again eventually
111
[a3427f]112 FragmentationResultContainer &container =
113 FragmentationResultContainer::getInstance();
114 const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets();
115 const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets();
116
[4f056e]117 size_t Exitflag = 0;
[a3427f]118 std::map<JobId_t, MPQCData> shortrangedata;
[4f056e]119 {
[ac9ca4]120 const size_t NumberJobs = FragmentJobQueue::getInstance().size();
[4f056e]121 MPQCFragmentController mpqccontroller(io_service);
122 mpqccontroller.setHost(params.host.get());
123 mpqccontroller.setPort(params.port.get());
124 // Phase One: obtain ids
[ac9ca4]125 mpqccontroller.requestIds(NumberJobs);
[4f056e]126
[ac9ca4]127 // Phase Two: add MPQCJobs and send
[503acc1]128 const size_t NoJobs = mpqccontroller.addJobsFromQueue(
[6ff62c]129 params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
130 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly
131 );
[503acc1]132 LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue.");
[ac9ca4]133 mpqccontroller.run();
[4f056e]134
135 // Phase Three: calculate result
[c09f94]136 setMaxSteps(NumberJobs);
137 mpqccontroller.setUpdateHandler(
138 boost::bind(&updateSteps, boost::ref(*this), _1, _2)
139 );
140 start();
141 boost::thread wait_thread(
142 boost::bind(&MPQCFragmentController::waitforResults, boost::ref(mpqccontroller), boost::cref(NumberJobs))
143 );
144 wait_thread.join();
145 stop();
[a3427f]146 mpqccontroller.getResults(shortrangedata);
[4f056e]147
148 Exitflag += mpqccontroller.getExitflag();
149 }
[c5324f]150
[69c733]151#ifdef HAVE_VMG
152 if (params.DoLongrange.get()) {
[db6f7d]153 if ( World::getInstance().getAllAtoms().size() == 0) {
[26b4d62]154 STATUS("Please load the full molecule into the world before starting this action.");
[db6f7d]155 return Action::failure;
156 }
[06865e]157
[849cd8]158 // obtain combined charge density
[27594e]159 FragmentationChargeDensity summedChargeDensity(
[a3427f]160 shortrangedata,
[bae7bc]161 FragmentJobQueue::getInstance().getKeySets());
[27594e]162 const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
[a3427f]163 LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and "
[4f056e]164 << full_sample.size() << " level-wise long-range jobs.");
[69c733]165
[1e5f84]166 // check boundary conditions
167 const BoundaryConditions::Conditions_t &conditions =
168 World::getInstance().getDomain().getConditions();
169 const bool OpenBoundaryConditions =
170 !((conditions[0] == BoundaryConditions::Wrap) &&
171 (conditions[1] == BoundaryConditions::Wrap) &&
172 (conditions[2] == BoundaryConditions::Wrap));
173 LOG(1, std::string("INFO: Using ")
174 << (OpenBoundaryConditions ? "open" : "periodic")
175 << " boundary conditions.");
176
[4f056e]177 // Phase Four: obtain more ids
[a3427f]178 std::map<JobId_t, VMGData> longrangedata;
[4f056e]179 {
180 VMGFragmentController vmgcontroller(io_service);
181 vmgcontroller.setHost(params.host.get());
182 vmgcontroller.setPort(params.port.get());
[a3427f]183 const size_t NoJobs = shortrangedata.size()+full_sample.size();
[e2925fd]184 vmgcontroller.requestIds(2*NoJobs);
[4f056e]185
[e2925fd]186 // Phase Five a: create VMGJobs for electronic charge distribution
[4f056e]187 const size_t near_field_cells = params.near_field_cells.get();
[cd2591]188 const size_t interpolation_degree = params.interpolation_degree.get();
[4f056e]189 if (!vmgcontroller.createLongRangeJobs(
[a3427f]190 shortrangedata,
[4f056e]191 full_sample,
[cd2591]192 near_field_cells,
[6ff62c]193 interpolation_degree,
[e2925fd]194 VMGFragmentController::DontSampleParticles,
195 VMGFragmentController::DoTreatGrid,
[b6b21a]196 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
[1e5f84]197 params.DoPrintDebug.get(),
[26b4d62]198 OpenBoundaryConditions)) {
199 STATUS("Could not create long-range jobs for electronic charge distribution.");
[4f056e]200 return Action::failure;
[26b4d62]201 }
[4f056e]202
[e2925fd]203 // Phase Six a: calculate result
[4f056e]204 vmgcontroller.waitforResults(NoJobs);
[a3427f]205 vmgcontroller.getResults(longrangedata);
206 ASSERT( NoJobs == longrangedata.size(),
[4f056e]207 "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
208 +toString(full_sample.size())+"="+toString(NoJobs)
[e2925fd]209 +" and first VMGresults "+toString(longrangedata.size())+" don't match.");
[4f056e]210 Exitflag += vmgcontroller.getExitflag();
[e2925fd]211
212 {
213 std::map<JobId_t, VMGData> longrangedata_both;
214 // Phase Five b: create VMGJobs for nuclei charge distributions
215 const size_t near_field_cells = params.near_field_cells.get();
216 const size_t interpolation_degree = params.interpolation_degree.get();
217 if (!vmgcontroller.createLongRangeJobs(
218 shortrangedata,
219 full_sample,
220 near_field_cells,
221 interpolation_degree,
222 VMGFragmentController::DoSampleParticles,
223 VMGFragmentController::DoTreatGrid,
224 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
[1e5f84]225 params.DoPrintDebug.get(),
[26b4d62]226 OpenBoundaryConditions)) {
227 STATUS("Could not create long-range jobs for nuclei charge distribution.");
[e2925fd]228 return Action::failure;
[26b4d62]229 }
[e2925fd]230
231 // Phase Six b: calculate result
232 vmgcontroller.waitforResults(NoJobs);
233 vmgcontroller.getResults(longrangedata_both);
234 ASSERT( NoJobs == longrangedata_both.size(),
235 "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
236 +toString(full_sample.size())+"="+toString(NoJobs)
237 +" and second VMGresults "+toString(longrangedata_both.size())+" don't match.");
238 Exitflag += vmgcontroller.getExitflag();
239
240 // go through either data and replace nuclei_long with contribution from both
241 ASSERT( longrangedata.size() == longrangedata_both.size(),
242 "FragmentationFragmentationAutomationAction::performCall() - longrange results have different sizes.");
243 std::map<JobId_t, VMGData>::iterator destiter = longrangedata.begin();
[e355b31]244 std::map<JobId_t, VMGData>::const_iterator srciter = longrangedata_both.begin();
245 for (;destiter != longrangedata.end(); ++srciter, ++destiter) {
246 destiter->second.both_sampled_potential = srciter->second.sampled_potential;
[e2925fd]247 destiter->second.nuclei_long = srciter->second.nuclei_long;
[e355b31]248 }
[e2925fd]249 }
[4f056e]250 }
[a2295a]251
[b6b21a]252 if (params.DoPrintDebug.get()) {
253 std::map<JobId_t, std::string> debugData;
254 {
255 if (!full_sample.empty()) {
256 // create debug jobs for each level to print the summed-up potential to vtk files
257 VMGDebugGridFragmentController debugcontroller(io_service);
258 debugcontroller.setHost(params.host.get());
259 debugcontroller.setPort(params.port.get());
260 debugcontroller.requestIds(full_sample.size());
[26b4d62]261 if (!debugcontroller.createDebugJobs(full_sample, OpenBoundaryConditions)) {
262 STATUS("Could not create debug jobs.");
[b6b21a]263 return Action::failure;
[26b4d62]264 }
[b6b21a]265 debugcontroller.waitforResults(full_sample.size());
266 debugcontroller.getResults(debugData);
267 Exitflag += debugcontroller.getExitflag();
268 }
[a2295a]269 }
[2764e0]270 }
[a3427f]271 container.addFullResults(keysets, forcekeysets, shortrangedata, longrangedata);
272 } else {
273 container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
[69c733]274 }
[a3427f]275#else
276 container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
[69c733]277#endif
[edecba]278
[bae7bc]279 // now clear all present jobs as we are done
280 FragmentJobQueue::getInstance().clear();
281
[55e1bc]282 // if file is given, advise results container to store to file
283 if (!params.resultsfile.get().empty()) {
284 boost::filesystem::path resultsfile = params.resultsfile.get();
285 std::ofstream returnstream(resultsfile.string().c_str());
286 if (returnstream.good()) {
287 boost::archive::text_oarchive oa(returnstream);
288 oa << container;
289 }
290 Exitflag += (int)(!returnstream.good());
291 returnstream.close();
292 }
293
[26b4d62]294 if (Exitflag != 0)
295 STATUS("Controller has returned failure.");
[edecba]296 return (Exitflag == 0) ? Action::success : Action::failure;
297}
298
[b5b01e]299ActionState::ptr FragmentationFragmentationAutomationAction::performUndo(ActionState::ptr _state) {
[edecba]300 return Action::success;
301}
302
[b5b01e]303ActionState::ptr FragmentationFragmentationAutomationAction::performRedo(ActionState::ptr _state){
[edecba]304 return Action::success;
305}
306
307bool FragmentationFragmentationAutomationAction::canUndo() {
308 return false;
309}
310
311bool FragmentationFragmentationAutomationAction::shouldUndo() {
312 return false;
313}
314/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.