Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save partial charges and properly read them from CJSON #1467

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading