"vscode:/vscode.git/clone" did not exist on "3ce8b5773e9eabd37de5a88b63909d3c3396176a"
TestReferenceNeighborList.cpp 5.93 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()
{
44
45
46
    vector<RealVec> particleList(2);
    particleList[0] = RealVec(13.6, 0, 0);
    particleList[1] = RealVec(0, 0, 0);
47
48
49
    vector<set<int> > exclusions(2);
    
    NeighborList neighborList;
50

51
52
    RealVec boxVectors[3];
    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
65
66
67
68
69
70
    assert(neighborList.size() == 0);
}

double periodicDifference(double val1, double val2, double period) {
    double diff = val1-val2;
    double base = floor(diff/period+0.5)*period;
    return diff-base;
}

71
double distance2(RealVec& pos1, RealVec& pos2, const RealVec& periodicBoxSize) {
72
73
74
75
76
77
    double dx = periodicDifference(pos1[0], pos2[0], periodicBoxSize[0]);
    double dy = periodicDifference(pos1[1], pos2[1], periodicBoxSize[1]);
    double dz = periodicDifference(pos1[2], pos2[2], periodicBoxSize[2]);
    return dx*dx+dy*dy+dz*dz;
}

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

void testPeriodic() {
Peter Eastman's avatar
Peter Eastman committed
93
    const int numParticles = 100;
94
    const double cutoff = 3.0;
95
    const RealVec periodicBoxSize(20.0, 15.0, 22.0);
96
97
98
99
    RealVec periodicBoxVectors[3];
    periodicBoxVectors[0] = RealVec(20, 0, 0);
    periodicBoxVectors[1] = RealVec(0, 15, 0);
    periodicBoxVectors[2] = RealVec(0, 0, 22);
100
    vector<RealVec> particleList(numParticles);
101
102
103
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);

Peter Eastman's avatar
Peter Eastman committed
104
    for (int i = 0; i <numParticles; i++) {
105
106
107
        particleList[i][0] = (RealOpenMM) (genrand_real2(sfmt)*periodicBoxSize[0]*3);
        particleList[i][1] = (RealOpenMM) (genrand_real2(sfmt)*periodicBoxSize[1]*3);
        particleList[i][2] = (RealOpenMM) (genrand_real2(sfmt)*periodicBoxSize[2]*3);
108
    }
Peter Eastman's avatar
Peter Eastman committed
109
    vector<set<int> > exclusions(numParticles);
110
    NeighborList neighborList;
111
    computeNeighborListNaive(neighborList, numParticles, particleList, exclusions, periodicBoxVectors, true, cutoff);
Peter Eastman's avatar
Peter Eastman committed
112
    verifyNeighborList(neighborList, numParticles, particleList, periodicBoxSize, cutoff);
113
    computeNeighborListVoxelHash(neighborList, numParticles, particleList, exclusions, periodicBoxVectors, true, cutoff);
Peter Eastman's avatar
Peter Eastman committed
114
    verifyNeighborList(neighborList, numParticles, particleList, periodicBoxSize, cutoff);
115
116
117
118
}

int main() 
{
peastman's avatar
Bug fix  
peastman committed
119
120
121
122
123
124
125
126
127
    try {
        testNeighborList();
        testPeriodic();
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
128
129
130
    return 0;
}