30#define TOLCUTOFF 1.0E-2
35InterfaceLammps::InterfaceLammps() : myRank (0 ),
48 char const*
const& emap,
68 string dir(directory);
69 char const separator =
'/';
70 if (dir.back() != separator) dir += separator;
75 bool collectStatistics =
false;
76 bool collectExtrapolationWarnings =
false;
78 bool stopOnExtrapolationWarnings =
false;
81 collectExtrapolationWarnings =
true;
84 collectExtrapolationWarnings,
86 stopOnExtrapolationWarnings);
90 log <<
"*** SETUP: LAMMPS INTERFACE *************"
91 "**************************************\n";
96 log <<
"Individual extrapolation warnings will be shown.\n";
100 log <<
"Individual extrapolation warnings will not be shown.\n";
105 log <<
strpr(
"Extrapolation warning summary will be shown every %d"
110 log <<
"Extrapolation warning summary will not be shown.\n";
115 log <<
strpr(
"The simulation will be stopped when %d extrapolation"
116 " warnings are exceeded.\n",
maxew);
120 log <<
"No extrapolation warning limit set.\n";
125 log <<
"Extrapolation warning counter is reset every time step.\n";
129 log <<
"Extrapolation warnings are accumulated over all time steps.\n";
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";
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)
147 throw runtime_error(
"ERROR: LAMMPS cutoff via pair_coeff keyword is"
148 " smaller than maximum symmetry function"
151 else if (fabs(sfCutoff - lammpsCutoff) / lammpsCutoff >
TOLCUTOFF)
153 log <<
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
154 log <<
"WARNING: Potential length units mismatch!\n";
155 log <<
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
159 log <<
"Cutoff radii are consistent.\n";
162 log <<
"-----------------------------------------"
163 "--------------------------------------\n";
164 log <<
"Element mapping string from LAMMPS to n2p2: \""
165 + this->emap +
"\"\n";
167 if (this->emap ==
"")
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",
176 log <<
"Element mapping string empty, creating default mapping.\n";
177 for (
int i = 0; i < lammpsNtypes; ++i)
190 throw runtime_error(
strpr(
"ERROR: Element mapping is inconsistent,"
191 " NNP elements: %zu,"
192 " emap elements: %zu.\n",
196 for (
string s : emapSplit)
198 vector<string> typeString =
split(s,
':');
199 if (typeString.size() != 2)
201 throw runtime_error(
strpr(
"ERROR: Invalid element mapping "
202 "string: \"%s\".\n", s.c_str()));
204 int t = stoi(typeString.at(0));
205 if (t > lammpsNtypes)
207 throw runtime_error(
strpr(
"ERROR: LAMMPS type \"%d\" not "
208 "present, there are only %d types "
209 "defined.\n", t, lammpsNtypes));
217 log <<
"CAUTION: Please ensure that this mapping between LAMMPS\n";
218 log <<
" atom types and NNP elements is consistent:\n";
220 log <<
"---------------------------\n";
221 log <<
"LAMMPS type | NNP element\n";
222 log <<
"---------------------------\n";
223 for (
int i = 1; i <= lammpsNtypes; ++i)
228 log <<
strpr(
"%11d <-> %2s (%3zu)\n",
241 log <<
"---------------------------\n";
243 log <<
"NNP setup for LAMMPS completed.\n";
245 log <<
"*****************************************"
246 "**************************************\n";
254 int const*
const atomType)
268 indexMap.resize(numAtomsLocal, numeric_limits<size_t>::max());
269 for (
int i = 0; i < numAtomsLocal; i++)
322 indexMap.at(i) == numeric_limits<size_t>::max())
return;
348#ifdef N2P2_NO_SF_GROUPS
395 double convForce = 1.0;
405 Atom const* a = NULL;
412#ifndef N2P2_FULL_SFD_MEMORY
413 vector<vector<size_t> >
const& tableFull
417 for (vector<Atom::Neighbor>::const_iterator n = a->
neighbors.begin();
422 size_t const in = n->index;
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)
429 double const dEdG = a->
dEdG[table.at(s)] * cfforce * convForce;
433 double const dEdG = a->
dEdG[s] * cfforce * convForce;
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];
443 size_t const ia = a->
index;
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];
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);
470 for (vector<Element>::const_iterator it =
elements.begin();
473 map<size_t, SymFncStatistics::Container>
const& m
474 = it->statistics.data;
476 for (map<size_t, SymFncStatistics::Container>::const_iterator
477 it2 = m.begin(); it2 != m.end(); ++it2)
485 bs += (it2->second.element.length() + 1) * cs;
486 size_t countEW = it2->second.countEW;
500 for (vector<Element>::const_iterator it =
elements.begin();
503 map<size_t, SymFncStatistics::Container>
const& m =
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)
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);
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);
533 for (vector<Element>::iterator it =
elements.begin();
537 MPI_Unpack((
void *) buf, bs, &
p, &(n), 1,
MPI_SIZE_T, MPI_COMM_WORLD);
538 for (
size_t i = 0; i < n; ++i)
541 MPI_Unpack((
void *) buf, bs, &
p, &(index), 1,
MPI_SIZE_T, MPI_COMM_WORLD);
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);
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);
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);
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);
562 d.valueEW.resize(
d.countEW + countEW);
563 MPI_Unpack((
void *) buf, bs, &
p, &(
d.valueEW[
d.countEW] ), countEW, MPI_DOUBLE, MPI_COMM_WORLD);
565 d.countEW += countEW;
574 for (vector<Element>::const_iterator it =
elements.begin();
577 vector<string> vs = it->statistics.getExtrapolationWarningLines();
578 for (vector<string>::const_iterator it2 = vs.begin();
579 it2 != vs.end(); ++it2)
590 for (vector<Element>::iterator it =
elements.begin();
593 it->statistics.clear();
std::size_t size() const
Get element map size.
std::size_t atomicNumber(std::size_t index) const
Get atomic number from element index.
Contains element-specific data.
double getAtomicEnergyOffset() const
Get atomicEnergyOffset.
Structure structure
Structure containing local atoms.
long getEWBufferSize() const
Calculate buffer size for extrapolation warning communication.
bool initialized
Initialization state.
void process()
Calculate symmetry functions, atomic neural networks and sum of local energy contributions.
void extractEWBuffer(char const *const &buf, int bs)
Extract given buffer to symmetry function statistics class.
std::map< int, bool > ignoreType
True if atoms of this LAMMPS type will be ignored.
void setLocalTags(int const *const atomTag)
Set atom tags (int version, -DLAMMPS_SMALLBIG).
std::map< int, std::size_t > mapTypeToElement
Map from LAMMPS type to n2p2 element index.
double getEnergy() const
Return sum of local energy contributions.
void fillEWBuffer(char *const &buf, int bs) const
Fill provided buffer with extrapolation warning entries.
double getAtomicEnergy(int index) const
Return energy contribution of one atom.
void getForces(double *const *const &atomF) const
Calculate forces and add to LAMMPS atomic force arrays.
bool resetew
Corresponds to LAMMPS resetew keyword.
void writeExtrapolationWarnings()
Write extrapolation warnings to log.
std::vector< size_t > indexMap
Map from LAMMPS index to n2p2 atom index.
double cfenergy
Corresponds to LAMMPS cfenergy keyword.
void setLocalAtoms(int numAtomsLocal, int const *const atomType)
(Re)set structure to contain only local LAMMPS atoms.
bool showew
Corresponds to LAMMPS showew keyword.
int showewsum
Corresponds to LAMMPS showewsum keyword.
void clearExtrapolationWarnings()
Clear extrapolation warnings storage.
std::map< std::size_t, int > mapElementToType
Map from n2p2 element index to LAMMPS type.
double getMaxCutoffRadius() const
Get largest cutoff.
int maxew
Corresponds to LAMMPS maxew keyword.
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).
double cflength
Corresponds to LAMMPS cflength keyword.
std::string emap
Corresponds to LAMMPS map keyword.
bool writeToStdout
Turn on/off output to stdout.
double physicalEnergy(Structure const &structure, bool ref=true) const
Undo normalization for a given energy of structure.
ElementMap elementMap
Global element map, populated by setupElementMap().
void addEnergyOffset(Structure &structure, bool ref=true)
Add atomic energy offsets to reference energy.
void initialize()
Write welcome message with version information.
double physical(std::string const &property, double value) const
Undo normalization for a given property.
std::vector< Element > elements
void calculateAtomicNeuralNetworks(Structure &structure, bool const derivatives)
Calculate a single atomic neural network for a given atom and nn type.
virtual void setupNeuralNetworkWeights(std::string const &fileNameFormatShort="weights.%03zu.data", std::string const &fileNameFormatCharge="weightse.%03zu.data")
Set up neural network weights from files.
void calculateEnergy(Structure &structure) const
Calculate potential energy for a given structure.
void calculateSymmetryFunctionGroups(Structure &structure, bool const derivatives)
Calculate all symmetry function groups for all atoms in given structure.
virtual void setupSymmetryFunctionScaling(std::string const &fileName="scaling.data")
Set up symmetry function scaling from file.
void setupGeneric(bool skipNormalize=false)
Combine multiple setup routines and provide a basic NNP setup.
void calculateSymmetryFunctions(Structure &structure, bool const derivatives)
Calculate all symmetry functions for all atoms in given structure.
void setupSymmetryFunctionStatistics(bool collectStatistics, bool collectExtrapolationWarnings, bool writeExtrapolationWarnings, bool stopOnExtrapolationWarnings)
Set up symmetry function statistics collection.
void loadSettingsFile(std::string const &fileName="input.nn")
Open settings file and load all keywords into memory.
string strpr(const char *format,...)
String version of printf function.
string trim(string const &line, string const &whitespace)
Remove leading and trailing whitespaces from string.
vector< string > split(string const &input, char delimiter)
Split string at each delimiter.
string reduce(string const &line, string const &whitespace, string const &fill)
Replace multiple whitespaces with fill.
Struct to store information on neighbor atoms.
std::size_t index
Index of neighbor atom.
std::size_t element
Element index of neighbor atom.
double d
Distance to neighbor atom.
Vec3D dr
Distance vector to neighbor atom.
int64_t tag
Tag of neighbor atom.
Storage for a single atom.
std::vector< Neighbor > neighbors
Neighbor array (maximum number defined in macros.h.
std::size_t numSymmetryFunctions
Number of symmetry functions used to describe the atom environment.
std::vector< double > dEdG
Derivative of atomic energy with respect to symmetry functions.
bool hasSymmetryFunctionDerivatives
If symmetry function derivatives are saved for this atom.
std::size_t index
Index number of this atom.
std::vector< Vec3D > dGdr
Derivative of symmetry functions with respect to this atom's coordinates.
std::size_t indexStructure
Index number of structure this atom belongs to.
std::size_t element
Element index of this atom.
bool hasSymmetryFunctions
If symmetry function values are saved for this atom.
double energy
Atomic energy determined by neural network.
std::vector< std::size_t > numNeighborsPerElement
Number of neighbors per element.
std::size_t numNeighbors
Total number of neighbors.
std::vector< std::size_t > numAtomsPerElement
Number of atoms of each element in this structure.
void setElementMap(ElementMap const &elementMap)
Set element map of structure.
std::size_t index
Index number of this structure.
bool hasSymmetryFunctionDerivatives
If symmetry function derivatives are saved for each atom.
double energy
Potential energy determined by neural network.
std::size_t numAtoms
Total number of atoms present in this structure.
std::vector< Atom > atoms
Vector of all atoms in this structure.
bool hasNeighborList
If the neighbor list has been calculated.
bool hasSymmetryFunctions
If symmetry function values are saved for each atom.
Struct containing statistics gathered during symmetry function calculation.