n2p2 - A neural network potential package
SymFncBaseCompAngWeighted.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
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
31SymFncBaseCompAngWeighted::
32SymFncBaseCompAngWeighted(size_t type, ElementMap const& elementMap) :
33 SymFncBaseComp(type, elementMap),
34 angleLeft (0.0),
35 angleRight (0.0),
36 angleLeftRadians (0.0),
37 angleRightRadians(0.0)
38{
39 minNeighbors = 2;
40 parameters.insert("angleLeft");
41 parameters.insert("angleRight");
42}
43
44void SymFncBaseCompAngWeighted::setParameters(string const& parameterString)
45{
46 vector<string> splitLine = split(reduce(parameterString));
47
48 if (type != (size_t)atoi(splitLine.at(1).c_str()))
49 {
50 throw runtime_error("ERROR: Incorrect symmetry function type.\n");
51 }
52
53 ec = elementMap[splitLine.at(0)];
54 rl = atof(splitLine.at(2).c_str());
55 rc = atof(splitLine.at(3).c_str());
56 angleLeft = atof(splitLine.at(4).c_str());
57 angleRight = atof(splitLine.at(5).c_str());
58 subtype = splitLine.at(6);
59
60 // Radial part.
61 if (rl > rc)
62 {
63 throw runtime_error("ERROR: Lower radial boundary >= upper "
64 "radial boundary.\n");
65 }
66 //if (rl < 0.0 && abs(rl + rc) > numeric_limits<double>::epsilon())
67 //{
68 // throw runtime_error("ERROR: Radial function not symmetric "
69 // "w.r.t. origin.\n");
70 //}
71
74
75 // Angular part.
76 if (angleLeft >= angleRight)
77 {
78 throw runtime_error("ERROR: Left angle boundary right of or equal to "
79 "right angle boundary.\n");
80 }
81
82 double const center = (angleLeft + angleRight) / 2.0;
83 if ( (angleLeft < 0.0 && center != 0.0) ||
84 (angleRight > 180.0 && center != 180.0) )
85 {
86 throw runtime_error("ERROR: Angle boundary out of [0,180] and "
87 "center of angular function /= 0 or /= 180.\n");
88 }
89 if (angleRight - angleLeft > 360.0)
90 {
91 throw runtime_error("ERROR: Periodic symmetry function cannot spread "
92 "over domain > 360 degrees\n");
93 }
94
96 angleLeftRadians = angleLeft * M_PI / 180.0;
97 angleRightRadians = angleRight * M_PI / 180.0;
99
100 return;
101}
102
104{
105 this->convLength = convLength;
106 rc *= convLength;
107 rl *= convLength;
108
110
111 return;
112}
113
115{
116 string s = strpr("symfunction_short %2s %2zu %16.8E %16.8E "
117 "%16.8E %16.8E %s\n",
118 elementMap[ec].c_str(),
119 type,
120 rl / convLength,
121 rc / convLength,
122 angleLeft,
124 subtype.c_str());
125
126 return s;
127}
128
130{
131 return strpr(getPrintFormat().c_str(),
132 index + 1,
133 elementMap[ec].c_str(),
134 type,
135 subtype.c_str(),
136 rl / convLength,
137 rc / convLength,
138 angleLeft,
140 lineNumber + 1);
141}
142
144{
145 vector<string> v = SymFncBaseComp::parameterInfo();
146 string s;
147 size_t w = sfinfoWidth;
148
149 s = "angleLeft";
150 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(), angleLeft));
151 s = "angleRight";
152 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(), angleRight));
153
154 return v;
155}
156
158{
159 double const& r = distance * convLength;
160
161 return cr.f(r);
162}
163
165{
166 return ca.f(angle);
167}
168
170{
171 return true;
172}
173
174#ifndef N2P2_NO_SF_CACHE
176{
177 vector<string> v;
178 string s("");
179
180 s += subtype;
181 s += " ";
182 s += strpr("rl = %16.8E", rl / convLength);
183 s += " ";
184 s += strpr("rc = %16.8E", rc / convLength);
185
186 for (size_t i = 0; i < elementMap.size(); ++i)
187 {
188 v.push_back(strpr("%zu f ", i) + s);
189 v.push_back(strpr("%zu df ", i) + s);
190 }
191
192 return v;
193}
194#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
std::size_t size() const
Get element map size.
Definition: ElementMap.h:140
virtual double calculateAngularPart(double angle) const
Calculate (partial) symmetry function value for one given angle.
CompactFunction ca
Compact function member for angular part.
virtual void changeLengthUnit(double convLength)
Change length unit.
virtual std::string parameterLine() const
Give symmetry function parameters in one line.
double angleLeft
Left angle boundary.
virtual std::vector< std::string > parameterInfo() const
Get description with parameter names and values.
double angleLeftRadians
Left angle boundary in radians.
double angleRight
Right angle boundary.
virtual std::string getSettingsLine() const
Get settings file line from currently set parameters.
virtual bool checkRelevantElement(std::size_t index) const
Check whether symmetry function is relevant for given element.
virtual double calculateRadialPart(double distance) const
Calculate (partial) symmetry function value for one given distance.
virtual std::vector< std::string > getCacheIdentifiers() const
Get unique cache identifiers.
virtual void setParameters(std::string const &parameterString)
Set symmetry function parameters.
double angleRightRadians
Right angle boundary in radians.
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:28
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