TestReferenceAmoebaStretchBendForce.cpp 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 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 ReferenceAmoebaStretchBendForce.
 */

36
#include "openmm/internal/AssertionUtilities.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
37
38
39
//#include "AmoebaTinkerParameterFile.h"
const double DegreesToRadians = 3.14159265/180.0;
#include "openmm/Context.h"
40
#include "OpenMMAmoeba.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
41
42
43
44
45
46
47
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>

using namespace OpenMM;

48
49
extern "C" OPENMM_EXPORT void registerAmoebaReferenceKernelFactories();

Mark Friedrichs's avatar
Mark Friedrichs committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const double TOL = 1e-4;
#define PI_M               3.141592653589
#define RADIAN            57.29577951308

/* ---------------------------------------------------------------------------------------

   Compute cross product of two 3-vectors and place in 3rd vector

   vectorZ = vectorX x vectorY

   @param vectorX             x-vector
   @param vectorY             y-vector
   @param vectorZ             z-vector

   @return vector is vectorZ

   --------------------------------------------------------------------------------------- */
     
68
static void crossProductVector3(double* vectorX, double* vectorY, double* vectorZ) {
Mark Friedrichs's avatar
Mark Friedrichs committed
69
70
71
72
73
74
75
76

    vectorZ[0]  = vectorX[1]*vectorY[2] - vectorX[2]*vectorY[1];
    vectorZ[1]  = vectorX[2]*vectorY[0] - vectorX[0]*vectorY[2];
    vectorZ[2]  = vectorX[0]*vectorY[1] - vectorX[1]*vectorY[0];

    return;
}

77
static double dotVector3(double* vectorX, double* vectorY) {
Mark Friedrichs's avatar
Mark Friedrichs committed
78
79
80
81
82
    return vectorX[0]*vectorY[0] + vectorX[1]*vectorY[1] + vectorX[2]*vectorY[2];
}


static void computeAmoebaStretchBendForce(int bondIndex,  std::vector<Vec3>& positions, AmoebaStretchBendForce& amoebaStretchBendForce,
83
                                          std::vector<Vec3>& forces, double* energy, FILE* log) {
Mark Friedrichs's avatar
Mark Friedrichs committed
84
85

    int particle1, particle2, particle3;
86
    double abBondLength, cbBondLength, angleStretchBend, kStretchBend, k2StretchBend;
Mark Friedrichs's avatar
Mark Friedrichs committed
87

88
    amoebaStretchBendForce.getStretchBendParameters(bondIndex, particle1, particle2, particle3, abBondLength, cbBondLength, angleStretchBend, kStretchBend, k2StretchBend);
Mark Friedrichs's avatar
Mark Friedrichs committed
89
    angleStretchBend *= RADIAN;
Mark Friedrichs's avatar
Mark Friedrichs committed
90
#ifdef AMOEBA_DEBUG
91
92
93
94
    if (log) {
        (void) fprintf(log, "computeAmoebaStretchBendForce: bond %d [%d %d %d] ab=%10.3e cb=%10.3e angle=%10.3e k1=%10.3e k2=%10.3e\n",
                             bondIndex, particle1, particle2, particle3, abBondLength, cbBondLength, angleStretchBend, kStretchBend, k2StretchBend);
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
95
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
96
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
97
98
99
100
101
102
103
104
105
106
107
108

    enum { A, B, C, LastAtomIndex };
    enum { AB, CB, CBxAB, ABxP, CBxP, LastDeltaIndex };
 
    // ---------------------------------------------------------------------------------------
 
    // get deltaR between various combinations of the 3 atoms
    // and various intermediate terms
 
    double deltaR[LastDeltaIndex][3];
    double rAB2 = 0.0;
    double rCB2 = 0.0;
109
    for (int ii = 0; ii < 3; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
110
111
112
113
114
115
         deltaR[AB][ii]  = positions[particle1][ii] - positions[particle2][ii];
         rAB2           += deltaR[AB][ii]*deltaR[AB][ii];

         deltaR[CB][ii]  = positions[particle3][ii] - positions[particle2][ii];
         rCB2           += deltaR[CB][ii]*deltaR[CB][ii];
    }
116
117
    double rAB   = sqrt(rAB2);
    double rCB   = sqrt(rCB2);
Mark Friedrichs's avatar
Mark Friedrichs committed
118

119
120
121
    crossProductVector3(deltaR[CB], deltaR[AB], deltaR[CBxAB]);
    double  rP   = dotVector3(deltaR[CBxAB], deltaR[CBxAB]);
            rP   = sqrt(rP);
Mark Friedrichs's avatar
Mark Friedrichs committed
122
 
123
    if (rP <= 0.0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
124
125
       return;
    }
126
    double dot    = dotVector3(deltaR[CB], deltaR[AB]);
Mark Friedrichs's avatar
Mark Friedrichs committed
127
128
129
    double cosine = dot/(rAB*rCB);
 
    double angle;
130
    if (cosine >= 1.0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
131
       angle = 0.0;
Jason Swails's avatar
More  
Jason Swails committed
132
    }
133
    else if (cosine <= -1.0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
134
       angle = PI_M;
Jason Swails's avatar
More  
Jason Swails committed
135
136
    }
    else {
Mark Friedrichs's avatar
Mark Friedrichs committed
137
138
139
140
141
142
143
144
       angle = RADIAN*acos(cosine);
    }
 
    double termA = -RADIAN/(rAB2*rP);
    double termC =  RADIAN/(rCB2*rP);
 
    // P = CBxAB
 
145
146
147
    crossProductVector3(deltaR[AB], deltaR[CBxAB], deltaR[ABxP]);
    crossProductVector3(deltaR[CB], deltaR[CBxAB], deltaR[CBxP]);
    for (int ii = 0; ii < 3; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
148
149
150
151
       deltaR[ABxP][ii] *= termA;
       deltaR[CBxP][ii] *= termC;
    }
 
152
153
    double dr1   = rAB - abBondLength;
    double dr2   = rCB - cbBondLength;
Mark Friedrichs's avatar
Mark Friedrichs committed
154
155
156
157
 
    termA        = 1.0/rAB;
    termC        = 1.0/rCB;
 
158
    double drkk = dr1 * kStretchBend + dr2 * k2StretchBend;
Mark Friedrichs's avatar
Mark Friedrichs committed
159
160
161
162
163
164
 
    // ---------------------------------------------------------------------------------------
 
    // forces
 
    // calculate forces for atoms a, b, c
165
    // the force for b is then -(a + c)
Mark Friedrichs's avatar
Mark Friedrichs committed
166
167
168
 
    double subForce[LastAtomIndex][3];
    double dt = angle - angleStretchBend;
169
    for (int jj = 0; jj < 3; jj++) {
170
171
        subForce[A][jj] = kStretchBend*dt*termA*deltaR[AB][jj] + drkk*deltaR[ABxP][jj];
        subForce[C][jj] = k2StretchBend*dt*termC*deltaR[CB][jj] + drkk*deltaR[CBxP][jj];
172
        subForce[B][jj] = -(subForce[A][jj] + subForce[C][jj]);
Mark Friedrichs's avatar
Mark Friedrichs committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    }
 
    // ---------------------------------------------------------------------------------------
 
    // accumulate forces and energy
 
    forces[particle1][0]       -= subForce[0][0];
    forces[particle1][1]       -= subForce[0][1];
    forces[particle1][2]       -= subForce[0][2];

    forces[particle2][0]       -= subForce[1][0];
    forces[particle2][1]       -= subForce[1][1];
    forces[particle2][2]       -= subForce[1][2];

    forces[particle3][0]       -= subForce[2][0];
    forces[particle3][1]       -= subForce[2][1];
    forces[particle3][2]       -= subForce[2][2];

191
    *energy                    += dt*drkk;
Mark Friedrichs's avatar
Mark Friedrichs committed
192
#ifdef AMOEBA_DEBUG
193
194
195
    if (log) {
        (void) fprintf(log, "computeAmoebaStretchBendForce: angle=%10.3e dt=%10.3e dr=%10.3e\n", angle, dt, dr); 
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
196
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
197
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
198
199
200
201

    return;
}
 
202
203
static void computeAmoebaStretchBendForces(Context& context, AmoebaStretchBendForce& amoebaStretchBendForce,
                                           std::vector<Vec3>& expectedForces, double* expectedEnergy, FILE* log) {
Mark Friedrichs's avatar
Mark Friedrichs committed
204
205
206
207
208

    // get positions and zero forces

    State state                 = context.getState(State::Positions);
    std::vector<Vec3> positions = state.getPositions();
209
    expectedForces.resize(positions.size());
Mark Friedrichs's avatar
Mark Friedrichs committed
210
    
211
    for (unsigned int ii = 0; ii < expectedForces.size(); ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
212
213
214
215
216
217
        expectedForces[ii][0] = expectedForces[ii][1] = expectedForces[ii][2] = 0.0;
    }

    // calculates forces/energy

    *expectedEnergy = 0.0;
218
219
    for (int ii = 0; ii < amoebaStretchBendForce.getNumStretchBends(); ii++) {
        computeAmoebaStretchBendForce(ii, positions, amoebaStretchBendForce, expectedForces, expectedEnergy, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
220
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
221
#ifdef AMOEBA_DEBUG
222
223
224
225
    if (log) {
        (void) fprintf(log, "computeAmoebaStretchBendForces: expected energy=%14.7e\n", *expectedEnergy);
        for (unsigned int ii = 0; ii < positions.size(); ii++) {
            (void) fprintf(log, "%6u [%14.7e %14.7e %14.7e]\n", ii, expectedForces[ii][0], expectedForces[ii][1], expectedForces[ii][2]);
Mark Friedrichs's avatar
Mark Friedrichs committed
226
        }
227
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
228
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
229
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
230
231
232
233
    return;

}

234
235
void compareWithExpectedForceAndEnergy(Context& context, AmoebaStretchBendForce& amoebaStretchBendForce,
                                       double tolerance, const std::string& idString, FILE* log) {
Mark Friedrichs's avatar
Mark Friedrichs committed
236
237
238

    std::vector<Vec3> expectedForces;
    double expectedEnergy;
239
    computeAmoebaStretchBendForces(context, amoebaStretchBendForce, expectedForces, &expectedEnergy, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
240
241
242
   
    State state                      = context.getState(State::Forces | State::Energy);
    const std::vector<Vec3> forces   = state.getForces();
Mark Friedrichs's avatar
Mark Friedrichs committed
243
#ifdef AMOEBA_DEBUG
244
245
246
247
248
    if (log) {
        (void) fprintf(log, "computeAmoebaStretchBendForces: expected energy=%14.7e %14.7e\n", expectedEnergy, state.getPotentialEnergy());
        for (unsigned int ii = 0; ii < forces.size(); ii++) {
            (void) fprintf(log, "%6u [%14.7e %14.7e %14.7e]   [%14.7e %14.7e %14.7e]\n", ii,
                            expectedForces[ii][0], expectedForces[ii][1], expectedForces[ii][2], forces[ii][0], forces[ii][1], forces[ii][2]);
Mark Friedrichs's avatar
Mark Friedrichs committed
249
        }
250
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
251
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
252
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
253

254
255
    for (unsigned int ii = 0; ii < forces.size(); ii++) {
        ASSERT_EQUAL_VEC(expectedForces[ii], forces[ii], tolerance);
Mark Friedrichs's avatar
Mark Friedrichs committed
256
    }
257
    ASSERT_EQUAL_TOL(expectedEnergy, state.getPotentialEnergy(), tolerance);
Mark Friedrichs's avatar
Mark Friedrichs committed
258
259
}

260
void testOneStretchBend(FILE* log) {
Mark Friedrichs's avatar
Mark Friedrichs committed
261
262
263

    System system;
    int numberOfParticles = 3;
264
    for (int ii = 0; ii < numberOfParticles; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
265
266
267
268
269
270
271
272
273
274
275
276
277
        system.addParticle(1.0);
    }

    LangevinIntegrator integrator(0.0, 0.1, 0.01);

    AmoebaStretchBendForce* amoebaStretchBendForce = new AmoebaStretchBendForce();

    double abLength         = 0.144800000E+01;
    double cbLength         = 0.101500000E+01;
    double angleStretchBend = 0.108500000E+03*DegreesToRadians;
    //double kStretchBend     = 0.750491578E-01;
    double kStretchBend     = 1.0;

278
    amoebaStretchBendForce->addStretchBend(0, 1, 2, abLength, cbLength, angleStretchBend, kStretchBend, kStretchBend);
Mark Friedrichs's avatar
Mark Friedrichs committed
279
280

    system.addForce(amoebaStretchBendForce);
281
282
    ASSERT(!amoebaStretchBendForce->usesPeriodicBoundaryConditions());
    ASSERT(!system.usesPeriodicBoundaryConditions());
283
    Context context(system, integrator, Platform::getPlatformByName("Reference"));
Mark Friedrichs's avatar
Mark Friedrichs committed
284
285
286

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

287
288
289
    positions[0] = Vec3(0.262660000E+02,  0.254130000E+02,  0.284200000E+01);
    positions[1] = Vec3(0.273400000E+02,  0.244300000E+02,  0.261400000E+01);
    positions[2] = Vec3(0.269573220E+02,  0.236108860E+02,  0.216376800E+01);
Mark Friedrichs's avatar
Mark Friedrichs committed
290
291

    context.setPositions(positions);
292
    compareWithExpectedForceAndEnergy(context, *amoebaStretchBendForce, TOL, "testOneStretchBend", log);
293
294
295
    
    // Try changing the stretch-bend parameters and make sure it's still correct.
    
296
    amoebaStretchBendForce->setStretchBendParameters(0, 0, 1, 2, 1.1*abLength, 1.2*cbLength, 1.3*angleStretchBend, 1.4*kStretchBend, 1.4*kStretchBend);
297
298
299
    bool exceptionThrown = false;
    try {
        // This should throw an exception.
300
        compareWithExpectedForceAndEnergy(context, *amoebaStretchBendForce, TOL, "testOneStretchBend", log);
301
302
303
304
305
306
    }
    catch (std::exception ex) {
        exceptionThrown = true;
    }
    ASSERT(exceptionThrown);
    amoebaStretchBendForce->updateParametersInContext(context);
307
    compareWithExpectedForceAndEnergy(context, *amoebaStretchBendForce, TOL, "testOneStretchBend", log);
Mark Friedrichs's avatar
Mark Friedrichs committed
308
309
}

310
int main(int numberOfArguments, char* argv[]) {
Mark Friedrichs's avatar
Mark Friedrichs committed
311
312
313

    try {
        std::cout << "TestReferenceAmoebaStretchBendForce running test..." << std::endl;
314
        registerAmoebaReferenceKernelFactories();
Mark Friedrichs's avatar
Mark Friedrichs committed
315

Mark Friedrichs's avatar
Mark Friedrichs committed
316
317
        FILE* log = NULL;
        //FILE* log = stderr;
318
319
        //FILE* log = fopen("AmoebaStretchBendForce1.log", "w");;
        testOneStretchBend(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
320
#ifdef AMOEBA_DEBUG
321
322
        if (log && log != stderr)
            (void) fclose(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
323
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
324
325
326
327
328
329
330

    }
    catch(const std::exception& e) {
        std::cout << "exception: " << e.what() << std::endl;
        std::cout << "FAIL - ERROR.  Test failed." << std::endl;
        return 1;
    }
331
332
    //std::cout << "PASS - Test succeeded." << std::endl;
    std::cout << "Done" << std::endl;
Mark Friedrichs's avatar
Mark Friedrichs committed
333
334
    return 0;
}