customHbondForce.cl 9.77 KB
Newer Older
1
2
3
/**
 * Compute the difference between two vectors, setting the fourth component to the squared magnitude.
 */
4
5
real4 delta(real4 vec1, real4 vec2) {
    real4 result = (real4) (vec1.x-vec2.x, vec1.y-vec2.y, vec1.z-vec2.z, 0);
6
7
8
9
10
11
12
13
    result.w = result.x*result.x + result.y*result.y + result.z*result.z;
    return result;
}

/**
 * Compute the difference between two vectors, taking periodic boundary conditions into account
 * and setting the fourth component to the squared magnitude.
 */
14
real4 deltaPeriodic(real4 vec1, real4 vec2, real4 periodicBoxSize, real4 invPeriodicBoxSize, real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ) {
15
    real4 result = (real4) (vec1.x-vec2.x, vec1.y-vec2.y, vec1.z-vec2.z, 0);
16
#ifdef USE_PERIODIC
17
    APPLY_PERIODIC_TO_DELTA(result)
18
19
20
21
22
23
24
25
#endif
    result.w = result.x*result.x + result.y*result.y + result.z*result.z;
    return result;
}

/**
 * Compute the angle between two vectors.  The w component of each vector should contain the squared magnitude.
 */
26
27
28
29
real computeAngle(real4 vec1, real4 vec2) {
    real dotProduct = vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z;
    real cosine = dotProduct*RSQRT(vec1.w*vec2.w);
    real angle;
30
31
32
    if (cosine > 0.99f || cosine < -0.99f) {
        // We're close to the singularity in acos(), so take the cross product and use asin() instead.

33
34
        real4 crossProduct = cross(vec1, vec2);
        real scale = vec1.w*vec2.w;
35
        angle = asin(SQRT(dot(crossProduct, crossProduct)/scale));
36
        if (cosine < 0.0f)
37
            angle = PI-angle;
38
39
40
41
42
43
44
    }
    else
       angle = acos(cosine);
    return angle;
}

/**
45
 * Compute the cross product of two vectors, setting the fourth component to the squared magnitude.
46
 */
47
48
real4 computeCross(real4 vec1, real4 vec2) {
    real4 result = cross(vec1, vec2);
49
50
51
    result.w = result.x*result.x + result.y*result.y + result.z*result.z;
    return result;
}
52

53
54
55
/**
 * Compute forces on donors.
 */
56
__kernel void computeDonorForces(__global real4* restrict forceBuffers, __global mixed* restrict energyBuffer, __global const real4* restrict posq, __global const int4* restrict exclusions,
57
58
        __global const int4* restrict donorAtoms, __global const int4* restrict acceptorAtoms, __global const int4* restrict donorBufferIndices, __local real4* posBuffer, real4 periodicBoxSize, real4 invPeriodicBoxSize,
        real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ
59
        PARAMETER_ARGUMENTS) {
60
    mixed energy = 0;
61
62
63
    real4 f1 = (real4) 0;
    real4 f2 = (real4) 0;
    real4 f3 = (real4) 0;
64
    for (int donorStart = 0; donorStart < NUM_DONORS; donorStart += get_global_size(0)) {
65
66
        // Load information about the donor this thread will compute forces on.

67
        int donorIndex = donorStart+get_global_id(0);
68
        int4 atoms, exclusionIndices;
69
        real4 d1, d2, d3;
70
71
        if (donorIndex < NUM_DONORS) {
            atoms = donorAtoms[donorIndex];
72
73
74
            d1 = (atoms.x > -1 ? posq[atoms.x] : (real4) 0);
            d2 = (atoms.y > -1 ? posq[atoms.y] : (real4) 0);
            d3 = (atoms.z > -1 ? posq[atoms.z] : (real4) 0);
75
76
77
#ifdef USE_EXCLUSIONS
            exclusionIndices = exclusions[donorIndex];
#endif
78
79
80
        }
        else
            atoms = (int4) (-1, -1, -1, -1);
81
82
83
84
        for (int acceptorStart = 0; acceptorStart < NUM_ACCEPTORS; acceptorStart += get_local_size(0)) {
            // Load the next block of acceptors into local memory.

            int blockSize = min((int) get_local_size(0), NUM_ACCEPTORS-acceptorStart);
85
            if (get_local_id(0) < blockSize) {
86
                int4 atoms2 = acceptorAtoms[acceptorStart+get_local_id(0)];
87
88
89
                posBuffer[3*get_local_id(0)] = (atoms2.x > -1 ? posq[atoms2.x] : (real4) 0);
                posBuffer[3*get_local_id(0)+1] = (atoms2.y > -1 ? posq[atoms2.y] : (real4) 0);
                posBuffer[3*get_local_id(0)+2] = (atoms2.z > -1 ? posq[atoms2.z] : (real4) 0);
90
91
            }
            barrier(CLK_LOCAL_MEM_FENCE);
92
93
            if (donorIndex < NUM_DONORS) {
                for (int index = 0; index < blockSize; index++) {
94
95
96
97
98
#ifdef USE_EXCLUSIONS
                    int acceptorIndex = acceptorStart+index;
                    if (acceptorIndex == exclusionIndices.x || acceptorIndex == exclusionIndices.y || acceptorIndex == exclusionIndices.z || acceptorIndex == exclusionIndices.w)
                        continue;
#endif
99
100
                    // Compute the interaction between a donor and an acceptor.

101
102
103
                    real4 a1 = posBuffer[3*index];
                    real4 a2 = posBuffer[3*index+1];
                    real4 a3 = posBuffer[3*index+2];
104
                    real4 deltaD1A1 = deltaPeriodic(d1, a1, periodicBoxSize, invPeriodicBoxSize, periodicBoxVecX, periodicBoxVecY, periodicBoxVecZ);
105
#ifdef USE_CUTOFF
106
                    if (deltaD1A1.w < CUTOFF_SQUARED) {
107
#endif
108
109
110
                        COMPUTE_DONOR_FORCE
#ifdef USE_CUTOFF
                    }
111
#endif
112
113
114
                }
            }
        }
115

116
        // Write results
117

118
119
120
121
        if (donorIndex < NUM_DONORS) {
            int4 bufferIndices = donorBufferIndices[donorIndex];
            if (atoms.x > -1) {
                unsigned int offset = atoms.x+bufferIndices.x*PADDED_NUM_ATOMS;
122
                real4 force = forceBuffers[offset];
123
124
125
126
127
                force.xyz += f1.xyz;
                forceBuffers[offset] = force;
            }
            if (atoms.y > -1) {
                unsigned int offset = atoms.y+bufferIndices.y*PADDED_NUM_ATOMS;
128
                real4 force = forceBuffers[offset];
129
130
131
132
133
                force.xyz += f2.xyz;
                forceBuffers[offset] = force;
            }
            if (atoms.z > -1) {
                unsigned int offset = atoms.z+bufferIndices.z*PADDED_NUM_ATOMS;
134
                real4 force = forceBuffers[offset];
135
136
137
                force.xyz += f3.xyz;
                forceBuffers[offset] = force;
            }
138
139
140
141
142
143
144
        }
    }
    energyBuffer[get_global_id(0)] += energy;
}
/**
 * Compute forces on acceptors.
 */
145
__kernel void computeAcceptorForces(__global real4* restrict forceBuffers, __global mixed* restrict energyBuffer, __global const real4* restrict posq, __global const int4* restrict exclusions,
146
147
        __global const int4* restrict donorAtoms, __global const int4* restrict acceptorAtoms, __global const int4* restrict acceptorBufferIndices, __local real4* restrict posBuffer, real4 periodicBoxSize, real4 invPeriodicBoxSize,
        real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ
148
        PARAMETER_ARGUMENTS) {
149
150
151
    real4 f1 = (real4) 0;
    real4 f2 = (real4) 0;
    real4 f3 = (real4) 0;
152
153
154
155
    for (int acceptorStart = 0; acceptorStart < NUM_ACCEPTORS; acceptorStart += get_global_size(0)) {
        // Load information about the acceptor this thread will compute forces on.

        int acceptorIndex = acceptorStart+get_global_id(0);
156
        int4 atoms, exclusionIndices;
157
        real4 a1, a2, a3;
158
159
        if (acceptorIndex < NUM_ACCEPTORS) {
            atoms = acceptorAtoms[acceptorIndex];
160
161
162
            a1 = (atoms.x > -1 ? posq[atoms.x] : (real4) 0);
            a2 = (atoms.y > -1 ? posq[atoms.y] : (real4) 0);
            a3 = (atoms.z > -1 ? posq[atoms.z] : (real4) 0);
163
164
165
#ifdef USE_EXCLUSIONS
            exclusionIndices = exclusions[acceptorIndex];
#endif
166
167
168
169
170
171
172
        }
        else
            atoms = (int4) (-1, -1, -1, -1);
        for (int donorStart = 0; donorStart < NUM_DONORS; donorStart += get_local_size(0)) {
            // Load the next block of donors into local memory.

            int blockSize = min((int) get_local_size(0), NUM_DONORS-donorStart);
173
            if (get_local_id(0) < blockSize) {
174
                int4 atoms2 = donorAtoms[donorStart+get_local_id(0)];
175
176
177
                posBuffer[3*get_local_id(0)] = (atoms2.x > -1 ? posq[atoms2.x] : (real4) 0);
                posBuffer[3*get_local_id(0)+1] = (atoms2.y > -1 ? posq[atoms2.y] : (real4) 0);
                posBuffer[3*get_local_id(0)+2] = (atoms2.z > -1 ? posq[atoms2.z] : (real4) 0);
178
179
180
181
            }
            barrier(CLK_LOCAL_MEM_FENCE);
            if (acceptorIndex < NUM_ACCEPTORS) {
                for (int index = 0; index < blockSize; index++) {
182
183
184
185
186
#ifdef USE_EXCLUSIONS
                    int donorIndex = donorStart+index;
                    if (donorIndex == exclusionIndices.x || donorIndex == exclusionIndices.y || donorIndex == exclusionIndices.z || donorIndex == exclusionIndices.w)
                        continue;
#endif
187
188
                    // Compute the interaction between a donor and an acceptor.

189
190
191
                    real4 d1 = posBuffer[3*index];
                    real4 d2 = posBuffer[3*index+1];
                    real4 d3 = posBuffer[3*index+2];
192
                    real4 deltaD1A1 = deltaPeriodic(d1, a1, periodicBoxSize, invPeriodicBoxSize, periodicBoxVecX, periodicBoxVecY, periodicBoxVecZ);
193
194
#ifdef USE_CUTOFF
                    if (deltaD1A1.w < CUTOFF_SQUARED) {
195
#endif
196
                        COMPUTE_ACCEPTOR_FORCE
197
#ifdef USE_CUTOFF
198
                    }
199
#endif
200
                }
201
202
203
204
205
            }
        }

        // Write results

206
207
208
209
        if (acceptorIndex < NUM_ACCEPTORS) {
            int4 bufferIndices = acceptorBufferIndices[acceptorIndex];
            if (atoms.x > -1) {
                unsigned int offset = atoms.x+bufferIndices.x*PADDED_NUM_ATOMS;
210
                real4 force = forceBuffers[offset];
211
212
213
214
215
                force.xyz += f1.xyz;
                forceBuffers[offset] = force;
            }
            if (atoms.y > -1) {
                unsigned int offset = atoms.y+bufferIndices.y*PADDED_NUM_ATOMS;
216
                real4 force = forceBuffers[offset];
217
218
219
220
221
                force.xyz += f2.xyz;
                forceBuffers[offset] = force;
            }
            if (atoms.z > -1) {
                unsigned int offset = atoms.z+bufferIndices.z*PADDED_NUM_ATOMS;
222
                real4 force = forceBuffers[offset];
223
224
225
                force.xyz += f3.xyz;
                forceBuffers[offset] = force;
            }
226
        }
227
228
    }
}