diff --git a/avogadro/io/cjsonformat.cpp b/avogadro/io/cjsonformat.cpp index 0056ad8f1d..4362ef3e27 100644 --- a/avogadro/io/cjsonformat.cpp +++ b/avogadro/io/cjsonformat.cpp @@ -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) { @@ -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