8#ifndef SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
9#define SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
24#include "madness/external/nlohmann_json/json.hpp"
28 using json = nlohmann::json;
31 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
35 ss << std::numeric_limits<T>::infinity();
38 if (sinf == str)
arg = std::numeric_limits<T>::infinity();
42 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
49 template <
typename T,
typename A=std::allocator<T>>
50 std::istream&
operator>>(std::istream& is, std::vector<T, A>&
v) {
52 std::string word, line =
"";
55 if (word.find(
']') != std::string::npos)
break;
57 if (line.size() != 0) is.clear();
60 auto find_c = [](
char&
c) {
return ((
c ==
',') or (
c ==
'[') or (
c ==
']')); };
61 std::replace_if(line.begin(), line.end(), find_c,
' ');
64 std::stringstream sline(line);
66 while (sline >> word) {
67 std::stringstream sword(word);
75 throw std::runtime_error(
"IO error");
84 template <
typename Q,
typename T>
85 std::istream&
operator>>(std::istream& is, std::pair<T, Q>&
p) {
87 std::string word, line =
"";
95 auto find_c = [](
char&
c) {
return ((
c ==
',') or (
c ==
'(') or (
c ==
')')); };
96 std::replace_if(line.begin(), line.end(), find_c,
' ');
99 std::stringstream sline(line);
102 sline >> tmp1 >> tmp2;
103 if (sline.bad() or sline.fail()) {
106 throw std::runtime_error(
"IO error");
108 p = std::pair<T, Q>(tmp1, tmp2);
128 const std::vector<std::string> allowed_values1 = {})
159 std::stringstream sprecedence;
161 throw std::runtime_error(
"unknown precedence in QCParameter" + sprecedence.str());
168 auto fill_left = [](
const int size,
const std::string word) {
169 int nspaces = std::max(
int(0), size -
int(word.length()));
170 return std::string(nspaces,
' ') + word;
172 auto fill_right = [](
const int size,
const std::string word) {
173 int nspaces = std::max(
int(0), size -
int(word.length()));
174 return word + std::string(nspaces,
' ');
178 std::string keyval = fill_left(20, key) +
" " + fill_right(10,
get_value()) +
" # " + fill_right(
180 std::string empty_keyval(keyval.size(),
' ');
181 empty_keyval[33] =
'#';
183 std::string allowed_val;
185 using madness::operators::operator<<;
186 std::stringstream ss;
188 allowed_val += ss.str();
192 bool leave_space_for_allowed_values = (allowed_val.size() > 0);
195 long keyvalsize = keyval.size();
198 std::vector<std::string> commentlines(1);
200 for (
auto word : commentwords) {
201 bool is_first_line = commentlines.size() == 1;
202 long thislinebreak = 120;
203 if (is_first_line and leave_space_for_allowed_values) thislinebreak = 80;
204 long commentsize = thislinebreak - keyvalsize;
206 nchar += word.size() + 1;
207 if (nchar > commentsize) {
208 commentlines.push_back(
"");
209 nchar = word.size() + 1;
211 commentlines.back() += word +
" ";
215 for (
size_t i = 0; i < commentlines.size(); ++i) {
216 if (i == 0) result = keyval + fill_right(40, commentlines[i]) + allowed_val;
217 else result +=
"\n" + empty_keyval + commentlines[i];
221 std::size_t last = result.find_last_not_of(
' ');
222 return result.substr(0, last + 1);
225 template <
typename Archive>
259 using madness::operators::operator<<;
260 std::stringstream ss;
262 std::string
errmsg =
"\ntrying to assign a value that's not allowed\n\n";
264 errmsg +=
"\tallowed values: " + ss.str() +
"\n\n";
297 void print(
const std::string header =
"",
const std::string footer =
"")
const;
301 template <
typename T>
302 T get(
const std::string key)
const {
305 if (std::is_same<T, std::string>::value) {
308 return fromstring<T>(parameter.
get_value());
315 template <
typename Archive>
330 const std::string tag) {
337 std::string msg =
"could not find user-defined input file: " + parser.
value(
"input") +
"\n";
338 throw std::invalid_argument(msg);
341 catch (std::invalid_argument&
e) {
343 }
catch (std::exception&
e) {
378 template <
typename T>
379 void initialize(
const std::string& key,
const T& value,
const std::string comment =
"",
380 const std::vector<T> allowed_values = {}) {
383 throw std::runtime_error(
"initialization error");
386 std::string svalue =
tostring(value);
387 std::string
type = std::type_index(
typeid(
T)).name();
390 std::string key_lower = key;
391 std::transform(key_lower.begin(), key_lower.end(), key_lower.begin(), ::tolower);
392 std::transform(svalue.begin(), svalue.end(), svalue.begin(), ::tolower);
393 std::vector<std::string> av_lower_vec;
394 for (
const T& av : allowed_values) {
395 std::string av_lower =
tostring(av);
396 std::transform(av_lower.begin(), av_lower.end(), av_lower.begin(), ::tolower);
397 av_lower_vec.push_back(av_lower);
400 parameters.insert(std::make_pair<std::string, QCParameter>
401 (std::string(key_lower), QCParameter(svalue,
type, comment, av_lower_vec)));
405 template <
typename T>
408 if (not check_type_silent<T>(parameter)) {
409 throw std::runtime_error(
"type error in set_derived_value for key " + key);
420 template <
typename Tuple, std::size_t...
I>
421 bool try_setting_any_impl(
const std::string& key,
const std::string& value, Tuple, std::index_sequence<I...>) {
422 bool success =
false;
423 (void)std::initializer_list<int>{
424 (success = try_setting_user_defined_value<std::tuple_element_t<I, Tuple>>(key, value) || success, 0)...
430 template <
typename Tuple>
432 return try_setting_any_impl(key, value, Tuple{}, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
436 template <
typename T>
438 if (not check_type_silent<T>(
get_parameter(key)))
return false;
441 T value = fromstring<T>(val);
442 set_user_defined_value<T>(key, value);
448 template <
typename... Types>
450 bool success =
false;
451 (void)std::initializer_list<int>{
452 (parameter.
get_type() == std::type_index(
typeid(Types)).name()
460 for (
const auto& [key, value1] : j.items()) {
461 const auto& value = value1;
465 std::apply([&](
auto&&... args) {
469 throw std::runtime_error(
"Unsupported parameter type for key: " + key);
474 template <
typename... Types>
476 const std::tuple<Types...>&)
const {
477 (void)std::initializer_list<int>{
478 (check_type_silent<Types>(
p.second) ? (j[
p.first] = get<Types>(
p.first), 0) : 0)...
483 template <
typename Tuple>
485 std::apply([&](
auto&&... args) {
503 j[
"parameters"] = j_params;
508 const std::string& precedence)
const {
511 if (
p.second.print_precedence() == precedence) {
523 template <
typename T>
526 if (not check_type_silent<T>(parameter)) {
527 throw std::runtime_error(
"type error in set_user_defined_value");
536 throw std::runtime_error(
"could not find parameter for key " + key);
546 throw std::runtime_error(
"could not find parameter for key " + key);
556 template <
typename T>
558 if (check_type_silent<T>(parameter))
562 "trying to get the wrong type in QCCalculationParametersBase");
570 template <
typename T>
572 return (parameter.
get_type() == std::type_index(
typeid(
T)).name());
583 std::ostringstream ss;
584 ss << std::boolalpha <<
arg;
588 template <
typename T>
590 using madness::operators::operator<<;
591 std::ostringstream ss;
592 static_assert(not std::is_same<T, bool>::value,
"you need to specialize tostring for this type");
594 ss << std::scientific << std::setprecision(4) <<
arg;
595 std::string str = ss.str();
596 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
602 template <
typename T>
603 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
605 if (std::isinf(
arg)) {
606 std::stringstream ss;
607 ss << std::numeric_limits<T>::infinity();
612 template <
typename T>
613 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
618 template <
typename T>
619 static typename std::enable_if<!std::is_same<T, bool>::value,
T>
::type
621 std::stringstream ssvalue(
arg);
624 T result = read_quotes<T>(ssvalue);
626 bool type_conversion_failed = ssvalue.fail();
629 if (type_conversion_failed and (std::is_floating_point<T>::value)) {
630 const static T inf = std::numeric_limits<T>::infinity();
634 type_conversion_failed =
false;
637 if (type_conversion_failed) {
638 std::string
errmsg =
"error in type conversion for argument >> " +
arg +
639 " << to type " + std::type_index(
typeid(
T)).name();
640 throw std::runtime_error(
errmsg);
645 while (ssvalue >> word) {
646 std::string
errmsg =
"trailing characters in arguement >> " +
arg +
" <<";
647 throw std::runtime_error(
errmsg);
652 template <
typename T>
653 static typename std::enable_if<std::is_same<T, std::string>::value,
T>
::type
655 T arg = ssvalue.str();
658 if (
arg.find(
"\"") == std::string::npos) {
663 while (counter < 2) {
666 if (ssvalue.fail()) {
667 std::string
errmsg =
"missing closing double quote in line >> " +
arg;
668 throw std::runtime_error(
errmsg);
671 counter = std::count(result.begin(), result.end(),
'"');
681 std::size_t first =
arg.find_first_not_of(
' ');
682 std::size_t last =
arg.find_last_not_of(
' ');
683 return arg.substr(first, last - first + 1);
687 std::size_t first =
arg.find_first_of(
'"');
688 std::size_t last =
arg.find_last_of(
'"');
689 return arg.substr(first + 1, last - first - 1);
693 return "\"" +
arg +
"\"";
696 template <
typename T>
697 static typename std::enable_if<!std::is_same<T, std::string>::value,
T>
::type
704 template <
typename T>
705 static typename std::enable_if<std::is_same<T, bool>::value,
T>
::type
707 std::string str =
arg;
708 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
709 if (str ==
"true" or str ==
"1" or str ==
"yes")
711 if (str ==
"false" or str ==
"0" or str ==
"no")
713 std::string
errmsg =
"error in type conversion for argument >> " +
arg +
714 " << to type " + std::type_index(
typeid(
T)).name();
715 throw std::runtime_error(
errmsg);
722 double, int,
unsigned int, long, std::size_t, bool, std::string,
723 std::vector<double>, std::vector<int>, std::vector<std::size_t>,
724 std::vector<std::string>, std::pair<std::string, double>>;
Interface templates for the archives (serialization).
class for holding the parameters for calculation
Definition QCCalculationParametersBase.h:294
static bool check_type_silent(const QCParameter ¶meter)
Definition QCCalculationParametersBase.h:571
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 QCCalculationParametersBase.h:724
static std::enable_if< std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition QCCalculationParametersBase.h:654
virtual void read_input_and_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
Definition QCCalculationParametersBase.h:328
ParameterContainerT get_all_parameters() const
Definition QCCalculationParametersBase.h:414
static std::string tostring(const T &arg)
Definition QCCalculationParametersBase.h:589
hashT hash() const
Definition QCCalculationParametersBase.h:320
static std::enable_if< std::is_floating_point< T >::value, void >::type overwrite_if_inf(std::string &str, const T &arg)
Definition QCCalculationParametersBase.h:604
static std::enable_if<!std::is_floating_point< T >::value, void >::type overwrite_if_inf(std::string &str, const T &arg)
Definition QCCalculationParametersBase.h:614
void qcparameter_to_json(json &j, const std::pair< std::string, QCParameter > &p, const std::tuple< Types... > &) const
Definition QCCalculationParametersBase.h:475
bool throw_if_datagroup_not_found
Definition QCCalculationParametersBase.h:365
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 QCCalculationParametersBase.h:421
void set_user_defined_value(const std::string &key, const T &value)
Definition QCCalculationParametersBase.h:524
bool print_debug
Definition QCCalculationParametersBase.h:362
const QCParameter & get_parameter(const std::string &key) const
Definition QCCalculationParametersBase.h:534
json to_json() const
convert all parameters to a json object
Definition QCCalculationParametersBase.h:491
void to_json(json &j) const
convert all parameters to a json object
Definition QCCalculationParametersBase.h:498
void qcparameter_to_json_from_tuple(json &j, const std::pair< std::string, QCParameter > &p, const Tuple &) const
Definition QCCalculationParametersBase.h:484
virtual void read_internal(World &world, std::string &filecontents, std::string tag)
read the stream, starting from tag
Definition QCCalculationParametersBase.cc:111
void read_input(World &world, const std::string filename, const std::string tag)
read the parameters from file
Definition QCCalculationParametersBase.cc:63
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:89
bool parameter_exists(const std::string &key) const
Definition QCCalculationParametersBase.h:552
T get(const std::string key) const
Definition QCCalculationParametersBase.h:302
bool try_set_from_json(QCParameter ¶meter, const nlohmann::json &value)
Definition QCCalculationParametersBase.h:449
void initialize(const std::string &key, const T &value, const std::string comment="", const std::vector< T > allowed_values={})
Definition QCCalculationParametersBase.h:379
void print(const std::string header="", const std::string footer="") const
print all parameters
Definition QCCalculationParametersBase.cc:22
void from_json(const json &j)
Definition QCCalculationParametersBase.h:459
bool ignore_unknown_keys
Definition QCCalculationParametersBase.h:363
bool file_exists(World &world, std::string filename) const
Definition QCCalculationParametersBase.cc:51
static std::string tostring(const bool &arg)
Definition QCCalculationParametersBase.h:582
QCCalculationParametersBase(const QCCalculationParametersBase &other)
copy ctor
Definition QCCalculationParametersBase.h:371
static std::enable_if<!std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition QCCalculationParametersBase.h:698
bool ignore_unknown_keys_silently
Definition QCCalculationParametersBase.h:364
QCParameter & get_parameter(const std::string key)
Definition QCCalculationParametersBase.h:543
bool is_user_defined(std::string key) const
Definition QCCalculationParametersBase.h:311
void serialize(Archive &ar)
Definition QCCalculationParametersBase.h:316
bool try_setting_user_defined_value(const std::string &key, const std::string &val)
helper function for try_setting_user_defined_value
Definition QCCalculationParametersBase.h:437
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 QCCalculationParametersBase.h:507
bool try_setting_any(const std::string &key, const std::string &value)
helper function for try_setting_user_defined_value
Definition QCCalculationParametersBase.h:431
static std::string trim_blanks(const std::string arg)
Definition QCCalculationParametersBase.h:680
ParameterContainerT parameters
Definition QCCalculationParametersBase.h:326
bool operator==(const QCCalculationParametersBase &other) const
Definition QCCalculationParametersBase.h:519
std::map< std::string, QCParameter > ParameterContainerT
Definition QCCalculationParametersBase.h:325
static bool check_type(const std::string key, const QCParameter ¶meter)
Definition QCCalculationParametersBase.h:557
static std::string add_quotes(const std::string arg)
Definition QCCalculationParametersBase.h:692
static std::enable_if< std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition QCCalculationParametersBase.h:706
static std::enable_if<!std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition QCCalculationParametersBase.h:620
virtual ~QCCalculationParametersBase()
destructor
Definition QCCalculationParametersBase.h:376
static std::string trim_quotes(const std::string arg)
Definition QCCalculationParametersBase.h:686
std::string print_to_string(bool non_defaults_only=false) const
Definition QCCalculationParametersBase.cc:31
void set_derived_value(const std::string &key, const T &value)
Definition QCCalculationParametersBase.h:406
QCCalculationParametersBase()
ctor for testing
Definition QCCalculationParametersBase.h:368
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
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:28
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:2503
static const double v
Definition hatom_sf_dirac.cc:20
#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
bool operator!=(const ResponseParameters &p1, const ResponseParameters &p2)
Definition response_parameters.cpp:13
nlohmann::json json
Definition QCCalculationParametersBase.h:28
static std::enable_if< std::is_floating_point< T >::value, void >::type check_for_inf(const std::string &str, T &arg)
Definition QCCalculationParametersBase.h:32
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:225
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 QCCalculationParametersBase.h:123
int print_order
Definition QCCalculationParametersBase.h:277
bool operator==(const QCParameter &other) const
Definition QCCalculationParametersBase.h:233
std::vector< std::string > allowed_values
Definition QCCalculationParametersBase.h:276
std::string print_line(const std::string &key) const
Definition QCCalculationParametersBase.h:167
QCParameter()
Definition QCCalculationParametersBase.h:125
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 QCCalculationParametersBase.h:127
int get_print_order() const
Definition QCCalculationParametersBase.h:165
std::string default_value
Definition QCCalculationParametersBase.h:270
std::string not_allowed_errmsg() const
Definition QCCalculationParametersBase.h:258
void serialize(Archive &ar)
Definition QCCalculationParametersBase.h:226
std::string user_defined_value
Definition QCCalculationParametersBase.h:272
@ derived
Definition QCCalculationParametersBase.h:231
@ defined
Definition QCCalculationParametersBase.h:231
@ def
Definition QCCalculationParametersBase.h:231
void set_all()
Definition QCCalculationParametersBase.h:245
bool check_allowed()
Definition QCCalculationParametersBase.h:252
std::string get_comment() const
Definition QCCalculationParametersBase.h:153
void set_user_defined_value(const std::string val)
Definition QCCalculationParametersBase.h:141
std::string type
Definition QCCalculationParametersBase.h:273
std::string print_precedence() const
Definition QCCalculationParametersBase.h:155
std::string value
Definition QCCalculationParametersBase.h:269
std::string derived_value
Definition QCCalculationParametersBase.h:271
std::string get_type() const
Definition QCCalculationParametersBase.h:152
std::string comment
Definition QCCalculationParametersBase.h:275
bool is_user_defined() const
Definition QCCalculationParametersBase.h:147
std::string null
Definition QCCalculationParametersBase.h:274
std::string get_value() const
Definition QCCalculationParametersBase.h:151
void set_derived_value(const std::string val)
Definition QCCalculationParametersBase.h:135
hashT hash() const
Definition QCCalculationParametersBase.h:240
very simple command line parser
Definition commandlineparser.h:15
std::string value(const std::string key) const
Definition commandlineparser.h:62
bool key_exists(std::string key) const
Definition commandlineparser.h:58
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:94
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.