diff --git a/src/atom.cpp b/src/atom.cpp index 2678326338..8b04accc13 100644 --- a/src/atom.cpp +++ b/src/atom.cpp @@ -2124,7 +2124,7 @@ int Atom::shape_consistency(int itype, double &shapex, double &shapey, double &s } /* ---------------------------------------------------------------------- - add a new molecule template = set of molecules + add a new molecule template = set of molecules from the "molecule" command ------------------------------------------------------------------------- */ void Atom::add_molecule(int narg, char **arg) @@ -2153,6 +2153,25 @@ void Atom::add_molecule(int narg, char **arg) } } +/* ---------------------------------------------------------------------- + add a new molecule template from a JSON object +------------------------------------------------------------------------- */ + +void Atom::add_molecule(const std::string &id, const json &moldata) +{ + if (id.empty()) error->all(FLERR, "Must provide molecule ID"); + + if (find_molecule(id.c_str()) >= 0) + error->all(FLERR, Error::NOLASTLINE, "Reuse of molecule template ID {}", id); + + molecules = (Molecule **) + memory->srealloc(molecules,(nmolecule+1)*sizeof(Molecule *), "atom::molecules"); + molecules[nmolecule] = new Molecule(lmp); + molecules[nmolecule]->from_json(id, moldata); + molecules[nmolecule]->nset = 1; + nmolecule++; +} + /* ---------------------------------------------------------------------- find first molecule in set with template ID return -1 if does not exist diff --git a/src/atom.h b/src/atom.h index c98f06cbe8..86d81c0148 100644 --- a/src/atom.h +++ b/src/atom.h @@ -16,6 +16,8 @@ #include "pointers.h" +#include "json.h" + #include #include @@ -358,6 +360,7 @@ class Atom : protected Pointers { int shape_consistency(int, double &, double &, double &); void add_molecule(int, char **); + void add_molecule(const std::string &, const json &); int find_molecule(const char *); std::vector get_molecule_by_id(const std::string &); void add_molecule_atom(Molecule *, int, int, tagint); diff --git a/src/molecule.cpp b/src/molecule.cpp index cb6562612e..3b9502c698 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -27,6 +27,7 @@ #include "math_special.h" #include "memory.h" #include "tokenizer.h" +#include "update.h" #include #include @@ -184,10 +185,10 @@ void Molecule::command(int narg, char **arg, int &index) // convert back to json class on all processors moldata.clear(); moldata = json::from_ubjson(jsondata); - jsondata.clear(); // free binary data + jsondata.clear(); // free binary data // process JSON data - Molecule::from_json(moldata); + Molecule::from_json(id, moldata); } else { // process native molecule file @@ -209,9 +210,15 @@ void Molecule::command(int narg, char **arg, int &index) // convert json data structure to molecule data structure // ------------------------------------------------------------------------------ -void Molecule::from_json(const json &moldata) +void Molecule::from_json(const std::string &molid, const json &moldata) { json_format = 1; + if (!utils::is_id(molid)) + error->all(FLERR, Error::NOLASTLINE, + "Molecule template ID {} must have only alphanumeric or underscore characters", + molid); + delete[] id; + id = utils::strdup(molid); // check if JSON file is compatible @@ -229,7 +236,13 @@ void Molecule::from_json(const json &moldata) if (moldata.contains("revision")) { int rev = moldata["revision"]; if ((rev < 1) || (rev > 1)) - error->one(FLERR, Error::NOLASTLINE, "JSON molecule data with unsupported revision {}", rev); + error->all(FLERR, Error::NOLASTLINE, "JSON molecule data with unsupported revision {}", rev); + } + + if (moldata.contains("units")) { + if (std::string(moldata["units"]) != update->unit_style) + error->all(FLERR, Error::NOLASTLINE, "JSON molecule data units {} incompatible with {} units", + std::string(moldata["units"]), update->unit_style); } if (moldata.contains("title")) title = moldata["title"]; diff --git a/src/molecule.h b/src/molecule.h index 2a9844ddeb..7444342357 100644 --- a/src/molecule.h +++ b/src/molecule.h @@ -128,7 +128,7 @@ class Molecule : protected Pointers { ~Molecule() override; void command(int, char **, int &); - void from_json(const json &); + void from_json(const std::string &id, const json &); void compute_center(); void compute_mass();