Skip to content

Commit

Permalink
Merge pull request #1467 from ghutchis/save-partial-charges
Browse files Browse the repository at this point in the history
Save partial charges and properly read them from CJSON
  • Loading branch information
ghutchis authored Nov 25, 2023
2 parents 2e59b64 + e2980fa commit 4fccfd3
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions avogadro/io/cjsonformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,6 @@ bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule,
}
}

// Partial charges are optional, but if present should be loaded.
json partialCharges = atoms["partialCharges"];
if (partialCharges.is_object()) {
// keys are types, values are arrays of charges
for (auto& kv : partialCharges.items()) {
MatrixX charges(atomCount, 1);
if (isNumericArray(kv.value()) && kv.value().size() == atomCount) {
for (size_t i = 0; i < kv.value().size(); ++i) {
charges(i, 0) = kv.value()[i];
}
molecule.setPartialCharges(kv.key(), charges);
}
}
}

// Bonds are optional, but if present should be loaded.
json bonds = jsonRoot["bonds"];
if (bonds.is_object() && isNumericArray(bonds["connections"]["index"])) {
Expand Down Expand Up @@ -609,6 +594,21 @@ bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule,
}
}

// Partial charges are optional, but if present should be loaded.
json partialCharges = atoms["partialCharges"];
if (partialCharges.is_object()) {
// keys are types, values are arrays of charges
for (auto& kv : partialCharges.items()) {
MatrixX charges(atomCount, 1);
if (isNumericArray(kv.value()) && kv.value().size() == atomCount) {
for (size_t i = 0; i < kv.value().size(); ++i) {
charges(i, 0) = kv.value()[i];
}
molecule.setPartialCharges(kv.key(), charges);
}
}
}

if (jsonRoot.find("layer") != jsonRoot.end()) {
auto names = LayerManager::getMoleculeInfo(&molecule);
json visible = jsonRoot["layer"]["visible"];
Expand Down Expand Up @@ -915,6 +915,20 @@ bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule,
if (hasCustomColors)
root["atoms"]["colors"] = colors;

// check for partial charges
auto partialCharges = molecule.partialChargeTypes();
if (!partialCharges.empty()) {
// add them to the atoms object
for (const auto& type : partialCharges) {
MatrixX chargesMatrix = molecule.partialCharges(type);
json charges;
for (Index i = 0; i < molecule.atomCount(); ++i) {
charges.push_back(chargesMatrix(i, 0));
}
root["atoms"]["partialCharges"][type] = charges;
}
}

// 3d positions:
if (molecule.atomPositions3d().size() == molecule.atomCount()) {
// everything gets real-space Cartesians
Expand Down

0 comments on commit 4fccfd3

Please sign in to comment.