/* -------------------------------------------------------------------------- * * OpenMM * * -------------------------------------------------------------------------- * * This is part of the OpenMM molecular simulation toolkit originating from * * Simbios, the NIH National Center for Physics-Based Simulation of * * Biological Structures at Stanford, funded under the NIH Roadmap for * * Medical Research, grant U54 GM072970. See https://simtk.org. * * * * Portions copyright (c) 2010-2014 Stanford University and the Authors. * * Authors: Andrew C. Simmonett and Andreas Kraemer * 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/NoseHooverIntegrator.h" #include "openmm/serialization/XmlSerializer.h" #include "openmm/System.h" #include "openmm/Context.h" #include #include using namespace OpenMM; using namespace std; void assertIntegratorsEqual(const NoseHooverIntegrator& integrator1, const NoseHooverIntegrator& integrator2){ ASSERT_EQUAL(integrator1.getStepSize(), integrator2.getStepSize()); ASSERT_EQUAL(integrator1.getConstraintTolerance(), integrator2.getConstraintTolerance()); ASSERT_EQUAL(integrator1.getNumNoseHooverThermostats(), integrator2.getNumNoseHooverThermostats()); for (int i = 0; i < integrator1.getNumNoseHooverThermostats(); i++) { const auto &thermostat1 = integrator1.getNoseHooverThermostat(i); const auto &thermostat2 = integrator2.getNoseHooverThermostat(i); ASSERT_EQUAL(thermostat1.getDefaultTemperature(), thermostat2.getDefaultTemperature()); ASSERT_EQUAL(thermostat1.getDefaultCollisionFrequency(), thermostat2.getDefaultCollisionFrequency()); ASSERT_EQUAL(thermostat1.getDefaultRelativeTemperature(), thermostat2.getDefaultRelativeTemperature()); ASSERT_EQUAL(thermostat1.getDefaultRelativeCollisionFrequency(), thermostat2.getDefaultRelativeCollisionFrequency()); ASSERT_EQUAL(thermostat1.getDefaultChainLength(), thermostat2.getDefaultChainLength()); ASSERT_EQUAL(thermostat1.getDefaultNumMultiTimeSteps(), thermostat2.getDefaultNumMultiTimeSteps()); ASSERT_EQUAL(thermostat1.getDefaultNumYoshidaSuzukiTimeSteps(), thermostat2.getDefaultNumYoshidaSuzukiTimeSteps()); ASSERT_EQUAL(thermostat1.getDefaultChainID(), thermostat2.getDefaultChainID()); const auto &thermostat1Atoms = thermostat1.getThermostatedAtoms(); const auto &thermostat2Atoms = thermostat2.getThermostatedAtoms(); ASSERT_EQUAL(thermostat1Atoms.size(), thermostat2Atoms.size()); for (int j = 0; j < thermostat1Atoms.size(); ++j) { ASSERT_EQUAL(thermostat1Atoms[j], thermostat2Atoms[j]); } const auto &thermostat1Pairs = thermostat1.getThermostatedPairs(); const auto &thermostat2Pairs = thermostat2.getThermostatedPairs(); ASSERT_EQUAL(thermostat1Pairs.size(), thermostat2Pairs.size()); for (int j = 0; j < thermostat1Pairs.size(); ++j) { ASSERT_EQUAL(thermostat1Pairs[j].first, thermostat2Pairs[j].first); ASSERT_EQUAL(thermostat1Pairs[j].second, thermostat2Pairs[j].second); } } } void testSerialization() { // Check with custom subsystem thermostats NoseHooverIntegrator integrator_sub (0.0006); integrator_sub.setConstraintTolerance(0.0404); integrator_sub.addSubsystemThermostat( {0,1,2,3,4,7}, {{0,7}}, 301.1, 1.1, 1.2, 1.3, 9, 2, 5 ); // Serialize and then deserialize it. stringstream buffer; XmlSerializer::serialize(&integrator_sub, "Integrator", buffer); NoseHooverIntegrator* copy = XmlSerializer::deserialize(buffer); assertIntegratorsEqual(integrator_sub, *copy); // Check with default constructor System system; for (int i=0; i<10; i++) system.addParticle(1.0); NoseHooverIntegrator integrator(331, 1.1, 0.004, 5, 5, 5); Context context(system, integrator); // Serialize and then deserialize it. stringstream buffer2; XmlSerializer::serialize(&integrator, "Integrator", buffer2); copy = XmlSerializer::deserialize(buffer2); // for thermostats that apply to the whole system, the particles are not serialized ... ASSERT_EQUAL(copy->getNoseHooverThermostat(0).getThermostatedAtoms().size(), 0); // ... but assigned when creating a context. Context context2(system, *copy); assertIntegratorsEqual(integrator, *copy); } int main() { try { testSerialization(); } catch(const exception& e) { cout << "exception: " << e.what() << endl; return 1; } cout << "Done" << endl; return 0; }