n2p2 - A neural network potential package
SymFnc.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 "SymFnc.h"
18#include "utility.h"
19#include <cstdlib> // atof, atoi
20#include <limits> // std::numeric_limits
21#include <stdexcept> // std::runtime_error, std::out_of_range
22
23using namespace std;
24using namespace nnp;
25
26size_t const SymFnc::sfinfoWidth = 12;
27
28SymFnc::PrintFormat const SymFnc::printFormat = initializePrintFormat();
29
30SymFnc::PrintOrder const SymFnc::printOrder = initializePrintOrder();
31
32vector<string> SymFnc::parameterInfo() const
33{
34 vector<string> v;
35 string s;
36 size_t w = sfinfoWidth;
37
38 s = "lineNumber";
39 v.push_back(strpr((pad(s, w) + "%zu" ).c_str(), lineNumber + 1));
40 s = "index";
41 v.push_back(strpr((pad(s, w) + "%zu" ).c_str(), index + 1));
42 s = "type";
43 v.push_back(strpr((pad(s, w) + "%zu" ).c_str(), type));
44 s = "ec";
45 v.push_back(strpr((pad(s, w) + "%s" ).c_str(), elementMap[ec].c_str()));
46 s = "rc";
47 v.push_back(strpr((pad(s, w) + "%14.8E").c_str(), rc / convLength));
48
49 return v;
50}
51
53 string statisticsLine,
54 double Smin,
55 double Smax)
56{
57 this->scalingType = scalingType;
58
59 vector<string> s = split(reduce(statisticsLine));
60 if (((size_t)atoi(s.at(0).c_str()) != ec + 1) &&
61 ((size_t)atoi(s.at(1).c_str()) != index + 1))
62 {
63 throw runtime_error("ERROR: Inconsistent scaling statistics.\n");
64 }
65 Gmin = atof(s.at(2).c_str());
66 Gmax = atof(s.at(3).c_str());
67 Gmean = atof(s.at(4).c_str());
68 // Older versions may not supply sigma.
69 if (s.size() > 5)
70 {
71 Gsigma = atof(s.at(5).c_str());
72 }
73 this->Smin = Smin;
74 this->Smax = Smax;
75
76 if(scalingType == ST_NONE)
77 {
78 scalingFactor = 1.0;
79 }
80 else if (scalingType == ST_SCALE)
81 {
82 scalingFactor = (Smax - Smin) / (Gmax - Gmin);
83 }
84 else if (scalingType == ST_CENTER)
85 {
86 scalingFactor = 1.0;
87 }
88 else if (scalingType == ST_SCALECENTER)
89 {
90 scalingFactor = (Smax - Smin) / (Gmax - Gmin);
91 }
92 else if (scalingType == ST_SCALESIGMA)
93 {
95 }
96
97 return;
98}
99
100#ifndef N2P2_NO_SF_CACHE
101vector<string> SymFnc::getCacheIdentifiers() const
102{
103 return vector<string>();
104}
105
106void SymFnc::addCacheIndex(size_t element,
107 size_t cacheIndex,
108 string cacheIdentifier)
109{
110 // Check if provided cache identifier is identical to the originally
111 // supplied one.
112 vector<vector<string>> identifiersPerElement(elementMap.size());
113 for (string id : getCacheIdentifiers())
114 {
115 size_t ne = atoi(split(id)[0].c_str());
116 identifiersPerElement.at(ne).push_back(id);
117 }
118 size_t current = cacheIndices.at(element).size();
119 if (identifiersPerElement.at(element).at(current) != cacheIdentifier)
120 {
121 throw runtime_error(strpr("ERROR: Cache identifiers do no match:\n"
122 "%s\n"
123 " !=\n"
124 "%s\n",
125 identifiersPerElement.at(element)
126 .at(current).c_str(),
127 cacheIdentifier.c_str()));
128 }
129 cacheIndices.at(element).push_back(cacheIndex);
130
131 return;
132}
133#endif
134
135SymFnc::SymFnc(size_t type, ElementMap const& elementMap) :
136 type (type ),
137 elementMap (elementMap),
138 index (0 ),
139 ec (0 ),
140 minNeighbors (0 ),
141 Smin (0.0 ),
142 Smax (0.0 ),
143 Gmin (0.0 ),
144 Gmax (0.0 ),
145 Gmean (0.0 ),
146 Gsigma (0.0 ),
147 rc (0.0 ),
148 scalingFactor(1.0 ),
149 convLength (1.0 ),
150 scalingType (ST_NONE )
151{
152 // Add standard parameter IDs to set.
153 parameters.insert("index");
154 parameters.insert("ec");
155 parameters.insert("type");
156 parameters.insert("rc");
157 parameters.insert("lineNumber");
158
159 // Initialize per-element index vector, use max to indicate
160 // "uninitialized" state.
161 indexPerElement.resize(elementMap.size(), numeric_limits<size_t>::max());
162
163#ifndef N2P2_NO_SF_CACHE
164 // Initialize cache indices vector.
165 cacheIndices.resize(elementMap.size(), vector<size_t>());
166#endif
167}
168
169double SymFnc::scale(double value) const
170{
171 if (scalingType == ST_NONE)
172 {
173 return value;
174 }
175 else if (scalingType == ST_SCALE)
176 {
177 return Smin + scalingFactor * (value - Gmin);
178 }
179 else if (scalingType == ST_CENTER)
180 {
181 return value - Gmean;
182 }
183 else if (scalingType == ST_SCALECENTER)
184 {
185 return Smin + scalingFactor * (value - Gmean);
186 }
187 else if (scalingType == ST_SCALESIGMA)
188 {
189 return Smin + scalingFactor * (value - Gmean);
190 }
191 else
192 {
193 return 0.0;
194 }
195}
196
197double SymFnc::unscale(double value) const
198{
199 if (scalingType == ST_NONE)
200 {
201 return value;
202 }
203 else if (scalingType == ST_SCALE)
204 {
205 return (value - Smin) / scalingFactor + Gmin;
206 }
207 else if (scalingType == ST_CENTER)
208 {
209 return value + Gmean;
210 }
211 else if (scalingType == ST_SCALECENTER)
212 {
213 return (value - Smin) / scalingFactor + Gmean;
214 }
215 else if (scalingType == ST_SCALESIGMA)
216 {
217 return (value - Smin) / scalingFactor + Gmean;
218 }
219 else
220 {
221 return 0.0;
222 }
223}
224
226{
227 return strpr("%4zu %9.2E %9.2E %9.2E %9.2E %9.2E %5.2f %5.2f %d\n",
228 index + 1,
229 Gmin,
230 Gmax,
231 Gmean,
232 Gsigma,
234 Smin,
235 Smax,
237}
238
240{
241 PrintFormat pf;
242
243 pf["index"] = make_pair("%4zu" , string(4, ' '));
244 pf["ec"] = make_pair("%2s" , string(2, ' '));
245 pf["type"] = make_pair("%2zu" , string(2, ' '));
246 pf["subtype"] = make_pair("%4s" , string(4, ' '));
247 pf["e1"] = make_pair("%2s" , string(2, ' '));
248 pf["e2"] = make_pair("%2s" , string(2, ' '));
249 pf["eta"] = make_pair("%9.3E" , string(9, ' '));
250 pf["rs/rl"] = make_pair("%10.3E", string(10, ' '));
251 pf["rc"] = make_pair("%10.3E", string(10, ' '));
252 pf["angleLeft"] = make_pair("%6.1f" , string(6, ' '));
253 pf["angleRight"] = make_pair("%6.1f" , string(6, ' '));
254 pf["lambda"] = make_pair("%2.0f" , string(2, ' '));
255 pf["zeta"] = make_pair("%4.1f" , string(4, ' '));
256 pf["alpha"] = make_pair("%4.2f" , string(4, ' '));
257 pf["lineNumber"] = make_pair("%5zu" , string(5, ' '));
258
259 return pf;
260}
261
263{
264 vector<string> po;
265
266 po.push_back("index" );
267 po.push_back("ec" );
268 po.push_back("type" );
269 po.push_back("subtype" );
270 po.push_back("e1" );
271 po.push_back("e2" );
272 po.push_back("eta" );
273 po.push_back("rs/rl" );
274 po.push_back("rc" );
275 po.push_back("angleLeft" );
276 po.push_back("angleRight");
277 po.push_back("lambda" );
278 po.push_back("zeta" );
279 po.push_back("alpha" );
280 po.push_back("lineNumber");
281
282 return po;
283}
284
286{
287 string s;
288
289 for (PrintOrder::const_iterator it = printOrder.begin();
290 it != printOrder.end(); ++it)
291 {
292 // If parameter is present add format string.
293 if (parameters.find(*it) != parameters.end())
294 {
295 s += safeFind(printFormat, (*it)).first + ' ';
296 }
297 // Else add just enough empty spaces.
298 else
299 {
300 s += safeFind(printFormat, (*it)).second + ' ';
301 }
302 }
303 // Remove extra space at the end.
304 if (s.size () > 0) s.resize (s.size () - 1);
305 s += '\n';
306
307 return s;
308}
309
Contains element map.
Definition: ElementMap.h:30
std::size_t size() const
Get element map size.
Definition: ElementMap.h:140
ScalingType
List of available scaling types.
Definition: SymFnc.h:44
@ ST_NONE
Definition: SymFnc.h:47
@ ST_SCALESIGMA
Definition: SymFnc.h:64
@ ST_SCALECENTER
Definition: SymFnc.h:60
@ ST_CENTER
Definition: SymFnc.h:55
@ ST_SCALE
Definition: SymFnc.h:52
double convLength
Data set normalization length conversion factor.
Definition: SymFnc.h:296
static PrintFormat const printFormat
Map of parameter format strings and empty strings.
Definition: SymFnc.h:311
std::map< std::string, std::pair< std::string, std::string > > PrintFormat
Definition: SymFnc.h:265
void addCacheIndex(std::size_t element, std::size_t cacheIndex, std::string cacheIdentifier)
Add one cache index for given neighbor element and check identifier.
Definition: SymFnc.cpp:106
double Gmean
Mean unscaled symmetry function value.
Definition: SymFnc.h:288
std::size_t type
Symmetry function type.
Definition: SymFnc.h:268
virtual std::vector< std::string > getCacheIdentifiers() const
Get unique cache identifiers.
Definition: SymFnc.cpp:101
double Gmax
Maximum unscaled symmetry function value.
Definition: SymFnc.h:286
std::set< std::string > parameters
Set with symmetry function parameter IDs (lookup for printing).
Definition: SymFnc.h:300
static PrintOrder const printOrder
Vector of parameters in order of printing.
Definition: SymFnc.h:313
std::size_t index
Symmetry function index (per element).
Definition: SymFnc.h:272
double scalingFactor
Scaling factor.
Definition: SymFnc.h:294
ScalingType scalingType
Symmetry function scaling type used by this symmetry function.
Definition: SymFnc.h:298
double rc
Cutoff radius .
Definition: SymFnc.h:292
void setScalingType(ScalingType scalingType, std::string statisticsLine, double Smin, double Smax)
Set symmetry function scaling type.
Definition: SymFnc.cpp:52
std::vector< std::string > PrintOrder
Definition: SymFnc.h:266
std::vector< std::vector< std::size_t > > cacheIndices
Cache indices for each element.
Definition: SymFnc.h:306
SymFnc(std::size_t type, ElementMap const &)
Constructor, initializes type.
Definition: SymFnc.cpp:135
double Smin
Minimum for scaling range.
Definition: SymFnc.h:280
double unscale(double value) const
Undo symmetry function scaling and/or centering.
Definition: SymFnc.cpp:197
ElementMap elementMap
Copy of element map.
Definition: SymFnc.h:270
double Smax
Maximum for scaling range.
Definition: SymFnc.h:282
double scale(double value) const
Apply symmetry function scaling and/or centering.
Definition: SymFnc.cpp:169
double Gsigma
Sigma of unscaled symmetry function values.
Definition: SymFnc.h:290
double Gmin
Minimum unscaled symmetry function value.
Definition: SymFnc.h:284
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
static std::size_t const sfinfoWidth
Width of the SFINFO parameter description field (see parameterInfo()).
Definition: SymFnc.h:309
static PrintFormat const initializePrintFormat()
Initialize static print format map for all possible parameters.
Definition: SymFnc.cpp:239
std::string scalingLine() const
Get string with scaling information.
Definition: SymFnc.cpp:225
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
static PrintOrder const initializePrintOrder()
Initialize static print order vector for all possible parameters.
Definition: SymFnc.cpp:262
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
V const & safeFind(std::map< K, V > const &stdMap, typename std::map< K, V >::key_type const &key)
Safely access map entry.
Definition: utility.h:144
@ ST_NONE
Definition: typesCabana.h:27