VirtualSite.cpp 8.16 KB
Newer Older
Peter Eastman's avatar
Peter Eastman committed
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) 2012-2017 Stanford University and the Authors.      *
Peter Eastman's avatar
Peter Eastman committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 * 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.                                     *
 * -------------------------------------------------------------------------- */

#include "openmm/VirtualSite.h"
#include "openmm/OpenMMException.h"
34
#include <cmath>
Peter Eastman's avatar
Peter Eastman committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <vector>

using namespace OpenMM;
using namespace std;

void VirtualSite::setParticles(const vector<int>& particleIndices) {
    particles = particleIndices;
}

int VirtualSite::getNumParticles() const {
    return particles.size();
}

int VirtualSite::getParticle(int particle) const {
    return particles[particle];
}

52
TwoParticleAverageSite::TwoParticleAverageSite(int particle1, int particle2, double weight1, double weight2) :
Peter Eastman's avatar
Peter Eastman committed
53
54
55
56
57
58
59
        weight1(weight1), weight2(weight2) {
    vector<int> particles(2);
    particles[0] = particle1;
    particles[1] = particle2;
    setParticles(particles);
}

60
double TwoParticleAverageSite::getWeight(int particle) const {
Peter Eastman's avatar
Peter Eastman committed
61
62
63
64
65
66
67
    if (particle == 0)
        return weight1;
    if (particle == 1)
        return weight2;
    throw OpenMMException("Illegal index for particle");
}

68
ThreeParticleAverageSite::ThreeParticleAverageSite(int particle1, int particle2, int particle3, double weight1, double weight2, double weight3) :
Peter Eastman's avatar
Peter Eastman committed
69
70
71
72
73
74
75
76
        weight1(weight1), weight2(weight2), weight3(weight3) {
    vector<int> particles(3);
    particles[0] = particle1;
    particles[1] = particle2;
    particles[2] = particle3;
    setParticles(particles);
}

77
double ThreeParticleAverageSite::getWeight(int particle) const {
Peter Eastman's avatar
Peter Eastman committed
78
79
80
81
82
83
84
85
86
    if (particle == 0)
        return weight1;
    if (particle == 1)
        return weight2;
    if (particle == 2)
        return weight3;
    throw OpenMMException("Illegal index for particle");
}

87
OutOfPlaneSite::OutOfPlaneSite(int particle1, int particle2, int particle3, double weight12, double weight13, double weightCross) :
Peter Eastman's avatar
Peter Eastman committed
88
89
90
91
92
93
94
95
        weight12(weight12), weight13(weight13), weightCross(weightCross) {
    vector<int> particles(3);
    particles[0] = particle1;
    particles[1] = particle2;
    particles[2] = particle3;
    setParticles(particles);
}

96
double OutOfPlaneSite::getWeight12() const {
Peter Eastman's avatar
Peter Eastman committed
97
98
99
    return weight12;
}

100
double OutOfPlaneSite::getWeight13() const {
Peter Eastman's avatar
Peter Eastman committed
101
102
103
    return weight13;
}

104
double OutOfPlaneSite::getWeightCross() const {
Peter Eastman's avatar
Peter Eastman committed
105
106
    return weightCross;
}
107

108
LocalCoordinatesSite::LocalCoordinatesSite(const vector<int>& particles, const vector<double>& originWeights, const vector<double>& xWeights, const vector<double>& yWeights, const Vec3& localPosition) :
109
        originWeights(originWeights), xWeights(xWeights), yWeights(yWeights), localPosition(localPosition) {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    int numParticles = particles.size();
    if (numParticles < 2)
        throw OpenMMException("LocalCoordinatesSite: Must depend on at least two other particles");
    if (originWeights.size() != numParticles || xWeights.size() != numParticles || yWeights.size() != numParticles)
        throw OpenMMException("LocalCoordinatesSite: Number of weights does not match number of particles");
    double originSum = 0, xSum = 0, ySum = 0;
    for (int i = 0; i < numParticles; i++) {
        originSum += originWeights[i];
        xSum += xWeights[i];
        ySum += yWeights[i];
    }
    if (fabs(originSum-1.0) > 1e-6)
        throw OpenMMException("LocalCoordinatesSite: Weights for computing origin must add to 1");
    if (fabs(xSum) > 1e-6)
        throw OpenMMException("LocalCoordinatesSite: Weights for computing x axis must add to 0");
    if (fabs(ySum) > 1e-6)
        throw OpenMMException("LocalCoordinatesSite: Weights for computing y axis must add to 0");
    setParticles(particles);
}

LocalCoordinatesSite::LocalCoordinatesSite(int particle1, int particle2, int particle3, const Vec3& originWeights, const Vec3& xWeights, const Vec3& yWeights, const Vec3& localPosition) :
        localPosition(localPosition) {
132
133
134
135
136
137
138
139
140
141
142
    if (fabs(originWeights[0]+originWeights[1]+originWeights[2]-1.0) > 1e-6)
        throw OpenMMException("LocalCoordinatesSite: Weights for computing origin must add to 1");
    if (fabs(xWeights[0]+xWeights[1]+xWeights[2]) > 1e-6)
        throw OpenMMException("LocalCoordinatesSite: Weights for computing x axis must add to 0");
    if (fabs(yWeights[0]+yWeights[1]+yWeights[2]) > 1e-6)
        throw OpenMMException("LocalCoordinatesSite: Weights for computing y axis must add to 0");
    vector<int> particles(3);
    particles[0] = particle1;
    particles[1] = particle2;
    particles[2] = particle3;
    setParticles(particles);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    this->originWeights.push_back(originWeights[0]);
    this->originWeights.push_back(originWeights[1]);
    this->originWeights.push_back(originWeights[2]);
    this->xWeights.push_back(xWeights[0]);
    this->xWeights.push_back(xWeights[1]);
    this->xWeights.push_back(xWeights[2]);
    this->yWeights.push_back(yWeights[0]);
    this->yWeights.push_back(yWeights[1]);
    this->yWeights.push_back(yWeights[2]);
}

void LocalCoordinatesSite::getOriginWeights(vector<double>& weights) const {
    weights = originWeights;
}

Vec3 LocalCoordinatesSite::getOriginWeights() const {
    if (originWeights.size() != 3)
        throw OpenMMException("LocalCoordinatesSite: This version of getOriginWeights() requires the site to depend on three particles");
    return Vec3(originWeights[0], originWeights[1], originWeights[2]);
}

void LocalCoordinatesSite::getXWeights(vector<double>& weights) const {
    weights = xWeights;
166
167
}

168
169
170
171
Vec3 LocalCoordinatesSite::getXWeights() const {
    if (xWeights.size() != 3)
        throw OpenMMException("LocalCoordinatesSite: This version of getXWeights() requires the site to depend on three particles");
    return Vec3(xWeights[0], xWeights[1], xWeights[2]);
172
173
}

174
175
void LocalCoordinatesSite::getYWeights(vector<double>& weights) const {
    weights = yWeights;
176
177
}

178
179
180
181
Vec3 LocalCoordinatesSite::getYWeights() const {
    if (yWeights.size() != 3)
        throw OpenMMException("LocalCoordinatesSite: This version of getYWeights() requires the site to depend on three particles");
    return Vec3(yWeights[0], yWeights[1], yWeights[2]);
182
183
184
185
186
}

const Vec3& LocalCoordinatesSite::getLocalPosition() const {
    return localPosition;
}