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 "angle_cosine.h"
16 
17 #include <cmath>
18 #include "atom.h"
19 #include "neighbor.h"
20 #include "domain.h"
21 #include "comm.h"
22 #include "force.h"
23 #include "math_const.h"
24 #include "memory.h"
25 #include "error.h"
26 
27 
28 using namespace LAMMPS_NS;
29 using namespace MathConst;
30 
31 #define SMALL 0.001
32 
33 /* ---------------------------------------------------------------------- */
34 
AngleCosine(LAMMPS * lmp)35 AngleCosine::AngleCosine(LAMMPS *lmp) : Angle(lmp) {}
36 
37 /* ---------------------------------------------------------------------- */
38 
~AngleCosine()39 AngleCosine::~AngleCosine()
40 {
41   if (allocated && !copymode) {
42     memory->destroy(setflag);
43     memory->destroy(k);
44   }
45 }
46 
47 /* ---------------------------------------------------------------------- */
48 
compute(int eflag,int vflag)49 void AngleCosine::compute(int eflag, int vflag)
50 {
51   int i1,i2,i3,n,type;
52   double delx1,dely1,delz1,delx2,dely2,delz2;
53   double eangle,f1[3],f3[3];
54   double rsq1,rsq2,r1,r2,c,a,a11,a12,a22;
55 
56   eangle = 0.0;
57   ev_init(eflag,vflag);
58 
59   double **x = atom->x;
60   double **f = atom->f;
61   int **anglelist = neighbor->anglelist;
62   int nanglelist = neighbor->nanglelist;
63   int nlocal = atom->nlocal;
64   int newton_bond = force->newton_bond;
65 
66   for (n = 0; n < nanglelist; n++) {
67     i1 = anglelist[n][0];
68     i2 = anglelist[n][1];
69     i3 = anglelist[n][2];
70     type = anglelist[n][3];
71 
72     // 1st bond
73 
74     delx1 = x[i1][0] - x[i2][0];
75     dely1 = x[i1][1] - x[i2][1];
76     delz1 = x[i1][2] - x[i2][2];
77 
78     rsq1 = delx1*delx1 + dely1*dely1 + delz1*delz1;
79     r1 = sqrt(rsq1);
80 
81     // 2nd bond
82 
83     delx2 = x[i3][0] - x[i2][0];
84     dely2 = x[i3][1] - x[i2][1];
85     delz2 = x[i3][2] - x[i2][2];
86 
87     rsq2 = delx2*delx2 + dely2*dely2 + delz2*delz2;
88     r2 = sqrt(rsq2);
89 
90     // c = cosine of angle
91 
92     c = delx1*delx2 + dely1*dely2 + delz1*delz2;
93     c /= r1*r2;
94     if (c > 1.0) c = 1.0;
95     if (c < -1.0) c = -1.0;
96 
97     // force & energy
98 
99     if (eflag) eangle = k[type]*(1.0+c);
100 
101     a = k[type];
102     a11 = a*c / rsq1;
103     a12 = -a / (r1*r2);
104     a22 = a*c / rsq2;
105 
106     f1[0] = a11*delx1 + a12*delx2;
107     f1[1] = a11*dely1 + a12*dely2;
108     f1[2] = a11*delz1 + a12*delz2;
109     f3[0] = a22*delx2 + a12*delx1;
110     f3[1] = a22*dely2 + a12*dely1;
111     f3[2] = a22*delz2 + a12*delz1;
112 
113     // apply force to each of 3 atoms
114 
115     if (newton_bond || i1 < nlocal) {
116       f[i1][0] += f1[0];
117       f[i1][1] += f1[1];
118       f[i1][2] += f1[2];
119     }
120 
121     if (newton_bond || i2 < nlocal) {
122       f[i2][0] -= f1[0] + f3[0];
123       f[i2][1] -= f1[1] + f3[1];
124       f[i2][2] -= f1[2] + f3[2];
125     }
126 
127     if (newton_bond || i3 < nlocal) {
128       f[i3][0] += f3[0];
129       f[i3][1] += f3[1];
130       f[i3][2] += f3[2];
131     }
132 
133     if (evflag) ev_tally(i1,i2,i3,nlocal,newton_bond,eangle,f1,f3,
134                          delx1,dely1,delz1,delx2,dely2,delz2);
135   }
136 }
137 
138 /* ---------------------------------------------------------------------- */
139 
allocate()140 void AngleCosine::allocate()
141 {
142   allocated = 1;
143   int n = atom->nangletypes;
144 
145   memory->create(k,n+1,"angle:k");
146   memory->create(setflag,n+1,"angle:setflag");
147   for (int i = 1; i <= n; i++) setflag[i] = 0;
148 }
149 
150 /* ----------------------------------------------------------------------
151    set coeffs for one type
152 ------------------------------------------------------------------------- */
153 
coeff(int narg,char ** arg)154 void AngleCosine::coeff(int narg, char **arg)
155 {
156   if (narg != 2) error->all(FLERR,"Incorrect args for angle coefficients");
157   if (!allocated) allocate();
158 
159   int ilo,ihi;
160   utils::bounds(FLERR,arg[0],1,atom->nangletypes,ilo,ihi,error);
161 
162   double k_one = utils::numeric(FLERR,arg[1],false,lmp);
163 
164   int count = 0;
165   for (int i = ilo; i <= ihi; i++) {
166     k[i] = k_one;
167     setflag[i] = 1;
168     count++;
169   }
170 
171   if (count == 0) error->all(FLERR,"Incorrect args for angle coefficients");
172 }
173 
174 /* ---------------------------------------------------------------------- */
175 
equilibrium_angle(int)176 double AngleCosine::equilibrium_angle(int /*i*/)
177 {
178   return MY_PI;
179 }
180 
181 /* ----------------------------------------------------------------------
182    proc 0 writes out coeffs to restart file
183 ------------------------------------------------------------------------- */
184 
write_restart(FILE * fp)185 void AngleCosine::write_restart(FILE *fp)
186 {
187   fwrite(&k[1],sizeof(double),atom->nangletypes,fp);
188 }
189 
190 /* ----------------------------------------------------------------------
191    proc 0 reads coeffs from restart file, bcasts them
192 ------------------------------------------------------------------------- */
193 
read_restart(FILE * fp)194 void AngleCosine::read_restart(FILE *fp)
195 {
196   allocate();
197 
198   if (comm->me == 0) utils::sfread(FLERR,&k[1],sizeof(double),atom->nangletypes,fp,nullptr,error);
199   MPI_Bcast(&k[1],atom->nangletypes,MPI_DOUBLE,0,world);
200 
201   for (int i = 1; i <= atom->nangletypes; i++) setflag[i] = 1;
202 }
203 
204 /* ----------------------------------------------------------------------
205    proc 0 writes to data file
206 ------------------------------------------------------------------------- */
207 
write_data(FILE * fp)208 void AngleCosine::write_data(FILE *fp)
209 {
210   for (int i = 1; i <= atom->nangletypes; i++)
211     fprintf(fp,"%d %g\n",i,k[i]);
212 }
213 
214 /* ---------------------------------------------------------------------- */
215 
single(int type,int i1,int i2,int i3)216 double AngleCosine::single(int type, int i1, int i2, int i3)
217 {
218   double **x = atom->x;
219 
220   double delx1 = x[i1][0] - x[i2][0];
221   double dely1 = x[i1][1] - x[i2][1];
222   double delz1 = x[i1][2] - x[i2][2];
223   domain->minimum_image(delx1,dely1,delz1);
224   double r1 = sqrt(delx1*delx1 + dely1*dely1 + delz1*delz1);
225 
226   double delx2 = x[i3][0] - x[i2][0];
227   double dely2 = x[i3][1] - x[i2][1];
228   double delz2 = x[i3][2] - x[i2][2];
229   domain->minimum_image(delx2,dely2,delz2);
230   double r2 = sqrt(delx2*delx2 + dely2*dely2 + delz2*delz2);
231 
232   double c = delx1*delx2 + dely1*dely2 + delz1*delz2;
233   c /= r1*r2;
234   if (c > 1.0) c = 1.0;
235   if (c < -1.0) c = -1.0;
236 
237   return k[type]*(1.0+c);
238 }
239