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 "angle_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
AngleHybrid(LAMMPS * lmp)32 AngleHybrid::AngleHybrid(LAMMPS *lmp) : Angle(lmp)
33 {
34 writedata = 0;
35 nstyles = 0;
36 nanglelist = nullptr;
37 maxangle = nullptr;
38 anglelist = nullptr;
39 }
40
41 /* ---------------------------------------------------------------------- */
42
~AngleHybrid()43 AngleHybrid::~AngleHybrid()
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[] nanglelist;
56 delete[] maxangle;
57 for (int i = 0; i < nstyles; i++) memory->destroy(anglelist[i]);
58 delete[] anglelist;
59 }
60 }
61
62 /* ---------------------------------------------------------------------- */
63
compute(int eflag,int vflag)64 void AngleHybrid::compute(int eflag, int vflag)
65 {
66 int i, j, m, n;
67
68 // save ptrs to original anglelist
69
70 int nanglelist_orig = neighbor->nanglelist;
71 int **anglelist_orig = neighbor->anglelist;
72
73 // if this is re-neighbor step, create sub-style anglelists
74 // nanglelist[] = length of each sub-style list
75 // realloc sub-style anglelist if necessary
76 // load sub-style anglelist with 4 values from original anglelist
77
78 if (neighbor->ago == 0) {
79 for (m = 0; m < nstyles; m++) nanglelist[m] = 0;
80 for (i = 0; i < nanglelist_orig; i++) {
81 m = map[anglelist_orig[i][3]];
82 if (m >= 0) nanglelist[m]++;
83 }
84 for (m = 0; m < nstyles; m++) {
85 if (nanglelist[m] > maxangle[m]) {
86 memory->destroy(anglelist[m]);
87 maxangle[m] = nanglelist[m] + EXTRA;
88 memory->create(anglelist[m], maxangle[m], 4, "angle_hybrid:anglelist");
89 }
90 nanglelist[m] = 0;
91 }
92 for (i = 0; i < nanglelist_orig; i++) {
93 m = map[anglelist_orig[i][3]];
94 if (m < 0) continue;
95 n = nanglelist[m];
96 anglelist[m][n][0] = anglelist_orig[i][0];
97 anglelist[m][n][1] = anglelist_orig[i][1];
98 anglelist[m][n][2] = anglelist_orig[i][2];
99 anglelist[m][n][3] = anglelist_orig[i][3];
100 nanglelist[m]++;
101 }
102 }
103
104 // call each sub-style's compute function
105 // set neighbor->anglelist to sub-style anglelist before call
106 // accumulate sub-style global/peratom energy/virial in hybrid
107
108 ev_init(eflag, vflag);
109
110 // need to clear per-thread storage here, when using multiple threads
111 // with thread-enabled substyles to avoid uninitlialized data access.
112
113 const int nthreads = comm->nthreads;
114 if (comm->nthreads > 1) {
115 const bigint nall = atom->nlocal + atom->nghost;
116 if (eflag_atom) memset(&eatom[0], 0, nall * nthreads * sizeof(double));
117 if (vflag_atom) memset(&vatom[0][0], 0, 6 * nall * nthreads * sizeof(double));
118 }
119
120 for (m = 0; m < nstyles; m++) {
121 neighbor->nanglelist = nanglelist[m];
122 neighbor->anglelist = anglelist[m];
123
124 styles[m]->compute(eflag, vflag);
125
126 if (eflag_global) energy += styles[m]->energy;
127 if (vflag_global)
128 for (n = 0; n < 6; n++) virial[n] += styles[m]->virial[n];
129 if (eflag_atom) {
130 n = atom->nlocal;
131 if (force->newton_bond) n += atom->nghost;
132 double *eatom_substyle = styles[m]->eatom;
133 for (i = 0; i < n; i++) eatom[i] += eatom_substyle[i];
134 }
135 if (vflag_atom) {
136 n = atom->nlocal;
137 if (force->newton_bond) n += atom->nghost;
138 double **vatom_substyle = styles[m]->vatom;
139 for (i = 0; i < n; i++)
140 for (j = 0; j < 6; j++) vatom[i][j] += vatom_substyle[i][j];
141 }
142 if (cvflag_atom) {
143 n = atom->nlocal;
144 if (force->newton_bond) n += atom->nghost;
145 double **cvatom_substyle = styles[m]->cvatom;
146 for (i = 0; i < n; i++)
147 for (j = 0; j < 9; j++) cvatom[i][j] += cvatom_substyle[i][j];
148 }
149 }
150
151 // restore ptrs to original anglelist
152
153 neighbor->nanglelist = nanglelist_orig;
154 neighbor->anglelist = anglelist_orig;
155 }
156
157 /* ---------------------------------------------------------------------- */
158
allocate()159 void AngleHybrid::allocate()
160 {
161 allocated = 1;
162 int n = atom->nangletypes;
163
164 memory->create(map, n + 1, "angle:map");
165 memory->create(setflag, n + 1, "angle:setflag");
166 for (int i = 1; i <= n; i++) setflag[i] = 0;
167
168 nanglelist = new int[nstyles];
169 maxangle = new int[nstyles];
170 anglelist = new int **[nstyles];
171 for (int m = 0; m < nstyles; m++) maxangle[m] = 0;
172 for (int m = 0; m < nstyles; m++) anglelist[m] = nullptr;
173 }
174
175 /* ----------------------------------------------------------------------
176 create one angle style for each arg in list
177 ------------------------------------------------------------------------- */
178
settings(int narg,char ** arg)179 void AngleHybrid::settings(int narg, char **arg)
180 {
181 int i, m, istyle;
182
183 if (narg < 1) error->all(FLERR, "Illegal angle_style command");
184
185 // delete old lists, since cannot just change settings
186
187 if (nstyles) {
188 for (i = 0; i < nstyles; i++) delete styles[i];
189 delete[] styles;
190 for (i = 0; i < nstyles; i++) delete[] keywords[i];
191 delete[] keywords;
192 }
193
194 if (allocated) {
195 memory->destroy(setflag);
196 memory->destroy(map);
197 delete[] nanglelist;
198 delete[] maxangle;
199 for (i = 0; i < nstyles; i++) memory->destroy(anglelist[i]);
200 delete[] anglelist;
201 }
202 allocated = 0;
203
204 // count sub-styles by skipping numeric args
205 // one exception is 1st arg of style "table", which is non-numeric word
206 // need a better way to skip these exceptions
207
208 nstyles = 0;
209 i = 0;
210 while (i < narg) {
211 if (strcmp(arg[i], "table") == 0) i++;
212 i++;
213 while (i < narg && !isalpha(arg[i][0])) i++;
214 nstyles++;
215 }
216
217 // allocate list of sub-styles
218
219 styles = new Angle *[nstyles];
220 keywords = new char *[nstyles];
221
222 // allocate each sub-style and call its settings() with subset of args
223 // allocate uses suffix, but don't store suffix version in keywords,
224 // else syntax in coeff() will not match
225 // define subset of args for a sub-style by skipping numeric args
226 // one exception is 1st arg of style "table", which is non-numeric
227 // need a better way to skip these exceptions
228
229 int dummy;
230 nstyles = 0;
231 i = 0;
232
233 while (i < narg) {
234 for (m = 0; m < nstyles; m++)
235 if (strcmp(arg[i], keywords[m]) == 0)
236 error->all(FLERR, "Angle style hybrid cannot use same angle style twice");
237 if (strcmp(arg[i], "hybrid") == 0)
238 error->all(FLERR, "Angle style hybrid cannot have hybrid as an argument");
239 if (strcmp(arg[i], "none") == 0)
240 error->all(FLERR, "Angle style hybrid cannot have none as an argument");
241
242 styles[nstyles] = force->new_angle(arg[i], 1, dummy);
243 force->store_style(keywords[nstyles], arg[i], 0);
244
245 istyle = i;
246 if (strcmp(arg[i], "table") == 0) i++;
247 i++;
248 while (i < narg && !isalpha(arg[i][0])) i++;
249 styles[nstyles]->settings(i - istyle - 1, &arg[istyle + 1]);
250 nstyles++;
251 }
252 }
253
254 /* ----------------------------------------------------------------------
255 set coeffs for one type
256 ---------------------------------------------------------------------- */
257
coeff(int narg,char ** arg)258 void AngleHybrid::coeff(int narg, char **arg)
259 {
260 if (!allocated) allocate();
261
262 int ilo, ihi;
263 utils::bounds(FLERR, arg[0], 1, atom->nangletypes, ilo, ihi, error);
264
265 // 2nd arg = angle sub-style name
266 // allow for "none" or "skip" as valid sub-style name
267
268 int m;
269 for (m = 0; m < nstyles; m++)
270 if (strcmp(arg[1], keywords[m]) == 0) break;
271
272 int none = 0;
273 int skip = 0;
274 if (m == nstyles) {
275 if (strcmp(arg[1], "none") == 0)
276 none = 1;
277 else if (strcmp(arg[1], "skip") == 0)
278 none = skip = 1;
279 else if (strcmp(arg[1], "ba") == 0)
280 error->all(FLERR, "BondAngle coeff for hybrid angle has invalid format");
281 else if (strcmp(arg[1], "bb") == 0)
282 error->all(FLERR, "BondBond coeff for hybrid angle has invalid format");
283 else
284 error->all(FLERR, "Angle coeff for hybrid has invalid style");
285 }
286
287 // move 1st arg to 2nd arg
288 // just copy ptrs, since arg[] points into original input line
289
290 arg[1] = arg[0];
291
292 // invoke sub-style coeff() starting with 1st arg
293
294 if (!none) styles[m]->coeff(narg - 1, &arg[1]);
295
296 // set setflag and which type maps to which sub-style
297 // if sub-style is skip: auxiliary class2 setting in data file so ignore
298 // if sub-style is none: set hybrid setflag, wipe out map
299
300 for (int i = ilo; i <= ihi; i++) {
301 if (skip)
302 continue;
303 else if (none) {
304 setflag[i] = 1;
305 map[i] = -1;
306 } else {
307 setflag[i] = styles[m]->setflag[i];
308 map[i] = m;
309 }
310 }
311 }
312
313 /* ----------------------------------------------------------------------
314 run angle style specific initialization
315 ------------------------------------------------------------------------- */
316
init_style()317 void AngleHybrid::init_style()
318 {
319 for (int m = 0; m < nstyles; m++)
320 if (styles[m]) styles[m]->init_style();
321 }
322
323 /* ----------------------------------------------------------------------
324 return an equilbrium angle length
325 ------------------------------------------------------------------------- */
326
equilibrium_angle(int i)327 double AngleHybrid::equilibrium_angle(int i)
328 {
329 if (map[i] < 0) error->one(FLERR, "Invoked angle equil angle on angle style none");
330 return styles[map[i]]->equilibrium_angle(i);
331 }
332
333 /* ----------------------------------------------------------------------
334 proc 0 writes to restart file
335 ------------------------------------------------------------------------- */
336
write_restart(FILE * fp)337 void AngleHybrid::write_restart(FILE *fp)
338 {
339 fwrite(&nstyles, sizeof(int), 1, fp);
340
341 int n;
342 for (int m = 0; m < nstyles; m++) {
343 n = strlen(keywords[m]) + 1;
344 fwrite(&n, sizeof(int), 1, fp);
345 fwrite(keywords[m], sizeof(char), n, fp);
346 styles[m]->write_restart_settings(fp);
347 }
348 }
349
350 /* ----------------------------------------------------------------------
351 proc 0 reads from restart file, bcasts
352 ------------------------------------------------------------------------- */
353
read_restart(FILE * fp)354 void AngleHybrid::read_restart(FILE *fp)
355 {
356 int me = comm->me;
357 if (me == 0) utils::sfread(FLERR, &nstyles, sizeof(int), 1, fp, nullptr, error);
358 MPI_Bcast(&nstyles, 1, MPI_INT, 0, world);
359 styles = new Angle *[nstyles];
360 keywords = new char *[nstyles];
361
362 allocate();
363
364 int n, dummy;
365 for (int m = 0; m < nstyles; m++) {
366 if (me == 0) utils::sfread(FLERR, &n, sizeof(int), 1, fp, nullptr, error);
367 MPI_Bcast(&n, 1, MPI_INT, 0, world);
368 keywords[m] = new char[n];
369 if (me == 0) utils::sfread(FLERR, keywords[m], sizeof(char), n, fp, nullptr, error);
370 MPI_Bcast(keywords[m], n, MPI_CHAR, 0, world);
371 styles[m] = force->new_angle(keywords[m], 0, dummy);
372 styles[m]->read_restart_settings(fp);
373 }
374 }
375
376 /* ---------------------------------------------------------------------- */
377
single(int type,int i1,int i2,int i3)378 double AngleHybrid::single(int type, int i1, int i2, int i3)
379 {
380 if (map[type] < 0) error->one(FLERR, "Invoked angle single on angle style none");
381 return styles[map[type]]->single(type, i1, i2, i3);
382 }
383
384 /* ----------------------------------------------------------------------
385 memory usage
386 ------------------------------------------------------------------------- */
387
memory_usage()388 double AngleHybrid::memory_usage()
389 {
390 double bytes = (double) maxeatom * sizeof(double);
391 bytes += (double) maxvatom * 6 * sizeof(double);
392 bytes += (double) maxcvatom * 9 * sizeof(double);
393 for (int m = 0; m < nstyles; m++) bytes += (double) maxangle[m] * 4 * sizeof(int);
394 for (int m = 0; m < nstyles; m++)
395 if (styles[m]) bytes += styles[m]->memory_usage();
396 return bytes;
397 }
398