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
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
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_external.h"
#include <cstring>
#include "atom.h"
#include "update.h"
#include "memory.h"
#include "error.h"
#include "force.h"
using namespace LAMMPS_NS;
using namespace FixConst;
enum{PF_CALLBACK,PF_ARRAY};
/* ---------------------------------------------------------------------- */
FixExternal::FixExternal(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg),
fexternal(NULL), caller_vector(NULL)
{
if (narg < 4) error->all(FLERR,"Illegal fix external command");
scalar_flag = 1;
global_freq = 1;
virial_flag = 1;
thermo_virial = 1;
extscalar = 1;
if (strcmp(arg[3],"pf/callback") == 0) {
if (narg != 6) error->all(FLERR,"Illegal fix external command");
mode = PF_CALLBACK;
ncall = force->inumeric(FLERR,arg[4]);
napply = force->inumeric(FLERR,arg[5]);
if (ncall <= 0 || napply <= 0)
error->all(FLERR,"Illegal fix external command");
} else if (strcmp(arg[3],"pf/array") == 0) {
if (narg != 5) error->all(FLERR,"Illegal fix external command");
mode = PF_ARRAY;
napply = force->inumeric(FLERR,arg[4]);
if (napply <= 0) error->all(FLERR,"Illegal fix external command");
} else error->all(FLERR,"Illegal fix external command");
callback = NULL;
// perform initial allocation of atom-based array
// register with Atom class
grow_arrays(atom->nmax);
atom->add_callback(0);
user_energy = 0.0;
// optional vector of values provided by caller
// vector_flag and size_vector are setup via set_vector_length()
caller_vector = NULL;
}
/* ---------------------------------------------------------------------- */
FixExternal::~FixExternal()
{
// unregister callbacks to this fix from Atom class
atom->delete_callback(id,0);
memory->destroy(fexternal);
delete [] caller_vector;
}
/* ---------------------------------------------------------------------- */
int FixExternal::setmask()
{
int mask = 0;
if (mode == PF_CALLBACK || mode == PF_ARRAY) {
mask |= PRE_REVERSE;
mask |= POST_FORCE;
mask |= THERMO_ENERGY;
mask |= MIN_POST_FORCE;
}
return mask;
}
/* ---------------------------------------------------------------------- */
void FixExternal::init()
{
if (mode == PF_CALLBACK && callback == NULL)
error->all(FLERR,"Fix external callback function not set");
}
/* ---------------------------------------------------------------------- */
void FixExternal::setup(int vflag)
{
post_force(vflag);
}
/* --------------------------------------------------------------------- */
void FixExternal::setup_pre_reverse(int eflag, int vflag)
{
pre_reverse(eflag,vflag);
}
/* ---------------------------------------------------------------------- */
void FixExternal::min_setup(int vflag)
{
post_force(vflag);
}
/* ----------------------------------------------------------------------
store eflag, so can use it in post_force to tally per-atom energies
------------------------------------------------------------------------- */
void FixExternal::pre_reverse(int eflag, int /*vflag*/)
{
eflag_caller = eflag;
}
/* ---------------------------------------------------------------------- */
void FixExternal::post_force(int vflag)
{
bigint ntimestep = update->ntimestep;
int eflag = eflag_caller;
ev_init(eflag,vflag);
// invoke the callback in driver program
// it will fill fexternal with forces
if (mode == PF_CALLBACK && ntimestep % ncall == 0)
(this->callback)(ptr_caller,update->ntimestep,
atom->nlocal,atom->tag,atom->x,fexternal);
// add forces from fexternal to atoms in group
if (ntimestep % napply == 0) {
double **f = atom->f;
int *mask = atom->mask;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
f[i][0] += fexternal[i][0];
f[i][1] += fexternal[i][1];
f[i][2] += fexternal[i][2];
}
}
}
/* ---------------------------------------------------------------------- */
void FixExternal::min_post_force(int vflag)
{
post_force(vflag);
}
// ----------------------------------------------------------------------
// "set" methods caller can invoke directly
// ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
caller invokes this method to set its contribution to global energy
unlike other energy/virial set methods:
do not just return if eflag_global is not set
b/c input script could access this quantity via compute_scalar()
even if eflag is not set on a particular timestep
------------------------------------------------------------------------- */
void FixExternal::set_energy_global(double caller_energy)
{
user_energy = caller_energy;
}
/* ----------------------------------------------------------------------
caller invokes this method to set its contribution to global virial
------------------------------------------------------------------------- */
void FixExternal::set_virial_global(double *caller_virial)
{
if (!evflag) return;
if (!vflag_global) return;
for (int i = 0; i < 6; i++)
virial[i] = caller_virial[i];
}
/* ----------------------------------------------------------------------
caller invokes this method to set its contribution to peratom energy
------------------------------------------------------------------------- */
void FixExternal::set_energy_peratom(double *caller_energy)
{
if (!eflag_atom) return;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
eatom[i] = caller_energy[i];
}
/* ----------------------------------------------------------------------
caller invokes this method to set its contribution to peratom virial
------------------------------------------------------------------------- */
void FixExternal::set_virial_peratom(double **caller_virial)
{
int i,j;
if (!evflag) return;
if (!vflag_atom) return;
int nlocal = atom->nlocal;
for (i = 0; i < nlocal; i++)
for (j = 0; j < 6; j++)
vatom[i][j] = caller_virial[i][j];
}
/* ----------------------------------------------------------------------
caller invokes this method to set length of vector of values
assume all vector values are extensive, could make this an option
------------------------------------------------------------------------- */
void FixExternal::set_vector_length(int n)
{
delete [] caller_vector;
vector_flag = 1;
size_vector = n;
extvector = 1;
caller_vector = new double[n];
}
/* ----------------------------------------------------------------------
caller invokes this method to set Index value in vector
index ranges from 1 to N inclusive
------------------------------------------------------------------------- */
void FixExternal::set_vector(int index, double value)
{
if (index >= size_vector)
error->all(FLERR,"Invalid set_vector index in fix external");
caller_vector[index-1] = value;
}
/* ----------------------------------------------------------------------
potential energy of added force
up to user to set it via set_energy()
------------------------------------------------------------------------- */
double FixExternal::compute_scalar()
{
return user_energy;
}
/* ----------------------------------------------------------------------
arbitrary value computed by caller
up to user to set it via set_vector()
------------------------------------------------------------------------- */
double FixExternal::compute_vector(int n)
{
return caller_vector[n];
}
/* ----------------------------------------------------------------------
memory usage of local atom-based array
------------------------------------------------------------------------- */
double FixExternal::memory_usage()
{
double bytes = 3*atom->nmax * sizeof(double);
return bytes;
}
/* ----------------------------------------------------------------------
allocate atom-based array
------------------------------------------------------------------------- */
void FixExternal::grow_arrays(int nmax)
{
memory->grow(fexternal,nmax,3,"external:fexternal");
}
/* ----------------------------------------------------------------------
copy values within local atom-based array
------------------------------------------------------------------------- */
void FixExternal::copy_arrays(int i, int j, int /*delflag*/)
{
fexternal[j][0] = fexternal[i][0];
fexternal[j][1] = fexternal[i][1];
fexternal[j][2] = fexternal[i][2];
}
/* ----------------------------------------------------------------------
pack values in local atom-based array for exchange with another proc
------------------------------------------------------------------------- */
int FixExternal::pack_exchange(int i, double *buf)
{
buf[0] = fexternal[i][0];
buf[1] = fexternal[i][1];
buf[2] = fexternal[i][2];
return 3;
}
/* ----------------------------------------------------------------------
unpack values in local atom-based array from exchange with another proc
------------------------------------------------------------------------- */
int FixExternal::unpack_exchange(int nlocal, double *buf)
{
fexternal[nlocal][0] = buf[0];
fexternal[nlocal][1] = buf[1];
fexternal[nlocal][2] = buf[2];
return 3;
}
/* ----------------------------------------------------------------------
external caller sets a callback function to invoke in post_force()
------------------------------------------------------------------------- */
void FixExternal::set_callback(FnPtr caller_callback, void *caller_ptr)
{
callback = caller_callback;
ptr_caller = caller_ptr;
}