[cbcbbd] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * controller_MPQCCommandJob.cpp
|
---|
| 10 | *
|
---|
[014bc4] | 11 | * Created on: 01.06.2012
|
---|
[cbcbbd] | 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | // boost asio needs specific operator new
|
---|
| 21 | #include <boost/asio.hpp>
|
---|
| 22 |
|
---|
| 23 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 24 |
|
---|
| 25 | #include "controller_MPQCCommandJob.hpp"
|
---|
| 26 |
|
---|
| 27 | #include <boost/assign.hpp>
|
---|
| 28 | #include <boost/bind.hpp>
|
---|
| 29 | #include <fstream>
|
---|
| 30 |
|
---|
[7da5cd] | 31 | #include "CodePatterns/Info.hpp"
|
---|
| 32 | #include "CodePatterns/Log.hpp"
|
---|
[cbcbbd] | 33 |
|
---|
[7da5cd] | 34 | #include "JobMarket/Controller/ControllerCommand.hpp"
|
---|
| 35 | #include "JobMarket/Controller/ControllerCommandRegistry.hpp"
|
---|
| 36 | #include "JobMarket/Controller/FragmentController.hpp"
|
---|
| 37 | #include "JobMarket/Jobs/FragmentJob.hpp"
|
---|
| 38 | #include "JobMarket/Results/FragmentResult.hpp"
|
---|
[cbcbbd] | 39 |
|
---|
| 40 | #include "Fragmentation/EnergyMatrix.hpp"
|
---|
| 41 | #include "Fragmentation/ForceMatrix.hpp"
|
---|
| 42 | #include "Fragmentation/KeySetsContainer.hpp"
|
---|
| 43 | #include "Fragmentation/defs.hpp"
|
---|
| 44 |
|
---|
| 45 | #include "Helpers/defs.hpp"
|
---|
| 46 |
|
---|
| 47 | #include "Jobs/MPQCCommandJob.hpp"
|
---|
| 48 | #include "Jobs/MPQCCommandJob_MPQCData.hpp"
|
---|
| 49 |
|
---|
[7da5cd] | 50 | #include "ControllerOptions_MPQCCommandJob.hpp"
|
---|
[014bc4] | 51 |
|
---|
[cbcbbd] | 52 | /** Creates a MPQCCommandJob with argument \a filename.
|
---|
| 53 | *
|
---|
| 54 | * @param jobs created job is added to this vector
|
---|
| 55 | * @param command mpqc command to execute
|
---|
| 56 | * @param filename filename being argument to job
|
---|
| 57 | * @param nextid id for this job
|
---|
| 58 | */
|
---|
| 59 | void parsejob(
|
---|
| 60 | std::vector<FragmentJob::ptr> &jobs,
|
---|
| 61 | const std::string &command,
|
---|
| 62 | const std::string &filename,
|
---|
| 63 | const JobId_t nextid)
|
---|
| 64 | {
|
---|
| 65 | std::ifstream file;
|
---|
| 66 | file.open(filename.c_str());
|
---|
| 67 | ASSERT( file.good(), "parsejob() - file "+filename+" does not exist.");
|
---|
| 68 | std::string output((std::istreambuf_iterator<char>(file)),
|
---|
| 69 | std::istreambuf_iterator<char>());
|
---|
| 70 | FragmentJob::ptr testJob( new MPQCCommandJob(output, nextid, command) );
|
---|
| 71 | jobs.push_back(testJob);
|
---|
| 72 | file.close();
|
---|
| 73 | LOG(1, "INFO: Added MPQCCommandJob from file "+filename+".");
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /** Print received results.
|
---|
| 77 | *
|
---|
| 78 | * @param results received results to print
|
---|
| 79 | */
|
---|
| 80 | void printReceivedResults(const std::vector<FragmentResult::ptr> &results)
|
---|
| 81 | {
|
---|
| 82 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
---|
| 83 | iter != results.end(); ++iter)
|
---|
| 84 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /** Print MPQCData from received results.
|
---|
| 88 | *
|
---|
| 89 | * @param results received results to extract MPQCData from
|
---|
| 90 | * @param KeySetFilename filename with keysets to associate forces correctly
|
---|
| 91 | * @param NoAtoms total number of atoms
|
---|
| 92 | */
|
---|
| 93 | bool printReceivedMPQCResults(
|
---|
| 94 | const std::vector<FragmentResult::ptr> &results,
|
---|
| 95 | const std::string &KeySetFilename,
|
---|
| 96 | size_t NoAtoms)
|
---|
| 97 | {
|
---|
| 98 | EnergyMatrix Energy;
|
---|
| 99 | EnergyMatrix EnergyFragments;
|
---|
| 100 | ForceMatrix Force;
|
---|
| 101 | ForceMatrix ForceFragments;
|
---|
| 102 | KeySetsContainer KeySet;
|
---|
| 103 |
|
---|
| 104 | // align fragments
|
---|
| 105 | std::map< JobId_t, size_t > MatrixNrLookup;
|
---|
| 106 | size_t FragmentCounter = 0;
|
---|
| 107 | {
|
---|
| 108 | // bring ids in order ...
|
---|
| 109 | typedef std::map< JobId_t, FragmentResult::ptr> IdResultMap_t;
|
---|
| 110 | IdResultMap_t IdResultMap;
|
---|
| 111 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
---|
| 112 | iter != results.end(); ++iter) {
|
---|
| 113 | #ifndef NDEBUG
|
---|
| 114 | std::pair< IdResultMap_t::iterator, bool> inserter =
|
---|
| 115 | #endif
|
---|
| 116 | IdResultMap.insert( make_pair((*iter)->getId(), *iter) );
|
---|
| 117 | ASSERT( inserter.second,
|
---|
| 118 | "printReceivedMPQCResults() - two results have same id "
|
---|
| 119 | +toString((*iter)->getId())+".");
|
---|
| 120 | }
|
---|
| 121 | // ... and fill lookup
|
---|
| 122 | for(IdResultMap_t::const_iterator iter = IdResultMap.begin();
|
---|
| 123 | iter != IdResultMap.end(); ++iter)
|
---|
| 124 | MatrixNrLookup.insert( make_pair(iter->first, FragmentCounter++) );
|
---|
| 125 | }
|
---|
| 126 | LOG(1, "INFO: There are " << FragmentCounter << " fragments.");
|
---|
| 127 |
|
---|
| 128 | // extract results
|
---|
| 129 | std::vector<MPQCData> fragmentData(results.size());
|
---|
| 130 | MPQCData combinedData;
|
---|
| 131 |
|
---|
| 132 | LOG(2, "DEBUG: Parsing now through " << results.size() << " results.");
|
---|
| 133 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
---|
| 134 | iter != results.end(); ++iter) {
|
---|
| 135 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
|
---|
| 136 | MPQCData extractedData;
|
---|
| 137 | std::stringstream inputstream((*iter)->result);
|
---|
| 138 | LOG(2, "DEBUG: First 50 characters FragmentResult's string: "+(*iter)->result.substr(0, 50));
|
---|
| 139 | boost::archive::text_iarchive ia(inputstream);
|
---|
| 140 | ia >> extractedData;
|
---|
| 141 | LOG(1, "INFO: extracted data is " << extractedData << ".");
|
---|
| 142 |
|
---|
| 143 | // place results into EnergyMatrix ...
|
---|
| 144 | {
|
---|
| 145 | MatrixContainer::MatrixArray matrix;
|
---|
| 146 | matrix.resize(1);
|
---|
| 147 | matrix[0].resize(1, extractedData.energy);
|
---|
| 148 | if (!Energy.AddMatrix(
|
---|
| 149 | std::string("MPQCJob ")+toString((*iter)->getId()),
|
---|
| 150 | matrix,
|
---|
| 151 | MatrixNrLookup[(*iter)->getId()])) {
|
---|
| 152 | ELOG(1, "Adding energy matrix failed.");
|
---|
| 153 | return false;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | // ... and ForceMatrix (with two empty columns in front)
|
---|
| 157 | {
|
---|
| 158 | MatrixContainer::MatrixArray matrix;
|
---|
| 159 | const size_t rows = extractedData.forces.size();
|
---|
| 160 | matrix.resize(rows);
|
---|
| 161 | for (size_t i=0;i<rows;++i) {
|
---|
| 162 | const size_t columns = 2+extractedData.forces[i].size();
|
---|
| 163 | matrix[i].resize(columns, 0.);
|
---|
| 164 | // for (size_t j=0;j<2;++j)
|
---|
| 165 | // matrix[i][j] = 0.;
|
---|
| 166 | for (size_t j=2;j<columns;++j)
|
---|
| 167 | matrix[i][j] = extractedData.forces[i][j-2];
|
---|
| 168 | }
|
---|
| 169 | if (!Force.AddMatrix(
|
---|
| 170 | std::string("MPQCJob ")+toString((*iter)->getId()),
|
---|
| 171 | matrix,
|
---|
| 172 | MatrixNrLookup[(*iter)->getId()])) {
|
---|
| 173 | ELOG(1, "Adding force matrix failed.");
|
---|
| 174 | return false;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | // add one more matrix (not required for energy)
|
---|
| 179 | MatrixContainer::MatrixArray matrix;
|
---|
| 180 | matrix.resize(1);
|
---|
| 181 | matrix[0].resize(1, 0.);
|
---|
| 182 | if (!Energy.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
---|
| 183 | return false;
|
---|
| 184 | // but for energy because we need to know total number of atoms
|
---|
| 185 | matrix.resize(NoAtoms);
|
---|
| 186 | for (size_t i = 0; i< NoAtoms; ++i)
|
---|
| 187 | matrix[i].resize(2+NDIM, 0.);
|
---|
| 188 | if (!Force.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
|
---|
| 189 | return false;
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | // combine all found data
|
---|
| 193 | if (!Energy.InitialiseIndices()) return false;
|
---|
| 194 |
|
---|
| 195 | if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
|
---|
| 196 |
|
---|
| 197 | if (!KeySet.ParseKeySets(KeySetFilename.c_str(), Force.RowCounter, Force.MatrixCounter)) return false;
|
---|
| 198 |
|
---|
| 199 | if (!KeySet.ParseManyBodyTerms()) return false;
|
---|
| 200 |
|
---|
| 201 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false;
|
---|
| 202 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false;
|
---|
| 203 |
|
---|
| 204 | if(!Energy.SetLastMatrix(0., 0)) return false;
|
---|
| 205 | if(!Force.SetLastMatrix(0., 2)) return false;
|
---|
| 206 |
|
---|
| 207 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
|
---|
| 208 | // --------- sum up energy --------------------
|
---|
| 209 | LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
|
---|
| 210 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
|
---|
| 211 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
|
---|
| 212 |
|
---|
| 213 | // --------- sum up Forces --------------------
|
---|
| 214 | LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
|
---|
| 215 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
|
---|
| 216 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | // for debugging print resulting energy and forces
|
---|
| 220 | LOG(1, "INFO: Resulting energy is " << Energy.Matrix[ FragmentCounter ][0][0]);
|
---|
| 221 | std::stringstream output;
|
---|
| 222 | for (int i=0; i< Force.RowCounter[FragmentCounter]; ++i) {
|
---|
| 223 | for (int j=0; j< Force.ColumnCounter[FragmentCounter]; ++j)
|
---|
| 224 | output << Force.Matrix[ FragmentCounter ][i][j] << " ";
|
---|
| 225 | output << "\n";
|
---|
| 226 | }
|
---|
| 227 | LOG(1, "INFO: Resulting forces are " << std::endl << output.str());
|
---|
| 228 |
|
---|
| 229 | return true;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 | /** Helper function to get number of atoms somehow.
|
---|
| 234 | *
|
---|
| 235 | * Here, we just parse the number of lines in the adjacency file as
|
---|
| 236 | * it should correspond to the number of atoms, except when some atoms
|
---|
| 237 | * are not bonded, but then fragmentation makes no sense.
|
---|
| 238 | *
|
---|
| 239 | * @param path path to the adjacency file
|
---|
| 240 | */
|
---|
| 241 | size_t getNoAtomsFromAdjacencyFile(const std::string &path)
|
---|
| 242 | {
|
---|
| 243 | size_t NoAtoms = 0;
|
---|
| 244 |
|
---|
| 245 | // parse in special file to get atom count (from line count)
|
---|
| 246 | std::string filename(path);
|
---|
| 247 | filename += FRAGMENTPREFIX;
|
---|
| 248 | filename += ADJACENCYFILE;
|
---|
| 249 | std::ifstream adjacency(filename.c_str());
|
---|
| 250 | if (adjacency.fail()) {
|
---|
| 251 | LOG(0, endl << "getNoAtomsFromAdjacencyFile() - Unable to open " << filename << ", is the directory correct?");
|
---|
| 252 | return false;
|
---|
| 253 | }
|
---|
| 254 | std::string buffer;
|
---|
| 255 | while (getline(adjacency, buffer))
|
---|
| 256 | NoAtoms++;
|
---|
| 257 | LOG(1, "INFO: There are " << NoAtoms << " atoms.");
|
---|
| 258 |
|
---|
| 259 | return NoAtoms;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /** Creates a MPQCCommandJob out of give \a command with \a argument.
|
---|
| 263 | *
|
---|
| 264 | * @param controller reference to controller to add jobs
|
---|
| 265 | * @param ControllerInfo information on the job
|
---|
| 266 | */
|
---|
| 267 | void AddJobs(FragmentController &controller, const ControllerOptions_MPQCCommandJob &ControllerInfo)
|
---|
| 268 | {
|
---|
| 269 | std::vector<FragmentJob::ptr> jobs;
|
---|
| 270 | for (std::vector< std::string >::const_iterator iter = ControllerInfo.jobfiles.begin();
|
---|
| 271 | iter != ControllerInfo.jobfiles.end(); ++iter) {
|
---|
| 272 | const JobId_t next_id = controller.getAvailableId();
|
---|
| 273 | const std::string &filename = *iter;
|
---|
| 274 | LOG(1, "INFO: Creating MPQCCommandJob with filename '"
|
---|
| 275 | +filename+"', and id "+toString(next_id)+".");
|
---|
| 276 | parsejob(jobs, ControllerInfo.executable, filename, next_id);
|
---|
| 277 | }
|
---|
| 278 | controller.addJobs(jobs);
|
---|
| 279 | controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[014bc4] | 282 | inline std::vector<std::string> getListOfCommands(const ControllerCommandRegistry &ControllerCommands)
|
---|
| 283 | {
|
---|
| 284 | std::vector<std::string> Commands;
|
---|
| 285 | for (ControllerCommandRegistry::const_iterator iter = ControllerCommands.getBeginIter();
|
---|
| 286 | iter != ControllerCommands.getEndIter(); ++iter)
|
---|
| 287 | Commands.push_back(iter->first);
|
---|
| 288 |
|
---|
| 289 | return Commands;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | ControllerOptions *controller_MPQCCommandJob::allocateControllerInfo()
|
---|
[cbcbbd] | 293 | {
|
---|
| 294 | return new ControllerOptions_MPQCCommandJob();
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[014bc4] | 297 | void controller_MPQCCommandJob::addSpecificCommands(
|
---|
[cbcbbd] | 298 | boost::function<void (ControllerCommand *)> ®istrator,
|
---|
| 299 | FragmentController &controller,
|
---|
[014bc4] | 300 | ControllerOptions &ControllerInfo)
|
---|
[cbcbbd] | 301 | {
|
---|
| 302 | ControllerOptions_MPQCCommandJob &CI =
|
---|
| 303 | reinterpret_cast<ControllerOptions_MPQCCommandJob &>(ControllerInfo);
|
---|
| 304 | registrator(new ControllerCommand("addjobs",
|
---|
| 305 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
[014bc4] | 306 | (boost::bind(&FragmentController::requestIds,
|
---|
| 307 | boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport),
|
---|
| 308 | boost::bind(&std::vector<std::string>::size, boost::cref(CI.jobfiles))))
|
---|
| 309 | (boost::bind(&AddJobs, boost::ref(controller), boost::cref(CI)))
|
---|
[cbcbbd] | 310 | ));
|
---|
[014bc4] | 311 | registrator(new ControllerCommand("receiveresults",
|
---|
| 312 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
| 313 | (boost::bind(&FragmentController::receiveResults,
|
---|
| 314 | boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
|
---|
| 315 | (boost::bind(&printReceivedResults,
|
---|
| 316 | boost::bind(&FragmentController::getReceivedResults, boost::ref(controller))))
|
---|
| 317 | ));
|
---|
[cbcbbd] | 318 | registrator(new ControllerCommand("receivempqc",
|
---|
| 319 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
| 320 | (boost::bind(&FragmentController::receiveResults,
|
---|
| 321 | boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
|
---|
| 322 | (boost::bind(&printReceivedMPQCResults,
|
---|
| 323 | boost::bind(&FragmentController::getReceivedResults, boost::ref(controller)),
|
---|
| 324 | boost::cref(CI.fragmentpath),
|
---|
| 325 | boost::bind(&getNoAtomsFromAdjacencyFile, boost::cref(CI.fragmentpath))))
|
---|
[014bc4] | 326 | ));
|
---|
[cbcbbd] | 327 | }
|
---|
| 328 |
|
---|
[014bc4] | 329 | void controller_MPQCCommandJob::addSpecificOptions(
|
---|
[cbcbbd] | 330 | boost::program_options::options_description_easy_init option)
|
---|
| 331 | {
|
---|
| 332 | option
|
---|
[014bc4] | 333 | ("executable", boost::program_options::value< std::string >(), "executable for commands 'createjobs'")
|
---|
[cbcbbd] | 334 | ("fragment-path", boost::program_options::value< std::string >(), "path to fragment files for 'receivempqc'")
|
---|
[014bc4] | 335 | ("jobfiles", boost::program_options::value< std::vector< std::string > >()->multitoken(), "list of files as single argument toexecutable for 'addjobs'")
|
---|
[cbcbbd] | 336 | ;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[014bc4] | 339 | int controller_MPQCCommandJob::addOtherParsings(
|
---|
[cbcbbd] | 340 | ControllerOptions &ControllerInfo,
|
---|
[014bc4] | 341 | boost::program_options::variables_map &vm)
|
---|
[cbcbbd] | 342 | {
|
---|
| 343 | ControllerOptions_MPQCCommandJob &CI =
|
---|
| 344 | reinterpret_cast<ControllerOptions_MPQCCommandJob &>(ControllerInfo);
|
---|
| 345 | int status = 0;
|
---|
| 346 | status = CI.parseExecutable(vm);
|
---|
| 347 | if (status) return status;
|
---|
| 348 | status = CI.parseFragmentpath(vm);
|
---|
| 349 | if (status) return status;
|
---|
| 350 | status = CI.parseJobfiles(vm);
|
---|
| 351 | return status;
|
---|
| 352 | }
|
---|