TestReferenceGBVIForce.cpp 9.13 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
#include <iostream>
#include <vector>

using namespace OpenMM;
using namespace std;

53
54
ReferencePlatform platform;

Mark Friedrichs's avatar
Mark Friedrichs committed
55
56
57
const double TOL = 1e-5;

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

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

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

72
    Context context(system, integrator, platform);
Mark Friedrichs's avatar
Mark Friedrichs committed
73
74
75
76
77
78
79
80
81
82
    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;
83
    double nonpolarEnergy = -gamma*tau*std::pow(radius/bornRadius, 3.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
84
85
86

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

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

92
void testEnergyEthane(int applyBornRadiiScaling) {
Mark Friedrichs's avatar
Mark Friedrichs committed
93
    const int numParticles = 8;
94
    System system;
Mark Friedrichs's avatar
Mark Friedrichs committed
95
96
    LangevinIntegrator integrator(0, 0.1, 0.01);

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

Mark Friedrichs's avatar
Mark Friedrichs committed
99
100
    double C_HBondDistance   = 0.1097;
    double C_CBondDistance   = 0.1504;
101
102
103
104
    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
105

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

108
109
110
    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
111
112
113
114
115
116
117
118

    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;
119
    if (AM1_BCC) {
Mark Friedrichs's avatar
Mark Friedrichs committed
120
121
122
123
       C_radius =  0.180;
       C_gamma  = -0.2863;
       H_radius =  0.125;
       H_gamma  =  0.2437;
124
125
    }
    else {
Mark Friedrichs's avatar
Mark Friedrichs committed
126
127
128
129
130
131
       C_radius =  0.215;
       C_gamma  = -1.1087;
       H_radius =  0.150;
       H_gamma  =  0.1237;
    }

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

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

191
    Context context(system, integrator, platform);
Mark Friedrichs's avatar
Mark Friedrichs committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    
    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
212
        norm        += f[0]*f[0] + f[1]*f[1] + f[2]*f[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        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();
238
239
        testEnergyEthane(0);
        testEnergyEthane(1);
Mark Friedrichs's avatar
Mark Friedrichs committed
240
241
242
243
244
245
246
247
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}