From b0694e4e7354e4975eebcdf8e176d08b86a4ef35 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 11 Jun 2025 18:09:40 -0400 Subject: [PATCH] add support for dipoles section --- src/molecule.cpp | 59 +++++++++++++++++++++++++++++++-- tools/json/molecule-schema.json | 23 +++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index 33f227a41c..f50eccc9d5 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -651,8 +651,61 @@ void Molecule::from_json(const std::string &molid, const json &moldata) id, secfmt[0], secfmt[1]); } } + // dipoles + if (muflag) { + + for (int i = 0; i < 4; ++i) secfmt[i] = moldata["dipoles"]["format"][i]; + if ((secfmt[0] == "atom-id") && (secfmt[1] == "mux") && (secfmt[2] == "muy") && + (secfmt[3] == "muz")) { + + memset(count, 0, natoms * sizeof(count)); + for (const auto &c : moldata["dipoles"]["data"]) { + if (c.size() < 4) + error->all( + FLERR, Error::NOLASTLINE, + "Molecule template {}: missing data in \"dipoles\" section of molecule JSON data: {}", + id, to_string(c)); + if (!c[0].is_number_integer()) + error->all( + FLERR, Error::NOLASTLINE, + "Molecule template {}: invalid atom-id in \"dipoles\" 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 dipoles section of molecule JSON data", + id, iatom + 1); + count[iatom]++; + mu[iatom][0] = c[1]; + mu[iatom][1] = c[2]; + mu[iatom][2] = c[3]; + mu[iatom][0] *= sizescale; + mu[iatom][1] *= sizescale; + mu[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 \"dipoles\" JSON section", id, + i + 1); + } + } + } else { + error->all( + FLERR, Error::NOLASTLINE, + "Molecule template {}: Expected \"dipoles\" format [\"atom-id\",\"mux\",\"muy\",\"muz\"] " + "but found [\"{}\",\"{}\",\"{}\",\"{}\"]", + id, secfmt[0], secfmt[1], secfmt[2], secfmt[3]); + } + } + // masses if (rmassflag) { @@ -1590,9 +1643,9 @@ void Molecule::dipoles(char *line) error->all(FLERR, fileiarg, "Invalid atom index in Dipoles section of molecule file"); count[iatom]++; - mu[iatom][0] = values.next_double(); - mu[iatom][1] = values.next_double(); - mu[iatom][2] = values.next_double(); + mu[iatom][0] = values.next_double() * sizescale; + mu[iatom][1] = values.next_double() * sizescale; + mu[iatom][2] = values.next_double() * sizescale; } } catch (TokenizerException &e) { error->all(FLERR, fileiarg, "Invalid line in Dipoles section of molecule file: {}\n{}", e.what(), line); diff --git a/tools/json/molecule-schema.json b/tools/json/molecule-schema.json index bb2c95ce57..51616931e8 100644 --- a/tools/json/molecule-schema.json +++ b/tools/json/molecule-schema.json @@ -143,6 +143,29 @@ }, "required": ["format", "data"] }, + "dipoles": { + "type": "object", + "properties": { + "format": { + "type": "array", + "const": ["atom-id", "mux", "muy", "muz"] + }, + "data": { + "type": "array", + "items": { + "type": "array", + "prefixItems": [ + {"type": "integer"}, + {"type": "number"}, + {"type": "number"}, + {"type": "number"} + ], + "items": false + } + } + }, + "required": ["format", "data"] + }, "diameters": { "type": "object", "properties": {