MADNESS 0.10.1
chem/QCCalculationParametersBase.h
Go to the documentation of this file.
1/*
2 * QCCalculationParametersBase.h
3 *
4 * Created on: 27 Jun 2019
5 * Author: fbischoff
6 */
7
8#ifndef SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
9#define SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
10
12#include <string>
13#include <algorithm>
14#include <iomanip>
15#include <typeindex>
16#include <map>
17#include <set>
18#include <fstream>
19#include <typeinfo>
20// #include <madness/external/gtest/include/gtest/internal/gtest-type-util.h>
21
22#include "madness/misc/misc.h"
24#include "madness/world/world.h"
26#include <nlohmann/json.hpp>
27
28
29namespace madness {
30 using json = nlohmann::json;
31
32 template <typename T>
33 static typename std::enable_if<std::is_floating_point<T>::value, void>::type
34 check_for_inf(const std::string& str, T& arg) {
35 std::string sinf;
36 std::stringstream ss;
37 ss << std::numeric_limits<T>::infinity();
38 sinf = ss.str();
39
40 if (sinf == str) arg = std::numeric_limits<T>::infinity();
41 }
42
43 template <typename T>
44 static typename std::enable_if<!std::is_floating_point<T>::value, void>::type
45 check_for_inf(const std::string& str, T& arg) {
46 return;
47 }
48
49 /// inverting the print method from print.h for std::vector
50 /// TODO: move this where it belongs (into print.h ??)
51 template <typename T, typename A=std::allocator<T>>
52 std::istream& operator>>(std::istream& is, std::vector<T, A>& v) {
53 // get the full line from opening to closing brackets [ .. ]
54 std::string word, line = "";
55 while (is >> word) {
56 line += word;
57 if (word.find(']') != std::string::npos) break;
58 }
59 if (line.size() != 0) is.clear();
60
61 // remove enclosing brackets and commas
62 auto find_c = [](char& c) { return ((c == ',') or (c == '[') or (c == ']')); };
63 std::replace_if(line.begin(), line.end(), find_c, ' '); // 0 2 0 4 0 6 0 8 0
64
65 // stream the values into the container
66 std::stringstream sline(line);
67 T tmp;
68 while (sline >> word) {
69 std::stringstream sword(word);
70 sword >> tmp;
71 check_for_inf(word, tmp);
72 v.push_back(tmp);
73 }
74 if (sline.bad()) {
75 madness::print("error while reading vector from istream: ");
76 madness::print(line, "\n");
77 throw std::runtime_error("IO error");
78 }
79
80 return is;
81 }
82
83
84 /// inverting the print method from print.h for std::vector
85 /// TODO: move this where it belongs (into print.h ??)
86 template <typename Q, typename T>
87 std::istream& operator>>(std::istream& is, std::pair<T, Q>& p) {
88 // get all words from the line
89 std::string word, line = "";
90 while (is >> word) {
91 line += word + " ";
92 };
93 // have to set this here to account for the error handling later..
94 is.clear();
95
96 // remove enclosing brackets and commas
97 auto find_c = [](char& c) { return ((c == ',') or (c == '(') or (c == ')')); };
98 std::replace_if(line.begin(), line.end(), find_c, ' '); // 0 2 0 4 0 6 0 8 0
99
100 // stream the values into the container
101 std::stringstream sline(line);
102 T tmp1;
103 Q tmp2;
104 sline >> tmp1 >> tmp2;
105 if (sline.bad() or sline.fail()) {
106 madness::print("error while reading vector from istream: ");
107 madness::print(line, "\n");
108 throw std::runtime_error("IO error");
109 }
110 p = std::pair<T, Q>(tmp1, tmp2);
111
112 return is;
113 }
114
115
116 /// structure holding the value for a given parameter
117
118 /// keeps logic about default, derived and user-defined settings (with increasing priority),
119 /// as well as comments for the user (e.g. a recommended range for a given parameter).
120 ///
121 /// might be extended to hold allowed values for certain parameters, e.g. localization procedures.
122 ///
123 /// all values are stored as strings and must be converted to their respective types by the
124 /// QCCalculationParametersBase class (see below)
125 struct QCParameter {
126 public:
128
129 QCParameter(const std::string v, const std::string t, const std::string comment = "",
130 const std::vector<std::string> allowed_values1 = {})
131 : default_value(v), type(t), comment(comment), allowed_values(allowed_values1) {
132 static int i = 0;
133 print_order = i++;
134 set_all();
135 }
136
137 void set_derived_value(const std::string val) {
138 precedence = std::max(derived, precedence);
139 derived_value = val;
140 set_all();
141 }
142
143 void set_user_defined_value(const std::string val) {
144 precedence = std::max(defined, precedence);
145 user_defined_value = val;
146 set_all();
147 }
148
149 bool is_user_defined() const {
150 return (precedence == defined);
151 }
152
153 std::string get_value() const { return value; }
154 std::string get_type() const { return type; }
155 std::string get_comment() const { return comment; }
156
157 std::string print_precedence() const {
158 if (precedence == def) return "default";
159 if (precedence == derived) return "derived";
160 if (precedence == defined) return "defined";
161 std::stringstream sprecedence;
162 sprecedence << precedence;
163 throw std::runtime_error("unknown precedence in QCParameter" + sprecedence.str());
164 return "darn";
165 }
166
167 int get_print_order() const { return print_order; }
168
169 std::string print_line(const std::string& key) const {
170 auto fill_left = [](const int size, const std::string word) {
171 int nspaces = std::max(int(0), size - int(word.length()));
172 return std::string(nspaces, ' ') + word;
173 };
174 auto fill_right = [](const int size, const std::string word) {
175 int nspaces = std::max(int(0), size - int(word.length()));
176 return word + std::string(nspaces, ' ');
177 };
178
179 // key-value block
180 std::string keyval = fill_left(20, key) + " " + fill_right(10, get_value()) + " # " + fill_right(
181 10, print_precedence());
182 std::string empty_keyval(keyval.size(), ' ');
183 empty_keyval[33] = '#';
184
185 std::string allowed_val;
186 if (allowed_values.size() > 0) {
187 using madness::operators::operator<<;
188 std::stringstream ss;
189 ss << allowed_values;
190 allowed_val += ss.str();
191 }
192
193 // split comment into several lines: split onto words and add linebreak
194 bool leave_space_for_allowed_values = (allowed_val.size() > 0);
195
196 // first line breaks after 80 characters, all other lines after 120 (leave space f
197 long keyvalsize = keyval.size(); // length of key, value, precedence
198 std::string comment1 = get_comment();
199 auto commentwords = commandlineparser::split(comment1, " ");
200 std::vector<std::string> commentlines(1);
201 long nchar = 0;
202 for (auto word : commentwords) {
203 bool is_first_line = commentlines.size() == 1;
204 long thislinebreak = 120;
205 if (is_first_line and leave_space_for_allowed_values) thislinebreak = 80;
206 long commentsize = thislinebreak - keyvalsize;
207
208 nchar += word.size() + 1;
209 if (nchar > commentsize) { // start newline
210 commentlines.push_back("");
211 nchar = word.size() + 1;
212 }
213 commentlines.back() += word + " ";
214 }
215
216 std::string result;
217 for (size_t i = 0; i < commentlines.size(); ++i) {
218 if (i == 0) result = keyval + fill_right(40, commentlines[i]) + allowed_val;
219 else result += "\n" + empty_keyval + commentlines[i];
220 }
221
222 // trim result
223 std::size_t last = result.find_last_not_of(' ');
224 return result.substr(0, last + 1);
225 }
226
227 template <typename Archive>
232
234
235 bool operator==(const QCParameter& other) const {
236 return (value == other.value and default_value == other.default_value and
238 type == other.type and null == other.null and comment == other.comment and
240 }
241
242 hashT hash() const {
243 return hash_value(value);
244 }
245
246 private:
247 void set_all() {
251 if (not check_allowed()) throw std::invalid_argument(not_allowed_errmsg());
252 }
253
255 if (allowed_values.size() == 0) return true;
256 auto it = std::find(allowed_values.begin(), allowed_values.end(), value);
257 return (it != allowed_values.end());
258 }
259
260 std::string not_allowed_errmsg() const {
261 using madness::operators::operator<<;
262 std::stringstream ss;
263 ss << allowed_values;
264 std::string errmsg = "\ntrying to assign a value that's not allowed\n\n";
265 errmsg += "\tuser-defined value: " + value + "\n";
266 errmsg += "\tallowed values: " + ss.str() + "\n\n";
267 return errmsg;
268 }
269
270
271 std::string value;
272 std::string default_value = "";
273 std::string derived_value = "";
274 std::string user_defined_value = "";
275 std::string type = "";
276 std::string null = "";
277 std::string comment = "";
278 std::vector<std::string> allowed_values = std::vector<std::string>();
279 int print_order = 0; // use this for printing the parameters in the same order as they are defined
280 };
281
282 /// class for holding the parameters for calculation
283
284 /// Actual parameter classes will be derived from this class with a simple constructor
285 /// (see test_QCCalculationParametersBase.cc for an example) and convenience
286 /// getters for the parameters of each parameter class.
287 /// Having the base class will allow consistent parameter input/output handling
288 /// for all madness programs and reuse of the parsing methods.
289 /// Even if the same parameter is used in different programs, default might differ
290 /// (i.e. econv for 3D/6D calculations). The parameter class will effectively serve as
291 /// a factory for the calculation classes (SCF, nemo, mp2, etc)
292 ///
293 /// parameters are kept in a map with key (string) and value (QCParameter),
294 /// types are converted whenever a parameter is accessed (i.e. should not be
295 /// done too frequently in an inner loop)
297 public:
298 /// print all parameters
299 void print(const std::string header = "", const std::string footer = "") const;
300
301 virtual std::string get_tag() const = 0;
302
303 std::string print_to_string(const std::list<std::string> precedences={"all"}) const;
304
305 template <typename T>
306 T get(const std::string key) const {
307 const QCParameter& parameter = get_parameter(key);
308 MADNESS_ASSERT(check_type<T>(key,parameter));
309 if (std::is_same<T, std::string>::value) {
310 return fromstring<T>(add_quotes(parameter.get_value()));
311 }
312 return fromstring<T>(parameter.get_value());
313 }
314
315 bool is_user_defined(std::string key) const {
316 return get_parameter(key).is_user_defined();
317 }
318
319 template <typename Archive>
320 void serialize(Archive& ar) {
321 ar & parameters & print_debug;
322 }
323
324 hashT hash() const {
325 return hash_range(parameters.begin(), parameters.end());
326 }
327
328 protected:
329 typedef std::map<std::string, QCParameter> ParameterContainerT;
331
333 const commandlineparser& parser,
334 const std::string tag) {
335 try {
336 // check that user-defined input files actually exist
337 bool file_ok = true;
338 if (parser.key_exists("user_defined_input_file")) file_ok = file_exists(world, parser.value("input"));
339 if (file_ok) read_input(world, parser.value("input"), tag);
340 else {
341 std::string msg = "could not find user-defined input file: " + parser.value("input") + "\n";
342 throw std::invalid_argument(msg);
343 }
344 }
345 catch (std::invalid_argument& e) {
346 throw;
347 } catch (std::exception& e) {
348 madness::print(e.what());
349 }
350 read_commandline_options(world, parser, tag);
351 }
352
353 public:
354 bool file_exists(World& world, std::string filename) const;
355
356 /// Apply `--<tag>=key=val` command-line overrides on top of the current
357 /// values. Public so ParameterManager::initFromJson can layer CLI
358 /// overrides after from_json (the text path gets them via the ctor's
359 /// read_input_and_commandline_options; JSON decks otherwise ignored them).
360 void read_commandline_options(World& world, const commandlineparser& parser, const std::string tag);
361
362 private:
363 /// read the parameters from file
364
365 /// only world.rank()==0 reads the input file and broadcasts to all other nodes,
366 /// so we don't need to serialize the ParameterMap
367 void read_input(World& world, const std::string filename, const std::string tag);
368
369 protected:
370 bool print_debug = false;
374
375 /// ctor for testing
377
378 /// copy ctor
382
383 /// destructor
385
386 template <typename T>
387 void initialize(const std::string& key, const T& value, const std::string comment = "",
388 const std::vector<T> allowed_values = {}) {
389 if (parameters.find(key) != parameters.end()) {
390 madness::print("you cannot initialize a parameter twice: ", key);
391 throw std::runtime_error("initialization error");
392 }
393
394 std::string svalue = tostring(value);
395 std::string type = std::type_index(typeid(T)).name();
396
397 // transform everything to lower case
398 std::string key_lower = key;
399 std::transform(key_lower.begin(), key_lower.end(), key_lower.begin(), ::tolower);
400 std::transform(svalue.begin(), svalue.end(), svalue.begin(), ::tolower);
401 std::vector<std::string> av_lower_vec;
402 for (const T& av : allowed_values) {
403 std::string av_lower = tostring(av);
404 std::transform(av_lower.begin(), av_lower.end(), av_lower.begin(), ::tolower);
405 av_lower_vec.push_back(av_lower);
406 }
407
408 parameters.insert(std::make_pair<std::string, QCParameter>
409 (std::string(key_lower), QCParameter(svalue, type, comment, av_lower_vec)));
410 }
411
412 public:
413 /// keys whose string values must NOT be case-folded
414
415 /// String parameter values are lowercased on the way in so that enum-like
416 /// knobs match their lowercased \c allowed_values (`xc=HF`, `units=Bohr`).
417 /// That is wrong for free-form values such as paths and filenames, which
418 /// are case-sensitive on the filesystem. Derived classes list those keys
419 /// here and they are then stored verbatim -- the parameter-store analogue
420 /// of \c commandlineparser::set_keyval_keep_case.
421 virtual std::set<std::string> case_sensitive_keys() const { return {}; }
422
423 bool keeps_case(const std::string& key) const {
424 const auto keys = case_sensitive_keys();
425 return keys.find(key) != keys.end();
426 }
427
428 template <typename T>
429 void set_derived_value(const std::string& key, const T& value) {
430 QCParameter& parameter = get_parameter(key);
431 if (not check_type_silent<T>(parameter)) {
432 throw std::runtime_error("type error in set_derived_value for key " + key);
433 }
434 parameter.set_derived_value(to_stored_string(key, value));
435 }
436
440
441 protected:
442 /// helper function for try_setting_user_defined_value
443 template <typename Tuple, std::size_t... I>
444 bool try_setting_any_impl(const std::string& key, const std::string& value, Tuple, std::index_sequence<I...>) {
445 bool success = false;
446 (void)std::initializer_list<int>{
447 (success = try_setting_user_defined_value<std::tuple_element_t<I, Tuple>>(key, value) || success, 0)...
448 };
449 return success;
450 }
451
452 /// helper function for try_setting_user_defined_value
453 template <typename Tuple>
454 bool try_setting_any(const std::string& key, const std::string& value) {
455 return try_setting_any_impl(key, value, Tuple{}, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
456 }
457
458 /// helper function for try_setting_user_defined_value
459 template <typename T>
460 bool try_setting_user_defined_value(const std::string& key, const std::string& val) {
461 if (not check_type_silent<T>(get_parameter(key))) return false;
462
463 if (print_debug) ::madness::print("key:", key, "will set type", std::type_index(typeid(T)).name());
464 T value = fromstring<T>(val);
465 set_user_defined_value<T>(key, value);
466 return true;
467 }
468
469 public:
470
471 template <typename... Types>
472 bool try_set_from_json(QCParameter& parameter, const nlohmann::json& value) {
473 bool success = false;
474 (void)std::initializer_list<int>{
475 (parameter.get_type() == std::type_index(typeid(Types)).name()
476 ? (parameter.set_user_defined_value(QCCalculationParametersBase::tostring(value.get<Types>())), success = true, 0)
477 : 0)...
478 };
479 return success;
480 }
481
482 void from_json(const json& j) {
483 for (const auto& [key, value1] : j.items()) {
484 const auto& value = value1; // avoid shadowing
485 QCParameter& parameter = get_parameter(key);
486 using all_types = all_parameter_types;
487 bool ok = false;
488 std::apply([&](auto&&... args) {
489 ok = try_set_from_json<std::decay_t<decltype(args)>...>(parameter, value);
490 }, all_types{});
491 if (!ok) {
492 throw std::runtime_error("Unsupported parameter type for key: " + key);
493 }
494 }
495 }
496 // Helper to forward tuple types to qcparameter_to_json
497 template <typename... Types>
498 void qcparameter_to_json(json& j, const std::pair<std::string, QCParameter>& p,
499 const std::tuple<Types...>&) const {
500 (void)std::initializer_list<int>{
501 (check_type_silent<Types>(p.second) ? (j[p.first] = get<Types>(p.first), 0) : 0)...
502 };
503 }
504
505 // Helper to forward tuple types to qcparameter_to_json
506 template <typename Tuple>
507 void qcparameter_to_json_from_tuple(json& j, const std::pair<std::string, QCParameter>& p, const Tuple&) const {
508 std::apply([&](auto&&... args) {
509 qcparameter_to_json(j, p, std::tuple<std::decay_t<decltype(args)>...>{});
510 }, Tuple{});
511 }
512
513 /// convert all parameters to a json object
514 [[nodiscard]] json to_json() const {
515 json j_params = {};
516 to_json(j_params);
517 return j_params;
518 }
519
520 /// convert all parameters to a json object
521 void to_json(json& j) const {
522 json j_params = {};
523 for (auto& p : parameters) {
525 }
526 j["parameters"] = j_params;
527 }
528
529 /// convert all parameters to a json object, but only those with a given precedence
531 const std::string& precedence) const {
532 json j_params = {};
533 for (auto& p : parameters) {
534 if (p.second.print_precedence() == precedence) {
536 }
537 }
538 return j_params;
539 }
540
541 template <typename T>
542 void set_user_defined_value(const std::string& key, const T& value) {
543 QCParameter& parameter = get_parameter(key);
544 if (not check_type_silent<T>(parameter)) {
545 throw std::runtime_error("type error in set_user_defined_value");
546 }
547
548 parameter.set_user_defined_value(to_stored_string(key, value));
549 }
550
551 /// stringify \p value for storage, honoring case_sensitive_keys()
552 template <typename T>
553 std::string to_stored_string(const std::string& key, const T& value) const {
554 if constexpr (std::is_same<T, std::string>::value) {
555 if (keeps_case(key)) return value;
556 }
557 return tostring(value);
558 }
559
560 friend bool operator==(const QCCalculationParametersBase& p1,
562
563 protected:
564 const QCParameter& get_parameter(const std::string& key) const {
565 if (not parameter_exists(key)) {
566 throw std::runtime_error("could not find parameter for key " + key);
567 }
568 const QCParameter& parameter = parameters.find(key)->second;
569 return parameter;
570 }
571
572 public:
573 QCParameter& get_parameter(const std::string key) {
574 if (not parameter_exists(key)) {
575 madness::print("\ncould not find parameter for key", key, "\n");
576 throw std::runtime_error("could not find parameter for key " + key);
577 }
578 QCParameter& parameter = parameters.find(key)->second;
579 return parameter;
580 }
581
582 bool parameter_exists(const std::string& key) const {
583 return (parameters.find(key) != parameters.end());
584 }
585
586 template <typename T>
587 static bool check_type(const std::string key, const QCParameter& parameter) {
588 if (check_type_silent<T>(parameter))
589 return true;
590
592 "trying to get the wrong type in QCCalculationParametersBase");
593 madness::print("key ", key);
594 madness::print("parameter type ", parameter.get_type());
595 madness::print("setting type ", std::type_index(typeid(T)).name());
596 madness::print("value ", parameter.get_value());
597 return false;
598 }
599
600 template <typename T>
601 static bool check_type_silent(const QCParameter& parameter) {
602 return (parameter.get_type() == std::type_index(typeid(T)).name());
603 }
604
605 /// read the stream, starting from tag
606
607 /// only parameters that are defined in the constructor will be processed,
608 /// all others will be discarded.
609 virtual void read_internal(World& world, std::string& filecontents,
610 std::string tag);
611
612 static std::string tostring(const bool& arg) {
613 std::ostringstream ss;
614 ss << std::boolalpha << arg;
615 return ss.str();
616 }
617
618 template <typename T>
619 static std::string tostring(const T& arg) {
620 using madness::operators::operator<<;
621 std::ostringstream ss;
622 static_assert(not std::is_same<T, bool>::value, "you need to specialize tostring for this type");
623
624 ss << std::scientific << std::setprecision(4) << arg;
625 std::string str = ss.str();
626 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
627
628 overwrite_if_inf(str, arg);
629 return str;
630 }
631
632 template <typename T>
633 static typename std::enable_if<std::is_floating_point<T>::value, void>::type
634 overwrite_if_inf(std::string& str, const T& arg) {
635 MADNESS_PRAGMA_CLANG(diagnostic push)
636 MADNESS_PRAGMA_CLANG(diagnostic ignored "-Wtautological-constant-compare")
637 if (std::isinf(arg)) {
638 std::stringstream ss;
639 ss << std::numeric_limits<T>::infinity();
640 str = ss.str();
641 }
642 MADNESS_PRAGMA_CLANG(diagnostic pop)
643 }
644
645 template <typename T>
646 static typename std::enable_if<!std::is_floating_point<T>::value, void>::type
647 overwrite_if_inf(std::string& str, const T& arg) {
648 return;
649 }
650
651 template <typename T>
652 static typename std::enable_if<!std::is_same<T, bool>::value, T>::type
653 fromstring(const std::string& arg) {
654 std::stringstream ssvalue(arg);
655
656 // if argument is std::string read the everything between possible double quotes
657 T result = read_quotes<T>(ssvalue);
658
659 bool type_conversion_failed = ssvalue.fail();
660
661 // check for infinity in floating point conversions
662 if (type_conversion_failed and (std::is_floating_point<T>::value)) {
663 const static T inf = std::numeric_limits<T>::infinity();
664 std::string sinf = tostring(inf); // repeat type conversion from above
665 if (sinf == arg)
666 result = inf;
667 type_conversion_failed = false;
668 }
669
670 if (type_conversion_failed) {
671 std::string errmsg = "error in type conversion for argument >> " + arg +
672 " << to type " + std::type_index(typeid(T)).name();
673 throw std::runtime_error(errmsg);
674 }
675
676 // check for trailing characters
677 std::string word;
678 while (ssvalue >> word) {
679 std::string errmsg = "trailing characters in arguement >> " + arg + " <<";
680 throw std::runtime_error(errmsg);
681 }
682 return result;
683 }
684
685 template <typename T>
686 static typename std::enable_if<std::is_same<T, std::string>::value, T>::type
687 read_quotes(std::stringstream& ssvalue) {
688 T arg = ssvalue.str();
689 T result;
690
691 if (arg.find("\"") == std::string::npos) { // no double quotes found
692 ssvalue >> result;
693 }
694 else { // found double quotes
695 int counter = 0;
696 while (counter < 2) {
697 T tmp;
698 ssvalue >> tmp;
699 if (ssvalue.fail()) {
700 std::string errmsg = "missing closing double quote in line >> " + arg;
701 throw std::runtime_error(errmsg);
702 }
703 result += " " + tmp;
704 counter = std::count(result.begin(), result.end(), '"');
705 }
706
707 // use only the text between the double quotes
708 result = trim_blanks(trim_quotes(result));
709 }
710 return result;
711 }
712
713 static std::string trim_blanks(const std::string arg) {
714 std::size_t first = arg.find_first_not_of(' ');
715 std::size_t last = arg.find_last_not_of(' ');
716 return arg.substr(first, last - first + 1);
717 }
718
719 static std::string trim_quotes(const std::string arg) {
720 std::size_t first = arg.find_first_of('"');
721 std::size_t last = arg.find_last_of('"');
722 return arg.substr(first + 1, last - first - 1);
723 }
724
725 static std::string add_quotes(const std::string arg) {
726 return "\"" + arg + "\"";
727 }
728
729 template <typename T>
730 static typename std::enable_if<!std::is_same<T, std::string>::value, T>::type
731 read_quotes(std::stringstream& ssvalue) {
732 T result;
733 ssvalue >> result;
734 return result;
735 }
736
737 template <typename T>
738 static typename std::enable_if<std::is_same<T, bool>::value, T>::type
739 fromstring(const std::string& arg) {
740 std::string str = arg;
741 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
742 if (str == "true" or str == "1" or str == "yes")
743 return true;
744 if (str == "false" or str == "0" or str == "no")
745 return false;
746 std::string errmsg = "error in type conversion for argument >> " + arg +
747 " << to type " + std::type_index(typeid(T)).name();
748 throw std::runtime_error(errmsg);
749 return 0;
750 }
751
752
753 // a templated list of all acceptable parameter types for looping in a parameter pack
754 using all_parameter_types = std::tuple<
755 double, int, unsigned int, long, std::size_t, bool, std::string,
756 std::vector<double>, std::vector<int>, std::vector<std::size_t>,
757 std::vector<std::string>, std::pair<std::string, double>>;
758 };
759
762} /* namespace madness */
763
764#endif /* SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_ */
Interface templates for the archives (serialization).
class for holding the parameters for calculation
Definition chem/QCCalculationParametersBase.h:296
static bool check_type_silent(const QCParameter &parameter)
Definition chem/QCCalculationParametersBase.h:601
std::tuple< double, int, unsigned int, long, std::size_t, bool, std::string, std::vector< double >, std::vector< int >, std::vector< std::size_t >, std::vector< std::string >, std::pair< std::string, double > > all_parameter_types
Definition chem/QCCalculationParametersBase.h:757
static std::enable_if< std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition chem/QCCalculationParametersBase.h:687
void read_input_and_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
Definition chem/QCCalculationParametersBase.h:332
ParameterContainerT get_all_parameters() const
Definition chem/QCCalculationParametersBase.h:437
static std::string tostring(const T &arg)
Definition chem/QCCalculationParametersBase.h:619
bool keeps_case(const std::string &key) const
Definition chem/QCCalculationParametersBase.h:423
hashT hash() const
Definition chem/QCCalculationParametersBase.h:324
friend bool operator==(const QCCalculationParametersBase &p1, const QCCalculationParametersBase &p2)
Definition QCCalculationParametersBase.cc:222
std::string to_stored_string(const std::string &key, const T &value) const
stringify value for storage, honoring case_sensitive_keys()
Definition chem/QCCalculationParametersBase.h:553
static std::enable_if< std::is_floating_point< T >::value, void >::type overwrite_if_inf(std::string &str, const T &arg)
Definition chem/QCCalculationParametersBase.h:634
static std::enable_if<!std::is_floating_point< T >::value, void >::type overwrite_if_inf(std::string &str, const T &arg)
Definition chem/QCCalculationParametersBase.h:647
void qcparameter_to_json(json &j, const std::pair< std::string, QCParameter > &p, const std::tuple< Types... > &) const
Definition chem/QCCalculationParametersBase.h:498
bool throw_if_datagroup_not_found
Definition chem/QCCalculationParametersBase.h:373
bool try_setting_any_impl(const std::string &key, const std::string &value, Tuple, std::index_sequence< I... >)
helper function for try_setting_user_defined_value
Definition chem/QCCalculationParametersBase.h:444
void set_user_defined_value(const std::string &key, const T &value)
Definition chem/QCCalculationParametersBase.h:542
virtual std::set< std::string > case_sensitive_keys() const
keys whose string values must NOT be case-folded
Definition chem/QCCalculationParametersBase.h:421
bool print_debug
Definition chem/QCCalculationParametersBase.h:370
const QCParameter & get_parameter(const std::string &key) const
Definition chem/QCCalculationParametersBase.h:564
json to_json() const
convert all parameters to a json object
Definition chem/QCCalculationParametersBase.h:514
void to_json(json &j) const
convert all parameters to a json object
Definition chem/QCCalculationParametersBase.h:521
void qcparameter_to_json_from_tuple(json &j, const std::pair< std::string, QCParameter > &p, const Tuple &) const
Definition chem/QCCalculationParametersBase.h:507
virtual void read_internal(World &world, std::string &filecontents, std::string tag)
read the stream, starting from tag
Definition QCCalculationParametersBase.cc:143
void read_input(World &world, const std::string filename, const std::string tag)
read the parameters from file
Definition QCCalculationParametersBase.cc:84
std::string print_to_string(const std::list< std::string > precedences={"all"}) const
Definition QCCalculationParametersBase.cc:41
void read_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
read the parameters from the command line and broadcast
Definition QCCalculationParametersBase.cc:121
bool parameter_exists(const std::string &key) const
Definition chem/QCCalculationParametersBase.h:582
T get(const std::string key) const
Definition chem/QCCalculationParametersBase.h:306
bool try_set_from_json(QCParameter &parameter, const nlohmann::json &value)
Definition chem/QCCalculationParametersBase.h:472
void initialize(const std::string &key, const T &value, const std::string comment="", const std::vector< T > allowed_values={})
Definition chem/QCCalculationParametersBase.h:387
void print(const std::string header="", const std::string footer="") const
print all parameters
Definition QCCalculationParametersBase.cc:32
void from_json(const json &j)
Definition chem/QCCalculationParametersBase.h:482
bool ignore_unknown_keys
Definition chem/QCCalculationParametersBase.h:371
bool file_exists(World &world, std::string filename) const
Definition QCCalculationParametersBase.cc:72
static std::string tostring(const bool &arg)
Definition chem/QCCalculationParametersBase.h:612
QCCalculationParametersBase(const QCCalculationParametersBase &other)
copy ctor
Definition chem/QCCalculationParametersBase.h:379
static std::enable_if<!std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition chem/QCCalculationParametersBase.h:731
bool ignore_unknown_keys_silently
Definition chem/QCCalculationParametersBase.h:372
QCParameter & get_parameter(const std::string key)
Definition chem/QCCalculationParametersBase.h:573
bool is_user_defined(std::string key) const
Definition chem/QCCalculationParametersBase.h:315
void serialize(Archive &ar)
Definition chem/QCCalculationParametersBase.h:320
bool try_setting_user_defined_value(const std::string &key, const std::string &val)
helper function for try_setting_user_defined_value
Definition chem/QCCalculationParametersBase.h:460
json to_json_if_precedence(const std::string &precedence) const
convert all parameters to a json object, but only those with a given precedence
Definition chem/QCCalculationParametersBase.h:530
bool try_setting_any(const std::string &key, const std::string &value)
helper function for try_setting_user_defined_value
Definition chem/QCCalculationParametersBase.h:454
static std::string trim_blanks(const std::string arg)
Definition chem/QCCalculationParametersBase.h:713
ParameterContainerT parameters
Definition chem/QCCalculationParametersBase.h:330
std::map< std::string, QCParameter > ParameterContainerT
Definition chem/QCCalculationParametersBase.h:329
static bool check_type(const std::string key, const QCParameter &parameter)
Definition chem/QCCalculationParametersBase.h:587
static std::string add_quotes(const std::string arg)
Definition chem/QCCalculationParametersBase.h:725
static std::enable_if< std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition chem/QCCalculationParametersBase.h:739
static std::enable_if<!std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition chem/QCCalculationParametersBase.h:653
virtual ~QCCalculationParametersBase()
destructor
Definition chem/QCCalculationParametersBase.h:384
static std::string trim_quotes(const std::string arg)
Definition chem/QCCalculationParametersBase.h:719
void set_derived_value(const std::string &key, const T &value)
Definition chem/QCCalculationParametersBase.h:429
QCCalculationParametersBase()
ctor for testing
Definition chem/QCCalculationParametersBase.h:376
virtual std::string get_tag() const =0
A parallel world class.
Definition world.h:132
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
Tensor< typename Tensor< T >::scalar_type > arg(const Tensor< T > &t)
Return a new tensor holding the argument of each element of t (complex types only)
Definition tensor.h:2643
static const double v
Definition hatom_sf_dirac.cc:20
Macros and tools pertaining to the configuration of MADNESS.
#define MADNESS_PRAGMA_CLANG(x)
Definition madness_config.h:200
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
Header to declare stuff which has not yet found a home.
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
void hash_range(hashT &seed, It first, It last)
Definition worldhash.h:280
static const char * filename
Definition legendre.cc:96
static std::enable_if< std::is_floating_point< T >::value, void >::type check_for_inf(const std::string &str, T &arg)
Definition chem/QCCalculationParametersBase.h:34
bool operator!=(const QCCalculationParametersBase &p1, const QCCalculationParametersBase &p2)
Definition QCCalculationParametersBase.cc:233
static double pop(std::vector< double > &v)
Definition SCF.cc:115
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:226
nlohmann::json json
Definition chem/QCCalculationParametersBase.h:30
std::istream & operator>>(std::istream &is, PairType &en)
Definition PNOParameters.cpp:24
std::size_t hashT
The hash value type.
Definition worldhash.h:145
std::string type(const PairType &n)
Definition PNOParameters.h:18
std::string name(const FuncType &type, const int ex=-1)
Definition ccpairfunction.h:28
madness::hashT hash_value(const std::array< T, N > &a)
Hash std::array with madness hash.
Definition array_addons.h:78
double Q(double a)
Definition relops.cc:20
static const double c
Definition relops.cc:10
structure holding the value for a given parameter
Definition chem/QCCalculationParametersBase.h:125
int print_order
Definition chem/QCCalculationParametersBase.h:279
bool operator==(const QCParameter &other) const
Definition chem/QCCalculationParametersBase.h:235
std::string print_line(const std::string &key) const
Definition chem/QCCalculationParametersBase.h:169
QCParameter()
Definition chem/QCCalculationParametersBase.h:127
enum madness::QCParameter::@0 precedence
QCParameter(const std::string v, const std::string t, const std::string comment="", const std::vector< std::string > allowed_values1={})
Definition chem/QCCalculationParametersBase.h:129
int get_print_order() const
Definition chem/QCCalculationParametersBase.h:167
std::string default_value
Definition chem/QCCalculationParametersBase.h:272
std::string not_allowed_errmsg() const
Definition chem/QCCalculationParametersBase.h:260
void serialize(Archive &ar)
Definition chem/QCCalculationParametersBase.h:228
std::string user_defined_value
Definition chem/QCCalculationParametersBase.h:274
void set_all()
Definition chem/QCCalculationParametersBase.h:247
bool check_allowed()
Definition chem/QCCalculationParametersBase.h:254
@ derived
Definition chem/QCCalculationParametersBase.h:233
@ defined
Definition chem/QCCalculationParametersBase.h:233
@ def
Definition chem/QCCalculationParametersBase.h:233
std::vector< std::string > allowed_values
Definition chem/QCCalculationParametersBase.h:278
std::string get_comment() const
Definition chem/QCCalculationParametersBase.h:155
void set_user_defined_value(const std::string val)
Definition chem/QCCalculationParametersBase.h:143
std::string type
Definition chem/QCCalculationParametersBase.h:275
std::string print_precedence() const
Definition chem/QCCalculationParametersBase.h:157
std::string value
Definition chem/QCCalculationParametersBase.h:271
std::string derived_value
Definition chem/QCCalculationParametersBase.h:273
std::string get_type() const
Definition chem/QCCalculationParametersBase.h:154
std::string comment
Definition chem/QCCalculationParametersBase.h:277
bool is_user_defined() const
Definition chem/QCCalculationParametersBase.h:149
std::string null
Definition chem/QCCalculationParametersBase.h:276
std::string get_value() const
Definition chem/QCCalculationParametersBase.h:153
void set_derived_value(const std::string val)
Definition chem/QCCalculationParametersBase.h:137
hashT hash() const
Definition chem/QCCalculationParametersBase.h:242
very simple command line parser
Definition commandlineparser.h:28
std::string value(const std::string key) const
Definition commandlineparser.h:84
bool key_exists(std::string key) const
Definition commandlineparser.h:80
static std::vector< std::string > split(std::string s, const std::string delimiter)
split a string s into a vector of strings, using delimiter
Definition commandlineparser.h:136
static const double_complex I
Definition tdse1d.cc:164
std::string ok(const bool b)
Definition test6.cc:43
void errmsg(const char *msg, int status)
Definition test_hashthreaded.cc:54
void e()
Definition test_sig.cc:75
Declares the World class for the parallel runtime environment.