1 // clang-format off
2 /* ----------------------------------------------------------------------
3    LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
4    https://www.lammps.org/, Sandia National Laboratories
5    Steve Plimpton, sjplimp@sandia.gov
6 
7    Copyright (2003) Sandia Corporation.  Under the terms of Contract
8    DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
9    certain rights in this software.  This software is distributed under
10    the GNU General Public License.
11 
12    See the README file in the top-level LAMMPS directory.
13 ------------------------------------------------------------------------- */
14 
15 #include "compute_gyration_chunk.h"
16 
17 #include <cmath>
18 #include <cstring>
19 #include "atom.h"
20 #include "update.h"
21 #include "modify.h"
22 #include "compute_chunk_atom.h"
23 #include "domain.h"
24 #include "memory.h"
25 #include "error.h"
26 
27 using namespace LAMMPS_NS;
28 
29 /* ---------------------------------------------------------------------- */
30 
ComputeGyrationChunk(LAMMPS * lmp,int narg,char ** arg)31 ComputeGyrationChunk::ComputeGyrationChunk(LAMMPS *lmp, int narg, char **arg) :
32   Compute(lmp, narg, arg),
33   idchunk(nullptr), massproc(nullptr), masstotal(nullptr), com(nullptr), comall(nullptr),
34   rg(nullptr), rgall(nullptr), rgt(nullptr), rgtall(nullptr)
35 {
36   if (narg < 4) error->all(FLERR,"Illegal compute gyration/chunk command");
37 
38   // ID of compute chunk/atom
39 
40   idchunk = utils::strdup(arg[3]);
41 
42   ComputeGyrationChunk::init();
43 
44   // optional args
45 
46   tensor = 0;
47   int iarg = 4;
48   while (iarg < narg) {
49     if (strcmp(arg[iarg],"tensor") == 0) {
50       tensor = 1;
51       iarg++;
52     } else error->all(FLERR,"Illegal compute gyration/chunk command");
53   }
54 
55   if (tensor) {
56     array_flag = 1;
57     size_array_cols = 6;
58     size_array_rows = 0;
59     size_array_rows_variable = 1;
60     extarray = 0;
61   } else {
62     vector_flag = 1;
63     size_vector = 0;
64     size_vector_variable = 1;
65     extvector = 0;
66   }
67 
68   // chunk-based data
69 
70   nchunk = 1;
71   maxchunk = 0;
72   allocate();
73 }
74 
75 /* ---------------------------------------------------------------------- */
76 
~ComputeGyrationChunk()77 ComputeGyrationChunk::~ComputeGyrationChunk()
78 {
79   delete [] idchunk;
80   memory->destroy(massproc);
81   memory->destroy(masstotal);
82   memory->destroy(com);
83   memory->destroy(comall);
84   memory->destroy(rg);
85   memory->destroy(rgall);
86   memory->destroy(rgt);
87   memory->destroy(rgtall);
88 }
89 
90 /* ---------------------------------------------------------------------- */
91 
init()92 void ComputeGyrationChunk::init()
93 {
94   int icompute = modify->find_compute(idchunk);
95   if (icompute < 0)
96     error->all(FLERR,"Chunk/atom compute does not exist for "
97                "compute gyration/chunk");
98   cchunk = (ComputeChunkAtom *) modify->compute[icompute];
99   if (strcmp(cchunk->style,"chunk/atom") != 0)
100     error->all(FLERR,"Compute gyration/chunk does not use chunk/atom compute");
101 }
102 
103 /* ---------------------------------------------------------------------- */
104 
compute_vector()105 void ComputeGyrationChunk::compute_vector()
106 {
107   int i,index;
108   double dx,dy,dz,massone;
109   double unwrap[3];
110 
111   invoked_array = update->ntimestep;
112 
113   com_chunk();
114   int *ichunk = cchunk->ichunk;
115 
116   for (i = 0; i < nchunk; i++) rg[i] = 0.0;
117 
118   // compute Rg for each chunk
119 
120   double **x = atom->x;
121   int *mask = atom->mask;
122   int *type = atom->type;
123   imageint *image = atom->image;
124   double *mass = atom->mass;
125   double *rmass = atom->rmass;
126   int nlocal = atom->nlocal;
127 
128   for (i = 0; i < nlocal; i++)
129     if (mask[i] & groupbit) {
130       index = ichunk[i]-1;
131       if (index < 0) continue;
132       domain->unmap(x[i],image[i],unwrap);
133       dx = unwrap[0] - comall[index][0];
134       dy = unwrap[1] - comall[index][1];
135       dz = unwrap[2] - comall[index][2];
136       if (rmass) massone = rmass[i];
137       else massone = mass[type[i]];
138       rg[index] += (dx*dx + dy*dy + dz*dz) * massone;
139     }
140 
141   MPI_Allreduce(rg,rgall,nchunk,MPI_DOUBLE,MPI_SUM,world);
142 
143   for (i = 0; i < nchunk; i++)
144     if (masstotal[i] > 0.0)
145       rgall[i] = sqrt(rgall[i]/masstotal[i]);
146 }
147 
148 /* ---------------------------------------------------------------------- */
149 
compute_array()150 void ComputeGyrationChunk::compute_array()
151 {
152   int i,j,index;
153   double dx,dy,dz,massone;
154   double unwrap[3];
155 
156   invoked_array = update->ntimestep;
157 
158   com_chunk();
159   int *ichunk = cchunk->ichunk;
160 
161   for (i = 0; i < nchunk; i++)
162     for (j = 0; j < 6; j++) rgt[i][j] = 0.0;
163 
164   double **x = atom->x;
165   int *mask = atom->mask;
166   int *type = atom->type;
167   imageint *image = atom->image;
168   double *mass = atom->mass;
169   double *rmass = atom->rmass;
170   int nlocal = atom->nlocal;
171 
172   for (i = 0; i < nlocal; i++)
173     if (mask[i] & groupbit) {
174       index = ichunk[i]-1;
175       if (index < 0) continue;
176       domain->unmap(x[i],image[i],unwrap);
177       dx = unwrap[0] - comall[index][0];
178       dy = unwrap[1] - comall[index][1];
179       dz = unwrap[2] - comall[index][2];
180       if (rmass) massone = rmass[i];
181       else massone = mass[type[i]];
182       rgt[index][0] += dx*dx * massone;
183       rgt[index][1] += dy*dy * massone;
184       rgt[index][2] += dz*dz * massone;
185       rgt[index][3] += dx*dy * massone;
186       rgt[index][4] += dx*dz * massone;
187       rgt[index][5] += dy*dz * massone;
188     }
189 
190   if (nchunk)
191     MPI_Allreduce(&rgt[0][0],&rgtall[0][0],nchunk*6,MPI_DOUBLE,MPI_SUM,world);
192 
193   for (i = 0; i < nchunk; i++) {
194     if (masstotal[i] > 0.0) {
195       for (j = 0; j < 6; j++)
196         rgtall[i][j] = rgtall[i][j]/masstotal[i];
197     }
198   }
199 }
200 
201 
202 /* ----------------------------------------------------------------------
203    calculate per-chunk COM, used by both scalar and tensor
204 ------------------------------------------------------------------------- */
205 
com_chunk()206 void ComputeGyrationChunk::com_chunk()
207 {
208   int index;
209   double massone;
210   double unwrap[3];
211 
212   // compute chunk/atom assigns atoms to chunk IDs
213   // extract ichunk index vector from compute
214   // ichunk = 1 to Nchunk for included atoms, 0 for excluded atoms
215 
216   nchunk = cchunk->setup_chunks();
217   cchunk->compute_ichunk();
218   int *ichunk = cchunk->ichunk;
219 
220   if (nchunk > maxchunk) allocate();
221   if (tensor) size_array_rows = nchunk;
222   else size_vector = nchunk;
223 
224   // zero local per-chunk values
225 
226   for (int i = 0; i < nchunk; i++) {
227     massproc[i] = 0.0;
228     com[i][0] = com[i][1] = com[i][2] = 0.0;
229   }
230 
231   // compute COM for each chunk
232 
233   double **x = atom->x;
234   int *mask = atom->mask;
235   int *type = atom->type;
236   imageint *image = atom->image;
237   double *mass = atom->mass;
238   double *rmass = atom->rmass;
239   int nlocal = atom->nlocal;
240 
241   for (int i = 0; i < nlocal; i++)
242     if (mask[i] & groupbit) {
243       index = ichunk[i]-1;
244       if (index < 0) continue;
245       if (rmass) massone = rmass[i];
246       else massone = mass[type[i]];
247       domain->unmap(x[i],image[i],unwrap);
248       massproc[index] += massone;
249       com[index][0] += unwrap[0] * massone;
250       com[index][1] += unwrap[1] * massone;
251       com[index][2] += unwrap[2] * massone;
252     }
253 
254   MPI_Allreduce(massproc,masstotal,nchunk,MPI_DOUBLE,MPI_SUM,world);
255   MPI_Allreduce(&com[0][0],&comall[0][0],3*nchunk,MPI_DOUBLE,MPI_SUM,world);
256 
257   for (int i = 0; i < nchunk; i++) {
258     if (masstotal[i] > 0.0) {
259       comall[i][0] /= masstotal[i];
260       comall[i][1] /= masstotal[i];
261       comall[i][2] /= masstotal[i];
262     }
263   }
264 }
265 
266 /* ----------------------------------------------------------------------
267    lock methods: called by fix ave/time
268    these methods insure vector/array size is locked for Nfreq epoch
269      by passing lock info along to compute chunk/atom
270 ------------------------------------------------------------------------- */
271 
272 /* ----------------------------------------------------------------------
273    increment lock counter
274 ------------------------------------------------------------------------- */
275 
lock_enable()276 void ComputeGyrationChunk::lock_enable()
277 {
278   cchunk->lockcount++;
279 }
280 
281 /* ----------------------------------------------------------------------
282    decrement lock counter in compute chunk/atom, it if still exists
283 ------------------------------------------------------------------------- */
284 
lock_disable()285 void ComputeGyrationChunk::lock_disable()
286 {
287   int icompute = modify->find_compute(idchunk);
288   if (icompute >= 0) {
289     cchunk = (ComputeChunkAtom *) modify->compute[icompute];
290     cchunk->lockcount--;
291   }
292 }
293 
294 /* ----------------------------------------------------------------------
295    calculate and return # of chunks = length of vector/array
296 ------------------------------------------------------------------------- */
297 
lock_length()298 int ComputeGyrationChunk::lock_length()
299 {
300   nchunk = cchunk->setup_chunks();
301   return nchunk;
302 }
303 
304 /* ----------------------------------------------------------------------
305    set the lock from startstep to stopstep
306 ------------------------------------------------------------------------- */
307 
lock(Fix * fixptr,bigint startstep,bigint stopstep)308 void ComputeGyrationChunk::lock(Fix *fixptr, bigint startstep, bigint stopstep)
309 {
310   cchunk->lock(fixptr,startstep,stopstep);
311 }
312 
313 /* ----------------------------------------------------------------------
314    unset the lock
315 ------------------------------------------------------------------------- */
316 
unlock(Fix * fixptr)317 void ComputeGyrationChunk::unlock(Fix *fixptr)
318 {
319   cchunk->unlock(fixptr);
320 }
321 
322 /* ----------------------------------------------------------------------
323    free and reallocate per-chunk arrays
324 ------------------------------------------------------------------------- */
325 
allocate()326 void ComputeGyrationChunk::allocate()
327 {
328   memory->destroy(massproc);
329   memory->destroy(masstotal);
330   memory->destroy(com);
331   memory->destroy(comall);
332   memory->destroy(rg);
333   memory->destroy(rgall);
334   memory->destroy(rgt);
335   memory->destroy(rgtall);
336   maxchunk = nchunk;
337   memory->create(massproc,maxchunk,"gyration/chunk:massproc");
338   memory->create(masstotal,maxchunk,"gyration/chunk:masstotal");
339   memory->create(com,maxchunk,3,"gyration/chunk:com");
340   memory->create(comall,maxchunk,3,"gyration/chunk:comall");
341   if (tensor) {
342     memory->create(rgt,maxchunk,6,"gyration/chunk:rgt");
343     memory->create(rgtall,maxchunk,6,"gyration/chunk:rgtall");
344     array = rgtall;
345   } else {
346     memory->create(rg,maxchunk,"gyration/chunk:rg");
347     memory->create(rgall,maxchunk,"gyration/chunk:rgall");
348     vector = rgall;
349   }
350 }
351 
352 /* ----------------------------------------------------------------------
353    memory usage of local data
354 ------------------------------------------------------------------------- */
355 
memory_usage()356 double ComputeGyrationChunk::memory_usage()
357 {
358   double bytes = (bigint) maxchunk * 2 * sizeof(double);
359   bytes += (double) maxchunk * 2*3 * sizeof(double);
360   if (tensor) bytes += (double) maxchunk * 2*6 * sizeof(double);
361   else bytes += (double) maxchunk * 2 * sizeof(double);
362   return bytes;
363 }
364