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