Skip to content

Commit

Permalink
Merge pull request #1674 from ghutchis/add-cjson-atom-forces
Browse files Browse the repository at this point in the history
Add support for reading and writing atom force vectors
  • Loading branch information
ghutchis authored Jul 4, 2024
2 parents f3a1411 + a995c53 commit c6c17b1
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 c6c17b1

Please sign in to comment.