Unverified Commit 7db12ae3 authored by Peter Eastman's avatar Peter Eastman Committed by GitHub
Browse files

Serializing CustomIntegrator preserves order of global variables (#4641)

parent fb50d376
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2010 Stanford University and the Authors. * * Portions copyright (c) 2010-2024 Stanford University and the Authors. *
* Authors: Peter Eastman, Yutong Zhao * * Authors: Peter Eastman, Yutong Zhao *
* Contributors: * * Contributors: *
* * * *
...@@ -36,23 +36,20 @@ using namespace std; ...@@ -36,23 +36,20 @@ using namespace std;
using namespace OpenMM; using namespace OpenMM;
CustomIntegratorProxy::CustomIntegratorProxy() : SerializationProxy("CustomIntegrator") { CustomIntegratorProxy::CustomIntegratorProxy() : SerializationProxy("CustomIntegrator") {
} }
void CustomIntegratorProxy::serialize(const void* object, SerializationNode& node) const { void CustomIntegratorProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 2); node.setIntProperty("version", 3);
const CustomIntegrator& integrator = *reinterpret_cast<const CustomIntegrator*>(object); const CustomIntegrator& integrator = *reinterpret_cast<const CustomIntegrator*>(object);
SerializationNode& globalVariablesNode = node.createChildNode("GlobalVariables"); SerializationNode& globalVariablesNode = node.createChildNode("GlobalVariables");
for (int i = 0; i < integrator.getNumGlobalVariables(); i++) { for (int i = 0; i < integrator.getNumGlobalVariables(); i++)
globalVariablesNode.setDoubleProperty(integrator.getGlobalVariableName(i), integrator.getGlobalVariable(i)); globalVariablesNode.createChildNode("Variable").setStringProperty("name", integrator.getGlobalVariableName(i)).setDoubleProperty("value", integrator.getGlobalVariable(i));
}
SerializationNode& perDofVariablesNode = node.createChildNode("PerDofVariables"); SerializationNode& perDofVariablesNode = node.createChildNode("PerDofVariables");
for (int i = 0; i < integrator.getNumPerDofVariables(); i++) { for (int i = 0; i < integrator.getNumPerDofVariables(); i++) {
SerializationNode& perDofValuesNode = perDofVariablesNode.createChildNode(integrator.getPerDofVariableName(i)); SerializationNode& perDofValuesNode = perDofVariablesNode.createChildNode(integrator.getPerDofVariableName(i));
vector<Vec3> perDofValues; integrator.getPerDofVariable(i, perDofValues); vector<Vec3> perDofValues; integrator.getPerDofVariable(i, perDofValues);
for (int j = 0; j < perDofValues.size(); j++) { for (int j = 0; j < perDofValues.size(); j++)
perDofValuesNode.createChildNode("Value").setDoubleProperty("x",perDofValues[j][0]).setDoubleProperty("y",perDofValues[j][1]).setDoubleProperty("z",perDofValues[j][2]); perDofValuesNode.createChildNode("Value").setDoubleProperty("x", perDofValues[j][0]).setDoubleProperty("y", perDofValues[j][1]).setDoubleProperty("z", perDofValues[j][2]);
}
} }
SerializationNode& computationsNode = node.createChildNode("Computations"); SerializationNode& computationsNode = node.createChildNode("Computations");
for (int i = 0; i < integrator.getNumComputations(); i++) { for (int i = 0; i < integrator.getNumComputations(); i++) {
...@@ -60,26 +57,30 @@ void CustomIntegratorProxy::serialize(const void* object, SerializationNode& nod ...@@ -60,26 +57,30 @@ void CustomIntegratorProxy::serialize(const void* object, SerializationNode& nod
string computationVariable; string computationVariable;
string computationExpression; string computationExpression;
integrator.getComputationStep(i, computationType, computationVariable, computationExpression); integrator.getComputationStep(i, computationType, computationVariable, computationExpression);
computationsNode.createChildNode("Computation").setIntProperty("computationType",static_cast<int>(computationType)) computationsNode.createChildNode("Computation").setIntProperty("computationType", static_cast<int>(computationType))
.setStringProperty("computationVariable",computationVariable).setStringProperty("computationExpression",computationExpression); .setStringProperty("computationVariable", computationVariable).setStringProperty("computationExpression", computationExpression);
} }
SerializationNode& functions = node.createChildNode("Functions"); SerializationNode& functions = node.createChildNode("Functions");
for (int i = 0; i < integrator.getNumTabulatedFunctions(); i++) for (int i = 0; i < integrator.getNumTabulatedFunctions(); i++)
functions.createChildNode("Function", &integrator.getTabulatedFunction(i)).setStringProperty("name", integrator.getTabulatedFunctionName(i)); functions.createChildNode("Function", &integrator.getTabulatedFunction(i)).setStringProperty("name", integrator.getTabulatedFunctionName(i));
node.setStringProperty("kineticEnergyExpression",integrator.getKineticEnergyExpression()); node.setStringProperty("kineticEnergyExpression", integrator.getKineticEnergyExpression());
node.setIntProperty("randomSeed",integrator.getRandomNumberSeed()); node.setIntProperty("randomSeed", integrator.getRandomNumberSeed());
node.setDoubleProperty("stepSize",integrator.getStepSize()); node.setDoubleProperty("stepSize", integrator.getStepSize());
node.setDoubleProperty("constraintTolerance",integrator.getConstraintTolerance()); node.setDoubleProperty("constraintTolerance", integrator.getConstraintTolerance());
} }
void* CustomIntegratorProxy::deserialize(const SerializationNode& node) const { void* CustomIntegratorProxy::deserialize(const SerializationNode& node) const {
int version = node.getIntProperty("version"); int version = node.getIntProperty("version");
if (version < 1 || version > 2) if (version < 1 || version > 3)
throw OpenMMException("Unsupported version number"); throw OpenMMException("Unsupported version number");
CustomIntegrator* integrator = new CustomIntegrator(node.getDoubleProperty("stepSize")); CustomIntegrator* integrator = new CustomIntegrator(node.getDoubleProperty("stepSize"));
const SerializationNode& globalVariablesNode = node.getChildNode("GlobalVariables"); const SerializationNode& globalVariablesNode = node.getChildNode("GlobalVariables");
for (auto& prop : globalVariablesNode.getProperties()) if (version < 3)
integrator->addGlobalVariable(prop.first, globalVariablesNode.getDoubleProperty(prop.first)); for (auto& prop : globalVariablesNode.getProperties())
integrator->addGlobalVariable(prop.first, globalVariablesNode.getDoubleProperty(prop.first));
else
for (auto& var : globalVariablesNode.getChildren())
integrator->addGlobalVariable(var.getStringProperty("name"), var.getDoubleProperty("value"));
const SerializationNode& perDofVariablesNode = node.getChildNode("PerDofVariables"); const SerializationNode& perDofVariablesNode = node.getChildNode("PerDofVariables");
int count = 0; int count = 0;
for (auto& var : perDofVariablesNode.getChildren()) { for (auto& var : perDofVariablesNode.getChildren()) {
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2010-2019 Stanford University and the Authors. * * Portions copyright (c) 2010-2024 Stanford University and the Authors. *
* Authors: Peter Eastman, Yutong Zhao * * Authors: Peter Eastman, Yutong Zhao *
* Contributors: * * Contributors: *
* * * *
...@@ -154,8 +154,8 @@ void testSerializeCustomIntegrator() { ...@@ -154,8 +154,8 @@ void testSerializeCustomIntegrator() {
intg->addGlobalVariable("oute", 0); intg->addGlobalVariable("oute", 0);
intg->addGlobalVariable("oute1", 0); intg->addGlobalVariable("oute1", 0);
intg->addGlobalVariable("oute2", 0); intg->addGlobalVariable("oute2", 0);
intg->addGlobalVariable("oute3_conditional_v1", 0);// HACK: need addGlobals to be alphabetical to work around bug
intg->addGlobalVariable("oute3_conditional_v2", 0); intg->addGlobalVariable("oute3_conditional_v2", 0);
intg->addGlobalVariable("oute3_conditional_v1", 0);
intg->addComputePerDof("outf", "f"); intg->addComputePerDof("outf", "f");
intg->addComputePerDof("outf1", "f1"); intg->addComputePerDof("outf1", "f1");
intg->addComputePerDof("outf2", "f2"); intg->addComputePerDof("outf2", "f2");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment