TestReferenceNeighborList.cpp 6.84 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) 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.                                     *
 * -------------------------------------------------------------------------- */

32
#include "openmm/internal/AssertionUtilities.h"
33
#include "ReferenceNeighborList.h"
34
#include "sfmt/SFMT.h"
35
36
#include <cassert>
#include <iostream>
37
#include <vector>
38
39
40
41
42
43

using namespace std;
using namespace OpenMM;

void testNeighborList()
{
peastman's avatar
peastman committed
44
45
46
    vector<Vec3> particleList(2);
    particleList[0] = Vec3(13.6, 0, 0);
    particleList[1] = Vec3(0, 0, 0);
47
48
49
    vector<set<int> > exclusions(2);
    
    NeighborList neighborList;
50

peastman's avatar
peastman committed
51
    Vec3 boxVectors[3];
52
    computeNeighborListNaive(neighborList, 2, particleList, exclusions, boxVectors, false, 13.7, 0.01);
53
54
    assert(neighborList.size() == 1);
    
55
    computeNeighborListNaive(neighborList, 2, particleList, exclusions, boxVectors, false, 13.5, 0.01);
56
57
    assert(neighborList.size() == 0);
    
58
    computeNeighborListVoxelHash(neighborList, 2, particleList, exclusions, boxVectors, false, 13.7, 0.01);
59
60
    assert(neighborList.size() == 1);
    
61
    computeNeighborListVoxelHash(neighborList, 2, particleList, exclusions, boxVectors, false, 13.5, 0.01);
62
63
64
    assert(neighborList.size() == 0);
}

peastman's avatar
peastman committed
65
66
double distance2(Vec3& pos1, Vec3& pos2, const Vec3* periodicBoxVectors) {
    Vec3 diff = pos1-pos2;
67
68
69
70
    diff -= periodicBoxVectors[2]*floor(diff[2]/periodicBoxVectors[2][2]+0.5);
    diff -= periodicBoxVectors[1]*floor(diff[1]/periodicBoxVectors[1][1]+0.5);
    diff -= periodicBoxVectors[0]*floor(diff[0]/periodicBoxVectors[0][0]+0.5);
    return diff.dot(diff);
71
72
}

peastman's avatar
peastman committed
73
void verifyNeighborList(NeighborList& list, int numParticles, vector<Vec3>& positions, const Vec3* periodicBoxVectors, double cutoff) {
74
    for (int i = 0; i < (int) list.size(); i++) {
Peter Eastman's avatar
Peter Eastman committed
75
76
        int particle1 = list[i].first;
        int particle2 = list[i].second;
77
        ASSERT(distance2(positions[particle1], positions[particle2], periodicBoxVectors) <= cutoff*cutoff);
78
79
    }
    int count = 0;
Peter Eastman's avatar
Peter Eastman committed
80
81
    for (int i = 0; i < numParticles; i++)
        for (int j = i+1; j < numParticles; j++)
82
            if (distance2(positions[i], positions[j], periodicBoxVectors) <= cutoff*cutoff)
83
                count++;
peastman's avatar
Bug fix  
peastman committed
84
    ASSERT_EQUAL(count, list.size());
85
86
87
}

void testPeriodic() {
Peter Eastman's avatar
Peter Eastman committed
88
    const int numParticles = 100;
89
    const double cutoff = 3.0;
peastman's avatar
peastman committed
90
91
92
93
94
    Vec3 periodicBoxVectors[3];
    periodicBoxVectors[0] = Vec3(20, 0, 0);
    periodicBoxVectors[1] = Vec3(0, 15, 0);
    periodicBoxVectors[2] = Vec3(0, 0, 22);
    vector<Vec3> particleList(numParticles);
95
96
97
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);

Peter Eastman's avatar
Peter Eastman committed
98
    for (int i = 0; i <numParticles; i++) {
peastman's avatar
peastman committed
99
100
101
        particleList[i][0] = genrand_real2(sfmt)*periodicBoxVectors[0][0]*3;
        particleList[i][1] = genrand_real2(sfmt)*periodicBoxVectors[1][1]*3;
        particleList[i][2] = genrand_real2(sfmt)*periodicBoxVectors[2][2]*3;
102
103
104
105
106
107
108
109
110
111
112
113
    }
    vector<set<int> > exclusions(numParticles);
    NeighborList neighborList;
    computeNeighborListNaive(neighborList, numParticles, particleList, exclusions, periodicBoxVectors, true, cutoff);
    verifyNeighborList(neighborList, numParticles, particleList, periodicBoxVectors, cutoff);
    computeNeighborListVoxelHash(neighborList, numParticles, particleList, exclusions, periodicBoxVectors, true, cutoff);
    verifyNeighborList(neighborList, numParticles, particleList, periodicBoxVectors, cutoff);
}

void testTriclinic() {
    const int numParticles = 1000;
    const double cutoff = 3.0;
peastman's avatar
peastman committed
114
115
116
117
118
    Vec3 periodicBoxVectors[3];
    periodicBoxVectors[0] = Vec3(20, 0, 0);
    periodicBoxVectors[1] = Vec3(5, 15, 0);
    periodicBoxVectors[2] = Vec3(-3, -7, 22);
    vector<Vec3> particleList(numParticles);
119
120
121
122
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);

    for (int i = 0; i <numParticles; i++) {
peastman's avatar
peastman committed
123
124
125
        particleList[i][0] = genrand_real2(sfmt)*periodicBoxVectors[0][0]*3;
        particleList[i][1] = genrand_real2(sfmt)*periodicBoxVectors[1][1]*3;
        particleList[i][2] = genrand_real2(sfmt)*periodicBoxVectors[2][2]*3;
126
    }
Peter Eastman's avatar
Peter Eastman committed
127
    vector<set<int> > exclusions(numParticles);
128
    NeighborList neighborList;
129
    computeNeighborListNaive(neighborList, numParticles, particleList, exclusions, periodicBoxVectors, true, cutoff);
130
    verifyNeighborList(neighborList, numParticles, particleList, periodicBoxVectors, cutoff);
131
    computeNeighborListVoxelHash(neighborList, numParticles, particleList, exclusions, periodicBoxVectors, true, cutoff);
132
    verifyNeighborList(neighborList, numParticles, particleList, periodicBoxVectors, cutoff);
133
134
135
136
}

int main() 
{
peastman's avatar
Bug fix  
peastman committed
137
138
139
    try {
        testNeighborList();
        testPeriodic();
140
        testTriclinic();
peastman's avatar
Bug fix  
peastman committed
141
142
143
144
145
146
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
147
148
149
    return 0;
}