"vscode:/vscode.git/clone" did not exist on "c28ae9037216fc35149cdc0167bd5c1d069e7c81"
TestCudaNonbondedForce.cpp 29.8 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
9
 * Portions copyright (c) 2008-2010 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * 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.                                     *
 * -------------------------------------------------------------------------- */

/**
33
 * This tests all the different force terms in the reference implementation of NonbondedForce.
34
35
36
 */

#include "../../../tests/AssertionUtilities.h"
37
#include "openmm/Context.h"
38
#include "CudaPlatform.h"
39
#include "ReferencePlatform.h"
40
41
42
43
44
#include "openmm/HarmonicBondForce.h"
#include "openmm/NonbondedForce.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include "openmm/VerletIntegrator.h"
45
#include "openmm/internal/ContextImpl.h"
46
#include "kernels/gputypes.h"
47
#include "../src/SimTKUtilities/SimTKOpenMMRealType.h"
48
#include "sfmt/SFMT.h"
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>

using namespace OpenMM;
using namespace std;

const double TOL = 1e-5;

void testCoulomb() {
    CudaPlatform platform;
59
60
61
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
62
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
63
64
65
    NonbondedForce* forceField = new NonbondedForce();
    forceField->addParticle(0.5, 1, 0);
    forceField->addParticle(-1.5, 1, 0);
66
    system.addForce(forceField);
67
    Context context(system, integrator, platform);
68
69
70
71
72
73
    vector<Vec3> positions(2);
    positions[0] = Vec3(0, 0, 0);
    positions[1] = Vec3(2, 0, 0);
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();
74
    double force = ONE_4PI_EPS0*(-0.75)/4.0;
75
76
    ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[1], TOL);
77
    ASSERT_EQUAL_TOL(ONE_4PI_EPS0*(-0.75)/2.0, state.getPotentialEnergy(), TOL);
78
79
80
81
}

void testLJ() {
    CudaPlatform platform;
82
83
84
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
85
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
86
87
88
    NonbondedForce* forceField = new NonbondedForce();
    forceField->addParticle(0, 1.2, 1);
    forceField->addParticle(0, 1.4, 2);
89
    system.addForce(forceField);
90
    Context context(system, integrator, platform);
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    vector<Vec3> positions(2);
    positions[0] = Vec3(0, 0, 0);
    positions[1] = Vec3(2, 0, 0);
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();
    double x = 1.3/2.0;
    double eps = SQRT_TWO;
    double force = 4.0*eps*(12*std::pow(x, 12.0)-6*std::pow(x, 6.0))/2.0;
    ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[1], TOL);
    ASSERT_EQUAL_TOL(4.0*eps*(std::pow(x, 12.0)-std::pow(x, 6.0)), state.getPotentialEnergy(), TOL);
}

void testExclusionsAnd14() {
    CudaPlatform platform;
107
    System system;
108
    NonbondedForce* nonbonded = new NonbondedForce();
109
110
    for (int i = 0; i < 5; ++i) {
        system.addParticle(1.0);
111
        nonbonded->addParticle(0, 1.5, 0);
112
    }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    vector<pair<int, int> > bonds;
    bonds.push_back(pair<int, int>(0, 1));
    bonds.push_back(pair<int, int>(1, 2));
    bonds.push_back(pair<int, int>(2, 3));
    bonds.push_back(pair<int, int>(3, 4));
    nonbonded->createExceptionsFromBonds(bonds, 0.0, 0.0);
    int first14, second14;
    for (int i = 0; i < nonbonded->getNumExceptions(); i++) {
        int particle1, particle2;
        double chargeProd, sigma, epsilon;
        nonbonded->getExceptionParameters(i, particle1, particle2, chargeProd, sigma, epsilon);
        if ((particle1 == 0 && particle2 == 3) || (particle1 == 3 && particle2 == 0))
            first14 = i;
        if ((particle1 == 1 && particle2 == 4) || (particle1 == 4 && particle2 == 1))
            second14 = i;
    }
129
    system.addForce(nonbonded);
130
131
132
    for (int i = 1; i < 5; ++i) {
 
        // Test LJ forces
133

134
135
136
        vector<Vec3> positions(5);
        const double r = 1.0;
        for (int j = 0; j < 5; ++j) {
Peter Eastman's avatar
Peter Eastman committed
137
            nonbonded->setParticleParameters(j, 0, 1.5, 0);
138
139
            positions[j] = Vec3(0, j, 0);
        }
Peter Eastman's avatar
Peter Eastman committed
140
141
        nonbonded->setParticleParameters(0, 0, 1.5, 1);
        nonbonded->setParticleParameters(i, 0, 1.5, 1);
142
143
        nonbonded->setExceptionParameters(first14, 0, 3, 0, 1.5, i == 3 ? 0.5 : 0.0);
        nonbonded->setExceptionParameters(second14, 1, 4, 0, 1.5, 0.0);
144
        positions[i] = Vec3(r, 0, 0);
145
146
147
        // The following is in its own block, because CUDA can't deal with multiple Contexts
        // existing on the same thread at the same time.
        {
148
            LangevinIntegrator integrator(0.0, 0.1, 0.01);
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
            Context context(system, integrator, platform);
            context.setPositions(positions);
            State state = context.getState(State::Forces | State::Energy);
            const vector<Vec3>& forces = state.getForces();
            double x = 1.5/r;
            double eps = 1.0;
            double force = 4.0*eps*(12*std::pow(x, 12.0)-6*std::pow(x, 6.0))/r;
            double energy = 4.0*eps*(std::pow(x, 12.0)-std::pow(x, 6.0));
            if (i == 3) {
                force *= 0.5;
                energy *= 0.5;
            }
            if (i < 3) {
                force = 0;
                energy = 0;
            }
            ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], TOL);
            ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[i], TOL);
            ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), TOL);
168
169
170
        }

        // Test Coulomb forces
171
172
173
174
175
176

        {
            nonbonded->setParticleParameters(0, 2, 1.5, 0);
            nonbonded->setParticleParameters(i, 2, 1.5, 0);
            nonbonded->setExceptionParameters(first14, 0, 3, i == 3 ? 4/1.2 : 0, 1.5, 0);
            nonbonded->setExceptionParameters(second14, 1, 4, 0, 1.5, 0);
177
            LangevinIntegrator integrator(0.0, 0.1, 0.01);
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
            Context context(system, integrator, platform);
            context.setPositions(positions);
            State state = context.getState(State::Forces | State::Energy);
            const vector<Vec3>& forces2 = state.getForces();
            double force = ONE_4PI_EPS0*4/(r*r);
            double energy = ONE_4PI_EPS0*4/r;
            if (i == 3) {
                force /= 1.2;
                energy /= 1.2;
            }
            if (i < 3) {
                force = 0;
                energy = 0;
            }
            ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces2[0], TOL);
            ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces2[i], TOL);
            ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), TOL);
195
196
197
198
199
200
        }
    }
}

void testCutoff() {
    CudaPlatform platform;
201
202
203
204
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
    system.addParticle(1.0);
205
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
206
207
208
209
    NonbondedForce* forceField = new NonbondedForce();
    forceField->addParticle(1.0, 1, 0);
    forceField->addParticle(1.0, 1, 0);
    forceField->addParticle(1.0, 1, 0);
210
    forceField->setNonbondedMethod(NonbondedForce::CutoffNonPeriodic);
211
212
    const double cutoff = 2.9;
    forceField->setCutoffDistance(cutoff);
213
214
    const double eps = 50.0;
    forceField->setReactionFieldDielectric(eps);
215
    system.addForce(forceField);
216
    Context context(system, integrator, platform);
217
218
219
220
221
222
223
224
225
    vector<Vec3> positions(3);
    positions[0] = Vec3(0, 0, 0);
    positions[1] = Vec3(0, 2, 0);
    positions[2] = Vec3(0, 3, 0);
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();
    const double krf = (1.0/(cutoff*cutoff*cutoff))*(eps-1.0)/(2.0*eps+1.0);
    const double crf = (1.0/cutoff)*(3.0*eps)/(2.0*eps+1.0);
226
227
    const double force1 = ONE_4PI_EPS0*(1.0)*(0.25-2.0*krf*2.0);
    const double force2 = ONE_4PI_EPS0*(1.0)*(1.0-2.0*krf*1.0);
228
229
230
    ASSERT_EQUAL_VEC(Vec3(0, -force1, 0), forces[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(0, force1-force2, 0), forces[1], TOL);
    ASSERT_EQUAL_VEC(Vec3(0, force2, 0), forces[2], TOL);
231
232
    const double energy1 = ONE_4PI_EPS0*(1.0)*(0.5+krf*4.0-crf);
    const double energy2 = ONE_4PI_EPS0*(1.0)*(1.0+krf*1.0-crf);
233
234
235
236
237
    ASSERT_EQUAL_TOL(energy1+energy2, state.getPotentialEnergy(), TOL);
}

void testCutoff14() {
    CudaPlatform platform;
238
    System system;
239
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
240
    NonbondedForce* nonbonded = new NonbondedForce();
241
    nonbonded->setNonbondedMethod(NonbondedForce::CutoffNonPeriodic);
242
243
    for (int i = 0; i < 5; ++i) {
        system.addParticle(1.0);
244
        nonbonded->addParticle(0, 1.5, 0);
245
    }
246
    const double cutoff = 3.5;
247
    nonbonded->setCutoffDistance(cutoff);
248
249
    const double eps = 30.0;
    nonbonded->setReactionFieldDielectric(eps);
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
    vector<pair<int, int> > bonds;
    bonds.push_back(pair<int, int>(0, 1));
    bonds.push_back(pair<int, int>(1, 2));
    bonds.push_back(pair<int, int>(2, 3));
    bonds.push_back(pair<int, int>(3, 4));
    nonbonded->createExceptionsFromBonds(bonds, 0.0, 0.0);
    int first14, second14;
    for (int i = 0; i < nonbonded->getNumExceptions(); i++) {
        int particle1, particle2;
        double chargeProd, sigma, epsilon;
        nonbonded->getExceptionParameters(i, particle1, particle2, chargeProd, sigma, epsilon);
        if ((particle1 == 0 && particle2 == 3) || (particle1 == 3 && particle2 == 0))
            first14 = i;
        if ((particle1 == 1 && particle2 == 4) || (particle1 == 4 && particle2 == 1))
            second14 = i;
    }
266
    system.addForce(nonbonded);
267
    Context context(system, integrator, platform);
268
269
270
271
272
273
274
275
276
277
    vector<Vec3> positions(5);
    positions[0] = Vec3(0, 0, 0);
    positions[1] = Vec3(1, 0, 0);
    positions[2] = Vec3(2, 0, 0);
    positions[3] = Vec3(3, 0, 0);
    positions[4] = Vec3(4, 0, 0);
    for (int i = 1; i < 5; ++i) {
 
        // Test LJ forces
        
Peter Eastman's avatar
Peter Eastman committed
278
        nonbonded->setParticleParameters(0, 0, 1.5, 1);
279
        for (int j = 1; j < 5; ++j)
Peter Eastman's avatar
Peter Eastman committed
280
281
            nonbonded->setParticleParameters(j, 0, 1.5, 0);
        nonbonded->setParticleParameters(i, 0, 1.5, 1);
282
283
        nonbonded->setExceptionParameters(first14, 0, 3, 0, 1.5, i == 3 ? 0.5 : 0.0);
        nonbonded->setExceptionParameters(second14, 1, 4, 0, 1.5, 0.0);
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
        context.reinitialize();
        context.setPositions(positions);
        State state = context.getState(State::Forces | State::Energy);
        const vector<Vec3>& forces = state.getForces();
        double r = positions[i][0];
        double x = 1.5/r;
        double e = 1.0;
        double force = 4.0*e*(12*std::pow(x, 12.0)-6*std::pow(x, 6.0))/r;
        double energy = 4.0*e*(std::pow(x, 12.0)-std::pow(x, 6.0));
        if (i == 3) {
            force *= 0.5;
            energy *= 0.5;
        }
        if (i < 3 || r > cutoff) {
            force = 0;
            energy = 0;
        }
        ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], TOL);
        ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[i], TOL);
        ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), TOL);

        // Test Coulomb forces
        
        const double q = 0.7;
Peter Eastman's avatar
Peter Eastman committed
308
309
        nonbonded->setParticleParameters(0, q, 1.5, 0);
        nonbonded->setParticleParameters(i, q, 1.5, 0);
310
311
        nonbonded->setExceptionParameters(first14, 0, 3, i == 3 ? q*q/1.2 : 0, 1.5, 0);
        nonbonded->setExceptionParameters(second14, 1, 4, 0, 1.5, 0);
312
313
314
315
        context.reinitialize();
        context.setPositions(positions);
        state = context.getState(State::Forces | State::Energy);
        const vector<Vec3>& forces2 = state.getForces();
316
317
        force = ONE_4PI_EPS0*q*q/(r*r);
        energy = ONE_4PI_EPS0*q*q/r;
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
        if (i == 3) {
            force /= 1.2;
            energy /= 1.2;
        }
        if (i < 3 || r > cutoff) {
            force = 0;
            energy = 0;
        }
        ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces2[0], TOL);
        ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces2[i], TOL);
        ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), TOL);
    }
}

void testPeriodic() {
    CudaPlatform platform;
334
335
336
337
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
    system.addParticle(1.0);
338
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
339
340
341
342
343
    NonbondedForce* nonbonded = new NonbondedForce();
    nonbonded->addParticle(1.0, 1, 0);
    nonbonded->addParticle(1.0, 1, 0);
    nonbonded->addParticle(1.0, 1, 0);
    nonbonded->addException(0, 1, 0.0, 1.0, 0.0);
344
    nonbonded->setNonbondedMethod(NonbondedForce::CutoffPeriodic);
345
    const double cutoff = 2.0;
346
    nonbonded->setCutoffDistance(cutoff);
347
    system.setDefaultPeriodicBoxVectors(Vec3(4, 0, 0), Vec3(0, 4, 0), Vec3(0, 0, 4));
348
    system.addForce(nonbonded);
349
    Context context(system, integrator, platform);
350
351
352
353
354
355
356
357
358
359
    vector<Vec3> positions(3);
    positions[0] = Vec3(0, 0, 0);
    positions[1] = Vec3(2, 0, 0);
    positions[2] = Vec3(3, 0, 0);
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();
    const double eps = 78.3;
    const double krf = (1.0/(cutoff*cutoff*cutoff))*(eps-1.0)/(2.0*eps+1.0);
    const double crf = (1.0/cutoff)*(3.0*eps)/(2.0*eps+1.0);
360
    const double force = ONE_4PI_EPS0*(1.0)*(1.0-2.0*krf*1.0);
361
362
363
    ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[1], TOL);
    ASSERT_EQUAL_VEC(Vec3(0, 0, 0), forces[2], TOL);
364
    ASSERT_EQUAL_TOL(2*ONE_4PI_EPS0*(1.0)*(1.0+krf*1.0-crf), state.getPotentialEnergy(), TOL);
365
366
}

367

368
369
370
371
void testLargeSystem() {
    const int numMolecules = 600;
    const int numParticles = numMolecules*2;
    const double cutoff = 2.0;
372
    const double boxSize = 20.0;
373
    const double tol = 2e-3;
374
375
    CudaPlatform cuda;
    ReferencePlatform reference;
376
377
378
    System system;
    for (int i = 0; i < numParticles; i++)
        system.addParticle(1.0);
379
    NonbondedForce* nonbonded = new NonbondedForce();
380
    HarmonicBondForce* bonds = new HarmonicBondForce();
381
382
    vector<Vec3> positions(numParticles);
    vector<Vec3> velocities(numParticles);
383
384
385
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);

386
387
    for (int i = 0; i < numMolecules; i++) {
        if (i < numMolecules/2) {
388
            nonbonded->addParticle(-1.0, 0.2, 0.1);
389
            nonbonded->addParticle(1.0, 0.1, 0.1);
390
391
        }
        else {
392
            nonbonded->addParticle(-1.0, 0.2, 0.2);
393
            nonbonded->addParticle(1.0, 0.1, 0.2);
394
        }
395
        positions[2*i] = Vec3(boxSize*genrand_real2(sfmt), boxSize*genrand_real2(sfmt), boxSize*genrand_real2(sfmt));
396
        positions[2*i+1] = Vec3(positions[2*i][0]+1.0, positions[2*i][1], positions[2*i][2]);
397
398
        velocities[2*i] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
        velocities[2*i+1] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
399
        bonds->addBond(2*i, 2*i+1, 1.0, 0.1);
400
        nonbonded->addException(2*i, 2*i+1, 0.0, 0.15, 0.0);
401
402
403
404
405
406
407
408
409
    }

    // Try with cutoffs but not periodic boundary conditions, and make sure the Cuda and Reference
    // platforms agree.

    nonbonded->setNonbondedMethod(NonbondedForce::CutoffNonPeriodic);
    nonbonded->setCutoffDistance(cutoff);
    system.addForce(nonbonded);
    system.addForce(bonds);
410
411
412
413
    VerletIntegrator integrator1(0.01);
    VerletIntegrator integrator2(0.01);
    Context cudaContext(system, integrator1, cuda);
    Context referenceContext(system, integrator2, reference);
414
415
416
417
    cudaContext.setPositions(positions);
    cudaContext.setVelocities(velocities);
    referenceContext.setPositions(positions);
    referenceContext.setVelocities(velocities);
418
419
    State cudaState = cudaContext.getState(State::Positions | State::Velocities | State::Forces | State::Energy);
    State referenceState = referenceContext.getState(State::Positions | State::Velocities | State::Forces | State::Energy);
420
421
422
423
424
    for (int i = 0; i < numParticles; i++) {
        ASSERT_EQUAL_VEC(cudaState.getPositions()[i], referenceState.getPositions()[i], tol);
        ASSERT_EQUAL_VEC(cudaState.getVelocities()[i], referenceState.getVelocities()[i], tol);
        ASSERT_EQUAL_VEC(cudaState.getForces()[i], referenceState.getForces()[i], tol);
    }
425
    ASSERT_EQUAL_TOL(cudaState.getPotentialEnergy(), referenceState.getPotentialEnergy(), tol);
426
427
428
429

    // Now do the same thing with periodic boundary conditions.

    nonbonded->setNonbondedMethod(NonbondedForce::CutoffPeriodic);
430
    system.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0, 0), Vec3(0, boxSize, 0), Vec3(0, 0, boxSize));
431
432
433
434
435
436
    cudaContext.reinitialize();
    referenceContext.reinitialize();
    cudaContext.setPositions(positions);
    cudaContext.setVelocities(velocities);
    referenceContext.setPositions(positions);
    referenceContext.setVelocities(velocities);
437
438
    cudaState = cudaContext.getState(State::Positions | State::Velocities | State::Forces | State::Energy);
    referenceState = referenceContext.getState(State::Positions | State::Velocities | State::Forces | State::Energy);
439
    for (int i = 0; i < numParticles; i++) {
440
441
442
        ASSERT_EQUAL_TOL(fmod(cudaState.getPositions()[i][0]-referenceState.getPositions()[i][0], boxSize), 0, tol);
        ASSERT_EQUAL_TOL(fmod(cudaState.getPositions()[i][1]-referenceState.getPositions()[i][1], boxSize), 0, tol);
        ASSERT_EQUAL_TOL(fmod(cudaState.getPositions()[i][2]-referenceState.getPositions()[i][2], boxSize), 0, tol);
443
444
445
        ASSERT_EQUAL_VEC(cudaState.getVelocities()[i], referenceState.getVelocities()[i], tol);
        ASSERT_EQUAL_VEC(cudaState.getForces()[i], referenceState.getForces()[i], tol);
    }
446
    ASSERT_EQUAL_TOL(cudaState.getPotentialEnergy(), referenceState.getPotentialEnergy(), tol);
447
448
449
450
451
452
453
454
455
}

void testBlockInteractions(bool periodic) {
    const int blockSize = 32;
    const int numBlocks = 100;
    const int numParticles = blockSize*numBlocks;
    const double cutoff = 1.0;
    const double boxSize = (periodic ? 5.1 : 1.1);
    CudaPlatform cuda;
456
    System system;
457
    VerletIntegrator integrator(0.01);
458
    NonbondedForce* nonbonded = new NonbondedForce();
459
    vector<Vec3> positions(numParticles);
460
461
462
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);

463
    for (int i = 0; i < numParticles; i++) {
464
        system.addParticle(1.0);
465
        nonbonded->addParticle(1.0, 0.2, 0.2);
466
        positions[i] = Vec3(boxSize*(3*genrand_real2(sfmt)-1), boxSize*(3*genrand_real2(sfmt)-1), boxSize*(3*genrand_real2(sfmt)-1));
467
468
469
    }
    nonbonded->setNonbondedMethod(periodic ? NonbondedForce::CutoffPeriodic : NonbondedForce::CutoffNonPeriodic);
    nonbonded->setCutoffDistance(cutoff);
470
    system.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0, 0), Vec3(0, boxSize, 0), Vec3(0, 0, boxSize));
471
    system.addForce(nonbonded);
472
    Context context(system, integrator, cuda);
473
474
    context.setPositions(positions);
    State state = context.getState(State::Positions | State::Velocities | State::Forces);
475
    ContextImpl* contextImpl = *reinterpret_cast<ContextImpl**>(&context);
476
477
478
479
480
481
482
483
    CudaPlatform::PlatformData& data = *static_cast<CudaPlatform::PlatformData*>(contextImpl->getPlatformData());
    
    // Verify that the bounds of each block were calculated correctly.

    data.gpu->psPosq4->Download();
    data.gpu->psGridBoundingBox->Download();
    data.gpu->psGridCenter->Download();
    for (int i = 0; i < numBlocks; i++) {
484
485
        float4 gridSize = (*data.gpu->psGridBoundingBox)[i];
        float4 center = (*data.gpu->psGridCenter)[i];
486
487
488
489
490
491
492
        if (periodic) {
            ASSERT(gridSize.x < 0.5*boxSize);
            ASSERT(gridSize.y < 0.5*boxSize);
            ASSERT(gridSize.z < 0.5*boxSize);
        }
        float minx = 0.0, maxx = 0.0, miny = 0.0, maxy = 0.0, minz = 0.0, maxz = 0.0, radius = 0.0;
        for (int j = 0; j < blockSize; j++) {
493
            float4 pos = (*data.gpu->psPosq4)[i*blockSize+j];
494
495
496
497
            float dx = pos.x-center.x;
            float dy = pos.y-center.y;
            float dz = pos.z-center.z;
            if (periodic) {
498
499
500
                dx -= (float)(floor(0.5+dx/boxSize)*boxSize);
                dy -= (float)(floor(0.5+dy/boxSize)*boxSize);
                dz -= (float)(floor(0.5+dz/boxSize)*boxSize);
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
            }
            ASSERT(abs(dx) < gridSize.x+TOL);
            ASSERT(abs(dy) < gridSize.y+TOL);
            ASSERT(abs(dz) < gridSize.z+TOL);
            minx = min(minx, dx);
            maxx = max(maxx, dx);
            miny = min(miny, dy);
            maxy = max(maxy, dy);
            minz = min(minz, dz);
            maxz = max(maxz, dz);
        }
        ASSERT_EQUAL_TOL(-minx, gridSize.x, TOL);
        ASSERT_EQUAL_TOL(maxx, gridSize.x, TOL);
        ASSERT_EQUAL_TOL(-miny, gridSize.y, TOL);
        ASSERT_EQUAL_TOL(maxy, gridSize.y, TOL);
        ASSERT_EQUAL_TOL(-minz, gridSize.z, TOL);
        ASSERT_EQUAL_TOL(maxz, gridSize.z, TOL);
    }

    // Verify that interactions were identified correctly.

    data.gpu->psInteractionCount->Download();
523
    int numWithInteractions = (*data.gpu->psInteractionCount)[0];
524
525
526
527
528
529
530
    vector<bool> hasInteractions(data.gpu->sim.workUnits, false);
    data.gpu->psInteractingWorkUnit->Download();
    data.gpu->psInteractionFlag->Download();
    const unsigned int atoms = data.gpu->sim.paddedNumberOfAtoms;
    const unsigned int grid = data.gpu->grid;
    const unsigned int dim = (atoms+(grid-1))/grid;
    for (int i = 0; i < numWithInteractions; i++) {
531
        unsigned int workUnit = (*data.gpu->psInteractingWorkUnit)[i];
532
533
534
535
536
537
538
        unsigned int x = (workUnit >> 17);
        unsigned int y = ((workUnit >> 2) & 0x7fff);
        int tile = (x > y ? x+y*dim-y*(y+1)/2 : y+x*dim-x*(x+1)/2);
        hasInteractions[tile] = true;

        // Make sure this tile really should have been flagged based on bounding volumes.

539
540
541
542
        float4 gridSize1 = (*data.gpu->psGridBoundingBox)[x];
        float4 gridSize2 = (*data.gpu->psGridBoundingBox)[y];
        float4 center1 = (*data.gpu->psGridCenter)[x];
        float4 center2 = (*data.gpu->psGridCenter)[y];
543
544
545
546
        float dx = center1.x-center2.x;
        float dy = center1.y-center2.y;
        float dz = center1.z-center2.z;
        if (periodic) {
547
548
549
            dx -= (float)(floor(0.5+dx/boxSize)*boxSize);
            dy -= (float)(floor(0.5+dy/boxSize)*boxSize);
            dz -= (float)(floor(0.5+dz/boxSize)*boxSize);
550
551
552
553
554
        }
        dx = max(0.0f, abs(dx)-gridSize1.x-gridSize2.x);
        dy = max(0.0f, abs(dy)-gridSize1.y-gridSize2.y);
        dz = max(0.0f, abs(dz)-gridSize1.z-gridSize2.z);
        ASSERT(sqrt(dx*dx+dy*dy+dz*dz) < cutoff+TOL);
555
556
557

        // Check the interaction flags.

558
        unsigned int flags = (*data.gpu->psInteractionFlag)[i];
559
560
        for (int atom2 = 0; atom2 < 32; atom2++) {
            if ((flags & 1) == 0) {
561
                float4 pos2 = (*data.gpu->psPosq4)[y*blockSize+atom2];
562
                for (int atom1 = 0; atom1 < blockSize; ++atom1) {
563
                    float4 pos1 = (*data.gpu->psPosq4)[x*blockSize+atom1];
564
565
566
567
                    float dx = pos2.x-pos1.x;
                    float dy = pos2.y-pos1.y;
                    float dz = pos2.z-pos1.z;
                    if (periodic) {
568
569
570
                        dx -= (float)(floor(0.5+dx/boxSize)*boxSize);
                        dy -= (float)(floor(0.5+dy/boxSize)*boxSize);
                        dz -= (float)(floor(0.5+dz/boxSize)*boxSize);
571
572
573
574
575
576
                    }
                    ASSERT(dx*dx+dy*dy+dz*dz > cutoff*cutoff);
                }
            }
            flags >>= 1;
        }
577
578
579
580
581
    }

    // Check the tiles that did not have interactions to make sure all atoms are beyond the cutoff.

    data.gpu->psWorkUnit->Download();
582
    for (int i = 0; i < (int)hasInteractions.size(); i++)
583
        if (!hasInteractions[i]) {
584
            unsigned int workUnit = (*data.gpu->psWorkUnit)[i];
585
586
587
            unsigned int x = (workUnit >> 17);
            unsigned int y = ((workUnit >> 2) & 0x7fff);
            for (int atom1 = 0; atom1 < blockSize; ++atom1) {
588
                float4 pos1 = (*data.gpu->psPosq4)[x*blockSize+atom1];
589
                for (int atom2 = 0; atom2 < blockSize; ++atom2) {
590
                    float4 pos2 = (*data.gpu->psPosq4)[y*blockSize+atom2];
591
592
593
594
                    float dx = pos1.x-pos2.x;
                    float dy = pos1.y-pos2.y;
                    float dz = pos1.z-pos2.z;
                    if (periodic) {
595
596
597
                        dx -= (float)(floor(0.5+dx/boxSize)*boxSize);
                        dy -= (float)(floor(0.5+dy/boxSize)*boxSize);
                        dz -= (float)(floor(0.5+dz/boxSize)*boxSize);
598
599
600
601
602
603
604
                    }
                    ASSERT(dx*dx+dy*dy+dz*dz > cutoff*cutoff);
                }
            }
        }
}

605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
void testDispersionCorrection() {
    // Create a box full of identical particles.

    int gridSize = 5;
    int numParticles = gridSize*gridSize*gridSize;
    double boxSize = gridSize*0.5;
    double cutoff = boxSize/3;
    CudaPlatform platform;
    System system;
    VerletIntegrator integrator(0.01);
    NonbondedForce* nonbonded = new NonbondedForce();
    vector<Vec3> positions(numParticles);
    int index = 0;
    for (int i = 0; i < gridSize; i++)
        for (int j = 0; j < gridSize; j++)
            for (int k = 0; k < gridSize; k++) {
                system.addParticle(1.0);
                nonbonded->addParticle(0, 1.1, 0.5);
                positions[index] = Vec3(i*boxSize/gridSize, j*boxSize/gridSize, k*boxSize/gridSize);
                index++;
            }
    nonbonded->setNonbondedMethod(NonbondedForce::CutoffPeriodic);
    nonbonded->setCutoffDistance(cutoff);
    system.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0, 0), Vec3(0, boxSize, 0), Vec3(0, 0, boxSize));
    system.addForce(nonbonded);

    // See if the correction has the correct value.

    Context context(system, integrator, platform);
    context.setPositions(positions);
    double energy1 = context.getState(State::Energy).getPotentialEnergy();
636
    nonbonded->setUseDispersionCorrection(false);
637
638
639
640
641
642
    context.reinitialize();
    context.setPositions(positions);
    double energy2 = context.getState(State::Energy).getPotentialEnergy();
    double term1 = (0.5*pow(1.1, 12)/pow(cutoff, 9))/9;
    double term2 = (0.5*pow(1.1, 6)/pow(cutoff, 3))/3;
    double expected = 8*M_PI*numParticles*numParticles*(term1-term2)/(boxSize*boxSize*boxSize);
643
    ASSERT_EQUAL_TOL(expected, energy1-energy2, 1e-4);
644
645
646
647
648
649
650
651
652

    // Now modify half the particles to be different, and see if it is still correct.

    int numType2 = 0;
    for (int i = 0; i < numParticles; i += 2) {
        nonbonded->setParticleParameters(i, 0, 1, 1);
        numType2++;
    }
    int numType1 = numParticles-numType2;
653
    nonbonded->setUseDispersionCorrection(true);
654
655
656
    context.reinitialize();
    context.setPositions(positions);
    energy1 = context.getState(State::Energy).getPotentialEnergy();
657
    nonbonded->setUseDispersionCorrection(false);
658
659
660
661
662
663
664
665
666
667
668
669
670
671
    context.reinitialize();
    context.setPositions(positions);
    energy2 = context.getState(State::Energy).getPotentialEnergy();
    term1 = ((numType1*(numType1+1))/2)*(0.5*pow(1.1, 12)/pow(cutoff, 9))/9;
    term2 = ((numType1*(numType1+1))/2)*(0.5*pow(1.1, 6)/pow(cutoff, 3))/3;
    term1 += ((numType2*(numType2+1))/2)*(1*pow(1.0, 12)/pow(cutoff, 9))/9;
    term2 += ((numType2*(numType2+1))/2)*(1*pow(1.0, 6)/pow(cutoff, 3))/3;
    double combinedSigma = 0.5*(1+1.1);
    double combinedEpsilon = sqrt(1*0.5);
    term1 += (numType1*numType2)*(combinedEpsilon*pow(combinedSigma, 12)/pow(cutoff, 9))/9;
    term2 += (numType1*numType2)*(combinedEpsilon*pow(combinedSigma, 6)/pow(cutoff, 3))/3;
    term1 /= (numParticles*(numParticles+1))/2;
    term2 /= (numParticles*(numParticles+1))/2;
    expected = 8*M_PI*numParticles*numParticles*(term1-term2)/(boxSize*boxSize*boxSize);
672
    ASSERT_EQUAL_TOL(expected, energy1-energy2, 1e-4);
673
674
}

675
676
677
678
679
int main() {
    try {
        testCoulomb();
        testLJ();
        testExclusionsAnd14();
680
681
682
683
684
685
        testCutoff();
        testCutoff14();
        testPeriodic();
        testLargeSystem();
        testBlockInteractions(false);
        testBlockInteractions(true);
686
        testDispersionCorrection();
687
688
689
690
691
692
693
694
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}