diff --git a/src/displace_atoms.cpp b/src/displace_atoms.cpp index df7850cf37..2a72958bc9 100644 --- a/src/displace_atoms.cpp +++ b/src/displace_atoms.cpp @@ -26,6 +26,9 @@ #include "random_park.h" #include "error.h" #include "force.h" +#include "input.h" +#include "variable.h" +#include "memory.h" using namespace LAMMPS_NS; using namespace MathConst; @@ -34,7 +37,17 @@ enum{MOVE,RAMP,RANDOM,ROTATE}; /* ---------------------------------------------------------------------- */ -DisplaceAtoms::DisplaceAtoms(LAMMPS *lmp) : Pointers(lmp) {} +DisplaceAtoms::DisplaceAtoms(LAMMPS *lmp) : Pointers(lmp) +{ + mvec = NULL; +} + +/* ---------------------------------------------------------------------- */ + +DisplaceAtoms::~DisplaceAtoms() +{ + memory->destroy(mvec); +} /* ---------------------------------------------------------------------- */ @@ -53,9 +66,9 @@ void DisplaceAtoms::command(int narg, char **arg) // group and style - int igroup = group->find(arg[0]); + igroup = group->find(arg[0]); if (igroup == -1) error->all(FLERR,"Could not find displace_atoms group ID"); - int groupbit = group->bitmask[igroup]; + groupbit = group->bitmask[igroup]; int style = -1; if (strcmp(arg[1],"move") == 0) style = MOVE; @@ -85,25 +98,12 @@ void DisplaceAtoms::command(int narg, char **arg) } else xscale = yscale = zscale = 1.0; - // move atoms by 3-vector + // move atoms by 3-vector or specified variable(s) if (style == MOVE) { - - double delx = xscale*force->numeric(FLERR,arg[2]); - double dely = yscale*force->numeric(FLERR,arg[3]); - double delz = zscale*force->numeric(FLERR,arg[4]); - - double **x = atom->x; - int *mask = atom->mask; - int nlocal = atom->nlocal; - - for (i = 0; i < nlocal; i++) { - if (mask[i] & groupbit) { - x[i][0] += delx; - x[i][1] += dely; - x[i][2] += delz; - } - } + move(0,arg[2],xscale); + move(1,arg[3],yscale); + move(2,arg[4],zscale); } // move atoms in ramped fashion @@ -286,6 +286,42 @@ void DisplaceAtoms::command(int narg, char **arg) } } +/* ---------------------------------------------------------------------- + move atoms either by specified numeric displacement or variable evaluation +------------------------------------------------------------------------- */ + +void DisplaceAtoms::move(int idim, char *arg, double scale) +{ + double **x = atom->x; + int *mask = atom->mask; + int nlocal = atom->nlocal; + + if (strstr(arg,"v_") != arg) { + double delta = scale*force->numeric(FLERR,arg); + for (int i = 0; i < nlocal; i++) + if (mask[i] & groupbit) x[i][idim] += delta; + + } else { + int n = strlen(&arg[2]) + 1; + char *vstr = new char[n]; + strcpy(vstr,&arg[2]); + int ivar = input->variable->find(vstr); + if (ivar < 0) + error->all(FLERR,"Variable name for displace_atoms does not exist"); + + if (input->variable->equalstyle(ivar)) { + double delta = scale * input->variable->compute_equal(ivar); + for (int i = 0; i < nlocal; i++) + if (mask[i] & groupbit) x[i][idim] += delta; + } else if (input->variable->atomstyle(ivar)) { + if (mvec == NULL) memory->create(mvec,nlocal,"displace_atoms:mvec"); + input->variable->compute_atom(ivar,igroup,mvec,1,0); + for (int i = 0; i < nlocal; i++) + if (mask[i] & groupbit) x[i][idim] += scale*mvec[i]; + } else error->all(FLERR,"Variable for displace_atoms is invalid style"); + } +} + /* ---------------------------------------------------------------------- parse optional parameters at end of displace_atoms input line ------------------------------------------------------------------------- */ diff --git a/src/displace_atoms.h b/src/displace_atoms.h index 5e70c5d801..d84f39e760 100644 --- a/src/displace_atoms.h +++ b/src/displace_atoms.h @@ -27,10 +27,15 @@ namespace LAMMPS_NS { class DisplaceAtoms : protected Pointers { public: DisplaceAtoms(class LAMMPS *); + ~DisplaceAtoms(); void command(int, char **); private: + int igroup,groupbit; int scaleflag; + double *mvec; + + void move(int, char *, double); void options(int, char **); }; diff --git a/src/fix_addforce.cpp b/src/fix_addforce.cpp index 4e57a604af..0f65873265 100644 --- a/src/fix_addforce.cpp +++ b/src/fix_addforce.cpp @@ -112,7 +112,7 @@ FixAddForce::FixAddForce(LAMMPS *lmp, int narg, char **arg) : force_flag = 0; foriginal[0] = foriginal[1] = foriginal[2] = foriginal[3] = 0.0; - maxatom = atom->nmax; + maxatom = 1; memory->create(sforce,maxatom,4,"addforce:sforce"); } @@ -371,6 +371,6 @@ double FixAddForce::compute_vector(int n) double FixAddForce::memory_usage() { double bytes = 0.0; - if (varflag == ATOM) bytes = atom->nmax*4 * sizeof(double); + if (varflag == ATOM) bytes = maxatom*4 * sizeof(double); return bytes; } diff --git a/src/fix_setforce.cpp b/src/fix_setforce.cpp index a6e1ecb972..6ba34ce822 100644 --- a/src/fix_setforce.cpp +++ b/src/fix_setforce.cpp @@ -99,7 +99,7 @@ FixSetForce::FixSetForce(LAMMPS *lmp, int narg, char **arg) : force_flag = 0; foriginal[0] = foriginal[1] = foriginal[2] = 0.0; - maxatom = atom->nmax; + maxatom = 1; memory->create(sforce,maxatom,3,"setforce:sforce"); } @@ -342,6 +342,6 @@ double FixSetForce::compute_vector(int n) double FixSetForce::memory_usage() { double bytes = 0.0; - if (varflag == ATOM) bytes = atom->nmax*3 * sizeof(double); + if (varflag == ATOM) bytes = maxatom*3 * sizeof(double); return bytes; }