/* -------------------------------------------------------------------------- * * 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/AssertionUtilities.h" #include "openmm/HarmonicBondForce.h" #include "openmm/NonbondedForce.h" #include "openmm/System.h" #include "openmm/Context.h" #include "openmm/LangevinIntegrator.h" #include "openmm/AndersenThermostat.h" #include "openmm/MonteCarloBarostat.h" #include "openmm/serialization/XmlSerializer.h" #include #include #include using namespace OpenMM; using namespace std; void testSerialization() { // Create a System. const int numParticles=50; System system; system.setDefaultPeriodicBoxVectors(Vec3(6.2, 0, 0), Vec3(0, 6.2, 0), Vec3(0, 0, 6.2 )); NonbondedForce* nonbonded = new NonbondedForce(); nonbonded->setNonbondedMethod(NonbondedForce::Ewald); nonbonded->setCutoffDistance(0.8); nonbonded->setEwaldErrorTolerance(0.01); for (int i = 0; i < numParticles/2; i++) system.addParticle(22.99); for (int i = 0; i < numParticles/2; i++) system.addParticle(35.45); for (int i = 0; i < numParticles/2; i++) nonbonded->addParticle(1.0, 1.0,0.0); for (int i = 0; i < numParticles/2; i++) nonbonded->addParticle(-1.0, 1.0,0.0); system.addForce(nonbonded); system.addForce(new AndersenThermostat(393.3, 19.3)); system.addForce(new MonteCarloBarostat(25, 393.3, 25)); Integrator *intg = new LangevinIntegrator(300,79,0.002); Context *ctxt = new Context(system, *intg); // Set positions, velocities, forces vector positions; for(int i=0;i velocities; for(int i=0;isetPositions(positions); ctxt->setVelocities(velocities); // Serialize and then deserialize it. State s1 = ctxt->getState(State::Positions | State::Velocities | State::Forces | State::Energy | State::Parameters); stringstream buffer; XmlSerializer::serialize(&s1, "State", buffer); State* copy = XmlSerializer::deserialize(buffer); State& s2 = *copy; // Compare the two states to see if they are identical. vector pos1 = s1.getPositions(); vector pos2 = s2.getPositions(); ASSERT_EQUAL(pos1.size(), pos2.size()); ASSERT_EQUAL(pos1.size(), positions.size()); for(int i=0; i vel1 = s1.getVelocities(); vector vel2 = s2.getVelocities(); ASSERT_EQUAL(vel1.size(), vel2.size()); for(int i=0; i forces1 = s1.getForces(); vector forces2 = s2.getForces(); ASSERT_EQUAL(forces1.size(), forces2.size()); for(int i=0; i p1 = s1.getParameters(); map p2 = s2.getParameters(); ASSERT_EQUAL(p1.size(), p2.size()); map::const_iterator it1=p1.begin(); map::const_iterator it2=p2.begin(); //maps are ordered, so iterators should be in the same order. for(it1=p1.begin(); it1!=p1.end(); it1++, it2++) { assert((it1->first).compare(it2->first) == 0); ASSERT_EQUAL(it1->second, it2->second); } } int main() { try { testSerialization(); } catch(const exception& e) { cout << "exception: " << e.what() << endl; return 1; } cout << "Done" << endl; return 0; }