Commit 6d91d79d authored by Peter Eastman's avatar Peter Eastman
Browse files

Continuing to implement serialization

parent 3cae6a22
...@@ -79,5 +79,8 @@ ADD_LIBRARY(${OPENMM_SERIALIZATION_LIBRARY_NAME} SHARED ${SOURCE_FILES} ${SOURCE ...@@ -79,5 +79,8 @@ ADD_LIBRARY(${OPENMM_SERIALIZATION_LIBRARY_NAME} SHARED ${SOURCE_FILES} ${SOURCE
TARGET_LINK_LIBRARIES(${OPENMM_SERIALIZATION_LIBRARY_NAME} debug ${OPENMM_LIBRARY_NAME}_d optimized ${OPENMM_LIBRARY_NAME}) TARGET_LINK_LIBRARIES(${OPENMM_SERIALIZATION_LIBRARY_NAME} debug ${OPENMM_LIBRARY_NAME}_d optimized ${OPENMM_LIBRARY_NAME})
SET_TARGET_PROPERTIES(${OPENMM_SERIALIZATION_LIBRARY_NAME} PROPERTIES COMPILE_FLAGS "-DOPENMM_BUILDING_SHARED_LIBRARY -DTIXML_USE_STL -DIEEE_8087") SET_TARGET_PROPERTIES(${OPENMM_SERIALIZATION_LIBRARY_NAME} PROPERTIES COMPILE_FLAGS "-DOPENMM_BUILDING_SHARED_LIBRARY -DTIXML_USE_STL -DIEEE_8087")
INSTALL_TARGETS(/lib RUNTIME_DIRECTORY /lib ${OPENMM_SERIALIZATION_LIBRARY_NAME}) INSTALL_TARGETS(/lib RUNTIME_DIRECTORY /lib ${OPENMM_SERIALIZATION_LIBRARY_NAME})
INSTALL_FILES(/include/openmm/serialization FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/openmm/serialization/SerializationNode.h)
INSTALL_FILES(/include/openmm/serialization FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/openmm/serialization/SerializationProxy.h)
INSTALL_FILES(/include/openmm/serialization FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/openmm/serialization/XmlSerializer.h)
ADD_SUBDIRECTORY(tests) ADD_SUBDIRECTORY(tests)
#ifndef OPENMM_CUSTOMANGLEFORCE_PROXY_H_
#define OPENMM_CUSTOMANGLEFORCE_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 CustomAngleForce objects.
*/
class OPENMM_EXPORT CustomAngleForceProxy : public SerializationProxy {
public:
CustomAngleForceProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_CUSTOMANGLEFORCE_PROXY_H_*/
#ifndef OPENMM_CUSTOMBONDFORCE_PROXY_H_
#define OPENMM_CUSTOMBONDFORCE_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 CustomBondForce objects.
*/
class OPENMM_EXPORT CustomBondForceProxy : public SerializationProxy {
public:
CustomBondForceProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_CUSTOMBONDFORCE_PROXY_H_*/
#ifndef OPENMM_CUSTOMEXTERNALFORCE_PROXY_H_
#define OPENMM_CUSTOMEXTERNALFORCE_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 CustomExternalForce objects.
*/
class OPENMM_EXPORT CustomExternalForceProxy : public SerializationProxy {
public:
CustomExternalForceProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_CUSTOMEXTERNALFORCE_PROXY_H_*/
#ifndef OPENMM_CUSTOMTORSIONFORCE_PROXY_H_
#define OPENMM_CUSTOMTORSIONFORCE_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 CustomTorsionForce objects.
*/
class OPENMM_EXPORT CustomTorsionForceProxy : public SerializationProxy {
public:
CustomTorsionForceProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_CUSTOMTORSIONFORCE_PROXY_H_*/
...@@ -42,28 +42,158 @@ ...@@ -42,28 +42,158 @@
namespace OpenMM { namespace OpenMM {
/** /**
* A SerializationNode stores information about an object during serialization or deserialization.
*
* When an object is serialized, its SerializationProxy is first called to copy information about the
* object into a SerializationNode. That information can then be written to the output stream in the
* desired format.
*
* When an object is deserialized, the input stream is read and the information is stored into a
* SerializationNode. The appropriate SerializationProxy is then called to reconstruct the object.
*
* SerializationNodes are arranged in a tree. There will often be a one-to-one correspondence between
* objects and SerializationNodes, but that need not always be true. A proxy is free to create whatever
* child nodes it wants and store information in them using whatever organization is most convenient.
*
* Each SerializationNode can store an arbitrary set of "properties", represented as key-value pairs.
* The key is always a string, while the value may be a string, an int, or a double. If a value is specified
* using one data type and then accessed as a different data type, the node will attempt to convert the value
* in an appropriate way. For example, it is always reasonable to call getStringProperty() to access a
* property as a string. Similarly, you can use setStringProperty() to specify a property and then access it
* using getIntProperty(). This will produce the expected result if the original value was, in fact, the
* string representation of an int, but if the original string was non-numeric, the result is undefined.
*/ */
class OPENMM_EXPORT SerializationNode { class OPENMM_EXPORT SerializationNode {
public: public:
/**
* Get the name of this SerializationNode.
*/
const std::string& getName() const; const std::string& getName() const;
/**
* Set the name of this SerializationNode.
*
* @param name the new name of the SerializationNode
*/
void setName(const std::string& name); void setName(const std::string& name);
/**
* Get a reference to this node's child nodes.
*/
const std::vector<SerializationNode>& getChildren() const; const std::vector<SerializationNode>& getChildren() const;
/**
* Get a reference to this node's child nodes.
*/
std::vector<SerializationNode>& getChildren(); std::vector<SerializationNode>& getChildren();
/**
* Get a reference to the child node with a particular name. If there is no child
* with the specified name, this throws an exception.
*
* @param the name of the child node to get
*/
const SerializationNode& getChildNode(const std::string& name) const; const SerializationNode& getChildNode(const std::string& name) const;
/**
* Get a reference to the child node with a particular name. If there is no child
* with the specified name, this throws an exception.
*
* @param the name of the child node to get
*/
SerializationNode& getChildNode(const std::string& name); SerializationNode& getChildNode(const std::string& name);
/**
* Get a map containing all of this node's properties.
*/
const std::map<std::string, std::string>& getProperties() const; const std::map<std::string, std::string>& getProperties() const;
/**
* Determine whether this node has a property with a particular node.
*
* @param the name of the property to check for
*/
bool hasProperty(const std::string& name) const; bool hasProperty(const std::string& name) const;
/**
* Get the property with a particular name, specified as a string. If there is no property with
* the specified name, an exception is thrown.
*
* @param name the name of the property to get
*/
const std::string& getStringProperty(const std::string& name) const; const std::string& getStringProperty(const std::string& name) const;
/**
* Get the property with a particular name, specified as a string. If there is no property with
* the specified name, a default value is returned instead.
*
* @param name the name of the property to get
* @param defaultValue the value to return if the specified property does not exist
*/
const std::string& getStringProperty(const std::string& name, const std::string& defaultValue) const; const std::string& getStringProperty(const std::string& name, const std::string& defaultValue) const;
/**
* Set the value of a property, specified as a string.
*
* @param name the name of the property to set
* @param value the value to set for the property
*/
SerializationNode& setStringProperty(const std::string& name, const std::string& value); SerializationNode& setStringProperty(const std::string& name, const std::string& value);
/**
* Get the property with a particular name, specified as an int. If there is no property with
* the specified name, an exception is thrown.
*
* @param name the name of the property to get
*/
int getIntProperty(const std::string& name) const; int getIntProperty(const std::string& name) const;
/**
* Get the property with a particular name, specified as an int. If there is no property with
* the specified name, a default value is returned instead.
*
* @param name the name of the property to get
* @param defaultValue the value to return if the specified property does not exist
*/
int getIntProperty(const std::string& name, int defaultValue) const; int getIntProperty(const std::string& name, int defaultValue) const;
/**
* Set the value of a property, specified as an int.
*
* @param name the name of the property to set
* @param value the value to set for the property
*/
SerializationNode& setIntProperty(const std::string& name, int value); SerializationNode& setIntProperty(const std::string& name, int value);
/**
* Get the property with a particular name, specified as a double. If there is no property with
* the specified name, an exception is thrown.
*
* @param name the name of the property to get
*/
double getDoubleProperty(const std::string& name) const; double getDoubleProperty(const std::string& name) const;
/**
* Get the property with a particular name, specified as a double. If there is no property with
* the specified name, a default value is returned instead.
*
* @param name the name of the property to get
* @param defaultValue the value to return if the specified property does not exist
*/
double getDoubleProperty(const std::string& name, double defaultValue) const; double getDoubleProperty(const std::string& name, double defaultValue) const;
/**
* Set the value of a property, specified as a double.
*
* @param name the name of the property to set
* @param value the value to set for the property
*/
SerializationNode& setDoubleProperty(const std::string& name, double value); SerializationNode& setDoubleProperty(const std::string& name, double value);
/**
* Create a new child node
*
* @param name the name of the new node to create
* @return a reference to the newly created node
*/
SerializationNode& createChildNode(const std::string& name); SerializationNode& createChildNode(const std::string& name);
/**
* Create a new child node by serializing an object. A SerializationProxy is automatically
* selected based on the object's type, then invoked to populate the newly created node.
*
* Note that, while this method is templatized based on the type of object being serialized,
* the typeid() operator is used to select the proxy. This means the template argument may
* be a base class, and the correct proxies will still be selected for objects of different
* subclasses.
*
* @param name the name of the new node to create
* @param object a pointer to the object to serialize
* @return a reference to the newly created node
*/
template <class T> template <class T>
SerializationNode& createChildNode(const std::string& name, const T* object) { SerializationNode& createChildNode(const std::string& name, const T* object) {
const SerializationProxy& proxy = SerializationProxy::getProxy(typeid(*object)); const SerializationProxy& proxy = SerializationProxy::getProxy(typeid(*object));
...@@ -74,6 +204,16 @@ public: ...@@ -74,6 +204,16 @@ public:
node.setStringProperty("type", proxy.getTypeName()); node.setStringProperty("type", proxy.getTypeName());
return node; return node;
} }
/**
* Reconstruct an object based on the information stored in this node. A SerializationProxy is
* automatically selected based on the information stored in the node, then it is invoked to
* create the object.
*
* The template parameter may be either the actual type of the object, or any base class to which
* it may be cast.
*
* @return a pointer to the newly created object. The caller assumes ownership of the object.
*/
template<class T> template<class T>
T* decodeObject() const { T* decodeObject() const {
return reinterpret_cast<T*>(SerializationProxy::getProxy(getStringProperty("type")).deserialize(*this)); return reinterpret_cast<T*>(SerializationProxy::getProxy(getStringProperty("type")).deserialize(*this));
......
...@@ -42,16 +42,64 @@ namespace OpenMM { ...@@ -42,16 +42,64 @@ namespace OpenMM {
class SerializationNode; class SerializationNode;
/** /**
* A SerializationProxy is an object that knows how to serialize and deserialize objects of a
* particular type. This is an abstract class. Subclasses implement the logic for serializing
* particular types of logic.
*
* A global registry maintains the list of what SerializationProxy to use for each type of
* object. Call registerProxy() to register the proxy for a particular type. This is typically
* done at application startup or by a dynamic library's initialization code.
*/ */
class OPENMM_EXPORT SerializationProxy { class OPENMM_EXPORT SerializationProxy {
public: public:
/**
* Create a new SerializationProxy.
*
* @param typeName the name of the object type this proxy knows how to serialize. This
* name is stored in the output stream during serialization, and is used
* to select a proxy during deserialization. This typically is the class
* name, although that is not a requirement.
*/
SerializationProxy(const std::string& typeName); SerializationProxy(const std::string& typeName);
/**
* Get the name of the object type this proxy manipulates, as passed to the constructor.
*/
const std::string& getTypeName() const; const std::string& getTypeName() const;
/**
* Subclasses implement this method to record information about an object being serialized.
*
* @param object a pointer to the object being serialized
* @param node all data to be serialized should be stored into this node, either directly
* as properties or indirectly by adding child nodes to it
*/
virtual void serialize(const void* object, SerializationNode& node) const = 0; virtual void serialize(const void* object, SerializationNode& node) const = 0;
/**
* Reconstruct an object from its serialized data.
*
* @param node a SerializationNode containing the object's description
* @return a pointer to a new object created from the data. The caller assumes ownership
* of the object.
*/
virtual void* deserialize(const SerializationNode& node) const = 0; virtual void* deserialize(const SerializationNode& node) const = 0;
/**
* Register a SerializationProxy to be used for objects of a particular type.
*
* @param type the type_info for the object type
* @param proxy the proxy to use for objects of the specified type
*/
static void registerProxy(const std::type_info& type, const SerializationProxy* proxy); static void registerProxy(const std::type_info& type, const SerializationProxy* proxy);
/**
* Get the SerializationProxy to use for objects of a particular type, specified by name.
*
* @param typeName the name of the object type to get a proxy for
*/
static const SerializationProxy& getProxy(const std::string& typeName); static const SerializationProxy& getProxy(const std::string& typeName);
/**
* Get the SerializationProxy to use for objects of a particular type, specified by type_info.
*
* @param type the type_info of the object type to get a proxy for
*/
static const SerializationProxy& getProxy(const std::type_info& type); static const SerializationProxy& getProxy(const std::type_info& type);
private: private:
std::string typeName; std::string typeName;
......
...@@ -43,10 +43,18 @@ class TiXmlElement; ...@@ -43,10 +43,18 @@ class TiXmlElement;
namespace OpenMM { namespace OpenMM {
/** /**
* XmlSerializer is used for serializing objects as XML, and for reconstructing them again.
*/ */
class OPENMM_EXPORT XmlSerializer { class OPENMM_EXPORT XmlSerializer {
public: public:
/**
* Serialize an object as XML.
*
* @param object the object to serialize
* @param rootName the name to use for the root node of the XML document
* @param stream an output stream to write the XML to
*/
template <class T> template <class T>
static void serialize(const T* object, const std::string& rootName, std::ostream& stream) { static void serialize(const T* object, const std::string& rootName, std::ostream& stream) {
const SerializationProxy& proxy = SerializationProxy::getProxy(typeid(*object)); const SerializationProxy& proxy = SerializationProxy::getProxy(typeid(*object));
...@@ -56,8 +64,14 @@ public: ...@@ -56,8 +64,14 @@ public:
if (node.hasProperty("type")) if (node.hasProperty("type"))
throw OpenMMException(proxy.getTypeName()+" created node with reserved property 'type'"); throw OpenMMException(proxy.getTypeName()+" created node with reserved property 'type'");
node.setStringProperty("type", proxy.getTypeName()); node.setStringProperty("type", proxy.getTypeName());
return serialize(node, stream); serialize(node, stream);
} }
/**
* Reconstruct an object that has been serialized as XML.
*
* @param stream an input stream to read the XML from
* @return a pointer to the newly created object. The caller assumes ownership of the object.
*/
template <class T> template <class T>
static T* deserialize(std::istream& stream) { static T* deserialize(std::istream& stream) {
return reinterpret_cast<T*>(deserializeStream(stream)); return reinterpret_cast<T*>(deserializeStream(stream));
......
/* -------------------------------------------------------------------------- *
* 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/CustomAngleForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomAngleForce.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
CustomAngleForceProxy::CustomAngleForceProxy() : SerializationProxy("CustomAngleForce") {
}
void CustomAngleForceProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const CustomAngleForce& force = *reinterpret_cast<const CustomAngleForce*>(object);
node.setStringProperty("energy", force.getEnergyFunction());
SerializationNode& perAngleParams = node.createChildNode("PerAngleParameters");
for (int i = 0; i < force.getNumPerAngleParameters(); i++) {
perAngleParams.createChildNode("Parameter").setStringProperty("name", force.getPerAngleParameterName(i));
}
SerializationNode& globalParams = node.createChildNode("GlobalParameters");
for (int i = 0; i < force.getNumGlobalParameters(); i++) {
globalParams.createChildNode("Parameter").setStringProperty("name", force.getGlobalParameterName(i)).setDoubleProperty("default", force.getGlobalParameterDefaultValue(i));
}
SerializationNode& angles = node.createChildNode("Angles");
for (int i = 0; i < force.getNumAngles(); i++) {
int p1, p2, p3;
vector<double> params;
force.getAngleParameters(i, p1, p2, p3, params);
SerializationNode& node = angles.createChildNode("Angle").setIntProperty("p1", p1).setIntProperty("p2", p2).setIntProperty("p3", p3);
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
node.setDoubleProperty(key.str(), params[j]);
}
}
}
void* CustomAngleForceProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
CustomAngleForce* force = NULL;
try {
CustomAngleForce* force = new CustomAngleForce(node.getStringProperty("energy"));
const SerializationNode& perAngleParams = node.getChildNode("PerAngleParameters");
for (int i = 0; i < (int) perAngleParams.getChildren().size(); i++) {
const SerializationNode& parameter = perAngleParams.getChildren()[i];
force->addPerAngleParameter(parameter.getStringProperty("name"));
}
const SerializationNode& globalParams = node.getChildNode("GlobalParameters");
for (int i = 0; i < (int) globalParams.getChildren().size(); i++) {
const SerializationNode& parameter = globalParams.getChildren()[i];
force->addGlobalParameter(parameter.getStringProperty("name"), parameter.getDoubleProperty("default"));
}
const SerializationNode& angles = node.getChildNode("Angles");
vector<double> params(force->getNumPerAngleParameters());
for (int i = 0; i < (int) angles.getChildren().size(); i++) {
const SerializationNode& angle = angles.getChildren()[i];
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
params[j] = angle.getDoubleProperty(key.str());
}
force->addAngle(angle.getIntProperty("p1"), angle.getIntProperty("p2"), angle.getIntProperty("p3"), params);
}
return force;
}
catch (...) {
if (force != NULL)
delete force;
throw;
}
}
/* -------------------------------------------------------------------------- *
* 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/CustomBondForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomBondForce.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
CustomBondForceProxy::CustomBondForceProxy() : SerializationProxy("CustomBondForce") {
}
void CustomBondForceProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const CustomBondForce& force = *reinterpret_cast<const CustomBondForce*>(object);
node.setStringProperty("energy", force.getEnergyFunction());
SerializationNode& perBondParams = node.createChildNode("PerBondParameters");
for (int i = 0; i < force.getNumPerBondParameters(); i++) {
perBondParams.createChildNode("Parameter").setStringProperty("name", force.getPerBondParameterName(i));
}
SerializationNode& globalParams = node.createChildNode("GlobalParameters");
for (int i = 0; i < force.getNumGlobalParameters(); i++) {
globalParams.createChildNode("Parameter").setStringProperty("name", force.getGlobalParameterName(i)).setDoubleProperty("default", force.getGlobalParameterDefaultValue(i));
}
SerializationNode& bonds = node.createChildNode("Bonds");
for (int i = 0; i < force.getNumBonds(); i++) {
int p1, p2;
vector<double> params;
force.getBondParameters(i, p1, p2, params);
SerializationNode& node = bonds.createChildNode("Bond").setIntProperty("p1", p1).setIntProperty("p2", p2);
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
node.setDoubleProperty(key.str(), params[j]);
}
}
}
void* CustomBondForceProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
CustomBondForce* force = NULL;
try {
CustomBondForce* force = new CustomBondForce(node.getStringProperty("energy"));
const SerializationNode& perBondParams = node.getChildNode("PerBondParameters");
for (int i = 0; i < (int) perBondParams.getChildren().size(); i++) {
const SerializationNode& parameter = perBondParams.getChildren()[i];
force->addPerBondParameter(parameter.getStringProperty("name"));
}
const SerializationNode& globalParams = node.getChildNode("GlobalParameters");
for (int i = 0; i < (int) globalParams.getChildren().size(); i++) {
const SerializationNode& parameter = globalParams.getChildren()[i];
force->addGlobalParameter(parameter.getStringProperty("name"), parameter.getDoubleProperty("default"));
}
const SerializationNode& bonds = node.getChildNode("Bonds");
vector<double> params(force->getNumPerBondParameters());
for (int i = 0; i < (int) bonds.getChildren().size(); i++) {
const SerializationNode& bond = bonds.getChildren()[i];
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
params[j] = bond.getDoubleProperty(key.str());
}
force->addBond(bond.getIntProperty("p1"), bond.getIntProperty("p2"), params);
}
return force;
}
catch (...) {
if (force != NULL)
delete force;
throw;
}
}
/* -------------------------------------------------------------------------- *
* 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/CustomExternalForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomExternalForce.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
CustomExternalForceProxy::CustomExternalForceProxy() : SerializationProxy("CustomExternalForce") {
}
void CustomExternalForceProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const CustomExternalForce& force = *reinterpret_cast<const CustomExternalForce*>(object);
node.setStringProperty("energy", force.getEnergyFunction());
SerializationNode& perParticleParams = node.createChildNode("PerParticleParameters");
for (int i = 0; i < force.getNumPerParticleParameters(); i++) {
perParticleParams.createChildNode("Parameter").setStringProperty("name", force.getPerParticleParameterName(i));
}
SerializationNode& globalParams = node.createChildNode("GlobalParameters");
for (int i = 0; i < force.getNumGlobalParameters(); i++) {
globalParams.createChildNode("Parameter").setStringProperty("name", force.getGlobalParameterName(i)).setDoubleProperty("default", force.getGlobalParameterDefaultValue(i));
}
SerializationNode& particles = node.createChildNode("Particles");
for (int i = 0; i < force.getNumParticles(); i++) {
int particle;
vector<double> params;
force.getParticleParameters(i, particle, params);
SerializationNode& node = particles.createChildNode("Particle").setIntProperty("index", particle);
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
node.setDoubleProperty(key.str(), params[j]);
}
}
}
void* CustomExternalForceProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
CustomExternalForce* force = NULL;
try {
CustomExternalForce* force = new CustomExternalForce(node.getStringProperty("energy"));
const SerializationNode& perParticleParams = node.getChildNode("PerParticleParameters");
for (int i = 0; i < (int) perParticleParams.getChildren().size(); i++) {
const SerializationNode& parameter = perParticleParams.getChildren()[i];
force->addPerParticleParameter(parameter.getStringProperty("name"));
}
const SerializationNode& globalParams = node.getChildNode("GlobalParameters");
for (int i = 0; i < (int) globalParams.getChildren().size(); i++) {
const SerializationNode& parameter = globalParams.getChildren()[i];
force->addGlobalParameter(parameter.getStringProperty("name"), parameter.getDoubleProperty("default"));
}
const SerializationNode& particles = node.getChildNode("Particles");
vector<double> params(force->getNumPerParticleParameters());
for (int i = 0; i < (int) particles.getChildren().size(); i++) {
const SerializationNode& particle = particles.getChildren()[i];
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
params[j] = particle.getDoubleProperty(key.str());
}
force->addParticle(particle.getIntProperty("index"), params);
}
return force;
}
catch (...) {
if (force != NULL)
delete force;
throw;
}
}
/* -------------------------------------------------------------------------- *
* 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/CustomTorsionForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomTorsionForce.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
CustomTorsionForceProxy::CustomTorsionForceProxy() : SerializationProxy("CustomTorsionForce") {
}
void CustomTorsionForceProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const CustomTorsionForce& force = *reinterpret_cast<const CustomTorsionForce*>(object);
node.setStringProperty("energy", force.getEnergyFunction());
SerializationNode& perTorsionParams = node.createChildNode("PerTorsionParameters");
for (int i = 0; i < force.getNumPerTorsionParameters(); i++) {
perTorsionParams.createChildNode("Parameter").setStringProperty("name", force.getPerTorsionParameterName(i));
}
SerializationNode& globalParams = node.createChildNode("GlobalParameters");
for (int i = 0; i < force.getNumGlobalParameters(); i++) {
globalParams.createChildNode("Parameter").setStringProperty("name", force.getGlobalParameterName(i)).setDoubleProperty("default", force.getGlobalParameterDefaultValue(i));
}
SerializationNode& torsions = node.createChildNode("Torsions");
for (int i = 0; i < force.getNumTorsions(); i++) {
int p1, p2, p3, p4;
vector<double> params;
force.getTorsionParameters(i, p1, p2, p3, p4, params);
SerializationNode& node = torsions.createChildNode("Torsion").setIntProperty("p1", p1).setIntProperty("p2", p2).setIntProperty("p3", p3).setIntProperty("p4", p4);
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
node.setDoubleProperty(key.str(), params[j]);
}
}
}
void* CustomTorsionForceProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
CustomTorsionForce* force = NULL;
try {
CustomTorsionForce* force = new CustomTorsionForce(node.getStringProperty("energy"));
const SerializationNode& perTorsionParams = node.getChildNode("PerTorsionParameters");
for (int i = 0; i < (int) perTorsionParams.getChildren().size(); i++) {
const SerializationNode& parameter = perTorsionParams.getChildren()[i];
force->addPerTorsionParameter(parameter.getStringProperty("name"));
}
const SerializationNode& globalParams = node.getChildNode("GlobalParameters");
for (int i = 0; i < (int) globalParams.getChildren().size(); i++) {
const SerializationNode& parameter = globalParams.getChildren()[i];
force->addGlobalParameter(parameter.getStringProperty("name"), parameter.getDoubleProperty("default"));
}
const SerializationNode& torsions = node.getChildNode("Torsions");
vector<double> params(force->getNumPerTorsionParameters());
for (int i = 0; i < (int) torsions.getChildren().size(); i++) {
const SerializationNode& torsion = torsions.getChildren()[i];
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
params[j] = torsion.getDoubleProperty(key.str());
}
force->addTorsion(torsion.getIntProperty("p1"), torsion.getIntProperty("p2"), torsion.getIntProperty("p3"), torsion.getIntProperty("p4"), params);
}
return force;
}
catch (...) {
if (force != NULL)
delete force;
throw;
}
}
...@@ -32,6 +32,10 @@ ...@@ -32,6 +32,10 @@
#include "openmm/AndersenThermostat.h" #include "openmm/AndersenThermostat.h"
#include "openmm/CMAPTorsionForce.h" #include "openmm/CMAPTorsionForce.h"
#include "openmm/CMMotionRemover.h" #include "openmm/CMMotionRemover.h"
#include "openmm/CustomAngleForce.h"
#include "openmm/CustomBondForce.h"
#include "openmm/CustomExternalForce.h"
#include "openmm/CustomTorsionForce.h"
#include "openmm/HarmonicAngleForce.h" #include "openmm/HarmonicAngleForce.h"
#include "openmm/GBSAOBCForce.h" #include "openmm/GBSAOBCForce.h"
#include "openmm/GBVIForce.h" #include "openmm/GBVIForce.h"
...@@ -45,6 +49,10 @@ ...@@ -45,6 +49,10 @@
#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"
#include "openmm/serialization/CustomAngleForceProxy.h"
#include "openmm/serialization/CustomBondForceProxy.h"
#include "openmm/serialization/CustomExternalForceProxy.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"
...@@ -73,6 +81,10 @@ extern "C" void registerSerializationProxies() { ...@@ -73,6 +81,10 @@ extern "C" void registerSerializationProxies() {
SerializationProxy::registerProxy(typeid(AndersenThermostat), new AndersenThermostatProxy()); SerializationProxy::registerProxy(typeid(AndersenThermostat), new AndersenThermostatProxy());
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(CustomAngleForce), new CustomAngleForceProxy());
SerializationProxy::registerProxy(typeid(CustomBondForce), new CustomBondForceProxy());
SerializationProxy::registerProxy(typeid(CustomExternalForce), new CustomExternalForceProxy());
SerializationProxy::registerProxy(typeid(CustomTorsionForce), new CustomTorsionForceProxy());
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());
......
/* -------------------------------------------------------------------------- *
* 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/CustomAngleForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testSerialization() {
// Create a Force.
CustomAngleForce force("5*sin(x)^2+y*z");
force.addGlobalParameter("x", 1.3);
force.addGlobalParameter("y", 2.221);
force.addPerAngleParameter("z");
vector<double> params(1);
params[0] = 1.0;
force.addAngle(1, 2, 3, params);
params[0] = -3.3;
force.addAngle(4, 0, 1, params);
params[0] = 2.1;
force.addAngle(3, 7, 6, params);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<CustomAngleForce>(&force, "Force", buffer);
CustomAngleForce* copy = XmlSerializer::deserialize<CustomAngleForce>(buffer);
// Compare the two forces to see if they are identical.
CustomAngleForce& force2 = *copy;
ASSERT_EQUAL(force.getEnergyFunction(), force2.getEnergyFunction());
ASSERT_EQUAL(force.getNumPerAngleParameters(), force2.getNumPerAngleParameters());
for (int i = 0; i < force.getNumPerAngleParameters(); i++)
ASSERT_EQUAL(force.getPerAngleParameterName(i), force2.getPerAngleParameterName(i));
ASSERT_EQUAL(force.getNumGlobalParameters(), force2.getNumGlobalParameters());
for (int i = 0; i < force.getNumGlobalParameters(); i++) {
ASSERT_EQUAL(force.getGlobalParameterName(i), force2.getGlobalParameterName(i));
ASSERT_EQUAL(force.getGlobalParameterDefaultValue(i), force2.getGlobalParameterDefaultValue(i));
}
ASSERT_EQUAL(force.getNumAngles(), force2.getNumAngles());
for (int i = 0; i < force.getNumAngles(); i++) {
int a1, a2, b1, b2, c1, c2;
vector<double> params1, params2;
force.getAngleParameters(i, a1, b1, c1, params1);
force2.getAngleParameters(i, a2, b2, c2, params2);
ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b2, b2);
ASSERT_EQUAL(c2, c2);
ASSERT_EQUAL(params1.size(), params2.size());
for (int j = 0; j < params1.size(); j++)
ASSERT_EQUAL(params1[j], params2[j]);
}
}
int main() {
try {
testSerialization();
}
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/CustomBondForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testSerialization() {
// Create a Force.
CustomBondForce force("5*sin(x)^2+y*z");
force.addGlobalParameter("x", 1.3);
force.addGlobalParameter("y", 2.221);
force.addPerBondParameter("z");
vector<double> params(1);
params[0] = 1.0;
force.addBond(1, 2, params);
params[0] = -3.3;
force.addBond(4, 0, params);
params[0] = 2.1;
force.addBond(3, 7, params);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<CustomBondForce>(&force, "Force", buffer);
CustomBondForce* copy = XmlSerializer::deserialize<CustomBondForce>(buffer);
// Compare the two forces to see if they are identical.
CustomBondForce& force2 = *copy;
ASSERT_EQUAL(force.getEnergyFunction(), force2.getEnergyFunction());
ASSERT_EQUAL(force.getNumPerBondParameters(), force2.getNumPerBondParameters());
for (int i = 0; i < force.getNumPerBondParameters(); i++)
ASSERT_EQUAL(force.getPerBondParameterName(i), force2.getPerBondParameterName(i));
ASSERT_EQUAL(force.getNumGlobalParameters(), force2.getNumGlobalParameters());
for (int i = 0; i < force.getNumGlobalParameters(); i++) {
ASSERT_EQUAL(force.getGlobalParameterName(i), force2.getGlobalParameterName(i));
ASSERT_EQUAL(force.getGlobalParameterDefaultValue(i), force2.getGlobalParameterDefaultValue(i));
}
ASSERT_EQUAL(force.getNumBonds(), force2.getNumBonds());
for (int i = 0; i < force.getNumBonds(); i++) {
int a1, a2, b1, b2;
vector<double> params1, params2;
force.getBondParameters(i, a1, b1, params1);
force2.getBondParameters(i, a2, b2, params2);
ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b2, b2);
ASSERT_EQUAL(params1.size(), params2.size());
for (int j = 0; j < params1.size(); j++)
ASSERT_EQUAL(params1[j], params2[j]);
}
}
int main() {
try {
testSerialization();
}
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/CustomExternalForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testSerialization() {
// Create a Force.
CustomExternalForce force("5*sin(x)^2+y*z");
force.addGlobalParameter("x", 1.3);
force.addGlobalParameter("y", 2.221);
force.addPerParticleParameter("z");
vector<double> params(1);
params[0] = 1.0;
force.addParticle(1, params);
params[0] = -3.3;
force.addParticle(4, params);
params[0] = 2.1;
force.addParticle(3, params);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<CustomExternalForce>(&force, "Force", buffer);
CustomExternalForce* copy = XmlSerializer::deserialize<CustomExternalForce>(buffer);
// Compare the two forces to see if they are identical.
CustomExternalForce& force2 = *copy;
ASSERT_EQUAL(force.getEnergyFunction(), force2.getEnergyFunction());
ASSERT_EQUAL(force.getNumPerParticleParameters(), force2.getNumPerParticleParameters());
for (int i = 0; i < force.getNumPerParticleParameters(); i++)
ASSERT_EQUAL(force.getPerParticleParameterName(i), force2.getPerParticleParameterName(i));
ASSERT_EQUAL(force.getNumGlobalParameters(), force2.getNumGlobalParameters());
for (int i = 0; i < force.getNumGlobalParameters(); i++) {
ASSERT_EQUAL(force.getGlobalParameterName(i), force2.getGlobalParameterName(i));
ASSERT_EQUAL(force.getGlobalParameterDefaultValue(i), force2.getGlobalParameterDefaultValue(i));
}
ASSERT_EQUAL(force.getNumParticles(), force2.getNumParticles());
for (int i = 0; i < force.getNumParticles(); i++) {
int particle1, particle2;
vector<double> params1, params2;
force.getParticleParameters(i, particle1, params1);
force2.getParticleParameters(i, particle2, params2);
ASSERT_EQUAL(particle1, particle2);
ASSERT_EQUAL(params1.size(), params2.size());
for (int j = 0; j < params1.size(); j++)
ASSERT_EQUAL(params1[j], params2[j]);
}
}
int main() {
try {
testSerialization();
}
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/CustomTorsionForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testSerialization() {
// Create a Force.
CustomTorsionForce force("5*sin(x)^2+y*z");
force.addGlobalParameter("x", 1.3);
force.addGlobalParameter("y", 2.221);
force.addPerTorsionParameter("z");
vector<double> params(1);
params[0] = 1.0;
force.addTorsion(1, 2, 3, 4, params);
params[0] = -3.3;
force.addTorsion(4, 0, 1, 5, params);
params[0] = 2.1;
force.addTorsion(3, 7, 6, 8, params);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<CustomTorsionForce>(&force, "Force", buffer);
CustomTorsionForce* copy = XmlSerializer::deserialize<CustomTorsionForce>(buffer);
// Compare the two forces to see if they are identical.
CustomTorsionForce& force2 = *copy;
ASSERT_EQUAL(force.getEnergyFunction(), force2.getEnergyFunction());
ASSERT_EQUAL(force.getNumPerTorsionParameters(), force2.getNumPerTorsionParameters());
for (int i = 0; i < force.getNumPerTorsionParameters(); i++)
ASSERT_EQUAL(force.getPerTorsionParameterName(i), force2.getPerTorsionParameterName(i));
ASSERT_EQUAL(force.getNumGlobalParameters(), force2.getNumGlobalParameters());
for (int i = 0; i < force.getNumGlobalParameters(); i++) {
ASSERT_EQUAL(force.getGlobalParameterName(i), force2.getGlobalParameterName(i));
ASSERT_EQUAL(force.getGlobalParameterDefaultValue(i), force2.getGlobalParameterDefaultValue(i));
}
ASSERT_EQUAL(force.getNumTorsions(), force2.getNumTorsions());
for (int i = 0; i < force.getNumTorsions(); i++) {
int a1, a2, b1, b2, c1, c2, d1, d2;
vector<double> params1, params2;
force.getTorsionParameters(i, a1, b1, c1, d1, params1);
force2.getTorsionParameters(i, a2, b2, c2, d2, params2);
ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b2, b2);
ASSERT_EQUAL(c2, c2);
ASSERT_EQUAL(d2, d2);
ASSERT_EQUAL(params1.size(), params2.size());
for (int j = 0; j < params1.size(); j++)
ASSERT_EQUAL(params1[j], params2[j]);
}
}
int main() {
try {
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