Skip to content

Commit

Permalink
Parse inputParameters if present in cjson (#1849)
Browse files Browse the repository at this point in the history
* Parse inputParameters if present in cjson and use in input generators

Signed-off-by: Geoff Hutchison <[email protected]>

---------

Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis authored Dec 8, 2024
1 parent 6baaa03 commit e550046
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
25 changes: 24 additions & 1 deletion avogadro/io/cjsonformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,13 @@ bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule,
}
}

// inputParameters are calculation metadata
if (jsonRoot.find("inputParameters") != jsonRoot.end()) {
json inputParameters = jsonRoot["inputParameters"];
// add this as a string to the molecule data
molecule.setData("inputParameters", inputParameters.dump());
}

// Partial charges are optional, but if present should be loaded.
json partialCharges = atoms["partialCharges"];
if (partialCharges.is_object()) {
Expand Down Expand Up @@ -763,6 +770,13 @@ bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule,
if (element.first == "name" || element.first == "inchi")
continue;

// check for "inputParameters" and handle it separately
if (element.first == "inputParameters") {
json inputParameters = json::parse(element.second.toString());
root["inputParameters"] = inputParameters;
continue;
}

if (element.second.type() == Variant::String)
properties[element.first] = element.second.toString().c_str();
else if (element.second.type() == Variant::Double)
Expand All @@ -773,7 +787,15 @@ bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule,
properties[element.first] = element.second.toInt();
else if (element.second.type() == Variant::Bool)
properties[element.first] = element.second.toBool();
else if (element.second.type() == Variant::Matrix) {
else if (element.second.type() == Variant::Vector) {
// e.g. dipole moment
Vector3 v = element.second.toVector3();
json vector;
vector.push_back(v.x());
vector.push_back(v.y());
vector.push_back(v.z());
properties[element.first] = vector;
} else if (element.second.type() == Variant::Matrix) {
MatrixX m = element.second.toMatrix();
json matrix;
for (int i = 0; i < m.rows(); ++i) {
Expand Down Expand Up @@ -820,6 +842,7 @@ bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule,
}

// check for spectra data
// vibrations are separate
if (molecule.spectraTypes().size() != 0) {
json spectra, electronic, nmr;
bool hasElectronic = false;
Expand Down
22 changes: 22 additions & 0 deletions avogadro/qtgui/jsonwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ void JsonWidget::setMolecule(QtGui::Molecule* mol)

setOption("Charge", charge);
setOption("Multiplicity", multiplicity);

// check the molecule for "inputParameters" from CJSON
// e.g.
// https://github.com/OpenChemistry/chemicaljson/blob/main/chemicaljson.py#L130
if (m_molecule->hasData("inputParameters")) {
QByteArray data(m_molecule->data("inputParameters").toString().c_str());
QJsonDocument doc = QJsonDocument::fromJson(data);
if (!doc.isNull() && doc.isObject()) {
QJsonObject inputParameters = doc.object();
// check for a few known keys
if (inputParameters.contains("processors"))
setOption("Processor Cores", inputParameters["processors"].toInt());
else if (inputParameters.contains("memory"))
setOption("Memory", inputParameters["memory"].toInt());
else if (inputParameters.contains("basis"))
setOption("Basis", inputParameters["basis"].toString());
else if (inputParameters.contains("functional"))
setOption("Theory", inputParameters["functional"].toString());
else if (inputParameters.contains("task"))
setOption("Calculation Type", inputParameters["task"].toString());
}
}
}

if (mol == m_molecule)
Expand Down

0 comments on commit e550046

Please sign in to comment.