source: src/controller_MPQCCommandJob.cpp@ 5c8807

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 5c8807 was 5aaa43, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Fixed new copyright line since start of 2013 in CodeChecks test.

  • we must look for either Uni Bonn or myself.
  • added second copyright line since from 1st of Jan 2013 I am not employed by University of Bonn anymore, hence changes to the code are my own copyright.
  • Property mode set to 100644
File size: 13.9 KB
RevLine 
[cbcbbd]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]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/>.
[cbcbbd]22 */
23
24/*
25 * controller_MPQCCommandJob.cpp
26 *
[014bc4]27 * Created on: 01.06.2012
[cbcbbd]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
[004d5c]39#include <boost/archive/text_iarchive.hpp>
40#include <boost/archive/text_oarchive.hpp>
41
[cbcbbd]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>
[004d5c]49#include <string>
[cbcbbd]50
[7da5cd]51#include "CodePatterns/Info.hpp"
52#include "CodePatterns/Log.hpp"
[cbcbbd]53
[7da5cd]54#include "JobMarket/Controller/ControllerCommand.hpp"
55#include "JobMarket/Controller/ControllerCommandRegistry.hpp"
56#include "JobMarket/Controller/FragmentController.hpp"
[004d5c]57#include "JobMarket/JobId.hpp"
[7da5cd]58#include "JobMarket/Jobs/FragmentJob.hpp"
59#include "JobMarket/Results/FragmentResult.hpp"
[cbcbbd]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"
[fbf143]69#include "Fragmentation/Summation/Containers/MPQCData.hpp"
[cbcbbd]70
[004d5c]71#include "LinearAlgebra/defs.hpp"
72
[7da5cd]73#include "ControllerOptions_MPQCCommandJob.hpp"
[014bc4]74
[cbcbbd]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 */
82void 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 */
103void 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 */
116bool 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 is " << extractedData << ".");
165
166 // place results into EnergyMatrix ...
167 {
168 MatrixContainer::MatrixArray matrix;
169 matrix.resize(1);
[a9558f]170 matrix[0].resize(1, extractedData.energies.total);
[cbcbbd]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
[8b58ac]220 {
221 std::stringstream filename;
222 filename << FRAGMENTPREFIX << KEYSETFILE;
223 if (!KeySet.ParseKeySets(KeySetFilename.c_str(), filename.str(), Force.MatrixCounter)) return 1;
224 }
[cbcbbd]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 */
268size_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 */
294void 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
[014bc4]309inline 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
319ControllerOptions *controller_MPQCCommandJob::allocateControllerInfo()
[cbcbbd]320{
321 return new ControllerOptions_MPQCCommandJob();
322}
323
[014bc4]324void controller_MPQCCommandJob::addSpecificCommands(
[cbcbbd]325 boost::function<void (ControllerCommand *)> &registrator,
326 FragmentController &controller,
[014bc4]327 ControllerOptions &ControllerInfo)
[cbcbbd]328{
329 ControllerOptions_MPQCCommandJob &CI =
330 reinterpret_cast<ControllerOptions_MPQCCommandJob &>(ControllerInfo);
331 registrator(new ControllerCommand("addjobs",
332 boost::assign::list_of< ControllerCommand::commands_t >
[014bc4]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)))
[cbcbbd]337 ));
[014bc4]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 ));
[cbcbbd]345 registrator(new ControllerCommand("receivempqc",
346 boost::assign::list_of< ControllerCommand::commands_t >
[856d05]347 (boost::bind(&FragmentController_JobIdProxy::setJobids,
348 boost::ref(controller), boost::cref(CI.ids)))
[cbcbbd]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))))
[014bc4]355 ));
[cbcbbd]356}
357
[014bc4]358void controller_MPQCCommandJob::addSpecificOptions(
[cbcbbd]359 boost::program_options::options_description_easy_init option)
360{
361 option
[014bc4]362 ("executable", boost::program_options::value< std::string >(), "executable for commands 'createjobs'")
[cbcbbd]363 ("fragment-path", boost::program_options::value< std::string >(), "path to fragment files for 'receivempqc'")
[014bc4]364 ("jobfiles", boost::program_options::value< std::vector< std::string > >()->multitoken(), "list of files as single argument toexecutable for 'addjobs'")
[856d05]365 ("ids", boost::program_options::value< std::vector< JobId_t > >()->multitoken(), "list of jobids to request from server for 'receivempqc'")
[cbcbbd]366 ;
367}
368
[014bc4]369int controller_MPQCCommandJob::addOtherParsings(
[cbcbbd]370 ControllerOptions &ControllerInfo,
[014bc4]371 boost::program_options::variables_map &vm)
[cbcbbd]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);
[856d05]381 if (status) return status;
382 status = CI.parseIds(vm);
[cbcbbd]383 return status;
384}
Note: See TracBrowser for help on using the repository browser.