lammps-sys 0.6.0

Generates bindings to LAMMPS' C interface (with optional builds from source)
Documentation
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
430
/*
//@HEADER
// ************************************************************************
// 
//                        Kokkos v. 2.0
//              Copyright (2014) Sandia Corporation
// 
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Christian R. Trott (crtrott@sandia.gov)
// 
// ************************************************************************
//@HEADER
*/

#include <system.h>
#include <cstdio>
#include <Kokkos_Core.hpp>

#define SMALL 1.0e-6
#define FACTOR 0.999

/* BinningFunctor puts atoms into bins of the simulation box
 * Neighborlists are then created by checking only distances of atoms
 * in adjacent bins. That makes neighborlist construction a O(N) operation.
 */

struct BinningFunctor {
  typedef t_int_2d::execution_space execution_space;

  System s;

  int atoms_per_bin;

  BinningFunctor(System _s): s(_s) {
    atoms_per_bin = s.bins.dimension_1();
  }

  KOKKOS_INLINE_FUNCTION
  void operator() (const int &i) const
  {
    const int ibin = coord2bin(s.d_x(i, 0), s.d_x(i, 1), s.d_x(i, 2));

    const int ac = Kokkos::atomic_fetch_add(&s.bincount[ibin], 1);

    if(ac < atoms_per_bin) {
      s.bins(ibin, ac) = i;
    } else if(s.d_resize(0) < ac) {
      s.d_resize(0) = ac;
    }
  }

  KOKKOS_INLINE_FUNCTION
  int coord2bin(double x, double y, double z) const
  {
    int ix, iy, iz;

    if(x >= s.box.xprd)
      ix = (int)((x - s.box.xprd) * s.bininvx) + s.nbinx - s.mbinxlo;
    else if(x >= 0.0)
      ix = (int)(x * s.bininvx) - s.mbinxlo;
    else
      ix = (int)(x * s.bininvx) - s.mbinxlo - 1;

    if(y >= s.box.yprd)
      iy = (int)((y - s.box.yprd) * s.bininvy) + s.nbiny - s.mbinylo;
    else if(y >= 0.0)
      iy = (int)(y * s.bininvy) - s.mbinylo;
    else
      iy = (int)(y * s.bininvy) - s.mbinylo - 1;

    if(z >= s.box.zprd)
      iz = (int)((z - s.box.zprd) * s.bininvz) + s.nbinz - s.mbinzlo;
    else if(z >= 0.0)
      iz = (int)(z * s.bininvz) - s.mbinzlo;
    else
      iz = (int)(z * s.bininvz) - s.mbinzlo - 1;

    return (iz * s.mbiny * s.mbinx + iy * s.mbinx + ix + 1);
  }
};

/* Build the actual neighborlist*/

struct BuildFunctor {

  typedef t_int_2d::execution_space execution_space;

  System s;

  int maxneighs;
  BuildFunctor(System _s): s(_s) {
    maxneighs = s.neighbors.dimension_1();
  }

  KOKKOS_INLINE_FUNCTION
  void operator() (const int &i) const
  {
    int n = 0;

    const t_int_1d_const_um bincount_c = s.bincount;

    const double xtmp = s.d_x(i, 0);
    const double ytmp = s.d_x(i, 1);
    const double ztmp = s.d_x(i, 2);

    const int ibin = coord2bin(xtmp, ytmp, ztmp);

    // loop over all bins in neighborhood (includes ibin)
    for(int k = 0; k < s.nstencil; k++) {
      const int jbin = ibin + s.d_stencil[k];

      // get subview of jbin
      const t_int_1d_const_um loc_bin =
          Kokkos::subview(s.bins,jbin,Kokkos::ALL());

      if(ibin == jbin)
        for(int m = 0; m < bincount_c[jbin]; m++) {
          const int j = loc_bin[m];

          //for same bin as atom i skip j if i==j
          if (j == i) continue;

          const double delx = xtmp - s.d_x(j, 0);
          const double dely = ytmp - s.d_x(j, 1);
          const double delz = ztmp - s.d_x(j, 2);
          const double rsq = delx * delx + dely * dely + delz * delz;

          if(rsq <= s.neigh_cutsq && n<maxneighs) s.neighbors(i,n++) = j;
        }
      else {
        for(int m = 0; m < bincount_c[jbin]; m++) {
          const int j = loc_bin[m];

          const double delx = xtmp - s.d_x(j, 0);
          const double dely = ytmp - s.d_x(j, 1);
          const double delz = ztmp - s.d_x(j, 2);
          const double rsq = delx * delx + dely * dely + delz * delz;

          if(rsq <= s.neigh_cutsq && n<maxneighs) s.neighbors(i,n++) = j;
        }
      }
    }

    s.numneigh[i] = n;

    if(n >= maxneighs) {
      if(n >= s.d_resize(0)) s.d_resize(0) = n;
    }
  }

  KOKKOS_INLINE_FUNCTION
  int coord2bin(double x, double y, double z) const
  {
    int ix, iy, iz;

    if(x >= s.box.xprd)
      ix = (int)((x - s.box.xprd) * s.bininvx) + s.nbinx - s.mbinxlo;
    else if(x >= 0.0)
      ix = (int)(x * s.bininvx) - s.mbinxlo;
    else
      ix = (int)(x * s.bininvx) - s.mbinxlo - 1;

    if(y >= s.box.yprd)
      iy = (int)((y - s.box.yprd) * s.bininvy) + s.nbiny - s.mbinylo;
    else if(y >= 0.0)
      iy = (int)(y * s.bininvy) - s.mbinylo;
    else
      iy = (int)(y * s.bininvy) - s.mbinylo - 1;

    if(z >= s.box.zprd)
      iz = (int)((z - s.box.zprd) * s.bininvz) + s.nbinz - s.mbinzlo;
    else if(z >= 0.0)
      iz = (int)(z * s.bininvz) - s.mbinzlo;
    else
      iz = (int)(z * s.bininvz) - s.mbinzlo - 1;

    return (iz * s.mbiny * s.mbinx + iy * s.mbinx + ix + 1);
  }
};

/* Reset an array to zero */

struct MemsetZeroFunctor {
  typedef t_x_array::execution_space  execution_space ;
  void* ptr;
  KOKKOS_INLINE_FUNCTION void operator()(const int i) const {
    ((int*)ptr)[i] = 0;
  }
};

/* Calculate distance of two bins */

double bindist(System &s, int i, int j, int k)
{
  double delx, dely, delz;

  if(i > 0)
    delx = (i - 1) * s.binsizex;
  else if(i == 0)
    delx = 0.0;
  else
    delx = (i + 1) * s.binsizex;

  if(j > 0)
    dely = (j - 1) * s.binsizey;
  else if(j == 0)
    dely = 0.0;
  else
    dely = (j + 1) * s.binsizey;

  if(k > 0)
    delz = (k - 1) * s.binsizez;
  else if(k == 0)
    delz = 0.0;
  else
    delz = (k + 1) * s.binsizez;

  return (delx * delx + dely * dely + delz * delz);
}

/* Setup the neighborlist construction
 * Determine binsizes, a stencil for defining adjacency, etc.
 */

void neigh_setup(System &s) {

  s.neigh_cutsq = s.neigh_cut * s.neigh_cut;

  /*
  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;
  }
  */

  s.binsizex = s.box.xprd / s.nbinx;
  s.binsizey = s.box.yprd / s.nbiny;
  s.binsizez = s.box.zprd / s.nbinz;
  s.bininvx = 1.0 / s.binsizex;
  s.bininvy = 1.0 / s.binsizey;
  s.bininvz = 1.0 / s.binsizez;

  double coord = s.box.xlo - s.neigh_cut - SMALL * s.box.xprd;
  s.mbinxlo = static_cast<int>(coord * s.bininvx);

  if(coord < 0.0) s.mbinxlo = s.mbinxlo - 1;

  coord = s.box.xhi + s.neigh_cut + SMALL * s.box.xprd;
  int mbinxhi = static_cast<int>(coord * s.bininvx);

  coord = s.box.ylo - s.neigh_cut - SMALL * s.box.yprd;
  s.mbinylo = static_cast<int>(coord * s.bininvy);

  if(coord < 0.0) s.mbinylo = s.mbinylo - 1;

  coord = s.box.yhi + s.neigh_cut + SMALL * s.box.yprd;
  int mbinyhi = static_cast<int>(coord * s.bininvy);

  coord = s.box.zlo - s.neigh_cut - SMALL * s.box.zprd;
  s.mbinzlo = static_cast<int>(coord * s.bininvz);

  if(coord < 0.0) s.mbinzlo = s.mbinzlo - 1;

  coord = s.box.zhi + s.neigh_cut + SMALL * s.box.zprd;
  int mbinzhi = static_cast<int>(coord * s.bininvz);

  /* extend bins by 1 in each direction to insure stencil coverage */

  s.mbinxlo = s.mbinxlo - 1;
  mbinxhi = mbinxhi + 1;
  s.mbinx = mbinxhi - s.mbinxlo + 1;

  s.mbinylo = s.mbinylo - 1;
  mbinyhi = mbinyhi + 1;
  s.mbiny = mbinyhi - s.mbinylo + 1;

  s.mbinzlo = s.mbinzlo - 1;
  mbinzhi = mbinzhi + 1;
  s.mbinz = mbinzhi - s.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
  */

  int nextx = static_cast<int>(s.neigh_cut * s.bininvx);

  if(nextx * s.binsizex < FACTOR * s.neigh_cut) nextx++;

  int nexty = static_cast<int>(s.neigh_cut * s.bininvy);

  if(nexty * s.binsizey < FACTOR * s.neigh_cut) nexty++;

  int nextz = static_cast<int>(s.neigh_cut * s.bininvz);

  if(nextz * s.binsizez < FACTOR * s.neigh_cut) nextz++;

  int nmax = (2 * nextz + 1) * (2 * nexty + 1) * (2 * nextx + 1);
  s.d_stencil = t_int_1d("stencil", nmax);
  s.h_stencil = Kokkos::create_mirror_view(s.d_stencil);
  s.nstencil = 0;
  int kstart = -nextz;

  for(int k = kstart; k <= nextz; k++) {
    for(int j = -nexty; j <= nexty; j++) {
      for(int i = -nextx; i <= nextx; i++) {
        if(bindist(s,i, j, k) < s.neigh_cutsq) {
          s.h_stencil(s.nstencil++) = k * s.mbiny * s.mbinx + j * s.mbinx + i;
        }
      }
    }
  }

  /* Allocate neighbor arrays */

  Kokkos::deep_copy(s.d_stencil, s.h_stencil);
  s.mbins = s.mbinx * s.mbiny * s.mbinz;
  s.bincount = t_int_1d("bincount", s.mbins);
  s.bins = t_int_2d("bins", s.mbins, 8);

  s.neighbors = t_neighbors("neighbors",s.natoms,80);
  s.numneigh = t_int_1d("numneigh",s.natoms);
  s.d_resize = t_int_scalar("resize");
  s.h_resize = Kokkos::create_mirror_view(s.d_resize);
}


/* Build the neighborlist
 * This is a try and rerun algorithm for handling the case where the bins array
 * and the neighbors array are not big enough. So if one is too small, it will
 * reallocate and rerun the binnind algorithm or the neighborlist construction.
 */

void neigh_build(System &s) {

  /* Binning of atoms */

  s.h_resize(0) = 1;

  while(s.h_resize(0) > 0) {
    s.h_resize(0) = 0;
    Kokkos::deep_copy(s.d_resize, s.h_resize);

    MemsetZeroFunctor f_zero;
    f_zero.ptr = (void*) s.bincount.ptr_on_device();
    Kokkos::parallel_for(s.mbins, f_zero);
    execution_space().fence();

    BinningFunctor f(s);
    Kokkos::parallel_for(s.natoms, f);
    execution_space().fence();

    /* Check if bins was large enough, if nor reallocated and rerun */

    deep_copy(s.h_resize, s.d_resize);

    if(s.h_resize(0)) {
      int atoms_per_bin = s.h_resize(0)+2;
      s.bins = t_int_2d("bins", s.mbins, atoms_per_bin);
    }
  }

  /* Neighborlist construction */

  s.h_resize(0) = 1;

  while(s.h_resize(0)) {
    s.h_resize(0) = 0;

    Kokkos::deep_copy(s.d_resize, s.h_resize);

    BuildFunctor f(s);
    Kokkos::parallel_for(s.nlocal, f);

    execution_space().fence();

    /* Check if neighbors was large enough, if nor reallocated and rerun */

    deep_copy(s.h_resize, s.d_resize);

    if(s.h_resize(0)) {
      int maxneighs = s.h_resize(0) * 1.2;
      s.neighbors = t_neighbors("neighbors", s.natoms, maxneighs);
    }
  }
}