n2p2 - A neural network potential package
ElementMap.cpp
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#include "ElementMap.h"
18#include "utility.h"
19#include <algorithm> // std::sort
20#include <stdexcept> // std::runtime_error
21
22using namespace std;
23using namespace nnp;
24
25string const ElementMap::knownElements[] = {
26"H" , "He", "Li", "Be", "B" , "C" , "N" , "O" , "F" , "Ne", "Na", "Mg", "Al",
27"Si", "P" , "S" , "Cl", "Ar", "K" , "Ca", "Sc", "Ti", "V" , "Cr", "Mn", "Fe",
28"Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y" ,
29"Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te",
30"I" , "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb",
31"Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W" , "Re", "Os", "Ir", "Pt",
32"Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa",
33"U" , "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No"
34};
35
36size_t ElementMap::registerElements(string const& elementLine)
37{
38 vector<string> elements = split(reduce(elementLine));
39
40 sort(elements.begin(), elements.end(), compareAtomicNumber);
41
42 if (!forwardMap.empty())
43 {
44 throw runtime_error("ERROR: Element map not empty.\n");
45 }
46
47 for (size_t i = 0; i < elements.size(); i++)
48 {
49 forwardMap[elements[i]] = i;
50 reverseMap[i] = elements[i];
51 }
52
53 return forwardMap.size();
54}
55
57{
58 string elements = "";
59
60 if (size() == 0) return elements;
61 elements += symbol(0);
62 if (size() == 1) return elements;
63 for (size_t i = 1; i < size(); ++i)
64 {
65 elements += strpr(" %s", symbol(i).c_str());
66 }
67
68 return elements;
69}
70
71size_t ElementMap::index(string const& symbol) const
72{
73 return safeFind(forwardMap, symbol);
74}
75
76string ElementMap::symbol(size_t const index) const
77{
78 return safeFind(reverseMap, index);
79}
80
82{
83 forwardMap.clear();
84 reverseMap.clear();
85
86 return;
87}
88
89size_t ElementMap::atomicNumber(string const& symbol)
90{
91 size_t numKnownElements = sizeof(knownElements) / sizeof(*knownElements);
92
93 for (size_t i = 0; i < numKnownElements; i++)
94 {
95 if (knownElements[i] == symbol)
96 {
97 return i + 1;
98 }
99 }
100
101 throw runtime_error("ERROR: Element \"" + symbol + "\" unknown.\n");
102
103 return 0;
104}
105
106string ElementMap::symbolFromAtomicNumber(size_t atomicNumber)
107{
108 size_t numKnownElements = sizeof(knownElements) / sizeof(*knownElements);
109
110 if (atomicNumber >= numKnownElements)
111 {
112 throw runtime_error(strpr("ERROR: Only the first %zu elements are "
113 "known to this library.\n",
114 numKnownElements));
115 }
116 else if (atomicNumber == 0)
117 {
118 throw runtime_error("ERROR: Invalid atomic number.\n");
119 }
120
121 atomicNumber--;
123}
124
125vector<string> ElementMap::info() const
126{
127 vector<string> v;
128
129 v.push_back(strpr("********************************\n"));
130 v.push_back(strpr("ELEMENT MAP \n"));
131 v.push_back(strpr("********************************\n"));
132 v.push_back(strpr("--------------------------------\n"));
133 v.push_back(strpr("forwardMap [*] : %d\n", forwardMap.size()));
134 v.push_back(strpr("--------------------------------\n"));
135 for (map<string, size_t>::const_iterator it = forwardMap.begin();
136 it != forwardMap.end(); ++it)
137 {
138 v.push_back(strpr("%29s : %d\n", it->first.c_str(), it->second));
139 }
140 v.push_back(strpr("--------------------------------\n"));
141 v.push_back(strpr("--------------------------------\n"));
142 v.push_back(strpr("reverseMap [*] : %d\n", reverseMap.size()));
143 v.push_back(strpr("--------------------------------\n"));
144 for (map<size_t, string>::const_iterator it = reverseMap.begin();
145 it != reverseMap.end(); ++it)
146 {
147 v.push_back(strpr("%29d : %s\n", it->first, it->second.c_str()));
148 }
149 v.push_back(strpr("--------------------------------\n"));
150 v.push_back(strpr("********************************\n"));
151
152 return v;
153}
std::size_t index(std::string const &symbol) const
Get index of given element.
Definition: ElementMap.cpp:71
std::map< std::string, std::size_t > forwardMap
Map of elements present and corresponding index number.
Definition: ElementMap.h:107
std::map< std::size_t, std::string > reverseMap
Reverse element map.
Definition: ElementMap.h:109
std::vector< std::string > info() const
Get map information as a vector of strings.
Definition: ElementMap.cpp:125
std::size_t size() const
Get element map size.
Definition: ElementMap.h:140
static std::string symbolFromAtomicNumber(std::size_t const atomicNumber)
Get element symbol from atomic number.
Definition: ElementMap.cpp:106
void deregisterElements()
Clear element map.
Definition: ElementMap.cpp:81
std::string getElementsString() const
Get sorted list of elements in one string (space separated).
Definition: ElementMap.cpp:56
static bool compareAtomicNumber(std::string const &symbol1, std::string const &symbol2)
Check if arguments are sorted according to atomic number.
Definition: ElementMap.h:150
std::size_t atomicNumber(std::size_t index) const
Get atomic number from element index.
Definition: ElementMap.h:145
static std::string const knownElements[]
List of element symbols (e.g. "He" for Helium).
Definition: ElementMap.h:111
std::string symbol(std::size_t const index) const
Get element symbol for given element index.
Definition: ElementMap.cpp:76
Definition: Atom.h:28
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
vector< string > split(string const &input, char delimiter)
Split string at each delimiter.
Definition: utility.cpp:33
string reduce(string const &line, string const &whitespace, string const &fill)
Replace multiple whitespaces with fill.
Definition: utility.cpp:60
V const & safeFind(std::map< K, V > const &stdMap, typename std::map< K, V >::key_type const &key)
Safely access map entry.
Definition: utility.h:135