n2p2 - A neural network potential package
LAMMPS_NS::PairNNP Class Reference

#include <pair_nnp.h>

Inheritance diagram for LAMMPS_NS::PairNNP:
Collaboration diagram for LAMMPS_NS::PairNNP:

Public Member Functions

 PairNNP (class LAMMPS *)
 
virtual ~PairNNP ()
 
virtual void compute (int, int)
 
virtual void settings (int, char **)
 
virtual void coeff (int, char **)
 
virtual void init_style ()
 
virtual double init_one (int, int)
 
virtual void write_restart (FILE *)
 
virtual void read_restart (FILE *)
 
virtual void write_restart_settings (FILE *)
 
virtual void read_restart_settings (FILE *)
 

Protected Member Functions

virtual void allocate ()
 
void transferNeighborList ()
 
void handleExtrapolationWarnings ()
 

Protected Attributes

bool showew
 
bool resetew
 
int showewsum
 
int maxew
 
long numExtrapolationWarningsTotal
 
long numExtrapolationWarningsSummary
 
double cflength
 
double cfenergy
 
double maxCutoffRadius
 
char * directory
 
char * emap
 
nnp::InterfaceLammps interface
 

Detailed Description

Definition at line 21 of file pair_nnp.h.

Constructor & Destructor Documentation

◆ PairNNP()

PairNNP::PairNNP ( class LAMMPS *  lmp)

Definition at line 24 of file pair_nnp.cpp.

24 : Pair(lmp)
25{
26}

◆ ~PairNNP()

PairNNP::~PairNNP ( )
virtual

Definition at line 32 of file pair_nnp.cpp.

33{
34}

Member Function Documentation

◆ compute()

void PairNNP::compute ( int  eflag,
int  vflag 
)
virtual

Definition at line 38 of file pair_nnp.cpp.

39{
40 if(eflag || vflag) ev_setup(eflag,vflag);
41 else evflag = vflag_fdotr = eflag_global = eflag_atom = 0;
42
43 // Set number of local atoms and add element.
44 interface.setLocalAtoms(atom->nlocal,atom->type);
45 // Transfer tags separately. Interface::setLocalTags is overloaded internally
46 // to work with both -DLAMMPS_SMALLBIG (tagint = int) and -DLAMMPS_BIGBIG
47 // (tagint = int64_t)
48 interface.setLocalTags(atom->tag);
49
50 // Transfer local neighbor list to NNP interface.
52
53 // Compute symmetry functions, atomic neural networks and add up energy.
55
56 // Do all stuff related to extrapolation warnings.
57 if(showew == true || showewsum > 0 || maxew >= 0) {
59 }
60
61 // Calculate forces of local and ghost atoms.
62 interface.getForces(atom->f);
63
64 // Add energy contribution to total energy.
65 if (eflag_global)
66 ev_tally(0,0,atom->nlocal,1,interface.getEnergy(),0.0,0.0,0.0,0.0,0.0);
67
68 // Add atomic energy if requested (CAUTION: no physical meaning!).
69 if (eflag_atom)
70 for (int i = 0; i < atom->nlocal; ++i)
71 eatom[i] = interface.getAtomicEnergy(i);
72
73 // If virial needed calculate via F dot r.
74 if (vflag_fdotr) virial_fdotr_compute();
75}
nnp::InterfaceLammps interface
Definition: pair_nnp.h:54
void handleExtrapolationWarnings()
Definition: pair_nnp.cpp:325
void transferNeighborList()
Definition: pair_nnp.cpp:304
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).
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 setLocalAtoms(int numAtomsLocal, int const *const atomType)
(Re)set structure to contain only local LAMMPS atoms.

References nnp::InterfaceLammps::getAtomicEnergy(), nnp::InterfaceLammps::getEnergy(), nnp::InterfaceLammps::getForces(), handleExtrapolationWarnings(), interface, maxew, nnp::InterfaceLammps::process(), nnp::InterfaceLammps::setLocalAtoms(), nnp::InterfaceLammps::setLocalTags(), showew, showewsum, and transferNeighborList().

Here is the call graph for this function:

◆ settings()

void PairNNP::settings ( int  narg,
char **  arg 
)
virtual

Definition at line 81 of file pair_nnp.cpp.

82{
83 int iarg = 0;
84
85 if (narg == 0) error->all(FLERR,"Illegal pair_style command");
86
87 // default settings
88 int len = strlen("nnp/") + 1;
89 directory = new char[len];
90 strcpy(directory,"nnp/");
91 showew = true;
92 showewsum = 0;
93 maxew = 0;
94 resetew = false;
95 cflength = 1.0;
96 cfenergy = 1.0;
97 len = strlen("") + 1;
98 emap = new char[len];
99 strcpy(emap,"");
102
103 while(iarg < narg) {
104 // set NNP directory
105 if (strcmp(arg[iarg],"dir") == 0) {
106 if (iarg+2 > narg)
107 error->all(FLERR,"Illegal pair_style command");
108 delete[] directory;
109 len = strlen(arg[iarg+1]) + 2;
110 directory = new char[len];
111 sprintf(directory, "%s/", arg[iarg+1]);
112 iarg += 2;
113 // element mapping
114 } else if (strcmp(arg[iarg],"emap") == 0) {
115 if (iarg+2 > narg)
116 error->all(FLERR,"Illegal pair_style command");
117 delete[] emap;
118 len = strlen(arg[iarg+1]) + 1;
119 emap = new char[len];
120 sprintf(emap, "%s", arg[iarg+1]);
121 iarg += 2;
122 // show extrapolation warnings
123 } else if (strcmp(arg[iarg],"showew") == 0) {
124 if (iarg+2 > narg)
125 error->all(FLERR,"Illegal pair_style command");
126 if (strcmp(arg[iarg+1],"yes") == 0)
127 showew = true;
128 else if (strcmp(arg[iarg+1],"no") == 0)
129 showew = false;
130 else
131 error->all(FLERR,"Illegal pair_style command");
132 iarg += 2;
133 // show extrapolation warning summary
134 } else if (strcmp(arg[iarg],"showewsum") == 0) {
135 if (iarg+2 > narg)
136 error->all(FLERR,"Illegal pair_style command");
137 showewsum = utils::inumeric(FLERR,arg[iarg+1],false,lmp);
138 iarg += 2;
139 // maximum allowed extrapolation warnings
140 } else if (strcmp(arg[iarg],"maxew") == 0) {
141 if (iarg+2 > narg)
142 error->all(FLERR,"Illegal pair_style command");
143 maxew = utils::inumeric(FLERR,arg[iarg+1],false,lmp);
144 iarg += 2;
145 // reset extrapolation warning counter
146 } else if (strcmp(arg[iarg],"resetew") == 0) {
147 if (iarg+2 > narg)
148 error->all(FLERR,"Illegal pair_style command");
149 if (strcmp(arg[iarg+1],"yes") == 0)
150 resetew = true;
151 else if (strcmp(arg[iarg+1],"no") == 0)
152 resetew = false;
153 else
154 error->all(FLERR,"Illegal pair_style command");
155 iarg += 2;
156 // length unit conversion factor
157 } else if (strcmp(arg[iarg],"cflength") == 0) {
158 if (iarg+2 > narg)
159 error->all(FLERR,"Illegal pair_style command");
160 cflength = utils::numeric(FLERR,arg[iarg+1],false,lmp);
161 iarg += 2;
162 // energy unit conversion factor
163 } else if (strcmp(arg[iarg],"cfenergy") == 0) {
164 if (iarg+2 > narg)
165 error->all(FLERR,"Illegal pair_style command");
166 cfenergy = utils::numeric(FLERR,arg[iarg+1],false,lmp);
167 iarg += 2;
168 } else error->all(FLERR,"Illegal pair_style command");
169 }
170}
char * directory
Definition: pair_nnp.h:52
long numExtrapolationWarningsSummary
Definition: pair_nnp.h:48
long numExtrapolationWarningsTotal
Definition: pair_nnp.h:47

References cfenergy, cflength, directory, emap, maxew, numExtrapolationWarningsSummary, numExtrapolationWarningsTotal, resetew, showew, and showewsum.

◆ coeff()

void PairNNP::coeff ( int  narg,
char **  arg 
)
virtual

Definition at line 176 of file pair_nnp.cpp.

177{
178 if (!allocated) allocate();
179
180 if (narg != 3) error->all(FLERR,"Incorrect args for pair coefficients");
181
182 int ilo,ihi,jlo,jhi;
183 utils::bounds(FLERR,arg[0],1,atom->ntypes,ilo,ihi,error);
184 utils::bounds(FLERR,arg[1],1,atom->ntypes,jlo,jhi,error);
185
186 maxCutoffRadius = utils::numeric(FLERR,arg[2],false,lmp);
187
188 // TODO: Check how this flag is set.
189 int count = 0;
190 for(int i=ilo; i<=ihi; i++) {
191 for(int j=MAX(jlo,i); j<=jhi; j++) {
192 setflag[i][j] = 1;
193 count++;
194 }
195 }
196
197 if (count == 0) error->all(FLERR,"Incorrect args for pair coefficients");
198}
double maxCutoffRadius
Definition: pair_nnp.h:51
virtual void allocate()
Definition: pair_nnp.cpp:291

References allocate(), and maxCutoffRadius.

Here is the call graph for this function:

◆ init_style()

void PairNNP::init_style ( )
virtual

Definition at line 204 of file pair_nnp.cpp.

205{
206 int irequest = neighbor->request((void *) this);
207 neighbor->requests[irequest]->pair = 1;
208 neighbor->requests[irequest]->half = 0;
209 neighbor->requests[irequest]->full = 1;
210
211 // Return immediately if NNP setup is already completed.
212 if (interface.isInitialized()) return;
213
214 // Activate screen and logfile output only for rank 0.
215 if (comm->me == 0) {
216 if (lmp->screen != NULL)
217 interface.log.registerCFilePointer(&(lmp->screen));
218 if (lmp->logfile != NULL)
219 interface.log.registerCFilePointer(&(lmp->logfile));
220 }
221
222 // Initialize interface on all processors.
224 emap,
225 showew,
226 resetew,
227 showewsum,
228 maxew,
229 cflength,
230 cfenergy,
232 atom->ntypes,
233 comm->me);
234
235 // LAMMPS cutoff radius (given via pair_coeff) should not be smaller than
236 // maximum symmetry function cutoff radius.
238 error->all(FLERR,"Inconsistent cutoff radius");
239}
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.
bool isInitialized() const
Check if this interface is correctly initialized.
double getMaxCutoffRadius() const
Get largest cutoff.
void registerCFilePointer(FILE **const &filePointer)
Register new C-style FILE* pointer for output.
Definition: Log.cpp:85
Log log
Global log file.
Definition: Mode.h:510

References cfenergy, cflength, directory, emap, nnp::InterfaceLammps::getMaxCutoffRadius(), nnp::InterfaceLammps::initialize(), interface, nnp::InterfaceLammps::isInitialized(), nnp::Mode::log, maxCutoffRadius, maxew, nnp::Log::registerCFilePointer(), resetew, showew, and showewsum.

Here is the call graph for this function:

◆ init_one()

double PairNNP::init_one ( int  i,
int  j 
)
virtual

Definition at line 245 of file pair_nnp.cpp.

246{
247 // TODO: Check how this actually works for different cutoffs.
248 return maxCutoffRadius;
249}

References maxCutoffRadius.

◆ write_restart()

void PairNNP::write_restart ( FILE *  fp)
virtual

Definition at line 255 of file pair_nnp.cpp.

256{
257 return;
258}

◆ read_restart()

void PairNNP::read_restart ( FILE *  fp)
virtual

Definition at line 264 of file pair_nnp.cpp.

265{
266 return;
267}

◆ write_restart_settings()

void PairNNP::write_restart_settings ( FILE *  fp)
virtual

Definition at line 273 of file pair_nnp.cpp.

274{
275 return;
276}

◆ read_restart_settings()

void PairNNP::read_restart_settings ( FILE *  fp)
virtual

Definition at line 282 of file pair_nnp.cpp.

283{
284 return;
285}

◆ allocate()

void PairNNP::allocate ( )
protectedvirtual

Definition at line 291 of file pair_nnp.cpp.

292{
293 allocated = 1;
294 int n = atom->ntypes;
295
296 memory->create(setflag,n+1,n+1,"pair:setflag");
297 for (int i = 1; i <= n; i++)
298 for (int j = i; j <= n; j++)
299 setflag[i][j] = 0;
300
301 memory->create(cutsq,n+1,n+1,"pair:cutsq");
302}

Referenced by coeff().

Here is the caller graph for this function:

◆ transferNeighborList()

void PairNNP::transferNeighborList ( )
protected

Definition at line 304 of file pair_nnp.cpp.

305{
306 // Transfer neighbor list to NNP.
307 double rc2 = maxCutoffRadius * maxCutoffRadius;
308 for (int ii = 0; ii < list->inum; ++ii) {
309 int i = list->ilist[ii];
310 for (int jj = 0; jj < list->numneigh[i]; ++jj) {
311 int j = list->firstneigh[i][jj];
312 j &= NEIGHMASK;
313 double dx = atom->x[i][0] - atom->x[j][0];
314 double dy = atom->x[i][1] - atom->x[j][1];
315 double dz = atom->x[i][2] - atom->x[j][2];
316 double d2 = dx * dx + dy * dy + dz * dz;
317 if (d2 <= rc2) {
318 // atom->tag[j] will be implicitly converted to int64_t internally.
319 interface.addNeighbor(i,j,atom->tag[j],atom->type[j],dx,dy,dz,d2);
320 }
321 }
322 }
323}
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).

References nnp::InterfaceLammps::addNeighbor(), interface, and maxCutoffRadius.

Referenced by compute().

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

◆ handleExtrapolationWarnings()

void PairNNP::handleExtrapolationWarnings ( )
protected

Definition at line 325 of file pair_nnp.cpp.

326{
327 // Get number of extrapolation warnings for local atoms.
328 // TODO: Is the conversion from std::size_t to long ok?
329 long numCurrentEW = (long)interface.getNumExtrapolationWarnings();
330
331 // Update (or set, resetew == true) total warnings counter.
332 if (resetew) numExtrapolationWarningsTotal = numCurrentEW;
333 else numExtrapolationWarningsTotal += numCurrentEW;
334
335 // Update warnings summary counter.
336 if(showewsum > 0) {
337 numExtrapolationWarningsSummary += numCurrentEW;
338 }
339
340 // If requested write extrapolation warnings.
341 // Requires communication of all symmetry functions statistics entries to
342 // rank 0.
343 if(showew > 0) {
344 // First collect an overview of extrapolation warnings per process.
345 long* numEWPerProc = NULL;
346 if(comm->me == 0) numEWPerProc = new long[comm->nprocs];
347 MPI_Gather(&numCurrentEW, 1, MPI_LONG, numEWPerProc, 1, MPI_LONG, 0, world);
348
349 if(comm->me == 0) {
350 for(int i=1;i<comm->nprocs;i++) {
351 if(numEWPerProc[i] > 0) {
352 long bs = 0;
353 MPI_Status ms;
354 // Get buffer size.
355 MPI_Recv(&bs, 1, MPI_LONG, i, 0, world, &ms);
356 char* buf = new char[bs];
357 // Receive buffer.
358 MPI_Recv(buf, bs, MPI_BYTE, i, 0, world, &ms);
359 interface.extractEWBuffer(buf, bs);
360 delete[] buf;
361 }
362 }
364 }
365 else if(numCurrentEW > 0) {
366 // Get desired buffer length for all extrapolation warning entries.
367 long bs = interface.getEWBufferSize();
368 // Allocate and fill buffer.
369 char* buf = new char[bs];
370 interface.fillEWBuffer(buf, bs);
371 // Send buffer size and buffer.
372 MPI_Send(&bs, 1, MPI_LONG, 0, 0, world);
373 MPI_Send(buf, bs, MPI_BYTE, 0, 0, world);
374 delete[] buf;
375 }
376
377 if(comm->me == 0) delete[] numEWPerProc;
378 }
379
380 // If requested gather number of warnings to display summary.
381 if(showewsum > 0 && update->ntimestep % showewsum == 0) {
382 long globalEW = 0;
383 // Communicate the sum over all processors to proc 0.
385 &globalEW, 1, MPI_LONG, MPI_SUM, 0, world);
386 // Write to screen or logfile.
387 if(comm->me == 0) {
388 if(screen) {
389 fprintf(screen,
390 "### NNP EW SUMMARY ### TS: %10ld EW %10ld EWPERSTEP %10.3E\n",
391 update->ntimestep,
392 globalEW,
393 double(globalEW) / showewsum);
394 }
395 if(logfile) {
396 fprintf(logfile,
397 "### NNP EW SUMMARY ### TS: %10ld EW %10ld EWPERSTEP %10.3E\n",
398 update->ntimestep,
399 globalEW,
400 double(globalEW) / showewsum);
401 }
402 }
403 // Reset summary counter.
405 }
406
407 // Stop if maximum number of extrapolation warnings is exceeded.
409 error->one(FLERR,"Too many extrapolation warnings");
410 }
411
412 // Reset internal extrapolation warnings counters.
414}
long getEWBufferSize() const
Calculate buffer size for extrapolation warning communication.
void extractEWBuffer(char const *const &buf, int bs)
Extract given buffer to symmetry function statistics class.
void fillEWBuffer(char *const &buf, int bs) const
Fill provided buffer with extrapolation warning entries.
void writeExtrapolationWarnings()
Write extrapolation warnings to log.
void clearExtrapolationWarnings()
Clear extrapolation warnings storage.
std::size_t getNumExtrapolationWarnings() const
Count total number of extrapolation warnings encountered for all elements and symmetry functions.
Definition: Mode.cpp:1595

References nnp::InterfaceLammps::clearExtrapolationWarnings(), nnp::InterfaceLammps::extractEWBuffer(), nnp::InterfaceLammps::fillEWBuffer(), nnp::InterfaceLammps::getEWBufferSize(), nnp::Mode::getNumExtrapolationWarnings(), interface, maxew, numExtrapolationWarningsSummary, numExtrapolationWarningsTotal, resetew, showew, showewsum, and nnp::InterfaceLammps::writeExtrapolationWarnings().

Referenced by compute().

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

Member Data Documentation

◆ showew

bool LAMMPS_NS::PairNNP::showew
protected

Definition at line 43 of file pair_nnp.h.

Referenced by compute(), handleExtrapolationWarnings(), init_style(), and settings().

◆ resetew

bool LAMMPS_NS::PairNNP::resetew
protected

Definition at line 44 of file pair_nnp.h.

Referenced by handleExtrapolationWarnings(), init_style(), and settings().

◆ showewsum

int LAMMPS_NS::PairNNP::showewsum
protected

Definition at line 45 of file pair_nnp.h.

Referenced by compute(), handleExtrapolationWarnings(), init_style(), and settings().

◆ maxew

int LAMMPS_NS::PairNNP::maxew
protected

Definition at line 46 of file pair_nnp.h.

Referenced by compute(), handleExtrapolationWarnings(), init_style(), and settings().

◆ numExtrapolationWarningsTotal

long LAMMPS_NS::PairNNP::numExtrapolationWarningsTotal
protected

Definition at line 47 of file pair_nnp.h.

Referenced by handleExtrapolationWarnings(), and settings().

◆ numExtrapolationWarningsSummary

long LAMMPS_NS::PairNNP::numExtrapolationWarningsSummary
protected

Definition at line 48 of file pair_nnp.h.

Referenced by handleExtrapolationWarnings(), and settings().

◆ cflength

double LAMMPS_NS::PairNNP::cflength
protected

Definition at line 49 of file pair_nnp.h.

Referenced by init_style(), and settings().

◆ cfenergy

double LAMMPS_NS::PairNNP::cfenergy
protected

Definition at line 50 of file pair_nnp.h.

Referenced by init_style(), and settings().

◆ maxCutoffRadius

double LAMMPS_NS::PairNNP::maxCutoffRadius
protected

Definition at line 51 of file pair_nnp.h.

Referenced by coeff(), init_one(), init_style(), and transferNeighborList().

◆ directory

char* LAMMPS_NS::PairNNP::directory
protected

Definition at line 52 of file pair_nnp.h.

Referenced by init_style(), and settings().

◆ emap

char* LAMMPS_NS::PairNNP::emap
protected

Definition at line 53 of file pair_nnp.h.

Referenced by init_style(), and settings().

◆ interface

nnp::InterfaceLammps LAMMPS_NS::PairNNP::interface
protected

Definition at line 54 of file pair_nnp.h.

Referenced by compute(), handleExtrapolationWarnings(), init_style(), and transferNeighborList().


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