TestSerializeAmoebaVdwForce.cpp 8.71 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
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.      *
Mark Friedrichs's avatar
Mark Friedrichs committed
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"
Mark Friedrichs's avatar
Mark Friedrichs committed
34
35
36
37
38
39
40
41
#include "openmm/AmoebaVdwForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>

using namespace OpenMM;
using namespace std;

42
43
extern "C" void registerAmoebaSerializationProxies();

Mark Friedrichs's avatar
Mark Friedrichs committed
44
45
46
void testSerialization() {
    // Create a Force.

47
    AmoebaVdwForce force1;
48
    force1.setForceGroup(3);
49
50
51
    force1.setSigmaCombiningRule("GEOMETRIC");
    force1.setEpsilonCombiningRule("GEOMETRIC");
    force1.setCutoff(0.9);
52
    force1.setNonbondedMethod(AmoebaVdwForce::CutoffPeriodic);
53
    force1.setAlchemicalMethod(AmoebaVdwForce::None);
54
    force1.setPotentialFunction(AmoebaVdwForce::Buffered147);
55

56
57
58
    force1.addParticle(0, 1.0, 2.0, 0.9, false);
    force1.addParticle(1, 1.1, 2.1, 0.9, true);
    force1.addParticle(2, 1.3, 4.1, 0.9, false);
59
    for (int i = 0; i < 3; i++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
60
        std::vector< int > exclusions;
61
62
63
64
        exclusions.push_back(i);
        exclusions.push_back(i + 1);
        exclusions.push_back(i + 10);
        force1.setParticleExclusions(i, exclusions);
Mark Friedrichs's avatar
Mark Friedrichs committed
65
66
67
68
69
    }

    // Serialize and then deserialize it.

    stringstream buffer;
70
    XmlSerializer::serialize<AmoebaVdwForce>(&force1, "Force", buffer);
Mark Friedrichs's avatar
Mark Friedrichs committed
71
72
73
74
    AmoebaVdwForce* copy = XmlSerializer::deserialize<AmoebaVdwForce>(buffer);

    // Compare the two forces to see if they are identical.  
    AmoebaVdwForce& force2 = *copy;
75

76
    ASSERT_EQUAL(force1.getForceGroup(), force2.getForceGroup());
77
78
79
    ASSERT_EQUAL(force1.getSigmaCombiningRule(),    force2.getSigmaCombiningRule());
    ASSERT_EQUAL(force1.getEpsilonCombiningRule(),  force2.getEpsilonCombiningRule());
    ASSERT_EQUAL(force1.getCutoff(),                force2.getCutoff());
80
    ASSERT_EQUAL(force1.getNonbondedMethod(),       force2.getNonbondedMethod());
81
    ASSERT_EQUAL(force1.getAlchemicalMethod(),      force2.getAlchemicalMethod());
82
    ASSERT_EQUAL(force1.getPotentialFunction(),     force2.getPotentialFunction());
83
84
85

    ASSERT_EQUAL(force1.getNumParticles(),          force2.getNumParticles());

86
    for (int i = 0; i < force1.getNumParticles(); i++) {
87

88
89
        int ivIndex1, type1;
        int ivIndex2, type2;
90

Mark Friedrichs's avatar
Mark Friedrichs committed
91
92
        double sigma1, epsilon1, reductionFactor1;
        double sigma2, epsilon2, reductionFactor2;
93

94
95
96
        bool isAlchemical1;
        bool isAlchemical2;

97
98
        force1.getParticleParameters(i, ivIndex1, sigma1, epsilon1, reductionFactor1, isAlchemical1, type1);
        force2.getParticleParameters(i, ivIndex2, sigma2, epsilon2, reductionFactor2, isAlchemical2, type2);
99

100
        ASSERT_EQUAL(ivIndex1,          ivIndex2);
101
102
103
        ASSERT_EQUAL(sigma1,            sigma2);
        ASSERT_EQUAL(epsilon1,          epsilon2);
        ASSERT_EQUAL(reductionFactor1,  reductionFactor2);
104
105
        ASSERT_EQUAL(isAlchemical1,     isAlchemical2);
        ASSERT_EQUAL(type1,             type2);
Mark Friedrichs's avatar
Mark Friedrichs committed
106
    }
107
    for (int i = 0; i < force1.getNumParticles(); i++) {
108

Mark Friedrichs's avatar
Mark Friedrichs committed
109
110
        std::vector< int > exclusions1;
        std::vector< int > exclusions2;
111

112
113
        force1.getParticleExclusions(i, exclusions1);
        force2.getParticleExclusions(i, exclusions2);
114

Mark Friedrichs's avatar
Mark Friedrichs committed
115
        ASSERT_EQUAL(exclusions1.size(), exclusions2.size());
116
        for (int j = 0; j < exclusions1.size(); j++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
117
            int hit = 0;
118
119
            for (int kk = 0; kk < exclusions2.size(); kk++) {
                if (exclusions2[j] == exclusions1[kk])hit++;
Mark Friedrichs's avatar
Mark Friedrichs committed
120
121
122
123
124
125
            }
            ASSERT_EQUAL(hit, 1);
        }
    }
}

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
void testSerializeTypes() {
    // Create a Force that specifies parameters by type.

    AmoebaVdwForce force1;
    force1.setPotentialFunction(AmoebaVdwForce::LennardJones);

    force1.addParticle(0, 2, 1.0, false);
    force1.addParticle(1, 2, 0.9, true);
    force1.addParticle(2, 0, 1.0, false);
    force1.addParticle(3, 1, 0.9, false);
    force1.addParticleType(1.1, 2.0);
    force1.addParticleType(1.2, 2.1);
    force1.addParticleType(1.3, 2.2);
    force1.addTypePair(0, 2, 1.5, 2.5);

    // Serialize and then deserialize it.

    stringstream buffer;
    XmlSerializer::serialize<AmoebaVdwForce>(&force1, "Force", buffer);
    AmoebaVdwForce* copy = XmlSerializer::deserialize<AmoebaVdwForce>(buffer);

    // Compare the two forces to see if they are identical.  
    AmoebaVdwForce& force2 = *copy;

    ASSERT_EQUAL(force1.getPotentialFunction(), force2.getPotentialFunction());
    ASSERT_EQUAL(force1.getNumParticles(),      force2.getNumParticles());
    ASSERT_EQUAL(force1.getNumParticleTypes(),  force2.getNumParticleTypes());
    ASSERT_EQUAL(force1.getNumTypePairs(),      force2.getNumTypePairs());

    for (int i = 0; i < force1.getNumParticles(); i++) {
        int ivIndex1, type1;
        int ivIndex2, type2;

        double sigma1, epsilon1, reductionFactor1;
        double sigma2, epsilon2, reductionFactor2;

        bool isAlchemical1;
        bool isAlchemical2;

        force1.getParticleParameters(i, ivIndex1, sigma1, epsilon1, reductionFactor1, isAlchemical1, type1);
        force2.getParticleParameters(i, ivIndex2, sigma2, epsilon2, reductionFactor2, isAlchemical2, type2);

        ASSERT_EQUAL(ivIndex1,          ivIndex2);
        ASSERT_EQUAL(sigma1,            sigma2);
        ASSERT_EQUAL(epsilon1,          epsilon2);
        ASSERT_EQUAL(reductionFactor1,  reductionFactor2);
        ASSERT_EQUAL(isAlchemical1,     isAlchemical2);
        ASSERT_EQUAL(type1,             type2);
    }
    for (int i = 0; i < force1.getNumParticleTypes(); i++) {
        double sigma1, epsilon1;
        double sigma2, epsilon2;
        force1.getParticleTypeParameters(i, sigma1, epsilon1);
        force2.getParticleTypeParameters(i, sigma2, epsilon2);
        ASSERT_EQUAL(sigma1, sigma2);
        ASSERT_EQUAL(epsilon1, epsilon2);
    }
    for (int i = 0; i < force1.getNumTypePairs(); i++) {
        int type11, type21;
        int type12, type22;
        double sigma1, epsilon1;
        double sigma2, epsilon2;
        force1.getTypePairParameters(i, type11, type21, sigma1, epsilon1);
        force2.getTypePairParameters(i, type12, type22, sigma2, epsilon2);
        ASSERT_EQUAL(type11, type12);
        ASSERT_EQUAL(type21, type22);
        ASSERT_EQUAL(sigma1, sigma2);
        ASSERT_EQUAL(epsilon1, epsilon2);
    }
}

Mark Friedrichs's avatar
Mark Friedrichs committed
197
198
int main() {
    try {
199
        registerAmoebaSerializationProxies();
Mark Friedrichs's avatar
Mark Friedrichs committed
200
        testSerialization();
201
        testSerializeTypes();
Mark Friedrichs's avatar
Mark Friedrichs committed
202
203
204
205
206
207
208
209
210
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}