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

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

#include <Settings.h>

Inheritance diagram for nnp::settings::Settings:
Collaboration diagram for nnp::settings::Settings:

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 (Key const &key, bool const exact=false) const override
 
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 (Key const &key) const override
 
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...
 
virtual bool keywordExists (Key const &key, bool const exact=false) const =0
 
virtual std::string getValue (Key const &key) const =0
 

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 37 of file Settings.h.

Member Typedef Documentation

◆ KeyMap

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

Definition at line 42 of file Settings.h.

◆ KeyRange

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

Definition at line 44 of file Settings.h.

◆ KeywordList

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

Definition at line 46 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 181 of file Settings.cpp.

182{
183 return getValue(keyword);
184}
std::string getValue(Key const &key) const override
Definition: Settings.cpp:251

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 187 of file Settings.cpp.

188{
189 this->fileName = fileName;
190
191 readFile();
192 return parseLines();
193}
std::string fileName
Settings file name.
Definition: Settings.h:151
std::size_t parseLines()
Parse lines and create contents map.
Definition: Settings.cpp:323
void readFile()
Read file once and save all lines in lines vector.
Definition: Settings.cpp:276

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() [1/2]

◆ keywordExists() [2/2]

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 199 of file Settings.cpp.

200{
201 if (knownKeywords.find(keyword) == knownKeywords.end())
202 {
203 throw runtime_error("ERROR: Not in the list of allowed keyword: \"" +
204 keyword + "\".\n");
205 }
206 if (exact || knownKeywords.at(keyword)->hasUniqueKeyword())
207 {
208 return (contents.find(keyword) != contents.end());
209 }
210 else
211 {
212 for (auto alternative : *knownKeywords.at(keyword))
213 {
214 if (contents.find(alternative) != contents.end()) return true;
215 }
216 }
217
218 return false;
219}
static KeywordList knownKeywords
Map containing all known keywords and a description.
Definition: Settings.h:149
KeyMap contents
Map containing all keyword-value pairs.
Definition: Settings.h:147

References contents, and knownKeywords.

◆ 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 221 of file Settings.cpp.

222{
223 bool exists = keywordExists(keyword, false);
224 bool unique = knownKeywords.at(keyword)->hasUniqueKeyword();
225 if (!exists)
226 {
227 if (unique)
228 {
229 throw std::runtime_error("ERROR: Keyword \"" + keyword
230 + "\" not found.\n");
231 }
232 else
233 {
234 throw std::runtime_error("ERROR: Neither keyword \"" + keyword
235 + "\" nor alternative keywords found.\n");
236 }
237 }
238
239 bool exact = keywordExists(keyword, true);
240 if (!exact)
241 {
242 for (auto const& alt : *knownKeywords.at(keyword))
243 {
244 if (contents.find(alt) != contents.end()) return alt;
245 }
246 }
247
248 return keyword;
249}

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() [1/2]

string Settings::getValue ( Key const &  key) const
overridevirtual

Implements nnp::settings::ISettings.

Definition at line 251 of file Settings.cpp.

252{
253 return contents.find(key.getMainKeyword())->second.first;
254}

References contents, and nnp::settings::Key::getMainKeyword().

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

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

◆ getValue() [2/2]

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 256 of file Settings.cpp.

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

References contents, and keywordCheck().

Here is the call 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:38
std::pair< KeyMap::const_iterator, KeyMap::const_iterator > KeyRange
Definition: Settings.h:44
KeyRange getValues(std::string const &keyword) const
Get all keyword-value pairs for given keyword.
Definition: Settings.cpp:261

Definition at line 261 of file Settings.cpp.

262{
263 return contents.equal_range(keywordCheck(keyword));
264}

References contents, and keywordCheck().

Referenced by nnp::Mode::setupElectrostatics(), 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 266 of file Settings.cpp.

267{
268 return log;
269}
std::vector< std::string > log
Vector with log lines.
Definition: Settings.h:145

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 271 of file Settings.cpp.

272{
273 return lines;
274}
std::vector< std::string > lines
Vector of all lines in settings file.
Definition: Settings.h:143

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 301 of file Settings.cpp.

303{
304 if (!file->is_open())
305 {
306 runtime_error("ERROR: Could not write to file.\n");
307 }
308
309 size_t i = 0;
310 for (auto const& l : lines)
311 {
312 if (replacements.find(i) != replacements.end())
313 {
314 (*file) << replacements.at(i);
315 }
316 else (*file) << l << '\n';
317 i++;
318 }
319
320 return;
321}

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 276 of file Settings.cpp.

277{
278 ifstream file;
279 string line;
280
281 log.push_back(strpr("Settings file name: %s\n", fileName.c_str()));
282 file.open(fileName.c_str());
283 if (!file.is_open())
284 {
285 throw runtime_error("ERROR: Could not open file: \"" + fileName
286 + "\".\n");
287 }
288
289 while (getline(file, line))
290 {
291 lines.push_back(line);
292 }
293
294 file.close();
295
296 log.push_back(strpr("Read %zu lines.\n", lines.size()));
297
298 return;
299}
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 323 of file Settings.cpp.

324{
325 for (size_t i = 0; i < lines.size(); ++i)
326 {
327 string line = lines.at(i);
328
329 // ignore empty and comment lines
330 if (line.empty())
331 {
332 continue;
333 }
334 // check for empty lines in Windows format
335 if (line == "\r")
336 {
337 continue;
338 }
339 if (line.find('#') != string::npos)
340 {
341 line.erase(line.find('#'));
342 }
343 if (line.find('!') != string::npos)
344 {
345 line.erase(line.find('!'));
346 }
347 if (line.find_first_not_of(' ') == string::npos)
348 {
349 continue;
350 }
351
352 // remove leading and trailing whitespaces and trim separating spaces
353 line = reduce(line);
354
355 // find separator position
356 size_t const separatorPosition = line.find_first_of(" ");
357 string key;
358 pair<string, size_t> value;
359
360 if (separatorPosition == string::npos)
361 {
362 // first check for single keyword without value
363 key = line;
364 value = pair<string, size_t>("", i);
365 }
366 else
367 {
368 // one or more arguments
369 key = line.substr(0, separatorPosition);
370 value = pair<string, size_t>(line.erase(0, separatorPosition + 1),
371 i);
372 }
373
374 contents.insert(pair<string, pair<string, size_t> >(key, value));
375 }
376
377 size_t numProblems = 0;
378 size_t numCritical = 0;
379 tie(numProblems, numCritical) = sanityCheck();
380 if (numProblems > 0)
381 {
382 log.push_back(strpr("WARNING: %zu problems detected (%zu critical).\n",
383 numProblems, numCritical));
384 }
385
386 log.push_back(strpr("Found %zu lines with keywords.\n", contents.size()));
387
388 return numCritical;
389}
std::pair< std::size_t, std::size_t > sanityCheck()
Check if all keywords are in known-keywords database and for duplicates.
Definition: Settings.cpp:391
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 391 of file Settings.cpp.

392{
393 size_t countProblems = 0;
394 size_t countCritical = 0;
395
396 // check for unknown keywords
397 for (multimap<string, pair<string, size_t> >::const_iterator
398 it = contents.begin(); it != contents.end(); ++it)
399 {
400 if (knownKeywords.find((*it).first) == knownKeywords.end())
401 {
402 countProblems++;
403 log.push_back(strpr(
404 "WARNING: Unknown keyword \"%s\" at line %zu.\n",
405 (*it).first.c_str(),
406 (*it).second.second + 1));
407 }
408 }
409
410 // check for multiple instances of known keywords (with exceptions)
411 for (KeywordList::const_iterator it = knownKeywords.begin();
412 it != knownKeywords.end(); ++it)
413 {
414 if (contents.count((*it).first) > 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")
421 {
422 countProblems++;
423 countCritical++;
424 log.push_back(strpr(
425 "WARNING (CRITICAL): Multiple instances of \"%s\" detected.\n",
426 (*it).first.c_str()));
427 }
428 }
429
430 // Check for usage of multiple keyword versions.
431 for (KeywordList::const_iterator it = knownKeywords.begin();
432 it != knownKeywords.end(); ++it)
433 {
434 if (it->second->hasUniqueKeyword()) continue;
435 vector<string> duplicates;
436 for (auto keyword : *it->second)
437 {
438 if (contents.find(keyword) != contents.end())
439 {
440 duplicates.push_back(keyword);
441 }
442 }
443 if (duplicates.size() > 1)
444 {
445 countProblems++;
446 countCritical++;
447 log.push_back(strpr(
448 "WARNING (CRITICAL): Multiple alternative versions of keyword "
449 "\"%s\" detected:.\n", (*it).first.c_str()));
450 for (auto d : duplicates)
451 {
452 log.push_back(strpr(
453 " - \"%s\"\n", d.c_str()));
454 }
455 }
456 }
457
458 return make_pair(countProblems, countCritical);
459}
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::Settings::lines
private

Vector of all lines in settings file.

Definition at line 143 of file Settings.h.

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

◆ log

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

Vector with log lines.

Definition at line 145 of file Settings.h.

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

◆ contents

KeyMap nnp::settings::Settings::contents
private

Map containing all keyword-value pairs.

Definition at line 147 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 149 of file Settings.h.

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

◆ fileName

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

Settings file name.

Definition at line 151 of file Settings.h.

Referenced by loadFile(), and readFile().


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