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