n2p2 - A neural network potential package
nnp::Settings Class Reference

Reads and analyzes settings file and stores parameters. More...

#include <Settings.h>

Collaboration diagram for nnp::Settings:

Classes

struct  Key
 Keyword properties. More...
 

Public Types

typedef std::multimap< std::string, std::pair< std::string, std::size_t > > KeyMap
 
typedef std::pair< KeyMap::const_iterator, KeyMap::const_iterator > KeyRange
 
typedef std::map< std::string, std::shared_ptr< Key > > const KeywordList
 

Public Member Functions

std::string operator[] (std::string const &keyword) const
 Overload [] operator. More...
 
std::size_t loadFile (std::string const &fileName="input.nn")
 Load a file with settings. More...
 
bool keywordExists (std::string const &keyword, bool exact=false) const
 Check if keyword is present in settings file. More...
 
std::string keywordCheck (std::string const &keyword) const
 Check for keyword and alternatives, throw exception if not present. More...
 
std::string getValue (std::string const &keyword) const
 Get value for given keyword. More...
 
KeyRange getValues (std::string const &keyword) const
 Get all keyword-value pairs for given keyword. More...
 
std::vector< std::string > info () const
 Get logged information about settings file. More...
 
std::vector< std::string > getSettingsLines () const
 Get complete settings file. More...
 
void writeSettingsFile (std::ofstream *const &file, std::map< std::size_t, std::string > const &replacements={}) const
 Write complete settings file. More...
 

Private Member Functions

void readFile ()
 Read file once and save all lines in lines vector. More...
 
std::size_t parseLines ()
 Parse lines and create contents map. More...
 
std::pair< std::size_t, std::size_t > sanityCheck ()
 Check if all keywords are in known-keywords database and for duplicates. More...
 

Private Attributes

std::vector< std::string > lines
 Vector of all lines in settings file. More...
 
std::vector< std::string > log
 Vector with log lines. More...
 
KeyMap contents
 Map containing all keyword-value pairs. More...
 
std::string fileName
 Settings file name. More...
 

Static Private Attributes

static KeywordList knownKeywords = createKnownKeywordsMap()
 Map containing all known keywords and a description. More...
 

Detailed Description

Reads and analyzes settings file and stores parameters.

Definition at line 32 of file Settings.h.

Member Typedef Documentation

◆ KeyMap

typedef std::multimap<std::string, std::pair<std::string, std::size_t> > nnp::Settings::KeyMap

Definition at line 48 of file Settings.h.

◆ KeyRange

typedef std::pair<KeyMap::const_iterator, KeyMap::const_iterator> nnp::Settings::KeyRange

Definition at line 50 of file Settings.h.

◆ KeywordList

typedef std::map<std::string, std::shared_ptr<Key> > const nnp::Settings::KeywordList

Definition at line 52 of file Settings.h.

Member Function Documentation

◆ operator[]()

string Settings::operator[] ( std::string const &  keyword) const

Overload [] operator.

Parameters
[in]keywordKeyword string.
Returns
Value string corresponding to keyword.

Internally calls getValue().

Definition at line 164 of file Settings.cpp.

165{
166 return getValue(keyword);
167}
std::string getValue(std::string const &keyword) const
Get value for given keyword.
Definition: Settings.cpp:229

References getValue().

Here is the call graph for this function:

◆ loadFile()

size_t Settings::loadFile ( std::string const &  fileName = "input.nn")

Load a file with settings.

Parameters
[in]fileNameName of file containing settings.
Returns
Number of critical problems detected.

Definition at line 169 of file Settings.cpp.

170{
171 this->fileName = fileName;
172
173 readFile();
174 return parseLines();
175}
std::size_t parseLines()
Parse lines and create contents map.
Definition: Settings.cpp:296
std::string fileName
Settings file name.
Definition: Settings.h:154
void readFile()
Read file once and save all lines in lines vector.
Definition: Settings.cpp:249

References fileName, parseLines(), and readFile().

Referenced by nnp::Mode::loadSettingsFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ keywordExists()

bool Settings::keywordExists ( std::string const &  keyword,
bool  exact = false 
) const

Check if keyword is present in settings file.

Parameters
[in]keywordKeyword string.
[in]exactIf True check only for exact spelling, no alternative keywords allowed.
Returns
True if keyword exists, False otherwise.
Note
This throws an error if the keyword is not in the list of known keywords.

Definition at line 177 of file Settings.cpp.

178{
179 if (knownKeywords.find(keyword) == knownKeywords.end())
180 {
181 throw runtime_error("ERROR: Not in the list of allowed keyword: \"" +
182 keyword + "\".\n");
183 }
184 if (exact || knownKeywords.at(keyword)->isUnique())
185 {
186 return (contents.find(keyword) != contents.end());
187 }
188 else
189 {
190 for (auto alternative : knownKeywords.at(keyword)->words)
191 {
192 if (contents.find(alternative) != contents.end()) return true;
193 }
194 }
195
196 return false;
197}
static KeywordList knownKeywords
Map containing all known keywords and a description.
Definition: Settings.h:152
KeyMap contents
Map containing all keyword-value pairs.
Definition: Settings.h:150

References contents, and knownKeywords.

Referenced by nnp::Training::dataSetNormalization(), nnp::Training::initializeWeights(), keywordCheck(), nnp::Mode::loadSettingsFile(), nnp::Training::randomizeNeuralNetworkWeights(), nnp::Training::setStage(), nnp::Mode::settingsKeywordExists(), nnp::Mode::setupElements(), nnp::Training::setupFileOutput(), nnp::Mode::setupNeuralNetwork(), nnp::Mode::setupNormalization(), nnp::Training::setupSelectionMode(), nnp::Mode::setupSymmetryFunctionScaling(), nnp::Training::setupTraining(), and nnp::Training::setupUpdatePlan().

Here is the caller graph for this function:

◆ keywordCheck()

string Settings::keywordCheck ( std::string const &  keyword) const

Check for keyword and alternatives, throw exception if not present.

Parameters
[in]keywordOriginal keyword for which alternatives should be searched.
Returns
Keyword or alternative found in file contents.

Definition at line 199 of file Settings.cpp.

200{
201 bool exists = keywordExists(keyword, false);
202 bool unique = knownKeywords.at(keyword)->isUnique();
203 if (!exists)
204 {
205 if (unique)
206 {
207 throw std::runtime_error("ERROR: Keyword \"" + keyword
208 + "\" not found.\n");
209 }
210 else
211 {
212 throw std::runtime_error("ERROR: Neither keyword \"" + keyword
213 + "\" nor alternative keywords found.\n");
214 }
215 }
216
217 bool exact = keywordExists(keyword, true);
218 if (!exact)
219 {
220 for (auto alt : knownKeywords.at(keyword)->words)
221 {
222 if (contents.find(alt) != contents.end()) return alt;
223 }
224 }
225
226 return keyword;
227}
bool keywordExists(std::string const &keyword, bool exact=false) const
Check if keyword is present in settings file.
Definition: Settings.cpp:177

References contents, keywordExists(), and knownKeywords.

Referenced by getValue(), and getValues().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getValue()

string Settings::getValue ( std::string const &  keyword) const

Get value for given keyword.

Parameters
[in]keywordKeyword string.
Returns
Value string corresponding to keyword.

If keyword is present multiple times only the value of the first keyword-value pair is returned (use getValues() instead).

Definition at line 229 of file Settings.cpp.

230{
231 return contents.find(keywordCheck(keyword))->second.first;
232}
std::string keywordCheck(std::string const &keyword) const
Check for keyword and alternatives, throw exception if not present.
Definition: Settings.cpp:199

References contents, and keywordCheck().

Referenced by operator[](), and nnp::Mode::settingsGetValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getValues()

Settings::KeyRange Settings::getValues ( std::string const &  keyword) const

Get all keyword-value pairs for given keyword.

Parameters
[in]keywordKeyword string.
Returns
Pair with begin and end values for iteration.

Useful if keyword appears multiple times. Returns a pair representing begin and end for an iterator over all corresponding keyword-value pairs. Use like this:

Settings* s = new Settings("input.nn");
Settings::KeyRange r = s->getValues("symfunction_short");
for (Settings::KeyMap::const_iterator i = r.first; i != r.second; i++)
{
cout << "keyword: " << i->first
<< " value: " << i->second.first
<< " line : " << i->second.second << "\n";
}
Reads and analyzes settings file and stores parameters.
Definition: Settings.h:33
KeyRange getValues(std::string const &keyword) const
Get all keyword-value pairs for given keyword.
Definition: Settings.cpp:234
std::pair< KeyMap::const_iterator, KeyMap::const_iterator > KeyRange
Definition: Settings.h:50

Definition at line 234 of file Settings.cpp.

235{
236 return contents.equal_range(keywordCheck(keyword));
237}

References contents, and keywordCheck().

Referenced by nnp::Mode::setupElements(), nnp::Mode::setupNeuralNetwork(), and nnp::Mode::setupSymmetryFunctions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ info()

vector< string > Settings::info ( ) const

Get logged information about settings file.

Returns
Vector with log lines.

Definition at line 239 of file Settings.cpp.

240{
241 return log;
242}
std::vector< std::string > log
Vector with log lines.
Definition: Settings.h:148

References log.

Referenced by nnp::Mode::loadSettingsFile().

Here is the caller graph for this function:

◆ getSettingsLines()

vector< string > Settings::getSettingsLines ( ) const

Get complete settings file.

Returns
Vector with settings file lines.

Definition at line 244 of file Settings.cpp.

245{
246 return lines;
247}
std::vector< std::string > lines
Vector of all lines in settings file.
Definition: Settings.h:146

References lines.

Referenced by nnp::Training::dataSetNormalization(), and nnp::Mode::writePrunedSettingsFile().

Here is the caller graph for this function:

◆ writeSettingsFile()

void Settings::writeSettingsFile ( std::ofstream *const &  file,
std::map< std::size_t, std::string > const &  replacements = {} 
) const

Write complete settings file.

Parameters
[in,out]fileSettings file.
[in]replacementsMap of line numbers with alternative content.

Definition at line 274 of file Settings.cpp.

276{
277 if (!file->is_open())
278 {
279 runtime_error("ERROR: Could not write to file.\n");
280 }
281
282 size_t i = 0;
283 for (auto const& l : lines)
284 {
285 if (replacements.find(i) != replacements.end())
286 {
287 (*file) << replacements.at(i);
288 }
289 else (*file) << l << '\n';
290 i++;
291 }
292
293 return;
294}

References lines.

Referenced by nnp::Training::dataSetNormalization(), and nnp::Mode::writeSettingsFile().

Here is the caller graph for this function:

◆ readFile()

void Settings::readFile ( )
private

Read file once and save all lines in lines vector.

Definition at line 249 of file Settings.cpp.

250{
251 ifstream file;
252 string line;
253
254 log.push_back(strpr("Settings file name: %s\n", fileName.c_str()));
255 file.open(fileName.c_str());
256 if (!file.is_open())
257 {
258 throw runtime_error("ERROR: Could not open file: \"" + fileName
259 + "\".\n");
260 }
261
262 while (getline(file, line))
263 {
264 lines.push_back(line);
265 }
266
267 file.close();
268
269 log.push_back(strpr("Read %zu lines.\n", lines.size()));
270
271 return;
272}
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90

References fileName, lines, log, and nnp::strpr().

Referenced by loadFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseLines()

size_t Settings::parseLines ( )
private

Parse lines and create contents map.

Returns
Number of critical problems detected.

Definition at line 296 of file Settings.cpp.

297{
298 for (size_t i = 0; i < lines.size(); ++i)
299 {
300 string line = lines.at(i);
301
302 // ignore empty and comment lines
303 if (line.empty())
304 {
305 continue;
306 }
307 // check for empty lines in Windows format
308 if (line == "\r")
309 {
310 continue;
311 }
312 if (line.find('#') != string::npos)
313 {
314 line.erase(line.find('#'));
315 }
316 if (line.find('!') != string::npos)
317 {
318 line.erase(line.find('!'));
319 }
320 if (line.find_first_not_of(' ') == string::npos)
321 {
322 continue;
323 }
324
325 // remove leading and trailing whitespaces and trim separating spaces
326 line = reduce(line);
327
328 // find separator position
329 size_t const separatorPosition = line.find_first_of(" ");
330 string key;
331 pair<string, size_t> value;
332
333 if (separatorPosition == string::npos)
334 {
335 // first check for single keyword without value
336 key = line;
337 value = pair<string, size_t>("", i);
338 }
339 else
340 {
341 // one or more arguments
342 key = line.substr(0, separatorPosition);
343 value = pair<string, size_t>(line.erase(0, separatorPosition + 1),
344 i);
345 }
346
347 contents.insert(pair<string, pair<string, size_t> >(key, value));
348 }
349
350 size_t numProblems = 0;
351 size_t numCritical = 0;
352 tie(numProblems, numCritical) = sanityCheck();
353 if (numProblems > 0)
354 {
355 log.push_back(strpr("WARNING: %zu problems detected (%zu critical).\n",
356 numProblems, numCritical));
357 }
358
359 log.push_back(strpr("Found %zu lines with keywords.\n", contents.size()));
360
361 return numCritical;
362}
std::pair< std::size_t, std::size_t > sanityCheck()
Check if all keywords are in known-keywords database and for duplicates.
Definition: Settings.cpp:364
string reduce(string const &line, string const &whitespace, string const &fill)
Replace multiple whitespaces with fill.
Definition: utility.cpp:60

References contents, lines, log, nnp::reduce(), sanityCheck(), and nnp::strpr().

Referenced by loadFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sanityCheck()

pair< size_t, size_t > Settings::sanityCheck ( )
private

Check if all keywords are in known-keywords database and for duplicates.

Returns
Number of detected/critical problems.

Definition at line 364 of file Settings.cpp.

365{
366 size_t countProblems = 0;
367 size_t countCritical = 0;
368
369 // check for unknown keywords
370 for (multimap<string, pair<string, size_t> >::const_iterator
371 it = contents.begin(); it != contents.end(); ++it)
372 {
373 if (knownKeywords.find((*it).first) == knownKeywords.end())
374 {
375 countProblems++;
376 log.push_back(strpr(
377 "WARNING: Unknown keyword \"%s\" at line %zu.\n",
378 (*it).first.c_str(),
379 (*it).second.second + 1));
380 }
381 }
382
383 // check for multiple instances of known keywords (with exceptions)
384 for (KeywordList::const_iterator it = knownKeywords.begin();
385 it != knownKeywords.end(); ++it)
386 {
387 if (contents.count((*it).first) > 1
388 && (*it).first != "symfunction_short"
389 && (*it).first != "atom_energy"
390 && (*it).first != "element_nodes_short")
391 {
392 countProblems++;
393 countCritical++;
394 log.push_back(strpr(
395 "WARNING (CRITICAL): Multiple instances of \"%s\" detected.\n",
396 (*it).first.c_str()));
397 }
398 }
399
400 // Check for usage of multiple keyword versions.
401 for (KeywordList::const_iterator it = knownKeywords.begin();
402 it != knownKeywords.end(); ++it)
403 {
404 if (it->second->isUnique()) continue;
405 vector<string> duplicates;
406 for (auto keyword : it->second->words)
407 {
408 if (contents.find(keyword) != contents.end())
409 {
410 duplicates.push_back(keyword);
411 }
412 }
413 if (duplicates.size() > 1)
414 {
415 countProblems++;
416 countCritical++;
417 log.push_back(strpr(
418 "WARNING (CRITICAL): Multiple alternative versions of keyword "
419 "\"%s\" detected:.\n", (*it).first.c_str()));
420 for (auto d : duplicates)
421 {
422 log.push_back(strpr(
423 " - \"%s\"\n", d.c_str()));
424 }
425 }
426 }
427
428 return make_pair(countProblems, countCritical);
429}
double d
Definition: nnp-cutoff.cpp:34

References contents, d, knownKeywords, log, and nnp::strpr().

Referenced by parseLines().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ lines

std::vector<std::string> nnp::Settings::lines
private

Vector of all lines in settings file.

Definition at line 146 of file Settings.h.

Referenced by getSettingsLines(), parseLines(), readFile(), and writeSettingsFile().

◆ log

std::vector<std::string> nnp::Settings::log
private

Vector with log lines.

Definition at line 148 of file Settings.h.

Referenced by info(), parseLines(), readFile(), and sanityCheck().

◆ contents

KeyMap nnp::Settings::contents
private

Map containing all keyword-value pairs.

Definition at line 150 of file Settings.h.

Referenced by getValue(), getValues(), keywordCheck(), keywordExists(), parseLines(), and sanityCheck().

◆ knownKeywords

Settings::KeywordList Settings::knownKeywords = createKnownKeywordsMap()
staticprivate

Map containing all known keywords and a description.

Definition at line 152 of file Settings.h.

Referenced by keywordCheck(), keywordExists(), and sanityCheck().

◆ fileName

std::string nnp::Settings::fileName
private

Settings file name.

Definition at line 154 of file Settings.h.

Referenced by loadFile(), and readFile().


The documentation for this class was generated from the following files: