-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
fix_ave_atom.cpp
428 lines (339 loc) · 13.8 KB
/
fix_ave_atom.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// clang-format off
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "fix_ave_atom.h"
#include "arg_info.h"
#include "atom.h"
#include "compute.h"
#include "error.h"
#include "input.h"
#include "memory.h"
#include "modify.h"
#include "update.h"
#include "variable.h"
#include <cstring>
using namespace LAMMPS_NS;
using namespace FixConst;
/* ---------------------------------------------------------------------- */
FixAveAtom::FixAveAtom(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg), array(nullptr)
{
if (narg < 7) utils::missing_cmd_args(FLERR, "fix ave/atom", error);
nevery = utils::inumeric(FLERR, arg[3], false, lmp);
nrepeat = utils::inumeric(FLERR, arg[4], false, lmp);
peratom_freq = utils::inumeric(FLERR, arg[5], false, lmp);
time_depend = 1;
// expand args if any have wildcard character "*"
// this can reset nvalues
int expand = 0;
char **earg;
int nvalues = utils::expand_args(FLERR, narg - 6, &arg[6], 1, earg, lmp);
if (earg != &arg[6]) expand = 1;
arg = earg;
// parse values
values.clear();
for (int i = 0; i < nvalues; i++) {
value_t val;
val.id = "";
val.val.c = nullptr;
if (strcmp(arg[i], "x") == 0) {
val.which = ArgInfo::X;
val.argindex = 0;
} else if (strcmp(arg[i], "y") == 0) {
val.which = ArgInfo::X;
val.argindex = 1;
} else if (strcmp(arg[i], "z") == 0) {
val.which = ArgInfo::X;
val.argindex = 2;
} else if (strcmp(arg[i], "vx") == 0) {
val.which = ArgInfo::V;
val.argindex = 0;
} else if (strcmp(arg[i], "vy") == 0) {
val.which = ArgInfo::V;
val.argindex = 1;
} else if (strcmp(arg[i], "vz") == 0) {
val.which = ArgInfo::V;
val.argindex = 2;
} else if (strcmp(arg[i], "fx") == 0) {
val.which = ArgInfo::F;
val.argindex = 0;
} else if (strcmp(arg[i], "fy") == 0) {
val.which = ArgInfo::F;
val.argindex = 1;
} else if (strcmp(arg[i], "fz") == 0) {
val.which = ArgInfo::F;
val.argindex = 2;
} else {
ArgInfo argi(arg[i]);
val.which = argi.get_type();
val.argindex = argi.get_index1();
val.id = argi.get_name();
if ((val.which == ArgInfo::UNKNOWN) || (val.which == ArgInfo::NONE) || (argi.get_dim() > 1))
error->all(FLERR, "Invalid fix ave/atom argument: {}", arg[i]);
}
values.push_back(val);
}
// if wildcard expansion occurred, free earg memory from exapnd_args()
if (expand) {
for (int i = 0; i < nvalues; i++) delete[] earg[i];
memory->sfree(earg);
}
// setup and error check
// for fix inputs, check that fix frequency is acceptable
if (nevery <= 0) error->all(FLERR,"Illegal fix ave/atom nevery value: {}", nevery);
if (nrepeat <= 0) error->all(FLERR,"Illegal fix ave/atom nrepeat value: {}", nrepeat);
if (peratom_freq <= 0) error->all(FLERR,"Illegal fix ave/atom nfreq value: {}", peratom_freq);
if (peratom_freq % nevery || nrepeat*nevery > peratom_freq)
error->all(FLERR,"Inconsistent fix ave/atom nevery/nrepeat/nfreq values");
for (auto &val : values) {
if (val.which == ArgInfo::COMPUTE) {
val.val.c = modify->get_compute_by_id(val.id);
if (!val.val.c) error->all(FLERR,"Compute ID {} for fix ave/atom does not exist", val.id);
if (val.val.c->peratom_flag == 0)
error->all(FLERR, "Fix ave/atom compute {} does not calculate per-atom values", val.id);
if (val.argindex == 0 && val.val.c->size_peratom_cols != 0)
error->all(FLERR,"Fix ave/atom compute {} does not calculate a per-atom vector", val.id);
if (val.argindex && val.val.c->size_peratom_cols == 0)
error->all(FLERR,"Fix ave/atom compute {} does not calculate a per-atom array", val.id);
if (val.argindex && val.argindex > val.val.c->size_peratom_cols)
error->all(FLERR,"Fix ave/atom compute {} array is accessed out-of-range", val.id);
} else if (val.which == ArgInfo::FIX) {
val.val.f = modify->get_fix_by_id(val.id);
if (!val.val.f) error->all(FLERR, "Fix ID {} for fix ave/atom does not exist", val.id);
if (val.val.f->peratom_flag == 0)
error->all(FLERR, "Fix ave/atom fix {} does not calculate per-atom values", val.id);
if (val.argindex == 0 && val.val.f->size_peratom_cols != 0)
error->all(FLERR, "Fix ave/atom fix {} does not calculate a per-atom vector", val.id);
if (val.argindex && val.val.f->size_peratom_cols == 0)
error->all(FLERR, "Fix ave/atom fix {} does not calculate a per-atom array", val.id);
if (val.argindex && val.argindex > val.val.f->size_peratom_cols)
error->all(FLERR,"Fix ave/atom fix {} array is accessed out-of-range", val.id);
if (nevery % val.val.f->peratom_freq)
error->all(FLERR, "Fix {} for fix ave/atom not computed at compatible time", val.id);
} else if (val.which == ArgInfo::VARIABLE) {
val.val.v = input->variable->find(val.id.c_str());
if (val.val.v < 0)
error->all(FLERR,"Variable name {} for fix ave/atom does not exist", val.id);
if (input->variable->atomstyle(val.val.v) == 0)
error->all(FLERR,"Fix ave/atom variable {} is not atom-style variable", val.id);
}
}
// this fix produces either a per-atom vector or array
peratom_flag = 1;
if (values.size() == 1) size_peratom_cols = 0;
else size_peratom_cols = values.size();
// perform initial allocation of atom-based array
// register with Atom class
FixAveAtom::grow_arrays(atom->nmax);
atom->add_callback(Atom::GROW);
// zero the array since dump may access it on timestep 0
// zero the array since a variable may access it before first run
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
for (std::size_t m = 0; m < values.size(); m++)
array[i][m] = 0.0;
// nvalid = next step on which end_of_step does something
// add nvalid to all computes that store invocation times
// since don't know a priori which are invoked by this fix
// once in end_of_step() can set timestep for ones actually invoked
irepeat = 0;
nvalid_last = -1;
nvalid = nextvalid();
modify->addstep_compute_all(nvalid);
}
/* ---------------------------------------------------------------------- */
FixAveAtom::~FixAveAtom()
{
// unregister callback to this fix from Atom class
atom->delete_callback(id,Atom::GROW);
memory->destroy(array);
}
/* ---------------------------------------------------------------------- */
int FixAveAtom::setmask()
{
int mask = 0;
mask |= END_OF_STEP;
return mask;
}
/* ---------------------------------------------------------------------- */
void FixAveAtom::init()
{
// set indices and check validity of all computes,fixes,variables
for (auto &val : values) {
if (val.which == ArgInfo::COMPUTE) {
val.val.c = modify->get_compute_by_id(val.id);
if (!val.val.c) error->all(FLERR, "Compute ID {} for fix ave/atom does not exist", val.id);
} else if (val.which == ArgInfo::FIX) {
val.val.f = modify->get_fix_by_id(val.id);
if (!val.val.f) error->all(FLERR, "Fix ID {} for fix ave/atom does not exist", val.id);
} else if (val.which == ArgInfo::VARIABLE) {
val.val.v = input->variable->find(val.id.c_str());
if (val.val.v < 0)
error->all(FLERR,"Variable name {} for fix ave/atom does not exist", val.id);
}
}
// need to reset nvalid if nvalid < ntimestep b/c minimize was performed
if (nvalid < update->ntimestep) {
irepeat = 0;
nvalid = nextvalid();
modify->addstep_compute_all(nvalid);
}
}
/* ----------------------------------------------------------------------
only does something if nvalid = current timestep
------------------------------------------------------------------------- */
void FixAveAtom::setup(int /*vflag*/)
{
end_of_step();
}
/* ---------------------------------------------------------------------- */
void FixAveAtom::end_of_step()
{
// skip if not step which requires doing something
bigint ntimestep = update->ntimestep;
if (ntimestep != nvalid) return;
nvalid_last = nvalid;
// zero if first step
int nlocal = atom->nlocal;
if (irepeat == 0)
for (int i = 0; i < nlocal; i++)
for (std::size_t m = 0; m < values.size(); m++)
array[i][m] = 0.0;
// accumulate results of attributes,computes,fixes,variables to local copy
// compute/fix/variable may invoke computes so wrap with clear/add
modify->clearstep_compute();
int *mask = atom->mask;
int i, j, m = 0;
for (auto &val : values) {
j = val.argindex;
if (val.which == ArgInfo::X) {
double **x = atom->x;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) array[i][m] += x[i][j];
} else if (val.which == ArgInfo::V) {
double **v = atom->v;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) array[i][m] += v[i][j];
} else if (val.which == ArgInfo::F) {
double **f = atom->f;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) array[i][m] += f[i][j];
// invoke compute if not previously invoked
} else if (val.which == ArgInfo::COMPUTE) {
if (!(val.val.c->invoked_flag & Compute::INVOKED_PERATOM)) {
val.val.c->compute_peratom();
val.val.c->invoked_flag |= Compute::INVOKED_PERATOM;
}
if (j == 0) {
double *compute_vector = val.val.c->vector_atom;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) array[i][m] += compute_vector[i];
} else {
int jm1 = j - 1;
double **compute_array = val.val.c->array_atom;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) array[i][m] += compute_array[i][jm1];
}
// access fix fields, guaranteed to be ready
} else if (val.which == ArgInfo::FIX) {
if (j == 0) {
double *fix_vector = val.val.f->vector_atom;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) array[i][m] += fix_vector[i];
} else {
int jm1 = j - 1;
double **fix_array = val.val.f->array_atom;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) array[i][m] += fix_array[i][jm1];
}
// evaluate atom-style variable
// final argument = 1 sums result to array
} else if (val.which == ArgInfo::VARIABLE) {
if (array) input->variable->compute_atom(val.val.v,igroup,&array[0][m],values.size(),1);
else input->variable->compute_atom(val.val.v,igroup,nullptr,values.size(),1);
}
++m;
}
// done if irepeat < nrepeat
// else reset irepeat and nvalid
irepeat++;
if (irepeat < nrepeat) {
nvalid += nevery;
modify->addstep_compute(nvalid);
return;
}
irepeat = 0;
nvalid = ntimestep+peratom_freq - ((bigint)nrepeat-1)*nevery;
modify->addstep_compute(nvalid);
if (array == nullptr) return;
// average the final result for the Nfreq timestep
double repeat = nrepeat;
for (i = 0; i < nlocal; i++)
for (m = 0; m < (int)values.size(); m++)
array[i][m] /= repeat;
}
/* ----------------------------------------------------------------------
memory usage of local atom-based array
------------------------------------------------------------------------- */
double FixAveAtom::memory_usage()
{
double bytes;
bytes = (double)atom->nmax*values.size() * sizeof(double);
return bytes;
}
/* ----------------------------------------------------------------------
allocate atom-based array
------------------------------------------------------------------------- */
void FixAveAtom::grow_arrays(int nmax)
{
memory->grow(array,nmax,values.size(),"fix_ave/atom:array");
array_atom = array;
if (array) vector_atom = array[0];
else vector_atom = nullptr;
}
/* ----------------------------------------------------------------------
copy values within local atom-based array
------------------------------------------------------------------------- */
void FixAveAtom::copy_arrays(int i, int j, int /*delflag*/)
{
for (std::size_t m = 0; m < values.size(); m++)
array[j][m] = array[i][m];
}
/* ----------------------------------------------------------------------
pack values in local atom-based array for exchange with another proc
------------------------------------------------------------------------- */
int FixAveAtom::pack_exchange(int i, double *buf)
{
for (std::size_t m = 0; m < values.size(); m++) buf[m] = array[i][m];
return values.size();
}
/* ----------------------------------------------------------------------
unpack values in local atom-based array from exchange with another proc
------------------------------------------------------------------------- */
int FixAveAtom::unpack_exchange(int nlocal, double *buf)
{
for (std::size_t m = 0; m < values.size(); m++) array[nlocal][m] = buf[m];
return values.size();
}
/* ----------------------------------------------------------------------
calculate nvalid = next step on which end_of_step does something
can be this timestep if multiple of nfreq and nrepeat = 1
else backup from next multiple of nfreq
------------------------------------------------------------------------- */
bigint FixAveAtom::nextvalid()
{
bigint nvalid = (update->ntimestep/peratom_freq)*peratom_freq + peratom_freq;
if (nvalid-peratom_freq == update->ntimestep && nrepeat == 1)
nvalid = update->ntimestep;
else
nvalid -= ((bigint)nrepeat-1)*nevery;
if (nvalid < update->ntimestep) nvalid += peratom_freq;
return nvalid;
}