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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* ----------------------------------------------------------------------
   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 authors: Mike Parks (SNL), Ezwanur Rahman, J.T. Foster (UTSA)
------------------------------------------------------------------------- */

#include "fix_peri_neigh.h"
#include <mpi.h>
#include <cmath>
#include "pair_peri_lps.h"
#include "pair_peri_ves.h"
#include "pair_peri_eps.h"
#include "atom.h"
#include "domain.h"
#include "force.h"
#include "comm.h"
#include "neighbor.h"
#include "neigh_list.h"
#include "neigh_request.h"
#include "pair.h"
#include "lattice.h"
#include "memory.h"
#include "error.h"

using namespace LAMMPS_NS;
using namespace FixConst;

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

FixPeriNeigh::FixPeriNeigh(LAMMPS *lmp,int narg, char **arg) :
  Fix(lmp, narg, arg)
{
  isPMB = isLPS = isVES = isEPS = 0;
  if (force->pair_match("peri/pmb",1)) isPMB = 1;
  if (force->pair_match("peri/lps",1)) isLPS = 1;
  if (force->pair_match("peri/ves",1)) isVES = 1;
  if (force->pair_match("peri/eps",1)) isEPS = 1;

  restart_global = 1;
  restart_peratom = 1;
  first = 1;

  // perform initial allocation of atom-based arrays
  // register with atom class
  // set maxpartner = 1 as placeholder

  maxpartner = 1;
  npartner = NULL;
  partner = NULL;
  deviatorextention = NULL;
  deviatorBackextention = NULL;
  deviatorPlasticextension = NULL;
  lambdaValue = NULL;
  r0 = NULL;
  vinter = NULL;
  wvolume = NULL;

  grow_arrays(atom->nmax);
  atom->add_callback(0);
  atom->add_callback(1);

  // initialize npartner to 0 so atom migration is OK the 1st time

  int nlocal = atom->nlocal;
  for (int i = 0; i < nlocal; i++) npartner[i] = 0;

  // set comm sizes needed by this fix

  comm_forward = 1;
}

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

FixPeriNeigh::~FixPeriNeigh()
{
  // unregister this fix so atom class doesn't invoke it any more

  atom->delete_callback(id,0);
  atom->delete_callback(id,1);

  // delete locally stored arrays

  memory->destroy(npartner);
  memory->destroy(partner);
  memory->destroy(deviatorextention);
  memory->destroy(deviatorBackextention);
  memory->destroy(deviatorPlasticextension);
  memory->destroy(lambdaValue);
  memory->destroy(r0);
  memory->destroy(vinter);
  memory->destroy(wvolume);
}

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

int FixPeriNeigh::setmask()
{
  int mask = 0;
  return mask;
}

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

void FixPeriNeigh::init()
{
  if (!first) return;

  // need a full neighbor list once

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

  // compute PD scale factor, stored in Atom class, used by DumpCFG

  int nlocal = atom->nlocal;
  double vone = 0.0;
  for (int i = 0; i < nlocal; i++) vone += atom->vfrac[i];
  double vave;
  MPI_Allreduce(&vone,&vave,1,MPI_DOUBLE,MPI_SUM,world);
  if (atom->natoms) vave /= atom->natoms;
  if (vave > 0.0) atom->pdscale = 1.44 / pow(vave,1.0/3.0);
  else atom->pdscale = 1.0;
}

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

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

/* ----------------------------------------------------------------------
   For minimization: setup as with dynamics
------------------------------------------------------------------------- */

void FixPeriNeigh::min_setup(int vflag)
{
  setup(vflag);
}

/* ----------------------------------------------------------------------
   create initial list of neighbor partners via call to neighbor->build()
   must be done in setup (not init) since fix init comes before neigh init
------------------------------------------------------------------------- */

void FixPeriNeigh::setup(int /*vflag*/)
{
  int i,j,ii,jj,itype,jtype,inum,jnum;
  double xtmp,ytmp,ztmp,delx,dely,delz,rsq;
  int *ilist,*jlist,*numneigh;
  int **firstneigh;

  double **x = atom->x;
  double *vfrac = atom->vfrac;
  int *type = atom->type;
  tagint *tag = atom->tag;
  int nlocal = atom->nlocal;

 // only build list of bonds on very first run

  if (!first) return;
  first = 0;

  // build full neighbor list, will copy or build as necessary

  neighbor->build_one(list);

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

  // scan neighbor list to set maxpartner

  Pair *anypair = force->pair_match("peri",0);
  double **cutsq = anypair->cutsq;

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

    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;
      jtype = type[j];
      if (rsq <= cutsq[itype][jtype]) npartner[i]++;
    }
  }

  maxpartner = 0;
  for (i = 0; i < nlocal; i++) maxpartner = MAX(maxpartner,npartner[i]);
  int maxall;
  MPI_Allreduce(&maxpartner,&maxall,1,MPI_INT,MPI_MAX,world);
  maxpartner = maxall;

  // realloc arrays with correct value for maxpartner

  memory->destroy(partner);
  memory->destroy(deviatorextention);
  memory->destroy(deviatorBackextention);
  memory->destroy(deviatorPlasticextension);
  memory->destroy(lambdaValue);
  memory->destroy(r0);
  memory->destroy(npartner);

  npartner = NULL;
  partner = NULL;
  deviatorextention = NULL;
  deviatorBackextention = NULL;
  deviatorPlasticextension = NULL;
  lambdaValue = NULL;
  r0 = NULL;
  grow_arrays(atom->nmax);

  // create partner list and r0 values from neighbor list
  // compute vinter for each atom

  for (i = 0; i < nlocal; i++) {
    npartner[i] = 0;
    vinter[i] = 0.0;
    wvolume[i] = 0.0;
    if (isEPS) lambdaValue[i] = 0.0;
  }

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

    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;
      jtype = type[j];

      if (rsq <= cutsq[itype][jtype]) {
        partner[i][npartner[i]] = tag[j];
        if (isVES)
          deviatorextention[i][npartner[i]] =
            deviatorBackextention[i][npartner[i]] = 0.0;
        if (isEPS)
           deviatorPlasticextension[i][npartner[i]] = 0.0;
        r0[i][npartner[i]] = sqrt(rsq);
        npartner[i]++;
        vinter[i] += vfrac[j];
      }
    }
  }

  // sanity check: does any atom appear twice in any neigborlist?
  // should only be possible if using pbc and domain < 2*delta

  if (domain->xperiodic || domain->yperiodic || domain->zperiodic) {
    for (i = 0; i < nlocal; i++) {
      jnum = npartner[i];
      for (jj = 0; jj < jnum; jj++) {
        for (int kk = jj+1; kk < jnum; kk++) {
          if (partner[i][jj] == partner[i][kk])
            error->one(FLERR,"Duplicate particle in PeriDynamic bond - "
                       "simulation box is too small");
        }
      }
    }
  }

  // compute wvolume for each atom

  double **x0 = atom->x0;
  double half_lc = 0.5*(domain->lattice->xlattice);
  double vfrac_scale;
  PairPeriLPS *pairlps = NULL;
  PairPeriVES *pairves = NULL;
  PairPeriEPS *paireps = NULL;
  if (isLPS)
    pairlps = static_cast<PairPeriLPS*>(anypair);
  else if (isVES)
    pairves = static_cast<PairPeriVES*>(anypair);
  else if (isEPS)
    paireps = static_cast<PairPeriEPS*>(anypair);

  for (i = 0; i < nlocal; i++) {
    double xtmp0 = x0[i][0];
    double ytmp0 = x0[i][1];
    double ztmp0 = x0[i][2];
    jnum = npartner[i];
    itype = type[i];

    // loop over partners of particle i

    for (jj = 0; jj < jnum; jj++) {

      // if bond already broken, skip this partner

      if (partner[i][jj] == 0) continue;

      // lookup local index of partner particle

      j = atom->map(partner[i][jj]);

      // skip if particle is "lost"

      if (j < 0) continue;

      double delx0 = xtmp0 - x0[j][0];
      double dely0 = ytmp0 - x0[j][1];
      double delz0 = ztmp0 - x0[j][2];

      double rsq0 = delx0*delx0 + dely0*dely0 + delz0*delz0;

      jtype = type[j];
      double delta = sqrt(cutsq[itype][jtype]);

      // scale vfrac[j] if particle j near the horizon

      if ((fabs(r0[i][jj] - delta)) <= half_lc)
        vfrac_scale = (-1.0/(2*half_lc))*(r0[i][jj]) +
          (1.0 + ((delta - half_lc)/(2*half_lc) ) );
      else vfrac_scale = 1.0;

      // for PMB, influence = 1.0, otherwise invoke influence function

      if (isPMB)
        wvolume[i] += 1.0 * rsq0 * vfrac[j] * vfrac_scale;
      else if (isLPS)
        wvolume[i] += pairlps->influence_function(delx0,dely0,delz0) *
          rsq0 * vfrac[j] * vfrac_scale;
      else if (isVES)
        wvolume[i] += pairves->influence_function(delx0,dely0,delz0) *
          rsq0 * vfrac[j] * vfrac_scale;
      else if (isEPS)
        wvolume[i] += paireps->influence_function(delx0,dely0,delz0) *
          rsq0 * vfrac[j] * vfrac_scale;
    }
  }

  // communicate wvolume to ghosts

  comm->forward_comm_fix(this);

  // bond statistics

  int n = 0;
  for (i = 0; i < nlocal; i++) n += npartner[i];
  int nall;
  MPI_Allreduce(&n,&nall,1,MPI_INT,MPI_SUM,world);

  if (comm->me == 0) {
    if (screen) {
      fprintf(screen,"Peridynamic bonds:\n");
      fprintf(screen,"  total # of bonds = %d\n",nall);
      fprintf(screen,"  bonds/atom = %g\n",(double)nall/atom->natoms);
    }
    if (logfile) {
      fprintf(logfile,"Peridynamic bonds:\n");
      fprintf(logfile,"  total # of bonds = %d\n",nall);
      fprintf(logfile,"  bonds/atom = %g\n",(double)nall/atom->natoms);
    }
  }
}

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

double FixPeriNeigh::memory_usage()
{
  int nmax = atom->nmax;
  int bytes = nmax * sizeof(int);
  bytes += nmax*maxpartner * sizeof(tagint);
  bytes += nmax*maxpartner * sizeof(double);
  if (isVES) {
    bytes += nmax*maxpartner * sizeof(double);
    bytes += nmax*maxpartner * sizeof(double);
  }
  if (isEPS) {
    bytes += nmax*maxpartner * sizeof(double);
    bytes += nmax * sizeof(double);
  }
  bytes += nmax * sizeof(double);
  bytes += nmax * sizeof(double);
  return bytes;
}

/* ----------------------------------------------------------------------
   allocate local atom-based arrays
------------------------------------------------------------------------- */

void FixPeriNeigh::grow_arrays(int nmax)
{
   memory->grow(npartner,nmax,"peri_neigh:npartner");
   memory->grow(partner,nmax,maxpartner,"peri_neigh:partner");
   if (isVES) {
     memory->grow(deviatorextention,nmax,maxpartner,
                  "peri_neigh:deviatorextention");
     memory->grow(deviatorBackextention,nmax,maxpartner,
                  "peri_neigh:deviatorBackextention");
   }
   if (isEPS) memory->grow(deviatorPlasticextension,nmax,maxpartner,
                           "peri_neigh:deviatorPlasticextension");
   memory->grow(r0,nmax,maxpartner,"peri_neigh:r0");
   if (isEPS) memory->grow(lambdaValue,nmax,"peri_neigh:lambdaValue");
   memory->grow(vinter,nmax,"peri_neigh:vinter");
   memory->grow(wvolume,nmax,"peri_neigh:wvolume");
}

/* ----------------------------------------------------------------------
   copy values within local atom-based arrays
------------------------------------------------------------------------- */

void FixPeriNeigh::copy_arrays(int i, int j, int /*delflag*/)
{
  npartner[j] = npartner[i];
  for (int m = 0; m < npartner[j]; m++) {
    partner[j][m] = partner[i][m];
    if (isVES) {
      deviatorextention[j][m] = deviatorextention[i][m];
      deviatorBackextention[j][m] = deviatorBackextention[i][m];
    }
    if (isEPS)
      deviatorPlasticextension[j][m] = deviatorPlasticextension[i][m];
    r0[j][m] = r0[i][m];
  }
  if (isEPS) lambdaValue[j] = lambdaValue[i];
  vinter[j] = vinter[i];
  wvolume[j] = wvolume[i];
}

/* ----------------------------------------------------------------------
   pack values in local atom-based arrays for exchange with another proc
------------------------------------------------------------------------- */

int FixPeriNeigh::pack_exchange(int i, double *buf)
{
  // compact list by eliminating partner = 0 entries
  // set buf[0] after compaction

  int m = 1;
  for (int n = 0; n < npartner[i]; n++) {
    if (partner[i][n] == 0) continue;
    buf[m++] = partner[i][n];
    if (isVES) {
      buf[m++] = deviatorextention[i][n];
      buf[m++] = deviatorBackextention[i][n];
    }
    if (isEPS) buf[m++] = deviatorPlasticextension[i][n];
    buf[m++] = r0[i][n];
  }
  if (isVES) buf[0] = m/4;
  else if (isEPS) buf[0] = m/3;
  else buf[0] = m/2;
  if (isEPS) buf[m++] = lambdaValue[i];
  buf[m++] = vinter[i];
  buf[m++] = wvolume[i];
  return m;
}

/* ----------------------------------------------------------------------
   unpack values in local atom-based arrays from exchange with another proc
------------------------------------------------------------------------- */

int FixPeriNeigh::unpack_exchange(int nlocal, double *buf)
{
  int m = 0;
  npartner[nlocal] = static_cast<int> (buf[m++]);
  for (int n = 0; n < npartner[nlocal]; n++) {
    partner[nlocal][n] = static_cast<tagint> (buf[m++]);
    if (isVES) {
      deviatorextention[nlocal][n] = buf[m++];
      deviatorBackextention[nlocal][n] = buf[m++];
    }
    if (isEPS) deviatorPlasticextension[nlocal][n] = buf[m++];
    r0[nlocal][n] = buf[m++];
  }
  if (isEPS) lambdaValue[nlocal] = buf[m++];
  vinter[nlocal] = buf[m++];
  wvolume[nlocal] = buf[m++];
  return m;
}

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

int FixPeriNeigh::pack_forward_comm(int n, int *list, double *buf,
                                    int /*pbc_flag*/, int * /*pbc*/)
{
  int i,j,m;

  m = 0;
  for (i = 0; i < n; i++) {
    j = list[i];
    buf[m++] = wvolume[j];
  }
  return m;
}

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

void FixPeriNeigh::unpack_forward_comm(int n, int first, double *buf)
{
  int i,m,last;

  m = 0;
  last = first + n;
  for (i = first; i < last; i++)
    wvolume[i] = buf[m++];
}

/* ----------------------------------------------------------------------
   pack entire state of Fix into one write
------------------------------------------------------------------------- */

void FixPeriNeigh::write_restart(FILE *fp)
{
  int n = 0;
  double list[2];
  list[n++] = first;
  list[n++] = maxpartner;

  if (comm->me == 0) {
    int size = n * sizeof(double);
    fwrite(&size,sizeof(int),1,fp);
    fwrite(list,sizeof(double),n,fp);
  }
}

/* ----------------------------------------------------------------------
   use state info from restart file to restart the Fix
------------------------------------------------------------------------- */

void FixPeriNeigh::restart(char *buf)
{
  int n = 0;
  double *list = (double *) buf;

  first = static_cast<int> (list[n++]);
  maxpartner = static_cast<int> (list[n++]);

  // grow 2D arrays now, cannot change size of 2nd array index later

  grow_arrays(atom->nmax);
}

/* ----------------------------------------------------------------------
   pack values in local atom-based arrays for restart file
------------------------------------------------------------------------- */

int FixPeriNeigh::pack_restart(int i, double *buf)
{
  int m = 0;
  if (isVES) buf[m++] = 4*npartner[i] + 4;
  else if (isEPS) buf[m++] = 3*npartner[i] + 5;
  else buf[m++] = 2*npartner[i] + 4;
  buf[m++] = npartner[i];
  for (int n = 0; n < npartner[i]; n++) {
    buf[m++] = partner[i][n];
    if (isVES) {
      buf[m++] = deviatorextention[i][n];
      buf[m++] = deviatorBackextention[i][n];
    }
    if (isEPS) buf[m++] = deviatorPlasticextension[i][n];
    buf[m++] = r0[i][n];
  }
  if (isEPS) buf[m++] = lambdaValue[i];
  buf[m++] = vinter[i];
  buf[m++] = wvolume[i];
  return m;
}

/* ----------------------------------------------------------------------
   unpack values from atom->extra array to restart the fix
------------------------------------------------------------------------- */

void FixPeriNeigh::unpack_restart(int nlocal, int nth)
{

  double **extra = atom->extra;

  // skip to Nth set of extra values

  int m = 0;
  for (int i = 0; i < nth; i++) m += static_cast<int> (extra[nlocal][m]);
  m++;

  npartner[nlocal] = static_cast<int> (extra[nlocal][m++]);
  for (int n = 0; n < npartner[nlocal]; n++) {
    partner[nlocal][n] = static_cast<tagint> (extra[nlocal][m++]);
    if (isVES) {
      deviatorextention[nlocal][n] = extra[nlocal][m++];
      deviatorBackextention[nlocal][n] = extra[nlocal][m++];
    }
    if (isEPS) deviatorPlasticextension[nlocal][n] = extra[nlocal][m++];
    r0[nlocal][n] = extra[nlocal][m++];
  }
  if (isEPS) lambdaValue[nlocal] = extra[nlocal][m++];
  vinter[nlocal] = extra[nlocal][m++];
  wvolume[nlocal] = extra[nlocal][m++];
}

/* ----------------------------------------------------------------------
   maxsize of any atom's restart data
------------------------------------------------------------------------- */

int FixPeriNeigh::maxsize_restart()
{
  if (isVES) return 4*maxpartner + 4;
  if (isEPS) return 3*maxpartner + 5;
  return 2*maxpartner + 4;
}

/* ----------------------------------------------------------------------
   size of atom nlocal's restart data
------------------------------------------------------------------------- */

int FixPeriNeigh::size_restart(int nlocal)
{
  if (isVES) return 4*npartner[nlocal] + 4;
  if (isEPS) return 3*npartner[nlocal] + 5;
  return 2*npartner[nlocal] + 4;
}