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 "omp_compat.h"
20 #include "angle_cosine_delta_omp.h"
21 #include <cmath>
22 #include "atom.h"
23 #include "comm.h"
24 #include "force.h"
25 #include "neighbor.h"
26 
27 
28 #include "suffix.h"
29 using namespace LAMMPS_NS;
30 
31 #define SMALL 0.001
32 
33 /* ---------------------------------------------------------------------- */
34 
AngleCosineDeltaOMP(class LAMMPS * lmp)35 AngleCosineDeltaOMP::AngleCosineDeltaOMP(class LAMMPS *lmp)
36  : AngleCosineDelta(lmp), ThrOMP(lmp,THR_ANGLE)
37 {
38   suffix_flag |= Suffix::OMP;
39 }
40 
41 /* ---------------------------------------------------------------------- */
42 
compute(int eflag,int vflag)43 void AngleCosineDeltaOMP::compute(int eflag, int vflag)
44 {
45   ev_init(eflag,vflag);
46 
47   const int nall = atom->nlocal + atom->nghost;
48   const int nthreads = comm->nthreads;
49   const int inum = neighbor->nanglelist;
50 
51 #if defined(_OPENMP)
52 #pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(eflag,vflag)
53 #endif
54   {
55     int ifrom, ito, tid;
56 
57     loop_setup_thr(ifrom, ito, tid, inum, nthreads);
58     ThrData *thr = fix->get_thr(tid);
59     thr->timer(Timer::START);
60     ev_setup_thr(eflag, vflag, nall, eatom, vatom, cvatom, thr);
61 
62     if (inum > 0) {
63       if (evflag) {
64         if (eflag) {
65           if (force->newton_bond) eval<1,1,1>(ifrom, ito, thr);
66           else eval<1,1,0>(ifrom, ito, thr);
67         } else {
68           if (force->newton_bond) eval<1,0,1>(ifrom, ito, thr);
69           else eval<1,0,0>(ifrom, ito, thr);
70         }
71       } else {
72         if (force->newton_bond) eval<0,0,1>(ifrom, ito, thr);
73         else eval<0,0,0>(ifrom, ito, thr);
74       }
75     }
76     thr->timer(Timer::BOND);
77     reduce_thr(this, eflag, vflag, thr);
78   } // end of omp parallel region
79 }
80 
81 template <int EVFLAG, int EFLAG, int NEWTON_BOND>
eval(int nfrom,int nto,ThrData * const thr)82 void AngleCosineDeltaOMP::eval(int nfrom, int nto, ThrData * const thr)
83 {
84   int i1,i2,i3,n,type;
85   double delx1,dely1,delz1,delx2,dely2,delz2,theta,dtheta,dcostheta,tk;
86   double eangle,f1[3],f3[3];
87   double rsq1,rsq2,r1,r2,c,a,cot,a11,a12,a22,b11,b12,b22,c0,s0,s;
88 
89   const dbl3_t * _noalias const x = (dbl3_t *) atom->x[0];
90   dbl3_t * _noalias const f = (dbl3_t *) thr->get_f()[0];
91   const int4_t * _noalias const anglelist = (int4_t *) neighbor->anglelist[0];
92   const int nlocal = atom->nlocal;
93   eangle = 0.0;
94 
95   for (n = nfrom; n < nto; n++) {
96     i1 = anglelist[n].a;
97     i2 = anglelist[n].b;
98     i3 = anglelist[n].c;
99     type = anglelist[n].t;
100 
101     // 1st bond
102 
103     delx1 = x[i1].x - x[i2].x;
104     dely1 = x[i1].y - x[i2].y;
105     delz1 = x[i1].z - x[i2].z;
106 
107     rsq1 = delx1*delx1 + dely1*dely1 + delz1*delz1;
108     r1 = sqrt(rsq1);
109 
110     // 2nd bond
111 
112     delx2 = x[i3].x - x[i2].x;
113     dely2 = x[i3].y - x[i2].y;
114     delz2 = x[i3].z - x[i2].z;
115 
116     rsq2 = delx2*delx2 + dely2*dely2 + delz2*delz2;
117     r2 = sqrt(rsq2);
118 
119     // angle (cos and sin)
120 
121     c = delx1*delx2 + dely1*dely2 + delz1*delz2;
122     c /= r1*r2;
123 
124     if (c > 1.0) c = 1.0;
125     if (c < -1.0) c = -1.0;
126 
127     theta = acos(c);
128 
129     s = sqrt(1.0 - c*c);
130     if (s < SMALL) s = SMALL;
131     s = 1.0/s;
132 
133     cot = c/s;
134 
135     // force & energy
136 
137     dtheta = theta - theta0[type];
138     dcostheta = cos(dtheta);
139     tk = k[type] * (1.0-dcostheta);
140 
141     if (EFLAG) eangle = tk;
142 
143     a = -k[type];
144 
145     // expand dtheta for cos and sin contribution to force
146 
147     a11 = a*c / rsq1;
148     a12 = -a / (r1*r2);
149     a22 = a*c / rsq2;
150 
151     b11 = -a*c*cot / rsq1;
152     b12 = a*cot / (r1*r2);
153     b22 = -a*c*cot / rsq2;
154 
155     c0 = cos(theta0[type]);
156     s0 = sin(theta0[type]);
157 
158     f1[0] = (a11*delx1 + a12*delx2)*c0 + (b11*delx1 + b12*delx2)*s0;
159     f1[1] = (a11*dely1 + a12*dely2)*c0 + (b11*dely1 + b12*dely2)*s0;
160     f1[2] = (a11*delz1 + a12*delz2)*c0 + (b11*delz1 + b12*delz2)*s0;
161     f3[0] = (a22*delx2 + a12*delx1)*c0 + (b22*delx2 + b12*delx1)*s0;
162     f3[1] = (a22*dely2 + a12*dely1)*c0 + (b22*dely2 + b12*dely1)*s0;
163     f3[2] = (a22*delz2 + a12*delz1)*c0 + (b22*delz2 + b12*delz1)*s0;
164 
165     // apply force to each of 3 atoms
166 
167     if (NEWTON_BOND || i1 < nlocal) {
168       f[i1].x += f1[0];
169       f[i1].y += f1[1];
170       f[i1].z += f1[2];
171     }
172 
173     if (NEWTON_BOND || i2 < nlocal) {
174       f[i2].x -= f1[0] + f3[0];
175       f[i2].y -= f1[1] + f3[1];
176       f[i2].z -= f1[2] + f3[2];
177     }
178 
179     if (NEWTON_BOND || i3 < nlocal) {
180       f[i3].x += f3[0];
181       f[i3].y += f3[1];
182       f[i3].z += f3[2];
183     }
184 
185     if (EVFLAG) ev_tally_thr(this,i1,i2,i3,nlocal,NEWTON_BOND,eangle,f1,f3,
186                              delx1,dely1,delz1,delx2,dely2,delz2,thr);
187   }
188 }
189