TestFindExclusions.cpp 5.88 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
9
 * Portions copyright (c) 2008-2009 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * 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.                                     *
 * -------------------------------------------------------------------------- */

/**
33
 * This tests the createExceptionsFromBonds() method of NonbondedForce, which identifies pairs of atoms
34
35
36
37
38
39
40
41
 * whose nonbonded atoms are either excluded or decreased.  The test system is a chain with branches:
 * 
 * 1  3  5  7  9  11  13  15  17  19
 * |  |  |  |  |  |   |   |   |   |
 * 0--2--4--6--8--10--12--14--16--18
 */

#include "AssertionUtilities.h"
42
#include "NonbondedForce.h"
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <set>
#include <vector>

using namespace OpenMM;
using namespace std;

static const int NUM_ATOMS = 20;

/**
 * Add a pair of atoms to the list of exclusions.
 */

56
void addAtomsToExclusions(int atom1, int atom2, vector<set<int> >& exclusions, int& totalExclusions) {
57
58
59
    if (atom2 < NUM_ATOMS) {
        exclusions[atom1].insert(atom2);
        exclusions[atom2].insert(atom1);
60
        totalExclusions++;
61
62
63
64
65
    }
}

int main() {
    try {
66
67
68
69
        NonbondedForce nonbonded;
        vector<pair<int, int> > bonds;
        for (int i = 0; i < NUM_ATOMS; i++)
            nonbonded.addParticle(1.0, 1.0, 2.0);
70
71
72
        // loop over all main-chain atoms (even numbered atoms)
        for (int i = 0; i < NUM_ATOMS-1; i += 2) 
        {
73
74
75
            // side-chain bonds
            bonds.push_back(pair<int, int>(i, i+1));
            // main-chain bonds
76
            if (i < NUM_ATOMS-2) // penultimate atom (NUM_ATOMS-2) has no subsequent main-chain atom
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
                bonds.push_back(pair<int, int>(i, i+2));
        }
        nonbonded.createExceptionsFromBonds(bonds, 0.2, 0.4);

        // Build lists of the expected exclusions and 1-4s.

        vector<set<int> > expectedExclusions(NUM_ATOMS);
        int totalExclusions = 0;
        for (int i = 0; i < NUM_ATOMS; i += 2) {
            addAtomsToExclusions(i, i+1, expectedExclusions, totalExclusions);
            addAtomsToExclusions(i, i+2, expectedExclusions, totalExclusions);
            addAtomsToExclusions(i, i+3, expectedExclusions, totalExclusions);
            addAtomsToExclusions(i, i+4, expectedExclusions, totalExclusions);
            addAtomsToExclusions(i+1, i+2, expectedExclusions, totalExclusions);
        }
        vector<set<int> > expected14(NUM_ATOMS);
        int total14 = 0;
        for (int i = 0; i < NUM_ATOMS; i += 2) {
            addAtomsToExclusions(i, i+5, expected14, total14);
            addAtomsToExclusions(i, i+6, expected14, total14);
            addAtomsToExclusions(i+1, i+3, expected14, total14);
            addAtomsToExclusions(i+1, i+4, expected14, total14);
        }

        // Compare them to the exceptions that were generated.

        ASSERT_EQUAL(totalExclusions+total14, nonbonded.getNumExceptions());
        for (int i = 0; i < nonbonded.getNumExceptions(); i++) {
            int particle1, particle2;
            double chargeProd, sigma, epsilon;
            nonbonded.getExceptionParameters(i, particle1, particle2, chargeProd, sigma, epsilon);
            if (chargeProd == 0) {
                // This is an exclusion.

                ASSERT_EQUAL(0.0, epsilon);
                ASSERT(expectedExclusions[particle1].find(particle2) != expectedExclusions[particle2].end());
            }
            else {
                // This is a 1-4.

                ASSERT_EQUAL_TOL(0.2, chargeProd, 1e-10);
                ASSERT_EQUAL_TOL(1.0, sigma, 1e-10);
                ASSERT_EQUAL_TOL(0.8, epsilon, 1e-10);
                ASSERT(expected14[particle1].find(particle2) != expected14[particle2].end());
            }
122
123
124
125
126
127
128
129
130
        }
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}