"vscode:/vscode.git/clone" did not exist on "9f717609213a36aa350a43f352786acf892a49b0"
TestCustomCentroidBondForce.h 11.2 KB
Newer Older
Peter Eastman's avatar
Peter Eastman 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * 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) 2015 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 "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h"
#include "openmm/CustomCentroidBondForce.h"
#include "openmm/CustomCompoundBondForce.h"
#include "openmm/System.h"
#include "openmm/TabulatedFunction.h"
#include "openmm/VerletIntegrator.h"
#include "sfmt/SFMT.h"
#include <iostream>
#include <vector>

using namespace OpenMM;
using namespace std;

const double TOL = 1e-5;

void testHarmonicBond() {
    System system;
    system.addParticle(1.0);
    system.addParticle(2.0);
    system.addParticle(3.0);
    system.addParticle(4.0);
    system.addParticle(5.0);
    CustomCentroidBondForce* force = new CustomCentroidBondForce(2, "k*distance(g1,g2)^2");
    force->addPerBondParameter("k");
    vector<int> particles1;
    particles1.push_back(0);
    particles1.push_back(1);
    vector<int> particles2;
    particles2.push_back(2);
    particles2.push_back(3);
    particles2.push_back(4);
    force->addGroup(particles1);
    force->addGroup(particles2);
    vector<int> groups;
    groups.push_back(0);
    groups.push_back(1);
    vector<double> parameters;
    parameters.push_back(1.0);
    force->addBond(groups, parameters);
    system.addForce(force);
    ASSERT(!system.usesPeriodicBoundaryConditions());

    // The center of mass of group 0 is (1.5, 0, 0).

    vector<Vec3> positions(5);
    positions[0] = Vec3(2.5, 0, 0);
    positions[1] = Vec3(1, 0, 0);

    // The center of mass of group 1 is (-1, 0, 0).

    positions[2] = Vec3(-6, 0, 0);
    positions[3] = Vec3(-1, 0, 0);
    positions[4] = Vec3(2, 0, 0);

    // Check the forces and energy.

    VerletIntegrator integrator(0.01);
    Context context(system, integrator, platform);
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    ASSERT_EQUAL_TOL(2.5*2.5, state.getPotentialEnergy(), TOL);
    ASSERT_EQUAL_VEC(Vec3(-2*2.5*(1.0/3.0), 0, 0), state.getForces()[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(-2*2.5*(2.0/3.0), 0, 0), state.getForces()[1], TOL);
    ASSERT_EQUAL_VEC(Vec3(2*2.5*(3.0/12.0), 0, 0), state.getForces()[2], TOL);
    ASSERT_EQUAL_VEC(Vec3(2*2.5*(4.0/12.0), 0, 0), state.getForces()[3], TOL);
    ASSERT_EQUAL_VEC(Vec3(2*2.5*(5.0/12.0), 0, 0), state.getForces()[4], TOL);

    // Update the per-bond parameter and see if the results change.

    parameters[0] = 2.0;
    force->setBondParameters(0, groups, parameters);
    force->updateParametersInContext(context);
    state = context.getState(State::Forces | State::Energy);
    ASSERT_EQUAL_TOL(2*2.5*2.5, state.getPotentialEnergy(), TOL);
    ASSERT_EQUAL_VEC(Vec3(-4*2.5*(1.0/3.0), 0, 0), state.getForces()[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(-4*2.5*(2.0/3.0), 0, 0), state.getForces()[1], TOL);
    ASSERT_EQUAL_VEC(Vec3(4*2.5*(3.0/12.0), 0, 0), state.getForces()[2], TOL);
    ASSERT_EQUAL_VEC(Vec3(4*2.5*(4.0/12.0), 0, 0), state.getForces()[3], TOL);
    ASSERT_EQUAL_VEC(Vec3(4*2.5*(5.0/12.0), 0, 0), state.getForces()[4], TOL);
    
    // All the particles should be treated as a single molecule.
    
    vector<std::vector<int> > molecules = context.getMolecules();
    ASSERT_EQUAL(1, molecules.size());
    ASSERT_EQUAL(5, molecules[0].size());
}

void testComplexFunction() {
    int numParticles = 5;
    System system;
    for (int i = 0; i < numParticles; i++)
        system.addParticle(2.0);
    vector<double> table(20);
    for (int i = 0; i < 20; i++)
        table[i] = sin(0.11*i);

    // When every group contains only one particle, a CustomCentroidBondForce is identical to a
    // CustomCompoundBondForce.  Use that to test a complicated energy function with lots of terms.

    CustomCompoundBondForce* compound = new CustomCompoundBondForce(4, "x1+y2+z4+fn(distance(p1,p2))*angle(p3,p2,p4)+scale*dihedral(p2,p1,p4,p3)");
    CustomCentroidBondForce* centroid = new CustomCentroidBondForce(4, "x1+y2+z4+fn(distance(g1,g2))*angle(g3,g2,g4)+scale*dihedral(g2,g1,g4,g3)");
    compound->addGlobalParameter("scale", 0.5);
    centroid->addGlobalParameter("scale", 0.5);
    compound->addTabulatedFunction("fn", new Continuous1DFunction(table, -1, 10));
    centroid->addTabulatedFunction("fn", new Continuous1DFunction(table, -1, 10));

    // Add two bonds to the CustomCompoundBondForce.

    vector<int> particles(4);
    vector<double> parameters;
    particles[0] = 0;
    particles[1] = 1;
    particles[2] = 2;
    particles[3] = 3;
    compound->addBond(particles, parameters);
    particles[0] = 2;
    particles[1] = 4;
    particles[2] = 3;
    particles[3] = 1;
    compound->addBond(particles, parameters);

    // Add identical bonds to the CustomCentroidBondForce.  As a stronger test, make sure that
    // group number is different from particle number.

    vector<int> groupMembers(1);
    groupMembers[0] = 3;
    centroid->addGroup(groupMembers);
    groupMembers[0] = 0;
    centroid->addGroup(groupMembers);
    groupMembers[0] = 1;
    centroid->addGroup(groupMembers);
    groupMembers[0] = 2;
    centroid->addGroup(groupMembers);
    groupMembers[0] = 4;
    centroid->addGroup(groupMembers);
    vector<int> groups(4);
    groups[0] = 1;
    groups[1] = 2;
    groups[2] = 3;
    groups[3] = 0;
    centroid->addBond(groups, parameters);
    groups[0] = 3;
    groups[1] = 4;
    groups[2] = 0;
    groups[3] = 2;
    centroid->addBond(groups, parameters);

    // Add both forces as different force groups, and create a context.

    centroid->setForceGroup(1);
    system.addForce(compound);
    system.addForce(centroid);
    VerletIntegrator integrator(0.01);
    Context context(system, integrator, platform);

    // Evaluate the force and energy for various positions and see if they match.

    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);
    vector<Vec3> positions(numParticles);
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < numParticles; j++)
            positions[j] = Vec3(5.0*genrand_real2(sfmt), 5.0*genrand_real2(sfmt), 5.0*genrand_real2(sfmt));
        context.setPositions(positions);
        State state1 = context.getState(State::Forces | State::Energy, false, 1<<0);
        State state2 = context.getState(State::Forces | State::Energy, false, 1<<1);
        ASSERT_EQUAL_TOL(state1.getPotentialEnergy(), state2.getPotentialEnergy(), TOL);
        for (int i = 0; i < numParticles; i++)
            ASSERT_EQUAL_VEC(state1.getForces()[i], state2.getForces()[i], TOL);
    }
}

void testCustomWeights() {
    System system;
    system.addParticle(1.0);
    system.addParticle(2.0);
    system.addParticle(3.0);
    system.addParticle(4.0);
    CustomCentroidBondForce* force = new CustomCentroidBondForce(2, "distance(g1,g2)^2");
    vector<int> particles(2);
    vector<double> weights(2);
    particles[0] = 0;
    particles[1] = 1;
    weights[0] = 0.5;
    weights[1] = 1.5;
    force->addGroup(particles, weights);
    particles[0] = 2;
    particles[1] = 3;
    weights[0] = 2.0;
    weights[1] = 1.0;
    force->addGroup(particles, weights);
    vector<int> groups;
    groups.push_back(0);
    groups.push_back(1);
    vector<double> parameters;
    force->addBond(groups, parameters);
    system.addForce(force);

    // The center of mass of group 0 is (0, 1, 0).

    vector<Vec3> positions(4);
    positions[0] = Vec3(0, 4, 0);
    positions[1] = Vec3(0, 0, 0);

    // The center of mass of group 1 is (0, 10, 0).

    positions[2] = Vec3(0, 9, 0);
    positions[3] = Vec3(0, 12, 0);

    // Check the forces and energy.

    VerletIntegrator integrator(0.01);
    Context context(system, integrator, platform);
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    ASSERT_EQUAL_TOL(9.0*9.0, state.getPotentialEnergy(), TOL);
    ASSERT_EQUAL_VEC(Vec3(0, 2*9*(0.5/2.0), 0), state.getForces()[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(0, 2*9*(1.5/2.0), 0), state.getForces()[1], TOL);
    ASSERT_EQUAL_VEC(Vec3(0, -2*9*(2.0/3.0), 0), state.getForces()[2], TOL);
    ASSERT_EQUAL_VEC(Vec3(0, -2*9*(1.0/3.0), 0), state.getForces()[3], TOL);
}

255
256
257
258
259
260
261
262
263
264
265
266
void testIllegalVariable() {
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
    CustomCentroidBondForce* force = new CustomCentroidBondForce(2, "1+none");
    vector<int> particles;
    particles.push_back(0);
    force->addGroup(particles);
    force->addGroup(particles);
    vector<int> groups;
    groups.push_back(0);
    groups.push_back(1);
267
    force->addBond(groups);
268
269
270
271
272
273
274
275
276
277
278
279
    system.addForce(force);
    VerletIntegrator integrator(0.001);
    bool threwException = false;
    try {
        Context(system, integrator, platform);
    }
    catch (const exception& e) {
        threwException = true;
    }
    ASSERT(threwException);
}

Peter Eastman's avatar
Peter Eastman committed
280
281
282
283
284
285
286
287
void runPlatformTests();

int main(int argc, char* argv[]) {
    try {
        initializeTests(argc, argv);
        testHarmonicBond();
        testComplexFunction();
        testCustomWeights();
288
        testIllegalVariable();
Peter Eastman's avatar
Peter Eastman committed
289
290
291
292
293
294
295
296
297
        runPlatformTests();
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}