source: src/Actions/FragmentationAction/AnalyseFragmentationResultsAction.cpp@ 9274f3

Last change on this file since 9274f3 was 35e83a, checked in by Frederik Heber <heber@…>, 10 years ago

World has locked getter for homologies, used by QtHomologyList and writing Actions.

  • Property mode set to 100644
File size: 28.7 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * AnalyseFragmentationResultsAction.cpp
26 *
27 * Created on: Mar 8, 2013
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36// include headers that implement a archive in simple text format
37// and before MemDebug due to placement new
38#include <boost/archive/text_oarchive.hpp>
39#include <boost/archive/text_iarchive.hpp>
40
41#include "CodePatterns/MemDebug.hpp"
42
43#include <boost/foreach.hpp>
44#include <boost/lambda/lambda.hpp>
45#include <boost/mpl/remove.hpp>
46
47#include <algorithm>
48#include <fstream>
49#include <iostream>
50//#include <numeric>
51#include <string>
52#include <vector>
53
54#include "CodePatterns/Assert.hpp"
55#include "CodePatterns/Log.hpp"
56
57#ifdef HAVE_JOBMARKET
58#include "JobMarket/types.hpp"
59#else
60typedef size_t JobId_t;
61#endif
62
63#include "Descriptors/AtomIdDescriptor.hpp"
64#include "Fragmentation/Summation/Containers/FragmentationChargeDensity.hpp"
65#include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
66#include "Fragmentation/Summation/Containers/FragmentationShortRangeResults.hpp"
67#include "Fragmentation/Summation/Containers/MPQCData.hpp"
68#include "Fragmentation/Summation/Containers/MPQCData_printKeyNames.hpp"
69#include "Fragmentation/Homology/HomologyContainer.hpp"
70#include "Fragmentation/Homology/HomologyGraph.hpp"
71#include "Fragmentation/KeySetsContainer.hpp"
72#include "Fragmentation/Summation/SetValues/Eigenvalues.hpp"
73#include "Fragmentation/Summation/SetValues/Fragment.hpp"
74#include "Fragmentation/Summation/SetValues/FragmentForces.hpp"
75#include "Fragmentation/Summation/SetValues/Histogram.hpp"
76#include "Fragmentation/Summation/SetValues/IndexedVectors.hpp"
77#include "Fragmentation/Summation/IndexSetContainer.hpp"
78#include "Fragmentation/Summation/writeIndexedTable.hpp"
79#include "Fragmentation/Summation/writeTable.hpp"
80#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
81#include "Fragmentation/Summation/Containers/FragmentationLongRangeResults.hpp"
82#include "Fragmentation/Summation/Containers/VMGData.hpp"
83#include "Fragmentation/Summation/Containers/VMGDataFused.hpp"
84#include "Fragmentation/Summation/Containers/VMGDataMap.hpp"
85#include "Fragmentation/Summation/Containers/VMGData_printKeyNames.hpp"
86#endif
87#include "Helpers/defs.hpp"
88#include "World.hpp"
89
90#include "Actions/FragmentationAction/AnalyseFragmentationResultsAction.hpp"
91
92using namespace MoleCuilder;
93
94// and construct the stuff
95#include "AnalyseFragmentationResultsAction.def"
96#include "Action_impl_pre.hpp"
97/** =========== define the function ====================== */
98
99void writeToFile(const std::string &filename, const std::string contents)
100{
101 std::ofstream tablefile(filename.c_str());
102 tablefile << contents;
103 tablefile.close();
104}
105
106/** Print cycle correction from received results.
107 *
108 * @param results summed up results container
109 */
110void printReceivedCycleResults(
111 const FragmentationShortRangeResults &results)
112{
113 typedef boost::mpl::remove<
114 boost::mpl::remove<MPQCDataEnergyVector_t, MPQCDataFused::energy_eigenvalues>::type,
115 MPQCDataFused::energy_eigenhistogram>::type
116 MPQCDataEnergyVector_noeigenvalues_t;
117 const std::string energyresult =
118 writeTable<MPQCDataEnergyMap_t, MPQCDataEnergyVector_noeigenvalues_t >()(
119 results.Result_Energy_fused, results.getMaxLevel());
120 LOG(2, "DEBUG: Energy table is \n" << energyresult);
121 std::string filename;
122 filename += FRAGMENTPREFIX + std::string("_CycleEnergy.dat");
123 writeToFile(filename, energyresult);
124}
125
126/** Print (short range) energy, forces, and timings from received results per index set.
127 *
128 * @param results summed up results container
129 */
130void printReceivedShortResultsPerIndex(
131 const FragmentationShortRangeResults &results)
132{
133 // print tables per keyset(without eigenvalues, they go extra)
134 typedef boost::mpl::remove<
135 boost::mpl::remove<MPQCDataEnergyVector_t, MPQCDataFused::energy_eigenvalues>::type,
136 MPQCDataFused::energy_eigenhistogram>::type
137 MPQCDataEnergyVector_noeigenvalues_t;
138 const std::string energyresult =
139 writeIndexedTable<MPQCDataEnergyMap_t, MPQCDataEnergyVector_noeigenvalues_t >()(
140 results.Result_perIndexSet_Energy, results.getMaxLevel());
141 LOG(2, "DEBUG: Indexed Energy table is \n" << energyresult);
142 std::string filename;
143 filename += FRAGMENTPREFIX + std::string("_IndexedEnergy.dat");
144 writeToFile(filename, energyresult);
145}
146
147/** Print (short range) energy, forces, and timings from received results.
148 *
149 * @param results summed up results container
150 */
151void printReceivedShortResults(
152 const FragmentationShortRangeResults &results)
153{
154 // print tables (without eigenvalues, they go extra)
155 {
156 typedef boost::mpl::remove<
157 boost::mpl::remove<MPQCDataEnergyVector_t, MPQCDataFused::energy_eigenvalues>::type,
158 MPQCDataFused::energy_eigenhistogram>::type
159 MPQCDataEnergyVector_noeigenvalues_t;
160 const std::string energyresult =
161 writeTable<MPQCDataEnergyMap_t, MPQCDataEnergyVector_noeigenvalues_t >()(
162 results.Result_Energy_fused, results.getMaxLevel());
163 LOG(2, "DEBUG: Energy table is \n" << energyresult);
164 std::string filename;
165 filename += FRAGMENTPREFIX + std::string("_Energy.dat");
166 writeToFile(filename, energyresult);
167 }
168
169 {
170 typedef boost::mpl::list<
171 MPQCDataFused::energy_eigenvalues
172 > MPQCDataEigenvalues_t;
173 const std::string eigenvalueresult =
174 writeTable<MPQCDataEnergyMap_t, MPQCDataEigenvalues_t >()(
175 results.Result_Energy_fused, results.getMaxLevel());
176 LOG(2, "DEBUG: Eigenvalue table is \n" << eigenvalueresult);
177 std::string filename;
178 filename += FRAGMENTPREFIX + std::string("_Eigenvalues.dat");
179 writeToFile(filename, eigenvalueresult);
180 }
181
182 {
183 typedef boost::mpl::list<
184 MPQCDataFused::energy_eigenhistogram
185 > MPQCDataEigenhistogram_t;
186 const std::string eigenhistogramresult =
187 writeTable<MPQCDataEnergyMap_t, MPQCDataEigenhistogram_t >()(
188 results.Result_Energy_fused, results.getMaxLevel());
189 LOG(2, "DEBUG: Eigenhistogram table is \n" << eigenhistogramresult);
190 std::string filename;
191 filename += FRAGMENTPREFIX + std::string("_Eigenhistogram.dat");
192 writeToFile(filename, eigenhistogramresult);
193 }
194
195 {
196 typedef boost::mpl::list<
197 MPQCDataFused::energy_eigenvalues
198 > MPQCDataEigenvalues_t;
199 const std::string eigenvalueresult =
200 writeTable<MPQCDataEnergyMap_t, MPQCDataEigenvalues_t >()(
201 results.Result_Energy_fused, results.getMaxLevel());
202 LOG(2, "DEBUG: Eigenvalue table is \n" << eigenvalueresult);
203 std::string filename;
204 filename += FRAGMENTPREFIX + std::string("_Eigenvalues.dat");
205 writeToFile(filename, eigenvalueresult);
206 }
207
208 {
209 const std::string forceresult =
210 writeTable<MPQCDataForceMap_t, MPQCDataForceVector_t>()(
211 results.Result_Force_fused, results.getMaxLevel());
212 LOG(2, "DEBUG: Force table is \n" << forceresult);
213 std::string filename;
214 filename += FRAGMENTPREFIX + std::string("_Forces.dat");
215 writeToFile(filename, forceresult);
216 }
217 // we don't want to print grid to a table
218 {
219 // print times (without flops for now)
220 typedef boost::mpl::remove<
221 boost::mpl::remove<MPQCDataTimeVector_t, MPQCDataFused::times_total_flops>::type,
222 MPQCDataFused::times_gather_flops>::type
223 MPQCDataTimeVector_noflops_t;
224 const std::string timesresult =
225 writeTable<MPQCDataTimeMap_t, MPQCDataTimeVector_noflops_t >()(
226 results.Result_Time_fused, results.getMaxLevel());
227 LOG(2, "DEBUG: Times table is \n" << timesresult);
228 std::string filename;
229 filename += FRAGMENTPREFIX + std::string("_Times.dat");
230 writeToFile(filename, timesresult);
231 }
232}
233
234
235#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
236/** Print long range energy from received results.
237 *
238 * @param results summed up results container
239 */
240void printReceivedFullResults(
241 const FragmentationLongRangeResults &results)
242{
243 // print tables per keyset(without grids, they go extra)
244
245 {
246 const std::string gridresult =
247 writeTable<VMGDataMap_t, VMGDataVector_t >()(
248 results.Result_LongRange_fused, results.getMaxLevel(), 1);
249 LOG(2, "DEBUG: VMG table is \n" << gridresult);
250 std::string filename;
251 filename += FRAGMENTPREFIX + std::string("_VMGEnergy.dat");
252 writeToFile(filename, gridresult);
253 }
254
255 if (results.hasLongRangeForces()) {
256 const std::string forceresult =
257 writeTable<VMGDataForceMap_t, VMGDataForceVector_t>()(
258 results.Result_ForceLongRange_fused, results.getMaxLevel());
259 LOG(2, "DEBUG: Force table is \n" << forceresult);
260 std::string filename;
261 filename += FRAGMENTPREFIX + std::string("_VMGForces.dat");
262 writeToFile(filename, forceresult);
263 }
264
265 {
266 const std::string gridresult =
267 writeTable<VMGDataLongRangeMap_t, VMGDataLongRangeVector_t >()(
268 results.Result_LongRangeIntegrated_fused, results.getMaxLevel(), 1);
269 LOG(2, "DEBUG: LongRange table is \n" << gridresult);
270 std::string filename;
271 filename += FRAGMENTPREFIX + std::string("_LongRangeEnergy.dat");
272 writeToFile(filename, gridresult);
273 }
274
275 if (results.hasLongRangeForces()) {
276 const std::string forceresult =
277 writeTable<VMGDataLongRangeForceMap_t, VMGDataLongRangeForceVector_t >()(
278 results.Result_ForcesLongRangeIntegrated_fused, results.getMaxLevel(), 1);
279 LOG(2, "DEBUG: ForcesLongRange table is \n" << forceresult);
280 std::string filename;
281 filename += FRAGMENTPREFIX + std::string("_LongRangeForces.dat");
282 writeToFile(filename, forceresult);
283 }
284}
285#endif
286
287void appendToHomologies(
288 const FragmentationShortRangeResults &shortrangeresults,
289#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
290 const FragmentationLongRangeResults &longrangeresults,
291#endif
292 const bool storeGrids
293 )
294{
295 /// append all fragments to a HomologyContainer
296 HomologyContainer::container_t values;
297
298 // convert KeySetContainer to IndexSetContainer
299 IndexSetContainer::ptr ForceContainer(new IndexSetContainer(shortrangeresults.getForceKeySet()));
300 const IndexSetContainer::Container_t &Indices = shortrangeresults.getContainer();
301 const IndexSetContainer::Container_t &ForceIndices = ForceContainer->getContainer();
302 IndexSetContainer::Container_t::const_iterator iter = Indices.begin();
303 IndexSetContainer::Container_t::const_iterator forceiter = ForceIndices.begin();
304
305 /// go through all fragments
306 for (;iter != Indices.end(); ++iter, ++forceiter) // go through each IndexSet
307 {
308 /// create new graph entry in HomologyContainer which is (key,value) type
309 LOG(1, "INFO: Creating new graph with " << **forceiter << ".");
310 HomologyGraph graph(**forceiter);
311 LOG(2, "DEBUG: Created graph " << graph << ".");
312 const IndexSet::ptr &index = *iter;
313
314 /// we fill the value structure
315 HomologyContainer::value_t value;
316 value.containsGrids = storeGrids;
317 // obtain fragment
318 std::map<IndexSet::ptr, std::pair< MPQCDataFragmentMap_t, MPQCDataFragmentMap_t> >::const_iterator fragmentiter
319 = shortrangeresults.Result_perIndexSet_Fragment.find(index);
320 ASSERT( fragmentiter != shortrangeresults.Result_perIndexSet_Fragment.end(),
321 "appendToHomologyFile() - cannot find index "+toString(*index)
322 +" in FragmentResults.");
323 value.fragment = boost::fusion::at_key<MPQCDataFused::fragment>(fragmentiter->second.first);
324
325 // obtain energy
326 std::map<IndexSet::ptr, std::pair< MPQCDataEnergyMap_t, MPQCDataEnergyMap_t> >::const_iterator energyiter
327 = shortrangeresults.Result_perIndexSet_Energy.find(index);
328 ASSERT( energyiter != shortrangeresults.Result_perIndexSet_Energy.end(),
329 "appendToHomologyFile() - cannot find index "+toString(*index)
330 +" in FragmentResults.");
331 value.energy = boost::fusion::at_key<MPQCDataFused::energy_total>(energyiter->second.second); // contributions
332
333 // only store sampled grids if desired
334 if (storeGrids) {
335#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
336 // obtain charge distribution
337 std::map<IndexSet::ptr, std::pair< MPQCDataGridMap_t, MPQCDataGridMap_t> >::const_iterator chargeiter
338 = longrangeresults.Result_perIndexSet_Grid.find(index);
339 ASSERT( chargeiter != longrangeresults.Result_perIndexSet_Grid.end(),
340 "appendToHomologyFile() - cannot find index "+toString(*index)
341 +" in FragmentResults.");
342 value.charge_distribution = boost::fusion::at_key<MPQCDataFused::sampled_grid>(chargeiter->second.second); // contributions
343
344 // obtain potential distribution
345 std::map<IndexSet::ptr, std::pair< VMGDataGridMap_t, VMGDataGridMap_t> >::const_iterator potentialiter
346 = longrangeresults.Result_perIndexSet_LongRange_Grid.find(index);
347 ASSERT( potentialiter != longrangeresults.Result_perIndexSet_LongRange_Grid.end(),
348 "appendToHomologyFile() - cannot find index "+toString(*index)
349 +" in FragmentResults.");
350 // add e+n potentials
351 value.potential_distribution =
352 boost::fusion::at_key<VMGDataFused::both_sampled_potential>(potentialiter->second.second); // contributions
353// // and re-average to zero (integral is times volume_element which we don't need here)
354// const double sum =
355// std::accumulate(
356// value.potential_distribution.sampled_grid.begin(),
357// value.potential_distribution.sampled_grid.end(),
358// 0.);
359// const double offset = sum/(double)value.potential_distribution.sampled_grid.size();
360// for (SamplingGrid::sampledvalues_t::iterator iter = value.potential_distribution.sampled_grid.begin();
361// iter != value.potential_distribution.sampled_grid.end();
362// ++iter)
363// *iter -= offset;
364#else
365 ELOG(1, "Long-range information in homology desired but long-range analysis capability not compiled in.");
366#endif
367 }
368 values.insert( std::make_pair( graph, value) );
369 }
370 {
371 AtomicInstance<HomologyContainer> homology_container = World::getInstance().getLockedHomologies();
372 (*homology_container).insert(values);
373 }
374
375 if (DoLog(2)) {
376 LOG(2, "DEBUG: Listing all present atomic ids ...");
377 std::stringstream output;
378 for (World::AtomIterator iter = World::getInstance().getAtomIter();
379 iter != World::getInstance().atomEnd(); ++iter)
380 output << (*iter)->getId() << " ";
381 LOG(2, "DEBUG: { " << output.str() << "} .");
382 }
383
384 // for debugging: print container
385 if (DoLog(2)) {
386 LOG(2, "DEBUG: Listing all present homologies ...");
387 const HomologyContainer& homology_container = World::getConstInstance().getHomologies();
388 for (HomologyContainer::container_t::const_iterator iter =
389 homology_container.begin(); iter != homology_container.end(); ++iter) {
390 std::stringstream output;
391 output << "DEBUG: graph " << iter->first
392 << " has Fragment " << iter->second.fragment
393 << ", associated energy " << iter->second.energy;
394 if (iter->second.containsGrids)
395#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
396 output << ", and sampled grid integral " << iter->second.charge_distribution.integral();
397#else
398 output << ", and there are sampled grids but capability not compiled in";
399#endif
400 output << ".";
401 LOG(2, output.str());
402 }
403 }
404}
405
406// this it taken from
407// http://stackoverflow.com/questions/2291802/is-there-a-c-iterator-that-can-iterate-over-a-file-line-by-line
408namespace detail
409{
410 /** Extend the string class by a friend function.
411 *
412 */
413 class Line : public std::string
414 {
415 friend std::istream & operator>>(std::istream & is, Line & line)
416 {
417 return std::getline(is, line);
418 }
419 };
420}
421
422/** Parse the given stream line-by-line, passing each to \a dest.
423 *
424 * \param is stream to parse line-wise
425 * \param dest output iterator
426 */
427template<class OutIt>
428void read_lines(std::istream& is, OutIt dest)
429{
430 typedef std::istream_iterator<detail::Line> InIt;
431 std::copy(InIt(is), InIt(), dest);
432}
433
434
435/** Determines the largest cycle in the container and returns its size.
436 *
437 * \param cycles set of cycles
438 * \return size if largest cycle
439 */
440size_t getMaxCycleSize(const KeySetsContainer &cycles)
441{
442 // gather cycle sizes
443 std::vector<size_t> cyclesizes(cycles.KeySets.size());
444 std::transform(
445 cycles.KeySets.begin(), cycles.KeySets.end(),
446 cyclesizes.begin(),
447 boost::bind(&KeySetsContainer::IntVector::size, boost::lambda::_1)
448 );
449 // get maximum
450 std::vector<size_t>::const_iterator maximum_size =
451 std::max_element(cyclesizes.begin(), cyclesizes.end());
452 if (maximum_size != cyclesizes.end())
453 return *maximum_size;
454 else
455 return 0;
456}
457
458void calculateCycleFullContribution(
459 const std::map<JobId_t, MPQCData> &shortrangedata,
460 const KeySetsContainer &keysets,
461 const KeySetsContainer &forcekeysets,
462 const KeySetsContainer &cycles,
463 const FragmentationShortRangeResults &shortrangeresults)
464{
465 // copy the shortrangeresults such that private MaxLevel is set in
466 // FragmentationShortRangeResults
467 FragmentationShortRangeResults cycleresults(shortrangeresults);
468 // get largest size
469 const size_t maximum_size = getMaxCycleSize(cycles);
470
471 /// The idea here is that (Orthogonal)Summation will place a result
472 /// consisting of level 1,2, and 3 fragment and a level 6 ring nonetheless
473 /// in level 6. If we want to have this result already at level 3, we
474 /// have to specifically inhibit all fragments from later levels but the
475 /// cycles and then pick the result from the last level and placing it at
476 /// the desired one
477
478 // loop from level 1 to max ring size and gather contributions
479 for (size_t level = 1; level <= maximum_size; ++level) {
480 // create ValueMask for this level by stepping through each keyset and checking size
481 std::vector<bool> localValueMask(shortrangedata.size(), false);
482 size_t index=0;
483 // TODO: if only KeySetsContainer was usable as a compliant STL container, might be able to use set_difference or alike.
484 KeySetsContainer::ArrayOfIntVectors::const_iterator keysetsiter = keysets.KeySets.begin();
485 KeySetsContainer::ArrayOfIntVectors::const_iterator cyclesiter = cycles.KeySets.begin();
486 for (; (keysetsiter != keysets.KeySets.end()) && (cyclesiter != cycles.KeySets.end());) {
487 if (cyclesiter->size() > keysetsiter->size()) {
488 // add if not greater than level in size
489 if ((*keysetsiter).size() <= level)
490 localValueMask[index] = true;
491 ++keysetsiter;
492 ++index;
493 } else if (cyclesiter->size() < keysetsiter->size()) {
494 ++cyclesiter;
495 } else { // both sets have same size
496 if (*cyclesiter > *keysetsiter) {
497 // add if not greater than level in size
498 if ((*keysetsiter).size() <= level)
499 localValueMask[index] = true;
500 ++keysetsiter;
501 ++index;
502 } else if (*cyclesiter < *keysetsiter) {
503 ++cyclesiter;
504 } else {
505 // also always add all cycles
506 localValueMask[index] = true;
507 ++cyclesiter;
508 ++keysetsiter;
509 ++index;
510 }
511 }
512 }
513 // activate rest if desired by level
514 for (; keysetsiter != keysets.KeySets.end(); ++keysetsiter) {
515 if ((*keysetsiter).size() <= level)
516 localValueMask[index] = true;
517 ++index;
518 }
519 LOG(2, "DEBUG: ValueMask for cycle correction at level " << level << " is "
520 << localValueMask << ".");
521 // create FragmentationShortRangeResults
522 FragmentationShortRangeResults localresults(shortrangedata, keysets, forcekeysets, localValueMask);
523 // and perform summation
524 localresults(shortrangedata);
525 // finally, extract the corrections from last level
526 cycleresults.Result_Energy_fused[level-1] =
527 localresults.Result_Energy_fused.back();
528 cycleresults.Result_Time_fused[level-1] =
529 localresults.Result_Time_fused.back();
530 cycleresults.Result_Force_fused[level-1] =
531 localresults.Result_Force_fused.back();
532 }
533 printReceivedCycleResults(cycleresults);
534}
535
536static void AddForces(
537 const IndexedVectors::indexedvectors_t &_forces,
538 const bool _IsAngstroem)
539{
540 for(IndexedVectors::indexedvectors_t::const_iterator iter = _forces.begin();
541 iter != _forces.end(); ++iter) {
542 const IndexedVectors::index_t &index = iter->first;
543 const IndexedVectors::vector_t &forcevector = iter->second;
544 ASSERT( forcevector.size() == NDIM,
545 "printReceivedShortResults() - obtained force vector has incorrect dimension.");
546 // note that mpqc calculates a gradient, hence force pointing into opposite direction
547 // we have to mind different units here: MPQC has a_o, while we may have angstroem
548 Vector ForceVector(-forcevector[0], -forcevector[1], -forcevector[2]);
549 if (_IsAngstroem)
550 for (size_t i=0;i<NDIM;++i)
551 ForceVector[i] *= AtomicLengthToAngstroem;
552 atom *_atom = World::getInstance().getAtom(AtomById(index));
553 if(_atom != NULL)
554 _atom->setAtomicForce(_atom->getAtomicForce() + ForceVector);
555 else
556 ELOG(2, "Could not find atom to id " << index << ".");
557 }
558}
559
560ActionState::ptr FragmentationAnalyseFragmentationResultsAction::performCall() {
561
562 /// if file is given, parse from file into ResultsContainer
563 {
564 AtomicInstance<FragmentationResultContainer> container = FragmentationResultContainer::getLockedInstance();
565 if (!params.resultsfile.get().empty()) {
566 boost::filesystem::path resultsfile = params.resultsfile.get();
567 if (boost::filesystem::exists(resultsfile)) {
568 LOG(1, "INFO: Parsing results from " << resultsfile.string() << ".");
569 std::ifstream returnstream(resultsfile.string().c_str());
570 if (returnstream.good()) {
571 boost::archive::text_iarchive ia(returnstream);
572 ia >> *container;
573 }
574 } else {
575 ELOG(1, "Given file" << resultsfile.string() << " does not exist.");
576 }
577 }
578 }
579 /// get data and keysets from ResultsContainer
580 const FragmentationResultContainer& container = FragmentationResultContainer::getConstInstance();
581 const std::map<JobId_t, MPQCData> &shortrangedata = container.getShortRangeResults();
582 const KeySetsContainer &keysets = container.getKeySets();
583 const KeySetsContainer &forcekeysets = container.getForceKeySets();
584 const bool DoLongrange = container.areFullRangeResultsPresent();
585 const bool IsAngstroem = true;
586
587 if (keysets.KeySets.empty()) {
588 STATUS("There are no results in the container.");
589 return Action::failure;
590 }
591
592 /// calculate normal contributions with (if present) cycles coming at their
593 /// respective bond order.
594 std::vector<bool> ValueMask(shortrangedata.size(), true);
595 FragmentationShortRangeResults shortrangeresults(shortrangedata, keysets, forcekeysets, ValueMask);
596 shortrangeresults(shortrangedata);
597 printReceivedShortResults(shortrangeresults);
598 printReceivedShortResultsPerIndex(shortrangeresults);
599 // add summed results to container
600 {
601 AtomicInstance<FragmentationResultContainer> container = FragmentationResultContainer::getLockedInstance();
602 (*container).addShortRangeSummedResults(shortrangeresults.getSummedShortRangeResults());
603 }
604
605 /// now do we need to calculate the cycle contribution
606 // check whether there are cycles in container or else in file
607 KeySetsContainer cycles = container.getCycles();
608 if (cycles.KeySets.empty()) {
609 // parse from file if cycles is empty
610 boost::filesystem::path filename(
611 params.prefix.get() + std::string(CYCLEKEYSETFILE));
612 if (boost::filesystem::exists(filename)) {
613 LOG(1, "INFO: Parsing cycles file " << filename.string() << ".");
614 // parse file line by line
615 std::ifstream File;
616 File.open(filename.string().c_str());
617 typedef std::istream_iterator<detail::Line> InIt;
618 for (InIt iter = InIt(File); iter != InIt(); ++iter) {
619 KeySetsContainer::IntVector cycle;
620 std::stringstream line(*iter);
621 while (line.good()) {
622 int id;
623 line >> id >> ws;
624 cycle.push_back(id);
625 }
626 if (!cycle.empty()) {
627 LOG(2, "DEBUG: Adding cycle " << cycle << ".");
628 cycles.insert( cycle, cycle.size());
629 }
630 }
631 File.close();
632 } else {
633 LOG(1, "INFO: Cycles file not found at " << filename.string() << ".");
634 }
635 }
636
637 // calculate energy if cycles are calculated fully at each level already
638 if (!cycles.KeySets.empty()) {
639 calculateCycleFullContribution(
640 shortrangedata,
641 keysets,
642 forcekeysets,
643 cycles,
644 shortrangeresults);
645 }
646
647 // adding obtained forces
648 if ( const_cast<const World &>(World::getInstance()).getAllAtoms().size() != 0) {
649 const IndexedVectors::indexedvectors_t shortrange_forces =
650 boost::fusion::at_key<MPQCDataFused::forces>(
651 shortrangeresults.Result_Force_fused.back()
652 ).getVectors();
653 AddForces(shortrange_forces,IsAngstroem);
654 } else {
655 LOG(1, "INFO: Full molecule not loaded, hence will not add forces to atoms.");
656 }
657
658#if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
659 if (DoLongrange) {
660 if ( const_cast<const World &>(World::getInstance()).getAllAtoms().size() == 0) {
661 STATUS("Please load the full molecule into std::map<JobId_t, VMGData> longrangeData the world before starting this action.");
662 return Action::failure;
663 }
664
665 FragmentationChargeDensity summedChargeDensity(shortrangedata,keysets);
666 const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
667
668 std::map<JobId_t, VMGData> longrangeData = container.getLongRangeResults();
669 // remove full solution corresponding to full_sample from map (must be highest ids), has to be treated extra
670 std::map<JobId_t, VMGData>::iterator iter = longrangeData.end();
671 std::advance(iter, -full_sample.size());
672 std::map<JobId_t, VMGData>::iterator remove_iter = iter;
673 std::vector<VMGData> fullsolutionData;
674 for (; iter != longrangeData.end(); ++iter)
675 fullsolutionData.push_back(iter->second);
676 if (longrangeData.size() > 1) // when there's just a single fragment, it corresponds to full solution
677 longrangeData.erase(remove_iter, longrangeData.end());
678
679 // Final phase: sum up and print result
680 FragmentationLongRangeResults longrangeresults(
681 shortrangedata,longrangeData,keysets, forcekeysets);
682 longrangeresults(
683 shortrangedata,
684 longrangeData,
685 fullsolutionData,
686 full_sample);
687 printReceivedFullResults(longrangeresults);
688
689 // add long-range forces
690 if ( const_cast<const World &>(World::getInstance()).getAllAtoms().size() != 0) {
691 const IndexedVectors::indexedvectors_t longrange_forces =
692 boost::fusion::at_key<VMGDataFused::forces_longrange>(
693 longrangeresults.Result_ForcesLongRangeIntegrated_fused.back()
694 ).getVectors();
695 AddForces(longrange_forces,IsAngstroem);
696 } else {
697 LOG(1, "INFO: Full molecule not loaded, hence will not add forces to atoms.");
698 }
699
700 // append all keysets to homology file
701 appendToHomologies(shortrangeresults, longrangeresults, params.DoStoreGrids.get());
702 } else {
703 // append all keysets to homology file with short-range info only (without grids)
704 std::map<JobId_t, VMGData> longrangeData;
705 FragmentationLongRangeResults longrangeresults(
706 shortrangedata,longrangeData,keysets, forcekeysets);
707 appendToHomologies(shortrangeresults, longrangeresults, false);
708 }
709#else
710 if (DoLongrange) {
711 ELOG(2, "File contains long-range information but long-range analysis capability not compiled in.");
712 }
713
714 // append all keysets to homology file with short-range info only (without grids)
715 appendToHomologies(shortrangeresults, false);
716#endif
717
718 // we no longer clear the container
719// container.clear();
720
721 return Action::success;
722}
723
724ActionState::ptr FragmentationAnalyseFragmentationResultsAction::performUndo(ActionState::ptr _state) {
725 return Action::success;
726}
727
728ActionState::ptr FragmentationAnalyseFragmentationResultsAction::performRedo(ActionState::ptr _state){
729 return Action::success;
730}
731
732bool FragmentationAnalyseFragmentationResultsAction::canUndo() {
733 return false;
734}
735
736bool FragmentationAnalyseFragmentationResultsAction::shouldUndo() {
737 return false;
738}
739/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.