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, ' ');
66 std::stringstream sline(line);
68 while (sline >> word) {
69 std::stringstream sword(word);
77 throw std::runtime_error(
"IO error");
86 template <
typename Q,
typename T>
87 std::istream&
operator>>(std::istream& is, std::pair<T, Q>&
p) {
89 std::string word, line =
"";
97 auto find_c = [](
char&
c) {
return ((
c ==
',') or (
c ==
'(') or (
c == ')')); };
98 std::replace_if(line.begin(), line.end(), find_c, ' ');
101 std::stringstream sline(line);
104 sline >> tmp1 >> tmp2;
105 if (sline.bad() or sline.fail()) {
108 throw std::runtime_error(
"IO error");
110 p = std::pair<T, Q>(tmp1, tmp2);
130 const std::vector<std::string> allowed_values1 = {})
161 std::stringstream sprecedence;
163 throw std::runtime_error(
"unknown precedence in QCParameter" + sprecedence.str());
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;
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,
' ');
180 std::string keyval = fill_left(20, key) +
" " + fill_right(10,
get_value()) +
" # " + fill_right(
182 std::string empty_keyval(keyval.size(),
' ');
183 empty_keyval[33] =
'#';
185 std::string allowed_val;
187 using madness::operators::operator<<;
188 std::stringstream ss;
190 allowed_val += ss.str();
194 bool leave_space_for_allowed_values = (allowed_val.size() > 0);
197 long keyvalsize = keyval.size();
200 std::vector<std::string> commentlines(1);
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;
208 nchar += word.size() + 1;
209 if (nchar > commentsize) {
210 commentlines.push_back(
"");
211 nchar = word.size() + 1;
213 commentlines.back() += word +
" ";
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];
223 std::size_t last = result.find_last_not_of(
' ');
224 return result.substr(0, last + 1);
227 template <
typename Archive>
238 type == other.type and
null == other.null and
comment == other.comment and
261 using madness::operators::operator<<;
262 std::stringstream ss;
264 std::string
errmsg =
"\ntrying to assign a value that's not allowed\n\n";
266 errmsg +=
"\tallowed values: " + ss.str() +
"\n\n";
275 std::string
type =
"";
276 std::string
null =
"";
278 std::vector<std::string>
allowed_values = std::vector<std::string>();
296 class QCCalculationParametersBase {
299 void print(
const std::string header =
"",
const std::string footer =
"")
const;
305 template <
typename T>
306 T
get(
const std::string key)
const {
309 if (std::is_same<T, std::string>::value) {
310 return fromstring<T>(
add_quotes(parameter.get_value()));
312 return fromstring<T>(parameter.get_value());
319 template <
typename Archive>
334 const std::string tag) {
341 std::string msg =
"could not find user-defined input file: " + parser.
value(
"input") +
"\n";
342 throw std::invalid_argument(msg);
345 catch (std::invalid_argument&
e) {
347 }
catch (std::exception&
e) {
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 = {}) {
391 throw std::runtime_error(
"initialization error");
394 std::string svalue =
tostring(value);
395 std::string
type = std::type_index(
typeid(T)).name();
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);
408 parameters.insert(std::make_pair<std::string, QCParameter>
409 (std::string(key_lower), QCParameter(svalue,
type, comment, av_lower_vec)));
425 return keys.find(key) != keys.end();
428 template <
typename T>
431 if (not check_type_silent<T>(parameter)) {
432 throw std::runtime_error(
"type error in set_derived_value for key " + key);
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)...
453 template <
typename Tuple>
455 return try_setting_any_impl(key, value, Tuple{}, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
459 template <
typename T>
461 if (not check_type_silent<T>(
get_parameter(key)))
return false;
464 T value = fromstring<T>(val);
465 set_user_defined_value<T>(key, value);
471 template <
typename... Types>
473 bool success =
false;
474 (void)std::initializer_list<int>{
475 (parameter.get_type() == std::type_index(
typeid(Types)).name()
483 for (
const auto& [key, value1] : j.items()) {
484 const auto& value = value1;
488 std::apply([&](
auto&&... args) {
492 throw std::runtime_error(
"Unsupported parameter type for key: " + key);
497 template <
typename... Types>
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)...
506 template <
typename Tuple>
508 std::apply([&](
auto&&... args) {
526 j[
"parameters"] = j_params;
531 const std::string& precedence)
const {
534 if (
p.second.print_precedence() == precedence) {
541 template <
typename T>
544 if (not check_type_silent<T>(parameter)) {
545 throw std::runtime_error(
"type error in set_user_defined_value");
552 template <
typename T>
554 if constexpr (std::is_same<T, std::string>::value) {
566 throw std::runtime_error(
"could not find parameter for key " + key);
576 throw std::runtime_error(
"could not find parameter for key " + key);
586 template <
typename T>
588 if (check_type_silent<T>(parameter))
592 "trying to get the wrong type in QCCalculationParametersBase");
600 template <
typename T>
602 return (parameter.get_type() == std::type_index(
typeid(T)).name());
613 std::ostringstream ss;
614 ss << std::boolalpha <<
arg;
618 template <
typename T>
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");
624 ss << std::scientific << std::setprecision(4) <<
arg;
625 std::string str = ss.str();
626 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
632 template <
typename T>
633 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
637 if (std::isinf(
arg)) {
638 std::stringstream ss;
639 ss << std::numeric_limits<T>::infinity();
645 template <
typename T>
646 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
651 template <
typename T>
652 static typename std::enable_if<!std::is_same<T, bool>::value, T>
::type
654 std::stringstream ssvalue(
arg);
657 T result = read_quotes<T>(ssvalue);
659 bool type_conversion_failed = ssvalue.fail();
662 if (type_conversion_failed and (std::is_floating_point<T>::value)) {
663 const static T inf = std::numeric_limits<T>::infinity();
667 type_conversion_failed =
false;
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);
678 while (ssvalue >> word) {
679 std::string
errmsg =
"trailing characters in arguement >> " +
arg +
" <<";
680 throw std::runtime_error(
errmsg);
685 template <
typename T>
686 static typename std::enable_if<std::is_same<T, std::string>::value, T>
::type
688 T
arg = ssvalue.str();
691 if (
arg.find(
"\"") == std::string::npos) {
696 while (counter < 2) {
699 if (ssvalue.fail()) {
700 std::string
errmsg =
"missing closing double quote in line >> " +
arg;
701 throw std::runtime_error(
errmsg);
704 counter = std::count(result.begin(), result.end(),
'"');
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);
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);
726 return "\"" +
arg +
"\"";
729 template <
typename T>
730 static typename std::enable_if<!std::is_same<T, std::string>::value, T>
::type
737 template <
typename T>
738 static typename std::enable_if<std::is_same<T, bool>::value, T>
::type
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")
744 if (str ==
"false" or str ==
"0" or str ==
"no")
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);
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>>;
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 ¶meter)
Definition mra/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 mra/QCCalculationParametersBase.h:687
void read_input_and_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
Definition mra/QCCalculationParametersBase.h:332
ParameterContainerT get_all_parameters() const
Definition mra/QCCalculationParametersBase.h:437
static std::string tostring(const T &arg)
Definition mra/QCCalculationParametersBase.h:619
bool keeps_case(const std::string &key) const
Definition mra/QCCalculationParametersBase.h:423
hashT hash() const
Definition mra/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 mra/QCCalculationParametersBase.h:647
void qcparameter_to_json(json &j, const std::pair< std::string, QCParameter > &p, const std::tuple< Types... > &) const
Definition mra/QCCalculationParametersBase.h:498
bool throw_if_datagroup_not_found
Definition chem/QCCalculationParametersBase.h:373
virtual void read_internal(World &world, std::string &filecontents, std::string tag)
read the stream, starting from tag
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 mra/QCCalculationParametersBase.h:444
void set_user_defined_value(const std::string &key, const T &value)
Definition mra/QCCalculationParametersBase.h:542
virtual std::set< std::string > case_sensitive_keys() const
keys whose string values must NOT be case-folded
Definition mra/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 mra/QCCalculationParametersBase.h:514
void to_json(json &j) const
convert all parameters to a json object
Definition mra/QCCalculationParametersBase.h:521
void qcparameter_to_json_from_tuple(json &j, const std::pair< std::string, QCParameter > &p, const Tuple &) const
Definition mra/QCCalculationParametersBase.h:507
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
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 mra/QCCalculationParametersBase.h:306
bool try_set_from_json(QCParameter ¶meter, const nlohmann::json &value)
Definition mra/QCCalculationParametersBase.h:472
void initialize(const std::string &key, const T &value, const std::string comment="", const std::vector< T > allowed_values={})
Definition mra/QCCalculationParametersBase.h:387
void print(const std::string header="", const std::string footer="") const
print all parameters
void from_json(const json &j)
Definition mra/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 mra/QCCalculationParametersBase.h:379
static std::enable_if<!std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition mra/QCCalculationParametersBase.h:731
bool ignore_unknown_keys_silently
Definition chem/QCCalculationParametersBase.h:372
QCParameter & get_parameter(const std::string key)
Definition mra/QCCalculationParametersBase.h:573
bool is_user_defined(std::string key) const
Definition mra/QCCalculationParametersBase.h:315
void serialize(Archive &ar)
Definition mra/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 mra/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 mra/QCCalculationParametersBase.h:530
bool try_setting_any(const std::string &key, const std::string &value)
helper function for try_setting_user_defined_value
Definition mra/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 mra/QCCalculationParametersBase.h:329
static bool check_type(const std::string key, const QCParameter ¶meter)
Definition mra/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 mra/QCCalculationParametersBase.h:739
static std::enable_if<!std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition mra/QCCalculationParametersBase.h:653
virtual ~QCCalculationParametersBase()
destructor
Definition mra/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 mra/QCCalculationParametersBase.h:429
QCCalculationParametersBase()
ctor for testing
Definition mra/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 mra/QCCalculationParametersBase.h:235
std::string print_line(const std::string &key) const
Definition mra/QCCalculationParametersBase.h:169
QCParameter()
Definition mra/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 mra/QCCalculationParametersBase.h:129
int get_print_order() const
Definition mra/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 mra/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 mra/QCCalculationParametersBase.h:155
void set_user_defined_value(const std::string val)
Definition mra/QCCalculationParametersBase.h:143
std::string type
Definition chem/QCCalculationParametersBase.h:275
std::string print_precedence() const
Definition mra/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 mra/QCCalculationParametersBase.h:154
std::string comment
Definition chem/QCCalculationParametersBase.h:277
bool is_user_defined() const
Definition mra/QCCalculationParametersBase.h:149
std::string null
Definition chem/QCCalculationParametersBase.h:276
std::string get_value() const
Definition mra/QCCalculationParametersBase.h:153
void set_derived_value(const std::string val)
Definition mra/QCCalculationParametersBase.h:137
hashT hash() const
Definition mra/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.