TestReferenceGBVIForce.cpp 9.16 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
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
32
33
34
35
/* -------------------------------------------------------------------------- *
 *                                   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-2009 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.                                     *
 * -------------------------------------------------------------------------- */

/**
 * This tests the reference implementation of GBVIForce.
 */

36
#include "openmm/internal/AssertionUtilities.h"
37
#include "openmm/Context.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
38
#include "ReferencePlatform.h"
39
40
41
42
43
44
#include "openmm/HarmonicBondForce.h"
#include "openmm/GBVIForce.h"
#include "openmm/GBSAOBCForce.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include "openmm/NonbondedForce.h"
45
#include "SimTKOpenMMRealType.h"
46
#include "sfmt/SFMT.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>

using namespace OpenMM;
using namespace std;

const double TOL = 1e-5;

void testSingleParticle() {
    ReferencePlatform platform;
57
58
    System system;
    system.addParticle(2.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
59
60
    LangevinIntegrator integrator(0, 0.1, 0.01);

61
    GBVIForce* forceField = new GBVIForce();
Mark Friedrichs's avatar
Mark Friedrichs committed
62

Mark Friedrichs's avatar
Mark Friedrichs committed
63
64
65
    double charge         = -1.0;
    double radius         =  0.15;
    double gamma          =  1.0;
66
    forceField->addParticle(charge, radius, gamma);
Mark Friedrichs's avatar
Mark Friedrichs committed
67
    system.addForce(forceField);
68
69
    ASSERT(!forceField->usesPeriodicBoundaryConditions());
    ASSERT(!system.usesPeriodicBoundaryConditions());
Mark Friedrichs's avatar
Mark Friedrichs committed
70

71
    Context context(system, integrator, platform);
Mark Friedrichs's avatar
Mark Friedrichs committed
72
73
74
75
76
77
78
79
80
81
    vector<Vec3> positions(1);
    positions[0] = Vec3(0, 0, 0);
    context.setPositions(positions);
    State state = context.getState(State::Energy);

    double bornRadius     = radius; 
    double eps0           = EPSILON0;
    double tau            = (1.0/forceField->getSoluteDielectric()-1.0/forceField->getSolventDielectric());

    double bornEnergy     = (-charge*charge/(8*PI_M*eps0))*tau/bornRadius;
82
    double nonpolarEnergy = -gamma*tau*std::pow(radius/bornRadius, 3.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
83
84
85

    double expectedE      = (bornEnergy+nonpolarEnergy); 
    double obtainedE      = state.getPotentialEnergy(); 
86
    double diff           = fabs((obtainedE - expectedE)/expectedE);
Mark Friedrichs's avatar
Mark Friedrichs committed
87

Mark Friedrichs's avatar
Mark Friedrichs committed
88
    ASSERT_EQUAL_TOL((bornEnergy+nonpolarEnergy), state.getPotentialEnergy(), 0.01);
Mark Friedrichs's avatar
Mark Friedrichs committed
89
90
}

91
void testEnergyEthane(int applyBornRadiiScaling) {
Mark Friedrichs's avatar
Mark Friedrichs committed
92
93
94

    ReferencePlatform platform;
    const int numParticles = 8;
95
    System system;
Mark Friedrichs's avatar
Mark Friedrichs committed
96
97
    LangevinIntegrator integrator(0, 0.1, 0.01);

Mark Friedrichs's avatar
Mark Friedrichs committed
98
99
    // harmonic bond

Mark Friedrichs's avatar
Mark Friedrichs committed
100
101
    double C_HBondDistance   = 0.1097;
    double C_CBondDistance   = 0.1504;
102
103
104
105
    HarmonicBondForce* bonds = new HarmonicBondForce();
    bonds->addBond(0, 1, C_HBondDistance, 0.0);
    bonds->addBond(2, 1, C_HBondDistance, 0.0);
    bonds->addBond(3, 1, C_HBondDistance, 0.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
106

107
    bonds->addBond(1, 4, C_CBondDistance, 0.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
108

109
110
111
    bonds->addBond(5, 4, C_HBondDistance, 0.0);
    bonds->addBond(6, 4, C_HBondDistance, 0.0);
    bonds->addBond(7, 4, C_HBondDistance, 0.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
112
113
114
115
116
117
118
119

    system.addForce(bonds);

    double C_radius, C_gamma, C_charge, H_radius, H_gamma, H_charge;

    int AM1_BCC = 1;
    H_charge    = -0.053;
    C_charge    = -3.0*H_charge;
120
    if (AM1_BCC) {
Mark Friedrichs's avatar
Mark Friedrichs committed
121
122
123
124
       C_radius =  0.180;
       C_gamma  = -0.2863;
       H_radius =  0.125;
       H_gamma  =  0.2437;
125
126
    }
    else {
Mark Friedrichs's avatar
Mark Friedrichs committed
127
128
129
130
131
132
       C_radius =  0.215;
       C_gamma  = -1.1087;
       H_radius =  0.150;
       H_gamma  =  0.1237;
    }

Mark Friedrichs's avatar
Mark Friedrichs committed
133
134
135
136
    NonbondedForce* nonbonded = new NonbondedForce();
    nonbonded->setNonbondedMethod(NonbondedForce::NoCutoff);

    GBVIForce* forceField = new GBVIForce();
137
138
    if (applyBornRadiiScaling) {
        forceField->setBornRadiusScalingMethod(GBVIForce::QuinticSpline);
139
140
    }
    else {
141
        forceField->setBornRadiusScalingMethod(GBVIForce::NoScaling);
142
    }
143
    for (int i = 0; i < numParticles; i++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
144
       system.addParticle(1.0);
145
146
       forceField->addParticle(H_charge, H_radius, H_gamma);
       nonbonded->addParticle( H_charge, H_radius, 0.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
147
148
    }
 
149
150
    forceField->setParticleParameters(1, C_charge, C_radius, C_gamma);
    forceField->setParticleParameters(4, C_charge, C_radius, C_gamma);
Mark Friedrichs's avatar
Mark Friedrichs committed
151
 
152
153
    nonbonded->setParticleParameters( 1, C_charge, C_radius, 0.0);
    nonbonded->setParticleParameters( 4, C_charge, C_radius, 0.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
154
 
155
156
157
158
159
160
161
    forceField->addBond(0, 1, C_HBondDistance);
    forceField->addBond(2, 1, C_HBondDistance);
    forceField->addBond(3, 1, C_HBondDistance);
    forceField->addBond(1, 4, C_CBondDistance);
    forceField->addBond(5, 4, C_HBondDistance);
    forceField->addBond(6, 4, C_HBondDistance);
    forceField->addBond(7, 4, C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
162
163
164
165
166
    
    std::vector<pair<int, int> > bondExceptions;
    std::vector<double> bondDistances;
    
    bondExceptions.push_back(pair<int, int>(0, 1)); 
167
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
168
169
    
    bondExceptions.push_back(pair<int, int>(2, 1)); 
170
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
171
172
    
    bondExceptions.push_back(pair<int, int>(3, 1)); 
173
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
174
175
    
    bondExceptions.push_back(pair<int, int>(1, 4)); 
176
    bondDistances.push_back(C_CBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
177
178
    
    bondExceptions.push_back(pair<int, int>(5, 4)); 
179
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
180
181
    
    bondExceptions.push_back(pair<int, int>(6, 4)); 
182
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
183
184
 
    bondExceptions.push_back(pair<int, int>(7, 4));
185
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
186
187
188
189
190
 
    nonbonded->createExceptionsFromBonds(bondExceptions, 0.0, 0.0);
 
    system.addForce(forceField);
    system.addForce(nonbonded);
Mark Friedrichs's avatar
Mark Friedrichs committed
191

192
    Context context(system, integrator, platform);
Mark Friedrichs's avatar
Mark Friedrichs committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    
    vector<Vec3> positions(numParticles);
    positions[0] = Vec3(0.5480,    1.7661,    0.0000);
    positions[1] = Vec3(0.7286,    0.8978,    0.6468);
    positions[2] = Vec3(0.4974,    0.0000,    0.0588);
    positions[3] = Vec3(0.0000,    0.9459,    1.4666);
    positions[4] = Vec3(2.1421,    0.8746,    1.1615);
    positions[5] = Vec3(2.3239,    0.0050,    1.8065);
    positions[6] = Vec3(2.8705,    0.8295,    0.3416);
    positions[7] = Vec3(2.3722,    1.7711,    1.7518);
    context.setPositions(positions);

    State state = context.getState(State::Forces | State::Energy);
    
    // Take a small step in the direction of the energy gradient.
    
    double norm        = 0.0;
    double forceSum[3] = { 0.0, 0.0, 0.0 };
    for (int i = 0; i < numParticles; ++i) {
        Vec3 f  = state.getForces()[i];
Mark Friedrichs's avatar
Mark Friedrichs committed
213
        norm        += f[0]*f[0] + f[1]*f[1] + f[2]*f[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
        forceSum[0] += f[0];
        forceSum[1] += f[1];
        forceSum[2] += f[2];
    }
    norm               = std::sqrt(norm);

    const double delta = 1e-4;
    double step = delta/norm;
    for (int i = 0; i < numParticles; ++i) {
        Vec3 p = positions[i];
        Vec3 f = state.getForces()[i];
        positions[i] = Vec3(p[0]-f[0]*step, p[1]-f[1]*step, p[2]-f[2]*step);
    }
    context.setPositions(positions);
    
    State state2 = context.getState(State::Energy);

    // See whether the potential energy changed by the expected amount.
    
    ASSERT_EQUAL_TOL(norm, (state2.getPotentialEnergy()-state.getPotentialEnergy())/delta, 0.01)
}

int main() {
    try {
        testSingleParticle();
239
240
        testEnergyEthane(0);
        testEnergyEthane(1);
Mark Friedrichs's avatar
Mark Friedrichs committed
241
242
243
244
245
246
247
248
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}