source: src/Parser/MpqcParser_Parameters.cpp@ bcedf7

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 bcedf7 was c1db05, checked in by Frederik Heber <heber@…>, 13 years ago

Abstracted FormatParser_Parameters and made it "Clone"able.

  • this is preparatory for having an undoable LoadAction.
  • is so far only used by MpqcParser_Parameters.
  • FormatParser_Parameters::clone() clones the instance (uses CodePattern's Clone pattern).
  • FormatParser has pointer to FormatParser_Parameters.
  • MpqcParser now has parameters as inheritance from FormatParser, allocates in cstor, removes in dstor.
  • new function MpqcParser_Parameters::makeClone() sets instance to be a copy of the given one.
  • MpqcParser: replaced direct access to params by getParams().
  • added ParameterCloneTest to ParserMpqcUnitTest.
  • Property mode set to 100644
File size: 16.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * MpqcParser_Parameters.cpp
10 *
11 * Created on: Feb 3, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include <iostream>
21#include <boost/tokenizer.hpp>
22#include <string>
23
24#include "CodePatterns/MemDebug.hpp"
25
26#include "CodePatterns/Log.hpp"
27#include "CodePatterns/Verbose.hpp"
28
29#include "MpqcParser.hpp"
30
31#include "MpqcParser_Parameters.hpp"
32
33
34using boost::any_cast;
35
36MpqcParser_Parameters::MpqcParser_Parameters()
37{
38 Init();
39}
40
41void MpqcParser_Parameters::Init()
42{
43 // add all known basis
44 initBasis();
45
46 // add all theory names
47 TheoryNames[CLHF]="CLHF";
48 TheoryNames[CLKS]="CLKS";
49 TheoryNames[MBPT2]="MBPT2";
50 TheoryNames[MBPT2_R12]="MBPT2_R12";
51
52 {
53 // TODO: throw exception instead of eLog()
54 std::pair<TheoryLookupType::iterator, bool> inserter;
55 for (TheoryNamesType::iterator iter = TheoryNames.begin();
56 iter != TheoryNames.end();
57 ++iter) {
58 inserter = TheoryLookup.insert( make_pair(iter->second, iter->first) );
59 if (!inserter.second)
60 DoeLog(0) && (eLog() << Verbose(0)
61 << "MpqcParser_Parameters::MpqcParser_Parameters() - Theory name already present: "
62 << (inserter.first)->second << " and " << iter->first << "!"
63 << std::endl);
64 }
65 }
66
67 // add all integration names
68 IntegrationNames[IntegralCints] = "IntegralCints";
69 {
70 // TODO: throw exception instead of eLog()
71 std::pair<IntegrationLookupType::iterator, bool> inserter;
72 for (IntegrationNamesType::iterator iter = IntegrationNames.begin();
73 iter != IntegrationNames.end();
74 ++iter) {
75 inserter = IntegrationLookup.insert( make_pair(iter->second, iter->first) );
76 if (!inserter.second)
77 DoeLog(0) && (eLog() << Verbose(0)
78 << "MpqcParser_Parameters::MpqcParser_Parameters() - Integration name already present: "
79 << (inserter.first)->second << " and " << iter->first << "!"
80 << std::endl);
81 }
82 }
83
84 // have names for all parmaters
85 ParamNames[hessianParam] = "Hessian";
86 ParamNames[savestateParam] = "savestate";
87 ParamNames[do_gradientParam] = "do_gradient";
88 ParamNames[maxiterParam] = "maxiter";
89 ParamNames[memoryParam] = "memory";
90 ParamNames[stdapproxParam] = "stdapprox";
91 ParamNames[nfzcParam] = "nfzc";
92 ParamNames[basisParam] = "basis";
93 ParamNames[aux_basisParam] = "aux_basis";
94 ParamNames[integrationParam] = "integration";
95 ParamNames[theoryParam] = "theory";
96
97 {
98 // TODO: throw exception instead of eLog()
99 std::pair<ParamLookupType::iterator, bool> inserter;
100 for (ParamNamesType::iterator iter = ParamNames.begin();
101 iter != ParamNames.end();
102 ++iter) {
103 inserter = ParamLookup.insert( make_pair(iter->second, iter->first) );
104 if (!inserter.second)
105 DoeLog(0) && (eLog() << Verbose(0)
106 << "MpqcParser_Parameters::MpqcParser_Parameters() - parameter name already present: "
107 << (inserter.first)->second << " and " << iter->first << "!"
108 << std::endl);
109 }
110 }
111
112 initParameters();
113}
114
115MpqcParser_Parameters::MpqcParser_Parameters(const MpqcParser_Parameters & state)
116{
117 // init
118 Init();
119
120 // copy values
121 copyParameters(state);
122}
123
124void MpqcParser_Parameters::copyParameters(const MpqcParser_Parameters & state)
125{
126 appendParameter(hessianParam, state.getBool(hessianParam));
127 appendParameter(savestateParam, state.getBool(savestateParam));
128 appendParameter(do_gradientParam, state.getBool(do_gradientParam));
129 appendParameter(maxiterParam, state.getInt(maxiterParam));
130 appendParameter(memoryParam, state.getInt(memoryParam));
131 appendParameter(stdapproxParam, state.getString(stdapproxParam));
132 appendParameter(nfzcParam, state.getInt(nfzcParam));
133 appendParameter(basisParam, state.getString(basisParam));
134 appendParameter(aux_basisParam, state.getString(aux_basisParam));
135 appendParameter(integrationParam, state.getIntegration());
136 appendParameter(theoryParam, state.getTheory());
137}
138
139FormatParser_Parameters* MpqcParser_Parameters::clone() const
140{
141 //LOG(3, "Cloning parameters.");
142 MpqcParser_Parameters *instance = new MpqcParser_Parameters(*this);
143 return instance;
144}
145
146void MpqcParser_Parameters::makeClone(const FormatParser_Parameters & _state)
147{
148 //LOG(3, "Cloning parameters from other instance.");
149 copyParameters(static_cast<const MpqcParser_Parameters &>(_state));
150}
151
152void MpqcParser_Parameters::initParameters()
153{
154 appendParameter(hessianParam, bool(false));
155 appendParameter(savestateParam, bool(false));
156 appendParameter(do_gradientParam, bool(true));
157 appendParameter(maxiterParam, int(1000));
158 appendParameter(memoryParam, int(16000000));
159 appendParameter(stdapproxParam, std::string("A'"));
160 appendParameter(nfzcParam, int(1));
161 appendParameter(basisParam, std::string("3-21G"));
162 appendParameter(aux_basisParam, std::string("aug-cc-pVDZ"));
163 appendParameter(integrationParam, IntegralCints);
164 appendParameter(theoryParam, MBPT2);
165}
166
167MpqcParser_Parameters::~MpqcParser_Parameters()
168{}
169
170std::ostream & operator << (std::ostream& ost, MpqcParser_Parameters const &_mpqc_params)
171{
172 // this is ugly, but with boost::any to safeguard const-ness is plain impossible
173 MpqcParser_Parameters &mpqc_params = const_cast<MpqcParser_Parameters &>(_mpqc_params);
174 std::ostringstream output;
175 output << "Hessian=" << mpqc_params.getBool(MpqcParser_Parameters::hessianParam) << ";";
176 output << "savestate=" << mpqc_params.getBool(MpqcParser_Parameters::savestateParam) << ";";
177 output << "do_gradient=" << mpqc_params.getBool(MpqcParser_Parameters::do_gradientParam) << ";";
178 output << "maxiter=" << mpqc_params.getInt(MpqcParser_Parameters::maxiterParam) << ";";
179 output << "memory=" << mpqc_params.getInt(MpqcParser_Parameters::memoryParam) << ";";
180 output << "stdapprox=" << mpqc_params.getString(MpqcParser_Parameters::stdapproxParam) << ";";
181 output << "nfzc=" << mpqc_params.getInt(MpqcParser_Parameters::nfzcParam) << ";";
182 output << "basis=" << mpqc_params.getString(MpqcParser_Parameters::basisParam) << ";";
183 output << "aux_basis=" << mpqc_params.getString(MpqcParser_Parameters::aux_basisParam) << ";";
184 output << "integration=" << mpqc_params.getString(MpqcParser_Parameters::integrationParam) << ";";
185 output << "theory=" << mpqc_params.getString(MpqcParser_Parameters::theoryParam) << ";";
186 ost << output.str();
187 return ost;
188}
189
190std::istream & operator >> (std::istream& ist, MpqcParser_Parameters &params)
191{
192 typedef boost::tokenizer<boost::char_separator<char> >
193 tokenizer;
194 boost::char_separator<char> semicolonsep(";");
195 boost::char_separator<char> equalitysep(" =");
196 boost::char_separator<char> ticksep("\"");
197 std::string line;
198 std::getline( ist, line );
199 //DoLog(0) && (Log() << Verbose(0) << "INFO: full line of parameters is '" << line << "'" << std::endl);
200 tokenizer tokens(line, semicolonsep);
201 ASSERT(tokens.begin() != tokens.end(),
202 "operator<< on MpqcParser_Parameters - empty string, need at least ';' in line "+line+"!");
203 for (tokenizer::iterator tok_iter = tokens.begin();
204 tok_iter != tokens.end(); ++tok_iter) {
205 tokenizer paramtokens(*tok_iter, equalitysep);
206 if (paramtokens.begin() != paramtokens.end()) {
207 tokenizer::iterator tok_paramiter = paramtokens.begin();
208 tokenizer::iterator tok_valueiter = tok_paramiter;
209 tokenizer::iterator tok_checkiter = ++tok_valueiter;
210 ++tok_checkiter;
211 // TODO: throw exception instead of ASSERT
212 ASSERT(tok_paramiter != paramtokens.end(),
213 "operator<< on MpqcParser_Parameters - missing value before ' =' in token "+*tok_iter+"!");
214 ASSERT(tok_valueiter != paramtokens.end(),
215 "operator<< on MpqcParser_Parameters - missing value after ' =' in token "+*tok_iter+"!");
216 ASSERT(tok_checkiter == paramtokens.end(),
217 "operator<< on MpqcParser_Parameters - still more tokens after ' =' in token "+*tok_iter+":"
218 +*tok_checkiter+"!");
219 std::stringstream keystream(*tok_paramiter);
220 std::string key;
221 keystream >> ws >> key;
222 tokenizer ticklesstokens(*tok_valueiter, ticksep);
223 ASSERT(ticklesstokens.begin() != ticklesstokens.end(),
224 "operator<< on MpqcParser_Parameters - no tokens present after removing ticks in token "+*tok_valueiter+"!");
225 std::stringstream valuestream(*(ticklesstokens.begin()));
226 DoLog(2) && (Log() << Verbose(2)
227 << "INFO: Token pair is " << key << "," << valuestream.str() << std::endl);
228
229 // TODO: throw exception instead of DoeLog()
230 ASSERT(params.haveParam(key),
231 "operator >> on MpqcParser_Parameters - unknown parameter name '"
232 +key+"' with value "+valuestream.str()+"!");
233 if (params.haveParam(key))
234 params.setter(params.getParam(key), valuestream);
235 } else {
236 ist.setstate(std::ios::eofbit);
237 }
238 }
239 return ist;
240}
241
242
243/** Sets a desired value in the params from a string.
244 *
245 * This is due to strict typing of C++ very ugly and boost::any does not make
246 * it any better because it offers to functions to use values directly from
247 * stringstream. Probably, because value is unknown to is as well and hence
248 * the author could not implement it beautifully, so he dropped it altogether.
249 * Grrr ....
250 *
251 * @param _param param to set
252 * @param _desired stringstream containing value as next argument
253 * @return true - type ok, false - unknown type in params.
254 */
255bool MpqcParser_Parameters::setter(enum Parameters _param, std::stringstream& _desired) {
256 if (_param == integrationParam) {
257 std::string tmp;
258 _desired >> tmp;
259 params[_param] = IntegrationLookup[tmp];
260 } else if(_param == theoryParam) {
261 std::string tmp;
262 _desired >> tmp;
263 params[_param] = TheoryLookup[tmp];
264 } else if (params[_param].type() == typeid(std::string)) {
265 std::string tmp;
266 _desired >> tmp;
267 params[_param] = tmp;
268 } else if (params[_param].type() == typeid(int)) {
269 int tmp;
270 _desired >> tmp;
271 params[_param] = tmp;
272 } else if (params[_param].type() == typeid(double)) {
273 double tmp;
274 _desired >> tmp;
275 params[_param] = tmp;
276 } else if (params[_param].type() == typeid(bool)) {
277 std::string tmp;
278 _desired >> tmp;
279 if ((tmp == "yes") || (tmp == "1")) {
280 params[_param] = bool(true);
281 } else if ((tmp == "no") || (tmp == "0")) {
282 params[_param] = bool(false);
283 } else {
284 DoeLog(0) && (eLog() << Verbose(0)
285 << "MpqcParser_Parameters::setter() - unknown boolean key "
286 << tmp << "!" << std::endl);
287 }
288 } else {
289 DoeLog(0) && (eLog() << Verbose(0)
290 << "MpqcParser_Parameters::setter() - unknown type!" << std::endl);
291 return false;
292 }
293 return true;
294}
295
296
297void MpqcParser_Parameters::setTheory(enum Theory _theory)
298{
299 // TODO: throw exception instead of eLog()
300// try {
301 params[theoryParam] = _theory;
302// } catch(const boost::bad_any_cast &) {
303// DoeLog(0) && (eLog() << Verbose(0)
304// << "MpqcParser_Parameters::setTheory() - could not set boolean!" << std::endl);
305// }
306}
307
308void MpqcParser_Parameters::setIntegration(enum MpqcParser_Parameters::IntegrationMethod _integration){
309 // TODO: throw exception instead of eLog()
310// try {
311 params[integrationParam] = _integration;
312// } catch(const boost::bad_any_cast &) {
313// DoeLog(0) && (eLog() << Verbose(0)
314// << "MpqcParser_Parameters::setIntegration() - could not set boolean!" << std::endl);
315// }
316}
317
318bool MpqcParser_Parameters::haveParam(std::string _name) const
319{
320 return ParamLookup.count(_name) != 0;
321}
322
323enum MpqcParser_Parameters::Parameters MpqcParser_Parameters::getParam(std::string _name) const
324{
325 ParamLookupType::const_iterator iter = ParamLookup.find(_name);
326 return iter->second;
327}
328
329enum MpqcParser_Parameters::IntegrationMethod MpqcParser_Parameters::getIntegration() const
330{
331 parameterlist::const_iterator iter = params.find(integrationParam);
332 enum IntegrationMethod value;
333 // TODO: throw exception instead of eLog()
334// try {
335 value = boost::any_cast<enum IntegrationMethod>(iter->second);
336// } catch(const boost::bad_any_cast &) {
337// DoeLog(0) && (eLog() << Verbose(0)
338// << "MpqcParser_Parameters::getIntegration() - could not convert "
339// +ParamNames[integrationParam]+" to enum IntegrationMethod!" << std::endl);
340// }
341 return value;
342}
343
344enum MpqcParser_Parameters::Theory MpqcParser_Parameters::getTheory() const
345{
346 parameterlist::const_iterator iter = params.find(theoryParam);
347 enum Theory value;
348 // TODO: throw exception instead of eLog()
349// try {
350 value = boost::any_cast<enum Theory>(iter->second);
351// } catch(const boost::bad_any_cast &) {
352// DoeLog(0) && (eLog() << Verbose(0)
353// << "MpqcParser_Parameters::getTheory() - could not convert "
354// +ParamNames[theoryParam]+" to enum Theory!" << std::endl);
355// }
356 return value;
357}
358
359std::string MpqcParser_Parameters::getString(enum Parameters _param) const
360{
361 std::string value;
362 enum IntegrationMethod Iindex;
363 enum Theory Tindex;
364 bool test;
365 parameterlist::const_iterator iter = params.find(_param);
366 switch (_param) {
367 case hessianParam:
368 case savestateParam:
369 case do_gradientParam:
370 test = boost::any_cast<bool>(iter->second);
371 if (test)
372 value = "yes";
373 else
374 value = "no";
375 break;
376 case integrationParam:
377 // TODO: throw exception instead of eLog()
378// try {
379 Iindex = boost::any_cast<enum IntegrationMethod>(iter->second);
380// } catch(const boost::bad_any_cast &) {
381// DoeLog(0) && (eLog() << Verbose(0)
382// << "MpqcParser_Parameters::getString() - could not convert "
383// +ParamNames[_param]+" to string!" << std::endl);
384// }
385 {
386 IntegrationNamesType::const_iterator Iiter = IntegrationNames.find(Iindex);
387 value = Iiter->second;
388 }
389 break;
390 case theoryParam:
391 // TODO: throw exception instead of eLog()
392// try {
393 Tindex = boost::any_cast<enum Theory>(iter->second);
394// } catch(const boost::bad_any_cast &) {
395// DoeLog(0) && (eLog() << Verbose(0)
396// << "MpqcParser_Parameters::getString() - could not convert "
397// +ParamNames[_param]+" to string!" << std::endl);
398// }
399 {
400 TheoryNamesType::const_iterator Titer = TheoryNames.find(Tindex);
401 value = Titer->second;
402 }
403 break;
404 default:
405 // TODO: throw exception instead of eLog()
406// try {
407 value = boost::any_cast<std::string>(iter->second);
408// } catch(const boost::bad_any_cast &) {
409// DoeLog(0) && (eLog() << Verbose(0)
410// << "MpqcParser_Parameters::getString() - could not convert "
411// +ParamNames[_param]+" to string!" << std::endl);
412// }
413 break;
414 }
415
416 return value;
417}
418
419int MpqcParser_Parameters::getInt(enum Parameters _param) const
420{
421 int value;
422 parameterlist::const_iterator iter = params.find(_param);
423 switch (_param) {
424 default:
425 // TODO: throw exception instead of eLog()
426// try {
427 value = boost::any_cast<int>(iter->second);
428// } catch(const boost::bad_any_cast &) {
429// DoeLog(0) && (eLog() << Verbose(0)
430// << "MpqcParser_Parameters::getInt() - could not convert "
431// +ParamNames[_param]+" to int!" << std::endl);
432// }
433 break;
434 }
435 return value;
436}
437
438double MpqcParser_Parameters::getDouble(enum Parameters _param) const
439{
440 double value;
441 parameterlist::const_iterator iter = params.find(_param);
442 // TODO: throw exception instead of eLog()
443// try {
444 value = boost::any_cast<double>(iter->second);
445// } catch(const boost::bad_any_cast &) {
446// DoeLog(0) && (eLog() << Verbose(0)
447// << "MpqcParser_Parameters::getDouble() - could not convert "
448// +ParamNames[_param]+" to double!" << std::endl);
449// }
450 return value;
451}
452
453bool MpqcParser_Parameters::getBool(enum Parameters _param) const
454{
455 bool value;
456 parameterlist::const_iterator iter = params.find(_param);
457 // TODO: throw exception instead of eLog()
458// try {
459 value = boost::any_cast<bool>(iter->second);
460// } catch(const boost::bad_any_cast &) {
461// DoeLog(0) && (eLog() << Verbose(0)
462// << "MpqcParser_Parameters::getBool() - could not convert "
463// +ParamNames[_param]+" to bool!" << std::endl);
464// }
465 return value;
466}
467
468
469/** Checks whether all elements in the world also have parameters in the basis.
470 *
471 * @return true - all elements parametrized, false - at least one element is missing.
472 */
473bool MpqcParser_Parameters::checkWorldElementsAgainstCurrentBasis() const
474{
475 DoeLog(0) && (eLog() << Verbose(0)
476 << "MpqcParser_Parameters::checkWorldElementsAgainstCurrentBasis() - not implemented yet."
477 << std::endl);
478
479 return false;
480}
481
Note: See TracBrowser for help on using the repository browser.