TestCpuNeighborList.cpp 6.25 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
33
34
35
36
/* -------------------------------------------------------------------------- *
 *                                   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.                                     *
 * -------------------------------------------------------------------------- */

/**
 * This tests all the CPU implementation of neighbor list construction.
 */

#include "openmm/internal/AssertionUtilities.h"
37
#include "openmm/internal/ThreadPool.h"
38
#include "AlignedArray.h"
39
#include "CpuNeighborList.h"
peastman's avatar
peastman committed
40
#include "CpuPlatform.h"
41
42
43
#include "sfmt/SFMT.h"
#include <iostream>
#include <set>
44
#include <utility>
45
#include <vector>
46
#include <algorithm>
47
48
49
50

using namespace OpenMM;
using namespace std;

51
void testNeighborList(bool periodic, bool triclinic) {
52
53
    const int numParticles = 500;
    const float cutoff = 2.0f;
54
55
    RealVec boxVectors[3];
    if (triclinic) {
56
57
58
        boxVectors[0] = RealVec(10, 0, 0);
        boxVectors[1] = RealVec(4, 9, 0);
        boxVectors[2] = RealVec(-3, -3.5, 11);
59
60
    }
    else {
61
62
63
        boxVectors[0] = RealVec(10, 0, 0);
        boxVectors[1] = RealVec(0, 9, 0);
        boxVectors[2] = RealVec(0, 0, 11);
64
    }
peastman's avatar
peastman committed
65
    const float boxSize[3] = {(float) boxVectors[0][0], (float) boxVectors[1][1], (float) boxVectors[2][2]};
66
    const int blockSize = 8;
67
68
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);
69
    AlignedArray<float> positions(4*numParticles);
70
    for (int i = 0; i < 4*numParticles; i++)
peastman's avatar
Bug fix  
peastman committed
71
72
        if (i%4 < 3)
            positions[i] = boxSize[i%4]*genrand_real2(sfmt);
73
74
75
76
77
78
79
80
    vector<set<int> > exclusions(numParticles);
    for (int i = 0; i < numParticles; i++) {
        int num = min(i+1, 10);
        for (int j = 0; j < num; j++) {
            exclusions[i].insert(i-j);
            exclusions[i-j].insert(i);
        }
    }
81
    ThreadPool threads;
82
    CpuNeighborList neighborList(blockSize);
83
    neighborList.computeNeighborList(numParticles, positions, exclusions, boxVectors, periodic, cutoff, threads);
84
85
86
87
    
    // Convert the neighbor list to a set for faster lookup.
    
    set<pair<int, int> > neighbors;
88
    for (int i = 0; i < (int) neighborList.getSortedAtoms().size(); i++) {
89
90
        int blockIndex = i/blockSize;
        int indexInBlock = i-blockIndex*blockSize;
91
92
93
94
95
96
97
98
99
100
        char mask = 1<<indexInBlock;
        for (int j = 0; j < (int) neighborList.getBlockExclusions(blockIndex).size(); j++) {
            if ((neighborList.getBlockExclusions(blockIndex)[j] & mask) == 0) {
                int atom1 = neighborList.getSortedAtoms()[i];
                int atom2 = neighborList.getBlockNeighbors(blockIndex)[j];
                pair<int, int> entry = make_pair(min(atom1, atom2), max(atom1, atom2));
                ASSERT(neighbors.find(entry) == neighbors.end() && neighbors.find(make_pair(entry.second, entry.first)) == neighbors.end()); // No duplicates
                neighbors.insert(entry);
            }
        }
101
102
103
    }
    
    // Check each particle pair and figure out whether they should be in the neighbor list.
104

105
106
107
    for (int i = 0; i < numParticles; i++)
        for (int j = 0; j <= i; j++) {
            bool shouldInclude = (exclusions[i].find(j) == exclusions[i].end());
108
            Vec3 diff(positions[4*i]-positions[4*j], positions[4*i+1]-positions[4*j+1], positions[4*i+2]-positions[4*j+2]);
109
            if (periodic) {
110
111
112
                diff -= boxVectors[2]*floor(diff[2]/boxSize[2]+0.5);
                diff -= boxVectors[1]*floor(diff[1]/boxSize[1]+0.5);
                diff -= boxVectors[0]*floor(diff[0]/boxSize[0]+0.5);
113
            }
114
            if (diff.dot(diff) > cutoff*cutoff)
115
116
                shouldInclude = false;
            bool isIncluded = (neighbors.find(make_pair(i, j)) != neighbors.end() || neighbors.find(make_pair(j, i)) != neighbors.end());
117
118
            if (shouldInclude)
                ASSERT(isIncluded);
119
120
121
122
123
        }
}

int main() {
    try {
peastman's avatar
peastman committed
124
125
126
127
        if (!CpuPlatform::isProcessorSupported()) {
            cout << "CPU is not supported.  Exiting." << endl;
            return 0;
        }
128
129
130
        testNeighborList(false, false);
        testNeighborList(true, false);
        testNeighborList(true, true);
131
132
133
134
135
136
137
138
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}