n2p2 - A neural network potential package
SymFncBaseExpAng.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 "SymFncBaseExpAng.h"
18#include "Atom.h"
19#include "ElementMap.h"
20#include "utility.h"
21#include "Vec3D.h"
22#include <cstdlib> // atof, atoi
23#include <cmath> // exp, pow, cos
24#include <limits> // std::numeric_limits
25#include <stdexcept> // std::runtime_error
26
27using namespace std;
28using namespace nnp;
29
30SymFncBaseExpAng::SymFncBaseExpAng(size_t type, ElementMap const& elementMap) :
31 SymFncBaseCutoff(type, elementMap),
32 useIntegerPow(false),
33 e1 (0 ),
34 e2 (0 ),
35 zetaInt (0 ),
36 lambda (0.0 ),
37 eta (0.0 ),
38 zeta (0.0 ),
39 rs (0.0 )
40{
41 minNeighbors = 2;
42 parameters.insert("e1");
43 parameters.insert("e2");
44 parameters.insert("eta");
45 parameters.insert("zeta");
46 parameters.insert("lambda");
47 parameters.insert("rs/rl");
48}
49
50void SymFncBaseExpAng::setParameters(string const& parameterString)
51{
52 vector<string> splitLine = split(reduce(parameterString));
53
54 if (type != (size_t)atoi(splitLine.at(1).c_str()))
55 {
56 throw runtime_error("ERROR: Incorrect symmetry function type.\n");
57 }
58
59 ec = elementMap[splitLine.at(0)];
60 e1 = elementMap[splitLine.at(2)];
61 e2 = elementMap[splitLine.at(3)];
62 eta = atof(splitLine.at(4).c_str());
63 lambda = atof(splitLine.at(5).c_str());
64 zeta = atof(splitLine.at(6).c_str());
65 rc = atof(splitLine.at(7).c_str());
66 // Shift parameter is optional.
67 if (splitLine.size() > 8)
68 {
69 rs = atof(splitLine.at(8).c_str());
70 }
71
74
75 if (e1 > e2)
76 {
77 size_t tmp = e1;
78 e1 = e2;
79 e2 = tmp;
80 }
81
82 zetaInt = round(zeta);
83 if (fabs(zeta - zetaInt) <= numeric_limits<double>::min())
84 {
85 useIntegerPow = true;
86 }
87 else
88 {
89 useIntegerPow = false;
90 }
91
92 return;
93}
94
96{
97 this->convLength = convLength;
99 rc *= convLength;
100 rs *= convLength;
101
104
105 return;
106}
107
109{
110 string s = strpr("symfunction_short %2s %2zu %2s %2s %16.8E %16.8E "
111 "%16.8E %16.8E %16.8E\n",
112 elementMap[ec].c_str(),
113 type,
114 elementMap[e1].c_str(),
115 elementMap[e2].c_str(),
117 lambda,
118 zeta,
119 rc / convLength,
120 rs / convLength);
121
122 return s;
123}
124
126{
127 return strpr(getPrintFormat().c_str(),
128 index + 1,
129 elementMap[ec].c_str(),
130 type,
131 subtype.c_str(),
132 elementMap[e1].c_str(),
133 elementMap[e2].c_str(),
135 rs / convLength,
136 rc / convLength,
137 lambda,
138 zeta,
140 lineNumber + 1);
141}
142
144{
145 vector<string> v = SymFncBaseCutoff::parameterInfo();
146 string s;
147 size_t w = sfinfoWidth;
148
149 s = "e1";
150 v.push_back(strpr((pad(s, w) + "%s" ).c_str(), elementMap[e1].c_str()));
151 s = "e2";
152 v.push_back(strpr((pad(s, w) + "%s" ).c_str(), elementMap[e2].c_str()));
153 s = "eta";
154 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(),
156 s = "lambda";
157 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(), lambda));
158 s = "zeta";
159 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(), zeta));
160 s = "rs";
161 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(), rs / convLength));
162
163 return v;
164}
165
166double SymFncBaseExpAng::calculateRadialPart(double distance) const
167{
168 double const& r = distance * convLength;
169 double const p = exp(-eta * (r - rs) * (r - rs)) * fc.f(r);
170
171 return p;
172}
173
175{
176 return 2.0 * pow((1.0 + lambda * cos(angle)) / 2.0, zeta);
177}
178
180{
181 if (index == e1 || index == e2) return true;
182 else return false;
183}
184
185#ifndef N2P2_NO_SF_CACHE
187{
188 vector<string> v;
189 string s("");
190
191 s += subtype;
192 s += " ";
193 s += strpr("alpha = %16.8E", cutoffAlpha);
194 s += " ";
195 s += strpr("rc = %16.8E", rc / convLength);
196
197 v.push_back(strpr("%zu f ", e1) + s);
198 v.push_back(strpr("%zu df ", e1) + s);
199 if (e1 != e2) v.push_back(strpr("%zu f ", e2) + s);
200 if (e1 != e2) v.push_back(strpr("%zu df ", e2) + s);
201
202 return v;
203}
204#endif
double f(double r) const
Cutoff function .
void setCutoffParameter(double const alpha)
Set parameter for polynomial cutoff function (CT_POLY).
void setCutoffRadius(double const cutoffRadius)
Set cutoff radius.
Contains element map.
Definition: ElementMap.h:30
Intermediate class for SFs based on cutoff functions.
std::string subtype
Subtype string (specifies cutoff type).
CutoffFunction fc
Cutoff function used by this symmetry function.
virtual std::vector< std::string > parameterInfo() const
Get description with parameter names and values.
double cutoffAlpha
Cutoff parameter .
virtual std::vector< std::string > getCacheIdentifiers() const
Get unique cache identifiers.
virtual double calculateAngularPart(double angle) const
Calculate (partial) symmetry function value for one given angle.
double lambda
Cosine shift factor.
double zeta
Exponent of cosine term.
virtual double calculateRadialPart(double distance) const
Calculate (partial) symmetry function value for one given distance.
double eta
Width of gaussian.
virtual void changeLengthUnit(double convLength)
Change length unit.
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.
std::size_t e1
Element index of neighbor atom 1.
std::size_t e2
Element index of neighbor atom 2.
virtual bool checkRelevantElement(std::size_t index) const
Check whether symmetry function is relevant for given element.
virtual std::string parameterLine() const
Give symmetry function parameters in one line.
int zetaInt
Integer version of .
double rs
Shift of gaussian.
virtual std::string getSettingsLine() const
Get settings file line from currently set parameters.
bool useIntegerPow
Whether to use integer version of power function (faster).
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
size_t p
Definition: nnp-cutoff.cpp:33