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: Mike Brown (SNL)
17 ------------------------------------------------------------------------- */
18
19 #include "pair_lj_charmm_coul_long_gpu.h"
20
21 #include "atom.h"
22 #include "domain.h"
23 #include "error.h"
24 #include "force.h"
25 #include "gpu_extra.h"
26 #include "kspace.h"
27 #include "neigh_list.h"
28 #include "neigh_request.h"
29 #include "neighbor.h"
30 #include "suffix.h"
31
32 #include <cmath>
33
34 #define EWALD_F 1.12837917
35 #define EWALD_P 0.3275911
36 #define A1 0.254829592
37 #define A2 -0.284496736
38 #define A3 1.421413741
39 #define A4 -1.453152027
40 #define A5 1.061405429
41
42 using namespace LAMMPS_NS;
43
44 // External functions from cuda library for atom decomposition
45
46 int crml_gpu_init(const int ntypes, double cut_bothsq, double **host_lj1,
47 double **host_lj2, double **host_lj3, double **host_lj4,
48 double **offset, double *special_lj, const int nlocal,
49 const int nall, const int max_nbors, const int maxspecial,
50 const double cell_size, int &gpu_mode, FILE *screen,
51 double host_cut_ljsq, double host_cut_coulsq,
52 double *host_special_coul, const double qqrd2e,
53 const double g_ewald, const double cut_lj_innersq,
54 const double denom_lj, double **epsilon, double **sigma,
55 const bool mix_arithmetic);
56 void crml_gpu_clear();
57 int ** crml_gpu_compute_n(const int ago, const int inum,
58 const int nall, double **host_x, int *host_type,
59 double *sublo, double *subhi, tagint *tag,
60 int **nspecial, tagint **special, const bool eflag,
61 const bool vflag, const bool eatom, const bool vatom,
62 int &host_start, int **ilist, int **jnum,
63 const double cpu_time, bool &success, double *host_q,
64 double *boxlo, double *prd);
65 void crml_gpu_compute(const int ago, const int inum, const int nall,
66 double **host_x, int *host_type, int *ilist, int *numj,
67 int **firstneigh, const bool eflag, const bool vflag,
68 const bool eatom, const bool vatom, int &host_start,
69 const double cpu_time, bool &success, double *host_q,
70 const int nlocal, double *boxlo, double *prd);
71 double crml_gpu_bytes();
72
73 /* ---------------------------------------------------------------------- */
74
PairLJCharmmCoulLongGPU(LAMMPS * lmp)75 PairLJCharmmCoulLongGPU::PairLJCharmmCoulLongGPU(LAMMPS *lmp) :
76 PairLJCharmmCoulLong(lmp), gpu_mode(GPU_FORCE)
77 {
78 respa_enable = 0;
79 reinitflag = 0;
80 cpu_time = 0.0;
81 suffix_flag |= Suffix::GPU;
82 GPU_EXTRA::gpu_ready(lmp->modify, lmp->error);
83 }
84
85 /* ----------------------------------------------------------------------
86 free all arrays
87 ------------------------------------------------------------------------- */
88
~PairLJCharmmCoulLongGPU()89 PairLJCharmmCoulLongGPU::~PairLJCharmmCoulLongGPU()
90 {
91 crml_gpu_clear();
92 }
93
94 /* ---------------------------------------------------------------------- */
95
compute(int eflag,int vflag)96 void PairLJCharmmCoulLongGPU::compute(int eflag, int vflag)
97 {
98 ev_init(eflag,vflag);
99
100 int nall = atom->nlocal + atom->nghost;
101 int inum, host_start;
102
103 bool success = true;
104 int *ilist, *numneigh, **firstneigh;
105 if (gpu_mode != GPU_FORCE) {
106 double sublo[3],subhi[3];
107 if (domain->triclinic == 0) {
108 sublo[0] = domain->sublo[0];
109 sublo[1] = domain->sublo[1];
110 sublo[2] = domain->sublo[2];
111 subhi[0] = domain->subhi[0];
112 subhi[1] = domain->subhi[1];
113 subhi[2] = domain->subhi[2];
114 } else {
115 domain->bbox(domain->sublo_lamda,domain->subhi_lamda,sublo,subhi);
116 }
117 inum = atom->nlocal;
118 firstneigh = crml_gpu_compute_n(neighbor->ago, inum, nall, atom->x,
119 atom->type, sublo, subhi,
120 atom->tag, atom->nspecial, atom->special,
121 eflag, vflag, eflag_atom, vflag_atom,
122 host_start, &ilist, &numneigh, cpu_time,
123 success, atom->q, domain->boxlo,
124 domain->prd);
125 } else {
126 inum = list->inum;
127 ilist = list->ilist;
128 numneigh = list->numneigh;
129 firstneigh = list->firstneigh;
130 crml_gpu_compute(neighbor->ago, inum, nall, atom->x, atom->type,
131 ilist, numneigh, firstneigh, eflag, vflag, eflag_atom,
132 vflag_atom, host_start, cpu_time, success, atom->q,
133 atom->nlocal, domain->boxlo, domain->prd);
134 }
135 if (!success)
136 error->one(FLERR,"Insufficient memory on accelerator");
137
138 if (host_start<inum) {
139 cpu_time = MPI_Wtime();
140 cpu_compute(host_start, inum, eflag, vflag, ilist, numneigh, firstneigh);
141 cpu_time = MPI_Wtime() - cpu_time;
142 }
143 }
144
145 /* ----------------------------------------------------------------------
146 init specific to this pair style
147 ------------------------------------------------------------------------- */
148
init_style()149 void PairLJCharmmCoulLongGPU::init_style()
150 {
151 cut_respa = nullptr;
152
153 if (!atom->q_flag)
154 error->all(FLERR,"Pair style lj/charmm/coul/long/gpu requires atom attribute q");
155 if (force->newton_pair)
156 error->all(FLERR,"Pair style lj/charmm/coul/long/gpu requires newton pair off");
157
158 // Repeat cutsq calculation because done after call to init_style
159
160 for (int i = 1; i <= atom->ntypes; i++) {
161 for (int j = i; j <= atom->ntypes; j++) {
162 if (setflag[i][j] != 0 || (setflag[i][i] != 0 && setflag[j][j] != 0))
163 init_one(i,j);
164 }
165 }
166
167 cut_lj_innersq = cut_lj_inner * cut_lj_inner;
168 cut_ljsq = cut_lj * cut_lj;
169 cut_coulsq = cut_coul * cut_coul;
170 cut_bothsq = MAX(cut_ljsq,cut_coulsq);
171
172 denom_lj = (cut_ljsq-cut_lj_innersq) * (cut_ljsq-cut_lj_innersq) *
173 (cut_ljsq-cut_lj_innersq);
174
175 double cell_size = sqrt(cut_bothsq) + neighbor->skin;
176
177 // insure use of KSpace long-range solver, set g_ewald
178
179 if (force->kspace == nullptr)
180 error->all(FLERR,"Pair style requires a KSpace style");
181 g_ewald = force->kspace->g_ewald;
182
183 // setup force tables
184
185 if (ncoultablebits) init_tables(cut_coul,cut_respa);
186
187 int maxspecial=0;
188 if (atom->molecular != Atom::ATOMIC)
189 maxspecial=atom->maxspecial;
190
191 bool arithmetic = true;
192 for (int i = 1; i < atom->ntypes + 1; i++)
193 for (int j = i + 1; j < atom->ntypes + 1; j++) {
194 if (epsilon[i][j] != sqrt(epsilon[i][i] * epsilon[j][j]))
195 arithmetic = false;
196 if (sigma[i][j] != 0.5 * (sigma[i][i] + sigma[j][j]))
197 arithmetic = false;
198 }
199
200 int mnf = 5e-2 * neighbor->oneatom;
201 int success = crml_gpu_init(atom->ntypes+1, cut_bothsq, lj1, lj2, lj3, lj4,
202 offset, force->special_lj, atom->nlocal,
203 atom->nlocal+atom->nghost, mnf, maxspecial,
204 cell_size, gpu_mode, screen, cut_ljsq,
205 cut_coulsq, force->special_coul, force->qqrd2e,
206 g_ewald, cut_lj_innersq,denom_lj,epsilon,sigma,
207 arithmetic);
208 GPU_EXTRA::check_flag(success,error,world);
209
210 if (gpu_mode == GPU_FORCE) {
211 int irequest = neighbor->request(this,instance_me);
212 neighbor->requests[irequest]->half = 0;
213 neighbor->requests[irequest]->full = 1;
214 }
215 }
216
217 /* ---------------------------------------------------------------------- */
218
memory_usage()219 double PairLJCharmmCoulLongGPU::memory_usage()
220 {
221 double bytes = Pair::memory_usage();
222 return bytes + crml_gpu_bytes();
223 }
224
225 /* ---------------------------------------------------------------------- */
226
cpu_compute(int start,int inum,int eflag,int,int * ilist,int * numneigh,int ** firstneigh)227 void PairLJCharmmCoulLongGPU::cpu_compute(int start, int inum, int eflag,
228 int /* vflag */, int *ilist,
229 int *numneigh, int **firstneigh)
230 {
231 int i,j,ii,jj,jnum,itype,jtype,itable;
232 double qtmp,xtmp,ytmp,ztmp,delx,dely,delz,evdwl,ecoul,fpair;
233 double fraction,table;
234 double r,r2inv,r6inv,forcecoul,forcelj,factor_coul,factor_lj;
235 double grij,expm2,prefactor,t,erfc;
236 double philj,switch1,switch2;
237 int *jlist;
238 double rsq;
239
240 evdwl = ecoul = 0.0;
241
242 double **x = atom->x;
243 double **f = atom->f;
244 double *q = atom->q;
245 int *type = atom->type;
246 double *special_coul = force->special_coul;
247 double *special_lj = force->special_lj;
248 double qqrd2e = force->qqrd2e;
249
250 // loop over neighbors of my atoms
251
252 for (ii = start; ii < inum; ii++) {
253 i = ilist[ii];
254 qtmp = q[i];
255 xtmp = x[i][0];
256 ytmp = x[i][1];
257 ztmp = x[i][2];
258 itype = type[i];
259 jlist = firstneigh[i];
260 jnum = numneigh[i];
261
262 for (jj = 0; jj < jnum; jj++) {
263 j = jlist[jj];
264 factor_lj = special_lj[sbmask(j)];
265 factor_coul = special_coul[sbmask(j)];
266 j &= NEIGHMASK;
267
268 delx = xtmp - x[j][0];
269 dely = ytmp - x[j][1];
270 delz = ztmp - x[j][2];
271 rsq = delx*delx + dely*dely + delz*delz;
272
273 if (rsq < cut_bothsq) {
274 r2inv = 1.0/rsq;
275
276 if (rsq < cut_coulsq) {
277 if (!ncoultablebits || rsq <= tabinnersq) {
278 r = sqrt(rsq);
279 grij = g_ewald * r;
280 expm2 = exp(-grij*grij);
281 t = 1.0 / (1.0 + EWALD_P*grij);
282 erfc = t * (A1+t*(A2+t*(A3+t*(A4+t*A5)))) * expm2;
283 prefactor = qqrd2e * qtmp*q[j]/r;
284 forcecoul = prefactor * (erfc + EWALD_F*grij*expm2);
285 if (factor_coul < 1.0) forcecoul -= (1.0-factor_coul)*prefactor;
286 } else {
287 union_int_float_t rsq_lookup;
288 rsq_lookup.f = rsq;
289 itable = rsq_lookup.i & ncoulmask;
290 itable >>= ncoulshiftbits;
291 fraction = (rsq_lookup.f - rtable[itable]) * drtable[itable];
292 table = ftable[itable] + fraction*dftable[itable];
293 forcecoul = qtmp*q[j] * table;
294 if (factor_coul < 1.0) {
295 table = ctable[itable] + fraction*dctable[itable];
296 prefactor = qtmp*q[j] * table;
297 forcecoul -= (1.0-factor_coul)*prefactor;
298 }
299 }
300 } else forcecoul = 0.0;
301
302 if (rsq < cut_ljsq) {
303 r6inv = r2inv*r2inv*r2inv;
304 jtype = type[j];
305 forcelj = r6inv * (lj1[itype][jtype]*r6inv - lj2[itype][jtype]);
306 if (rsq > cut_lj_innersq) {
307 switch1 = (cut_ljsq-rsq) * (cut_ljsq-rsq) *
308 (cut_ljsq + 2.0*rsq - 3.0*cut_lj_innersq) / denom_lj;
309 switch2 = 12.0*rsq * (cut_ljsq-rsq) *
310 (rsq-cut_lj_innersq) / denom_lj;
311 philj = r6inv * (lj3[itype][jtype]*r6inv - lj4[itype][jtype]);
312 forcelj = forcelj*switch1 + philj*switch2;
313 }
314 } else forcelj = 0.0;
315
316 fpair = (forcecoul + factor_lj*forcelj) * r2inv;
317
318 f[i][0] += delx*fpair;
319 f[i][1] += dely*fpair;
320 f[i][2] += delz*fpair;
321
322 if (eflag) {
323 if (rsq < cut_coulsq) {
324 if (!ncoultablebits || rsq <= tabinnersq)
325 ecoul = prefactor*erfc;
326 else {
327 table = etable[itable] + fraction*detable[itable];
328 ecoul = qtmp*q[j] * table;
329 }
330 if (factor_coul < 1.0) ecoul -= (1.0-factor_coul)*prefactor;
331 } else ecoul = 0.0;
332
333 if (rsq < cut_ljsq) {
334 evdwl = r6inv*(lj3[itype][jtype]*r6inv-lj4[itype][jtype]);
335 if (rsq > cut_lj_innersq) {
336 switch1 = (cut_ljsq-rsq) * (cut_ljsq-rsq) *
337 (cut_ljsq + 2.0*rsq - 3.0*cut_lj_innersq) / denom_lj;
338 evdwl *= switch1;
339 }
340 evdwl *= factor_lj;
341 } else evdwl = 0.0;
342 }
343
344 if (evflag) ev_tally_full(i,evdwl,ecoul,fpair,delx,dely,delz);
345 }
346 }
347 }
348 }
349