Commit 7f6c8bbc authored by Peter Eastman's avatar Peter Eastman
Browse files

Beginnings of serialization support

parent 2584685c
#ifndef OPENMM_HARMONICBONDFORCE_PROXY_H_
#define OPENMM_HARMONICBONDFORCE_PROXY_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) 2010 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 HarmonicBondForce objects.
*/
class OPENMM_EXPORT HarmonicBondForceProxy : public SerializationProxy {
public:
HarmonicBondForceProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_HARMONICBONDFORCE_PROXY_H_*/
#ifndef OPENMM_SERIALIZATIONNODE_H_
#define OPENMM_SERIALIZATIONNODE_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) 2010 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/SerializationProxy.h"
#include "openmm/OpenMMException.h"
#include "openmm/internal/windowsExport.h"
#include <map>
#include <string>
#include <vector>
namespace OpenMM {
/**
*/
class OPENMM_EXPORT SerializationNode {
public:
const std::string& getName() const;
void setName(const std::string& name);
const std::vector<SerializationNode>& getChildren() const;
std::vector<SerializationNode>& getChildren();
const SerializationNode& getChildNode(const std::string& name) const;
SerializationNode& getChildNode(const std::string& name);
const std::map<std::string, std::string>& getProperties() const;
bool hasProperty(const std::string& name) const;
const std::string& getStringProperty(const std::string& name) const;
const std::string& getStringProperty(const std::string& name, const std::string& defaultValue) const;
SerializationNode& setStringProperty(const std::string& name, const std::string& value);
int getIntProperty(const std::string& name) const;
int getIntProperty(const std::string& name, int defaultValue) const;
SerializationNode& setIntProperty(const std::string& name, int value);
double getDoubleProperty(const std::string& name) const;
double getDoubleProperty(const std::string& name, double defaultValue) const;
SerializationNode& setDoubleProperty(const std::string& name, double value);
SerializationNode& createChildNode(const std::string& name);
template <class T>
SerializationNode& createChildNode(const std::string& name, const T* object) {
const SerializationProxy& proxy = SerializationProxy::getProxy(typeid(*object));
SerializationNode& node = createChildNode(name);
proxy.serialize(object, node);
if (node.hasProperty("type"))
throw OpenMMException(proxy.getTypeName()+" created node with reserved property 'type'");
node.setStringProperty("type", proxy.getTypeName());
return node;
}
template<class T>
T* decodeObject() const {
return reinterpret_cast<T*>(SerializationProxy::getProxy(getStringProperty("type")).deserialize(*this));
}
private:
std::string name;
std::vector<SerializationNode> children;
std::map<std::string, std::string> properties;
};
} // namespace OpenMM
#endif /*OPENMM_SERIALIZATIONNODE_H_*/
#ifndef OPENMM_SERIALIZATIONPROXY_H_
#define OPENMM_SERIALIZATIONPROXY_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) 2010 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 <map>
#include <string>
#include <typeinfo>
namespace OpenMM {
class SerializationNode;
/**
*/
class OPENMM_EXPORT SerializationProxy {
public:
SerializationProxy(const std::string& typeName);
const std::string& getTypeName() const;
virtual void serialize(const void* object, SerializationNode& node) const = 0;
virtual void* deserialize(const SerializationNode& node) const = 0;
static void registerProxy(const std::type_info& type, const SerializationProxy* proxy);
static const SerializationProxy& getProxy(const std::string& typeName);
static const SerializationProxy& getProxy(const std::type_info& type);
private:
std::string typeName;
static std::map<const std::type_info*, const SerializationProxy*> proxiesByType;
static std::map<const std::string, const SerializationProxy*> proxiesByName;
};
} // namespace OpenMM
#endif /*OPENMM_SERIALIZATIONPROXY_H_*/
#ifndef OPENMM_SYSTEM_PROXY_H_
#define OPENMM_SYSTEM_PROXY_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) 2010 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 System objects.
*/
class OPENMM_EXPORT SystemProxy : public SerializationProxy {
public:
SystemProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_SYSTEM_PROXY_H_*/
#ifndef OPENMM_XML_SERIALIZER_H_
#define OPENMM_XML_SERIALIZER_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) 2010 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/SerializationNode.h"
#include "openmm/serialization/SerializationProxy.h"
#include "openmm/OpenMMException.h"
#include "openmm/internal/windowsExport.h"
#include <iosfwd>
class TiXmlElement;
namespace OpenMM {
/**
*/
class OPENMM_EXPORT XmlSerializer {
public:
template <class T>
static void serialize(const T* object, const std::string& rootName, std::ostream& stream) {
const SerializationProxy& proxy = SerializationProxy::getProxy(typeid(*object));
SerializationNode node;
node.setName(rootName);
proxy.serialize(object, node);
if (node.hasProperty("type"))
throw OpenMMException(proxy.getTypeName()+" created node with reserved property 'type'");
node.setStringProperty("type", proxy.getTypeName());
return serialize(node, stream);
}
template <class T>
static T* deserialize(std::istream& stream) {
return reinterpret_cast<T*>(deserializeStream(stream));
}
private:
static void serialize(const SerializationNode& node, std::ostream& stream);
static void* deserializeStream(std::istream& stream);
static TiXmlElement* encodeNode(const SerializationNode& node);
static SerializationNode* decodeNode(SerializationNode& node, const TiXmlElement& element);
};
} // namespace OpenMM
#endif /*OPENMM_XML_SERIALIZER_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) 2010 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/HarmonicBondForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/HarmonicBondForce.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
HarmonicBondForceProxy::HarmonicBondForceProxy() : SerializationProxy("HarmonicBondForce") {
}
void HarmonicBondForceProxy::serialize(const void* object, SerializationNode& node) const {
const HarmonicBondForce& force = *reinterpret_cast<const HarmonicBondForce*>(object);
SerializationNode& bonds = node.createChildNode("Bonds");
for (int i = 0; i < force.getNumBonds(); i++) {
int particle1, particle2;
double distance, k;
force.getBondParameters(i, particle1, particle2, distance, k);
bonds.createChildNode("Bond").setIntProperty("p1", particle1).setIntProperty("p2", particle2).setDoubleProperty("d", distance).setDoubleProperty("k", k);
}
}
void* HarmonicBondForceProxy::deserialize(const SerializationNode& node) const {
return new HarmonicBondForce();
}
/* -------------------------------------------------------------------------- *
* 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 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/SerializationNode.h"
#include "openmm/OpenMMException.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
const string& SerializationNode::getName() const {
return name;
}
void SerializationNode::setName(const string& name) {
this->name = name;
}
const vector<SerializationNode>& SerializationNode::getChildren() const {
return children;
}
vector<SerializationNode>& SerializationNode::getChildren() {
return children;
}
const SerializationNode& SerializationNode::getChildNode(const std::string& name) const {
for (int i = 0; i < (int) children.size(); i++)
if (children[i].name == name)
return children[i];
throw OpenMMException("Unknown child '"+name+"' for node '"+getName()+"'");
}
SerializationNode& SerializationNode::getChildNode(const std::string& name) {
for (int i = 0; i < (int) children.size(); i++)
if (children[i].name == name)
return children[i];
throw OpenMMException("Unknown child '"+name+"' for node '"+getName()+"'");
}
const map<string, string>& SerializationNode::getProperties() const {
return properties;
}
bool SerializationNode::hasProperty(const string& name) const {
return (properties.find(name) != properties.end());
}
const string& SerializationNode::getStringProperty(const string& name) const {
map<string, string>::const_iterator iter = properties.find(name);
if (iter == properties.end())
throw OpenMMException("Unknown property '"+name+"' in node '"+getName()+"'");
return iter->second;
}
const string& SerializationNode::getStringProperty(const string& name, const string& defaultValue) const {
map<string, string>::const_iterator iter = properties.find(name);
if (iter == properties.end())
return defaultValue;
return iter->second;
}
SerializationNode& SerializationNode::setStringProperty(const string& name, const string& value) {
properties[name] = value;
return *this;
}
int SerializationNode::getIntProperty(const string& name) const {
map<string, string>::const_iterator iter = properties.find(name);
if (iter == properties.end())
throw OpenMMException("Unknown property '"+name+"' in node '"+getName()+"'");
int value;
stringstream(iter->second) >> value;
return value;
}
int SerializationNode::getIntProperty(const string& name, int defaultValue) const {
map<string, string>::const_iterator iter = properties.find(name);
if (iter == properties.end())
return defaultValue;
int value;
stringstream(iter->second) >> value;
return value;
}
SerializationNode& SerializationNode::setIntProperty(const string& name, int value) {
stringstream s;
s << value;
properties[name] = s.str();
return *this;
}
double SerializationNode::getDoubleProperty(const string& name) const {
map<string, string>::const_iterator iter = properties.find(name);
if (iter == properties.end())
throw OpenMMException("Unknown property '"+name+"' in node '"+getName()+"'");
double value;
stringstream(iter->second) >> value;
return value;
}
double SerializationNode::getDoubleProperty(const string& name, double defaultValue) const {
map<string, string>::const_iterator iter = properties.find(name);
if (iter == properties.end())
return defaultValue;
double value;
stringstream(iter->second) >> value;
return value;
}
SerializationNode& SerializationNode::setDoubleProperty(const string& name, double value) {
stringstream s;
s.precision(16);
s << value;
properties[name] = s.str();
return *this;
}
SerializationNode& SerializationNode::createChildNode(const std::string& name) {
children.push_back(SerializationNode());
children.back().setName(name);
return children.back();
}
/* -------------------------------------------------------------------------- *
* 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 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/SerializationProxy.h"
#include "openmm/OpenMMException.h"
#include <typeinfo>
using namespace OpenMM;
using namespace std;
map<const type_info*, const SerializationProxy*> SerializationProxy::proxiesByType;
map<const string, const SerializationProxy*> SerializationProxy::proxiesByName;
SerializationProxy::SerializationProxy(const string& typeName) : typeName(typeName) {
}
const string& SerializationProxy::getTypeName() const {
return typeName;
}
void SerializationProxy::registerProxy(const type_info& type, const SerializationProxy* proxy) {
proxiesByType[&type] = proxy;
proxiesByName[proxy->getTypeName()] = proxy;
}
const SerializationProxy& SerializationProxy::getProxy(const string& typeName) {
map<string, const SerializationProxy*>::const_iterator iter = proxiesByName.find(typeName);
if (iter == proxiesByName.end())
throw OpenMMException("There is no serialization proxy registered for type '"+string(typeName)+"'");
return *iter->second;
}
const SerializationProxy& SerializationProxy::getProxy(const type_info& type) {
map<const type_info*, const SerializationProxy*>::const_iterator iter = proxiesByType.find(&type);
if (iter == proxiesByType.end())
throw OpenMMException("There is no serialization proxy registered for type "+string(type.name()));
return *iter->second;
}
/* -------------------------------------------------------------------------- *
* 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 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/SystemProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/System.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
SystemProxy::SystemProxy() : SerializationProxy("System") {
}
void SystemProxy::serialize(const void* object, SerializationNode& node) const {
const System& system = *reinterpret_cast<const System*>(object);
Vec3 a, b, c;
system.getDefaultPeriodicBoxVectors(a, b, c);
SerializationNode& box = node.createChildNode("PeriodicBoxVectors");
box.createChildNode("A").setDoubleProperty("x", a[0]).setDoubleProperty("y", a[1]).setDoubleProperty("z", a[2]);
box.createChildNode("B").setDoubleProperty("x", b[0]).setDoubleProperty("y", b[1]).setDoubleProperty("z", b[2]);
box.createChildNode("C").setDoubleProperty("x", c[0]).setDoubleProperty("y", c[1]).setDoubleProperty("z", c[2]);
SerializationNode& particles = node.createChildNode("Particles");
for (int i = 0; i < system.getNumParticles(); i++)
particles.createChildNode("Particle").setDoubleProperty("mass", system.getParticleMass(i));
SerializationNode& constraints = node.createChildNode("Constraints");
for (int i = 0; i < system.getNumConstraints(); i++) {
int particle1, particle2;
double distance;
system.getConstraintParameters(i, particle1, particle2, distance);
constraints.createChildNode("Constraint").setIntProperty("p1", particle1).setIntProperty("p2", particle2).setDoubleProperty("d", distance);
}
SerializationNode& forces = node.createChildNode("Forces");
for (int i = 0; i < system.getNumForces(); i++)
forces.createChildNode("Force", &system.getForce(i));
}
void* SystemProxy::deserialize(const SerializationNode& node) const {
System* system = new System();
try {
const SerializationNode& box = node.getChildNode("PeriodicBoxVectors");
const SerializationNode& boxa = box.getChildNode("A");
const SerializationNode& boxb = box.getChildNode("B");
const SerializationNode& boxc = box.getChildNode("C");
Vec3 a(boxa.getDoubleProperty("x"), boxa.getDoubleProperty("y"), boxa.getDoubleProperty("z"));
Vec3 b(boxb.getDoubleProperty("x"), boxb.getDoubleProperty("y"), boxb.getDoubleProperty("z"));
Vec3 c(boxc.getDoubleProperty("x"), boxc.getDoubleProperty("y"), boxc.getDoubleProperty("z"));
system->setDefaultPeriodicBoxVectors(a, b, c);
const SerializationNode& particles = node.getChildNode("Particles");
for (int i = 0; i < (int) particles.getChildren().size(); i++)
system->addParticle(particles.getChildren()[i].getDoubleProperty("mass"));
const SerializationNode& constraints = node.getChildNode("Constraints");
for (int i = 0; i < (int) constraints.getChildren().size(); i++) {
const SerializationNode& constraint = constraints.getChildren()[i];
system->addConstraint(constraint.getDoubleProperty("p1"), constraint.getDoubleProperty("p2"), constraint.getDoubleProperty("d"));
}
const SerializationNode& forces = node.getChildNode("Forces");
for (int i = 0; i < (int) forces.getChildren().size(); i++) {
system->addForce(forces.getChildren()[i].decodeObject<Force>());
}
}
catch (...) {
delete system;
throw;
}
return system;
}
\ No newline at end of file
/* -------------------------------------------------------------------------- *
* 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 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/XmlSerializer.h"
#include "tinyxml.h"
using namespace OpenMM;
using namespace std;
void XmlSerializer::serialize(const SerializationNode& node, std::ostream& stream) {
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild(decl);
doc.LinkEndChild(encodeNode(node));
TiXmlPrinter printer;
printer.SetIndent("\t");
doc.Accept(&printer);
stream << printer.Str();
}
TiXmlElement* XmlSerializer::encodeNode(const SerializationNode& node) {
TiXmlElement* element = new TiXmlElement(node.getName());
const map<string, string>& properties = node.getProperties();
for (map<string, string>::const_iterator iter = properties.begin(); iter != properties.end(); ++iter)
element->SetAttribute(iter->first.c_str(), iter->second.c_str());
const vector<SerializationNode>& children = node.getChildren();
for (int i = 0; i < (int) children.size(); i++)
element->LinkEndChild(encodeNode(children[i]));
return element;
}
void* XmlSerializer::deserializeStream(std::istream& stream) {
TiXmlDocument doc;
stream >> doc;
SerializationNode root;
decodeNode(root, *doc.FirstChildElement());
const SerializationProxy& proxy = SerializationProxy::getProxy(root.getStringProperty("type"));
return proxy.deserialize(root);
}
SerializationNode* XmlSerializer::decodeNode(SerializationNode& node, const TiXmlElement& element) {
for (const TiXmlAttribute* attribute = element.FirstAttribute(); attribute != NULL; attribute = attribute->Next())
node.setStringProperty(attribute->NameTStr(), attribute->ValueStr());
for (const TiXmlElement* child = element.FirstChildElement(); child != NULL; child = child->NextSiblingElement()) {
SerializationNode& childNode = node.createChildNode(child->ValueTStr());
decodeNode(childNode, *child);
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
/*
www.sourceforge.net/projects/tinyxml
Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "tinyxml.h"
// The goal of the seperate error file is to make the first
// step towards localization. tinyxml (currently) only supports
// english error messages, but the could now be translated.
//
// It also cleans up the code a bit.
//
const char* TiXmlBase::errorString[ TIXML_ERROR_STRING_COUNT ] =
{
"No error",
"Error",
"Failed to open file",
"Error parsing Element.",
"Failed to read Element name",
"Error reading Element value.",
"Error reading Attributes.",
"Error: empty tag.",
"Error reading end tag.",
"Error parsing Unknown.",
"Error parsing Comment.",
"Error parsing Declaration.",
"Error document empty.",
"Error null (0) or unexpected EOF found in input stream.",
"Error parsing CDATA.",
"Error when TiXmlDocument added to document, because TiXmlDocument can only be at the root.",
};
This diff is collapsed.
#
# Testing
#
ENABLE_TESTING()
# Automatically create tests using files named "Test*.cpp"
FILE(GLOB TEST_PROGS "*Test*.cpp")
FOREACH(TEST_PROG ${TEST_PROGS})
GET_FILENAME_COMPONENT(TEST_ROOT ${TEST_PROG} NAME_WE)
# All tests use shared libraries
ADD_EXECUTABLE(${TEST_ROOT} ${TEST_PROG})
TARGET_LINK_LIBRARIES(${TEST_ROOT} ${OPENMM_S11N_LIBRARY_NAME})
ADD_TEST(${TEST_ROOT} ${EXECUTABLE_OUTPUT_PATH}/${TEST_ROOT})
ENDFOREACH(TEST_PROG ${TEST_PROGS})
/* -------------------------------------------------------------------------- *
* 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 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 "../../../tests/AssertionUtilities.h"
#include "openmm/serialization/SerializationNode.h"
#include <iostream>
using namespace OpenMM;
using namespace std;
void testProperties() {
SerializationNode node;
ASSERT_EQUAL(false, node.hasProperty("prop1"));
ASSERT_EQUAL(false, node.hasProperty("prop2"));
bool exists = false;
try {
node.getStringProperty("prop1");
exists = true;
}
catch (const exception& ex) {
}
ASSERT_EQUAL(false, exists);
try {
node.getIntProperty("prop1");
exists = true;
}
catch (const exception& ex) {
}
ASSERT_EQUAL(false, exists);
try {
node.getDoubleProperty("prop1");
exists = true;
}
catch (const exception& ex) {
}
ASSERT_EQUAL(false, exists);
ASSERT_EQUAL(3, node.getIntProperty("prop1", 3));
ASSERT_EQUAL(3.5, node.getDoubleProperty("prop1", 3.5));
ASSERT_EQUAL("abc", node.getStringProperty("prop1", "abc"));
node.setIntProperty("prop1", 1);
ASSERT_EQUAL(1, node.getIntProperty("prop1"));
node.setDoubleProperty("prop1", 1.5);
ASSERT_EQUAL(1.5, node.getDoubleProperty("prop1"));
node.setStringProperty("prop1", "hello");
ASSERT_EQUAL("hello", node.getStringProperty("prop1"));
ASSERT_EQUAL(true, node.hasProperty("prop1"));
ASSERT_EQUAL(false, node.hasProperty("prop2"));
}
int main() {
try {
testProperties();
}
catch(const exception& e) {
cout << "exception: " << e.what() << endl;
return 1;
}
cout << "Done" << endl;
return 0;
}
/* -------------------------------------------------------------------------- *
* 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 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 "../../../tests/AssertionUtilities.h"
#include "openmm/HarmonicBondForce.h"
#include "openmm/System.h"
#include "openmm/serialization/HarmonicBondForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/serialization/SerializationProxy.h"
#include "openmm/serialization/SystemProxy.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testSerialization() {
// Create a System.
System system;
for (int i = 0; i < 5; i++)
system.addParticle(0.1*i+1);
system.addConstraint(0, 1, 3.0);
system.addConstraint(1, 2, 2.5);
system.addConstraint(4, 1, 1.001);
system.setDefaultPeriodicBoxVectors(Vec3(5, 0, 0), Vec3(0, 4, 0), Vec3(0, 0, 1.5));
system.addForce(new HarmonicBondForce());
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<System>(&system, "System", buffer);
System* copy = XmlSerializer::deserialize<System>(buffer);
// Compare the two systems to see if they are identical.
System& system2 = *copy;
ASSERT_EQUAL(system.getNumParticles(), system2.getNumParticles());
for (int i = 0; i < system.getNumParticles(); i++)
ASSERT_EQUAL(system.getParticleMass(i), system2.getParticleMass(i));
ASSERT_EQUAL(system.getNumConstraints(), system2.getNumConstraints());
for (int i = 0; i < system.getNumConstraints(); i++) {
int p1, p2, p3, p4;
double d1, d2;
system.getConstraintParameters(i, p1, p2, d1);
system2.getConstraintParameters(i, p3, p4, d2);
ASSERT_EQUAL(p1, p3);
ASSERT_EQUAL(p2, p4);
ASSERT_EQUAL(d1, d2);
}
Vec3 a, b, c;
Vec3 a2, b2, c2;
system.getDefaultPeriodicBoxVectors(a, b, c);
system2.getDefaultPeriodicBoxVectors(a2, b2, c2);
ASSERT_EQUAL_VEC(a, a2, 0);
ASSERT_EQUAL_VEC(b, b2, 0);
ASSERT_EQUAL_VEC(c, c2, 0);
ASSERT_EQUAL(system.getNumForces(), system2.getNumForces());
for (int i = 0; i < system.getNumForces(); i++)
ASSERT(typeid(system.getForce(i)) == typeid(system2.getForce(i)))
}
int main() {
try {
SerializationProxy::registerProxy(typeid(System), new SystemProxy());
SerializationProxy::registerProxy(typeid(HarmonicBondForce), new HarmonicBondForceProxy());
testSerialization();
}
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