HippoNonbondedForce.cpp 11.7 KB
Newer Older
peastman's avatar
peastman committed
1
2
3
/* -------------------------------------------------------------------------- *
 *                                OpenMMAmoeba                                *
 * -------------------------------------------------------------------------- *
Evan Pretti's avatar
Evan Pretti committed
4
5
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
peastman's avatar
peastman committed
6
 *                                                                            *
7
 * Portions copyright (c) 2008-2019 Stanford University and the Authors.      *
peastman's avatar
peastman committed
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
 * 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/Force.h"
#include "openmm/OpenMMException.h"
#include "openmm/HippoNonbondedForce.h"
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/internal/HippoNonbondedForceImpl.h"
#include <sstream>

using namespace OpenMM;
using namespace std;

HippoNonbondedForce::HippoNonbondedForce() : nonbondedMethod(NoCutoff), cutoffDistance(1.0), switchingDistance(0.9),
        ewaldErrorTol(1e-4), alpha(0.0), dalpha(0.0), nx(0), ny(0), nz(0), dnx(0), dny(0), dnz(0) {
    extrapolationCoefficients = {0.042, 0.635, 0.414};
}

HippoNonbondedForce::NonbondedMethod HippoNonbondedForce::getNonbondedMethod() const {
    return nonbondedMethod;
}

void HippoNonbondedForce::setNonbondedMethod(HippoNonbondedForce::NonbondedMethod method) {
    if (method < 0 || method > 1)
        throw OpenMMException("HippoNonbondedForce: Illegal value for nonbonded method");
    nonbondedMethod = method;
}

double HippoNonbondedForce::getCutoffDistance() const {
    return cutoffDistance;
}

void HippoNonbondedForce::setCutoffDistance(double distance) {
    cutoffDistance = distance;
}

double HippoNonbondedForce::getSwitchingDistance() const {
    return switchingDistance;
}

void HippoNonbondedForce::setSwitchingDistance(double distance) {
    switchingDistance = distance;
}

const std::vector<double> & HippoNonbondedForce::getExtrapolationCoefficients() const {
    return extrapolationCoefficients;
}

void HippoNonbondedForce::setExtrapolationCoefficients(const std::vector<double> &coefficients) {
    extrapolationCoefficients = coefficients;
}

void HippoNonbondedForce::getPMEParameters(double& alpha, int& nx, int& ny, int& nz) const {
    alpha = this->alpha;
    nx = this->nx;
    ny = this->ny;
    nz = this->nz;
}

void HippoNonbondedForce::getDPMEParameters(double& alpha, int& nx, int& ny, int& nz) const {
    alpha = this->dalpha;
    nx = this->dnx;
    ny = this->dny;
    nz = this->dnz;
}

void HippoNonbondedForce::setPMEParameters(double alpha, int nx, int ny, int nz) {
    this->alpha = alpha;
    this->nx = nx;
    this->ny = ny;
    this->nz = nz;
}

void HippoNonbondedForce::setDPMEParameters(double alpha, int nx, int ny, int nz) {
    this->dalpha = alpha;
    this->dnx = nx;
    this->dny = ny;
    this->dnz = nz;
}

void HippoNonbondedForce::getPMEParametersInContext(const Context& context, double& alpha, int& nx, int& ny, int& nz) const {
    dynamic_cast<const HippoNonbondedForceImpl&>(getImplInContext(context)).getPMEParameters(alpha, nx, ny, nz);
}

void HippoNonbondedForce::getDPMEParametersInContext(const Context& context, double& alpha, int& nx, int& ny, int& nz) const {
    dynamic_cast<const HippoNonbondedForceImpl&>(getImplInContext(context)).getDPMEParameters(alpha, nx, ny, nz);
}

double HippoNonbondedForce::getEwaldErrorTolerance() const {
    return ewaldErrorTol;
}

void HippoNonbondedForce::setEwaldErrorTolerance(double tol) {
    ewaldErrorTol = tol;
}

int HippoNonbondedForce::addParticle(double charge, const std::vector<double>& dipole, const std::vector<double>& quadrupole, double coreCharge,
                                     double alpha, double epsilon, double damping, double c6, double pauliK, double pauliQ, double pauliAlpha,
                                     double polarizability, int axisType, int multipoleAtomZ, int multipoleAtomX, int multipoleAtomY) {
    particles.push_back(ParticleInfo(charge, dipole, quadrupole, coreCharge, alpha, epsilon, damping, c6, pauliK, pauliQ, pauliAlpha,
                                     polarizability, axisType, multipoleAtomZ, multipoleAtomX, multipoleAtomY));
    return particles.size()-1;
}

void HippoNonbondedForce::getParticleParameters(int index, double& charge, std::vector<double>& dipole, std::vector<double>& quadrupole, double& coreCharge,
                                                double& alpha, double& epsilon, double& damping, double& c6, double& pauliK, double& pauliQ, double& pauliAlpha,
                                                double& polarizability, int& axisType, int& multipoleAtomZ, int& multipoleAtomX, int& multipoleAtomY) const {
    charge = particles[index].charge;
    dipole = particles[index].dipole;
    quadrupole = particles[index].quadrupole;
    coreCharge = particles[index].coreCharge;
    alpha = particles[index].alpha;
    epsilon = particles[index].epsilon;
    damping = particles[index].damping;
    c6 = particles[index].c6;
    pauliK = particles[index].pauliK;
    pauliQ = particles[index].pauliQ;
    pauliAlpha = particles[index].pauliAlpha;
    polarizability = particles[index].polarizability;
    axisType = particles[index].axisType;
    multipoleAtomZ = particles[index].multipoleAtomZ;
    multipoleAtomX = particles[index].multipoleAtomX;
    multipoleAtomY = particles[index].multipoleAtomY;
}

void HippoNonbondedForce::setParticleParameters(int index, double charge, const std::vector<double>& dipole, const std::vector<double>& quadrupole, double coreCharge,
                                                double alpha, double epsilon, double damping, double c6, double pauliK, double pauliQ, double pauliAlpha,
                                                double polarizability, int axisType, int multipoleAtomZ, int multipoleAtomX, int multipoleAtomY) {
    particles[index].charge = charge;
    particles[index].dipole = dipole;
    particles[index].quadrupole = quadrupole;
    particles[index].coreCharge = coreCharge;
    particles[index].alpha = alpha;
    particles[index].epsilon = epsilon;
    particles[index].damping = damping;
    particles[index].c6 = c6;
    particles[index].pauliK = pauliK;
    particles[index].pauliQ = pauliQ;
    particles[index].pauliAlpha = pauliAlpha;
    particles[index].polarizability = polarizability;
    particles[index].axisType = axisType;
    particles[index].multipoleAtomZ = multipoleAtomZ;
    particles[index].multipoleAtomX = multipoleAtomX;
    particles[index].multipoleAtomY = multipoleAtomY;
}

int HippoNonbondedForce::addException(int particle1, int particle2, double multipoleMultipoleScale, double dipoleMultipoleScale, double dipoleDipoleScale,
174
        double dispersionScale, double repulsionScale, double chargeTransferScale, bool replace) {
peastman's avatar
peastman committed
175
176
177
178
179
180
181
182
183
184
185
186
187
    map<pair<int, int>, int>::iterator iter = exceptionMap.find(pair<int, int>(particle1, particle2));
    int newIndex;
    if (iter == exceptionMap.end())
        iter = exceptionMap.find(pair<int, int>(particle2, particle1));
    if (iter != exceptionMap.end()) {
        if (!replace) {
            stringstream msg;
            msg << "HippoNonbondedForce: There is already an exception for particles ";
            msg << particle1;
            msg << " and ";
            msg << particle2;
            throw OpenMMException(msg.str());
        }
188
        exceptions[iter->second] = ExceptionInfo(particle1, particle2, multipoleMultipoleScale, dipoleMultipoleScale, dipoleDipoleScale, dispersionScale, repulsionScale, chargeTransferScale);
peastman's avatar
peastman committed
189
190
191
192
        newIndex = iter->second;
        exceptionMap.erase(iter->first);
    }
    else {
193
        exceptions.push_back(ExceptionInfo(particle1, particle2, multipoleMultipoleScale, dipoleMultipoleScale, dipoleDipoleScale, dispersionScale, repulsionScale, chargeTransferScale));
peastman's avatar
peastman committed
194
195
196
197
198
199
        newIndex = exceptions.size()-1;
    }
    exceptionMap[pair<int, int>(particle1, particle2)] = newIndex;
    return newIndex;
}
void HippoNonbondedForce::getExceptionParameters(int index, int& particle1, int& particle2, double& multipoleMultipoleScale, double& dipoleMultipoleScale, double& dipoleDipoleScale,
200
        double& dispersionScale, double& repulsionScale, double& chargeTransferScale) const {
peastman's avatar
peastman committed
201
202
203
204
205
206
207
208
    ASSERT_VALID_INDEX(index, exceptions);
    particle1 = exceptions[index].particle1;
    particle2 = exceptions[index].particle2;
    multipoleMultipoleScale = exceptions[index].multipoleMultipoleScale;
    dipoleMultipoleScale = exceptions[index].dipoleMultipoleScale;
    dipoleDipoleScale = exceptions[index].dipoleDipoleScale;
    dispersionScale = exceptions[index].dispersionScale;
    repulsionScale = exceptions[index].repulsionScale;
209
    chargeTransferScale = exceptions[index].chargeTransferScale;
peastman's avatar
peastman committed
210
211
212
}

void HippoNonbondedForce::setExceptionParameters(int index, int particle1, int particle2, double multipoleMultipoleScale, double dipoleMultipoleScale, double dipoleDipoleScale,
213
        double dispersionScale, double repulsionScale, double chargeTransferScale) {
peastman's avatar
peastman committed
214
215
216
217
218
219
220
221
    ASSERT_VALID_INDEX(index, exceptions);
    exceptions[index].particle1 = particle1;
    exceptions[index].particle2 = particle2;
    exceptions[index].multipoleMultipoleScale = multipoleMultipoleScale;
    exceptions[index].dipoleMultipoleScale = dipoleMultipoleScale;
    exceptions[index].dipoleDipoleScale = dipoleDipoleScale;
    exceptions[index].dispersionScale = dispersionScale;
    exceptions[index].repulsionScale = repulsionScale;
222
    exceptions[index].chargeTransferScale = chargeTransferScale;
peastman's avatar
peastman committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
}

void HippoNonbondedForce::getInducedDipoles(Context& context, vector<Vec3>& dipoles) {
    dynamic_cast<HippoNonbondedForceImpl&>(getImplInContext(context)).getInducedDipoles(getContextImpl(context), dipoles);
}

void HippoNonbondedForce::getLabFramePermanentDipoles(Context& context, vector<Vec3>& dipoles) {
    dynamic_cast<HippoNonbondedForceImpl&>(getImplInContext(context)).getLabFramePermanentDipoles(getContextImpl(context), dipoles);
}

ForceImpl* HippoNonbondedForce::createImpl()  const {
    return new HippoNonbondedForceImpl(*this);
}

void HippoNonbondedForce::updateParametersInContext(Context& context) {
    dynamic_cast<HippoNonbondedForceImpl&>(getImplInContext(context)).updateParametersInContext(getContextImpl(context));
}