1 /* ----------------------------------------------------------------------
2    LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
3    https://www.lammps.org/, Sandia National Laboratories
4    Steve Plimpton, sjplimp@sandia.gov
5 
6    Copyright (2003) Sandia Corporation.  Under the terms of Contract
7    DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
8    certain rights in this software.  This software is distributed under
9    the GNU General Public License.
10 
11    See the README file in the top-level LAMMPS directory.
12 ------------------------------------------------------------------------- */
13 
14 #include "dihedral_hybrid.h"
15 
16 #include "atom.h"
17 #include "comm.h"
18 #include "error.h"
19 #include "force.h"
20 #include "memory.h"
21 #include "neighbor.h"
22 
23 #include <cctype>
24 #include <cstring>
25 
26 using namespace LAMMPS_NS;
27 
28 #define EXTRA 1000
29 
30 /* ---------------------------------------------------------------------- */
31 
DihedralHybrid(LAMMPS * lmp)32 DihedralHybrid::DihedralHybrid(LAMMPS *lmp) : Dihedral(lmp)
33 {
34   writedata = 0;
35   nstyles = 0;
36   ndihedrallist = nullptr;
37   dihedrallist = nullptr;
38   maxdihedral = nullptr;
39 }
40 
41 /* ---------------------------------------------------------------------- */
42 
~DihedralHybrid()43 DihedralHybrid::~DihedralHybrid()
44 {
45   if (nstyles) {
46     for (int i = 0; i < nstyles; i++) delete styles[i];
47     delete[] styles;
48     for (int i = 0; i < nstyles; i++) delete[] keywords[i];
49     delete[] keywords;
50   }
51 
52   if (allocated) {
53     memory->destroy(setflag);
54     memory->destroy(map);
55     delete[] ndihedrallist;
56     delete[] maxdihedral;
57     for (int i = 0; i < nstyles; i++) memory->destroy(dihedrallist[i]);
58     delete[] dihedrallist;
59   }
60 }
61 
62 /* ---------------------------------------------------------------------- */
63 
compute(int eflag,int vflag)64 void DihedralHybrid::compute(int eflag, int vflag)
65 {
66   int i, j, m, n;
67 
68   // save ptrs to original dihedrallist
69 
70   int ndihedrallist_orig = neighbor->ndihedrallist;
71   int **dihedrallist_orig = neighbor->dihedrallist;
72 
73   // if this is re-neighbor step, create sub-style dihedrallists
74   // ndihedrallist[] = length of each sub-style list
75   // realloc sub-style dihedrallist if necessary
76   // load sub-style dihedrallist with 5 values from original dihedrallist
77 
78   if (neighbor->ago == 0) {
79     for (m = 0; m < nstyles; m++) ndihedrallist[m] = 0;
80     for (i = 0; i < ndihedrallist_orig; i++) {
81       m = map[dihedrallist_orig[i][4]];
82       if (m >= 0) ndihedrallist[m]++;
83     }
84     for (m = 0; m < nstyles; m++) {
85       if (ndihedrallist[m] > maxdihedral[m]) {
86         memory->destroy(dihedrallist[m]);
87         maxdihedral[m] = ndihedrallist[m] + EXTRA;
88         memory->create(dihedrallist[m], maxdihedral[m], 5, "dihedral_hybrid:dihedrallist");
89       }
90       ndihedrallist[m] = 0;
91     }
92     for (i = 0; i < ndihedrallist_orig; i++) {
93       m = map[dihedrallist_orig[i][4]];
94       if (m < 0) continue;
95       n = ndihedrallist[m];
96       dihedrallist[m][n][0] = dihedrallist_orig[i][0];
97       dihedrallist[m][n][1] = dihedrallist_orig[i][1];
98       dihedrallist[m][n][2] = dihedrallist_orig[i][2];
99       dihedrallist[m][n][3] = dihedrallist_orig[i][3];
100       dihedrallist[m][n][4] = dihedrallist_orig[i][4];
101       ndihedrallist[m]++;
102     }
103   }
104 
105   // call each sub-style's compute function
106   // set neighbor->dihedrallist to sub-style dihedrallist before call
107   // accumulate sub-style global/peratom energy/virial in hybrid
108 
109   ev_init(eflag, vflag);
110 
111   // need to clear per-thread storage here, when using multiple threads
112   // with thread-enabled substyles to avoid uninitlialized data access.
113 
114   const int nthreads = comm->nthreads;
115   if (comm->nthreads > 1) {
116     const bigint nall = atom->nlocal + atom->nghost;
117     if (eflag_atom) memset(&eatom[0], 0, nall * nthreads * sizeof(double));
118     if (vflag_atom) memset(&vatom[0][0], 0, 6 * nall * nthreads * sizeof(double));
119   }
120 
121   for (m = 0; m < nstyles; m++) {
122     neighbor->ndihedrallist = ndihedrallist[m];
123     neighbor->dihedrallist = dihedrallist[m];
124 
125     styles[m]->compute(eflag, vflag);
126 
127     if (eflag_global) energy += styles[m]->energy;
128     if (vflag_global)
129       for (n = 0; n < 6; n++) virial[n] += styles[m]->virial[n];
130     if (eflag_atom) {
131       n = atom->nlocal;
132       if (force->newton_bond) n += atom->nghost;
133       double *eatom_substyle = styles[m]->eatom;
134       for (i = 0; i < n; i++) eatom[i] += eatom_substyle[i];
135     }
136     if (vflag_atom) {
137       n = atom->nlocal;
138       if (force->newton_bond) n += atom->nghost;
139       double **vatom_substyle = styles[m]->vatom;
140       for (i = 0; i < n; i++)
141         for (j = 0; j < 6; j++) vatom[i][j] += vatom_substyle[i][j];
142     }
143     if (cvflag_atom) {
144       n = atom->nlocal;
145       if (force->newton_bond) n += atom->nghost;
146       double **cvatom_substyle = styles[m]->cvatom;
147       for (i = 0; i < n; i++)
148         for (j = 0; j < 9; j++) cvatom[i][j] += cvatom_substyle[i][j];
149     }
150   }
151 
152   // restore ptrs to original dihedrallist
153 
154   neighbor->ndihedrallist = ndihedrallist_orig;
155   neighbor->dihedrallist = dihedrallist_orig;
156 }
157 
158 /* ---------------------------------------------------------------------- */
159 
allocate()160 void DihedralHybrid::allocate()
161 {
162   allocated = 1;
163   int n = atom->ndihedraltypes;
164 
165   memory->create(map, n + 1, "dihedral:map");
166   memory->create(setflag, n + 1, "dihedral:setflag");
167   for (int i = 1; i <= n; i++) setflag[i] = 0;
168 
169   ndihedrallist = new int[nstyles];
170   maxdihedral = new int[nstyles];
171   dihedrallist = new int **[nstyles];
172   for (int m = 0; m < nstyles; m++) maxdihedral[m] = 0;
173   for (int m = 0; m < nstyles; m++) dihedrallist[m] = nullptr;
174 }
175 
176 /* ----------------------------------------------------------------------
177    create one dihedral style for each arg in list
178 ------------------------------------------------------------------------- */
179 
settings(int narg,char ** arg)180 void DihedralHybrid::settings(int narg, char **arg)
181 {
182   int i, m, istyle;
183 
184   if (narg < 1) error->all(FLERR, "Illegal dihedral_style command");
185 
186   // delete old lists, since cannot just change settings
187 
188   if (nstyles) {
189     for (i = 0; i < nstyles; i++) delete styles[i];
190     delete[] styles;
191     for (i = 0; i < nstyles; i++) delete[] keywords[i];
192     delete[] keywords;
193   }
194 
195   if (allocated) {
196     memory->destroy(setflag);
197     memory->destroy(map);
198     delete[] ndihedrallist;
199     delete[] maxdihedral;
200     for (i = 0; i < nstyles; i++) memory->destroy(dihedrallist[i]);
201     delete[] dihedrallist;
202   }
203   allocated = 0;
204 
205   // count sub-styles by skipping numeric args
206   // one exception is 1st arg of style "table", which is non-numeric word
207   // need a better way to skip these exceptions
208 
209   nstyles = 0;
210   i = 0;
211   while (i < narg) {
212     if (strcmp(arg[i], "table") == 0) i++;
213     i++;
214     while (i < narg && !isalpha(arg[i][0])) i++;
215     nstyles++;
216   }
217 
218   // allocate list of sub-styles
219 
220   styles = new Dihedral *[nstyles];
221   keywords = new char *[nstyles];
222 
223   // allocate each sub-style and call its settings() with subset of args
224   // allocate uses suffix, but don't store suffix version in keywords,
225   //   else syntax in coeff() will not match
226   // define subset of args for a sub-style by skipping numeric args
227   // one exception is 1st arg of style "table", which is non-numeric
228   // need a better way to skip these exceptions
229 
230   int dummy;
231   nstyles = 0;
232   i = 0;
233 
234   while (i < narg) {
235     for (m = 0; m < nstyles; m++)
236       if (strcmp(arg[i], keywords[m]) == 0)
237         error->all(FLERR, "Dihedral style hybrid cannot use same dihedral style twice");
238     if (strcmp(arg[i], "hybrid") == 0)
239       error->all(FLERR, "Dihedral style hybrid cannot have hybrid as an argument");
240     if (strcmp(arg[i], "none") == 0)
241       error->all(FLERR, "Dihedral style hybrid cannot have none as an argument");
242 
243     styles[nstyles] = force->new_dihedral(arg[i], 1, dummy);
244     force->store_style(keywords[nstyles], arg[i], 0);
245 
246     istyle = i;
247     if (strcmp(arg[i], "table") == 0) i++;
248     i++;
249     while (i < narg && !isalpha(arg[i][0])) i++;
250     styles[nstyles]->settings(i - istyle - 1, &arg[istyle + 1]);
251     nstyles++;
252   }
253 }
254 
255 /* ----------------------------------------------------------------------
256    set coeffs for one type
257 ---------------------------------------------------------------------- */
258 
coeff(int narg,char ** arg)259 void DihedralHybrid::coeff(int narg, char **arg)
260 {
261   if (!allocated) allocate();
262 
263   int ilo, ihi;
264   utils::bounds(FLERR, arg[0], 1, atom->ndihedraltypes, ilo, ihi, error);
265 
266   // 2nd arg = dihedral sub-style name
267   // allow for "none" or "skip" as valid sub-style name
268 
269   int m;
270   for (m = 0; m < nstyles; m++)
271     if (strcmp(arg[1], keywords[m]) == 0) break;
272 
273   int none = 0;
274   int skip = 0;
275   if (m == nstyles) {
276     if (strcmp(arg[1], "none") == 0)
277       none = 1;
278     else if (strcmp(arg[1], "skip") == 0)
279       none = skip = 1;
280     else if (strcmp(arg[1], "mbt") == 0)
281       error->all(FLERR, "MiddleBondTorsion coeff for hybrid dihedral has invalid format");
282     else if (strcmp(arg[1], "ebt") == 0)
283       error->all(FLERR, "EndBondTorsion coeff for hybrid dihedral has invalid format");
284     else if (strcmp(arg[1], "at") == 0)
285       error->all(FLERR, "AngleTorsion coeff for hybrid dihedral has invalid format");
286     else if (strcmp(arg[1], "aat") == 0)
287       error->all(FLERR, "AngleAngleTorsion coeff for hybrid dihedral has invalid format");
288     else if (strcmp(arg[1], "bb13") == 0)
289       error->all(FLERR, "BondBond13 coeff for hybrid dihedral has invalid format");
290     else
291       error->all(FLERR, "Dihedral coeff for hybrid has invalid style");
292   }
293 
294   // move 1st arg to 2nd arg
295   // just copy ptrs, since arg[] points into original input line
296 
297   arg[1] = arg[0];
298 
299   // invoke sub-style coeff() starting with 1st arg
300 
301   if (!none) styles[m]->coeff(narg - 1, &arg[1]);
302 
303   // set setflag and which type maps to which sub-style
304   // if sub-style is skip: auxiliary class2 setting in data file so ignore
305   // if sub-style is none and not skip: set hybrid setflag, wipe out map
306 
307   for (int i = ilo; i <= ihi; i++) {
308     if (skip)
309       continue;
310     else if (none) {
311       setflag[i] = 1;
312       map[i] = -1;
313     } else {
314       setflag[i] = styles[m]->setflag[i];
315       map[i] = m;
316     }
317   }
318 }
319 
320 /* ----------------------------------------------------------------------
321    run dihedral style specific initialization
322 ------------------------------------------------------------------------- */
323 
init_style()324 void DihedralHybrid::init_style()
325 {
326   for (int m = 0; m < nstyles; m++)
327     if (styles[m]) styles[m]->init_style();
328 }
329 
330 /* ----------------------------------------------------------------------
331    proc 0 writes to restart file
332 ------------------------------------------------------------------------- */
333 
write_restart(FILE * fp)334 void DihedralHybrid::write_restart(FILE *fp)
335 {
336   fwrite(&nstyles, sizeof(int), 1, fp);
337 
338   int n;
339   for (int m = 0; m < nstyles; m++) {
340     n = strlen(keywords[m]) + 1;
341     fwrite(&n, sizeof(int), 1, fp);
342     fwrite(keywords[m], sizeof(char), n, fp);
343     styles[m]->write_restart_settings(fp);
344   }
345 }
346 
347 /* ----------------------------------------------------------------------
348    proc 0 reads from restart file, bcasts
349 ------------------------------------------------------------------------- */
350 
read_restart(FILE * fp)351 void DihedralHybrid::read_restart(FILE *fp)
352 {
353   int me = comm->me;
354   if (me == 0) utils::sfread(FLERR, &nstyles, sizeof(int), 1, fp, nullptr, error);
355   MPI_Bcast(&nstyles, 1, MPI_INT, 0, world);
356   styles = new Dihedral *[nstyles];
357   keywords = new char *[nstyles];
358 
359   allocate();
360 
361   int n, dummy;
362   for (int m = 0; m < nstyles; m++) {
363     if (me == 0) utils::sfread(FLERR, &n, sizeof(int), 1, fp, nullptr, error);
364     MPI_Bcast(&n, 1, MPI_INT, 0, world);
365     keywords[m] = new char[n];
366     if (me == 0) utils::sfread(FLERR, keywords[m], sizeof(char), n, fp, nullptr, error);
367     MPI_Bcast(keywords[m], n, MPI_CHAR, 0, world);
368     styles[m] = force->new_dihedral(keywords[m], 0, dummy);
369     styles[m]->read_restart_settings(fp);
370   }
371 }
372 
373 /* ----------------------------------------------------------------------
374    memory usage
375 ------------------------------------------------------------------------- */
376 
memory_usage()377 double DihedralHybrid::memory_usage()
378 {
379   double bytes = (double) maxeatom * sizeof(double);
380   bytes += (double) maxvatom * 6 * sizeof(double);
381   bytes += (double) maxcvatom * 9 * sizeof(double);
382   for (int m = 0; m < nstyles; m++) bytes += (double) maxdihedral[m] * 5 * sizeof(int);
383   for (int m = 0; m < nstyles; m++)
384     if (styles[m]) bytes += styles[m]->memory_usage();
385   return bytes;
386 }
387