Commit 212e8367 authored by peastman's avatar peastman
Browse files

Serialization of tabulated functions for CustomIntegrator

parent 85c0f767
...@@ -40,22 +40,22 @@ CustomIntegratorProxy::CustomIntegratorProxy() : SerializationProxy("CustomInteg ...@@ -40,22 +40,22 @@ CustomIntegratorProxy::CustomIntegratorProxy() : SerializationProxy("CustomInteg
} }
void CustomIntegratorProxy::serialize(const void* object, SerializationNode& node) const { void CustomIntegratorProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1); node.setIntProperty("version", 2);
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.setDoubleProperty(integrator.getGlobalVariableName(i), 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++) {
CustomIntegrator::ComputationType computationType; CustomIntegrator::ComputationType computationType;
string computationVariable; string computationVariable;
string computationExpression; string computationExpression;
...@@ -63,6 +63,9 @@ void CustomIntegratorProxy::serialize(const void* object, SerializationNode& nod ...@@ -63,6 +63,9 @@ void CustomIntegratorProxy::serialize(const void* object, SerializationNode& nod
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");
for (int i = 0; i < integrator.getNumTabulatedFunctions(); 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());
...@@ -70,7 +73,8 @@ void CustomIntegratorProxy::serialize(const void* object, SerializationNode& nod ...@@ -70,7 +73,8 @@ void CustomIntegratorProxy::serialize(const void* object, SerializationNode& nod
} }
void* CustomIntegratorProxy::deserialize(const SerializationNode& node) const { void* CustomIntegratorProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1) int version = node.getIntProperty("version");
if (version < 1 || version > 2)
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");
...@@ -112,6 +116,11 @@ void* CustomIntegratorProxy::deserialize(const SerializationNode& node) const { ...@@ -112,6 +116,11 @@ void* CustomIntegratorProxy::deserialize(const SerializationNode& node) const {
throw(OpenMMException("Custom Integrator Deserialization: Unknown computation type")); throw(OpenMMException("Custom Integrator Deserialization: Unknown computation type"));
} }
} }
if (version > 1) {
const SerializationNode& functions = node.getChildNode("Functions");
for (auto& function : functions.getChildren())
integrator->addTabulatedFunction(function.getStringProperty("name"), function.decodeObject<TabulatedFunction>());
}
integrator->setKineticEnergyExpression(node.getStringProperty("kineticEnergyExpression")); integrator->setKineticEnergyExpression(node.getStringProperty("kineticEnergyExpression"));
integrator->setRandomNumberSeed(node.getIntProperty("randomSeed")); integrator->setRandomNumberSeed(node.getIntProperty("randomSeed"));
integrator->setConstraintTolerance(node.getDoubleProperty("constraintTolerance")); integrator->setConstraintTolerance(node.getDoubleProperty("constraintTolerance"));
......
...@@ -156,6 +156,10 @@ void testSerializeCustomIntegrator() { ...@@ -156,6 +156,10 @@ void testSerializeCustomIntegrator() {
intg->addComputeSum("summand2", "v*v+f*f"); intg->addComputeSum("summand2", "v*v+f*f");
intg->setConstraintTolerance(1e-5); intg->setConstraintTolerance(1e-5);
intg->setKineticEnergyExpression("m*v1*v1/2; v1=v+0.5*dt*f/m"); intg->setKineticEnergyExpression("m*v1*v1/2; v1=v+0.5*dt*f/m");
vector<double> values(10);
for (int i = 0; i < 10; i++)
values[i] = sin((double) i);
intg->addTabulatedFunction("f", new Continuous1DFunction(values, 0.5, 1.5));
stringstream ss; stringstream ss;
XmlSerializer::serialize<Integrator>(intg, "CustomIntegrator", ss); XmlSerializer::serialize<Integrator>(intg, "CustomIntegrator", ss);
CustomIntegrator *intg2 = dynamic_cast<CustomIntegrator*>(XmlSerializer::deserialize<Integrator>(ss)); CustomIntegrator *intg2 = dynamic_cast<CustomIntegrator*>(XmlSerializer::deserialize<Integrator>(ss));
...@@ -190,6 +194,19 @@ void testSerializeCustomIntegrator() { ...@@ -190,6 +194,19 @@ void testSerializeCustomIntegrator() {
ASSERT_EQUAL(intg->getRandomNumberSeed(), intg2->getRandomNumberSeed()); ASSERT_EQUAL(intg->getRandomNumberSeed(), intg2->getRandomNumberSeed());
ASSERT_EQUAL(intg->getStepSize(), intg2->getStepSize()); ASSERT_EQUAL(intg->getStepSize(), intg2->getStepSize());
ASSERT_EQUAL(intg->getConstraintTolerance(), intg2->getConstraintTolerance()); ASSERT_EQUAL(intg->getConstraintTolerance(), intg2->getConstraintTolerance());
ASSERT_EQUAL(intg->getNumTabulatedFunctions(), intg2->getNumTabulatedFunctions());
for (int i = 0; i < intg->getNumTabulatedFunctions(); i++) {
double min1, min2, max1, max2;
vector<double> val1, val2;
dynamic_cast<Continuous1DFunction&>(intg->getTabulatedFunction(i)).getFunctionParameters(val1, min1, max1);
dynamic_cast<Continuous1DFunction&>(intg2->getTabulatedFunction(i)).getFunctionParameters(val2, min2, max2);
ASSERT_EQUAL(intg->getTabulatedFunctionName(i), intg2->getTabulatedFunctionName(i));
ASSERT_EQUAL(min1, min2);
ASSERT_EQUAL(max1, max2);
ASSERT_EQUAL(val1.size(), val2.size());
for (int j = 0; j < (int) val1.size(); j++)
ASSERT_EQUAL(val1[j], val2[j]);
}
delete intg; delete intg;
delete intg2; delete intg2;
} }
......
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