1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * This file is part of MoleCuilder.
|
---|
8 | *
|
---|
9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * FragmentationAutomationAction.cpp
|
---|
25 | *
|
---|
26 | * Created on: May 18, 2012
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include <boost/archive/text_iarchive.hpp>
|
---|
36 | // boost asio needs specific operator new
|
---|
37 | #include <boost/asio.hpp>
|
---|
38 |
|
---|
39 | #include "CodePatterns/MemDebug.hpp"
|
---|
40 |
|
---|
41 | #include <boost/assign.hpp>
|
---|
42 |
|
---|
43 | #include "CodePatterns/Assert.hpp"
|
---|
44 | #include "CodePatterns/Info.hpp"
|
---|
45 | #include "CodePatterns/Log.hpp"
|
---|
46 | #include "JobMarket/Controller/FragmentController.hpp"
|
---|
47 | #include "JobMarket/Jobs/FragmentJob.hpp"
|
---|
48 |
|
---|
49 | #include "Atom/atom.hpp"
|
---|
50 | #include "Fragmentation/EnergyMatrix.hpp"
|
---|
51 | #include "Fragmentation/ForceMatrix.hpp"
|
---|
52 | #include "Fragmentation/Fragmentation.hpp"
|
---|
53 | #include "Fragmentation/SetValues/Histogram.hpp"
|
---|
54 | #include "Fragmentation/SetValues/IndexedVectors.hpp"
|
---|
55 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
|
---|
56 | #include "Fragmentation/KeySet.hpp"
|
---|
57 | #include "Fragmentation/KeySetsContainer.hpp"
|
---|
58 | #include "Fragmentation/Summation/printSum.hpp"
|
---|
59 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
---|
60 | #include "Jobs/MPQCJob.hpp"
|
---|
61 | #include "Jobs/MPQCData.hpp"
|
---|
62 | #include "Jobs/MPQCData_printKeyNames.hpp"
|
---|
63 | #include "molecule.hpp"
|
---|
64 | #include "World.hpp"
|
---|
65 |
|
---|
66 | #include <iostream>
|
---|
67 | #include <string>
|
---|
68 | #include <vector>
|
---|
69 |
|
---|
70 | #include <boost/mpl/for_each.hpp>
|
---|
71 |
|
---|
72 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
|
---|
73 |
|
---|
74 | using namespace MoleCuilder;
|
---|
75 |
|
---|
76 | using namespace boost::assign;
|
---|
77 |
|
---|
78 | // and construct the stuff
|
---|
79 | #include "FragmentationAutomationAction.def"
|
---|
80 | #include "Action_impl_pre.hpp"
|
---|
81 | /** =========== define the function ====================== */
|
---|
82 |
|
---|
83 | class controller_AddOn;
|
---|
84 |
|
---|
85 | // needs to be defined for using the FragmentController
|
---|
86 | controller_AddOn *getAddOn()
|
---|
87 | {
|
---|
88 | return NULL;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Creates a MPQCCommandJob with argument \a filename.
|
---|
92 | *
|
---|
93 | * @param jobs created job is added to this vector
|
---|
94 | * @param command mpqc command to execute
|
---|
95 | * @param filename filename being argument to job
|
---|
96 | * @param nextid id for this job
|
---|
97 | */
|
---|
98 | void parsejob(
|
---|
99 | std::vector<FragmentJob::ptr> &jobs,
|
---|
100 | const std::string &command,
|
---|
101 | const std::string &filename,
|
---|
102 | const JobId_t nextid)
|
---|
103 | {
|
---|
104 | std::ifstream file;
|
---|
105 | file.open(filename.c_str());
|
---|
106 | ASSERT( file.good(), "parsejob() - file "+filename+" does not exist.");
|
---|
107 | std::string output((std::istreambuf_iterator<char>(file)),
|
---|
108 | std::istreambuf_iterator<char>());
|
---|
109 | FragmentJob::ptr testJob( new MPQCJob(nextid, output) );
|
---|
110 | jobs.push_back(testJob);
|
---|
111 | file.close();
|
---|
112 | LOG(1, "INFO: Added MPQCCommandJob from file "+filename+".");
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** Helper function to get number of atoms somehow.
|
---|
116 | *
|
---|
117 | * Here, we just parse the number of lines in the adjacency file as
|
---|
118 | * it should correspond to the number of atoms, except when some atoms
|
---|
119 | * are not bonded, but then fragmentation makes no sense.
|
---|
120 | *
|
---|
121 | * @param path path to the adjacency file
|
---|
122 | */
|
---|
123 | size_t getNoAtomsFromAdjacencyFile(const std::string &path)
|
---|
124 | {
|
---|
125 | size_t NoAtoms = 0;
|
---|
126 |
|
---|
127 | // parse in special file to get atom count (from line count)
|
---|
128 | std::string filename(path);
|
---|
129 | filename += FRAGMENTPREFIX;
|
---|
130 | filename += ADJACENCYFILE;
|
---|
131 | std::ifstream adjacency(filename.c_str());
|
---|
132 | if (adjacency.fail()) {
|
---|
133 | LOG(0, endl << "getNoAtomsFromAdjacencyFile() - Unable to open " << filename << ", is the directory correct?");
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | std::string buffer;
|
---|
137 | while (getline(adjacency, buffer))
|
---|
138 | NoAtoms++;
|
---|
139 | LOG(1, "INFO: There are " << NoAtoms << " atoms.");
|
---|
140 |
|
---|
141 | return NoAtoms;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /** Print MPQCData from received results.
|
---|
146 | *
|
---|
147 | * @param results received results to extract MPQCData from
|
---|
148 | * @param KeySetFilename filename with keysets to associate forces correctly
|
---|
149 | * @param NoAtoms total number of atoms
|
---|
150 | */
|
---|
151 | bool printReceivedMPQCResults(
|
---|
152 | const std::vector<FragmentResult::ptr> &results,
|
---|
153 | const std::string &KeySetFilename,
|
---|
154 | size_t NoAtoms)
|
---|
155 | {
|
---|
156 | EnergyMatrix Energy;
|
---|
157 | EnergyMatrix EnergyFragments;
|
---|
158 | ForceMatrix Force;
|
---|
159 | ForceMatrix ForceFragments;
|
---|
160 |
|
---|
161 | // align fragments
|
---|
162 | std::map< JobId_t, size_t > MatrixNrLookup;
|
---|
163 | size_t FragmentCounter = 0;
|
---|
164 | {
|
---|
165 | // bring ids in order ...
|
---|
166 | typedef std::map< JobId_t, FragmentResult::ptr> IdResultMap_t;
|
---|
167 | IdResultMap_t IdResultMap;
|
---|
168 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
---|
169 | iter != results.end(); ++iter) {
|
---|
170 | #ifndef NDEBUG
|
---|
171 | std::pair< IdResultMap_t::iterator, bool> inserter =
|
---|
172 | #endif
|
---|
173 | IdResultMap.insert( make_pair((*iter)->getId(), *iter) );
|
---|
174 | ASSERT( inserter.second,
|
---|
175 | "printReceivedMPQCResults() - two results have same id "
|
---|
176 | +toString((*iter)->getId())+".");
|
---|
177 | }
|
---|
178 | // ... and fill lookup
|
---|
179 | for(IdResultMap_t::const_iterator iter = IdResultMap.begin();
|
---|
180 | iter != IdResultMap.end(); ++iter)
|
---|
181 | MatrixNrLookup.insert( make_pair(iter->first, FragmentCounter++) );
|
---|
182 | }
|
---|
183 | LOG(1, "INFO: There are " << FragmentCounter << " fragments.");
|
---|
184 |
|
---|
185 | // extract results
|
---|
186 | std::vector<MPQCData> fragmentData(results.size());
|
---|
187 | MPQCData combinedData;
|
---|
188 |
|
---|
189 | LOG(2, "DEBUG: Parsing now through " << results.size() << " results.");
|
---|
190 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
---|
191 | iter != results.end(); ++iter) {
|
---|
192 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
|
---|
193 | MPQCData extractedData;
|
---|
194 | std::stringstream inputstream((*iter)->result);
|
---|
195 | LOG(2, "DEBUG: First 50 characters FragmentResult's string: "+(*iter)->result.substr(0, 50));
|
---|
196 | boost::archive::text_iarchive ia(inputstream);
|
---|
197 | ia >> extractedData;
|
---|
198 | LOG(1, "INFO: extracted data is " << extractedData << ".");
|
---|
199 |
|
---|
200 | // place results into EnergyMatrix ...
|
---|
201 | {
|
---|
202 | MatrixContainer::MatrixArray matrix;
|
---|
203 | matrix.resize(1);
|
---|
204 | matrix[0].resize(1, extractedData.energies.total);
|
---|
205 | if (!Energy.AddMatrix(
|
---|
206 | std::string("MPQCJob ")+toString((*iter)->getId()),
|
---|
207 | matrix,
|
---|
208 | MatrixNrLookup[(*iter)->getId()])) {
|
---|
209 | ELOG(1, "Adding energy matrix failed.");
|
---|
210 | return false;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | // ... and ForceMatrix (with two empty columns in front)
|
---|
214 | {
|
---|
215 | MatrixContainer::MatrixArray matrix;
|
---|
216 | const size_t rows = extractedData.forces.size();
|
---|
217 | matrix.resize(rows);
|
---|
218 | for (size_t i=0;i<rows;++i) {
|
---|
219 | const size_t columns = 2+extractedData.forces[i].size();
|
---|
220 | matrix[i].resize(columns, 0.);
|
---|
221 | // for (size_t j=0;j<2;++j)
|
---|
222 | // matrix[i][j] = 0.;
|
---|
223 | for (size_t j=2;j<columns;++j)
|
---|
224 | matrix[i][j] = extractedData.forces[i][j-2];
|
---|
225 | }
|
---|
226 | if (!Force.AddMatrix(
|
---|
227 | std::string("MPQCJob ")+toString((*iter)->getId()),
|
---|
228 | matrix,
|
---|
229 | MatrixNrLookup[(*iter)->getId()])) {
|
---|
230 | ELOG(1, "Adding force matrix failed.");
|
---|
231 | return false;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|
235 | // add one more matrix (not required for energy)
|
---|
236 | MatrixContainer::MatrixArray matrix;
|
---|
237 | matrix.resize(1);
|
---|
238 | matrix[0].resize(1, 0.);
|
---|
239 | if (!Energy.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
---|
240 | return false;
|
---|
241 | // but for energy because we need to know total number of atoms
|
---|
242 | matrix.resize(NoAtoms);
|
---|
243 | for (size_t i = 0; i< NoAtoms; ++i)
|
---|
244 | matrix[i].resize(2+NDIM, 0.);
|
---|
245 | if (!Force.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
---|
246 | return false;
|
---|
247 |
|
---|
248 | // initialise indices
|
---|
249 | KeySetsContainer KeySet;
|
---|
250 | if (!Energy.InitialiseIndices()) return false;
|
---|
251 |
|
---|
252 | if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
|
---|
253 |
|
---|
254 | if (!KeySet.ParseKeySets(KeySetFilename.c_str(), Force.RowCounter, Force.MatrixCounter)) return false;
|
---|
255 |
|
---|
256 | /// prepare for OrthogonalSummation
|
---|
257 |
|
---|
258 | // gather all present indices in AllIndices
|
---|
259 | IndexSet::ptr AllIndices(new IndexSet);
|
---|
260 | for (KeySetsContainer::ArrayOfIntVectors::const_iterator iter = KeySet.KeySets.begin();
|
---|
261 | iter != KeySet.KeySets.end(); ++iter)
|
---|
262 | for(KeySetsContainer::IntVector::const_iterator keyiter = (*iter).begin();
|
---|
263 | keyiter != (*iter).end(); ++keyiter) {
|
---|
264 | if (*keyiter != -1)
|
---|
265 | (*AllIndices) += *keyiter;
|
---|
266 | }
|
---|
267 | LOG(1, "INFO: AllIndices is " << AllIndices << ".");
|
---|
268 | // create container with all keysets
|
---|
269 | IndexSetContainer::ptr container(new IndexSetContainer(AllIndices));
|
---|
270 | for (KeySetsContainer::ArrayOfIntVectors::const_iterator iter = KeySet.KeySets.begin();
|
---|
271 | iter != KeySet.KeySets.end(); ++iter) {
|
---|
272 | IndexSet tempset;
|
---|
273 | for(KeySetsContainer::IntVector::const_iterator keyiter = (*iter).begin();
|
---|
274 | keyiter != (*iter).end(); ++keyiter)
|
---|
275 | if (*keyiter != -1)
|
---|
276 | tempset += *keyiter;
|
---|
277 | container->insert(tempset);
|
---|
278 | }
|
---|
279 | // create the map of all keysets
|
---|
280 | SubsetMap::ptr subsetmap(new SubsetMap(*container));
|
---|
281 |
|
---|
282 | /// convert all MPQCData to MPQCDataMap_t
|
---|
283 | {
|
---|
284 | // energy_t
|
---|
285 | std::vector<MPQCDataEnergyMap_t> MPQCData_Energy_fused;
|
---|
286 | MPQCData_Energy_fused.reserve(fragmentData.size());
|
---|
287 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
288 | dataiter != fragmentData.end(); ++dataiter) {
|
---|
289 | const MPQCData &extractedData = *dataiter;
|
---|
290 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
---|
291 | MPQCDataEnergyMap_t instance;
|
---|
292 | boost::fusion::at_key<MPQCDataFused::energy_total>(instance) = extractedData.energies.total;
|
---|
293 | boost::fusion::at_key<MPQCDataFused::energy_nuclear_repulsion>(instance) = extractedData.energies.nuclear_repulsion;
|
---|
294 | boost::fusion::at_key<MPQCDataFused::energy_electron_coulomb>(instance) = extractedData.energies.electron_coulomb;
|
---|
295 | boost::fusion::at_key<MPQCDataFused::energy_electron_exchange>(instance) = extractedData.energies.electron_exchange;
|
---|
296 | boost::fusion::at_key<MPQCDataFused::energy_correlation>(instance) = extractedData.energies.correlation;
|
---|
297 | boost::fusion::at_key<MPQCDataFused::energy_overlap>(instance) = extractedData.energies.overlap;
|
---|
298 | boost::fusion::at_key<MPQCDataFused::energy_kinetic>(instance) = extractedData.energies.kinetic;
|
---|
299 | boost::fusion::at_key<MPQCDataFused::energy_hcore>(instance) = extractedData.energies.hcore;
|
---|
300 | boost::fusion::at_key<MPQCDataFused::energy_eigenvalues>(instance) = extractedData.energies.eigenvalues;
|
---|
301 | MPQCData_Energy_fused.push_back(instance);
|
---|
302 | }
|
---|
303 |
|
---|
304 | // forces
|
---|
305 | const IndexSetContainer::Container_t &indices = container->getContainer();
|
---|
306 | ASSERT( indices.size() == fragmentData.size(),
|
---|
307 | "FragmentationAutomationAction::performCall() - indices and fragmentData differ in size.");
|
---|
308 | std::vector<MPQCDataForceMap_t> MPQCData_Force_fused;
|
---|
309 | MPQCData_Force_fused.reserve(fragmentData.size());
|
---|
310 | std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
311 | IndexSetContainer::Container_t::const_iterator indexiter = indices.begin();
|
---|
312 | for(;dataiter != fragmentData.end(); ++dataiter, ++indexiter) {
|
---|
313 | const MPQCData &extractedData = *dataiter;
|
---|
314 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
---|
315 | MPQCDataForceMap_t instance;
|
---|
316 | boost::fusion::at_key<MPQCDataFused::forces>(instance) =
|
---|
317 | IndexedVectors(**indexiter, extractedData.forces);
|
---|
318 | MPQCData_Force_fused.push_back(instance);
|
---|
319 | }
|
---|
320 |
|
---|
321 | // times
|
---|
322 | std::vector<MPQCDataTimeMap_t> MPQCData_Time_fused;
|
---|
323 | MPQCData_Time_fused.reserve(fragmentData.size());
|
---|
324 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
325 | dataiter != fragmentData.end(); ++dataiter) {
|
---|
326 | const MPQCData &extractedData = *dataiter;
|
---|
327 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
---|
328 | MPQCDataTimeMap_t instance;
|
---|
329 | boost::fusion::at_key<MPQCDataFused::times_walltime>(instance) = extractedData.times.walltime;
|
---|
330 | boost::fusion::at_key<MPQCDataFused::times_cputime>(instance) = extractedData.times.cputime;
|
---|
331 | boost::fusion::at_key<MPQCDataFused::times_flops>(instance) = extractedData.times.flops;
|
---|
332 | MPQCData_Time_fused.push_back(instance);
|
---|
333 | }
|
---|
334 |
|
---|
335 | // create a vector of all job ids
|
---|
336 | std::vector<JobId_t> jobids(results.size(), JobId::IllegalJob);
|
---|
337 | std::transform(results.begin(), results.end(), jobids.begin(),
|
---|
338 | boost::bind(&FragmentResult::getId,
|
---|
339 | boost::bind(&FragmentResult::ptr::operator->, _1)));
|
---|
340 |
|
---|
341 | // sum up and print energies
|
---|
342 | boost::mpl::for_each<MPQCDataEnergyVector_t>(
|
---|
343 | printSum<MPQCDataEnergyMap_t>(
|
---|
344 | subsetmap,
|
---|
345 | MPQCData_Energy_fused,
|
---|
346 | jobids,
|
---|
347 | container->getContainer(),
|
---|
348 | MatrixNrLookup)
|
---|
349 | );
|
---|
350 |
|
---|
351 | // sum up and print forces
|
---|
352 | boost::mpl::for_each<MPQCDataForceVector_t>(
|
---|
353 | printSum<MPQCDataForceMap_t>(
|
---|
354 | subsetmap,
|
---|
355 | MPQCData_Force_fused,
|
---|
356 | jobids,
|
---|
357 | container->getContainer(),
|
---|
358 | MatrixNrLookup)
|
---|
359 | );
|
---|
360 |
|
---|
361 | // TODO: Times should just be summed not orthogonalized
|
---|
362 | // // sum up and print times
|
---|
363 | // boost::mpl::for_each<MPQCDataTimeVector_t>(
|
---|
364 | // printSum<MPQCDataTimeMap_t>(
|
---|
365 | // subsetmap,
|
---|
366 | // MPQCData_Time_fused,
|
---|
367 | // jobids,
|
---|
368 | // container->getContainer(),
|
---|
369 | // MatrixNrLookup)
|
---|
370 | // );
|
---|
371 | }
|
---|
372 |
|
---|
373 | // combine all found data
|
---|
374 | if (!KeySet.ParseManyBodyTerms()) return false;
|
---|
375 |
|
---|
376 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false;
|
---|
377 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false;
|
---|
378 |
|
---|
379 | if(!Energy.SetLastMatrix(0., 0)) return false;
|
---|
380 | if(!Force.SetLastMatrix(0., 2)) return false;
|
---|
381 |
|
---|
382 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
|
---|
383 | // --------- sum up energy --------------------
|
---|
384 | LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
|
---|
385 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
|
---|
386 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
|
---|
387 |
|
---|
388 | // --------- sum up Forces --------------------
|
---|
389 | LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
|
---|
390 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
|
---|
391 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
|
---|
392 | }
|
---|
393 |
|
---|
394 | // for debugging print resulting energy and forces
|
---|
395 | LOG(1, "INFO: Resulting energy is " << Energy.Matrix[ FragmentCounter ][0][0]);
|
---|
396 | std::stringstream output;
|
---|
397 | for (int i=0; i< Force.RowCounter[FragmentCounter]; ++i) {
|
---|
398 | for (int j=0; j< Force.ColumnCounter[FragmentCounter]; ++j)
|
---|
399 | output << Force.Matrix[ FragmentCounter ][i][j] << " ";
|
---|
400 | output << "\n";
|
---|
401 | }
|
---|
402 | LOG(1, "INFO: Resulting forces are " << std::endl << output.str());
|
---|
403 |
|
---|
404 | return true;
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | void RunService(
|
---|
409 | boost::asio::io_service &io_service,
|
---|
410 | std::string message)
|
---|
411 | {
|
---|
412 | message = std::string("io_service: ") + message;
|
---|
413 | io_service.reset();
|
---|
414 | Info info(message.c_str());
|
---|
415 | io_service.run();
|
---|
416 | }
|
---|
417 |
|
---|
418 | void requestIds(
|
---|
419 | FragmentController &controller,
|
---|
420 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
---|
421 | const size_t numberjobs)
|
---|
422 | {
|
---|
423 | controller.requestIds(params.host.get(), params.port.get(), numberjobs);
|
---|
424 | }
|
---|
425 |
|
---|
426 | bool createJobsFromFiles(
|
---|
427 | FragmentController &controller,
|
---|
428 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
---|
429 | const std::vector< boost::filesystem::path > &jobfiles)
|
---|
430 | {
|
---|
431 | std::vector<FragmentJob::ptr> jobs;
|
---|
432 | for (std::vector< boost::filesystem::path >::const_iterator iter = jobfiles.begin();
|
---|
433 | iter != jobfiles .end(); ++iter) {
|
---|
434 | const std::string &filename = (*iter).string();
|
---|
435 | if (boost::filesystem::exists(filename)) {
|
---|
436 | const JobId_t next_id = controller.getAvailableId();
|
---|
437 | LOG(1, "INFO: Creating MPQCCommandJob with filename'"
|
---|
438 | +filename+"', and id "+toString(next_id)+".");
|
---|
439 | parsejob(jobs, params.executable.get().string(), filename, next_id);
|
---|
440 | } else {
|
---|
441 | ELOG(1, "Fragment job "+filename+" does not exist.");
|
---|
442 | return false;
|
---|
443 | }
|
---|
444 | }
|
---|
445 | controller.addJobs(jobs);
|
---|
446 | controller.sendJobs(params.host.get(), params.port.get());
|
---|
447 | return true;
|
---|
448 | }
|
---|
449 |
|
---|
450 | void WaitforResults(
|
---|
451 | boost::asio::io_service &io_service,
|
---|
452 | FragmentController &controller,
|
---|
453 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
---|
454 | const size_t NoExpectedResults
|
---|
455 | )
|
---|
456 | {
|
---|
457 | size_t NoCalculatedResults = 0;
|
---|
458 | while (NoCalculatedResults != NoExpectedResults) {
|
---|
459 | // wait a bit
|
---|
460 | boost::asio::deadline_timer timer(io_service);
|
---|
461 | timer.expires_from_now(boost::posix_time::milliseconds(500));
|
---|
462 | timer.wait();
|
---|
463 | // then request status
|
---|
464 | controller.checkResults(params.host.get(), params.port.get());
|
---|
465 | RunService(io_service, "Checking on results");
|
---|
466 |
|
---|
467 | const std::pair<size_t, size_t> JobStatus = controller.getJobStatus();
|
---|
468 | LOG(1, "INFO: #" << JobStatus.first << " are waiting in the queue and #" << JobStatus.second << " jobs are calculated so far.");
|
---|
469 | NoCalculatedResults = JobStatus.second;
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | Action::state_ptr FragmentationFragmentationAutomationAction::performCall() {
|
---|
475 | boost::asio::io_service io_service;
|
---|
476 | FragmentController controller(io_service);
|
---|
477 |
|
---|
478 | // TODO: Have io_service run in second thread and merge with current again eventually
|
---|
479 |
|
---|
480 | // Phase One: obtain ids
|
---|
481 | std::vector< boost::filesystem::path > jobfiles = params.jobfiles.get();
|
---|
482 | requestIds(controller, params, jobfiles.size());
|
---|
483 | RunService(io_service, "Requesting ids");
|
---|
484 |
|
---|
485 | // Phase Two: create and add jobs
|
---|
486 | if (!createJobsFromFiles(controller, params, jobfiles))
|
---|
487 | return Action::failure;
|
---|
488 | RunService(io_service, "Adding jobs");
|
---|
489 |
|
---|
490 | // Phase Three: calculate result
|
---|
491 | WaitforResults(io_service, controller, params, jobfiles.size());
|
---|
492 |
|
---|
493 | // Phase Three: get result
|
---|
494 | controller.receiveResults(params.host.get(), params.port.get());
|
---|
495 | RunService(io_service, "Phase Four");
|
---|
496 |
|
---|
497 | // Final phase: print result
|
---|
498 | {
|
---|
499 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
---|
500 | std::vector<FragmentResult::ptr> results = controller.getReceivedResults();
|
---|
501 | printReceivedMPQCResults(
|
---|
502 | results,
|
---|
503 | params.path.get(),
|
---|
504 | getNoAtomsFromAdjacencyFile(params.path.get()));
|
---|
505 | }
|
---|
506 | size_t Exitflag = controller.getExitflag();
|
---|
507 |
|
---|
508 | return (Exitflag == 0) ? Action::success : Action::failure;
|
---|
509 | }
|
---|
510 |
|
---|
511 | Action::state_ptr FragmentationFragmentationAutomationAction::performUndo(Action::state_ptr _state) {
|
---|
512 | return Action::success;
|
---|
513 | }
|
---|
514 |
|
---|
515 | Action::state_ptr FragmentationFragmentationAutomationAction::performRedo(Action::state_ptr _state){
|
---|
516 | return Action::success;
|
---|
517 | }
|
---|
518 |
|
---|
519 | bool FragmentationFragmentationAutomationAction::canUndo() {
|
---|
520 | return false;
|
---|
521 | }
|
---|
522 |
|
---|
523 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
|
---|
524 | return false;
|
---|
525 | }
|
---|
526 | /** =========== end of function ====================== */
|
---|