n2p2 - A neural network potential package
pair_hdnnp_develop.cpp
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#include <mpi.h>
18#include <string.h>
19#include "pair_hdnnp_develop.h"
20#include "atom.h"
21#include "domain.h"
22#include "comm.h"
23#include "neighbor.h"
24#include "neigh_list.h"
25#include "neigh_request.h"
26#include "memory.h"
27#include "error.h"
28#include "force.h"
29#include "update.h"
30#include "utils.h"
31
32#include <stdexcept>
33//TODO: remove later
34#include <iostream>
35
36#include "InterfaceLammps.h" // n2p2 interface header
37
38using namespace LAMMPS_NS;
39using namespace std;
40
41
42/* ---------------------------------------------------------------------- */
43
44PairHDNNPDevelop::PairHDNNPDevelop(LAMMPS *lmp) : PairHDNNP(lmp) {}
45
46/* ---------------------------------------------------------------------- */
47
48void PairHDNNPDevelop::compute(int eflag, int vflag)
49{
50 if(eflag || vflag) ev_setup(eflag,vflag);
51 else evflag = vflag_fdotr = eflag_global = eflag_atom = 0;
52
53 // Set number of local atoms and add index and element.
54 interface->setLocalAtoms(atom->nlocal, atom->type);
55 // Transfer tags separately. Interface::setLocalTags is overloaded internally
56 // to work with both -DLAMMPS_SMALLBIG (tagint = int) and -DLAMMPS_BIGBIG
57 // (tagint = int64_t)
58 interface->setLocalTags(atom->tag);
59
60
61 // Also set absolute atom positions.
63
64 // Set Box vectors if system is periodic in all 3 dims.
65 if(domain->nonperiodic == 0)
66 {
67 interface->setBoxVectors(domain->boxlo,
68 domain->boxhi,
69 domain->xy,
70 domain->xz,
71 domain->yz);
72 }
73
75
76 // Transfer local neighbor list to NNP interface. Has to be called after
77 // setBoxVectors if structure is periodic!
79
80 // Compute symmetry functions, atomic neural networks and add up energy.
82
83 // Do all stuff related to extrapolation warnings.
84 if(showew == true || showewsum > 0 || maxew >= 0) {
86 }
87
88 // Calculate forces of local and ghost atoms.
89 interface->getForces(atom->f);
90
91 // Transfer charges LAMMPS. Does nothing if nnpType != 4G.
92 interface->getCharges(atom->q);
93
94 // Add energy contribution to total energy.
95 if (eflag_global)
96 ev_tally(0,0,atom->nlocal,1,interface->getEnergy(),0.0,0.0,0.0,0.0,0.0);
97
98 // Add atomic energy if requested (CAUTION: no physical meaning!).
99 if (eflag_atom)
100 for (int i = 0; i < atom->nlocal; ++i)
101 eatom[i] = interface->getAtomicEnergy(i);
102
103 // If virial needed calculate via F dot r.
104 if (vflag_fdotr) virial_fdotr_compute();
105
106 //interface.writeToFile("md_run.data", true);
107}
108
109/* ----------------------------------------------------------------------
110 init specific to this pair style
111------------------------------------------------------------------------- */
112
114{
115 if (comm->nprocs > 1) {
116 throw runtime_error("ERROR: Pair style \"nnp/develop\" can only be used "
117 "with a single MPI task.\n");
118 }
119
120 if(domain->dimension != 3)
121 throw runtime_error("ERROR: Only 3d systems can be used!");
122
123 if (!(domain->xperiodic == domain->yperiodic
124 && domain->yperiodic == domain->zperiodic))
125 throw runtime_error("ERROR: System must be either aperiodic or periodic "
126 "in all 3 dimmensions!");
127
128 // Required for charge equilibration scheme.
129 if (atom->map_style == Atom::MAP_NONE)
130 throw runtime_error("ERROR: pair style requires atom map yes");
131
133
134
136
138}
139
140
141double PairHDNNPDevelop::init_one(int i, int j)
142{
143 //cutsq[i][j] = cutsq[j][i] = pow(maxCutoffRadiusNeighborList,2);
145}
146
147
148void PairHDNNPDevelop::transferNeighborList(double const cutoffRadius)
149{
150 // Transfer neighbor list to NNP.
151 double rc2 = cutoffRadius * cutoffRadius;
152 interface->allocateNeighborlists(list->numneigh);
153#ifdef _OPENMP
154 #pragma omp parallel for
155#endif
156 for (int ii = 0; ii < list->inum; ++ii) {
157 int i = list->ilist[ii];
158 for (int jj = 0; jj < list->numneigh[i]; ++jj) {
159 int j = list->firstneigh[i][jj];
160 j &= NEIGHMASK;
161 double dx = atom->x[i][0] - atom->x[j][0];
162 double dy = atom->x[i][1] - atom->x[j][1];
163 double dz = atom->x[i][2] - atom->x[j][2];
164 double d2 = dx * dx + dy * dy + dz * dz;
165 if (d2 <= rc2) {
167 // atom->tag[j] will be implicitly converted to int64_t internally.
168 interface->addNeighbor(i,j,atom->tag[j],atom->type[j],dx,dy,dz,d2);
169 else
170 interface->addNeighbor(i,j,atom->map(atom->tag[j]),atom->type[j],dx,dy,dz,d2);
171 }
172 }
173 }
175}
176
177
179{
180 double maxCutoffRadiusOverall = interface->getMaxCutoffRadiusOverall();
181 if(maxCutoffRadiusOverall > maxCutoffRadiusNeighborList)
182 {
183 // TODO: Increase slightly to compensate for rounding errors?
184 utils::logmesg(lmp, fmt::format("WARNING: Cutoff too small, need at "
185 "least: {}\n", maxCutoffRadiusOverall));
186
187 //maxCutoffRadiusNeighborList = maxCutoffRadiusOverall;
188 //reinit();
189
190 //mixed_flag = 1;
191 //for (int i = 1; i <= atom->ntypes; i++)
192 //{
193 // for (int j = i; j <= atom->ntypes; j++)
194 // {
195 // if ((i != j) && setflag[i][j]) mixed_flag = 0;
196 // cutsq[i][j] = cutsq[j][i] = pow(maxCutoffRadiusNeighborList, 2);
197 // }
198 //}
199 //cutforce = MAX(cutforce, maxCutoffRadiusNeighborList);
200
201 //neighbor->init();
202 //neighbor->build(0);
203 }
204}
205
206
double maxCutoffRadiusNeighborList
Keeps track of the maximum cutoff radius that was used for the neighbor list.
void updateNeighborlistCutoff()
Update neighborlist if maxCutoffRadiusNeighborList has changed.
void compute(int, int) override
double init_one(int i, int j) override
void init_style() override
Definition: pair_hdnnp.cpp:226
void handleExtrapolationWarnings()
Definition: pair_hdnnp.cpp:292
nnp::InterfaceLammps * interface
Definition: pair_hdnnp.h:65
void process()
Calculate symmetry functions, atomic neural networks and sum of local energy contributions.
void setLocalTags(int const *const atomTag)
Set atom tags (int version, -DLAMMPS_SMALLBIG).
void allocateNeighborlists(int const *const numneigh)
Allocate neighbor lists.
double getMaxCutoffRadiusOverall()
Get largest cutoff including structure specific cutoff and screening cutoff.
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.
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.
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).
bool getGlobalStructureStatus()
Check if n2p2 knows about global structure.
void setLocalAtoms(int numAtomsLocal, int const *const atomType)
(Re)set structure to contain only local LAMMPS atoms.
void getCharges(double *const &atomQ) const
Transfer charges (in units of e) to LAMMPS atomic charge vector.
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 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).