kCalculateCMAPTorsionForces.cu 10.8 KB
Newer Older
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* -------------------------------------------------------------------------- *
 *                                   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) 2010 Stanford University and the Authors.           *
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

#include <stdio.h>
#include <cuda.h>
#include <vector_functions.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

#include "gputypes.h"
#include "cudatypes.h"


#define LOCAL_HACK_PI 3.1415926535897932384626433832795

#define DOT3(v1, v2) (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z)

#define CROSS_PRODUCT(v1, v2) make_float3(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x)

#define GETNORMEDDOTPRODUCT(v1, v2, dp) \
{ \
    dp          = DOT3(v1, v2); \
    float norm1 = DOT3(v1, v1); \
    float norm2 = DOT3(v2, v2); \
51
    dp /= sqrtf(norm1 * norm2); \
52
53
54
55
56
57
58
59
60
61
62
    dp = min(dp, 1.0f); \
    dp = max(dp, -1.0f); \
}

#define GETANGLEBETWEENTWOVECTORS(v1, v2, angle) \
{ \
    float dp; \
    GETNORMEDDOTPRODUCT(v1, v2, dp); \
    if (dp > 0.99f || dp < -0.99f) { \
        float3 cross = CROSS_PRODUCT(v1, v2); \
        float scale = DOT3(v1, v1)*DOT3(v2, v2); \
63
        angle = asinf(sqrtf(DOT3(cross, cross)/scale)); \
64
65
66
67
        if (dp < 0.0f) \
            angle = LOCAL_HACK_PI-angle; \
    } \
    else { \
68
        angle = acosf(dp); \
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    } \
}

#define GETDIHEDRALANGLEBETWEENTHREEVECTORS(vector1, vector2, vector3, signVector, cp0, cp1, angle) \
{ \
    cp0 = CROSS_PRODUCT(vector1, vector2); \
    cp1 = CROSS_PRODUCT(vector2, vector3); \
    GETANGLEBETWEENTWOVECTORS(cp0, cp1, angle); \
    float dp = DOT3(signVector, cp1); \
    angle = (dp >= 0) ? angle : -angle; \
}

__global__
#if (__CUDA_ARCH__ >= 200)
__launch_bounds__(GF1XX_LOCALFORCES_THREADS_PER_BLOCK, 1)
84
#elif (__CUDA_ARCH__ >= 120)
85
86
87
88
89
90
91
92
93
94
95
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
__launch_bounds__(GT2XX_LOCALFORCES_THREADS_PER_BLOCK, 1)
#else
__launch_bounds__(G8X_LOCALFORCES_THREADS_PER_BLOCK, 1)
#endif
void kCalculateCMAPTorsionForces_kernel(int numAtoms, int numTorsions, float4* forceBuffers, float* energyBuffer,
        float4* posq, float4* coeff, int2* mapPositions, int4* indices, int* maps)
{
    const float PI = 3.14159265358979323846f;
    float energy = 0.0f;
    for (int index = blockIdx.x*blockDim.x+threadIdx.x; index < numTorsions; index += blockDim.x*gridDim.x) {
        int4 atoms1 = indices[4*index];
        int4 atoms2 = indices[4*index+1];
        int4 atoms3 = indices[4*index+2];
        int4 atoms4 = indices[4*index+3];
        float4 a1 = posq[atoms1.x];
        float4 a2 = posq[atoms1.y];
        float4 a3 = posq[atoms1.z];
        float4 a4 = posq[atoms1.w];
        float4 b1 = posq[atoms2.x];
        float4 b2 = posq[atoms2.y];
        float4 b3 = posq[atoms2.z];
        float4 b4 = posq[atoms2.w];

        // Compute the first angle.

        float3 v0a = make_float3(a1.x-a2.x, a1.y-a2.y, a1.z-a2.z);
        float3 v1a = make_float3(a3.x-a2.x, a3.y-a2.y, a3.z-a2.z);
        float3 v2a = make_float3(a3.x-a4.x, a3.y-a4.y, a3.z-a4.z);
        float3 cp0a, cp1a;
        float angleA;
        GETDIHEDRALANGLEBETWEENTHREEVECTORS(v0a, v1a, v2a, v0a, cp0a, cp1a, angleA);
        angleA = fmod(angleA+2.0f*PI, 2.0f*PI);

        // Compute the second angle.

        float3 v0b = make_float3(b1.x-b2.x, b1.y-b2.y, b1.z-b2.z);
        float3 v1b = make_float3(b3.x-b2.x, b3.y-b2.y, b3.z-b2.z);
        float3 v2b = make_float3(b3.x-b4.x, b3.y-b4.y, b3.z-b4.z);
        float3 cp0b, cp1b;
        float angleB;
        GETDIHEDRALANGLEBETWEENTHREEVECTORS(v0b, v1b, v2b, v0b, cp0b, cp1b, angleB);
        angleB = fmod(angleB+2.0f*PI, 2.0f*PI);

        // Identify which patch this is in.

        int2 pos = mapPositions[maps[index]];
        int size = pos.y;
        float delta = 2.0f*PI/size;
        int s = (int) (angleA/delta);
        int t = (int) (angleB/delta);
        float4 c[4];
Peter Eastman's avatar
Peter Eastman committed
136
        int coeffIndex = pos.x+4*(s+size*t);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        c[0] = coeff[coeffIndex];
        c[1] = coeff[coeffIndex+1];
        c[2] = coeff[coeffIndex+2];
        c[3] = coeff[coeffIndex+3];
        float da = angleA/delta-s;
        float db = angleB/delta-t;

        // Evaluate the spline to determine the energy and gradients.

        float torsionEnergy = 0.0f;
        float dEdA = 0.0f;
        float dEdB = 0.0f;
        torsionEnergy = da*torsionEnergy + ((c[3].w*db + c[3].z)*db + c[3].y)*db + c[3].x;
        dEdA = db*dEdA + (3.0f*c[3].w*da + 2.0f*c[2].w)*da + c[1].w;
        dEdB = da*dEdB + (3.0f*c[3].w*db + 2.0f*c[3].z)*db + c[3].y;
        torsionEnergy = da*torsionEnergy + ((c[2].w*db + c[2].z)*db + c[2].y)*db + c[2].x;
        dEdA = db*dEdA + (3.0f*c[3].z*da + 2.0f*c[2].z)*da + c[1].z;
        dEdB = da*dEdB + (3.0f*c[2].w*db + 2.0f*c[2].z)*db + c[2].y;
        torsionEnergy = da*torsionEnergy + ((c[1].w*db + c[1].z)*db + c[1].y)*db + c[1].x;
        dEdA = db*dEdA + (3.0f*c[3].y*da + 2.0f*c[2].y)*da + c[1].y;
        dEdB = da*dEdB + (3.0f*c[1].w*db + 2.0f*c[1].z)*db + c[1].y;
        torsionEnergy = da*torsionEnergy + ((c[0].w*db + c[0].z)*db + c[0].y)*db + c[0].x;
        dEdA = db*dEdA + (3.0f*c[3].x*da + 2.0f*c[2].x)*da + c[1].x;
        dEdB = da*dEdB + (3.0f*c[0].w*db + 2.0f*c[0].z)*db + c[0].y;
        dEdA /= delta;
        dEdB /= delta;
        energy += torsionEnergy;

        // Apply the force to the first torsion.

        float normCross1 = DOT3(cp0a, cp0a);
        float normSqrBC = DOT3(v1a, v1a);
169
        float normBC = sqrtf(normSqrBC);
170
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
204
205
206
        float normCross2 = DOT3(cp1a, cp1a);
        float dp = 1.0f/normSqrBC;
        float4 ff = make_float4((-dEdA*normBC)/normCross1, DOT3(v0a, v1a)*dp, DOT3(v2a, v1a)*dp, (dEdA*normBC)/normCross2);
        float3 internalF0 = make_float3(ff.x*cp0a.x, ff.x*cp0a.y, ff.x*cp0a.z);
        float3 internalF3 = make_float3(ff.w*cp1a.x, ff.w*cp1a.y, ff.w*cp1a.z);
        float3 d = make_float3(ff.y*internalF0.x - ff.z*internalF3.x,
                               ff.y*internalF0.y - ff.z*internalF3.y,
                               ff.y*internalF0.z - ff.z*internalF3.z);
        unsigned int offsetA = atoms1.x+atoms3.x*numAtoms;
        unsigned int offsetB = atoms1.y+atoms3.y*numAtoms;
        unsigned int offsetC = atoms1.z+atoms3.z*numAtoms;
        unsigned int offsetD = atoms1.w+atoms3.w*numAtoms;
        float4 forceA = forceBuffers[offsetA];
        float4 forceB = forceBuffers[offsetB];
        float4 forceC = forceBuffers[offsetC];
        float4 forceD = forceBuffers[offsetD];
        forceA.x += internalF0.x;
        forceA.y += internalF0.y;
        forceA.z += internalF0.z;
        forceB.x += d.x-internalF0.x;
        forceB.y += d.y-internalF0.y;
        forceB.z += d.z-internalF0.z;
        forceC.x += -d.x-internalF3.x;
        forceC.y += -d.y-internalF3.y;
        forceC.z += -d.z-internalF3.z;
        forceD.x += internalF3.x;
        forceD.y += internalF3.y;
        forceD.z += internalF3.z;
        forceBuffers[offsetA] = forceA;
        forceBuffers[offsetB] = forceB;
        forceBuffers[offsetC] = forceC;
        forceBuffers[offsetD] = forceD;

        // Apply the force to the second torsion.

        normCross1 = DOT3(cp0b, cp0b);
        normSqrBC = DOT3(v1b, v1b);
207
        normBC = sqrtf(normSqrBC);
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
246
247
248
249
250
        normCross2 = DOT3(cp1b, cp1b);
        dp = 1.0f/normSqrBC;
        ff = make_float4((-dEdB*normBC)/normCross1, DOT3(v0b, v1b)*dp, DOT3(v2b, v1b)*dp, (dEdB*normBC)/normCross2);
        internalF0 = make_float3(ff.x*cp0b.x, ff.x*cp0b.y, ff.x*cp0b.z);
        internalF3 = make_float3(ff.w*cp1b.x, ff.w*cp1b.y, ff.w*cp1b.z);
        d = make_float3(ff.y*internalF0.x - ff.z*internalF3.x,
                        ff.y*internalF0.y - ff.z*internalF3.y,
                        ff.y*internalF0.z - ff.z*internalF3.z);
        offsetA = atoms2.x+atoms4.x*numAtoms;
        offsetB = atoms2.y+atoms4.y*numAtoms;
        offsetC = atoms2.z+atoms4.z*numAtoms;
        offsetD = atoms2.w+atoms4.w*numAtoms;
        forceA = forceBuffers[offsetA];
        forceB = forceBuffers[offsetB];
        forceC = forceBuffers[offsetC];
        forceD = forceBuffers[offsetD];
        forceA.x += internalF0.x;
        forceA.y += internalF0.y;
        forceA.z += internalF0.z;
        forceB.x += d.x-internalF0.x;
        forceB.y += d.y-internalF0.y;
        forceB.z += d.z-internalF0.z;
        forceC.x += -d.x-internalF3.x;
        forceC.y += -d.y-internalF3.y;
        forceC.z += -d.z-internalF3.z;
        forceD.x += internalF3.x;
        forceD.y += internalF3.y;
        forceD.z += internalF3.z;
        forceBuffers[offsetA] = forceA;
        forceBuffers[offsetB] = forceB;
        forceBuffers[offsetC] = forceC;
        forceBuffers[offsetD] = forceD;
    }
    energyBuffer[blockIdx.x*blockDim.x+threadIdx.x] += energy;
}

void kCalculateCMAPTorsionForces(gpuContext gpu, CUDAStream<float4>& coefficients, CUDAStream<int2>& mapPositions, CUDAStream<int4>& torsionIndices, CUDAStream<int>& torsionMaps)
{
    kCalculateCMAPTorsionForces_kernel<<<gpu->sim.blocks, gpu->sim.localForces_threads_per_block>>>(gpu->sim.stride,
            torsionMaps._length, gpu->sim.pForce4, gpu->sim.pEnergy, gpu->sim.pPosq, coefficients._pDevData,
            mapPositions._pDevData, torsionIndices._pDevData, torsionMaps._pDevData);
    LAUNCHERROR("kCalculateCMAPTorsionForces");
}