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 "fix_read_restart.h"
16
17 #include "atom.h"
18 #include "memory.h"
19
20 using namespace LAMMPS_NS;
21 using namespace FixConst;
22
23 /* ---------------------------------------------------------------------- */
24
FixReadRestart(LAMMPS * lmp,int narg,char ** arg)25 FixReadRestart::FixReadRestart(LAMMPS *lmp, int narg, char **arg) :
26 Fix(lmp, narg, arg),
27 count(nullptr), extra(nullptr)
28 {
29 nextra = utils::inumeric(FLERR,arg[3],false,lmp);
30 int nfix = utils::inumeric(FLERR,arg[4],false,lmp);
31
32 // perform initial allocation of atom-based array
33 // register with Atom class
34
35 FixReadRestart::grow_arrays(atom->nmax);
36 atom->add_callback(Atom::GROW);
37
38 // extra = copy of atom->extra
39
40 double **atom_extra = atom->extra;
41 int nlocal = atom->nlocal;
42 int i,j,m;
43
44 for (i = 0; i < nlocal; i++) {
45 m = 0;
46 for (j = 0; j < nfix; j++) m += static_cast<int> (atom_extra[i][m]);
47 count[i] = m;
48 for (j = 0; j < m; j++) extra[i][j] = atom_extra[i][j];
49 }
50 }
51
52 /* ---------------------------------------------------------------------- */
53
~FixReadRestart()54 FixReadRestart::~FixReadRestart()
55 {
56 // unregister callback to this fix from Atom class
57
58 atom->delete_callback(id,Atom::GROW);
59
60 // delete locally stored arrays
61
62 memory->destroy(count);
63 memory->destroy(extra);
64 }
65
66 /* ---------------------------------------------------------------------- */
67
setmask()68 int FixReadRestart::setmask()
69 {
70 int mask = 0;
71 return mask;
72 }
73
74 /* ----------------------------------------------------------------------
75 memory usage of local atom-based array
76 ------------------------------------------------------------------------- */
77
memory_usage()78 double FixReadRestart::memory_usage()
79 {
80 double bytes = (double)atom->nmax*nextra * sizeof(double);
81 bytes += (double)atom->nmax * sizeof(int);
82 return bytes;
83 }
84
85 /* ----------------------------------------------------------------------
86 allocate atom-based array
87 ------------------------------------------------------------------------- */
88
grow_arrays(int nmax)89 void FixReadRestart::grow_arrays(int nmax)
90 {
91 memory->grow(count,nmax,"read_restart:count");
92 memory->grow(extra,nmax,nextra,"read_restart:extra");
93 }
94
95 /* ----------------------------------------------------------------------
96 copy values within local atom-based array
97 ------------------------------------------------------------------------- */
98
copy_arrays(int i,int j,int)99 void FixReadRestart::copy_arrays(int i, int j, int /*delflag*/)
100 {
101 count[j] = count[i];
102 for (int m = 0; m < count[i]; m++) extra[j][m] = extra[i][m];
103 }
104
105 /* ----------------------------------------------------------------------
106 pack values in local atom-based array for exchange with another proc
107 ------------------------------------------------------------------------- */
108
pack_exchange(int i,double * buf)109 int FixReadRestart::pack_exchange(int i, double *buf)
110 {
111 buf[0] = count[i];
112 for (int m = 0; m < count[i]; m++) buf[m+1] = extra[i][m];
113 return count[i]+1;
114 }
115
116 /* ----------------------------------------------------------------------
117 unpack values in local atom-based array from exchange with another proc
118 ------------------------------------------------------------------------- */
119
unpack_exchange(int nlocal,double * buf)120 int FixReadRestart::unpack_exchange(int nlocal, double *buf)
121 {
122 count[nlocal] = static_cast<int> (buf[0]);
123 for (int m = 0; m < count[nlocal]; m++) extra[nlocal][m] = buf[m+1];
124 return count[nlocal]+1;
125 }
126