Completed serial version with PBC, but incorrect

This commit is contained in:
Aidan Thompson
2019-10-19 17:03:19 -06:00
parent ea9c1002fe
commit 762ecf8f0e
4 changed files with 266 additions and 208 deletions

View File

@ -18,169 +18,218 @@
#include "update.h"
#include "modify.h"
#include "domain.h"
#include "force.h"
#include "memory.h"
#include "error.h"
using namespace LAMMPS_NS;
enum{ONCE,NFREQ,EVERY};
/* ---------------------------------------------------------------------- */
ComputeGrid::ComputeGrid(LAMMPS *lmp, int narg, char **arg) :
Compute(lmp, narg, arg),
idchunk(NULL), masstotal(NULL), massproc(NULL), com(NULL), comall(NULL)
Compute(lmp, narg, arg)
{
if (narg != 4) error->all(FLERR,"Illegal compute com/chunk command");
if (narg < 6) error->all(FLERR,"Illegal compute grid command");
array_flag = 1;
size_array_cols = 3;
size_array_cols = 0;
size_array_rows = 0;
size_array_rows_variable = 1;
extarray = 0;
extarray = 1;
// ID of compute chunk/atom
int iarg0 = 3;
int iarg = iarg0;
if (strcmp(arg[iarg],"grid") == 0) {
if (iarg+4 > narg) error->all(FLERR,"Illegal compute grid command");
nx = force->inumeric(FLERR,arg[iarg+1]);
ny = force->inumeric(FLERR,arg[iarg+2]);
nz = force->inumeric(FLERR,arg[iarg+3]);
if (nx <= 0 || ny <= 0 || nz <= 0)
error->all(FLERR,"All grid dimensions must be positive");
iarg += 4;
} else error->all(FLERR,"Illegal compute grid command");
int n = strlen(arg[3]) + 1;
idchunk = new char[n];
strcpy(idchunk,arg[3]);
nargbase = iarg - iarg0;
init();
// chunk-based data
nchunk = 1;
maxchunk = 0;
allocate();
firstflag = massneed = 1;
size_array_rows = nx*ny*nz;
}
/* ---------------------------------------------------------------------- */
ComputeGrid::~ComputeGrid()
{
delete [] idchunk;
memory->destroy(massproc);
memory->destroy(masstotal);
memory->destroy(com);
memory->destroy(comall);
}
/* ---------------------------------------------------------------------- */
void ComputeGrid::init()
{
int icompute = modify->find_compute(idchunk);
if (icompute < 0)
error->all(FLERR,"Chunk/atom compute does not exist for compute com/chunk");
cchunk = (ComputeChunkAtom *) modify->compute[icompute];
if (strcmp(cchunk->style,"chunk/atom") != 0)
error->all(FLERR,"Compute com/chunk does not use chunk/atom compute");
}
/* ---------------------------------------------------------------------- */
void ComputeGrid::setup()
{
// one-time calculation of per-chunk mass
// done in setup, so that ComputeChunkAtom::setup() is already called
// calculate grid layout
if (firstflag && cchunk->idsflag == ONCE) {
compute_array();
firstflag = massneed = 0;
triclinic = domain->triclinic;
if (triclinic == 0) {
prd = domain->prd;
boxlo = domain->boxlo;
} else {
prd = domain->prd_lamda;
boxlo = domain->boxlo_lamda;
}
double xprd = prd[0];
double yprd = prd[1];
double zprd = prd[2];
delxinv = nx/xprd;
delyinv = ny/yprd;
delzinv = nz/zprd;
delx = 1.0/delxinv;
dely = 1.0/delyinv;
delz = 1.0/delzinv;
// sufficient conditions for stencil bounding rcut
// require |delz*mz|^2 <= rcut^2
// require |dely*my|^2 <= rcut^2 + |delyz*mz_max|^2
// require |delx*mx|^2 <= rcut^2 + |delxz*mz_max|^2 + |delxy*my_max|^2
double delxy = domain->xy/ny;
double delxz = domain->xz/nz;
double delyz = domain->yz/nz;
if (!triclinic) {
mz = cutmax*delzinv + 1;
my = sqrt(cutmax*cutmax + pow(delyz*mz,2))*delyinv + 1;
mx = sqrt(cutmax*cutmax + pow(delxz*mz,2)
+ pow(delxy*my,2))*delxinv + 1;
} else {
double delxinvtmp = nx/domain->xprd;
double delyinvtmp = ny/domain->yprd;
double delzinvtmp = nz/domain->zprd;
mz = cutmax*delzinvtmp + 1;
my = sqrt(cutmax*cutmax + pow(delyz*mz,2))*delyinvtmp + 1;
mx = sqrt(cutmax*cutmax + pow(delxz*mz,2)
+ pow(delxy*my,2))*delxinvtmp + 1;
}
printf("mx = %d\n",mx);
printf("my = %d\n",my);
printf("mz = %d\n",mz);
// size global grid to accomodate periodic interactions
nxfull = nx + 2*mx;
nyfull = ny + 2*my;
nzfull = nz + 2*mz;
nxyfull = nxfull * nyfull;
printf("nxfull = %d\n",nxfull);
printf("nyfull = %d\n",nyfull);
printf("nzfull = %d\n",nzfull);
x0full = boxlo[0] - mx*delx;
y0full = boxlo[1] - my*dely;
z0full = boxlo[2] - mz*delz;
allocate();
}
/* ---------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
convert grid index to box coords
------------------------------------------------------------------------- */
void ComputeGrid::compute_array()
void ComputeGrid::igridfull2x(int igrid, double *x)
{
int index;
double massone;
double unwrap[3];
int iz = igrid / nxyfull;
igrid -= iz*nxyfull;
int iy = igrid / nxfull;
igrid -= igrid*nxfull;
int ix = igrid;
invoked_array = update->ntimestep;
x[0] = x0full+ix*delx;
x[1] = y0full+iy*dely;
x[2] = z0full+iz*delz;
// compute chunk/atom assigns atoms to chunk IDs
// extract ichunk index vector from compute
// ichunk = 1 to Nchunk for included atoms, 0 for excluded atoms
if (triclinic) domain->lamda2x(x, x);
nchunk = cchunk->setup_chunks();
cchunk->compute_ichunk();
int *ichunk = cchunk->ichunk;
}
if (nchunk > maxchunk) allocate();
size_array_rows = nchunk;
/* ----------------------------------------------------------------------
gather global array from full grid
------------------------------------------------------------------------- */
// zero local per-chunk values
void ComputeGrid::gather_global_array()
{
int iarray;
memset(&array[0][0],0,size_array_rows*size_array_cols*sizeof(double));
for (int i = 0; i < nchunk; i++)
com[i][0] = com[i][1] = com[i][2] = 0.0;
if (massneed)
for (int i = 0; i < nchunk; i++) massproc[i] = 0.0;
for (int igrid = 0; igrid < ngridfull; igrid++) {
// compute COM for each chunk
// inefficient, should exploit shared ix structure
double **x = atom->x;
int *mask = atom->mask;
int *type = atom->type;
imageint *image = atom->image;
double *mass = atom->mass;
double *rmass = atom->rmass;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
if (rmass) massone = rmass[i];
else massone = mass[type[i]];
domain->unmap(x[i],image[i],unwrap);
com[index][0] += unwrap[0] * massone;
com[index][1] += unwrap[1] * massone;
com[index][2] += unwrap[2] * massone;
if (massneed) massproc[index] += massone;
}
MPI_Allreduce(&com[0][0],&comall[0][0],3*nchunk,MPI_DOUBLE,MPI_SUM,world);
if (massneed)
MPI_Allreduce(massproc,masstotal,nchunk,MPI_DOUBLE,MPI_SUM,world);
for (int i = 0; i < nchunk; i++) {
if (masstotal[i] > 0.0) {
comall[i][0] /= masstotal[i];
comall[i][1] /= masstotal[i];
comall[i][2] /= masstotal[i];
} else comall[i][0] = comall[i][1] = comall[i][2] = 0.0;
iarray = igridfull2iarray(igrid);
for (int icol = 0; icol < size_array_cols; icol++)
array[iarray][icol] += gridfull[igrid][icol];
}
}
/* ----------------------------------------------------------------------
free and reallocate per-chunk arrays
convert full grid index to compute array index
inefficient, should exploit shared ix structure
------------------------------------------------------------------------- */
int ComputeGrid::igridfull2iarray(int igrid)
{
int iz = igrid / nxyfull;
igrid -= iz*nxyfull;
int iy = igrid / nxfull;
igrid -= igrid*nxfull;
int ix = igrid;
ix -= mx;
iy -= my;
iz -= mz;
while (ix < 0) ix += nx;
while (iy < 0) iy += ny;
while (iz < 0) iz += nz;
while (ix >= nx) ix -= nx;
while (iy >= ny) iy -= ny;
while (iz >= nz) iz -= nz;
int iarray = (iz * ny + iy) * nx + ix;
return iarray;
}
/* ----------------------------------------------------------------------
free and reallocate arrays
------------------------------------------------------------------------- */
void ComputeGrid::allocate()
{
memory->destroy(massproc);
memory->destroy(masstotal);
memory->destroy(com);
memory->destroy(comall);
maxchunk = nchunk;
memory->create(massproc,maxchunk,"com/chunk:massproc");
memory->create(masstotal,maxchunk,"com/chunk:masstotal");
memory->create(com,maxchunk,3,"com/chunk:com");
memory->create(comall,maxchunk,3,"com/chunk:comall");
array = comall;
}
ngridfull = nxfull*nyfull*nzfull;
// grow global array if necessary
memory->destroy(array);
memory->create(array,size_array_rows,size_array_cols,"sna/grid:array");
memory->create(gridfull,ngridfull,size_array_cols,"sna/grid:gridfull");
}
/* ----------------------------------------------------------------------
memory usage of local data
------------------------------------------------------------------------- */
double ComputeGrid::memory_usage()
{
double bytes = (bigint) maxchunk * 2 * sizeof(double);
bytes += (bigint) maxchunk * 2*3 * sizeof(double);
return bytes;
int nbytes = 0;
return nbytes;
}

View File

@ -11,12 +11,6 @@
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifdef COMPUTE_CLASS
ComputeStyle(grid,ComputeGrid)
#else
#ifndef LMP_COMPUTE_GRID_H
#define LMP_COMPUTE_GRID_H
@ -26,36 +20,39 @@ namespace LAMMPS_NS {
class ComputeGrid : public Compute {
public:
char *idchunk; // fields accessed by other classes
double *masstotal;
ComputeGrid(class LAMMPS *, int, char **);
~ComputeGrid();
virtual ~ComputeGrid();
void init();
void setup();
void compute_array();
void lock_enable();
void lock_disable();
int lock_length();
void lock(class Fix *, bigint, bigint);
void unlock(class Fix *);
virtual void compute_array() = 0;
double memory_usage();
protected:
int nx, ny, nz; // grid dimensions
int nxfull, nyfull, nzfull; // grid dimensions with ghost points
int nxyfull; // nx_full*ny_full
int ngridfull; // number of full grid points
double **gridfull; // full grid points
int mx, my, mz; // cutmax stencil dimensions
int triclinic; // triclinic flag
double *boxlo, *prd; // box info (units real/ortho or reduced/tri)
double delxinv,delyinv,delzinv; // inverse grid spacing
double delx,dely,delz; // grid spacing
double x0full, y0full, z0full; // origin of full grid
int nargbase; // number of base class args
double cutmax; // largest cutoff distance
virtual void allocate();
void igridfull2x(int, double*); // convert full grid point to coord
void gather_global_array(); // gather global array from full grid
int igridfull2iarray(int); // convert full grid index to compute array index
private:
int nchunk,maxchunk;
int firstflag,massneed;
double *massproc;
double **com,**comall;
void allocate();
};
}
#endif
#endif
/* ERROR/WARNING messages:

View File

@ -11,6 +11,7 @@
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "compute_grid.h"
#include "compute_sna_grid.h"
#include <cstring>
#include <cstdlib>
@ -30,7 +31,7 @@
using namespace LAMMPS_NS;
ComputeSNAGrid::ComputeSNAGrid(LAMMPS *lmp, int narg, char **arg) :
Compute(lmp, narg, arg), cutsq(NULL), list(NULL), sna(NULL),
ComputeGrid(lmp, narg, arg), cutsq(NULL), list(NULL), sna(NULL),
radelem(NULL), wjelem(NULL)
{
double rmin0, rfac0;
@ -38,6 +39,13 @@ ComputeSNAGrid::ComputeSNAGrid(LAMMPS *lmp, int narg, char **arg) :
radelem = NULL;
wjelem = NULL;
// skip over arguments used by base class
// so that argument positions are identical to
// regular per-atom compute
arg += nargbase;
narg -= nargbase;
int ntypes = atom->ntypes;
int nargmin = 6+2*ntypes;
@ -58,6 +66,7 @@ ComputeSNAGrid::ComputeSNAGrid(LAMMPS *lmp, int narg, char **arg) :
rcutfac = atof(arg[3]);
rfac0 = atof(arg[4]);
twojmax = atoi(arg[5]);
printf("rcutfac = %g rfac0 = %g twojmax = %d\n",rcutfac, rfac0, twojmax);
for(int i = 0; i < ntypes; i++)
radelem[i+1] = atof(arg[6+i]);
@ -73,6 +82,7 @@ ComputeSNAGrid::ComputeSNAGrid(LAMMPS *lmp, int narg, char **arg) :
cut = 2.0*radelem[i]*rcutfac;
if (cut > cutmax) cutmax = cut;
cutsq[i][i] = cut*cut;
printf("i = %d cutsq[i][i] = %g\n",i,cutsq[i][i]);
for(int j = i+1; j <= ntypes; j++) {
cut = (radelem[i]+radelem[j])*rcutfac;
cutsq[i][j] = cutsq[j][i] = cut*cut;
@ -84,6 +94,7 @@ ComputeSNAGrid::ComputeSNAGrid(LAMMPS *lmp, int narg, char **arg) :
int iarg = nargmin;
while (iarg < narg) {
printf("iarg = %d arg = %s\n",iarg, arg[iarg]);
if (strcmp(arg[iarg],"rmin0") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal compute sna/grid command");
@ -105,18 +116,19 @@ ComputeSNAGrid::ComputeSNAGrid(LAMMPS *lmp, int narg, char **arg) :
quadraticflag = atoi(arg[iarg+1]);
iarg += 2;
} else error->all(FLERR,"Illegal compute sna/grid command");
}
printf("rmin0 = %g, bzeroflag = %d, quadraticflag = %d\n",
rmin0, bzeroflag, quadraticflag);
snaptr = new SNA(lmp,rfac0,twojmax,
rmin0,switchflag,bzeroflag);
ncoeff = snaptr->ncoeff;
size_peratom_cols = ncoeff;
if (quadraticflag) size_peratom_cols += (ncoeff*(ncoeff+1))/2;
peratom_flag = 1;
nmax = 0;
sna = NULL;
size_array_cols = ncoeff;
if (quadraticflag) size_array_cols += (ncoeff*(ncoeff+1))/2;
array_flag = 1;
}
/* ---------------------------------------------------------------------- */
@ -166,101 +178,101 @@ void ComputeSNAGrid::init_list(int /*id*/, NeighList *ptr)
/* ---------------------------------------------------------------------- */
void ComputeSNAGrid::compute_pergrid()
void ComputeSNAGrid::compute_array()
{
invoked_peratom = update->ntimestep;
invoked_array = update->ntimestep;
// grow sna array if necessary
// // invoke full neighbor list (will copy or build if necessary)
if (atom->nmax > nmax) {
memory->destroy(sna);
nmax = atom->nmax;
memory->create(sna,nmax,size_peratom_cols,"sna/grid:sna");
array_atom = sna;
}
// neighbor->build_one(list);
// invoke full neighbor list (will copy or build if necessary)
// const int inum = list->inum;
// const int* const ilist = list->ilist;
// const int* const numneigh = list->numneigh;
// int** const firstneigh = list->firstneigh;
neighbor->build_one(list);
const int inum = list->inum;
const int* const ilist = list->ilist;
const int* const numneigh = list->numneigh;
int** const firstneigh = list->firstneigh;
int * const type = atom->type;
// compute sna for each atom in group
// use full neighbor list to count atoms less than cutoff
// compute sna for each gridpoint
double** const x = atom->x;
const int* const mask = atom->mask;
const int ntotal = atom->nlocal + atom->nghost;
for (int ii = 0; ii < inum; ii++) {
const int i = ilist[ii];
if (mask[i] & groupbit) {
printf("ngridfull = %d\n",ngridfull);
for (int igrid = 0; igrid < ngridfull; igrid++) {
printf("igrid = %d\n",igrid);
double rtmp[3];
igridfull2x(igrid, rtmp);
const double xtmp = rtmp[0];
const double ytmp = rtmp[1];
const double ztmp = rtmp[2];
const double xtmp = x[i][0];
const double ytmp = x[i][1];
const double ztmp = x[i][2];
const int itype = type[i];
const double radi = radelem[itype];
const int* const jlist = firstneigh[i];
const int jnum = numneigh[i];
// rij[][3] = displacements between atom I and those neighbors
// inside = indices of neighbors of I within cutoff
// typej = types of neighbors of I within cutoff
// insure rij, inside, and typej are of size jnum
int ninside = 0;
for (int j = 0; j < ntotal; j++) {
snaptr->grow_rij(jnum);
// check that j is in comute group
// rij[][3] = displacements between atom I and those neighbors
// inside = indices of neighbors of I within cutoff
// typej = types of neighbors of I within cutoff
if (!(mask[j] & groupbit)) continue;
int ninside = 0;
for (int jj = 0; jj < jnum; jj++) {
int j = jlist[jj];
j &= NEIGHMASK;
// insure rij, inside, and typej are of size jnum
const double delx = xtmp - x[j][0];
const double dely = ytmp - x[j][1];
const double delz = ztmp - x[j][2];
const double rsq = delx*delx + dely*dely + delz*delz;
int jtype = type[j];
if (rsq < cutsq[itype][jtype] && rsq>1e-20) {
snaptr->rij[ninside][0] = delx;
snaptr->rij[ninside][1] = dely;
snaptr->rij[ninside][2] = delz;
snaptr->inside[ninside] = j;
snaptr->wj[ninside] = wjelem[jtype];
snaptr->rcutij[ninside] = (radi+radelem[jtype])*rcutfac;
ninside++;
}
snaptr->grow_rij(ninside+1);
const double delx = xtmp - x[j][0];
const double dely = ytmp - x[j][1];
const double delz = ztmp - x[j][2];
const double rsq = delx*delx + dely*dely + delz*delz;
int jtype = type[j];
if (rsq < cutsq[jtype][jtype] && rsq>1e-20) {
printf("ninside = %d\n",ninside);
snaptr->rij[ninside][0] = delx;
snaptr->rij[ninside][1] = dely;
snaptr->rij[ninside][2] = delz;
snaptr->inside[ninside] = j;
snaptr->wj[ninside] = wjelem[jtype];
snaptr->rcutij[ninside] = 2.0*radelem[jtype]*rcutfac;
ninside++;
}
}
snaptr->compute_ui(ninside);
snaptr->compute_zi();
snaptr->compute_bi();
for (int icoeff = 0; icoeff < ncoeff; icoeff++)
sna[i][icoeff] = snaptr->blist[icoeff];
if (quadraticflag) {
int ncount = ncoeff;
for (int icoeff = 0; icoeff < ncoeff; icoeff++) {
double bi = snaptr->blist[icoeff];
snaptr->compute_ui(ninside);
snaptr->compute_zi();
snaptr->compute_bi();
for (int icoeff = 0; icoeff < ncoeff; icoeff++)
sna[igrid][icoeff] = snaptr->blist[icoeff];
printf("igrid = %d B0 = %g\n",igrid,sna[igrid][0]);
if (quadraticflag) {
int ncount = ncoeff;
for (int icoeff = 0; icoeff < ncoeff; icoeff++) {
double bi = snaptr->blist[icoeff];
// diagonal element of quadratic matrix
// diagonal element of quadratic matrix
sna[i][ncount++] = 0.5*bi*bi;
sna[igrid][ncount++] = 0.5*bi*bi;
// upper-triangular elements of quadratic matrix
// upper-triangular elements of quadratic matrix
for (int jcoeff = icoeff+1; jcoeff < ncoeff; jcoeff++)
sna[i][ncount++] = bi*snaptr->blist[jcoeff];
}
for (int jcoeff = icoeff+1; jcoeff < ncoeff; jcoeff++)
sna[igrid][ncount++] = bi*snaptr->blist[jcoeff];
}
} else {
for (int icoeff = 0; icoeff < size_peratom_cols; icoeff++)
sna[i][icoeff] = 0.0;
}
}
gather_global_array();
}
/* ----------------------------------------------------------------------
allocate array in base class and then set up pointers
------------------------------------------------------------------------- */
void ComputeSNAGrid::allocate()
{
ComputeGrid::allocate();
sna = gridfull;
}
/* ----------------------------------------------------------------------
@ -269,7 +281,7 @@ void ComputeSNAGrid::compute_pergrid()
double ComputeSNAGrid::memory_usage()
{
double bytes = nmax*size_peratom_cols * sizeof(double); // sna
double bytes = size_array_rows*size_array_cols * sizeof(double); // grid
bytes += snaptr->memory_usage(); // SNA object
return bytes;

View File

@ -20,18 +20,19 @@ ComputeStyle(sna/grid,ComputeSNAGrid)
#ifndef LMP_COMPUTE_SNA_GRID_H
#define LMP_COMPUTE_SNA_GRID_H
#include "compute.h"
#include "compute_grid.h"
namespace LAMMPS_NS {
class ComputeSNAGrid : public Compute {
class ComputeSNAGrid : public ComputeGrid {
public:
ComputeSNAGrid(class LAMMPS *, int, char **);
~ComputeSNAGrid();
void init();
void init_list(int, class NeighList *);
void compute_grid();
void compute_array();
double memory_usage();
void allocate();
private:
int nmax;
@ -43,7 +44,6 @@ class ComputeSNAGrid : public Compute {
double *radelem;
double *wjelem;
class SNA* snaptr;
double cutmax;
int quadraticflag;
};