-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathSettings.cpp
More file actions
430 lines (386 loc) · 13.4 KB
/
Settings.cpp
File metadata and controls
430 lines (386 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// n2p2 - A neural network potential package
// Copyright (C) 2018 Andreas Singraber (University of Vienna)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Settings.h"
#include "utility.h"
#include <fstream> // std::ifstream
#include <stdexcept> // std::runtime_error
#include <tuple> // std::tie
using namespace std;
using namespace nnp;
map<string, shared_ptr<Settings::Key>> const createKnownKeywordsMap()
{
// Main keyword names and descriptions.
map<string, string> m;
// Alternative names.
map<string, vector<string>> a;
// Complete keyword map to return.
map<string, shared_ptr<Settings::Key>> r;
// Required for prediction.
m["number_of_elements" ] = "";
m["elements" ] = "";
m["atom_energy" ] = "";
m["cutoff_type" ] = "";
m["inner_cutoff" ] = "";
m["symfunction_short" ] = "";
m["scale_symmetry_functions" ] = "";
m["scale_min_short" ] = "";
m["scale_max_short" ] = "";
m["center_symmetry_functions" ] = "";
m["scale_symmetry_functions_sigma"] = "";
m["global_hidden_layers_short" ] = "";
m["global_nodes_short" ] = "";
m["global_activation_short" ] = "";
m["element_nodes_short" ] = "";
m["normalize_nodes" ] = "";
m["mean_energy" ] = "";
m["conv_length" ] = "";
m["conv_energy" ] = "";
m["nnp_type" ] = "";
// Training keywords.
m["random_seed" ] = "";
m["test_fraction" ] = "";
m["epochs" ] = "";
m["normalize_data_set" ] = "";
m["use_short_forces" ] = "";
m["rmse_threshold" ] = "";
m["rmse_threshold_energy" ] = "";
m["rmse_threshold_force" ] = "";
m["rmse_threshold_charge" ] = "";
m["rmse_threshold_trials" ] = "";
m["rmse_threshold_trials_energy" ] = "";
m["rmse_threshold_trials_force" ] = "";
m["rmse_threshold_trials_charge" ] = "";
m["energy_fraction" ] = "";
m["force_fraction" ] = "";
m["force_energy_ratio" ] = "";
m["charge_fraction" ] = "";
m["use_old_weights_short" ] = "";
m["use_old_weights_charge" ] = "";
m["weights_min" ] = "";
m["weights_max" ] = "";
m["nguyen_widrow_weights_short" ] = "";
m["nguyen_widrow_weights_charge" ] = "";
m["precondition_weights" ] = "";
m["main_error_metric" ] = "";
m["write_trainpoints" ] = "";
m["write_trainforces" ] = "";
m["write_traincharges" ] = "";
m["write_weights_epoch" ] = "";
m["write_neuronstats" ] = "";
m["write_trainlog" ] = "";
m["repeated_energy_update" ] = "";
m["updater_type" ] = "";
m["parallel_mode" ] = "";
m["jacobian_mode" ] = "";
m["update_strategy" ] = "";
m["selection_mode" ] = "";
m["selection_mode_energy" ] = "";
m["selection_mode_force" ] = "";
m["selection_mode_charge" ] = "";
m["task_batch_size_energy" ] = "";
m["task_batch_size_force" ] = "";
m["task_batch_size_charge" ] = "";
m["gradient_type" ] = "";
m["gradient_eta" ] = "";
m["gradient_adam_eta" ] = "";
m["gradient_adam_beta1" ] = "";
m["gradient_adam_beta2" ] = "";
m["gradient_adam_epsilon" ] = "";
m["kalman_type" ] = "";
m["kalman_epsilon" ] = "";
m["kalman_eta" ] = "";
m["kalman_etatau" ] = "";
m["kalman_etamax" ] = "";
m["kalman_q0" ] = "";
m["kalman_qtau" ] = "";
m["kalman_qmin" ] = "";
m["kalman_lambda_short" ] = "";
m["kalman_nue_short" ] = "";
m["memorize_symfunc_results" ] = "";
m["force_weight" ] = "";
// Alternative keyword names.
a["nnp_type"] = {"nn_type"};
a["rmse_threshold_energy"] = {"short_energy_error_threshold"};
a["rmse_threshold_force" ] = {"short_force_error_threshold"};
a["energy_fraction" ] = {"short_energy_fraction"};
a["force_fraction" ] = {"short_force_fraction"};
for (auto im : m)
{
// Check if keyword was already inserted.
if (r.find(im.first) != r.end())
{
throw runtime_error("ERROR: Multiple definition of keyword.\n");
}
// Insert new shared pointer to a Key object.
r[im.first] = make_shared<Settings::Key>();
// Add main keyword as first entry in alternatives list.
r.at(im.first)->words.push_back(im.first);
// Add description text.
r.at(im.first)->description = im.second;
// Check if alternative keywords exist.
if (a.find(im.first) != a.end())
{
// Loop over all alternative keywords.
for (auto alt : a.at(im.first))
{
// Check if alternative keyword is already inserted.
if (r.find(alt) != r.end())
{
throw runtime_error("ERROR: Multiple definition of "
"alternative keyword.\n");
}
// Set map entry, i.e. shared pointer, to Key object.
r[alt] = r.at(im.first);
// Add alternative keyword to list.
r[alt]->words.push_back(alt);
}
}
}
return r;
}
Settings::KeywordList Settings::knownKeywords = createKnownKeywordsMap();
string Settings::operator[](string const& keyword) const
{
return getValue(keyword);
}
size_t Settings::loadFile(string const& fileName)
{
this->fileName = fileName;
readFile();
return parseLines();
}
bool Settings::keywordExists(string const& keyword, bool exact) const
{
if (knownKeywords.find(keyword) == knownKeywords.end())
{
throw runtime_error("ERROR: Not in the list of allowed keyword: \"" +
keyword + "\".\n");
}
if (exact || knownKeywords.at(keyword)->isUnique())
{
return (contents.find(keyword) != contents.end());
}
else
{
for (auto alternative : knownKeywords.at(keyword)->words)
{
if (contents.find(alternative) != contents.end()) return true;
}
}
return false;
}
string Settings::keywordCheck(string const& keyword) const
{
bool exists = keywordExists(keyword, false);
bool unique = knownKeywords.at(keyword)->isUnique();
if (!exists)
{
if (unique)
{
throw std::runtime_error("ERROR: Keyword \"" + keyword
+ "\" not found.\n");
}
else
{
throw std::runtime_error("ERROR: Neither keyword \"" + keyword
+ "\" nor alternative keywords found.\n");
}
}
bool exact = keywordExists(keyword, true);
if (!exact)
{
for (auto alt : knownKeywords.at(keyword)->words)
{
if (contents.find(alt) != contents.end()) return alt;
}
}
return keyword;
}
string Settings::getValue(string const& keyword) const
{
return contents.find(keywordCheck(keyword))->second.first;
}
Settings::KeyRange Settings::getValues(string const& keyword) const
{
return contents.equal_range(keywordCheck(keyword));
}
vector<string> Settings::info() const
{
return log;
}
vector<string> Settings::getSettingsLines() const
{
return lines;
}
void Settings::readFile()
{
ifstream file;
string line;
log.push_back(strpr("Settings file name: %s\n", fileName.c_str()));
file.open(fileName.c_str());
if (!file.is_open())
{
throw runtime_error("ERROR: Could not open file: \"" + fileName
+ "\".\n");
}
while (getline(file, line))
{
lines.push_back(line);
}
file.close();
log.push_back(strpr("Read %zu lines.\n", lines.size()));
return;
}
void Settings::writeSettingsFile(ofstream* const& file,
map<size_t, string> const& replacements) const
{
if (!file->is_open())
{
runtime_error("ERROR: Could not write to file.\n");
}
size_t i = 0;
for (auto const& l : lines)
{
if (replacements.find(i) != replacements.end())
{
(*file) << replacements.at(i);
}
else (*file) << l << '\n';
i++;
}
return;
}
size_t Settings::parseLines()
{
for (size_t i = 0; i < lines.size(); ++i)
{
string line = lines.at(i);
// ignore empty and comment lines
if (line.empty())
{
continue;
}
// check for empty lines in Windows format
if (line == "\r")
{
continue;
}
if (line.find('#') != string::npos)
{
line.erase(line.find('#'));
}
if (line.find('!') != string::npos)
{
line.erase(line.find('!'));
}
if (line.find_first_not_of(' ') == string::npos)
{
continue;
}
// remove leading and trailing whitespaces and trim separating spaces
line = reduce(line);
// find separator position
size_t const separatorPosition = line.find_first_of(" ");
string key;
pair<string, size_t> value;
if (separatorPosition == string::npos)
{
// first check for single keyword without value
key = line;
value = pair<string, size_t>("", i);
}
else
{
// one or more arguments
key = line.substr(0, separatorPosition);
value = pair<string, size_t>(line.erase(0, separatorPosition + 1),
i);
}
contents.insert(pair<string, pair<string, size_t> >(key, value));
}
size_t numProblems = 0;
size_t numCritical = 0;
tie(numProblems, numCritical) = sanityCheck();
if (numProblems > 0)
{
log.push_back(strpr("WARNING: %zu problems detected (%zu critical).\n",
numProblems, numCritical));
}
log.push_back(strpr("Found %zu lines with keywords.\n", contents.size()));
return numCritical;
}
pair<size_t, size_t> Settings::sanityCheck()
{
size_t countProblems = 0;
size_t countCritical = 0;
// check for unknown keywords
for (multimap<string, pair<string, size_t> >::const_iterator
it = contents.begin(); it != contents.end(); ++it)
{
if (knownKeywords.find((*it).first) == knownKeywords.end())
{
countProblems++;
log.push_back(strpr(
"WARNING: Unknown keyword \"%s\" at line %zu.\n",
(*it).first.c_str(),
(*it).second.second + 1));
}
}
// check for multiple instances of known keywords (with exceptions)
for (KeywordList::const_iterator it = knownKeywords.begin();
it != knownKeywords.end(); ++it)
{
if (contents.count((*it).first) > 1
&& (*it).first != "symfunction_short"
&& (*it).first != "atom_energy"
&& (*it).first != "element_nodes_short")
{
countProblems++;
countCritical++;
log.push_back(strpr(
"WARNING (CRITICAL): Multiple instances of \"%s\" detected.\n",
(*it).first.c_str()));
}
}
// Check for usage of multiple keyword versions.
for (KeywordList::const_iterator it = knownKeywords.begin();
it != knownKeywords.end(); ++it)
{
if (it->second->isUnique()) continue;
vector<string> duplicates;
for (auto keyword : it->second->words)
{
if (contents.find(keyword) != contents.end())
{
duplicates.push_back(keyword);
}
}
if (duplicates.size() > 1)
{
countProblems++;
countCritical++;
log.push_back(strpr(
"WARNING (CRITICAL): Multiple alternative versions of keyword "
"\"%s\" detected:.\n", (*it).first.c_str()));
for (auto d : duplicates)
{
log.push_back(strpr(
" - \"%s\"\n", d.c_str()));
}
}
}
return make_pair(countProblems, countCritical);
}