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_torque_chunk.h"
16
17 #include <cstring>
18 #include "atom.h"
19 #include "update.h"
20 #include "modify.h"
21 #include "compute_chunk_atom.h"
22 #include "domain.h"
23 #include "memory.h"
24 #include "error.h"
25
26 using namespace LAMMPS_NS;
27
28 /* ---------------------------------------------------------------------- */
29
ComputeTorqueChunk(LAMMPS * lmp,int narg,char ** arg)30 ComputeTorqueChunk::ComputeTorqueChunk(LAMMPS *lmp, int narg, char **arg) :
31 Compute(lmp, narg, arg),
32 idchunk(nullptr), massproc(nullptr), masstotal(nullptr), com(nullptr), comall(nullptr), torque(nullptr), torqueall(nullptr)
33 {
34 if (narg != 4) error->all(FLERR,"Illegal compute torque/chunk command");
35
36 array_flag = 1;
37 size_array_cols = 3;
38 size_array_rows = 0;
39 size_array_rows_variable = 1;
40 extarray = 0;
41
42 // ID of compute chunk/atom
43
44 idchunk = utils::strdup(arg[3]);
45
46 ComputeTorqueChunk::init();
47
48 // chunk-based data
49
50 nchunk = 1;
51 maxchunk = 0;
52 allocate();
53 }
54
55 /* ---------------------------------------------------------------------- */
56
~ComputeTorqueChunk()57 ComputeTorqueChunk::~ComputeTorqueChunk()
58 {
59 delete [] idchunk;
60 memory->destroy(massproc);
61 memory->destroy(masstotal);
62 memory->destroy(com);
63 memory->destroy(comall);
64 memory->destroy(torque);
65 memory->destroy(torqueall);
66 }
67
68 /* ---------------------------------------------------------------------- */
69
init()70 void ComputeTorqueChunk::init()
71 {
72 int icompute = modify->find_compute(idchunk);
73 if (icompute < 0)
74 error->all(FLERR,"Chunk/atom compute does not exist for "
75 "compute torque/chunk");
76 cchunk = (ComputeChunkAtom *) modify->compute[icompute];
77 if (strcmp(cchunk->style,"chunk/atom") != 0)
78 error->all(FLERR,"Compute torque/chunk does not use chunk/atom compute");
79 }
80
81 /* ---------------------------------------------------------------------- */
82
compute_array()83 void ComputeTorqueChunk::compute_array()
84 {
85 int i,index;
86 double dx,dy,dz,massone;
87 double unwrap[3];
88
89 invoked_array = update->ntimestep;
90
91 // compute chunk/atom assigns atoms to chunk IDs
92 // extract ichunk index vector from compute
93 // ichunk = 1 to Nchunk for included atoms, 0 for excluded atoms
94
95 nchunk = cchunk->setup_chunks();
96 cchunk->compute_ichunk();
97 int *ichunk = cchunk->ichunk;
98
99 if (nchunk > maxchunk) allocate();
100 size_array_rows = nchunk;
101
102 // zero local per-chunk values
103
104 for (i = 0; i < nchunk; i++) {
105 massproc[i] = 0.0;
106 com[i][0] = com[i][1] = com[i][2] = 0.0;
107 torque[i][0] = torque[i][1] = torque[i][2] = 0.0;
108 }
109
110 // compute COM for each chunk
111
112 double **x = atom->x;
113 int *mask = atom->mask;
114 int *type = atom->type;
115 imageint *image = atom->image;
116 double *mass = atom->mass;
117 double *rmass = atom->rmass;
118 int nlocal = atom->nlocal;
119
120 for (i = 0; i < nlocal; i++)
121 if (mask[i] & groupbit) {
122 index = ichunk[i]-1;
123 if (index < 0) continue;
124 if (rmass) massone = rmass[i];
125 else massone = mass[type[i]];
126 domain->unmap(x[i],image[i],unwrap);
127 massproc[index] += massone;
128 com[index][0] += unwrap[0] * massone;
129 com[index][1] += unwrap[1] * massone;
130 com[index][2] += unwrap[2] * massone;
131 }
132
133 MPI_Allreduce(massproc,masstotal,nchunk,MPI_DOUBLE,MPI_SUM,world);
134 MPI_Allreduce(&com[0][0],&comall[0][0],3*nchunk,MPI_DOUBLE,MPI_SUM,world);
135
136 for (i = 0; i < nchunk; i++) {
137 if (masstotal[i] > 0.0) {
138 comall[i][0] /= masstotal[i];
139 comall[i][1] /= masstotal[i];
140 comall[i][2] /= masstotal[i];
141 }
142 }
143
144 // compute torque on each chunk
145
146 double **f = atom->f;
147
148 for (i = 0; i < nlocal; i++)
149 if (mask[i] & groupbit) {
150 index = ichunk[i]-1;
151 if (index < 0) continue;
152 domain->unmap(x[i],image[i],unwrap);
153 dx = unwrap[0] - comall[index][0];
154 dy = unwrap[1] - comall[index][1];
155 dz = unwrap[2] - comall[index][2];
156 torque[index][0] += dy*f[i][2] - dz*f[i][1];
157 torque[index][1] += dz*f[i][0] - dx*f[i][2];
158 torque[index][2] += dx*f[i][1] - dy*f[i][0];
159 }
160
161 MPI_Allreduce(&torque[0][0],&torqueall[0][0],3*nchunk,
162 MPI_DOUBLE,MPI_SUM,world);
163 }
164
165 /* ----------------------------------------------------------------------
166 lock methods: called by fix ave/time
167 these methods insure vector/array size is locked for Nfreq epoch
168 by passing lock info along to compute chunk/atom
169 ------------------------------------------------------------------------- */
170
171 /* ----------------------------------------------------------------------
172 increment lock counter
173 ------------------------------------------------------------------------- */
174
lock_enable()175 void ComputeTorqueChunk::lock_enable()
176 {
177 cchunk->lockcount++;
178 }
179
180 /* ----------------------------------------------------------------------
181 decrement lock counter in compute chunk/atom, it if still exists
182 ------------------------------------------------------------------------- */
183
lock_disable()184 void ComputeTorqueChunk::lock_disable()
185 {
186 int icompute = modify->find_compute(idchunk);
187 if (icompute >= 0) {
188 cchunk = (ComputeChunkAtom *) modify->compute[icompute];
189 cchunk->lockcount--;
190 }
191 }
192
193 /* ----------------------------------------------------------------------
194 calculate and return # of chunks = length of vector/array
195 ------------------------------------------------------------------------- */
196
lock_length()197 int ComputeTorqueChunk::lock_length()
198 {
199 nchunk = cchunk->setup_chunks();
200 return nchunk;
201 }
202
203 /* ----------------------------------------------------------------------
204 set the lock from startstep to stopstep
205 ------------------------------------------------------------------------- */
206
lock(Fix * fixptr,bigint startstep,bigint stopstep)207 void ComputeTorqueChunk::lock(Fix *fixptr, bigint startstep, bigint stopstep)
208 {
209 cchunk->lock(fixptr,startstep,stopstep);
210 }
211
212 /* ----------------------------------------------------------------------
213 unset the lock
214 ------------------------------------------------------------------------- */
215
unlock(Fix * fixptr)216 void ComputeTorqueChunk::unlock(Fix *fixptr)
217 {
218 cchunk->unlock(fixptr);
219 }
220
221 /* ----------------------------------------------------------------------
222 free and reallocate per-chunk arrays
223 ------------------------------------------------------------------------- */
224
allocate()225 void ComputeTorqueChunk::allocate()
226 {
227 memory->destroy(massproc);
228 memory->destroy(masstotal);
229 memory->destroy(com);
230 memory->destroy(comall);
231 memory->destroy(torque);
232 memory->destroy(torqueall);
233 maxchunk = nchunk;
234 memory->create(massproc,maxchunk,"torque/chunk:massproc");
235 memory->create(masstotal,maxchunk,"torque/chunk:masstotal");
236 memory->create(com,maxchunk,3,"torque/chunk:com");
237 memory->create(comall,maxchunk,3,"torque/chunk:comall");
238 memory->create(torque,maxchunk,3,"torque/chunk:torque");
239 memory->create(torqueall,maxchunk,3,"torque/chunk:torqueall");
240 array = torqueall;
241 }
242
243 /* ----------------------------------------------------------------------
244 memory usage of local data
245 ------------------------------------------------------------------------- */
246
memory_usage()247 double ComputeTorqueChunk::memory_usage()
248 {
249 double bytes = (bigint) maxchunk * 2 * sizeof(double);
250 bytes += (double) maxchunk * 2*3 * sizeof(double);
251 bytes += (double) maxchunk * 2*3 * sizeof(double);
252 return bytes;
253 }
254