source: src/Actions/FragmentationAction/FragmentationAutomationAction.cpp@ 786d28

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 786d28 was 1dfe00, checked in by Frederik Heber <heber@…>, 11 years ago

Added inclusion switch on HAVE_JOBMARKET for files depending on JobMarket.

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