8#ifndef SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
9#define SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
25#include "madness/external/nlohmann_json/json.hpp"
29 using json = nlohmann::json;
32 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
39 if (
sinf == str)
arg = std::numeric_limits<T>::infinity();
43 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
50 template <
typename T,
typename A=std::allocator<T>>
51 std::istream&
operator>>(std::istream& is, std::vector<T, A>&
v) {
53 std::string
word, line =
"";
56 if (
word.find(
']') != std::string::npos)
break;
58 if (line.size() != 0) is.clear();
61 auto find_c = [](
char&
c) {
return ((
c ==
',')
or (
c ==
'[')
or (
c ==
']')); };
62 std::replace_if(line.begin(), line.end(),
find_c,
' ');
65 std::stringstream
sline(line);
76 throw std::runtime_error(
"IO error");
85 template <
typename Q,
typename T>
86 std::istream&
operator>>(std::istream& is, std::pair<T, Q>&
p) {
88 std::string
word, line =
"";
96 auto find_c = [](
char&
c) {
return ((
c ==
',')
or (
c ==
'(')
or (
c ==
')')); };
97 std::replace_if(line.begin(), line.end(),
find_c,
' ');
100 std::stringstream
sline(line);
107 throw std::runtime_error(
"IO error");
162 throw std::runtime_error(
"unknown precedence in QCParameter" +
sprecedence.str());
169 auto fill_left = [](
const int size,
const std::string
word) {
170 int nspaces = std::max(
int(0), size -
int(
word.length()));
174 int nspaces = std::max(
int(0), size -
int(
word.length()));
186 using madness::operators::operator<<;
187 std::stringstream
ss;
222 std::size_t
last = result.find_last_not_of(
' ');
223 return result.substr(0,
last + 1);
226 template <
typename Archive>
260 using madness::operators::operator<<;
261 std::stringstream
ss;
263 std::string
errmsg =
"\ntrying to assign a value that's not allowed\n\n";
265 errmsg +=
"\tallowed values: " +
ss.str() +
"\n\n";
298 void print(
const std::string header =
"",
const std::string
footer =
"")
const;
304 template <
typename T>
305 T get(
const std::string key)
const {
308 if (std::is_same<T, std::string>::value) {
318 template <
typename Archive>
333 const std::string tag) {
340 std::string msg =
"could not find user-defined input file: " + parser.
value(
"input") +
"\n";
341 throw std::invalid_argument(msg);
344 catch (std::invalid_argument&
e) {
346 }
catch (std::exception&
e) {
381 template <
typename T>
382 void initialize(
const std::string& key,
const T& value,
const std::string comment =
"",
383 const std::vector<T> allowed_values = {}) {
386 throw std::runtime_error(
"initialization error");
390 std::string
type = std::type_index(
typeid(
T)).name();
397 for (
const T&
av : allowed_values) {
403 parameters.insert(std::make_pair<std::string, QCParameter>
408 template <
typename T>
412 throw std::runtime_error(
"type error in set_derived_value for key " + key);
423 template <
typename Tuple, std::size_t...
I>
426 (
void)std::initializer_list<int>{
433 template <
typename Tuple>
439 template <
typename T>
451 template <
typename...
Types>
454 (
void)std::initializer_list<int>{
463 for (
const auto& [key,
value1] : j.items()) {
464 const auto& value =
value1;
468 std::apply([&](
auto&&... args) {
472 throw std::runtime_error(
"Unsupported parameter type for key: " + key);
477 template <
typename...
Types>
479 const std::tuple<Types...>&)
const {
480 (
void)std::initializer_list<int>{
486 template <
typename Tuple>
488 std::apply([&](
auto&&... args) {
511 const std::string& precedence)
const {
514 if (
p.second.print_precedence() == precedence) {
521 template <
typename T>
525 throw std::runtime_error(
"type error in set_user_defined_value");
537 throw std::runtime_error(
"could not find parameter for key " + key);
547 throw std::runtime_error(
"could not find parameter for key " + key);
557 template <
typename T>
563 "trying to get the wrong type in QCCalculationParametersBase");
571 template <
typename T>
573 return (
parameter.get_type() == std::type_index(
typeid(
T)).name());
584 std::ostringstream
ss;
585 ss << std::boolalpha <<
arg;
589 template <
typename T>
591 using madness::operators::operator<<;
592 std::ostringstream
ss;
593 static_assert(
not std::is_same<T, bool>::value,
"you need to specialize tostring for this type");
595 ss << std::scientific << std::setprecision(4) <<
arg;
596 std::string str =
ss.str();
597 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
603 template <
typename T>
604 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
608 if (std::isinf(
arg)) {
609 std::stringstream
ss;
616 template <
typename T>
617 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
622 template <
typename T>
623 static typename std::enable_if<!std::is_same<T, bool>::value,
T>
::type
634 const static T inf = std::numeric_limits<T>::infinity();
642 std::string
errmsg =
"error in type conversion for argument >> " +
arg +
643 " << to type " + std::type_index(
typeid(
T)).name();
644 throw std::runtime_error(
errmsg);
650 std::string
errmsg =
"trailing characters in arguement >> " +
arg +
" <<";
651 throw std::runtime_error(
errmsg);
656 template <
typename T>
657 static typename std::enable_if<std::is_same<T, std::string>::value,
T>
::type
662 if (
arg.find(
"\"") == std::string::npos) {
671 std::string
errmsg =
"missing closing double quote in line >> " +
arg;
672 throw std::runtime_error(
errmsg);
675 counter = std::count(result.begin(), result.end(),
'"');
685 std::size_t
first =
arg.find_first_not_of(
' ');
686 std::size_t
last =
arg.find_last_not_of(
' ');
691 std::size_t
first =
arg.find_first_of(
'"');
692 std::size_t
last =
arg.find_last_of(
'"');
697 return "\"" +
arg +
"\"";
700 template <
typename T>
701 static typename std::enable_if<!std::is_same<T, std::string>::value,
T>
::type
708 template <
typename T>
709 static typename std::enable_if<std::is_same<T, bool>::value,
T>
::type
711 std::string str =
arg;
712 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
713 if (str ==
"true" or str ==
"1" or str ==
"yes")
715 if (str ==
"false" or str ==
"0" or str ==
"no")
717 std::string
errmsg =
"error in type conversion for argument >> " +
arg +
718 " << to type " + std::type_index(
typeid(
T)).name();
719 throw std::runtime_error(
errmsg);
727 std::vector<double>, std::vector<int>, std::vector<std::size_t>,
728 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:295
static bool check_type_silent(const QCParameter ¶meter)
Definition QCCalculationParametersBase.h:572
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:728
static std::enable_if< std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition QCCalculationParametersBase.h:658
void read_input_and_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
Definition QCCalculationParametersBase.h:331
ParameterContainerT get_all_parameters() const
Definition QCCalculationParametersBase.h:417
static std::string tostring(const T &arg)
Definition QCCalculationParametersBase.h:590
hashT hash() const
Definition QCCalculationParametersBase.h:323
friend bool operator==(const QCCalculationParametersBase &p1, const QCCalculationParametersBase &p2)
Definition QCCalculationParametersBase.cc:190
static std::enable_if< std::is_floating_point< T >::value, void >::type overwrite_if_inf(std::string &str, const T &arg)
Definition QCCalculationParametersBase.h:605
static std::enable_if<!std::is_floating_point< T >::value, void >::type overwrite_if_inf(std::string &str, const T &arg)
Definition QCCalculationParametersBase.h:618
void qcparameter_to_json(json &j, const std::pair< std::string, QCParameter > &p, const std::tuple< Types... > &) const
Definition QCCalculationParametersBase.h:478
bool throw_if_datagroup_not_found
Definition QCCalculationParametersBase.h:368
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:424
void set_user_defined_value(const std::string &key, const T &value)
Definition QCCalculationParametersBase.h:522
bool print_debug
Definition QCCalculationParametersBase.h:365
const QCParameter & get_parameter(const std::string &key) const
Definition QCCalculationParametersBase.h:535
json to_json() const
convert all parameters to a json object
Definition QCCalculationParametersBase.h:494
void to_json(json &j) const
convert all parameters to a json object
Definition QCCalculationParametersBase.h:501
void qcparameter_to_json_from_tuple(json &j, const std::pair< std::string, QCParameter > &p, const Tuple &) const
Definition QCCalculationParametersBase.h:487
virtual void read_internal(World &world, std::string &filecontents, std::string tag)
read the stream, starting from tag
Definition QCCalculationParametersBase.cc:122
void read_input(World &world, const std::string filename, const std::string tag)
read the parameters from file
Definition QCCalculationParametersBase.cc:74
std::string print_to_string(const std::list< std::string > precedences={"all"}) const
Definition QCCalculationParametersBase.cc:31
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:100
bool parameter_exists(const std::string &key) const
Definition QCCalculationParametersBase.h:553
T get(const std::string key) const
Definition QCCalculationParametersBase.h:305
bool try_set_from_json(QCParameter ¶meter, const nlohmann::json &value)
Definition QCCalculationParametersBase.h:452
void initialize(const std::string &key, const T &value, const std::string comment="", const std::vector< T > allowed_values={})
Definition QCCalculationParametersBase.h:382
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:462
bool ignore_unknown_keys
Definition QCCalculationParametersBase.h:366
bool file_exists(World &world, std::string filename) const
Definition QCCalculationParametersBase.cc:62
static std::string tostring(const bool &arg)
Definition QCCalculationParametersBase.h:583
QCCalculationParametersBase(const QCCalculationParametersBase &other)
copy ctor
Definition QCCalculationParametersBase.h:374
static std::enable_if<!std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition QCCalculationParametersBase.h:702
bool ignore_unknown_keys_silently
Definition QCCalculationParametersBase.h:367
QCParameter & get_parameter(const std::string key)
Definition QCCalculationParametersBase.h:544
bool is_user_defined(std::string key) const
Definition QCCalculationParametersBase.h:314
void serialize(Archive &ar)
Definition QCCalculationParametersBase.h:319
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:440
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:510
bool try_setting_any(const std::string &key, const std::string &value)
helper function for try_setting_user_defined_value
Definition QCCalculationParametersBase.h:434
static std::string trim_blanks(const std::string arg)
Definition QCCalculationParametersBase.h:684
ParameterContainerT parameters
Definition QCCalculationParametersBase.h:329
std::map< std::string, QCParameter > ParameterContainerT
Definition QCCalculationParametersBase.h:328
static bool check_type(const std::string key, const QCParameter ¶meter)
Definition QCCalculationParametersBase.h:558
static std::string add_quotes(const std::string arg)
Definition QCCalculationParametersBase.h:696
static std::enable_if< std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition QCCalculationParametersBase.h:710
static std::enable_if<!std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition QCCalculationParametersBase.h:624
virtual ~QCCalculationParametersBase()
destructor
Definition QCCalculationParametersBase.h:379
static std::string trim_quotes(const std::string arg)
Definition QCCalculationParametersBase.h:690
void set_derived_value(const std::string &key, const T &value)
Definition QCCalculationParametersBase.h:409
QCCalculationParametersBase()
ctor for testing
Definition QCCalculationParametersBase.h:371
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
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
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
bool operator!=(const ResponseParameters &p1, const ResponseParameters &p2)
Definition response_parameters.cpp:13
nlohmann::json json
Definition QCCalculationParametersBase.h:29
static std::enable_if< std::is_floating_point< T >::value, void >::type check_for_inf(const std::string &str, T &arg)
Definition QCCalculationParametersBase.h:33
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
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 QCCalculationParametersBase.h:124
int print_order
Definition QCCalculationParametersBase.h:278
bool operator==(const QCParameter &other) const
Definition QCCalculationParametersBase.h:234
std::vector< std::string > allowed_values
Definition QCCalculationParametersBase.h:277
std::string print_line(const std::string &key) const
Definition QCCalculationParametersBase.h:168
QCParameter()
Definition QCCalculationParametersBase.h:126
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:128
int get_print_order() const
Definition QCCalculationParametersBase.h:166
std::string default_value
Definition QCCalculationParametersBase.h:271
std::string not_allowed_errmsg() const
Definition QCCalculationParametersBase.h:259
void serialize(Archive &ar)
Definition QCCalculationParametersBase.h:227
std::string user_defined_value
Definition QCCalculationParametersBase.h:273
@ derived
Definition QCCalculationParametersBase.h:232
@ defined
Definition QCCalculationParametersBase.h:232
@ def
Definition QCCalculationParametersBase.h:232
void set_all()
Definition QCCalculationParametersBase.h:246
bool check_allowed()
Definition QCCalculationParametersBase.h:253
std::string get_comment() const
Definition QCCalculationParametersBase.h:154
void set_user_defined_value(const std::string val)
Definition QCCalculationParametersBase.h:142
std::string type
Definition QCCalculationParametersBase.h:274
std::string print_precedence() const
Definition QCCalculationParametersBase.h:156
std::string value
Definition QCCalculationParametersBase.h:270
std::string derived_value
Definition QCCalculationParametersBase.h:272
std::string get_type() const
Definition QCCalculationParametersBase.h:153
std::string comment
Definition QCCalculationParametersBase.h:276
bool is_user_defined() const
Definition QCCalculationParametersBase.h:148
std::string null
Definition QCCalculationParametersBase.h:275
std::string get_value() const
Definition QCCalculationParametersBase.h:152
void set_derived_value(const std::string val)
Definition QCCalculationParametersBase.h:136
hashT hash() const
Definition QCCalculationParametersBase.h:241
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.