TestCpuNeighborList.cpp 5.14 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
37
/* -------------------------------------------------------------------------- *
 *                                   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"
#include "CpuNeighborList.h"
peastman's avatar
peastman committed
38
#include "CpuPlatform.h"
39
40
41
42
43
44
45
46
47
48
49
#include "sfmt/SFMT.h"
#include <iostream>
#include <set>
#include <vector>

using namespace OpenMM;
using namespace std;

void testNeighborList(bool periodic) {
    const int numParticles = 500;
    const float cutoff = 2.0f;
peastman's avatar
Bug fix  
peastman committed
50
    const float boxSize[3] = {20.0f, 15.0f, 22.0f};
51
52
53
54
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);
    vector<float> positions(4*numParticles);
    for (int i = 0; i < 4*numParticles; i++)
peastman's avatar
Bug fix  
peastman committed
55
56
        if (i%4 < 3)
            positions[i] = boxSize[i%4]*genrand_real2(sfmt);
57
58
59
60
61
62
63
64
65
    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);
        }
    }
    CpuNeighborList neighborList;
peastman's avatar
Bug fix  
peastman committed
66
    neighborList.computeNeighborList(numParticles, positions, exclusions, boxSize, periodic, cutoff);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    
    // Convert the neighbor list to a set for faster lookup.
    
    set<pair<int, int> > neighbors;
    for (int i = 0; i < (int) neighborList.getNeighbors().size(); i++) {
        pair<int, int> entry = neighborList.getNeighbors()[i];
        ASSERT(neighbors.find(entry) == neighbors.end() && neighbors.find(make_pair(entry.second, entry.first)) == neighbors.end()); // No duplicates
        neighbors.insert(entry);
    }
    
    // Check each particle pair and figure out whether they should be in the neighbor list.
    
    for (int i = 0; i < numParticles; i++)
        for (int j = 0; j <= i; j++) {
            bool shouldInclude = (exclusions[i].find(j) == exclusions[i].end());
            float dx = positions[4*i]-positions[4*j];
            float dy = positions[4*i+1]-positions[4*j+1];
            float dz = positions[4*i+2]-positions[4*j+2];
            if (periodic) {
peastman's avatar
Bug fix  
peastman committed
86
87
88
                dx -= floor(dx/boxSize[0]+0.5f)*boxSize[0];
                dy -= floor(dy/boxSize[1]+0.5f)*boxSize[1];
                dz -= floor(dz/boxSize[2]+0.5f)*boxSize[2];
89
90
91
92
93
94
95
96
97
98
            }
            if (dx*dx + dy*dy + dz*dz > cutoff*cutoff)
                shouldInclude = false;
            bool isIncluded = (neighbors.find(make_pair(i, j)) != neighbors.end() || neighbors.find(make_pair(j, i)) != neighbors.end());
            ASSERT_EQUAL(shouldInclude, isIncluded);
        }
}

int main() {
    try {
peastman's avatar
peastman committed
99
100
101
102
        if (!CpuPlatform::isProcessorSupported()) {
            cout << "CPU is not supported.  Exiting." << endl;
            return 0;
        }
103
104
105
106
107
108
109
110
111
112
        testNeighborList(false);
        testNeighborList(true);
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}