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_cosine_shift_exp_omp.h"
20
21 #include "atom.h"
22 #include "comm.h"
23 #include "force.h"
24 #include "neighbor.h"
25
26 #include <cmath>
27
28 #include "omp_compat.h"
29 #include "suffix.h"
30 using namespace LAMMPS_NS;
31
32 #define TOLERANCE 0.05
33 #define SMALL 0.001
34
35 /* ---------------------------------------------------------------------- */
36
DihedralCosineShiftExpOMP(class LAMMPS * lmp)37 DihedralCosineShiftExpOMP::DihedralCosineShiftExpOMP(class LAMMPS *lmp)
38 : DihedralCosineShiftExp(lmp), ThrOMP(lmp,THR_DIHEDRAL)
39 {
40 suffix_flag |= Suffix::OMP;
41 }
42
43 /* ---------------------------------------------------------------------- */
44
compute(int eflag,int vflag)45 void DihedralCosineShiftExpOMP::compute(int eflag, int vflag)
46 {
47 ev_init(eflag,vflag);
48
49 const int nall = atom->nlocal + atom->nghost;
50 const int nthreads = comm->nthreads;
51 const int inum = neighbor->ndihedrallist;
52
53 #if defined(_OPENMP)
54 #pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(eflag,vflag)
55 #endif
56 {
57 int ifrom, ito, tid;
58
59 loop_setup_thr(ifrom, ito, tid, inum, nthreads);
60 ThrData *thr = fix->get_thr(tid);
61 thr->timer(Timer::START);
62 ev_setup_thr(eflag, vflag, nall, eatom, vatom, cvatom, thr);
63
64 if (inum > 0) {
65 if (evflag) {
66 if (eflag) {
67 if (force->newton_bond) eval<1,1,1>(ifrom, ito, thr);
68 else eval<1,1,0>(ifrom, ito, thr);
69 } else {
70 if (force->newton_bond) eval<1,0,1>(ifrom, ito, thr);
71 else eval<1,0,0>(ifrom, ito, thr);
72 }
73 } else {
74 if (force->newton_bond) eval<0,0,1>(ifrom, ito, thr);
75 else eval<0,0,0>(ifrom, ito, thr);
76 }
77 }
78 thr->timer(Timer::BOND);
79 reduce_thr(this, eflag, vflag, thr);
80 } // end of omp parallel region
81 }
82
83 template <int EVFLAG, int EFLAG, int NEWTON_BOND>
eval(int nfrom,int nto,ThrData * const thr)84 void DihedralCosineShiftExpOMP::eval(int nfrom, int nto, ThrData * const thr)
85 {
86
87 int i1,i2,i3,i4,n,type;
88 double vb1x,vb1y,vb1z,vb2x,vb2y,vb2z,vb3x,vb3y,vb3z,vb2xm,vb2ym,vb2zm;
89 double edihedral,f1[3],f2[3],f3[3],f4[3];
90 double ax,ay,az,bx,by,bz,rasq,rbsq,rgsq,rg,rginv,ra2inv,rb2inv,rabinv;
91 double df,fg,hg,fga,hgb,gaa,gbb;
92 double dtfx,dtfy,dtfz,dtgx,dtgy,dtgz,dthx,dthy,dthz;
93 double c,s,sx2,sy2,sz2;
94 double cccpsss,cssmscc,exp2;
95
96 edihedral = 0.0;
97
98 const dbl3_t * _noalias const x = (dbl3_t *) atom->x[0];
99 dbl3_t * _noalias const f = (dbl3_t *) thr->get_f()[0];
100 const int5_t * _noalias const dihedrallist = (int5_t *) neighbor->dihedrallist[0];
101 const int nlocal = atom->nlocal;
102
103 for (n = nfrom; n < nto; n++) {
104 i1 = dihedrallist[n].a;
105 i2 = dihedrallist[n].b;
106 i3 = dihedrallist[n].c;
107 i4 = dihedrallist[n].d;
108 type = dihedrallist[n].t;
109
110 // 1st bond
111
112 vb1x = x[i1].x - x[i2].x;
113 vb1y = x[i1].y - x[i2].y;
114 vb1z = x[i1].z - x[i2].z;
115
116 // 2nd bond
117
118 vb2x = x[i3].x - x[i2].x;
119 vb2y = x[i3].y - x[i2].y;
120 vb2z = x[i3].z - x[i2].z;
121
122 vb2xm = -vb2x;
123 vb2ym = -vb2y;
124 vb2zm = -vb2z;
125
126 // 3rd bond
127
128 vb3x = x[i4].x - x[i3].x;
129 vb3y = x[i4].y - x[i3].y;
130 vb3z = x[i4].z - x[i3].z;
131
132 // c,s calculation
133
134 ax = vb1y*vb2zm - vb1z*vb2ym;
135 ay = vb1z*vb2xm - vb1x*vb2zm;
136 az = vb1x*vb2ym - vb1y*vb2xm;
137 bx = vb3y*vb2zm - vb3z*vb2ym;
138 by = vb3z*vb2xm - vb3x*vb2zm;
139 bz = vb3x*vb2ym - vb3y*vb2xm;
140
141 rasq = ax*ax + ay*ay + az*az;
142 rbsq = bx*bx + by*by + bz*bz;
143 rgsq = vb2xm*vb2xm + vb2ym*vb2ym + vb2zm*vb2zm;
144 rg = sqrt(rgsq);
145
146 rginv = ra2inv = rb2inv = 0.0;
147 if (rg > 0) rginv = 1.0/rg;
148 if (rasq > 0) ra2inv = 1.0/rasq;
149 if (rbsq > 0) rb2inv = 1.0/rbsq;
150 rabinv = sqrt(ra2inv*rb2inv);
151
152 c = (ax*bx + ay*by + az*bz)*rabinv;
153 s = rg*rabinv*(ax*vb3x + ay*vb3y + az*vb3z);
154
155 // error check
156
157 if (c > 1.0 + TOLERANCE || c < (-1.0 - TOLERANCE))
158 problem(FLERR, i1, i2, i3, i4);
159
160 if (c > 1.0) c = 1.0;
161 if (c < -1.0) c = -1.0;
162
163 double aa=a[type];
164 double uumin=umin[type];
165
166 cccpsss = c*cost[type]+s*sint[type];
167 cssmscc = c*sint[type]-s*cost[type];
168
169 if (doExpansion[type]) {
170 // |a|<0.001 so use expansions relative precision <1e-5
171 if (EFLAG) edihedral = -0.125*(1+cccpsss)*(4+aa*(cccpsss-1))*uumin;
172 df=0.5*uumin*( cssmscc + 0.5*aa*cccpsss);
173 } else {
174 exp2=exp(0.5*aa*(1+cccpsss));
175 if (EFLAG) edihedral = opt1[type]*(1-exp2);
176 df= 0.5*opt1[type]*aa* ( exp2*cssmscc );
177 }
178
179 fg = vb1x*vb2xm + vb1y*vb2ym + vb1z*vb2zm;
180 hg = vb3x*vb2xm + vb3y*vb2ym + vb3z*vb2zm;
181 fga = fg*ra2inv*rginv;
182 hgb = hg*rb2inv*rginv;
183 gaa = -ra2inv*rg;
184 gbb = rb2inv*rg;
185
186 dtfx = gaa*ax;
187 dtfy = gaa*ay;
188 dtfz = gaa*az;
189 dtgx = fga*ax - hgb*bx;
190 dtgy = fga*ay - hgb*by;
191 dtgz = fga*az - hgb*bz;
192 dthx = gbb*bx;
193 dthy = gbb*by;
194 dthz = gbb*bz;
195
196 sx2 = df*dtgx;
197 sy2 = df*dtgy;
198 sz2 = df*dtgz;
199
200 f1[0] = df*dtfx;
201 f1[1] = df*dtfy;
202 f1[2] = df*dtfz;
203
204 f2[0] = sx2 - f1[0];
205 f2[1] = sy2 - f1[1];
206 f2[2] = sz2 - f1[2];
207
208 f4[0] = df*dthx;
209 f4[1] = df*dthy;
210 f4[2] = df*dthz;
211
212 f3[0] = -sx2 - f4[0];
213 f3[1] = -sy2 - f4[1];
214 f3[2] = -sz2 - f4[2];
215
216 // apply force to each of 4 atoms
217
218 if (NEWTON_BOND || i1 < nlocal) {
219 f[i1].x += f1[0];
220 f[i1].y += f1[1];
221 f[i1].z += f1[2];
222 }
223
224 if (NEWTON_BOND || i2 < nlocal) {
225 f[i2].x += f2[0];
226 f[i2].y += f2[1];
227 f[i2].z += f2[2];
228 }
229
230 if (NEWTON_BOND || i3 < nlocal) {
231 f[i3].x += f3[0];
232 f[i3].y += f3[1];
233 f[i3].z += f3[2];
234 }
235
236 if (NEWTON_BOND || i4 < nlocal) {
237 f[i4].x += f4[0];
238 f[i4].y += f4[1];
239 f[i4].z += f4[2];
240 }
241
242 if (EVFLAG)
243 ev_tally_thr(this,i1,i2,i3,i4,nlocal,NEWTON_BOND,edihedral,f1,f3,f4,
244 vb1x,vb1y,vb1z,vb2x,vb2y,vb2z,vb3x,vb3y,vb3z,thr);
245 }
246 }
247