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
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 == ']')); };
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 == ')')); };
108 throw std::runtime_error(
"IO error");
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()));
175 int nspaces = std::max(
int(0), size -
int(
word.length()));
187 using madness::operators::operator<<;
188 std::stringstream
ss;
223 std::size_t
last = result.find_last_not_of(
' ');
224 return result.substr(0,
last + 1);
227 template <
typename Archive>
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) {
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");
395 std::string
type = std::type_index(
typeid(T)).name();
402 for (
const T&
av : allowed_values) {
408 parameters.insert(std::make_pair<std::string, QCParameter>
425 return keys.find(key) !=
keys.end();
428 template <
typename T>
432 throw std::runtime_error(
"type error in set_derived_value for key " + key);
443 template <
typename Tuple, std::size_t...
I>
446 (
void)std::initializer_list<int>{
453 template <
typename Tuple>
459 template <
typename T>
471 template <
typename...
Types>
474 (
void)std::initializer_list<int>{
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>{
506 template <
typename Tuple>
508 std::apply([&](
auto&&... args) {
531 const std::string& precedence)
const {
534 if (
p.second.print_precedence() == precedence) {
541 template <
typename T>
545 throw std::runtime_error(
"type error in set_user_defined_value");
555 }
catch (
const std::invalid_argument&
e) {
556 throw std::invalid_argument(
"in keyword `" + key +
"`: " +
e.what());
561 template <
typename T>
563 if constexpr (std::is_same<T, std::string>::value) {
575 throw std::runtime_error(
"could not find parameter for key " + key);
585 throw std::runtime_error(
"could not find parameter for key " + key);
595 template <
typename T>
601 "trying to get the wrong type in QCCalculationParametersBase");
609 template <
typename T>
611 return (
parameter.get_type() == std::type_index(
typeid(T)).name());
622 std::ostringstream
ss;
623 ss << std::boolalpha <<
arg;
627 template <
typename T>
629 using madness::operators::operator<<;
630 std::ostringstream
ss;
631 static_assert(
not std::is_same<T, bool>::value,
"you need to specialize tostring for this type");
633 ss << std::scientific << std::setprecision(4) <<
arg;
634 std::string str =
ss.str();
635 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
641 template <
typename T>
642 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
646 if (std::isinf(
arg)) {
647 std::stringstream
ss;
654 template <
typename T>
655 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
660 template <
typename T>
661 static typename std::enable_if<!std::is_same<T, bool>::value, T>
::type
672 const static T
inf = std::numeric_limits<T>::infinity();
680 std::string
errmsg =
"error in type conversion for argument >> " +
arg +
681 " << to type " + std::type_index(
typeid(T)).name();
682 throw std::runtime_error(
errmsg);
688 std::string
errmsg =
"trailing characters in arguement >> " +
arg +
" <<";
689 throw std::runtime_error(
errmsg);
694 template <
typename T>
695 static typename std::enable_if<std::is_same<T, std::string>::value, T>
::type
700 if (
arg.find(
"\"") == std::string::npos) {
709 std::string
errmsg =
"missing closing double quote in line >> " +
arg;
710 throw std::runtime_error(
errmsg);
713 counter = std::count(result.begin(), result.end(),
'"');
723 std::size_t
first =
arg.find_first_not_of(
' ');
724 std::size_t
last =
arg.find_last_not_of(
' ');
729 std::size_t
first =
arg.find_first_of(
'"');
730 std::size_t
last =
arg.find_last_of(
'"');
735 return "\"" +
arg +
"\"";
738 template <
typename T>
739 static typename std::enable_if<!std::is_same<T, std::string>::value, T>
::type
746 template <
typename T>
747 static typename std::enable_if<std::is_same<T, bool>::value, T>
::type
749 std::string str =
arg;
750 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
751 if (str ==
"true" or str ==
"1" or str ==
"yes")
753 if (str ==
"false" or str ==
"0" or str ==
"no")
755 std::string
errmsg =
"error in type conversion for argument >> " +
arg +
756 " << to type " + std::type_index(
typeid(T)).name();
757 throw std::runtime_error(
errmsg);
765 std::vector<double>, std::vector<int>, std::vector<std::size_t>,
766 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:610
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:766
static std::enable_if< std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition mra/QCCalculationParametersBase.h:696
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:628
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:562
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:643
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:656
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:573
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:591
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:621
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:740
bool ignore_unknown_keys_silently
Definition chem/QCCalculationParametersBase.h:372
QCParameter & get_parameter(const std::string key)
Definition mra/QCCalculationParametersBase.h:582
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:722
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:596
static std::string add_quotes(const std::string arg)
Definition chem/QCCalculationParametersBase.h:734
static std::enable_if< std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition mra/QCCalculationParametersBase.h:748
static std::enable_if<!std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition mra/QCCalculationParametersBase.h:662
virtual ~QCCalculationParametersBase()
destructor
Definition mra/QCCalculationParametersBase.h:384
static std::string trim_quotes(const std::string arg)
Definition chem/QCCalculationParametersBase.h:728
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: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: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:116
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:227
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
static XNonlinearSolver< std::vector< Function< T, NDIM > >, T, vector_function_allocator< T, NDIM > > nonlinear_vector_solver(World &world, const long nvec)
Definition nonlinsol.h:371
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.