-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbond_list.cpp
321 lines (251 loc) · 8.82 KB
/
bond_list.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
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, [email protected]
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: Eric Simon (Cray)
------------------------------------------------------------------------- */
#include "math.h"
#include "stdlib.h"
#include "bond_list.h"
#include "atom.h"
#include "neighbor.h"
#include "domain.h"
#include "comm.h"
#include "force.h"
#include "memory.h"
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace LAMMPS_NS;
/* ---------------------------------------------------------------------- */
BondList::BondList(LAMMPS *lmp) : Bond(lmp) {}
/* ---------------------------------------------------------------------- */
BondList::~BondList()
{
if (allocated) {
memory->destroy(setflag);
memory->destroy(r0);
memory->destroy(k2);
memory->destroy(k3);
memory->destroy(k4);
}
}
/* ---------------------------------------------------------------------- */
void BondList::compute(int eflag, int vflag)
{
int i1,i2,n,type;
double delx,dely,delz,ebond,fbond;
double rsq,r,dr,dr2,dr3,dr4,de_bond;
ebond = 0.0;
if (eflag || vflag) ev_setup(eflag,vflag);
else evflag = 0;
double **x = atom->x;
int *tag = atom->tag;
double **f = atom->f;
int **bondlist = neighbor->bondlist;
int nbondlist = neighbor->nbondlist;
int nlocal = atom->nlocal;
int newton_bond = force->newton_bond;
for (n = 0; n < nbondlist; n++) {
i1 = bondlist[n][0];
i2 = bondlist[n][1];
type =tag[i1]+ tag[i2];
delx = x[i1][0] - x[i2][0];
dely = x[i1][1] - x[i2][1];
delz = x[i1][2] - x[i2][2];
rsq = delx*delx + dely*dely + delz*delz;
r = sqrt(rsq);
dr = r - r0[type];
dr2 = dr*dr;
dr3 = dr2*dr;
dr4 = dr3*dr;
// force & energy
de_bond = 2.0*k2[type]*dr + 3.0*k3[type]*dr2 + 4.0*k4[type]*dr3;
if (r > 0.0) fbond = -de_bond/r;
else fbond = 0.0;
if (eflag) ebond = k2[type]*dr2 + k3[type]*dr3 + k4[type]*dr4;
// apply force to each of 2 atoms
if (newton_bond || i1 < nlocal) {
f[i1][0] += delx*fbond;
f[i1][1] += dely*fbond;
f[i1][2] += delz*fbond;
}
if (newton_bond || i2 < nlocal) {
f[i2][0] -= delx*fbond;
f[i2][1] -= dely*fbond;
f[i2][2] -= delz*fbond;
}
if (evflag) ev_tally(i1,i2,nlocal,newton_bond,ebond,fbond,delx,dely,delz);
}
}
/* ---------------------------------------------------------------------- */
void BondList::allocate()
{
allocated = 1;
int n = atom->nbondtypes;
//memory->create(r0,n+1,"bond:r0");
//memory->create(k2,n+1,"bond:k2");
//memory->create(k3,n+1,"bond:k3");
//memory->create(k4,n+1,"bond:k4");
memory->create(setflag,n+1,"bond:setflag");
for (int i = 1; i <= n; i++) setflag[i] = 0;
}
/* ----------------------------------------------------------------------
global settings
------------------------------------------------------------------------- */
void BondList::settings(int narg, char **arg)
{
if (narg != 1) error->all(FLERR,"Illegal bond_style command");
FILE *fp = fopen(arg[0],"r");
char line[1024];
if (fp == NULL)
error->all(FLERR,"Cannot open bond list file");
int num = 1;
char *ptr;
int idx, id1, id2;
while(fgets(line,1024,fp)) {
ptr = strtok(line," \t\n\r\f");
// skip empty lines
if (!ptr) continue;
// skip comment lines starting with #
if (*ptr == '#') continue;
id1 = atoi(ptr);
ptr = strtok(NULL," \t\n\r\f");
// The second site
if (!ptr)
error->all(FLERR,"Incorrectly formatted bond list file");
id2 = atoi(ptr);
// Setting the idx in the base array
if (id1>num) num=id1;
if (id2>num) num=id2;
}
// while(fgets(line,1024,fp)) ++num;
rewind(fp);
int array_size = 4*num+4; // This array size is currently ad hoc
memory->create(r0,array_size,"bond:r0");
memory->create(k2,array_size,"bond:k2");
memory->create(k3,array_size,"bond:k3");
memory->create(k4,array_size,"bond:k4");
// Loop through the rest of the lines
while(fgets(line,1024,fp)) {
ptr = strtok(line," \t\n\r\f");
// skip empty lines
if (!ptr) continue;
// skip comment lines starting with #
if (*ptr == '#') continue;
id1 = atoi(ptr);
ptr = strtok(NULL," \t\n\r\f");
// The second site
if (!ptr)
error->all(FLERR,"Incorrectly formatted bond list file");
id2 = atoi(ptr);
// Setting the idx in the base array
idx = id1 + id2;
if ((idx-1) > array_size)
error->all(FLERR,"Parameter array in bond_list.cpp is too short!");
// r0
ptr = strtok(NULL," \t\n\r\f");
if (!ptr)
error->all(FLERR,"Incorrectly formatted bond list file");
r0[idx] = force->numeric(FLERR,ptr);
//k2
ptr = strtok(NULL," \t\n\r\f");
if (!ptr)
error->all(FLERR,"Incorrectly formatted bond list file");
k2[idx] = force->numeric(FLERR,ptr);
//k3
ptr = strtok(NULL," \t\n\r\f");
if (!ptr)
error->all(FLERR,"Incorrectly formatted bond list file");
k3[idx] = force->numeric(FLERR,ptr);
//k4
ptr = strtok(NULL," \t\n\r\f");
if (!ptr)
error->all(FLERR,"Incorrectly formatted bond list file");
k4[idx] = force->numeric(FLERR,ptr);
}
fclose(fp);
}
/* ----------------------------------------------------------------------
set coeffs for one or more type pairs
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
there are no coeffs to be set, but we need to update setflag and pretend
------------------------------------------------------------------------- */
void BondList::coeff(int narg, char **arg)
{
if (narg < 1) error->all(FLERR,"Incorrect args for bond coefficients");
if (!allocated) allocate();
int ilo,ihi;
force->bounds(FLERR,arg[0],atom->nbondtypes,ilo,ihi);
int count = 0;
for (int i = ilo; i <= ihi; i++) {
setflag[i] = 1;
count++;
}
if (count == 0) error->all(FLERR,"Incorrect args for bond coefficients");
}
/* ----------------------------------------------------------------------
return an equilbrium bond length
------------------------------------------------------------------------- */
double BondList::equilibrium_distance(int i)
{
return r0[i];
}
/* ----------------------------------------------------------------------
proc 0 writes out coeffs to restart file
------------------------------------------------------------------------- */
void BondList::write_restart(FILE *fp)
{
fwrite(&r0[1],sizeof(double),atom->nbondtypes,fp);
fwrite(&k2[1],sizeof(double),atom->nbondtypes,fp);
fwrite(&k3[1],sizeof(double),atom->nbondtypes,fp);
fwrite(&k4[1],sizeof(double),atom->nbondtypes,fp);
}
/* ----------------------------------------------------------------------
proc 0 reads coeffs from restart file, bcasts them
------------------------------------------------------------------------- */
void BondList::read_restart(FILE *fp)
{
allocate();
if (comm->me == 0) {
fread(&r0[1],sizeof(double),atom->nbondtypes,fp);
fread(&k2[1],sizeof(double),atom->nbondtypes,fp);
fread(&k3[1],sizeof(double),atom->nbondtypes,fp);
fread(&k4[1],sizeof(double),atom->nbondtypes,fp);
}
MPI_Bcast(&r0[1],atom->nbondtypes,MPI_DOUBLE,0,world);
MPI_Bcast(&k2[1],atom->nbondtypes,MPI_DOUBLE,0,world);
MPI_Bcast(&k3[1],atom->nbondtypes,MPI_DOUBLE,0,world);
MPI_Bcast(&k4[1],atom->nbondtypes,MPI_DOUBLE,0,world);
for (int i = 1; i <= atom->nbondtypes; i++) setflag[i] = 1;
}
/* ----------------------------------------------------------------------
proc 0 writes to data file
------------------------------------------------------------------------- */
void BondList::write_data(FILE *fp)
{
for (int i = 1; i <= atom->nbondtypes; i++)
fprintf(fp,"%d %g %g %g %g\n",i,r0[i],k2[i],k3[i],k4[i]);
}
/* ---------------------------------------------------------------------- */
double BondList::single(int type, double rsq, int i, int j, double &fforce)
{
double r = sqrt(rsq);
double dr = r - r0[type];
double dr2 = dr*dr;
double dr3 = dr2*dr;
double dr4 = dr3*dr;
double de_bond = 2.0*k2[type]*dr + 3.0*k3[type]*dr2 + 4.0*k4[type]*dr3;
if (r > 0.0) fforce = -de_bond/r;
else fforce = 0.0;
return (k2[type]*dr2 + k3[type]*dr3 + k4[type]*dr4);
}