source: src/Parser/MpqcParser_Parameters.cpp@ a7d753

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

Added templated InvertMap function to ease MpqcParser_Parameters::Init().

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