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 | #include <boost/lambda/lambda.hpp>
|
---|
43 |
|
---|
44 | #include "CodePatterns/Assert.hpp"
|
---|
45 | #include "CodePatterns/Info.hpp"
|
---|
46 | #include "CodePatterns/Log.hpp"
|
---|
47 | #include "JobMarket/Jobs/FragmentJob.hpp"
|
---|
48 |
|
---|
49 | #include "Fragmentation/Automation/createMatrixNrLookup.hpp"
|
---|
50 | #include "Fragmentation/Automation/extractJobIds.hpp"
|
---|
51 | #include "Fragmentation/Automation/MPQCFragmentController.hpp"
|
---|
52 | #include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp"
|
---|
53 | #include "Fragmentation/Automation/VMGFragmentController.hpp"
|
---|
54 | #include "Fragmentation/EnergyMatrix.hpp"
|
---|
55 | #include "Fragmentation/ForceMatrix.hpp"
|
---|
56 | #include "Fragmentation/Fragmentation.hpp"
|
---|
57 | #include "Fragmentation/SetValues/Fragment.hpp"
|
---|
58 | #include "Fragmentation/SetValues/Histogram.hpp"
|
---|
59 | #include "Fragmentation/SetValues/IndexedVectors.hpp"
|
---|
60 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
|
---|
61 | #include "Fragmentation/KeySet.hpp"
|
---|
62 | #include "Fragmentation/KeySetsContainer.hpp"
|
---|
63 | #include "Fragmentation/Summation/OrthogonalSumUpPerLevel.hpp"
|
---|
64 | #include "Fragmentation/Summation/SumUpPerLevel.hpp"
|
---|
65 | #include "Fragmentation/Summation/OrthogonalFullSummator.hpp"
|
---|
66 | #include "Fragmentation/Summation/writeTable.hpp"
|
---|
67 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
---|
68 | #include "Helpers/defs.hpp"
|
---|
69 | #include "Jobs/MPQCJob.hpp"
|
---|
70 | #include "Jobs/MPQCData.hpp"
|
---|
71 | #include "Jobs/MPQCData_printKeyNames.hpp"
|
---|
72 | #include "Jobs/Grid/SamplingGrid.hpp"
|
---|
73 | #ifdef HAVE_VMG
|
---|
74 | #include "Jobs/VMGDebugGridJob.hpp"
|
---|
75 | #include "Jobs/VMGJob.hpp"
|
---|
76 | #include "Jobs/VMGData.hpp"
|
---|
77 | #include "Jobs/VMGDataFused.hpp"
|
---|
78 | #include "Jobs/VMGDataMap.hpp"
|
---|
79 | #include "Jobs/VMGData_printKeyNames.hpp"
|
---|
80 | #endif
|
---|
81 | #include "World.hpp"
|
---|
82 |
|
---|
83 | #include <iostream>
|
---|
84 | #include <string>
|
---|
85 | #include <vector>
|
---|
86 |
|
---|
87 | #include <boost/mpl/for_each.hpp>
|
---|
88 |
|
---|
89 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
|
---|
90 |
|
---|
91 | using namespace MoleCuilder;
|
---|
92 |
|
---|
93 | // and construct the stuff
|
---|
94 | #include "FragmentationAutomationAction.def"
|
---|
95 | #include "Action_impl_pre.hpp"
|
---|
96 | /** =========== define the function ====================== */
|
---|
97 |
|
---|
98 | class controller_AddOn;
|
---|
99 |
|
---|
100 | // needs to be defined for using the FragmentController
|
---|
101 | controller_AddOn *getAddOn()
|
---|
102 | {
|
---|
103 | return NULL;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Helper function to get number of atoms somehow.
|
---|
107 | *
|
---|
108 | * Here, we just parse the number of lines in the adjacency file as
|
---|
109 | * it should correspond to the number of atoms, except when some atoms
|
---|
110 | * are not bonded, but then fragmentation makes no sense.
|
---|
111 | *
|
---|
112 | * @param path path to the adjacency file
|
---|
113 | */
|
---|
114 | size_t getNoAtomsFromAdjacencyFile(const std::string &path)
|
---|
115 | {
|
---|
116 | size_t NoAtoms = 0;
|
---|
117 |
|
---|
118 | // parse in special file to get atom count (from line count)
|
---|
119 | std::string filename(path);
|
---|
120 | filename += FRAGMENTPREFIX;
|
---|
121 | filename += ADJACENCYFILE;
|
---|
122 | std::ifstream adjacency(filename.c_str());
|
---|
123 | if (adjacency.fail()) {
|
---|
124 | LOG(0, endl << "getNoAtomsFromAdjacencyFile() - Unable to open " << filename << ", is the directory correct?");
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 | std::string buffer;
|
---|
128 | while (getline(adjacency, buffer))
|
---|
129 | NoAtoms++;
|
---|
130 | LOG(1, "INFO: There are " << NoAtoms << " atoms.");
|
---|
131 |
|
---|
132 | return NoAtoms;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 | /** Place results from FragmentResult into EnergyMatrix and ForceMatrix.
|
---|
138 | *
|
---|
139 | * @param fragmentData MPQCData resulting from the jobs
|
---|
140 | * @param MatrixNrLookup Lookup up-map from job id to fragment number
|
---|
141 | * @param FragmentCounter total number of fragments
|
---|
142 | * @param NoAtoms total number of atoms
|
---|
143 | * @param Energy energy matrix to be filled on return
|
---|
144 | * @param Force force matrix to be filled on return
|
---|
145 | * @return true - everything ok, false - else
|
---|
146 | */
|
---|
147 | bool putResultsintoMatrices(
|
---|
148 | const std::map<JobId_t, MPQCData> &fragmentData,
|
---|
149 | const std::map< JobId_t, size_t > &MatrixNrLookup,
|
---|
150 | const size_t FragmentCounter,
|
---|
151 | const size_t NoAtoms,
|
---|
152 | EnergyMatrix &Energy,
|
---|
153 | ForceMatrix &Force)
|
---|
154 | {
|
---|
155 | for (std::map<JobId_t, MPQCData>::const_iterator dataiter = fragmentData.begin();
|
---|
156 | dataiter != fragmentData.end(); ++dataiter) {
|
---|
157 | const MPQCData &extractedData = dataiter->second;
|
---|
158 | const JobId_t &jobid = dataiter->first;
|
---|
159 | std::map< JobId_t, size_t >::const_iterator nriter = MatrixNrLookup.find(jobid);
|
---|
160 | ASSERT( nriter != MatrixNrLookup.end(),
|
---|
161 | "putResultsintoMatrices() - MatrixNrLookup does not contain id "
|
---|
162 | +toString(jobid)+".");
|
---|
163 | // place results into EnergyMatrix ...
|
---|
164 | {
|
---|
165 | MatrixContainer::MatrixArray matrix;
|
---|
166 | matrix.resize(1);
|
---|
167 | matrix[0].resize(1, extractedData.energies.total);
|
---|
168 | if (!Energy.AddMatrix(
|
---|
169 | std::string("MPQCJob ")+toString(jobid),
|
---|
170 | matrix,
|
---|
171 | nriter->second)) {
|
---|
172 | ELOG(1, "Adding energy matrix failed.");
|
---|
173 | return false;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | // ... and ForceMatrix (with two empty columns in front)
|
---|
177 | {
|
---|
178 | MatrixContainer::MatrixArray matrix;
|
---|
179 | const size_t rows = extractedData.forces.size();
|
---|
180 | matrix.resize(rows);
|
---|
181 | for (size_t i=0;i<rows;++i) {
|
---|
182 | const size_t columns = 2+extractedData.forces[i].size();
|
---|
183 | matrix[i].resize(columns, 0.);
|
---|
184 | // for (size_t j=0;j<2;++j)
|
---|
185 | // matrix[i][j] = 0.;
|
---|
186 | for (size_t j=2;j<columns;++j)
|
---|
187 | matrix[i][j] = extractedData.forces[i][j-2];
|
---|
188 | }
|
---|
189 | if (!Force.AddMatrix(
|
---|
190 | std::string("MPQCJob ")+toString(jobid),
|
---|
191 | matrix,
|
---|
192 | nriter->second)) {
|
---|
193 | ELOG(1, "Adding force matrix failed.");
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | // add one more matrix (not required for energy)
|
---|
199 | MatrixContainer::MatrixArray matrix;
|
---|
200 | matrix.resize(1);
|
---|
201 | matrix[0].resize(1, 0.);
|
---|
202 | if (!Energy.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
---|
203 | return false;
|
---|
204 | // but for energy because we need to know total number of atoms
|
---|
205 | matrix.resize(NoAtoms);
|
---|
206 | for (size_t i = 0; i< NoAtoms; ++i)
|
---|
207 | matrix[i].resize(2+NDIM, 0.);
|
---|
208 | if (!Force.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
---|
209 | return false;
|
---|
210 |
|
---|
211 | return true;
|
---|
212 | }
|
---|
213 | /** Print MPQCData from received results.
|
---|
214 | *
|
---|
215 | * @param fragmentData MPQCData resulting from the jobs, each associated to a job
|
---|
216 | * @param KeySetFilename filename with keysets to associate forces correctly
|
---|
217 | * @param NoAtoms total number of atoms
|
---|
218 | * @param full_sample summed up charge density of electrons from fragments on return
|
---|
219 | * @param full_fragment summed up positions and charges of nuclei from fragments on return
|
---|
220 | */
|
---|
221 | bool sumUpChargeDensity(
|
---|
222 | const std::map<JobId_t,MPQCData> &fragmentData,
|
---|
223 | const std::string &KeySetFilename,
|
---|
224 | std::vector<SamplingGrid> &full_sample,
|
---|
225 | Fragment &full_fragment)
|
---|
226 | {
|
---|
227 | // create a vector of all job ids
|
---|
228 | std::vector<JobId_t> jobids;
|
---|
229 | std::transform(fragmentData.begin(),fragmentData.end(),
|
---|
230 | std::back_inserter(jobids),
|
---|
231 | boost::bind( &std::map<JobId_t,MPQCData>::value_type::first, boost::lambda::_1 )
|
---|
232 | );
|
---|
233 |
|
---|
234 | // create lookup from job nr to fragment number
|
---|
235 | size_t FragmentCounter = 0;
|
---|
236 | const std::map< JobId_t, size_t > MatrixNrLookup =
|
---|
237 | createMatrixNrLookup(jobids, FragmentCounter);
|
---|
238 |
|
---|
239 | // initialise keysets
|
---|
240 | KeySetsContainer KeySet;
|
---|
241 | {
|
---|
242 | // else needs keysets without hydrogens
|
---|
243 | std::stringstream filename;
|
---|
244 | filename << FRAGMENTPREFIX << KEYSETFILE;
|
---|
245 | if (!KeySet.ParseKeySets(KeySetFilename, filename.str(), FragmentCounter)) return false;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /// prepare for OrthogonalSummation
|
---|
249 |
|
---|
250 | // convert KeySetContainer to IndexSetContainer
|
---|
251 | IndexSetContainer::ptr container(new IndexSetContainer(KeySet));
|
---|
252 | // create the map of all keysets
|
---|
253 | SubsetMap::ptr subsetmap(new SubsetMap(*container));
|
---|
254 |
|
---|
255 | /// convert all MPQCData to MPQCDataMap_t
|
---|
256 | std::vector<MPQCDataGridMap_t> Result_Grid_fused(
|
---|
257 | OrthogonalSumUpPerLevel<MPQCDataGridMap_t, MPQCDataGridVector_t>(
|
---|
258 | fragmentData, MatrixNrLookup, container, subsetmap));
|
---|
259 | std::vector<MPQCDataFragmentMap_t> Result_Fragment_fused(
|
---|
260 | OrthogonalSumUpPerLevel<MPQCDataFragmentMap_t, MPQCDataFragmentVector_t>(
|
---|
261 | fragmentData, MatrixNrLookup, container, subsetmap));
|
---|
262 | // obtain full grid
|
---|
263 | full_sample.clear();
|
---|
264 | full_sample.reserve(Result_Grid_fused.size());
|
---|
265 | for (std::vector<MPQCDataGridMap_t>::const_iterator iter = ++Result_Grid_fused.begin();
|
---|
266 | iter !=Result_Grid_fused.end();
|
---|
267 | ++iter)
|
---|
268 | full_sample.push_back(boost::fusion::at_key<MPQCDataFused::sampled_grid>((*iter)));
|
---|
269 | full_fragment = boost::fusion::at_key<MPQCDataFused::fragment>(Result_Fragment_fused.back());
|
---|
270 |
|
---|
271 | return true;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** Print MPQCData from received results.
|
---|
275 | *
|
---|
276 | * @param fragmentData MPQCData resulting from the jobs, associated to job id
|
---|
277 | * @param KeySetFilename filename with keysets to associate forces correctly
|
---|
278 | * @param NoAtoms total number of atoms
|
---|
279 | * @param full_sample summed up charge from fragments on return
|
---|
280 | */
|
---|
281 | bool printReceivedMPQCResults(
|
---|
282 | const std::map<JobId_t, MPQCData> &fragmentData,
|
---|
283 | const std::string &KeySetFilename,
|
---|
284 | size_t NoAtoms)
|
---|
285 | {
|
---|
286 | // create a vector of all job ids
|
---|
287 | std::vector<JobId_t> jobids;
|
---|
288 | std::transform(fragmentData.begin(),fragmentData.end(),
|
---|
289 | std::back_inserter(jobids),
|
---|
290 | boost::bind( &std::map<JobId_t,MPQCData>::value_type::first, boost::lambda::_1 )
|
---|
291 | );
|
---|
292 |
|
---|
293 | // create lookup from job nr to fragment number
|
---|
294 | size_t FragmentCounter = 0;
|
---|
295 | const std::map< JobId_t, size_t > MatrixNrLookup=
|
---|
296 | createMatrixNrLookup(jobids, FragmentCounter);
|
---|
297 |
|
---|
298 |
|
---|
299 | // place results into maps
|
---|
300 | EnergyMatrix Energy;
|
---|
301 | ForceMatrix Force;
|
---|
302 | if (!putResultsintoMatrices(fragmentData, MatrixNrLookup, FragmentCounter, NoAtoms, Energy, Force))
|
---|
303 | return false;
|
---|
304 |
|
---|
305 | // initialise keysets
|
---|
306 | KeySetsContainer KeySet;
|
---|
307 | KeySetsContainer ForceKeySet;
|
---|
308 | if (!Energy.InitialiseIndices()) return false;
|
---|
309 |
|
---|
310 | if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
|
---|
311 |
|
---|
312 | {
|
---|
313 | // else needs keysets without hydrogens
|
---|
314 | std::stringstream filename;
|
---|
315 | filename << FRAGMENTPREFIX << KEYSETFILE;
|
---|
316 | if (!KeySet.ParseKeySets(KeySetFilename, filename.str(), FragmentCounter)) return false;
|
---|
317 | }
|
---|
318 |
|
---|
319 | {
|
---|
320 | // forces need keysets including hydrogens
|
---|
321 | std::stringstream filename;
|
---|
322 | filename << FRAGMENTPREFIX << FORCESFILE;
|
---|
323 | if (!ForceKeySet.ParseKeySets(KeySetFilename, filename.str(), FragmentCounter)) return false;
|
---|
324 | }
|
---|
325 |
|
---|
326 | // combine all found data
|
---|
327 | if (!KeySet.ParseManyBodyTerms()) return false;
|
---|
328 |
|
---|
329 | EnergyMatrix EnergyFragments;
|
---|
330 | ForceMatrix ForceFragments;
|
---|
331 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false;
|
---|
332 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false;
|
---|
333 |
|
---|
334 | if(!Energy.SetLastMatrix(0., 0)) return false;
|
---|
335 | if(!Force.SetLastMatrix(0., 2)) return false;
|
---|
336 |
|
---|
337 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
|
---|
338 | // --------- sum up energy --------------------
|
---|
339 | LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
|
---|
340 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
|
---|
341 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
|
---|
342 |
|
---|
343 | // --------- sum up Forces --------------------
|
---|
344 | LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
|
---|
345 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
|
---|
346 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
|
---|
347 | }
|
---|
348 |
|
---|
349 | // for debugging print resulting energy and forces
|
---|
350 | LOG(1, "INFO: Resulting energy is " << Energy.Matrix[ FragmentCounter ][0][0]);
|
---|
351 | std::stringstream output;
|
---|
352 | for (int i=0; i< Force.RowCounter[FragmentCounter]; ++i) {
|
---|
353 | for (int j=0; j< Force.ColumnCounter[FragmentCounter]; ++j)
|
---|
354 | output << Force.Matrix[ FragmentCounter ][i][j] << " ";
|
---|
355 | output << "\n";
|
---|
356 | }
|
---|
357 | LOG(1, "INFO: Resulting forces are " << std::endl << output.str());
|
---|
358 |
|
---|
359 | return true;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /** Print MPQCData from received results.
|
---|
363 | *
|
---|
364 | * @param fragmentData MPQCData resulting from the jobs
|
---|
365 | * @param longrangeData VMGData resulting from long-range jobs
|
---|
366 | * @param fullsolutionData VMGData resulting from long-range of full problem from level 2 onward
|
---|
367 | * @param KeySetFilename filename with keysets to associate forces correctly
|
---|
368 | * @param NoAtoms total number of atoms
|
---|
369 | * @param full_sample summed up charge from fragments on return
|
---|
370 | */
|
---|
371 | bool printReceivedFullResults(
|
---|
372 | const std::map<JobId_t,MPQCData> &fragmentData,
|
---|
373 | const std::map<JobId_t,VMGData> &longrangeData,
|
---|
374 | const std::vector<VMGData> &fullsolutionData,
|
---|
375 | const std::string &KeySetFilename,
|
---|
376 | size_t NoAtoms,
|
---|
377 | std::vector<SamplingGrid> &full_sample)
|
---|
378 | {
|
---|
379 | // create lookup from job nr to fragment number
|
---|
380 | size_t MPQCFragmentCounter = 0;
|
---|
381 | const std::vector<JobId_t> mpqcjobids = extractJobIds<MPQCData>(fragmentData);
|
---|
382 | const std::map< JobId_t, size_t > MPQCMatrixNrLookup =
|
---|
383 | createMatrixNrLookup(mpqcjobids, MPQCFragmentCounter);
|
---|
384 |
|
---|
385 | size_t VMGFragmentCounter = 0;
|
---|
386 | const std::vector<JobId_t> vmgjobids = extractJobIds<VMGData>(longrangeData);
|
---|
387 | const std::map< JobId_t, size_t > VMGMatrixNrLookup =
|
---|
388 | createMatrixNrLookup(vmgjobids, VMGFragmentCounter);
|
---|
389 |
|
---|
390 | // initialise keysets
|
---|
391 | KeySetsContainer KeySet;
|
---|
392 | KeySetsContainer ForceKeySet;
|
---|
393 | {
|
---|
394 | // else needs keysets without hydrogens
|
---|
395 | std::stringstream filename;
|
---|
396 | filename << FRAGMENTPREFIX << KEYSETFILE;
|
---|
397 | if (!KeySet.ParseKeySets(KeySetFilename, filename.str(), MPQCFragmentCounter)) return false;
|
---|
398 | }
|
---|
399 |
|
---|
400 | {
|
---|
401 | // forces need keysets including hydrogens
|
---|
402 | std::stringstream filename;
|
---|
403 | filename << FRAGMENTPREFIX << FORCESFILE;
|
---|
404 | if (!ForceKeySet.ParseKeySets(KeySetFilename, filename.str(), MPQCFragmentCounter)) return false;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /// prepare for OrthogonalSummation
|
---|
408 |
|
---|
409 | // convert KeySetContainer to IndexSetContainer
|
---|
410 | IndexSetContainer::ptr container(new IndexSetContainer(KeySet));
|
---|
411 | // create the map of all keysets
|
---|
412 | SubsetMap::ptr subsetmap(new SubsetMap(*container));
|
---|
413 |
|
---|
414 | /// convert all MPQCData to MPQCDataMap_t
|
---|
415 | {
|
---|
416 | ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(),
|
---|
417 | "printReceivedFullResults() - ForceKeySet's KeySets and fragmentData differ in size.");
|
---|
418 |
|
---|
419 | typedef boost::mpl::remove<MPQCDataEnergyVector_t, MPQCDataFused::energy_eigenvalues>::type MPQCDataEnergyVector_noeigenvalues_t;
|
---|
420 | std::vector<MPQCDataEnergyMap_t> Result_Energy_fused(
|
---|
421 | OrthogonalSumUpPerLevel<MPQCDataEnergyMap_t, MPQCDataEnergyVector_t>(
|
---|
422 | fragmentData, MPQCMatrixNrLookup, container, subsetmap));
|
---|
423 | std::vector<MPQCDataGridMap_t> Result_Grid_fused(
|
---|
424 | OrthogonalSumUpPerLevel<MPQCDataGridMap_t, MPQCDataGridVector_t>(
|
---|
425 | fragmentData, MPQCMatrixNrLookup, container, subsetmap));
|
---|
426 | std::vector<MPQCDataTimeMap_t> Result_Time_fused(
|
---|
427 | SumUpPerLevel<MPQCDataTimeMap_t, MPQCDataTimeVector_t>(
|
---|
428 | fragmentData, MPQCMatrixNrLookup, container, subsetmap));
|
---|
429 | std::vector<MPQCDataFragmentMap_t> Result_Fragment_fused(
|
---|
430 | OrthogonalSumUpPerLevel<MPQCDataFragmentMap_t, MPQCDataFragmentVector_t>(
|
---|
431 | fragmentData, MPQCMatrixNrLookup, container, subsetmap));
|
---|
432 |
|
---|
433 | // force has extra converter
|
---|
434 | std::map<JobId_t, MPQCDataForceMap_t> MPQCData_Force_fused;
|
---|
435 | convertMPQCDatatoForceMap(fragmentData, ForceKeySet, MPQCData_Force_fused);
|
---|
436 | std::vector<MPQCDataForceMap_t> Result_Force_fused(subsetmap->getMaximumSubsetLevel());
|
---|
437 | AllLevelOrthogonalSummator<MPQCDataForceMap_t> forceSummer(
|
---|
438 | subsetmap,
|
---|
439 | MPQCData_Force_fused,
|
---|
440 | container->getContainer(),
|
---|
441 | MPQCMatrixNrLookup,
|
---|
442 | Result_Force_fused);
|
---|
443 | boost::mpl::for_each<MPQCDataForceVector_t>(boost::ref(forceSummer));
|
---|
444 |
|
---|
445 | // obtain full grid
|
---|
446 | std::map<JobId_t, VMGDataMap_t> VMGData_Potential_fused;
|
---|
447 | convertDataTo<VMGData, VMGDataMap_t>(longrangeData, VMGData_Potential_fused);
|
---|
448 | OrthogonalFullSummator<VMGDataMap_t, VMGDataFused::sampled_potential> potentialSummer(
|
---|
449 | subsetmap,
|
---|
450 | VMGData_Potential_fused,
|
---|
451 | container->getContainer(),
|
---|
452 | VMGMatrixNrLookup);
|
---|
453 | potentialSummer(subsetmap->getMaximumSubsetLevel());
|
---|
454 | OrthogonalFullSummator<VMGDataMap_t, VMGDataFused::energy_potential> epotentialSummer(
|
---|
455 | subsetmap,
|
---|
456 | VMGData_Potential_fused,
|
---|
457 | container->getContainer(),
|
---|
458 | VMGMatrixNrLookup);
|
---|
459 | epotentialSummer(subsetmap->getMaximumSubsetLevel());
|
---|
460 |
|
---|
461 | std::vector<VMGDataLongRangeMap_t> Result_LongRange_fused;
|
---|
462 | Result_LongRange_fused.reserve(subsetmap->getMaximumSubsetLevel());
|
---|
463 | for (size_t level = 1; level <= subsetmap->getMaximumSubsetLevel(); ++level) {
|
---|
464 | // weight times correct charge density of the same level
|
---|
465 | // NOTE: potential for level 1 is not calculated as saturation hydrogen
|
---|
466 | // are not removed on this level yet
|
---|
467 | const size_t potentiallevel = level < 2 ? 0 : (level-2);
|
---|
468 | SamplingGrid charge_weight = boost::fusion::at_key<MPQCDataFused::sampled_grid>(Result_Grid_fused[level-1]);
|
---|
469 | SamplingGrid full_sample_solution = fullsolutionData[potentiallevel].sampled_potential;
|
---|
470 | SamplingGrid short_range_correction = potentialSummer(level);
|
---|
471 | // LOG(0, "Remaining long-range energy from energy_potential is " << full_sample_solution.integral()-epotentialSummer.getFullContribution() << ".");
|
---|
472 | full_sample_solution -= short_range_correction;
|
---|
473 | // multiply element-wise with charge distribution
|
---|
474 | VMGDataLongRangeMap_t instance;
|
---|
475 | boost::fusion::at_key<VMGDataFused::potential_longrange>(instance) = full_sample_solution.integral();
|
---|
476 | LOG(0, "Remaining long-range potential integral of level " << level << " is "
|
---|
477 | << full_sample_solution.integral() << ".");
|
---|
478 | boost::fusion::at_key<VMGDataFused::potential_shortrange>(instance) = short_range_correction.integral();
|
---|
479 | LOG(0, "Short-range correction potential integral of level " << level << " is "
|
---|
480 | << short_range_correction.integral() << ".");
|
---|
481 | boost::fusion::at_key<VMGDataFused::energy_longrange>(instance) = full_sample_solution.integral(charge_weight);
|
---|
482 | LOG(0, "Remaining long-range energy from potential integral of level " << level << " is "
|
---|
483 | << full_sample_solution.integral(charge_weight) << ".");
|
---|
484 | boost::fusion::at_key<VMGDataFused::energy_shortrange>(instance) = short_range_correction.integral(charge_weight);
|
---|
485 | LOG(0, "Short-range correction energy from potential integral of level " << level << " is "
|
---|
486 | << short_range_correction.integral(charge_weight) << ".");
|
---|
487 | Result_LongRange_fused.push_back(instance);
|
---|
488 | }
|
---|
489 | {
|
---|
490 | // LOG(0, "Remaining long-range energy from energy_potential is " << full_sample_solution.integral()-epotentialSummer.getFullContribution() << ".");
|
---|
491 | SamplingGrid full_sample_solution = fullsolutionData.back().sampled_potential;
|
---|
492 | SamplingGrid short_range_correction = potentialSummer.getFullContribution();
|
---|
493 | full_sample_solution -= short_range_correction;
|
---|
494 | // multiply element-wise with charge distribution
|
---|
495 | LOG(0, "Remaining long-range potential integral is " << full_sample_solution.integral() << ".");
|
---|
496 | LOG(0, "Short-range correction potential integral of level is " << short_range_correction.integral() << ".");
|
---|
497 | LOG(0, "Remaining long-range energy from potential integral is "
|
---|
498 | << full_sample_solution.integral(full_sample.back()) << ".");
|
---|
499 | LOG(0, "Short-range correction energy from potential integral is "
|
---|
500 | << short_range_correction.integral(full_sample.back()) << ".");
|
---|
501 | }
|
---|
502 |
|
---|
503 | // TODO: Extract long-range corrections to forces
|
---|
504 | // NOTE: potential is in atomic length units, NOT IN ANGSTROEM!
|
---|
505 |
|
---|
506 | OrthogonalFullSummator<VMGDataMap_t, VMGDataFused::energy_long> elongSummer(
|
---|
507 | subsetmap,
|
---|
508 | VMGData_Potential_fused,
|
---|
509 | container->getContainer(),
|
---|
510 | VMGMatrixNrLookup);
|
---|
511 | elongSummer(subsetmap->getMaximumSubsetLevel());
|
---|
512 | double e_long = fullsolutionData.back().e_long;
|
---|
513 | e_long -= elongSummer.getFullContribution();
|
---|
514 | LOG(0, "Remaining long-range energy is " << e_long << ".");
|
---|
515 |
|
---|
516 | // print tables (without eigenvalues, they go extra)
|
---|
517 | const size_t MaxLevel = subsetmap->getMaximumSubsetLevel();
|
---|
518 | const std::string energyresult =
|
---|
519 | writeTable<MPQCDataEnergyMap_t, MPQCDataEnergyVector_noeigenvalues_t >()(
|
---|
520 | Result_Energy_fused, MaxLevel);
|
---|
521 | LOG(0, "Energy table is \n" << energyresult);
|
---|
522 |
|
---|
523 | const std::string gridresult =
|
---|
524 | writeTable<VMGDataLongRangeMap_t, VMGDataLongRangeVector_t >()(
|
---|
525 | Result_LongRange_fused, MaxLevel);
|
---|
526 | LOG(0, "LongRange table is \n" << gridresult);
|
---|
527 |
|
---|
528 | const std::string eigenvalueresult;
|
---|
529 | LOG(0, "Eigenvalue table is \n" << eigenvalueresult);
|
---|
530 |
|
---|
531 | const std::string forceresult =
|
---|
532 | writeTable<MPQCDataForceMap_t, MPQCDataForceVector_t>()(
|
---|
533 | Result_Force_fused, MaxLevel);
|
---|
534 | LOG(0, "Force table is \n" << forceresult);
|
---|
535 | // we don't want to print grid to a table
|
---|
536 | // print times (without flops for now)
|
---|
537 | typedef boost::mpl::remove<
|
---|
538 | boost::mpl::remove<MPQCDataTimeVector_t, MPQCDataFused::times_total_flops>::type,
|
---|
539 | MPQCDataFused::times_gather_flops>::type
|
---|
540 | MPQCDataTimeVector_noflops_t;
|
---|
541 | const std::string timesresult =
|
---|
542 | writeTable<MPQCDataTimeMap_t, MPQCDataTimeVector_noflops_t >()(
|
---|
543 | Result_Time_fused, MaxLevel);
|
---|
544 | LOG(0, "Times table is \n" << timesresult);
|
---|
545 | }
|
---|
546 |
|
---|
547 | return true;
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | Action::state_ptr FragmentationFragmentationAutomationAction::performCall() {
|
---|
552 | boost::asio::io_service io_service;
|
---|
553 | MPQCFragmentController mpqccontroller(io_service);
|
---|
554 | mpqccontroller.setHost(params.host.get());
|
---|
555 | mpqccontroller.setPort(params.port.get());
|
---|
556 | mpqccontroller.setLevel(params.level.get());
|
---|
557 | VMGFragmentController vmgcontroller(io_service);
|
---|
558 | vmgcontroller.setHost(params.host.get());
|
---|
559 | vmgcontroller.setPort(params.port.get());
|
---|
560 | VMGDebugGridFragmentController debugcontroller(io_service);
|
---|
561 | debugcontroller.setHost(params.host.get());
|
---|
562 | debugcontroller.setPort(params.port.get());
|
---|
563 |
|
---|
564 | // TODO: Have io_service run in second thread and merge with current again eventually
|
---|
565 |
|
---|
566 | // Phase One: obtain ids
|
---|
567 | std::vector< boost::filesystem::path > jobfiles = params.jobfiles.get();
|
---|
568 | mpqccontroller.requestIds(jobfiles.size());
|
---|
569 |
|
---|
570 | // Phase Two: create and add MPQCJobs
|
---|
571 | if (!mpqccontroller.addJobsFromFiles(params.executable.get().string(), jobfiles))
|
---|
572 | return Action::failure;
|
---|
573 |
|
---|
574 | // Phase Three: calculate result
|
---|
575 | mpqccontroller.waitforResults(jobfiles.size());
|
---|
576 | std::map<JobId_t, MPQCData> fragmentData;
|
---|
577 | mpqccontroller.getResults(fragmentData);
|
---|
578 |
|
---|
579 | #ifdef HAVE_VMG
|
---|
580 | if (params.DoLongrange.get()) {
|
---|
581 | ASSERT( World::getInstance().getAllAtoms().size() != 0,
|
---|
582 | "FragmentationFragmentationAutomationAction::performCall() - please load the full molecule into the World before.");
|
---|
583 |
|
---|
584 | // obtain combined charge density
|
---|
585 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
---|
586 | std::vector<SamplingGrid> full_sample; // have charges from level 2 onward summed up
|
---|
587 | Fragment full_fragment;
|
---|
588 | sumUpChargeDensity(
|
---|
589 | fragmentData,
|
---|
590 | params.path.get(),
|
---|
591 | full_sample,
|
---|
592 | full_fragment);
|
---|
593 |
|
---|
594 | // Phase Four: obtain more ids
|
---|
595 | vmgcontroller.requestIds(fragmentData.size()+full_sample.size());
|
---|
596 |
|
---|
597 | // Phase Five: create VMGJobs
|
---|
598 | const size_t near_field_cells = params.near_field_cells.get();
|
---|
599 | if (!vmgcontroller.createLongRangeJobs(fragmentData, full_sample, full_fragment, near_field_cells))
|
---|
600 | return Action::failure;
|
---|
601 |
|
---|
602 | // Phase Six: calculate result
|
---|
603 | vmgcontroller.waitforResults(fragmentData.size()+full_sample.size());
|
---|
604 | std::map<JobId_t, VMGData> longrangeData;
|
---|
605 | vmgcontroller.getResults(longrangeData);
|
---|
606 | ASSERT( fragmentData.size()+full_sample.size() == longrangeData.size(),
|
---|
607 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
|
---|
608 | +toString(full_sample.size())+" "+toString(fragmentData.size()+full_sample.size())
|
---|
609 | +" and VMGresults "+toString(longrangeData.size())+" don't match.");
|
---|
610 |
|
---|
611 | // remove full solution corresponding to full_sample from map (must be highest ids), has to be treated extra
|
---|
612 | std::map<JobId_t, VMGData>::iterator iter = longrangeData.end();
|
---|
613 | for (size_t i=0;i<full_sample.size();++i)
|
---|
614 | --iter;
|
---|
615 | std::map<JobId_t, VMGData>::iterator remove_iter = iter;
|
---|
616 | std::vector<VMGData> fullsolutionData;
|
---|
617 | for (; iter != longrangeData.end(); ++iter)
|
---|
618 | fullsolutionData.push_back(iter->second);
|
---|
619 | longrangeData.erase(remove_iter, longrangeData.end());
|
---|
620 |
|
---|
621 | // Final phase: print result
|
---|
622 | {
|
---|
623 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
---|
624 | printReceivedFullResults(
|
---|
625 | fragmentData,
|
---|
626 | longrangeData,
|
---|
627 | fullsolutionData,
|
---|
628 | params.path.get(),
|
---|
629 | getNoAtomsFromAdjacencyFile(params.path.get()),
|
---|
630 | full_sample);
|
---|
631 |
|
---|
632 | if (!full_sample.empty()) {
|
---|
633 | // create debug jobs to print the summed-up potential to vtk files
|
---|
634 | debugcontroller.requestIds(full_sample.size());
|
---|
635 | if (!debugcontroller.createDebugJobs(full_sample))
|
---|
636 | return Action::failure;
|
---|
637 | debugcontroller.waitforResults(full_sample.size());
|
---|
638 | std::map<JobId_t, std::string> debugData;
|
---|
639 | debugcontroller.getResults(debugData);
|
---|
640 | }
|
---|
641 | }
|
---|
642 | }
|
---|
643 | #else
|
---|
644 | // Final phase: print result
|
---|
645 | {
|
---|
646 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
---|
647 | printReceivedMPQCResults(
|
---|
648 | fragmentData,
|
---|
649 | params.path.get(),
|
---|
650 | getNoAtomsFromAdjacencyFile(params.path.get()));
|
---|
651 | }
|
---|
652 | #endif
|
---|
653 | size_t Exitflag = vmgcontroller.getExitflag() + mpqccontroller.getExitflag();
|
---|
654 |
|
---|
655 | return (Exitflag == 0) ? Action::success : Action::failure;
|
---|
656 | }
|
---|
657 |
|
---|
658 | Action::state_ptr FragmentationFragmentationAutomationAction::performUndo(Action::state_ptr _state) {
|
---|
659 | return Action::success;
|
---|
660 | }
|
---|
661 |
|
---|
662 | Action::state_ptr FragmentationFragmentationAutomationAction::performRedo(Action::state_ptr _state){
|
---|
663 | return Action::success;
|
---|
664 | }
|
---|
665 |
|
---|
666 | bool FragmentationFragmentationAutomationAction::canUndo() {
|
---|
667 | return false;
|
---|
668 | }
|
---|
669 |
|
---|
670 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
|
---|
671 | return false;
|
---|
672 | }
|
---|
673 | /** =========== end of function ====================== */
|
---|