n2p2 - A neural network potential package
SymFncCompAngw.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 "SymFncCompAngw.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
31SymFncCompAngw::
32SymFncCompAngw(ElementMap const& elementMap) :
33 SymFncBaseCompAng(22, elementMap)
34{
35}
36
37bool SymFncCompAngw::operator==(SymFnc const& rhs) const
38{
39 if (ec != rhs.getEc() ) return false;
40 if (type != rhs.getType()) return false;
41 SymFncCompAngw const& c = dynamic_cast<SymFncCompAngw const&>(rhs);
42 if (subtype != c.getSubtype()) return false;
43 if (e1 != c.e1 ) return false;
44 if (e2 != c.e2 ) return false;
45 if (rc != c.rc ) return false;
46 if (rl != c.rl ) return false;
47 if (angleLeft != c.angleLeft ) return false;
48 if (angleRight != c.angleRight ) return false;
49 return true;
50}
51
52bool SymFncCompAngw::operator<(SymFnc const& rhs) const
53{
54 if (ec < rhs.getEc() ) return true;
55 else if (ec > rhs.getEc() ) return false;
56 if (type < rhs.getType()) return true;
57 else if (type > rhs.getType()) return false;
58 SymFncCompAngw const& c = dynamic_cast<SymFncCompAngw const&>(rhs);
59 if (subtype < c.getSubtype()) return true;
60 else if (subtype > c.getSubtype()) return false;
61 if (e1 < c.e1 ) return true;
62 else if (e1 > c.e1 ) return false;
63 if (e2 < c.e2 ) return true;
64 else if (e2 > c.e2 ) return false;
65 if (rc < c.rc ) return true;
66 else if (rc > c.rc ) return false;
67 if (rl < c.rl ) return true;
68 else if (rl > c.rl ) return false;
69 if (angleLeft < c.angleLeft ) return true;
70 else if (angleLeft > c.angleLeft ) return false;
71 if (angleRight < c.angleRight ) return true;
72 else if (angleRight > c.angleRight ) return false;
73 return false;
74}
75
76void SymFncCompAngw::calculate(Atom& atom, bool const derivatives) const
77{
78 double result = 0.0;
79
80 size_t numNeighbors = atom.numNeighbors;
81 // Prevent problematic condition in loop test below (j < numNeighbors - 1).
82 if (numNeighbors == 0) numNeighbors = 1;
83
84 for (size_t j = 0; j < numNeighbors - 1; j++)
85 {
86 Atom::Neighbor& nj = atom.neighbors[j];
87 size_t const nej = nj.element;
88 double const rij = nj.d;
89 if ((e1 == nej || e2 == nej) && rij < rc && rij > rl)
90 {
91 double radij;
92 double dradij;
93#ifndef N2P2_NO_SF_CACHE
94 if (cacheIndices[nej].size() == 0) cr.fdf(rij, radij, dradij);
95 else
96 {
97 double& crad = nj.cache[cacheIndices[nej][0]];
98 double& cdrad = nj.cache[cacheIndices[nej][1]];
99 if (crad < 0) cr.fdf(rij, crad, cdrad);
100 radij = crad;
101 dradij = cdrad;
102 }
103#else
104 cr.fdf(rij, radij, dradij);
105#endif
106 for (size_t k = j + 1; k < numNeighbors; k++)
107 {
108 Atom::Neighbor& nk = atom.neighbors[k];
109 size_t const nek = nk.element;
110 if ((e1 == nej && e2 == nek) ||
111 (e2 == nej && e1 == nek))
112 {
113 double const rik = nk.d;
114 if (rik < rc && rik > rl)
115 {
116 // Energy calculation.
117 double radik;
118 double dradik;
119#ifndef N2P2_NO_SF_CACHE
120 if (cacheIndices[nek].size() == 0)
121 {
122 cr.fdf(rik, radik, dradik);
123 }
124 else
125 {
126 double& crad = nk.cache[cacheIndices[nek][0]];
127 double& cdrad = nk.cache[cacheIndices[nek][1]];
128 if (crad < 0) cr.fdf(rik, crad, cdrad);
129 radik = crad;
130 dradik = cdrad;
131 }
132#else
133 cr.fdf(rik, radik, dradik);
134#endif
135
136 Vec3D drij = nj.dr;
137 Vec3D drik = nk.dr;
138 Vec3D drjk = drik - drij;
139 double costijk = drij * drik;
140 double rinvijik = 1.0 / rij / rik;
141 costijk *= rinvijik;
142
143 // By definition, our polynomial is zero at 0 and 180 deg.
144 // Therefore, skip the whole rest which might yield some NaN
145 if (costijk <= -1.0 || costijk >= 1.0) continue;
146
147 // Regroup later: Get acos(cos)
148 double const acostijk = acos(costijk);
149 // Only go on if we are within our compact support
150 if (acostijk < angleLeftRadians ||
151 acostijk > angleRightRadians) continue;
152 double ang = 0.0;
153 double dang = 0.0;
154 ca.fdf(acostijk, ang, dang);
155
156 double const rad = radij * radik; // product of cutoff fcts
157 result += rad * ang;
158
159 // Force calculation.
160 if (!derivatives) continue;
161
162 double const dacostijk = -1.0
163 / sqrt(1.0 - costijk * costijk);
164 dang *= dacostijk;
165
166 double const rinvij = rinvijik * rik;
167 double const rinvik = rinvijik * rij;
168 double phiijik = rinvij * (rinvik - rinvij * costijk);
169 double phiikij = rinvik * (rinvij - rinvik * costijk);
170 double psiijik = rinvijik; // careful: sign flip w.r.t. notes due to nj.dGd...
171 phiijik *= dang;
172 phiikij *= dang;
173 psiijik *= dang;
174
175 // Cutoff function might be a divide by zero, but we screen that case before
176 double const chiij = rinvij * radik * dradij;
177 double const chiik = rinvik * radij * dradik;
178
179 // rijs/rij due to the shifted radial part of the Gaussian
180 double const p1 = rad * phiijik + ang * chiij;
181 double const p2 = rad * phiikij + ang * chiik;
182 double const p3 = rad * psiijik;
183 drij *= p1 * scalingFactor;
184 drik *= p2 * scalingFactor;
185 drjk *= p3 * scalingFactor;
186
187 // Save force contributions in Atom storage.
188 atom.dGdr[index] += drij + drik;
189#ifndef N2P2_FULL_SFD_MEMORY
190 nj.dGdr[indexPerElement[nej]] -= drij + drjk;
191 nk.dGdr[indexPerElement[nek]] -= drik - drjk;
192#else
193 nj.dGdr[index] -= drij + drjk;
194 nk.dGdr[index] -= drik - drjk;
195#endif
196 } // rik <= rc
197 } // elem
198 } // k
199 } // rij <= rc
200 } // j
201
202 atom.G[index] = scale(result);
203
204 return;
205}
void fdf(double a, double &fa, double &dfa) const
Calculate compact function and derivative at once.
Contains element map.
Definition: ElementMap.h:30
Intermediate symmetry function class for angular SFs with compact support.
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.
CompactFunction ca
Compact function member for angular part.
double angleLeftRadians
Left angle boundary in radians.
std::size_t e1
Element index of neighbor atom 1.
std::string getSubtype() const
Get private subtype member variable.
double rl
Lower bound of compact function, .
CompactFunction cr
Compact function for radial part.
std::string subtype
Subtype string (specifies e.g. polynom type).
Wide angular symmetry function with compact support (type 22)
virtual void calculate(Atom &atom, bool const derivatives) const
Calculate symmetry function for one atom.
virtual bool operator<(SymFnc const &rhs) const
Overload < operator.
virtual bool operator==(SymFnc const &rhs) const
Overload == operator.
Symmetry function base class.
Definition: SymFnc.h:40
std::size_t type
Symmetry function type.
Definition: SymFnc.h:268
std::size_t index
Symmetry function index (per element).
Definition: SymFnc.h:272
double scalingFactor
Scaling factor.
Definition: SymFnc.h:294
std::size_t getType() const
Get private type member variable.
Definition: SymFnc.h:355
double rc
Cutoff radius .
Definition: SymFnc.h:292
std::vector< std::vector< std::size_t > > cacheIndices
Cache indices for each element.
Definition: SymFnc.h:306
double scale(double value) const
Apply symmetry function scaling and/or centering.
Definition: SymFnc.cpp:169
std::size_t getEc() const
Get private ec member variable.
Definition: SymFnc.h:356
std::vector< std::size_t > indexPerElement
Per-element index for derivative memory in Atom::Neighbor::dGdr arrays.
Definition: SymFnc.h:302
std::size_t ec
Element index of center atom.
Definition: SymFnc.h:276
Definition: Atom.h:29
Struct to store information on neighbor atoms.
Definition: Atom.h:36
std::vector< double > cache
Symmetry function cache (e.g. for cutoffs, compact functions).
Definition: Atom.h:49
std::size_t element
Element index of neighbor atom.
Definition: Atom.h:42
double d
Distance to neighbor atom.
Definition: Atom.h:44
Vec3D dr
Distance vector to neighbor atom.
Definition: Atom.h:46
std::vector< Vec3D > dGdr
Derivatives of symmetry functions with respect to neighbor coordinates.
Definition: Atom.h:60
Storage for a single atom.
Definition: Atom.h:33
std::vector< Neighbor > neighbors
Neighbor array (maximum number defined in macros.h.
Definition: Atom.h:170
std::vector< Vec3D > dGdr
Derivative of symmetry functions with respect to this atom's coordinates.
Definition: Atom.h:161
std::vector< double > G
Symmetry function values.
Definition: Atom.h:146
std::size_t numNeighbors
Total number of neighbors.
Definition: Atom.h:109
Vector in 3 dimensional real space.
Definition: Vec3D.h:30