TestSerializeAmoebaVdwForce.cpp 9.05 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
    force1.setName("custom name");
50
51
52
    force1.setSigmaCombiningRule("GEOMETRIC");
    force1.setEpsilonCombiningRule("GEOMETRIC");
    force1.setCutoff(0.9);
53
    force1.setNonbondedMethod(AmoebaVdwForce::CutoffPeriodic);
54
    force1.setAlchemicalMethod(AmoebaVdwForce::None);
55
    force1.setPotentialFunction(AmoebaVdwForce::Buffered147);
56

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

    // Serialize and then deserialize it.

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

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

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

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

88
    for (int i = 0; i < force1.getNumParticles(); i++) {
89

90
91
        int ivIndex1, type1;
        int ivIndex2, type2;
92

93
94
        double sigma1, epsilon1, reductionFactor1, scaleFactor1;
        double sigma2, epsilon2, reductionFactor2, scaleFactor2;
95

96
97
98
        bool isAlchemical1;
        bool isAlchemical2;

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
112
113
        std::vector< int > exclusions1;
        std::vector< int > exclusions2;
114

115
116
        force1.getParticleExclusions(i, exclusions1);
        force2.getParticleExclusions(i, exclusions2);
117

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

129
130
131
132
133
134
void testSerializeTypes() {
    // Create a Force that specifies parameters by type.

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

135
136
137
138
    force1.addParticle(0, 2, 1.0, false, 1.0);
    force1.addParticle(1, 2, 0.9, true, 0.0);
    force1.addParticle(2, 0, 1.0, false, 0.5);
    force1.addParticle(3, 1, 0.9, false, 0.9);
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    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;

162
163
        double sigma1, epsilon1, reductionFactor1, scaleFactor1;
        double sigma2, epsilon2, reductionFactor2, scaleFactor2;
164
165
166
167

        bool isAlchemical1;
        bool isAlchemical2;

168
169
        force1.getParticleParameters(i, ivIndex1, sigma1, epsilon1, reductionFactor1, isAlchemical1, type1, scaleFactor1);
        force2.getParticleParameters(i, ivIndex2, sigma2, epsilon2, reductionFactor2, isAlchemical2, type2, scaleFactor2);
170
171
172
173
174
175
176

        ASSERT_EQUAL(ivIndex1,          ivIndex2);
        ASSERT_EQUAL(sigma1,            sigma2);
        ASSERT_EQUAL(epsilon1,          epsilon2);
        ASSERT_EQUAL(reductionFactor1,  reductionFactor2);
        ASSERT_EQUAL(isAlchemical1,     isAlchemical2);
        ASSERT_EQUAL(type1,             type2);
177
        ASSERT_EQUAL(scaleFactor1,      scaleFactor2);
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    }
    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
201
202
int main() {
    try {
203
        registerAmoebaSerializationProxies();
Mark Friedrichs's avatar
Mark Friedrichs committed
204
        testSerialization();
205
        testSerializeTypes();
Mark Friedrichs's avatar
Mark Friedrichs committed
206
207
208
209
210
211
212
213
214
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}