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 /* ----------------------------------------------------------------------
16    Contributing author: Axel Kohlmeyer (Temple U)
17 ------------------------------------------------------------------------- */
18 
19 #include "dihedral_quadratic_omp.h"
20 
21 #include "atom.h"
22 #include "comm.h"
23 #include "force.h"
24 #include "math_const.h"
25 #include "neighbor.h"
26 
27 #include <cmath>
28 
29 #include "omp_compat.h"
30 #include "suffix.h"
31 using namespace LAMMPS_NS;
32 using namespace MathConst;
33 
34 #define TOLERANCE 0.05
35 #define SMALL     0.001
36 #define SMALLER   0.00001
37 
38 /* ---------------------------------------------------------------------- */
39 
DihedralQuadraticOMP(class LAMMPS * lmp)40 DihedralQuadraticOMP::DihedralQuadraticOMP(class LAMMPS *lmp)
41   : DihedralQuadratic(lmp), ThrOMP(lmp,THR_DIHEDRAL)
42 {
43   suffix_flag |= Suffix::OMP;
44 }
45 
46 /* ---------------------------------------------------------------------- */
47 
compute(int eflag,int vflag)48 void DihedralQuadraticOMP::compute(int eflag, int vflag)
49 {
50   ev_init(eflag,vflag);
51 
52   const int nall = atom->nlocal + atom->nghost;
53   const int nthreads = comm->nthreads;
54   const int inum = neighbor->ndihedrallist;
55 
56 #if defined(_OPENMP)
57 #pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(eflag,vflag)
58 #endif
59   {
60     int ifrom, ito, tid;
61 
62     loop_setup_thr(ifrom, ito, tid, inum, nthreads);
63     ThrData *thr = fix->get_thr(tid);
64     thr->timer(Timer::START);
65     ev_setup_thr(eflag, vflag, nall, eatom, vatom, cvatom, thr);
66 
67     if (inum > 0) {
68       if (evflag) {
69         if (eflag) {
70           if (force->newton_bond) eval<1,1,1>(ifrom, ito, thr);
71           else eval<1,1,0>(ifrom, ito, thr);
72         } else {
73           if (force->newton_bond) eval<1,0,1>(ifrom, ito, thr);
74           else eval<1,0,0>(ifrom, ito, thr);
75         }
76       } else {
77         if (force->newton_bond) eval<0,0,1>(ifrom, ito, thr);
78         else eval<0,0,0>(ifrom, ito, thr);
79       }
80     }
81     thr->timer(Timer::BOND);
82     reduce_thr(this, eflag, vflag, thr);
83   } // end of omp parallel region
84 }
85 
86 template <int EVFLAG, int EFLAG, int NEWTON_BOND>
eval(int nfrom,int nto,ThrData * const thr)87 void DihedralQuadraticOMP::eval(int nfrom, int nto, ThrData * const thr)
88 {
89   int i1,i2,i3,i4,n,type;
90   double vb1x,vb1y,vb1z,vb2x,vb2y,vb2z,vb3x,vb3y,vb3z,vb2xm,vb2ym,vb2zm;
91   double edihedral,f1[3],f2[3],f3[3],f4[3];
92   double sb1,sb2,sb3,rb1,rb3,c0,b1mag2,b1mag,b2mag2;
93   double b2mag,b3mag2,b3mag,ctmp,r12c1,c1mag,r12c2;
94   double c2mag,sc1,sc2,s1,s12,c,p,pd,a,a11,a22;
95   double a33,a12,a13,a23,sx2,sy2,sz2;
96   double s2,cx,cy,cz,cmag,dx,phi,si,siinv,sin2;
97 
98   edihedral = 0.0;
99 
100   const dbl3_t * _noalias const x = (dbl3_t *) atom->x[0];
101   dbl3_t * _noalias const f = (dbl3_t *) thr->get_f()[0];
102   const int5_t * _noalias const dihedrallist = (int5_t *) neighbor->dihedrallist[0];
103   const int nlocal = atom->nlocal;
104 
105   for (n = nfrom; n < nto; n++) {
106     i1 = dihedrallist[n].a;
107     i2 = dihedrallist[n].b;
108     i3 = dihedrallist[n].c;
109     i4 = dihedrallist[n].d;
110     type = dihedrallist[n].t;
111 
112     // 1st bond
113 
114     vb1x = x[i1].x - x[i2].x;
115     vb1y = x[i1].y - x[i2].y;
116     vb1z = x[i1].z - x[i2].z;
117 
118     // 2nd bond
119 
120     vb2x = x[i3].x - x[i2].x;
121     vb2y = x[i3].y - x[i2].y;
122     vb2z = x[i3].z - x[i2].z;
123 
124     vb2xm = -vb2x;
125     vb2ym = -vb2y;
126     vb2zm = -vb2z;
127 
128     // 3rd bond
129 
130     vb3x = x[i4].x - x[i3].x;
131     vb3y = x[i4].y - x[i3].y;
132     vb3z = x[i4].z - x[i3].z;
133 
134     // c0 calculation
135 
136     sb1 = 1.0 / (vb1x*vb1x + vb1y*vb1y + vb1z*vb1z);
137     sb2 = 1.0 / (vb2x*vb2x + vb2y*vb2y + vb2z*vb2z);
138     sb3 = 1.0 / (vb3x*vb3x + vb3y*vb3y + vb3z*vb3z);
139 
140     rb1 = sqrt(sb1);
141     rb3 = sqrt(sb3);
142 
143     c0 = (vb1x*vb3x + vb1y*vb3y + vb1z*vb3z) * rb1*rb3;
144 
145     // 1st and 2nd angle
146 
147     b1mag2 = vb1x*vb1x + vb1y*vb1y + vb1z*vb1z;
148     b1mag = sqrt(b1mag2);
149     b2mag2 = vb2x*vb2x + vb2y*vb2y + vb2z*vb2z;
150     b2mag = sqrt(b2mag2);
151     b3mag2 = vb3x*vb3x + vb3y*vb3y + vb3z*vb3z;
152     b3mag = sqrt(b3mag2);
153 
154     ctmp = vb1x*vb2x + vb1y*vb2y + vb1z*vb2z;
155     r12c1 = 1.0 / (b1mag*b2mag);
156     c1mag = ctmp * r12c1;
157 
158     ctmp = vb2xm*vb3x + vb2ym*vb3y + vb2zm*vb3z;
159     r12c2 = 1.0 / (b2mag*b3mag);
160     c2mag = ctmp * r12c2;
161 
162     // cos and sin of 2 angles and final c
163 
164     sin2 = MAX(1.0 - c1mag*c1mag,0.0);
165     sc1 = sqrt(sin2);
166     if (sc1 < SMALL) sc1 = SMALL;
167     sc1 = 1.0/sc1;
168 
169     sin2 = MAX(1.0 - c2mag*c2mag,0.0);
170     sc2 = sqrt(sin2);
171     if (sc2 < SMALL) sc2 = SMALL;
172     sc2 = 1.0/sc2;
173 
174     s1 = sc1 * sc1;
175     s2 = sc2 * sc2;
176     s12 = sc1 * sc2;
177     c = (c0 + c1mag*c2mag) * s12;
178 
179     cx = vb1y*vb2z - vb1z*vb2y;
180     cy = vb1z*vb2x - vb1x*vb2z;
181     cz = vb1x*vb2y - vb1y*vb2x;
182     cmag = sqrt(cx*cx + cy*cy + cz*cz);
183     dx = (cx*vb3x + cy*vb3y + cz*vb3z)/cmag/b3mag;
184 
185     // error check
186 
187     if (c > 1.0 + TOLERANCE || c < (-1.0 - TOLERANCE))
188       problem(FLERR, i1, i2, i3, i4);
189 
190     if (c > 1.0) c = 1.0;
191     if (c < -1.0) c = -1.0;
192 
193     // force & energy
194     // p = k ( phi- phi0)^2
195     // pd = dp/dc
196 
197     phi = acos(c);
198     if (dx > 0.0) phi *= -1.0;
199     si = sin(phi);
200     if (fabs(si) < SMALLER) si = SMALLER;
201     siinv = 1.0/si;
202 
203     double dphi = phi-phi0[type];
204     if (dphi > MY_PI) dphi -= 2*MY_PI;
205     else if (dphi < -MY_PI) dphi += 2*MY_PI;
206     p = k[type]*dphi;
207     pd = - 2.0 * p * siinv;
208     p = p * dphi;
209 
210     if (EFLAG) edihedral = p;
211 
212     a = pd;
213     c = c * a;
214     s12 = s12 * a;
215     a11 = c*sb1*s1;
216     a22 = -sb2 * (2.0*c0*s12 - c*(s1+s2));
217     a33 = c*sb3*s2;
218     a12 = -r12c1 * (c1mag*c*s1 + c2mag*s12);
219     a13 = -rb1*rb3*s12;
220     a23 = r12c2 * (c2mag*c*s2 + c1mag*s12);
221 
222     sx2  = a12*vb1x + a22*vb2x + a23*vb3x;
223     sy2  = a12*vb1y + a22*vb2y + a23*vb3y;
224     sz2  = a12*vb1z + a22*vb2z + a23*vb3z;
225 
226     f1[0] = a11*vb1x + a12*vb2x + a13*vb3x;
227     f1[1] = a11*vb1y + a12*vb2y + a13*vb3y;
228     f1[2] = a11*vb1z + a12*vb2z + a13*vb3z;
229 
230     f2[0] = -sx2 - f1[0];
231     f2[1] = -sy2 - f1[1];
232     f2[2] = -sz2 - f1[2];
233 
234     f4[0] = a13*vb1x + a23*vb2x + a33*vb3x;
235     f4[1] = a13*vb1y + a23*vb2y + a33*vb3y;
236     f4[2] = a13*vb1z + a23*vb2z + a33*vb3z;
237 
238     f3[0] = sx2 - f4[0];
239     f3[1] = sy2 - f4[1];
240     f3[2] = sz2 - f4[2];
241 
242     // apply force to each of 4 atoms
243 
244     if (NEWTON_BOND || i1 < nlocal) {
245       f[i1].x += f1[0];
246       f[i1].y += f1[1];
247       f[i1].z += f1[2];
248     }
249 
250     if (NEWTON_BOND || i2 < nlocal) {
251       f[i2].x += f2[0];
252       f[i2].y += f2[1];
253       f[i2].z += f2[2];
254     }
255 
256     if (NEWTON_BOND || i3 < nlocal) {
257       f[i3].x += f3[0];
258       f[i3].y += f3[1];
259       f[i3].z += f3[2];
260     }
261 
262     if (NEWTON_BOND || i4 < nlocal) {
263       f[i4].x += f4[0];
264       f[i4].y += f4[1];
265       f[i4].z += f4[2];
266     }
267 
268     if (EVFLAG)
269       ev_tally_thr(this,i1,i2,i3,i4,nlocal,NEWTON_BOND,edihedral,f1,f3,f4,
270                    vb1x,vb1y,vb1z,vb2x,vb2y,vb2z,vb3x,vb3y,vb3z,thr);
271   }
272 }
273