"plugins/freeEnergy/vscode:/vscode.git/clone" did not exist on "5003591d7bd4816a9dde89dea97fced7192b8dcf"
TestCudaAmoebaAngleForce.cpp 12.3 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
/* -------------------------------------------------------------------------- *
2
 *                                   OpenMMAmoeba                             *
Mark Friedrichs's avatar
Mark Friedrichs committed
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
 * -------------------------------------------------------------------------- *
 * 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.                                     *
 * -------------------------------------------------------------------------- */

/**
33
 * This tests the Cuda implementation of CudaAmoebaAngleForce.
Mark Friedrichs's avatar
Mark Friedrichs committed
34
35
 */

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>

using namespace OpenMM;

const double TOL = 1e-5;
#define PI_M               3.141592653589
#define RADIAN            57.29577951308
#define RADIAN_TO_DEGREE  57.29577951308
#define DEGREE_TO_RADIAN   0.01745329252
#define RADIAN_INVERSE     0.01745329252

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

   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

   --------------------------------------------------------------------------------------- */
     
static void crossProductVector3( double* vectorX, double* vectorY, double* vectorZ ){

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

static void getPrefactorsGivenAngleCosine( double cosine, double idealAngle, double quadraticK, double cubicK,
                                           double quarticK, double penticK, double sexticK,
                                           double* dEdR, double* energyTerm, FILE* log ) {

    double angle;
    if( cosine >= 1.0 ){
        angle = 0.0f;
    } else if( cosine <= -1.0 ){
        angle = RADIAN*PI_M;
    } else {
        angle = RADIAN*acos(cosine);
    }

Mark Friedrichs's avatar
Mark Friedrichs committed
90
#ifdef AMOEBA_DEBUG
Mark Friedrichs's avatar
Mark Friedrichs committed
91
92
93
94
    if( log ){
        (void) fprintf( log, "getPrefactorsGivenAngleCosine: cosine=%10.3e angle=%10.3e ideal=%10.3e\n", cosine, angle, idealAngle ); 
        (void) fflush( log );
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
95
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

    double deltaIdeal         = angle - idealAngle;
    double deltaIdeal2        = deltaIdeal*deltaIdeal;
    double deltaIdeal3        = deltaIdeal*deltaIdeal2;
    double deltaIdeal4        = deltaIdeal2*deltaIdeal2;
 
    // deltaIdeal = r - r_0
 
    *dEdR        = ( 2.0                        +
                     3.0*cubicK*  deltaIdeal    +
                     4.0*quarticK*deltaIdeal2   +
                     5.0*penticK* deltaIdeal3   +
                     6.0*sexticK* deltaIdeal4     );
 
    *dEdR       *= RADIAN*quadraticK*deltaIdeal;
 

    *energyTerm  = 1.0f + cubicK* deltaIdeal    +
                          quarticK*deltaIdeal2   +
                          penticK* deltaIdeal3   +
                          sexticK* deltaIdeal4;
    *energyTerm *= quadraticK*deltaIdeal2;

    return;
}

122
static void computeAmoebaAngleForce(int bondIndex,  std::vector<Vec3>& positions, AmoebaAngleForce& amoebaAngleForce,
Mark Friedrichs's avatar
Mark Friedrichs committed
123
124
125
126
127
                                             std::vector<Vec3>& forces, double* energy, FILE* log ) {

    int particle1, particle2, particle3;
    double idealAngle;
    double quadraticK;
128
    amoebaAngleForce.getAngleParameters(bondIndex, particle1, particle2, particle3, idealAngle, quadraticK );
Mark Friedrichs's avatar
Mark Friedrichs committed
129

130
131
132
133
    double cubicK         = amoebaAngleForce.getAmoebaGlobalAngleCubic();
    double quarticK       = amoebaAngleForce.getAmoebaGlobalAngleQuartic();
    double penticK        = amoebaAngleForce.getAmoebaGlobalAnglePentic();
    double sexticK        = amoebaAngleForce.getAmoebaGlobalAngleSextic();
Mark Friedrichs's avatar
Mark Friedrichs committed
134

Mark Friedrichs's avatar
Mark Friedrichs committed
135
#ifdef AMOEBA_DEBUG
Mark Friedrichs's avatar
Mark Friedrichs committed
136
    if( log ){
137
        (void) fprintf( log, "computeAmoebaAngleForce: bond %d [%d %d %d] ang=%10.3f k=%10.3f [%10.3e %10.3e %10.3e %10.3e]\n", 
Mark Friedrichs's avatar
Mark Friedrichs committed
138
139
140
                             bondIndex, particle1, particle2, particle3, idealAngle, quadraticK, cubicK, quarticK, penticK, sexticK );
        (void) fflush( log );
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
141
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

    double deltaR[2][3];
    double r2_0 = 0.0;
    double r2_1 = 0.0;
    for( int ii = 0; ii < 3; ii++ ){

           deltaR[0][ii]    = positions[particle1][ii] - positions[particle2][ii];
           r2_0            += deltaR[0][ii]*deltaR[0][ii];

           deltaR[1][ii]    = positions[particle3][ii] - positions[particle2][ii];
           r2_1            += deltaR[1][ii]*deltaR[1][ii];

    }

    double pVector[3];
    crossProductVector3( deltaR[0], deltaR[1], pVector );
    double rp      = sqrt( pVector[0]*pVector[0] + pVector[1]*pVector[1] + pVector[2]*pVector[2] );
    if( rp < 1.0e-06 ){
       rp = 1.0e-06;
    }   
    double dot    = deltaR[0][0]*deltaR[1][0] + deltaR[0][1]*deltaR[1][1] + deltaR[0][2]*deltaR[1][2];
    double cosine = dot/sqrt(r2_0*r2_1);

Mark Friedrichs's avatar
Mark Friedrichs committed
165
#ifdef AMOEBA_DEBUG
Mark Friedrichs's avatar
Mark Friedrichs committed
166
167
168
169
    if( log ){
        (void) fprintf( log, "dot=%10.3e r2_0=%10.3e r2_1=%10.3e\n", dot, r2_0, r2_1 ); 
        (void) fflush( log );
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
170
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

    double dEdR;
    double energyTerm;
    getPrefactorsGivenAngleCosine( cosine, idealAngle, quadraticK, cubicK,
                                   quarticK, penticK, sexticK, &dEdR, &energyTerm, log );

    double termA  = -dEdR/(r2_0*rp);
    double termC  =  dEdR/(r2_1*rp);

    double deltaCrossP[3][3];
    crossProductVector3( deltaR[0], pVector, deltaCrossP[0] );
    crossProductVector3( deltaR[1], pVector, deltaCrossP[2] );
    for( int ii = 0; ii < 3; ii++ ){
        deltaCrossP[0][ii] *= termA;
        deltaCrossP[2][ii] *= termC;
        deltaCrossP[1][ii]  = -1.0*(deltaCrossP[0][ii] + deltaCrossP[2][ii]);
    }

    forces[particle1][0]       += deltaCrossP[0][0];
    forces[particle1][1]       += deltaCrossP[0][1];
    forces[particle1][2]       += deltaCrossP[0][2];

    forces[particle2][0]       += deltaCrossP[1][0];
    forces[particle2][1]       += deltaCrossP[1][1];
    forces[particle2][2]       += deltaCrossP[1][2];

    forces[particle3][0]       += deltaCrossP[2][0];
    forces[particle3][1]       += deltaCrossP[2][1];
    forces[particle3][2]       += deltaCrossP[2][2];

    *energy                    += energyTerm;
}

204
static void computeAmoebaAngleForces( Context& context, AmoebaAngleForce& amoebaAngleForce,
Mark Friedrichs's avatar
Mark Friedrichs committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
                                             std::vector<Vec3>& expectedForces, double* expectedEnergy, FILE* log ) {

    // get positions and zero forces

    State state = context.getState(State::Positions);
    std::vector<Vec3> positions = state.getPositions();
    expectedForces.resize( positions.size() );
    
    for( unsigned int ii = 0; ii < expectedForces.size(); ii++ ){
        expectedForces[ii][0] = expectedForces[ii][1] = expectedForces[ii][2] = 0.0;
    }

    // calculates forces/energy

    *expectedEnergy = 0.0;
220
221
    for( int ii = 0; ii < amoebaAngleForce.getNumAngles(); ii++ ){
        computeAmoebaAngleForce(ii, positions, amoebaAngleForce, expectedForces, expectedEnergy, log );
Mark Friedrichs's avatar
Mark Friedrichs committed
222
223
    }

Mark Friedrichs's avatar
Mark Friedrichs committed
224
#ifdef AMOEBA_DEBUG
Mark Friedrichs's avatar
Mark Friedrichs committed
225
    if( log ){
226
        (void) fprintf( log, "computeAmoebaAngleForces: expected energy=%14.7e\n", *expectedEnergy );
Mark Friedrichs's avatar
Mark Friedrichs committed
227
228
229
230
231
        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] );
        }
        (void) fflush( log );
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
232
233
#endif

Mark Friedrichs's avatar
Mark Friedrichs committed
234
235
236
237
    return;

}

238
void compareWithExpectedForceAndEnergy( Context& context, AmoebaAngleForce& amoebaAngleForce,
Mark Friedrichs's avatar
Mark Friedrichs committed
239
240
241
242
                                        double tolerance, const std::string& idString, FILE* log) {

    std::vector<Vec3> expectedForces;
    double expectedEnergy;
243
    computeAmoebaAngleForces( context, amoebaAngleForce, expectedForces, &expectedEnergy, log );
Mark Friedrichs's avatar
Mark Friedrichs committed
244
245
246
247
   
    State state                      = context.getState(State::Forces | State::Energy);
    const std::vector<Vec3> forces   = state.getForces();

Mark Friedrichs's avatar
Mark Friedrichs committed
248
#ifdef AMOEBA_DEBUG
Mark Friedrichs's avatar
Mark Friedrichs committed
249
    if( log ){
250
        (void) fprintf( log, "computeAmoebaAngleForces: expected energy=%14.7e %14.7e\n", expectedEnergy, state.getPotentialEnergy() );
Mark Friedrichs's avatar
Mark Friedrichs committed
251
252
253
254
255
256
        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] );
        }
        (void) fflush( log );
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
257
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

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

void testOneAngle( FILE* log ) {

    System system;
    int numberOfParticles = 3;
    for( int ii = 0; ii < numberOfParticles; ii++ ){
        system.addParticle(1.0);
    }

    LangevinIntegrator integrator(0.0, 0.1, 0.01);

275
    AmoebaAngleForce* amoebaAngleForce = new AmoebaAngleForce();
Mark Friedrichs's avatar
Mark Friedrichs committed
276
277
278
279
280
281
282

    double angle      = 100.0;
    double quadraticK = 1.0;
    double cubicK     = 1.0e-01;
    double quarticK   = 1.0e-02;
    double penticK    = 1.0e-03;
    double sexticK    = 1.0e-04;
283
    amoebaAngleForce->addAngle(0, 1, 2, angle, quadraticK);
Mark Friedrichs's avatar
Mark Friedrichs committed
284

285
286
287
288
    amoebaAngleForce->setAmoebaGlobalAngleCubic(cubicK);
    amoebaAngleForce->setAmoebaGlobalAngleQuartic(quarticK);
    amoebaAngleForce->setAmoebaGlobalAnglePentic(penticK);
    amoebaAngleForce->setAmoebaGlobalAngleSextic(sexticK);
Mark Friedrichs's avatar
Mark Friedrichs committed
289

290
    system.addForce(amoebaAngleForce);
Mark Friedrichs's avatar
Mark Friedrichs committed
291
292
293
294
295
296
297
298
299
    Context context(system, integrator, Platform::getPlatformByName( "Cuda"));

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

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

    context.setPositions(positions);
300
    compareWithExpectedForceAndEnergy( context, *amoebaAngleForce, TOL, "testOneAngle", log );
Mark Friedrichs's avatar
Mark Friedrichs committed
301
302
303
304
305
306

}

int main( int numberOfArguments, char* argv[] ) {

    try {
307
        std::cout << "TestCudaAmoebaAngleForce running test..." << std::endl;
308
        registerAmoebaCudaKernelFactories();
Mark Friedrichs's avatar
Mark Friedrichs committed
309
        FILE* log = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
310
311
        testOneAngle( log );

312
    } catch(const std::exception& e) {
Mark Friedrichs's avatar
Mark Friedrichs committed
313
314
315
316
        std::cout << "exception: " << e.what() << std::endl;
        std::cout << "FAIL - ERROR.  Test failed." << std::endl;
        return 1;
    }
317
    std::cout << "Done" << std::endl;
Mark Friedrichs's avatar
Mark Friedrichs committed
318
319
    return 0;
}