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

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 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 e5f61ba was d8821e, checked in by Frederik Heber <heber@…>, 11 years ago

Merge branch 'Fragmentation_Automation_wo_JobMarket' into stable

Conflicts:

src/Actions/ActionQueue.cpp
src/Actions/FragmentationAction/FragmentationAction.cpp
src/Actions/PotentialAction/FitParticleChargesAction.cpp

  • Property mode set to 100644
File size: 13.0 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
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/>.
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
42//// include headers that implement a archive in simple text format
43#include <boost/archive/text_oarchive.hpp>
44#include <boost/archive/text_iarchive.hpp>
45
46//
47//#include <boost/mpl/remove.hpp>
48//#include <boost/lambda/lambda.hpp>
49
50//#include <iostream>
51
52#include "CodePatterns/Assert.hpp"
53#include "CodePatterns/Info.hpp"
54#include "CodePatterns/Log.hpp"
55
56#ifdef HAVE_JOBMARKET
57#include "JobMarket/Jobs/FragmentJob.hpp"
58#else
59#include "Jobs/JobMarket/FragmentJob.hpp"
60#endif
61
62#include "Fragmentation/Automation/FragmentJobQueue.hpp"
63#ifdef HAVE_JOBMARKET
64#include "Fragmentation/Automation/MPQCFragmentController.hpp"
65#else
66#include "Fragmentation/Automation/MPQCCommandFragmentController.hpp"
67#endif
68#include "Fragmentation/Summation/Containers/FragmentationChargeDensity.hpp"
69#include "Fragmentation/Summation/Containers/FragmentationLongRangeResults.hpp"
70#include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
71#include "Fragmentation/Summation/Containers/FragmentationShortRangeResults.hpp"
72#include "Fragmentation/Summation/Containers/MPQCData.hpp"
73#include "Fragmentation/KeySetsContainer.hpp"
74#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
75#include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp"
76#include "Fragmentation/Automation/VMGFragmentController.hpp"
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"
81#endif
82#include "World.hpp"
83
84#include <boost/bind.hpp>
85#include <boost/thread.hpp>
86#include <fstream>
87#include <iostream>
88#include <string>
89#include <vector>
90
91#include <boost/mpl/for_each.hpp>
92
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
110#ifdef HAVE_JOBMARKET
111static void updateSteps(Process &p, const size_t step, const size_t total)
112{
113 LOG(1, "There are " << step << " steps out of " << total << " done.");
114 p.setCurrStep(step);
115}
116#endif
117
118ActionState::ptr FragmentationFragmentationAutomationAction::performCall() {
119 boost::asio::io_service io_service;
120
121 // TODO: Have io_service run in second thread and merge with current again eventually
122
123 FragmentationResultContainer &container =
124 FragmentationResultContainer::getInstance();
125 const KeySetsContainer& keysets = FragmentJobQueue::getInstance().getKeySets();
126 const KeySetsContainer& forcekeysets = FragmentJobQueue::getInstance().getFullKeySets();
127
128 size_t Exitflag = 0;
129 std::map<JobId_t, MPQCData> shortrangedata;
130#ifdef HAVE_JOBMARKET
131 {
132 const size_t NumberJobs = FragmentJobQueue::getInstance().size();
133 MPQCFragmentController mpqccontroller(io_service);
134 mpqccontroller.setHost(params.host.get());
135 mpqccontroller.setPort(params.port.get());
136 // Phase One: obtain ids
137 mpqccontroller.requestIds(NumberJobs);
138
139 // Phase Two: add MPQCJobs and send
140 const bool AddJobsStatus = mpqccontroller.addJobsFromQueue(
141 params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
142 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly
143 );
144 if (AddJobsStatus)
145 LOG(1, "INFO: Added jobs from FragmentJobsQueue.");
146 else {
147 ELOG(1, "Adding jobs failed.");
148 return Action::failure;
149 }
150 mpqccontroller.run();
151
152 // Phase Three: calculate result
153 setMaxSteps(NumberJobs);
154 mpqccontroller.setUpdateHandler(
155 boost::bind(&updateSteps, boost::ref(*this), _1, _2)
156 );
157 start();
158 boost::thread wait_thread(
159 boost::bind(&MPQCFragmentController::waitforResults, boost::ref(mpqccontroller), boost::cref(NumberJobs))
160 );
161 wait_thread.join();
162 stop();
163 mpqccontroller.getResults(shortrangedata);
164
165 Exitflag += mpqccontroller.getExitflag();
166 }
167#else
168 {
169 const size_t NumberJobs = FragmentJobQueue::getInstance().size();
170 MPQCCommandFragmentController mpqccontroller;
171 // Phase One: obtain ids: not needed, we have infinite pool
172
173 // Phase Two: add MPQCJobs and send
174 const size_t NoJobs = mpqccontroller.addJobsFromQueue(
175 params.DoLongrange.get() ? MPQCData::DoSampleDensity : MPQCData::DontSampleDensity,
176 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
177 params.executable.get().string()
178 );
179 LOG(1, "INFO: Added " << NoJobs << " from FragmentJobsQueue.");
180 mpqccontroller.run();
181
182 // get back the results and place them in shortrangedata
183 mpqccontroller.getResults(shortrangedata);
184 ASSERT( shortrangedata.size() == NumberJobs,
185 "FragmentationFragmentationAutomationAction::performCall() - number of converted results "
186 +toString(shortrangedata.size())+" and number of jobs "+toString(NumberJobs)+ " differ.");
187
188 Exitflag += mpqccontroller.getExitflag();
189 }
190#endif
191
192#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
193 if (params.DoLongrange.get()) {
194 if ( World::getInstance().getAllAtoms().size() == 0) {
195 STATUS("Please load the full molecule into the world before starting this action.");
196 return Action::failure;
197 }
198
199 // obtain combined charge density
200 FragmentationChargeDensity summedChargeDensity(
201 shortrangedata,
202 FragmentJobQueue::getInstance().getKeySets());
203 const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
204 LOG(1, "INFO: There are " << shortrangedata.size() << " short-range and "
205 << full_sample.size() << " level-wise long-range jobs.");
206
207 // check boundary conditions
208 const BoundaryConditions::Conditions_t &conditions =
209 World::getInstance().getDomain().getConditions();
210 const bool OpenBoundaryConditions =
211 !((conditions[0] == BoundaryConditions::Wrap) &&
212 (conditions[1] == BoundaryConditions::Wrap) &&
213 (conditions[2] == BoundaryConditions::Wrap));
214 LOG(1, std::string("INFO: Using ")
215 << (OpenBoundaryConditions ? "open" : "periodic")
216 << " boundary conditions.");
217
218 // Phase Four: obtain more ids
219 std::map<JobId_t, VMGData> longrangedata;
220 {
221 VMGFragmentController vmgcontroller(io_service);
222 vmgcontroller.setHost(params.host.get());
223 vmgcontroller.setPort(params.port.get());
224 const size_t NoJobs = shortrangedata.size()+full_sample.size();
225 vmgcontroller.requestIds(2*NoJobs);
226
227 // Phase Five a: create VMGJobs for electronic charge distribution
228 const size_t near_field_cells = params.near_field_cells.get();
229 const size_t interpolation_degree = params.interpolation_degree.get();
230 if (!vmgcontroller.createLongRangeJobs(
231 shortrangedata,
232 full_sample,
233 near_field_cells,
234 interpolation_degree,
235 VMGFragmentController::DontSampleParticles,
236 VMGFragmentController::DoTreatGrid,
237 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
238 params.DoPrintDebug.get(),
239 OpenBoundaryConditions)) {
240 STATUS("Could not create long-range jobs for electronic charge distribution.");
241 return Action::failure;
242 }
243
244 // Phase Six a: calculate result
245 vmgcontroller.waitforResults(NoJobs);
246 vmgcontroller.getResults(longrangedata);
247 ASSERT( NoJobs == longrangedata.size(),
248 "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
249 +toString(full_sample.size())+"="+toString(NoJobs)
250 +" and first VMGresults "+toString(longrangedata.size())+" don't match.");
251 Exitflag += vmgcontroller.getExitflag();
252
253 {
254 std::map<JobId_t, VMGData> longrangedata_both;
255 // Phase Five b: create VMGJobs for nuclei charge distributions
256 const size_t near_field_cells = params.near_field_cells.get();
257 const size_t interpolation_degree = params.interpolation_degree.get();
258 if (!vmgcontroller.createLongRangeJobs(
259 shortrangedata,
260 full_sample,
261 near_field_cells,
262 interpolation_degree,
263 VMGFragmentController::DoSampleParticles,
264 VMGFragmentController::DoTreatGrid,
265 params.DoValenceOnly.get() ? MPQCData::DoSampleValenceOnly : MPQCData::DontSampleValenceOnly,
266 params.DoPrintDebug.get(),
267 OpenBoundaryConditions)) {
268 STATUS("Could not create long-range jobs for nuclei charge distribution.");
269 return Action::failure;
270 }
271
272 // Phase Six b: calculate result
273 vmgcontroller.waitforResults(NoJobs);
274 vmgcontroller.getResults(longrangedata_both);
275 ASSERT( NoJobs == longrangedata_both.size(),
276 "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
277 +toString(full_sample.size())+"="+toString(NoJobs)
278 +" and second VMGresults "+toString(longrangedata_both.size())+" don't match.");
279 Exitflag += vmgcontroller.getExitflag();
280
281 // go through either data and replace nuclei_long with contribution from both
282 ASSERT( longrangedata.size() == longrangedata_both.size(),
283 "FragmentationFragmentationAutomationAction::performCall() - longrange results have different sizes.");
284 std::map<JobId_t, VMGData>::iterator destiter = longrangedata.begin();
285 std::map<JobId_t, VMGData>::const_iterator srciter = longrangedata_both.begin();
286 for (;destiter != longrangedata.end(); ++srciter, ++destiter) {
287 destiter->second.both_sampled_potential = srciter->second.sampled_potential;
288 destiter->second.nuclei_long = srciter->second.nuclei_long;
289 }
290 }
291 }
292
293 if (params.DoPrintDebug.get()) {
294 std::map<JobId_t, std::string> debugData;
295 {
296 if (!full_sample.empty()) {
297 // create debug jobs for each level to print the summed-up potential to vtk files
298 VMGDebugGridFragmentController debugcontroller(io_service);
299 debugcontroller.setHost(params.host.get());
300 debugcontroller.setPort(params.port.get());
301 debugcontroller.requestIds(full_sample.size());
302 if (!debugcontroller.createDebugJobs(full_sample, OpenBoundaryConditions)) {
303 STATUS("Could not create debug jobs.");
304 return Action::failure;
305 }
306 debugcontroller.waitforResults(full_sample.size());
307 debugcontroller.getResults(debugData);
308 Exitflag += debugcontroller.getExitflag();
309 }
310 }
311 }
312 container.addFullResults(keysets, forcekeysets, shortrangedata, longrangedata);
313 } else {
314 container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
315 }
316#else
317 container.addShortRangeResults(keysets, forcekeysets, shortrangedata);
318#endif
319
320 // now clear all present jobs as we are done
321 FragmentJobQueue::getInstance().clear();
322
323 // if file is given, advise results container to store to file
324 if (!params.resultsfile.get().empty()) {
325 boost::filesystem::path resultsfile = params.resultsfile.get();
326 std::ofstream returnstream(resultsfile.string().c_str());
327 if (returnstream.good()) {
328 boost::archive::text_oarchive oa(returnstream);
329 oa << container;
330 }
331 Exitflag += (int)(!returnstream.good());
332 returnstream.close();
333 }
334
335 if (Exitflag != 0)
336 STATUS("Controller has returned failure.");
337 return (Exitflag == 0) ? Action::success : Action::failure;
338}
339
340ActionState::ptr FragmentationFragmentationAutomationAction::performUndo(ActionState::ptr _state) {
341 return Action::success;
342}
343
344ActionState::ptr FragmentationFragmentationAutomationAction::performRedo(ActionState::ptr _state){
345 return Action::success;
346}
347
348bool FragmentationFragmentationAutomationAction::canUndo() {
349 return false;
350}
351
352bool FragmentationFragmentationAutomationAction::shouldUndo() {
353 return false;
354}
355/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.