TestReferenceAmoebaOutOfPlaneBendForce.cpp 21.6 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 ReferenceAmoebaOutOfPlaneBendForce.
 */

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

using namespace OpenMM;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const double TOL = 1e-3;
#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

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

    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;
}

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


static void computeAmoebaOutOfPlaneBendForce(int bondIndex,  std::vector<Vec3>& positions, AmoebaOutOfPlaneBendForce& amoebaOutOfPlaneBendForce,
82
                                             std::vector<Vec3>& forces, double* energy, FILE* log) {
Mark Friedrichs's avatar
Mark Friedrichs committed
83
84
85
86
87
88
89
90
91


    double kAngleCubic     = amoebaOutOfPlaneBendForce.getAmoebaGlobalOutOfPlaneBendCubic();
    double kAngleQuartic   = amoebaOutOfPlaneBendForce.getAmoebaGlobalOutOfPlaneBendQuartic();
    double kAnglePentic    = amoebaOutOfPlaneBendForce.getAmoebaGlobalOutOfPlaneBendPentic();
    double kAngleSextic    = amoebaOutOfPlaneBendForce.getAmoebaGlobalOutOfPlaneBendSextic();

    int particle1, particle2, particle3, particle4;
    double kAngleQuadratic;
92
    amoebaOutOfPlaneBendForce.getOutOfPlaneBendParameters(bondIndex, particle1, particle2, particle3, particle4, kAngleQuadratic);
Mark Friedrichs's avatar
Mark Friedrichs committed
93
#ifdef AMOEBA_DEBUG
94
95
96
97
    if (log) {
        (void) fprintf(log, "computeAmoebaOutOfPlaneBendForce: bond %d [%d %d %d %d] k=[%10.3e %10.3e %10.3e %10.3e %10.3e]\n", 
                             bondIndex, particle1, particle2, particle3, particle4, kAngleQuadratic, kAngleCubic, kAngleQuartic, kAnglePentic, kAngleSextic);
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
98
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
99
100
#endif

Mark Friedrichs's avatar
Mark Friedrichs committed
101
102
103
104
105
106
107
108
109
110

    enum { A, B, C, D, LastAtomIndex };
    enum { AB, CB, DB, AD, CD, LastDeltaIndex };
 
    // ---------------------------------------------------------------------------------------
 
    // get deltaR between various combinations of the 4 atoms
    // and various intermediate terms
 
    double deltaR[LastDeltaIndex][6];
111
    for (int ii = 0; ii < 3; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
112
113
114
115
116
         deltaR[AB][ii] = positions[particle1][ii] - positions[particle2][ii];
         deltaR[CB][ii] = positions[particle3][ii] - positions[particle2][ii];
         deltaR[DB][ii] = positions[particle4][ii] - positions[particle2][ii];
         deltaR[AD][ii] = positions[particle1][ii] - positions[particle4][ii];
         deltaR[CD][ii] = positions[particle3][ii] - positions[particle4][ii];
Jason Swails's avatar
More  
Jason Swails committed
117
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
118

119
120
121
    double rDB2  = dotVector3(deltaR[DB], deltaR[DB]);
    double rAD2  = dotVector3(deltaR[AD], deltaR[AD]);
    double rCD2  = dotVector3(deltaR[CD], deltaR[CD]);
Mark Friedrichs's avatar
Mark Friedrichs committed
122
123
 
    double tempVector[3];
124
125
126
    crossProductVector3(deltaR[CB], deltaR[DB], tempVector);
    double   eE  = dotVector3(deltaR[AB], tempVector );
    double  dot  = dotVector3(deltaR[AD],  deltaR[CD]);
Mark Friedrichs's avatar
Mark Friedrichs committed
127
128
    double   cc  = rAD2*rCD2 - dot*dot;
 
129
    if (rDB2 <= 0.0 || cc == 0.0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
130
131
132
133
134
       return;
    }
    double bkk2   = rDB2 - eE*eE/cc;
    double cosine = sqrt(bkk2/rDB2);
    double angle;
135
    if (cosine >= 1.0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
136
        angle = 0.0;
Jason Swails's avatar
More  
Jason Swails committed
137
    }
138
    else if (cosine <= -1.0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
139
        angle = PI_M;
Jason Swails's avatar
More  
Jason Swails committed
140
141
    }
    else {
Mark Friedrichs's avatar
Mark Friedrichs committed
142
143
        angle = RADIAN*acos(cosine);
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
144
#ifdef AMOEBA_DEBUG 
145
146
147
148
    if (log) {
        (void) fprintf(log, "computeAmoebaOutOfPlaneBendForce: bkk2=%14.7e rDB2=%14.7e cos=%14.7e dt=%14.7e]\n", 
                             bkk2, rDB2, cosine, angle);
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
149
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
150
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
151
152
153
154
155
156
157
158
159
160
161
162
163

    // chain rule
 
    double dt    = angle;
    double dt2   = dt*dt;
    double dt3   = dt2*dt;
    double dt4   = dt2*dt2;
 
    double dEdDt = 2.0 + 3.0*kAngleCubic*dt  + 4.0*kAngleQuartic*dt2 + 5.0*kAnglePentic *dt3 + 6.0*kAngleSextic *dt4;
     dEdDt      *= kAngleQuadratic*dt*RADIAN;
 
    double dEdCos;
    dEdCos       = dEdDt/sqrt(cc*bkk2);
164
    if (eE > 0.0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
165
166
167
168
169
170
        dEdCos *= -1.0;
    }
 
    double term = eE/cc;
 
    double dccd[LastAtomIndex][3];
171
    for (int ii = 0; ii < 3; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
172
173
174
175
176
177
        dccd[A][ii] = (deltaR[AD][ii]*rCD2 - deltaR[CD][ii]*dot)*term;
        dccd[C][ii] = (deltaR[CD][ii]*rAD2 - deltaR[AD][ii]*dot)*term;
        dccd[D][ii] = -1.0*(dccd[A][ii] + dccd[C][ii]);
    }
 
    double deed[LastAtomIndex][3];
178
179
180
    crossProductVector3(deltaR[DB], deltaR[CB], deed[A]);
    crossProductVector3(deltaR[AB], deltaR[DB], deed[C]);
    crossProductVector3(deltaR[CB], deltaR[AB], deed[D]);
Mark Friedrichs's avatar
Mark Friedrichs committed
181
182
183
184
185
186
187
188
189
190
191
 
    term        = eE/rDB2;
    deed[D][0] += deltaR[DB][0]*term;
    deed[D][1] += deltaR[DB][1]*term;
    deed[D][2] += deltaR[DB][2]*term;
 
    // ---------------------------------------------------------------------------------------
 
    // forces
 
    // calculate forces for atoms a, c, d
192
    // the force for b is then -(a+ c + d)
Mark Friedrichs's avatar
Mark Friedrichs committed
193
194
195
 
    double subForce[LastAtomIndex][3];
 
196
    for (int jj = 0; jj < LastAtomIndex; jj++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
197
198
199
 
        // A, C, D
  
200
201
        for (int ii = 0; ii < 3; ii++) {
            subForce[jj][ii] = dEdCos*(dccd[jj][ii] + deed[jj][ii]);
Mark Friedrichs's avatar
Mark Friedrichs committed
202
203
        }
  
204
        if (jj == 0)jj++; // skip B
Mark Friedrichs's avatar
Mark Friedrichs committed
205
206
207
  
        // now compute B
  
208
209
        if (jj == 3) {
           for (int ii = 0; ii < 3; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
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
               subForce[1][ii] = -1.0*(subForce[0][ii] + subForce[2][ii] + subForce[3][ii]);
           }
        }
    }
 
    // 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];
    
    forces[particle4][0]       -= subForce[3][0];
    forces[particle4][1]       -= subForce[3][1];
    forces[particle4][2]       -= subForce[3][2];
    
    // ---------------------------------------------------------------------------------------
 
    // calculate energy if 'energy' is set
 
    double energyTerm  = 1.0 + kAngleCubic  *dt  +
                               kAngleQuartic*dt2 +
                               kAnglePentic *dt3 +
                               kAngleSextic *dt4;
    energyTerm        *= kAngleQuadratic*dt2;
    *energy           += energyTerm;
    return;
}
 
246
247
static void computeAmoebaOutOfPlaneBendForces(Context& context, AmoebaOutOfPlaneBendForce& amoebaOutOfPlaneBendForce,
                                              std::vector<Vec3>& expectedForces, double* expectedEnergy, FILE* log) {
Mark Friedrichs's avatar
Mark Friedrichs committed
248
249
250
251
252

    // get positions and zero forces

    State state                 = context.getState(State::Positions);
    std::vector<Vec3> positions = state.getPositions();
253
    expectedForces.resize(positions.size());
Mark Friedrichs's avatar
Mark Friedrichs committed
254
    
255
    for (unsigned int ii = 0; ii < expectedForces.size(); ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
256
257
258
259
260
261
        expectedForces[ii][0] = expectedForces[ii][1] = expectedForces[ii][2] = 0.0;
    }

    // calculates forces/energy

    *expectedEnergy = 0.0;
262
263
    for (int ii = 0; ii < amoebaOutOfPlaneBendForce.getNumOutOfPlaneBends(); ii++) {
        computeAmoebaOutOfPlaneBendForce(ii, positions, amoebaOutOfPlaneBendForce, expectedForces, expectedEnergy, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
264
265
    }

Mark Friedrichs's avatar
Mark Friedrichs committed
266
#ifdef AMOEBA_DEBUG
267
268
269
270
    if (log) {
        (void) fprintf(log, "computeAmoebaOutOfPlaneBendForces: 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
271
        }
272
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
273
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
274
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
275
276
277
278
    return;

}

279
280
void compareWithExpectedForceAndEnergy(Context& context, AmoebaOutOfPlaneBendForce& amoebaOutOfPlaneBendForce,
                                       double tolerance, const std::string& idString, FILE* log) {
Mark Friedrichs's avatar
Mark Friedrichs committed
281
282
283

    std::vector<Vec3> expectedForces;
    double expectedEnergy;
284
    computeAmoebaOutOfPlaneBendForces(context, amoebaOutOfPlaneBendForce, expectedForces, &expectedEnergy, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
285
286
287
   
    State state                      = context.getState(State::Forces | State::Energy);
    const std::vector<Vec3> forces   = state.getForces();
Mark Friedrichs's avatar
Mark Friedrichs committed
288
#ifdef AMOEBA_DEBUG
289
290
291
292
293
    if (log) {
        (void) fprintf(log, "computeAmoebaOutOfPlaneBendForces: 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
294
        }
295
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
296
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
297
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
298

299
300
    for (unsigned int ii = 0; ii < forces.size(); ii++) {
        ASSERT_EQUAL_VEC(expectedForces[ii], forces[ii], tolerance);
Mark Friedrichs's avatar
Mark Friedrichs committed
301
    }
302
    ASSERT_EQUAL_TOL(expectedEnergy, state.getPotentialEnergy(), tolerance);
Mark Friedrichs's avatar
Mark Friedrichs committed
303
304
}

305
void testOneOutOfPlaneBend(FILE* log) {
Mark Friedrichs's avatar
Mark Friedrichs committed
306
307
308

    System system;
    int numberOfParticles = 4;
309
    for (int ii = 0; ii < numberOfParticles; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
310
311
312
313
314
315
316
        system.addParticle(1.0);
    }

    LangevinIntegrator integrator(0.0, 0.1, 0.01);

    AmoebaOutOfPlaneBendForce* amoebaOutOfPlaneBendForce = new AmoebaOutOfPlaneBendForce();

317
318
319
320
    amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendCubic(  -0.1400000E-01);
    amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendQuartic( 0.5600000E-04);
    amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendPentic( -0.7000000E-06);
    amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendSextic(  0.2200000E-07);
Mark Friedrichs's avatar
Mark Friedrichs committed
321
322

    double kOutOfPlaneBend = 0.328682196E-01;
323
    amoebaOutOfPlaneBendForce->addOutOfPlaneBend(0, 1, 2, 3, kOutOfPlaneBend);
Mark Friedrichs's avatar
Mark Friedrichs committed
324
325

    system.addForce(amoebaOutOfPlaneBendForce);
326
327
    ASSERT(!amoebaOutOfPlaneBendForce->usesPeriodicBoundaryConditions());
    ASSERT(!system.usesPeriodicBoundaryConditions());
328
    Context context(system, integrator, Platform::getPlatformByName("Reference"));
Mark Friedrichs's avatar
Mark Friedrichs committed
329
330
331

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

332
333
    positions[0] = Vec3(0.262660000E+02,  0.254130000E+02,  0.284200000E+01);
    positions[1] = Vec3(0.269130000E+02,  0.266390000E+02,  0.353100000E+01);
Mark Friedrichs's avatar
Mark Friedrichs committed
334

335
336
    positions[2] = Vec3(0.278860000E+02,  0.264630000E+02,  0.426300000E+01);
    positions[3] = Vec3(0.245568230E+02,  0.250215290E+02,  0.796852800E+01);
Mark Friedrichs's avatar
Mark Friedrichs committed
337
338

    context.setPositions(positions);
339
    compareWithExpectedForceAndEnergy(context, *amoebaOutOfPlaneBendForce, TOL, "testOneOutOfPlaneBend", log);
340
341
342
343
344
345
346
    
    // Try changing the bend parameters and make sure it's still correct.
    
    amoebaOutOfPlaneBendForce->setOutOfPlaneBendParameters(0, 0, 1, 2, 3, 1.1*kOutOfPlaneBend);
    bool exceptionThrown = false;
    try {
        // This should throw an exception.
347
        compareWithExpectedForceAndEnergy(context, *amoebaOutOfPlaneBendForce, TOL, "testOneOutOfPlaneBend", log);
348
349
350
351
352
353
    }
    catch (std::exception ex) {
        exceptionThrown = true;
    }
    ASSERT(exceptionThrown);
    amoebaOutOfPlaneBendForce->updateParametersInContext(context);
354
    compareWithExpectedForceAndEnergy(context, *amoebaOutOfPlaneBendForce, TOL, "testOneOutOfPlaneBend", log);
Mark Friedrichs's avatar
Mark Friedrichs committed
355
356
}

357
void testOneOutOfPlaneBend2(FILE* log, int setId) {
Mark Friedrichs's avatar
Mark Friedrichs committed
358
359
360

    System system;
    int numberOfParticles = 4;
361
    for (int ii = 0; ii < numberOfParticles; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
362
363
364
365
366
367
368
        system.addParticle(1.0);
    }

    LangevinIntegrator integrator(0.0, 0.1, 0.01);

    AmoebaOutOfPlaneBendForce* amoebaOutOfPlaneBendForce = new AmoebaOutOfPlaneBendForce();

369
370
371
372
    amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendCubic(  -0.1400000E-01);
    amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendQuartic( 0.5600000E-04);
    amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendPentic( -0.7000000E-06);
    amoebaOutOfPlaneBendForce->setAmoebaGlobalOutOfPlaneBendSextic(  0.2200000E-07);
Mark Friedrichs's avatar
Mark Friedrichs committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
   285    441    442    443    444  0.328682196E-01
   286    441    442    444    443  0.164493407E-01
   287    443    442    444    441  0.636650407E-02
   288    442    444    447    448  0.392956472E-02
   289    442    444    448    447  0.392956472E-02
   290    447    444    448    442  0.214755281E-01
  441  0.893800000E+01  0.439800000E+01  0.343100000E+01
  442  0.779100000E+01  0.614600000E+01  0.390100000E+01
  443  0.915400000E+01  0.683900000E+01  0.389400000E+01
  444  0.101770000E+02  0.619000000E+01  0.379900000E+01
  445  0.921000000E+01  0.813800000E+01  0.398600000E+01
  446  0.708500000E+01  0.672900000E+01  0.332700000E+01
  447  0.744300000E+01  0.605200000E+01  0.491900000E+01
  448  0.100820000E+02  0.859300000E+01  0.398200000E+01
  449  0.838000000E+01  0.866100000E+01  0.406000000E+01
*/

    std::map<int,Vec3> coordinates;
392
393
394
395
396
397
398
399
400
    coordinates[440] = Vec3( 0.893800000E+01,  0.439800000E+01,  0.343100000E+01);
    coordinates[441] = Vec3( 0.779100000E+01,  0.614600000E+01,  0.390100000E+01);
    coordinates[442] = Vec3( 0.915400000E+01,  0.683900000E+01,  0.389400000E+01);
    coordinates[443] = Vec3( 0.101770000E+02,  0.619000000E+01,  0.379900000E+01);
    coordinates[444] = Vec3( 0.921000000E+01,  0.813800000E+01,  0.398600000E+01);
    coordinates[445] = Vec3( 0.708500000E+01,  0.672900000E+01,  0.332700000E+01);
    coordinates[446] = Vec3( 0.744300000E+01,  0.605200000E+01,  0.491900000E+01);
    coordinates[447] = Vec3( 0.100820000E+02,  0.859300000E+01,  0.398200000E+01);
    coordinates[448] = Vec3( 0.838000000E+01,  0.866100000E+01,  0.406000000E+01);
Mark Friedrichs's avatar
Mark Friedrichs committed
401
402
403
    
    double kOutOfPlaneBend = 0.328682196E-01;
    std::vector<int> particleIndices;
404
405
406
407
408
    if (setId == 1) {
        particleIndices.push_back(441); 
        particleIndices.push_back(442); 
        particleIndices.push_back(443); 
        particleIndices.push_back(444); 
Mark Friedrichs's avatar
Mark Friedrichs committed
409
        kOutOfPlaneBend = 0.328682196E-01;
Jason Swails's avatar
More  
Jason Swails committed
410
    }
411
412
413
414
415
    else if (setId == 2) {
        particleIndices.push_back(441); 
        particleIndices.push_back(442); 
        particleIndices.push_back(444); 
        particleIndices.push_back(443); 
Mark Friedrichs's avatar
Mark Friedrichs committed
416
        kOutOfPlaneBend = 0.164493407E-01;
Jason Swails's avatar
More  
Jason Swails committed
417
    }
418
419
420
421
422
    else if (setId == 3) {
        particleIndices.push_back(443); 
        particleIndices.push_back(442); 
        particleIndices.push_back(444); 
        particleIndices.push_back(441); 
Mark Friedrichs's avatar
Mark Friedrichs committed
423
        kOutOfPlaneBend = 0.636650407E-02;
Jason Swails's avatar
More  
Jason Swails committed
424
    }
425
426
427
428
429
    else if (setId == 4) {
        particleIndices.push_back(442); 
        particleIndices.push_back(444); 
        particleIndices.push_back(447); 
        particleIndices.push_back(448); 
Mark Friedrichs's avatar
Mark Friedrichs committed
430
        kOutOfPlaneBend = 0.392956472E-02;
Jason Swails's avatar
More  
Jason Swails committed
431
    }
432
433
434
435
436
    else if (setId == 5) {
        particleIndices.push_back(442); 
        particleIndices.push_back(444); 
        particleIndices.push_back(448); 
        particleIndices.push_back(447); 
Mark Friedrichs's avatar
Mark Friedrichs committed
437
        kOutOfPlaneBend = 0.392956472E-02;
Jason Swails's avatar
More  
Jason Swails committed
438
    }
439
440
441
442
443
    else if (setId == 6) {
        particleIndices.push_back(447); 
        particleIndices.push_back(444); 
        particleIndices.push_back(448); 
        particleIndices.push_back(442); 
Mark Friedrichs's avatar
Mark Friedrichs committed
444
        kOutOfPlaneBend = 0.214755281E-01;
Jason Swails's avatar
More  
Jason Swails committed
445
446
    }
    else {
Mark Friedrichs's avatar
Mark Friedrichs committed
447
#ifdef AMOEBA_DEBUG
448
449
        if (log) {
            (void) fprintf(log, "Set id %d not recognized.\n", setId);
Mark Friedrichs's avatar
Mark Friedrichs committed
450
        }
Mark Friedrichs's avatar
Mark Friedrichs committed
451
452
453
#endif
        std::stringstream buffer;
        buffer << "Set id " << setId << " not recognized.";
454
        throw OpenMMException(buffer.str());
Mark Friedrichs's avatar
Mark Friedrichs committed
455
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
456
#ifdef AMOEBA_DEBUG
457
458
    if (log) {
        (void) fprintf(log, "Set id %d.\n", setId);
Mark Friedrichs's avatar
Mark Friedrichs committed
459
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
460
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
461

462
    amoebaOutOfPlaneBendForce->addOutOfPlaneBend(0, 1, 2, 3, kOutOfPlaneBend);
Mark Friedrichs's avatar
Mark Friedrichs committed
463
464

    system.addForce(amoebaOutOfPlaneBendForce);
465
    Context context(system, integrator, Platform::getPlatformByName("Reference"));
Mark Friedrichs's avatar
Mark Friedrichs committed
466
467
    std::vector<Vec3> positions(numberOfParticles);

468
469
    for (unsigned int ii = 0; ii < static_cast<unsigned int>(numberOfParticles); ii++) {
        if (coordinates.find(particleIndices[ii]) == coordinates.end()) {
Mark Friedrichs's avatar
Mark Friedrichs committed
470
#ifdef AMOEBA_DEBUG
471
472
            if (log) {
                (void) fprintf(log, "Coordinates %d not loaded.", particleIndices[ii]);
Mark Friedrichs's avatar
Mark Friedrichs committed
473
            }
Mark Friedrichs's avatar
Mark Friedrichs committed
474
475
476
#endif
            std::stringstream buffer;
            buffer << "Coordinates " << particleIndices[ii] << " not loaded.";
477
            throw OpenMMException(buffer.str());
Mark Friedrichs's avatar
Mark Friedrichs committed
478
479
480
481
482
        }
        positions[ii] = coordinates[particleIndices[ii]];
    }

    context.setPositions(positions);
483
    compareWithExpectedForceAndEnergy(context, *amoebaOutOfPlaneBendForce, TOL, "testOneOutOfPlaneBend", log);
Mark Friedrichs's avatar
Mark Friedrichs committed
484
485
486
487

    static int iter = 0;
    static std::map<int,Vec3> totalForces;
    static double totalEnergy;
488
489
490
491
492
493
494
495
496
497
498
    if (iter == 0) {

        totalForces[441] = Vec3( 0.0, 0.0, 0.0);
        totalForces[442] = Vec3( 0.0, 0.0, 0.0);
        totalForces[443] = Vec3( 0.0, 0.0, 0.0);
        totalForces[444] = Vec3( 0.0, 0.0, 0.0);
        totalForces[445] = Vec3( 0.0, 0.0, 0.0);
        totalForces[446] = Vec3( 0.0, 0.0, 0.0);
        totalForces[447] = Vec3( 0.0, 0.0, 0.0);
        totalForces[448] = Vec3( 0.0, 0.0, 0.0);
        totalForces[449] = Vec3( 0.0, 0.0, 0.0);
Mark Friedrichs's avatar
Mark Friedrichs committed
499
500
501
502
503
        totalEnergy      = 0.0;
    }
    iter++;

    std::vector<Vec3> forces;
504
    forces.resize(numberOfParticles);
Mark Friedrichs's avatar
Mark Friedrichs committed
505
    double energy;
506
    computeAmoebaOutOfPlaneBendForce(0, positions, *amoebaOutOfPlaneBendForce, forces, &energy, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
507
508

    totalEnergy += energy;
509
510
    for (unsigned int ii = 0; ii < static_cast<unsigned int>(numberOfParticles); ii++) {
        for (unsigned int jj = 0; jj < 3; jj++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
511
512
513
514
            totalForces[particleIndices[ii]][jj] += forces[ii][jj]; 
        }
    }

515
    if (iter == 6) {
Mark Friedrichs's avatar
Mark Friedrichs committed
516
#ifdef AMOEBA_DEBUG
517
518
519
    if (log) {
        (void) fprintf(log, "computeAmoebaOutOfPlaneBendForces: energy=%14.7e\n", totalEnergy);
        for (std::map<int,Vec3>::iterator ii = totalForces.begin(); ii != totalForces.end(); ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
520
521
            int particleIndex =  ii->first;
            Vec3 forces       =  ii->second;
522
523
            (void) fprintf(log, "%6d [%14.7e %14.7e %14.7e] \n", particleIndex,
                            forces[0], forces[1], forces[2]);
Mark Friedrichs's avatar
Mark Friedrichs committed
524
        }
525
        (void) fflush(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
526
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
527
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
528
529
530
531
    }

}

532
int main(int numberOfArguments, char* argv[]) {
Mark Friedrichs's avatar
Mark Friedrichs committed
533
534
535

    try {
        std::cout << "TestReferenceAmoebaOutOfPlaneBendForce running test..." << std::endl;
536
        registerAmoebaReferenceKernelFactories();
Mark Friedrichs's avatar
Mark Friedrichs committed
537

Mark Friedrichs's avatar
Mark Friedrichs committed
538
539
        //FILE* log = stderr;
        FILE* log = NULL;
540
        //FILE* log = fopen("AmoebaOutOfPlaneBendForce.log", "w");;
Mark Friedrichs's avatar
Mark Friedrichs committed
541

542
543
544
545
        testOneOutOfPlaneBend(log);
        //testOneOutOfPlaneBend2(log, atoi(argv[1]));
        //for (int ii = 1; ii <= 6; ii++) {
        //     testOneOutOfPlaneBend2(log, ii);
Mark Friedrichs's avatar
Mark Friedrichs committed
546
        //}
Mark Friedrichs's avatar
Mark Friedrichs committed
547
#ifdef AMOEBA_DEBUG
548
549
        if (log && log != stderr)
            (void) fclose(log);
Mark Friedrichs's avatar
Mark Friedrichs committed
550
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
551
552
553
554
555
556
557

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