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

Contains element map. More...

#include <ElementMap.h>

Collaboration diagram for nnp::ElementMap:

Public Member Functions

std::string operator[] (std::size_t const index) const
 Overload [] operator for index search. More...
 
std::size_t operator[] (std::string const symbol) const
 Overload [] operator for symbol search. More...
 
std::size_t size () const
 Get element map size. More...
 
std::string getElementsString () const
 Get sorted list of elements in one string (space separated). More...
 
std::size_t index (std::string const &symbol) const
 Get index of given element. More...
 
std::string symbol (std::size_t const index) const
 Get element symbol for given element index. More...
 
std::size_t atomicNumber (std::size_t index) const
 Get atomic number from element index. More...
 
std::size_t registerElements (std::string const &elementLine)
 Extract all elements and store in element map. More...
 
void deregisterElements ()
 Clear element map. More...
 
std::vector< std::string > info () const
 Get map information as a vector of strings. More...
 

Static Public Member Functions

static std::string symbolFromAtomicNumber (std::size_t const atomicNumber)
 Get element symbol from atomic number. More...
 
static std::size_t atomicNumber (std::string const &symbol)
 Get atomic number from element string. More...
 

Static Private Member Functions

static bool compareAtomicNumber (std::string const &symbol1, std::string const &symbol2)
 Check if arguments are sorted according to atomic number. More...
 

Private Attributes

std::map< std::string, std::size_t > forwardMap
 Map of elements present and corresponding index number. More...
 
std::map< std::size_t, std::string > reverseMap
 Reverse element map. More...
 

Static Private Attributes

static std::string const knownElements []
 List of element symbols (e.g. "He" for Helium). More...
 

Detailed Description

Contains element map.

Definition at line 29 of file ElementMap.h.

Member Function Documentation

◆ operator[]() [1/2]

std::string nnp::ElementMap::operator[] ( std::size_t const  index) const
inline

Overload [] operator for index search.

Parameters
[in]indexElement index.
Returns
Symbol of element in map.

Definition at line 130 of file ElementMap.h.

131{
132 return symbol(index);
133}
std::size_t index(std::string const &symbol) const
Get index of given element.
Definition: ElementMap.cpp:71
std::string symbol(std::size_t const index) const
Get element symbol for given element index.
Definition: ElementMap.cpp:76

References index(), and symbol().

Here is the call graph for this function:

◆ operator[]() [2/2]

std::size_t nnp::ElementMap::operator[] ( std::string const  symbol) const
inline

Overload [] operator for symbol search.

Parameters
[in]symbolElement symbol.
Returns
Index of element in map.

Definition at line 135 of file ElementMap.h.

136{
137 return index(symbol);
138}

References index(), and symbol().

Here is the call graph for this function:

◆ size()

◆ getElementsString()

string ElementMap::getElementsString ( ) const

Get sorted list of elements in one string (space separated).

Returns
Element string.

Definition at line 56 of file ElementMap.cpp.

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}
std::size_t size() const
Get element map size.
Definition: ElementMap.h:140
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90

References size(), nnp::strpr(), and symbol().

Referenced by nnp::Structure::writeToFilePoscar().

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

◆ index()

size_t ElementMap::index ( std::string const &  symbol) const

Get index of given element.

Parameters
[in]symbolElement symbol.
Returns
Element index.

Definition at line 71 of file ElementMap.cpp.

72{
73 return safeFind(forwardMap, symbol);
74}
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:144

References forwardMap, nnp::safeFind(), and symbol().

Referenced by atomicNumber(), operator[](), and symbol().

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

◆ symbol()

string ElementMap::symbol ( std::size_t const  index) const

Get element symbol for given element index.

Parameters
[in]indexElement index.
Returns
Element symbol.

Definition at line 76 of file ElementMap.cpp.

77{
78 return safeFind(reverseMap, index);
79}
std::map< std::size_t, std::string > reverseMap
Reverse element map.
Definition: ElementMap.h:109

References index(), reverseMap, and nnp::safeFind().

Referenced by atomicNumber(), getElementsString(), index(), and operator[]().

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

◆ atomicNumber() [1/2]

◆ registerElements()

size_t ElementMap::registerElements ( std::string const &  elementLine)

Extract all elements and store in element map.

Parameters
[in]elementLineString containing all elements, e.g. "Cd S Zn", separated by whitespaces.
Returns
Number of registered elements.

Sorts all elements according to their atomic number and populates forwardMap and reverseMap with the symbol and the corresponding index number.

Definition at line 36 of file ElementMap.cpp.

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}
static bool compareAtomicNumber(std::string const &symbol1, std::string const &symbol2)
Check if arguments are sorted according to atomic number.
Definition: ElementMap.h:150
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

References compareAtomicNumber(), forwardMap, nnp::reduce(), reverseMap, and nnp::split().

Referenced by main(), LAMMPS_NS::PairHDNNPExternal::settings(), and nnp::Mode::setupElementMap().

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

◆ deregisterElements()

void ElementMap::deregisterElements ( )

Clear element map.

Definition at line 81 of file ElementMap.cpp.

82{
83 forwardMap.clear();
84 reverseMap.clear();
85
86 return;
87}

References forwardMap, and reverseMap.

◆ symbolFromAtomicNumber()

string ElementMap::symbolFromAtomicNumber ( std::size_t const  atomicNumber)
static

Get element symbol from atomic number.

Parameters
[in]atomicNumberAtomic number of element.
Returns
Element symbol.

Definition at line 106 of file ElementMap.cpp.

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}
static std::string const knownElements[]
List of element symbols (e.g. "He" for Helium).
Definition: ElementMap.h:111

References atomicNumber(), knownElements, and nnp::strpr().

Here is the call graph for this function:

◆ atomicNumber() [2/2]

size_t ElementMap::atomicNumber ( std::string const &  symbol)
static

Get atomic number from element string.

Parameters
[in]symbolElement symbol (e.g. "He" for Helium).
Returns
Atomic number (proton number Z) of element.

Definition at line 89 of file ElementMap.cpp.

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}

References knownElements, and symbol().

Here is the call graph for this function:

◆ info()

vector< string > ElementMap::info ( ) const

Get map information as a vector of strings.

Returns
Lines with map information.

Definition at line 125 of file ElementMap.cpp.

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}

References forwardMap, reverseMap, and nnp::strpr().

Referenced by nnp::Structure::info().

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

◆ compareAtomicNumber()

bool nnp::ElementMap::compareAtomicNumber ( std::string const &  symbol1,
std::string const &  symbol2 
)
inlinestaticprivate

Check if arguments are sorted according to atomic number.

Parameters
[in]symbol1Element symbol 1.
[in]symbol2Element symbol 2.
Returns
true if atomic number of element 1 is smaller than atomic number of element 2, false otherwise.

Used for sorting in registerElements().

Definition at line 150 of file ElementMap.h.

152{
153 return atomicNumber(symbol1) < atomicNumber(symbol2);
154}

References atomicNumber().

Referenced by registerElements().

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

Member Data Documentation

◆ forwardMap

std::map<std::string, std::size_t> nnp::ElementMap::forwardMap
private

Map of elements present and corresponding index number.

Definition at line 107 of file ElementMap.h.

Referenced by deregisterElements(), index(), info(), registerElements(), and size().

◆ reverseMap

std::map<std::size_t, std::string> nnp::ElementMap::reverseMap
private

Reverse element map.

Definition at line 109 of file ElementMap.h.

Referenced by deregisterElements(), info(), registerElements(), and symbol().

◆ knownElements

string const ElementMap::knownElements
staticprivate
Initial value:
= {
"H" , "He", "Li", "Be", "B" , "C" , "N" , "O" , "F" , "Ne", "Na", "Mg", "Al",
"Si", "P" , "S" , "Cl", "Ar", "K" , "Ca", "Sc", "Ti", "V" , "Cr", "Mn", "Fe",
"Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y" ,
"Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te",
"I" , "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb",
"Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W" , "Re", "Os", "Ir", "Pt",
"Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa",
"U" , "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No"
}

List of element symbols (e.g. "He" for Helium).

Definition at line 111 of file ElementMap.h.

Referenced by atomicNumber(), and symbolFromAtomicNumber().


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