diff --git a/src/compute.cpp b/src/compute.cpp index 5f41cbf83..f3b24f7bb 100644 --- a/src/compute.cpp +++ b/src/compute.cpp @@ -1,316 +1,321 @@ /* ---------------------------------------------------------------------- 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 "mpi.h" #include "stdlib.h" #include "string.h" #include "ctype.h" #include "compute.h" #include "atom.h" #include "domain.h" #include "comm.h" #include "group.h" #include "modify.h" #include "fix.h" #include "atom_masks.h" #include "memory.h" #include "error.h" #include "force.h" using namespace LAMMPS_NS; #define DELTA 4 #define BIG MAXTAGINT /* ---------------------------------------------------------------------- */ Compute::Compute(LAMMPS *lmp, int narg, char **arg) : Pointers(lmp) { if (narg < 3) error->all(FLERR,"Illegal compute command"); // compute ID, group, and style // ID must be all alphanumeric chars or underscores int n = strlen(arg[0]) + 1; id = new char[n]; strcpy(id,arg[0]); for (int i = 0; i < n-1; i++) if (!isalnum(id[i]) && id[i] != '_') error->all(FLERR, "Compute ID must be alphanumeric or underscore characters"); igroup = group->find(arg[1]); if (igroup == -1) error->all(FLERR,"Could not find compute group ID"); groupbit = group->bitmask[igroup]; n = strlen(arg[2]) + 1; style = new char[n]; strcpy(style,arg[2]); // set child class defaults scalar_flag = vector_flag = array_flag = 0; peratom_flag = local_flag = 0; tempflag = pressflag = peflag = 0; pressatomflag = peatomflag = 0; tempbias = 0; timeflag = 0; comm_forward = comm_reverse = 0; dynamic = 0; dynamic_group_allow = 1; cudable = 0; invoked_scalar = invoked_vector = invoked_array = -1; invoked_peratom = invoked_local = -1; invoked_flag = 0; // set modify defaults extra_dof = domain->dimension; dynamic_user = 0; // setup list of timesteps ntime = maxtime = 0; tlist = NULL; // setup map for molecule IDs molmap = NULL; datamask = ALL_MASK; datamask_ext = ALL_MASK; + + // force init to zero in case these are used as logicals + + vector = vector_atom = vector_local = NULL; + array = array_atom = array_local = NULL; } /* ---------------------------------------------------------------------- */ Compute::~Compute() { delete [] id; delete [] style; memory->destroy(tlist); memory->destroy(molmap); } /* ---------------------------------------------------------------------- */ void Compute::modify_params(int narg, char **arg) { if (narg == 0) error->all(FLERR,"Illegal compute_modify command"); int iarg = 0; while (iarg < narg) { if (strcmp(arg[iarg],"extra") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal compute_modify command"); extra_dof = force->inumeric(FLERR,arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"dynamic") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal compute_modify command"); if (strcmp(arg[iarg+1],"no") == 0) dynamic_user = 0; else if (strcmp(arg[iarg+1],"yes") == 0) dynamic_user = 1; else error->all(FLERR,"Illegal compute_modify command"); iarg += 2; } else if (strcmp(arg[iarg],"thermo") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal compute_modify command"); if (strcmp(arg[iarg+1],"no") == 0) thermoflag = 0; else if (strcmp(arg[iarg+1],"yes") == 0) thermoflag = 1; else error->all(FLERR,"Illegal compute_modify command"); iarg += 2; } else error->all(FLERR,"Illegal compute_modify command"); } } /* ---------------------------------------------------------------------- calculate adjustment in DOF due to fixes ------------------------------------------------------------------------- */ void Compute::adjust_dof_fix() { fix_dof = 0; for (int i = 0; i < modify->nfix; i++) fix_dof += modify->fix[i]->dof(igroup); } /* ---------------------------------------------------------------------- reset extra_dof to its default value ------------------------------------------------------------------------- */ void Compute::reset_extra_dof() { extra_dof = domain->dimension; } /* ---------------------------------------------------------------------- */ void Compute::reset_extra_compute_fix(const char *) { error->all(FLERR, "Compute does not allow an extra compute or fix to be reset"); } /* ---------------------------------------------------------------------- add ntimestep to list of timesteps the compute will be called on do not add if already in list search from top downward, since list of times is in decreasing order ------------------------------------------------------------------------- */ void Compute::addstep(bigint ntimestep) { // i = location in list to insert ntimestep int i; for (i = ntime-1; i >= 0; i--) { if (ntimestep == tlist[i]) return; if (ntimestep < tlist[i]) break; } i++; // extend list as needed if (ntime == maxtime) { maxtime += DELTA; memory->grow(tlist,maxtime,"compute:tlist"); } // move remainder of list upward and insert ntimestep for (int j = ntime-1; j >= i; j--) tlist[j+1] = tlist[j]; tlist[i] = ntimestep; ntime++; } /* ---------------------------------------------------------------------- return 1/0 if ntimestep is or is not in list of calling timesteps if value(s) on top of list are less than ntimestep, delete them search from top downward, since list of times is in decreasing order ------------------------------------------------------------------------- */ int Compute::matchstep(bigint ntimestep) { for (int i = ntime-1; i >= 0; i--) { if (ntimestep < tlist[i]) return 0; if (ntimestep == tlist[i]) return 1; if (ntimestep > tlist[i]) ntime--; } return 0; } /* ---------------------------------------------------------------------- clean out list of timesteps to call the compute on ------------------------------------------------------------------------- */ void Compute::clearstep() { ntime = 0; } /* ---------------------------------------------------------------------- identify molecule IDs with atoms in group warn if any atom in group has molecule ID = 0 warn if any molecule has only some atoms in group return Ncount = # of molecules with atoms in group set molmap to NULL if molecule IDs include all in range from 1 to Ncount else: molecule IDs range from idlo to idhi set molmap to vector of length idhi-idlo+1 molmap[id-idlo] = index from 0 to Ncount-1 return idlo and idhi ------------------------------------------------------------------------- */ int Compute::molecules_in_group(tagint &idlo, tagint &idhi) { int i; memory->destroy(molmap); molmap = NULL; // find lo/hi molecule ID for any atom in group // warn if atom in group has ID = 0 tagint *molecule = atom->molecule; int *mask = atom->mask; int nlocal = atom->nlocal; tagint lo = BIG; tagint hi = -BIG; int flag = 0; for (i = 0; i < nlocal; i++) if (mask[i] & groupbit) { if (molecule[i] == 0) flag = 1; lo = MIN(lo,molecule[i]); hi = MAX(hi,molecule[i]); } int flagall; MPI_Allreduce(&flag,&flagall,1,MPI_INT,MPI_SUM,world); if (flagall && comm->me == 0) error->warning(FLERR,"Atom with molecule ID = 0 included in " "compute molecule group"); MPI_Allreduce(&lo,&idlo,1,MPI_LMP_TAGINT,MPI_MIN,world); MPI_Allreduce(&hi,&idhi,1,MPI_LMP_TAGINT,MPI_MAX,world); if (idlo == BIG) return 0; // molmap = vector of length nlen // set to 1 for IDs that appear in group across all procs, else 0 tagint nlen_tag = idhi-idlo+1; if (nlen_tag > MAXSMALLINT) error->all(FLERR,"Too many molecules for compute"); int nlen = (int) nlen_tag; memory->create(molmap,nlen,"compute:molmap"); for (i = 0; i < nlen; i++) molmap[i] = 0; for (i = 0; i < nlocal; i++) if (mask[i] & groupbit) molmap[molecule[i]-idlo] = 1; int *molmapall; memory->create(molmapall,nlen,"compute:molmapall"); MPI_Allreduce(molmap,molmapall,nlen,MPI_INT,MPI_MAX,world); // nmolecules = # of non-zero IDs in molmap // molmap[i] = index of molecule, skipping molecules not in group with -1 int nmolecules = 0; for (i = 0; i < nlen; i++) if (molmapall[i]) molmap[i] = nmolecules++; else molmap[i] = -1; memory->destroy(molmapall); // warn if any molecule has some atoms in group and some not in group flag = 0; for (i = 0; i < nlocal; i++) { if (mask[i] & groupbit) continue; if (molecule[i] < idlo || molecule[i] > idhi) continue; if (molmap[molecule[i]-idlo] >= 0) flag = 1; } MPI_Allreduce(&flag,&flagall,1,MPI_INT,MPI_SUM,world); if (flagall && comm->me == 0) error->warning(FLERR, "One or more compute molecules has atoms not in group"); // if molmap simply stores 1 to Nmolecules, then free it if (idlo == 1 && idhi == nmolecules && nlen == nmolecules) { memory->destroy(molmap); molmap = NULL; } return nmolecules; } diff --git a/src/compute_atom_molecule.cpp b/src/compute_atom_molecule.cpp index 891f083ca..8b0d413c2 100644 --- a/src/compute_atom_molecule.cpp +++ b/src/compute_atom_molecule.cpp @@ -1,348 +1,347 @@ /* ---------------------------------------------------------------------- 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 "stdlib.h" #include "compute_atom_molecule.h" #include "atom.h" #include "update.h" #include "modify.h" #include "compute.h" #include "fix.h" #include "input.h" #include "variable.h" #include "memory.h" #include "error.h" using namespace LAMMPS_NS; enum{COMPUTE,FIX,VARIABLE}; #define INVOKED_PERATOM 8 /* ---------------------------------------------------------------------- */ ComputeAtomMolecule:: ComputeAtomMolecule(LAMMPS *lmp, int narg, char **arg) : Compute(lmp, narg, arg) { if (narg < 4) error->all(FLERR,"Illegal compute atom/molecule command"); if (atom->molecular == 0) error->all(FLERR,"Compute atom/molecule requires molecular atom style"); // parse args which = new int[narg-3]; argindex = new int[narg-3]; ids = new char*[narg-3]; value2index = new int[narg-3]; nvalues = 0; int iarg = 3; while (iarg < narg) { if (strncmp(arg[iarg],"c_",2) == 0 || strncmp(arg[iarg],"f_",2) == 0 || strncmp(arg[iarg],"v_",2) == 0) { if (arg[iarg][0] == 'c') which[nvalues] = COMPUTE; else if (arg[iarg][0] == 'f') which[nvalues] = FIX; else if (arg[iarg][0] == 'v') which[nvalues] = VARIABLE; int n = strlen(arg[iarg]); char *suffix = new char[n]; strcpy(suffix,&arg[iarg][2]); char *ptr = strchr(suffix,'['); if (ptr) { if (suffix[strlen(suffix)-1] != ']') error->all(FLERR,"Illegal compute reduce command"); argindex[nvalues] = atoi(ptr+1); *ptr = '\0'; } else argindex[nvalues] = 0; n = strlen(suffix) + 1; ids[nvalues] = new char[n]; strcpy(ids[nvalues],suffix); nvalues++; delete [] suffix; } else error->all(FLERR,"Illegal compute atom/molecule command"); iarg++; } // setup and error check for (int i = 0; i < nvalues; i++) { if (which[i] == COMPUTE) { int icompute = modify->find_compute(ids[i]); if (icompute < 0) error->all(FLERR,"Compute ID for compute atom/molecule does not exist"); if (modify->compute[icompute]->peratom_flag == 0) error->all(FLERR,"Compute atom/molecule compute does not " "calculate per-atom values"); if (argindex[i] == 0 && modify->compute[icompute]->size_peratom_cols != 0) error->all(FLERR,"Compute atom/molecule compute does not " "calculate a per-atom vector"); if (argindex[i] && modify->compute[icompute]->size_peratom_cols == 0) error->all(FLERR,"Compute atom/molecule compute does not " "calculate a per-atom array"); if (argindex[i] && argindex[i] > modify->compute[icompute]->size_peratom_cols) error->all(FLERR,"Compute atom/molecule compute array is " "accessed out-of-range"); } else if (which[i] == FIX) { int ifix = modify->find_fix(ids[i]); if (ifix < 0) error->all(FLERR,"Fix ID for compute atom/molecule does not exist"); if (modify->fix[ifix]->peratom_flag == 0) error->all(FLERR,"Compute atom/molecule fix does not " "calculate per-atom values"); if (argindex[i] == 0 && modify->fix[ifix]->size_peratom_cols != 0) error->all(FLERR,"Compute atom/molecule fix does not " "calculate a per-atom vector"); if (argindex[i] && modify->fix[ifix]->size_peratom_cols == 0) error->all(FLERR,"Compute atom/molecule fix does not " "calculate a per-atom array"); if (argindex[i] && argindex[i] > modify->fix[ifix]->size_peratom_cols) error->all(FLERR, "Compute atom/molecule fix array is accessed out-of-range"); } else if (which[i] == VARIABLE) { int ivariable = input->variable->find(ids[i]); if (ivariable < 0) error->all(FLERR, "Variable name for compute atom/molecule does not exist"); if (input->variable->atomstyle(ivariable) == 0) error->all(FLERR,"Compute atom/molecule variable is not " "atom-style variable"); } } // setup molecule-based data nmolecules = molecules_in_group(idlo,idhi); vone = vector = NULL; aone = array = NULL; if (nvalues == 1) { memory->create(vone,nmolecules,"atom/molecule:vone"); memory->create(vector,nmolecules,"atom/molecule:vector"); vector_flag = 1; size_vector = nmolecules; extvector = 0; } else { memory->create(aone,nmolecules,nvalues,"atom/molecule:aone"); memory->create(array,nmolecules,nvalues,"atom/molecule:array"); array_flag = 1; size_array_rows = nmolecules; size_array_cols = nvalues; extarray = 0; } maxatom = 0; scratch = NULL; } /* ---------------------------------------------------------------------- */ ComputeAtomMolecule::~ComputeAtomMolecule() { delete [] which; delete [] argindex; for (int m = 0; m < nvalues; m++) delete [] ids[m]; delete [] ids; delete [] value2index; memory->destroy(vone); memory->destroy(vector); memory->destroy(aone); memory->destroy(array); memory->destroy(scratch); } /* ---------------------------------------------------------------------- */ void ComputeAtomMolecule::init() { int ntmp = molecules_in_group(idlo,idhi); if (ntmp != nmolecules) error->all(FLERR,"Molecule count changed in compute atom/molecule"); // set indices and check validity of all computes,fixes,variables for (int m = 0; m < nvalues; m++) { if (which[m] == COMPUTE) { int icompute = modify->find_compute(ids[m]); if (icompute < 0) error->all(FLERR,"Compute ID for compute atom/molecule does not exist"); value2index[m] = icompute; } else if (which[m] == FIX) { int ifix = modify->find_fix(ids[m]); if (ifix < 0) error->all(FLERR,"Fix ID for compute atom/molecule does not exist"); value2index[m] = ifix; } else if (which[m] == VARIABLE) { int ivariable = input->variable->find(ids[m]); if (ivariable < 0) error->all(FLERR, "Variable name for compute atom/molecule does not exist"); value2index[m] = ivariable; } else value2index[m] = -1; } } /* ---------------------------------------------------------------------- */ void ComputeAtomMolecule::compute_vector() { int i,j,n; tagint imol; invoked_vector = update->ntimestep; for (n = 0; n < nmolecules; n++) vone[n] = 0.0; compute_one(0); tagint *molecule = atom->molecule; int *mask = atom->mask; int nlocal = atom->nlocal; j = 0; for (i = 0; i < nlocal; i++) { if (mask[i] & groupbit) { imol = molecule[i]; if (molmap) imol = molmap[imol-idlo]; else imol--; vone[imol] += peratom[j]; } j += nstride; } int me; MPI_Comm_rank(world,&me); MPI_Allreduce(vone,vector,nmolecules,MPI_DOUBLE,MPI_SUM,world); } /* ---------------------------------------------------------------------- */ void ComputeAtomMolecule::compute_array() { int i,j,m,n; tagint imol; invoked_array = update->ntimestep; tagint *molecule = atom->molecule; int *mask = atom->mask; int nlocal = atom->nlocal; for (m = 0; m < nvalues; m++) { for (n = 0; n < nmolecules; n++) aone[n][m] = 0.0; compute_one(m); j = 0; for (i = 0; i < nlocal; i++) { if (mask[i] & groupbit) { imol = molecule[i]; if (molmap) imol = molmap[imol-idlo]; else imol--; aone[imol][m] += peratom[j]; } j += nstride; } } if (array) MPI_Allreduce(&aone[0][0],&array[0][0],nvalues*nmolecules, MPI_DOUBLE,MPI_SUM,world); } /* ---------------------------------------------------------------------- calculate per-atom values for one input M invoke the appropriate compute,fix,variable reallocate scratch if necessary for per-atom variable scratch space ------------------------------------------------------------------------- */ void ComputeAtomMolecule::compute_one(int m) { int vidx = value2index[m]; int aidx = argindex[m]; // invoke compute if not previously invoked if (which[m] == COMPUTE) { Compute *compute = modify->compute[vidx]; if (!(compute->invoked_flag & INVOKED_PERATOM)) { compute->compute_peratom(); compute->invoked_flag |= INVOKED_PERATOM; } if (aidx == 0) { peratom = compute->vector_atom; nstride = 1; } else { - if (compute->array_atom) peratom = &compute->array_atom[0][aidx-1]; - else peratom = NULL; - nstride = compute->size_array_cols; + peratom = &compute->array_atom[0][aidx-1]; + nstride = compute->size_peratom_cols; } // access fix fields, check if fix frequency is a match } else if (which[m] == FIX) { if (update->ntimestep % modify->fix[vidx]->peratom_freq) error->all(FLERR,"Fix used in compute atom/molecule not computed " "at compatible time"); Fix *fix = modify->fix[vidx]; if (aidx == 0) { peratom = fix->vector_atom; nstride = 1; } else { peratom = &fix->array_atom[0][aidx-1]; - nstride = fix->size_array_cols; + nstride = fix->size_peratom_cols; } // evaluate atom-style variable } else if (which[m] == VARIABLE) { if (atom->nlocal > maxatom) { maxatom = atom->nmax; memory->destroy(scratch); memory->create(scratch,maxatom,"atom/molecule:scratch"); } peratom = scratch; input->variable->compute_atom(vidx,igroup,peratom,1,0); nstride = 1; } } /* ---------------------------------------------------------------------- memory usage of local data ------------------------------------------------------------------------- */ double ComputeAtomMolecule::memory_usage() { double bytes = (bigint) nmolecules * 2*nvalues * sizeof(double); if (molmap) bytes += (idhi-idlo+1) * sizeof(int); bytes += maxatom * sizeof(double); return bytes; }