| [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 |
|
|---|
| [6ca578] | 41 | #include "CodePatterns/Assert.hpp"
|
|---|
| [edecba] | 42 | #include "CodePatterns/Info.hpp"
|
|---|
| 43 | #include "CodePatterns/Log.hpp"
|
|---|
| 44 | #include "JobMarket/Controller/FragmentController.hpp"
|
|---|
| 45 | #include "JobMarket/Jobs/FragmentJob.hpp"
|
|---|
| 46 |
|
|---|
| 47 | #include "Atom/atom.hpp"
|
|---|
| [d12d621] | 48 | #include "Box.hpp"
|
|---|
| [edecba] | 49 | #include "Fragmentation/EnergyMatrix.hpp"
|
|---|
| 50 | #include "Fragmentation/ForceMatrix.hpp"
|
|---|
| 51 | #include "Fragmentation/Fragmentation.hpp"
|
|---|
| [e06820] | 52 | #include "Fragmentation/SetValues/Histogram.hpp"
|
|---|
| [e13990b] | 53 | #include "Fragmentation/SetValues/IndexedVectors.hpp"
|
|---|
| [edecba] | 54 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
|
|---|
| [6ca578] | 55 | #include "Fragmentation/KeySet.hpp"
|
|---|
| [edecba] | 56 | #include "Fragmentation/KeySetsContainer.hpp"
|
|---|
| [ff9963] | 57 | #include "Fragmentation/Summation/AllLevelOrthogonalSummator.hpp"
|
|---|
| 58 | #include "Fragmentation/Summation/AllLevelSummator.hpp"
|
|---|
| [20b3df] | 59 | #include "Fragmentation/Summation/HeaderPrinter.hpp"
|
|---|
| [edecba] | 60 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
|---|
| [8b58ac] | 61 | #include "Helpers/defs.hpp"
|
|---|
| [28e894] | 62 | #include "Jobs/MPQCJob.hpp"
|
|---|
| [4bc75d] | 63 | #include "Jobs/MPQCData.hpp"
|
|---|
| [1f66c7] | 64 | #include "Jobs/MPQCData_printKeyNames.hpp"
|
|---|
| [d12d621] | 65 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
|---|
| [69c733] | 66 | #ifdef HAVE_VMG
|
|---|
| 67 | #include "Jobs/VMGJob.hpp"
|
|---|
| 68 | #endif
|
|---|
| [edecba] | 69 | #include "molecule.hpp"
|
|---|
| 70 | #include "World.hpp"
|
|---|
| 71 |
|
|---|
| 72 | #include <iostream>
|
|---|
| 73 | #include <string>
|
|---|
| 74 | #include <vector>
|
|---|
| 75 |
|
|---|
| [8cae4c] | 76 | #include <boost/mpl/for_each.hpp>
|
|---|
| 77 |
|
|---|
| [edecba] | 78 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
|
|---|
| 79 |
|
|---|
| 80 | using namespace MoleCuilder;
|
|---|
| 81 |
|
|---|
| 82 | // and construct the stuff
|
|---|
| 83 | #include "FragmentationAutomationAction.def"
|
|---|
| 84 | #include "Action_impl_pre.hpp"
|
|---|
| 85 | /** =========== define the function ====================== */
|
|---|
| 86 |
|
|---|
| 87 | class controller_AddOn;
|
|---|
| 88 |
|
|---|
| 89 | // needs to be defined for using the FragmentController
|
|---|
| 90 | controller_AddOn *getAddOn()
|
|---|
| 91 | {
|
|---|
| 92 | return NULL;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| [d12d621] | 95 | const int LEVEL = 5;
|
|---|
| 96 |
|
|---|
| [edecba] | 97 | /** Creates a MPQCCommandJob with argument \a filename.
|
|---|
| 98 | *
|
|---|
| 99 | * @param jobs created job is added to this vector
|
|---|
| 100 | * @param command mpqc command to execute
|
|---|
| 101 | * @param filename filename being argument to job
|
|---|
| 102 | * @param nextid id for this job
|
|---|
| 103 | */
|
|---|
| 104 | void parsejob(
|
|---|
| 105 | std::vector<FragmentJob::ptr> &jobs,
|
|---|
| 106 | const std::string &command,
|
|---|
| 107 | const std::string &filename,
|
|---|
| 108 | const JobId_t nextid)
|
|---|
| 109 | {
|
|---|
| 110 | std::ifstream file;
|
|---|
| 111 | file.open(filename.c_str());
|
|---|
| 112 | ASSERT( file.good(), "parsejob() - file "+filename+" does not exist.");
|
|---|
| 113 | std::string output((std::istreambuf_iterator<char>(file)),
|
|---|
| 114 | std::istreambuf_iterator<char>());
|
|---|
| [d12d621] | 115 | double begin[NDIM] = { 0., 0., 0. };
|
|---|
| 116 | const RealSpaceMatrix& M = World::getInstance().getDomain().getM();
|
|---|
| 117 | const double size = M.at(0,0);
|
|---|
| 118 | ASSERT( M.determinant() == size*size*size,
|
|---|
| 119 | "parsejob() - current domain matrix "+toString(M)+" is not cubic.");
|
|---|
| 120 | const int level = LEVEL;
|
|---|
| 121 | FragmentJob::ptr testJob( new MPQCJob(nextid, output, begin, size, level) );
|
|---|
| [edecba] | 122 | jobs.push_back(testJob);
|
|---|
| 123 | file.close();
|
|---|
| 124 | LOG(1, "INFO: Added MPQCCommandJob from file "+filename+".");
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /** Helper function to get number of atoms somehow.
|
|---|
| 128 | *
|
|---|
| 129 | * Here, we just parse the number of lines in the adjacency file as
|
|---|
| 130 | * it should correspond to the number of atoms, except when some atoms
|
|---|
| 131 | * are not bonded, but then fragmentation makes no sense.
|
|---|
| 132 | *
|
|---|
| 133 | * @param path path to the adjacency file
|
|---|
| 134 | */
|
|---|
| 135 | size_t getNoAtomsFromAdjacencyFile(const std::string &path)
|
|---|
| 136 | {
|
|---|
| 137 | size_t NoAtoms = 0;
|
|---|
| 138 |
|
|---|
| 139 | // parse in special file to get atom count (from line count)
|
|---|
| 140 | std::string filename(path);
|
|---|
| 141 | filename += FRAGMENTPREFIX;
|
|---|
| 142 | filename += ADJACENCYFILE;
|
|---|
| 143 | std::ifstream adjacency(filename.c_str());
|
|---|
| 144 | if (adjacency.fail()) {
|
|---|
| 145 | LOG(0, endl << "getNoAtomsFromAdjacencyFile() - Unable to open " << filename << ", is the directory correct?");
|
|---|
| 146 | return false;
|
|---|
| 147 | }
|
|---|
| 148 | std::string buffer;
|
|---|
| 149 | while (getline(adjacency, buffer))
|
|---|
| 150 | NoAtoms++;
|
|---|
| 151 | LOG(1, "INFO: There are " << NoAtoms << " atoms.");
|
|---|
| 152 |
|
|---|
| 153 | return NoAtoms;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| [2764e0] | 156 | /** Extracts MPQCData from received vector of FragmentResults.
|
|---|
| 157 | *
|
|---|
| 158 | * @param results results to extract MPQCData from
|
|---|
| 159 | * @param fragmentData on return array filled with extracted MPQCData
|
|---|
| 160 | */
|
|---|
| 161 | void ConvertFragmentResultToMPQCData(
|
|---|
| 162 | const std::vector<FragmentResult::ptr> &results,
|
|---|
| 163 | std::vector<MPQCData> &fragmentData)
|
|---|
| 164 | {
|
|---|
| 165 | // extract results
|
|---|
| 166 | fragmentData.clear();
|
|---|
| 167 | fragmentData.reserve(results.size());
|
|---|
| 168 |
|
|---|
| 169 | LOG(2, "DEBUG: Parsing now through " << results.size() << " results.");
|
|---|
| 170 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
|---|
| 171 | iter != results.end(); ++iter) {
|
|---|
| 172 | //LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
|
|---|
| 173 | MPQCData extractedData;
|
|---|
| 174 | std::stringstream inputstream((*iter)->result);
|
|---|
| 175 | LOG(2, "DEBUG: First 50 characters FragmentResult's string: "+(*iter)->result.substr(0, 50));
|
|---|
| 176 | boost::archive::text_iarchive ia(inputstream);
|
|---|
| 177 | ia >> extractedData;
|
|---|
| 178 | LOG(1, "INFO: extracted data is " << extractedData << ".");
|
|---|
| 179 | fragmentData.push_back(extractedData);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | ASSERT( results.size() == fragmentData.size(),
|
|---|
| 183 | "ConvertFragmentResultToMPQCData() - the number of extracted data differs from the number of results.");
|
|---|
| 184 | }
|
|---|
| [edecba] | 185 |
|
|---|
| [20b3df] | 186 |
|
|---|
| [edecba] | 187 | /** Print MPQCData from received results.
|
|---|
| 188 | *
|
|---|
| [2764e0] | 189 | * @param results results with ids to associate with fragment number
|
|---|
| 190 | * @param fragmentData MPQCData resulting from the jobs
|
|---|
| [edecba] | 191 | * @param KeySetFilename filename with keysets to associate forces correctly
|
|---|
| 192 | * @param NoAtoms total number of atoms
|
|---|
| 193 | */
|
|---|
| 194 | bool printReceivedMPQCResults(
|
|---|
| 195 | const std::vector<FragmentResult::ptr> &results,
|
|---|
| [2764e0] | 196 | const std::vector<MPQCData> &fragmentData,
|
|---|
| [edecba] | 197 | const std::string &KeySetFilename,
|
|---|
| 198 | size_t NoAtoms)
|
|---|
| 199 | {
|
|---|
| 200 | EnergyMatrix Energy;
|
|---|
| 201 | EnergyMatrix EnergyFragments;
|
|---|
| 202 | ForceMatrix Force;
|
|---|
| 203 | ForceMatrix ForceFragments;
|
|---|
| 204 |
|
|---|
| 205 | // align fragments
|
|---|
| 206 | std::map< JobId_t, size_t > MatrixNrLookup;
|
|---|
| 207 | size_t FragmentCounter = 0;
|
|---|
| 208 | {
|
|---|
| 209 | // bring ids in order ...
|
|---|
| 210 | typedef std::map< JobId_t, FragmentResult::ptr> IdResultMap_t;
|
|---|
| 211 | IdResultMap_t IdResultMap;
|
|---|
| 212 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
|---|
| 213 | iter != results.end(); ++iter) {
|
|---|
| 214 | #ifndef NDEBUG
|
|---|
| 215 | std::pair< IdResultMap_t::iterator, bool> inserter =
|
|---|
| 216 | #endif
|
|---|
| 217 | IdResultMap.insert( make_pair((*iter)->getId(), *iter) );
|
|---|
| 218 | ASSERT( inserter.second,
|
|---|
| [d12d621] | 219 | "ExtractMPQCDataFromResults() - two results have same id "
|
|---|
| [edecba] | 220 | +toString((*iter)->getId())+".");
|
|---|
| 221 | }
|
|---|
| 222 | // ... and fill lookup
|
|---|
| 223 | for(IdResultMap_t::const_iterator iter = IdResultMap.begin();
|
|---|
| 224 | iter != IdResultMap.end(); ++iter)
|
|---|
| 225 | MatrixNrLookup.insert( make_pair(iter->first, FragmentCounter++) );
|
|---|
| 226 | }
|
|---|
| 227 | LOG(1, "INFO: There are " << FragmentCounter << " fragments.");
|
|---|
| 228 |
|
|---|
| [2764e0] | 229 | std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
|---|
| 230 | std::vector<FragmentResult::ptr>::const_iterator resultiter = results.begin();
|
|---|
| 231 | for (; dataiter != fragmentData.end(); ++dataiter, ++resultiter) {
|
|---|
| 232 | const MPQCData &extractedData = *dataiter;
|
|---|
| [edecba] | 233 | // place results into EnergyMatrix ...
|
|---|
| 234 | {
|
|---|
| 235 | MatrixContainer::MatrixArray matrix;
|
|---|
| 236 | matrix.resize(1);
|
|---|
| [a9558f] | 237 | matrix[0].resize(1, extractedData.energies.total);
|
|---|
| [edecba] | 238 | if (!Energy.AddMatrix(
|
|---|
| [2764e0] | 239 | std::string("MPQCJob ")+toString((*resultiter)->getId()),
|
|---|
| [edecba] | 240 | matrix,
|
|---|
| [2764e0] | 241 | MatrixNrLookup[(*resultiter)->getId()])) {
|
|---|
| [edecba] | 242 | ELOG(1, "Adding energy matrix failed.");
|
|---|
| 243 | return false;
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 | // ... and ForceMatrix (with two empty columns in front)
|
|---|
| 247 | {
|
|---|
| 248 | MatrixContainer::MatrixArray matrix;
|
|---|
| 249 | const size_t rows = extractedData.forces.size();
|
|---|
| 250 | matrix.resize(rows);
|
|---|
| 251 | for (size_t i=0;i<rows;++i) {
|
|---|
| 252 | const size_t columns = 2+extractedData.forces[i].size();
|
|---|
| 253 | matrix[i].resize(columns, 0.);
|
|---|
| 254 | // for (size_t j=0;j<2;++j)
|
|---|
| 255 | // matrix[i][j] = 0.;
|
|---|
| 256 | for (size_t j=2;j<columns;++j)
|
|---|
| 257 | matrix[i][j] = extractedData.forces[i][j-2];
|
|---|
| 258 | }
|
|---|
| 259 | if (!Force.AddMatrix(
|
|---|
| [2764e0] | 260 | std::string("MPQCJob ")+toString((*resultiter)->getId()),
|
|---|
| [edecba] | 261 | matrix,
|
|---|
| [2764e0] | 262 | MatrixNrLookup[(*resultiter)->getId()])) {
|
|---|
| [edecba] | 263 | ELOG(1, "Adding force matrix failed.");
|
|---|
| 264 | return false;
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 | // add one more matrix (not required for energy)
|
|---|
| 269 | MatrixContainer::MatrixArray matrix;
|
|---|
| 270 | matrix.resize(1);
|
|---|
| 271 | matrix[0].resize(1, 0.);
|
|---|
| 272 | if (!Energy.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
|---|
| 273 | return false;
|
|---|
| 274 | // but for energy because we need to know total number of atoms
|
|---|
| 275 | matrix.resize(NoAtoms);
|
|---|
| 276 | for (size_t i = 0; i< NoAtoms; ++i)
|
|---|
| 277 | matrix[i].resize(2+NDIM, 0.);
|
|---|
| 278 | if (!Force.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
|---|
| 279 | return false;
|
|---|
| 280 |
|
|---|
| [6ca578] | 281 | // initialise indices
|
|---|
| 282 | KeySetsContainer KeySet;
|
|---|
| [8b58ac] | 283 | KeySetsContainer ForceKeySet;
|
|---|
| [edecba] | 284 | if (!Energy.InitialiseIndices()) return false;
|
|---|
| 285 |
|
|---|
| 286 | if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
|
|---|
| 287 |
|
|---|
| [8b58ac] | 288 | {
|
|---|
| 289 | std::stringstream filename;
|
|---|
| 290 | filename << FRAGMENTPREFIX << KEYSETFILE;
|
|---|
| 291 | if (!KeySet.ParseKeySets(KeySetFilename, filename.str(), FragmentCounter)) return false;
|
|---|
| 292 | }
|
|---|
| [edecba] | 293 |
|
|---|
| [8b58ac] | 294 | {
|
|---|
| 295 | std::stringstream filename;
|
|---|
| 296 | filename << FRAGMENTPREFIX << FORCESFILE;
|
|---|
| 297 | if (!ForceKeySet.ParseKeySets(KeySetFilename, filename.str(), FragmentCounter)) return false;
|
|---|
| 298 | }
|
|---|
| [6ca578] | 299 |
|
|---|
| [a67a04] | 300 | /// prepare for OrthogonalSummation
|
|---|
| [fb69e9] | 301 |
|
|---|
| 302 | // convert KeySetContainer to IndexSetContainer
|
|---|
| 303 | IndexSetContainer::ptr container(new IndexSetContainer(KeySet));
|
|---|
| [6ca578] | 304 | // create the map of all keysets
|
|---|
| 305 | SubsetMap::ptr subsetmap(new SubsetMap(*container));
|
|---|
| 306 |
|
|---|
| [a67a04] | 307 | // we probably don't need this, it suffices if IndexedVectors has some extra indices
|
|---|
| 308 | // // forces need different keysets: they must include hydrogen
|
|---|
| 309 | // // (though without ones added by saturation)
|
|---|
| 310 | // IndexSetContainer::ptr forceindices(new IndexSetContainer(ForceKeySet));
|
|---|
| 311 | // // create the map of all keysets
|
|---|
| 312 | // SubsetMap::ptr subsetmapforces(new SubsetMap(*forceindices));
|
|---|
| 313 |
|
|---|
| [e13990b] | 314 | /// convert all MPQCData to MPQCDataMap_t
|
|---|
| [8cae4c] | 315 | {
|
|---|
| 316 | // energy_t
|
|---|
| 317 | std::vector<MPQCDataEnergyMap_t> MPQCData_Energy_fused;
|
|---|
| 318 | MPQCData_Energy_fused.reserve(fragmentData.size());
|
|---|
| 319 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
|---|
| 320 | dataiter != fragmentData.end(); ++dataiter) {
|
|---|
| 321 | const MPQCData &extractedData = *dataiter;
|
|---|
| 322 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
|---|
| 323 | MPQCDataEnergyMap_t instance;
|
|---|
| 324 | boost::fusion::at_key<MPQCDataFused::energy_total>(instance) = extractedData.energies.total;
|
|---|
| 325 | boost::fusion::at_key<MPQCDataFused::energy_nuclear_repulsion>(instance) = extractedData.energies.nuclear_repulsion;
|
|---|
| [adccae] | 326 | boost::fusion::at_key<MPQCDataFused::energy_electron_coulomb>(instance) = extractedData.energies.electron_coulomb;
|
|---|
| 327 | boost::fusion::at_key<MPQCDataFused::energy_electron_exchange>(instance) = extractedData.energies.electron_exchange;
|
|---|
| [8cae4c] | 328 | boost::fusion::at_key<MPQCDataFused::energy_correlation>(instance) = extractedData.energies.correlation;
|
|---|
| 329 | boost::fusion::at_key<MPQCDataFused::energy_overlap>(instance) = extractedData.energies.overlap;
|
|---|
| 330 | boost::fusion::at_key<MPQCDataFused::energy_kinetic>(instance) = extractedData.energies.kinetic;
|
|---|
| 331 | boost::fusion::at_key<MPQCDataFused::energy_hcore>(instance) = extractedData.energies.hcore;
|
|---|
| [dde8ec] | 332 | boost::fusion::at_key<MPQCDataFused::energy_eigenvalues>(instance) = extractedData.energies.eigenvalues;
|
|---|
| [8cae4c] | 333 | MPQCData_Energy_fused.push_back(instance);
|
|---|
| 334 | }
|
|---|
| [965e2f] | 335 |
|
|---|
| [8cae4c] | 336 | // forces
|
|---|
| [a67a04] | 337 | ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(),
|
|---|
| 338 | "FragmentationAutomationAction::performCall() - ForceKeySet's KeySets and fragmentData differ in size.");
|
|---|
| [8cae4c] | 339 | std::vector<MPQCDataForceMap_t> MPQCData_Force_fused;
|
|---|
| 340 | MPQCData_Force_fused.reserve(fragmentData.size());
|
|---|
| [e13990b] | 341 | std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
|---|
| [a67a04] | 342 | KeySetsContainer::ArrayOfIntVectors::const_iterator arrayiter = ForceKeySet.KeySets.begin();
|
|---|
| 343 | for(;dataiter != fragmentData.end(); ++dataiter, ++arrayiter) {
|
|---|
| [8cae4c] | 344 | const MPQCData &extractedData = *dataiter;
|
|---|
| 345 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
|---|
| 346 | MPQCDataForceMap_t instance;
|
|---|
| [a67a04] | 347 | // must convert int to index_t
|
|---|
| 348 | IndexedVectors::indices_t indices(arrayiter->begin(), arrayiter->end());
|
|---|
| [e13990b] | 349 | boost::fusion::at_key<MPQCDataFused::forces>(instance) =
|
|---|
| [a67a04] | 350 | IndexedVectors(indices, extractedData.forces);
|
|---|
| [8cae4c] | 351 | MPQCData_Force_fused.push_back(instance);
|
|---|
| 352 | }
|
|---|
| [6ca578] | 353 |
|
|---|
| [c37c20] | 354 | // sampled_grid
|
|---|
| 355 | std::vector<MPQCDataGridMap_t> MPQCData_Grid_fused;
|
|---|
| 356 | MPQCData_Grid_fused.reserve(fragmentData.size());
|
|---|
| 357 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
|---|
| 358 | dataiter != fragmentData.end(); ++dataiter) {
|
|---|
| 359 | const MPQCData &extractedData = *dataiter;
|
|---|
| 360 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
|---|
| 361 | MPQCDataGridMap_t instance;
|
|---|
| 362 | boost::fusion::at_key<MPQCDataFused::sampled_grid>(instance) = extractedData.sampled_grid;
|
|---|
| 363 | MPQCData_Grid_fused.push_back(instance);
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| [8cae4c] | 366 | // times
|
|---|
| 367 | std::vector<MPQCDataTimeMap_t> MPQCData_Time_fused;
|
|---|
| 368 | MPQCData_Time_fused.reserve(fragmentData.size());
|
|---|
| 369 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
|---|
| 370 | dataiter != fragmentData.end(); ++dataiter) {
|
|---|
| 371 | const MPQCData &extractedData = *dataiter;
|
|---|
| 372 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
|---|
| 373 | MPQCDataTimeMap_t instance;
|
|---|
| 374 | boost::fusion::at_key<MPQCDataFused::times_walltime>(instance) = extractedData.times.walltime;
|
|---|
| 375 | boost::fusion::at_key<MPQCDataFused::times_cputime>(instance) = extractedData.times.cputime;
|
|---|
| 376 | boost::fusion::at_key<MPQCDataFused::times_flops>(instance) = extractedData.times.flops;
|
|---|
| 377 | MPQCData_Time_fused.push_back(instance);
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | // create a vector of all job ids
|
|---|
| 381 | std::vector<JobId_t> jobids(results.size(), JobId::IllegalJob);
|
|---|
| 382 | std::transform(results.begin(), results.end(), jobids.begin(),
|
|---|
| 383 | boost::bind(&FragmentResult::getId,
|
|---|
| 384 | boost::bind(&FragmentResult::ptr::operator->, _1)));
|
|---|
| 385 |
|
|---|
| [a3a382] | 386 | // create summation instances
|
|---|
| [ff9963] | 387 | std::vector<MPQCDataEnergyMap_t> Result_Energy_fused(subsetmap->getMaximumSubsetLevel());
|
|---|
| 388 | AllLevelOrthogonalSummator<MPQCDataEnergyMap_t> energySummer(
|
|---|
| [a3a382] | 389 | subsetmap,
|
|---|
| 390 | MPQCData_Energy_fused,
|
|---|
| 391 | jobids,
|
|---|
| 392 | container->getContainer(),
|
|---|
| [ff9963] | 393 | MatrixNrLookup,
|
|---|
| 394 | Result_Energy_fused);
|
|---|
| 395 | std::vector<MPQCDataForceMap_t> Result_Force_fused(subsetmap->getMaximumSubsetLevel());
|
|---|
| 396 | AllLevelOrthogonalSummator<MPQCDataForceMap_t> forceSummer(
|
|---|
| [a3a382] | 397 | subsetmap,
|
|---|
| 398 | MPQCData_Force_fused,
|
|---|
| 399 | jobids,
|
|---|
| 400 | container->getContainer(),
|
|---|
| [ff9963] | 401 | MatrixNrLookup,
|
|---|
| 402 | Result_Force_fused);
|
|---|
| 403 | std::vector<MPQCDataGridMap_t> Result_Grid_fused(subsetmap->getMaximumSubsetLevel());
|
|---|
| 404 | AllLevelOrthogonalSummator<MPQCDataGridMap_t> gridSummer(
|
|---|
| [a3a382] | 405 | subsetmap,
|
|---|
| 406 | MPQCData_Grid_fused,
|
|---|
| 407 | jobids,
|
|---|
| 408 | container->getContainer(),
|
|---|
| [ff9963] | 409 | MatrixNrLookup,
|
|---|
| 410 | Result_Grid_fused);
|
|---|
| 411 | std::vector<MPQCDataTimeMap_t> Result_Time_fused(subsetmap->getMaximumSubsetLevel());
|
|---|
| 412 | AllLevelSummator<MPQCDataTimeMap_t> timeSummer(
|
|---|
| [a3a382] | 413 | subsetmap,
|
|---|
| 414 | MPQCData_Time_fused,
|
|---|
| 415 | jobids,
|
|---|
| 416 | container->getContainer(),
|
|---|
| [ff9963] | 417 | MatrixNrLookup,
|
|---|
| 418 | Result_Time_fused);
|
|---|
| [a3a382] | 419 |
|
|---|
| 420 | // sum up
|
|---|
| 421 | boost::mpl::for_each<MPQCDataEnergyVector_t>(boost::ref(energySummer));
|
|---|
| 422 | boost::mpl::for_each<MPQCDataForceVector_t>(boost::ref(forceSummer));
|
|---|
| 423 | boost::mpl::for_each<MPQCDataGridVector_t>(boost::ref(gridSummer));
|
|---|
| 424 | boost::mpl::for_each<MPQCDataTimeVector_t>(boost::ref(timeSummer));
|
|---|
| [20b3df] | 425 |
|
|---|
| 426 | // create header line
|
|---|
| 427 | HeaderPrinter header("level");
|
|---|
| 428 | boost::mpl::for_each<MPQCDataEnergyVector_t>(boost::ref(header));
|
|---|
| 429 | LOG(1, "INFO: header is: " << header.get() << ".");
|
|---|
| [8cae4c] | 430 | }
|
|---|
| [56df37] | 431 |
|
|---|
| [6ca578] | 432 | // combine all found data
|
|---|
| [edecba] | 433 | if (!KeySet.ParseManyBodyTerms()) return false;
|
|---|
| 434 |
|
|---|
| 435 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false;
|
|---|
| 436 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false;
|
|---|
| 437 |
|
|---|
| 438 | if(!Energy.SetLastMatrix(0., 0)) return false;
|
|---|
| 439 | if(!Force.SetLastMatrix(0., 2)) return false;
|
|---|
| 440 |
|
|---|
| 441 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
|
|---|
| 442 | // --------- sum up energy --------------------
|
|---|
| 443 | LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
|
|---|
| 444 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
|
|---|
| 445 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
|
|---|
| 446 |
|
|---|
| 447 | // --------- sum up Forces --------------------
|
|---|
| 448 | LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
|
|---|
| 449 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
|
|---|
| 450 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | // for debugging print resulting energy and forces
|
|---|
| 454 | LOG(1, "INFO: Resulting energy is " << Energy.Matrix[ FragmentCounter ][0][0]);
|
|---|
| 455 | std::stringstream output;
|
|---|
| 456 | for (int i=0; i< Force.RowCounter[FragmentCounter]; ++i) {
|
|---|
| 457 | for (int j=0; j< Force.ColumnCounter[FragmentCounter]; ++j)
|
|---|
| 458 | output << Force.Matrix[ FragmentCounter ][i][j] << " ";
|
|---|
| 459 | output << "\n";
|
|---|
| 460 | }
|
|---|
| 461 | LOG(1, "INFO: Resulting forces are " << std::endl << output.str());
|
|---|
| 462 |
|
|---|
| 463 | return true;
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| [c5324f] | 467 | void RunService(
|
|---|
| 468 | boost::asio::io_service &io_service,
|
|---|
| 469 | std::string message)
|
|---|
| 470 | {
|
|---|
| 471 | message = std::string("io_service: ") + message;
|
|---|
| 472 | io_service.reset();
|
|---|
| 473 | Info info(message.c_str());
|
|---|
| 474 | io_service.run();
|
|---|
| 475 | }
|
|---|
| [edecba] | 476 |
|
|---|
| [c5324f] | 477 | void requestIds(
|
|---|
| 478 | FragmentController &controller,
|
|---|
| 479 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
|---|
| 480 | const size_t numberjobs)
|
|---|
| 481 | {
|
|---|
| 482 | controller.requestIds(params.host.get(), params.port.get(), numberjobs);
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | bool createJobsFromFiles(
|
|---|
| 486 | FragmentController &controller,
|
|---|
| 487 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
|---|
| 488 | const std::vector< boost::filesystem::path > &jobfiles)
|
|---|
| 489 | {
|
|---|
| 490 | std::vector<FragmentJob::ptr> jobs;
|
|---|
| 491 | for (std::vector< boost::filesystem::path >::const_iterator iter = jobfiles.begin();
|
|---|
| 492 | iter != jobfiles .end(); ++iter) {
|
|---|
| 493 | const std::string &filename = (*iter).string();
|
|---|
| 494 | if (boost::filesystem::exists(filename)) {
|
|---|
| 495 | const JobId_t next_id = controller.getAvailableId();
|
|---|
| 496 | LOG(1, "INFO: Creating MPQCCommandJob with filename'"
|
|---|
| 497 | +filename+"', and id "+toString(next_id)+".");
|
|---|
| 498 | parsejob(jobs, params.executable.get().string(), filename, next_id);
|
|---|
| 499 | } else {
|
|---|
| 500 | ELOG(1, "Fragment job "+filename+" does not exist.");
|
|---|
| 501 | return false;
|
|---|
| [edecba] | 502 | }
|
|---|
| 503 | }
|
|---|
| [c5324f] | 504 | controller.addJobs(jobs);
|
|---|
| 505 | controller.sendJobs(params.host.get(), params.port.get());
|
|---|
| 506 | return true;
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| [69c733] | 509 | #ifdef HAVE_VMG
|
|---|
| 510 | bool createLongRangeJobs(
|
|---|
| 511 | FragmentController &controller,
|
|---|
| 512 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
|---|
| [d12d621] | 513 | const std::vector<MPQCData> &fragmentData)
|
|---|
| [69c733] | 514 | {
|
|---|
| 515 | std::vector<FragmentJob::ptr> jobs;
|
|---|
| [d12d621] | 516 | // add one job for each fragment as the short-range correction which we need
|
|---|
| 517 | // to subtract from the obtained full potential to get the long-range part only
|
|---|
| 518 | for (std::vector<MPQCData>::const_iterator iter = fragmentData.begin();
|
|---|
| 519 | iter != fragmentData.end(); ++iter) {
|
|---|
| [69c733] | 520 | const JobId_t next_id = controller.getAvailableId();
|
|---|
| 521 | LOG(1, "INFO: Creating VMGJob.");
|
|---|
| [d12d621] | 522 | FragmentJob::ptr testJob(
|
|---|
| [cd77fc] | 523 | new VMGJob(next_id, iter->sampled_grid, iter->positions, iter->charges) );
|
|---|
| [69c733] | 524 | jobs.push_back(testJob);
|
|---|
| 525 | }
|
|---|
| [d12d621] | 526 |
|
|---|
| 527 | // add one more job for the full calculation
|
|---|
| 528 | // TODO: Here, we actually have to combine all the other sampled_grids
|
|---|
| 529 | {
|
|---|
| 530 | const int level = LEVEL;
|
|---|
| 531 | const int GRID = pow(2, level);
|
|---|
| [28c025] | 532 | std::vector<double> full_sample(GRID*GRID*GRID, 0.);
|
|---|
| [d12d621] | 533 | double begin[NDIM] = { 0., 0., 0. };
|
|---|
| 534 | const RealSpaceMatrix& M = World::getInstance().getDomain().getM();
|
|---|
| 535 | const double size = M.at(0,0);
|
|---|
| 536 | ASSERT( M.determinant() == size*size*size,
|
|---|
| 537 | "createLongRangeJobs() - current domain matrix "+toString(M)+" is not cubic.");
|
|---|
| [28c025] | 538 | const SamplingGrid full_sampled_grid(begin, size, level, full_sample);
|
|---|
| [cd77fc] | 539 | const std::vector< std::vector<double> > positions;
|
|---|
| 540 | const std::vector<double> charges;
|
|---|
| [d12d621] | 541 | const JobId_t next_id = controller.getAvailableId();
|
|---|
| 542 | FragmentJob::ptr testJob(
|
|---|
| [cd77fc] | 543 | new VMGJob(next_id, full_sampled_grid, positions, charges) );
|
|---|
| [d12d621] | 544 | jobs.push_back(testJob);
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | // then send jobs to controller
|
|---|
| [69c733] | 548 | controller.addJobs(jobs);
|
|---|
| 549 | controller.sendJobs(params.host.get(), params.port.get());
|
|---|
| 550 | return true;
|
|---|
| 551 | }
|
|---|
| 552 | #endif
|
|---|
| 553 |
|
|---|
| [c5324f] | 554 | void WaitforResults(
|
|---|
| 555 | boost::asio::io_service &io_service,
|
|---|
| 556 | FragmentController &controller,
|
|---|
| 557 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
|---|
| 558 | const size_t NoExpectedResults
|
|---|
| 559 | )
|
|---|
| 560 | {
|
|---|
| [edecba] | 561 | size_t NoCalculatedResults = 0;
|
|---|
| [c5324f] | 562 | while (NoCalculatedResults != NoExpectedResults) {
|
|---|
| [edecba] | 563 | // wait a bit
|
|---|
| 564 | boost::asio::deadline_timer timer(io_service);
|
|---|
| 565 | timer.expires_from_now(boost::posix_time::milliseconds(500));
|
|---|
| 566 | timer.wait();
|
|---|
| 567 | // then request status
|
|---|
| [bd81f9] | 568 | controller.checkResults(params.host.get(), params.port.get());
|
|---|
| [c5324f] | 569 | RunService(io_service, "Checking on results");
|
|---|
| 570 |
|
|---|
| [edecba] | 571 | const std::pair<size_t, size_t> JobStatus = controller.getJobStatus();
|
|---|
| 572 | LOG(1, "INFO: #" << JobStatus.first << " are waiting in the queue and #" << JobStatus.second << " jobs are calculated so far.");
|
|---|
| 573 | NoCalculatedResults = JobStatus.second;
|
|---|
| 574 | }
|
|---|
| [c5324f] | 575 | }
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 | Action::state_ptr FragmentationFragmentationAutomationAction::performCall() {
|
|---|
| 579 | boost::asio::io_service io_service;
|
|---|
| 580 | FragmentController controller(io_service);
|
|---|
| 581 |
|
|---|
| 582 | // TODO: Have io_service run in second thread and merge with current again eventually
|
|---|
| 583 |
|
|---|
| 584 | // Phase One: obtain ids
|
|---|
| 585 | std::vector< boost::filesystem::path > jobfiles = params.jobfiles.get();
|
|---|
| 586 | requestIds(controller, params, jobfiles.size());
|
|---|
| 587 | RunService(io_service, "Requesting ids");
|
|---|
| 588 |
|
|---|
| [69c733] | 589 | // Phase Two: create and add MPQCJobs
|
|---|
| [c5324f] | 590 | if (!createJobsFromFiles(controller, params, jobfiles))
|
|---|
| 591 | return Action::failure;
|
|---|
| [69c733] | 592 | RunService(io_service, "Adding MPQCJobs");
|
|---|
| [c5324f] | 593 |
|
|---|
| 594 | // Phase Three: calculate result
|
|---|
| 595 | WaitforResults(io_service, controller, params, jobfiles.size());
|
|---|
| [bd81f9] | 596 | controller.receiveResults(params.host.get(), params.port.get());
|
|---|
| [69c733] | 597 | RunService(io_service, "Requesting short-range results");
|
|---|
| [2764e0] | 598 | std::vector<FragmentResult::ptr> MPQCresults = controller.getReceivedResults();
|
|---|
| 599 | std::vector<MPQCData> fragmentData;
|
|---|
| 600 | ConvertFragmentResultToMPQCData(MPQCresults, fragmentData);
|
|---|
| [c5324f] | 601 |
|
|---|
| [69c733] | 602 | // print intermediate short-range results
|
|---|
| [edecba] | 603 | {
|
|---|
| [bd81f9] | 604 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
|---|
| [edecba] | 605 | printReceivedMPQCResults(
|
|---|
| [69c733] | 606 | MPQCresults,
|
|---|
| [2764e0] | 607 | fragmentData,
|
|---|
| [bd81f9] | 608 | params.path.get(),
|
|---|
| 609 | getNoAtomsFromAdjacencyFile(params.path.get()));
|
|---|
| [edecba] | 610 | }
|
|---|
| [69c733] | 611 |
|
|---|
| 612 | #ifdef HAVE_VMG
|
|---|
| 613 | if (params.DoLongrange.get()) {
|
|---|
| 614 | // Phase Four: obtain more ids
|
|---|
| [2764e0] | 615 | requestIds(controller, params, fragmentData.size()+1);
|
|---|
| [69c733] | 616 | RunService(io_service, "Requesting ids");
|
|---|
| 617 |
|
|---|
| 618 | // Phase Five: create VMGJobs
|
|---|
| [d12d621] | 619 | if (!createLongRangeJobs(controller, params, fragmentData))
|
|---|
| [69c733] | 620 | return Action::failure;
|
|---|
| 621 | RunService(io_service, "Adding VMGJobs");
|
|---|
| 622 |
|
|---|
| 623 | // Phase Six: calculate result
|
|---|
| [2764e0] | 624 | WaitforResults(io_service, controller, params, fragmentData.size()+1);
|
|---|
| [69c733] | 625 | controller.receiveResults(params.host.get(), params.port.get());
|
|---|
| 626 | RunService(io_service, "Requesting long-range results");
|
|---|
| 627 | std::vector<FragmentResult::ptr> VMGresults = controller.getReceivedResults();
|
|---|
| [2764e0] | 628 | ASSERT( MPQCresults.size()+1 == VMGresults.size(),
|
|---|
| 629 | "FragmentationFragmentationAutomationAction::performCall() - number of MPQCresultd and VMGresults don't match.");
|
|---|
| 630 |
|
|---|
| 631 | // Final phase: print result
|
|---|
| 632 | {
|
|---|
| 633 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
|---|
| 634 | printReceivedMPQCResults(
|
|---|
| 635 | MPQCresults,
|
|---|
| 636 | fragmentData,
|
|---|
| 637 | params.path.get(),
|
|---|
| 638 | getNoAtomsFromAdjacencyFile(params.path.get()));
|
|---|
| 639 | }
|
|---|
| [69c733] | 640 | }
|
|---|
| 641 | #endif
|
|---|
| [edecba] | 642 | size_t Exitflag = controller.getExitflag();
|
|---|
| 643 |
|
|---|
| 644 | return (Exitflag == 0) ? Action::success : Action::failure;
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | Action::state_ptr FragmentationFragmentationAutomationAction::performUndo(Action::state_ptr _state) {
|
|---|
| 648 | return Action::success;
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | Action::state_ptr FragmentationFragmentationAutomationAction::performRedo(Action::state_ptr _state){
|
|---|
| 652 | return Action::success;
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | bool FragmentationFragmentationAutomationAction::canUndo() {
|
|---|
| 656 | return false;
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
|
|---|
| 660 | return false;
|
|---|
| 661 | }
|
|---|
| 662 | /** =========== end of function ====================== */
|
|---|