Commit 027fe028 authored by Peter Eastman's avatar Peter Eastman
Browse files

Finished initial implementation of serialization

parent 6a36e240
#ifndef OPENMM_CUSTOMGBFORCE_PROXY_H_
#define OPENMM_CUSTOMGBFORCE_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 CustomGBForce objects.
*/
class OPENMM_EXPORT CustomGBForceProxy : public SerializationProxy {
public:
CustomGBForceProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_CUSTOMGBFORCE_PROXY_H_*/
#ifndef OPENMM_CUSTOMHBONDFORCE_PROXY_H_
#define OPENMM_CUSTOMHBONDFORCE_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 CustomHbondForce objects.
*/
class OPENMM_EXPORT CustomHbondForceProxy : public SerializationProxy {
public:
CustomHbondForceProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_CUSTOMHBONDFORCE_PROXY_H_*/
#ifndef OPENMM_CUSTOMNONBONDEDFORCE_PROXY_H_
#define OPENMM_CUSTOMNONBONDEDFORCE_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 CustomNonbondedForce objects.
*/
class OPENMM_EXPORT CustomNonbondedForceProxy : public SerializationProxy {
public:
CustomNonbondedForceProxy();
void serialize(const void* object, SerializationNode& node) const;
void* deserialize(const SerializationNode& node) const;
};
} // namespace OpenMM
#endif /*OPENMM_CUSTOMNONBONDEDFORCE_PROXY_H_*/
......@@ -80,7 +80,7 @@ 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);
static void decodeNode(SerializationNode& node, const TiXmlElement& element);
};
} // namespace OpenMM
......
/* -------------------------------------------------------------------------- *
* 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/CustomGBForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomGBForce.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
CustomGBForceProxy::CustomGBForceProxy() : SerializationProxy("CustomGBForce") {
}
void CustomGBForceProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const CustomGBForce& force = *reinterpret_cast<const CustomGBForce*>(object);
node.setIntProperty("method", (int) force.getNonbondedMethod());
node.setDoubleProperty("cutoff", force.getCutoffDistance());
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& computedValues = node.createChildNode("ComputedValues");
for (int i = 0; i < force.getNumComputedValues(); i++) {
string name, expression;
CustomGBForce::ComputationType type;
force.getComputedValueParameters(i, name, expression, type);
computedValues.createChildNode("Value").setStringProperty("name", name).setStringProperty("expression", expression).setIntProperty("type", (int) type);
}
SerializationNode& energyTerms = node.createChildNode("EnergyTerms");
for (int i = 0; i < force.getNumEnergyTerms(); i++) {
string expression;
CustomGBForce::ComputationType type;
force.getEnergyTermParameters(i, expression, type);
energyTerms.createChildNode("Term").setStringProperty("expression", expression).setIntProperty("type", (int) type);
}
SerializationNode& particles = node.createChildNode("Particles");
for (int i = 0; i < force.getNumParticles(); i++) {
vector<double> params;
force.getParticleParameters(i, params);
SerializationNode& node = particles.createChildNode("Particle");
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
node.setDoubleProperty(key.str(), params[j]);
}
}
SerializationNode& exclusions = node.createChildNode("Exclusions");
for (int i = 0; i < force.getNumExclusions(); i++) {
int particle1, particle2;
force.getExclusionParticles(i, particle1, particle2);
exclusions.createChildNode("Exclusion").setIntProperty("p1", particle1).setIntProperty("p2", particle2);
}
SerializationNode& functions = node.createChildNode("Functions");
for (int i = 0; i < force.getNumFunctions(); i++) {
string name;
vector<double> values;
double min, max;
bool interpolating;
force.getFunctionParameters(i, name, values, min, max, interpolating);
SerializationNode& node = functions.createChildNode("Function").setStringProperty("name", name).setDoubleProperty("min", min).setDoubleProperty("max", max).setIntProperty("interpolating", interpolating);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
}
void* CustomGBForceProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
CustomGBForce* force = NULL;
try {
CustomGBForce* force = new CustomGBForce();
force->setNonbondedMethod((CustomGBForce::NonbondedMethod) node.getIntProperty("method"));
force->setCutoffDistance(node.getDoubleProperty("cutoff"));
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& computedValues = node.getChildNode("ComputedValues");
for (int i = 0; i < (int) computedValues.getChildren().size(); i++) {
const SerializationNode& value = computedValues.getChildren()[i];
force->addComputedValue(value.getStringProperty("name"), value.getStringProperty("expression"), (CustomGBForce::ComputationType) value.getIntProperty("type"));
}
const SerializationNode& energyTerms = node.getChildNode("EnergyTerms");
for (int i = 0; i < (int) energyTerms.getChildren().size(); i++) {
const SerializationNode& term = energyTerms.getChildren()[i];
force->addEnergyTerm(term.getStringProperty("expression"), (CustomGBForce::ComputationType) term.getIntProperty("type"));
}
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(params);
}
const SerializationNode& exclusions = node.getChildNode("Exclusions");
for (int i = 0; i < (int) exclusions.getChildren().size(); i++) {
const SerializationNode& exclusion = exclusions.getChildren()[i];
force->addExclusion(exclusion.getIntProperty("p1"), exclusion.getIntProperty("p2"));
}
const SerializationNode& functions = node.getChildNode("Functions");
for (int i = 0; i < (int) functions.getChildren().size(); i++) {
const SerializationNode& function = functions.getChildren()[i];
const SerializationNode& valuesNode = function.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
force->addFunction(function.getStringProperty("name"), values, function.getDoubleProperty("min"), function.getDoubleProperty("max"), (bool) function.getIntProperty("interpolating"));
}
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/CustomHbondForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomHbondForce.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
CustomHbondForceProxy::CustomHbondForceProxy() : SerializationProxy("CustomHbondForce") {
}
void CustomHbondForceProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const CustomHbondForce& force = *reinterpret_cast<const CustomHbondForce*>(object);
node.setStringProperty("energy", force.getEnergyFunction());
node.setIntProperty("method", (int) force.getNonbondedMethod());
node.setDoubleProperty("cutoff", force.getCutoffDistance());
SerializationNode& perDonorParams = node.createChildNode("PerDonorParameters");
for (int i = 0; i < force.getNumPerDonorParameters(); i++) {
perDonorParams.createChildNode("Parameter").setStringProperty("name", force.getPerDonorParameterName(i));
}
SerializationNode& perAcceptorParams = node.createChildNode("PerAcceptorParameters");
for (int i = 0; i < force.getNumPerAcceptorParameters(); i++) {
perAcceptorParams.createChildNode("Parameter").setStringProperty("name", force.getPerAcceptorParameterName(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& donors = node.createChildNode("Donors");
for (int i = 0; i < force.getNumDonors(); i++) {
int p1, p2, p3;
vector<double> params;
force.getDonorParameters(i, p1, p2, p3, params);
SerializationNode& node = donors.createChildNode("Donor").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]);
}
}
SerializationNode& acceptors = node.createChildNode("Acceptors");
for (int i = 0; i < force.getNumAcceptors(); i++) {
int p1, p2, p3;
vector<double> params;
force.getAcceptorParameters(i, p1, p2, p3, params);
SerializationNode& node = acceptors.createChildNode("Acceptor").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]);
}
}
SerializationNode& exclusions = node.createChildNode("Exclusions");
for (int i = 0; i < force.getNumExclusions(); i++) {
int donor, acceptor;
force.getExclusionParticles(i, donor, acceptor);
exclusions.createChildNode("Exclusion").setIntProperty("donor", donor).setIntProperty("acceptor", acceptor);
}
SerializationNode& functions = node.createChildNode("Functions");
for (int i = 0; i < force.getNumFunctions(); i++) {
string name;
vector<double> values;
double min, max;
bool interpolating;
force.getFunctionParameters(i, name, values, min, max, interpolating);
SerializationNode& node = functions.createChildNode("Function").setStringProperty("name", name).setDoubleProperty("min", min).setDoubleProperty("max", max).setIntProperty("interpolating", interpolating);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
}
void* CustomHbondForceProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
CustomHbondForce* force = NULL;
try {
CustomHbondForce* force = new CustomHbondForce(node.getStringProperty("energy"));
force->setNonbondedMethod((CustomHbondForce::NonbondedMethod) node.getIntProperty("method"));
force->setCutoffDistance(node.getDoubleProperty("cutoff"));
const SerializationNode& perDonorParams = node.getChildNode("PerDonorParameters");
for (int i = 0; i < (int) perDonorParams.getChildren().size(); i++) {
const SerializationNode& parameter = perDonorParams.getChildren()[i];
force->addPerDonorParameter(parameter.getStringProperty("name"));
}
const SerializationNode& perAcceptorParams = node.getChildNode("PerAcceptorParameters");
for (int i = 0; i < (int) perAcceptorParams.getChildren().size(); i++) {
const SerializationNode& parameter = perAcceptorParams.getChildren()[i];
force->addPerAcceptorParameter(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& donors = node.getChildNode("Donors");
vector<double> params(force->getNumPerDonorParameters());
for (int i = 0; i < (int) donors.getChildren().size(); i++) {
const SerializationNode& donor = donors.getChildren()[i];
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
params[j] = donor.getDoubleProperty(key.str());
}
force->addDonor(donor.getIntProperty("p1"), donor.getIntProperty("p2"), donor.getIntProperty("p3"), params);
}
const SerializationNode& acceptors = node.getChildNode("Acceptors");
params.resize(force->getNumPerAcceptorParameters());
for (int i = 0; i < (int) acceptors.getChildren().size(); i++) {
const SerializationNode& acceptor = acceptors.getChildren()[i];
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
params[j] = acceptor.getDoubleProperty(key.str());
}
force->addAcceptor(acceptor.getIntProperty("p1"), acceptor.getIntProperty("p2"), acceptor.getIntProperty("p3"), params);
}
const SerializationNode& exclusions = node.getChildNode("Exclusions");
for (int i = 0; i < (int) exclusions.getChildren().size(); i++) {
const SerializationNode& exclusion = exclusions.getChildren()[i];
force->addExclusion(exclusion.getIntProperty("donor"), exclusion.getIntProperty("acceptor"));
}
const SerializationNode& functions = node.getChildNode("Functions");
for (int i = 0; i < (int) functions.getChildren().size(); i++) {
const SerializationNode& function = functions.getChildren()[i];
const SerializationNode& valuesNode = function.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
force->addFunction(function.getStringProperty("name"), values, function.getDoubleProperty("min"), function.getDoubleProperty("max"), (bool) function.getIntProperty("interpolating"));
}
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/CustomNonbondedForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomNonbondedForce.h"
#include <sstream>
using namespace OpenMM;
using namespace std;
CustomNonbondedForceProxy::CustomNonbondedForceProxy() : SerializationProxy("CustomNonbondedForce") {
}
void CustomNonbondedForceProxy::serialize(const void* object, SerializationNode& node) const {
node.setIntProperty("version", 1);
const CustomNonbondedForce& force = *reinterpret_cast<const CustomNonbondedForce*>(object);
node.setStringProperty("energy", force.getEnergyFunction());
node.setIntProperty("method", (int) force.getNonbondedMethod());
node.setDoubleProperty("cutoff", force.getCutoffDistance());
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++) {
vector<double> params;
force.getParticleParameters(i, params);
SerializationNode& node = particles.createChildNode("Particle");
for (int j = 0; j < (int) params.size(); j++) {
stringstream key;
key << "param";
key << j+1;
node.setDoubleProperty(key.str(), params[j]);
}
}
SerializationNode& exclusions = node.createChildNode("Exclusions");
for (int i = 0; i < force.getNumExclusions(); i++) {
int particle1, particle2;
force.getExclusionParticles(i, particle1, particle2);
exclusions.createChildNode("Exclusion").setIntProperty("p1", particle1).setIntProperty("p2", particle2);
}
SerializationNode& functions = node.createChildNode("Functions");
for (int i = 0; i < force.getNumFunctions(); i++) {
string name;
vector<double> values;
double min, max;
bool interpolating;
force.getFunctionParameters(i, name, values, min, max, interpolating);
SerializationNode& node = functions.createChildNode("Function").setStringProperty("name", name).setDoubleProperty("min", min).setDoubleProperty("max", max).setIntProperty("interpolating", interpolating);
SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]);
}
}
void* CustomNonbondedForceProxy::deserialize(const SerializationNode& node) const {
if (node.getIntProperty("version") != 1)
throw OpenMMException("Unsupported version number");
CustomNonbondedForce* force = NULL;
try {
CustomNonbondedForce* force = new CustomNonbondedForce(node.getStringProperty("energy"));
force->setNonbondedMethod((CustomNonbondedForce::NonbondedMethod) node.getIntProperty("method"));
force->setCutoffDistance(node.getDoubleProperty("cutoff"));
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(params);
}
const SerializationNode& exclusions = node.getChildNode("Exclusions");
for (int i = 0; i < (int) exclusions.getChildren().size(); i++) {
const SerializationNode& exclusion = exclusions.getChildren()[i];
force->addExclusion(exclusion.getIntProperty("p1"), exclusion.getIntProperty("p2"));
}
const SerializationNode& functions = node.getChildNode("Functions");
for (int i = 0; i < (int) functions.getChildren().size(); i++) {
const SerializationNode& function = functions.getChildren()[i];
const SerializationNode& valuesNode = function.getChildNode("Values");
vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++)
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v"));
force->addFunction(function.getStringProperty("name"), values, function.getDoubleProperty("min"), function.getDoubleProperty("max"), (bool) function.getIntProperty("interpolating"));
}
return force;
}
catch (...) {
if (force != NULL)
delete force;
throw;
}
}
......@@ -82,7 +82,7 @@ void* NonbondedForceProxy::deserialize(const SerializationNode& node) const {
const SerializationNode& exceptions = node.getChildNode("Exceptions");
for (int i = 0; i < (int) exceptions.getChildren().size(); i++) {
const SerializationNode& exception = exceptions.getChildren()[i];
force->addException(exception.getDoubleProperty("p1"), exception.getDoubleProperty("p2"), exception.getDoubleProperty("q"), exception.getDoubleProperty("sig"), exception.getDoubleProperty("eps"));
force->addException(exception.getIntProperty("p1"), exception.getIntProperty("p2"), exception.getDoubleProperty("q"), exception.getDoubleProperty("sig"), exception.getDoubleProperty("eps"));
}
}
catch (...) {
......
......@@ -35,6 +35,9 @@
#include "openmm/CustomAngleForce.h"
#include "openmm/CustomBondForce.h"
#include "openmm/CustomExternalForce.h"
#include "openmm/CustomGBForce.h"
#include "openmm/CustomHbondForce.h"
#include "openmm/CustomNonbondedForce.h"
#include "openmm/CustomTorsionForce.h"
#include "openmm/HarmonicAngleForce.h"
#include "openmm/GBSAOBCForce.h"
......@@ -52,6 +55,9 @@
#include "openmm/serialization/CustomAngleForceProxy.h"
#include "openmm/serialization/CustomBondForceProxy.h"
#include "openmm/serialization/CustomExternalForceProxy.h"
#include "openmm/serialization/CustomGBForceProxy.h"
#include "openmm/serialization/CustomHbondForceProxy.h"
#include "openmm/serialization/CustomNonbondedForceProxy.h"
#include "openmm/serialization/CustomTorsionForceProxy.h"
#include "openmm/serialization/GBSAOBCForceProxy.h"
#include "openmm/serialization/GBVIForceProxy.h"
......@@ -84,6 +90,9 @@ extern "C" void registerSerializationProxies() {
SerializationProxy::registerProxy(typeid(CustomAngleForce), new CustomAngleForceProxy());
SerializationProxy::registerProxy(typeid(CustomBondForce), new CustomBondForceProxy());
SerializationProxy::registerProxy(typeid(CustomExternalForce), new CustomExternalForceProxy());
SerializationProxy::registerProxy(typeid(CustomGBForce), new CustomGBForceProxy());
SerializationProxy::registerProxy(typeid(CustomHbondForce), new CustomHbondForceProxy());
SerializationProxy::registerProxy(typeid(CustomNonbondedForce), new CustomNonbondedForceProxy());
SerializationProxy::registerProxy(typeid(CustomTorsionForce), new CustomTorsionForceProxy());
SerializationProxy::registerProxy(typeid(GBSAOBCForce), new GBSAOBCForceProxy());
SerializationProxy::registerProxy(typeid(GBVIForce), new GBVIForceProxy());
......
......@@ -66,7 +66,7 @@ void* XmlSerializer::deserializeStream(std::istream& stream) {
return proxy.deserialize(root);
}
SerializationNode* XmlSerializer::decodeNode(SerializationNode& node, const TiXmlElement& element) {
void 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()) {
......
/* -------------------------------------------------------------------------- *
* 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/CustomGBForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testSerialization() {
// Create a Force.
CustomGBForce force;
force.setNonbondedMethod(CustomGBForce::CutoffPeriodic);
force.setCutoffDistance(2.1);
force.addGlobalParameter("x", 1.3);
force.addGlobalParameter("y", 2.221);
force.addPerParticleParameter("z");
force.addComputedValue("a", "x+1", CustomGBForce::ParticlePairNoExclusions);
force.addComputedValue("b", "y-1", CustomGBForce::SingleParticle);
force.addEnergyTerm("a*b", CustomGBForce::SingleParticle);
force.addEnergyTerm("a*b+1", CustomGBForce::ParticlePair);
vector<double> params(1);
params[0] = 1.0;
force.addParticle(params);
params[0] = -3.3;
force.addParticle(params);
params[0] = 2.1;
force.addParticle(params);
force.addExclusion(0, 1);
force.addExclusion(1, 2);
vector<double> values(10);
for (int i = 0; i < 10; i++)
values[i] = sin(i);
force.addFunction("f", values, 0.5, 1.5, true);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<CustomGBForce>(&force, "Force", buffer);
CustomGBForce* copy = XmlSerializer::deserialize<CustomGBForce>(buffer);
// Compare the two forces to see if they are identical.
CustomGBForce& force2 = *copy;
ASSERT_EQUAL(force.getNonbondedMethod(), force2.getNonbondedMethod());
ASSERT_EQUAL(force.getCutoffDistance(), force2.getCutoffDistance());
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.getNumComputedValues(), force2.getNumComputedValues());
for (int i = 0; i < force.getNumComputedValues(); i++) {
string name1, name2, expression1, expression2;
CustomGBForce::ComputationType type1, type2;
force.getComputedValueParameters(i, name1, expression1, type1);
force2.getComputedValueParameters(i, name2, expression2, type2);
ASSERT_EQUAL(name1, name2);
ASSERT_EQUAL(expression1, expression2);
ASSERT_EQUAL(type1, type2);
}
ASSERT_EQUAL(force.getNumEnergyTerms(), force2.getNumEnergyTerms());
for (int i = 0; i < force.getNumEnergyTerms(); i++) {
string expression1, expression2;
CustomGBForce::ComputationType type1, type2;
force.getEnergyTermParameters(i, expression1, type1);
force2.getEnergyTermParameters(i, expression2, type2);
ASSERT_EQUAL(expression1, expression2);
ASSERT_EQUAL(type1, type2);
}
ASSERT_EQUAL(force.getNumParticles(), force2.getNumParticles());
for (int i = 0; i < force.getNumParticles(); i++) {
vector<double> params1, params2;
force.getParticleParameters(i, params1);
force2.getParticleParameters(i, params2);
ASSERT_EQUAL(params1.size(), params2.size());
for (int j = 0; j < params1.size(); j++)
ASSERT_EQUAL(params1[j], params2[j]);
}
ASSERT_EQUAL(force.getNumExclusions(), force2.getNumExclusions());
for (int i = 0; i < force.getNumExclusions(); i++) {
int a1, a2, b1, b2;
force.getExclusionParticles(i, a1, b1);
force2.getExclusionParticles(i, a2, b2);
ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b1, b2);
}
ASSERT_EQUAL(force.getNumFunctions(), force2.getNumFunctions());
for (int i = 0; i < force.getNumFunctions(); i++) {
string name1, name2;
double min1, min2, max1, max2;
vector<double> val1, val2;
bool interp1, interp2;
force.getFunctionParameters(i, name1, val1, min1, max1, interp1);
force2.getFunctionParameters(i, name2, val2, min2, max2, interp2);
ASSERT_EQUAL(name1, name2);
ASSERT_EQUAL(min1, min2);
ASSERT_EQUAL(max1, max2);
ASSERT_EQUAL(interp1, interp2);
ASSERT_EQUAL(val1.size(), val2.size());
for (int j = 0; j < val1.size(); j++)
ASSERT_EQUAL(val1[j], val2[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/CustomHbondForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testSerialization() {
// Create a Force.
CustomHbondForce force("5*sin(x)^2+y*z");
force.setNonbondedMethod(CustomHbondForce::CutoffPeriodic);
force.setCutoffDistance(2.1);
force.addGlobalParameter("x", 1.3);
force.addGlobalParameter("y", 2.221);
force.addPerDonorParameter("z");
force.addPerAcceptorParameter("w");
force.addPerAcceptorParameter("q");
vector<double> params(1);
params[0] = 1.0;
force.addDonor(0, 1, 2, params);
params[0] = -3.3;
force.addDonor(5, 4, 3, params);
params.resize(2);
params[0] = 2.1;
params[1] = 3.3;
force.addAcceptor(1, 0, -1, params);
params[0] = -1;
params[1] = -1.1;
force.addAcceptor(2, 3, -1, params);
force.addExclusion(0, 1);
force.addExclusion(1, 2);
vector<double> values(10);
for (int i = 0; i < 10; i++)
values[i] = sin(i);
force.addFunction("f", values, 0.5, 1.5, true);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<CustomHbondForce>(&force, "Force", buffer);
CustomHbondForce* copy = XmlSerializer::deserialize<CustomHbondForce>(buffer);
// Compare the two forces to see if they are identical.
CustomHbondForce& force2 = *copy;
ASSERT_EQUAL(force.getEnergyFunction(), force2.getEnergyFunction());
ASSERT_EQUAL(force.getNonbondedMethod(), force2.getNonbondedMethod());
ASSERT_EQUAL(force.getCutoffDistance(), force2.getCutoffDistance());
ASSERT_EQUAL(force.getNumPerDonorParameters(), force2.getNumPerDonorParameters());
for (int i = 0; i < force.getNumPerDonorParameters(); i++)
ASSERT_EQUAL(force.getPerDonorParameterName(i), force2.getPerDonorParameterName(i));
ASSERT_EQUAL(force.getNumPerAcceptorParameters(), force2.getNumPerAcceptorParameters());
for (int i = 0; i < force.getNumPerAcceptorParameters(); i++)
ASSERT_EQUAL(force.getPerAcceptorParameterName(i), force2.getPerAcceptorParameterName(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.getNumDonors(), force2.getNumDonors());
for (int i = 0; i < force.getNumDonors(); i++) {
int a1, b1, c1, a2, b2, c2;
vector<double> params1, params2;
force.getDonorParameters(i, a1, b1, c1, params1);
force2.getDonorParameters(i, a2, b2, c2, params2);
ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b1, b2);
ASSERT_EQUAL(c1, c2);
ASSERT_EQUAL(params1.size(), params2.size());
for (int j = 0; j < params1.size(); j++)
ASSERT_EQUAL(params1[j], params2[j]);
}
ASSERT_EQUAL(force.getNumAcceptors(), force2.getNumAcceptors());
for (int i = 0; i < force.getNumAcceptors(); i++) {
int a1, b1, c1, a2, b2, c2;
vector<double> params1, params2;
force.getAcceptorParameters(i, a1, b1, c1, params1);
force2.getAcceptorParameters(i, a2, b2, c2, params2);
ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b1, b2);
ASSERT_EQUAL(c1, c2);
ASSERT_EQUAL(params1.size(), params2.size());
for (int j = 0; j < params1.size(); j++)
ASSERT_EQUAL(params1[j], params2[j]);
}
ASSERT_EQUAL(force.getNumExclusions(), force2.getNumExclusions());
for (int i = 0; i < force.getNumExclusions(); i++) {
int a1, a2, b1, b2;
force.getExclusionParticles(i, a1, b1);
force2.getExclusionParticles(i, a2, b2);
ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b1, b2);
}
ASSERT_EQUAL(force.getNumFunctions(), force2.getNumFunctions());
for (int i = 0; i < force.getNumFunctions(); i++) {
string name1, name2;
double min1, min2, max1, max2;
vector<double> val1, val2;
bool interp1, interp2;
force.getFunctionParameters(i, name1, val1, min1, max1, interp1);
force2.getFunctionParameters(i, name2, val2, min2, max2, interp2);
ASSERT_EQUAL(name1, name2);
ASSERT_EQUAL(min1, min2);
ASSERT_EQUAL(max1, max2);
ASSERT_EQUAL(interp1, interp2);
ASSERT_EQUAL(val1.size(), val2.size());
for (int j = 0; j < val1.size(); j++)
ASSERT_EQUAL(val1[j], val2[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/CustomNonbondedForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
using namespace OpenMM;
using namespace std;
void testSerialization() {
// Create a Force.
CustomNonbondedForce force("5*sin(x)^2+y*z");
force.setNonbondedMethod(CustomNonbondedForce::CutoffPeriodic);
force.setCutoffDistance(2.1);
force.addGlobalParameter("x", 1.3);
force.addGlobalParameter("y", 2.221);
force.addPerParticleParameter("z");
vector<double> params(1);
params[0] = 1.0;
force.addParticle(params);
params[0] = -3.3;
force.addParticle(params);
params[0] = 2.1;
force.addParticle(params);
force.addExclusion(0, 1);
force.addExclusion(1, 2);
vector<double> values(10);
for (int i = 0; i < 10; i++)
values[i] = sin(i);
force.addFunction("f", values, 0.5, 1.5, true);
// Serialize and then deserialize it.
stringstream buffer;
XmlSerializer::serialize<CustomNonbondedForce>(&force, "Force", buffer);
CustomNonbondedForce* copy = XmlSerializer::deserialize<CustomNonbondedForce>(buffer);
// Compare the two forces to see if they are identical.
CustomNonbondedForce& force2 = *copy;
ASSERT_EQUAL(force.getEnergyFunction(), force2.getEnergyFunction());
ASSERT_EQUAL(force.getNonbondedMethod(), force2.getNonbondedMethod());
ASSERT_EQUAL(force.getCutoffDistance(), force2.getCutoffDistance());
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++) {
vector<double> params1, params2;
force.getParticleParameters(i, params1);
force2.getParticleParameters(i, params2);
ASSERT_EQUAL(params1.size(), params2.size());
for (int j = 0; j < params1.size(); j++)
ASSERT_EQUAL(params1[j], params2[j]);
}
ASSERT_EQUAL(force.getNumExclusions(), force2.getNumExclusions());
for (int i = 0; i < force.getNumExclusions(); i++) {
int a1, a2, b1, b2;
force.getExclusionParticles(i, a1, b1);
force2.getExclusionParticles(i, a2, b2);
ASSERT_EQUAL(a1, a2);
ASSERT_EQUAL(b1, b2);
}
ASSERT_EQUAL(force.getNumFunctions(), force2.getNumFunctions());
for (int i = 0; i < force.getNumFunctions(); i++) {
string name1, name2;
double min1, min2, max1, max2;
vector<double> val1, val2;
bool interp1, interp2;
force.getFunctionParameters(i, name1, val1, min1, max1, interp1);
force2.getFunctionParameters(i, name2, val2, min2, max2, interp2);
ASSERT_EQUAL(name1, name2);
ASSERT_EQUAL(min1, min2);
ASSERT_EQUAL(max1, max2);
ASSERT_EQUAL(interp1, interp2);
ASSERT_EQUAL(val1.size(), val2.size());
for (int j = 0; j < val1.size(); j++)
ASSERT_EQUAL(val1[j], val2[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