Skip to content

Commit

Permalink
Add support for reading and writing atom force vectors
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Jun 20, 2024
1 parent 88ecc4e commit a995c53
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion avogadro/io/cjsonformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,16 @@ bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule,
}
}

// todo? 2d position
// Array of vector3 for forces if available
json forces = atoms["forces"];
if (isNumericArray(forces) && forces.size() == 3 * atomCount) {
for (Index i = 0; i < atomCount; ++i) {
auto a = molecule.atom(i);
a.setForceVector(
Vector3(forces[3 * i], forces[3 * i + 1], forces[3 * i + 2]));
}
}

// labels
json labels = atoms["labels"];
if (labels.is_array() && labels.size() == atomCount) {
Expand Down Expand Up @@ -1058,6 +1067,18 @@ bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule,
}
root["atoms"]["coords"]["2d"] = coords2d;
}

// forces if present
const auto forceVectors = molecule.forceVectors();
if (forceVectors.size() == molecule.atomCount()) {
json forces;
for (const auto& force : forceVectors) {
forces.push_back(force.x());
forces.push_back(force.y());
forces.push_back(force.z());
}
root["atoms"]["forces"] = forces;
}
}

// check for atom labels
Expand Down

0 comments on commit a995c53

Please sign in to comment.