TestCustomCentroidBondForce.h 14.8 KB
Newer Older
Peter Eastman's avatar
Peter Eastman committed
1
2
3
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
Evan Pretti's avatar
Evan Pretti committed
4
5
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
Peter Eastman's avatar
Peter Eastman committed
6
 *                                                                            *
7
 * Portions copyright (c) 2015-2021 Stanford University and the Authors.      *
Peter Eastman's avatar
Peter Eastman committed
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
 * 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");
Peter Eastman's avatar
Peter Eastman committed
55
56
57
    force->addGroup({0, 1});
    force->addGroup({2, 3, 4});
    force->addBond({0, 1}, {1.0});
Peter Eastman's avatar
Peter Eastman committed
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
    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.

Peter Eastman's avatar
Peter Eastman committed
88
    force->setBondParameters(0, {0, 1}, {2.0});
Peter Eastman's avatar
Peter Eastman committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    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());
}

105
void testComplexFunction(bool byGroups) {
Peter Eastman's avatar
Peter Eastman committed
106
107
108
109
110
111
112
113
114
115
116
117
    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)");
118
119
120
121
122
123
    string expression;
    if (byGroups)
        expression = "x1+y2+z4+fn(distance(g1,g2))*angle(g3,g2,g4)+scale*dihedral(g2,g1,g4,g3)";
    else
        expression = "x1+y2+z4+fn(pointdistance(x1,y1,z1,x2,y2,z2))*pointangle(x3,y3,z3,x2,y2,z2,x4,y4,z4)+scale*pointdihedral(x2,y2,z2,x1,y1,z1,x4,y4,z4,x3,y3,z3)";
    CustomCentroidBondForce* centroid = new CustomCentroidBondForce(4, expression);
Peter Eastman's avatar
Peter Eastman committed
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
    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);
    }
193
194
195
196
197
198
199
200
201
202
203
204

    // Try updating the tabulated function.

    for (int i = 0; i < table.size(); i++)
        table[i] *= 0.5;
    dynamic_cast<Continuous1DFunction&>(compound->getTabulatedFunction(0)).setFunctionParameters(table, -1, 10);
    dynamic_cast<Continuous1DFunction&>(centroid->getTabulatedFunction(0)).setFunctionParameters(table, -1, 10);
    compound->updateParametersInContext(context);
    centroid->updateParametersInContext(context);
    State state1 = context.getState(State::Energy, false, 1<<0);
    State state2 = context.getState(State::Energy, false, 1<<1);
    ASSERT_EQUAL_TOL(state1.getPotentialEnergy(), state2.getPotentialEnergy(), TOL);
Peter Eastman's avatar
Peter Eastman committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
}

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);
Peter Eastman's avatar
Peter Eastman committed
226
    force->addBond({0, 1});
Peter Eastman's avatar
Peter Eastman committed
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
    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);
}

253
254
255
256
257
void testIllegalVariable() {
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
    CustomCentroidBondForce* force = new CustomCentroidBondForce(2, "1+none");
Peter Eastman's avatar
Peter Eastman committed
258
259
260
    force->addGroup({0});
    force->addGroup({0});
    force->addBond({0, 1});
261
262
263
264
265
266
267
268
269
270
271
272
    system.addForce(force);
    VerletIntegrator integrator(0.001);
    bool threwException = false;
    try {
        Context(system, integrator, platform);
    }
    catch (const exception& e) {
        threwException = true;
    }
    ASSERT(threwException);
}

273
void testPeriodic(bool byGroups) {
274
275
276
277
278
279
280
281
282
    // Create a force that uses periodic boundary conditions.
    
    System system;
    system.addParticle(1.0);
    system.addParticle(2.0);
    system.addParticle(3.0);
    system.addParticle(4.0);
    system.addParticle(5.0);
    system.setDefaultPeriodicBoxVectors(Vec3(2, 0, 0), Vec3(0, 3, 0), Vec3(0, 0, 3));
283
284
285
286
287
288
    string expression;
    if (byGroups)
        expression = "k*distance(g1,g2)^2";
    else
        expression = "k*pointdistance(x1,y1,z1,x2,y2,z2)^2";
    CustomCentroidBondForce* force = new CustomCentroidBondForce(2, expression);
289
    force->addPerBondParameter("k");
Peter Eastman's avatar
Peter Eastman committed
290
291
292
    force->addGroup({0, 1});
    force->addGroup({2, 3, 4});
    force->addBond({0, 1}, {1.0});
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    force->setUsesPeriodicBoundaryConditions(true);
    system.addForce(force);

    // 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(0.5*0.5, state.getPotentialEnergy(), TOL);
    ASSERT_EQUAL_VEC(Vec3(-2*0.5*(1.0/3.0), 0, 0), state.getForces()[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(-2*0.5*(2.0/3.0), 0, 0), state.getForces()[1], TOL);
    ASSERT_EQUAL_VEC(Vec3(2*0.5*(3.0/12.0), 0, 0), state.getForces()[2], TOL);
    ASSERT_EQUAL_VEC(Vec3(2*0.5*(4.0/12.0), 0, 0), state.getForces()[3], TOL);
    ASSERT_EQUAL_VEC(Vec3(2*0.5*(5.0/12.0), 0, 0), state.getForces()[4], TOL);
}

322
323
324
325
326
327
328
329
330
331
332
333
void testEnergyParameterDerivatives() {
    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)-r0)^2");
    force->addGlobalParameter("r0", 0.0);
    force->addGlobalParameter("k", 0.0);
    force->addEnergyParameterDerivative("r0");
    force->addEnergyParameterDerivative("k");
Peter Eastman's avatar
Peter Eastman committed
334
335
336
    force->addGroup({0, 1});
    force->addGroup({2, 3, 4});
    force->addBond({0, 1});
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
    system.addForce(force);

    // 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 derivatives.

    VerletIntegrator integrator(0.01);
    Context context(system, integrator, platform);
    context.setPositions(positions);
    for (int i = 0; i < 10; i++) {
        double r0 = 0.1*i;
        double k = 10-i;
        context.setParameter("r0", r0);
        context.setParameter("k", k);
        State state = context.getState(State::ParameterDerivatives);
        map<string, double> derivs = state.getEnergyParameterDerivatives();
        double dEdr0 = -2*k*(2.5-r0);
        double dEdk = (2.5-r0)*(2.5-r0);
        ASSERT_EQUAL_TOL(dEdr0, derivs["r0"], 1e-5);
        ASSERT_EQUAL_TOL(dEdk, derivs["k"], 1e-5);
    }
}

Peter Eastman's avatar
Peter Eastman committed
370
371
372
373
374
375
void runPlatformTests();

int main(int argc, char* argv[]) {
    try {
        initializeTests(argc, argv);
        testHarmonicBond();
376
377
        testComplexFunction(true);
        testComplexFunction(false);
Peter Eastman's avatar
Peter Eastman committed
378
        testCustomWeights();
379
        testIllegalVariable();
380
381
        testPeriodic(true);
        testPeriodic(false);
382
        testEnergyParameterDerivatives();
Peter Eastman's avatar
Peter Eastman committed
383
384
385
386
387
388
389
390
391
        runPlatformTests();
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}