improve error messages

This commit is contained in:
Axel Kohlmeyer
2025-06-12 21:21:14 -04:00
parent f389b6676b
commit 37b02a27fa

View File

@ -222,26 +222,37 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
std::string val; std::string val;
if (moldata.contains("application")) { if (moldata.contains("application")) {
if (moldata["application"] != "LAMMPS") if (moldata["application"] != "LAMMPS")
error->all(FLERR, Error::NOLASTLINE, "JSON data is for incompatible application: {}", error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: JSON data is for incompatible application: {}", id,
std::string(moldata["application"])); std::string(moldata["application"]));
} else { } else {
error->all(FLERR, Error::NOLASTLINE, "JSON data does not contain required 'application' field"); error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: JSON data does not contain required 'application' field", id);
} }
if (moldata.contains("format")) { if (moldata.contains("format")) {
if (moldata["format"] != "molecule") if (moldata["format"] != "molecule")
error->all(FLERR, Error::NOLASTLINE, "JSON data is not for a molecule: {}", error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: JSON data is not for a molecule: {}", id,
std::string(moldata["format"])); std::string(moldata["format"]));
} else { } else {
error->all(FLERR, Error::NOLASTLINE, "JSON data does not contain required 'format' field"); error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: JSON data does not contain required 'format' field", id);
} }
if (moldata.contains("revision")) { if (moldata.contains("revision")) {
int rev = moldata["revision"]; int rev = moldata["revision"];
if ((rev < 1) || (rev > 1)) if ((rev < 1) || (rev > 1))
error->all(FLERR, Error::NOLASTLINE, "JSON molecule data with unsupported revision {}", rev); error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: JSON molecule data with unsupported revision {}", id, rev);
} else { } else {
error->all(FLERR, Error::NOLASTLINE, "JSON data does not contain required 'revision' field"); error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: JSON data does not contain required 'revision' field", id);
} }
// length of types data list determines the number of atoms in the template and is thus required
if (!moldata.contains("types"))
error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: JSON data does not contain required 'types' field", id);
// optional fields // optional fields
// check for compatible units // check for compatible units
@ -274,23 +285,31 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
flagvar = 0; \ flagvar = 0; \
if (!moldata[#field].contains("format")) \ if (!moldata[#field].contains("format")) \
error->all(FLERR, Error::NOLASTLINE, \ error->all(FLERR, Error::NOLASTLINE, \
"JSON molecule data does not contain required 'format' field for '{}'", #field); \ "Molecule template {}: JSON molecule data does not contain required 'format' " \
"field for '{}'", \
id, #field); \
if (moldata[#field].contains("data")) { \ if (moldata[#field].contains("data")) { \
flagvar = 1; \ flagvar = 1; \
sizevar = moldata[#field]["data"].size(); \ sizevar = moldata[#field]["data"].size(); \
} else { \ } else { \
error->all(FLERR, Error::NOLASTLINE, \ error->all(FLERR, Error::NOLASTLINE, \
"JSON molecule data does not contain required 'data' field for '{}'", #field); \ "Molecule template {}: JSON molecule data does not contain required 'data' " \
"field for '{}'", \
id, #field); \
} \ } \
if (sizevar < 1) \ if (sizevar < 1) \
error->all(FLERR, Error::NOLASTLINE, "No {} in JSON data for molecule", #field); \ error->all(FLERR, Error::NOLASTLINE, \
"Molecule template {}: No {} entries in JSON data for molecule", id, #field); \
} else { \ } else { \
if (required) \ if (required) \
error->all(FLERR, Error::NOLASTLINE, \ error->all( \
"JSON data for molecule does not contain required '{}' field", #field); \ FLERR, Error::NOLASTLINE, \
"Molecule template {}: JSON data for molecule does not contain required '{}' field", id, \
#field); \
} \ } \
if (sizecheck && (sizecheck != sizevar)) \ if (sizecheck && (sizecheck != sizevar)) \
error->all(FLERR, Error::NOLASTLINE, "Found {} instead of {} data entries for '{}'", sizevar, \ error->all(FLERR, Error::NOLASTLINE, \
"Molecule template {}: Found {} instead of {} data entries for '{}'", id, sizevar, \
sizecheck, #field); sizecheck, #field);
JSON_INIT_FIELD(types, natoms, typeflag, true, 0); JSON_INIT_FIELD(types, natoms, typeflag, true, 0);
@ -340,7 +359,8 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
const double scale5 = powint(sizescale, 5); const double scale5 = powint(sizescale, 5);
avec_body = dynamic_cast<AtomVecBody *>(atom->style_match("body")); avec_body = dynamic_cast<AtomVecBody *>(atom->style_match("body"));
if (!avec_body) if (!avec_body)
error->all(FLERR, Error::NOLASTLINE, "JSON molecule data requires atom style body"); error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: JSON molecule data requires atom style body", id);
nibody = moldata["body"][0]; nibody = moldata["body"][0];
ndbody = moldata["body"][1]; ndbody = moldata["body"][1];
} }
@ -349,7 +369,8 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
if ((domain->dimension == 2) && (com[2] != 0.0)) if ((domain->dimension == 2) && (com[2] != 0.0))
error->all(FLERR, Error::NOLASTLINE, error->all(FLERR, Error::NOLASTLINE,
"Molecule data z center-of-mass must be 0.0 for 2d systems"); "Molecule template {}: Molecule data z center-of-mass must be 0.0 for 2d systems",
id);
// allocate required storage // allocate required storage
@ -363,8 +384,10 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
std::vector<std::string> secfmt; std::vector<std::string> secfmt;
// coords // coords
if (xflag) {
for (int i = 0; i < 4; ++i) secfmt.push_back(moldata["coords"]["format"][i]); for (int i = 0; i < 4; ++i) secfmt.push_back(moldata["coords"]["format"][i]);
if ((secfmt[0] == "atom-id") && (secfmt[1] == "x") && (secfmt[2] == "y") && (secfmt[3] == "z")) { if ((secfmt[0] == "atom-id") && (secfmt[1] == "x") && (secfmt[2] == "y") &&
(secfmt[3] == "z")) {
memset(count, 0, natoms * sizeof(int)); memset(count, 0, natoms * sizeof(int));
for (const auto &c : moldata["coords"]["data"]) { for (const auto &c : moldata["coords"]["data"]) {
@ -374,17 +397,17 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
"Molecule template {}: missing data in \"coords\" section of molecule JSON data: {}", "Molecule template {}: missing data in \"coords\" section of molecule JSON data: {}",
id, to_string(c)); id, to_string(c));
if (!c[0].is_number_integer()) if (!c[0].is_number_integer())
error->all( error->all(FLERR, Error::NOLASTLINE,
FLERR, Error::NOLASTLINE, "Molecule template {}: invalid atom-id in \"coords\" section of molecule JSON "
"Molecule template {}: invalid atom-id in \"coords\" section of molecule JSON data: {}", "data: {}",
id, to_string(c[0])); id, to_string(c[0]));
const int iatom = int(c[0]) - 1; const int iatom = int(c[0]) - 1;
if ((iatom < 0) || (iatom >= natoms)) if ((iatom < 0) || (iatom >= natoms))
error->all( error->all(
FLERR, Error::NOLASTLINE, FLERR, Error::NOLASTLINE,
"Molecule template {}: invalid atom-id {} in coords section of molecule JSON data", id, "Molecule template {}: invalid atom-id {} in coords section of molecule JSON data",
iatom + 1); id, iatom + 1);
count[iatom]++; count[iatom]++;
x[iatom][0] = c[1]; x[iatom][0] = c[1];
x[iatom][1] = c[2]; x[iatom][1] = c[2];
@ -406,8 +429,8 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
for (int i = 0; i < natoms; i++) { for (int i = 0; i < natoms; i++) {
if (x[i][2] != 0.0) { if (x[i][2] != 0.0) {
error->all(FLERR, Error::NOLASTLINE, error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: Z coord for atom {} must be 0.0 for 2d-simulation", id, "Molecule template {}: Z coord for atom {} must be 0.0 for 2d-simulation",
i + 1); id, i + 1);
} }
} }
} }
@ -417,8 +440,9 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
"but found [\"{}\",\"{}\",\"{}\",\"{}\"]", "but found [\"{}\",\"{}\",\"{}\",\"{}\"]",
id, secfmt[0], secfmt[1], secfmt[2], secfmt[3]); id, secfmt[0], secfmt[1], secfmt[2], secfmt[3]);
} }
}
// types // types (is a required section and we tested for it above)
secfmt.clear(); secfmt.clear();
for (int i = 0; i < 2; ++i) secfmt.push_back(moldata["types"]["format"][i]); for (int i = 0; i < 2; ++i) secfmt.push_back(moldata["types"]["format"][i]);
@ -438,8 +462,10 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
id, to_string(c[0])); id, to_string(c[0]));
const int iatom = int(c[0]) - 1; const int iatom = int(c[0]) - 1;
if ((iatom < 0) || (iatom >= natoms)) if ((iatom < 0) || (iatom >= natoms))
error->all(FLERR, Error::NOLASTLINE, error->all(
"Invalid atom-id {} in types section of molecule JSON data", iatom + 1); FLERR, Error::NOLASTLINE,
"Molecule template {}: invalid atom-id {} in types section of molecule JSON data", id,
iatom + 1);
if (c[1].is_number_integer()) { // numeric type if (c[1].is_number_integer()) { // numeric type
type[iatom] = int(c[1]) + toffset; type[iatom] = int(c[1]) + toffset;
} else { } else {