// clang-format off /* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories LAMMPS development team: developers@lammps.org Copyright (2003) Sandia Corporation. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain rights in this software. This software is distributed under the GNU General Public License. See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- Contributing authors: Stan Moore (SNL) ------------------------------------------------------------------------- */ #include "pair_zbl_kokkos.h" #include "atom_kokkos.h" #include "atom_masks.h" #include "error.h" #include "force.h" #include "kokkos.h" #include "memory_kokkos.h" #include "neigh_list.h" #include "neigh_request.h" #include "neighbor.h" #include "respa.h" #include "update.h" #include "pair_zbl_const.h" #include #include // From J.F. Zeigler, J. P. Biersack and U. Littmark, // "The Stopping and Range of Ions in Matter" volume 1, Pergamon, 1985. using namespace LAMMPS_NS; using namespace PairZBLConstants; /* ---------------------------------------------------------------------- */ template PairZBLKokkos::PairZBLKokkos(LAMMPS *lmp) : PairZBL(lmp) { respa_enable = 0; kokkosable = 1; atomKK = (AtomKokkos *) atom; execution_space = ExecutionSpaceFromDevice::space; datamask_read = X_MASK | F_MASK | TYPE_MASK | ENERGY_MASK | VIRIAL_MASK; datamask_modify = F_MASK | ENERGY_MASK | VIRIAL_MASK; } /* ---------------------------------------------------------------------- */ template PairZBLKokkos::~PairZBLKokkos() { if (copymode) return; if (allocated) { memoryKK->destroy_kokkos(k_eatom,eatom); memoryKK->destroy_kokkos(k_vatom,vatom); } } /* ---------------------------------------------------------------------- init specific to this pair style ------------------------------------------------------------------------- */ template void PairZBLKokkos::init_style() { PairZBL::init_style(); Kokkos::deep_copy(d_cutsq,cut_globalsq); // error if rRESPA with inner levels if (update->whichflag == 1 && utils::strmatch(update->integrate_style,"^respa")) { int respa = 0; if (((Respa *) update->integrate)->level_inner >= 0) respa = 1; if (((Respa *) update->integrate)->level_middle >= 0) respa = 2; if (respa) error->all(FLERR,"Cannot use Kokkos pair style with rRESPA inner/middle"); } // adjust neighbor list request for KOKKOS neighflag = lmp->kokkos->neighflag; auto request = neighbor->find_request(this); request->set_kokkos_host(std::is_same_v && !std::is_same_v); request->set_kokkos_device(std::is_same_v); if (neighflag == FULL) request->enable_full(); } /* ---------------------------------------------------------------------- */ template void PairZBLKokkos::compute(int eflag_in, int vflag_in) { eflag = eflag_in; vflag = vflag_in; if (neighflag == FULL) no_virial_fdotr_compute = 1; ev_init(eflag,vflag,0); // reallocate per-atom arrays if necessary if (eflag_atom) { memoryKK->destroy_kokkos(k_eatom,eatom); memoryKK->create_kokkos(k_eatom,eatom,maxeatom,"pair:eatom"); d_eatom = k_eatom.view(); } if (vflag_atom) { memoryKK->destroy_kokkos(k_vatom,vatom); memoryKK->create_kokkos(k_vatom,vatom,maxvatom,"pair:vatom"); d_vatom = k_vatom.view(); } atomKK->sync(execution_space,datamask_read); x = atomKK->k_x.view(); f = atomKK->k_f.view(); type = atomKK->k_type.view(); nlocal = atom->nlocal; nall = atom->nlocal + atom->nghost; newton_pair = force->newton_pair; special_lj[0] = force->special_lj[0]; special_lj[1] = force->special_lj[1]; special_lj[2] = force->special_lj[2]; special_lj[3] = force->special_lj[3]; k_z.sync(); k_d1a.sync(); k_d2a.sync(); k_d3a.sync(); k_d4a.sync(); k_zze.sync(); k_sw1.sync(); k_sw2.sync(); k_sw3.sync(); k_sw4.sync(); k_sw5.sync(); // loop over neighbors of my atoms EV_FLOAT ev = pair_compute,void >(this,(NeighListKokkos*)list); if (eflag_global) eng_vdwl += ev.evdwl; if (vflag_global) { virial[0] += ev.v[0]; virial[1] += ev.v[1]; virial[2] += ev.v[2]; virial[3] += ev.v[3]; virial[4] += ev.v[4]; virial[5] += ev.v[5]; } if (eflag_atom) { k_eatom.template modify(); k_eatom.template sync(); } if (vflag_atom) { k_vatom.template modify(); k_vatom.template sync(); } if (vflag_fdotr) pair_virial_fdotr_compute(this); if (eflag || vflag) atomKK->modified(execution_space,datamask_modify); else atomKK->modified(execution_space,F_MASK); } template template KOKKOS_INLINE_FUNCTION F_FLOAT PairZBLKokkos:: compute_fpair(const F_FLOAT& rsq, const int &, const int &, const int &itype, const int &jtype) const { const F_FLOAT r = sqrt(rsq); F_FLOAT fpair = dzbldr(r, itype, jtype); if (rsq > cut_innersq) { const F_FLOAT t = r - cut_inner; const F_FLOAT fswitch = t*t * (d_sw1(itype,jtype) + d_sw2(itype,jtype)*t); fpair += fswitch; } fpair *= -1.0/r; return fpair; } template template KOKKOS_INLINE_FUNCTION F_FLOAT PairZBLKokkos:: compute_evdwl(const F_FLOAT &rsq, const int &, const int &, const int &itype, const int &jtype) const { const F_FLOAT r = sqrt(rsq); F_FLOAT evdwl = e_zbl(r, itype, jtype); evdwl += d_sw5(itype,jtype); if (rsq > cut_innersq) { const F_FLOAT t = r - cut_inner; const F_FLOAT eswitch = t*t*t * (d_sw3(itype,jtype) + d_sw4(itype,jtype)*t); evdwl += eswitch; } return evdwl; } /* ---------------------------------------------------------------------- allocate all arrays ------------------------------------------------------------------------- */ template void PairZBLKokkos::allocate() { PairZBL::allocate(); int n = atom->ntypes; k_z = DAT::tdual_ffloat_1d("pair_zbl:z ",n+1); k_d1a = DAT::tdual_ffloat_2d_dl("pair_zbl:d1a",n+1,n+1); k_d2a = DAT::tdual_ffloat_2d_dl("pair_zbl:d2a",n+1,n+1); k_d3a = DAT::tdual_ffloat_2d_dl("pair_zbl:d3a",n+1,n+1); k_d4a = DAT::tdual_ffloat_2d_dl("pair_zbl:d4a",n+1,n+1); k_zze = DAT::tdual_ffloat_2d_dl("pair_zbl:zze",n+1,n+1); k_sw1 = DAT::tdual_ffloat_2d_dl("pair_zbl:sw1",n+1,n+1); k_sw2 = DAT::tdual_ffloat_2d_dl("pair_zbl:sw2",n+1,n+1); k_sw3 = DAT::tdual_ffloat_2d_dl("pair_zbl:sw3",n+1,n+1); k_sw4 = DAT::tdual_ffloat_2d_dl("pair_zbl:sw4",n+1,n+1); k_sw5 = DAT::tdual_ffloat_2d_dl("pair_zbl:sw5",n+1,n+1); d_z = k_z.view(); d_d1a = k_d1a.view(); d_d2a = k_d2a.view(); d_d3a = k_d3a.view(); d_d4a = k_d4a.view(); d_zze = k_zze.view(); d_sw1 = k_sw1.view(); d_sw2 = k_sw2.view(); d_sw3 = k_sw3.view(); d_sw4 = k_sw4.view(); d_sw5 = k_sw5.view(); d_cutsq = typename AT::t_ffloat_2d_dl("pair_zbl:cutsq",n+1,n+1); } /* ---------------------------------------------------------------------- init for one type pair i,j and corresponding j,i ------------------------------------------------------------------------- */ template double PairZBLKokkos::init_one(int i, int j) { double cutone = PairZBL::init_one(i,j); k_z.h_view(i) = z[i]; k_z.h_view(j) = z[j]; k_d1a.h_view(i,j) = k_d1a.h_view(j,i) = d1a[i][j]; k_d2a.h_view(i,j) = k_d2a.h_view(j,i) = d2a[i][j]; k_d3a.h_view(i,j) = k_d3a.h_view(j,i) = d3a[i][j]; k_d4a.h_view(i,j) = k_d4a.h_view(j,i) = d4a[i][j]; k_zze.h_view(i,j) = k_zze.h_view(j,i) = zze[i][j]; k_sw1.h_view(i,j) = k_sw1.h_view(j,i) = sw1[i][j]; k_sw2.h_view(i,j) = k_sw2.h_view(j,i) = sw2[i][j]; k_sw3.h_view(i,j) = k_sw3.h_view(j,i) = sw3[i][j]; k_sw4.h_view(i,j) = k_sw4.h_view(j,i) = sw4[i][j]; k_sw5.h_view(i,j) = k_sw5.h_view(j,i) = sw5[i][j]; k_z.modify(); k_d1a.modify(); k_d2a.modify(); k_d3a.modify(); k_d4a.modify(); k_zze.modify(); k_sw1.modify(); k_sw2.modify(); k_sw3.modify(); k_sw4.modify(); k_sw5.modify(); if (i KOKKOS_INLINE_FUNCTION F_FLOAT PairZBLKokkos::e_zbl(F_FLOAT r, int i, int j) const { const F_FLOAT d1aij = d_d1a(i,j); const F_FLOAT d2aij = d_d2a(i,j); const F_FLOAT d3aij = d_d3a(i,j); const F_FLOAT d4aij = d_d4a(i,j); const F_FLOAT zzeij = d_zze(i,j); const F_FLOAT rinv = 1.0/r; F_FLOAT sum = c1*exp(-d1aij*r); sum += c2*exp(-d2aij*r); sum += c3*exp(-d3aij*r); sum += c4*exp(-d4aij*r); F_FLOAT result = zzeij*sum*rinv; return result; } /* ---------------------------------------------------------------------- compute ZBL first derivative ------------------------------------------------------------------------- */ template KOKKOS_INLINE_FUNCTION F_FLOAT PairZBLKokkos::dzbldr(F_FLOAT r, int i, int j) const { const F_FLOAT d1aij = d_d1a(i,j); const F_FLOAT d2aij = d_d2a(i,j); const F_FLOAT d3aij = d_d3a(i,j); const F_FLOAT d4aij = d_d4a(i,j); const F_FLOAT zzeij = d_zze(i,j); const F_FLOAT rinv = 1.0/r; const F_FLOAT e1 = exp(-d1aij*r); const F_FLOAT e2 = exp(-d2aij*r); const F_FLOAT e3 = exp(-d3aij*r); const F_FLOAT e4 = exp(-d4aij*r); F_FLOAT sum = c1*e1; sum += c2*e2; sum += c3*e3; sum += c4*e4; F_FLOAT sum_p = -c1*d1aij*e1; sum_p -= c2*d2aij*e2; sum_p -= c3*d3aij*e3; sum_p -= c4*d4aij*e4; F_FLOAT result = zzeij*(sum_p - sum*rinv)*rinv; return result; } /* ---------------------------------------------------------------------- compute ZBL second derivative ------------------------------------------------------------------------- */ template KOKKOS_INLINE_FUNCTION F_FLOAT PairZBLKokkos::d2zbldr2(F_FLOAT r, int i, int j) const { const F_FLOAT d1aij = d_d1a(i,j); const F_FLOAT d2aij = d_d2a(i,j); const F_FLOAT d3aij = d_d3a(i,j); const F_FLOAT d4aij = d_d4a(i,j); const F_FLOAT zzeij = d_zze(i,j); const F_FLOAT rinv = 1.0/r; const F_FLOAT e1 = exp(-d1aij*r); const F_FLOAT e2 = exp(-d2aij*r); const F_FLOAT e3 = exp(-d3aij*r); const F_FLOAT e4 = exp(-d4aij*r); F_FLOAT sum = c1*e1; sum += c2*e2; sum += c3*e3; sum += c4*e4; F_FLOAT sum_p = c1*e1*d1aij; sum_p += c2*e2*d2aij; sum_p += c3*e3*d3aij; sum_p += c4*e4*d4aij; F_FLOAT sum_pp = c1*e1*d1aij*d1aij; sum_pp += c2*e2*d2aij*d2aij; sum_pp += c3*e3*d3aij*d3aij; sum_pp += c4*e4*d4aij*d4aij; F_FLOAT result = zzeij*(sum_pp + 2.0*sum_p*rinv + 2.0*sum*rinv*rinv)*rinv; return result; } namespace LAMMPS_NS { template class PairZBLKokkos; #ifdef LMP_KOKKOS_GPU template class PairZBLKokkos; #endif }