1
/* ----------------------------------------------------------------------
2
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
3
http://lammps.sandia.gov, Sandia National Laboratories
4
Steve Plimpton, sjplimp@sandia.gov
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.
11
See the README file in the top-level LAMMPS directory.
12
------------------------------------------------------------------------- */
15
#include "compute_com_chunk.h"
19
#include "compute_chunk_atom.h"
24
using namespace LAMMPS_NS;
26
enum{ONCE,NFREQ,EVERY};
28
/* ---------------------------------------------------------------------- */
30
ComputeCOMChunk::ComputeCOMChunk(LAMMPS *lmp, int narg, char **arg) :
31
Compute(lmp, narg, arg)
33
if (narg != 4) error->all(FLERR,"Illegal compute com/chunk command");
38
size_array_rows_variable = 1;
41
// ID of compute chunk/atom
43
int n = strlen(arg[3]) + 1;
44
idchunk = new char[n];
45
strcpy(idchunk,arg[3]);
53
massproc = masstotal = NULL;
57
firstflag = massneed = 1;
60
/* ---------------------------------------------------------------------- */
62
ComputeCOMChunk::~ComputeCOMChunk()
65
memory->destroy(massproc);
66
memory->destroy(masstotal);
68
memory->destroy(comall);
71
/* ---------------------------------------------------------------------- */
73
void ComputeCOMChunk::init()
75
int icompute = modify->find_compute(idchunk);
77
error->all(FLERR,"Chunk/atom compute does not exist for compute com/chunk");
78
cchunk = (ComputeChunkAtom *) modify->compute[icompute];
79
if (strcmp(cchunk->style,"chunk/atom") != 0)
80
error->all(FLERR,"Compute com/chunk does not use chunk/atom compute");
83
/* ---------------------------------------------------------------------- */
85
void ComputeCOMChunk::setup()
87
// one-time calculation of per-chunk mass
88
// done in setup, so that ComputeChunkAtom::setup() is already called
90
if (firstflag && cchunk->idsflag == ONCE) {
91
firstflag = massneed = 0;
96
/* ---------------------------------------------------------------------- */
98
void ComputeCOMChunk::compute_array()
104
invoked_array = update->ntimestep;
106
// compute chunk/atom assigns atoms to chunk IDs
107
// extract ichunk index vector from compute
108
// ichunk = 1 to Nchunk for included atoms, 0 for excluded atoms
110
nchunk = cchunk->setup_chunks();
111
cchunk->compute_ichunk();
112
int *ichunk = cchunk->ichunk;
114
if (nchunk > maxchunk) allocate();
115
size_array_rows = nchunk;
117
// zero local per-chunk values
119
for (int i = 0; i < nchunk; i++)
120
com[i][0] = com[i][1] = com[i][2] = 0.0;
122
for (int i = 0; i < nchunk; i++) massproc[i] = 0.0;
124
// compute COM for each chunk
126
double **x = atom->x;
127
int *mask = atom->mask;
128
int *type = atom->type;
129
imageint *image = atom->image;
130
double *mass = atom->mass;
131
double *rmass = atom->rmass;
132
int nlocal = atom->nlocal;
134
for (int i = 0; i < nlocal; i++)
135
if (mask[i] & groupbit) {
137
if (index < 0) continue;
138
if (rmass) massone = rmass[i];
139
else massone = mass[type[i]];
140
domain->unmap(x[i],image[i],unwrap);
141
com[index][0] += unwrap[0] * massone;
142
com[index][1] += unwrap[1] * massone;
143
com[index][2] += unwrap[2] * massone;
144
if (massneed) massproc[index] += massone;
147
MPI_Allreduce(&com[0][0],&comall[0][0],3*nchunk,MPI_DOUBLE,MPI_SUM,world);
149
MPI_Allreduce(massproc,masstotal,nchunk,MPI_DOUBLE,MPI_SUM,world);
151
for (int i = 0; i < nchunk; i++) {
152
if (masstotal[i] > 0.0) {
153
comall[i][0] /= masstotal[i];
154
comall[i][1] /= masstotal[i];
155
comall[i][2] /= masstotal[i];
156
} else comall[i][0] = comall[i][1] = comall[i][2] = 0.0;
160
/* ----------------------------------------------------------------------
161
lock methods: called by fix ave/time
162
these methods insure vector/array size is locked for Nfreq epoch
163
by passing lock info along to compute chunk/atom
164
------------------------------------------------------------------------- */
166
/* ----------------------------------------------------------------------
167
increment lock counter
168
------------------------------------------------------------------------- */
170
void ComputeCOMChunk::lock_enable()
175
/* ----------------------------------------------------------------------
176
decrement lock counter in compute chunk/atom, it if still exists
177
------------------------------------------------------------------------- */
179
void ComputeCOMChunk::lock_disable()
181
int icompute = modify->find_compute(idchunk);
183
cchunk = (ComputeChunkAtom *) modify->compute[icompute];
188
/* ----------------------------------------------------------------------
189
calculate and return # of chunks = length of vector/array
190
------------------------------------------------------------------------- */
192
int ComputeCOMChunk::lock_length()
194
nchunk = cchunk->setup_chunks();
198
/* ----------------------------------------------------------------------
199
set the lock from startstep to stopstep
200
------------------------------------------------------------------------- */
202
void ComputeCOMChunk::lock(Fix *fixptr, bigint startstep, bigint stopstep)
204
cchunk->lock(fixptr,startstep,stopstep);
207
/* ----------------------------------------------------------------------
209
------------------------------------------------------------------------- */
211
void ComputeCOMChunk::unlock(Fix *fixptr)
213
cchunk->unlock(fixptr);
216
/* ----------------------------------------------------------------------
217
free and reallocate per-chunk arrays
218
------------------------------------------------------------------------- */
220
void ComputeCOMChunk::allocate()
222
memory->destroy(massproc);
223
memory->destroy(masstotal);
224
memory->destroy(com);
225
memory->destroy(comall);
227
memory->create(massproc,maxchunk,"com/chunk:massproc");
228
memory->create(masstotal,maxchunk,"com/chunk:masstotal");
229
memory->create(com,maxchunk,3,"com/chunk:com");
230
memory->create(comall,maxchunk,3,"com/chunk:comall");
234
/* ----------------------------------------------------------------------
235
memory usage of local data
236
------------------------------------------------------------------------- */
238
double ComputeCOMChunk::memory_usage()
240
double bytes = (bigint) maxchunk * 2 * sizeof(double);
241
bytes += (bigint) maxchunk * 2*3 * sizeof(double);