| 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 <boost/assign.hpp>
|
|---|
| 42 |
|
|---|
| 43 | #include "CodePatterns/Assert.hpp"
|
|---|
| 44 | #include "CodePatterns/Info.hpp"
|
|---|
| 45 | #include "CodePatterns/Log.hpp"
|
|---|
| 46 | #include "JobMarket/Controller/FragmentController.hpp"
|
|---|
| 47 | #include "JobMarket/Jobs/FragmentJob.hpp"
|
|---|
| 48 |
|
|---|
| 49 | #include "Atom/atom.hpp"
|
|---|
| 50 | #include "Fragmentation/EnergyMatrix.hpp"
|
|---|
| 51 | #include "Fragmentation/ForceMatrix.hpp"
|
|---|
| 52 | #include "Fragmentation/Fragmentation.hpp"
|
|---|
| 53 | #include "Fragmentation/Histogram/Histogram.hpp"
|
|---|
| 54 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
|
|---|
| 55 | #include "Fragmentation/KeySet.hpp"
|
|---|
| 56 | #include "Fragmentation/KeySetsContainer.hpp"
|
|---|
| 57 | #include "Fragmentation/Summation/printSum.hpp"
|
|---|
| 58 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
|---|
| 59 | #include "Jobs/MPQCJob.hpp"
|
|---|
| 60 | #include "Jobs/MPQCData.hpp"
|
|---|
| 61 | #include "Jobs/MPQCData_printName.hpp"
|
|---|
| 62 | #include "molecule.hpp"
|
|---|
| 63 | #include "World.hpp"
|
|---|
| 64 |
|
|---|
| 65 | #include <iostream>
|
|---|
| 66 | #include <string>
|
|---|
| 67 | #include <vector>
|
|---|
| 68 |
|
|---|
| 69 | #include <boost/mpl/for_each.hpp>
|
|---|
| 70 |
|
|---|
| 71 | #include "Actions/FragmentationAction/FragmentationAutomationAction.hpp"
|
|---|
| 72 |
|
|---|
| 73 | using namespace MoleCuilder;
|
|---|
| 74 |
|
|---|
| 75 | using namespace boost::assign;
|
|---|
| 76 |
|
|---|
| 77 | // and construct the stuff
|
|---|
| 78 | #include "FragmentationAutomationAction.def"
|
|---|
| 79 | #include "Action_impl_pre.hpp"
|
|---|
| 80 | /** =========== define the function ====================== */
|
|---|
| 81 |
|
|---|
| 82 | class controller_AddOn;
|
|---|
| 83 |
|
|---|
| 84 | // needs to be defined for using the FragmentController
|
|---|
| 85 | controller_AddOn *getAddOn()
|
|---|
| 86 | {
|
|---|
| 87 | return NULL;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /** Creates a MPQCCommandJob with argument \a filename.
|
|---|
| 91 | *
|
|---|
| 92 | * @param jobs created job is added to this vector
|
|---|
| 93 | * @param command mpqc command to execute
|
|---|
| 94 | * @param filename filename being argument to job
|
|---|
| 95 | * @param nextid id for this job
|
|---|
| 96 | */
|
|---|
| 97 | void parsejob(
|
|---|
| 98 | std::vector<FragmentJob::ptr> &jobs,
|
|---|
| 99 | const std::string &command,
|
|---|
| 100 | const std::string &filename,
|
|---|
| 101 | const JobId_t nextid)
|
|---|
| 102 | {
|
|---|
| 103 | std::ifstream file;
|
|---|
| 104 | file.open(filename.c_str());
|
|---|
| 105 | ASSERT( file.good(), "parsejob() - file "+filename+" does not exist.");
|
|---|
| 106 | std::string output((std::istreambuf_iterator<char>(file)),
|
|---|
| 107 | std::istreambuf_iterator<char>());
|
|---|
| 108 | FragmentJob::ptr testJob( new MPQCJob(nextid, output) );
|
|---|
| 109 | jobs.push_back(testJob);
|
|---|
| 110 | file.close();
|
|---|
| 111 | LOG(1, "INFO: Added MPQCCommandJob from file "+filename+".");
|
|---|
| 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 | /** Print MPQCData from received results.
|
|---|
| 145 | *
|
|---|
| 146 | * @param results received results to extract MPQCData from
|
|---|
| 147 | * @param KeySetFilename filename with keysets to associate forces correctly
|
|---|
| 148 | * @param NoAtoms total number of atoms
|
|---|
| 149 | */
|
|---|
| 150 | bool printReceivedMPQCResults(
|
|---|
| 151 | const std::vector<FragmentResult::ptr> &results,
|
|---|
| 152 | const std::string &KeySetFilename,
|
|---|
| 153 | size_t NoAtoms)
|
|---|
| 154 | {
|
|---|
| 155 | EnergyMatrix Energy;
|
|---|
| 156 | EnergyMatrix EnergyFragments;
|
|---|
| 157 | ForceMatrix Force;
|
|---|
| 158 | ForceMatrix ForceFragments;
|
|---|
| 159 |
|
|---|
| 160 | // align fragments
|
|---|
| 161 | std::map< JobId_t, size_t > MatrixNrLookup;
|
|---|
| 162 | size_t FragmentCounter = 0;
|
|---|
| 163 | {
|
|---|
| 164 | // bring ids in order ...
|
|---|
| 165 | typedef std::map< JobId_t, FragmentResult::ptr> IdResultMap_t;
|
|---|
| 166 | IdResultMap_t IdResultMap;
|
|---|
| 167 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
|---|
| 168 | iter != results.end(); ++iter) {
|
|---|
| 169 | #ifndef NDEBUG
|
|---|
| 170 | std::pair< IdResultMap_t::iterator, bool> inserter =
|
|---|
| 171 | #endif
|
|---|
| 172 | IdResultMap.insert( make_pair((*iter)->getId(), *iter) );
|
|---|
| 173 | ASSERT( inserter.second,
|
|---|
| 174 | "printReceivedMPQCResults() - two results have same id "
|
|---|
| 175 | +toString((*iter)->getId())+".");
|
|---|
| 176 | }
|
|---|
| 177 | // ... and fill lookup
|
|---|
| 178 | for(IdResultMap_t::const_iterator iter = IdResultMap.begin();
|
|---|
| 179 | iter != IdResultMap.end(); ++iter)
|
|---|
| 180 | MatrixNrLookup.insert( make_pair(iter->first, FragmentCounter++) );
|
|---|
| 181 | }
|
|---|
| 182 | LOG(1, "INFO: There are " << FragmentCounter << " fragments.");
|
|---|
| 183 |
|
|---|
| 184 | // extract results
|
|---|
| 185 | std::vector<MPQCData> fragmentData(results.size());
|
|---|
| 186 | MPQCData combinedData;
|
|---|
| 187 |
|
|---|
| 188 | LOG(2, "DEBUG: Parsing now through " << results.size() << " results.");
|
|---|
| 189 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
|---|
| 190 | iter != results.end(); ++iter) {
|
|---|
| 191 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
|
|---|
| 192 | MPQCData extractedData;
|
|---|
| 193 | std::stringstream inputstream((*iter)->result);
|
|---|
| 194 | LOG(2, "DEBUG: First 50 characters FragmentResult's string: "+(*iter)->result.substr(0, 50));
|
|---|
| 195 | boost::archive::text_iarchive ia(inputstream);
|
|---|
| 196 | ia >> extractedData;
|
|---|
| 197 | LOG(1, "INFO: extracted data is " << extractedData << ".");
|
|---|
| 198 |
|
|---|
| 199 | // place results into EnergyMatrix ...
|
|---|
| 200 | {
|
|---|
| 201 | MatrixContainer::MatrixArray matrix;
|
|---|
| 202 | matrix.resize(1);
|
|---|
| 203 | matrix[0].resize(1, extractedData.energies.total);
|
|---|
| 204 | if (!Energy.AddMatrix(
|
|---|
| 205 | std::string("MPQCJob ")+toString((*iter)->getId()),
|
|---|
| 206 | matrix,
|
|---|
| 207 | MatrixNrLookup[(*iter)->getId()])) {
|
|---|
| 208 | ELOG(1, "Adding energy matrix failed.");
|
|---|
| 209 | return false;
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 | // ... and ForceMatrix (with two empty columns in front)
|
|---|
| 213 | {
|
|---|
| 214 | MatrixContainer::MatrixArray matrix;
|
|---|
| 215 | const size_t rows = extractedData.forces.size();
|
|---|
| 216 | matrix.resize(rows);
|
|---|
| 217 | for (size_t i=0;i<rows;++i) {
|
|---|
| 218 | const size_t columns = 2+extractedData.forces[i].size();
|
|---|
| 219 | matrix[i].resize(columns, 0.);
|
|---|
| 220 | // for (size_t j=0;j<2;++j)
|
|---|
| 221 | // matrix[i][j] = 0.;
|
|---|
| 222 | for (size_t j=2;j<columns;++j)
|
|---|
| 223 | matrix[i][j] = extractedData.forces[i][j-2];
|
|---|
| 224 | }
|
|---|
| 225 | if (!Force.AddMatrix(
|
|---|
| 226 | std::string("MPQCJob ")+toString((*iter)->getId()),
|
|---|
| 227 | matrix,
|
|---|
| 228 | MatrixNrLookup[(*iter)->getId()])) {
|
|---|
| 229 | ELOG(1, "Adding force matrix failed.");
|
|---|
| 230 | return false;
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | // add one more matrix (not required for energy)
|
|---|
| 235 | MatrixContainer::MatrixArray matrix;
|
|---|
| 236 | matrix.resize(1);
|
|---|
| 237 | matrix[0].resize(1, 0.);
|
|---|
| 238 | if (!Energy.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
|---|
| 239 | return false;
|
|---|
| 240 | // but for energy because we need to know total number of atoms
|
|---|
| 241 | matrix.resize(NoAtoms);
|
|---|
| 242 | for (size_t i = 0; i< NoAtoms; ++i)
|
|---|
| 243 | matrix[i].resize(2+NDIM, 0.);
|
|---|
| 244 | if (!Force.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
|---|
| 245 | return false;
|
|---|
| 246 |
|
|---|
| 247 | // initialise indices
|
|---|
| 248 | KeySetsContainer KeySet;
|
|---|
| 249 | if (!Energy.InitialiseIndices()) return false;
|
|---|
| 250 |
|
|---|
| 251 | if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
|
|---|
| 252 |
|
|---|
| 253 | if (!KeySet.ParseKeySets(KeySetFilename.c_str(), Force.RowCounter, Force.MatrixCounter)) return false;
|
|---|
| 254 |
|
|---|
| 255 | /// prepare for OrthogonalSummation
|
|---|
| 256 |
|
|---|
| 257 | // gather all present indices in AllIndices
|
|---|
| 258 | IndexSet::ptr AllIndices(new IndexSet);
|
|---|
| 259 | for (KeySetsContainer::ArrayOfIntVectors::const_iterator iter = KeySet.KeySets.begin();
|
|---|
| 260 | iter != KeySet.KeySets.end(); ++iter)
|
|---|
| 261 | for(KeySetsContainer::IntVector::const_iterator keyiter = (*iter).begin();
|
|---|
| 262 | keyiter != (*iter).end(); ++keyiter) {
|
|---|
| 263 | if (*keyiter != -1)
|
|---|
| 264 | (*AllIndices) += *keyiter;
|
|---|
| 265 | }
|
|---|
| 266 | LOG(1, "INFO: AllIndices is " << AllIndices << ".");
|
|---|
| 267 | // create container with all keysets
|
|---|
| 268 | IndexSetContainer::ptr container(new IndexSetContainer(AllIndices));
|
|---|
| 269 | for (KeySetsContainer::ArrayOfIntVectors::const_iterator iter = KeySet.KeySets.begin();
|
|---|
| 270 | iter != KeySet.KeySets.end(); ++iter) {
|
|---|
| 271 | IndexSet tempset;
|
|---|
| 272 | for(KeySetsContainer::IntVector::const_iterator keyiter = (*iter).begin();
|
|---|
| 273 | keyiter != (*iter).end(); ++keyiter)
|
|---|
| 274 | if (*keyiter != -1)
|
|---|
| 275 | tempset += *keyiter;
|
|---|
| 276 | container->insert(tempset);
|
|---|
| 277 | }
|
|---|
| 278 | // create the map of all keysets
|
|---|
| 279 | SubsetMap::ptr subsetmap(new SubsetMap(*container));
|
|---|
| 280 |
|
|---|
| 281 | {
|
|---|
| 282 | /// convert all MPQCData to MPQCDataMap_t
|
|---|
| 283 |
|
|---|
| 284 | // energy_t
|
|---|
| 285 | std::vector<MPQCDataEnergyMap_t> MPQCData_Energy_fused;
|
|---|
| 286 | MPQCData_Energy_fused.reserve(fragmentData.size());
|
|---|
| 287 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
|---|
| 288 | dataiter != fragmentData.end(); ++dataiter) {
|
|---|
| 289 | const MPQCData &extractedData = *dataiter;
|
|---|
| 290 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
|---|
| 291 | MPQCDataEnergyMap_t instance;
|
|---|
| 292 | boost::fusion::at_key<MPQCDataFused::energy_total>(instance) = extractedData.energies.total;
|
|---|
| 293 | boost::fusion::at_key<MPQCDataFused::energy_nuclear_repulsion>(instance) = extractedData.energies.nuclear_repulsion;
|
|---|
| 294 | boost::fusion::at_key<MPQCDataFused::energy_electron_coulomb>(instance) = extractedData.energies.electron_coulomb;
|
|---|
| 295 | boost::fusion::at_key<MPQCDataFused::energy_electron_exchange>(instance) = extractedData.energies.electron_exchange;
|
|---|
| 296 | boost::fusion::at_key<MPQCDataFused::energy_correlation>(instance) = extractedData.energies.correlation;
|
|---|
| 297 | boost::fusion::at_key<MPQCDataFused::energy_overlap>(instance) = extractedData.energies.overlap;
|
|---|
| 298 | boost::fusion::at_key<MPQCDataFused::energy_kinetic>(instance) = extractedData.energies.kinetic;
|
|---|
| 299 | boost::fusion::at_key<MPQCDataFused::energy_hcore>(instance) = extractedData.energies.hcore;
|
|---|
| 300 | // boost::fusion::at_key<MPQCDataFused::energy_eigenvalues>(instance) = extractedData.energies.eigenvalues;
|
|---|
| 301 | MPQCData_Energy_fused.push_back(instance);
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | // forces
|
|---|
| 305 | std::vector<MPQCDataForceMap_t> MPQCData_Force_fused;
|
|---|
| 306 | MPQCData_Force_fused.reserve(fragmentData.size());
|
|---|
| 307 | for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
|
|---|
| 308 | dataiter != fragmentData.end(); ++dataiter) {
|
|---|
| 309 | const MPQCData &extractedData = *dataiter;
|
|---|
| 310 | LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
|
|---|
| 311 | MPQCDataForceMap_t instance;
|
|---|
| 312 | boost::fusion::at_key<MPQCDataFused::forces>(instance) = extractedData.forces;
|
|---|
| 313 | MPQCData_Force_fused.push_back(instance);
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | // times
|
|---|
| 317 | std::vector<MPQCDataTimeMap_t> MPQCData_Time_fused;
|
|---|
| 318 | MPQCData_Time_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 | MPQCDataTimeMap_t instance;
|
|---|
| 324 | boost::fusion::at_key<MPQCDataFused::times_walltime>(instance) = extractedData.times.walltime;
|
|---|
| 325 | boost::fusion::at_key<MPQCDataFused::times_cputime>(instance) = extractedData.times.cputime;
|
|---|
| 326 | boost::fusion::at_key<MPQCDataFused::times_flops>(instance) = extractedData.times.flops;
|
|---|
| 327 | MPQCData_Time_fused.push_back(instance);
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | // create a vector of all job ids
|
|---|
| 331 | std::vector<JobId_t> jobids(results.size(), JobId::IllegalJob);
|
|---|
| 332 | std::transform(results.begin(), results.end(), jobids.begin(),
|
|---|
| 333 | boost::bind(&FragmentResult::getId,
|
|---|
| 334 | boost::bind(&FragmentResult::ptr::operator->, _1)));
|
|---|
| 335 |
|
|---|
| 336 | // sum up and print energies
|
|---|
| 337 | boost::mpl::for_each<MPQCDataEnergyVector_t>(
|
|---|
| 338 | printSum<MPQCDataEnergyMap_t>(
|
|---|
| 339 | subsetmap,
|
|---|
| 340 | MPQCData_Energy_fused,
|
|---|
| 341 | jobids,
|
|---|
| 342 | container->getContainer(),
|
|---|
| 343 | MatrixNrLookup)
|
|---|
| 344 | );
|
|---|
| 345 | // TODO: Histogram still needs cstor taking just vector<double>
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 | // TODO: We need to put forces into specific class and implement operators
|
|---|
| 349 | // // sum up and print forces
|
|---|
| 350 | // boost::mpl::for_each<MPQCDataForceVector_t>(
|
|---|
| 351 | // printSum<MPQCDataForceMap_t>(
|
|---|
| 352 | // subsetmap,
|
|---|
| 353 | // MPQCData_Force_fused,
|
|---|
| 354 | // jobids,
|
|---|
| 355 | // container->getContainer(),
|
|---|
| 356 | // MatrixNrLookup)
|
|---|
| 357 | // );
|
|---|
| 358 |
|
|---|
| 359 | // TODO: Times should just be summed not orthogonalized
|
|---|
| 360 | // // sum up and print times
|
|---|
| 361 | // boost::mpl::for_each<MPQCDataTimeVector_t>(
|
|---|
| 362 | // printSum<MPQCDataTimeMap_t>(
|
|---|
| 363 | // subsetmap,
|
|---|
| 364 | // MPQCData_Time_fused,
|
|---|
| 365 | // jobids,
|
|---|
| 366 | // container->getContainer(),
|
|---|
| 367 | // MatrixNrLookup)
|
|---|
| 368 | // );
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | // combine all found data
|
|---|
| 372 | if (!KeySet.ParseManyBodyTerms()) return false;
|
|---|
| 373 |
|
|---|
| 374 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false;
|
|---|
| 375 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false;
|
|---|
| 376 |
|
|---|
| 377 | if(!Energy.SetLastMatrix(0., 0)) return false;
|
|---|
| 378 | if(!Force.SetLastMatrix(0., 2)) return false;
|
|---|
| 379 |
|
|---|
| 380 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
|
|---|
| 381 | // --------- sum up energy --------------------
|
|---|
| 382 | LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
|
|---|
| 383 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
|
|---|
| 384 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
|
|---|
| 385 |
|
|---|
| 386 | // --------- sum up Forces --------------------
|
|---|
| 387 | LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
|
|---|
| 388 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
|
|---|
| 389 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | // for debugging print resulting energy and forces
|
|---|
| 393 | LOG(1, "INFO: Resulting energy is " << Energy.Matrix[ FragmentCounter ][0][0]);
|
|---|
| 394 | std::stringstream output;
|
|---|
| 395 | for (int i=0; i< Force.RowCounter[FragmentCounter]; ++i) {
|
|---|
| 396 | for (int j=0; j< Force.ColumnCounter[FragmentCounter]; ++j)
|
|---|
| 397 | output << Force.Matrix[ FragmentCounter ][i][j] << " ";
|
|---|
| 398 | output << "\n";
|
|---|
| 399 | }
|
|---|
| 400 | LOG(1, "INFO: Resulting forces are " << std::endl << output.str());
|
|---|
| 401 |
|
|---|
| 402 | return true;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 | void RunService(
|
|---|
| 407 | boost::asio::io_service &io_service,
|
|---|
| 408 | std::string message)
|
|---|
| 409 | {
|
|---|
| 410 | message = std::string("io_service: ") + message;
|
|---|
| 411 | io_service.reset();
|
|---|
| 412 | Info info(message.c_str());
|
|---|
| 413 | io_service.run();
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | void requestIds(
|
|---|
| 417 | FragmentController &controller,
|
|---|
| 418 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
|---|
| 419 | const size_t numberjobs)
|
|---|
| 420 | {
|
|---|
| 421 | controller.requestIds(params.host.get(), params.port.get(), numberjobs);
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | bool createJobsFromFiles(
|
|---|
| 425 | FragmentController &controller,
|
|---|
| 426 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
|---|
| 427 | const std::vector< boost::filesystem::path > &jobfiles)
|
|---|
| 428 | {
|
|---|
| 429 | std::vector<FragmentJob::ptr> jobs;
|
|---|
| 430 | for (std::vector< boost::filesystem::path >::const_iterator iter = jobfiles.begin();
|
|---|
| 431 | iter != jobfiles .end(); ++iter) {
|
|---|
| 432 | const std::string &filename = (*iter).string();
|
|---|
| 433 | if (boost::filesystem::exists(filename)) {
|
|---|
| 434 | const JobId_t next_id = controller.getAvailableId();
|
|---|
| 435 | LOG(1, "INFO: Creating MPQCCommandJob with filename'"
|
|---|
| 436 | +filename+"', and id "+toString(next_id)+".");
|
|---|
| 437 | parsejob(jobs, params.executable.get().string(), filename, next_id);
|
|---|
| 438 | } else {
|
|---|
| 439 | ELOG(1, "Fragment job "+filename+" does not exist.");
|
|---|
| 440 | return false;
|
|---|
| 441 | }
|
|---|
| 442 | }
|
|---|
| 443 | controller.addJobs(jobs);
|
|---|
| 444 | controller.sendJobs(params.host.get(), params.port.get());
|
|---|
| 445 | return true;
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | void WaitforResults(
|
|---|
| 449 | boost::asio::io_service &io_service,
|
|---|
| 450 | FragmentController &controller,
|
|---|
| 451 | const FragmentationFragmentationAutomationAction::FragmentationFragmentationAutomationParameters ¶ms,
|
|---|
| 452 | const size_t NoExpectedResults
|
|---|
| 453 | )
|
|---|
| 454 | {
|
|---|
| 455 | size_t NoCalculatedResults = 0;
|
|---|
| 456 | while (NoCalculatedResults != NoExpectedResults) {
|
|---|
| 457 | // wait a bit
|
|---|
| 458 | boost::asio::deadline_timer timer(io_service);
|
|---|
| 459 | timer.expires_from_now(boost::posix_time::milliseconds(500));
|
|---|
| 460 | timer.wait();
|
|---|
| 461 | // then request status
|
|---|
| 462 | controller.checkResults(params.host.get(), params.port.get());
|
|---|
| 463 | RunService(io_service, "Checking on results");
|
|---|
| 464 |
|
|---|
| 465 | const std::pair<size_t, size_t> JobStatus = controller.getJobStatus();
|
|---|
| 466 | LOG(1, "INFO: #" << JobStatus.first << " are waiting in the queue and #" << JobStatus.second << " jobs are calculated so far.");
|
|---|
| 467 | NoCalculatedResults = JobStatus.second;
|
|---|
| 468 | }
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 | Action::state_ptr FragmentationFragmentationAutomationAction::performCall() {
|
|---|
| 473 | boost::asio::io_service io_service;
|
|---|
| 474 | FragmentController controller(io_service);
|
|---|
| 475 |
|
|---|
| 476 | // TODO: Have io_service run in second thread and merge with current again eventually
|
|---|
| 477 |
|
|---|
| 478 | // Phase One: obtain ids
|
|---|
| 479 | std::vector< boost::filesystem::path > jobfiles = params.jobfiles.get();
|
|---|
| 480 | requestIds(controller, params, jobfiles.size());
|
|---|
| 481 | RunService(io_service, "Requesting ids");
|
|---|
| 482 |
|
|---|
| 483 | // Phase Two: create and add jobs
|
|---|
| 484 | if (!createJobsFromFiles(controller, params, jobfiles))
|
|---|
| 485 | return Action::failure;
|
|---|
| 486 | RunService(io_service, "Adding jobs");
|
|---|
| 487 |
|
|---|
| 488 | // Phase Three: calculate result
|
|---|
| 489 | WaitforResults(io_service, controller, params, jobfiles.size());
|
|---|
| 490 |
|
|---|
| 491 | // Phase Three: get result
|
|---|
| 492 | controller.receiveResults(params.host.get(), params.port.get());
|
|---|
| 493 | RunService(io_service, "Phase Four");
|
|---|
| 494 |
|
|---|
| 495 | // Final phase: print result
|
|---|
| 496 | {
|
|---|
| 497 | LOG(1, "INFO: Parsing fragment files from " << params.path.get() << ".");
|
|---|
| 498 | std::vector<FragmentResult::ptr> results = controller.getReceivedResults();
|
|---|
| 499 | printReceivedMPQCResults(
|
|---|
| 500 | results,
|
|---|
| 501 | params.path.get(),
|
|---|
| 502 | getNoAtomsFromAdjacencyFile(params.path.get()));
|
|---|
| 503 | }
|
|---|
| 504 | size_t Exitflag = controller.getExitflag();
|
|---|
| 505 |
|
|---|
| 506 | return (Exitflag == 0) ? Action::success : Action::failure;
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | Action::state_ptr FragmentationFragmentationAutomationAction::performUndo(Action::state_ptr _state) {
|
|---|
| 510 | return Action::success;
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | Action::state_ptr FragmentationFragmentationAutomationAction::performRedo(Action::state_ptr _state){
|
|---|
| 514 | return Action::success;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | bool FragmentationFragmentationAutomationAction::canUndo() {
|
|---|
| 518 | return false;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | bool FragmentationFragmentationAutomationAction::shouldUndo() {
|
|---|
| 522 | return false;
|
|---|
| 523 | }
|
|---|
| 524 | /** =========== end of function ====================== */
|
|---|