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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* ----------------------------------------------------------------------
   LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
   http://lammps.sandia.gov, Sandia National Laboratories
   Steve Plimpton, sjplimp@sandia.gov

   Copyright (2003) Sandia Corporation.  Under the terms of Contract
   DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
   certain rights in this software.  This software is distributed under
   the GNU General Public License.

   See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */

/* ----------------------------------------------------------------------
   Contributing author:  G. Ziegenhain, gerolf@ziegenhain.com
                         Copyright (C) 2007
   Updated algorithm by: Brian Barnes, brian.c.barnes11.civ@mail.mil
------------------------------------------------------------------------- */

#include <cmath>
#include <cstring>
#include "compute_ackland_atom.h"
#include "atom.h"
#include "update.h"
#include "modify.h"
#include "neighbor.h"
#include "neigh_list.h"
#include "neigh_request.h"
#include "force.h"
#include "pair.h"
#include "comm.h"
#include "memory.h"
#include "error.h"

using namespace LAMMPS_NS;

enum{UNKNOWN,BCC,FCC,HCP,ICO};

/* ---------------------------------------------------------------------- */

ComputeAcklandAtom::ComputeAcklandAtom(LAMMPS *lmp, int narg, char **arg) :
  Compute(lmp, narg, arg)
{
  if ((narg < 3) || (narg > 5))
    error->all(FLERR,"Illegal compute ackland/atom command");

  peratom_flag = 1;
  size_peratom_cols = 0;

  nmax = 0;
  structure = NULL;
  maxneigh = 0;
  legacy = 0;
  distsq = NULL;
  nearest = NULL;
  nearest_n0 = NULL;
  nearest_n1 = NULL;

  int iarg = 3;
  while (narg > iarg) {
    if (strcmp("legacy",arg[iarg]) == 0) {
      ++iarg;
      if (iarg >= narg)
        error->all(FLERR,"Invalid compute ackland/atom command");
      if (strcmp("yes",arg[iarg]) == 0)
        legacy = 1;
      else if (strcmp("no",arg[iarg]) == 0)
        legacy = 0;
      else error->all(FLERR,"Invalid compute ackland/atom command");
    }
    ++iarg;
  }
}

/* ---------------------------------------------------------------------- */

ComputeAcklandAtom::~ComputeAcklandAtom()
{
  memory->destroy(structure);
  memory->destroy(distsq);
  memory->destroy(nearest);
  memory->destroy(nearest_n0);
  memory->destroy(nearest_n1);
}

/* ---------------------------------------------------------------------- */

void ComputeAcklandAtom::init()
{
  // need an occasional full neighbor list

  int irequest = neighbor->request(this,instance_me);
  neighbor->requests[irequest]->pair = 0;
  neighbor->requests[irequest]->compute = 1;
  neighbor->requests[irequest]->half = 0;
  neighbor->requests[irequest]->full = 1;
  neighbor->requests[irequest]->occasional = 1;

  int count = 0;
  for (int i = 0; i < modify->ncompute; i++)
    if (strcmp(modify->compute[i]->style,"ackland/atom") == 0) count++;
  if (count > 1 && comm->me == 0)
    error->warning(FLERR,"More than one compute ackland/atom");
}

/* ---------------------------------------------------------------------- */

void ComputeAcklandAtom::init_list(int /*id*/, NeighList *ptr)
{
  list = ptr;
}

/* ---------------------------------------------------------------------- */

void ComputeAcklandAtom::compute_peratom()
{
  int i,j,ii,jj,k,n,inum,jnum;
  double xtmp,ytmp,ztmp,delx,dely,delz,rsq;
  int *ilist,*jlist,*numneigh,**firstneigh;
  int chi[8];

  invoked_peratom = update->ntimestep;

  // grow structure array if necessary

  if (atom->nmax > nmax) {
    memory->destroy(structure);
    nmax = atom->nmax;
    memory->create(structure,nmax,"compute/ackland/atom:ackland");
    vector_atom = structure;
  }

  // invoke full neighbor list (will copy or build if necessary)

  neighbor->build_one(list);

  inum = list->inum;
  ilist = list->ilist;
  numneigh = list->numneigh;
  firstneigh = list->firstneigh;

  // compute structure parameter for each atom in group
  // use full neighbor list

  double **x = atom->x;
  int *mask = atom->mask;
  double cutsq = force->pair->cutforce * force->pair->cutforce;

  for (ii = 0; ii < inum; ii++) {
    i = ilist[ii];
    if (mask[i] & groupbit) {
      xtmp = x[i][0];
      ytmp = x[i][1];
      ztmp = x[i][2];
      jlist = firstneigh[i];
      jnum = numneigh[i];

      // ensure distsq and nearest arrays are long enough

      if (jnum > maxneigh) {
        memory->destroy(distsq);
        memory->destroy(nearest);
        memory->destroy(nearest_n0);
        memory->destroy(nearest_n1);
        maxneigh = jnum;
        memory->create(distsq,maxneigh,"compute/ackland/atom:distsq");
        memory->create(nearest,maxneigh,"compute/ackland/atom:nearest");
        memory->create(nearest_n0,maxneigh,"compute/ackland/atom:nearest_n0");
        memory->create(nearest_n1,maxneigh,"compute/ackland/atom:nearest_n1");
      }

      // loop over list of all neighbors within force cutoff
      // distsq[] = distance sq to each
      // nearest[] = atom indices of neighbors

      n = 0;
      for (jj = 0; jj < jnum; jj++) {
        j = jlist[jj];
        j &= NEIGHMASK;

        delx = xtmp - x[j][0];
        dely = ytmp - x[j][1];
        delz = ztmp - x[j][2];
        rsq = delx*delx + dely*dely + delz*delz;
        if (rsq < cutsq) {
          distsq[n] = rsq;
          nearest[n++] = j;
        }
      }

      // Select 6 nearest neighbors

      select2(6,n,distsq,nearest);

      // Mean squared separation

      double r0_sq = 0.;
      for (j = 0; j < 6; j++)
        r0_sq += distsq[j];
      r0_sq /= 6.;

      // n0 near neighbors with: distsq<1.45*r0_sq
      // n1 near neighbors with: distsq<1.55*r0_sq

      double n0_dist_sq = 1.45*r0_sq,
        n1_dist_sq = 1.55*r0_sq;
      int n0 = 0, n1 = 0;
      for (j = 0; j < n; j++) {
        if (distsq[j] < n1_dist_sq) {
          nearest_n1[n1++] = nearest[j];
          if (distsq[j] < n0_dist_sq) {
            nearest_n0[n0++] = nearest[j];
          }
        }
      }

      // Evaluate all angles <(r_ij,rik) forall n0 particles with:
      // distsq < 1.45*r0_sq

      double bond_angle;
      double norm_j, norm_k;
      chi[0] = chi[1] = chi[2] = chi[3] = chi[4] = chi[5] = chi[6] = chi[7] = 0;
      double x_ij, y_ij, z_ij, x_ik, y_ik, z_ik;
      for (j = 0; j < n0; j++) {
        x_ij = x[i][0]-x[nearest_n0[j]][0];
        y_ij = x[i][1]-x[nearest_n0[j]][1];
        z_ij = x[i][2]-x[nearest_n0[j]][2];
        norm_j = sqrt (x_ij*x_ij + y_ij*y_ij + z_ij*z_ij);
        if (norm_j <= 0.) continue;
        for (k = j+1; k < n0; k++) {
          x_ik = x[i][0]-x[nearest_n0[k]][0];
          y_ik = x[i][1]-x[nearest_n0[k]][1];
          z_ik = x[i][2]-x[nearest_n0[k]][2];
          norm_k = sqrt (x_ik*x_ik + y_ik*y_ik + z_ik*z_ik);
          if (norm_k <= 0.)
            continue;

          bond_angle = (x_ij*x_ik + y_ij*y_ik + z_ij*z_ik) / (norm_j*norm_k);

          // Histogram for identifying the relevant peaks

          if (bond_angle < -0.945) chi[0]++;
          else if (bond_angle < -0.915) chi[1]++;
          else if (bond_angle < -0.755) chi[2]++;
          else if (bond_angle < -0.195) chi[3]++;
          else if (bond_angle < 0.195) chi[4]++;
          else if (bond_angle < 0.245) chi[5]++;
          else if (bond_angle < 0.795) chi[6]++;
          else chi[7]++;
        }
      }
      if (legacy) {

        // This is the original implementation by Gerolf Ziegenhain
        // Deviations from the different lattice structures

        double delta_bcc = 0.35*chi[4]/(double)(chi[5]+chi[6]-chi[4]);
        double delta_cp = fabs(1.-(double)chi[6]/24.);
        double delta_fcc = 0.61*(fabs((double)(chi[0]+chi[1]-6.))+
                                 (double)chi[2])/6.0;
        double delta_hcp = (fabs((double)chi[0]-3.)+
                            fabs((double)chi[0]+(double)chi[1]+
                                 (double)chi[2]+(double)chi[3]-9.0))/12.0;

        // Identification of the local structure according to the reference

        if (chi[0] == 7)       { delta_bcc = 0.; }
        else if (chi[0] == 6)  { delta_fcc = 0.; }
        else if (chi[0] <= 3)  { delta_hcp = 0.; }

        if (chi[7] > 0.)
          structure[i] = UNKNOWN;
        else
          if (chi[4] < 3.)
            {
              if (n1 > 13 || n1 < 11)
                structure[i] = UNKNOWN;
              else
                structure[i] = ICO;
            } else
            if (delta_bcc <= delta_cp)
              {
                if (n1 < 11)
                  structure[i] = UNKNOWN;
                else
                  structure[i] = BCC;
              } else
              if (n1 > 12 || n1 < 11)
                structure[i] = UNKNOWN;
              else
                if (delta_fcc < delta_hcp)
                  structure[i] = FCC;
                else
                  structure[i] = HCP;

      } else {

        // This is the updated implementation by Brian Barnes

        if (chi[7] > 0 || n0 < 11) structure[i] = UNKNOWN;
        else if (chi[0] == 7) structure[i] = BCC;
        else if (chi[0] == 6) structure[i] = FCC;
        else if (chi[0] == 3) structure[i] = HCP;
        else {
          // Deviations from the different lattice structures

          double delta_cp = fabs(1.-(double)chi[6]/24.);

          // ensure we do not get divide by zero
          // and if we will, make delta_bcc irrelevant
          double delta_bcc = delta_cp + 1.0;
          int chi56m4 = chi[5]+chi[6]-chi[4];

          // note that chi[7] presumed zero
          if (chi56m4 != 0) delta_bcc = 0.35*chi[4]/(double)chi56m4;

          double delta_fcc = 0.61*(fabs((double)(chi[0]+chi[1]-6))
                                   +(double)chi[2])/6.0;

          double delta_hcp = (fabs((double)chi[0]-3.)
                              +fabs((double)chi[0]
                                    +(double)chi[1]
                                    +(double)chi[2]
                                    +(double)chi[3]
                                    -9.0))/12.0;

          // Identification of the local structure according to the reference

          if (delta_bcc >= 0.1 && delta_cp >= 0.1 && delta_fcc >= 0.1
              && delta_hcp >= 0.1) structure[i] = UNKNOWN;

          // not part of Ackland-Jones 2006; included for backward compatibility
          if (chi[4] < 3. && n1 == 12) structure[i] = ICO;

          else {
            if (delta_bcc <= delta_cp && n1 > 10 && n1 < 13) structure[i] = BCC;
            else {
              if (n0 > 12) structure[i] = UNKNOWN;
              else {
                if (delta_fcc < delta_hcp) structure[i] = FCC;
                else
                  structure[i] = HCP;
              }
            }
          }
        }
      }
    } else structure[i] = 0.0;
  }
}

/* ----------------------------------------------------------------------
   2 select routines from Numerical Recipes (slightly modified)
   find k smallest values in array of length n
   2nd routine sorts auxiliary array at same time
------------------------------------------------------------------------- */

#define SWAP(a,b)   tmp = a; a = b; b = tmp;
#define ISWAP(a,b) itmp = a; a = b; b = itmp;

void ComputeAcklandAtom::select(int k, int n, double *arr)
  {
  int i,ir,j,l,mid;
  double a,tmp;

  arr--;
  l = 1;
  ir = n;
  for (;;) {
    if (ir <= l+1) {
      if (ir == l+1 && arr[ir] < arr[l]) {
        SWAP(arr[l],arr[ir])
      }
      return;
    } else {
      mid=(l+ir) >> 1;
      SWAP(arr[mid],arr[l+1])
      if (arr[l] > arr[ir]) {
        SWAP(arr[l],arr[ir])
      }
      if (arr[l+1] > arr[ir]) {
        SWAP(arr[l+1],arr[ir])
      }
      if (arr[l] > arr[l+1]) {
        SWAP(arr[l],arr[l+1])
      }
      i = l+1;
      j = ir;
      a = arr[l+1];
      for (;;) {
        do i++; while (arr[i] < a);
        do j--; while (arr[j] > a);
        if (j < i) break;
        SWAP(arr[i],arr[j])
      }
      arr[l+1] = arr[j];
      arr[j] = a;
      if (j >= k) ir = j-1;
      if (j <= k) l = i;
    }
  }
}

/* ---------------------------------------------------------------------- */

void ComputeAcklandAtom::select2(int k, int n, double *arr, int *iarr)
{
  int i,ir,j,l,mid,ia,itmp;
  double a,tmp;

  arr--;
  iarr--;
  l = 1;
  ir = n;
  for (;;) {
    if (ir <= l+1) {
      if (ir == l+1 && arr[ir] < arr[l]) {
        SWAP(arr[l],arr[ir])
        ISWAP(iarr[l],iarr[ir])
      }
      return;
    } else {
      mid=(l+ir) >> 1;
      SWAP(arr[mid],arr[l+1])
      ISWAP(iarr[mid],iarr[l+1])
      if (arr[l] > arr[ir]) {
        SWAP(arr[l],arr[ir])
        ISWAP(iarr[l],iarr[ir])
      }
      if (arr[l+1] > arr[ir]) {
        SWAP(arr[l+1],arr[ir])
        ISWAP(iarr[l+1],iarr[ir])
      }
      if (arr[l] > arr[l+1]) {
        SWAP(arr[l],arr[l+1])
        ISWAP(iarr[l],iarr[l+1])
      }
      i = l+1;
      j = ir;
      a = arr[l+1];
      ia = iarr[l+1];
      for (;;) {
        do i++; while (arr[i] < a);
        do j--; while (arr[j] > a);
        if (j < i) break;
        SWAP(arr[i],arr[j])
        ISWAP(iarr[i],iarr[j])
      }
      arr[l+1] = arr[j];
      arr[j] = a;
      iarr[l+1] = iarr[j];
      iarr[j] = ia;
      if (j >= k) ir = j-1;
      if (j <= k) l = i;
    }
  }
}

/* ----------------------------------------------------------------------
   memory usage of local atom-based array
------------------------------------------------------------------------- */

double ComputeAcklandAtom::memory_usage()
{
  double bytes = nmax * sizeof(double);
  return bytes;
}