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/mpl/remove.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 "Box.hpp"
|
---|
51 | #include "Fragmentation/EnergyMatrix.hpp"
|
---|
52 | #include "Fragmentation/ForceMatrix.hpp"
|
---|
53 | #include "Fragmentation/Fragmentation.hpp"
|
---|
54 | #include "Fragmentation/SetValues/Histogram.hpp"
|
---|
55 | #include "Fragmentation/SetValues/IndexedVectors.hpp"
|
---|
56 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
|
---|
57 | #include "Fragmentation/KeySet.hpp"
|
---|
58 | #include "Fragmentation/KeySetsContainer.hpp"
|
---|
59 | #include "Fragmentation/Summation/AllLevelOrthogonalSummator.hpp"
|
---|
60 | #include "Fragmentation/Summation/AllLevelSummator.hpp"
|
---|
61 | #include "Fragmentation/Summation/writeTable.hpp"
|
---|
62 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
---|
63 | #include "Helpers/defs.hpp"
|
---|
64 | #include "Jobs/MPQCJob.hpp"
|
---|
65 | #include "Jobs/MPQCData.hpp"
|
---|
66 | #include "Jobs/MPQCData_printKeyNames.hpp"
|
---|
67 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
68 | #ifdef HAVE_VMG
|
---|
69 | #include "Jobs/VMGJob.hpp"
|
---|
70 | #endif
|
---|
71 | #include "molecule.hpp"
|
---|
72 | #include "World.hpp"
|
---|
73 |
|
---|
74 | #include <iostream>
|
---|
75 | #include <string>
|
---|
76 | #include <vector>
|
---|
77 |
|
---|
78 | #include <boost/mpl/for_each.hpp>
|
---|
79 |
|
---|
80 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
|
---|
81 |
|
---|
82 | using namespace MoleCuilder;
|
---|
83 |
|
---|
84 | // and construct the stuff
|
---|
85 | #include "FragmentationAutomationAction.def"
|
---|
86 | #include "Action_impl_pre.hpp"
|
---|
87 | /** =========== define the function ====================== */
|
---|
88 |
|
---|
89 | class controller_AddOn;
|
---|
90 |
|
---|
91 | // needs to be defined for using the FragmentController
|
---|
92 | controller_AddOn *getAddOn()
|
---|
93 | {
|
---|
94 | return NULL;
|
---|
95 | }
|
---|
96 |
|
---|
97 | const int LEVEL = 5;
|
---|
98 |
|
---|
99 | /** Creates a MPQCCommandJob with argument \a filename.
|
---|
100 | *
|
---|
101 | * @param jobs created job is added to this vector
|
---|
102 | * @param command mpqc command to execute
|
---|
103 | * @param filename filename being argument to job
|
---|
104 | * @param nextid id for this job
|
---|
105 | */
|
---|
106 | void parsejob(
|
---|
107 | std::vector<FragmentJob::ptr> &jobs,
|
---|
108 | const std::string &command,
|
---|
109 | const std::string &filename,
|
---|
110 | const JobId_t nextid)
|
---|
111 | {
|
---|
112 | std::ifstream file;
|
---|
113 | file.open(filename.c_str());
|
---|
114 | ASSERT( file.good(), "parsejob() - file "+filename+" does not exist.");
|
---|
115 | std::string output((std::istreambuf_iterator<char>(file)),
|
---|
116 | std::istreambuf_iterator<char>());
|
---|
117 | double begin[NDIM] = { 0., 0., 0. };
|
---|
118 | const RealSpaceMatrix& M = World::getInstance().getDomain().getM();
|
---|
119 | const double size = M.at(0,0);
|
---|
120 | ASSERT( M.determinant() == size*size*size,
|
---|
121 | "parsejob() - current domain matrix "+toString(M)+" is not cubic.");
|
---|
122 | const int level = LEVEL;
|
---|
123 | FragmentJob::ptr testJob( new MPQCJob(nextid, output, begin, size, level) );
|
---|
124 | jobs.push_back(testJob);
|
---|
125 | file.close();
|
---|
126 | LOG(1, "INFO: Added MPQCCommandJob from file "+filename+".");
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Helper function to get number of atoms somehow.
|
---|
130 | *
|
---|
131 | * Here, we just parse the number of lines in the adjacency file as
|
---|
132 | * it should correspond to the number of atoms, except when some atoms
|
---|
133 | * are not bonded, but then fragmentation makes no sense.
|
---|
134 | *
|
---|
135 | * @param path path to the adjacency file
|
---|
136 | */
|
---|
137 | size_t getNoAtomsFromAdjacencyFile(const std::string &path)
|
---|
138 | {
|
---|
139 | size_t NoAtoms = 0;
|
---|
140 |
|
---|
141 | // parse in special file to get atom count (from line count)
|
---|
142 | std::string filename(path);
|
---|
143 | filename += FRAGMENTPREFIX;
|
---|
144 | filename += ADJACENCYFILE;
|
---|
145 | std::ifstream adjacency(filename.c_str());
|
---|
146 | if (adjacency.fail()) {
|
---|
147 | LOG(0, endl << "getNoAtomsFromAdjacencyFile() - Unable to open " << filename << ", is the directory correct?");
|
---|
148 | return false;
|
---|
149 | }
|
---|
150 | std::string buffer;
|
---|
151 | while (getline(adjacency, buffer))
|
---|
152 | NoAtoms++;
|
---|
153 | LOG(1, "INFO: There are " << NoAtoms << " atoms.");
|
---|
154 |
|
---|
155 | return NoAtoms;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Extracts MPQCData from received vector of FragmentResults.
|
---|
159 | *
|
---|
160 | * @param results results to extract MPQCData from
|
---|
161 | * @param fragmentData on return array filled with extracted MPQCData
|
---|
162 | */
|
---|
163 | void ConvertFragmentResultToMPQCData(
|
---|
164 | const std::vector<FragmentResult::ptr> &results,
|
---|
165 | std::vector<MPQCData> &fragmentData)
|
---|
166 | {
|
---|
167 | // extract results
|
---|
168 | fragmentData.clear();
|
---|
169 | fragmentData.reserve(results.size());
|
---|
170 |
|
---|
171 | LOG(2, "DEBUG: Parsing now through " << results.size() << " results.");
|
---|
172 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
---|
173 | iter != results.end(); ++iter) {
|
---|
174 | //LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
|
---|
175 | MPQCData extractedData;
|
---|
176 | std::stringstream inputstream((*iter)->result);
|
---|
177 | LOG(2, "DEBUG: First 50 characters FragmentResult's string: "+(*iter)->result.substr(0, 50));
|
---|
178 | boost::archive::text_iarchive ia(inputstream);
|
---|
179 | ia >> extractedData;
|
---|
180 | LOG(1, "INFO: extracted data is " << extractedData << ".");
|
---|
181 | fragmentData.push_back(extractedData);
|
---|
182 | }
|
---|
183 |
|
---|
184 | ASSERT( results.size() == fragmentData.size(),
|
---|
185 | "ConvertFragmentResultToMPQCData() - the number of extracted data differs from the number of results.");
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** Creates a lookup from FragmentJob::id to the true fragment number.
|
---|
189 | *
|
---|
190 | * @param results result with job ids
|
---|
191 | * @param MatrixNrLookup Lookup up-map, filled on return
|
---|
192 | * @param FragmentCounter total number of fragments on return
|
---|
193 | */
|
---|
194 | void createMatrixNrLookup(
|
---|
195 | const std::vector<FragmentResult::ptr> &results,
|
---|
196 | std::map< JobId_t, size_t > &MatrixNrLookup,
|
---|
197 | size_t &FragmentCounter)
|
---|
198 | {
|
---|
199 | // align fragments
|
---|
200 | MatrixNrLookup.clear();
|
---|
201 | FragmentCounter = 0;
|
---|
202 | {
|
---|
203 | // bring ids in order ...
|
---|
204 | typedef std::map< JobId_t, FragmentResult::ptr> IdResultMap_t;
|
---|
205 | IdResultMap_t IdResultMap;
|
---|
206 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
---|
207 | iter != results.end(); ++iter) {
|
---|
208 | #ifndef NDEBUG
|
---|
209 | std::pair< IdResultMap_t::iterator, bool> inserter =
|
---|
210 | #endif
|
---|
211 | IdResultMap.insert( make_pair((*iter)->getId(), *iter) );
|
---|
212 | ASSERT( inserter.second,
|
---|
213 | "ExtractMPQCDataFromResults() - two results have same id "
|
---|
214 | +toString((*iter)->getId())+".");
|
---|
215 | }
|
---|
216 | // ... and fill lookup
|
---|
217 | for(IdResultMap_t::const_iterator iter = IdResultMap.begin();
|
---|
218 | iter != IdResultMap.end(); ++iter)
|
---|
219 | MatrixNrLookup.insert( make_pair(iter->first, FragmentCounter++) );
|
---|
220 | }
|
---|
221 | LOG(1, "INFO: There are " << FragmentCounter << " fragments.");
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** Place results from FragmentResult into EnergyMatrix and ForceMatrix.
|
---|
225 | *
|
---|
226 | * @param results results with ids to associate with fragment number
|
---|
227 | * @param fragmentData MPQCData resulting from the jobs
|
---|
228 | * @param MatrixNrLookup Lookup up-map from job id to fragment number
|
---|
229 | * @param FragmentCounter total number of fragments
|
---|
230 | * @param NoAtoms total number of atoms
|
---|
231 | * @param Energy energy matrix to be filled on return
|
---|
232 | * @param Force force matrix to be filled on return
|
---|
233 | * @return true - everything ok, false - else
|
---|
234 | */
|
---|
235 | bool putResultsintoMatrices(
|
---|
236 | const std::vector<FragmentResult::ptr> &results,
|
---|
237 | const std::vector<MPQCData> &fragmentData,
|
---|
238 | std::map< JobId_t, size_t > &MatrixNrLookup,
|
---|
239 | const size_t FragmentCounter,
|
---|
240 | const size_t NoAtoms,
|
---|
241 | EnergyMatrix &Energy,
|
---|
242 | ForceMatrix &Force)
|
---|
243 | {
|
---|
244 | ASSERT( results.size() == fragmentData.size(),
|
---|
245 | "printReceivedMPQCResults() - results and fragmentData differ in size.");
|
---|
246 | std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
247 | std::vector<FragmentResult::ptr>::const_iterator resultiter = results.begin();
|
---|
248 | for (; dataiter != fragmentData.end(); ++dataiter, ++resultiter) {
|
---|
249 | const MPQCData &extractedData = *dataiter;
|
---|
250 | // place results into EnergyMatrix ...
|
---|
251 | {
|
---|
252 | MatrixContainer::MatrixArray matrix;
|
---|
253 | matrix.resize(1);
|
---|
254 | matrix[0].resize(1, extractedData.energies.total);
|
---|
255 | if (!Energy.AddMatrix(
|
---|
256 | std::string("MPQCJob ")+toString((*resultiter)->getId()),
|
---|
257 | matrix,
|
---|
258 | MatrixNrLookup[(*resultiter)->getId()])) {
|
---|
259 | ELOG(1, "Adding energy matrix failed.");
|
---|
260 | return false;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | // ... and ForceMatrix (with two empty columns in front)
|
---|
264 | {
|
---|
265 | MatrixContainer::MatrixArray matrix;
|
---|
266 | const size_t rows = extractedData.forces.size();
|
---|
267 | matrix.resize(rows);
|
---|
268 | for (size_t i=0;i<rows;++i) {
|
---|
269 | const size_t columns = 2+extractedData.forces[i].size();
|
---|
270 | matrix[i].resize(columns, 0.);
|
---|
271 | // for (size_t j=0;j<2;++j)
|
---|
272 | // matrix[i][j] = 0.;
|
---|
273 | for (size_t j=2;j<columns;++j)
|
---|
274 | matrix[i][j] = extractedData.forces[i][j-2];
|
---|
275 | }
|
---|
276 | if (!Force.AddMatrix(
|
---|
277 | std::string("MPQCJob ")+toString((*resultiter)->getId()),
|
---|
278 | matrix,
|
---|
279 | MatrixNrLookup[(*resultiter)->getId()])) {
|
---|
280 | ELOG(1, "Adding force matrix failed.");
|
---|
281 | return false;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 | // add one more matrix (not required for energy)
|
---|
286 | MatrixContainer::MatrixArray matrix;
|
---|
287 | matrix.resize(1);
|
---|
288 | matrix[0].resize(1, 0.);
|
---|
289 | if (!Energy.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
---|
290 | return false;
|
---|
291 | // but for energy because we need to know total number of atoms
|
---|
292 | matrix.resize(NoAtoms);
|
---|
293 | for (size_t i = 0; i< NoAtoms; ++i)
|
---|
294 | matrix[i].resize(2+NDIM, 0.);
|
---|
295 | if (!Force.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
---|
296 | return false;
|
---|
297 |
|
---|
298 | return true;
|
---|
299 | }
|
---|
300 |
|
---|
301 | void convertMPQCDatatoEnergyMap(
|
---|
302 | const std::vector<MPQCData> &fragmentData,
|
---|
303 | std::vector<MPQCDataEnergyMap_t> &MPQCData_Energy_fused)
|
---|
304 | {
|
---|
305 | // energy_t
|
---|
306 | MPQCData_Energy_fused.clear();
|
---|
307 | MPQCData_Energy_fused.reserve(fragmentData.size());
|
---|
308 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
309 | dataiter != fragmentData.end(); ++dataiter) {
|
---|
310 | const MPQCData &extractedData = *dataiter;
|
---|
311 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
---|
312 | MPQCDataEnergyMap_t instance;
|
---|
313 | boost::fusion::at_key<MPQCDataFused::energy_total>(instance) = extractedData.energies.total;
|
---|
314 | boost::fusion::at_key<MPQCDataFused::energy_nuclear_repulsion>(instance) = extractedData.energies.nuclear_repulsion;
|
---|
315 | boost::fusion::at_key<MPQCDataFused::energy_electron_coulomb>(instance) = extractedData.energies.electron_coulomb;
|
---|
316 | boost::fusion::at_key<MPQCDataFused::energy_electron_exchange>(instance) = extractedData.energies.electron_exchange;
|
---|
317 | boost::fusion::at_key<MPQCDataFused::energy_correlation>(instance) = extractedData.energies.correlation;
|
---|
318 | boost::fusion::at_key<MPQCDataFused::energy_overlap>(instance) = extractedData.energies.overlap;
|
---|
319 | boost::fusion::at_key<MPQCDataFused::energy_kinetic>(instance) = extractedData.energies.kinetic;
|
---|
320 | boost::fusion::at_key<MPQCDataFused::energy_hcore>(instance) = extractedData.energies.hcore;
|
---|
321 | boost::fusion::at_key<MPQCDataFused::energy_eigenvalues>(instance) = extractedData.energies.eigenvalues;
|
---|
322 | MPQCData_Energy_fused.push_back(instance);
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | void convertMPQCDatatoForceMap(
|
---|
327 | const std::vector<MPQCData> &fragmentData,
|
---|
328 | const KeySetsContainer &ForceKeySet,
|
---|
329 | std::vector<MPQCDataForceMap_t> &MPQCData_Force_fused)
|
---|
330 | {
|
---|
331 | // forces
|
---|
332 | ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(),
|
---|
333 | "FragmentationAutomationAction::performCall() - indices and fragmentData differ in size.");
|
---|
334 | MPQCData_Force_fused.clear();
|
---|
335 | MPQCData_Force_fused.reserve(fragmentData.size());
|
---|
336 | std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
337 | KeySetsContainer::ArrayOfIntVectors::const_iterator arrayiter = ForceKeySet.KeySets.begin();
|
---|
338 | for(;dataiter != fragmentData.end(); ++dataiter, ++arrayiter) {
|
---|
339 | const MPQCData &extractedData = *dataiter;
|
---|
340 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
---|
341 | MPQCDataForceMap_t instance;
|
---|
342 | // must convert int to index_t
|
---|
343 | IndexedVectors::indices_t indices(arrayiter->begin(), arrayiter->end());
|
---|
344 | boost::fusion::at_key<MPQCDataFused::forces>(instance) =
|
---|
345 | IndexedVectors(indices, extractedData.forces);
|
---|
346 | MPQCData_Force_fused.push_back(instance);
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | void convertMPQCDatatoGridMap(
|
---|
351 | const std::vector<MPQCData> &fragmentData,
|
---|
352 | std::vector<MPQCDataGridMap_t> &MPQCData_Grid_fused)
|
---|
353 | {
|
---|
354 | // sampled_grid
|
---|
355 | MPQCData_Grid_fused.clear();
|
---|
356 | MPQCData_Grid_fused.reserve(fragmentData.size());
|
---|
357 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
358 | dataiter != fragmentData.end(); ++dataiter) {
|
---|
359 | const MPQCData &extractedData = *dataiter;
|
---|
360 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
---|
361 | MPQCDataGridMap_t instance;
|
---|
362 | boost::fusion::at_key<MPQCDataFused::sampled_grid>(instance) = extractedData.sampled_grid;
|
---|
363 | MPQCData_Grid_fused.push_back(instance);
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | void convertMPQCDatatoTimeMap(
|
---|
368 | const std::vector<MPQCData> &fragmentData,
|
---|
369 | std::vector<MPQCDataTimeMap_t> &MPQCData_Time_fused)
|
---|
370 | {
|
---|
371 | // times
|
---|
372 | MPQCData_Time_fused.clear();
|
---|
373 | MPQCData_Time_fused.reserve(fragmentData.size());
|
---|
374 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
375 | dataiter != fragmentData.end(); ++dataiter) {
|
---|
376 | const MPQCData &extractedData = *dataiter;
|
---|
377 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
---|
378 | MPQCDataTimeMap_t instance;
|
---|
379 | boost::fusion::at_key<MPQCDataFused::times_walltime>(instance) = extractedData.times.walltime;
|
---|
380 | boost::fusion::at_key<MPQCDataFused::times_cputime>(instance) = extractedData.times.cputime;
|
---|
381 | boost::fusion::at_key<MPQCDataFused::times_flops>(instance) = extractedData.times.flops;
|
---|
382 | MPQCData_Time_fused.push_back(instance);
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | /** Print MPQCData from received results.
|
---|
387 | *
|
---|
388 | * @param results results with ids to associate with fragment number
|
---|
389 | * @param fragmentData MPQCData resulting from the jobs
|
---|
390 | * @param KeySetFilename filename with keysets to associate forces correctly
|
---|
391 | * @param NoAtoms total number of atoms
|
---|
392 | */
|
---|
393 | bool printReceivedMPQCResults(
|
---|
394 | const std::vector<FragmentResult::ptr> &results,
|
---|
395 | const std::vector<MPQCData> &fragmentData,
|
---|
396 | const std::string &KeySetFilename,
|
---|
397 | size_t NoAtoms)
|
---|
398 | {
|
---|
399 | // create lookup from job nr to fragment number
|
---|
400 | std::map< JobId_t, size_t > MatrixNrLookup;
|
---|
401 | size_t FragmentCounter = 0;
|
---|
402 | createMatrixNrLookup(results, MatrixNrLookup, FragmentCounter);
|
---|
403 |
|
---|
404 | // place results into maps
|
---|
405 | EnergyMatrix Energy;
|
---|
406 | ForceMatrix Force;
|
---|
407 | if (!putResultsintoMatrices(results, fragmentData, MatrixNrLookup, FragmentCounter, NoAtoms, Energy, Force))
|
---|
408 | return false;
|
---|
409 |
|
---|
410 | // initialise keysets
|
---|
411 | KeySetsContainer KeySet;
|
---|
412 | KeySetsContainer ForceKeySet;
|
---|
413 | if (!Energy.InitialiseIndices()) return false;
|
---|
414 |
|
---|
415 | if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
|
---|
416 |
|
---|
417 | {
|
---|
418 | // else needs keysets without hydrogens
|
---|
419 | std::stringstream filename;
|
---|
420 | filename << FRAGMENTPREFIX << KEYSETFILE;
|
---|
421 | if (!KeySet.ParseKeySets(KeySetFilename, filename.str(), FragmentCounter)) return false;
|
---|
422 | }
|
---|
423 |
|
---|
424 | {
|
---|
425 | // forces need keysets including hydrogens
|
---|
426 | std::stringstream filename;
|
---|
427 | filename << FRAGMENTPREFIX << FORCESFILE;
|
---|
428 | if (!ForceKeySet.ParseKeySets(KeySetFilename, filename.str(), FragmentCounter)) return false;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /// prepare for OrthogonalSummation
|
---|
432 |
|
---|
433 | // convert KeySetContainer to IndexSetContainer
|
---|
434 | IndexSetContainer::ptr container(new IndexSetContainer(KeySet));
|
---|
435 | // create the map of all keysets
|
---|
436 | SubsetMap::ptr subsetmap(new SubsetMap(*container));
|
---|
437 |
|
---|
438 | /// convert all MPQCData to MPQCDataMap_t
|
---|
439 | {
|
---|
440 | ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(),
|
---|
441 | "FragmentationAutomationAction::performCall() - ForceKeySet's KeySets and fragmentData differ in size.");
|
---|
442 |
|
---|
443 | std::vector<MPQCDataEnergyMap_t> MPQCData_Energy_fused;
|
---|
444 | convertMPQCDatatoEnergyMap(fragmentData, MPQCData_Energy_fused);
|
---|
445 | std::vector<MPQCDataForceMap_t> MPQCData_Force_fused;
|
---|
446 | convertMPQCDatatoForceMap(fragmentData, ForceKeySet, MPQCData_Force_fused);
|
---|
447 | std::vector<MPQCDataGridMap_t> MPQCData_Grid_fused;
|
---|
448 | convertMPQCDatatoGridMap(fragmentData, MPQCData_Grid_fused);
|
---|
449 | std::vector<MPQCDataTimeMap_t> MPQCData_Time_fused;
|
---|
450 | convertMPQCDatatoTimeMap(fragmentData, MPQCData_Time_fused);
|
---|
451 |
|
---|
452 | // create a vector of all job ids
|
---|
453 | std::vector<JobId_t> jobids(results.size(), JobId::IllegalJob);
|
---|
454 | std::transform(results.begin(), results.end(), jobids.begin(),
|
---|
455 | boost::bind(&FragmentResult::getId,
|
---|
456 | boost::bind(&FragmentResult::ptr::operator->, _1)));
|
---|
457 |
|
---|
458 | // create summation instances
|
---|
459 | std::vector<MPQCDataEnergyMap_t> Result_Energy_fused(subsetmap->getMaximumSubsetLevel());
|
---|
460 | AllLevelOrthogonalSummator<MPQCDataEnergyMap_t> energySummer(
|
---|
461 | subsetmap,
|
---|
462 | MPQCData_Energy_fused,
|
---|
463 | jobids,
|
---|
464 | container->getContainer(),
|
---|
465 | MatrixNrLookup,
|
---|
466 | Result_Energy_fused);
|
---|
467 | std::vector<MPQCDataForceMap_t> Result_Force_fused(subsetmap->getMaximumSubsetLevel());
|
---|
468 | AllLevelOrthogonalSummator<MPQCDataForceMap_t> forceSummer(
|
---|
469 | subsetmap,
|
---|
470 | MPQCData_Force_fused,
|
---|
471 | jobids,
|
---|
472 | container->getContainer(),
|
---|
473 | MatrixNrLookup,
|
---|
474 | Result_Force_fused);
|
---|
475 | std::vector<MPQCDataGridMap_t> Result_Grid_fused(subsetmap->getMaximumSubsetLevel());
|
---|
476 | AllLevelOrthogonalSummator<MPQCDataGridMap_t> gridSummer(
|
---|
477 | subsetmap,
|
---|
478 | MPQCData_Grid_fused,
|
---|
479 | jobids,
|
---|
480 | container->getContainer(),
|
---|
481 | MatrixNrLookup,
|
---|
482 | Result_Grid_fused);
|
---|
483 | std::vector<MPQCDataTimeMap_t> Result_Time_fused(subsetmap->getMaximumSubsetLevel());
|
---|
484 | AllLevelSummator<MPQCDataTimeMap_t> timeSummer(
|
---|
485 | subsetmap,
|
---|
486 | MPQCData_Time_fused,
|
---|
487 | jobids,
|
---|
488 | container->getContainer(),
|
---|
489 | MatrixNrLookup,
|
---|
490 | Result_Time_fused);
|
---|
491 |
|
---|
492 | // sum up
|
---|
493 | boost::mpl::for_each<MPQCDataEnergyVector_t>(boost::ref(energySummer));
|
---|
494 | boost::mpl::for_each<MPQCDataForceVector_t>(boost::ref(forceSummer));
|
---|
495 | boost::mpl::for_each<MPQCDataGridVector_t>(boost::ref(gridSummer));
|
---|
496 | boost::mpl::for_each<MPQCDataTimeVector_t>(boost::ref(timeSummer));
|
---|
497 |
|
---|
498 | // print tables (without eigenvalues, they go extra)
|
---|
499 | const size_t MaxLevel = subsetmap->getMaximumSubsetLevel();
|
---|
500 | typedef boost::mpl::remove<MPQCDataEnergyVector_t, MPQCDataFused::energy_eigenvalues>::type MPQCDataEnergyVector_noeigenvalues_t;
|
---|
501 | const std::string energyresult =
|
---|
502 | writeTable<MPQCDataEnergyMap_t, MPQCDataEnergyVector_noeigenvalues_t >()(
|
---|
503 | Result_Energy_fused, MaxLevel);
|
---|
504 | LOG(0, "Energy table is \n" << energyresult);
|
---|
505 | const std::string eigenvalueresult;
|
---|
506 |
|
---|
507 | LOG(0, "Eigenvalue table is \n" << eigenvalueresult);
|
---|
508 | const std::string forceresult =
|
---|
509 | writeTable<MPQCDataForceMap_t, MPQCDataForceVector_t>()(
|
---|
510 | Result_Force_fused, MaxLevel);
|
---|
511 | LOG(0, "Force table is \n" << forceresult);
|
---|
512 | // we don't want to print grid to a table
|
---|
513 | // print times (without flops for now)
|
---|
514 | typedef boost::mpl::remove<MPQCDataTimeVector_t, MPQCDataFused::times_flops>::type MPQCDataTimeVector_noflops_t;
|
---|
515 | const std::string timesresult =
|
---|
516 | writeTable<MPQCDataTimeMap_t, MPQCDataTimeVector_noflops_t >()(
|
---|
517 | Result_Time_fused, MaxLevel);
|
---|
518 | LOG(0, "Times table is \n" << timesresult);
|
---|
519 | }
|
---|
520 |
|
---|
521 | // combine all found data
|
---|
522 | if (!KeySet.ParseManyBodyTerms()) return false;
|
---|
523 |
|
---|
524 | EnergyMatrix EnergyFragments;
|
---|
525 | ForceMatrix ForceFragments;
|
---|
526 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false;
|
---|
527 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false;
|
---|
528 |
|
---|
529 | if(!Energy.SetLastMatrix(0., 0)) return false;
|
---|
530 | if(!Force.SetLastMatrix(0., 2)) return false;
|
---|
531 |
|
---|
532 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
|
---|
533 | // --------- sum up energy --------------------
|
---|
534 | LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
|
---|
535 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
|
---|
536 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
|
---|
537 |
|
---|
538 | // --------- sum up Forces --------------------
|
---|
539 | LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
|
---|
540 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
|
---|
541 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
|
---|
542 | }
|
---|
543 |
|
---|
544 | // for debugging print resulting energy and forces
|
---|
545 | LOG(1, "INFO: Resulting energy is " << Energy.Matrix[ FragmentCounter ][0][0]);
|
---|
546 | std::stringstream output;
|
---|
547 | for (int i=0; i< Force.RowCounter[FragmentCounter]; ++i) {
|
---|
548 | for (int j=0; j< Force.ColumnCounter[FragmentCounter]; ++j)
|
---|
549 | output << Force.Matrix[ FragmentCounter ][i][j] << " ";
|
---|
550 | output << "\n";
|
---|
551 | }
|
---|
552 | LOG(1, "INFO: Resulting forces are " << std::endl << output.str());
|
---|
553 |
|
---|
554 | return true;
|
---|
555 | }
|
---|
556 |
|
---|
557 |
|
---|
558 | void RunService(
|
---|
559 | boost::asio::io_service &io_service,
|
---|
560 | std::string message)
|
---|
561 | {
|
---|
562 | message = std::string("io_service: ") + message;
|
---|
563 | io_service.reset();
|
---|
564 | Info info(message.c_str());
|
---|
565 | io_service.run();
|
---|
566 | }
|
---|
567 |
|
---|
568 | void requestIds(
|
---|
569 | FragmentController &controller,
|
---|
570 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
---|
571 | const size_t numberjobs)
|
---|
572 | {
|
---|
573 | controller.requestIds(params.host.get(), params.port.get(), numberjobs);
|
---|
574 | }
|
---|
575 |
|
---|
576 | bool createJobsFromFiles(
|
---|
577 | FragmentController &controller,
|
---|
578 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
---|
579 | const std::vector< boost::filesystem::path > &jobfiles)
|
---|
580 | {
|
---|
581 | std::vector<FragmentJob::ptr> jobs;
|
---|
582 | for (std::vector< boost::filesystem::path >::const_iterator iter = jobfiles.begin();
|
---|
583 | iter != jobfiles .end(); ++iter) {
|
---|
584 | const std::string &filename = (*iter).string();
|
---|
585 | if (boost::filesystem::exists(filename)) {
|
---|
586 | const JobId_t next_id = controller.getAvailableId();
|
---|
587 | LOG(1, "INFO: Creating MPQCCommandJob with filename'"
|
---|
588 | +filename+"', and id "+toString(next_id)+".");
|
---|
589 | parsejob(jobs, params.executable.get().string(), filename, next_id);
|
---|
590 | } else {
|
---|
591 | ELOG(1, "Fragment job "+filename+" does not exist.");
|
---|
592 | return false;
|
---|
593 | }
|
---|
594 | }
|
---|
595 | controller.addJobs(jobs);
|
---|
596 | controller.sendJobs(params.host.get(), params.port.get());
|
---|
597 | return true;
|
---|
598 | }
|
---|
599 |
|
---|
600 | #ifdef HAVE_VMG
|
---|
601 | bool createLongRangeJobs(
|
---|
602 | FragmentController &controller,
|
---|
603 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
---|
604 | const std::vector<MPQCData> &fragmentData)
|
---|
605 | {
|
---|
606 | std::vector<FragmentJob::ptr> jobs;
|
---|
607 | // add one job for each fragment as the short-range correction which we need
|
---|
608 | // to subtract from the obtained full potential to get the long-range part only
|
---|
609 | for (std::vector<MPQCData>::const_iterator iter = fragmentData.begin();
|
---|
610 | iter != fragmentData.end(); ++iter) {
|
---|
611 | const JobId_t next_id = controller.getAvailableId();
|
---|
612 | LOG(1, "INFO: Creating VMGJob with " << iter->sampled_grid.sampled_grid.size()
|
---|
613 | << " gridpoints and " << iter->charges.size() << " particle charges.");
|
---|
614 | FragmentJob::ptr testJob(
|
---|
615 | new VMGJob(next_id, iter->sampled_grid, iter->positions, iter->charges) );
|
---|
616 | jobs.push_back(testJob);
|
---|
617 | }
|
---|
618 |
|
---|
619 | // add one more job for the full calculation
|
---|
620 | // TODO: Here, we actually have to combine all the other sampled_grids
|
---|
621 | {
|
---|
622 | const int level = LEVEL;
|
---|
623 | const int GRID = pow(2, level);
|
---|
624 | std::vector<double> full_sample(GRID*GRID*GRID, 0.);
|
---|
625 | double begin[NDIM] = { 0., 0., 0. };
|
---|
626 | const RealSpaceMatrix& M = World::getInstance().getDomain().getM();
|
---|
627 | const double size = M.at(0,0);
|
---|
628 | ASSERT( M.determinant() == size*size*size,
|
---|
629 | "createLongRangeJobs() - current domain matrix "+toString(M)+" is not cubic.");
|
---|
630 | const SamplingGrid full_sampled_grid(begin, size, level, full_sample);
|
---|
631 | const std::vector< std::vector<double> > positions;
|
---|
632 | const std::vector<double> charges;
|
---|
633 | const JobId_t next_id = controller.getAvailableId();
|
---|
634 | LOG(1, "INFO: Creating full VMGJob with " << full_sample.size()
|
---|
635 | << " gridpoints and " << charges.size() << " particle charges.");
|
---|
636 | FragmentJob::ptr testJob(
|
---|
637 | new VMGJob(next_id, full_sampled_grid, positions, charges) );
|
---|
638 | jobs.push_back(testJob);
|
---|
639 | }
|
---|
640 |
|
---|
641 | // then send jobs to controller
|
---|
642 | controller.addJobs(jobs);
|
---|
643 | controller.sendJobs(params.host.get(), params.port.get());
|
---|
644 | return true;
|
---|
645 | }
|
---|
646 | #endif
|
---|
647 |
|
---|
648 | void WaitforResults(
|
---|
649 | boost::asio::io_service &io_service,
|
---|
650 | FragmentController &controller,
|
---|
651 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
---|
652 | const size_t NoExpectedResults
|
---|
653 | )
|
---|
654 | {
|
---|
655 | size_t NoCalculatedResults = 0;
|
---|
656 | while (NoCalculatedResults != NoExpectedResults) {
|
---|
657 | // wait a bit
|
---|
658 | boost::asio::deadline_timer timer(io_service);
|
---|
659 | timer.expires_from_now(boost::posix_time::milliseconds(500));
|
---|
660 | timer.wait();
|
---|
661 | // then request status
|
---|
662 | controller.checkResults(params.host.get(), params.port.get());
|
---|
663 | RunService(io_service, "Checking on results");
|
---|
664 |
|
---|
665 | const std::pair<size_t, size_t> JobStatus = controller.getJobStatus();
|
---|
666 | LOG(1, "INFO: #" << JobStatus.first << " are waiting in the queue and #" << JobStatus.second << " jobs are calculated so far.");
|
---|
667 | NoCalculatedResults = JobStatus.second;
|
---|
668 | }
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | Action::state_ptr FragmentationFragmentationAutomationAction::performCall() {
|
---|
673 | boost::asio::io_service io_service;
|
---|
674 | FragmentController controller(io_service);
|
---|
675 |
|
---|
676 | // TODO: Have io_service run in second thread and merge with current again eventually
|
---|
677 |
|
---|
678 | // Phase One: obtain ids
|
---|
679 | std::vector< boost::filesystem::path > jobfiles = params.jobfiles.get();
|
---|
680 | requestIds(controller, params, jobfiles.size());
|
---|
681 | RunService(io_service, "Requesting ids");
|
---|
682 |
|
---|
683 | // Phase Two: create and add MPQCJobs
|
---|
684 | if (!createJobsFromFiles(controller, params, jobfiles))
|
---|
685 | return Action::failure;
|
---|
686 | RunService(io_service, "Adding MPQCJobs");
|
---|
687 |
|
---|
688 | // Phase Three: calculate result
|
---|
689 | WaitforResults(io_service, controller, params, jobfiles.size());
|
---|
690 | controller.receiveResults(params.host.get(), params.port.get());
|
---|
691 | RunService(io_service, "Requesting short-range results");
|
---|
692 | std::vector<FragmentResult::ptr> MPQCresults = controller.getReceivedResults();
|
---|
693 | std::vector<MPQCData> fragmentData;
|
---|
694 | ConvertFragmentResultToMPQCData(MPQCresults, fragmentData);
|
---|
695 |
|
---|
696 | // print intermediate short-range results
|
---|
697 | {
|
---|
698 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
---|
699 | printReceivedMPQCResults(
|
---|
700 | MPQCresults,
|
---|
701 | fragmentData,
|
---|
702 | params.path.get(),
|
---|
703 | getNoAtomsFromAdjacencyFile(params.path.get()));
|
---|
704 | }
|
---|
705 |
|
---|
706 | #ifdef HAVE_VMG
|
---|
707 | if (params.DoLongrange.get()) {
|
---|
708 | // Phase Four: obtain more ids
|
---|
709 | requestIds(controller, params, fragmentData.size()+1);
|
---|
710 | RunService(io_service, "Requesting ids");
|
---|
711 |
|
---|
712 | // Phase Five: create VMGJobs
|
---|
713 | if (!createLongRangeJobs(controller, params, fragmentData))
|
---|
714 | return Action::failure;
|
---|
715 | RunService(io_service, "Adding VMGJobs");
|
---|
716 |
|
---|
717 | // Phase Six: calculate result
|
---|
718 | WaitforResults(io_service, controller, params, fragmentData.size()+1);
|
---|
719 | controller.receiveResults(params.host.get(), params.port.get());
|
---|
720 | RunService(io_service, "Requesting long-range results");
|
---|
721 | std::vector<FragmentResult::ptr> VMGresults = controller.getReceivedResults();
|
---|
722 | ASSERT( MPQCresults.size()+1 == VMGresults.size(),
|
---|
723 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresultd and VMGresults don't match.");
|
---|
724 |
|
---|
725 | // Final phase: print result
|
---|
726 | {
|
---|
727 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
---|
728 | printReceivedMPQCResults(
|
---|
729 | MPQCresults,
|
---|
730 | fragmentData,
|
---|
731 | params.path.get(),
|
---|
732 | getNoAtomsFromAdjacencyFile(params.path.get()));
|
---|
733 | }
|
---|
734 | }
|
---|
735 | #endif
|
---|
736 | size_t Exitflag = controller.getExitflag();
|
---|
737 |
|
---|
738 | return (Exitflag == 0) ? Action::success : Action::failure;
|
---|
739 | }
|
---|
740 |
|
---|
741 | Action::state_ptr FragmentationFragmentationAutomationAction::performUndo(Action::state_ptr _state) {
|
---|
742 | return Action::success;
|
---|
743 | }
|
---|
744 |
|
---|
745 | Action::state_ptr FragmentationFragmentationAutomationAction::performRedo(Action::state_ptr _state){
|
---|
746 | return Action::success;
|
---|
747 | }
|
---|
748 |
|
---|
749 | bool FragmentationFragmentationAutomationAction::canUndo() {
|
---|
750 | return false;
|
---|
751 | }
|
---|
752 |
|
---|
753 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
|
---|
754 | return false;
|
---|
755 | }
|
---|
756 | /** =========== end of function ====================== */
|
---|