n2p2 - A neural network potential package
InterfaceLammps.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#ifndef N2P2_NO_MPI
18#include <mpi.h>
19#include "mpi-extra.h"
20#endif
21#include "InterfaceLammps.h"
22#include "Atom.h"
23#include "Element.h"
24#include "utility.h"
25#include <Eigen/Dense>
26#include <cmath>
27#include <string>
28#include <iostream>
29#include <limits>
30//#include <stdexcept>
31
32#define TOLCUTOFF 1.0E-2
33
34using namespace std;
35using namespace nnp;
36using namespace Eigen;
37
38InterfaceLammps::InterfaceLammps() : myRank (0 ),
39 initialized (false),
40 hasGlobalStructure (false),
41 showew (true ),
42 resetew (false),
43 showewsum (0 ),
44 maxew (0 ),
45 cflength (1.0 ),
46 cfenergy (1.0 )
47
48{
49}
50
51void InterfaceLammps::initialize(char const* const& directory,
52 char const* const& emap,
53 bool showew,
54 bool resetew,
55 int showewsum,
56 int maxew,
57 double cflength,
58 double cfenergy,
59 double lammpsCutoff,
60 int lammpsNtypes,
61 int myRank)
62{
63 this->emap = emap;
64 this->showew = showew;
65 this->resetew = resetew;
66 this->showewsum = showewsum;
67 this->maxew = maxew;
68 this->cflength = cflength;
69 this->cfenergy = cfenergy;
70 this->myRank = myRank;
71 log.writeToStdout = false;
72 string dir(directory);
73 char const separator = '/';
74 if (dir.back() != separator) dir += separator;
76 loadSettingsFile(dir + "input.nn");
77 setupGeneric(dir);
78 setupSymmetryFunctionScaling(dir + "scaling.data");
79 bool collectStatistics = false;
80 bool collectExtrapolationWarnings = false;
81 bool writeExtrapolationWarnings = false;
82 bool stopOnExtrapolationWarnings = false;
83 if (showew == true || showewsum > 0 || maxew >= 0)
84 {
85 collectExtrapolationWarnings = true;
86 }
87 setupSymmetryFunctionStatistics(collectStatistics,
88 collectExtrapolationWarnings,
90 stopOnExtrapolationWarnings);
92
93 log << "\n";
94 log << "*** SETUP: LAMMPS INTERFACE *************"
95 "**************************************\n";
96 log << "\n";
97
98 if (showew)
99 {
100 log << "Individual extrapolation warnings will be shown.\n";
101 }
102 else
103 {
104 log << "Individual extrapolation warnings will not be shown.\n";
105 }
106
107 if (showewsum != 0)
108 {
109 log << strpr("Extrapolation warning summary will be shown every %d"
110 " timesteps.\n", showewsum);
111 }
112 else
113 {
114 log << "Extrapolation warning summary will not be shown.\n";
115 }
116
117 if (maxew != 0)
118 {
119 log << strpr("The simulation will be stopped when %d extrapolation"
120 " warnings are exceeded.\n", maxew);
121 }
122 else
123 {
124 log << "No extrapolation warning limit set.\n";
125 }
126
127 if (resetew)
128 {
129 log << "Extrapolation warning counter is reset every time step.\n";
130 }
131 else
132 {
133 log << "Extrapolation warnings are accumulated over all time steps.\n";
134 }
135
136 log << "-----------------------------------------"
137 "--------------------------------------\n";
138 log << "CAUTION: If the LAMMPS unit system differs from the one used\n";
139 log << " during NN training, appropriate conversion factors\n";
140 log << " must be provided (see keywords cflength and cfenergy).\n";
141 log << "\n";
142 log << strpr("Length unit conversion factor: %24.16E\n", cflength);
143 log << strpr("Energy unit conversion factor: %24.16E\n", cfenergy);
144 double sfCutoff = getMaxCutoffRadius();
145 log << "\n";
146 log << "Checking consistency of cutoff radii (in LAMMPS units):\n";
147 log << strpr("LAMMPS Cutoff (via pair_coeff) : %11.3E\n", lammpsCutoff);
148 log << strpr("Maximum symmetry function cutoff: %11.3E\n", sfCutoff);
149 if (lammpsCutoff < sfCutoff)
150 {
151 throw runtime_error("ERROR: LAMMPS cutoff via pair_coeff keyword is"
152 " smaller than maximum symmetry function"
153 " cutoff.\n");
154 }
155 else if (fabs(sfCutoff - lammpsCutoff) / lammpsCutoff > TOLCUTOFF)
156 {
157 log << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
158 log << "WARNING: Potential length units mismatch!\n";
159 log << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
160 }
161 else
162 {
163 log << "Cutoff radii are consistent.\n";
164 }
165
166 log << "-----------------------------------------"
167 "--------------------------------------\n";
168 log << "Element mapping string from LAMMPS to n2p2: \""
169 + this->emap + "\"\n";
170 // Create default element mapping.
171 if (this->emap == "")
172 {
173 if (elementMap.size() != (size_t)lammpsNtypes)
174 {
175 throw runtime_error(strpr("ERROR: No element mapping given and "
176 "number of LAMMPS atom types (%d) and "
177 "NNP elements (%zu) does not match.\n",
178 lammpsNtypes, elementMap.size()));
179 }
180 log << "Element mapping string empty, creating default mapping.\n";
181 for (int i = 0; i < lammpsNtypes; ++i)
182 {
183 mapTypeToElement[i + 1] = i;
184 mapElementToType[i] = i + 1;
185 }
186 }
187 // Read element mapping from pair_style argument.
188 else
189 {
190 vector<string> emapSplit = split(reduce(trim(this->emap), " \t", ""),
191 ',');
192 if (elementMap.size() < emapSplit.size())
193 {
194 throw runtime_error(strpr("ERROR: Element mapping is inconsistent,"
195 " NNP elements: %zu,"
196 " emap elements: %zu.\n",
198 emapSplit.size()));
199 }
200 for (string s : emapSplit)
201 {
202 vector<string> typeString = split(s, ':');
203 if (typeString.size() != 2)
204 {
205 throw runtime_error(strpr("ERROR: Invalid element mapping "
206 "string: \"%s\".\n", s.c_str()));
207 }
208 int t = stoi(typeString.at(0));
209 if (t > lammpsNtypes)
210 {
211 throw runtime_error(strpr("ERROR: LAMMPS type \"%d\" not "
212 "present, there are only %d types "
213 "defined.\n", t, lammpsNtypes));
214 }
215 size_t e = elementMap[typeString.at(1)];
216 mapTypeToElement[t] = e;
217 mapElementToType[e] = t;
218 }
219 }
220 log << "\n";
221 log << "CAUTION: Please ensure that this mapping between LAMMPS\n";
222 log << " atom types and NNP elements is consistent:\n";
223 log << "\n";
224 log << "---------------------------\n";
225 log << "LAMMPS type | NNP element\n";
226 log << "---------------------------\n";
227 for (int i = 1; i <= lammpsNtypes; ++i)
228 {
229 if (mapTypeToElement.find(i) != mapTypeToElement.end())
230 {
231 size_t e = mapTypeToElement.at(i);
232 log << strpr("%11d <-> %2s (%3zu)\n",
233 i,
234 elementMap[e].c_str(),
236 ignoreType[i] = false;
237 }
238 else
239 {
240 log << strpr("%11d <-> --\n", i);
241 ignoreType[i] = true;
242
243 }
244 }
245 log << "---------------------------\n";
246 log << "\n";
247 log << "NNP setup for LAMMPS completed.\n";
248
249 log << "*****************************************"
250 "**************************************\n";
251
253
254 initialized = true;
255}
256
258{
259 hasGlobalStructure = status;
260}
261
263{
264 return hasGlobalStructure;
265}
266
267void InterfaceLammps::setLocalAtoms(int numAtomsLocal,
268 int const* const atomType)
269{
270 for (size_t i = 0; i < numElements; ++i)
271 {
273 }
279 structure.energy = 0.0;
280 structure.atoms.clear();
281 indexMap.clear();
282 structure.atoms.reserve(numAtomsLocal);
283 indexMap.resize(numAtomsLocal, numeric_limits<size_t>::max());
284 for (int i = 0; i < numAtomsLocal; i++)
285 {
286 if (ignoreType[atomType[i]]) continue;
289 structure.atoms.push_back(Atom());
290 Atom& a = structure.atoms.back();
291 a.index = i;
293 a.element = mapTypeToElement[atomType[i]];
294 a.numNeighbors = 0;
295 a.hasSymmetryFunctions = false;
297 a.neighbors.clear();
298 a.numNeighborsPerElement.clear();
301 }
302
303 return;
304}
305
306void InterfaceLammps::setLocalAtomPositions(double const* const* const atomPos)
307{
308 for (size_t i = 0; i < structure.numAtoms; ++i)
309 {
310 Atom& a = structure.atoms.at(i);
311 a.r[0] = atomPos[i][0] * cflength;
312 a.r[1] = atomPos[i][1] * cflength;
313 a.r[2] = atomPos[i][2] * cflength;
314 if (normalize)
315 {
316 a.r[0] *= convLength;
317 a.r[1] *= convLength;
318 a.r[2] *= convLength;
319 }
320 }
321
322 return;
323}
324
325void InterfaceLammps::setLocalTags(int const* const atomTag)
326{
327 for (size_t i = 0; i < structure.atoms.size(); i++)
328 {
329 // Implicit conversion from int to int64_t!
330 structure.atoms.at(i).tag = atomTag[i];
331 }
332
333 return;
334}
335
336void InterfaceLammps::setLocalTags(int64_t const* const atomTag)
337{
338 for (size_t i = 0; i < structure.atoms.size(); i++)
339 {
340 structure.atoms.at(i).tag = atomTag[i];
341 }
342
343 return;
344}
345
346void InterfaceLammps::setBoxVectors(double const* boxlo,
347 double const* boxhi,
348 double const xy,
349 double const xz,
350 double const yz)
351{
352 structure.isPeriodic = true;
353
354 // Box vector a
355 structure.box[0][0] = boxhi[0] - boxlo[0];
356 structure.box[0][1] = 0;
357 structure.box[0][2] = 0;
358
359 // Box vector b
360 structure.box[1][0] = xy;
361 structure.box[1][1] = boxhi[1] - boxlo[1];
362 structure.box[1][2] = 0;
363
364 // Box vector c
365 structure.box[2][0] = xz;
366 structure.box[2][1] = yz;
367 structure.box[2][2] = boxhi[2] - boxlo[2];
368
369 // LAMMPS may set triclinic = 1 even if the following condition is not
370 // satisfied.
371 if (structure.box[0][1] > numeric_limits<double>::min() ||
372 structure.box[0][2] > numeric_limits<double>::min() ||
373 structure.box[1][0] > numeric_limits<double>::min() ||
374 structure.box[1][2] > numeric_limits<double>::min() ||
375 structure.box[2][0] > numeric_limits<double>::min() ||
376 structure.box[2][1] > numeric_limits<double>::min())
377 {
378 structure.isTriclinic = true;
379 }
380
381 for(size_t i = 0; i < 3; ++i)
382 {
383 structure.box[i] *= cflength;
385 }
386
389 //cout << "Box vectors: \n";
390 //for(size_t i = 0; i < 3; ++i)
391 //{
392 // for(size_t j = 0; j < 3; ++j)
393 // {
394 // cout << structure.box[i][j] / convLength << " ";
395 // }
396 // cout << endl;
397 //}
398
399}
400
401void InterfaceLammps::allocateNeighborlists(int const* const numneigh)
402{
403 for(size_t i = 0; i < structure.numAtoms; ++i)
404 {
405 auto& a = structure.atoms.at(i);
406 a.neighbors.reserve(numneigh[i]);
407 }
408}
409
411 int j,
412 int64_t tag,
413 int type,
414 double dx,
415 double dy,
416 double dz,
417 double d2)
418{
419 if (ignoreType[type] ||
420 indexMap.at(i) == numeric_limits<size_t>::max()) return;
421 Atom& a = structure.atoms[indexMap.at(i)];
422 a.numNeighbors++;
423 a.neighbors.push_back(Atom::Neighbor());
425 Atom::Neighbor& n = a.neighbors.back();
426
427 n.index = j;
428 n.tag = tag;
429 n.element = mapTypeToElement[type];
430 n.dr[0] = dx * cflength;
431 n.dr[1] = dy * cflength;
432 n.dr[2] = dz * cflength;
433 n.d = sqrt(d2) * cflength;
434 if (normalize)
435 {
436 n.dr *= convLength;
437 n.d *= convLength;
438 }
439
440 return;
441}
442
443
445{
447 {
448 for (auto& a : structure.atoms)
449 {
450 a.hasNeighborList = true;
451 }
452 // Ewald summation cut-off depends on box vectors.
459 }
460
461}
462
464{
465#ifdef N2P2_NO_SF_GROUPS
467#else
469#endif
472 {
476 }
478 if (nnpType == NNPType::HDNNP_4G ||
480 if (normalize)
481 {
483 }
485
486 return;
487}
488
490{
492 else return maxCutoffRadius / cflength;
493}
494
496{
497 double cutoff = 0;
499 {
505 if (normalize) cutoff /= convLength;
506 }
507 else cutoff = getMaxCutoffRadius();
508 return cutoff;
509}
510
512{
513 return structure.energy / cfenergy;
514}
515
516double InterfaceLammps::getAtomicEnergy(int index) const
517{
518 Atom const& a = structure.atoms.at(index);
519 Element const& e = elements.at(a.element);
520
521 if (normalize)
522 {
523 return (physical("energy", a.energy)
524 + meanEnergy
526 }
527 else
528 {
529 return (a.energy + e.getAtomicEnergyOffset()) / cfenergy;
530 }
531}
532
533void InterfaceLammps::getForces(double* const* const& atomF) const
534{
535 double const cfforce = cflength / cfenergy;
536 double convForce = 1.0;
537 if (normalize)
538 {
539 convForce = convLength / convEnergy;
540 }
541
542 // Loop over all local atoms. Neural network and Symmetry function
543 // derivatives are saved in the dEdG arrays of atoms and dGdr arrays of
544 // atoms and their neighbors. These are now summed up to the force
545 // contributions of local and ghost atoms.
546 for (auto const& a : structure.atoms)
547 {
548 size_t const ia = a.index;
549 Vec3D selfForce = a.calculateSelfForceShort();
550 selfForce *= cfforce * convForce;
551 // TODO: ia is not the right index when some atom types are excluded / ignored
552 // (see use of indexmap)
553 add3DVecToArray(atomF[ia], selfForce);
554
555#ifndef N2P2_FULL_SFD_MEMORY
556 vector<vector<size_t> > const& tableFull
557 = elements.at(a.element).getSymmetryFunctionTable();
558#endif
559 // Loop over all neighbor atoms. Some are local, some are ghost atoms.
560
561 //for (auto const& n : a.neighbors)
562 size_t const numNeighbors = a.getStoredMinNumNeighbors(maxCutoffRadius);
563#ifdef _OPENMP
564 #pragma omp parallel for
565#endif
566 for (size_t k = 0; k < numNeighbors; ++k)
567 {
568 Atom::Neighbor const& n = a.neighbors[k];
569 // Temporarily save the neighbor index. Note: this is the index for
570 // the LAMMPS force array.
571 size_t const in = n.index;
572
573#ifndef N2P2_FULL_SFD_MEMORY
574 Vec3D pairForce = a.calculatePairForceShort(n, &tableFull);
575#else
576 Vec3D pairForce = a.calculatePairForceShort(n);
577#endif
578 pairForce *= cfforce * convForce;
579 add3DVecToArray(atomF[in], pairForce);
580 }
581 }
582
583 // Comment: Will not work with multiple MPI tasks but this routine will
584 // probably be obsolete when Emir's solution is finished.
586 {
587 Structure const& s = structure;
588 VectorXd lambdaTotal = s.calculateForceLambdaTotal();
589
590#ifdef _OPENMP
591 #pragma omp parallel for
592#endif
593 // OpenMP 4.0 doesn't support range based loops
594 for (size_t i = 0; i < s.numAtoms; ++i)
595 {
596 auto const& ai = s.atoms[i];
597 add3DVecToArray(atomF[i], -ai.pEelecpr * cfforce * convForce);
598
599 for (auto const& aj : s.atoms)
600 {
601 size_t const j = aj.index;
602
603#ifndef N2P2_FULL_SFD_MEMORY
604 vector<vector<size_t> > const& tableFull
605 = elements.at(aj.element).getSymmetryFunctionTable();
606 Vec3D dChidr = aj.calculateDChidr(ai.index,
608 &tableFull);
609#else
610 Vec3D dChidr = aj.calculateDChidr(ai.index,
612#endif
613
614 Vec3D remainingForce = -lambdaTotal(j) * (ai.dAdrQ[j] + dChidr);
615 add3DVecToArray(atomF[i], remainingForce * cfforce * convForce);
616
617 }
618 }
619 }
620 return;
621}
622
623void InterfaceLammps::getCharges(double* const& atomQ) const
624{
625 if (nnpType != NNPType::HDNNP_4G) return;
626 if (!atomQ) return;
627
628 Structure const& s = structure;
629#ifdef _OPENMP
630 #pragma omp parallel for
631#endif
632 for (size_t i = 0; i < s.numAtoms; ++i)
633 {
634 atomQ[i] = s.atoms[i].charge;
635 }
636}
637
639{
640 long bs = 0;
641#ifndef N2P2_NO_MPI
642 int ss = 0; // size_t size.
643 int ds = 0; // double size.
644 int cs = 0; // char size.
645 MPI_Pack_size(1, MPI_SIZE_T, MPI_COMM_WORLD, &ss);
646 MPI_Pack_size(1, MPI_DOUBLE, MPI_COMM_WORLD, &ds);
647 MPI_Pack_size(1, MPI_CHAR , MPI_COMM_WORLD, &cs);
648
649 for (vector<Element>::const_iterator it = elements.begin();
650 it != elements.end(); ++it)
651 {
652 map<size_t, SymFncStatistics::Container> const& m
653 = it->statistics.data;
654 bs += ss; // n.
655 for (map<size_t, SymFncStatistics::Container>::const_iterator
656 it2 = m.begin(); it2 != m.end(); ++it2)
657 {
658 bs += ss; // index (it2->first).
659 bs += ss; // countEW (it2->second.countEW).
660 bs += ss; // type (it2->second.type).
661 bs += ds; // Gmin (it2->second.Gmin).
662 bs += ds; // Gmax (it2->second.Gmax).
663 bs += ss; // element.length() (it2->second.element.length()).
664 bs += (it2->second.element.length() + 1) * cs; // element.
665 size_t countEW = it2->second.countEW;
666 bs += countEW * ss; // indexStructureEW.
667 bs += countEW * ss; // indexAtomEW.
668 bs += countEW * ds; // valueEW.
669 }
670 }
671#endif
672 return bs;
673}
674
675void InterfaceLammps::fillEWBuffer(char* const& buf, int bs) const
676{
677#ifndef N2P2_NO_MPI
678 int p = 0;
679 for (vector<Element>::const_iterator it = elements.begin();
680 it != elements.end(); ++it)
681 {
682 map<size_t, SymFncStatistics::Container> const& m =
683 it->statistics.data;
684 size_t n = m.size();
685 MPI_Pack((void *) &(n), 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
686 for (map<size_t, SymFncStatistics::Container>::const_iterator
687 it2 = m.begin(); it2 != m.end(); ++it2)
688 {
689 MPI_Pack((void *) &(it2->first ), 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
690 size_t countEW = it2->second.countEW;
691 MPI_Pack((void *) &(countEW ), 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
692 MPI_Pack((void *) &(it2->second.type ), 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
693 MPI_Pack((void *) &(it2->second.Gmin ), 1, MPI_DOUBLE, buf, bs, &p, MPI_COMM_WORLD);
694 MPI_Pack((void *) &(it2->second.Gmax ), 1, MPI_DOUBLE, buf, bs, &p, MPI_COMM_WORLD);
695 // it2->element
696 size_t ts = it2->second.element.length() + 1;
697 MPI_Pack((void *) &ts , 1, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
698 MPI_Pack((void *) it2->second.element.c_str() , ts, MPI_CHAR , buf, bs, &p, MPI_COMM_WORLD);
699 MPI_Pack((void *) &(it2->second.indexStructureEW.front()), countEW, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
700 MPI_Pack((void *) &(it2->second.indexAtomEW.front() ), countEW, MPI_SIZE_T, buf, bs, &p, MPI_COMM_WORLD);
701 MPI_Pack((void *) &(it2->second.valueEW.front() ), countEW, MPI_DOUBLE, buf, bs, &p, MPI_COMM_WORLD);
702 }
703 }
704#endif
705 return;
706}
707
708void InterfaceLammps::extractEWBuffer(char const* const& buf, int bs)
709{
710#ifndef N2P2_NO_MPI
711 int p = 0;
712 for (vector<Element>::iterator it = elements.begin();
713 it != elements.end(); ++it)
714 {
715 size_t n = 0;
716 MPI_Unpack((void *) buf, bs, &p, &(n), 1, MPI_SIZE_T, MPI_COMM_WORLD);
717 for (size_t i = 0; i < n; ++i)
718 {
719 size_t index = 0;
720 MPI_Unpack((void *) buf, bs, &p, &(index), 1, MPI_SIZE_T, MPI_COMM_WORLD);
721 SymFncStatistics::Container& d = it->statistics.data[index];
722 size_t countEW = 0;
723 MPI_Unpack((void *) buf, bs, &p, &(countEW ), 1, MPI_SIZE_T, MPI_COMM_WORLD);
724 MPI_Unpack((void *) buf, bs, &p, &(d.type ), 1, MPI_SIZE_T, MPI_COMM_WORLD);
725 MPI_Unpack((void *) buf, bs, &p, &(d.Gmin ), 1, MPI_DOUBLE, MPI_COMM_WORLD);
726 MPI_Unpack((void *) buf, bs, &p, &(d.Gmax ), 1, MPI_DOUBLE, MPI_COMM_WORLD);
727 // d.element
728 size_t ts = 0;
729 MPI_Unpack((void *) buf, bs, &p, &ts , 1, MPI_SIZE_T, MPI_COMM_WORLD);
730 char* element = new char[ts];
731 MPI_Unpack((void *) buf, bs, &p, element , ts, MPI_CHAR , MPI_COMM_WORLD);
732 d.element = element;
733 delete[] element;
734 // indexStructureEW.
735 d.indexStructureEW.resize(d.countEW + countEW);
736 MPI_Unpack((void *) buf, bs, &p, &(d.indexStructureEW[d.countEW]), countEW, MPI_SIZE_T, MPI_COMM_WORLD);
737 // indexAtomEW.
738 d.indexAtomEW.resize(d.countEW + countEW);
739 MPI_Unpack((void *) buf, bs, &p, &(d.indexAtomEW[d.countEW] ), countEW, MPI_SIZE_T, MPI_COMM_WORLD);
740 // valueEW.
741 d.valueEW.resize(d.countEW + countEW);
742 MPI_Unpack((void *) buf, bs, &p, &(d.valueEW[d.countEW] ), countEW, MPI_DOUBLE, MPI_COMM_WORLD);
743
744 d.countEW += countEW;
745 }
746 }
747#endif
748 return;
749}
750
752{
753 for (vector<Element>::const_iterator it = elements.begin();
754 it != elements.end(); ++it)
755 {
756 vector<string> vs = it->statistics.getExtrapolationWarningLines();
757 for (vector<string>::const_iterator it2 = vs.begin();
758 it2 != vs.end(); ++it2)
759 {
760 log << (*it2);
761 }
762 }
763
764 return;
765}
766
768{
769 for (vector<Element>::iterator it = elements.begin();
770 it != elements.end(); ++it)
771 {
772 it->statistics.clear();
773 }
774
775 return;
776}
777
778void InterfaceLammps::writeToFile(string const fileName,
779 bool const append)
780{
782 structure.writeToFile(fileName, false, append);
784}
785
786void InterfaceLammps::add3DVecToArray(double *const & arr, Vec3D const& v) const
787{
788 arr[0] += v[0];
789 arr[1] += v[1];
790 arr[2] += v[2];
791}
#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
Contains element-specific data.
Definition: Element.h:39
double getAtomicEnergyOffset() const
Get atomicEnergyOffset.
Definition: Element.h:315
void logEwaldCutoffs(Log &log, double const lengthConversion) const
Use after Ewald summation!
Definition: EwaldSetup.cpp:95
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).
void allocateNeighborlists(int const *const numneigh)
Allocate neighbor lists.
std::map< int, std::size_t > mapTypeToElement
Map from LAMMPS type to n2p2 element index.
void add3DVecToArray(double *const &arr, Vec3D const &v) const
Add a Vec3D vector to a 3D array in place.
double getMaxCutoffRadiusOverall()
Get largest cutoff including structure specific cutoff and screening cutoff.
bool hasGlobalStructure
Whether n2p2 knows about the global structure or only a local part.
void setLocalAtomPositions(double const *const *const atomPos)
Set absolute atom positions from LAMMPS (nnp/develop only).
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.
void setBoxVectors(double const *boxlo, double const *boxhi, double const xy, double const xz, double const yz)
Set box vectors of structure stored in LAMMPS (nnp/develop only).
std::vector< size_t > indexMap
Map from LAMMPS index to n2p2 atom index.
bool getGlobalStructureStatus()
Check if n2p2 knows about global structure.
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.
void writeToFile(std::string const fileName, bool const append)
Write current structure to file in units used in training data.
void getCharges(double *const &atomQ) const
Transfer charges (in units of e) to LAMMPS atomic charge vector.
int showewsum
Corresponds to LAMMPS showewsum keyword.
void setGlobalStructureStatus(bool const status)
Specify whether n2p2 knows about global structure or only local structure.
void finalizeNeighborList()
Sorts neighbor list and creates cutoff map if necessary.
void clearExtrapolationWarnings()
Clear extrapolation warnings storage.
std::map< std::size_t, int > mapElementToType
Map from n2p2 element index to LAMMPS type.
int myRank
Process rank.
double getMaxCutoffRadius() const
Get largest cutoff of symmetry functions.
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.
Definition: Log.h:85
double physicalEnergy(Structure const &structure, bool ref=true) const
Undo normalization for a given energy of structure.
Definition: Mode.cpp:2122
std::vector< std::vector< double > > cutoffs
Matrix storing all symmetry function cut-offs for all elements.
Definition: Mode.h:652
bool normalize
Definition: Mode.h:629
double convEnergy
Definition: Mode.h:637
ElementMap elementMap
Global element map, populated by setupElementMap().
Definition: Mode.h:591
NNPType nnpType
Definition: Mode.h:628
void addEnergyOffset(Structure &structure, bool ref=true)
Add atomic energy offsets to reference energy.
Definition: Mode.cpp:2018
void initialize()
Write welcome message with version information.
Definition: Mode.cpp:55
double convLength
Definition: Mode.h:638
double maxCutoffRadius
Definition: Mode.h:634
void setupGeneric(std::string const &nnpDir="", bool skipNormalize=false, bool initialHardness=false)
Combine multiple setup routines and provide a basic NNP setup.
Definition: Mode.cpp:212
@ HDNNP_Q
Short range NNP with charge NN, no electrostatics/Qeq (M.
@ HDNNP_4G
NNP with electrostatics and non-local charge transfer (4G-HDNNP).
double meanEnergy
Definition: Mode.h:636
virtual void setupNeuralNetworkWeights(std::map< std::string, std::string > fileNameFormats=std::map< std::string, std::string >())
Set up neural network weights from files with given name format.
Definition: Mode.cpp:1445
ScreeningFunction screeningFunction
Definition: Mode.h:645
void calculateAtomicNeuralNetworks(Structure &structure, bool const derivatives, std::string id="")
Calculate atomic neural networks for all atoms in given structure.
Definition: Mode.cpp:1642
double physical(std::string const &property, double value) const
Undo normalization for a given property.
Definition: Mode.cpp:2110
std::vector< Element > elements
Definition: Mode.h:646
std::size_t numElements
Definition: Mode.h:631
void calculateEnergy(Structure &structure) const
Calculate potential energy for a given structure.
Definition: Mode.cpp:1803
void calculateSymmetryFunctionGroups(Structure &structure, bool const derivatives)
Calculate all symmetry function groups for all atoms in given structure.
Definition: Mode.cpp:1561
virtual void setupSymmetryFunctionScaling(std::string const &fileName="scaling.data")
Set up symmetry function scaling from file.
Definition: Mode.cpp:712
double convCharge
Definition: Mode.h:639
Log log
Global log file.
Definition: Mode.h:593
void calculateCharge(Structure &structure) const
Calculate total charge for a given structure.
Definition: Mode.cpp:1830
void chargeEquilibration(Structure &structure, bool const derivativesElec)
Perform global charge equilibration method.
Definition: Mode.cpp:1754
void calculateSymmetryFunctions(Structure &structure, bool const derivatives)
Calculate all symmetry functions for all atoms in given structure.
Definition: Mode.cpp:1480
EwaldSetup ewaldSetup
Definition: Mode.h:641
void setupSymmetryFunctionStatistics(bool collectStatistics, bool collectExtrapolationWarnings, bool writeExtrapolationWarnings, bool stopOnExtrapolationWarnings)
Set up symmetry function statistics collection.
Definition: Mode.cpp:1103
void loadSettingsFile(std::string const &fileName="input.nn")
Open settings file and load all keywords into memory.
Definition: Mode.cpp:161
double getOuter() const
Getter for outer.
#define MPI_SIZE_T
Definition: mpi-extra.h:22
Definition: Atom.h:29
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
size_t p
Definition: nnp-cutoff.cpp:33
double d
Definition: nnp-cutoff.cpp:34
Struct to store information on neighbor atoms.
Definition: Atom.h:36
std::size_t index
Index of neighbor atom.
Definition: Atom.h:38
std::size_t element
Element index of neighbor atom.
Definition: Atom.h:42
double d
Distance to neighbor atom.
Definition: Atom.h:44
Vec3D dr
Distance vector to neighbor atom.
Definition: Atom.h:46
int64_t tag
Tag of neighbor atom.
Definition: Atom.h:40
Storage for a single atom.
Definition: Atom.h:33
std::vector< Neighbor > neighbors
Neighbor array (maximum number defined in macros.h.
Definition: Atom.h:170
Vec3D r
Cartesian coordinates.
Definition: Atom.h:125
bool hasSymmetryFunctionDerivatives
If symmetry function derivatives are saved for this atom.
Definition: Atom.h:97
std::size_t index
Index number of this atom.
Definition: Atom.h:101
std::size_t indexStructure
Index number of structure this atom belongs to.
Definition: Atom.h:103
std::size_t element
Element index of this atom.
Definition: Atom.h:107
bool hasSymmetryFunctions
If symmetry function values are saved for this atom.
Definition: Atom.h:95
double energy
Atomic energy determined by neural network.
Definition: Atom.h:115
std::vector< std::size_t > numNeighborsPerElement
Number of neighbors per element.
Definition: Atom.h:138
std::size_t numNeighbors
Total number of neighbors.
Definition: Atom.h:109
Storage for one atomic configuration.
Definition: Structure.h:39
void calculateVolume()
Calculate volume from box vectors.
Definition: Structure.cpp:581
void toPhysicalUnits(double meanEnergy, double convEnergy, double convLength, double convCharge)
Switch to physical units, shift energy and change energy, length and charge unit.
Definition: Structure.cpp:1278
Vec3D box[3]
Simulation box vectors.
Definition: Structure.h:112
bool isTriclinic
If the simulation box is triclinic.
Definition: Structure.h:63
void setupNeighborCutoffMap(std::vector< std::vector< double > > cutoffs)
Set up a neighbor cut-off map which gives the index (value) of the last needed neighbor corresponding...
Definition: Structure.cpp:423
void sortNeighborList()
Sort all neighbor lists of this structure with respect to the distance.
Definition: Structure.cpp:409
std::vector< std::size_t > numAtomsPerElement
Number of atoms of each element in this structure.
Definition: Structure.h:120
void calculateMaxCutoffRadiusOverall(EwaldSetup &ewaldSetup, double rcutScreen, double maxCutoffRadius)
Calculate maximal cut-off if cut-off of screening and real part Ewald summation are also considered.
Definition: Structure.cpp:273
void writeToFile(std::string const fileName="output.data", bool const ref=true, bool const append=false) const
Write configuration to file.
Definition: Structure.cpp:1449
bool isPeriodic
If periodic boundary conditions apply.
Definition: Structure.h:61
double maxCutoffRadiusOverall
Maximum cut-off radius with respect to symmetry functions, screening function and Ewald summation.
Definition: Structure.h:103
void setElementMap(ElementMap const &elementMap)
Set element map of structure.
Definition: Structure.cpp:71
std::size_t index
Index number of this structure.
Definition: Structure.h:73
void calculateInverseBox()
Calculate inverse box.
Definition: Structure.cpp:519
bool hasSymmetryFunctionDerivatives
If symmetry function derivatives are saved for each atom.
Definition: Structure.h:71
Eigen::VectorXd const calculateForceLambdaTotal() const
Calculate lambda_total vector which is needed for the total force calculation in 4G NN.
Definition: Structure.cpp:1188
double energy
Potential energy determined by neural network.
Definition: Structure.h:83
void toNormalizedUnits(double meanEnergy, double convEnergy, double convLength, double convCharge)
Normalize structure, shift energy and change energy, length and charge unit.
Definition: Structure.cpp:1249
std::size_t numAtoms
Total number of atoms present in this structure.
Definition: Structure.h:75
std::vector< Atom > atoms
Vector of all atoms in this structure.
Definition: Structure.h:122
bool hasNeighborList
If the neighbor list has been calculated.
Definition: Structure.h:65
bool hasSymmetryFunctions
If symmetry function values are saved for each atom.
Definition: Structure.h:69
Struct containing statistics gathered during symmetry function calculation.
Vector in 3 dimensional real space.
Definition: Vec3D.h:30