TestSerializeAmoebaTorsionTorsionForce.cpp 6.57 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                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.               *
 *                                                                            *
9
 * Portions copyright (c) 2010-2016 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * 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.                                     *
 * -------------------------------------------------------------------------- */

32
#include "openmm/Platform.h"
33
#include "openmm/internal/AssertionUtilities.h"
34
35
36
37
38
39
40
41
42
#include "openmm/AmoebaTorsionTorsionForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>
#include <stdlib.h>

using namespace OpenMM;
using namespace std;

43
44
extern "C" void registerAmoebaSerializationProxies();

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

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

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

void testSerialization() {
    // Create a Force.

82
    AmoebaTorsionTorsionForce force1;
83

84
    force1.setForceGroup(3);
85
    for (unsigned int ii = 0; ii < 5; ii++) {
86
        std::vector< std::vector< std::vector<double> > > gridVector;
87
88
        loadTorsionTorsionGrid(gridVector);
        force1.setTorsionTorsionGrid(ii, gridVector);
89
    }
90
91
    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));
92
    }
93
    force1.setUsesPeriodicBoundaryConditions(true);
94
95
96
97

    // Serialize and then deserialize it.

    stringstream buffer;
98
    XmlSerializer::serialize<AmoebaTorsionTorsionForce>(&force1, "Force", buffer);
99
100
    AmoebaTorsionTorsionForce* copy = XmlSerializer::deserialize<AmoebaTorsionTorsionForce>(buffer);

101
    // Compare the two force1s to see if they are identical.  
102
103

    AmoebaTorsionTorsionForce & force2 = *copy;
104
    ASSERT_EQUAL(force1.getForceGroup(), force2.getForceGroup());
105
    ASSERT_EQUAL(force1.usesPeriodicBoundaryConditions(), force2.usesPeriodicBoundaryConditions());
106
    ASSERT_EQUAL(force1.getNumTorsionTorsions(), force2.getNumTorsionTorsions());
107
    for (unsigned int ii = 0; ii < static_cast<unsigned int>(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

        ASSERT_EQUAL(a1, b1);
        ASSERT_EQUAL(a2, b2);
        ASSERT_EQUAL(a3, b3);
        ASSERT_EQUAL(a4, b4);
        ASSERT_EQUAL(a5, b5);
        ASSERT_EQUAL(aChiral, bChiral);
120
        ASSERT_EQUAL(aGridIndex, bGridIndex);
121
122
    }

123
    ASSERT_EQUAL(force1.getNumTorsionTorsionGrids(), force2.getNumTorsionTorsionGrids());
124
    for (unsigned int ii = 0; ii < static_cast<unsigned int>(force1.getNumTorsionTorsionGrids()); ii++) {
125
126
127
        const std::vector< std::vector< std::vector<double> > >& grid1 = force1.getTorsionTorsionGrid(ii);
        const std::vector< std::vector< std::vector<double> > >& grid2 = force2.getTorsionTorsionGrid(ii);
        compareGrids(grid1, grid2);
128
129
130
131
132
133
    }

}

int main() {
    try {
134
        registerAmoebaSerializationProxies();
135
136
137
138
139
140
141
142
143
144
        testSerialization();
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}