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: Stan Moore (SNL)
17 ------------------------------------------------------------------------- */
18
19 #include "angle_cosine_kokkos.h"
20
21 #include "atom_kokkos.h"
22 #include "atom_masks.h"
23 #include "comm.h"
24 #include "force.h"
25 #include "math_const.h"
26 #include "memory_kokkos.h"
27 #include "neighbor_kokkos.h"
28
29 #include <cmath>
30
31 using namespace LAMMPS_NS;
32 using namespace MathConst;
33
34 #define SMALL 0.001
35
36 /* ---------------------------------------------------------------------- */
37
38 template<class DeviceType>
AngleCosineKokkos(LAMMPS * lmp)39 AngleCosineKokkos<DeviceType>::AngleCosineKokkos(LAMMPS *lmp) : AngleCosine(lmp)
40 {
41 atomKK = (AtomKokkos *) atom;
42 neighborKK = (NeighborKokkos *) neighbor;
43 execution_space = ExecutionSpaceFromDevice<DeviceType>::space;
44 datamask_read = X_MASK | F_MASK | ENERGY_MASK | VIRIAL_MASK;
45 datamask_modify = F_MASK | ENERGY_MASK | VIRIAL_MASK;
46
47 centroidstressflag = CENTROID_NOTAVAIL;
48 }
49
50 /* ---------------------------------------------------------------------- */
51
52 template<class DeviceType>
~AngleCosineKokkos()53 AngleCosineKokkos<DeviceType>::~AngleCosineKokkos()
54 {
55 if (!copymode) {
56 memoryKK->destroy_kokkos(k_eatom,eatom);
57 memoryKK->destroy_kokkos(k_vatom,vatom);
58 }
59 }
60
61 /* ---------------------------------------------------------------------- */
62
63 template<class DeviceType>
compute(int eflag_in,int vflag_in)64 void AngleCosineKokkos<DeviceType>::compute(int eflag_in, int vflag_in)
65 {
66 eflag = eflag_in;
67 vflag = vflag_in;
68
69 ev_init(eflag,vflag,0);
70
71 // reallocate per-atom arrays if necessary
72
73 if (eflag_atom) {
74 memoryKK->destroy_kokkos(k_eatom,eatom);
75 memoryKK->create_kokkos(k_eatom,eatom,maxeatom,"angle:eatom");
76 d_eatom = k_eatom.template view<DeviceType>();
77 }
78 if (vflag_atom) {
79 memoryKK->destroy_kokkos(k_vatom,vatom);
80 memoryKK->create_kokkos(k_vatom,vatom,maxvatom,"angle:vatom");
81 d_vatom = k_vatom.template view<DeviceType>();
82 }
83
84 //atomKK->sync(execution_space,datamask_read);
85 k_k.template sync<DeviceType>();
86 // if (eflag || vflag) atomKK->modified(execution_space,datamask_modify);
87 // else atomKK->modified(execution_space,F_MASK);
88
89 x = atomKK->k_x.template view<DeviceType>();
90 f = atomKK->k_f.template view<DeviceType>();
91 neighborKK->k_anglelist.template sync<DeviceType>();
92 anglelist = neighborKK->k_anglelist.template view<DeviceType>();
93 int nanglelist = neighborKK->nanglelist;
94 nlocal = atom->nlocal;
95 newton_bond = force->newton_bond;
96
97 copymode = 1;
98
99 // loop over neighbors of my atoms
100
101 EV_FLOAT ev;
102
103 if (evflag) {
104 if (newton_bond) {
105 Kokkos::parallel_reduce(Kokkos::RangePolicy<DeviceType, TagAngleCosineCompute<1,1> >(0,nanglelist),*this,ev);
106 } else {
107 Kokkos::parallel_reduce(Kokkos::RangePolicy<DeviceType, TagAngleCosineCompute<0,1> >(0,nanglelist),*this,ev);
108 }
109 } else {
110 if (newton_bond) {
111 Kokkos::parallel_for(Kokkos::RangePolicy<DeviceType, TagAngleCosineCompute<1,0> >(0,nanglelist),*this);
112 } else {
113 Kokkos::parallel_for(Kokkos::RangePolicy<DeviceType, TagAngleCosineCompute<0,0> >(0,nanglelist),*this);
114 }
115 }
116
117 if (eflag_global) energy += ev.evdwl;
118 if (vflag_global) {
119 virial[0] += ev.v[0];
120 virial[1] += ev.v[1];
121 virial[2] += ev.v[2];
122 virial[3] += ev.v[3];
123 virial[4] += ev.v[4];
124 virial[5] += ev.v[5];
125 }
126
127 if (eflag_atom) {
128 k_eatom.template modify<DeviceType>();
129 k_eatom.template sync<LMPHostType>();
130 }
131
132 if (vflag_atom) {
133 k_vatom.template modify<DeviceType>();
134 k_vatom.template sync<LMPHostType>();
135 }
136
137 copymode = 0;
138 }
139
140 template<class DeviceType>
141 template<int NEWTON_BOND, int EVFLAG>
142 KOKKOS_INLINE_FUNCTION
operator ()(TagAngleCosineCompute<NEWTON_BOND,EVFLAG>,const int & n,EV_FLOAT & ev) const143 void AngleCosineKokkos<DeviceType>::operator()(TagAngleCosineCompute<NEWTON_BOND,EVFLAG>, const int &n, EV_FLOAT& ev) const {
144
145 // The f array is atomic
146 Kokkos::View<F_FLOAT*[3], typename DAT::t_f_array::array_layout,typename KKDevice<DeviceType>::value,Kokkos::MemoryTraits<Kokkos::Atomic|Kokkos::Unmanaged> > a_f = f;
147
148 const int i1 = anglelist(n,0);
149 const int i2 = anglelist(n,1);
150 const int i3 = anglelist(n,2);
151 const int type = anglelist(n,3);
152
153 // 1st bond
154
155 const F_FLOAT delx1 = x(i1,0) - x(i2,0);
156 const F_FLOAT dely1 = x(i1,1) - x(i2,1);
157 const F_FLOAT delz1 = x(i1,2) - x(i2,2);
158
159 const F_FLOAT rsq1 = delx1*delx1 + dely1*dely1 + delz1*delz1;
160 const F_FLOAT r1 = sqrt(rsq1);
161
162 // 2nd bond
163
164 const F_FLOAT delx2 = x(i3,0) - x(i2,0);
165 const F_FLOAT dely2 = x(i3,1) - x(i2,1);
166 const F_FLOAT delz2 = x(i3,2) - x(i2,2);
167
168 const F_FLOAT rsq2 = delx2*delx2 + dely2*dely2 + delz2*delz2;
169 const F_FLOAT r2 = sqrt(rsq2);
170
171 // c = cosine of angle
172
173 F_FLOAT c = delx1*delx2 + dely1*dely2 + delz1*delz2;
174 c /= r1*r2;
175 if (c > 1.0) c = 1.0;
176 if (c < -1.0) c = -1.0;
177
178 // force & energy
179
180 F_FLOAT eangle = 0.0;
181 if (eflag) eangle = d_k[type]*(1.0+c);
182
183 const F_FLOAT a = d_k[type];
184 const F_FLOAT a11 = a*c / rsq1;
185 const F_FLOAT a12 = -a / (r1*r2);
186 const F_FLOAT a22 = a*c / rsq2;
187
188 F_FLOAT f1[3],f3[3];
189 f1[0] = a11*delx1 + a12*delx2;
190 f1[1] = a11*dely1 + a12*dely2;
191 f1[2] = a11*delz1 + a12*delz2;
192 f3[0] = a22*delx2 + a12*delx1;
193 f3[1] = a22*dely2 + a12*dely1;
194 f3[2] = a22*delz2 + a12*delz1;
195
196 // apply force to each of 3 atoms
197
198 if (NEWTON_BOND || i1 < nlocal) {
199 a_f(i1,0) += f1[0];
200 a_f(i1,1) += f1[1];
201 a_f(i1,2) += f1[2];
202 }
203
204 if (NEWTON_BOND || i2 < nlocal) {
205 a_f(i2,0) -= f1[0] + f3[0];
206 a_f(i2,1) -= f1[1] + f3[1];
207 a_f(i2,2) -= f1[2] + f3[2];
208 }
209
210 if (NEWTON_BOND || i3 < nlocal) {
211 a_f(i3,0) += f3[0];
212 a_f(i3,1) += f3[1];
213 a_f(i3,2) += f3[2];
214 }
215
216 if (EVFLAG) ev_tally(ev,i1,i2,i3,eangle,f1,f3,
217 delx1,dely1,delz1,delx2,dely2,delz2);
218 }
219
220 template<class DeviceType>
221 template<int NEWTON_BOND, int EVFLAG>
222 KOKKOS_INLINE_FUNCTION
operator ()(TagAngleCosineCompute<NEWTON_BOND,EVFLAG>,const int & n) const223 void AngleCosineKokkos<DeviceType>::operator()(TagAngleCosineCompute<NEWTON_BOND,EVFLAG>, const int &n) const {
224 EV_FLOAT ev;
225 this->template operator()<NEWTON_BOND,EVFLAG>(TagAngleCosineCompute<NEWTON_BOND,EVFLAG>(), n, ev);
226 }
227
228 /* ---------------------------------------------------------------------- */
229
230 template<class DeviceType>
allocate()231 void AngleCosineKokkos<DeviceType>::allocate()
232 {
233 AngleCosine::allocate();
234
235 int n = atom->nangletypes;
236 k_k = typename ArrayTypes<DeviceType>::tdual_ffloat_1d("AngleCosine::k",n+1);
237 d_k = k_k.template view<DeviceType>();
238 }
239
240 /* ----------------------------------------------------------------------
241 set coeffs for one or more types
242 ------------------------------------------------------------------------- */
243
244 template<class DeviceType>
coeff(int narg,char ** arg)245 void AngleCosineKokkos<DeviceType>::coeff(int narg, char **arg)
246 {
247 AngleCosine::coeff(narg, arg);
248
249 int n = atom->nangletypes;
250 for (int i = 1; i <= n; i++)
251 k_k.h_view[i] = k[i];
252
253 k_k.template modify<LMPHostType>();
254 }
255
256 /* ----------------------------------------------------------------------
257 proc 0 reads coeffs from restart file, bcasts them
258 ------------------------------------------------------------------------- */
259
260 template<class DeviceType>
read_restart(FILE * fp)261 void AngleCosineKokkos<DeviceType>::read_restart(FILE *fp)
262 {
263 AngleCosine::read_restart(fp);
264
265 int n = atom->nangletypes;
266 for (int i = 1; i <= n; i++)
267 k_k.h_view[i] = k[i];
268
269 k_k.template modify<LMPHostType>();
270 }
271
272 /* ----------------------------------------------------------------------
273 tally energy and virial into global and per-atom accumulators
274 virial = r1F1 + r2F2 + r3F3 = (r1-r2) F1 + (r3-r2) F3 = del1*f1 + del2*f3
275 ------------------------------------------------------------------------- */
276
277 template<class DeviceType>
278 //template<int NEWTON_BOND>
279 KOKKOS_INLINE_FUNCTION
ev_tally(EV_FLOAT & ev,const int i,const int j,const int k,F_FLOAT & eangle,F_FLOAT * f1,F_FLOAT * f3,const F_FLOAT & delx1,const F_FLOAT & dely1,const F_FLOAT & delz1,const F_FLOAT & delx2,const F_FLOAT & dely2,const F_FLOAT & delz2) const280 void AngleCosineKokkos<DeviceType>::ev_tally(EV_FLOAT &ev, const int i, const int j, const int k,
281 F_FLOAT &eangle, F_FLOAT *f1, F_FLOAT *f3,
282 const F_FLOAT &delx1, const F_FLOAT &dely1, const F_FLOAT &delz1,
283 const F_FLOAT &delx2, const F_FLOAT &dely2, const F_FLOAT &delz2) const
284 {
285 E_FLOAT eanglethird;
286 F_FLOAT v[6];
287
288 // The eatom and vatom arrays are atomic
289 Kokkos::View<E_FLOAT*, typename DAT::t_efloat_1d::array_layout,typename KKDevice<DeviceType>::value,Kokkos::MemoryTraits<Kokkos::Atomic|Kokkos::Unmanaged> > v_eatom = k_eatom.template view<DeviceType>();
290 Kokkos::View<F_FLOAT*[6], typename DAT::t_virial_array::array_layout,typename KKDevice<DeviceType>::value,Kokkos::MemoryTraits<Kokkos::Atomic|Kokkos::Unmanaged> > v_vatom = k_vatom.template view<DeviceType>();
291
292 if (eflag_either) {
293 if (eflag_global) {
294 if (newton_bond) ev.evdwl += eangle;
295 else {
296 eanglethird = THIRD*eangle;
297
298 if (i < nlocal) ev.evdwl += eanglethird;
299 if (j < nlocal) ev.evdwl += eanglethird;
300 if (k < nlocal) ev.evdwl += eanglethird;
301 }
302 }
303 if (eflag_atom) {
304 eanglethird = THIRD*eangle;
305
306 if (newton_bond || i < nlocal) v_eatom[i] += eanglethird;
307 if (newton_bond || j < nlocal) v_eatom[j] += eanglethird;
308 if (newton_bond || k < nlocal) v_eatom[k] += eanglethird;
309 }
310 }
311
312 if (vflag_either) {
313 v[0] = delx1*f1[0] + delx2*f3[0];
314 v[1] = dely1*f1[1] + dely2*f3[1];
315 v[2] = delz1*f1[2] + delz2*f3[2];
316 v[3] = delx1*f1[1] + delx2*f3[1];
317 v[4] = delx1*f1[2] + delx2*f3[2];
318 v[5] = dely1*f1[2] + dely2*f3[2];
319
320 if (vflag_global) {
321 if (newton_bond) {
322 ev.v[0] += v[0];
323 ev.v[1] += v[1];
324 ev.v[2] += v[2];
325 ev.v[3] += v[3];
326 ev.v[4] += v[4];
327 ev.v[5] += v[5];
328 } else {
329 if (i < nlocal) {
330 ev.v[0] += THIRD*v[0];
331 ev.v[1] += THIRD*v[1];
332 ev.v[2] += THIRD*v[2];
333 ev.v[3] += THIRD*v[3];
334 ev.v[4] += THIRD*v[4];
335 ev.v[5] += THIRD*v[5];
336 }
337 if (j < nlocal) {
338 ev.v[0] += THIRD*v[0];
339 ev.v[1] += THIRD*v[1];
340 ev.v[2] += THIRD*v[2];
341 ev.v[3] += THIRD*v[3];
342 ev.v[4] += THIRD*v[4];
343 ev.v[5] += THIRD*v[5];
344 }
345 if (k < nlocal) {
346 ev.v[0] += THIRD*v[0];
347
348 ev.v[1] += THIRD*v[1];
349 ev.v[2] += THIRD*v[2];
350 ev.v[3] += THIRD*v[3];
351 ev.v[4] += THIRD*v[4];
352 ev.v[5] += THIRD*v[5];
353 }
354 }
355 }
356
357 if (vflag_atom) {
358 if (newton_bond || i < nlocal) {
359 v_vatom(i,0) += THIRD*v[0];
360 v_vatom(i,1) += THIRD*v[1];
361 v_vatom(i,2) += THIRD*v[2];
362 v_vatom(i,3) += THIRD*v[3];
363 v_vatom(i,4) += THIRD*v[4];
364 v_vatom(i,5) += THIRD*v[5];
365 }
366 if (newton_bond || j < nlocal) {
367 v_vatom(j,0) += THIRD*v[0];
368 v_vatom(j,1) += THIRD*v[1];
369 v_vatom(j,2) += THIRD*v[2];
370 v_vatom(j,3) += THIRD*v[3];
371 v_vatom(j,4) += THIRD*v[4];
372 v_vatom(j,5) += THIRD*v[5];
373 }
374 if (newton_bond || k < nlocal) {
375 v_vatom(k,0) += THIRD*v[0];
376 v_vatom(k,1) += THIRD*v[1];
377 v_vatom(k,2) += THIRD*v[2];
378 v_vatom(k,3) += THIRD*v[3];
379 v_vatom(k,4) += THIRD*v[4];
380 v_vatom(k,5) += THIRD*v[5];
381
382 }
383 }
384 }
385 }
386
387 /* ---------------------------------------------------------------------- */
388
389 namespace LAMMPS_NS {
390 template class AngleCosineKokkos<LMPDeviceType>;
391 #ifdef LMP_KOKKOS_GPU
392 template class AngleCosineKokkos<LMPHostType>;
393 #endif
394 }
395
396