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