#include "pair_sph_lj.h"
#include <cmath>
#include "atom.h"
#include "force.h"
#include "neigh_list.h"
#include "memory.h"
#include "error.h"
#include "domain.h"
using namespace LAMMPS_NS;
PairSPHLJ::PairSPHLJ(LAMMPS *lmp) : Pair(lmp)
{
restartinfo = 0;
}
PairSPHLJ::~PairSPHLJ() {
if (allocated) {
memory->destroy(setflag);
memory->destroy(cutsq);
memory->destroy(cut);
memory->destroy(viscosity);
}
}
void PairSPHLJ::compute(int eflag, int vflag) {
int i, j, ii, jj, inum, jnum, itype, jtype;
double xtmp, ytmp, ztmp, delx, dely, delz, fpair;
int *ilist, *jlist, *numneigh, **firstneigh;
double vxtmp, vytmp, vztmp, imass, jmass, fi, fj, fvisc, h, ih, ihsq, ihcub;
double rsq, wfd, delVdotDelR, mu, deltaE, ci, cj, lrc;
ev_init(eflag, vflag);
double **v = atom->vest;
double **x = atom->x;
double **f = atom->f;
double *rho = atom->rho;
double *mass = atom->mass;
double *de = atom->de;
double *e = atom->e;
double *cv = atom->cv;
double *drho = atom->drho;
int *type = atom->type;
int nlocal = atom->nlocal;
int newton_pair = force->newton_pair;
inum = list->inum;
ilist = list->ilist;
numneigh = list->numneigh;
firstneigh = list->firstneigh;
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
xtmp = x[i][0];
ytmp = x[i][1];
ztmp = x[i][2];
vxtmp = v[i][0];
vytmp = v[i][1];
vztmp = v[i][2];
itype = type[i];
jlist = firstneigh[i];
jnum = numneigh[i];
imass = mass[itype];
LJEOS2(rho[i], e[i], cv[i], &fi, &ci);
fi /= (rho[i] * rho[i]);
for (jj = 0; jj < jnum; jj++) {
j = jlist[jj];
j &= NEIGHMASK;
delx = xtmp - x[j][0];
dely = ytmp - x[j][1];
delz = ztmp - x[j][2];
rsq = delx * delx + dely * dely + delz * delz;
jtype = type[j];
jmass = mass[jtype];
if (rsq < cutsq[itype][jtype]) {
h = cut[itype][jtype];
ih = 1.0 / h;
ihsq = ih * ih;
ihcub = ihsq * ih;
wfd = h - sqrt(rsq);
if (domain->dimension == 3) {
wfd = -25.066903536973515383e0 * wfd * wfd * ihsq * ihsq * ihsq * ih;
} else {
wfd = -19.098593171027440292e0 * wfd * wfd * ihsq * ihsq * ihsq;
}
LJEOS2(rho[j], e[j], cv[j], &fj, &cj);
fj /= (rho[j] * rho[j]);
lrc = - 11.1701 * (ihcub * ihcub * ihcub - 1.5 * ihcub);
fi += lrc;
fj += lrc;
delVdotDelR = delx * (vxtmp - v[j][0]) + dely * (vytmp - v[j][1])
+ delz * (vztmp - v[j][2]);
if (delVdotDelR < 0.) {
mu = h * delVdotDelR / (rsq + 0.01 * h * h);
fvisc = -viscosity[itype][jtype] * (ci + cj) * mu / (rho[i] + rho[j]);
} else {
fvisc = 0.;
}
fpair = -imass * jmass * (fi + fj + fvisc) * wfd;
deltaE = -0.5 * fpair * delVdotDelR;
f[i][0] += delx * fpair;
f[i][1] += dely * fpair;
f[i][2] += delz * fpair;
drho[i] += jmass * delVdotDelR * wfd;
de[i] += deltaE;
if (newton_pair || j < nlocal) {
f[j][0] -= delx * fpair;
f[j][1] -= dely * fpair;
f[j][2] -= delz * fpair;
de[j] += deltaE;
drho[j] += imass * delVdotDelR * wfd;
}
if (evflag)
ev_tally(i, j, nlocal, newton_pair, 0.0, 0.0, fpair, delx, dely, delz);
}
}
}
if (vflag_fdotr) virial_fdotr_compute();
}
void PairSPHLJ::allocate() {
allocated = 1;
int n = atom->ntypes;
memory->create(setflag, n + 1, n + 1, "pair:setflag");
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
setflag[i][j] = 0;
memory->create(cutsq, n + 1, n + 1, "pair:cutsq");
memory->create(cut, n + 1, n + 1, "pair:cut");
memory->create(viscosity, n + 1, n + 1, "pair:viscosity");
}
void PairSPHLJ::settings(int narg, char **) {
if (narg != 0)
error->all(FLERR,
"Illegal number of arguments for pair_style sph/lj");
}
void PairSPHLJ::coeff(int narg, char **arg) {
if (narg != 4)
error->all(FLERR,
"Incorrect args for pair_style sph/lj coefficients");
if (!allocated)
allocate();
int ilo, ihi, jlo, jhi;
force->bounds(FLERR,arg[0], atom->ntypes, ilo, ihi);
force->bounds(FLERR,arg[1], atom->ntypes, jlo, jhi);
double viscosity_one = force->numeric(FLERR,arg[2]);
double cut_one = force->numeric(FLERR,arg[3]);
int count = 0;
for (int i = ilo; i <= ihi; i++) {
for (int j = MAX(jlo,i); j <= jhi; j++) {
viscosity[i][j] = viscosity_one;
printf("setting cut[%d][%d] = %f\n", i, j, cut_one);
cut[i][j] = cut_one;
setflag[i][j] = 1;
count++;
}
}
if (count == 0)
error->all(FLERR,"Incorrect args for pair coefficients");
}
double PairSPHLJ::init_one(int i, int j) {
if (setflag[i][j] == 0) {
error->all(FLERR,"All pair sph/lj coeffs are not set");
}
cut[j][i] = cut[i][j];
viscosity[j][i] = viscosity[i][j];
return cut[i][j];
}
double PairSPHLJ::single(int , int , int , int ,
double , double , double , double &fforce) {
fforce = 0.0;
return 0.0;
}
void PairSPHLJ::LJEOS2(double rho, double e, double cv, double *p, double *c) {
double T = e/cv;
double beta = 1.0 / T;
double beta_sqrt = sqrt(beta);
double x = rho * sqrt(beta_sqrt);
double xsq = x * x;
double xpow3 = xsq * x;
double xpow4 = xsq * xsq;
double diff_A_NkT = 3.629 + 7.264*x - beta*(3.492 - 18.698*x + 35.505*xsq - 31.816*xpow3 + 11.195*xpow4)
- beta_sqrt*(5.369 + 13.16*x + 18.525*xsq - 17.076*xpow3 + 9.32*xpow4)
+ 10.4925*xsq + 11.46*xpow3 + 2.176*xpow4*xpow4*x;
double d2A_dx2 = 7.264 + 20.985*x \
+ beta*(18.698 - 71.01*x + 95.448*xsq - 44.78*xpow3)\
- beta_sqrt*(13.16 + 37.05*x - 51.228*xsq + 37.28*xpow3)\
+ 34.38*xsq + 19.584*xpow4*xpow4;
*p = rho * T * (1.0 + diff_A_NkT * x); double csq = T * (1.0 + 2.0 * diff_A_NkT * x + d2A_dx2 * x * x); if (csq > 0.0) {
*c = sqrt(csq); } else {
*c = 0.0;
}
}