n2p2 - A neural network potential package
SymFncExpAngw.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 "SymFncExpAngw.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
30SymFncExpAngw::SymFncExpAngw(ElementMap const& elementMap) :
31 SymFncBaseExpAng(9, elementMap)
32{
33}
34
35bool SymFncExpAngw::operator==(SymFnc const& rhs) const
36{
37 if (ec != rhs.getEc() ) return false;
38 if (type != rhs.getType()) return false;
39 SymFncExpAngw const& c = dynamic_cast<SymFncExpAngw const&>(rhs);
40 if (cutoffType != c.cutoffType ) return false;
41 if (cutoffAlpha != c.cutoffAlpha) return false;
42 if (rc != c.rc ) return false;
43 if (eta != c.eta ) return false;
44 if (zeta != c.zeta ) return false;
45 if (lambda != c.lambda ) return false;
46 if (e1 != c.e1 ) return false;
47 if (e2 != c.e2 ) return false;
48 return true;
49}
50
51bool SymFncExpAngw::operator<(SymFnc const& rhs) const
52{
53 if (ec < rhs.getEc() ) return true;
54 else if (ec > rhs.getEc() ) return false;
55 if (type < rhs.getType()) return true;
56 else if (type > rhs.getType()) return false;
57 SymFncExpAngw const& c = dynamic_cast<SymFncExpAngw const&>(rhs);
58 if (cutoffType < c.cutoffType ) return true;
59 else if (cutoffType > c.cutoffType ) return false;
60 if (cutoffAlpha < c.cutoffAlpha) return true;
61 else if (cutoffAlpha > c.cutoffAlpha) return false;
62 if (rc < c.rc ) return true;
63 else if (rc > c.rc ) return false;
64 if (eta < c.eta ) return true;
65 else if (eta > c.eta ) return false;
66 if (rs < c.rs ) return true;
67 else if (rs > c.rs ) return false;
68 if (zeta < c.zeta ) return true;
69 else if (zeta > c.zeta ) return false;
70 if (lambda < c.lambda ) return true;
71 else if (lambda > c.lambda ) return false;
72 if (e1 < c.e1 ) return true;
73 else if (e1 > c.e1 ) return false;
74 if (e2 < c.e2 ) return true;
75 else if (e2 > c.e2 ) return false;
76 return false;
77}
78
79void SymFncExpAngw::calculate(Atom& atom, bool const derivatives) const
80{
81 double const pnorm = pow(2.0, 1.0 - zeta);
82 double const pzl = zeta * lambda;
83 double result = 0.0;
84
85 size_t numNeighbors = atom.numNeighbors;
86 // Prevent problematic condition in loop test below (j < numNeighbors - 1).
87 if (numNeighbors == 0) numNeighbors = 1;
88
89 for (size_t j = 0; j < numNeighbors - 1; j++)
90 {
91 Atom::Neighbor& nj = atom.neighbors[j];
92 size_t const nej = nj.element;
93 double const rij = nj.d;
94 if ((e1 == nej || e2 == nej) && rij < rc)
95 {
96 double const r2ij = rij * rij;
97
98 // Calculate cutoff function and derivative.
99 double pfcij;
100 double pdfcij;
101#ifndef N2P2_NO_SF_CACHE
102 if (cacheIndices[nej].size() == 0) fc.fdf(rij, pfcij, pdfcij);
103 else
104 {
105 double& cfc = nj.cache[cacheIndices[nej][0]];
106 double& cdfc = nj.cache[cacheIndices[nej][1]];
107 if (cfc < 0) fc.fdf(rij, cfc, cdfc);
108 pfcij = cfc;
109 pdfcij = cdfc;
110 }
111#else
112 fc.fdf(rij, pfcij, pdfcij);
113#endif
114 for (size_t k = j + 1; k < numNeighbors; k++)
115 {
116 Atom::Neighbor& nk = atom.neighbors[k];
117 size_t const nek = nk.element;
118 if ((e1 == nej && e2 == nek) ||
119 (e2 == nej && e1 == nek))
120 {
121 double const rik = nk.d;
122 if (rik < rc)
123 {
124 // Energy calculation.
125 double pfcik;
126 double pdfcik;
127#ifndef N2P2_NO_SF_CACHE
128 if (cacheIndices[nej].size() == 0)
129 {
130 fc.fdf(rik, pfcik, pdfcik);
131 }
132 else
133 {
134 double& cfc = nk.cache[cacheIndices[nek][0]];
135 double& cdfc = nk.cache[cacheIndices[nek][1]];
136 if (cfc < 0) fc.fdf(rik, cfc, cdfc);
137 pfcik = cfc;
138 pdfcik = cdfc;
139 }
140#else
141 fc.fdf(rik, pfcik, pdfcik);
142#endif
143 Vec3D drij = nj.dr;
144 Vec3D drik = nk.dr;
145 Vec3D drjk = drik - drij;
146 double costijk = drij * drik;;
147 double rinvijik = 1.0 / rij / rik;
148 costijk *= rinvijik;
149
150 double const pfc = pfcij * pfcik;
151 double const r2ik = rik * rik;
152 double const rijs = rij - rs;
153 double const riks = rik - rs;
154 double const pexp = exp(-eta * (rijs * rijs
155 + riks * riks));
156 double const plambda = 1.0 + lambda * costijk;
157 double fg = pexp;
158 if (plambda <= 0.0) fg = 0.0;
159 else
160 {
161 if (useIntegerPow)
162 {
163 fg *= pow_int(plambda, zetaInt - 1);
164 }
165 else
166 {
167 fg *= pow(plambda, zeta - 1.0);
168 }
169 }
170 result += fg * plambda * pfc;
171
172 // Force calculation.
173 if (!derivatives) continue;
174 fg *= pnorm;
175 rinvijik *= pzl;
176 costijk *= pzl;
177 double const p2etapl = 2.0 * eta * plambda;
178 double const p1 = fg * (pfc * (rinvijik - costijk
179 / r2ij - p2etapl * rijs / rij) + pfcik
180 * pdfcij * plambda / rij);
181 double const p2 = fg * (pfc * (rinvijik - costijk
182 / r2ik - p2etapl * riks / rik) + pfcij
183 * pdfcik * plambda / rik);
184 double const p3 = fg * pfc * rinvijik;
185 drij *= p1 * scalingFactor;
186 drik *= p2 * scalingFactor;
187 drjk *= p3 * scalingFactor;
188
189 // Save force contributions in Atom storage.
190 atom.dGdr[index] += drij + drik;
191#ifndef N2P2_FULL_SFD_MEMORY
192 nj.dGdr[indexPerElement[nej]] -= drij + drjk;
193 nk.dGdr[indexPerElement[nek]] -= drik - drjk;
194#else
195 nj.dGdr[index] -= drij + drjk;
196 nk.dGdr[index] -= drik - drjk;
197#endif
198 } // rik <= rc
199 } // elem
200 } // k
201 } // rij <= rc
202 } // j
203 result *= pnorm;
204
205 atom.G[index] = scale(result);
206
207 return;
208}
void fdf(double r, double &fc, double &dfc) const
Calculate cutoff function and derivative .
Contains element map.
Definition: ElementMap.h:30
CutoffFunction fc
Cutoff function used by this symmetry function.
CutoffFunction::CutoffType cutoffType
Cutoff type used by this symmetry function.
double cutoffAlpha
Cutoff parameter .
Intermediate class for angular SFs based on cutoffs and exponentials.
double lambda
Cosine shift factor.
double zeta
Exponent of cosine term.
double eta
Width of gaussian.
std::size_t e1
Element index of neighbor atom 1.
std::size_t e2
Element index of neighbor atom 2.
int zetaInt
Integer version of .
double rs
Shift of gaussian.
bool useIntegerPow
Whether to use integer version of power function (faster).
Angular symmetry function (type 9)
Definition: SymFncExpAngw.h:54
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
double pow_int(double x, int n)
Integer version of power function, "fast exponentiation algorithm".
Definition: utility.cpp:292
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