n2p2 - A neural network potential package
ModeCabana.h
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 Saaketh Desai and Sam Reeve
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#ifndef MODE_CABANA_H
19#define MODE_CABANA_H
20
21#include "./ElementCabana.h"
22#include "typesCabana.h"
23
24#include <Cabana_Core.hpp>
25#include <Kokkos_Core.hpp>
26
27#include "CutoffFunction.h"
28#include "Log.h"
29#include "Mode.h"
30#include "Settings.h"
31
32#include <cstddef> // std::size_t
33#include <string> // std::string
34#include <vector> // std::vector
35
36namespace nnp
37{
38
45template <class t_device>
46class ModeCabana : public Mode
47{
48
49 public:
50 using Mode::Mode;
51
52 // Kokkos settings
53 using device_type = t_device;
54 using exe_space = typename device_type::execution_space;
55 using memory_space = typename device_type::memory_space;
56 typedef typename exe_space::array_layout layout;
57 using host_space = Kokkos::HostSpace;
58
59 // Per-type Kokkos::Views
60 using d_t_mass = Kokkos::View<T_V_FLOAT *, memory_space>;
61 using h_t_mass = Kokkos::View<T_V_FLOAT *, layout, host_space>;
62 using d_t_int = Kokkos::View<T_INT *, memory_space>;
63 using h_t_int = Kokkos::View<T_INT *, layout, host_space>;
64
65 // SymmetryFunctionTypes Kokkos::Views
66 using d_t_SF = Kokkos::View<T_FLOAT * * [15], memory_space>;
67 using t_SF = Kokkos::View<T_FLOAT * * [15], layout, host_space>;
68 using d_t_SFscaling = Kokkos::View<T_FLOAT * * [8], memory_space>;
69 using t_SFscaling = Kokkos::View<T_FLOAT * * [8], layout, host_space>;
70 using d_t_SFGmemberlist = Kokkos::View<T_INT ***, memory_space>;
71 using t_SFGmemberlist = Kokkos::View<T_INT ***, layout, host_space>;
72
73 // NN Kokkos::Views
74 using d_t_bias = Kokkos::View<T_FLOAT ***, memory_space>;
75 using t_bias = Kokkos::View<T_FLOAT ***, layout, host_space>;
76 using d_t_weights = Kokkos::View<T_FLOAT ****, memory_space>;
77 using t_weights = Kokkos::View<T_FLOAT ****, layout, host_space>;
78 using d_t_NN = Kokkos::View<T_FLOAT ***, memory_space>;
79
85 void setupElementMap() override;
91 void setupElements() override;
97 void setupSymmetryFunctions() override;
109 std::string const &fileName = "scaling.data") override;
115 void setupSymmetryFunctionGroups() override;
123 void setupNeuralNetwork() override;
133 std::string const &fileNameFormat = "weights.%03zu.data") override;
134
135 /* Compute cutoff for a single atom pair.
136 *
137 * @param[in] cutoffType Cutoff type.
138 * @param[in] fc Cutoff function value.
139 * @param[in] dfc Derivative of cutoff function.
140 * @param[in] r Atom separation distance.
141 * @param[in] rc Cutoff radius.
142 * @param[in] derivative If the cutoff derivative should be computed.
143 *
144 * Callable within device kernel.
145 */
146 KOKKOS_INLINE_FUNCTION
148 double &fc, double &dfc, double r, double rc,
149 bool derivative) const;
150 /*
151 * Scale one symmetry function.
152 *
153 * @param[in] attype Atom type.
154 * @param[in] value Scaled symmetry function value.
155 * @param[in] k Symmetry function index.
156 * @param[in] SFscaling Kokkos host View of symmetry function scaling.
157 *
158 * Callable within device kernel.
159 */
160 KOKKOS_INLINE_FUNCTION
161 double scale(int attype, double value, int k, d_t_SFscaling SFscaling) const;
162
177 template <class t_slice_x, class t_slice_f, class t_slice_type,
178 class t_slice_dEdG, class t_neigh_list, class t_neigh_parallel,
179 class t_angle_parallel>
180 void calculateForces(t_slice_x x, t_slice_f f, t_slice_type type,
181 t_slice_dEdG dEdG, t_neigh_list neigh_list, int N_local,
182 t_neigh_parallel neigh_op, t_angle_parallel angle_op);
183
195 template <class t_slice_type, class t_slice_G, class t_slice_dEdG,
196 class t_slice_E>
197 void calculateAtomicNeuralNetworks(t_slice_type type, t_slice_G G,
198 t_slice_dEdG dEdG, t_slice_E E,
199 int N_local);
200
215 template <class t_slice_x, class t_slice_type, class t_slice_G,
216 class t_neigh_list, class t_neigh_parallel, class t_angle_parallel>
217 void calculateSymmetryFunctionGroups(t_slice_x x, t_slice_type type,
218 t_slice_G G, t_neigh_list neigh_list,
219 int N_local, t_neigh_parallel neigh_op,
220 t_angle_parallel angle_op);
221
222 using Mode::log;
223
225 // (duplicated from ElementMap)
226 std::vector<std::string> knownElements = {
227 "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg",
228 "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr",
229 "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr",
230 "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd",
231 "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd",
232 "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf",
233 "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po",
234 "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm",
235 "Bk", "Cf", "Es", "Fm", "Md", "No"};
236
237 // Symmetry function Kokkos::Views
244
245 // NN Kokkos::Views
255
256 // Per type Kokkos::Views
263
264 using Mode::numElements;
265 using Mode::minNeighbors;
268 using Mode::cutoffAlpha;
270 using Mode::convEnergy;
271 using Mode::convLength;
273 using Mode::settings;
274 using Mode::cutoffType;
275 std::vector<ElementCabana> elements;
276 std::vector<std::string> elementStrings;
277};
278
280// Inlined function definitions //
282
283template <class t_device>
284KOKKOS_INLINE_FUNCTION void
286 double cutoffAlpha, double &fc, double &dfc,
287 double r, double rc, bool derivative) const
288{
289 double temp;
290 if (cutoffType == CutoffFunction::CT_TANHU) {
291 temp = tanh(1.0 - r / rc);
292 fc = temp * temp * temp;
293 if (derivative)
294 dfc = 3.0 * temp * temp * (temp * temp - 1.0) / rc;
295 }
296
297 if (cutoffType == CutoffFunction::CT_COS) {
298
299 double rci = rc * cutoffAlpha;
300 double iw = 1.0 / (rc - rci);
301 double PI = 4.0 * atan(1.0);
302 if (r < rci) {
303 fc = 1.0;
304 dfc = 0.0;
305 } else {
306 temp = cos(PI * (r - rci) * iw);
307 fc = 0.5 * (temp + 1.0);
308 if (derivative)
309 dfc = -0.5 * iw * PI * sqrt(1.0 - temp * temp);
310 }
311 }
312}
313
314template <class t_device>
315KOKKOS_INLINE_FUNCTION double
316ModeCabana<t_device>::scale(int attype, double value, int k,
317 d_t_SFscaling SFscaling_) const
318{
319 double scalingType = SFscaling_(attype, k, 7);
320 double scalingFactor = SFscaling_(attype, k, 6);
321 double Gmin = SFscaling_(attype, k, 0);
322 // double Gmax = SFscaling_(attype,k,1);
323 double Gmean = SFscaling_(attype, k, 2);
324 // double Gsigma = SFscaling_(attype,k,3);
325 double Smin = SFscaling_(attype, k, 4);
326 // double Smax = SFscaling_(attype,k,5);
327
328 if (scalingType == 0.0) {
329 return value;
330 } else if (scalingType == 1.0) {
331 return Smin + scalingFactor * (value - Gmin);
332 } else if (scalingType == 2.0) {
333 return value - Gmean;
334 } else if (scalingType == 3.0) {
335 return Smin + scalingFactor * (value - Gmean);
336 } else if (scalingType == 4.0) {
337 return Smin + scalingFactor * (value - Gmean);
338 } else {
339 return 0.0;
340 }
341}
342
343}
344
345#include "ModeCabana_impl.h"
346
347#endif
CutoffType
List of available cutoff function types.
Derived Cabana main NNP class.
Definition: ModeCabana.h:47
void calculateSymmetryFunctionGroups(t_slice_x x, t_slice_type type, t_slice_G G, t_neigh_list neigh_list, int N_local, t_neigh_parallel neigh_op, t_angle_parallel angle_op)
Calculate all symmetry function groups for all atoms in given structure.
std::vector< std::string > knownElements
list of element symbols in order of periodic table
Definition: ModeCabana.h:226
std::vector< std::string > elementStrings
Definition: ModeCabana.h:276
void setupSymmetryFunctions() override
Set up all symmetry functions.
d_t_SFGmemberlist d_SFGmemberlist
Definition: ModeCabana.h:240
d_t_int numNeuronsPerLayer
Definition: ModeCabana.h:251
void setupSymmetryFunctionGroups() override
Set up symmetry function groups.
void setupNeuralNetworkWeights(std::string const &fileNameFormat="weights.%03zu.data") override
Set up neural network weights from files.
void setupElementMap() override
Set up the element map.
Kokkos::View< T_FLOAT **[15], layout, host_space > t_SF
Definition: ModeCabana.h:67
t_device device_type
Definition: ModeCabana.h:53
h_t_int h_numSFGperElem
Definition: ModeCabana.h:260
void calculateForces(t_slice_x x, t_slice_f f, t_slice_type type, t_slice_dEdG dEdG, t_neigh_list neigh_list, int N_local, t_neigh_parallel neigh_op, t_angle_parallel angle_op)
Calculate forces for all atoms in given structure.
h_t_mass atomicEnergyOffset
Definition: ModeCabana.h:257
t_SFGmemberlist SFGmemberlist
Definition: ModeCabana.h:241
typename device_type::execution_space exe_space
Definition: ModeCabana.h:54
Kokkos::View< T_FLOAT ***, layout, host_space > t_bias
Definition: ModeCabana.h:75
double meanEnergy
Definition: ModeCabana.h:269
typename device_type::memory_space memory_space
Definition: ModeCabana.h:55
Kokkos::View< T_INT *, layout, host_space > h_t_int
Definition: ModeCabana.h:63
Kokkos::View< T_INT ***, memory_space > d_t_SFGmemberlist
Definition: ModeCabana.h:70
Kokkos::View< T_FLOAT **[15], memory_space > d_t_SF
Definition: ModeCabana.h:66
d_t_int numSFperElem
Definition: ModeCabana.h:259
Kokkos::View< T_INT ***, layout, host_space > t_SFGmemberlist
Definition: ModeCabana.h:71
Kokkos::View< T_FLOAT ****, layout, host_space > t_weights
Definition: ModeCabana.h:77
Kokkos::View< T_FLOAT ***, memory_space > d_t_NN
Definition: ModeCabana.h:78
d_t_int numSFGperElem
Definition: ModeCabana.h:261
ScalingType scalingType
Definition: ModeCabana.h:272
Kokkos::View< T_V_FLOAT *, layout, host_space > h_t_mass
Definition: ModeCabana.h:61
d_t_SFscaling d_SFscaling
Definition: ModeCabana.h:242
d_t_weights weights
Definition: ModeCabana.h:247
KOKKOS_INLINE_FUNCTION double scale(int attype, double value, int k, d_t_SFscaling SFscaling) const
Definition: ModeCabana.h:316
Kokkos::View< T_FLOAT **[8], memory_space > d_t_SFscaling
Definition: ModeCabana.h:68
KOKKOS_INLINE_FUNCTION void compute_cutoff(CutoffFunction::CutoffType cutoffType, double cutoffAlpha, double &fc, double &dfc, double r, double rc, bool derivative) const
Definition: ModeCabana.h:285
Kokkos::View< T_INT *, memory_space > d_t_int
Definition: ModeCabana.h:62
h_t_int h_numNeuronsPerLayer
Definition: ModeCabana.h:252
Kokkos::View< T_FLOAT **[8], layout, host_space > t_SFscaling
Definition: ModeCabana.h:69
d_t_bias bias
Definition: ModeCabana.h:246
void calculateAtomicNeuralNetworks(t_slice_type type, t_slice_G G, t_slice_dEdG dEdG, t_slice_E E, int N_local)
Calculate atomic neural networks for all atoms in given structure.
Kokkos::View< T_FLOAT ****, memory_space > d_t_weights
Definition: ModeCabana.h:76
Kokkos::View< T_V_FLOAT *, memory_space > d_t_mass
Definition: ModeCabana.h:60
h_t_int h_numSFperElem
Definition: ModeCabana.h:258
t_weights h_weights
Definition: ModeCabana.h:249
double cutoffAlpha
Definition: Mode.h:635
Kokkos::HostSpace host_space
Definition: ModeCabana.h:57
std::vector< ElementCabana > elements
Definition: ModeCabana.h:275
void setupSymmetryFunctionScaling(std::string const &fileName="scaling.data") override
Set up symmetry function scaling from file.
Kokkos::View< T_FLOAT ***, memory_space > d_t_bias
Definition: ModeCabana.h:74
void setupElements() override
Set up all Element instances.
CutoffFunction::CutoffType cutoffType
Definition: Mode.h:644
t_SFscaling SFscaling
Definition: ModeCabana.h:243
exe_space::array_layout layout
Definition: ModeCabana.h:56
void setupNeuralNetwork() override
Set up neural networks for all elements.
Base class for all NNP applications.
Definition: Mode.h:87
double convEnergy
Definition: Mode.h:637
Mode()
Definition: Mode.cpp:40
std::vector< double > minCutoffRadius
Definition: Mode.h:633
double convLength
Definition: Mode.h:638
double maxCutoffRadius
Definition: Mode.h:634
std::size_t numElements
Definition: Mode.h:631
settings::Settings settings
Definition: Mode.h:642
Log log
Global log file.
Definition: Mode.h:593
std::vector< std::size_t > minNeighbors
Definition: Mode.h:632
double cutoffAlpha
Definition: Mode.h:635
CutoffFunction::CutoffType cutoffType
Definition: Mode.h:644
Definition: Atom.h:29
CutoffFunction fc
Definition: nnp-cutoff.cpp:27
ScalingType
Definition: typesCabana.h:26