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