diff --git a/src/compute_chunk_atom.cpp b/src/compute_chunk_atom.cpp index 92567101de..34bd855879 100644 --- a/src/compute_chunk_atom.cpp +++ b/src/compute_chunk_atom.cpp @@ -36,6 +36,7 @@ #include "math_const.h" #include "memory.h" #include "error.h" +#include "fmt/format.h" using namespace LAMMPS_NS; using namespace MathConst; @@ -581,21 +582,9 @@ void ComputeChunkAtom::init() // fixstore initializes all values to 0.0 if ((idsflag == ONCE || lockcount) && !fixstore) { - 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 *) "1"; - modify->add_fix(6,newarg); + modify->add_fix(fmt::format("{}_COMPUTE_STORE {} STORE peratom 1 1", + id,group->names[igroup])); fixstore = (FixStore *) modify->fix[modify->nfix-1]; - delete [] newarg; } if ((idsflag != ONCE && !lockcount) && fixstore) { diff --git a/src/compute_displace_atom.cpp b/src/compute_displace_atom.cpp index 41db4db1d6..9a82924acb 100644 --- a/src/compute_displace_atom.cpp +++ b/src/compute_displace_atom.cpp @@ -24,6 +24,7 @@ #include "variable.h" #include "memory.h" #include "error.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -72,21 +73,9 @@ ComputeDisplaceAtom::ComputeDisplaceAtom(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); + modify->add_fix(fmt::format("{}_COMPUTE_STORE {} STORE peratom 1 3", + id,group->names[igroup])); fix = (FixStore *) modify->fix[modify->nfix-1]; - delete [] newarg; // calculate xu,yu,zu for fix store array // skip if reset from restart file diff --git a/src/modify.cpp b/src/modify.cpp index f540116641..52a0ed50b8 100644 --- a/src/modify.cpp +++ b/src/modify.cpp @@ -941,6 +941,23 @@ void Modify::add_fix(int narg, char **arg, int trysuffix) fix[ifix]->post_constructor(); } +/* ---------------------------------------------------------------------- + convenience function to allow adding a fix from a single string +------------------------------------------------------------------------- */ + +void Modify::add_fix(const std::string &fixcmd, int trysuffix) +{ + std::vector args = utils::split_words(fixcmd); + char **newarg = new char*[args.size()]; + int i=0; + for (const auto &arg : args) { + newarg[i++] = (char *)arg.c_str(); + } + add_fix(args.size(),newarg,trysuffix); + delete[] newarg; +} + + /* ---------------------------------------------------------------------- replace replaceID fix with a new fix this is used by callers to preserve ordering of fixes @@ -1228,6 +1245,23 @@ void Modify::add_compute(int narg, char **arg, int trysuffix) ncompute++; } +/* ---------------------------------------------------------------------- + convenience function to allow adding a compute from a single string +------------------------------------------------------------------------- */ + +void Modify::add_compute(const std::string &computecmd, int trysuffix) +{ + std::vector args = utils::split_words(computecmd); + char **newarg = new char*[args.size()]; + int i=0; + for (const auto &arg : args) { + newarg[i++] = (char *)arg.c_str(); + } + add_compute(args.size(),newarg,trysuffix); + delete[] newarg; +} + + /* ---------------------------------------------------------------------- one instance per compute in style_compute.h ------------------------------------------------------------------------- */ diff --git a/src/modify.h b/src/modify.h index 0b809d4d6a..a6cb5feab0 100644 --- a/src/modify.h +++ b/src/modify.h @@ -96,6 +96,7 @@ class Modify : protected Pointers { virtual int min_reset_ref(); void add_fix(int, char **, int trysuffix=1); + void add_fix(const std::string &, int trysuffix=1); void replace_fix(const char *, int, char **, int trysuffix=1); void modify_fix(int, char **); void delete_fix(const char *); @@ -108,6 +109,7 @@ class Modify : protected Pointers { int check_rigid_list_overlap(int *); void add_compute(int, char **, int trysuffix=1); + void add_compute(const std::string &, int trysuffix=1); void modify_compute(int, char **); void delete_compute(const char *); int find_compute(const char *); diff --git a/src/output.cpp b/src/output.cpp index ee3d5b3996..42d73f2c31 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -46,28 +46,13 @@ Output::Output(LAMMPS *lmp) : Pointers(lmp) { // create default computes for temp,pressure,pe - char **newarg = new char*[4]; - newarg[0] = (char *) "thermo_temp"; - newarg[1] = (char *) "all"; - newarg[2] = (char *) "temp"; - modify->add_compute(3,newarg); - - newarg[0] = (char *) "thermo_press"; - newarg[1] = (char *) "all"; - newarg[2] = (char *) "pressure"; - newarg[3] = (char *) "thermo_temp"; - modify->add_compute(4,newarg); - - newarg[0] = (char *) "thermo_pe"; - newarg[1] = (char *) "all"; - newarg[2] = (char *) "pe"; - modify->add_compute(3,newarg); - - delete [] newarg; + modify->add_compute("thermo_temp all temp"); + modify->add_compute("thermo_press all pressure thermo_temp"); + modify->add_compute("thermo_pe all pe"); // create default Thermo class - newarg = new char*[1]; + char **newarg = new char*[1]; newarg[0] = (char *) "one"; thermo = new Thermo(lmp,1,newarg); delete [] newarg;