Commit 75c9f3d5 authored by peastman's avatar peastman
Browse files

Implemented serialization for TabulatedFunctions

parent f25db674
#ifndef OPENMM_TABULATEDFUNCTION_PROXIES_H_
#define OPENMM_TABULATEDFUNCTION_PROXIES_H_
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2014 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
#include "openmm/internal/windowsExport.h"
#include "openmm/serialization/SerializationProxy.h"
namespace OpenMM {
/**
* This is a proxy for serializing Continuous1DFunction objects.
*/
class OPENMM_EXPORT Continuous1DFunctionProxy : public SerializationProxy {
public:
Continuous1DFunctionProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
/**
* This is a proxy for serializing Continuous2DFunction objects.
*/
class OPENMM_EXPORT Continuous2DFunctionProxy : public SerializationProxy {
public:
Continuous2DFunctionProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
/**
* This is a proxy for serializing Continuous3DFunction objects.
*/
class OPENMM_EXPORT Continuous3DFunctionProxy : public SerializationProxy {
public:
Continuous3DFunctionProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
/**
* This is a proxy for serializing Discrete1DFunction objects.
*/
class OPENMM_EXPORT Discrete1DFunctionProxy : public SerializationProxy {
public:
Discrete1DFunctionProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
/**
* This is a proxy for serializing Discrete2DFunction objects.
*/
class OPENMM_EXPORT Discrete2DFunctionProxy : public SerializationProxy {
public:
Discrete2DFunctionProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
/**
* This is a proxy for serializing Discrete3DFunction objects.
*/
class OPENMM_EXPORT Discrete3DFunctionProxy : public SerializationProxy {
public:
Discrete3DFunctionProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_TABULATEDFUNCTION_PROXIES_H_*/
...@@ -73,6 +73,9 @@ void CustomCompoundBondForceProxy::serialize(const void* object, SerializationNo ...@@ -73,6 +73,9 @@ void CustomCompoundBondForceProxy::serialize(const void* object, SerializationNo
node.setDoubleProperty(key.str(), params[j]); node.setDoubleProperty(key.str(), params[j]);
} }
} }
SerializationNode& functions = node.createChildNode("Functions");
for (int i = 0; i < force.getNumTabulatedFunctions(); i++)
functions.createChildNode("Function", &force.getTabulatedFunction(i)).setStringProperty("name", force.getTabulatedFunctionName(i));
} }
void* CustomCompoundBondForceProxy::deserialize(const SerializationNode& node) const { void* CustomCompoundBondForceProxy::deserialize(const SerializationNode& node) const {
...@@ -110,6 +113,22 @@ void* CustomCompoundBondForceProxy::deserialize(const SerializationNode& node) c ...@@ -110,6 +113,22 @@ void* CustomCompoundBondForceProxy::deserialize(const SerializationNode& node) c
} }
force->addBond(particles, params); force->addBond(particles, params);
} }
const SerializationNode& functions = node.getChildNode("Functions");
for (int i = 0; i < (int) functions.getChildren().size(); i++) {
const SerializationNode& function = functions.getChildren()[i];
if (function.hasProperty("type")) {
force->addTabulatedFunction(function.getStringProperty("name"), function.decodeObject<TabulatedFunction>());
}
else {
// This is an old file created before TabulatedFunction existed.
const SerializationNode& valuesNode = function.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
force->addTabulatedFunction(function.getStringProperty("name"), new Continuous1DFunction(values, function.getDoubleProperty("min"), function.getDoubleProperty("max")));
}
}
return force; return force;
} }
catch (...) { catch (...) {
......
...@@ -87,16 +87,8 @@ void CustomGBForceProxy::serialize(const void* object, SerializationNode& node) ...@@ -87,16 +87,8 @@ void CustomGBForceProxy::serialize(const void* object, SerializationNode& node)
exclusions.createChildNode("Exclusion").setIntProperty("p1", particle1).setIntProperty("p2", particle2); exclusions.createChildNode("Exclusion").setIntProperty("p1", particle1).setIntProperty("p2", particle2);
} }
SerializationNode& functions = node.createChildNode("Functions"); SerializationNode& functions = node.createChildNode("Functions");
for (int i = 0; i < force.getNumFunctions(); i++) { for (int i = 0; i < force.getNumTabulatedFunctions(); i++)
string name; functions.createChildNode("Function", &force.getTabulatedFunction(i)).setStringProperty("name", force.getTabulatedFunctionName(i));
vector<double> values;
double min, max;
force.getFunctionParameters(i, name, values, min, max);
SerializationNode& node = functions.createChildNode("Function").setStringProperty("name", name).setDoubleProperty("min", min).setDoubleProperty("max", max);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
} }
void* CustomGBForceProxy::deserialize(const SerializationNode& node) const { void* CustomGBForceProxy::deserialize(const SerializationNode& node) const {
...@@ -147,11 +139,18 @@ void* CustomGBForceProxy::deserialize(const SerializationNode& node) const { ...@@ -147,11 +139,18 @@ void* CustomGBForceProxy::deserialize(const SerializationNode& node) const {
const SerializationNode& functions = node.getChildNode("Functions"); const SerializationNode& functions = node.getChildNode("Functions");
for (int i = 0; i < (int) functions.getChildren().size(); i++) { for (int i = 0; i < (int) functions.getChildren().size(); i++) {
const SerializationNode& function = functions.getChildren()[i]; const SerializationNode& function = functions.getChildren()[i];
const SerializationNode& valuesNode = function.getChildNode("Values"); if (function.hasProperty("type")) {
vector<double> values; force->addTabulatedFunction(function.getStringProperty("name"), function.decodeObject<TabulatedFunction>());
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) }
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); else {
force->addFunction(function.getStringProperty("name"), values, function.getDoubleProperty("min"), function.getDoubleProperty("max")); // This is an old file created before TabulatedFunction existed.
const SerializationNode& valuesNode = function.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
force->addTabulatedFunction(function.getStringProperty("name"), new Continuous1DFunction(values, function.getDoubleProperty("min"), function.getDoubleProperty("max")));
}
} }
return force; return force;
} }
......
...@@ -92,16 +92,8 @@ void CustomHbondForceProxy::serialize(const void* object, SerializationNode& nod ...@@ -92,16 +92,8 @@ void CustomHbondForceProxy::serialize(const void* object, SerializationNode& nod
exclusions.createChildNode("Exclusion").setIntProperty("donor", donor).setIntProperty("acceptor", acceptor); exclusions.createChildNode("Exclusion").setIntProperty("donor", donor).setIntProperty("acceptor", acceptor);
} }
SerializationNode& functions = node.createChildNode("Functions"); SerializationNode& functions = node.createChildNode("Functions");
for (int i = 0; i < force.getNumFunctions(); i++) { for (int i = 0; i < force.getNumTabulatedFunctions(); i++)
string name; functions.createChildNode("Function", &force.getTabulatedFunction(i)).setStringProperty("name", force.getTabulatedFunctionName(i));
vector<double> values;
double min, max;
force.getFunctionParameters(i, name, values, min, max);
SerializationNode& node = functions.createChildNode("Function").setStringProperty("name", name).setDoubleProperty("min", min).setDoubleProperty("max", max);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
} }
void* CustomHbondForceProxy::deserialize(const SerializationNode& node) const { void* CustomHbondForceProxy::deserialize(const SerializationNode& node) const {
...@@ -159,11 +151,18 @@ void* CustomHbondForceProxy::deserialize(const SerializationNode& node) const { ...@@ -159,11 +151,18 @@ void* CustomHbondForceProxy::deserialize(const SerializationNode& node) const {
const SerializationNode& functions = node.getChildNode("Functions"); const SerializationNode& functions = node.getChildNode("Functions");
for (int i = 0; i < (int) functions.getChildren().size(); i++) { for (int i = 0; i < (int) functions.getChildren().size(); i++) {
const SerializationNode& function = functions.getChildren()[i]; const SerializationNode& function = functions.getChildren()[i];
const SerializationNode& valuesNode = function.getChildNode("Values"); if (function.hasProperty("type")) {
vector<double> values; force->addTabulatedFunction(function.getStringProperty("name"), function.decodeObject<TabulatedFunction>());
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) }
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); else {
force->addFunction(function.getStringProperty("name"), values, function.getDoubleProperty("min"), function.getDoubleProperty("max")); // This is an old file created before TabulatedFunction existed.
const SerializationNode& valuesNode = function.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
force->addTabulatedFunction(function.getStringProperty("name"), new Continuous1DFunction(values, function.getDoubleProperty("min"), function.getDoubleProperty("max")));
}
} }
return force; return force;
} }
......
...@@ -74,16 +74,8 @@ void CustomNonbondedForceProxy::serialize(const void* object, SerializationNode& ...@@ -74,16 +74,8 @@ void CustomNonbondedForceProxy::serialize(const void* object, SerializationNode&
exclusions.createChildNode("Exclusion").setIntProperty("p1", particle1).setIntProperty("p2", particle2); exclusions.createChildNode("Exclusion").setIntProperty("p1", particle1).setIntProperty("p2", particle2);
} }
SerializationNode& functions = node.createChildNode("Functions"); SerializationNode& functions = node.createChildNode("Functions");
for (int i = 0; i < force.getNumFunctions(); i++) { for (int i = 0; i < force.getNumTabulatedFunctions(); i++)
string name; functions.createChildNode("Function", &force.getTabulatedFunction(i)).setStringProperty("name", force.getTabulatedFunctionName(i));
vector<double> values;
double min, max;
force.getFunctionParameters(i, name, values, min, max);
SerializationNode& node = functions.createChildNode("Function").setStringProperty("name", name).setDoubleProperty("min", min).setDoubleProperty("max", max);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
} }
void* CustomNonbondedForceProxy::deserialize(const SerializationNode& node) const { void* CustomNonbondedForceProxy::deserialize(const SerializationNode& node) const {
...@@ -124,11 +116,18 @@ void* CustomNonbondedForceProxy::deserialize(const SerializationNode& node) cons ...@@ -124,11 +116,18 @@ void* CustomNonbondedForceProxy::deserialize(const SerializationNode& node) cons
const SerializationNode& functions = node.getChildNode("Functions"); const SerializationNode& functions = node.getChildNode("Functions");
for (int i = 0; i < (int) functions.getChildren().size(); i++) { for (int i = 0; i < (int) functions.getChildren().size(); i++) {
const SerializationNode& function = functions.getChildren()[i]; const SerializationNode& function = functions.getChildren()[i];
const SerializationNode& valuesNode = function.getChildNode("Values"); if (function.hasProperty("type")) {
vector<double> values; force->addTabulatedFunction(function.getStringProperty("name"), function.decodeObject<TabulatedFunction>());
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) }
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); else {
force->addFunction(function.getStringProperty("name"), values, function.getDoubleProperty("min"), function.getDoubleProperty("max")); // This is an old file created before TabulatedFunction existed.
const SerializationNode& valuesNode = function.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
force->addTabulatedFunction(function.getStringProperty("name"), new Continuous1DFunction(values, function.getDoubleProperty("min"), function.getDoubleProperty("max")));
}
} }
return force; return force;
} }
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
#include "openmm/AndersenThermostat.h" #include "openmm/AndersenThermostat.h"
#include "openmm/BrownianIntegrator.h"
#include "openmm/CMAPTorsionForce.h" #include "openmm/CMAPTorsionForce.h"
#include "openmm/CMMotionRemover.h" #include "openmm/CMMotionRemover.h"
#include "openmm/CustomAngleForce.h" #include "openmm/CustomAngleForce.h"
...@@ -38,26 +39,26 @@ ...@@ -38,26 +39,26 @@
#include "openmm/CustomExternalForce.h" #include "openmm/CustomExternalForce.h"
#include "openmm/CustomGBForce.h" #include "openmm/CustomGBForce.h"
#include "openmm/CustomHbondForce.h" #include "openmm/CustomHbondForce.h"
#include "openmm/CustomIntegrator.h"
#include "openmm/CustomNonbondedForce.h" #include "openmm/CustomNonbondedForce.h"
#include "openmm/CustomTorsionForce.h" #include "openmm/CustomTorsionForce.h"
#include "openmm/HarmonicAngleForce.h"
#include "openmm/GBSAOBCForce.h" #include "openmm/GBSAOBCForce.h"
#include "openmm/GBVIForce.h" #include "openmm/GBVIForce.h"
#include "openmm/HarmonicAngleForce.h"
#include "openmm/HarmonicBondForce.h" #include "openmm/HarmonicBondForce.h"
#include "openmm/LangevinIntegrator.h"
#include "openmm/MonteCarloBarostat.h" #include "openmm/MonteCarloBarostat.h"
#include "openmm/NonbondedForce.h" #include "openmm/NonbondedForce.h"
#include "openmm/PeriodicTorsionForce.h" #include "openmm/PeriodicTorsionForce.h"
#include "openmm/RBTorsionForce.h" #include "openmm/RBTorsionForce.h"
#include "openmm/System.h" #include "openmm/System.h"
#include "openmm/TabulatedFunction.h"
#include "openmm/BrownianIntegrator.h"
#include "openmm/CustomIntegrator.h"
#include "openmm/LangevinIntegrator.h"
#include "openmm/VariableLangevinIntegrator.h" #include "openmm/VariableLangevinIntegrator.h"
#include "openmm/VariableVerletIntegrator.h" #include "openmm/VariableVerletIntegrator.h"
#include "openmm/VerletIntegrator.h" #include "openmm/VerletIntegrator.h"
#include "openmm/serialization/SerializationProxy.h" #include "openmm/serialization/SerializationProxy.h"
#include "openmm/serialization/BrownianIntegratorProxy.h"
#include "openmm/serialization/AndersenThermostatProxy.h" #include "openmm/serialization/AndersenThermostatProxy.h"
#include "openmm/serialization/CMAPTorsionForceProxy.h" #include "openmm/serialization/CMAPTorsionForceProxy.h"
#include "openmm/serialization/CMMotionRemoverProxy.h" #include "openmm/serialization/CMMotionRemoverProxy.h"
...@@ -67,23 +68,21 @@ ...@@ -67,23 +68,21 @@
#include "openmm/serialization/CustomExternalForceProxy.h" #include "openmm/serialization/CustomExternalForceProxy.h"
#include "openmm/serialization/CustomGBForceProxy.h" #include "openmm/serialization/CustomGBForceProxy.h"
#include "openmm/serialization/CustomHbondForceProxy.h" #include "openmm/serialization/CustomHbondForceProxy.h"
#include "openmm/serialization/CustomIntegratorProxy.h"
#include "openmm/serialization/CustomNonbondedForceProxy.h" #include "openmm/serialization/CustomNonbondedForceProxy.h"
#include "openmm/serialization/CustomTorsionForceProxy.h" #include "openmm/serialization/CustomTorsionForceProxy.h"
#include "openmm/serialization/GBSAOBCForceProxy.h" #include "openmm/serialization/GBSAOBCForceProxy.h"
#include "openmm/serialization/GBVIForceProxy.h" #include "openmm/serialization/GBVIForceProxy.h"
#include "openmm/serialization/HarmonicAngleForceProxy.h" #include "openmm/serialization/HarmonicAngleForceProxy.h"
#include "openmm/serialization/HarmonicBondForceProxy.h" #include "openmm/serialization/HarmonicBondForceProxy.h"
#include "openmm/serialization/LangevinIntegratorProxy.h"
#include "openmm/serialization/MonteCarloBarostatProxy.h" #include "openmm/serialization/MonteCarloBarostatProxy.h"
#include "openmm/serialization/NonbondedForceProxy.h" #include "openmm/serialization/NonbondedForceProxy.h"
#include "openmm/serialization/PeriodicTorsionForceProxy.h" #include "openmm/serialization/PeriodicTorsionForceProxy.h"
#include "openmm/serialization/RBTorsionForceProxy.h" #include "openmm/serialization/RBTorsionForceProxy.h"
#include "openmm/serialization/SystemProxy.h"
#include "openmm/serialization/StateProxy.h" #include "openmm/serialization/StateProxy.h"
#include "openmm/serialization/SystemProxy.h"
#include "openmm/serialization/BrownianIntegratorProxy.h" #include "openmm/serialization/TabulatedFunctionProxies.h"
#include "openmm/serialization/CustomIntegratorProxy.h"
#include "openmm/serialization/LangevinIntegratorProxy.h"
#include "openmm/serialization/VariableLangevinIntegratorProxy.h" #include "openmm/serialization/VariableLangevinIntegratorProxy.h"
#include "openmm/serialization/VariableVerletIntegratorProxy.h" #include "openmm/serialization/VariableVerletIntegratorProxy.h"
#include "openmm/serialization/VerletIntegratorProxy.h" #include "openmm/serialization/VerletIntegratorProxy.h"
...@@ -104,29 +103,35 @@ using namespace OpenMM; ...@@ -104,29 +103,35 @@ using namespace OpenMM;
extern "C" void registerSerializationProxies() { extern "C" void registerSerializationProxies() {
SerializationProxy::registerProxy(typeid(AndersenThermostat), new AndersenThermostatProxy()); SerializationProxy::registerProxy(typeid(AndersenThermostat), new AndersenThermostatProxy());
SerializationProxy::registerProxy(typeid(BrownianIntegrator), new BrownianIntegratorProxy());
SerializationProxy::registerProxy(typeid(CMAPTorsionForce), new CMAPTorsionForceProxy()); SerializationProxy::registerProxy(typeid(CMAPTorsionForce), new CMAPTorsionForceProxy());
SerializationProxy::registerProxy(typeid(CMMotionRemover), new CMMotionRemoverProxy()); SerializationProxy::registerProxy(typeid(CMMotionRemover), new CMMotionRemoverProxy());
SerializationProxy::registerProxy(typeid(Continuous1DFunction), new Continuous1DFunctionProxy());
SerializationProxy::registerProxy(typeid(Continuous2DFunction), new Continuous2DFunctionProxy());
SerializationProxy::registerProxy(typeid(Continuous3DFunction), new Continuous3DFunctionProxy());
SerializationProxy::registerProxy(typeid(CustomAngleForce), new CustomAngleForceProxy()); SerializationProxy::registerProxy(typeid(CustomAngleForce), new CustomAngleForceProxy());
SerializationProxy::registerProxy(typeid(CustomBondForce), new CustomBondForceProxy()); SerializationProxy::registerProxy(typeid(CustomBondForce), new CustomBondForceProxy());
SerializationProxy::registerProxy(typeid(CustomCompoundBondForce), new CustomCompoundBondForceProxy()); SerializationProxy::registerProxy(typeid(CustomCompoundBondForce), new CustomCompoundBondForceProxy());
SerializationProxy::registerProxy(typeid(CustomExternalForce), new CustomExternalForceProxy()); SerializationProxy::registerProxy(typeid(CustomExternalForce), new CustomExternalForceProxy());
SerializationProxy::registerProxy(typeid(CustomGBForce), new CustomGBForceProxy()); SerializationProxy::registerProxy(typeid(CustomGBForce), new CustomGBForceProxy());
SerializationProxy::registerProxy(typeid(CustomHbondForce), new CustomHbondForceProxy()); SerializationProxy::registerProxy(typeid(CustomHbondForce), new CustomHbondForceProxy());
SerializationProxy::registerProxy(typeid(CustomIntegrator), new CustomIntegratorProxy());
SerializationProxy::registerProxy(typeid(CustomNonbondedForce), new CustomNonbondedForceProxy()); SerializationProxy::registerProxy(typeid(CustomNonbondedForce), new CustomNonbondedForceProxy());
SerializationProxy::registerProxy(typeid(CustomTorsionForce), new CustomTorsionForceProxy()); SerializationProxy::registerProxy(typeid(CustomTorsionForce), new CustomTorsionForceProxy());
SerializationProxy::registerProxy(typeid(Discrete1DFunction), new Discrete1DFunctionProxy());
SerializationProxy::registerProxy(typeid(Discrete2DFunction), new Discrete2DFunctionProxy());
SerializationProxy::registerProxy(typeid(Discrete3DFunction), new Discrete3DFunctionProxy());
SerializationProxy::registerProxy(typeid(GBSAOBCForce), new GBSAOBCForceProxy()); SerializationProxy::registerProxy(typeid(GBSAOBCForce), new GBSAOBCForceProxy());
SerializationProxy::registerProxy(typeid(GBVIForce), new GBVIForceProxy()); SerializationProxy::registerProxy(typeid(GBVIForce), new GBVIForceProxy());
SerializationProxy::registerProxy(typeid(HarmonicAngleForce), new HarmonicAngleForceProxy()); SerializationProxy::registerProxy(typeid(HarmonicAngleForce), new HarmonicAngleForceProxy());
SerializationProxy::registerProxy(typeid(HarmonicBondForce), new HarmonicBondForceProxy()); SerializationProxy::registerProxy(typeid(HarmonicBondForce), new HarmonicBondForceProxy());
SerializationProxy::registerProxy(typeid(LangevinIntegrator), new LangevinIntegratorProxy());
SerializationProxy::registerProxy(typeid(MonteCarloBarostat), new MonteCarloBarostatProxy()); SerializationProxy::registerProxy(typeid(MonteCarloBarostat), new MonteCarloBarostatProxy());
SerializationProxy::registerProxy(typeid(NonbondedForce), new NonbondedForceProxy()); SerializationProxy::registerProxy(typeid(NonbondedForce), new NonbondedForceProxy());
SerializationProxy::registerProxy(typeid(PeriodicTorsionForce), new PeriodicTorsionForceProxy()); SerializationProxy::registerProxy(typeid(PeriodicTorsionForce), new PeriodicTorsionForceProxy());
SerializationProxy::registerProxy(typeid(RBTorsionForce), new RBTorsionForceProxy()); SerializationProxy::registerProxy(typeid(RBTorsionForce), new RBTorsionForceProxy());
SerializationProxy::registerProxy(typeid(System), new SystemProxy()); SerializationProxy::registerProxy(typeid(System), new SystemProxy());
SerializationProxy::registerProxy(typeid(State), new StateProxy()); SerializationProxy::registerProxy(typeid(State), new StateProxy());
SerializationProxy::registerProxy(typeid(BrownianIntegrator), new BrownianIntegratorProxy());
SerializationProxy::registerProxy(typeid(CustomIntegrator), new CustomIntegratorProxy());
SerializationProxy::registerProxy(typeid(LangevinIntegrator), new LangevinIntegratorProxy());
SerializationProxy::registerProxy(typeid(VariableLangevinIntegrator), new VariableLangevinIntegratorProxy()); SerializationProxy::registerProxy(typeid(VariableLangevinIntegrator), new VariableLangevinIntegratorProxy());
SerializationProxy::registerProxy(typeid(VariableVerletIntegrator), new VariableVerletIntegratorProxy()); SerializationProxy::registerProxy(typeid(VariableVerletIntegrator), new VariableVerletIntegratorProxy());
SerializationProxy::registerProxy(typeid(VerletIntegrator), new VerletIntegratorProxy()); SerializationProxy::registerProxy(typeid(VerletIntegrator), new VerletIntegratorProxy());
......
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2014 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
#include "openmm/serialization/TabulatedFunctionProxies.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/TabulatedFunction.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
Continuous1DFunctionProxy::Continuous1DFunctionProxy() : SerializationProxy("Continuous1DFunction") {
}
void Continuous1DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const Continuous1DFunction& function = *reinterpret_cast<const Continuous1DFunction*>(object);
double min, max;
vector<double> values;
function.getFunctionParameters(values, min, max);
node.setDoubleProperty("min", min);
node.setDoubleProperty("max", max);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
return new Continuous1DFunction(values, node.getDoubleProperty("min"), node.getDoubleProperty("max"));
}
Continuous2DFunctionProxy::Continuous2DFunctionProxy() : SerializationProxy("Continuous2DFunction") {
}
void Continuous2DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const Continuous2DFunction& function = *reinterpret_cast<const Continuous2DFunction*>(object);
int xsize, ysize;
double xmin, xmax, ymin, ymax;
vector<double> values;
function.getFunctionParameters(xsize, ysize, values, xmin, xmax, ymin, ymax);
node.setDoubleProperty("xsize", xsize);
node.setDoubleProperty("ysize", ysize);
node.setDoubleProperty("xmin", xmin);
node.setDoubleProperty("xmax", xmax);
node.setDoubleProperty("ymin", ymin);
node.setDoubleProperty("ymax", ymax);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
void* Continuous2DFunctionProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
return new Continuous2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values,
node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax"));
}
Continuous3DFunctionProxy::Continuous3DFunctionProxy() : SerializationProxy("Continuous3DFunction") {
}
void Continuous3DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const Continuous3DFunction& function = *reinterpret_cast<const Continuous3DFunction*>(object);
int xsize, ysize, zsize;
double xmin, xmax, ymin, ymax, zmin, zmax;
vector<double> values;
function.getFunctionParameters(xsize, ysize, zsize, values, xmin, xmax, ymin, ymax, zmin, zmax);
node.setDoubleProperty("xsize", xsize);
node.setDoubleProperty("ysize", ysize);
node.setDoubleProperty("zsize", zsize);
node.setDoubleProperty("xmin", xmin);
node.setDoubleProperty("xmax", xmax);
node.setDoubleProperty("ymin", ymin);
node.setDoubleProperty("ymax", ymax);
node.setDoubleProperty("zmin", zmin);
node.setDoubleProperty("zmax", zmax);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
void* Continuous3DFunctionProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
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("zmin"), node.getDoubleProperty("zmax"));
}
Discrete1DFunctionProxy::Discrete1DFunctionProxy() : SerializationProxy("Discrete1DFunction") {
}
void Discrete1DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const Discrete1DFunction& function = *reinterpret_cast<const Discrete1DFunction*>(object);
vector<double> values;
function.getFunctionParameters(values);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
void* Discrete1DFunctionProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
return new Discrete1DFunction(values);
}
Discrete2DFunctionProxy::Discrete2DFunctionProxy() : SerializationProxy("Discrete2DFunction") {
}
void Discrete2DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const Discrete2DFunction& function = *reinterpret_cast<const Discrete2DFunction*>(object);
int xsize, ysize;
vector<double> values;
function.getFunctionParameters(xsize, ysize, values);
node.setDoubleProperty("xsize", xsize);
node.setDoubleProperty("ysize", ysize);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
void* Discrete2DFunctionProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
return new Discrete2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values);
}
Discrete3DFunctionProxy::Discrete3DFunctionProxy() : SerializationProxy("Discrete3DFunction") {
}
void Discrete3DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const Discrete3DFunction& function = *reinterpret_cast<const Discrete3DFunction*>(object);
int xsize, ysize, zsize;
vector<double> values;
function.getFunctionParameters(xsize, ysize, zsize, values);
node.setDoubleProperty("xsize", xsize);
node.setDoubleProperty("ysize", ysize);
node.setDoubleProperty("zsize", zsize);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
void* Discrete3DFunctionProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
return new Discrete3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values);
}
...@@ -62,6 +62,10 @@ void testSerialization() { ...@@ -62,6 +62,10 @@ void testSerialization() {
particles[2] = 1; particles[2] = 1;
params[0] = 2.1; params[0] = 2.1;
force.addBond(particles, params); force.addBond(particles, params);
vector<double> values(10);
for (int i = 0; i < 10; i++)
values[i] = sin((double) i);
force.addTabulatedFunction("f", new Continuous1DFunction(values, 0.5, 1.5));
// Serialize and then deserialize it. // Serialize and then deserialize it.
...@@ -95,6 +99,19 @@ void testSerialization() { ...@@ -95,6 +99,19 @@ void testSerialization() {
for (int j = 0; j < (int) particles1.size(); j++) for (int j = 0; j < (int) particles1.size(); j++)
ASSERT_EQUAL(particles1[j], particles2[j]); ASSERT_EQUAL(particles1[j], particles2[j]);
} }
ASSERT_EQUAL(force.getNumTabulatedFunctions(), force2.getNumTabulatedFunctions());
for (int i = 0; i < force.getNumTabulatedFunctions(); i++) {
double min1, min2, max1, max2;
vector<double> val1, val2;
dynamic_cast<Continuous1DFunction&>(force.getTabulatedFunction(i)).getFunctionParameters(val1, min1, max1);
dynamic_cast<Continuous1DFunction&>(force2.getTabulatedFunction(i)).getFunctionParameters(val2, min2, max2);
ASSERT_EQUAL(force.getTabulatedFunctionName(i), force2.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]);
}
} }
int main() { int main() {
......
...@@ -63,7 +63,7 @@ void testSerialization() { ...@@ -63,7 +63,7 @@ void testSerialization() {
vector<double> values(10); vector<double> values(10);
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
values[i] = sin((double) i); values[i] = sin((double) i);
force.addFunction("f", values, 0.5, 1.5); force.addTabulatedFunction("f", new Discrete1DFunction(values));
// Serialize and then deserialize it. // Serialize and then deserialize it.
...@@ -120,16 +120,12 @@ void testSerialization() { ...@@ -120,16 +120,12 @@ void testSerialization() {
ASSERT_EQUAL(a1, a2); ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b1, b2); ASSERT_EQUAL(b1, b2);
} }
ASSERT_EQUAL(force.getNumFunctions(), force2.getNumFunctions()); ASSERT_EQUAL(force.getNumTabulatedFunctions(), force2.getNumTabulatedFunctions());
for (int i = 0; i < force.getNumFunctions(); i++) { for (int i = 0; i < force.getNumTabulatedFunctions(); i++) {
string name1, name2;
double min1, min2, max1, max2;
vector<double> val1, val2; vector<double> val1, val2;
force.getFunctionParameters(i, name1, val1, min1, max1); dynamic_cast<Discrete1DFunction&>(force.getTabulatedFunction(i)).getFunctionParameters(val1);
force2.getFunctionParameters(i, name2, val2, min2, max2); dynamic_cast<Discrete1DFunction&>(force2.getTabulatedFunction(i)).getFunctionParameters(val2);
ASSERT_EQUAL(name1, name2); ASSERT_EQUAL(force.getTabulatedFunctionName(i), force2.getTabulatedFunctionName(i));
ASSERT_EQUAL(min1, min2);
ASSERT_EQUAL(max1, max2);
ASSERT_EQUAL(val1.size(), val2.size()); ASSERT_EQUAL(val1.size(), val2.size());
for (int j = 0; j < (int) val1.size(); j++) for (int j = 0; j < (int) val1.size(); j++)
ASSERT_EQUAL(val1[j], val2[j]); ASSERT_EQUAL(val1[j], val2[j]);
......
...@@ -66,7 +66,7 @@ void testSerialization() { ...@@ -66,7 +66,7 @@ void testSerialization() {
vector<double> values(10); vector<double> values(10);
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
values[i] = sin((double) i); values[i] = sin((double) i);
force.addFunction("f", values, 0.5, 1.5); force.addTabulatedFunction("f", new Discrete1DFunction(values));
// Serialize and then deserialize it. // Serialize and then deserialize it.
...@@ -125,16 +125,12 @@ void testSerialization() { ...@@ -125,16 +125,12 @@ void testSerialization() {
ASSERT_EQUAL(a1, a2); ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b1, b2); ASSERT_EQUAL(b1, b2);
} }
ASSERT_EQUAL(force.getNumFunctions(), force2.getNumFunctions()); ASSERT_EQUAL(force.getNumTabulatedFunctions(), force2.getNumTabulatedFunctions());
for (int i = 0; i < force.getNumFunctions(); i++) { for (int i = 0; i < force.getNumTabulatedFunctions(); i++) {
string name1, name2;
double min1, min2, max1, max2;
vector<double> val1, val2; vector<double> val1, val2;
force.getFunctionParameters(i, name1, val1, min1, max1); dynamic_cast<Discrete1DFunction&>(force.getTabulatedFunction(i)).getFunctionParameters(val1);
force2.getFunctionParameters(i, name2, val2, min2, max2); dynamic_cast<Discrete1DFunction&>(force2.getTabulatedFunction(i)).getFunctionParameters(val2);
ASSERT_EQUAL(name1, name2); ASSERT_EQUAL(force.getTabulatedFunctionName(i), force2.getTabulatedFunctionName(i));
ASSERT_EQUAL(min1, min2);
ASSERT_EQUAL(max1, max2);
ASSERT_EQUAL(val1.size(), val2.size()); ASSERT_EQUAL(val1.size(), val2.size());
for (int j = 0; j < (int) val1.size(); j++) for (int j = 0; j < (int) val1.size(); j++)
ASSERT_EQUAL(val1[j], val2[j]); ASSERT_EQUAL(val1[j], val2[j]);
......
...@@ -98,14 +98,13 @@ void testSerialization() { ...@@ -98,14 +98,13 @@ void testSerialization() {
ASSERT_EQUAL(a1, a2); ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b1, b2); ASSERT_EQUAL(b1, b2);
} }
ASSERT_EQUAL(force.getNumFunctions(), force2.getNumFunctions()); ASSERT_EQUAL(force.getNumTabulatedFunctions(), force2.getNumTabulatedFunctions());
for (int i = 0; i < force.getNumFunctions(); i++) { for (int i = 0; i < force.getNumTabulatedFunctions(); i++) {
string name1, name2;
double min1, min2, max1, max2; double min1, min2, max1, max2;
vector<double> val1, val2; vector<double> val1, val2;
force.getFunctionParameters(i, name1, val1, min1, max1); dynamic_cast<Continuous1DFunction&>(force.getTabulatedFunction(i)).getFunctionParameters(val1, min1, max1);
force2.getFunctionParameters(i, name2, val2, min2, max2); dynamic_cast<Continuous1DFunction&>(force2.getTabulatedFunction(i)).getFunctionParameters(val2, min2, max2);
ASSERT_EQUAL(name1, name2); ASSERT_EQUAL(force.getTabulatedFunctionName(i), force2.getTabulatedFunctionName(i));
ASSERT_EQUAL(min1, min2); ASSERT_EQUAL(min1, min2);
ASSERT_EQUAL(max1, max2); ASSERT_EQUAL(max1, max2);
ASSERT_EQUAL(val1.size(), val2.size()); ASSERT_EQUAL(val1.size(), val2.size());
......
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2010-2014 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
#include "openmm/TabulatedFunction.h"
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testContinuous1DFunction() {
// Create a function.
double min = 0.5, max = 1.5;
vector<double> values(60);
for (int i = 0; i < (int) values.size(); i++)
values[i] = sin((double) i);
Continuous1DFunction function(values, min, max);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<Continuous1DFunction>(&function, "Function", buffer);
Continuous1DFunction* copy = XmlSerializer::deserialize<Continuous1DFunction>(buffer);
// Compare the two forces to see if they are identical.
double min2, max2;
vector<double> values2;
copy->getFunctionParameters(values2, min2, max2);
ASSERT_EQUAL(min, min2);
ASSERT_EQUAL(max, max2);
ASSERT_EQUAL(values.size(), values2.size());
for (int j = 0; j < (int) values.size(); j++)
ASSERT_EQUAL(values[j], values2[j]);
}
void testContinuous2DFunction() {
// Create a function.
int xsize = 5, ysize = 12;
double xmin = 0.5, xmax = 1.5, ymin = 0.1, ymax = 5.0;
vector<double> values(xsize*ysize);
for (int i = 0; i < (int) values.size(); i++)
values[i] = sin((double) i);
Continuous2DFunction function(xsize, ysize, values, xmin, xmax, ymin, ymax);
// 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() {
// Create a function.
int xsize = 5, ysize = 4, zsize = 3;
double xmin = 0.5, xmax = 1.5, ymin = 0.1, ymax = 5.0, zmin = 0.3, zmax = 0.9;
vector<double> values(xsize*ysize*zsize);
for (int i = 0; i < (int) values.size(); i++)
values[i] = sin((double) i);
Continuous3DFunction function(xsize, ysize, zsize, values, xmin, xmax, ymin, ymax, zmin, zmax);
// 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() {
// Create a function.
vector<double> values(60);
for (int i = 0; i < (int) values.size(); i++)
values[i] = sin((double) i);
Discrete1DFunction function(values);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<Discrete1DFunction>(&function, "Function", buffer);
Discrete1DFunction* copy = XmlSerializer::deserialize<Discrete1DFunction>(buffer);
// Compare the two forces to see if they are identical.
vector<double> values2;
copy->getFunctionParameters(values2);
ASSERT_EQUAL(values.size(), values2.size());
for (int j = 0; j < (int) values.size(); j++)
ASSERT_EQUAL(values[j], values2[j]);
}
void testDiscrete2DFunction() {
// Create a function.
int xsize = 5, ysize = 12;
vector<double> values(xsize*ysize);
for (int i = 0; i < (int) values.size(); i++)
values[i] = sin((double) i);
Discrete2DFunction function(xsize, ysize, values);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<Discrete2DFunction>(&function, "Function", buffer);
Discrete2DFunction* copy = XmlSerializer::deserialize<Discrete2DFunction>(buffer);
// Compare the two forces to see if they are identical.
int xsize2, ysize2;
vector<double> values2;
copy->getFunctionParameters(xsize2, ysize2, values2);
ASSERT_EQUAL(xsize, xsize2);
ASSERT_EQUAL(ysize, ysize2);
ASSERT_EQUAL(values.size(), values2.size());
for (int j = 0; j < (int) values.size(); j++)
ASSERT_EQUAL(values[j], values2[j]);
}
void testDiscrete3DFunction() {
// Create a function.
int xsize = 5, ysize = 4, zsize = 3;
vector<double> values(xsize*ysize*zsize);
for (int i = 0; i < (int) values.size(); i++)
values[i] = sin((double) i);
Discrete3DFunction function(xsize, ysize, zsize, values);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<Discrete3DFunction>(&function, "Function", buffer);
Discrete3DFunction* copy = XmlSerializer::deserialize<Discrete3DFunction>(buffer);
// Compare the two forces to see if they are identical.
int xsize2, ysize2, zsize2;
vector<double> values2;
copy->getFunctionParameters(xsize2, ysize2, zsize2, values2);
ASSERT_EQUAL(xsize, xsize2);
ASSERT_EQUAL(ysize, ysize2);
ASSERT_EQUAL(zsize, zsize2);
ASSERT_EQUAL(values.size(), values2.size());
for (int j = 0; j < (int) values.size(); j++)
ASSERT_EQUAL(values[j], values2[j]);
}
int main() {
try {
testContinuous1DFunction();
testContinuous2DFunction();
testContinuous3DFunction();
testDiscrete1DFunction();
testDiscrete2DFunction();
testDiscrete3DFunction();
}
catch(const exception& e) {
cout << "exception: " << e.what() << endl;
return 1;
}
cout << "Done" << endl;
return 0;
}
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