// clang-format off /* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, 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. ------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- Contributing author: Naveen Michaud-Agrawal (Johns Hopkins U) Paul Crozier (SNL) ------------------------------------------------------------------------- */ #include "fix_spring_rg.h" #include "atom.h" #include "comm.h" #include "domain.h" #include "error.h" #include "group.h" #include "respa.h" #include "update.h" #include using namespace LAMMPS_NS; using namespace FixConst; /* ---------------------------------------------------------------------- */ FixSpringRG::FixSpringRG(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) { if (narg != 5) error->all(FLERR,"Illegal fix spring/rg command"); k = utils::numeric(FLERR,arg[3],false,lmp); rg0_flag = 0; if (strcmp(arg[4],"NULL") == 0) rg0_flag = 1; else rg0 = utils::numeric(FLERR,arg[4],false,lmp); restart_global = 1; scalar_flag = 1; restart_global = 1; dynamic_group_allow = 1; respa_level_support = 1; ilevel_respa = 0; } /* ---------------------------------------------------------------------- */ int FixSpringRG::setmask() { int mask = 0; mask |= POST_FORCE; mask |= POST_FORCE_RESPA; return mask; } /* ---------------------------------------------------------------------- */ void FixSpringRG::init() { masstotal = group->mass(igroup); // Compute current Rg // only occurs on 1st run if (rg0_flag) { double xcm[3]; group->xcm(igroup,masstotal,xcm); rg0 = group->gyration(igroup,masstotal,xcm); rg0_flag = 0; } if (utils::strmatch(update->integrate_style,"^respa")) { ilevel_respa = ((Respa *) update->integrate)->nlevels-1; if (respa_level >= 0) ilevel_respa = MIN(respa_level,ilevel_respa); } } /* ---------------------------------------------------------------------- */ void FixSpringRG::setup(int vflag) { if (utils::strmatch(update->integrate_style,"^verlet")) post_force(vflag); else { ((Respa *) update->integrate)->copy_flevel_f(ilevel_respa); post_force_respa(vflag,ilevel_respa,0); ((Respa *) update->integrate)->copy_f_flevel(ilevel_respa); } } /* ---------------------------------------------------------------------- */ void FixSpringRG::post_force(int /*vflag*/) { // compute current Rg and center-of-mass double xcm[3]; if (group->dynamic[igroup]) masstotal = group->mass(igroup); group->xcm(igroup,masstotal,xcm); double rg = group->gyration(igroup,masstotal,xcm); // apply restoring force to atoms in group // f = -k*(r-r0)*mass/masstotal double dx,dy,dz,term1; double **f = atom->f; 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; double massfrac; double unwrap[3]; for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { domain->unmap(x[i],image[i],unwrap); dx = unwrap[0] - xcm[0]; dy = unwrap[1] - xcm[1]; dz = unwrap[2] - xcm[2]; term1 = 2.0 * k * (1.0 - rg0/rg); if (masstotal > 0.0) { if (rmass) massfrac = rmass[i]/masstotal; else massfrac = mass[type[i]]/masstotal; f[i][0] -= term1*dx*massfrac; f[i][1] -= term1*dy*massfrac; f[i][2] -= term1*dz*massfrac; } } } /* ---------------------------------------------------------------------- */ void FixSpringRG::post_force_respa(int vflag, int ilevel, int /*iloop*/) { if (ilevel == ilevel_respa) post_force(vflag); } /* ---------------------------------------------------------------------- pack entire state of Fix into one write ------------------------------------------------------------------------- */ void FixSpringRG::write_restart(FILE *fp) { int n = 0; double list[1]; list[n++] = rg0; if (comm->me == 0) { int size = n * sizeof(double); fwrite(&size,sizeof(int),1,fp); fwrite(list,sizeof(double),n,fp); } } /* ---------------------------------------------------------------------- use state info from restart file to restart the Fix ------------------------------------------------------------------------- */ void FixSpringRG::restart(char *buf) { int n = 0; double *list = (double *) buf; rg0 = list[n++]; rg0_flag = 0; } /* ---------------------------------------------------------------------- return reference radius of gyration ------------------------------------------------------------------------- */ double FixSpringRG::compute_scalar() { return rg0; }