TestSerializeAmoebaTorsionTorsionForce.cpp 6.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* -------------------------------------------------------------------------- *
 *                                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 <iostream>
#include <sstream>
#include <stdlib.h>

using namespace OpenMM;
using namespace std;

static void loadTorsionTorsionGrid( std::vector< std::vector< std::vector<double> > >& gridVector ){
 
    static const int gridSize = 25;
    gridVector.resize( gridSize );
46
    for( unsigned int ii = 0; ii < gridSize; ii++ ){
47
        gridVector[ii].resize( gridSize );
48
        for( unsigned int jj = 0; jj < gridSize; jj++ ){
49
            gridVector[ii][jj].resize( 6 );
50
            for( unsigned int kk = 0; kk < 6; kk++ ){
51
52
53
54
55
56
57
58
59
60
61
62
63
64
                gridVector[ii][jj][0] = -180.0 + 15.0*static_cast<double>(ii);
                gridVector[ii][jj][1] = -180.0 + 15.0*static_cast<double>(jj);
                gridVector[ii][jj][2] = static_cast<double>( rand());
                gridVector[ii][jj][3] = static_cast<double>( rand());
                gridVector[ii][jj][4] = static_cast<double>( rand());
                gridVector[ii][jj][5] = static_cast<double>( rand());
            }
        }
     }
}

static void compareGrids( const std::vector< std::vector< std::vector<double> > >& grid1, const std::vector< std::vector< std::vector<double> > >& grid2 ) {

    ASSERT_EQUAL(grid1.size(), grid2.size());
65
66
67
68
69
70
    for (unsigned int ii = 0; ii < grid1.size(); ii++) {
        ASSERT_EQUAL(grid1[ii].size(), grid2[ii].size());
        for (int jj = 0; jj < grid1[ii].size(); jj++) {
            ASSERT_EQUAL(grid1[ii][jj].size(), grid2[ii][jj].size());
            for (int kk = 0; kk < grid1[ii][jj].size(); kk++) {
                ASSERT_EQUAL(grid1[ii][jj][kk], grid2[ii][jj][kk]);
71
72
73
74
75
76
77
78
            }
        }
    }
}

void testSerialization() {
    // Create a Force.

79
    AmoebaTorsionTorsionForce force1;
80

81
    for( unsigned int ii = 0; ii < 5; ii++ ){
82
83
        std::vector< std::vector< std::vector<double> > > gridVector;
        loadTorsionTorsionGrid( gridVector );
84
        force1.setTorsionTorsionGrid( ii, gridVector );
85
    }
86
87
    for( unsigned int ii = 0; ii < 5; ii++ ){
        force1.addTorsionTorsion( ii, ii+1,ii+3, ii+4, ii+5, ( (ii % 2 ) ? 1 : 0), (ii % 4) );
88
89
90
91
92
    }

    // Serialize and then deserialize it.

    stringstream buffer;
93
    XmlSerializer::serialize<AmoebaTorsionTorsionForce>(&force1, "Force", buffer);
94

95
96
97
98
99
    if( 0 ){
        FILE* filePtr = fopen("TorsionTorsion.xml", "w" );
        (void) fprintf( filePtr, "%s", buffer.str().c_str() );
        (void) fclose( filePtr );
    }
100
101
102

    AmoebaTorsionTorsionForce* copy = XmlSerializer::deserialize<AmoebaTorsionTorsionForce>(buffer);

103
    // Compare the two force1s to see if they are identical.  
104
105

    AmoebaTorsionTorsionForce & force2 = *copy;
106
107
    ASSERT_EQUAL(force1.getNumTorsionTorsions(), force2.getNumTorsionTorsions());
    for (unsigned int ii = 0; ii < force1.getNumTorsionTorsions(); ii++) {
108
109
110

        int a1, a2, a3, a4, a5, aChiral, aGridIndex, b1, b2, b3, b4, b5, bChiral, bGridIndex;

111
112
        force1.getTorsionTorsionParameters( ii, a1, a2, a3, a4, a5, aChiral, aGridIndex);
        force2.getTorsionTorsionParameters( ii, b1, b2, b3, b4, b5, bChiral, bGridIndex);
113
114
115
116
117
118
119
120
121
122

        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 );
    }

123
124
125
126
    ASSERT_EQUAL(force1.getNumTorsionTorsionGrids(), force2.getNumTorsionTorsionGrids());
    for (unsigned int ii = 0; ii < force1.getNumTorsionTorsionGrids(); ii++) {
        const std::vector< std::vector< std::vector<double> > >& grid1 = force1.getTorsionTorsionGrid( ii );
        const std::vector< std::vector< std::vector<double> > >& grid2 = force2.getTorsionTorsionGrid( ii );
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        compareGrids(grid1, grid2 );
    }

}

int main() {
    try {
        testSerialization();
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}