TestCudaAmoebaBondForce.cpp 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   OpenMMAmoeba                             *
 * -------------------------------------------------------------------------- *
 * 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-2016 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: Mark Friedrichs                                                   *
 * 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 Cuda implementation of AmoebaBondForce.
34
35
36
37
38
 */

#include "openmm/internal/AssertionUtilities.h"
#include "CudaPlatform.h"
#include "openmm/Context.h"
39
#include "openmm/CustomBondForce.h"
40
41
42
43
44
45
46
47
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>

using namespace OpenMM;

48
49
extern "C" void registerAmoebaCudaKernelFactories();

50
const double TOL = 1e-5;
51
static void computeAmoebaBondForce(int bondIndex,  std::vector<Vec3>& positions, AmoebaBondForce& amoebaBondForce,
52
                                           std::vector<Vec3>& forces, double* energy) {
53
54
55
56

    int particle1, particle2;
    double bondLength;
    double quadraticK;
57
58
    double cubicK    = amoebaBondForce.getAmoebaGlobalBondCubic();
    double quarticK  = amoebaBondForce.getAmoebaGlobalBondQuartic();
59
    amoebaBondForce.getBondParameters(bondIndex, particle1, particle2,  bondLength,  quadraticK);
60
61
62

    double deltaR[3];
    double r2 = 0.0;
63
    for (int ii = 0; ii < 3; ii++) {
64
65
66
           deltaR[ii]    = positions[particle2][ii] - positions[particle1][ii];
           r2           += deltaR[ii]*deltaR[ii];
    }
67
    double r                   = sqrt(r2);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    double bondDelta           = (r - bondLength);
    double bondDelta2          = bondDelta*bondDelta;
    double dEdR                = 1.0 + 1.5*cubicK*bondDelta + 2.0*quarticK*bondDelta2;

           dEdR               *= (r > 0.0) ? (2.0*quadraticK*bondDelta)/r : 0.0;

   forces[particle1][0]       += dEdR*deltaR[0];
   forces[particle1][1]       += dEdR*deltaR[1];
   forces[particle1][2]       += dEdR*deltaR[2];

   forces[particle2][0]       -= dEdR*deltaR[0];
   forces[particle2][1]       -= dEdR*deltaR[1];
   forces[particle2][2]       -= dEdR*deltaR[2];

   *energy                    += (1.0f + cubicK*bondDelta + quarticK*bondDelta2)*quadraticK*bondDelta2;

}

87
88
static void computeAmoebaBondForces(Context& context, AmoebaBondForce& amoebaBondForce,
                                             std::vector<Vec3>& expectedForces, double* expectedEnergy) {
89
90
91
92
93

    // get positions and zero forces

    State state = context.getState(State::Positions);
    std::vector<Vec3> positions = state.getPositions();
94
    expectedForces.resize(positions.size());
95
    
96
    for (unsigned int ii = 0; ii < expectedForces.size(); ii++) {
97
98
99
100
101
102
        expectedForces[ii][0] = expectedForces[ii][1] = expectedForces[ii][2] = 0.0;
    }

    // calculates forces/energy

    *expectedEnergy = 0.0;
103
104
    for (int ii = 0; ii < amoebaBondForce.getNumBonds(); ii++) {
        computeAmoebaBondForce(ii, positions, amoebaBondForce, expectedForces, expectedEnergy);
105
106
107
    }
}

108
void compareWithExpectedForceAndEnergy(Context& context, AmoebaBondForce& amoebaBondForce, double tolerance, const std::string& idString) {
109
110
111

    std::vector<Vec3> expectedForces;
    double expectedEnergy;
112
    computeAmoebaBondForces(context, amoebaBondForce, expectedForces, &expectedEnergy);
113
114
115
116
   
    State state                      = context.getState(State::Forces | State::Energy);
    const std::vector<Vec3> forces   = state.getForces();

117
118
    for (unsigned int ii = 0; ii < forces.size(); ii++) {
        ASSERT_EQUAL_VEC(expectedForces[ii], forces[ii], tolerance);
119
    }
120
    ASSERT_EQUAL_TOL(expectedEnergy, state.getPotentialEnergy(), tolerance);
121
122
}

peastman's avatar
peastman committed
123
void testOneBond() {
124
125
126
127
128
129
130
131

    System system;

    system.addParticle(1.0);
    system.addParticle(1.0);

    LangevinIntegrator integrator(0.0, 0.1, 0.01);

132
    AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
133
134
135
136
137

    double bondLength = 1.5;
    double quadraticK = 1.0;
    double cubicK     = 2.0;
    double quarticicK = 3.0;
138
139
    amoebaBondForce->setAmoebaGlobalBondCubic(cubicK);
    amoebaBondForce->setAmoebaGlobalBondQuartic(quarticicK);
140
    amoebaBondForce->addBond(0, 1, bondLength, quadraticK);
141

142
    system.addForce(amoebaBondForce);
143
    Context context(system, integrator, Platform::getPlatformByName("CUDA"));
144
145
146
147
148
149
    std::vector<Vec3> positions(2);

    positions[0] = Vec3(0, 1, 0);
    positions[1] = Vec3(0, 0, 0);

    context.setPositions(positions);
150
    compareWithExpectedForceAndEnergy(context, *amoebaBondForce, TOL, "testOneBond");
151
152
}

153
void testTwoBond() {
154
155
156
157
158
159
160
161
162

    System system;

    system.addParticle(1.0);
    system.addParticle(1.0);
    system.addParticle(1.0);

    LangevinIntegrator integrator(0.0, 0.1, 0.01);

163
    AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
164
165
166
167
168

    double bondLength = 1.5;
    double quadraticK = 1.0;
    double cubicK     = 2.0;
    double quarticicK = 3.0;
169
170
    amoebaBondForce->setAmoebaGlobalBondCubic(cubicK);
    amoebaBondForce->setAmoebaGlobalBondQuartic(quarticicK);
171
172
    amoebaBondForce->addBond(0, 1, bondLength, quadraticK);
    amoebaBondForce->addBond(1, 2, bondLength, quadraticK);
173

174
    system.addForce(amoebaBondForce);
175
176
    Context context(system, integrator, Platform::getPlatformByName("CUDA"));
    //Context context(system, integrator, platform);
177
178
179
180
181
182
183
    std::vector<Vec3> positions(3);

    positions[0] = Vec3(0, 1, 0);
    positions[1] = Vec3(0, 0, 0);
    positions[2] = Vec3(1, 0, 1);

    context.setPositions(positions);
184
    compareWithExpectedForceAndEnergy(context, *amoebaBondForce, TOL, "testTwoBond");
185
186
187
188
189
190
191
192
    
    // Try changing the bond parameters and make sure it's still correct.
    
    amoebaBondForce->setBondParameters(0, 0, 1, 1.1*bondLength, 1.4*quadraticK);
    amoebaBondForce->setBondParameters(1, 1, 2, 1.2*bondLength, 0.9*quadraticK);
    bool exceptionThrown = false;
    try {
        // This should throw an exception.
193
        compareWithExpectedForceAndEnergy(context, *amoebaBondForce, TOL, "testTwoBond");
194
195
196
197
198
199
    }
    catch (std::exception ex) {
        exceptionThrown = true;
    }
    ASSERT(exceptionThrown);
    amoebaBondForce->updateParametersInContext(context);
200
    compareWithExpectedForceAndEnergy(context, *amoebaBondForce, TOL, "testTwoBond");
201
202
}

203
204
205
206
207
208
209
210
211
212
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
238
239
240
241
242
243
244
245
void testPeriodic() {
    // Create a force that uses periodic boundary conditions, then compare to an identical custom force.
    
    System system;
    system.setDefaultPeriodicBoxVectors(Vec3(3, 0, 0), Vec3(0, 3, 0), Vec3(0, 0, 3));
    int numParticles = 2;
    for (int ii = 0; ii < numParticles; ii++)
        system.addParticle(1.0);
    LangevinIntegrator integrator(0.0, 0.1, 0.01);
    AmoebaBondForce* amoebaBondForce = new AmoebaBondForce();
    double bondLength = 1.5;
    double quadraticK = 1.0;
    double cubicK     = 2.0;
    double quarticK   = 3.0;
    amoebaBondForce->setAmoebaGlobalBondCubic(cubicK);
    amoebaBondForce->setAmoebaGlobalBondQuartic(quarticK);
    amoebaBondForce->addBond(0, 1, bondLength, quadraticK);
    amoebaBondForce->setUsesPeriodicBoundaryConditions(true);
    system.addForce(amoebaBondForce);
    CustomBondForce* customForce = new CustomBondForce("k2*delta^2 + k3*delta^3 + k4*delta^4; delta=r-r0");
    customForce->addGlobalParameter("r0", bondLength);
    customForce->addGlobalParameter("k2", quadraticK);
    customForce->addGlobalParameter("k3", cubicK);
    customForce->addGlobalParameter("k4", quarticK);
    customForce->addBond(0, 1);
    customForce->setUsesPeriodicBoundaryConditions(true);
    customForce->setForceGroup(1);
    system.addForce(customForce);
    Context context(system, integrator, Platform::getPlatformByName("CUDA"));

    std::vector<Vec3> positions(numParticles);

    positions[0] = Vec3(0, 2, 0);
    positions[1] = Vec3(0, 0, 0);

    context.setPositions(positions);
    State s1 = context.getState(State::Forces | State::Energy, true, 1);
    State s2 = context.getState(State::Forces | State::Energy, true, 2);
    ASSERT_EQUAL_TOL(s2.getPotentialEnergy(), s1.getPotentialEnergy(), 1e-5);
    for (int i = 0; i < numParticles; i++)
        ASSERT_EQUAL_VEC(s2.getForces()[i], s1.getForces()[i], 1e-5);
}

246
int main(int argc, char* argv[]) {
247
    try {
248
        std::cout << "TestCudaAmoebaBondForce running test..." << std::endl;
249
        registerAmoebaCudaKernelFactories();
250
251
        if (argc > 1)
            Platform::getPlatformByName("CUDA").setPropertyDefaultValue("CudaPrecision", std::string(argv[1]));
peastman's avatar
peastman committed
252
        testTwoBond();
253
        testPeriodic();
254
255
256
257
258
259
260
261
    } catch(const std::exception& e) {
        std::cout << "exception: " << e.what() << std::endl;
        std::cout << "FAIL - ERROR.  Test failed." << std::endl;
        return 1;
    }
    std::cout << "Done" << std::endl;
    return 0;
}