expand_type_int: optional flag to check types

verifying type could probably be default, except for historical reasons
would involve removing redundant checks after calling routine
This commit is contained in:
Jacob Gissinger
2024-08-18 22:39:01 -06:00
parent 7ecdea20eb
commit 1afa56e0da
10 changed files with 61 additions and 35 deletions

View File

@ -388,7 +388,7 @@ void PairE3B::coeff(int narg, char **arg)
if (narg < 4) error->all(FLERR, "There must be at least one keyword given to pair_coeff"); if (narg < 4) error->all(FLERR, "There must be at least one keyword given to pair_coeff");
if (typeO_str.size() > 0) if (typeO_str.size() > 0)
typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp); typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp, 1);
// clear setflag since coeff() called once with I,J = * * // clear setflag since coeff() called once with I,J = * *
int n = atom->ntypes; int n = atom->ntypes;

View File

@ -441,10 +441,10 @@ void PairLJCutTIP4PLongSoft::coeff(int narg, char **arg)
// and the types are already set and the strings are empty. // and the types are already set and the strings are empty.
if (typeO_str.size() > 0) { if (typeO_str.size() > 0) {
typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp); typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp, 1);
typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp); typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp, 1);
typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp); typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp, 1);
typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp); typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp, 1);
} }
PairLJCutCoulLongSoft::coeff(narg, arg); PairLJCutCoulLongSoft::coeff(narg, arg);

View File

@ -398,10 +398,10 @@ void PairTIP4PLongSoft::coeff(int narg, char **arg)
// and the types are already set and the strings are empty. // and the types are already set and the strings are empty.
if (typeO_str.size() > 0) { if (typeO_str.size() > 0) {
typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp); typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp, 1);
typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp); typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp, 1);
typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp); typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp, 1);
typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp); typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp, 1);
} }
PairCoulLongSoft::coeff(narg, arg); PairCoulLongSoft::coeff(narg, arg);

View File

@ -455,10 +455,10 @@ void PairLJCutTIP4PLong::coeff(int narg, char **arg)
// and the types are already set and the strings are empty. // and the types are already set and the strings are empty.
if (typeO_str.size() > 0) { if (typeO_str.size() > 0) {
typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp); typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp, 1);
typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp); typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp, 1);
typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp); typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp, 1);
typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp); typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp, 1);
} }
PairLJCutCoulLong::coeff(narg, arg); PairLJCutCoulLong::coeff(narg, arg);

View File

@ -1472,10 +1472,10 @@ void PairLJLongTIP4PLong::coeff(int narg, char **arg)
// and the types are already set and the strings are empty. // and the types are already set and the strings are empty.
if (typeO_str.size() > 0) { if (typeO_str.size() > 0) {
typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp); typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp, 1);
typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp); typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp, 1);
typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp); typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp, 1);
typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp); typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp, 1);
} }
PairLJLongCoulLong::coeff(narg, arg); PairLJLongCoulLong::coeff(narg, arg);

View File

@ -411,10 +411,10 @@ void PairTIP4PLong::coeff(int narg, char **arg)
// and the types are already set and the strings are empty. // and the types are already set and the strings are empty.
if (typeO_str.size() > 0) { if (typeO_str.size() > 0) {
typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp); typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp, 1);
typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp); typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp, 1);
typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp); typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp, 1);
typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp); typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp, 1);
} }
PairCoulLong::coeff(narg, arg); PairCoulLong::coeff(narg, arg);

View File

@ -465,10 +465,10 @@ void PairLJCutTIP4PCut::coeff(int narg, char **arg)
// and the types are already set and the strings are empty. // and the types are already set and the strings are empty.
if (typeO_str.size() > 0) { if (typeO_str.size() > 0) {
typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp); typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp, 1);
typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp); typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp, 1);
typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp); typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp, 1);
typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp); typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp, 1);
} }
int ilo,ihi,jlo,jhi; int ilo,ihi,jlo,jhi;

View File

@ -400,10 +400,10 @@ void PairTIP4PCut::coeff(int narg, char **arg)
// and the types are already set and the strings are empty. // and the types are already set and the strings are empty.
if (typeO_str.size() > 0) { if (typeO_str.size() > 0) {
typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp); typeO = utils::expand_type_int(FLERR, typeO_str, Atom::ATOM, lmp, 1);
typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp); typeH = utils::expand_type_int(FLERR, typeH_str, Atom::ATOM, lmp, 1);
typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp); typeB = utils::expand_type_int(FLERR, typeB_str, Atom::BOND, lmp, 1);
typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp); typeA = utils::expand_type_int(FLERR, typeA_str, Atom::ANGLE, lmp, 1);
} }
int ilo,ihi,jlo,jhi; int ilo,ihi,jlo,jhi;

View File

@ -995,17 +995,42 @@ char *utils::expand_type(const char *file, int line, const std::string &str, int
/* ------------------------------------------------------------------------- /* -------------------------------------------------------------------------
Expand type string to integer-valued numeric type from labelmap. Expand type string to integer-valued numeric type from labelmap.
Not guaranteed to return a valid type. Not guaranteed to return a valid type if param verify = 0 (default)
For example, type <= 0 or type > Ntypes is checked in calling routine. In this case, type <= 0 or type > Ntypes should be checked in calling routine.
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
int utils::expand_type_int(const char *file, int line, const std::string &str, int mode, int utils::expand_type_int(const char *file, int line, const std::string &str, int mode,
LAMMPS *lmp) LAMMPS *lmp, int verify)
{ {
char *typestr = expand_type(file, line, str, mode, lmp); char *typestr = expand_type(file, line, str, mode, lmp);
int out = inumeric(file, line, typestr ? typestr : str, false, lmp); int type = inumeric(file, line, typestr ? typestr : str, false, lmp);
if (verify) {
int errorflag = 0;
if (type <= 0) errorflag = 1;
int nmax;
switch (mode) {
case Atom::ATOM:
nmax = lmp->atom->ntypes;
break;
case Atom::BOND:
nmax = lmp->atom->nbondtypes;
break;
case Atom::ANGLE:
nmax = lmp->atom->nangletypes;
break;
case Atom::DIHEDRAL:
nmax = lmp->atom->ndihedraltypes;
break;
case Atom::IMPROPER:
nmax = lmp->atom->nimpropertypes;
break;
}
if (type > nmax) errorflag = 1;
if (errorflag) lmp->error->all(file, line, "{} type {} is out of bounds ({}-{})",
labeltypes[mode], type, 1, nmax);
}
delete[] typestr; delete[] typestr;
return out; return type;
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------

View File

@ -409,7 +409,7 @@ This functions adds the following case to :cpp:func:`utils::bounds() <LAMMPS_NS:
* *
* This function has the same arguments as expand_type() but returns an integer value */ * 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); int expand_type_int(const char *file, int line, const std::string &str, int mode, LAMMPS *lmp, int verify = 0);
/*! Check grid reference for valid Compute or Fix which produces per-grid data /*! Check grid reference for valid Compute or Fix which produces per-grid data
* *
@ -425,6 +425,7 @@ This functions adds the following case to :cpp:func:`utils::bounds() <LAMMPS_NS:
* \param nevery frequency at which caller will access fix for per-grid info, * \param nevery frequency at which caller will access fix for per-grid info,
* ignored when reference is to a compute * ignored when reference is to a compute
* \param lmp pointer to top-level LAMMPS class instance * \param lmp pointer to top-level LAMMPS class instance
* \param verify check bounds for interaction type
* \return id ID of Compute or Fix * \return id ID of Compute or Fix
* \return igrid which grid is referenced (0 to N-1) * \return igrid which grid is referenced (0 to N-1)
* \return idata which data on grid is referenced (0 to N-1) * \return idata which data on grid is referenced (0 to N-1)