25using namespace settings;
30 map<string, string> m;
32 map<string, vector<string>> a;
34 map<string, shared_ptr<settings::Key>> r;
37 m[
"number_of_elements" ] =
"";
39 m[
"atom_energy" ] =
"";
40 m[
"cutoff_type" ] =
"";
41 m[
"symfunction_short" ] =
"";
42 m[
"scale_symmetry_functions" ] =
"";
43 m[
"scale_min_short" ] =
"";
44 m[
"scale_max_short" ] =
"";
45 m[
"center_symmetry_functions" ] =
"";
46 m[
"scale_symmetry_functions_sigma" ] =
"";
47 m[
"global_hidden_layers_short" ] =
"";
48 m[
"global_hidden_layers_electrostatic" ] =
"";
49 m[
"global_nodes_short" ] =
"";
50 m[
"global_nodes_electrostatic" ] =
"";
51 m[
"global_activation_short" ] =
"";
52 m[
"global_activation_electrostatic" ] =
"";
53 m[
"element_hidden_layers_short" ] =
"";
54 m[
"element_hidden_layers_electrostatic"] =
"";
55 m[
"element_nodes_short" ] =
"";
56 m[
"element_nodes_electrostatic" ] =
"";
57 m[
"element_activation_short" ] =
"";
58 m[
"element_activation_electrostatic" ] =
"";
59 m[
"normalize_nodes" ] =
"";
60 m[
"mean_energy" ] =
"";
61 m[
"conv_length" ] =
"";
62 m[
"conv_energy" ] =
"";
63 m[
"conv_charge" ] =
"";
65 m[
"fixed_gausswidth" ] =
"";
66 m[
"ewald_truncation_error_method" ] =
"";
67 m[
"ewald_prec" ] =
"";
68 m[
"screen_electrostatics" ] =
"";
69 m[
"four_pi_epsilon" ] =
"";
72 m[
"random_seed" ] =
"";
73 m[
"test_fraction" ] =
"";
75 m[
"normalize_data_set" ] =
"";
76 m[
"use_short_forces" ] =
"";
77 m[
"rmse_threshold" ] =
"";
78 m[
"rmse_threshold_energy" ] =
"";
79 m[
"rmse_threshold_force" ] =
"";
80 m[
"rmse_threshold_charge" ] =
"";
81 m[
"rmse_threshold_trials" ] =
"";
82 m[
"rmse_threshold_trials_energy" ] =
"";
83 m[
"rmse_threshold_trials_force" ] =
"";
84 m[
"rmse_threshold_trials_charge" ] =
"";
85 m[
"energy_fraction" ] =
"";
86 m[
"force_fraction" ] =
"";
87 m[
"force_energy_ratio" ] =
"";
88 m[
"charge_fraction" ] =
"";
89 m[
"use_old_weights_short" ] =
"";
90 m[
"use_old_weights_charge" ] =
"";
91 m[
"weights_min" ] =
"";
92 m[
"weights_max" ] =
"";
93 m[
"initial_hardness" ] =
"";
94 m[
"nguyen_widrow_weights_short" ] =
"";
95 m[
"nguyen_widrow_weights_charge" ] =
"";
96 m[
"precondition_weights" ] =
"";
97 m[
"main_error_metric" ] =
"";
98 m[
"write_trainpoints" ] =
"";
99 m[
"write_trainforces" ] =
"";
100 m[
"write_traincharges" ] =
"";
101 m[
"write_weights_epoch" ] =
"";
102 m[
"write_neuronstats" ] =
"";
103 m[
"write_trainlog" ] =
"";
104 m[
"repeated_energy_update" ] =
"";
105 m[
"updater_type" ] =
"";
106 m[
"parallel_mode" ] =
"";
107 m[
"jacobian_mode" ] =
"";
108 m[
"update_strategy" ] =
"";
109 m[
"selection_mode" ] =
"";
110 m[
"selection_mode_energy" ] =
"";
111 m[
"selection_mode_force" ] =
"";
112 m[
"selection_mode_charge" ] =
"";
113 m[
"task_batch_size_energy" ] =
"";
114 m[
"task_batch_size_force" ] =
"";
115 m[
"task_batch_size_charge" ] =
"";
116 m[
"gradient_type" ] =
"";
117 m[
"gradient_eta" ] =
"";
118 m[
"gradient_adam_eta" ] =
"";
119 m[
"gradient_adam_beta1" ] =
"";
120 m[
"gradient_adam_beta2" ] =
"";
121 m[
"gradient_adam_epsilon" ] =
"";
122 m[
"kalman_type" ] =
"";
123 m[
"kalman_epsilon" ] =
"";
124 m[
"kalman_eta" ] =
"";
125 m[
"kalman_etatau" ] =
"";
126 m[
"kalman_etamax" ] =
"";
127 m[
"kalman_q0" ] =
"";
128 m[
"kalman_qtau" ] =
"";
129 m[
"kalman_qmin" ] =
"";
130 m[
"kalman_lambda_short" ] =
"";
131 m[
"kalman_nue_short" ] =
"";
132 m[
"memorize_symfunc_results" ] =
"";
133 m[
"force_weight" ] =
"";
136 a[
"nnp_type" ] = {
"nnp_generation",
"nnp_type_gen",
"nnp_gen"};
137 a[
"rmse_threshold_energy"] = {
"short_energy_error_threshold"};
138 a[
"rmse_threshold_force" ] = {
"short_force_error_threshold"};
139 a[
"energy_fraction" ] = {
"short_energy_fraction"};
140 a[
"force_fraction" ] = {
"short_force_fraction"};
141 a[
"symfunction_short" ] = {
"symfunction"};
146 if (r.find(im.first) != r.end())
148 throw runtime_error(
"ERROR: Multiple definition of keyword.\n");
151 r[im.first] = make_shared<settings::Key>();
153 r.at(im.first)->addAlternative(im.first);
155 r.at(im.first)->setDescription(im.second);
157 if (a.find(im.first) != a.end())
160 for (
auto alt : a.at(im.first))
163 if (r.find(alt) != r.end())
165 throw runtime_error(
"ERROR: Multiple definition of "
166 "alternative keyword.\n");
169 r[alt] = r.at(im.first);
171 r[alt]->addAlternative(alt);
181string Settings::operator[](
string const& keyword)
const
195 bool const exact)
const
203 throw runtime_error(
"ERROR: Not in the list of allowed keyword: \"" +
229 throw std::runtime_error(
"ERROR: Keyword \"" + keyword
230 +
"\" not found.\n");
234 throw std::runtime_error(
"ERROR: Neither keyword \"" + keyword
235 +
"\" nor alternative keywords found.\n");
285 throw runtime_error(
"ERROR: Could not open file: \"" +
fileName
289 while (getline(file, line))
291 lines.push_back(line);
302 map<size_t, string>
const& replacements)
const
304 if (!file->is_open())
306 runtime_error(
"ERROR: Could not write to file.\n");
310 for (
auto const& l :
lines)
312 if (replacements.find(i) != replacements.end())
314 (*file) << replacements.at(i);
316 else (*file) << l <<
'\n';
325 for (
size_t i = 0; i <
lines.size(); ++i)
327 string line =
lines.at(i);
339 if (line.find(
'#') != string::npos)
341 line.erase(line.find(
'#'));
343 if (line.find(
'!') != string::npos)
345 line.erase(line.find(
'!'));
347 if (line.find_first_not_of(
' ') == string::npos)
356 size_t const separatorPosition = line.find_first_of(
" ");
358 pair<string, size_t> value;
360 if (separatorPosition == string::npos)
364 value = pair<string, size_t>(
"", i);
369 key = line.substr(0, separatorPosition);
370 value = pair<string, size_t>(line.erase(0, separatorPosition + 1),
374 contents.insert(pair<
string, pair<string, size_t> >(key, value));
377 size_t numProblems = 0;
378 size_t numCritical = 0;
382 log.push_back(
strpr(
"WARNING: %zu problems detected (%zu critical).\n",
383 numProblems, numCritical));
393 size_t countProblems = 0;
394 size_t countCritical = 0;
397 for (multimap<
string, pair<string, size_t> >::const_iterator
404 "WARNING: Unknown keyword \"%s\" at line %zu.\n",
406 (*it).second.second + 1));
415 && (*it).first !=
"symfunction"
416 && (*it).first !=
"symfunction_short"
417 && (*it).first !=
"atom_energy"
418 && (*it).first !=
"element_nodes_short"
419 && (*it).first !=
"fixed_gausswidth"
420 && (*it).first !=
"initial_hardness")
425 "WARNING (CRITICAL): Multiple instances of \"%s\" detected.\n",
426 (*it).first.c_str()));
434 if (it->second->hasUniqueKeyword())
continue;
435 vector<string> duplicates;
436 for (
auto keyword : *it->second)
440 duplicates.push_back(keyword);
443 if (duplicates.size() > 1)
448 "WARNING (CRITICAL): Multiple alternative versions of keyword "
449 "\"%s\" detected:.\n", (*it).first.c_str()));
450 for (
auto d : duplicates)
453 " - \"%s\"\n",
d.c_str()));
458 return make_pair(countProblems, countCritical);
map< string, shared_ptr< settings::Key > > const createKnownKeywordsMap()
std::string getMainKeyword() const
std::vector< std::string > info() const
Get logged information about settings file.
std::string keywordCheck(std::string const &keyword) const
Check for keyword and alternatives, throw exception if not present.
static KeywordList knownKeywords
Map containing all known keywords and a description.
std::vector< std::string > lines
Vector of all lines in settings file.
std::string getValue(Key const &key) const override
void writeSettingsFile(std::ofstream *const &file, std::map< std::size_t, std::string > const &replacements={}) const
Write complete settings file.
std::pair< KeyMap::const_iterator, KeyMap::const_iterator > KeyRange
std::size_t loadFile(std::string const &fileName="input.nn")
Load a file with settings.
std::map< std::string, std::shared_ptr< Key > > const KeywordList
KeyRange getValues(std::string const &keyword) const
Get all keyword-value pairs for given keyword.
std::vector< std::string > log
Vector with log lines.
std::string fileName
Settings file name.
KeyMap contents
Map containing all keyword-value pairs.
std::vector< std::string > getSettingsLines() const
Get complete settings file.
bool keywordExists(Key const &key, bool const exact=false) const override
std::size_t parseLines()
Parse lines and create contents map.
void readFile()
Read file once and save all lines in lines vector.
std::pair< std::size_t, std::size_t > sanityCheck()
Check if all keywords are in known-keywords database and for duplicates.
string strpr(const char *format,...)
String version of printf function.
string reduce(string const &line, string const &whitespace, string const &fill)
Replace multiple whitespaces with fill.