/* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator http://lammps.sandia.gov, Sandia National Laboratories Steve Plimpton, sjplimp@sandia.gov 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. ------------------------------------------------------------------------- */ #include "math.h" #include "stdio.h" #include "stdlib.h" #include "pair_rgl.h" #include "atom.h" #include "comm.h" #include "force.h" #include "update.h" #include "memory.h" #include "neighbor.h" #include "error.h" #include using namespace LAMMPS_NS; using namespace std; #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) /* ---------------------------------------------------------------------- */ PairRGL::PairRGL(LAMMPS *lmp) : Pair(lmp) { nmax = 0; phi = NULL; } /* ---------------------------------------------------------------------- */ PairRGL::~PairRGL() { memory->sfree(phi); if (allocated) { memory->destroy_2d_int_array(setflag); memory->destroy_2d_double_array(cutsq); memory->destroy_2d_double_array(cut); memory->destroy_2d_double_array(cutbsq); memory->destroy_2d_double_array(rr0); memory->destroy_2d_double_array(repsilon); memory->destroy_2d_double_array(rc); memory->destroy_2d_double_array(rp); memory->destroy_2d_double_array(rq); } } /* ---------------------------------------------------------------------- */ void PairRGL::compute(int eflag, int vflag) { int i,j,k,numneigh,itype,jtype; double xtmp,ytmp,ztmp,delx,dely,delz; double rsq,r,dr,dexp,pexp,bexp,pforce,bforce,fforce,peng,beng,eng_bond; int *neighs; double **f; // grow energy array if necessary if (atom->nmax > nmax) { memory->sfree(phi); nmax = atom->nmax; phi = (double *)memory->smalloc(nmax*sizeof(double),"pair:phi"); } eng_vdwl = 0.0; if (vflag) for (i = 0; i < 6; i++) virial[i] = 0.0; if (vflag == 2) f = update->f_pair; else f = atom->f; double **x = atom->x; int *type = atom->type; int nlocal = atom->nlocal; int nall = nlocal + atom->nghost; int newton_pair = force->newton_pair; // zero out density if (newton_pair) { for (i = 0; i < nall; i++) phi[i] = 0.0; } else for (i = 0; i < nlocal; i++) phi[i] = 0.0; // loop over neighbors of my atoms, calculating phi for (i = 0; i < nlocal; i++) { xtmp = x[i][0]; ytmp = x[i][1]; ztmp = x[i][2]; itype = type[i]; neighs = neighbor->firstneigh[i]; numneigh = neighbor->numneigh[i]; for (k = 0; k < numneigh; k++) { j = neighs[k]; 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]; if (rsq < cutbsq[itype][jtype]) { r = sqrt(rsq); phi[i] += exp(-2.0 * rq[itype][jtype] * (r / rr0[itype][jtype] - 1.0)); } } } // loop over neighbors of my atoms, compute force for (i = 0; i < nlocal; i++) { xtmp = x[i][0]; ytmp = x[i][1]; ztmp = x[i][2]; itype = type[i]; neighs = neighbor->firstneigh[i]; numneigh = neighbor->numneigh[i]; eng_bond = 0.0; for (k = 0; k < numneigh; k++) { j = neighs[k]; 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]; if (rsq < cutsq[itype][jtype]) { r = sqrt(rsq); pexp = exp(-rp[itype][jtype] * (r / rr0[itype][jtype] - 1.0)); pforce = -repsilon[itype][jtype] * rp[itype][jtype] / rr0[itype][jtype] * pexp; bexp = exp(-2.0 * rq[itype][jtype] * (r / rr0[itype][jtype] - 1.0)); bforce = rq[itype][jtype] * rc[itype][jtype] / rr0[itype][jtype] * (1.0 / sqrt(phi[i]) + 1.0 / sqrt(phi[j])) * bexp; fforce = pforce + bforce; f[i][0] += delx*fforce; f[i][1] += dely*fforce; f[i][2] += delz*fforce; if (newton_pair || j < nlocal) { f[j][0] -= delx*fforce; f[j][1] -= dely*fforce; f[j][2] -= delz*fforce; } if (eflag) { peng = 0.5 * repsilon[itype][jtype] * pexp; beng = rc[itype][jtype] * rc[itype][jtype] * bexp; if (newton_pair || j < nlocal) { eng_vdwl += peng; eng_bond += beng; } else { eng_vdwl += 0.5 * peng; eng_bond += 0.5 * beng; } } if (vflag == 1) { if (newton_pair == 0 && j >= nlocal) fforce *= 0.5; virial[0] += delx*delx*fforce; virial[1] += dely*dely*fforce; virial[2] += delz*delz*fforce; virial[3] += delx*dely*fforce; virial[4] += delx*delz*fforce; virial[5] += dely*delz*fforce; } } } if (eflag) { eng_vdwl -= sqrt(eng_bond); } } if (vflag == 2) virial_compute(); } /* ---------------------------------------------------------------------- allocate all arrays ------------------------------------------------------------------------- */ void PairRGL::allocate() { allocated = 1; int n = atom->ntypes; setflag = memory->create_2d_int_array(n+1,n+1,"pair:setflag"); for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) setflag[i][j] = 0; cutsq = memory->create_2d_double_array(n+1,n+1,"pair:cutsq"); cutbsq = memory->create_2d_double_array(n+1,n+1,"pair:cutbsq"); cut = memory->create_2d_double_array(n+1,n+1,"pair:cut"); rr0 = memory->create_2d_double_array(n+1,n+1,"pair:rr0"); repsilon = memory->create_2d_double_array(n+1,n+1,"pair:repsilon"); rc = memory->create_2d_double_array(n+1,n+1,"pair:rc"); rp = memory->create_2d_double_array(n+1,n+1,"pair:rp"); rq = memory->create_2d_double_array(n+1,n+1,"pair:rq"); // offset = memory->create_2d_double_array(n+1,n+1,"pair:offset"); } /* ---------------------------------------------------------------------- global settings ------------------------------------------------------------------------- */ void PairRGL::settings(int narg, char **arg) { if (narg != 1) error->all("Illegal pair_style command"); cut_global = atof(arg[0]); // reset cutoffs that have been explicitly set if (allocated) { int i,j; for (i = 1; i <= atom->ntypes; i++) for (j = i+1; j <= atom->ntypes; j++) if (setflag[i][j]) cut[i][j] = cut_global; } } /* ---------------------------------------------------------------------- set coeffs for one or more type pairs ------------------------------------------------------------------------- */ void PairRGL::coeff(int narg, char **arg) { if (narg < 8 || narg > 9) error->all("Incrrect args for pair coefficients"); if (!allocated) allocate(); int ilo,ihi,jlo,jhi; force->bounds(arg[0],atom->ntypes,ilo,ihi); force->bounds(arg[1],atom->ntypes,jlo,jhi); double rr0_one = atof(arg[2]); double repsilon_one = atof(arg[3]); double rc_one = atof(arg[4]); double rp_one = atof(arg[6]); double rq_one = atof(arg[5]); double cutb_one = atof(arg[7]); double cut_one = cut_global; // if (narg == 9) cut_one = atof(arg[8]); int count = 0; for (int i = ilo; i <= ihi; i++) { for (int j = MAX(jlo,i); j <= jhi; j++) { rr0[i][j] = rr0_one; repsilon[i][j] = repsilon_one; rc[i][j] = rc_one; rp[i][j] = rp_one; rq[i][j] = rq_one; cut[i][j] = cut_one; cutbsq[i][j] = cutb_one * cutb_one; setflag[i][j] = 1; count++; } } if (count == 0) error->all("Incoooorrect args for pair coefficients"); } /* ---------------------------------------------------------------------- init for one type pair i,j and corresponding j,i ------------------------------------------------------------------------- */ double PairRGL::init_one(int i, int j) { if (setflag[i][j] == 0) error->all("All pair coeffs are not set"); /* if (offset_flag) { double alpha_dr = -repsilon[i][j] * (cut[i][j] - rc[i][j]); offset[i][j] = rr0[i][j] * (exp(2.0*alpha_dr) - 2.0*exp(alpha_dr)); } else offset[i][j] = 0.0; */ rr0[j][i] = rr0[i][j]; repsilon[j][i] = repsilon[i][j]; rc[j][i] = rc[i][j]; rp[j][i] = rp[i][j]; rq[j][i] = rq[i][j]; cutbsq[j][i] = cutbsq[i][j]; // offset[j][i] = offset[i][j]; return cut[i][j]; } /* ---------------------------------------------------------------------- proc 0 writes to restart file ------------------------------------------------------------------------- */ void PairRGL::write_restart(FILE *fp) { write_restart_settings(fp); int i,j; for (i = 1; i <= atom->ntypes; i++) for (j = i; j <= atom->ntypes; j++) { fwrite(&setflag[i][j],sizeof(int),1,fp); if (setflag[i][j]) { fwrite(&rr0[i][j],sizeof(double),1,fp); fwrite(&repsilon[i][j],sizeof(double),1,fp); fwrite(&rc[i][j],sizeof(double),1,fp); fwrite(&rp[i][j],sizeof(double),1,fp); fwrite(&rq[i][j],sizeof(double),1,fp); fwrite(&cut[i][j],sizeof(double),1,fp); } } } /* ---------------------------------------------------------------------- proc 0 reads from restart file, bcasts ------------------------------------------------------------------------- */ void PairRGL::read_restart(FILE *fp) { read_restart_settings(fp); allocate(); int i,j; int me = comm->me; for (i = 1; i <= atom->ntypes; i++) for (j = i; j <= atom->ntypes; j++) { if (me == 0) fread(&setflag[i][j],sizeof(int),1,fp); MPI_Bcast(&setflag[i][j],1,MPI_INT,0,world); if (setflag[i][j]) { if (me == 0) { fread(&rr0[i][j],sizeof(double),1,fp); fread(&repsilon[i][j],sizeof(double),1,fp); fread(&rc[i][j],sizeof(double),1,fp); fread(&rp[i][j],sizeof(double),1,fp); fread(&rq[i][j],sizeof(double),1,fp); fread(&cut[i][j],sizeof(double),1,fp); } MPI_Bcast(&rr0[i][j],1,MPI_DOUBLE,0,world); MPI_Bcast(&repsilon[i][j],1,MPI_DOUBLE,0,world); MPI_Bcast(&rc[i][j],1,MPI_DOUBLE,0,world); MPI_Bcast(&rp[i][j],1,MPI_DOUBLE,0,world); MPI_Bcast(&rq[i][j],1,MPI_DOUBLE,0,world); MPI_Bcast(&cut[i][j],1,MPI_DOUBLE,0,world); } } } /* ---------------------------------------------------------------------- proc 0 writes to restart file ------------------------------------------------------------------------- */ void PairRGL::write_restart_settings(FILE *fp) { fwrite(&cut_global,sizeof(double),1,fp); fwrite(&offset_flag,sizeof(int),1,fp); fwrite(&mix_flag,sizeof(int),1,fp); } /* ---------------------------------------------------------------------- proc 0 reads from restart file, bcasts ------------------------------------------------------------------------- */ void PairRGL::read_restart_settings(FILE *fp) { if (comm->me == 0) { fread(&cut_global,sizeof(double),1,fp); fread(&offset_flag,sizeof(int),1,fp); fread(&mix_flag,sizeof(int),1,fp); } MPI_Bcast(&cut_global,1,MPI_DOUBLE,0,world); MPI_Bcast(&offset_flag,1,MPI_INT,0,world); MPI_Bcast(&mix_flag,1,MPI_INT,0,world); } /* ---------------------------------------------------------------------- */ void PairRGL::single(int i, int j, int itype, int jtype, double rsq, double factor_coul, double factor_lj, int eflag, One &one) { double r,pexp,bexp,pforce,bforce,peng,beng; r = sqrt(rsq); pexp = exp(-rp[itype][jtype] * (r / rr0[itype][jtype] - 1.0)); pforce = -repsilon[itype][jtype] * rp[itype][jtype] / rr0[itype][jtype] * pexp; bexp = exp(-2.0 * rq[itype][jtype] * (r / rr0[itype][jtype] - 1.0)); bforce = rq[itype][jtype] * rc[itype][jtype] / rr0[itype][jtype] * (1.0 / sqrt(phi[i]) + 1.0 / sqrt(phi[j])) * bexp; one.fforce = (pforce + bforce); if (eflag) { peng = 0.5 * repsilon[itype][jtype] * pexp; beng =0.0; one.eng_vdwl = peng; one.eng_coul = 0.0; } }