overload Atom::add_molecule() with function accepting a JSON object

This commit is contained in:
Axel Kohlmeyer
2025-05-23 00:08:58 -04:00
parent 2fe88a1e9e
commit 76ef41a901
4 changed files with 41 additions and 6 deletions

View File

@ -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

View File

@ -16,6 +16,8 @@
#include "pointers.h"
#include "json.h"
#include <map>
#include <set>
@ -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<Molecule *> get_molecule_by_id(const std::string &);
void add_molecule_atom(Molecule *, int, int, tagint);

View File

@ -27,6 +27,7 @@
#include "math_special.h"
#include "memory.h"
#include "tokenizer.h"
#include "update.h"
#include <cmath>
#include <cstring>
@ -187,7 +188,7 @@ void Molecule::command(int narg, char **arg, int &index)
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"];

View File

@ -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();