diff --git a/src/MC/fix_mol_swap.cpp b/src/MC/fix_mol_swap.cpp index 666d1dd36b..de408df204 100644 --- a/src/MC/fix_mol_swap.cpp +++ b/src/MC/fix_mol_swap.cpp @@ -58,12 +58,8 @@ FixMolSwap::FixMolSwap(LAMMPS *lmp, int narg, char **arg) : nevery = utils::inumeric(FLERR, arg[3], false, lmp); ncycles = utils::inumeric(FLERR, arg[4], false, lmp); - char *typestr = utils::expand_type(FLERR, arg[5], Atom::ATOM, lmp); - itype = utils::inumeric(FLERR, typestr?typestr:arg[5], false, lmp); - delete[] typestr; - typestr = utils::expand_type(FLERR, arg[6], Atom::ATOM, lmp); - jtype = utils::inumeric(FLERR, typestr?typestr:arg[6], false, lmp); - delete[] typestr; + itype = utils::expand_type_int(FLERR, arg[5], Atom::ATOM, lmp); + jtype = utils::expand_type_int(FLERR, arg[6], Atom::ATOM, lmp); seed = utils::inumeric(FLERR,arg[7],false,lmp); double temperature = utils::numeric(FLERR,arg[8],false,lmp); diff --git a/src/create_atoms.cpp b/src/create_atoms.cpp index ec9d8078a2..be649dae2a 100644 --- a/src/create_atoms.cpp +++ b/src/create_atoms.cpp @@ -89,9 +89,7 @@ void CreateAtoms::command(int narg, char **arg) // parse arguments if (narg < 2) utils::missing_cmd_args(FLERR, "create_atoms", error); - char *typestr = utils::expand_type(FLERR, arg[0], Atom::ATOM, lmp); - ntype = utils::inumeric(FLERR, typestr?typestr:arg[0], false, lmp); - delete[] typestr; + ntype = utils::expand_type_int(FLERR, arg[0], Atom::ATOM, lmp); const char *meshfile; int iarg; @@ -165,9 +163,7 @@ void CreateAtoms::command(int narg, char **arg) if (strcmp(arg[iarg], "basis") == 0) { if (iarg + 3 > narg) utils::missing_cmd_args(FLERR, "create_atoms basis", error); int ibasis = utils::inumeric(FLERR, arg[iarg + 1], false, lmp); - char *typestr = utils::expand_type(FLERR, arg[iarg + 2], Atom::ATOM, lmp); - int itype = utils::inumeric(FLERR, typestr?typestr:arg[iarg + 2], false, lmp); - delete[] typestr; + int itype = utils::expand_type_int(FLERR, arg[iarg + 2], Atom::ATOM, lmp); if (ibasis <= 0 || ibasis > nbasis || itype <= 0 || itype > atom->ntypes) error->all(FLERR, "Out of range basis setting '{} {}' in create_atoms command", ibasis, itype); diff --git a/src/fix_deposit.cpp b/src/fix_deposit.cpp index 97e76c6ccc..300a30d832 100644 --- a/src/fix_deposit.cpp +++ b/src/fix_deposit.cpp @@ -60,9 +60,7 @@ FixDeposit::FixDeposit(LAMMPS *lmp, int narg, char **arg) : // required args ninsert = utils::inumeric(FLERR, arg[3], false, lmp); - char *typestr = utils::expand_type(FLERR, arg[4], Atom::ATOM, lmp); - ntype = utils::inumeric(FLERR, typestr?typestr:arg[4], false, lmp); - delete[] typestr; + ntype = utils::expand_type_int(FLERR, arg[4], Atom::ATOM, lmp); nfreq = utils::inumeric(FLERR, arg[5], false, lmp); seed = utils::inumeric(FLERR, arg[6], false, lmp); diff --git a/src/utils.cpp b/src/utils.cpp index 8aa33e14e6..b604f730a0 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -915,6 +915,20 @@ char *utils::expand_type(const char *file, int line, const std::string &str, int return nullptr; } +/* ------------------------------------------------------------------------- + Expand type string to integer-valued numeric type from labelmap. + Not guaranteed to return a valid type. + For example, type <= 0 or type > Ntypes is checked in calling routine. +------------------------------------------------------------------------- */ + +int utils::expand_type_int(const char *file, int line, const std::string &str, int mode, LAMMPS *lmp) +{ + char *typestr = expand_type(file, line, str, mode, lmp); + int out = inumeric(FLERR, typestr?typestr:str, false, lmp); + delete[] typestr; + return out; +} + /* ---------------------------------------------------------------------- Check grid reference for valid Compute or Fix which produces per-grid data errstr = name of calling command used if error is generated diff --git a/src/utils.h b/src/utils.h index 335016831d..383927ad34 100644 --- a/src/utils.h +++ b/src/utils.h @@ -379,6 +379,12 @@ namespace utils { char *expand_type(const char *file, int line, const std::string &str, int mode, LAMMPS *lmp); + /*! Expand type label string into its equivalent integer-valued numeric type + * + * This function has the same arguments as expand_type() but returns an integer value */ + + int expand_type_int(const char *file, int line, const std::string &str, int mode, LAMMPS *lmp); + /*! Check grid reference for valid Compute or Fix which produces per-grid data * * This function checks if a command argument in the input script