8#ifndef SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
9#define SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
26#include <nlohmann/json.hpp>
30 using json = nlohmann::json;
33 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
37 ss << std::numeric_limits<T>::infinity();
40 if (sinf == str)
arg = std::numeric_limits<T>::infinity();
44 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
51 template <
typename T,
typename A=std::allocator<T>>
52 std::istream&
operator>>(std::istream& is, std::vector<T, A>&
v) {
54 std::string word, line =
"";
57 if (word.find(
']') != std::string::npos)
break;
59 if (line.size() != 0) is.clear();
62 auto find_c = [](
char&
c) {
return ((
c ==
',') or (
c ==
'[') or (
c ==
']')); };
63 std::replace_if(line.begin(), line.end(), find_c,
' ');
68 std::stringstream sline(line);
70 while (sline >> word) {
71 if constexpr (std::is_same_v<T, std::string>) {
72 if (word.size() >= 2 and word.front() ==
'"' and word.back() ==
'"')
73 word = word.substr(1, word.size() - 2);
74 if (word.empty())
continue;
76 std::stringstream sword(word);
84 throw std::runtime_error(
"IO error");
93 template <
typename Q,
typename T>
94 std::istream&
operator>>(std::istream& is, std::pair<T, Q>&
p) {
96 std::string word, line =
"";
104 auto find_c = [](
char&
c) {
return ((
c ==
',') or (
c ==
'(') or (
c ==
')')); };
105 std::replace_if(line.begin(), line.end(), find_c,
' ');
108 std::stringstream sline(line);
111 sline >> tmp1 >> tmp2;
112 if (sline.bad() or sline.fail()) {
115 throw std::runtime_error(
"IO error");
117 p = std::pair<T, Q>(tmp1, tmp2);
137 const std::vector<std::string> allowed_values1 = {})
168 std::stringstream sprecedence;
170 throw std::runtime_error(
"unknown precedence in QCParameter" + sprecedence.str());
177 auto fill_left = [](
const int size,
const std::string word) {
178 int nspaces = std::max(
int(0), size -
int(word.length()));
179 return std::string(nspaces,
' ') + word;
181 auto fill_right = [](
const int size,
const std::string word) {
182 int nspaces = std::max(
int(0), size -
int(word.length()));
183 return word + std::string(nspaces,
' ');
187 std::string keyval = fill_left(20, key) +
" " + fill_right(10,
get_value()) +
" # " + fill_right(
189 std::string empty_keyval(keyval.size(),
' ');
190 empty_keyval[33] =
'#';
192 std::string allowed_val;
194 using madness::operators::operator<<;
195 std::stringstream ss;
197 allowed_val += ss.str();
201 bool leave_space_for_allowed_values = (allowed_val.size() > 0);
204 long keyvalsize = keyval.size();
207 std::vector<std::string> commentlines(1);
209 for (
auto word : commentwords) {
210 bool is_first_line = commentlines.size() == 1;
211 long thislinebreak = 120;
212 if (is_first_line and leave_space_for_allowed_values) thislinebreak = 80;
213 long commentsize = thislinebreak - keyvalsize;
215 nchar += word.size() + 1;
216 if (nchar > commentsize) {
217 commentlines.push_back(
"");
218 nchar = word.size() + 1;
220 commentlines.back() += word +
" ";
224 for (
size_t i = 0; i < commentlines.size(); ++i) {
225 if (i == 0) result = keyval + fill_right(40, commentlines[i]) + allowed_val;
226 else result +=
"\n" + empty_keyval + commentlines[i];
230 std::size_t last = result.find_last_not_of(
' ');
231 return result.substr(0, last + 1);
234 template <
typename Archive>
268 using madness::operators::operator<<;
269 std::stringstream ss;
271 std::string
errmsg =
"\ntrying to assign a value that's not allowed\n\n";
273 errmsg +=
"\tallowed values: " + ss.str() +
"\n\n";
306 void print(
const std::string header =
"",
const std::string footer =
"")
const;
310 std::string
print_to_string(
const std::list<std::string> precedences={
"all"})
const;
312 template <
typename T>
313 T
get(
const std::string key)
const {
316 if (std::is_same<T, std::string>::value) {
319 return fromstring<T>(parameter.
get_value());
326 template <
typename Archive>
341 const std::string tag) {
346 std::string msg =
"could not find user-defined input file: " + parser.
value(
"input") +
"\n";
347 throw std::invalid_argument(msg);
393 template <
typename T>
394 void initialize(
const std::string& key,
const T& value,
const std::string comment =
"",
395 const std::vector<T> allowed_values = {}) {
398 throw std::runtime_error(
"initialization error");
401 std::string svalue =
tostring(value);
402 std::string
type = std::type_index(
typeid(T)).name();
405 std::string key_lower = key;
406 std::transform(key_lower.begin(), key_lower.end(), key_lower.begin(), ::tolower);
407 std::transform(svalue.begin(), svalue.end(), svalue.begin(), ::tolower);
408 std::vector<std::string> av_lower_vec;
409 for (
const T& av : allowed_values) {
410 std::string av_lower =
tostring(av);
411 std::transform(av_lower.begin(), av_lower.end(), av_lower.begin(), ::tolower);
412 av_lower_vec.push_back(av_lower);
415 parameters.insert(std::make_pair<std::string, QCParameter>
416 (std::string(key_lower), QCParameter(svalue,
type, comment, av_lower_vec)));
432 return keys.find(key) != keys.end();
435 template <
typename T>
438 if (not check_type_silent<T>(parameter)) {
439 throw std::runtime_error(
"type error in set_derived_value for key " + key);
450 template <
typename Tuple, std::size_t...
I>
451 bool try_setting_any_impl(
const std::string& key,
const std::string& value, Tuple, std::index_sequence<I...>) {
452 bool success =
false;
453 (void)std::initializer_list<int>{
454 (success = try_setting_user_defined_value<std::tuple_element_t<I, Tuple>>(key, value) || success, 0)...
460 template <
typename Tuple>
462 return try_setting_any_impl(key, value, Tuple{}, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
466 template <
typename T>
468 if (not check_type_silent<T>(
get_parameter(key)))
return false;
471 T value = fromstring<T>(val);
472 set_user_defined_value<T>(key, value);
478 template <
typename... Types>
480 bool success =
false;
481 (void)std::initializer_list<int>{
482 (parameter.
get_type() == std::type_index(
typeid(Types)).name()
490 for (
const auto& [key, value1] : j.items()) {
491 const auto& value = value1;
495 std::apply([&](
auto&&... args) {
499 throw std::runtime_error(
"Unsupported parameter type for key: " + key);
504 template <
typename... Types>
506 const std::tuple<Types...>&)
const {
507 (void)std::initializer_list<int>{
508 (check_type_silent<Types>(
p.second) ? (j[
p.first] = get<Types>(
p.first), 0) : 0)...
513 template <
typename Tuple>
515 std::apply([&](
auto&&... args) {
533 j[
"parameters"] = j_params;
538 const std::string& precedence)
const {
541 if (
p.second.print_precedence() == precedence) {
548 template <
typename T>
551 if (not check_type_silent<T>(parameter)) {
552 throw std::runtime_error(
"type error in set_user_defined_value");
562 }
catch (
const std::invalid_argument&
e) {
563 throw std::invalid_argument(
"in keyword `" + key +
"`: " +
e.what());
568 template <
typename T>
570 if constexpr (std::is_same<T, std::string>::value) {
582 throw std::runtime_error(
"could not find parameter for key " + key);
592 throw std::runtime_error(
"could not find parameter for key " + key);
602 template <
typename T>
604 if (check_type_silent<T>(parameter))
608 "trying to get the wrong type in QCCalculationParametersBase");
616 template <
typename T>
618 return (parameter.
get_type() == std::type_index(
typeid(T)).name());
629 std::ostringstream ss;
630 ss << std::boolalpha <<
arg;
634 template <
typename T>
636 using madness::operators::operator<<;
637 std::ostringstream ss;
638 static_assert(not std::is_same<T, bool>::value,
"you need to specialize tostring for this type");
640 ss << std::scientific << std::setprecision(4) <<
arg;
641 std::string str = ss.str();
642 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
648 template <
typename T>
649 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
653 if (std::isinf(
arg)) {
654 std::stringstream ss;
655 ss << std::numeric_limits<T>::infinity();
661 template <
typename T>
662 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
667 template <
typename T>
668 static typename std::enable_if<!std::is_same<T, bool>::value, T>
::type
670 std::stringstream ssvalue(
arg);
673 T result = read_quotes<T>(ssvalue);
675 bool type_conversion_failed = ssvalue.fail();
681 if (type_conversion_failed and (std::is_floating_point<T>::value)) {
682 const static T inf = std::numeric_limits<T>::infinity();
686 type_conversion_failed =
false;
690 if (type_conversion_failed) {
691 std::string
errmsg =
"error in type conversion for argument >> " +
arg +
692 " << to type " + std::type_index(
typeid(T)).name();
693 throw std::runtime_error(
errmsg);
698 while (ssvalue >> word) {
699 std::string
errmsg =
"trailing characters in arguement >> " +
arg +
" <<";
700 throw std::runtime_error(
errmsg);
705 template <
typename T>
706 static typename std::enable_if<std::is_same<T, std::string>::value, T>
::type
708 T
arg = ssvalue.str();
711 if (
arg.find(
"\"") == std::string::npos) {
724 if (result.empty()) ssvalue.setstate(std::ios::failbit);
728 while (counter < 2) {
731 if (ssvalue.fail()) {
732 std::string
errmsg =
"missing closing double quote in line >> " +
arg;
733 throw std::runtime_error(
errmsg);
736 counter = std::count(result.begin(), result.end(),
'"');
746 std::size_t first =
arg.find_first_not_of(
' ');
747 if (first == std::string::npos)
return "";
748 std::size_t last =
arg.find_last_not_of(
' ');
749 return arg.substr(first, last - first + 1);
753 std::size_t first =
arg.find_first_of(
'"');
754 std::size_t last =
arg.find_last_of(
'"');
755 return arg.substr(first + 1, last - first - 1);
759 return "\"" +
arg +
"\"";
762 template <
typename T>
763 static typename std::enable_if<!std::is_same<T, std::string>::value, T>
::type
770 template <
typename T>
771 static typename std::enable_if<std::is_same<T, bool>::value, T>
::type
773 std::string str =
arg;
774 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
775 if (str ==
"true" or str ==
"1" or str ==
"yes")
777 if (str ==
"false" or str ==
"0" or str ==
"no")
779 std::string
errmsg =
"error in type conversion for argument >> " +
arg +
780 " << to type " + std::type_index(
typeid(T)).name();
781 throw std::runtime_error(
errmsg);
788 double, int,
unsigned int, long, std::size_t, bool, std::string,
789 std::vector<double>, std::vector<int>, std::vector<std::size_t>,
790 std::vector<std::string>, std::pair<std::string, double>>;
Interface templates for the archives (serialization).
class for holding the parameters for calculation
Definition chem/QCCalculationParametersBase.h:303
static bool check_type_silent(const QCParameter ¶meter)
Definition chem/QCCalculationParametersBase.h:617
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:790
static std::enable_if< std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition chem/QCCalculationParametersBase.h:707
void read_input_and_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
Definition chem/QCCalculationParametersBase.h:339
ParameterContainerT get_all_parameters() const
Definition chem/QCCalculationParametersBase.h:444
static std::string tostring(const T &arg)
Definition chem/QCCalculationParametersBase.h:635
bool keeps_case(const std::string &key) const
Definition chem/QCCalculationParametersBase.h:430
hashT hash() const
Definition chem/QCCalculationParametersBase.h:331
friend bool operator==(const QCCalculationParametersBase &p1, const QCCalculationParametersBase &p2)
Definition QCCalculationParametersBase.cc:237
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:569
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:650
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:663
void qcparameter_to_json(json &j, const std::pair< std::string, QCParameter > &p, const std::tuple< Types... > &) const
Definition chem/QCCalculationParametersBase.h:505
bool throw_if_datagroup_not_found
Definition chem/QCCalculationParametersBase.h:380
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:451
void set_user_defined_value(const std::string &key, const T &value)
Definition chem/QCCalculationParametersBase.h:549
virtual std::set< std::string > case_sensitive_keys() const
keys whose string values must NOT be case-folded
Definition chem/QCCalculationParametersBase.h:428
bool print_debug
Definition chem/QCCalculationParametersBase.h:377
const QCParameter & get_parameter(const std::string &key) const
Definition chem/QCCalculationParametersBase.h:580
json to_json() const
convert all parameters to a json object
Definition chem/QCCalculationParametersBase.h:521
void to_json(json &j) const
convert all parameters to a json object
Definition chem/QCCalculationParametersBase.h:528
void qcparameter_to_json_from_tuple(json &j, const std::pair< std::string, QCParameter > &p, const Tuple &) const
Definition chem/QCCalculationParametersBase.h:514
virtual void read_internal(World &world, std::string &filecontents, std::string tag)
read the stream, starting from tag
Definition QCCalculationParametersBase.cc:155
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:133
bool parameter_exists(const std::string &key) const
Definition chem/QCCalculationParametersBase.h:598
T get(const std::string key) const
Definition chem/QCCalculationParametersBase.h:313
bool try_set_from_json(QCParameter ¶meter, const nlohmann::json &value)
Definition chem/QCCalculationParametersBase.h:479
void initialize(const std::string &key, const T &value, const std::string comment="", const std::vector< T > allowed_values={})
Definition chem/QCCalculationParametersBase.h:394
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:489
bool ignore_unknown_keys
Definition chem/QCCalculationParametersBase.h:378
bool file_exists(World &world, std::string filename) const
Definition QCCalculationParametersBase.cc:72
static std::string tostring(const bool &arg)
Definition chem/QCCalculationParametersBase.h:628
QCCalculationParametersBase(const QCCalculationParametersBase &other)
copy ctor
Definition chem/QCCalculationParametersBase.h:386
static std::enable_if<!std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition chem/QCCalculationParametersBase.h:764
bool ignore_unknown_keys_silently
Definition chem/QCCalculationParametersBase.h:379
QCParameter & get_parameter(const std::string key)
Definition chem/QCCalculationParametersBase.h:589
bool is_user_defined(std::string key) const
Definition chem/QCCalculationParametersBase.h:322
void serialize(Archive &ar)
Definition chem/QCCalculationParametersBase.h:327
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:467
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:537
bool try_setting_any(const std::string &key, const std::string &value)
helper function for try_setting_user_defined_value
Definition chem/QCCalculationParametersBase.h:461
static std::string trim_blanks(const std::string arg)
Definition chem/QCCalculationParametersBase.h:745
ParameterContainerT parameters
Definition chem/QCCalculationParametersBase.h:337
std::map< std::string, QCParameter > ParameterContainerT
Definition chem/QCCalculationParametersBase.h:336
static bool check_type(const std::string key, const QCParameter ¶meter)
Definition chem/QCCalculationParametersBase.h:603
static std::string add_quotes(const std::string arg)
Definition chem/QCCalculationParametersBase.h:758
static std::enable_if< std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition chem/QCCalculationParametersBase.h:772
static std::enable_if<!std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition chem/QCCalculationParametersBase.h:669
virtual ~QCCalculationParametersBase()
destructor
Definition chem/QCCalculationParametersBase.h:391
static std::string trim_quotes(const std::string arg)
Definition chem/QCCalculationParametersBase.h:752
void set_derived_value(const std::string &key, const T &value)
Definition chem/QCCalculationParametersBase.h:436
QCCalculationParametersBase()
ctor for testing
Definition chem/QCCalculationParametersBase.h:383
virtual std::string get_tag() const =0
A parallel world class.
Definition world.h:134
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:2757
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 DFConvergence.h:9
void hash_range(hashT &seed, It first, It last)
Definition worldhash.h:281
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:248
static double pop(std::vector< double > &v)
Definition SCF.cc:118
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:146
void print(const T &t, Ts &&... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:232
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:132
int print_order
Definition chem/QCCalculationParametersBase.h:286
bool operator==(const QCParameter &other) const
Definition chem/QCCalculationParametersBase.h:242
std::string print_line(const std::string &key) const
Definition chem/QCCalculationParametersBase.h:176
QCParameter()
Definition chem/QCCalculationParametersBase.h:134
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:136
int get_print_order() const
Definition chem/QCCalculationParametersBase.h:174
std::string default_value
Definition chem/QCCalculationParametersBase.h:279
std::string not_allowed_errmsg() const
Definition chem/QCCalculationParametersBase.h:267
void serialize(Archive &ar)
Definition chem/QCCalculationParametersBase.h:235
std::string user_defined_value
Definition chem/QCCalculationParametersBase.h:281
void set_all()
Definition chem/QCCalculationParametersBase.h:254
bool check_allowed()
Definition chem/QCCalculationParametersBase.h:261
@ derived
Definition chem/QCCalculationParametersBase.h:240
@ defined
Definition chem/QCCalculationParametersBase.h:240
@ def
Definition chem/QCCalculationParametersBase.h:240
std::vector< std::string > allowed_values
Definition chem/QCCalculationParametersBase.h:285
std::string get_comment() const
Definition chem/QCCalculationParametersBase.h:162
void set_user_defined_value(const std::string val)
Definition chem/QCCalculationParametersBase.h:150
std::string type
Definition chem/QCCalculationParametersBase.h:282
std::string print_precedence() const
Definition chem/QCCalculationParametersBase.h:164
std::string value
Definition chem/QCCalculationParametersBase.h:278
std::string derived_value
Definition chem/QCCalculationParametersBase.h:280
std::string get_type() const
Definition chem/QCCalculationParametersBase.h:161
std::string comment
Definition chem/QCCalculationParametersBase.h:284
bool is_user_defined() const
Definition chem/QCCalculationParametersBase.h:156
std::string null
Definition chem/QCCalculationParametersBase.h:283
std::string get_value() const
Definition chem/QCCalculationParametersBase.h:160
void set_derived_value(const std::string val)
Definition chem/QCCalculationParametersBase.h:144
hashT hash() const
Definition chem/QCCalculationParametersBase.h:249
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.