n2p2 - A neural network potential package
SymFncBaseCompAng.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// Copyright (C) 2020 Martin P. Bircher
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18#include "SymFncBaseCompAng.h"
19#include "Atom.h"
20#include "ElementMap.h"
21#include "utility.h"
22#include "Vec3D.h"
23#include <cstdlib> // atof, atoi
24#include <cmath> // exp, pow, cos
25#include <limits> // std::numeric_limits
26#include <stdexcept> // std::runtime_error
27
28using namespace std;
29using namespace nnp;
30
31SymFncBaseCompAng::
32SymFncBaseCompAng(size_t type, ElementMap const& elementMap) :
33 SymFncBaseComp(type, elementMap),
34 e1 (0 ),
35 e2 (0 ),
36 angleLeft (0.0),
37 angleRight (0.0),
38 angleLeftRadians (0.0),
39 angleRightRadians(0.0)
40{
41 minNeighbors = 2;
42 parameters.insert("e1");
43 parameters.insert("e2");
44 parameters.insert("angleLeft");
45 parameters.insert("angleRight");
46}
47
48void SymFncBaseCompAng::setParameters(string const& parameterString)
49{
50 vector<string> splitLine = split(reduce(parameterString));
51
52 if (type != (size_t)atoi(splitLine.at(1).c_str()))
53 {
54 throw runtime_error("ERROR: Incorrect symmetry function type.\n");
55 }
56
57 ec = elementMap[splitLine.at(0)];
58 e1 = elementMap[splitLine.at(2)];
59 e2 = elementMap[splitLine.at(3)];
60 rl = atof(splitLine.at(4).c_str());
61 rc = atof(splitLine.at(5).c_str());
62 angleLeft = atof(splitLine.at(6).c_str());
63 angleRight = atof(splitLine.at(7).c_str());
64 subtype = splitLine.at(8);
65
66 if (e1 > e2)
67 {
68 size_t tmp = e1;
69 e1 = e2;
70 e2 = tmp;
71 }
72
73 // Radial part.
74 if (rl > rc)
75 {
76 throw runtime_error("ERROR: Lower radial boundary >= upper "
77 "radial boundary.\n");
78 }
79 //if (rl < 0.0 && abs(rl + rc) > numeric_limits<double>::epsilon())
80 //{
81 // throw runtime_error("ERROR: Radial function not symmetric "
82 // "w.r.t. origin.\n");
83 //}
84
87
88 // Angular part.
89 if (angleLeft >= angleRight)
90 {
91 throw runtime_error("ERROR: Left angle boundary right of or equal to "
92 "right angle boundary.\n");
93 }
94
95 double const center = (angleLeft + angleRight) / 2.0;
96 if ( (angleLeft < 0.0 && center != 0.0) ||
97 (angleRight > 180.0 && center != 180.0) )
98 {
99 throw runtime_error("ERROR: Angle boundary out of [0,180] and "
100 "center of angular function /= 0 or /= 180.\n");
101 }
102 if (angleRight - angleLeft > 360.0)
103 {
104 throw runtime_error("ERROR: Periodic symmetry function cannot spread "
105 "over domain > 360 degrees\n");
106 }
107
109 angleLeftRadians = angleLeft * M_PI / 180.0;
110 angleRightRadians = angleRight * M_PI / 180.0;
112
113 return;
114}
115
117{
118 this->convLength = convLength;
119 rc *= convLength;
120 rl *= convLength;
121
123
124 return;
125}
126
128{
129 string s = strpr("symfunction_short %2s %2zu %2s %2s %16.8E %16.8E "
130 "%16.8E %16.8E %s\n",
131 elementMap[ec].c_str(),
132 type,
133 elementMap[e1].c_str(),
134 elementMap[e2].c_str(),
135 rl / convLength,
136 rc / convLength,
137 angleLeft,
139 subtype.c_str());
140
141 return s;
142}
143
145{
146 return strpr(getPrintFormat().c_str(),
147 index + 1,
148 elementMap[ec].c_str(),
149 type,
150 subtype.c_str(),
151 elementMap[e1].c_str(),
152 elementMap[e2].c_str(),
153 rl / convLength,
154 rc / convLength,
155 angleLeft,
157 lineNumber + 1);
158}
159
161{
162 vector<string> v = SymFncBaseComp::parameterInfo();
163 string s;
164 size_t w = sfinfoWidth;
165
166 s = "e1";
167 v.push_back(strpr((pad(s, w) + "%s" ).c_str(), elementMap[e1].c_str()));
168 s = "e2";
169 v.push_back(strpr((pad(s, w) + "%s" ).c_str(), elementMap[e2].c_str()));
170 s = "angleLeft";
171 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(), angleLeft));
172 s = "angleRight";
173 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(), angleRight));
174
175 return v;
176}
177
178double SymFncBaseCompAng::calculateRadialPart(double distance) const
179{
180 double const& r = distance * convLength;
181
182 return cr.f(r);
183}
184
186{
187 return ca.f(angle);
188}
189
191{
192 if (index == e1 || index == e2) return true;
193 else return false;
194}
195
196#ifndef N2P2_NO_SF_CACHE
198{
199 vector<string> v;
200 string s("");
201
202 s += subtype;
203 s += " ";
204 s += strpr("rl = %16.8E", rl / convLength);
205 s += " ";
206 s += strpr("rc = %16.8E", rc / convLength);
207
208 v.push_back(strpr("%zu f ", e1) + s);
209 v.push_back(strpr("%zu df ", e1) + s);
210 if (e1 != e2) v.push_back(strpr("%zu f ", e2) + s);
211 if (e1 != e2) v.push_back(strpr("%zu df ", e2) + s);
212
213 return v;
214}
215#endif
void setCoreFunction(CoreFunction::Type const type)
Set type.
double f(double a) const
Compact function .
CoreFunction::Type getCoreFunctionType() const
Getter for #type.
void setLeftRight(double left, double right)
Set left and right boundary.
Contains element map.
Definition: ElementMap.h:30
double angleLeft
Left angle boundary.
double angleRight
Right angle boundary.
double angleRightRadians
Right angle boundary in radians.
std::size_t e2
Element index of neighbor atom 2.
virtual std::vector< std::string > getCacheIdentifiers() const
Get unique cache identifiers.
virtual void changeLengthUnit(double convLength)
Change length unit.
virtual bool checkRelevantElement(std::size_t index) const
Check whether symmetry function is relevant for given element.
CompactFunction ca
Compact function member for angular part.
virtual std::string parameterLine() const
Give symmetry function parameters in one line.
virtual double calculateAngularPart(double angle) const
Calculate (partial) symmetry function value for one given angle.
virtual void setParameters(std::string const &parameterString)
Set symmetry function parameters.
virtual std::vector< std::string > parameterInfo() const
Get description with parameter names and values.
virtual double calculateRadialPart(double distance) const
Calculate (partial) symmetry function value for one given distance.
double angleLeftRadians
Left angle boundary in radians.
virtual std::string getSettingsLine() const
Get settings file line from currently set parameters.
std::size_t e1
Element index of neighbor atom 1.
Symmetry function base class for SFs with compact support.
double rl
Lower bound of compact function, .
virtual std::vector< std::string > parameterInfo() const
Get description with parameter names and values.
CompactFunction cr
Compact function for radial part.
std::string subtype
Subtype string (specifies e.g. polynom type).
void setCompactFunction(std::string subtype)
Set radial compact function.
double convLength
Data set normalization length conversion factor.
Definition: SymFnc.h:296
std::size_t type
Symmetry function type.
Definition: SymFnc.h:268
std::set< std::string > parameters
Set with symmetry function parameter IDs (lookup for printing).
Definition: SymFnc.h:300
std::size_t index
Symmetry function index (per element).
Definition: SymFnc.h:272
double rc
Cutoff radius .
Definition: SymFnc.h:292
ElementMap elementMap
Copy of element map.
Definition: SymFnc.h:270
std::size_t ec
Element index of center atom.
Definition: SymFnc.h:276
static std::size_t const sfinfoWidth
Width of the SFINFO parameter description field (see parameterInfo()).
Definition: SymFnc.h:309
std::size_t minNeighbors
Minimum number of neighbors required.
Definition: SymFnc.h:278
std::size_t lineNumber
Line number.
Definition: SymFnc.h:274
std::string getPrintFormat() const
Generate format string for symmetry function parameter printing.
Definition: SymFnc.cpp:285
Definition: Atom.h:29
string pad(string const &input, size_t num, char fill, bool right)
Definition: utility.cpp:79
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