-
Notifications
You must be signed in to change notification settings - Fork 0
/
neighbor.cpp
429 lines (353 loc) · 11 KB
/
neighbor.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
429
/* ----------------------------------------------------------------------
miniMD is a simple, parallel molecular dynamics (MD) code. miniMD is
an MD microapplication in the Mantevo project at Sandia National
Laboratories ( http://software.sandia.gov/mantevo/ ). The primary
authors of miniMD are Steve Plimpton and Paul Crozier
Copyright (2008) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This library is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later
version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. See also: http://www.gnu.org/licenses/lgpl.txt .
For questions, contact Paul S. Crozier ([email protected]).
Please read the accompanying README and LICENSE files.
---------------------------------------------------------------------- */
#include "stdio.h"
#include "stdlib.h"
#include "neighbor.h"
#if defined(_OPENMP)
#include <omp.h>
#endif
#define PAGESIZE 10000
#define ONEATOM 1000
#define PAGEDELTA 1
#define FACTOR 0.999
#define SMALL 1.0e-6
Neighbor::Neighbor()
{
ncalls = 0;
max_totalneigh = 0;
numneigh = NULL;
firstneigh = NULL;
nmax = 0;
binhead = NULL;
bins = NULL;
stencil = NULL;
maxpage = 1;
pages = (int **) malloc(maxpage*sizeof(int *));
pages[0] = (int *) malloc(PAGESIZE*sizeof(int));
}
Neighbor::~Neighbor()
{
if (numneigh) free(numneigh);
if (firstneigh) free(firstneigh);
for (int i = 0; i < maxpage; i++) free(pages[i]);
if (pages) free(pages);
if (binhead) free(binhead);
if (bins) free(bins);
}
/* binned neighbor list construction with full Newton's 3rd law
every pair stored exactly once by some processor
each owned atom i checks its own bin and other bins in Newton stencil */
void Neighbor::build(Atom &atom)
{
ncalls++;
const int nthreads = atom.nthreads;
const int nlocal = atom.nlocal;
const int nall = atom.nlocal + atom.nghost;
/* extend atom arrays if necessary */
if (nall > nmax) {
nmax = nall;
if (numneigh) free(numneigh);
if (firstneigh) free(firstneigh);
if (bins) free(bins);
numneigh = (int *) malloc(nmax*sizeof(int));
firstneigh = (int **) malloc(nmax*sizeof(int *));
bins = (int *) malloc(nmax*sizeof(int));
}
/* bin local & ghost atoms */
binatoms(atom);
if (nthreads > maxpage) {
const int oldpage = maxpage;
maxpage = nthreads;
pages = (int **) realloc(pages,maxpage*sizeof(int *));
for (int m = oldpage; m < maxpage; m++)
pages[m] = (int *) malloc(PAGESIZE*sizeof(int));
}
#if defined(_OPENMP)
#pragma omp parallel default(none) shared(atom)
#endif
{
#if defined(_OPENMP)
const int tid = omp_get_thread_num();
const int idelta = 1 + nlocal/nthreads;
const int ifrom = tid*idelta;
const int imax = ifrom + idelta;
const int ito = (imax > nlocal) ? nlocal : imax;
#else
const int tid = 0;
const int ifrom = 0;
const int ito = nlocal;
#endif
int i,j,k,m,n,ibin;
double xtmp,ytmp,ztmp,delx,dely,delz,rsq;
int *neighptr;
double *x = atom.x;
double *y = atom.y;
double *z = atom.z;
int npnt = 0;
int npage = tid;
/* loop over each atom, storing neighbors */
for (i = ifrom; i < ito; i++) {
/* if necessary, goto next page and add pages */
#if defined(_OPENMP)
#pragma omp critical
#endif
if (PAGESIZE - npnt < ONEATOM) {
npnt = 0;
npage += nthreads;
if (npage >= maxpage) {
const int oldpage = maxpage;
maxpage += nthreads;
pages = (int **) realloc(pages,maxpage*sizeof(int *));
for (m = oldpage; m < maxpage; m++)
pages[m] = (int *) malloc(PAGESIZE*sizeof(int));
}
}
neighptr = &pages[npage][npnt];
n = 0;
xtmp = x[i];
ytmp = y[i];
ztmp = z[i];
/* loop over rest of atoms in i's bin, ghosts are at end of linked list
if j is owned atom, store it, since j is beyond i in linked list
if j is ghost, only store if j coords are "above and to the right" of i
*/
j = bins[i];
while (j >= 0) {
if (j >= nlocal) {
if ((z[j] < ztmp) || (z[j] == ztmp && y[j] < ytmp) ||
(z[j] == ztmp && y[j] == ytmp && x[j] < xtmp)) {
j = bins[j];
continue;
}
}
delx = xtmp - x[j];
dely = ytmp - y[j];
delz = ztmp - z[j];
rsq = delx*delx + dely*dely + delz*delz;
if (rsq <= cutneighsq) neighptr[n++] = j;
j = bins[j];
}
/* loop over all atoms in other bins in stencil, store every pair */
ibin = coord2bin(xtmp,ytmp,ztmp);
for (k = 0; k < nstencil; k++) {
j = binhead[ibin+stencil[k]];
while (j >= 0) {
delx = xtmp - x[j];
dely = ytmp - y[j];
delz = ztmp - z[j];
rsq = delx*delx + dely*dely + delz*delz;
if (rsq <= cutneighsq) neighptr[n++] = j;
j = bins[j];
}
}
firstneigh[i] = neighptr;
numneigh[i] = n;
npnt += n;
}
}
}
/* bin owned and ghost atoms */
void Neighbor::binatoms(Atom &atom)
{
int i,ibin,nlocal,nall;
double *x,*y,*z;
nlocal = atom.nlocal;
nall = atom.nlocal + atom.nghost;
x = atom.x;
y = atom.y;
z = atom.z;
xprd = atom.box.xprd;
yprd = atom.box.yprd;
zprd = atom.box.zprd;
for (i = 0; i < mbins; i++) binhead[i] = -1;
/* bin ghost atoms 1st, so will be at end of linked list */
for (i = nlocal; i < nall; i++) {
ibin = coord2bin(x[i],y[i],z[i]);
bins[i] = binhead[ibin];
binhead[ibin] = i;
}
/* bin owned atoms */
for (i = 0; i < nlocal; i++) {
ibin = coord2bin(x[i],y[i],z[i]);
bins[i] = binhead[ibin];
binhead[ibin] = i;
}
}
/* convert xyz atom coords into local bin #
take special care to insure ghost atoms with
coord >= prd or coord < 0.0 are put in correct bins */
int Neighbor::coord2bin(double x, double y, double z)
{
int ix,iy,iz;
if (x >= xprd)
ix = (int) ((x-xprd)*bininvx) + nbinx - mbinxlo;
else if (x >= 0.0)
ix = (int) (x*bininvx) - mbinxlo;
else
ix = (int) (x*bininvx) - mbinxlo - 1;
if (y >= yprd)
iy = (int) ((y-yprd)*bininvy) + nbiny - mbinylo;
else if (y >= 0.0)
iy = (int) (y*bininvy) - mbinylo;
else
iy = (int) (y*bininvy) - mbinylo - 1;
if (z >= zprd)
iz = (int) ((z-zprd)*bininvz) + nbinz - mbinzlo;
else if (z >= 0.0)
iz = (int) (z*bininvz) - mbinzlo;
else
iz = (int) (z*bininvz) - mbinzlo - 1;
return (iz*mbiny*mbinx + iy*mbinx + ix + 1);
}
/*
setup neighbor binning parameters
bin numbering is global: 0 = 0.0 to binsize
1 = binsize to 2*binsize
nbin-1 = prd-binsize to binsize
nbin = prd to prd+binsize
-1 = -binsize to 0.0
coord = lowest and highest values of ghost atom coords I will have
add in "small" for round-off safety
mbinlo = lowest global bin any of my ghost atoms could fall into
mbinhi = highest global bin any of my ghost atoms could fall into
mbin = number of bins I need in a dimension
stencil() = bin offsets in 1-d sense for stencil of surrounding bins
*/
int Neighbor::setup(Atom &atom)
{
int i,j,k,nmax;
double coord;
int mbinxhi,mbinyhi,mbinzhi;
int nextx,nexty,nextz;
cutneighsq = cutneigh*cutneigh;
xprd = atom.box.xprd;
yprd = atom.box.yprd;
zprd = atom.box.zprd;
/*
c bins must evenly divide into box size,
c becoming larger than cutneigh if necessary
c binsize = 1/2 of cutoff is near optimal
if (flag == 0) {
nbinx = 2.0 * xprd / cutneigh;
nbiny = 2.0 * yprd / cutneigh;
nbinz = 2.0 * zprd / cutneigh;
if (nbinx == 0) nbinx = 1;
if (nbiny == 0) nbiny = 1;
if (nbinz == 0) nbinz = 1;
}
*/
binsizex = xprd/nbinx;
binsizey = yprd/nbiny;
binsizez = zprd/nbinz;
bininvx = 1.0 / binsizex;
bininvy = 1.0 / binsizey;
bininvz = 1.0 / binsizez;
coord = atom.box.xlo - cutneigh - SMALL*xprd;
mbinxlo = static_cast<int>(coord*bininvx);
if (coord < 0.0) mbinxlo = mbinxlo - 1;
coord = atom.box.xhi + cutneigh + SMALL*xprd;
mbinxhi = static_cast<int>(coord*bininvx);
coord = atom.box.ylo - cutneigh - SMALL*yprd;
mbinylo = static_cast<int>(coord*bininvy);
if (coord < 0.0) mbinylo = mbinylo - 1;
coord = atom.box.yhi + cutneigh + SMALL*yprd;
mbinyhi = static_cast<int>(coord*bininvy);
coord = atom.box.zlo - cutneigh - SMALL*zprd;
mbinzlo = static_cast<int>(coord*bininvz);
if (coord < 0.0) mbinzlo = mbinzlo - 1;
coord = atom.box.zhi + cutneigh + SMALL*zprd;
mbinzhi = static_cast<int>(coord*bininvz);
/* extend bins by 1 in each direction to insure stencil coverage */
mbinxlo = mbinxlo - 1;
mbinxhi = mbinxhi + 1;
mbinx = mbinxhi - mbinxlo + 1;
mbinylo = mbinylo - 1;
mbinyhi = mbinyhi + 1;
mbiny = mbinyhi - mbinylo + 1;
mbinzlo = mbinzlo - 1;
mbinzhi = mbinzhi + 1;
mbinz = mbinzhi - mbinzlo + 1;
/*
compute bin stencil of all bins whose closest corner to central bin
is within neighbor cutoff
for partial Newton (newton = 0),
stencil is all surrounding bins including self
for full Newton (newton = 1),
stencil is bins to the "upper right" of central bin, does NOT include self
next(xyz) = how far the stencil could possibly extend
factor < 1.0 for special case of LJ benchmark so code will create
correct-size stencil when there are 3 bins for every 5 lattice spacings
*/
nextx = static_cast<int>(cutneigh*bininvx);
if (nextx*binsizex < FACTOR*cutneigh) nextx++;
nexty = static_cast<int>(cutneigh*bininvy);
if (nexty*binsizey < FACTOR*cutneigh) nexty++;
nextz = static_cast<int>(cutneigh*bininvz);
if (nextz*binsizez < FACTOR*cutneigh) nextz++;
nmax = (nextz+1) * (2*nexty+1) * (2*nextx+1);
if (stencil) free(stencil);
stencil = (int *) malloc(nmax*sizeof(int));
nstencil = 0;
for (k = 0; k <= nextz; k++) {
for (j = -nexty; j <= nexty; j++) {
for (i = -nextx; i <= nextx; i++) {
if (k > 0 || j > 0 || (j == 0 && i > 0)) {
if (bindist(i,j,k) < cutneighsq) {
stencil[nstencil] = k*mbiny*mbinx + j*mbinx + i;
nstencil++;
}
}
}
}
}
if (binhead) free(binhead);
mbins = mbinx*mbiny*mbinz;
binhead = (int *) malloc(mbins*sizeof(int));
return 0;
}
/* compute closest distance between central bin (0,0,0) and bin (i,j,k) */
double Neighbor::bindist(int i, int j, int k)
{
double delx,dely,delz;
if (i > 0)
delx = (i-1)*binsizex;
else if (i == 0)
delx = 0.0;
else
delx = (i+1)*binsizex;
if (j > 0)
dely = (j-1)*binsizey;
else if (j == 0)
dely = 0.0;
else
dely = (j+1)*binsizey;
if (k > 0)
delz = (k-1)*binsizez;
else if (k == 0)
delz = 0.0;
else
delz = (k+1)*binsizez;
return (delx*delx + dely*dely + delz*delz);
}