-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathSymGrpExpRad.cpp
More file actions
269 lines (237 loc) · 8.59 KB
/
SymGrpExpRad.cpp
File metadata and controls
269 lines (237 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// n2p2 - A neural network potential package
// Copyright (C) 2018 Andreas Singraber (University of Vienna)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "SymGrpExpRad.h"
#include "Atom.h"
#include "SymFnc.h"
#include "SymFncExpRad.h"
#include "Vec3D.h"
#include "utility.h"
#include <algorithm> // std::sort
#include <cmath> // exp
#include <stdexcept> // std::runtime_error
using namespace std;
using namespace nnp;
SymGrpExpRad::SymGrpExpRad(ElementMap const& elementMap) :
SymGrpBaseCutoff(2, elementMap),
e1(0)
{
parametersCommon.insert("e1");
parametersMember.insert("eta");
parametersMember.insert("rs/rl");
parametersMember.insert("mindex");
parametersMember.insert("sfindex");
}
bool SymGrpExpRad::operator==(SymGrp const& rhs) const
{
if (ec != rhs.getEc() ) return false;
if (type != rhs.getType()) return false;
SymGrpExpRad const& c = dynamic_cast<SymGrpExpRad const&>(rhs);
if (cutoffType != c.cutoffType ) return false;
if (cutoffAlpha != c.cutoffAlpha) return false;
if (rc != c.rc ) return false;
if (e1 != c.e1 ) return false;
return true;
}
bool SymGrpExpRad::operator<(SymGrp const& rhs) const
{
if (ec < rhs.getEc() ) return true;
else if (ec > rhs.getEc() ) return false;
if (type < rhs.getType()) return true;
else if (type > rhs.getType()) return false;
SymGrpExpRad const& c = dynamic_cast<SymGrpExpRad const&>(rhs);
if (cutoffType < c.cutoffType ) return true;
else if (cutoffType > c.cutoffType ) return false;
if (cutoffAlpha < c.cutoffAlpha) return true;
else if (cutoffAlpha > c.cutoffAlpha) return false;
if (rc < c.rc ) return true;
else if (rc > c.rc ) return false;
if (e1 < c.e1 ) return true;
else if (e1 > c.e1 ) return false;
return false;
}
bool SymGrpExpRad::addMember(SymFnc const* const symmetryFunction)
{
if (symmetryFunction->getType() != type) return false;
SymFncExpRad const* sf =
dynamic_cast<SymFncExpRad const*>(symmetryFunction);
if (members.empty())
{
cutoffType = sf->getCutoffType();
subtype = sf->getSubtype();
cutoffAlpha = sf->getCutoffAlpha();
ec = sf->getEc();
rc = sf->getRc();
e1 = sf->getE1();
convLength = sf->getConvLength();
icut_beta = sf->geticut_beta();
icut_gamma = sf->geticut_gamma();
fc.setCutoffType(cutoffType);
fc.setCutoffRadius(rc);
fc.setCutoffParameter(cutoffAlpha);
fc.setInnerCutoffParameter(icut_beta, icut_gamma);
}
if (sf->getCutoffType() != cutoffType ) return false;
if (sf->getCutoffAlpha() != cutoffAlpha) return false;
if (sf->getEc() != ec ) return false;
if (sf->getRc() != rc ) return false;
if (sf->getE1() != e1 ) return false;
if (sf->getConvLength() != convLength )
{
throw runtime_error("ERROR: Unable to add symmetry function members "
"with different conversion factors.\n");
}
members.push_back(sf);
return true;
}
void SymGrpExpRad::sortMembers()
{
sort(members.begin(),
members.end(),
comparePointerTargets<SymFncExpRad const>);
for (size_t i = 0; i < members.size(); i++)
{
memberIndex.push_back(members[i]->getIndex());
eta.push_back(members[i]->getEta());
rs.push_back(members[i]->getRs());
memberIndexPerElement.push_back(members[i]->getIndexPerElement());
}
return;
}
void SymGrpExpRad::setScalingFactors()
{
scalingFactors.resize(members.size(), 0.0);
for (size_t i = 0; i < members.size(); i++)
{
scalingFactors.at(i) = members[i]->getScalingFactor();
}
return;
}
// Depending on chosen symmetry functions this function may be very
// time-critical when predicting new structures (e.g. in MD simulations). Thus,
// lots of optimizations were used sacrificing some readablity. Vec3D
// operations have been rewritten in simple C array style and the use of
// temporary objects has been minmized. Some of the originally coded
// expressions are kept in comments marked with "SIMPLE EXPRESSIONS:".
void SymGrpExpRad::calculate(Atom& atom, bool const derivatives) const
{
#ifndef N2P2_NO_SF_CACHE
// Can use cache indices of any member because this group is defined via
// identical symmetry function type, neighbors and cutoff functions.
auto cacheIndices = members.at(0)->getCacheIndices();
bool unique = true;
size_t c0 = 0;
size_t c1 = 0;
if (cacheIndices.at(e1).size() > 0)
{
unique = false;
c0 = cacheIndices.at(e1).at(0);
c1 = cacheIndices.at(e1).at(1);
}
#endif
double* result = new double[members.size()];
for (size_t k = 0; k < members.size(); ++k)
{
result[k] = 0.0;
}
for (size_t j = 0; j < atom.numNeighbors; ++j)
{
Atom::Neighbor& n = atom.neighbors[j];
if (e1 == n.element && n.d < rc)
{
// Energy calculation.
double const rij = n.d;
// Calculate cutoff function and derivative.
double pfc;
double pdfc;
#ifndef N2P2_NO_SF_CACHE
if (unique) fc.fdf(rij, pfc, pdfc);
else
{
double& cfc = n.cache[c0];
double& cdfc = n.cache[c1];
if (cfc < 0) fc.fdf(rij, cfc, cdfc);
pfc = cfc;
pdfc = cdfc;
}
#else
fc.fdf(rij, pfc, pdfc);
#endif
double const* const d1 = n.dr.r;
for (size_t k = 0; k < members.size(); ++k)
{
double pexp = exp(-eta[k] * (rij - rs[k]) * (rij - rs[k]));
result[k] += pexp * pfc;
// Force calculation.
if (!derivatives) continue;
double const p1 = scalingFactors[k] * (pdfc - 2.0 * eta[k]
* (rij - rs[k]) * pfc) * pexp / rij;
// SIMPLE EXPRESSIONS:
//Vec3D const dij = p1 * atom.neighbors[j].dr;
double const p1drijx = p1 * d1[0];
double const p1drijy = p1 * d1[1];
double const p1drijz = p1 * d1[2];
// Save force contributions in Atom storage.
#ifndef N2P2_FULL_SFD_MEMORY
size_t ki = memberIndex[k];
#else
size_t const ki = memberIndex[k];
#endif
// SIMPLE EXPRESSIONS:
//atom.dGdr[ki] += dij;
//atom.neighbors[j].dGdr[ki] -= dij;
double* dGdr = atom.dGdr[ki].r;
dGdr[0] += p1drijx;
dGdr[1] += p1drijy;
dGdr[2] += p1drijz;
#ifndef N2P2_FULL_SFD_MEMORY
ki = memberIndexPerElement[k][e1];
#endif
dGdr = n.dGdr[ki].r;
dGdr[0] -= p1drijx;
dGdr[1] -= p1drijy;
dGdr[2] -= p1drijz;
}
}
}
for (size_t k = 0; k < members.size(); ++k)
{
atom.G[memberIndex[k]] = members[k]->scale(result[k]);
}
delete[] result;
return;
}
vector<string> SymGrpExpRad::parameterLines() const
{
vector<string> v;
v.push_back(strpr(getPrintFormatCommon().c_str(),
index + 1,
elementMap[ec].c_str(),
type,
subtype.c_str(),
elementMap[e1].c_str(),
rc / convLength,
cutoffAlpha));
for (size_t i = 0; i < members.size(); ++i)
{
v.push_back(strpr(getPrintFormatMember().c_str(),
members[i]->getEta() * convLength * convLength,
members[i]->getRs() / convLength,
members[i]->getLineNumber() + 1,
i + 1,
members[i]->getIndex() + 1));
}
return v;
}