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