TestCudaAmoebaHarmonicBondForce.cpp 9.23 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
33
34
35
 * -------------------------------------------------------------------------- *
 * 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 Cuda implementation of HarmonicBondForce.
 */

36
#include "openmm/internal/AssertionUtilities.h"
37
#include "CudaPlatform.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
38
39
#include "AmoebaTinkerParameterFile.h"
#include "openmm/Context.h"
40
#include "OpenMMAmoeba.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>

using namespace OpenMM;

const double TOL = 1e-5;
static void computeAmoebaHarmonicBondForce(int bondIndex,  std::vector<Vec3>& positions, AmoebaHarmonicBondForce& amoebaHarmonicBondForce,
                                           std::vector<Vec3>& forces, double* energy ) {

    int particle1, particle2;
    double bondLength;
    double quadraticK;
55
56
    double cubicK    = amoebaHarmonicBondForce.getAmoebaGlobalHarmonicBondCubic();
    double quarticK  = amoebaHarmonicBondForce.getAmoebaGlobalHarmonicBondQuartic();
57
    amoebaHarmonicBondForce.getBondParameters(bondIndex, particle1, particle2,  bondLength,  quadraticK );
Mark Friedrichs's avatar
Mark Friedrichs committed
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    double deltaR[3];
    double r2 = 0.0;
    for( int ii = 0; ii < 3; ii++ ){
           deltaR[ii]    = positions[particle2][ii] - positions[particle1][ii];
           r2           += deltaR[ii]*deltaR[ii];
    }
    double r                   = sqrt( r2 );

    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;

}

static void computeAmoebaHarmonicBondForces( Context& context, AmoebaHarmonicBondForce& amoebaHarmonicBondForce,
                                             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;
    for( int ii = 0; ii < amoebaHarmonicBondForce.getNumBonds(); ii++ ){
        computeAmoebaHarmonicBondForce(ii, positions, amoebaHarmonicBondForce, expectedForces, expectedEnergy );
    }

Mark Friedrichs's avatar
Mark Friedrichs committed
105
#ifdef AMOEBA_DEBUG
Mark Friedrichs's avatar
Mark Friedrichs committed
106
    if( log ){
Mark Friedrichs's avatar
Mark Friedrichs committed
107
        (void) fprintf( log, "computeAmoebaHarmonicBondForces: expected energy=%15.7e\n", *expectedEnergy );
Mark Friedrichs's avatar
Mark Friedrichs committed
108
        for( unsigned int ii = 0; ii < positions.size(); ii++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
109
            (void) fprintf( log, "%6u [%15.7e %15.7e %15.7e]\n", ii, expectedForces[ii][0], expectedForces[ii][1], expectedForces[ii][2] );
Mark Friedrichs's avatar
Mark Friedrichs committed
110
111
112
        }
        (void) fflush( log );
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
113
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
114
115
116
117
118
119
120
121
122
123
124
125
126
    return;

}

void compareWithExpectedForceAndEnergy( Context& context, AmoebaHarmonicBondForce& amoebaHarmonicBondForce, double tolerance, const std::string& idString, FILE* log) {

    std::vector<Vec3> expectedForces;
    double expectedEnergy;
    computeAmoebaHarmonicBondForces( context, amoebaHarmonicBondForce, expectedForces, &expectedEnergy, NULL );
   
    State state                      = context.getState(State::Forces | State::Energy);
    const std::vector<Vec3> forces   = state.getForces();

Mark Friedrichs's avatar
Mark Friedrichs committed
127
#ifdef AMOEBA_DEBUG
Mark Friedrichs's avatar
Mark Friedrichs committed
128
    if( log ){
Mark Friedrichs's avatar
Mark Friedrichs committed
129
        (void) fprintf( log, "computeAmoebaHarmonicBondForces: expected energy=%15.7e %15.7e\n", expectedEnergy, state.getPotentialEnergy() );
Mark Friedrichs's avatar
Mark Friedrichs committed
130
        for( unsigned int ii = 0; ii < forces.size(); ii++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
131
            (void) fprintf( log, "%6u [%15.7e %15.7e %15.7e]   [%15.7e %15.7e %15.7e]\n", ii,
Mark Friedrichs's avatar
Mark Friedrichs committed
132
133
134
135
                            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
136
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

    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 testOneBond( FILE* log ) {

    System system;

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

    LangevinIntegrator integrator(0.0, 0.1, 0.01);

    AmoebaHarmonicBondForce* amoebaHarmonicBondForce = new AmoebaHarmonicBondForce();

    double bondLength = 1.5;
    double quadraticK = 1.0;
    double cubicK     = 2.0;
    double quarticicK = 3.0;
159
160
    amoebaHarmonicBondForce->setAmoebaGlobalHarmonicBondCubic( cubicK );
    amoebaHarmonicBondForce->setAmoebaGlobalHarmonicBondQuartic( quarticicK );
161
    amoebaHarmonicBondForce->addBond(0, 1, bondLength, quadraticK);
Mark Friedrichs's avatar
Mark Friedrichs committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

    system.addForce(amoebaHarmonicBondForce);
    Context context(system, integrator, Platform::getPlatformByName( "Cuda"));
    std::vector<Vec3> positions(2);

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

    context.setPositions(positions);
    compareWithExpectedForceAndEnergy( context, *amoebaHarmonicBondForce, TOL, "testOneBond", log );
}

void testTwoBond( FILE* log ) {

    System system;
177
    registerAmoebaCudaKernelFactories();
Mark Friedrichs's avatar
Mark Friedrichs committed
178
179
180
181
182
183
184
185
186
187
188
189
190

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

    LangevinIntegrator integrator(0.0, 0.1, 0.01);

    AmoebaHarmonicBondForce* amoebaHarmonicBondForce = new AmoebaHarmonicBondForce();

    double bondLength = 1.5;
    double quadraticK = 1.0;
    double cubicK     = 2.0;
    double quarticicK = 3.0;
191
192
    amoebaHarmonicBondForce->setAmoebaGlobalHarmonicBondCubic( cubicK );
    amoebaHarmonicBondForce->setAmoebaGlobalHarmonicBondQuartic( quarticicK );
193
194
    amoebaHarmonicBondForce->addBond(0, 1, bondLength, quadraticK);
    amoebaHarmonicBondForce->addBond(1, 2, bondLength, quadraticK);
Mark Friedrichs's avatar
Mark Friedrichs committed
195
196
197

    system.addForce(amoebaHarmonicBondForce);
    Context context(system, integrator, Platform::getPlatformByName( "Cuda"));
198
    //Context context(system, integrator, platform );
Mark Friedrichs's avatar
Mark Friedrichs committed
199
200
201
202
203
204
205
206
207
208
209
210
211
    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);
    compareWithExpectedForceAndEnergy( context, *amoebaHarmonicBondForce, TOL, "testTwoBond", log );
}

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

    try {
Mark Friedrichs's avatar
Mark Friedrichs committed
212
        std::cout << "TestCudaAmoebaHarmonicBondForce running test..." << std::endl;
213
        registerAmoebaCudaKernelFactories();
Mark Friedrichs's avatar
Mark Friedrichs committed
214
        FILE* log = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
215
        testTwoBond( log );
216
    } catch(const std::exception& e) {
Mark Friedrichs's avatar
Mark Friedrichs committed
217
218
219
220
        std::cout << "exception: " << e.what() << std::endl;
        std::cout << "FAIL - ERROR.  Test failed." << std::endl;
        return 1;
    }
221
    std::cout << "Done" << std::endl;
Mark Friedrichs's avatar
Mark Friedrichs committed
222
223
    return 0;
}