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
@ -268,29 +279,37 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
int dummyvar; int dummyvar;
#define JSON_INIT_FIELD(field, sizevar, flagvar, required, sizecheck) \ #define JSON_INIT_FIELD(field, sizevar, flagvar, required, sizecheck) \
if (moldata.contains(#field)) { \ if (moldata.contains(#field)) { \
sizevar = 0; \ sizevar = 0; \
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' " \
if (moldata[#field].contains("data")) { \ "field for '{}'", \
flagvar = 1; \ id, #field); \
sizevar = moldata[#field]["data"].size(); \ if (moldata[#field].contains("data")) { \
} else { \ flagvar = 1; \
error->all(FLERR, Error::NOLASTLINE, \ sizevar = moldata[#field]["data"].size(); \
"JSON molecule data does not contain required 'data' field for '{}'", #field); \ } else { \
} \ error->all(FLERR, Error::NOLASTLINE, \
if (sizevar < 1) \ "Molecule template {}: JSON molecule data does not contain required 'data' " \
error->all(FLERR, Error::NOLASTLINE, "No {} in JSON data for molecule", #field); \ "field for '{}'", \
} else { \ id, #field); \
if (required) \ } \
error->all(FLERR, Error::NOLASTLINE, \ if (sizevar < 1) \
"JSON data for molecule does not contain required '{}' field", #field); \ error->all(FLERR, Error::NOLASTLINE, \
} \ "Molecule template {}: No {} entries in JSON data for molecule", id, #field); \
if (sizecheck && (sizecheck != sizevar)) \ } else { \
error->all(FLERR, Error::NOLASTLINE, "Found {} instead of {} data entries for '{}'", sizevar, \ if (required) \
error->all( \
FLERR, Error::NOLASTLINE, \
"Molecule template {}: JSON data for molecule does not contain required '{}' field", id, \
#field); \
} \
if (sizecheck && (sizecheck != 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,62 +384,65 @@ void Molecule::from_json(const std::string &molid, const json &moldata)
std::vector<std::string> secfmt; std::vector<std::string> secfmt;
// coords // coords
for (int i = 0; i < 4; ++i) secfmt.push_back(moldata["coords"]["format"][i]); if (xflag) {
if ((secfmt[0] == "atom-id") && (secfmt[1] == "x") && (secfmt[2] == "y") && (secfmt[3] == "z")) { 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")) {
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"]) {
if (c.size() < 4) if (c.size() < 4)
error->all( error->all(
FLERR, Error::NOLASTLINE, FLERR, Error::NOLASTLINE,
"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(
FLERR, Error::NOLASTLINE,
"Molecule template {}: invalid atom-id in \"coords\" section of molecule JSON data: {}",
id, to_string(c[0]));
const int iatom = int(c[0]) - 1;
if ((iatom < 0) || (iatom >= natoms))
error->all(
FLERR, Error::NOLASTLINE,
"Molecule template {}: invalid atom-id {} in coords section of molecule JSON data", id,
iatom + 1);
count[iatom]++;
x[iatom][0] = c[1];
x[iatom][1] = c[2];
x[iatom][2] = c[3];
x[iatom][0] *= sizescale;
x[iatom][1] *= sizescale;
x[iatom][2] *= sizescale;
}
// checks
for (int i = 0; i < natoms; i++) {
if (count[i] == 0) {
error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: atom {} missing in \"coords\" JSON section", id, i + 1);
}
}
if (domain->dimension == 2) {
for (int i = 0; i < natoms; i++) {
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 {}: invalid atom-id in \"coords\" section of molecule JSON "
i + 1); "data: {}",
id, to_string(c[0]));
const int iatom = int(c[0]) - 1;
if ((iatom < 0) || (iatom >= natoms))
error->all(
FLERR, Error::NOLASTLINE,
"Molecule template {}: invalid atom-id {} in coords section of molecule JSON data",
id, iatom + 1);
count[iatom]++;
x[iatom][0] = c[1];
x[iatom][1] = c[2];
x[iatom][2] = c[3];
x[iatom][0] *= sizescale;
x[iatom][1] *= sizescale;
x[iatom][2] *= sizescale;
}
// checks
for (int i = 0; i < natoms; i++) {
if (count[i] == 0) {
error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: atom {} missing in \"coords\" JSON section", id, i + 1);
} }
} }
if (domain->dimension == 2) {
for (int i = 0; i < natoms; i++) {
if (x[i][2] != 0.0) {
error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: Z coord for atom {} must be 0.0 for 2d-simulation",
id, i + 1);
}
}
}
} else {
error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: Expected \"coords\" format [\"atom-id\",\"x\",\"y\",\"z\"] "
"but found [\"{}\",\"{}\",\"{}\",\"{}\"]",
id, secfmt[0], secfmt[1], secfmt[2], secfmt[3]);
} }
} else {
error->all(FLERR, Error::NOLASTLINE,
"Molecule template {}: Expected \"coords\" format [\"atom-id\",\"x\",\"y\",\"z\"] "
"but found [\"{}\",\"{}\",\"{}\",\"{}\"]",
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 {