/* -------------------------------------------------------------------------- * * OpenMMAmoeba * * -------------------------------------------------------------------------- * * 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/AmoebaTorsionTorsionForce.h" #include "openmm/serialization/XmlSerializer.h" #include #include #include using namespace OpenMM; using namespace std; static void loadTorsionTorsionGrid( std::vector< std::vector< std::vector > >& gridVector ){ static const int gridSize = 25; gridVector.resize( gridSize ); for( int ii = 0; ii < gridSize; ii++ ){ gridVector[ii].resize( gridSize ); for( int jj = 0; jj < gridSize; jj++ ){ gridVector[ii][jj].resize( 6 ); for( int kk = 0; kk < 6; kk++ ){ gridVector[ii][jj][0] = -180.0 + 15.0*static_cast(ii); gridVector[ii][jj][1] = -180.0 + 15.0*static_cast(jj); gridVector[ii][jj][2] = static_cast( rand()); gridVector[ii][jj][3] = static_cast( rand()); gridVector[ii][jj][4] = static_cast( rand()); gridVector[ii][jj][5] = static_cast( rand()); } } } } static void compareGrids( const std::vector< std::vector< std::vector > >& grid1, const std::vector< std::vector< std::vector > >& grid2 ) { ASSERT_EQUAL(grid1.size(), grid2.size()); for (int i = 0; i < grid1.size(); i++) { ASSERT_EQUAL(grid1[i].size(), grid2[i].size()); for (int jj = 0; jj < grid1[i].size(); jj++) { ASSERT_EQUAL(grid1[i][jj].size(), grid2[i][jj].size()); for (int kk = 0; kk < grid1[i][jj].size(); kk++) { ASSERT_EQUAL(grid1[i][jj][kk], grid2[i][jj][kk]); } } } } void testSerialization() { // Create a Force. AmoebaTorsionTorsionForce force; for( int ii = 0; ii < 5; ii++ ){ std::vector< std::vector< std::vector > > gridVector; loadTorsionTorsionGrid( gridVector ); force.setTorsionTorsionGrid( ii, gridVector ); } for( int ii = 0; ii < 5; ii++ ){ force.addTorsionTorsion( ii, ii+1,ii+3, ii+4, ii+5, ( (ii % 2 ) ? 1 : 0), (ii % 4) ); } // Serialize and then deserialize it. stringstream buffer; XmlSerializer::serialize(&force, "Force", buffer); if( 0 ){ FILE* filePtr = fopen("TorsionTorsion.xml", "w" ); (void) fprintf( filePtr, "%s", buffer.str().c_str() ); (void) fclose( filePtr ); } AmoebaTorsionTorsionForce* copy = XmlSerializer::deserialize(buffer); // Compare the two forces to see if they are identical. AmoebaTorsionTorsionForce & force2 = *copy; ASSERT_EQUAL(force.getNumTorsionTorsions(), force2.getNumTorsionTorsions()); for (int i = 0; i < force.getNumTorsionTorsions(); i++) { int a1, a2, a3, a4, a5, aChiral, aGridIndex, b1, b2, b3, b4, b5, bChiral, bGridIndex; force.getTorsionTorsionParameters( i, a1, a2, a3, a4, a5, aChiral, aGridIndex); force2.getTorsionTorsionParameters( i, b1, b2, b3, b4, b5, bChiral, bGridIndex); ASSERT_EQUAL(a1, b1); ASSERT_EQUAL(a2, b2); ASSERT_EQUAL(a3, b3); ASSERT_EQUAL(a4, b4); ASSERT_EQUAL(a5, b5); ASSERT_EQUAL(aChiral, bChiral); ASSERT_EQUAL(aGridIndex, bGridIndex ); } ASSERT_EQUAL(force.getNumTorsionTorsionGrids(), force2.getNumTorsionTorsionGrids()); for (int i = 0; i < force.getNumTorsionTorsionGrids(); i++) { const std::vector< std::vector< std::vector > >& grid1 = force.getTorsionTorsionGrid( i ); const std::vector< std::vector< std::vector > >& grid2 = force2.getTorsionTorsionGrid( i ); compareGrids(grid1, grid2 ); } } int main() { try { testSerialization(); } catch(const exception& e) { cout << "exception: " << e.what() << endl; return 1; } cout << "Done" << endl; return 0; }