n2p2 - A neural network potential package
nnp::InterfaceLammps Class Reference

#include <InterfaceLammps.h>

Inheritance diagram for nnp::InterfaceLammps:
Collaboration diagram for nnp::InterfaceLammps:

Public Member Functions

 InterfaceLammps ()
 
void initialize (char const *const &directory, char const *const &emap, bool showew, bool resetew, int showewsum, int maxew, double cflength, double cfenergy, double lammpsCutoff, int lammpsNtypes, int myRank)
 Initialize the LAMMPS interface. More...
 
void setLocalAtoms (int numAtomsLocal, int const *const atomType)
 (Re)set structure to contain only local LAMMPS atoms. More...
 
void setLocalTags (int const *const atomTag)
 Set atom tags (int version, -DLAMMPS_SMALLBIG). More...
 
void setLocalTags (int64_t const *const atomTag)
 Set atom tags (int64_t version, -DLAMMPS_BIGBIG). More...
 
void addNeighbor (int i, int j, int64_t tag, int type, double dx, double dy, double dz, double d2)
 Add one neighbor to atom (int64_t version, -DLAMMPS_BIGBIG). More...
 
void process ()
 Calculate symmetry functions, atomic neural networks and sum of local energy contributions. More...
 
double getEnergy () const
 Return sum of local energy contributions. More...
 
double getAtomicEnergy (int index) const
 Return energy contribution of one atom. More...
 
void getForces (double *const *const &atomF) const
 Calculate forces and add to LAMMPS atomic force arrays. More...
 
bool isInitialized () const
 Check if this interface is correctly initialized. More...
 
double getMaxCutoffRadius () const
 Get largest cutoff. More...
 
long getEWBufferSize () const
 Calculate buffer size for extrapolation warning communication. More...
 
void fillEWBuffer (char *const &buf, int bs) const
 Fill provided buffer with extrapolation warning entries. More...
 
void extractEWBuffer (char const *const &buf, int bs)
 Extract given buffer to symmetry function statistics class. More...
 
void writeExtrapolationWarnings ()
 Write extrapolation warnings to log. More...
 
void clearExtrapolationWarnings ()
 Clear extrapolation warnings storage. More...
 
- Public Member Functions inherited from nnp::Mode
 Mode ()
 
void initialize ()
 Write welcome message with version information. More...
 
void loadSettingsFile (std::string const &fileName="input.nn")
 Open settings file and load all keywords into memory. More...
 
void setupGeneric (bool skipNormalize=false)
 Combine multiple setup routines and provide a basic NNP setup. More...
 
void setupNormalization (bool standalone=true)
 Set up normalization. More...
 
virtual void setupElementMap ()
 Set up the element map. More...
 
virtual void setupElements ()
 Set up all Element instances. More...
 
void setupCutoff ()
 Set up cutoff function for all symmetry functions. More...
 
virtual void setupSymmetryFunctions ()
 Set up all symmetry functions. More...
 
void setupSymmetryFunctionScalingNone ()
 Set up "empy" symmetry function scaling. More...
 
virtual void setupSymmetryFunctionScaling (std::string const &fileName="scaling.data")
 Set up symmetry function scaling from file. More...
 
virtual void setupSymmetryFunctionGroups ()
 Set up symmetry function groups. More...
 
virtual void setupSymmetryFunctionCache (bool verbose=false)
 Set up symmetry function cache. More...
 
void setupSymmetryFunctionMemory (bool verbose=false)
 Extract required memory dimensions for symmetry function derivatives. More...
 
void setupSymmetryFunctionStatistics (bool collectStatistics, bool collectExtrapolationWarnings, bool writeExtrapolationWarnings, bool stopOnExtrapolationWarnings)
 Set up symmetry function statistics collection. More...
 
virtual void setupNeuralNetwork ()
 Set up neural networks for all elements. More...
 
virtual void setupNeuralNetworkWeights (std::string const &fileNameFormatShort="weights.%03zu.data", std::string const &fileNameFormatCharge="weightse.%03zu.data")
 Set up neural network weights from files. More...
 
void calculateSymmetryFunctions (Structure &structure, bool const derivatives)
 Calculate all symmetry functions for all atoms in given structure. More...
 
void calculateSymmetryFunctionGroups (Structure &structure, bool const derivatives)
 Calculate all symmetry function groups for all atoms in given structure. More...
 
void calculateAtomicNeuralNetworks (Structure &structure, bool const derivatives)
 Calculate a single atomic neural network for a given atom and nn type. More...
 
void calculateEnergy (Structure &structure) const
 Calculate potential energy for a given structure. More...
 
void calculateCharge (Structure &structure) const
 Calculate total charge for a given structure. More...
 
void calculateForces (Structure &structure) const
 Calculate forces for all atoms in given structure. More...
 
void addEnergyOffset (Structure &structure, bool ref=true)
 Add atomic energy offsets to reference energy. More...
 
void removeEnergyOffset (Structure &structure, bool ref=true)
 Remove atomic energy offsets from reference energy. More...
 
double getEnergyOffset (Structure const &structure) const
 Get atomic energy offset for given structure. More...
 
double getEnergyWithOffset (Structure const &structure, bool ref=true) const
 Add atomic energy offsets and return energy. More...
 
double normalized (std::string const &property, double value) const
 Apply normalization to given property. More...
 
double normalizedEnergy (Structure const &structure, bool ref=true) const
 Apply normalization to given energy of structure. More...
 
double physical (std::string const &property, double value) const
 Undo normalization for a given property. More...
 
double physicalEnergy (Structure const &structure, bool ref=true) const
 Undo normalization for a given energy of structure. More...
 
void convertToNormalizedUnits (Structure &structure) const
 Convert one structure to normalized units. More...
 
void convertToPhysicalUnits (Structure &structure) const
 Convert one structure to physical units. More...
 
std::size_t getNumExtrapolationWarnings () const
 Count total number of extrapolation warnings encountered for all elements and symmetry functions. More...
 
void resetExtrapolationWarnings ()
 Erase all extrapolation warnings and reset counters. More...
 
NNPType getNnpType () const
 Getter for Mode::nnpType. More...
 
double getMeanEnergy () const
 Getter for Mode::meanEnergy. More...
 
double getConvEnergy () const
 Getter for Mode::convEnergy. More...
 
double getConvLength () const
 Getter for Mode::convLength. More...
 
double getMaxCutoffRadius () const
 Getter for Mode::maxCutoffRadius. More...
 
std::size_t getNumElements () const
 Getter for Mode::numElements. More...
 
std::vector< std::size_t > getNumSymmetryFunctions () const
 Get number of symmetry functions per element. More...
 
bool useNormalization () const
 Check if normalization is enabled. More...
 
bool settingsKeywordExists (std::string const &keyword) const
 Check if keyword was found in settings file. More...
 
std::string settingsGetValue (std::string const &keyword) const
 Get value for given keyword in Settings instance. More...
 
std::vector< std::size_t > pruneSymmetryFunctionsRange (double threshold)
 Prune symmetry functions according to their range and write settings file. More...
 
std::vector< std::size_t > pruneSymmetryFunctionsSensitivity (double threshold, std::vector< std::vector< double > > sensitivity)
 Prune symmetry functions with sensitivity analysis data. More...
 
void writePrunedSettingsFile (std::vector< std::size_t > prune, std::string fileName="output.nn") const
 Copy settings file but comment out lines provided. More...
 
void writeSettingsFile (std::ofstream *const &file) const
 Write complete settings file. More...
 

Protected Attributes

int myRank
 Process rank. More...
 
bool initialized
 Initialization state. More...
 
bool showew
 Corresponds to LAMMPS showew keyword. More...
 
bool resetew
 Corresponds to LAMMPS resetew keyword. More...
 
int showewsum
 Corresponds to LAMMPS showewsum keyword. More...
 
int maxew
 Corresponds to LAMMPS maxew keyword. More...
 
double cflength
 Corresponds to LAMMPS cflength keyword. More...
 
double cfenergy
 Corresponds to LAMMPS cfenergy keyword. More...
 
std::string emap
 Corresponds to LAMMPS map keyword. More...
 
std::vector< size_t > indexMap
 Map from LAMMPS index to n2p2 atom index. More...
 
std::map< int, bool > ignoreType
 True if atoms of this LAMMPS type will be ignored. More...
 
std::map< int, std::size_t > mapTypeToElement
 Map from LAMMPS type to n2p2 element index. More...
 
std::map< std::size_t, int > mapElementToType
 Map from n2p2 element index to LAMMPS type. More...
 
Structure structure
 Structure containing local atoms. More...
 
- Protected Attributes inherited from nnp::Mode
NNPType nnpType
 
bool normalize
 
bool checkExtrapolationWarnings
 
bool useChargeNN
 
std::size_t numElements
 
std::vector< std::size_t > minNeighbors
 
std::vector< double > minCutoffRadius
 
double maxCutoffRadius
 
double cutoffAlpha
 
double meanEnergy
 
double convEnergy
 
double convLength
 
Settings settings
 
SymFnc::ScalingType scalingType
 
CutoffFunction::CutoffType cutoffType
 
std::vector< Elementelements
 

Additional Inherited Members

- Public Types inherited from nnp::Mode
enum class  NNPType { SHORT_ONLY , SHORT_CHARGE_NN }
 
- Public Attributes inherited from nnp::Mode
ElementMap elementMap
 Global element map, populated by setupElementMap(). More...
 
Log log
 Global log file. More...
 
- Protected Member Functions inherited from nnp::Mode
void readNeuralNetworkWeights (std::string const &type, std::string const &fileName)
 Read in weights for a specific type of neural network. More...
 

Detailed Description

Definition at line 30 of file InterfaceLammps.h.

Constructor & Destructor Documentation

◆ InterfaceLammps()

InterfaceLammps::InterfaceLammps ( )

Definition at line 35 of file InterfaceLammps.cpp.

35 : myRank (0 ),
36 initialized (false),
37 showew (true ),
38 resetew (false),
39 showewsum (0 ),
40 maxew (0 ),
41 cflength (1.0 ),
42 cfenergy (1.0 )
43
44{
45}
bool initialized
Initialization state.
bool resetew
Corresponds to LAMMPS resetew keyword.
double cfenergy
Corresponds to LAMMPS cfenergy keyword.
bool showew
Corresponds to LAMMPS showew keyword.
int showewsum
Corresponds to LAMMPS showewsum keyword.
int myRank
Process rank.
int maxew
Corresponds to LAMMPS maxew keyword.
double cflength
Corresponds to LAMMPS cflength keyword.

Member Function Documentation

◆ initialize()

void InterfaceLammps::initialize ( char const *const &  directory,
char const *const &  emap,
bool  showew,
bool  resetew,
int  showewsum,
int  maxew,
double  cflength,
double  cfenergy,
double  lammpsCutoff,
int  lammpsNtypes,
int  myRank 
)

Initialize the LAMMPS interface.

Parameters
[in]directoryDirectory containing NNP data files (weights, scaling, settings).
[in]emapElement mapping from LAMMPS to n2p2.
[in]showewIf detailed extrapolation warnings for all atoms are shown.
[in]resetewIf extrapolation warnings counter is reset every timestep.
[in]showewsumShow number of warnings every this many timesteps.
[in]maxewAbort simulation if more than this many warnings are encountered.
[in]cflengthLength unit conversion factor.
[in]cfenergyEnergy unit conversion factor.
[in]lammpsCutoffCutoff radius from LAMMPS (via pair_coeff).
[in]lammpsNtypesNumber of atom types in LAMMPS.
[in]myRankMPI process rank (passed on to structure index).

Definition at line 47 of file InterfaceLammps.cpp.

58{
59 this->emap = emap;
60 this->showew = showew;
61 this->resetew = resetew;
62 this->showewsum = showewsum;
63 this->maxew = maxew;
64 this->cflength = cflength;
65 this->cfenergy = cfenergy;
66 this->myRank = myRank;
67 log.writeToStdout = false;
68 string dir(directory);
69 char const separator = '/';
70 if (dir.back() != separator) dir += separator;
72 loadSettingsFile(dir + "input.nn");
74 setupSymmetryFunctionScaling(dir + "scaling.data");
75 bool collectStatistics = false;
76 bool collectExtrapolationWarnings = false;
77 bool writeExtrapolationWarnings = false;
78 bool stopOnExtrapolationWarnings = false;
79 if (showew == true || showewsum > 0 || maxew >= 0)
80 {
81 collectExtrapolationWarnings = true;
82 }
83 setupSymmetryFunctionStatistics(collectStatistics,
84 collectExtrapolationWarnings,
86 stopOnExtrapolationWarnings);
87 setupNeuralNetworkWeights(dir + "weights.%03d.data");
88
89 log << "\n";
90 log << "*** SETUP: LAMMPS INTERFACE *************"
91 "**************************************\n";
92 log << "\n";
93
94 if (showew)
95 {
96 log << "Individual extrapolation warnings will be shown.\n";
97 }
98 else
99 {
100 log << "Individual extrapolation warnings will not be shown.\n";
101 }
102
103 if (showewsum != 0)
104 {
105 log << strpr("Extrapolation warning summary will be shown every %d"
106 " timesteps.\n", showewsum);
107 }
108 else
109 {
110 log << "Extrapolation warning summary will not be shown.\n";
111 }
112
113 if (maxew != 0)
114 {
115 log << strpr("The simulation will be stopped when %d extrapolation"
116 " warnings are exceeded.\n", maxew);
117 }
118 else
119 {
120 log << "No extrapolation warning limit set.\n";
121 }
122
123 if (resetew)
124 {
125 log << "Extrapolation warning counter is reset every time step.\n";
126 }
127 else
128 {
129 log << "Extrapolation warnings are accumulated over all time steps.\n";
130 }
131
132 log << "-----------------------------------------"
133 "--------------------------------------\n";
134 log << "CAUTION: If the LAMMPS unit system differs from the one used\n";
135 log << " during NN training, appropriate conversion factors\n";
136 log << " must be provided (see keywords cflength and cfenergy).\n";
137 log << "\n";
138 log << strpr("Length unit conversion factor: %24.16E\n", cflength);
139 log << strpr("Energy unit conversion factor: %24.16E\n", cfenergy);
140 double sfCutoff = getMaxCutoffRadius();
141 log << "\n";
142 log << "Checking consistency of cutoff radii (in LAMMPS units):\n";
143 log << strpr("LAMMPS Cutoff (via pair_coeff) : %11.3E\n", lammpsCutoff);
144 log << strpr("Maximum symmetry function cutoff: %11.3E\n", sfCutoff);
145 if (lammpsCutoff < sfCutoff)
146 {
147 throw runtime_error("ERROR: LAMMPS cutoff via pair_coeff keyword is"
148 " smaller than maximum symmetry function"
149 " cutoff.\n");
150 }
151 else if (fabs(sfCutoff - lammpsCutoff) / lammpsCutoff > TOLCUTOFF)
152 {
153 log << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
154 log << "WARNING: Potential length units mismatch!\n";
155 log << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
156 }
157 else
158 {
159 log << "Cutoff radii are consistent.\n";
160 }
161
162 log << "-----------------------------------------"
163 "--------------------------------------\n";
164 log << "Element mapping string from LAMMPS to n2p2: \""
165 + this->emap + "\"\n";
166 // Create default element mapping.
167 if (this->emap == "")
168 {
169 if (elementMap.size() != (size_t)lammpsNtypes)
170 {
171 throw runtime_error(strpr("ERROR: No element mapping given and "
172 "number of LAMMPS atom types (%d) and "
173 "NNP elements (%zu) does not match.\n",
174 lammpsNtypes, elementMap.size()));
175 }
176 log << "Element mapping string empty, creating default mapping.\n";
177 for (int i = 0; i < lammpsNtypes; ++i)
178 {
179 mapTypeToElement[i + 1] = i;
180 mapElementToType[i] = i + 1;
181 }
182 }
183 // Read element mapping from pair_style argument.
184 else
185 {
186 vector<string> emapSplit = split(reduce(trim(this->emap), " \t", ""),
187 ',');
188 if (elementMap.size() < emapSplit.size())
189 {
190 throw runtime_error(strpr("ERROR: Element mapping is inconsistent,"
191 " NNP elements: %zu,"
192 " emap elements: %zu.\n",
194 emapSplit.size()));
195 }
196 for (string s : emapSplit)
197 {
198 vector<string> typeString = split(s, ':');
199 if (typeString.size() != 2)
200 {
201 throw runtime_error(strpr("ERROR: Invalid element mapping "
202 "string: \"%s\".\n", s.c_str()));
203 }
204 int t = stoi(typeString.at(0));
205 if (t > lammpsNtypes)
206 {
207 throw runtime_error(strpr("ERROR: LAMMPS type \"%d\" not "
208 "present, there are only %d types "
209 "defined.\n", t, lammpsNtypes));
210 }
211 size_t e = elementMap[typeString.at(1)];
212 mapTypeToElement[t] = e;
213 mapElementToType[e] = t;
214 }
215 }
216 log << "\n";
217 log << "CAUTION: Please ensure that this mapping between LAMMPS\n";
218 log << " atom types and NNP elements is consistent:\n";
219 log << "\n";
220 log << "---------------------------\n";
221 log << "LAMMPS type | NNP element\n";
222 log << "---------------------------\n";
223 for (int i = 1; i <= lammpsNtypes; ++i)
224 {
225 if (mapTypeToElement.find(i) != mapTypeToElement.end())
226 {
227 size_t e = mapTypeToElement.at(i);
228 log << strpr("%11d <-> %2s (%3zu)\n",
229 i,
230 elementMap[e].c_str(),
232 ignoreType[i] = false;
233 }
234 else
235 {
236 log << strpr("%11d <-> --\n", i);
237 ignoreType[i] = true;
238
239 }
240 }
241 log << "---------------------------\n";
242 log << "\n";
243 log << "NNP setup for LAMMPS completed.\n";
244
245 log << "*****************************************"
246 "**************************************\n";
247
249
250 initialized = true;
251}
#define TOLCUTOFF
std::size_t size() const
Get element map size.
Definition: ElementMap.h:140
std::size_t atomicNumber(std::size_t index) const
Get atomic number from element index.
Definition: ElementMap.h:145
Structure structure
Structure containing local atoms.
std::map< int, bool > ignoreType
True if atoms of this LAMMPS type will be ignored.
std::map< int, std::size_t > mapTypeToElement
Map from LAMMPS type to n2p2 element index.
void writeExtrapolationWarnings()
Write extrapolation warnings to log.
std::map< std::size_t, int > mapElementToType
Map from n2p2 element index to LAMMPS type.
double getMaxCutoffRadius() const
Get largest cutoff.
std::string emap
Corresponds to LAMMPS map keyword.
bool writeToStdout
Turn on/off output to stdout.
Definition: Log.h:85
ElementMap elementMap
Global element map, populated by setupElementMap().
Definition: Mode.h:508
void initialize()
Write welcome message with version information.
Definition: Mode.cpp:50
virtual void setupNeuralNetworkWeights(std::string const &fileNameFormatShort="weights.%03zu.data", std::string const &fileNameFormatCharge="weightse.%03zu.data")
Set up neural network weights from files.
Definition: Mode.cpp:1126
virtual void setupSymmetryFunctionScaling(std::string const &fileName="scaling.data")
Set up symmetry function scaling from file.
Definition: Mode.cpp:532
Log log
Global log file.
Definition: Mode.h:510
void setupGeneric(bool skipNormalize=false)
Combine multiple setup routines and provide a basic NNP setup.
Definition: Mode.cpp:197
void setupSymmetryFunctionStatistics(bool collectStatistics, bool collectExtrapolationWarnings, bool writeExtrapolationWarnings, bool stopOnExtrapolationWarnings)
Set up symmetry function statistics collection.
Definition: Mode.cpp:923
void loadSettingsFile(std::string const &fileName="input.nn")
Open settings file and load all keywords into memory.
Definition: Mode.cpp:156
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
string trim(string const &line, string const &whitespace)
Remove leading and trailing whitespaces from string.
Definition: utility.cpp:47
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
void setElementMap(ElementMap const &elementMap)
Set element map of structure.
Definition: Structure.cpp:60

References nnp::ElementMap::atomicNumber(), cfenergy, cflength, nnp::Mode::elementMap, emap, getMaxCutoffRadius(), ignoreType, nnp::Mode::initialize(), initialized, nnp::Mode::loadSettingsFile(), nnp::Mode::log, mapElementToType, mapTypeToElement, maxew, myRank, nnp::reduce(), resetew, nnp::Structure::setElementMap(), nnp::Mode::setupGeneric(), nnp::Mode::setupNeuralNetworkWeights(), nnp::Mode::setupSymmetryFunctionScaling(), nnp::Mode::setupSymmetryFunctionStatistics(), showew, showewsum, nnp::ElementMap::size(), nnp::split(), nnp::strpr(), structure, TOLCUTOFF, nnp::trim(), writeExtrapolationWarnings(), and nnp::Log::writeToStdout.

Referenced by LAMMPS_NS::PairNNP::init_style().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setLocalAtoms()

void InterfaceLammps::setLocalAtoms ( int  numAtomsLocal,
int const *const  atomType 
)

(Re)set structure to contain only local LAMMPS atoms.

Parameters
[in]numAtomsLocalNumber of local atoms.
[in]atomTypeLAMMPS atom type.

Definition at line 253 of file InterfaceLammps.cpp.

255{
256 for (size_t i = 0; i < numElements; ++i)
257 {
259 }
265 structure.energy = 0.0;
266 structure.atoms.clear();
267 indexMap.clear();
268 indexMap.resize(numAtomsLocal, numeric_limits<size_t>::max());
269 for (int i = 0; i < numAtomsLocal; i++)
270 {
271 if (ignoreType[atomType[i]]) continue;
274 structure.atoms.push_back(Atom());
275 Atom& a = structure.atoms.back();
276 a.index = i;
278 a.element = mapTypeToElement[atomType[i]];
279 a.numNeighbors = 0;
280 a.hasSymmetryFunctions = false;
282 a.neighbors.clear();
283 a.numNeighborsPerElement.clear();
286 }
287
288 return;
289}
std::vector< size_t > indexMap
Map from LAMMPS index to n2p2 atom index.
std::size_t numElements
Definition: Mode.h:517
Storage for a single atom.
Definition: Atom.h:32
std::vector< Neighbor > neighbors
Neighbor array (maximum number defined in macros.h.
Definition: Atom.h:148
bool hasSymmetryFunctionDerivatives
If symmetry function derivatives are saved for this atom.
Definition: Atom.h:94
std::size_t index
Index number of this atom.
Definition: Atom.h:98
std::size_t indexStructure
Index number of structure this atom belongs to.
Definition: Atom.h:100
std::size_t element
Element index of this atom.
Definition: Atom.h:104
bool hasSymmetryFunctions
If symmetry function values are saved for this atom.
Definition: Atom.h:92
std::vector< std::size_t > numNeighborsPerElement
Number of neighbors per element.
Definition: Atom.h:126
std::size_t numNeighbors
Total number of neighbors.
Definition: Atom.h:106
std::vector< std::size_t > numAtomsPerElement
Number of atoms of each element in this structure.
Definition: Structure.h:94
std::size_t index
Index number of this structure.
Definition: Structure.h:66
bool hasSymmetryFunctionDerivatives
If symmetry function derivatives are saved for each atom.
Definition: Structure.h:64
double energy
Potential energy determined by neural network.
Definition: Structure.h:76
std::size_t numAtoms
Total number of atoms present in this structure.
Definition: Structure.h:68
std::vector< Atom > atoms
Vector of all atoms in this structure.
Definition: Structure.h:96
bool hasNeighborList
If the neighbor list has been calculated.
Definition: Structure.h:60
bool hasSymmetryFunctions
If symmetry function values are saved for each atom.
Definition: Structure.h:62

References nnp::Structure::atoms, nnp::Atom::element, nnp::Structure::energy, nnp::Structure::hasNeighborList, nnp::Atom::hasSymmetryFunctionDerivatives, nnp::Structure::hasSymmetryFunctionDerivatives, nnp::Atom::hasSymmetryFunctions, nnp::Structure::hasSymmetryFunctions, ignoreType, nnp::Atom::index, nnp::Structure::index, indexMap, nnp::Atom::indexStructure, mapTypeToElement, myRank, nnp::Atom::neighbors, nnp::Structure::numAtoms, nnp::Structure::numAtomsPerElement, nnp::Mode::numElements, nnp::Atom::numNeighbors, nnp::Atom::numNeighborsPerElement, and structure.

Referenced by LAMMPS_NS::PairNNP::compute().

Here is the caller graph for this function:

◆ setLocalTags() [1/2]

void InterfaceLammps::setLocalTags ( int const *const  atomTag)

Set atom tags (int version, -DLAMMPS_SMALLBIG).

Parameters
[in]atomTagLAMMPS atom tag.

Definition at line 291 of file InterfaceLammps.cpp.

292{
293 for (size_t i = 0; i < structure.atoms.size(); i++)
294 {
295 // Implicit conversion from int to int64_t!
296 structure.atoms.at(i).tag = atomTag[i];
297 }
298
299 return;
300}

References nnp::Structure::atoms, and structure.

Referenced by LAMMPS_NS::PairNNP::compute().

Here is the caller graph for this function:

◆ setLocalTags() [2/2]

void InterfaceLammps::setLocalTags ( int64_t const *const  atomTag)

Set atom tags (int64_t version, -DLAMMPS_BIGBIG).

Parameters
[in]atomTagLAMMPS atom tag.

Definition at line 302 of file InterfaceLammps.cpp.

303{
304 for (size_t i = 0; i < structure.atoms.size(); i++)
305 {
306 structure.atoms.at(i).tag = atomTag[i];
307 }
308
309 return;
310}

References nnp::Structure::atoms, and structure.

◆ addNeighbor()

void InterfaceLammps::addNeighbor ( int  i,
int  j,
int64_t  tag,
int  type,
double  dx,
double  dy,
double  dz,
double  d2 
)

Add one neighbor to atom (int64_t version, -DLAMMPS_BIGBIG).

Parameters
[in]iLocal atom index.
[in]jNeighbor atom index.
[in]tagNeighbor atom tag.
[in]typeNeighbor atom type.
[in]dxNeighbor atom distance in x direction.
[in]dyNeighbor atom distance in y direction.
[in]dzNeighbor atom distance in z direction.
[in]d2Square of neighbor atom distance.

If -DLAMMPS_SMALLBIG implicit conversion is applied for tag.

Definition at line 312 of file InterfaceLammps.cpp.

320{
321 if (ignoreType[type] ||
322 indexMap.at(i) == numeric_limits<size_t>::max()) return;
323 Atom& a = structure.atoms[indexMap.at(i)];
324 a.numNeighbors++;
325 a.neighbors.push_back(Atom::Neighbor());
327 Atom::Neighbor& n = a.neighbors.back();
328 n.index = j;
329 n.tag = tag;
330 n.element = mapTypeToElement[type];
331 n.dr[0] = dx * cflength;
332 n.dr[1] = dy * cflength;
333 n.dr[2] = dz * cflength;
334 n.d = sqrt(d2) * cflength;
335 if (normalize)
336 {
337 n.dr[0] *= convLength;
338 n.dr[1] *= convLength;
339 n.dr[2] *= convLength;
340 n.d *= convLength;
341 }
342
343 return;
344}
bool normalize
Definition: Mode.h:514
double convLength
Definition: Mode.h:524
Struct to store information on neighbor atoms.
Definition: Atom.h:35
std::size_t index
Index of neighbor atom.
Definition: Atom.h:37
std::size_t element
Element index of neighbor atom.
Definition: Atom.h:41
double d
Distance to neighbor atom.
Definition: Atom.h:43
Vec3D dr
Distance vector to neighbor atom.
Definition: Atom.h:45
int64_t tag
Tag of neighbor atom.
Definition: Atom.h:39

References nnp::Structure::atoms, cflength, nnp::Mode::convLength, nnp::Atom::Neighbor::d, nnp::Atom::Neighbor::dr, nnp::Atom::Neighbor::element, ignoreType, nnp::Atom::Neighbor::index, indexMap, mapTypeToElement, nnp::Atom::neighbors, nnp::Mode::normalize, nnp::Atom::numNeighbors, nnp::Atom::numNeighborsPerElement, structure, and nnp::Atom::Neighbor::tag.

Referenced by LAMMPS_NS::PairNNP::transferNeighborList().

Here is the caller graph for this function:

◆ process()

void InterfaceLammps::process ( )

Calculate symmetry functions, atomic neural networks and sum of local energy contributions.

Definition at line 346 of file InterfaceLammps.cpp.

347{
348#ifdef N2P2_NO_SF_GROUPS
350#else
352#endif
355 if (normalize)
356 {
358 }
360
361 return;
362}
double physicalEnergy(Structure const &structure, bool ref=true) const
Undo normalization for a given energy of structure.
Definition: Mode.cpp:1557
void addEnergyOffset(Structure &structure, bool ref=true)
Add atomic energy offsets to reference energy.
Definition: Mode.cpp:1461
void calculateAtomicNeuralNetworks(Structure &structure, bool const derivatives)
Calculate a single atomic neural network for a given atom and nn type.
Definition: Mode.cpp:1310
void calculateEnergy(Structure &structure) const
Calculate potential energy for a given structure.
Definition: Mode.cpp:1366
void calculateSymmetryFunctionGroups(Structure &structure, bool const derivatives)
Calculate all symmetry function groups for all atoms in given structure.
Definition: Mode.cpp:1230
void calculateSymmetryFunctions(Structure &structure, bool const derivatives)
Calculate all symmetry functions for all atoms in given structure.
Definition: Mode.cpp:1150

References nnp::Mode::addEnergyOffset(), nnp::Mode::calculateAtomicNeuralNetworks(), nnp::Mode::calculateEnergy(), nnp::Mode::calculateSymmetryFunctionGroups(), nnp::Mode::calculateSymmetryFunctions(), nnp::Structure::energy, nnp::Mode::normalize, nnp::Mode::physicalEnergy(), and structure.

Referenced by LAMMPS_NS::PairNNP::compute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getEnergy()

double InterfaceLammps::getEnergy ( ) const

Return sum of local energy contributions.

Returns
Sum of local energy contributions.

Definition at line 370 of file InterfaceLammps.cpp.

371{
372 return structure.energy / cfenergy;
373}

References cfenergy, nnp::Structure::energy, and structure.

Referenced by LAMMPS_NS::PairNNP::compute().

Here is the caller graph for this function:

◆ getAtomicEnergy()

double InterfaceLammps::getAtomicEnergy ( int  index) const

Return energy contribution of one atom.

Parameters
[in]indexAtom index.
Returns
energy contribution of atom with given index.
Attention
These atomic contributions are not physical!

Definition at line 375 of file InterfaceLammps.cpp.

376{
377 Atom const& a = structure.atoms.at(index);
378 Element const& e = elements.at(a.element);
379
380 if (normalize)
381 {
382 return (physical("energy", a.energy)
383 + meanEnergy
385 }
386 else
387 {
388 return (a.energy + e.getAtomicEnergyOffset()) / cfenergy;
389 }
390}
Contains element-specific data.
Definition: Element.h:39
double getAtomicEnergyOffset() const
Get atomicEnergyOffset.
Definition: Element.h:280
double meanEnergy
Definition: Mode.h:522
double physical(std::string const &property, double value) const
Undo normalization for a given property.
Definition: Mode.cpp:1549
std::vector< Element > elements
Definition: Mode.h:528
double energy
Atomic energy determined by neural network.
Definition: Atom.h:112

References nnp::Structure::atoms, cfenergy, nnp::Atom::element, nnp::Mode::elements, nnp::Atom::energy, nnp::Element::getAtomicEnergyOffset(), nnp::Mode::meanEnergy, nnp::Mode::normalize, nnp::Mode::physical(), and structure.

Referenced by LAMMPS_NS::PairNNP::compute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getForces()

void InterfaceLammps::getForces ( double *const *const &  atomF) const

Calculate forces and add to LAMMPS atomic force arrays.

Parameters
[in,out]atomFLAMMPS force array for local and ghost atoms.

Definition at line 392 of file InterfaceLammps.cpp.

393{
394 double const cfforce = cflength / cfenergy;
395 double convForce = 1.0;
396 if (normalize)
397 {
398 convForce = convLength / convEnergy;
399 }
400
401 // Loop over all local atoms. Neural network and Symmetry function
402 // derivatives are saved in the dEdG arrays of atoms and dGdr arrays of
403 // atoms and their neighbors. These are now summed up to the force
404 // contributions of local and ghost atoms.
405 Atom const* a = NULL;
406
407 for (size_t i = 0; i < structure.atoms.size(); ++i)
408 {
409 // Set pointer to atom.
410 a = &(structure.atoms.at(i));
411
412#ifndef N2P2_FULL_SFD_MEMORY
413 vector<vector<size_t> > const& tableFull
414 = elements.at(a->element).getSymmetryFunctionTable();
415#endif
416 // Loop over all neighbor atoms. Some are local, some are ghost atoms.
417 for (vector<Atom::Neighbor>::const_iterator n = a->neighbors.begin();
418 n != a->neighbors.end(); ++n)
419 {
420 // Temporarily save the neighbor index. Note: this is the index for
421 // the LAMMPS force array.
422 size_t const in = n->index;
423 // Now loop over all symmetry functions and add force contributions
424 // (local + ghost atoms).
425#ifndef N2P2_FULL_SFD_MEMORY
426 vector<size_t> const& table = tableFull.at(n->element);
427 for (size_t s = 0; s < n->dGdr.size(); ++s)
428 {
429 double const dEdG = a->dEdG[table.at(s)] * cfforce * convForce;
430#else
431 for (size_t s = 0; s < a->numSymmetryFunctions; ++s)
432 {
433 double const dEdG = a->dEdG[s] * cfforce * convForce;
434#endif
435 double const* const dGdr = n->dGdr[s].r;
436 atomF[in][0] -= dEdG * dGdr[0];
437 atomF[in][1] -= dEdG * dGdr[1];
438 atomF[in][2] -= dEdG * dGdr[2];
439 }
440 }
441 // Temporarily save the atom index. Note: this is the index for
442 // the LAMMPS force array.
443 size_t const ia = a->index;
444 // Loop over all symmetry functions and add force contributions (local
445 // atoms).
446 for (size_t s = 0; s < a->numSymmetryFunctions; ++s)
447 {
448 double const dEdG = a->dEdG[s] * cfforce * convForce;
449 double const* const dGdr = a->dGdr[s].r;
450 atomF[ia][0] -= dEdG * dGdr[0];
451 atomF[ia][1] -= dEdG * dGdr[1];
452 atomF[ia][2] -= dEdG * dGdr[2];
453 }
454 }
455
456 return;
457}
double convEnergy
Definition: Mode.h:523
std::size_t numSymmetryFunctions
Number of symmetry functions used to describe the atom environment.
Definition: Atom.h:110
std::vector< double > dEdG
Derivative of atomic energy with respect to symmetry functions.
Definition: Atom.h:136
std::vector< Vec3D > dGdr
Derivative of symmetry functions with respect to this atom's coordinates.
Definition: Atom.h:146

References nnp::Structure::atoms, cfenergy, cflength, nnp::Mode::convEnergy, nnp::Mode::convLength, nnp::Atom::dEdG, nnp::Atom::dGdr, nnp::Atom::element, nnp::Mode::elements, nnp::Atom::index, nnp::Atom::neighbors, nnp::Mode::normalize, nnp::Atom::numSymmetryFunctions, and structure.

Referenced by LAMMPS_NS::PairNNP::compute().

Here is the caller graph for this function:

◆ isInitialized()

bool nnp::InterfaceLammps::isInitialized ( ) const
inline

Check if this interface is correctly initialized.

Returns
True if initialized, False otherwise.

Definition at line 194 of file InterfaceLammps.h.

195{
196 return initialized;
197}

References initialized.

Referenced by LAMMPS_NS::PairNNP::init_style().

Here is the caller graph for this function:

◆ getMaxCutoffRadius()

double InterfaceLammps::getMaxCutoffRadius ( ) const

Get largest cutoff.

Returns
Largest cutoff of all symmetry functions.

Definition at line 364 of file InterfaceLammps.cpp.

365{
367 else return maxCutoffRadius / cflength;
368}
double maxCutoffRadius
Definition: Mode.h:520

References cflength, nnp::Mode::convLength, nnp::Mode::maxCutoffRadius, and nnp::Mode::normalize.

Referenced by LAMMPS_NS::PairNNP::init_style(), and initialize().

Here is the caller graph for this function:

◆ getEWBufferSize()

long InterfaceLammps::getEWBufferSize ( ) const

Calculate buffer size for extrapolation warning communication.

Returns
Buffer size.

Definition at line 459 of file InterfaceLammps.cpp.

460{
461 long bs = 0;
462#ifndef N2P2_NO_MPI
463 int ss = 0; // size_t size.
464 int ds = 0; // double size.
465 int cs = 0; // char size.
466 MPI_Pack_size(1, MPI_SIZE_T, MPI_COMM_WORLD, &ss);
467 MPI_Pack_size(1, MPI_DOUBLE, MPI_COMM_WORLD, &ds);
468 MPI_Pack_size(1, MPI_CHAR , MPI_COMM_WORLD, &cs);
469
470 for (vector<Element>::const_iterator it = elements.begin();
471 it != elements.end(); ++it)
472 {
473 map<size_t, SymFncStatistics::Container> const& m
474 = it->statistics.data;
475 bs += ss; // n.
476 for (map<size_t, SymFncStatistics::Container>::const_iterator
477 it2 = m.begin(); it2 != m.end(); ++it2)
478 {
479 bs += ss; // index (it2->first).
480 bs += ss; // countEW (it2->second.countEW).
481 bs += ss; // type (it2->second.type).
482 bs += ds; // Gmin (it2->second.Gmin).
483 bs += ds; // Gmax (it2->second.Gmax).
484 bs += ss; // element.length() (it2->second.element.length()).
485 bs += (it2->second.element.length() + 1) * cs; // element.
486 size_t countEW = it2->second.countEW;
487 bs += countEW * ss; // indexStructureEW.
488 bs += countEW * ss; // indexAtomEW.
489 bs += countEW * ds; // valueEW.
490 }
491 }
492#endif
493 return bs;
494}
#define MPI_SIZE_T
Definition: mpi-extra.h:22

References nnp::Mode::elements, and MPI_SIZE_T.

Referenced by LAMMPS_NS::PairNNP::handleExtrapolationWarnings().

Here is the caller graph for this function:

◆ fillEWBuffer()

void InterfaceLammps::fillEWBuffer ( char *const &  buf,
int  bs 
) const

Fill provided buffer with extrapolation warning entries.

Parameters
[in,out]bufCommunication buffer to fill.
[in]bsBuffer size.

Definition at line 496 of file InterfaceLammps.cpp.

497{
498#ifndef N2P2_NO_MPI
499 int p = 0;
500 for (vector<Element>::const_iterator it = elements.begin();
501 it != elements.end(); ++it)
502 {
503 map<size_t, SymFncStatistics::Container> const& m =
504 it->statistics.data;
505 size_t n = m.size();
506 MPI_Pack((void *) &(n), 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
507 for (map<size_t, SymFncStatistics::Container>::const_iterator
508 it2 = m.begin(); it2 != m.end(); ++it2)
509 {
510 MPI_Pack((void *) &(it2->first ), 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
511 size_t countEW = it2->second.countEW;
512 MPI_Pack((void *) &(countEW ), 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
513 MPI_Pack((void *) &(it2->second.type ), 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
514 MPI_Pack((void *) &(it2->second.Gmin ), 1, MPI_DOUBLE, buf, bs, &p, MPI_COMM_WORLD);
515 MPI_Pack((void *) &(it2->second.Gmax ), 1, MPI_DOUBLE, buf, bs, &p, MPI_COMM_WORLD);
516 // it2->element
517 size_t ts = it2->second.element.length() + 1;
518 MPI_Pack((void *) &ts , 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
519 MPI_Pack((void *) it2->second.element.c_str() , ts, MPI_CHAR , buf, bs, &p, MPI_COMM_WORLD);
520 MPI_Pack((void *) &(it2->second.indexStructureEW.front()), countEW, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
521 MPI_Pack((void *) &(it2->second.indexAtomEW.front() ), countEW, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
522 MPI_Pack((void *) &(it2->second.valueEW.front() ), countEW, MPI_DOUBLE, buf, bs, &p, MPI_COMM_WORLD);
523 }
524 }
525#endif
526 return;
527}
size_t p
Definition: nnp-cutoff.cpp:33

References nnp::Mode::elements, MPI_SIZE_T, and p.

Referenced by LAMMPS_NS::PairNNP::handleExtrapolationWarnings().

Here is the caller graph for this function:

◆ extractEWBuffer()

void InterfaceLammps::extractEWBuffer ( char const *const &  buf,
int  bs 
)

Extract given buffer to symmetry function statistics class.

Parameters
[in]bufBuffer with extrapolation warnings data.
[in]bsBuffer size.

Definition at line 529 of file InterfaceLammps.cpp.

530{
531#ifndef N2P2_NO_MPI
532 int p = 0;
533 for (vector<Element>::iterator it = elements.begin();
534 it != elements.end(); ++it)
535 {
536 size_t n = 0;
537 MPI_Unpack((void *) buf, bs, &p, &(n), 1, MPI_SIZE_T, MPI_COMM_WORLD);
538 for (size_t i = 0; i < n; ++i)
539 {
540 size_t index = 0;
541 MPI_Unpack((void *) buf, bs, &p, &(index), 1, MPI_SIZE_T, MPI_COMM_WORLD);
542 SymFncStatistics::Container& d = it->statistics.data[index];
543 size_t countEW = 0;
544 MPI_Unpack((void *) buf, bs, &p, &(countEW ), 1, MPI_SIZE_T, MPI_COMM_WORLD);
545 MPI_Unpack((void *) buf, bs, &p, &(d.type ), 1, MPI_SIZE_T, MPI_COMM_WORLD);
546 MPI_Unpack((void *) buf, bs, &p, &(d.Gmin ), 1, MPI_DOUBLE, MPI_COMM_WORLD);
547 MPI_Unpack((void *) buf, bs, &p, &(d.Gmax ), 1, MPI_DOUBLE, MPI_COMM_WORLD);
548 // d.element
549 size_t ts = 0;
550 MPI_Unpack((void *) buf, bs, &p, &ts , 1, MPI_SIZE_T, MPI_COMM_WORLD);
551 char* element = new char[ts];
552 MPI_Unpack((void *) buf, bs, &p, element , ts, MPI_CHAR , MPI_COMM_WORLD);
553 d.element = element;
554 delete[] element;
555 // indexStructureEW.
556 d.indexStructureEW.resize(d.countEW + countEW);
557 MPI_Unpack((void *) buf, bs, &p, &(d.indexStructureEW[d.countEW]), countEW, MPI_SIZE_T, MPI_COMM_WORLD);
558 // indexAtomEW.
559 d.indexAtomEW.resize(d.countEW + countEW);
560 MPI_Unpack((void *) buf, bs, &p, &(d.indexAtomEW[d.countEW] ), countEW, MPI_SIZE_T, MPI_COMM_WORLD);
561 // valueEW.
562 d.valueEW.resize(d.countEW + countEW);
563 MPI_Unpack((void *) buf, bs, &p, &(d.valueEW[d.countEW] ), countEW, MPI_DOUBLE, MPI_COMM_WORLD);
564
565 d.countEW += countEW;
566 }
567 }
568#endif
569 return;
570}
double d
Definition: nnp-cutoff.cpp:34
Struct containing statistics gathered during symmetry function calculation.

References d, nnp::Mode::elements, MPI_SIZE_T, and p.

Referenced by LAMMPS_NS::PairNNP::handleExtrapolationWarnings().

Here is the caller graph for this function:

◆ writeExtrapolationWarnings()

void InterfaceLammps::writeExtrapolationWarnings ( )

Write extrapolation warnings to log.

Definition at line 572 of file InterfaceLammps.cpp.

573{
574 for (vector<Element>::const_iterator it = elements.begin();
575 it != elements.end(); ++it)
576 {
577 vector<string> vs = it->statistics.getExtrapolationWarningLines();
578 for (vector<string>::const_iterator it2 = vs.begin();
579 it2 != vs.end(); ++it2)
580 {
581 log << (*it2);
582 }
583 }
584
585 return;
586}

References nnp::Mode::elements, and nnp::Mode::log.

Referenced by LAMMPS_NS::PairNNP::handleExtrapolationWarnings(), and initialize().

Here is the caller graph for this function:

◆ clearExtrapolationWarnings()

void InterfaceLammps::clearExtrapolationWarnings ( )

Clear extrapolation warnings storage.

Definition at line 588 of file InterfaceLammps.cpp.

589{
590 for (vector<Element>::iterator it = elements.begin();
591 it != elements.end(); ++it)
592 {
593 it->statistics.clear();
594 }
595
596 return;
597}

References nnp::Mode::elements.

Referenced by LAMMPS_NS::PairNNP::handleExtrapolationWarnings().

Here is the caller graph for this function:

Member Data Documentation

◆ myRank

int nnp::InterfaceLammps::myRank
protected

Process rank.

Definition at line 161 of file InterfaceLammps.h.

Referenced by initialize(), and setLocalAtoms().

◆ initialized

bool nnp::InterfaceLammps::initialized
protected

Initialization state.

Definition at line 163 of file InterfaceLammps.h.

Referenced by initialize(), and isInitialized().

◆ showew

bool nnp::InterfaceLammps::showew
protected

Corresponds to LAMMPS showew keyword.

Definition at line 165 of file InterfaceLammps.h.

Referenced by initialize().

◆ resetew

bool nnp::InterfaceLammps::resetew
protected

Corresponds to LAMMPS resetew keyword.

Definition at line 167 of file InterfaceLammps.h.

Referenced by initialize().

◆ showewsum

int nnp::InterfaceLammps::showewsum
protected

Corresponds to LAMMPS showewsum keyword.

Definition at line 169 of file InterfaceLammps.h.

Referenced by initialize().

◆ maxew

int nnp::InterfaceLammps::maxew
protected

Corresponds to LAMMPS maxew keyword.

Definition at line 171 of file InterfaceLammps.h.

Referenced by initialize().

◆ cflength

double nnp::InterfaceLammps::cflength
protected

Corresponds to LAMMPS cflength keyword.

Definition at line 173 of file InterfaceLammps.h.

Referenced by addNeighbor(), getForces(), getMaxCutoffRadius(), and initialize().

◆ cfenergy

double nnp::InterfaceLammps::cfenergy
protected

Corresponds to LAMMPS cfenergy keyword.

Definition at line 175 of file InterfaceLammps.h.

Referenced by getAtomicEnergy(), getEnergy(), getForces(), and initialize().

◆ emap

std::string nnp::InterfaceLammps::emap
protected

Corresponds to LAMMPS map keyword.

Definition at line 177 of file InterfaceLammps.h.

Referenced by initialize().

◆ indexMap

std::vector<size_t> nnp::InterfaceLammps::indexMap
protected

Map from LAMMPS index to n2p2 atom index.

Definition at line 179 of file InterfaceLammps.h.

Referenced by addNeighbor(), and setLocalAtoms().

◆ ignoreType

std::map<int, bool> nnp::InterfaceLammps::ignoreType
protected

True if atoms of this LAMMPS type will be ignored.

Definition at line 181 of file InterfaceLammps.h.

Referenced by addNeighbor(), initialize(), and setLocalAtoms().

◆ mapTypeToElement

std::map<int, std::size_t> nnp::InterfaceLammps::mapTypeToElement
protected

Map from LAMMPS type to n2p2 element index.

Definition at line 183 of file InterfaceLammps.h.

Referenced by addNeighbor(), initialize(), and setLocalAtoms().

◆ mapElementToType

std::map<std::size_t, int> nnp::InterfaceLammps::mapElementToType
protected

Map from n2p2 element index to LAMMPS type.

Definition at line 185 of file InterfaceLammps.h.

Referenced by initialize().

◆ structure

Structure nnp::InterfaceLammps::structure
protected

Structure containing local atoms.

Definition at line 187 of file InterfaceLammps.h.

Referenced by addNeighbor(), getAtomicEnergy(), getEnergy(), getForces(), initialize(), process(), setLocalAtoms(), and setLocalTags().


The documentation for this class was generated from the following files: