"wrappers/python/vscode:/vscode.git/clone" did not exist on "598fdaa20c3482293fd8bfd30a70b3994ae1f046"
TestSerializeAmoebaVdwForce.cpp 5.48 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
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
/* -------------------------------------------------------------------------- *
 *                                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.                                     *
 * -------------------------------------------------------------------------- */

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
49
50
    force1.setSigmaCombiningRule("GEOMETRIC");
    force1.setEpsilonCombiningRule("GEOMETRIC");
    force1.setCutoff(0.9);
51
    force1.setNonbondedMethod(AmoebaVdwForce::CutoffPeriodic);
52

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

    // Serialize and then deserialize it.

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

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

    ASSERT_EQUAL(force1.getSigmaCombiningRule(),    force2.getSigmaCombiningRule());
    ASSERT_EQUAL(force1.getEpsilonCombiningRule(),  force2.getEpsilonCombiningRule());
    ASSERT_EQUAL(force1.getCutoff(),                force2.getCutoff());
76
    ASSERT_EQUAL(force1.getNonbondedMethod(),       force2.getNonbondedMethod());
77
78
79

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

80
    for (unsigned int ii = 0; ii < static_cast<unsigned int>(force1.getNumParticles()); ii++) {
81

82
83
        int ivIndex1;
        int ivIndex2;
84

Mark Friedrichs's avatar
Mark Friedrichs committed
85
86
        double sigma1, epsilon1, reductionFactor1;
        double sigma2, epsilon2, reductionFactor2;
87

88
89
        force1.getParticleParameters(ii, ivIndex1, sigma1, epsilon1, reductionFactor1);
        force2.getParticleParameters(ii, ivIndex2, sigma2, epsilon2, reductionFactor2);
90

91
        ASSERT_EQUAL(ivIndex1,          ivIndex2);
92
93
94
        ASSERT_EQUAL(sigma1,            sigma2);
        ASSERT_EQUAL(epsilon1,          epsilon2);
        ASSERT_EQUAL(reductionFactor1,  reductionFactor2);
Mark Friedrichs's avatar
Mark Friedrichs committed
95
    }
96
    for (unsigned int ii = 0; ii < static_cast<unsigned int>(force1.getNumParticles()); ii++) {
97

Mark Friedrichs's avatar
Mark Friedrichs committed
98
99
        std::vector< int > exclusions1;
        std::vector< int > exclusions2;
100

101
102
        force1.getParticleExclusions(ii, exclusions1);
        force2.getParticleExclusions(ii, exclusions2);
103

Mark Friedrichs's avatar
Mark Friedrichs committed
104
        ASSERT_EQUAL(exclusions1.size(), exclusions2.size());
105
        for (unsigned int jj = 0; jj < exclusions1.size(); jj++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
106
            int hit = 0;
107
            for (unsigned int kk = 0; kk < exclusions2.size(); kk++) {
108
                if (exclusions2[jj] == exclusions1[kk])hit++;
Mark Friedrichs's avatar
Mark Friedrichs committed
109
110
111
112
113
114
115
116
            }
            ASSERT_EQUAL(hit, 1);
        }
    }
}

int main() {
    try {
117
        registerAmoebaSerializationProxies();
Mark Friedrichs's avatar
Mark Friedrichs committed
118
119
120
121
122
123
124
125
126
127
        testSerialization();
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}