From 3cd1341e64463361454354daffddf91ebeaa552e Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sun, 28 Jun 2020 18:40:48 -0400 Subject: [PATCH] make more use of convenience functions for adding fixes and computes --- src/balance.cpp | 2 +- src/compute_vacf.cpp | 21 +++++++-------------- src/fix_adapt.cpp | 32 +++++++++++--------------------- src/group.cpp | 35 ++++++++++------------------------- src/min.cpp | 11 +++-------- src/read_restart.cpp | 16 +++------------- src/respa.cpp | 19 ++++--------------- src/thermo.cpp | 2 +- 8 files changed, 40 insertions(+), 98 deletions(-) diff --git a/src/balance.cpp b/src/balance.cpp index 6b01ce4dae..9731a66f51 100644 --- a/src/balance.cpp +++ b/src/balance.cpp @@ -494,7 +494,7 @@ void Balance::weight_storage(char *prefix) if (prefix) cmd = prefix; cmd += "IMBALANCE_WEIGHTS"; - int ifix = modify->find_fix(cmd.c_str()); + int ifix = modify->find_fix(cmd); if (ifix < 1) { cmd += " all STORE peratom 0 1"; modify->add_fix(cmd); diff --git a/src/compute_vacf.cpp b/src/compute_vacf.cpp index 5123e3aab4..ec19137a0e 100644 --- a/src/compute_vacf.cpp +++ b/src/compute_vacf.cpp @@ -14,12 +14,14 @@ #include "compute_vacf.h" #include #include +#include #include "atom.h" #include "update.h" #include "group.h" #include "modify.h" #include "fix_store.h" #include "error.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -39,21 +41,12 @@ ComputeVACF::ComputeVACF(LAMMPS *lmp, int narg, char **arg) : // create a new fix STORE style // id = compute-ID + COMPUTE_STORE, fix group = compute group - int n = strlen(id) + strlen("_COMPUTE_STORE") + 1; - id_fix = new char[n]; - strcpy(id_fix,id); - strcat(id_fix,"_COMPUTE_STORE"); - - char **newarg = new char*[6]; - newarg[0] = id_fix; - newarg[1] = group->names[igroup]; - newarg[2] = (char *) "STORE"; - newarg[3] = (char *) "peratom"; - newarg[4] = (char *) "1"; - newarg[5] = (char *) "3"; - modify->add_fix(6,newarg); + std::string fixcmd = id + std::string("_COMPUTE_STORE"); + id_fix = new char[fixcmd.size()+1]; + strcpy(id_fix,fixcmd.c_str()); + fixcmd += fmt::format(" {} STORE peratom 1 3", group->names[igroup]); + modify->add_fix(fixcmd); fix = (FixStore *) modify->fix[modify->nfix-1]; - delete [] newarg; // store current velocities in fix store array // skip if reset from restart file diff --git a/src/fix_adapt.cpp b/src/fix_adapt.cpp index e7254e77d7..848b0b99a5 100644 --- a/src/fix_adapt.cpp +++ b/src/fix_adapt.cpp @@ -31,6 +31,7 @@ #include "memory.h" #include "error.h" #include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; using namespace FixConst; @@ -265,20 +266,12 @@ void FixAdapt::post_constructor() id_fix_diam = NULL; id_fix_chg = NULL; - char **newarg = new char*[6]; - newarg[1] = group->names[igroup]; - newarg[2] = (char *) "STORE"; - newarg[3] = (char *) "peratom"; - newarg[4] = (char *) "1"; - newarg[5] = (char *) "1"; - if (diamflag && atom->radius_flag) { - int n = strlen(id) + strlen("_FIX_STORE_DIAM") + 1; - id_fix_diam = new char[n]; - strcpy(id_fix_diam,id); - strcat(id_fix_diam,"_FIX_STORE_DIAM"); - newarg[0] = id_fix_diam; - modify->add_fix(6,newarg); + std::string fixcmd = id + std::string("_FIX_STORE_DIAM"); + id_fix_diam = new char[fixcmd.size()+1]; + strcpy(id_fix_diam,fixcmd.c_str()); + fixcmd += fmt::format(" {} STORE peratom 1 1",group->names[igroup]); + modify->add_fix(fixcmd); fix_diam = (FixStore *) modify->fix[modify->nfix-1]; if (fix_diam->restart_reset) fix_diam->restart_reset = 0; @@ -296,12 +289,11 @@ void FixAdapt::post_constructor() } if (chgflag && atom->q_flag) { - int n = strlen(id) + strlen("_FIX_STORE_CHG") + 1; - id_fix_chg = new char[n]; - strcpy(id_fix_chg,id); - strcat(id_fix_chg,"_FIX_STORE_CHG"); - newarg[0] = id_fix_chg; - modify->add_fix(6,newarg); + std::string fixcmd = id + std::string("_FIX_STORE_CHG"); + id_fix_chg = new char[fixcmd.size()+1]; + strcpy(id_fix_chg,fixcmd.c_str()); + fixcmd += fmt::format(" {} STORE peratom 1 1",group->names[igroup]); + modify->add_fix(fixcmd); fix_chg = (FixStore *) modify->fix[modify->nfix-1]; if (fix_chg->restart_reset) fix_chg->restart_reset = 0; @@ -317,8 +309,6 @@ void FixAdapt::post_constructor() } } } - - delete [] newarg; } /* ---------------------------------------------------------------------- */ diff --git a/src/group.cpp b/src/group.cpp index 9d070e319c..be3ca5219a 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -124,11 +124,8 @@ void Group::assign(int narg, char **arg) int bits = inversemask[igroup]; for (i = 0; i < nlocal; i++) mask[i] &= bits; - if (dynamic[igroup]) { - std::string fixID = "GROUP_"; - fixID += names[igroup]; - modify->delete_fix(fixID.c_str()); - } + if (dynamic[igroup]) + modify->delete_fix(std::string("GROUP_") + names[igroup]); delete [] names[igroup]; names[igroup] = NULL; @@ -491,24 +488,15 @@ void Group::assign(int narg, char **arg) // if group is already dynamic, delete existing FixGroup - if (dynamic[igroup]) { - std::string fixID = "GROUP_"; - fixID += names[igroup]; - modify->delete_fix(fixID.c_str()); - } + if (dynamic[igroup]) + modify->delete_fix(std::string("GROUP_") + names[igroup]); dynamic[igroup] = 1; - std::string fixID = "GROUP_"; - fixID += names[igroup]; - - char **newarg = new char*[narg]; - newarg[0] = (char *)fixID.c_str(); - newarg[1] = arg[2]; - newarg[2] = (char *) "GROUP"; - for (int i = 3; i < narg; i++) newarg[i] = arg[i]; - modify->add_fix(narg,newarg); - delete [] newarg; + std::string fixcmd = "GROUP_"; + fixcmd += fmt::format("{} {} GROUP",names[igroup],arg[2]); + for (int i = 3; i < narg; i++) fixcmd += std::string(" ") + arg[i]; + modify->add_fix(fixcmd); // style = static // remove dynamic FixGroup if necessary @@ -517,11 +505,8 @@ void Group::assign(int narg, char **arg) if (narg != 2) error->all(FLERR,"Illegal group command"); - if (dynamic[igroup]) { - std::string fixID = "GROUP_"; - fixID += names[igroup]; - modify->delete_fix(fixID.c_str()); - } + if (dynamic[igroup]) + modify->delete_fix(std::string("GROUP_") + names[igroup]); dynamic[igroup] = 0; diff --git a/src/min.cpp b/src/min.cpp index 6bdadfb355..b14137f689 100644 --- a/src/min.cpp +++ b/src/min.cpp @@ -113,18 +113,13 @@ Min::~Min() void Min::init() { if (lmp->kokkos && !kokkosable) - error->all(FLERR,"Must use a Kokkos-enabled min style (e.g. min_style cg/kk) " - "with Kokkos minimize"); + error->all(FLERR,"Must use a Kokkos-enabled min style " + "(e.g. min_style cg/kk) with Kokkos minimize"); // create fix needed for storing atom-based quantities // will delete it at end of run - char **fixarg = new char*[3]; - fixarg[0] = (char *) "MINIMIZE"; - fixarg[1] = (char *) "all"; - fixarg[2] = (char *) "MINIMIZE"; - modify->add_fix(3,fixarg); - delete [] fixarg; + modify->add_fix("MINIMIZE all MINIMIZE"); fix_minimize = (FixMinimize *) modify->fix[modify->nfix-1]; // clear out extra global and per-atom dof diff --git a/src/read_restart.cpp b/src/read_restart.cpp index c1e0526963..bcd20c9b51 100644 --- a/src/read_restart.cpp +++ b/src/read_restart.cpp @@ -423,19 +423,9 @@ void ReadRestart::command(int narg, char **arg) // create a temporary fix to hold and migrate extra atom info // necessary b/c irregular will migrate atoms - if (nextra) { - char cextra[8],fixextra[8]; - sprintf(cextra,"%d",nextra); - sprintf(fixextra,"%d",modify->nfix_restart_peratom); - char **newarg = new char*[5]; - newarg[0] = (char *) "_read_restart"; - newarg[1] = (char *) "all"; - newarg[2] = (char *) "READ_RESTART"; - newarg[3] = cextra; - newarg[4] = fixextra; - modify->add_fix(5,newarg); - delete [] newarg; - } + if (nextra) + modify->add_fix(fmt::format("_read_restart all READ_RESTART {} {}", + nextra,modify->nfix_restart_peratom)); // move atoms to new processors via irregular() // turn sorting on in migrate_atoms() to avoid non-reproducible restarts diff --git a/src/respa.cpp b/src/respa.cpp index 8a94b18736..f894333e39 100644 --- a/src/respa.cpp +++ b/src/respa.cpp @@ -299,22 +299,11 @@ void Respa::init() // create fix needed for storing atom-based respa level forces // will delete it at end of run - - char **fixarg = new char*[5]; - fixarg[0] = (char *) "RESPA"; - fixarg[1] = (char *) "all"; - fixarg[2] = (char *) "RESPA"; - fixarg[3] = new char[8]; - sprintf(fixarg[3],"%d",nlevels); // if supported, we also store torques on a per-level basis - if (atom->torque_flag) { - fixarg[4] = (char *) "torque"; - modify->add_fix(5,fixarg); - } else { - modify->add_fix(4,fixarg); - } - delete [] fixarg[3]; - delete [] fixarg; + + std::string cmd = fmt::format("RESPA all RESPA {}",nlevels); + if (atom->torque_flag) modify->add_fix(cmd + " torque"); + else modify->add_fix(cmd); fix_respa = (FixRespa *) modify->fix[modify->nfix-1]; // insure respa inner/middle/outer is using Pair class that supports it diff --git a/src/thermo.cpp b/src/thermo.cpp index 5aebef88af..dcec00fafe 100644 --- a/src/thermo.cpp +++ b/src/thermo.cpp @@ -483,7 +483,7 @@ void Thermo::modify_params(int narg, char **arg) icompute = modify->find_compute(id_compute[index_press_vector]); if (icompute < 0) error->all(FLERR, "Pressure ID for thermo does not exist"); - } else icompute = modify->find_compute((char *) "thermo_press"); + } else icompute = modify->find_compute("thermo_press"); modify->compute[icompute]->reset_extra_compute_fix(arg[iarg+1]);