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 &mpqc_params)
|
---|
129 | {
|
---|
130 | std::ostringstream output;
|
---|
131 | output << "Hessian=" << mpqc_params.getString(MpqcParser_Parameters::hessianParam) << ";";
|
---|
132 | output << "savestate=" << mpqc_params.getString(MpqcParser_Parameters::savestateParam) << ";";
|
---|
133 | output << "do_gradient=" << mpqc_params.getString(MpqcParser_Parameters::do_gradientParam) << ";";
|
---|
134 | output << "maxiter=" << mpqc_params.getString(MpqcParser_Parameters::maxiterParam) << ";";
|
---|
135 | output << "memory=" << mpqc_params.getString(MpqcParser_Parameters::memoryParam) << ";";
|
---|
136 | output << "stdapprox=" << mpqc_params.getString(MpqcParser_Parameters::stdapproxParam) << ";";
|
---|
137 | output << "nfzc=" << mpqc_params.getString(MpqcParser_Parameters::nfzcParam) << ";";
|
---|
138 | output << "basis=" << mpqc_params.getString(MpqcParser_Parameters::basisParam) << ";";
|
---|
139 | output << "aux_basis=" << mpqc_params.getString(MpqcParser_Parameters::aux_basisParam) << ";";
|
---|
140 | output << "integration=" << mpqc_params.getString(MpqcParser_Parameters::integrationParam) << ";";
|
---|
141 | output << "theory=" << mpqc_params.getString(MpqcParser_Parameters::theoryParam) << ";";
|
---|
142 | ost << output.str();
|
---|
143 | return ost;
|
---|
144 | }
|
---|
145 |
|
---|
146 | std::istream & operator >> (std::istream& ist, MpqcParser_Parameters ¶ms)
|
---|
147 | {
|
---|
148 | typedef boost::tokenizer<boost::char_separator<char> >
|
---|
149 | tokenizer;
|
---|
150 | boost::char_separator<char> semicolonsep(";");
|
---|
151 | boost::char_separator<char> equalitysep(" =");
|
---|
152 | std::string line;
|
---|
153 | std::getline( ist, line );
|
---|
154 | //DoLog(0) && (Log() << Verbose(0) << "INFO: full line of parameters is '" << line << "'" << std::endl);
|
---|
155 | tokenizer tokens(line, semicolonsep);
|
---|
156 | ASSERT(tokens.begin() != tokens.end(),
|
---|
157 | "operator<< on MpqcParser_Parameters - empty string, need at least ';'!");
|
---|
158 | for (tokenizer::iterator tok_iter = tokens.begin();
|
---|
159 | tok_iter != tokens.end(); ++tok_iter) {
|
---|
160 | tokenizer paramtokens(*tok_iter, equalitysep);
|
---|
161 | if (paramtokens.begin() != paramtokens.end()) {
|
---|
162 | tokenizer::iterator tok_paramiter = paramtokens.begin();
|
---|
163 | tokenizer::iterator tok_valueiter = tok_paramiter;
|
---|
164 | tokenizer::iterator tok_checkiter = ++tok_valueiter;
|
---|
165 | // TODO: throw exception instead of ASSERT
|
---|
166 | ASSERT(tok_valueiter == params.end(),
|
---|
167 | "operator<< on MpqcParser_Parameters - missing value before ';'!");
|
---|
168 | ++tok_checkiter;
|
---|
169 | ASSERT(tok_checkiter == params.end(),
|
---|
170 | "operator<< on MpqcParser_Parameters - still tokens before ';'!");
|
---|
171 | DoLog(0) && (Log() << Verbose(0)
|
---|
172 | << "INFO: Token pair is " << *tok_paramiter << "," << *tok_valueiter << std::endl);
|
---|
173 | std::stringstream value(*tok_valueiter);
|
---|
174 |
|
---|
175 | // TODO: throw exception instead of DoeLog()
|
---|
176 | if ((params.haveParam(*tok_paramiter))) {
|
---|
177 | params.setter(params.getParam(*tok_paramiter), value);
|
---|
178 | } else {
|
---|
179 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
180 | << *tok_paramiter << " is not a valid parameter name!" << std::endl);
|
---|
181 | }
|
---|
182 | } else {
|
---|
183 | ist.setstate(std::ios::eofbit);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return ist;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | /** Sets a desired value in the params from a string.
|
---|
191 | *
|
---|
192 | * This is due to strict typing of C++ very ugly and boost::any does not make
|
---|
193 | * it any better because it offers to functions to use values directly from
|
---|
194 | * stringstream. Probably, because value is unknown to is as well and hence
|
---|
195 | * the author could not implement it beautifully, so he dropped it altogether.
|
---|
196 | * Grrr ....
|
---|
197 | *
|
---|
198 | * @param _param param to set
|
---|
199 | * @param _desired stringstream containing value as next argument
|
---|
200 | * @return true - type ok, false - unknown type in params.
|
---|
201 | */
|
---|
202 | bool MpqcParser_Parameters::setter(enum Parameters _param, std::stringstream& _desired) {
|
---|
203 | if (_param == integrationParam) {
|
---|
204 | std::string tmp;
|
---|
205 | _desired >> tmp;
|
---|
206 | params[_param] = IntegrationLookup[tmp];
|
---|
207 | } else if(_param == theoryParam) {
|
---|
208 | std::string tmp;
|
---|
209 | _desired >> tmp;
|
---|
210 | params[_param] = TheoryLookup[tmp];
|
---|
211 | } else if (params[_param].type() == typeid(std::string)) {
|
---|
212 | std::string tmp;
|
---|
213 | _desired >> tmp;
|
---|
214 | params[_param] = tmp;
|
---|
215 | } else if (params[_param].type() == typeid(int)) {
|
---|
216 | int tmp;
|
---|
217 | _desired >> tmp;
|
---|
218 | params[_param] = tmp;
|
---|
219 | } else if (params[_param].type() == typeid(double)) {
|
---|
220 | double tmp;
|
---|
221 | _desired >> tmp;
|
---|
222 | params[_param] = tmp;
|
---|
223 | } else if (params[_param].type() == typeid(bool)) {
|
---|
224 | std::string tmp;
|
---|
225 | _desired >> tmp;
|
---|
226 | if (tmp == "yes") {
|
---|
227 | params[_param] = bool(true);
|
---|
228 | } else if (tmp == "no") {
|
---|
229 | params[_param] = bool(false);
|
---|
230 | } else {
|
---|
231 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
232 | << "MpqcParser_Parameters::setter() - unknown boolean key "
|
---|
233 | << tmp << "!" << std::endl);
|
---|
234 | }
|
---|
235 | } else {
|
---|
236 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
237 | << "MpqcParser_Parameters::setter() - unknown type!" << std::endl);
|
---|
238 | return false;
|
---|
239 | }
|
---|
240 | return true;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | void MpqcParser_Parameters::setTheory(enum Theory _theory)
|
---|
245 | {
|
---|
246 | // TODO: throw exception instead of eLog()
|
---|
247 | try {
|
---|
248 | params[theoryParam] = _theory;
|
---|
249 | } catch(const boost::bad_any_cast &) {
|
---|
250 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
251 | << "MpqcParser_Parameters::setTheory() - could not set boolean!" << std::endl);
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | void MpqcParser_Parameters::setIntegration(enum MpqcParser_Parameters::IntegrationMethod _integration){
|
---|
256 | // TODO: throw exception instead of eLog()
|
---|
257 | try {
|
---|
258 | params[integrationParam] = _integration;
|
---|
259 | } catch(const boost::bad_any_cast &) {
|
---|
260 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
261 | << "MpqcParser_Parameters::setIntegration() - could not set boolean!" << std::endl);
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | bool MpqcParser_Parameters::haveParam(std::string _name) const
|
---|
266 | {
|
---|
267 | return ParamLookup.count(_name) != 0;
|
---|
268 | }
|
---|
269 |
|
---|
270 | enum MpqcParser_Parameters::Parameters MpqcParser_Parameters::getParam(std::string _name)
|
---|
271 | {
|
---|
272 | return ParamLookup[_name];
|
---|
273 | }
|
---|
274 |
|
---|
275 | enum MpqcParser_Parameters::IntegrationMethod MpqcParser_Parameters::getIntegration()
|
---|
276 | {
|
---|
277 | enum IntegrationMethod value;
|
---|
278 | // TODO: throw exception instead of eLog()
|
---|
279 | try {
|
---|
280 | value = boost::any_cast<enum IntegrationMethod>(params[integrationParam]);
|
---|
281 | } catch(const boost::bad_any_cast &) {
|
---|
282 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
283 | << "MpqcParser_Parameters::getIntegration() - could not convert "
|
---|
284 | +ParamNames[integrationParam]+" to enum IntegrationMethod!" << std::endl);
|
---|
285 | }
|
---|
286 | return value;
|
---|
287 | }
|
---|
288 |
|
---|
289 | enum MpqcParser_Parameters::Theory MpqcParser_Parameters::getTheory()
|
---|
290 | {
|
---|
291 | enum Theory value;
|
---|
292 | // TODO: throw exception instead of eLog()
|
---|
293 | try {
|
---|
294 | value = boost::any_cast<enum Theory>(params[theoryParam]);
|
---|
295 | } catch(const boost::bad_any_cast &) {
|
---|
296 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
297 | << "MpqcParser_Parameters::getTheory() - could not convert "
|
---|
298 | +ParamNames[theoryParam]+" to enum Theory!" << std::endl);
|
---|
299 | }
|
---|
300 | return value;
|
---|
301 | }
|
---|
302 |
|
---|
303 | std::string MpqcParser_Parameters::getString(enum Parameters _param)
|
---|
304 | {
|
---|
305 | std::string value;
|
---|
306 | enum IntegrationMethod Iindex;
|
---|
307 | enum Theory Tindex;
|
---|
308 | bool test;
|
---|
309 | switch (_param) {
|
---|
310 | case hessianParam:
|
---|
311 | case savestateParam:
|
---|
312 | case do_gradientParam:
|
---|
313 | test = boost::any_cast<bool>(params[_param]);
|
---|
314 | if (test)
|
---|
315 | value = "yes";
|
---|
316 | else
|
---|
317 | value = "no";
|
---|
318 | break;
|
---|
319 | case integrationParam:
|
---|
320 | // TODO: throw exception instead of eLog()
|
---|
321 | try {
|
---|
322 | Iindex = boost::any_cast<enum IntegrationMethod>(params[_param]);
|
---|
323 | } catch(const boost::bad_any_cast &) {
|
---|
324 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
325 | << "MpqcParser_Parameters::getString() - could not convert "
|
---|
326 | +ParamNames[_param]+" to string!" << std::endl);
|
---|
327 | }
|
---|
328 | value = IntegrationNames[Iindex];
|
---|
329 | break;
|
---|
330 | case theoryParam:
|
---|
331 | // TODO: throw exception instead of eLog()
|
---|
332 | try {
|
---|
333 | Tindex = boost::any_cast<enum Theory>(params[_param]);
|
---|
334 | } catch(const boost::bad_any_cast &) {
|
---|
335 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
336 | << "MpqcParser_Parameters::getString() - could not convert "
|
---|
337 | +ParamNames[_param]+" to string!" << std::endl);
|
---|
338 | }
|
---|
339 | value = TheoryNames[(enum Theory)Tindex];
|
---|
340 | break;
|
---|
341 | default:
|
---|
342 | // TODO: throw exception instead of eLog()
|
---|
343 | try {
|
---|
344 | value = boost::any_cast<std::string>(params[_param]);
|
---|
345 | } catch(const boost::bad_any_cast &) {
|
---|
346 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
347 | << "MpqcParser_Parameters::getString() - could not convert "
|
---|
348 | +ParamNames[_param]+" to string!" << std::endl);
|
---|
349 | }
|
---|
350 | break;
|
---|
351 | }
|
---|
352 |
|
---|
353 | return value;
|
---|
354 | }
|
---|
355 |
|
---|
356 | int MpqcParser_Parameters::getInt(enum Parameters _param)
|
---|
357 | {
|
---|
358 | int value;
|
---|
359 | switch (_param) {
|
---|
360 | default:
|
---|
361 | // TODO: throw exception instead of eLog()
|
---|
362 | try {
|
---|
363 | value = boost::any_cast<int>(params[_param]);
|
---|
364 | } catch(const boost::bad_any_cast &) {
|
---|
365 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
366 | << "MpqcParser_Parameters::getInt() - could not convert "
|
---|
367 | +ParamNames[_param]+" to int!" << std::endl);
|
---|
368 | }
|
---|
369 | break;
|
---|
370 | }
|
---|
371 | return value;
|
---|
372 | }
|
---|
373 |
|
---|
374 | double MpqcParser_Parameters::getDouble(enum Parameters _param)
|
---|
375 | {
|
---|
376 | double value;
|
---|
377 | // TODO: throw exception instead of eLog()
|
---|
378 | try {
|
---|
379 | value = boost::any_cast<double>(params[_param]);
|
---|
380 | } catch(const boost::bad_any_cast &) {
|
---|
381 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
382 | << "MpqcParser_Parameters::getDouble() - could not convert "
|
---|
383 | +ParamNames[_param]+" to double!" << std::endl);
|
---|
384 | }
|
---|
385 | return value;
|
---|
386 | }
|
---|
387 |
|
---|
388 | bool MpqcParser_Parameters::getBool(enum Parameters _param)
|
---|
389 | {
|
---|
390 | bool value;
|
---|
391 | // TODO: throw exception instead of eLog()
|
---|
392 | try {
|
---|
393 | value = boost::any_cast<bool>(params[_param]);
|
---|
394 | } catch(const boost::bad_any_cast &) {
|
---|
395 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
396 | << "MpqcParser_Parameters::getBool() - could not convert "
|
---|
397 | +ParamNames[_param]+" to bool!" << std::endl);
|
---|
398 | }
|
---|
399 | return value;
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 | /** Checks whether all elements in the world also have parameters in the basis.
|
---|
404 | *
|
---|
405 | * @return true - all elements parametrized, false - at least one element is missing.
|
---|
406 | */
|
---|
407 | bool MpqcParser_Parameters::checkWorldElementsAgainstCurrentBasis() const
|
---|
408 | {
|
---|
409 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
410 | << "MpqcParser_Parameters::checkWorldElementsAgainstCurrentBasis() - not implemented yet."
|
---|
411 | << std::endl);
|
---|
412 |
|
---|
413 | return false;
|
---|
414 | }
|
---|
415 |
|
---|