CpuNeighborList.cpp 22.8 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
/* -------------------------------------------------------------------------- *
 *                                   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) 2013 Stanford University and the Authors.           *
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

32
#include "CpuNeighborList.h"
33
#include "openmm/internal/hardware.h"
34
#include "openmm/internal/vectorize.h"
35
#include "hilbert.h"
36
#include <algorithm>
37
38
39
40
41
42
43
44
45
46
47
#include <set>
#include <map>
#include <cmath>

using namespace std;

namespace OpenMM {

class VoxelIndex 
{
public:
48
    VoxelIndex() : y(0), z(0) {
49
    }
50
    VoxelIndex(int y, int z) : y(y), z(z) {
51
    }
52
    int y;
53
    int z;
54
55
};

56
57
58
59
60
/**
 * This data structure organizes the particles spatially.  It divides them into bins along the x and y axes,
 * then sorts each bin along the z axis so ranges can be identified quickly with a binary search.
 */
class CpuNeighborList::Voxels {
61
public:
62
63
64
65
66
67
68
69
70
71
72
    Voxels(int blockSize, float vsy, float vsz, float miny, float maxy, float minz, float maxz, const RealVec* periodicBoxVectors, bool usePeriodic) :
            blockSize(blockSize), voxelSizeY(vsy), voxelSizeZ(vsz), miny(miny), maxy(maxy), minz(minz), maxz(maxz), periodicBoxVectors(periodicBoxVectors), usePeriodic(usePeriodic) {
        periodicBoxSize[0] = (float) periodicBoxVectors[0][0];
        periodicBoxSize[1] = (float) periodicBoxVectors[1][1];
        periodicBoxSize[2] = (float) periodicBoxVectors[2][2];
        recipBoxSize[0] = (float) (1/periodicBoxVectors[0][0]);
        recipBoxSize[1] = (float) (1/periodicBoxVectors[1][1]);
        recipBoxSize[2] = (float) (1/periodicBoxVectors[2][2]);
        triclinic = (periodicBoxVectors[0][1] != 0.0 || periodicBoxVectors[0][2] != 0.0 ||
                     periodicBoxVectors[1][0] != 0.0 || periodicBoxVectors[1][2] != 0.0 ||
                     periodicBoxVectors[2][0] != 0.0 || periodicBoxVectors[2][1] != 0.0);
73
        if (usePeriodic) {
74
75
76
77
            ny = (int) floorf(periodicBoxVectors[1][1]/voxelSizeY+0.5f);
            nz = (int) floorf(periodicBoxVectors[2][2]/voxelSizeZ+0.5f);
            voxelSizeY = periodicBoxVectors[1][1]/ny;
            voxelSizeZ = periodicBoxVectors[2][2]/nz;
78
79
80
        }
        else {
            ny = max(1, (int) floorf((maxy-miny)/voxelSizeY+0.5f));
81
            nz = max(1, (int) floorf((maxz-minz)/voxelSizeZ+0.5f));
82
83
            if (maxy > miny)
                voxelSizeY = (maxy-miny)/ny;
84
85
            if (maxz > minz)
                voxelSizeZ = (maxz-minz)/nz;
86
        }
87
88
89
90
        bins.resize(ny);
        for (int i = 0; i < ny; i++) {
            bins[i].resize(nz);
            for (int j = 0; j < nz; j++)
91
                bins[i][j].resize(0);
92
93
94
        }
    }

95
96
97
98
    /**
     * Insert a particle into the voxel data structure.
     */
    void insert(const int& atom, const float* location) {
99
        VoxelIndex voxelIndex = getVoxelIndex(location);
100
        bins[voxelIndex.y][voxelIndex.z].push_back(make_pair(location[0], atom));
101
102
103
    }
    
    /**
104
     * Sort the particles in each voxel by x coordinate.
105
106
     */
    void sortItems() {
107
108
        for (int i = 0; i < ny; i++)
            for (int j = 0; j < nz; j++)
109
                sort(bins[i][j].begin(), bins[i][j].end());
110
    }
111
    
112
    /**
113
     * Find the index of the first particle in voxel (y,z) whose x coordinate in >= the specified value.
114
     */
115
116
    int findLowerBound(int y, int z, double x) const {
        const vector<pair<float, int> >& bin = bins[y][z];
117
118
119
120
        int lower = 0;
        int upper = bin.size();
        while (lower < upper) {
            int middle = (lower+upper)/2;
121
            if (bin[middle].first < x)
122
123
124
125
126
127
128
129
                lower = middle+1;
            else
                upper = middle;
        }
        return lower;
    }
    
    /**
130
     * Find the index of the first particle in voxel (y,z) whose x coordinate in greater than the specified value.
131
     */
132
133
    int findUpperBound(int y, int z, double x) const {
        const vector<pair<float, int> >& bin = bins[y][z];
134
135
136
137
        int lower = 0;
        int upper = bin.size();
        while (lower < upper) {
            int middle = (lower+upper)/2;
138
            if (bin[middle].first > x)
139
140
141
142
143
144
                upper = middle;
            else
                lower = middle+1;
        }
        return upper;
    }
145

146
147
148
    /**
     * Get the voxel index containing a particular location.
     */
149
    VoxelIndex getVoxelIndex(const float* location) const {
150
        float yperiodic, zperiodic;
151
        if (!usePeriodic) {
152
            yperiodic = location[1]-miny;
153
            zperiodic = location[2]-minz;
154
155
        }
        else {
156
157
158
159
160
            float scale2 = floorf(location[2]*recipBoxSize[2]);
            yperiodic = location[1]-periodicBoxVectors[2][1]*scale2;
            zperiodic = location[2]-periodicBoxVectors[2][2]*scale2;
            float scale1 = floorf(yperiodic*recipBoxSize[1]);
            yperiodic -= periodicBoxVectors[1][0]*scale1;
161
        }
162
        int y = min(ny-1, int(floorf(yperiodic / voxelSizeY)));
163
        int z = min(nz-1, int(floorf(zperiodic / voxelSizeZ)));
164
        
165
        return VoxelIndex(y, z);
166
167
    }

168
    void getNeighbors(vector<int>& neighbors, int blockIndex, const fvec4& blockCenter, const fvec4& blockWidth, const vector<int>& sortedAtoms, vector<char>& exclusions, float maxDistance, const vector<int>& blockAtoms, const float* atomLocations, const vector<VoxelIndex>& atomVoxelIndex) const {
169
170
        neighbors.resize(0);
        exclusions.resize(0);
171
        fvec4 boxSize(periodicBoxSize[0], periodicBoxSize[1], periodicBoxSize[2], 0);
172
173
174
175
176
177
        fvec4 invBoxSize(recipBoxSize[0], recipBoxSize[1], recipBoxSize[2], 0);
        fvec4 periodicBoxVec4[3];
        periodicBoxVec4[0] = fvec4(periodicBoxVectors[0][0], periodicBoxVectors[0][1], periodicBoxVectors[0][2], 0);
        periodicBoxVec4[1] = fvec4(periodicBoxVectors[1][0], periodicBoxVectors[1][1], periodicBoxVectors[1][2], 0);
        periodicBoxVec4[2] = fvec4(periodicBoxVectors[2][0], periodicBoxVectors[2][1], periodicBoxVectors[2][2], 0);

178
        float maxDistanceSquared = maxDistance * maxDistance;
179
180
        float refineCutoff = maxDistance-max(max(blockWidth[0], blockWidth[1]), blockWidth[2]);
        float refineCutoffSquared = refineCutoff*refineCutoff;
181

182
183
        int dIndexY = int((maxDistance+blockWidth[1])/voxelSizeY)+1; // How may voxels away do we have to look?
        int dIndexZ = int((maxDistance+blockWidth[2])/voxelSizeZ)+1;
peastman's avatar
peastman committed
184
185
        if (usePeriodic) {
            dIndexY = min(ny/2, dIndexY);
186
            dIndexZ = min(nz/2, dIndexZ);
peastman's avatar
peastman committed
187
        }
188
189
190
        float centerPos[4];
        blockCenter.store(centerPos);
        VoxelIndex centerVoxelIndex = getVoxelIndex(centerPos);
191
192
193
194
195
196
197

        // Loop over voxels along the z axis.

        int startz = centerVoxelIndex.z-dIndexZ;
        int endz = centerVoxelIndex.z+dIndexZ;
        if (usePeriodic)
            endz = min(endz, startz+nz-1);
198
        else {
199
200
            startz = max(startz, 0);
            endz = min(endz, nz-1);
201
        }
202
        int lastSortedIndex = blockSize*(blockIndex+1);
203
        VoxelIndex voxelIndex(0, 0);
204
205
        for (int z = startz; z <= endz; ++z) {
            voxelIndex.z = z;
206
            if (usePeriodic)
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
                voxelIndex.z = (z < 0 ? z+nz : (z >= nz ? z-nz : z));

            // Loop over voxels along the y axis.

            int boxz = (int) floor((float) z/nz);
            int starty = centerVoxelIndex.y-dIndexY;
            int endy = centerVoxelIndex.y+dIndexY;
            float yoffset = (float) (usePeriodic ? boxz*periodicBoxVectors[2][1] : 0);
            if (usePeriodic) {
                starty -= (int) ceil(yoffset/voxelSizeY);
                endy -= (int) floor(yoffset/voxelSizeY);
                endy = min(endy, starty+ny-1);
            }
            else {
                starty = max(starty, 0);
                endy = min(endy, ny-1);
            }
224
            for (int y = starty; y <= endy; ++y) {
225
226
227
                voxelIndex.y = y;
                if (usePeriodic)
                    voxelIndex.y = (y < 0 ? y+ny : (y >= ny ? y-ny : y));
228
229
                int boxy = (int) floor((float) y/ny);
                float xoffset = (float) (usePeriodic ? boxy*periodicBoxVectors[1][0]+boxz*periodicBoxVectors[2][0] : 0);
230
231
232
233
                
                // Identify the range of atoms within this bin we need to search.  When using periodic boundary
                // conditions, there may be two separate ranges.
                
234
235
236
                float minx = centerPos[0];
                float maxx = centerPos[0];
                fvec4 offset(-xoffset, -yoffset+voxelSizeY*y+(usePeriodic ? 0.0f : miny), voxelSizeZ*z+(usePeriodic ? 0.0f : minz), 0);
237
238
239
240
                for (int k = 0; k < (int) blockAtoms.size(); k++) {
                    const float* atomPos = &atomLocations[4*blockAtoms[k]];
                    fvec4 posVec(atomPos);
                    fvec4 delta1 = offset-posVec;
241
                    fvec4 delta2 = delta1+fvec4(0, voxelSizeY, voxelSizeZ, 0);
242
243
244
245
246
247
                    if (usePeriodic) {
                        delta1 -= round(delta1*invBoxSize)*boxSize;
                        delta2 -= round(delta2*invBoxSize)*boxSize;
                    }
                    fvec4 delta = min(abs(delta1), abs(delta2));
                    float dy = (y == atomVoxelIndex[k].y ? 0.0f : delta[1]);
248
249
                    float dz = (z == atomVoxelIndex[k].z ? 0.0f : delta[2]);
                    float dist2 = maxDistanceSquared-dy*dy-dz*dz;
250
251
                    if (dist2 > 0) {
                        float dist = sqrtf(dist2);
252
253
                        minx = min(minx, atomPos[0]-dist-xoffset);
                        maxx = max(maxx, atomPos[0]+dist-xoffset);
254
255
                    }
                }
256
                if (minx == maxx)
peastman's avatar
peastman committed
257
                    continue;
258
259
260
261
                bool needPeriodic = (centerPos[1]-blockWidth[1] < maxDistance || centerPos[1]+blockWidth[1] > periodicBoxSize[1]-maxDistance ||
                                     centerPos[2]-blockWidth[2] < maxDistance || centerPos[2]+blockWidth[2] > periodicBoxSize[2]-maxDistance ||
                                     minx < 0.0f || maxx > periodicBoxVectors[0][0]);
                int numRanges;
262
263
                int rangeStart[2];
                int rangeEnd[2];
264
                rangeStart[0] = findLowerBound(voxelIndex.y, voxelIndex.z, minx);
265
266
                if (needPeriodic) {
                    numRanges = 2;
267
                    rangeEnd[0] = findUpperBound(voxelIndex.y, voxelIndex.z, maxx);
268
269
                    if (rangeStart[0] > 0) {
                        rangeStart[1] = 0;
270
                        rangeEnd[1] = min(findUpperBound(voxelIndex.y, voxelIndex.z, maxx-periodicBoxSize[0]), rangeStart[0]);
271
272
                    }
                    else {
273
274
                        rangeStart[1] = max(findLowerBound(voxelIndex.y, voxelIndex.z, minx+periodicBoxSize[0]), rangeEnd[0]);
                        rangeEnd[1] = bins[voxelIndex.y][voxelIndex.z].size();
275
276
                    }
                }
277
278
                else {
                    numRanges = 1;
279
                    rangeEnd[0] = findUpperBound(voxelIndex.y, voxelIndex.z, maxx);
280
                }
281
                bool periodicRectangular = (needPeriodic && !triclinic);
282
283
284
285
286
                
                // Loop over atoms and check to see if they are neighbors of this block.
                
                for (int range = 0; range < numRanges; range++) {
                    for (int item = rangeStart[range]; item < rangeEnd[range]; item++) {
287
                        const int sortedIndex = bins[voxelIndex.y][voxelIndex.z][item].second;
288

peastman's avatar
peastman committed
289
                        // Avoid duplicate entries.
290
                        if (sortedIndex >= lastSortedIndex)
291
                            continue;
292
                        
peastman's avatar
peastman committed
293
                        fvec4 atomPos(atomLocations+4*sortedAtoms[sortedIndex]);
294
                        fvec4 delta = atomPos-centerPos;
295
                        if (periodicRectangular) {
296
297
                            fvec4 base = round(delta*invBoxSize)*boxSize;
                            delta = delta-base;
298
                        }
299
300
301
302
303
                        else if (needPeriodic) {
                            delta -= periodicBoxVec4[2]*floorf(delta[2]*recipBoxSize[2]+0.5f);
                            delta -= periodicBoxVec4[1]*floorf(delta[1]*recipBoxSize[1]+0.5f);
                            delta -= periodicBoxVec4[0]*floorf(delta[0]*recipBoxSize[0]+0.5f);
                        }
304
                        delta = max(0.0f, abs(delta)-blockWidth);
305
                        float dSquared = dot3(delta, delta);
306
307
                        if (dSquared > maxDistanceSquared)
                            continue;
peastman's avatar
peastman committed
308
                        
309
310
311
312
313
314
315
316
                        if (dSquared > refineCutoffSquared) {
                            // The distance is large enough that there might not be any actual interactions.
                            // Check individual atom pairs to be sure.
                            
                            bool any = false;
                            for (int k = 0; k < (int) blockAtoms.size(); k++) {
                                fvec4 pos1(&atomLocations[4*blockAtoms[k]]);
                                delta = atomPos-pos1;
317
                                if (periodicRectangular) {
318
319
320
                                    fvec4 base = round(delta*invBoxSize)*boxSize;
                                    delta = delta-base;
                                }
321
322
323
324
325
                                else if (needPeriodic) {
                                    delta -= periodicBoxVec4[2]*floorf(delta[2]*recipBoxSize[2]+0.5f);
                                    delta -= periodicBoxVec4[1]*floorf(delta[1]*recipBoxSize[1]+0.5f);
                                    delta -= periodicBoxVec4[0]*floorf(delta[0]*recipBoxSize[0]+0.5f);
                                }
326
327
328
329
330
331
332
333
334
335
336
337
338
                                float r2 = dot3(delta, delta);
                                if (r2 < maxDistanceSquared) {
                                    any = true;
                                    break;
                                }
                            }
                            if (!any)
                                continue;
                        }
                        
                        // Add this atom to the list of neighbors.
                        
                        neighbors.push_back(sortedAtoms[sortedIndex]);
339
                        if (sortedIndex < blockSize*blockIndex)
340
                            exclusions.push_back(0);
341
342
343
344
                        else {
                            int mask = (1<<blockSize)-1;
                            exclusions.push_back(mask & (mask<<(sortedIndex-blockSize*blockIndex)));
                        }
345
346
347
348
349
350
351
                    }
                }
            }
        }
    }

private:
352
    int blockSize;
353
354
355
356
357
358
    float voxelSizeY, voxelSizeZ;
    float miny, maxy, minz, maxz;
    int ny, nz;
    float periodicBoxSize[3], recipBoxSize[3];
    bool triclinic;
    const RealVec* periodicBoxVectors;
359
    const bool usePeriodic;
peastman's avatar
peastman committed
360
    vector<vector<vector<pair<float, int> > > > bins;
361
362
};

363
class CpuNeighborList::ThreadTask : public ThreadPool::Task {
364
public:
365
366
367
368
    ThreadTask(CpuNeighborList& owner) : owner(owner) {
    }
    void execute(ThreadPool& threads, int threadIndex) {
        owner.threadComputeNeighborList(threads, threadIndex);
369
370
371
    }
    CpuNeighborList& owner;
};
372

373
CpuNeighborList::CpuNeighborList(int blockSize) : blockSize(blockSize) {
374
}
375

376
377
void CpuNeighborList::computeNeighborList(int numAtoms, const AlignedArray<float>& atomLocations, const vector<set<int> >& exclusions,
            const RealVec* periodicBoxVectors, bool usePeriodic, float maxDistance, ThreadPool& threads) {
378
    int numBlocks = (numAtoms+blockSize-1)/blockSize;
379
380
381
382
    blockNeighbors.resize(numBlocks);
    blockExclusions.resize(numBlocks);
    sortedAtoms.resize(numAtoms);
    
383
    // Record the parameters for the threads.
384
    
385
386
    this->exclusions = &exclusions;
    this->atomLocations = &atomLocations[0];
387
388
389
    this->periodicBoxVectors[0] = periodicBoxVectors[0];
    this->periodicBoxVectors[1] = periodicBoxVectors[1];
    this->periodicBoxVectors[2] = periodicBoxVectors[2];
390
391
392
393
394
395
396
397
    this->numAtoms = numAtoms;
    this->usePeriodic = usePeriodic;
    this->maxDistance = maxDistance;
    
    // Identify the range of atom positions along each axis.
    
    fvec4 minPos(&atomLocations[0]);
    fvec4 maxPos = minPos;
398
    for (int i = 0; i < numAtoms; i++) {
399
400
401
        fvec4 pos(&atomLocations[4*i]);
        minPos = min(minPos, pos);
        maxPos = max(maxPos, pos);
402
    }
403
404
405
406
407
408
409
410
411
412
    minx = minPos[0];
    maxx = maxPos[0];
    miny = minPos[1];
    maxy = maxPos[1];
    minz = minPos[2];
    maxz = maxPos[2];
    
    // Sort the atoms based on a Hilbert curve.
    
    atomBins.resize(numAtoms);
413
414
415
    ThreadTask task(*this);
    threads.execute(task);
    threads.waitForThreads();
416
417
    sort(atomBins.begin(), atomBins.end());

418
    // Build the voxel hash.
419

420
    float edgeSizeY, edgeSizeZ;
421
    if (!usePeriodic)
422
        edgeSizeY = edgeSizeZ = maxDistance; // TODO - adjust this as needed
423
    else {
424
425
        edgeSizeY = 0.6f*periodicBoxVectors[1][1]/floorf(periodicBoxVectors[1][1]/maxDistance);
        edgeSizeZ = 0.6f*periodicBoxVectors[2][2]/floorf(periodicBoxVectors[2][2]/maxDistance);
426
    }
427
    Voxels voxels(blockSize, edgeSizeY, edgeSizeZ, miny, maxy, minz, maxz, periodicBoxVectors, usePeriodic);
428
429
430
    for (int i = 0; i < numAtoms; i++) {
        int atomIndex = atomBins[i].second;
        sortedAtoms[i] = atomIndex;
431
        voxels.insert(i, &atomLocations[4*atomIndex]);
432
    }
433
434
    voxels.sortItems();
    this->voxels = &voxels;
435

436
437
    // Signal the threads to start running and wait for them to finish.
    
438
439
    threads.resumeThreads();
    threads.waitForThreads();
440
    
441
    // Add padding atoms to fill up the last block.
442
    
443
    int numPadding = numBlocks*blockSize-numAtoms;
444
    if (numPadding > 0) {
445
        char mask = ((0xFFFF-(1<<blockSize)+1) >> numPadding);
446
447
448
449
450
451
        for (int i = 0; i < numPadding; i++)
            sortedAtoms.push_back(0);
        vector<char>& exc = blockExclusions[blockExclusions.size()-1];
        for (int i = 0; i < (int) exc.size(); i++)
            exc[i] |= mask;
    }
452
453
}

454
int CpuNeighborList::getNumBlocks() const {
455
    return sortedAtoms.size()/blockSize;
456
457
}

458
459
460
461
462
463
464
465
466
467
468
469
470
const std::vector<int>& CpuNeighborList::getSortedAtoms() const {
    return sortedAtoms;
}

const std::vector<int>& CpuNeighborList::getBlockNeighbors(int blockIndex) const {
    return blockNeighbors[blockIndex];
}

const std::vector<char>& CpuNeighborList::getBlockExclusions(int blockIndex) const {
    return blockExclusions[blockIndex];
    
}

471
472
void CpuNeighborList::threadComputeNeighborList(ThreadPool& threads, int threadIndex) {
    // Compute the positions of atoms along the Hilbert curve.
473

474
475
476
477
478
479
480
481
482
483
484
485
486
    float binWidth = max(max(maxx-minx, maxy-miny), maxz-minz)/255.0f;
    float invBinWidth = 1.0f/binWidth;
    bitmask_t coords[3];
    int numThreads = threads.getNumThreads();
    for (int i = threadIndex; i < numAtoms; i += numThreads) {
        const float* pos = &atomLocations[4*i];
        coords[0] = (bitmask_t) ((pos[0]-minx)*invBinWidth);
        coords[1] = (bitmask_t) ((pos[1]-miny)*invBinWidth);
        coords[2] = (bitmask_t) ((pos[2]-minz)*invBinWidth);
        int bin = (int) hilbert_c2i(3, 8, coords);
        atomBins[i] = pair<int, int>(bin, i);
    }
    threads.syncThreads();
487

488
489
490
491
    // Compute this thread's subset of neighbors.

    int numBlocks = blockNeighbors.size();
    vector<int> blockAtoms;
492
    vector<VoxelIndex> atomVoxelIndex;
493
494
495
    for (int i = threadIndex; i < numBlocks; i += numThreads) {
        // Find the atoms in this block and compute their bounding box.
        
496
497
        int firstIndex = blockSize*i;
        int atomsInBlock = min(blockSize, numAtoms-firstIndex);
498
        blockAtoms.resize(atomsInBlock);
499
500
        atomVoxelIndex.resize(atomsInBlock);
        for (int j = 0; j < atomsInBlock; j++) {
501
            blockAtoms[j] = sortedAtoms[firstIndex+j];
502
503
            atomVoxelIndex[j] = voxels->getVoxelIndex(&atomLocations[4*blockAtoms[j]]);
        }
504
505
506
507
508
509
        fvec4 minPos(&atomLocations[4*sortedAtoms[firstIndex]]);
        fvec4 maxPos = minPos;
        for (int j = 1; j < atomsInBlock; j++) {
            fvec4 pos(&atomLocations[4*sortedAtoms[firstIndex+j]]);
            minPos = min(minPos, pos);
            maxPos = max(maxPos, pos);
510
        }
511
        voxels->getNeighbors(blockNeighbors[i], i, (maxPos+minPos)*0.5f, (maxPos-minPos)*0.5f, sortedAtoms, blockExclusions[i], maxDistance, blockAtoms, atomLocations, atomVoxelIndex);
512

513
        // Record the exclusions for this block.
514

515
516
517
518
519
520
521
522
523
        for (int j = 0; j < atomsInBlock; j++) {
            const set<int>& atomExclusions = (*exclusions)[sortedAtoms[firstIndex+j]];
            char mask = 1<<j;
            for (int k = 0; k < (int) blockNeighbors[i].size(); k++) {
                int atomIndex = blockNeighbors[i][k];
                if (atomExclusions.find(atomIndex) != atomExclusions.end())
                    blockExclusions[i][k] |= mask;
            }
        }
524
525
526
    }
}

527
} // namespace OpenMM