source: src/Parser/MpqcParser_Parameters.cpp@ 89a67f

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 89a67f 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
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#include <typeinfo>
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()
37{
38 Init();
39}
40
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
70void MpqcParser_Parameters::Init()
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";
80 InvertMap<TheoryNamesType,TheoryLookupType>(TheoryNames,TheoryLookup);
81
82 // add all integration names
83 IntegrationNames[IntegralCints] = "IntegralCints";
84 InvertMap<IntegrationNamesType,IntegrationLookupType>(IntegrationNames, IntegrationLookup);
85
86 // have names for all parmaters
87 ParamNames[hessianParam] = "Hessian";
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";
98 InvertMap<ParamNamesType,ParamLookupType>(ParamNames, ParamLookup);
99
100 initParameters();
101}
102
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
140void MpqcParser_Parameters::initParameters()
141{
142 appendParameter(hessianParam, bool(false));
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
158std::ostream & operator << (std::ostream& ost, MpqcParser_Parameters const &_mpqc_params)
159{
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);
162 std::ostringstream output;
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) << ";";
168 output << "stdapprox=" << mpqc_params.getString(MpqcParser_Parameters::stdapproxParam) << ";";
169 output << "nfzc=" << mpqc_params.getInt(MpqcParser_Parameters::nfzcParam) << ";";
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(";");
183 boost::char_separator<char> equalitysep(" =");
184 boost::char_separator<char> ticksep("\"");
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(),
190 "operator<< on MpqcParser_Parameters - empty string, need at least ';' in line "+line+"!");
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;
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);
216
217 // TODO: throw exception instead of DoeLog()
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);
223 } else {
224 ist.setstate(std::ios::eofbit);
225 }
226 }
227 return ist;
228}
229
230
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.
242 */
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;
267 if ((tmp == "yes") || (tmp == "1")) {
268 params[_param] = bool(true);
269 } else if ((tmp == "no") || (tmp == "0")) {
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 {
277 DoeLog(0) && (eLog() << Verbose(0)
278 << "MpqcParser_Parameters::setter() - unknown type!" << std::endl);
279 return false;
280 }
281 return true;
282}
283
284
285void MpqcParser_Parameters::setTheory(enum Theory _theory)
286{
287 // TODO: throw exception instead of eLog()
288// try {
289 params[theoryParam] = _theory;
290// } catch(const boost::bad_any_cast &) {
291// DoeLog(0) && (eLog() << Verbose(0)
292// << "MpqcParser_Parameters::setTheory() - could not set boolean!" << std::endl);
293// }
294}
295
296void MpqcParser_Parameters::setIntegration(enum MpqcParser_Parameters::IntegrationMethod _integration){
297 // TODO: throw exception instead of eLog()
298// try {
299 params[integrationParam] = _integration;
300// } catch(const boost::bad_any_cast &) {
301// DoeLog(0) && (eLog() << Verbose(0)
302// << "MpqcParser_Parameters::setIntegration() - could not set boolean!" << std::endl);
303// }
304}
305
306bool MpqcParser_Parameters::haveParam(std::string _name) const
307{
308 return ParamLookup.count(_name) != 0;
309}
310
311enum MpqcParser_Parameters::Parameters MpqcParser_Parameters::getParam(std::string _name) const
312{
313 ParamLookupType::const_iterator iter = ParamLookup.find(_name);
314 return iter->second;
315}
316
317enum MpqcParser_Parameters::IntegrationMethod MpqcParser_Parameters::getIntegration() const
318{
319 parameterlist::const_iterator iter = params.find(integrationParam);
320 enum IntegrationMethod value;
321 // TODO: throw exception instead of eLog()
322// try {
323 value = boost::any_cast<enum IntegrationMethod>(iter->second);
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// }
329 return value;
330}
331
332enum MpqcParser_Parameters::Theory MpqcParser_Parameters::getTheory() const
333{
334 parameterlist::const_iterator iter = params.find(theoryParam);
335 enum Theory value;
336 // TODO: throw exception instead of eLog()
337// try {
338 value = boost::any_cast<enum Theory>(iter->second);
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// }
344 return value;
345}
346
347std::string MpqcParser_Parameters::getString(enum Parameters _param) const
348{
349 std::string value;
350 enum IntegrationMethod Iindex;
351 enum Theory Tindex;
352 bool test;
353 parameterlist::const_iterator iter = params.find(_param);
354 switch (_param) {
355 case hessianParam:
356 case savestateParam:
357 case do_gradientParam:
358 test = boost::any_cast<bool>(iter->second);
359 if (test)
360 value = "yes";
361 else
362 value = "no";
363 break;
364 case integrationParam:
365 // TODO: throw exception instead of eLog()
366// try {
367 Iindex = boost::any_cast<enum IntegrationMethod>(iter->second);
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// }
373 {
374 IntegrationNamesType::const_iterator Iiter = IntegrationNames.find(Iindex);
375 value = Iiter->second;
376 }
377 break;
378 case theoryParam:
379 // TODO: throw exception instead of eLog()
380// try {
381 Tindex = boost::any_cast<enum Theory>(iter->second);
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// }
387 {
388 TheoryNamesType::const_iterator Titer = TheoryNames.find(Tindex);
389 value = Titer->second;
390 }
391 break;
392 default:
393 // TODO: throw exception instead of eLog()
394// try {
395 value = boost::any_cast<std::string>(iter->second);
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// }
401 break;
402 }
403
404 return value;
405}
406
407int MpqcParser_Parameters::getInt(enum Parameters _param) const
408{
409 int value;
410 parameterlist::const_iterator iter = params.find(_param);
411 switch (_param) {
412 default:
413 // TODO: throw exception instead of eLog()
414// try {
415 value = boost::any_cast<int>(iter->second);
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// }
421 break;
422 }
423 return value;
424}
425
426double MpqcParser_Parameters::getDouble(enum Parameters _param) const
427{
428 double value;
429 parameterlist::const_iterator iter = params.find(_param);
430 // TODO: throw exception instead of eLog()
431// try {
432 value = boost::any_cast<double>(iter->second);
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// }
438 return value;
439}
440
441bool MpqcParser_Parameters::getBool(enum Parameters _param) const
442{
443 bool value;
444 parameterlist::const_iterator iter = params.find(_param);
445 // TODO: throw exception instead of eLog()
446// try {
447 value = boost::any_cast<bool>(iter->second);
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// }
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.