8#ifndef SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
9#define SRC_APPS_CHEM_QCCALCULATIONPARAMETERSBASE_H_
22#include "madness/external/nlohmann_json/json.hpp"
27 using json =nlohmann::json;
30static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
34 ss << std::numeric_limits<T>::infinity();
37 if (sinf==str)
arg=std::numeric_limits<T>::infinity();
41static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
48template <
typename T,
typename A=std::allocator<T> >
49std::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");
86template <
typename Q,
typename T>
87std::istream&
operator>>(std::istream& is, std::pair<T,Q>&
p) {
90 std::string word, line=
"";
98 auto find_c = [](
char&
c){
return ((
c==
',') or (
c==
'(') or (
c==
')')); };
99 std::replace_if (line.begin(), line.end(), find_c,
' ');
102 std::stringstream sline(line);
105 sline >> tmp1 >> tmp2;
106 if (sline.bad() or sline.fail()) {
109 throw std::runtime_error(
"IO error");
111 p=std::pair<T,Q>(tmp1,tmp2);
131 const std::vector<std::string> allowed_values1={})
160 std::stringstream sprecedence;
162 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,
' ');
181 std::string empty_keyval(keyval.size(),
' ');
182 empty_keyval[33]=
'#';
184 std::string allowed_val;
186 using madness::operators::operator<<;
187 std::stringstream ss;
189 allowed_val+=ss.str();
193 bool leave_space_for_allowed_values=(allowed_val.size()>0);
196 long keyvalsize=keyval.size();
199 std::vector<std::string> commentlines(1);
201 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(
"");
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>
void serialize (Archive& ar) {
254 using madness::operators::operator<<;
255 std::stringstream ss;
257 std::string
errmsg=
"\ntrying to assign a value that's not allowed\n\n";
259 errmsg+=
"\tallowed values: " + ss.str() +
"\n\n";
294 void print(
const std::string header=
"",
const std::string footer=
"")
const;
299 T get(
const std::string key)
const {
302 if (std::is_same<T,std::string>::value) {
305 return fromstring<T>(parameter.
get_value());
312 template <
typename Archive>
void serialize (Archive& ar) {
327 const std::string tag) {
334 std::string msg=
"could not find user-defined input file: "+parser.
value(
"input")+
"\n";
335 throw std::invalid_argument(msg);
337 }
catch (std::invalid_argument&
e) {
339 }
catch (std::exception&
e) {
375 void initialize(
const std::string& key,
const T& value,
const std::string comment=
"",
376 const std::vector<T> allowed_values={}) {
380 throw std::runtime_error(
"initialization error");
384 std::string
type = std::type_index(
typeid(
T)).name();
387 std::string key_lower=key;
388 std::transform(key_lower.begin(), key_lower.end(), key_lower.begin(), ::tolower);
389 std::transform(svalue.begin(), svalue.end(), svalue.begin(), ::tolower);
390 std::vector<std::string> av_lower_vec;
391 for (
const T& av : allowed_values) {
393 std::transform(av_lower.begin(), av_lower.end(), av_lower.begin(), ::tolower);
394 av_lower_vec.push_back(av_lower);
397 parameters.insert(std::make_pair<std::string, QCParameter>
398 (std::string(key_lower),QCParameter(svalue,
type,comment,av_lower_vec)));
406 if (not check_type_silent<T>(parameter)) {
407 throw std::runtime_error(
"type error in set_derived_value for key "+key);
422 if (not check_type_silent<T>(
get_parameter(key)))
return false;
425 T value=fromstring<T>(val);
426 set_user_defined_value<T>(key,value);
432 for (
auto [key, value]: j.items()) {
442 auto param_type =
p.second.get_type();
443 if (param_type ==
"i") {
444 j_params[
p.first] = get<int>(
p.first);
446 }
else if (param_type ==
"d") {
447 j_params[
p.first] = get<double>(
p.first);
450 }
else if (param_type ==
"b") {
451 j_params[
p.first] = get<bool>(
p.first);
454 }
else if (param_type ==
"St6vectorIdSaIdEE") {
455 j_params[
p.first] = get<std::vector<double>>(
p.first);
456 }
else if (param_type ==
"NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") {
457 auto sval = get<std::string>(
p.first);
458 if (!sval.empty())
continue;
459 j_params[
p.first] = sval;
461 }
else if (
p.second.get_type() ==
"m") {
462 j_params[
p.first] = get<size_t>(
p.first);
475 auto param_type =
p.second.get_type();
476 if (param_type ==
"i") {
477 j_params[
p.first] = get<int>(
p.first);
479 }
else if (param_type ==
"d") {
480 j_params[
p.first] = get<double>(
p.first);
483 }
else if (param_type ==
"b") {
484 j_params[
p.first] = get<bool>(
p.first);
487 }
else if (param_type ==
"St6vectorIdSaIdEE") {
488 j_params[
p.first] = get<std::vector<double>>(
p.first);
489 }
else if (param_type ==
"NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") {
490 auto sval = get<std::string>(
p.first);
491 if (!sval.empty())
continue;
492 j_params[
p.first] = sval;
494 }
else if (
p.second.get_type() ==
"m") {
495 j_params[
p.first] = get<size_t>(
p.first);
498 j[
"parameters"] = j_params;
504 auto param_type =
p.second.get_type();
506 if (param_type ==
"i") {
507 if (get<int>(
p.first) != other.
get<
int>(
p.first)) {
return false; };
509 }
else if (param_type ==
"d") {
510 if (get<double>(
p.first) != other.
get<
double>(
p.first)) {
return false; };
512 }
else if (param_type ==
"b") {
513 if (get<bool>(
p.first) != other.
get<
bool>(
p.first)) {
return false; };
515 }
else if (param_type ==
"St6vectorIdSaIdEE") {
516 if (
get<std::vector<double>>(
p.first) !=
517 other.
get<std::vector<double>>(
p.first)) {
521 }
else if (param_type ==
"NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") {
522 if (get<std::string>(
p.first) != other.
get<std::string>(
p.first)) {
525 }
else if (
p.second.get_type() ==
"m") {
526 if (get<size_t>(
p.first) != other.
get<
size_t>(
p.first)) {
return false; };
536 if (not check_type_silent<T>(parameter)) {
537 throw std::runtime_error(
"type error in set_user_defined_value");
546 throw std::runtime_error(
"could not find parameter for key "+key);
556 throw std::runtime_error(
"could not find parameter for key "+key);
569 if (check_type_silent<T>(parameter))
return true;
571 madness::print(
"trying to get the wrong type in QCCalculationParametersBase");
581 return (parameter.
get_type()==std::type_index(
typeid(
T)).name());
592 std::ostringstream ss;
593 ss << std::boolalpha <<
arg;
599 using madness::operators::operator<<;
600 std::ostringstream ss;
601 static_assert(not std::is_same<T,bool>::value,
"you need to specialize tostring for this type");
603 ss<<std::scientific << std::setprecision(4) <<
arg;
604 std::string str=ss.str();
605 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
612 static typename std::enable_if<std::is_floating_point<T>::value,
void>
::type
614 if (std::isinf(
arg)) {
615 std::stringstream ss;
616 ss << std::numeric_limits<T>::infinity();
622 static typename std::enable_if<!std::is_floating_point<T>::value,
void>
::type
628 static typename std::enable_if<!std::is_same<T,bool>::value,
T>
::type
631 std::stringstream ssvalue(
arg);
634 T result=read_quotes<T>(ssvalue);
636 bool type_conversion_failed=ssvalue.fail();
639 if (type_conversion_failed and (std::is_floating_point<T>::value)) {
641 const static T inf=std::numeric_limits<T>::infinity();
643 if (sinf==
arg) result=inf;
644 type_conversion_failed=
false;
647 if (type_conversion_failed) {
649 std::string
errmsg=
"error in type conversion for argument >> " +
arg
650 +
" << to type " + std::type_index(
typeid(
T)).name();
651 throw std::runtime_error(
errmsg);
656 while (ssvalue >> word) {
657 std::string
errmsg=
"trailing characters in arguement >> " +
arg +
" <<";
658 throw std::runtime_error(
errmsg);
664 static typename std::enable_if<std::is_same<T,std::string>::value,
T>
::type
669 if (
arg.find(
"\"")==std::string::npos) {
677 if (ssvalue.fail()) {
678 std::string
errmsg=
"missing closing double quote in line >> " +
arg;
679 throw std::runtime_error(
errmsg);
682 counter=std::count(result.begin(), result.end(),
'"');
692 std::size_t first=
arg.find_first_not_of(
' ');
693 std::size_t last=
arg.find_last_not_of(
' ');
694 return arg.substr(first,last-first+1);
698 std::size_t first=
arg.find_first_of(
'"');
699 std::size_t last=
arg.find_last_of(
'"');
700 return arg.substr(first+1,last-first-1);
704 return "\""+
arg+
"\"";
709 static typename std::enable_if<!std::is_same<T,std::string>::value,
T>
::type
717 static typename std::enable_if<std::is_same<T,bool>::value,
T>
::type
720 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
721 if (str==
"true" or str==
"1" or str==
"yes")
return true;
722 if (str==
"false" or str==
"0" or str==
"no")
return false;
723 std::string
errmsg=
"error in type conversion for argument >> " +
arg
724 +
" << to type " + std::type_index(
typeid(
T)).name();
725 throw std::runtime_error(
errmsg);
731 bool operator!=(
const QCCalculationParametersBase& p1,
const QCCalculationParametersBase& p2);
Interface templates for the archives (serialization).
class for holding the parameters for calculation
Definition QCCalculationParametersBase.h:290
static bool check_type_silent(const QCParameter ¶meter)
Definition QCCalculationParametersBase.h:580
static std::enable_if< std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition QCCalculationParametersBase.h:665
virtual void read_input_and_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
Definition QCCalculationParametersBase.h:325
ParameterContainerT get_all_parameters() const
Definition QCCalculationParametersBase.h:412
static std::string tostring(const T &arg)
Definition QCCalculationParametersBase.h:598
hashT hash() const
Definition QCCalculationParametersBase.h:316
const QCParameter & get_parameter(const std::string key) const
Definition QCCalculationParametersBase.h:544
static std::enable_if< std::is_floating_point< T >::value, void >::type overwrite_if_inf(std::string &str, const T &arg)
Definition QCCalculationParametersBase.h:613
static std::enable_if<!std::is_floating_point< T >::value, void >::type overwrite_if_inf(std::string &str, const T &arg)
Definition QCCalculationParametersBase.h:623
std::map< std::string, QCParameter > ParameterContainerT
Definition QCCalculationParametersBase.h:322
bool throw_if_datagroup_not_found
Definition QCCalculationParametersBase.h:360
void set_user_defined_value(const std::string &key, const T &value)
Definition QCCalculationParametersBase.h:533
bool print_debug
Definition QCCalculationParametersBase.h:357
json to_json() const
Definition QCCalculationParametersBase.h:438
void to_json(json &j) const
Definition QCCalculationParametersBase.h:471
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:562
T get(const std::string key) const
Definition QCCalculationParametersBase.h:299
void initialize(const std::string &key, const T &value, const std::string comment="", const std::vector< T > allowed_values={})
Definition QCCalculationParametersBase.h:375
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:431
bool ignore_unknown_keys
Definition QCCalculationParametersBase.h:358
bool file_exists(World &world, std::string filename) const
Definition QCCalculationParametersBase.cc:51
static std::string tostring(const bool &arg)
Definition QCCalculationParametersBase.h:591
QCCalculationParametersBase(const QCCalculationParametersBase &other)
copy ctor
Definition QCCalculationParametersBase.h:366
static std::enable_if<!std::is_same< T, std::string >::value, T >::type read_quotes(std::stringstream &ssvalue)
Definition QCCalculationParametersBase.h:710
bool ignore_unknown_keys_silently
Definition QCCalculationParametersBase.h:359
QCParameter & get_parameter(const std::string key)
Definition QCCalculationParametersBase.h:553
bool is_user_defined(std::string key) const
Definition QCCalculationParametersBase.h:308
void serialize(Archive &ar)
Definition QCCalculationParametersBase.h:312
bool try_setting_user_defined_value(const std::string &key, const std::string &val)
Definition QCCalculationParametersBase.h:420
static std::string trim_blanks(const std::string arg)
Definition QCCalculationParametersBase.h:691
ParameterContainerT parameters
Definition QCCalculationParametersBase.h:323
bool operator==(const QCCalculationParametersBase &other) const
Definition QCCalculationParametersBase.h:501
static bool check_type(const std::string key, const QCParameter ¶meter)
Definition QCCalculationParametersBase.h:568
static std::string add_quotes(const std::string arg)
Definition QCCalculationParametersBase.h:703
static std::enable_if< std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition QCCalculationParametersBase.h:718
static std::enable_if<!std::is_same< T, bool >::value, T >::type fromstring(const std::string &arg)
Definition QCCalculationParametersBase.h:629
virtual ~QCCalculationParametersBase()
destructor
Definition QCCalculationParametersBase.h:372
static std::string trim_quotes(const std::string arg)
Definition QCCalculationParametersBase.h:697
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:403
QCCalculationParametersBase()
ctor for testing
Definition QCCalculationParametersBase.h:363
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:34
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:2502
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:17
nlohmann::json json
Definition QCCalculationParametersBase.h:27
static std::enable_if< std::is_floating_point< T >::value, void >::type check_for_inf(const std::string &str, T &arg)
Definition QCCalculationParametersBase.h:31
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:126
int print_order
Definition QCCalculationParametersBase.h:272
std::vector< std::string > allowed_values
Definition QCCalculationParametersBase.h:271
std::string print_line(const std::string &key) const
Definition QCCalculationParametersBase.h:168
QCParameter()
Definition QCCalculationParametersBase.h:128
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:130
int get_print_order() const
Definition QCCalculationParametersBase.h:166
std::string default_value
Definition QCCalculationParametersBase.h:265
std::string not_allowed_errmsg() const
Definition QCCalculationParametersBase.h:253
void serialize(Archive &ar)
Definition QCCalculationParametersBase.h:227
std::string user_defined_value
Definition QCCalculationParametersBase.h:267
@ derived
Definition QCCalculationParametersBase.h:232
@ defined
Definition QCCalculationParametersBase.h:232
@ def
Definition QCCalculationParametersBase.h:232
void set_all()
Definition QCCalculationParametersBase.h:240
bool check_allowed()
Definition QCCalculationParametersBase.h:247
std::string get_comment() const
Definition QCCalculationParametersBase.h:155
void set_user_defined_value(const std::string val)
Definition QCCalculationParametersBase.h:143
std::string type
Definition QCCalculationParametersBase.h:268
std::string print_precedence() const
Definition QCCalculationParametersBase.h:156
std::string value
Definition QCCalculationParametersBase.h:264
std::string derived_value
Definition QCCalculationParametersBase.h:266
std::string get_type() const
Definition QCCalculationParametersBase.h:154
std::string comment
Definition QCCalculationParametersBase.h:270
bool is_user_defined() const
Definition QCCalculationParametersBase.h:149
std::string null
Definition QCCalculationParametersBase.h:269
std::string get_value() const
Definition QCCalculationParametersBase.h:153
void set_derived_value(const std::string val)
Definition QCCalculationParametersBase.h:138
hashT hash() const
Definition QCCalculationParametersBase.h:234
very simple command line parser
Definition commandlineparser.h:15
std::string value(const std::string key) const
Definition commandlineparser.h:59
bool key_exists(std::string key) const
Definition commandlineparser.h:55
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:86
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.