diff --git a/src/fix_adapt.cpp b/src/fix_adapt.cpp index caeef8bfb..701e16ad6 100644 --- a/src/fix_adapt.cpp +++ b/src/fix_adapt.cpp @@ -1,223 +1,227 @@ /* ---------------------------------------------------------------------- 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 "string.h" #include "stdlib.h" #include "fix_adapt.h" #include "atom.h" #include "update.h" #include "modify.h" #include "force.h" #include "pair.h" #include "input.h" #include "variable.h" #include "error.h" using namespace LAMMPS_NS; enum{PAIR,ATOM}; enum{DIAMETER}; /* ---------------------------------------------------------------------- */ FixAdapt::FixAdapt(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) { if (narg < 4) error->all("Illegal fix adapt command"); nevery = atoi(arg[3]); if (nevery <= 0) error->all("Illegal fix adapt command"); // count # of adaptations nadapt = 0; int iarg = 4; while (iarg < narg) { if (strcmp(arg[iarg],"pair") == 0) { if (iarg+6 > narg) error->all("Illegal fix adapt command"); nadapt++; iarg += 6; } else if (strcmp(arg[iarg],"atom") == 0) { if (iarg+3 > narg) error->all("Illegal fix adapt command"); nadapt++; iarg += 3; } else error->all("Illegal fix adapt command"); } // allocate per-adapt vectors which = new int[nadapt]; pair = new char*[nadapt]; param = new char*[nadapt]; ilo = new int[nadapt]; ihi = new int[nadapt]; jlo = new int[nadapt]; jhi = new int[nadapt]; var = new char*[nadapt]; ivar = new int[nadapt]; pairptr = new Pair*[nadapt]; pairindex = new int[nadapt]; awhich = new int[nadapt]; // parse keywords nadapt = 0; iarg = 4; while (iarg < narg) { if (strcmp(arg[iarg],"pair") == 0) { if (iarg+6 > narg) error->all("Illegal fix adapt command"); which[nadapt] = PAIR; int n = strlen(arg[iarg+1]) + 1; pair[nadapt] = new char[n]; strcpy(pair[nadapt],arg[iarg+1]); n = strlen(arg[iarg+2]) + 1; param[nadapt] = new char[n]; strcpy(param[nadapt],arg[iarg+2]); force->bounds(arg[iarg+3],atom->ntypes,ilo[nadapt],ihi[nadapt]); force->bounds(arg[iarg+4],atom->ntypes,jlo[nadapt],jhi[nadapt]); - n = strlen(arg[iarg+5]) + 1; - var[nadapt] = new char[n]; - strcpy(var[nadapt],arg[iarg+5]); + if (strstr(arg[iarg+5],"v_") == arg[iarg+1]) { + n = strlen(&arg[iarg+1][2]) + 1; + var[nadapt] = new char[n]; + strcpy(var[nadapt],&arg[iarg+5][2]); + } else error->all("Illegal fix adapt command"); nadapt++; iarg += 6; } else if (strcmp(arg[iarg],"atom") == 0) { if (iarg+3 > narg) error->all("Illegal fix adapt command"); which[nadapt] = ATOM; int n = strlen(arg[iarg+1]) + 1; param[nadapt] = new char[n]; strcpy(param[nadapt],arg[iarg+1]); - n = strlen(arg[iarg+2]) + 1; - var[nadapt] = new char[n]; - strcpy(var[nadapt],arg[iarg+2]); + if (strstr(arg[iarg+2],"v_") == arg[iarg+1]) { + n = strlen(&arg[iarg+1][2]) + 1; + var[nadapt] = new char[n]; + strcpy(var[nadapt],&arg[iarg+2][2]); + } else error->all("Illegal fix adapt command"); nadapt++; iarg += 3; } else error->all("Illegal fix adapt command"); } } /* ---------------------------------------------------------------------- */ FixAdapt::~FixAdapt() { for (int i = 0; i < nadapt; i++) { if (which[i] == PAIR) delete [] pair[i]; delete [] param[i]; delete [] var[i]; } delete [] which; delete [] pair; delete [] param; delete [] ilo; delete [] ihi; delete [] jlo; delete [] jhi; delete [] var; delete [] ivar; delete [] pairptr; delete [] pairindex; delete [] awhich; } /* ---------------------------------------------------------------------- */ int FixAdapt::setmask() { int mask = 0; mask |= PRE_FORCE; return mask; } /* ---------------------------------------------------------------------- */ void FixAdapt::init() { // error checks for (int m = 0; m < nadapt; m++) { if (which[m] == PAIR) { pairptr[m] = force->pair_match(pair[m],1); if (pairptr[m] == NULL) error->all("Fix adapt pair style does not exist"); pairindex[m] = pairptr[m]->pre_adapt(param[m],ilo[m],ihi[m],jlo[m],jhi[m]); if (pairindex[m] == -1) error->all("Fix adapt pair parameter is not recognized"); if (pairindex[m] == -2) error->all("Fix adapt pair types are not valid"); } else if (which[m] == ATOM) { if (strcmp(param[m],"diameter") == 0) { awhich[m] = DIAMETER; if (!atom->radius_flag) error->all("Fix adapt requires atom attribute radius"); } else error->all("Fix adapt atom attribute is not recognized"); } ivar[m] = input->variable->find(var[m]); if (ivar[m] < 0) error->all("Variable name for fix adapt does not exist"); if (!input->variable->equalstyle(ivar[m])) error->all("Variable for fix adapt is not equal style"); } } /* ---------------------------------------------------------------------- */ void FixAdapt::setup_pre_force(int vflag) { pre_force(vflag); } /* ---------------------------------------------------------------------- */ void FixAdapt::pre_force(int vflag) { if (update->ntimestep % nevery) return; // variable evaluation may invoke computes so wrap with clear/add modify->clearstep_compute(); for (int m = 0; m < nadapt; m++) { double value = input->variable->compute_equal(ivar[m]); if (which[m] == PAIR) pairptr[m]->adapt(pairindex[m],ilo[m],ihi[m],jlo[m],jhi[m],value); else if (which[m] == ATOM) { // set radius from diameter // set rmass if both rmass and density are defined if (awhich[m] == DIAMETER) { int mflag = 0; if (atom->rmass_flag && atom->density_flag) mflag = 1; double PI = 4.0*atan(1.0); double *radius = atom->radius; double *rmass = atom->rmass; double *density = atom->density; int *mask = atom->mask; int nlocal = atom->nlocal; for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { radius[i] = 0.5*value; rmass[i] = 4.0*PI/3.0 * radius[i]*radius[i]*radius[i] * density[i]; } } } } modify->addstep_compute(update->ntimestep + nevery); } diff --git a/src/fix_addforce.cpp b/src/fix_addforce.cpp index 08c041f7b..23bc116ba 100644 --- a/src/fix_addforce.cpp +++ b/src/fix_addforce.cpp @@ -1,325 +1,327 @@ /* ---------------------------------------------------------------------- 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 "fix_addforce.h" #include "atom.h" #include "update.h" #include "domain.h" #include "region.h" #include "respa.h" #include "input.h" #include "variable.h" #include "memory.h" #include "error.h" using namespace LAMMPS_NS; enum{NONE,CONSTANT,EQUAL,ATOM}; /* ---------------------------------------------------------------------- */ FixAddForce::FixAddForce(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) { if (narg < 6) error->all("Illegal fix addforce command"); scalar_flag = 1; vector_flag = 1; size_vector = 3; global_freq = 1; extscalar = 1; extvector = 1; xstr = ystr = zstr = NULL; if (strstr(arg[3],"v_") == arg[3]) { int n = strlen(&arg[3][2]) + 1; xstr = new char[n]; strcpy(xstr,&arg[3][2]); } else { xvalue = atof(arg[3]); xstyle = CONSTANT; } if (strstr(arg[4],"v_") == arg[4]) { int n = strlen(&arg[4][2]) + 1; ystr = new char[n]; strcpy(ystr,&arg[4][2]); } else { yvalue = atof(arg[4]); ystyle = CONSTANT; } if (strstr(arg[5],"v_") == arg[5]) { int n = strlen(&arg[5][2]) + 1; zstr = new char[n]; strcpy(zstr,&arg[5][2]); } else { zvalue = atof(arg[5]); zstyle = CONSTANT; } // optional args iregion = -1; estr = NULL; int iarg = 6; while (iarg < narg) { if (strcmp(arg[iarg],"region") == 0) { if (iarg+2 > narg) error->all("Illegal fix addforce command"); iregion = domain->find_region(arg[iarg+1]); if (iregion == -1) error->all("Fix addforce region ID does not exist"); iarg += 2; } else if (strcmp(arg[iarg],"energy") == 0) { if (iarg+2 > narg) error->all("Illegal fix addforce command"); - int n = strlen(arg[iarg+1]) + 1; - estr = new char[n]; - strcpy(estr,arg[iarg+1]); + if (strstr(arg[iarg+1],"v_") == arg[iarg+1]) { + int n = strlen(&arg[iarg+1][2]) + 1; + estr = new char[n]; + strcpy(estr,&arg[iarg+1][2]); + } else error->all("Illegal fix addforce command"); iarg += 2; } else error->all("Illegal fix addforce command"); } force_flag = 0; foriginal[0] = foriginal[1] = foriginal[2] = foriginal[3] = 0.0; maxatom = 0; sforce = NULL; } /* ---------------------------------------------------------------------- */ FixAddForce::~FixAddForce() { delete [] xstr; delete [] ystr; delete [] zstr; delete [] estr; memory->destroy_2d_double_array(sforce); } /* ---------------------------------------------------------------------- */ int FixAddForce::setmask() { int mask = 0; mask |= POST_FORCE; mask |= THERMO_ENERGY; mask |= POST_FORCE_RESPA; mask |= MIN_POST_FORCE; return mask; } /* ---------------------------------------------------------------------- */ void FixAddForce::init() { // check variables if (xstr) { xvar = input->variable->find(xstr); if (xvar < 0) error->all("Variable name for fix addforce does not exist"); if (input->variable->equalstyle(xvar)) xstyle = EQUAL; else if (input->variable->atomstyle(xvar)) xstyle = ATOM; else error->all("Variable for fix setforce is invalid style"); } if (ystr) { yvar = input->variable->find(ystr); if (yvar < 0) error->all("Variable name for fix addforce does not exist"); if (input->variable->equalstyle(yvar)) ystyle = EQUAL; else if (input->variable->atomstyle(yvar)) ystyle = ATOM; else error->all("Variable for fix setforce is invalid style"); } if (zstr) { zvar = input->variable->find(zstr); if (zvar < 0) error->all("Variable name for fix addforce does not exist"); if (input->variable->equalstyle(zvar)) zstyle = EQUAL; else if (input->variable->atomstyle(zvar)) zstyle = ATOM; else error->all("Variable for fix setforce is invalid style"); } if (estr) { evar = input->variable->find(estr); if (evar < 0) error->all("Variable name for fix addforce does not exist"); if (input->variable->atomstyle(evar)) estyle = ATOM; else error->all("Variable for fix setforce is invalid style"); } else estyle = NONE; if (xstyle == ATOM || ystyle == ATOM || zstyle == ATOM) varflag = ATOM; else if (xstyle == EQUAL || ystyle == EQUAL || zstyle == EQUAL) varflag = EQUAL; else varflag = CONSTANT; if (varflag == CONSTANT && estyle != NONE) error->all("Cannot use variable energy with " "constant force in fix addforce"); if ((varflag == EQUAL || varflag == ATOM) && update->whichflag == 2 && estyle == NONE) error->all("Must use variable energy with fix addforce"); if (strcmp(update->integrate_style,"respa") == 0) nlevels_respa = ((Respa *) update->integrate)->nlevels; } /* ---------------------------------------------------------------------- */ void FixAddForce::setup(int vflag) { if (strcmp(update->integrate_style,"verlet") == 0) post_force(vflag); else { ((Respa *) update->integrate)->copy_flevel_f(nlevels_respa-1); post_force_respa(vflag,nlevels_respa-1,0); ((Respa *) update->integrate)->copy_f_flevel(nlevels_respa-1); } } /* ---------------------------------------------------------------------- */ void FixAddForce::min_setup(int vflag) { post_force(vflag); } /* ---------------------------------------------------------------------- */ void FixAddForce::post_force(int vflag) { double **x = atom->x; double **f = atom->f; int *mask = atom->mask; int nlocal = atom->nlocal; // reallocate sforce array if necessary if ((varflag == ATOM || estyle == ATOM) && nlocal > maxatom) { maxatom = atom->nmax; memory->destroy_2d_double_array(sforce); sforce = memory->create_2d_double_array(maxatom,4,"addforce:sforce"); } // foriginal[0] = "potential energy" for added force // foriginal[123] = force on atoms before extra force added foriginal[0] = foriginal[1] = foriginal[2] = foriginal[3] = 0.0; force_flag = 0; // constant force // potential energy = - x dot f if (varflag == CONSTANT) { for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { if (iregion >= 0 && !domain->regions[iregion]->match(x[i][0],x[i][1],x[i][2])) continue; foriginal[0] -= xvalue*x[i][0] + yvalue*x[i][1] + zvalue*x[i][2]; foriginal[1] += f[i][0]; foriginal[2] += f[i][1]; foriginal[3] += f[i][2]; f[i][0] += xvalue; f[i][1] += yvalue; f[i][2] += zvalue; } // variable force // potential energy = evar if defined, else 0.0 } else { if (xstyle == EQUAL) xvalue = input->variable->compute_equal(xvar); else if (xstyle == ATOM) input->variable->compute_atom(xvar,igroup,&sforce[0][0],4,0); if (ystyle == EQUAL) yvalue = input->variable->compute_equal(yvar); else if (ystyle == ATOM) input->variable->compute_atom(yvar,igroup,&sforce[0][1],4,0); if (zstyle == EQUAL) zvalue = input->variable->compute_equal(zvar); else if (zstyle == ATOM) input->variable->compute_atom(zvar,igroup,&sforce[0][2],4,0); if (estyle == ATOM) input->variable->compute_atom(evar,igroup,&sforce[0][3],4,0); for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { if (iregion >= 0 && !domain->regions[iregion]->match(x[i][0],x[i][1],x[i][2])) continue; if (estyle == ATOM) foriginal[0] += sforce[i][3]; foriginal[1] += f[i][0]; foriginal[2] += f[i][1]; foriginal[3] += f[i][2]; if (xstyle == ATOM) f[i][0] += sforce[i][0]; else if (xstyle) f[i][0] += xvalue; if (ystyle == ATOM) f[i][1] += sforce[i][1]; else if (ystyle) f[i][1] += yvalue; if (zstyle == ATOM) f[i][2] += sforce[i][2]; else if (zstyle) f[i][2] += zvalue; } } } /* ---------------------------------------------------------------------- */ void FixAddForce::post_force_respa(int vflag, int ilevel, int iloop) { if (ilevel == nlevels_respa-1) post_force(vflag); } /* ---------------------------------------------------------------------- */ void FixAddForce::min_post_force(int vflag) { post_force(vflag); } /* ---------------------------------------------------------------------- potential energy of added force ------------------------------------------------------------------------- */ double FixAddForce::compute_scalar() { // only sum across procs one time if (force_flag == 0) { MPI_Allreduce(foriginal,foriginal_all,4,MPI_DOUBLE,MPI_SUM,world); force_flag = 1; } return foriginal_all[0]; } /* ---------------------------------------------------------------------- return components of total force on fix group before force was changed ------------------------------------------------------------------------- */ double FixAddForce::compute_vector(int n) { // only sum across procs one time if (force_flag == 0) { MPI_Allreduce(foriginal,foriginal_all,4,MPI_DOUBLE,MPI_SUM,world); force_flag = 1; } return foriginal_all[n+1]; } /* ---------------------------------------------------------------------- memory usage of local atom-based array ------------------------------------------------------------------------- */ double FixAddForce::memory_usage() { double bytes = 0.0; if (varflag == ATOM) bytes = atom->nmax*4 * sizeof(double); return bytes; }