| [edecba] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 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/>.
 | 
|---|
| [edecba] | 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 | 
 | 
|---|
| [240c3e] | 41 | // include headers that implement a archive in simple text format
 | 
|---|
 | 42 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
 | 43 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | 
 | 
|---|
| [fb881a] | 46 | #include <boost/mpl/remove.hpp>
 | 
|---|
| [c9f9bb] | 47 | #include <boost/lambda/lambda.hpp>
 | 
|---|
| [fb881a] | 48 | 
 | 
|---|
| [240c3e] | 49 | #include <iostream>
 | 
|---|
 | 50 | 
 | 
|---|
| [6ca578] | 51 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [edecba] | 52 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 53 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 54 | #include "JobMarket/Jobs/FragmentJob.hpp"
 | 
|---|
 | 55 | 
 | 
|---|
| [184615] | 56 | #include "Fragmentation/Automation/createMatrixNrLookup.hpp"
 | 
|---|
| [447481] | 57 | #include "Fragmentation/Automation/extractJobIds.hpp"
 | 
|---|
| [27594e] | 58 | #include "Fragmentation/Automation/FragmentationChargeDensity.hpp"
 | 
|---|
| [376a3b] | 59 | #include "Fragmentation/Automation/FragmentationResults.hpp"
 | 
|---|
| [ffe057] | 60 | #include "Fragmentation/Automation/MPQCFragmentController.hpp"
 | 
|---|
| [17c03f] | 61 | #include "Fragmentation/Automation/parseKeySetFile.hpp"
 | 
|---|
| [238868] | 62 | #include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp"
 | 
|---|
| [ffe057] | 63 | #include "Fragmentation/Automation/VMGFragmentController.hpp"
 | 
|---|
| [edecba] | 64 | #include "Fragmentation/EnergyMatrix.hpp"
 | 
|---|
 | 65 | #include "Fragmentation/ForceMatrix.hpp"
 | 
|---|
 | 66 | #include "Fragmentation/Fragmentation.hpp"
 | 
|---|
| [240c3e] | 67 | #include "Fragmentation/Homology/HomologyContainer.hpp"
 | 
|---|
 | 68 | #include "Fragmentation/Homology/HomologyGraph.hpp"
 | 
|---|
| [766767] | 69 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
 | 70 | #include "Fragmentation/KeySetsContainer.hpp"
 | 
|---|
| [184943] | 71 | #include "Fragmentation/SetValues/Fragment.hpp"
 | 
|---|
| [e06820] | 72 | #include "Fragmentation/SetValues/Histogram.hpp"
 | 
|---|
| [e13990b] | 73 | #include "Fragmentation/SetValues/IndexedVectors.hpp"
 | 
|---|
| [766767] | 74 | #include "Fragmentation/Summation/IndexSetContainer.hpp"
 | 
|---|
| [27afbf] | 75 | #include "Fragmentation/Summation/writeTable.hpp"
 | 
|---|
| [edecba] | 76 | #include "Graph/DepthFirstSearchAnalysis.hpp"
 | 
|---|
| [28e894] | 77 | #include "Jobs/MPQCJob.hpp"
 | 
|---|
| [4bc75d] | 78 | #include "Jobs/MPQCData.hpp"
 | 
|---|
| [1f66c7] | 79 | #include "Jobs/MPQCData_printKeyNames.hpp"
 | 
|---|
| [69c733] | 80 | #ifdef HAVE_VMG
 | 
|---|
| [238868] | 81 | #include "Jobs/VMGDebugGridJob.hpp"
 | 
|---|
| [69c733] | 82 | #include "Jobs/VMGJob.hpp"
 | 
|---|
| [358ddb] | 83 | #include "Jobs/VMGData.hpp"
 | 
|---|
 | 84 | #include "Jobs/VMGDataFused.hpp"
 | 
|---|
 | 85 | #include "Jobs/VMGDataMap.hpp"
 | 
|---|
 | 86 | #include "Jobs/VMGData_printKeyNames.hpp"
 | 
|---|
| [69c733] | 87 | #endif
 | 
|---|
| [edecba] | 88 | #include "World.hpp"
 | 
|---|
 | 89 | 
 | 
|---|
| [86cfac5] | 90 | #include <fstream>
 | 
|---|
| [edecba] | 91 | #include <iostream>
 | 
|---|
 | 92 | #include <string>
 | 
|---|
 | 93 | #include <vector>
 | 
|---|
 | 94 | 
 | 
|---|
| [8cae4c] | 95 | #include <boost/mpl/for_each.hpp>
 | 
|---|
 | 96 | 
 | 
|---|
| [edecba] | 97 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 | using namespace MoleCuilder;
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | // and construct the stuff
 | 
|---|
 | 102 | #include "FragmentationAutomationAction.def"
 | 
|---|
 | 103 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 104 | /** =========== define the function ====================== */
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | class controller_AddOn;
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | // needs to be defined for using the FragmentController
 | 
|---|
 | 109 | controller_AddOn *getAddOn()
 | 
|---|
 | 110 | {
 | 
|---|
 | 111 |   return NULL;
 | 
|---|
 | 112 | }
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 | /** Helper function to get number of atoms somehow.
 | 
|---|
 | 115 |  *
 | 
|---|
 | 116 |  * Here, we just parse the number of lines in the adjacency file as
 | 
|---|
 | 117 |  * it should correspond to the number of atoms, except when some atoms
 | 
|---|
 | 118 |  * are not bonded, but then fragmentation makes no sense.
 | 
|---|
 | 119 |  *
 | 
|---|
 | 120 |  * @param path path to the adjacency file
 | 
|---|
 | 121 |  */
 | 
|---|
 | 122 | size_t getNoAtomsFromAdjacencyFile(const std::string &path)
 | 
|---|
 | 123 | {
 | 
|---|
 | 124 |   size_t NoAtoms = 0;
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 |   // parse in special file to get atom count (from line count)
 | 
|---|
 | 127 |   std::string filename(path);
 | 
|---|
 | 128 |   filename += FRAGMENTPREFIX;
 | 
|---|
 | 129 |   filename += ADJACENCYFILE;
 | 
|---|
 | 130 |   std::ifstream adjacency(filename.c_str());
 | 
|---|
 | 131 |   if (adjacency.fail()) {
 | 
|---|
 | 132 |     LOG(0, endl << "getNoAtomsFromAdjacencyFile() - Unable to open " << filename << ", is the directory correct?");
 | 
|---|
 | 133 |     return false;
 | 
|---|
 | 134 |   }
 | 
|---|
 | 135 |   std::string buffer;
 | 
|---|
 | 136 |   while (getline(adjacency, buffer))
 | 
|---|
 | 137 |     NoAtoms++;
 | 
|---|
 | 138 |   LOG(1, "INFO: There are " << NoAtoms << " atoms.");
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 |   return NoAtoms;
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 | 
 | 
|---|
| [c4ee08] | 145 | /** Place results from FragmentResult into EnergyMatrix and ForceMatrix.
 | 
|---|
 | 146 |  *
 | 
|---|
 | 147 |  * @param fragmentData MPQCData resulting from the jobs
 | 
|---|
 | 148 |  * @param MatrixNrLookup Lookup up-map from job id to fragment number
 | 
|---|
 | 149 |  * @param FragmentCounter total number of fragments
 | 
|---|
 | 150 |  * @param NoAtoms total number of atoms
 | 
|---|
 | 151 |  * @param Energy energy matrix to be filled on return
 | 
|---|
 | 152 |  * @param Force force matrix to be filled on return
 | 
|---|
 | 153 |  * @return true - everything ok, false - else
 | 
|---|
 | 154 |  */
 | 
|---|
 | 155 | bool putResultsintoMatrices(
 | 
|---|
| [c9f9bb] | 156 |     const std::map<JobId_t, MPQCData> &fragmentData,
 | 
|---|
| [3dd32f] | 157 |     const std::map< JobId_t, size_t > &MatrixNrLookup,
 | 
|---|
| [c4ee08] | 158 |     const size_t FragmentCounter,
 | 
|---|
 | 159 |     const size_t NoAtoms,
 | 
|---|
 | 160 |     EnergyMatrix &Energy,
 | 
|---|
 | 161 |     ForceMatrix &Force)
 | 
|---|
 | 162 | {
 | 
|---|
| [c9f9bb] | 163 |   for (std::map<JobId_t, MPQCData>::const_iterator dataiter = fragmentData.begin();
 | 
|---|
 | 164 |       dataiter != fragmentData.end(); ++dataiter) {
 | 
|---|
 | 165 |     const MPQCData &extractedData = dataiter->second;
 | 
|---|
 | 166 |     const JobId_t &jobid = dataiter->first;
 | 
|---|
| [3dd32f] | 167 |     std::map< JobId_t, size_t >::const_iterator nriter = MatrixNrLookup.find(jobid);
 | 
|---|
 | 168 |     ASSERT( nriter != MatrixNrLookup.end(),
 | 
|---|
 | 169 |         "putResultsintoMatrices() - MatrixNrLookup does not contain id "
 | 
|---|
 | 170 |         +toString(jobid)+".");
 | 
|---|
| [edecba] | 171 |     // place results into EnergyMatrix ...
 | 
|---|
 | 172 |     {
 | 
|---|
 | 173 |       MatrixContainer::MatrixArray matrix;
 | 
|---|
 | 174 |       matrix.resize(1);
 | 
|---|
| [a9558f] | 175 |       matrix[0].resize(1, extractedData.energies.total);
 | 
|---|
| [edecba] | 176 |       if (!Energy.AddMatrix(
 | 
|---|
| [c9f9bb] | 177 |           std::string("MPQCJob ")+toString(jobid),
 | 
|---|
| [edecba] | 178 |           matrix,
 | 
|---|
| [3dd32f] | 179 |           nriter->second)) {
 | 
|---|
| [edecba] | 180 |         ELOG(1, "Adding energy matrix failed.");
 | 
|---|
 | 181 |         return false;
 | 
|---|
 | 182 |       }
 | 
|---|
 | 183 |     }
 | 
|---|
 | 184 |     // ... and ForceMatrix (with two empty columns in front)
 | 
|---|
 | 185 |     {
 | 
|---|
 | 186 |       MatrixContainer::MatrixArray matrix;
 | 
|---|
 | 187 |       const size_t rows = extractedData.forces.size();
 | 
|---|
 | 188 |       matrix.resize(rows);
 | 
|---|
 | 189 |       for (size_t i=0;i<rows;++i) {
 | 
|---|
 | 190 |         const size_t columns = 2+extractedData.forces[i].size();
 | 
|---|
 | 191 |         matrix[i].resize(columns, 0.);
 | 
|---|
 | 192 |   //      for (size_t j=0;j<2;++j)
 | 
|---|
 | 193 |   //        matrix[i][j] = 0.;
 | 
|---|
 | 194 |         for (size_t j=2;j<columns;++j)
 | 
|---|
 | 195 |           matrix[i][j] = extractedData.forces[i][j-2];
 | 
|---|
 | 196 |       }
 | 
|---|
 | 197 |       if (!Force.AddMatrix(
 | 
|---|
| [c9f9bb] | 198 |           std::string("MPQCJob ")+toString(jobid),
 | 
|---|
| [edecba] | 199 |           matrix,
 | 
|---|
| [3dd32f] | 200 |           nriter->second)) {
 | 
|---|
| [edecba] | 201 |         ELOG(1, "Adding force matrix failed.");
 | 
|---|
 | 202 |         return false;
 | 
|---|
 | 203 |       }
 | 
|---|
 | 204 |     }
 | 
|---|
 | 205 |   }
 | 
|---|
 | 206 |   // add one more matrix (not required for energy)
 | 
|---|
 | 207 |   MatrixContainer::MatrixArray matrix;
 | 
|---|
 | 208 |   matrix.resize(1);
 | 
|---|
 | 209 |   matrix[0].resize(1, 0.);
 | 
|---|
 | 210 |   if (!Energy.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
 | 
|---|
 | 211 |     return false;
 | 
|---|
 | 212 |   // but for energy because we need to know total number of atoms
 | 
|---|
 | 213 |   matrix.resize(NoAtoms);
 | 
|---|
 | 214 |   for (size_t i = 0; i< NoAtoms; ++i)
 | 
|---|
 | 215 |     matrix[i].resize(2+NDIM, 0.);
 | 
|---|
 | 216 |   if (!Force.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
 | 
|---|
 | 217 |     return false;
 | 
|---|
 | 218 | 
 | 
|---|
| [c4ee08] | 219 |   return true;
 | 
|---|
 | 220 | }
 | 
|---|
| [849cd8] | 221 | 
 | 
|---|
 | 222 | /** Print MPQCData from received results.
 | 
|---|
 | 223 |  *
 | 
|---|
| [c9f9bb] | 224 |  * @param fragmentData MPQCData resulting from the jobs, associated to job id
 | 
|---|
| [849cd8] | 225 |  * @param KeySetFilename filename with keysets to associate forces correctly
 | 
|---|
 | 226 |  * @param NoAtoms total number of atoms
 | 
|---|
 | 227 |  * @param full_sample summed up charge from fragments on return
 | 
|---|
| [c4ee08] | 228 |  */
 | 
|---|
 | 229 | bool printReceivedMPQCResults(
 | 
|---|
| [c9f9bb] | 230 |     const std::map<JobId_t, MPQCData> &fragmentData,
 | 
|---|
| [c4ee08] | 231 |     const std::string &KeySetFilename,
 | 
|---|
| [c699f4] | 232 |     size_t NoAtoms)
 | 
|---|
| [c4ee08] | 233 | {
 | 
|---|
| [c9f9bb] | 234 |   // create a vector of all job ids
 | 
|---|
 | 235 |   std::vector<JobId_t> jobids;
 | 
|---|
 | 236 |   std::transform(fragmentData.begin(),fragmentData.end(),
 | 
|---|
 | 237 |       std::back_inserter(jobids),
 | 
|---|
 | 238 |       boost::bind( &std::map<JobId_t,MPQCData>::value_type::first, boost::lambda::_1 )
 | 
|---|
 | 239 |   );
 | 
|---|
 | 240 | 
 | 
|---|
| [c4ee08] | 241 |   // create lookup from job nr to fragment number
 | 
|---|
 | 242 |   size_t FragmentCounter = 0;
 | 
|---|
| [3dd32f] | 243 |   const std::map< JobId_t, size_t > MatrixNrLookup=
 | 
|---|
 | 244 |       createMatrixNrLookup(jobids, FragmentCounter);
 | 
|---|
| [c4ee08] | 245 | 
 | 
|---|
 | 246 |   // place results into maps
 | 
|---|
 | 247 |   EnergyMatrix Energy;
 | 
|---|
 | 248 |   ForceMatrix Force;
 | 
|---|
| [c9f9bb] | 249 |   if (!putResultsintoMatrices(fragmentData, MatrixNrLookup, FragmentCounter, NoAtoms, Energy, Force))
 | 
|---|
| [c4ee08] | 250 |     return false;
 | 
|---|
 | 251 | 
 | 
|---|
| [edecba] | 252 |   if (!Energy.InitialiseIndices()) return false;
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 |   if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
 | 
|---|
 | 255 | 
 | 
|---|
| [17c03f] | 256 |   // initialise keysets
 | 
|---|
 | 257 |   KeySetsContainer KeySet;
 | 
|---|
 | 258 |   parseKeySetFile(KeySet, KeySetFilename, FragmentCounter, NonHydrogenKeySets);
 | 
|---|
 | 259 |   KeySetsContainer ForceKeySet;
 | 
|---|
 | 260 |   parseKeySetFile(ForceKeySet, KeySetFilename, FragmentCounter, HydrogenKeySets);
 | 
|---|
| [6ca578] | 261 | 
 | 
|---|
 | 262 |   // combine all found data
 | 
|---|
| [edecba] | 263 |   if (!KeySet.ParseManyBodyTerms()) return false;
 | 
|---|
 | 264 | 
 | 
|---|
| [c4ee08] | 265 |   EnergyMatrix EnergyFragments;
 | 
|---|
 | 266 |   ForceMatrix ForceFragments;
 | 
|---|
| [edecba] | 267 |   if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false;
 | 
|---|
 | 268 |   if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false;
 | 
|---|
 | 269 | 
 | 
|---|
 | 270 |   if(!Energy.SetLastMatrix(0., 0)) return false;
 | 
|---|
 | 271 |   if(!Force.SetLastMatrix(0., 2)) return false;
 | 
|---|
 | 272 | 
 | 
|---|
 | 273 |   for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
 | 
|---|
 | 274 |     // --------- sum up energy --------------------
 | 
|---|
 | 275 |     LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
 | 
|---|
 | 276 |     if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
 | 
|---|
 | 277 |     if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
 | 
|---|
 | 278 | 
 | 
|---|
 | 279 |     // --------- sum up Forces --------------------
 | 
|---|
 | 280 |     LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
 | 
|---|
 | 281 |     if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
 | 
|---|
 | 282 |     if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
 | 
|---|
 | 283 |   }
 | 
|---|
 | 284 | 
 | 
|---|
 | 285 |   // for debugging print resulting energy and forces
 | 
|---|
 | 286 |   LOG(1, "INFO: Resulting energy is " << Energy.Matrix[ FragmentCounter ][0][0]);
 | 
|---|
 | 287 |   std::stringstream output;
 | 
|---|
 | 288 |   for (int i=0; i< Force.RowCounter[FragmentCounter]; ++i) {
 | 
|---|
 | 289 |     for (int j=0; j< Force.ColumnCounter[FragmentCounter]; ++j)
 | 
|---|
 | 290 |       output << Force.Matrix[ FragmentCounter ][i][j] << " ";
 | 
|---|
 | 291 |     output << "\n";
 | 
|---|
 | 292 |   }
 | 
|---|
 | 293 |   LOG(1, "INFO: Resulting forces are " << std::endl << output.str());
 | 
|---|
 | 294 | 
 | 
|---|
 | 295 |   return true;
 | 
|---|
 | 296 | }
 | 
|---|
 | 297 | 
 | 
|---|
| [86cfac5] | 298 | void writeToFile(const std::string &filename, const std::string contents)
 | 
|---|
 | 299 | {
 | 
|---|
 | 300 |   std::ofstream tablefile(filename.c_str());
 | 
|---|
 | 301 |   tablefile << contents;
 | 
|---|
 | 302 |   tablefile.close();
 | 
|---|
 | 303 | }
 | 
|---|
 | 304 | 
 | 
|---|
| [358ddb] | 305 | /** Print MPQCData from received results.
 | 
|---|
 | 306 |  *
 | 
|---|
| [376a3b] | 307 |  * @param results summed up results container
 | 
|---|
| [358ddb] | 308 |  */
 | 
|---|
| [376a3b] | 309 | void printReceivedFullResults(
 | 
|---|
 | 310 |     const FragmentationResults &results)
 | 
|---|
| [358ddb] | 311 | {
 | 
|---|
| [376a3b] | 312 |   // print tables (without eigenvalues, they go extra)
 | 
|---|
| [86cfac5] | 313 |   {
 | 
|---|
 | 314 |     typedef boost::mpl::remove<MPQCDataEnergyVector_t, MPQCDataFused::energy_eigenvalues>::type
 | 
|---|
 | 315 |       MPQCDataEnergyVector_noeigenvalues_t;
 | 
|---|
 | 316 |     const std::string energyresult =
 | 
|---|
 | 317 |         writeTable<MPQCDataEnergyMap_t, MPQCDataEnergyVector_noeigenvalues_t >()(
 | 
|---|
 | 318 |             results.Result_Energy_fused, results.getMaxLevel());
 | 
|---|
 | 319 |     LOG(0, "Energy table is \n" << energyresult);
 | 
|---|
 | 320 |     std::string filename;
 | 
|---|
 | 321 |     filename += FRAGMENTPREFIX + std::string("_Energy.dat");
 | 
|---|
 | 322 |     writeToFile(filename, energyresult);
 | 
|---|
 | 323 |   }
 | 
|---|
 | 324 | 
 | 
|---|
| [bf12d1] | 325 |   if (results.Result_LongRange_fused.size() >= (results.getMaxLevel()-2))
 | 
|---|
| [82cbe4] | 326 |   {
 | 
|---|
 | 327 |     const std::string gridresult =
 | 
|---|
 | 328 |         writeTable<VMGDataMap_t, VMGDataVector_t >()(
 | 
|---|
 | 329 |             results.Result_LongRange_fused, results.getMaxLevel(), 2);
 | 
|---|
 | 330 |     LOG(0, "VMG table is \n" << gridresult);
 | 
|---|
 | 331 |     std::string filename;
 | 
|---|
 | 332 |     filename += FRAGMENTPREFIX + std::string("_VMGEnergy.dat");
 | 
|---|
 | 333 |     writeToFile(filename, gridresult);
 | 
|---|
 | 334 |   }
 | 
|---|
 | 335 | 
 | 
|---|
| [bf12d1] | 336 |   if (results.Result_LongRangeIntegrated_fused.size() >= (results.getMaxLevel()-2))
 | 
|---|
| [86cfac5] | 337 |   {
 | 
|---|
 | 338 |     const std::string gridresult =
 | 
|---|
 | 339 |         writeTable<VMGDataLongRangeMap_t, VMGDataLongRangeVector_t >()(
 | 
|---|
| [d4f31f] | 340 |             results.Result_LongRangeIntegrated_fused, results.getMaxLevel(), 2);
 | 
|---|
| [86cfac5] | 341 |     LOG(0, "LongRange table is \n" << gridresult);
 | 
|---|
 | 342 |     std::string filename;
 | 
|---|
 | 343 |     filename += FRAGMENTPREFIX + std::string("_LongRangeEnergy.dat");
 | 
|---|
 | 344 |     writeToFile(filename, gridresult);
 | 
|---|
 | 345 |   }
 | 
|---|
 | 346 | 
 | 
|---|
 | 347 |   {
 | 
|---|
 | 348 |     const std::string eigenvalueresult;
 | 
|---|
 | 349 |     LOG(0, "Eigenvalue table is \n" << eigenvalueresult);
 | 
|---|
 | 350 |     std::string filename;
 | 
|---|
 | 351 |     filename += FRAGMENTPREFIX + std::string("_Eigenvalues.dat");
 | 
|---|
 | 352 |     writeToFile(filename, eigenvalueresult);
 | 
|---|
 | 353 |   }
 | 
|---|
 | 354 | 
 | 
|---|
 | 355 |   {
 | 
|---|
 | 356 |     const std::string forceresult =
 | 
|---|
 | 357 |         writeTable<MPQCDataForceMap_t, MPQCDataForceVector_t>()(
 | 
|---|
 | 358 |             results.Result_Force_fused, results.getMaxLevel());
 | 
|---|
 | 359 |     LOG(0, "Force table is \n" << forceresult);
 | 
|---|
 | 360 |     std::string filename;
 | 
|---|
 | 361 |     filename += FRAGMENTPREFIX + std::string("_Forces.dat");
 | 
|---|
 | 362 |     writeToFile(filename, forceresult);
 | 
|---|
 | 363 |   }
 | 
|---|
| [376a3b] | 364 |   // we don't want to print grid to a table
 | 
|---|
| [86cfac5] | 365 |   {
 | 
|---|
 | 366 |     // print times (without flops for now)
 | 
|---|
 | 367 |     typedef boost::mpl::remove<
 | 
|---|
 | 368 |         boost::mpl::remove<MPQCDataTimeVector_t, MPQCDataFused::times_total_flops>::type,
 | 
|---|
 | 369 |         MPQCDataFused::times_gather_flops>::type
 | 
|---|
 | 370 |         MPQCDataTimeVector_noflops_t;
 | 
|---|
 | 371 |     const std::string timesresult =
 | 
|---|
 | 372 |         writeTable<MPQCDataTimeMap_t, MPQCDataTimeVector_noflops_t >()(
 | 
|---|
 | 373 |             results.Result_Time_fused, results.getMaxLevel());
 | 
|---|
 | 374 |     LOG(0, "Times table is \n" << timesresult);
 | 
|---|
 | 375 |     std::string filename;
 | 
|---|
 | 376 |     filename += FRAGMENTPREFIX + std::string("_Times.dat");
 | 
|---|
 | 377 |     writeToFile(filename, timesresult);
 | 
|---|
 | 378 |   }
 | 
|---|
| [358ddb] | 379 | }
 | 
|---|
 | 380 | 
 | 
|---|
| [766767] | 381 | bool appendToHomologyFile(
 | 
|---|
 | 382 |     const boost::filesystem::path &homology_file,
 | 
|---|
 | 383 |     const FragmentationResults &results,
 | 
|---|
 | 384 |     const std::string &KeySetFilename)
 | 
|---|
| [240c3e] | 385 | {
 | 
|---|
| [bf12d1] | 386 |   /// read homology container (if present)
 | 
|---|
| [240c3e] | 387 |   HomologyContainer homology_container;
 | 
|---|
 | 388 |   if (boost::filesystem::exists(homology_file)) {
 | 
|---|
 | 389 |     std::ifstream returnstream(homology_file.string().c_str());
 | 
|---|
 | 390 |     if (returnstream.good()) {
 | 
|---|
 | 391 |       boost::archive::text_iarchive ia(returnstream);
 | 
|---|
 | 392 |       ia >> homology_container;
 | 
|---|
 | 393 |     } else {
 | 
|---|
 | 394 |       ELOG(2, "Failed to parse from " << homology_file.string() << ".");
 | 
|---|
 | 395 |     }
 | 
|---|
 | 396 |     returnstream.close();
 | 
|---|
 | 397 |   } else {
 | 
|---|
 | 398 |     LOG(2, "Could not open " << homology_file.string()
 | 
|---|
 | 399 |         << ", creating empty container.");
 | 
|---|
 | 400 |   }
 | 
|---|
 | 401 | 
 | 
|---|
| [bf12d1] | 402 |   /// append all fragments to a HomologyContainer
 | 
|---|
| [240c3e] | 403 |   HomologyContainer::container_t values;
 | 
|---|
| [17c03f] | 404 |   const size_t FragmentCounter = results.Result_perIndexSet_Energy.size();
 | 
|---|
 | 405 | 
 | 
|---|
| [766767] | 406 |   // convert KeySetContainer to IndexSetContainer
 | 
|---|
| [2dd305] | 407 |   IndexSetContainer::ptr ForceContainer(new IndexSetContainer(results.getForceKeySet()));
 | 
|---|
 | 408 |   const IndexSetContainer::Container_t &Indices = results.getContainer();
 | 
|---|
 | 409 |   const IndexSetContainer::Container_t &ForceIndices = ForceContainer->getContainer();
 | 
|---|
| [d1ba0d] | 410 |   IndexSetContainer::Container_t::const_iterator iter = Indices.begin();
 | 
|---|
 | 411 |   IndexSetContainer::Container_t::const_iterator forceiter = ForceIndices.begin();
 | 
|---|
| [bf12d1] | 412 |   /// go through all fragments
 | 
|---|
| [d1ba0d] | 413 |   for (;iter != Indices.end(); ++iter, ++forceiter) // go through each IndexSet
 | 
|---|
| [766767] | 414 |   {
 | 
|---|
| [bf12d1] | 415 |     /// create new graph entry in HomologyContainer which is (key,value) type
 | 
|---|
| [91a565] | 416 |     LOG(1, "INFO: Creating new graph with " << **forceiter << ".");
 | 
|---|
| [d1ba0d] | 417 |     HomologyGraph graph(**forceiter);
 | 
|---|
| [bf12d1] | 418 |     LOG(2, "DEBUG: Created graph " << graph << ".");
 | 
|---|
| [766767] | 419 |     const IndexSet::ptr &index = *iter;
 | 
|---|
 | 420 |     HomologyContainer::value_t value;
 | 
|---|
| [bf12d1] | 421 | 
 | 
|---|
 | 422 |     // obtain fragment as key
 | 
|---|
| [79398d] | 423 |     std::map<IndexSet::ptr, std::pair< MPQCDataFragmentMap_t, MPQCDataFragmentMap_t> >::const_iterator fragmentiter
 | 
|---|
| [766767] | 424 |         = results.Result_perIndexSet_Fragment.find(index);
 | 
|---|
 | 425 |     ASSERT( fragmentiter != results.Result_perIndexSet_Fragment.end(),
 | 
|---|
 | 426 |         "appendToHomologyFile() - cannot find index "+toString(*index)
 | 
|---|
 | 427 |         +" in FragmentResults.");
 | 
|---|
| [79398d] | 428 |     value.first = boost::fusion::at_key<MPQCDataFused::fragment>(fragmentiter->second.first);
 | 
|---|
| [bf12d1] | 429 | 
 | 
|---|
 | 430 |     // obtain energy as value
 | 
|---|
| [79398d] | 431 |     std::map<IndexSet::ptr, std::pair< MPQCDataEnergyMap_t, MPQCDataEnergyMap_t> >::const_iterator energyiter
 | 
|---|
| [766767] | 432 |         = results.Result_perIndexSet_Energy.find(index);
 | 
|---|
 | 433 |     ASSERT( energyiter != results.Result_perIndexSet_Energy.end(),
 | 
|---|
 | 434 |         "appendToHomologyFile() - cannot find index "+toString(*index)
 | 
|---|
 | 435 |         +" in FragmentResults.");
 | 
|---|
| [91a565] | 436 |     // value.second = boost::fusion::at_key<MPQCDataFused::energy_total>(energyiter->second.first); // values
 | 
|---|
 | 437 |     value.second = boost::fusion::at_key<MPQCDataFused::energy_total>(energyiter->second.second); // contributions
 | 
|---|
| [766767] | 438 |     values.insert( std::make_pair( graph, value) );
 | 
|---|
 | 439 |   }
 | 
|---|
| [240c3e] | 440 |   homology_container.insert(values);
 | 
|---|
 | 441 | 
 | 
|---|
| [91a565] | 442 |   LOG(1, "INFO: Listing all present atomic ids ...");
 | 
|---|
 | 443 |   std::stringstream output;
 | 
|---|
 | 444 |   for (World::AtomIterator iter = World::getInstance().getAtomIter();
 | 
|---|
 | 445 |       iter != World::getInstance().atomEnd(); ++iter)
 | 
|---|
 | 446 |     output << (*iter)->getId() << " ";
 | 
|---|
 | 447 |   LOG(1, "INFO: { " << output.str() << "} .");
 | 
|---|
 | 448 | 
 | 
|---|
 | 449 |   // for debugging: print container
 | 
|---|
 | 450 |   LOG(1, "INFO: Listing all present homologies ...");
 | 
|---|
 | 451 |   for (HomologyContainer::container_t::const_iterator iter =
 | 
|---|
 | 452 |       homology_container.begin(); iter != homology_container.end(); ++iter) {
 | 
|---|
 | 453 |     LOG(1, "INFO: graph " << iter->first << " has Fragment "
 | 
|---|
 | 454 |         << iter->second.first << " and associated energy " << iter->second.second << ".");
 | 
|---|
 | 455 |   }
 | 
|---|
 | 456 | 
 | 
|---|
| [bf12d1] | 457 |   /// store homology container again
 | 
|---|
| [240c3e] | 458 |   std::ofstream outputstream(homology_file.string().c_str());
 | 
|---|
| [b8f0b25] | 459 |   if (outputstream.good()) { // check if opened
 | 
|---|
| [240c3e] | 460 |     boost::archive::text_oarchive oa(outputstream);
 | 
|---|
 | 461 |     oa << homology_container;
 | 
|---|
 | 462 |     if (outputstream.fail()) { // check if correctly written
 | 
|---|
 | 463 |       LOG(1, "Failed to write to file " << homology_file.string() << ".");
 | 
|---|
 | 464 |       return false;
 | 
|---|
 | 465 |     } else
 | 
|---|
 | 466 |       outputstream.close();
 | 
|---|
 | 467 |   } else {
 | 
|---|
 | 468 |     LOG(1, "Failed to open file " << homology_file.string()
 | 
|---|
 | 469 |         << " for writing.");
 | 
|---|
 | 470 |     return false;
 | 
|---|
 | 471 |   }
 | 
|---|
 | 472 |   return true;
 | 
|---|
 | 473 | }
 | 
|---|
| [edecba] | 474 | 
 | 
|---|
| [c5324f] | 475 | Action::state_ptr FragmentationFragmentationAutomationAction::performCall() {
 | 
|---|
 | 476 |   boost::asio::io_service io_service;
 | 
|---|
 | 477 | 
 | 
|---|
 | 478 |   // TODO: Have io_service run in second thread and merge with current again eventually
 | 
|---|
 | 479 | 
 | 
|---|
| [4f056e] | 480 |   size_t Exitflag = 0;
 | 
|---|
| [c9f9bb] | 481 |   std::map<JobId_t, MPQCData> fragmentData;
 | 
|---|
| [4f056e] | 482 |   {
 | 
|---|
 | 483 |     MPQCFragmentController mpqccontroller(io_service);
 | 
|---|
 | 484 |     mpqccontroller.setHost(params.host.get());
 | 
|---|
 | 485 |     mpqccontroller.setPort(params.port.get());
 | 
|---|
 | 486 |     mpqccontroller.setLevel(params.level.get());
 | 
|---|
 | 487 |     // Phase One: obtain ids
 | 
|---|
 | 488 |     std::vector< boost::filesystem::path > jobfiles = params.jobfiles.get();
 | 
|---|
 | 489 |     mpqccontroller.requestIds(jobfiles.size());
 | 
|---|
 | 490 | 
 | 
|---|
 | 491 |     // Phase Two: create and add MPQCJobs
 | 
|---|
 | 492 |     if (!mpqccontroller.addJobsFromFiles(params.executable.get().string(), jobfiles))
 | 
|---|
 | 493 |       return Action::failure;
 | 
|---|
 | 494 | 
 | 
|---|
 | 495 |     // Phase Three: calculate result
 | 
|---|
 | 496 |     mpqccontroller.waitforResults(jobfiles.size());
 | 
|---|
 | 497 |     mpqccontroller.getResults(fragmentData);
 | 
|---|
 | 498 | 
 | 
|---|
 | 499 |     Exitflag += mpqccontroller.getExitflag();
 | 
|---|
 | 500 |   }
 | 
|---|
| [c5324f] | 501 | 
 | 
|---|
| [69c733] | 502 | #ifdef HAVE_VMG
 | 
|---|
 | 503 |   if (params.DoLongrange.get()) {
 | 
|---|
| [db6f7d] | 504 |   if ( World::getInstance().getAllAtoms().size() == 0) {
 | 
|---|
 | 505 |     ELOG(1, "Please load the full molecule into the world before starting this action.");
 | 
|---|
 | 506 |     return Action::failure;
 | 
|---|
 | 507 |   }
 | 
|---|
| [06865e] | 508 | 
 | 
|---|
| [849cd8] | 509 |   // obtain combined charge density
 | 
|---|
| [27594e] | 510 |   FragmentationChargeDensity summedChargeDensity(
 | 
|---|
| [849cd8] | 511 |       fragmentData,
 | 
|---|
| [27594e] | 512 |       params.path.get());
 | 
|---|
 | 513 |   const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid();
 | 
|---|
| [849cd8] | 514 | 
 | 
|---|
| [4f056e] | 515 |   LOG(1, "INFO: There are " << fragmentData.size() << " short-range and "
 | 
|---|
 | 516 |       << full_sample.size() << " level-wise long-range jobs.");
 | 
|---|
| [69c733] | 517 | 
 | 
|---|
| [4f056e] | 518 |   // Phase Four: obtain more ids
 | 
|---|
| [c9f9bb] | 519 |   std::map<JobId_t, VMGData> longrangeData;
 | 
|---|
| [4f056e] | 520 |   {
 | 
|---|
 | 521 |     VMGFragmentController vmgcontroller(io_service);
 | 
|---|
 | 522 |     vmgcontroller.setHost(params.host.get());
 | 
|---|
 | 523 |     vmgcontroller.setPort(params.port.get());
 | 
|---|
 | 524 |     const size_t NoJobs = fragmentData.size()+full_sample.size();
 | 
|---|
 | 525 |     vmgcontroller.requestIds(NoJobs);
 | 
|---|
 | 526 | 
 | 
|---|
 | 527 |     // Phase Five: create VMGJobs
 | 
|---|
 | 528 |     const size_t near_field_cells = params.near_field_cells.get();
 | 
|---|
| [cd2591] | 529 |     const size_t interpolation_degree = params.interpolation_degree.get();
 | 
|---|
| [4f056e] | 530 |     if (!vmgcontroller.createLongRangeJobs(
 | 
|---|
 | 531 |         fragmentData,
 | 
|---|
 | 532 |         full_sample,
 | 
|---|
| [cd2591] | 533 |         summedChargeDensity.getFragment(),
 | 
|---|
 | 534 |         near_field_cells,
 | 
|---|
 | 535 |         interpolation_degree))
 | 
|---|
| [4f056e] | 536 |       return Action::failure;
 | 
|---|
 | 537 | 
 | 
|---|
 | 538 |     // Phase Six: calculate result
 | 
|---|
 | 539 |     vmgcontroller.waitforResults(NoJobs);
 | 
|---|
 | 540 |     vmgcontroller.getResults(longrangeData);
 | 
|---|
 | 541 |     ASSERT( NoJobs == longrangeData.size(),
 | 
|---|
 | 542 |         "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresults+"
 | 
|---|
 | 543 |         +toString(full_sample.size())+"="+toString(NoJobs)
 | 
|---|
 | 544 |         +" and VMGresults "+toString(longrangeData.size())+" don't match.");
 | 
|---|
 | 545 |     Exitflag += vmgcontroller.getExitflag();
 | 
|---|
 | 546 |   }
 | 
|---|
| [a2295a] | 547 | 
 | 
|---|
 | 548 |   // remove full solution corresponding to full_sample from map (must be highest ids), has to be treated extra
 | 
|---|
 | 549 |   std::map<JobId_t, VMGData>::iterator iter = longrangeData.end();
 | 
|---|
 | 550 |   for (size_t i=0;i<full_sample.size();++i)
 | 
|---|
 | 551 |     --iter;
 | 
|---|
 | 552 |   std::map<JobId_t, VMGData>::iterator remove_iter = iter;
 | 
|---|
 | 553 |   std::vector<VMGData> fullsolutionData;
 | 
|---|
 | 554 |   for (; iter != longrangeData.end(); ++iter)
 | 
|---|
 | 555 |     fullsolutionData.push_back(iter->second);
 | 
|---|
 | 556 |   longrangeData.erase(remove_iter, longrangeData.end());
 | 
|---|
| [358ddb] | 557 | 
 | 
|---|
| [376a3b] | 558 |   // Final phase: sum up and print result
 | 
|---|
| [b8f0b25] | 559 |   FragmentationResults results(
 | 
|---|
| [2dd305] | 560 |       fragmentData,
 | 
|---|
 | 561 |       longrangeData,
 | 
|---|
 | 562 |       params.path.get());
 | 
|---|
 | 563 |   results(
 | 
|---|
| [b8f0b25] | 564 |       fragmentData,
 | 
|---|
 | 565 |       longrangeData,
 | 
|---|
 | 566 |       fullsolutionData,
 | 
|---|
 | 567 |       full_sample);
 | 
|---|
 | 568 |   {
 | 
|---|
| [376a3b] | 569 |     LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
 | 
|---|
 | 570 |     printReceivedFullResults(results);
 | 
|---|
| [4f056e] | 571 |   }
 | 
|---|
| [376a3b] | 572 | 
 | 
|---|
| [240c3e] | 573 |   // append all keysets to homology file
 | 
|---|
| [b8f0b25] | 574 |   if (Exitflag == 0) {
 | 
|---|
| [240c3e] | 575 |     const boost::filesystem::path &homology_file = params.homology_file.get();
 | 
|---|
| [b8f0b25] | 576 |     if (homology_file.string() != "") {
 | 
|---|
 | 577 |       LOG(1, "INFO: Appending HomologyGraphs to file " << homology_file.string() << ".");
 | 
|---|
| [766767] | 578 |       if (!appendToHomologyFile(homology_file, results, params.path.get()))
 | 
|---|
| [b8f0b25] | 579 |         Exitflag = 1;
 | 
|---|
 | 580 |     }
 | 
|---|
| [240c3e] | 581 |   }
 | 
|---|
 | 582 | 
 | 
|---|
| [4f056e] | 583 |   std::map<JobId_t, std::string> debugData;
 | 
|---|
 | 584 |   {
 | 
|---|
| [a2295a] | 585 |     if (!full_sample.empty()) {
 | 
|---|
| [376a3b] | 586 |       // create debug jobs for each level to print the summed-up potential to vtk files
 | 
|---|
| [4f056e] | 587 |       VMGDebugGridFragmentController debugcontroller(io_service);
 | 
|---|
 | 588 |       debugcontroller.setHost(params.host.get());
 | 
|---|
 | 589 |       debugcontroller.setPort(params.port.get());
 | 
|---|
| [a2295a] | 590 |       debugcontroller.requestIds(full_sample.size());
 | 
|---|
 | 591 |       if (!debugcontroller.createDebugJobs(full_sample))
 | 
|---|
 | 592 |         return Action::failure;
 | 
|---|
 | 593 |       debugcontroller.waitforResults(full_sample.size());
 | 
|---|
 | 594 |       debugcontroller.getResults(debugData);
 | 
|---|
| [4f056e] | 595 |       Exitflag += debugcontroller.getExitflag();
 | 
|---|
| [a2295a] | 596 |     }
 | 
|---|
| [2764e0] | 597 |   }
 | 
|---|
| [69c733] | 598 |   }
 | 
|---|
| [358ddb] | 599 | #else
 | 
|---|
 | 600 |   // Final phase: print result
 | 
|---|
 | 601 |   {
 | 
|---|
 | 602 |     LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
 | 
|---|
 | 603 |     printReceivedMPQCResults(
 | 
|---|
 | 604 |         fragmentData,
 | 
|---|
 | 605 |         params.path.get(),
 | 
|---|
| [c699f4] | 606 |         getNoAtomsFromAdjacencyFile(params.path.get()));
 | 
|---|
| [358ddb] | 607 |   }
 | 
|---|
| [69c733] | 608 | #endif
 | 
|---|
| [edecba] | 609 | 
 | 
|---|
 | 610 |   return (Exitflag == 0) ? Action::success : Action::failure;
 | 
|---|
 | 611 | }
 | 
|---|
 | 612 | 
 | 
|---|
 | 613 | Action::state_ptr FragmentationFragmentationAutomationAction::performUndo(Action::state_ptr _state) {
 | 
|---|
 | 614 |   return Action::success;
 | 
|---|
 | 615 | }
 | 
|---|
 | 616 | 
 | 
|---|
 | 617 | Action::state_ptr FragmentationFragmentationAutomationAction::performRedo(Action::state_ptr _state){
 | 
|---|
 | 618 |   return Action::success;
 | 
|---|
 | 619 | }
 | 
|---|
 | 620 | 
 | 
|---|
 | 621 | bool FragmentationFragmentationAutomationAction::canUndo() {
 | 
|---|
 | 622 |   return false;
 | 
|---|
 | 623 | }
 | 
|---|
 | 624 | 
 | 
|---|
 | 625 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
 | 
|---|
 | 626 |   return false;
 | 
|---|
 | 627 | }
 | 
|---|
 | 628 | /** =========== end of function ====================== */
 | 
|---|