| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * FragmentationAutomationAction.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: May 18, 2012 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include <boost/archive/text_iarchive.hpp> | 
|---|
| 36 | // boost asio needs specific operator new | 
|---|
| 37 | #include <boost/asio.hpp> | 
|---|
| 38 |  | 
|---|
| 39 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 40 |  | 
|---|
| 41 | // include 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 |  | 
|---|
| 46 | #include <boost/mpl/remove.hpp> | 
|---|
| 47 | #include <boost/lambda/lambda.hpp> | 
|---|
| 48 |  | 
|---|
| 49 | #include <iostream> | 
|---|
| 50 |  | 
|---|
| 51 | #include "CodePatterns/Assert.hpp" | 
|---|
| 52 | #include "CodePatterns/Info.hpp" | 
|---|
| 53 | #include "CodePatterns/Log.hpp" | 
|---|
| 54 | #include "JobMarket/Jobs/FragmentJob.hpp" | 
|---|
| 55 |  | 
|---|
| 56 | #include "Fragmentation/Automation/createMatrixNrLookup.hpp" | 
|---|
| 57 | #include "Fragmentation/Automation/extractJobIds.hpp" | 
|---|
| 58 | #include "Fragmentation/Automation/FragmentationChargeDensity.hpp" | 
|---|
| 59 | #include "Fragmentation/Automation/FragmentationResults.hpp" | 
|---|
| 60 | #include "Fragmentation/Automation/MPQCFragmentController.hpp" | 
|---|
| 61 | #include "Fragmentation/Automation/parseKeySetFile.hpp" | 
|---|
| 62 | #include "Fragmentation/Automation/VMGDebugGridFragmentController.hpp" | 
|---|
| 63 | #include "Fragmentation/Automation/VMGFragmentController.hpp" | 
|---|
| 64 | #include "Fragmentation/EnergyMatrix.hpp" | 
|---|
| 65 | #include "Fragmentation/ForceMatrix.hpp" | 
|---|
| 66 | #include "Fragmentation/Fragmentation.hpp" | 
|---|
| 67 | #include "Fragmentation/Homology/HomologyContainer.hpp" | 
|---|
| 68 | #include "Fragmentation/Homology/HomologyGraph.hpp" | 
|---|
| 69 | #include "Fragmentation/KeySet.hpp" | 
|---|
| 70 | #include "Fragmentation/KeySetsContainer.hpp" | 
|---|
| 71 | #include "Fragmentation/SetValues/Fragment.hpp" | 
|---|
| 72 | #include "Fragmentation/SetValues/Histogram.hpp" | 
|---|
| 73 | #include "Fragmentation/SetValues/IndexedVectors.hpp" | 
|---|
| 74 | #include "Fragmentation/Summation/IndexSetContainer.hpp" | 
|---|
| 75 | #include "Fragmentation/Summation/writeTable.hpp" | 
|---|
| 76 | #include "Graph/DepthFirstSearchAnalysis.hpp" | 
|---|
| 77 | #include "Jobs/MPQCJob.hpp" | 
|---|
| 78 | #include "Jobs/MPQCData.hpp" | 
|---|
| 79 | #include "Jobs/MPQCData_printKeyNames.hpp" | 
|---|
| 80 | #ifdef HAVE_VMG | 
|---|
| 81 | #include "Jobs/VMGDebugGridJob.hpp" | 
|---|
| 82 | #include "Jobs/VMGJob.hpp" | 
|---|
| 83 | #include "Jobs/VMGData.hpp" | 
|---|
| 84 | #include "Jobs/VMGDataFused.hpp" | 
|---|
| 85 | #include "Jobs/VMGDataMap.hpp" | 
|---|
| 86 | #include "Jobs/VMGData_printKeyNames.hpp" | 
|---|
| 87 | #endif | 
|---|
| 88 | #include "World.hpp" | 
|---|
| 89 |  | 
|---|
| 90 | #include <fstream> | 
|---|
| 91 | #include <iostream> | 
|---|
| 92 | #include <string> | 
|---|
| 93 | #include <vector> | 
|---|
| 94 |  | 
|---|
| 95 | #include <boost/mpl/for_each.hpp> | 
|---|
| 96 |  | 
|---|
| 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 |  | 
|---|
| 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( | 
|---|
| 156 | const std::map<JobId_t, MPQCData> &fragmentData, | 
|---|
| 157 | const std::map< JobId_t, size_t > &MatrixNrLookup, | 
|---|
| 158 | const size_t FragmentCounter, | 
|---|
| 159 | const size_t NoAtoms, | 
|---|
| 160 | EnergyMatrix &Energy, | 
|---|
| 161 | ForceMatrix &Force) | 
|---|
| 162 | { | 
|---|
| 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; | 
|---|
| 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)+"."); | 
|---|
| 171 | // place results into EnergyMatrix ... | 
|---|
| 172 | { | 
|---|
| 173 | MatrixContainer::MatrixArray matrix; | 
|---|
| 174 | matrix.resize(1); | 
|---|
| 175 | matrix[0].resize(1, extractedData.energies.total); | 
|---|
| 176 | if (!Energy.AddMatrix( | 
|---|
| 177 | std::string("MPQCJob ")+toString(jobid), | 
|---|
| 178 | matrix, | 
|---|
| 179 | nriter->second)) { | 
|---|
| 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( | 
|---|
| 198 | std::string("MPQCJob ")+toString(jobid), | 
|---|
| 199 | matrix, | 
|---|
| 200 | nriter->second)) { | 
|---|
| 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 |  | 
|---|
| 219 | return true; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | /** Print MPQCData from received results. | 
|---|
| 223 | * | 
|---|
| 224 | * @param fragmentData MPQCData resulting from the jobs, associated to job id | 
|---|
| 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 | 
|---|
| 228 | */ | 
|---|
| 229 | bool printReceivedMPQCResults( | 
|---|
| 230 | const std::map<JobId_t, MPQCData> &fragmentData, | 
|---|
| 231 | const std::string &KeySetFilename, | 
|---|
| 232 | size_t NoAtoms) | 
|---|
| 233 | { | 
|---|
| 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 |  | 
|---|
| 241 | // create lookup from job nr to fragment number | 
|---|
| 242 | size_t FragmentCounter = 0; | 
|---|
| 243 | const std::map< JobId_t, size_t > MatrixNrLookup= | 
|---|
| 244 | createMatrixNrLookup(jobids, FragmentCounter); | 
|---|
| 245 |  | 
|---|
| 246 | // place results into maps | 
|---|
| 247 | EnergyMatrix Energy; | 
|---|
| 248 | ForceMatrix Force; | 
|---|
| 249 | if (!putResultsintoMatrices(fragmentData, MatrixNrLookup, FragmentCounter, NoAtoms, Energy, Force)) | 
|---|
| 250 | return false; | 
|---|
| 251 |  | 
|---|
| 252 | if (!Energy.InitialiseIndices()) return false; | 
|---|
| 253 |  | 
|---|
| 254 | if (!Force.ParseIndices(KeySetFilename.c_str())) return false; | 
|---|
| 255 |  | 
|---|
| 256 | // initialise keysets | 
|---|
| 257 | KeySetsContainer KeySet; | 
|---|
| 258 | parseKeySetFile(KeySet, KeySetFilename, FragmentCounter, NonHydrogenKeySets); | 
|---|
| 259 | KeySetsContainer ForceKeySet; | 
|---|
| 260 | parseKeySetFile(ForceKeySet, KeySetFilename, FragmentCounter, HydrogenKeySets); | 
|---|
| 261 |  | 
|---|
| 262 | // combine all found data | 
|---|
| 263 | if (!KeySet.ParseManyBodyTerms()) return false; | 
|---|
| 264 |  | 
|---|
| 265 | EnergyMatrix EnergyFragments; | 
|---|
| 266 | ForceMatrix ForceFragments; | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 305 | /** Print MPQCData from received results. | 
|---|
| 306 | * | 
|---|
| 307 | * @param results summed up results container | 
|---|
| 308 | */ | 
|---|
| 309 | void printReceivedFullResults( | 
|---|
| 310 | const FragmentationResults &results) | 
|---|
| 311 | { | 
|---|
| 312 | // print tables (without eigenvalues, they go extra) | 
|---|
| 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 |  | 
|---|
| 325 | if (results.Result_LongRange_fused.size() >= (results.getMaxLevel()-2)) | 
|---|
| 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 |  | 
|---|
| 336 | if (results.Result_LongRangeIntegrated_fused.size() >= (results.getMaxLevel()-2)) | 
|---|
| 337 | { | 
|---|
| 338 | const std::string gridresult = | 
|---|
| 339 | writeTable<VMGDataLongRangeMap_t, VMGDataLongRangeVector_t >()( | 
|---|
| 340 | results.Result_LongRangeIntegrated_fused, results.getMaxLevel(), 2); | 
|---|
| 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 | } | 
|---|
| 364 | // we don't want to print grid to a table | 
|---|
| 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 | } | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 | bool appendToHomologyFile( | 
|---|
| 382 | const boost::filesystem::path &homology_file, | 
|---|
| 383 | const FragmentationResults &results, | 
|---|
| 384 | const std::string &KeySetFilename) | 
|---|
| 385 | { | 
|---|
| 386 | /// read homology container (if present) | 
|---|
| 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 |  | 
|---|
| 402 | /// append all fragments to a HomologyContainer | 
|---|
| 403 | HomologyContainer::container_t values; | 
|---|
| 404 | const size_t FragmentCounter = results.Result_perIndexSet_Energy.size(); | 
|---|
| 405 |  | 
|---|
| 406 | // convert KeySetContainer to IndexSetContainer | 
|---|
| 407 | IndexSetContainer::ptr ForceContainer(new IndexSetContainer(results.getForceKeySet())); | 
|---|
| 408 | const IndexSetContainer::Container_t &Indices = results.getContainer(); | 
|---|
| 409 | const IndexSetContainer::Container_t &ForceIndices = ForceContainer->getContainer(); | 
|---|
| 410 | IndexSetContainer::Container_t::const_iterator iter = Indices.begin(); | 
|---|
| 411 | IndexSetContainer::Container_t::const_iterator forceiter = ForceIndices.begin(); | 
|---|
| 412 | /// go through all fragments | 
|---|
| 413 | for (;iter != Indices.end(); ++iter, ++forceiter) // go through each IndexSet | 
|---|
| 414 | { | 
|---|
| 415 | /// create new graph entry in HomologyContainer which is (key,value) type | 
|---|
| 416 | LOG(1, "INFO: Creating new graph with " << **forceiter << "."); | 
|---|
| 417 | HomologyGraph graph(**forceiter); | 
|---|
| 418 | LOG(2, "DEBUG: Created graph " << graph << "."); | 
|---|
| 419 | const IndexSet::ptr &index = *iter; | 
|---|
| 420 | HomologyContainer::value_t value; | 
|---|
| 421 |  | 
|---|
| 422 | // obtain fragment as key | 
|---|
| 423 | std::map<IndexSet::ptr, std::pair< MPQCDataFragmentMap_t, MPQCDataFragmentMap_t> >::const_iterator fragmentiter | 
|---|
| 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."); | 
|---|
| 428 | value.first = boost::fusion::at_key<MPQCDataFused::fragment>(fragmentiter->second.first); | 
|---|
| 429 |  | 
|---|
| 430 | // obtain energy as value | 
|---|
| 431 | std::map<IndexSet::ptr, std::pair< MPQCDataEnergyMap_t, MPQCDataEnergyMap_t> >::const_iterator energyiter | 
|---|
| 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."); | 
|---|
| 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 | 
|---|
| 438 | values.insert( std::make_pair( graph, value) ); | 
|---|
| 439 | } | 
|---|
| 440 | homology_container.insert(values); | 
|---|
| 441 |  | 
|---|
| 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 |  | 
|---|
| 457 | /// store homology container again | 
|---|
| 458 | std::ofstream outputstream(homology_file.string().c_str()); | 
|---|
| 459 | if (outputstream.good()) { // check if opened | 
|---|
| 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 | } | 
|---|
| 474 |  | 
|---|
| 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 |  | 
|---|
| 480 | size_t Exitflag = 0; | 
|---|
| 481 | std::map<JobId_t, MPQCData> fragmentData; | 
|---|
| 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 | } | 
|---|
| 501 |  | 
|---|
| 502 | #ifdef HAVE_VMG | 
|---|
| 503 | if (params.DoLongrange.get()) { | 
|---|
| 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 | } | 
|---|
| 508 |  | 
|---|
| 509 | // obtain combined charge density | 
|---|
| 510 | FragmentationChargeDensity summedChargeDensity( | 
|---|
| 511 | fragmentData, | 
|---|
| 512 | params.path.get()); | 
|---|
| 513 | const std::vector<SamplingGrid> full_sample = summedChargeDensity.getFullSampledGrid(); | 
|---|
| 514 |  | 
|---|
| 515 | LOG(1, "INFO: There are " << fragmentData.size() << " short-range and " | 
|---|
| 516 | << full_sample.size() << " level-wise long-range jobs."); | 
|---|
| 517 |  | 
|---|
| 518 | // Phase Four: obtain more ids | 
|---|
| 519 | std::map<JobId_t, VMGData> longrangeData; | 
|---|
| 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(); | 
|---|
| 529 | const size_t interpolation_degree = params.interpolation_degree.get(); | 
|---|
| 530 | if (!vmgcontroller.createLongRangeJobs( | 
|---|
| 531 | fragmentData, | 
|---|
| 532 | full_sample, | 
|---|
| 533 | summedChargeDensity.getFragment(), | 
|---|
| 534 | near_field_cells, | 
|---|
| 535 | interpolation_degree)) | 
|---|
| 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 | } | 
|---|
| 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()); | 
|---|
| 557 |  | 
|---|
| 558 | // Final phase: sum up and print result | 
|---|
| 559 | FragmentationResults results( | 
|---|
| 560 | fragmentData, | 
|---|
| 561 | longrangeData, | 
|---|
| 562 | params.path.get()); | 
|---|
| 563 | results( | 
|---|
| 564 | fragmentData, | 
|---|
| 565 | longrangeData, | 
|---|
| 566 | fullsolutionData, | 
|---|
| 567 | full_sample); | 
|---|
| 568 | { | 
|---|
| 569 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << "."); | 
|---|
| 570 | printReceivedFullResults(results); | 
|---|
| 571 | } | 
|---|
| 572 |  | 
|---|
| 573 | // append all keysets to homology file | 
|---|
| 574 | if (Exitflag == 0) { | 
|---|
| 575 | const boost::filesystem::path &homology_file = params.homology_file.get(); | 
|---|
| 576 | if (homology_file.string() != "") { | 
|---|
| 577 | LOG(1, "INFO: Appending HomologyGraphs to file " << homology_file.string() << "."); | 
|---|
| 578 | if (!appendToHomologyFile(homology_file, results, params.path.get())) | 
|---|
| 579 | Exitflag = 1; | 
|---|
| 580 | } | 
|---|
| 581 | } | 
|---|
| 582 |  | 
|---|
| 583 | std::map<JobId_t, std::string> debugData; | 
|---|
| 584 | { | 
|---|
| 585 | if (!full_sample.empty()) { | 
|---|
| 586 | // create debug jobs for each level to print the summed-up potential to vtk files | 
|---|
| 587 | VMGDebugGridFragmentController debugcontroller(io_service); | 
|---|
| 588 | debugcontroller.setHost(params.host.get()); | 
|---|
| 589 | debugcontroller.setPort(params.port.get()); | 
|---|
| 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); | 
|---|
| 595 | Exitflag += debugcontroller.getExitflag(); | 
|---|
| 596 | } | 
|---|
| 597 | } | 
|---|
| 598 | } | 
|---|
| 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(), | 
|---|
| 606 | getNoAtomsFromAdjacencyFile(params.path.get())); | 
|---|
| 607 | } | 
|---|
| 608 | #endif | 
|---|
| 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 ====================== */ | 
|---|