TestCudaNonbondedForce.cpp 25.2 KB
Newer Older
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
/* -------------------------------------------------------------------------- *
 *                                   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) 2008 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.                                     *
 * -------------------------------------------------------------------------- */

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

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

using namespace OpenMM;
using namespace std;

const double TOL = 1e-5;

void testCoulomb() {
    CudaPlatform platform;
    System system(2, 0);
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
61
    NonbondedForce* forceField = new NonbondedForce(2, 0);
Peter Eastman's avatar
Peter Eastman committed
62
63
    forceField->setParticleParameters(0, 0.5, 1, 0);
    forceField->setParticleParameters(1, -1.5, 1, 0);
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    system.addForce(forceField);
    OpenMMContext context(system, integrator, platform);
    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 force = 138.935485*(-0.75)/4.0;
    ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], TOL);
    ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[1], TOL);
    ASSERT_EQUAL_TOL(138.935485*(-0.75)/2.0, state.getPotentialEnergy(), TOL);
}

void testLJ() {
    CudaPlatform platform;
    System system(2, 0);
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
82
    NonbondedForce* forceField = new NonbondedForce(2, 0);
Peter Eastman's avatar
Peter Eastman committed
83
84
    forceField->setParticleParameters(0, 0, 1.2, 1);
    forceField->setParticleParameters(1, 0, 1.4, 2);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    system.addForce(forceField);
    OpenMMContext context(system, integrator, platform);
    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;
    System system(5, 0);
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
105
106
107
108
109
110
111
112
    HarmonicBondForce* bonds = new HarmonicBondForce(4);
    bonds->setBondParameters(0, 0, 1, 1, 0);
    bonds->setBondParameters(1, 1, 2, 1, 0);
    bonds->setBondParameters(2, 2, 3, 1, 0);
    bonds->setBondParameters(3, 3, 4, 1, 0);
    system.addForce(bonds);
    NonbondedForce* nonbonded = new NonbondedForce(5, 2);
    system.addForce(nonbonded);
113
114
115
116
117
118
119
    for (int i = 1; i < 5; ++i) {
 
        // Test LJ forces
        
        vector<Vec3> positions(5);
        const double r = 1.0;
        for (int j = 0; j < 5; ++j) {
Peter Eastman's avatar
Peter Eastman committed
120
            nonbonded->setParticleParameters(j, 0, 1.5, 0);
121
122
            positions[j] = Vec3(0, j, 0);
        }
Peter Eastman's avatar
Peter Eastman committed
123
124
        nonbonded->setParticleParameters(0, 0, 1.5, 1);
        nonbonded->setParticleParameters(i, 0, 1.5, 1);
125
126
        nonbonded->setNonbonded14Parameters(0, 0, 3, 0, 1.5, i == 3 ? 0.5 : 0.0);
        nonbonded->setNonbonded14Parameters(1, 1, 4, 0, 1.5, 0.0);
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        positions[i] = Vec3(r, 0, 0);
        OpenMMContext 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);

        // Test Coulomb forces
        
Peter Eastman's avatar
Peter Eastman committed
150
151
        nonbonded->setParticleParameters(0, 2, 1.5, 0);
        nonbonded->setParticleParameters(i, 2, 1.5, 0);
152
153
        nonbonded->setNonbonded14Parameters(0, 0, 3, i == 3 ? 4/1.2 : 0, 1.5, 0);
        nonbonded->setNonbonded14Parameters(1, 1, 4, 0, 1.5, 0);
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        OpenMMContext context2(system, integrator, platform);
        context2.setPositions(positions);
        state = context2.getState(State::Forces | State::Energy);
        const vector<Vec3>& forces2 = state.getForces();
        force = 138.935485*4/(r*r);
        energy = 138.935485*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);
    }
}

void testCutoff() {
    CudaPlatform platform;
    System system(3, 0);
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
178
    NonbondedForce* forceField = new NonbondedForce(3, 0);
Peter Eastman's avatar
Peter Eastman committed
179
180
181
    forceField->setParticleParameters(0, 1.0, 1, 0);
    forceField->setParticleParameters(1, 1.0, 1, 0);
    forceField->setParticleParameters(2, 1.0, 1, 0);
182
    forceField->setNonbondedMethod(NonbondedForce::CutoffNonPeriodic);
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
    const double cutoff = 2.9;
    forceField->setCutoffDistance(cutoff);
    system.addForce(forceField);
    OpenMMContext context(system, integrator, platform);
    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 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);
    const double force1 = 138.935485*(1.0)*(0.25-2.0*krf*2.0);
    const double force2 = 138.935485*(1.0)*(1.0-2.0*krf*1.0);
    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);
    const double energy1 = 138.935485*(1.0)*(0.5+krf*4.0-crf);
    const double energy2 = 138.935485*(1.0)*(1.0+krf*1.0-crf);
    ASSERT_EQUAL_TOL(energy1+energy2, state.getPotentialEnergy(), TOL);
}

void testCutoff14() {
    CudaPlatform platform;
    System system(5, 0);
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
211
212
213
214
215
216
217
218
    HarmonicBondForce* bonds = new HarmonicBondForce(4);
    bonds->setBondParameters(0, 0, 1, 1, 0);
    bonds->setBondParameters(1, 1, 2, 1, 0);
    bonds->setBondParameters(2, 2, 3, 1, 0);
    bonds->setBondParameters(3, 3, 4, 1, 0);
    system.addForce(bonds);
    NonbondedForce* nonbonded = new NonbondedForce(5, 2);
    nonbonded->setNonbondedMethod(NonbondedForce::CutoffNonPeriodic);
219
220
    nonbonded->setNonbonded14Parameters(0, 0, 3, 0, 1.5, 0.0);
    nonbonded->setNonbonded14Parameters(1, 1, 4, 0, 1.5, 0.0);
221
    const double cutoff = 3.5;
222
223
    nonbonded->setCutoffDistance(cutoff);
    system.addForce(nonbonded);
224
225
226
227
228
229
230
231
232
233
234
    OpenMMContext context(system, integrator, platform);
    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
235
        nonbonded->setParticleParameters(0, 0, 1.5, 1);
236
        for (int j = 1; j < 5; ++j)
Peter Eastman's avatar
Peter Eastman committed
237
238
            nonbonded->setParticleParameters(j, 0, 1.5, 0);
        nonbonded->setParticleParameters(i, 0, 1.5, 1);
239
240
        nonbonded->setNonbonded14Parameters(0, 0, 3, 0, 1.5, i == 3 ? 0.5 : 0.0);
        nonbonded->setNonbonded14Parameters(1, 1, 4, 0, 1.5, 0.0);
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        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
265
266
        nonbonded->setParticleParameters(0, q, 1.5, 0);
        nonbonded->setParticleParameters(i, q, 1.5, 0);
267
268
        nonbonded->setNonbonded14Parameters(0, 0, 3, i == 3 ? q*q/1.2 : 0, 1.5, 0);
        nonbonded->setNonbonded14Parameters(1, 1, 4, 0, 1.5, 0);
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
        context.reinitialize();
        context.setPositions(positions);
        state = context.getState(State::Forces | State::Energy);
        const vector<Vec3>& forces2 = 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);
        force = 138.935485*q*q*(1.0/(r*r)-2.0*krf*r);
        energy = 138.935485*q*q*(1.0/r+krf*r*r-crf);
        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;
    System system(3, 0);
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
296
297
298
299
    HarmonicBondForce* bonds = new HarmonicBondForce(1);
    bonds->setBondParameters(0, 0, 1, 1, 0);
    system.addForce(bonds);
    NonbondedForce* nonbonded = new NonbondedForce(3, 0);
Peter Eastman's avatar
Peter Eastman committed
300
301
302
    nonbonded->setParticleParameters(0, 1.0, 1, 0);
    nonbonded->setParticleParameters(1, 1.0, 1, 0);
    nonbonded->setParticleParameters(2, 1.0, 1, 0);
303
    nonbonded->setNonbondedMethod(NonbondedForce::CutoffPeriodic);
304
    const double cutoff = 2.0;
305
306
307
    nonbonded->setCutoffDistance(cutoff);
    nonbonded->setPeriodicBoxVectors(Vec3(4, 0, 0), Vec3(0, 4, 0), Vec3(0, 0, 4));
    system.addForce(nonbonded);
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    OpenMMContext context(system, integrator, platform);
    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);
    const double force = 138.935485*(1.0)*(1.0-2.0*krf*1.0);
    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);
    ASSERT_EQUAL_TOL(2*138.935485*(1.0)*(1.0+krf*1.0-crf), state.getPotentialEnergy(), TOL);
}

326
327
328
329
330
331
332
333
334
335
336
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
void testLargeSystem() {
    const int numMolecules = 600;
    const int numParticles = numMolecules*2;
    const double cutoff = 2.0;
    const double boxSize = 10.0;
    const double tol = 1e-3;
    CudaPlatform cuda;
    ReferencePlatform reference;
    System system(numParticles, 0);
    VerletIntegrator integrator(0.01);
    NonbondedForce* nonbonded = new NonbondedForce(numParticles, 0);
    HarmonicBondForce* bonds = new HarmonicBondForce(numMolecules);
    vector<Vec3> positions(numParticles);
    vector<Vec3> velocities(numParticles);
    init_gen_rand(0);
    for (int i = 0; i < numMolecules; i++) {
        if (i < numMolecules/2) {
            nonbonded->setParticleParameters(2*i, 1.0, 0.2, 0.1);
            nonbonded->setParticleParameters(2*i+1, 1.0, 0.1, 0.1);
        }
        else {
            nonbonded->setParticleParameters(2*i, 1.0, 0.2, 0.2);
            nonbonded->setParticleParameters(2*i+1, 1.0, 0.1, 0.2);
        }
        positions[2*i] = Vec3(boxSize*genrand_real2(), boxSize*genrand_real2(), boxSize*genrand_real2());
        positions[2*i+1] = Vec3(positions[2*i][0]+1.0, positions[2*i][1], positions[2*i][2]);
        velocities[2*i] = Vec3(genrand_real2(), genrand_real2(), genrand_real2());
        velocities[2*i+1] = Vec3(genrand_real2(), genrand_real2(), genrand_real2());
        bonds->setBondParameters(i, 2*i, 2*i+1, 1.0, 0.1);
    }

    // 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);
    OpenMMContext cudaContext(system, integrator, cuda);
    OpenMMContext referenceContext(system, integrator, reference);
    cudaContext.setPositions(positions);
    cudaContext.setVelocities(velocities);
    referenceContext.setPositions(positions);
    referenceContext.setVelocities(velocities);
    State cudaState = cudaContext.getState(State::Positions | State::Velocities | State::Forces);
    State referenceState = referenceContext.getState(State::Positions | State::Velocities | State::Forces);
    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);
    }

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

    nonbonded->setNonbondedMethod(NonbondedForce::CutoffPeriodic);
    nonbonded->setPeriodicBoxVectors(Vec3(boxSize, 0, 0), Vec3(0, boxSize, 0), Vec3(0, 0, boxSize));
    cudaContext.reinitialize();
    referenceContext.reinitialize();
    cudaContext.setPositions(positions);
    cudaContext.setVelocities(velocities);
    referenceContext.setPositions(positions);
    referenceContext.setVelocities(velocities);
    cudaState = cudaContext.getState(State::Positions | State::Velocities | State::Forces);
    referenceState = referenceContext.getState(State::Positions | State::Velocities | State::Forces);
    for (int i = 0; i < numParticles; i++) {
391
392
393
        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);
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
        ASSERT_EQUAL_VEC(cudaState.getVelocities()[i], referenceState.getVelocities()[i], tol);
        ASSERT_EQUAL_VEC(cudaState.getForces()[i], referenceState.getForces()[i], tol);
    }
}

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;
    System system(numParticles, 0);
    VerletIntegrator integrator(0.01);
    NonbondedForce* nonbonded = new NonbondedForce(numParticles, 0);
    vector<Vec3> positions(numParticles);
    init_gen_rand(0);
    for (int i = 0; i < numParticles; i++) {
        nonbonded->setParticleParameters(i, 1.0, 0.2, 0.2);
        positions[i] = Vec3(boxSize*(3*genrand_real2()-1), boxSize*(3*genrand_real2()-1), boxSize*(3*genrand_real2()-1));
    }
    nonbonded->setNonbondedMethod(periodic ? NonbondedForce::CutoffPeriodic : NonbondedForce::CutoffNonPeriodic);
    nonbonded->setCutoffDistance(cutoff);
    nonbonded->setPeriodicBoxVectors(Vec3(boxSize, 0, 0), Vec3(0, boxSize, 0), Vec3(0, 0, boxSize));
    system.addForce(nonbonded);
    OpenMMContext context(system, integrator, cuda);
    context.setPositions(positions);
    State state = context.getState(State::Positions | State::Velocities | State::Forces);
    OpenMMContextImpl* contextImpl = *reinterpret_cast<OpenMMContextImpl**>(&context);
    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++) {
        float4 gridSize = data.gpu->psGridBoundingBox->_pSysData[i];
        float4 center = data.gpu->psGridCenter->_pSysData[i];
        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++) {
            float4 pos = data.gpu->psPosq4->_pSysData[i*blockSize+j];
            float dx = pos.x-center.x;
            float dy = pos.y-center.y;
            float dz = pos.z-center.z;
            if (periodic) {
                dx -= floor(0.5+dx/boxSize)*boxSize;
                dy -= floor(0.5+dy/boxSize)*boxSize;
                dz -= floor(0.5+dz/boxSize)*boxSize;
            }
            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();
    int numWithInteractions = data.gpu->psInteractionCount->_pSysData[0];
    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++) {
        unsigned int workUnit = data.gpu->psInteractingWorkUnit->_pSysData[i];
        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.

        float4 gridSize1 = data.gpu->psGridBoundingBox->_pSysData[x];
        float4 gridSize2 = data.gpu->psGridBoundingBox->_pSysData[y];
        float4 center1 = data.gpu->psGridCenter->_pSysData[x];
        float4 center2 = data.gpu->psGridCenter->_pSysData[y];
        float dx = center1.x-center2.x;
        float dy = center1.y-center2.y;
        float dz = center1.z-center2.z;
        if (periodic) {
            dx -= floor(0.5+dx/boxSize)*boxSize;
            dy -= floor(0.5+dy/boxSize)*boxSize;
            dz -= floor(0.5+dz/boxSize)*boxSize;
        }
        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);
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531

        // Check the interaction flags.

        unsigned int flags = data.gpu->psInteractionFlag->_pSysData[i];
        for (int atom2 = 0; atom2 < 32; atom2++) {
            if ((flags & 1) == 0) {
                float4 pos2 = data.gpu->psPosq4->_pSysData[y*blockSize+atom2];
                for (int atom1 = 0; atom1 < blockSize; ++atom1) {
                    float4 pos1 = data.gpu->psPosq4->_pSysData[x*blockSize+atom1];
                    float dx = pos2.x-pos1.x;
                    float dy = pos2.y-pos1.y;
                    float dz = pos2.z-pos1.z;
                    if (periodic) {
                        dx -= floor(0.5+dx/boxSize)*boxSize;
                        dy -= floor(0.5+dy/boxSize)*boxSize;
                        dz -= floor(0.5+dz/boxSize)*boxSize;
                    }
                    if (dx*dx+dy*dy+dz*dz < cutoff*cutoff) {
                        dx = pos2.x-center1.x;
                        dy = pos2.y-center1.y;
                        dz = pos2.z-center1.z;
                        dx = max(0.0f, abs(dx)-gridSize1.x);
                        dy = max(0.0f, abs(dy)-gridSize1.y);
                        dz = max(0.0f, abs(dz)-gridSize1.z);
                    }
                    ASSERT(dx*dx+dy*dy+dz*dz > cutoff*cutoff);
                }
            }
            flags >>= 1;
        }
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
    }

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

    data.gpu->psWorkUnit->Download();
    for (int i = 0; i < hasInteractions.size(); i++)
        if (!hasInteractions[i]) {
            unsigned int workUnit = data.gpu->psWorkUnit->_pSysData[i];
            unsigned int x = (workUnit >> 17);
            unsigned int y = ((workUnit >> 2) & 0x7fff);
            for (int atom1 = 0; atom1 < blockSize; ++atom1) {
                float4 pos1 = data.gpu->psPosq4->_pSysData[x*blockSize+atom1];
                for (int atom2 = 0; atom2 < blockSize; ++atom2) {
                    float4 pos2 = data.gpu->psPosq4->_pSysData[y*blockSize+atom2];
                    float dx = pos1.x-pos2.x;
                    float dy = pos1.y-pos2.y;
                    float dz = pos1.z-pos2.z;
                    if (periodic) {
                        dx -= floor(0.5+dx/boxSize)*boxSize;
                        dy -= floor(0.5+dy/boxSize)*boxSize;
                        dz -= floor(0.5+dz/boxSize)*boxSize;
                    }
                    ASSERT(dx*dx+dy*dy+dz*dz > cutoff*cutoff);
                }
            }
        }
}

560
561
562
563
564
int main() {
    try {
        testCoulomb();
        testLJ();
        testExclusionsAnd14();
565
566
567
568
569
570
        testCutoff();
        testCutoff14();
        testPeriodic();
        testLargeSystem();
        testBlockInteractions(false);
        testBlockInteractions(true);
571
572
573
574
575
576
577
578
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}