Commit 5696464c authored by Charlles Abreu's avatar Charlles Abreu
Browse files

Serialization and tests for periodic 2D/3D tabulated functions

parent 0109698b
...@@ -51,24 +51,18 @@ void Continuous1DFunctionProxy::serialize(const void* object, SerializationNode& ...@@ -51,24 +51,18 @@ void Continuous1DFunctionProxy::serialize(const void* object, SerializationNode&
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (auto v : values) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", v); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
bool periodic; node.setBoolProperty("periodic", function.getPeriodic());
periodic = function.getPeriodic();
node.setBoolProperty("periodic", periodic);
} }
void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) const { void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) const {
int version = node.getIntProperty("version"); int version = node.getIntProperty("version");
if (version < 1 || version > 2) if (!(version == 1 || version == 2))
throw OpenMMException("Unsupported version number"); throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (auto& child : valuesNode.getChildren()) for (auto& child : valuesNode.getChildren())
values.push_back(child.getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
bool periodic; bool periodic = version == 1 ? false : node.getBoolProperty("periodic");
if (version == 1)
periodic = false;
else
periodic = node.getBoolProperty("periodic");
return new Continuous1DFunction(values, node.getDoubleProperty("min"), node.getDoubleProperty("max"), periodic); return new Continuous1DFunction(values, node.getDoubleProperty("min"), node.getDoubleProperty("max"), periodic);
} }
...@@ -76,7 +70,7 @@ Continuous2DFunctionProxy::Continuous2DFunctionProxy() : SerializationProxy("Con ...@@ -76,7 +70,7 @@ Continuous2DFunctionProxy::Continuous2DFunctionProxy() : SerializationProxy("Con
} }
void Continuous2DFunctionProxy::serialize(const void* object, SerializationNode& node) const { void Continuous2DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1); node.setIntProperty("version", 2);
const Continuous2DFunction& function = *reinterpret_cast<const Continuous2DFunction*>(object); const Continuous2DFunction& function = *reinterpret_cast<const Continuous2DFunction*>(object);
int xsize, ysize; int xsize, ysize;
double xmin, xmax, ymin, ymax; double xmin, xmax, ymin, ymax;
...@@ -91,24 +85,28 @@ void Continuous2DFunctionProxy::serialize(const void* object, SerializationNode& ...@@ -91,24 +85,28 @@ void Continuous2DFunctionProxy::serialize(const void* object, SerializationNode&
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (auto v : values) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", v); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
node.setBoolProperty("periodic", function.getPeriodic());
} }
void* Continuous2DFunctionProxy::deserialize(const SerializationNode& node) const { void* Continuous2DFunctionProxy::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");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (auto& child : valuesNode.getChildren()) for (auto& child : valuesNode.getChildren())
values.push_back(child.getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
bool periodic = version == 1 ? false : node.getBoolProperty("periodic");
return new Continuous2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values, return new Continuous2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values,
node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax")); node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"),
node.getDoubleProperty("ymin"),node.getDoubleProperty("ymax"), periodic);
} }
Continuous3DFunctionProxy::Continuous3DFunctionProxy() : SerializationProxy("Continuous3DFunction") { Continuous3DFunctionProxy::Continuous3DFunctionProxy() : SerializationProxy("Continuous3DFunction") {
} }
void Continuous3DFunctionProxy::serialize(const void* object, SerializationNode& node) const { void Continuous3DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1); node.setIntProperty("version", 2);
const Continuous3DFunction& function = *reinterpret_cast<const Continuous3DFunction*>(object); const Continuous3DFunction& function = *reinterpret_cast<const Continuous3DFunction*>(object);
int xsize, ysize, zsize; int xsize, ysize, zsize;
double xmin, xmax, ymin, ymax, zmin, zmax; double xmin, xmax, ymin, ymax, zmin, zmax;
...@@ -126,18 +124,21 @@ void Continuous3DFunctionProxy::serialize(const void* object, SerializationNode& ...@@ -126,18 +124,21 @@ void Continuous3DFunctionProxy::serialize(const void* object, SerializationNode&
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (auto v : values) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", v); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
node.setBoolProperty("periodic", function.getPeriodic());
} }
void* Continuous3DFunctionProxy::deserialize(const SerializationNode& node) const { void* Continuous3DFunctionProxy::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");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (auto& child : valuesNode.getChildren()) for (auto& child : valuesNode.getChildren())
values.push_back(child.getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
bool periodic = version == 1 ? false : node.getBoolProperty("periodic");
return new Continuous3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values, return new Continuous3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values,
node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax"), node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax"),
node.getDoubleProperty("zmin"), node.getDoubleProperty("zmax")); node.getDoubleProperty("zmin"), node.getDoubleProperty("zmax"), periodic);
} }
Discrete1DFunctionProxy::Discrete1DFunctionProxy() : SerializationProxy("Discrete1DFunction") { Discrete1DFunctionProxy::Discrete1DFunctionProxy() : SerializationProxy("Discrete1DFunction") {
......
...@@ -68,11 +68,11 @@ void testContinuous1DFunction() { ...@@ -68,11 +68,11 @@ void testContinuous1DFunction() {
void testPeriodicContinuous1DFunction() { void testPeriodicContinuous1DFunction() {
// Create a function. // Create a function.
double min = 0.5, max = 1.5; double min = 0.0, max = 2.0*M_PI;
vector<double> values(60); vector<double> values(60);
for (int i = 0; i < (int) values.size()-1; i++) for (int i = 0; i < (int) values.size(); i++)
values[i] = sin((double) i); values[i] = sin(2.0*M_PI*i/(values.size()-1));
values[values.size()-1] = values[0];
Continuous1DFunction function(values, min, max, true); Continuous1DFunction function(values, min, max, true);
// Serialize and then deserialize it. // Serialize and then deserialize it.
...@@ -127,6 +127,41 @@ void testContinuous2DFunction() { ...@@ -127,6 +127,41 @@ void testContinuous2DFunction() {
ASSERT_EQUAL(values[j], values2[j]); ASSERT_EQUAL(values[j], values2[j]);
} }
void testPeriodicContinuous2DFunction() {
// Create a function.
int xsize = 5, ysize = 12;
double xmin = 0.0, xmax = 2.0*M_PI, ymin = 0.0, ymax = 2.0*M_PI;
vector<double> values(xsize*ysize);
for (int i = 0; i < xsize; i++)
for (int j = 0; j < (int) ysize; j++)
values[i+j*xsize] = sin(2.0*M_PI*i/(xsize-1))*cos(2.0*M_PI*j/(ysize-1));
Continuous2DFunction function(xsize, ysize, values, xmin, xmax, ymin, ymax, true);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<Continuous2DFunction>(&function, "Function", buffer);
Continuous2DFunction* copy = XmlSerializer::deserialize<Continuous2DFunction>(buffer);
// Compare the two forces to see if they are identical.
int xsize2, ysize2;
double xmin2, xmax2, ymin2, ymax2;
vector<double> values2;
copy->getFunctionParameters(xsize2, ysize2, values2, xmin2, xmax2, ymin2, ymax2);
ASSERT_EQUAL(xsize, xsize2);
ASSERT_EQUAL(ysize, ysize2);
ASSERT_EQUAL(xmin, xmin2);
ASSERT_EQUAL(xmax, xmax2);
ASSERT_EQUAL(ymin, ymin2);
ASSERT_EQUAL(ymax, ymax2);
ASSERT_EQUAL(values.size(), values2.size());
for (int j = 0; j < (int) values.size(); j++)
ASSERT_EQUAL(values[j], values2[j]);
}
void testContinuous3DFunction() { void testContinuous3DFunction() {
// Create a function. // Create a function.
...@@ -163,6 +198,44 @@ void testContinuous3DFunction() { ...@@ -163,6 +198,44 @@ void testContinuous3DFunction() {
ASSERT_EQUAL(values[j], values2[j]); ASSERT_EQUAL(values[j], values2[j]);
} }
void testPeriodicContinuous3DFunction() {
// Create a function.
int xsize = 5, ysize = 4, zsize = 3;
double xmin = 0.0, xmax = 2.0*M_PI, ymin = 0.0, ymax = 2.0*M_PI, zmin = 0.0, zmax = 2.0*M_PI;
vector<double> values(xsize*ysize*zsize);
for (int i = 0; i < xsize; i++)
for (int j = 0; j < ysize; j++)
for (int k = 0; k < zsize; k++)
values[i+j*xsize+k*xsize*ysize] = sin(2.0*M_PI*i/(xsize-1))*cos(2.0*M_PI*j/(ysize-1))*sin(2.0*M_PI*k/(zsize-1));
Continuous3DFunction function(xsize, ysize, zsize, values, xmin, xmax, ymin, ymax, zmin, zmax, true);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<Continuous3DFunction>(&function, "Function", buffer);
Continuous3DFunction* copy = XmlSerializer::deserialize<Continuous3DFunction>(buffer);
// Compare the two forces to see if they are identical.
int xsize2, ysize2, zsize2;
double xmin2, xmax2, ymin2, ymax2, zmin2, zmax2;
vector<double> values2;
copy->getFunctionParameters(xsize2, ysize2, zsize2, values2, xmin2, xmax2, ymin2, ymax2, zmin2, zmax2);
ASSERT_EQUAL(xsize, xsize2);
ASSERT_EQUAL(ysize, ysize2);
ASSERT_EQUAL(zsize, zsize2);
ASSERT_EQUAL(xmin, xmin2);
ASSERT_EQUAL(xmax, xmax2);
ASSERT_EQUAL(ymin, ymin2);
ASSERT_EQUAL(ymax, ymax2);
ASSERT_EQUAL(zmin, zmin2);
ASSERT_EQUAL(zmax, zmax2);
ASSERT_EQUAL(values.size(), values2.size());
for (int j = 0; j < (int) values.size(); j++)
ASSERT_EQUAL(values[j], values2[j]);
}
void testDiscrete1DFunction() { void testDiscrete1DFunction() {
// Create a function. // Create a function.
...@@ -244,8 +317,11 @@ void testDiscrete3DFunction() { ...@@ -244,8 +317,11 @@ void testDiscrete3DFunction() {
int main() { int main() {
try { try {
testContinuous1DFunction(); testContinuous1DFunction();
testPeriodicContinuous1DFunction();
testContinuous2DFunction(); testContinuous2DFunction();
testPeriodicContinuous2DFunction();
testContinuous3DFunction(); testContinuous3DFunction();
testPeriodicContinuous3DFunction();
testDiscrete1DFunction(); testDiscrete1DFunction();
testDiscrete2DFunction(); testDiscrete2DFunction();
testDiscrete3DFunction(); testDiscrete3DFunction();
......
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