/* ---------------------------------------------------------------------- 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 "string.h" #include "compute_fcm_chunk.h" #include "atom.h" #include "update.h" #include "modify.h" #include "compute_chunk_atom.h" #include "domain.h" #include "memory.h" #include "error.h" using namespace LAMMPS_NS; enum{ONCE,NFREQ,EVERY}; /* ---------------------------------------------------------------------- */ ComputeFCMChunk::ComputeFCMChunk(LAMMPS *lmp, int narg, char **arg) : Compute(lmp, narg, arg) { if (narg != 4) error->all(FLERR,"Illegal compute fcm/chunk command"); array_flag = 1; size_array_cols = 3; size_array_rows = 0; size_array_rows_variable = 1; extarray = 0; // ID of compute chunk/atom int n = strlen(arg[3]) + 1; idchunk = new char[n]; strcpy(idchunk,arg[3]); init(); //nchunk = cchunk->setup_chunks(); //size_array_rows = nchunk; // chunk-based data nchunk = 1; maxchunk = 0; massproc = masstotal = NULL; fcm = fcmall = NULL; allocate(); firstflag = massneed = 1; } /* ---------------------------------------------------------------------- */ ComputeFCMChunk::~ComputeFCMChunk() { delete [] idchunk; memory->destroy(massproc); memory->destroy(masstotal); memory->destroy(fcm); memory->destroy(fcmall); } /* ---------------------------------------------------------------------- */ void ComputeFCMChunk::init() { int icompute = modify->find_compute(idchunk); if (icompute < 0) error->all(FLERR,"Chunk/atom compute does not exist for compute fcm/chunk"); cchunk = (ComputeChunkAtom *) modify->compute[icompute]; if (strcmp(cchunk->style,"chunk/atom") != 0) error->all(FLERR,"Compute fcm/chunk does not use chunk/atom compute"); } /* ---------------------------------------------------------------------- */ void ComputeFCMChunk::setup() { // one-time calculation of per-chunk mass // done in setup, so that ComputeChunkAtom::setup() is already called if (firstflag && cchunk->idsflag == ONCE) { compute_array(); firstflag = massneed = 0; } } /* ---------------------------------------------------------------------- */ void ComputeFCMChunk::compute_array() { int index; double massone; invoked_array = update->ntimestep; // 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 nchunk = cchunk->setup_chunks(); cchunk->compute_ichunk(); int *ichunk = cchunk->ichunk; if (nchunk > maxchunk) allocate(); size_array_rows = nchunk; // zero local per-chunk values for (int i = 0; i < nchunk; i++) fcm[i][0] = fcm[i][1] = fcm[i][2] = 0.0; if (massneed) for (int i = 0; i < nchunk; i++) massproc[i] = 0.0; // compute FCM for each chunk double **f = atom->f; int *mask = atom->mask; int *type = atom->type; 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]]; fcm[index][0] += f[i][0]; fcm[index][1] += f[i][1]; fcm[index][2] += f[i][2]; if (massneed) massproc[index] += massone; } MPI_Allreduce(&fcm[0][0],&fcmall[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) { fcmall[i][0] /= masstotal[i]; fcmall[i][1] /= masstotal[i]; fcmall[i][2] /= masstotal[i]; } else fcmall[i][0] = fcmall[i][1] = fcmall[i][2] = 0.0; } } /* ---------------------------------------------------------------------- lock methods: called by fix ave/time these methods insure vector/array size is locked for Nfreq epoch by passing lock info along to compute chunk/atom ------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- increment lock counter ------------------------------------------------------------------------- */ void ComputeFCMChunk::lock_enable() { cchunk->lockcount++; } /* ---------------------------------------------------------------------- decrement lock counter in compute chunk/atom, it if still exists ------------------------------------------------------------------------- */ void ComputeFCMChunk::lock_disable() { int icompute = modify->find_compute(idchunk); if (icompute >= 0) { cchunk = (ComputeChunkAtom *) modify->compute[icompute]; cchunk->lockcount--; } } /* ---------------------------------------------------------------------- calculate and return # of chunks = length of vector/array ------------------------------------------------------------------------- */ int ComputeFCMChunk::lock_length() { nchunk = cchunk->setup_chunks(); return nchunk; } /* ---------------------------------------------------------------------- set the lock from startstep to stopstep ------------------------------------------------------------------------- */ void ComputeFCMChunk::lock(Fix *fixptr, bigint startstep, bigint stopstep) { cchunk->lock(fixptr,startstep,stopstep); } /* ---------------------------------------------------------------------- unset the lock ------------------------------------------------------------------------- */ void ComputeFCMChunk::unlock(Fix *fixptr) { cchunk->unlock(fixptr); } /* ---------------------------------------------------------------------- free and reallocate per-chunk arrays ------------------------------------------------------------------------- */ void ComputeFCMChunk::allocate() { memory->destroy(massproc); memory->destroy(masstotal); memory->destroy(fcm); memory->destroy(fcmall); maxchunk = nchunk; memory->create(massproc,maxchunk,"fcm/chunk:massproc"); memory->create(masstotal,maxchunk,"fcm/chunk:masstotal"); memory->create(fcm,maxchunk,3,"fcm/chunk:fcm"); memory->create(fcmall,maxchunk,3,"fcm/chunk:fcmall"); array = fcmall; } /* ---------------------------------------------------------------------- memory usage of local data ------------------------------------------------------------------------- */ double ComputeFCMChunk::memory_usage() { double bytes = (bigint) maxchunk * 2 * sizeof(double); bytes += (bigint) maxchunk * 2*3 * sizeof(double); return bytes; }