overload Atom::add_molecule() with function accepting a JSON object
This commit is contained in:
21
src/atom.cpp
21
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
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -27,6 +27,7 @@
|
||||
#include "math_special.h"
|
||||
#include "memory.h"
|
||||
#include "tokenizer.h"
|
||||
#include "update.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
@ -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"];
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
Reference in New Issue
Block a user