example with augmented utils::bounds

option to check for type label
This commit is contained in:
Jacob Gissinger
2024-05-12 01:00:09 -04:00
parent 2492c57c8e
commit 25d4b3484d
5 changed files with 34 additions and 58 deletions

View File

@ -91,20 +91,10 @@ FixAdaptFEP::FixAdaptFEP(LAMMPS *lmp, int narg, char **arg) :
adapt[nadapt].which = PAIR;
adapt[nadapt].pstyle = utils::strdup(arg[iarg+1]);
adapt[nadapt].pparam = utils::strdup(arg[iarg+2]);
char *typestr = nullptr;
typestr = utils::expand_type(FLERR, arg[iarg+3], Atom::ATOM, lmp);
if (typestr)
adapt[nadapt].ilo = adapt[nadapt].ihi = utils::inumeric(FLERR, typestr, false, lmp);
else utils::bounds(FLERR, arg[iarg+3], 1, atom->ntypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error);
delete[] typestr;
typestr = nullptr;
typestr = utils::expand_type(FLERR, arg[iarg+4], Atom::ATOM, lmp);
if (typestr)
adapt[nadapt].jlo = adapt[nadapt].jhi = utils::inumeric(FLERR, typestr, false, lmp);
else utils::bounds(FLERR, arg[iarg+4], 1, atom->ntypes,
adapt[nadapt].jlo, adapt[nadapt].jhi, error);
delete[] typestr;
utils::bounds(FLERR, arg[iarg+3], 1, atom->ntypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error, Atom::ATOM);
utils::bounds(FLERR, arg[iarg+4], 1, atom->ntypes,
adapt[nadapt].jlo, adapt[nadapt].jhi, error, Atom::ATOM);
// switch i,j if i > j, if wildcards were not used
@ -140,13 +130,8 @@ FixAdaptFEP::FixAdaptFEP(LAMMPS *lmp, int narg, char **arg) :
adapt[nadapt].aparam = CHARGE;
chgflag = 1;
} else error->all(FLERR,"Illegal fix adapt/fep command");
char *typestr = nullptr;
typestr = utils::expand_type(FLERR, arg[iarg+2], Atom::ATOM, lmp);
if (typestr)
adapt[nadapt].ilo = adapt[nadapt].ihi = utils::inumeric(FLERR, typestr, false, lmp);
else utils::bounds(FLERR, arg[iarg+2], 1, atom->ntypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error);
delete[] typestr;
utils::bounds(FLERR, arg[iarg+2], 1, atom->ntypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error, Atom::ATOM);
if (utils::strmatch(arg[iarg+3],"^v_")) {
adapt[nadapt].var = utils::strdup(arg[iarg+3]+2);
} else error->all(FLERR,"Illegal fix adapt/fep command");

View File

@ -33,7 +33,7 @@ class Atom : protected Pointers {
enum { DOUBLE, INT, BIGINT };
enum { GROW = 0, RESTART = 1, BORDER = 2 };
enum { ATOMIC = 0, MOLECULAR = 1, TEMPLATE = 2 };
enum { ATOM = 0, BOND = 1, ANGLE = 2, DIHEDRAL = 3, IMPROPER = 4 };
enum { ATOM = 0, BOND = 1, ANGLE = 2, DIHEDRAL = 3, IMPROPER = 4, NONTYPE = 5 };
enum { NUMERIC = 0, LABELS = 1 };
enum { MAP_NONE = 0, MAP_ARRAY = 1, MAP_HASH = 2, MAP_YES = 3 };

View File

@ -99,20 +99,10 @@ FixAdapt::FixAdapt(LAMMPS *lmp, int narg, char **arg) :
adapt[nadapt].pair = nullptr;
adapt[nadapt].pstyle = utils::strdup(arg[iarg+1]);
adapt[nadapt].pparam = utils::strdup(arg[iarg+2]);
char *typestr = nullptr;
typestr = utils::expand_type(FLERR, arg[iarg+3], Atom::ATOM, lmp);
if (typestr)
adapt[nadapt].ilo = adapt[nadapt].ihi = utils::inumeric(FLERR, typestr, false, lmp);
else utils::bounds(FLERR, arg[iarg+3], 1, atom->ntypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error);
delete[] typestr;
typestr = nullptr;
typestr = utils::expand_type(FLERR, arg[iarg+4], Atom::ATOM, lmp);
if (typestr)
adapt[nadapt].jlo = adapt[nadapt].jhi = utils::inumeric(FLERR, typestr, false, lmp);
else utils::bounds(FLERR, arg[iarg+4], 1, atom->ntypes,
adapt[nadapt].jlo, adapt[nadapt].jhi, error);
delete[] typestr;
utils::bounds(FLERR, arg[iarg+3], 1, atom->ntypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error, Atom::ATOM);
utils::bounds(FLERR, arg[iarg+4], 1, atom->ntypes,
adapt[nadapt].jlo, adapt[nadapt].jhi, error, Atom::ATOM);
// switch i,j if i > j, if wildcards were not used
@ -136,13 +126,8 @@ FixAdapt::FixAdapt(LAMMPS *lmp, int narg, char **arg) :
adapt[nadapt].bond = nullptr;
adapt[nadapt].bstyle = utils::strdup(arg[iarg+1]);
adapt[nadapt].bparam = utils::strdup(arg[iarg+2]);
char *typestr = nullptr;
typestr = utils::expand_type(FLERR, arg[iarg+3], Atom::BOND, lmp);
if (typestr)
adapt[nadapt].ilo = adapt[nadapt].ihi = utils::inumeric(FLERR, typestr, false, lmp);
else utils::bounds(FLERR, arg[iarg+3], 1, atom->nbondtypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error);
delete[] typestr;
utils::bounds(FLERR, arg[iarg+3], 1, atom->nbondtypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error, Atom::BOND);
if (utils::strmatch(arg[iarg+4],"^v_")) {
adapt[nadapt].var = utils::strdup(arg[iarg+4]+2);
} else error->all(FLERR,"Argument #{} must be variable not {}", iarg+5, arg[iarg+4]);
@ -154,13 +139,8 @@ FixAdapt::FixAdapt(LAMMPS *lmp, int narg, char **arg) :
adapt[nadapt].angle = nullptr;
adapt[nadapt].astyle = utils::strdup(arg[iarg+1]);
adapt[nadapt].aparam = utils::strdup(arg[iarg+2]);
char *typestr = nullptr;
typestr = utils::expand_type(FLERR, arg[iarg+3], Atom::ANGLE, lmp);
if (typestr)
adapt[nadapt].ilo = adapt[nadapt].ihi = utils::inumeric(FLERR, typestr, false, lmp);
else utils::bounds(FLERR, arg[iarg+3], 1, atom->nangletypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error);
delete[] typestr;
utils::bounds(FLERR, arg[iarg+3], 1, atom->nangletypes,
adapt[nadapt].ilo, adapt[nadapt].ihi, error, Atom::ANGLE);
if (utils::strmatch(arg[iarg+4],"^v_")) {
adapt[nadapt].var = utils::strdup(arg[iarg+4]+2);
} else error->all(FLERR,"Argument #{} must be variable not {}", iarg+5, arg[iarg+4]);

View File

@ -596,15 +596,24 @@ tagint utils::tnumeric(const char *file, int line, const char *str, bool do_abor
/* ----------------------------------------------------------------------
compute bounds implied by numeric str with a possible wildcard asterisk
if str is a single number or type label, return nlo = nhi = label2type(str)
------------------------------------------------------------------------- */
// clang-format off
template <typename TYPE>
void utils::bounds(const char *file, int line, const std::string &str,
bigint nmin, bigint nmax, TYPE &nlo, TYPE &nhi, Error *error)
void utils::bounds(const char *file, int line, const std::string &str, bigint nmin,
bigint nmax, TYPE &nlo, TYPE &nhi, Error *error, int mode = Atom::NONTYPE)
{
nlo = nhi = -1;
// check for illegal charcters
if (mode != Atom::NONTYPE) {
char *typestr;
if ( typestr = utils::expand_type(FLERR, str, mode, lmp) )
nlo = nhi = utils::inumeric(FLERR, typestr, false, lmp);
delete[] typestr;
if (nlo > -1) return;
}
// check for illegal characters
size_t found = str.find_first_not_of("*-0123456789");
if (found != std::string::npos) {
if (error) error->all(file, line, "Invalid range string: {}", str);
@ -642,11 +651,11 @@ void utils::bounds(const char *file, int line, const std::string &str,
}
template void utils::bounds<>(const char *, int, const std::string &,
bigint, bigint, int &, int &, Error *);
bigint, bigint, int &, int &, Error *, int);
template void utils::bounds<>(const char *, int, const std::string &,
bigint, bigint, long &, long &, Error *);
bigint, bigint, long &, long &, Error *, int);
template void utils::bounds<>(const char *, int, const std::string &,
bigint, bigint, long long &, long long &, Error *);
bigint, bigint, long long &, long long &, Error *, int);
// clang-format on
/* -------------------------------------------------------------------------

View File

@ -310,9 +310,10 @@ namespace utils {
/*! Compute index bounds derived from a string with a possible wildcard
*
* This functions processes the string in *str* and set the values of *nlo*
* and *nhi* according to the following five cases:
* and *nhi* according to the following six cases:
*
* - a single number, i: nlo = i; nhi = i;
* - a single type label, typestr: nlo = nhi = label2type(typestr)
* - a single asterisk, \*: nlo = nmin; nhi = nmax;
* - a single number followed by an asterisk, i\*: nlo = i; nhi = nmax;
* - a single asterisk followed by a number, \*i: nlo = nmin; nhi = i;
@ -325,11 +326,12 @@ namespace utils {
* \param nmax largest allowed upper bound
* \param nlo lower bound
* \param nhi upper bound
* \param mode select labelmap using constants from Atom class
* \param error pointer to Error class for out-of-bounds messages */
template <typename TYPE>
void bounds(const char *file, int line, const std::string &str, bigint nmin, bigint nmax,
TYPE &nlo, TYPE &nhi, Error *error);
TYPE &nlo, TYPE &nhi, Error *error, int mode);
/*! Expand list of arguments when containing fix/compute wildcards
*