"tests/TestCMAPTorsionForce.h" did not exist on "881df1a5d917293966585ded4c33074cd95f8805"
gbsaObc_nvidia.cl 23.6 KB
Newer Older
1
#define TILE_SIZE 32
2

3
4
5
6
7
8
9
10
11
12
typedef struct {
    float x, y, z;
    float q;
    float fx, fy, fz, fw;
    float radius, scaledRadius;
    float bornSum;
    float bornRadius;
    float bornForce;
} AtomData;

13
14
15
16
/**
 * Compute the Born sum.
 */

17
18
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
void computeBornSum(__global float* global_bornSum, __global float4* posq, __global float2* global_params, __local AtomData* localData, __local float* tempBuffer, __global unsigned int* tiles,
19
#ifdef USE_CUTOFF
20
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize) {
21
22
23
24
25
26
#else
        unsigned int numTiles) {
#endif
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
27
28
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
29
30
31
32
33
34
35
36
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
        // Extract the coordinates of this tile
        unsigned int x = tiles[pos];
37
38
39
        unsigned int y = ((x >> 2) & 0x7fff)*TILE_SIZE;
        x = (x>>17)*TILE_SIZE;
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
40
        unsigned int tbx = get_local_id(0) - tgx;
41
        unsigned int atom1 = x + tgx;
42
        float bornSum = 0.0f;
43
44
        float4 posq1 = posq[atom1];
        float2 params1 = global_params[atom1];
45
46
47
        if (x == y) {
            // This tile is on the diagonal.

48
49
50
51
52
53
            localData[get_local_id(0)].x = posq1.x;
            localData[get_local_id(0)].y = posq1.y;
            localData[get_local_id(0)].z = posq1.z;
            localData[get_local_id(0)].q = posq1.w;
            localData[get_local_id(0)].radius = params1.x;
            localData[get_local_id(0)].scaledRadius = params1.y;
54
55
56
            unsigned int xi = x/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-xi*(xi+1)/2;
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
57
                float4 delta = (float4) (localData[tbx+j].x-posq1.x, localData[tbx+j].y-posq1.y, localData[tbx+j].z-posq1.z, 0.0f);
58
#ifdef USE_PERIODIC
59
60
61
                delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
62
63
64
#endif
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
65
                if (atom1 < NUM_ATOMS && y+j < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
66
#else
67
                if (atom1 < NUM_ATOMS && y+j < NUM_ATOMS) {
68
#endif
69
70
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
71
                    float2 params2 = (float2) (localData[tbx+j].radius, localData[tbx+j].scaledRadius);
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
                    float rScaledRadiusJ = r+params2.y;
                    if ((j != tgx) && (params1.x < rScaledRadiusJ)) {
                        float l_ij = 1.0f/max(params1.x, fabs(r-params2.y));
                        float u_ij = 1.0f/rScaledRadiusJ;
                        float l_ij2 = l_ij*l_ij;
                        float u_ij2 = u_ij*u_ij;
                        float ratio = log(u_ij / l_ij);
                        bornSum += l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                         (0.25f*params2.y*params2.y*invR)*(l_ij2-u_ij2);
                        if (params1.x < params2.x-r)
                            bornSum += 2.0f*(1.0f/params1.x-l_ij);
                    }
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
89
            unsigned int offset = x + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
90
#else
91
            unsigned int offset = x + tgx + warp*PADDED_NUM_ATOMS;
92
93
94
95
96
97
98
99
#endif
            global_bornSum[offset] += bornSum;
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
                unsigned int j = y + tgx;
100
101
102
103
104
105
106
107
                float4 tempPosq = posq[j];
                localData[get_local_id(0)].x = tempPosq.x;
                localData[get_local_id(0)].y = tempPosq.y;
                localData[get_local_id(0)].z = tempPosq.z;
                localData[get_local_id(0)].q = tempPosq.w;
                float2 tempParams = global_params[j];
                localData[get_local_id(0)].radius = tempParams.x;
                localData[get_local_id(0)].scaledRadius = tempParams.y;
108
            }
109
            localData[get_local_id(0)].bornSum = 0.0f;
110
111
112
113
114
115
116
117
118
#ifdef USE_CUTOFF
            unsigned int flags = interactionFlags[pos];
            if (flags != 0xFFFFFFFF) {
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

119
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
120
                        if ((flags&(1<<j)) != 0) {
121
                            float4 delta = (float4) (localData[tbx+j].x-posq1.x, localData[tbx+j].y-posq1.y, localData[tbx+j].z-posq1.z, 0.0f);
122
#ifdef USE_PERIODIC
123
124
125
                            delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                            delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                            delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
126
127
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
Peter Eastman's avatar
Peter Eastman committed
128
                            tempBuffer[get_local_id(0)] = 0.0f;
129
#ifdef USE_CUTOFF
130
                            if (atom1 < NUM_ATOMS && y+j < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
131
#else
132
                            if (atom1 < NUM_ATOMS && y+j < NUM_ATOMS) {
133
#endif
134
135
                                float invR = RSQRT(r2);
                                float r = RECIP(invR);
136
                                float2 params2 = (float2) (localData[tbx+j].radius, localData[tbx+j].scaledRadius);
137
                                float rScaledRadiusJ = r+params2.y;
Peter Eastman's avatar
Peter Eastman committed
138
                                if (params1.x < rScaledRadiusJ) {
139
140
141
142
143
144
145
146
147
148
149
                                    float l_ij = 1.0f/max(params1.x, fabs(r-params2.y));
                                    float u_ij = 1.0f/rScaledRadiusJ;
                                    float l_ij2 = l_ij*l_ij;
                                    float u_ij2 = u_ij*u_ij;
                                    float ratio = log(u_ij / l_ij);
                                    bornSum += l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                                     (0.25f*params2.y*params2.y*invR)*(l_ij2-u_ij2);
                                    if (params1.x < params2.x-r)
                                        bornSum += 2.0f*(1.0f/params1.x-l_ij);
                                }
                                float rScaledRadiusI = r+params1.y;
Peter Eastman's avatar
Peter Eastman committed
150
                                if (params2.x < rScaledRadiusI) {
151
                                    float l_ij = 1.0f/max(params2.x, fabs(r-params1.y));
Peter Eastman's avatar
Peter Eastman committed
152
                                    float u_ij = 1.0f/rScaledRadiusI;
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
                                    float l_ij2 = l_ij*l_ij;
                                    float u_ij2 = u_ij*u_ij;
                                    float ratio = log(u_ij / l_ij);
                                    float term = l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                                     (0.25f*params1.y*params1.y*invR)*(l_ij2-u_ij2);
                                    if (params2.x < params1.x-r)
                                        term += 2.0f*(1.0f/params2.x-l_ij);
                                    tempBuffer[get_local_id(0)] = term;
                                }
                            }

                            // Sum the forces on atom j.

                            if (tgx % 2 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+1];
                            if (tgx % 4 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+2];
                            if (tgx % 8 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+4];
                            if (tgx % 16 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+8];
                            if (tgx == 0)
175
                                localData[tbx+j].bornSum += tempBuffer[get_local_id(0)] + tempBuffer[get_local_id(0)+16];
176
177
178
179
180
181
182
183
184
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

185
186
187
188
189
                unsigned int xi = x/TILE_SIZE;
                unsigned int yi = y/TILE_SIZE;
                unsigned int tile = xi+yi*PADDED_NUM_ATOMS/TILE_SIZE-yi*(yi+1)/2;
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
190
                    float4 delta = (float4) (localData[tbx+tj].x-posq1.x, localData[tbx+tj].y-posq1.y, localData[tbx+tj].z-posq1.z, 0.0f);
191
#ifdef USE_PERIODIC
192
193
194
                    delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                    delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                    delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
195
196
197
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
198
                    if (atom1 < NUM_ATOMS && y+tj < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
199
#else
200
                    if (atom1 < NUM_ATOMS && y+tj < NUM_ATOMS) {
201
#endif
202
203
                        float invR = RSQRT(r2);
                        float r = RECIP(invR);
204
                        float2 params2 = (float2) (localData[tbx+tj].radius, localData[tbx+tj].scaledRadius);
205
                        float rScaledRadiusJ = r+params2.y;
Peter Eastman's avatar
Peter Eastman committed
206
                        if (params1.x < rScaledRadiusJ) {
207
208
209
210
211
212
213
214
215
216
217
                            float l_ij = 1.0f/max(params1.x, fabs(r-params2.y));
                            float u_ij = 1.0f/rScaledRadiusJ;
                            float l_ij2 = l_ij*l_ij;
                            float u_ij2 = u_ij*u_ij;
                            float ratio = log(u_ij / l_ij);
                            bornSum += l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                             (0.25f*params2.y*params2.y*invR)*(l_ij2-u_ij2);
                            if (params1.x < params2.x-r)
                                bornSum += 2.0f*(1.0f/params1.x-l_ij);
                        }
                        float rScaledRadiusI = r+params1.y;
Peter Eastman's avatar
Peter Eastman committed
218
                        if (params2.x < rScaledRadiusI) {
219
                            float l_ij = 1.0f/max(params2.x, fabs(r-params1.y));
Peter Eastman's avatar
Peter Eastman committed
220
                            float u_ij = 1.0f/rScaledRadiusI;
221
222
223
224
225
226
227
                            float l_ij2 = l_ij*l_ij;
                            float u_ij2 = u_ij*u_ij;
                            float ratio = log(u_ij / l_ij);
                            float term = l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                             (0.25f*params1.y*params1.y*invR)*(l_ij2-u_ij2);
                            if (params2.x < params1.x-r)
                                term += 2.0f*(1.0f/params2.x-l_ij);
228
                            localData[tbx+tj].bornSum += term;
229
230
                        }
                    }
231
                    tj = (tj + 1) & (TILE_SIZE - 1);
232
233
234
235
236
237
                }
            }

            // Write results
            float4 of;
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
238
239
            unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
240
#else
241
242
            unsigned int offset1 = x + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + warp*PADDED_NUM_ATOMS;
243
244
#endif
            global_bornSum[offset1] += bornSum;
245
            global_bornSum[offset2] += localData[get_local_id(0)].bornSum;
246
247
248
249
250
251
            lasty = y;
        }
        pos++;
    }
}

252
253
254
255
/**
 * First part of computing the GBSA interaction.
 */

256
257
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
void computeGBSAForce1(__global float4* forceBuffers, __global float* energyBuffer,
258
259
        __global float4* posq, __global float* global_bornRadii,
        __global float* global_bornForce, __local AtomData* localData, __local float4* tempBuffer, __global unsigned int* tiles,
260
#ifdef USE_CUTOFF
261
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize) {
262
263
264
265
266
267
#else
        unsigned int numTiles) {
#endif
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
268
269
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
270
271
272
273
274
275
276
277
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
        // Extract the coordinates of this tile
        unsigned int x = tiles[pos];
278
279
280
        unsigned int y = ((x >> 2) & 0x7fff)*TILE_SIZE;
        x = (x>>17)*TILE_SIZE;
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
281
282
283
284
285
286
287
288
        unsigned int tbx = get_local_id(0) - tgx;
        unsigned int atom1 = x + tgx;
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        float bornRadius1 = global_bornRadii[atom1];
        if (x == y) {
            // This tile is on the diagonal.

289
290
291
292
293
            localData[get_local_id(0)].x = posq1.x;
            localData[get_local_id(0)].y = posq1.y;
            localData[get_local_id(0)].z = posq1.z;
            localData[get_local_id(0)].q = posq1.w;
            localData[get_local_id(0)].bornRadius = bornRadius1;
294
295
296
            unsigned int xi = x/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-xi*(xi+1)/2;
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
297
                if (atom1 < NUM_ATOMS && y+j < NUM_ATOMS) {
298
                    float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
299
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
300
#ifdef USE_PERIODIC
301
302
303
                    delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                    delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                    delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
304
305
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
306
307
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
308
                    float bornRadius2 = localData[tbx+j].bornRadius;
309
310
311
312
                    float alpha2_ij = bornRadius1*bornRadius2;
                    float D_ij = r2/(4.0f*alpha2_ij);
                    float expTerm = exp(-D_ij);
                    float denominator2 = r2 + alpha2_ij*expTerm;
313
                    float denominator = SQRT(denominator2);
314
                    float tempEnergy = (PREFACTOR*posq1.w*posq2.w)/denominator;
315
316
317
318
319
                    float Gpol = tempEnergy/denominator2;
                    float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                    force.w += dGpol_dalpha2_ij*bornRadius2;
                    float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
320
                    if (r2 > CUTOFF_SQUARED) {
321
322
323
324
325
326
327
328
329
330
331
332
                        dEdR = 0.0f;
                        tempEnergy  = 0.0f;
                    }
#endif
                    energy += 0.5f*tempEnergy;
                    delta.xyz *= dEdR;
                    force.xyz -= delta.xyz;
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
333
            unsigned int offset = x + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
334
#else
335
            unsigned int offset = x + tgx + warp*PADDED_NUM_ATOMS;
336
337
#endif
            forceBuffers[offset].xyz += force.xyz;
Peter Eastman's avatar
Peter Eastman committed
338
            global_bornForce[offset] += force.w;
339
340
341
342
343
344
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
                unsigned int j = y + tgx;
345
346
347
348
349
350
                float4 tempPosq = posq[j];
                localData[get_local_id(0)].x = tempPosq.x;
                localData[get_local_id(0)].y = tempPosq.y;
                localData[get_local_id(0)].z = tempPosq.z;
                localData[get_local_id(0)].q = tempPosq.w;
                localData[get_local_id(0)].bornRadius = global_bornRadii[j];
351
            }
352
353
354
355
            localData[get_local_id(0)].fx = 0.0f;
            localData[get_local_id(0)].fy = 0.0f;
            localData[get_local_id(0)].fz = 0.0f;
            localData[get_local_id(0)].fw = 0.0f;
356
357
358
359
360
361
362
363
364
#ifdef USE_CUTOFF
            unsigned int flags = interactionFlags[pos];
            if (flags != 0xFFFFFFFF) {
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

365
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
366
                        if ((flags&(1<<j)) != 0) {
367
                            float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
368
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
369
#ifdef USE_PERIODIC
370
371
372
                            delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                            delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                            delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
373
374
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
375
376
                            float invR = RSQRT(r2);
                            float r = RECIP(invR);
377
                            float bornRadius2 = localData[tbx+j].bornRadius;
378
379
380
381
                            float alpha2_ij = bornRadius1*bornRadius2;
                            float D_ij = r2/(4.0f*alpha2_ij);
                            float expTerm = exp(-D_ij);
                            float denominator2 = r2 + alpha2_ij*expTerm;
382
                            float denominator = SQRT(denominator2);
383
                            float tempEnergy = (PREFACTOR*posq1.w*posq2.w)/denominator;
384
385
386
387
388
                            float Gpol = tempEnergy/denominator2;
                            float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                            force.w += dGpol_dalpha2_ij*bornRadius2;
                            float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
389
                            if (atom1 >= NUM_ATOMS || y+j >= NUM_ATOMS || r2 > CUTOFF_SQUARED) {
390
#else
391
                            if (atom1 >= NUM_ATOMS || y+j >= NUM_ATOMS) {
392
393
394
395
396
397
398
#endif
                                dEdR = 0.0f;
				tempEnergy = 0.0f;
                            }
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
                            force.xyz -= delta.xyz;
Peter Eastman's avatar
Peter Eastman committed
399
                            tempBuffer[get_local_id(0)] = (float4) (delta.xyz, dGpol_dalpha2_ij*bornRadius1);
400
401
402
403
404
405
406
407
408
409
410

                            // Sum the forces on atom j.

                            if (tgx % 2 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+1];
                            if (tgx % 4 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+2];
                            if (tgx % 8 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+4];
                            if (tgx % 16 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+8];
411
412
413
414
415
416
417
                            if (tgx == 0) {
                                float4 sum = tempBuffer[get_local_id(0)] + tempBuffer[get_local_id(0)+16];
                                localData[tbx+j].fx += sum.x;
                                localData[tbx+j].fy += sum.y;
                                localData[tbx+j].fz += sum.z;
                                localData[tbx+j].fw += sum.w;
                            }
418
419
420
421
422
423
424
425
426
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

427
428
429
430
431
                unsigned int xi = x/TILE_SIZE;
                unsigned int yi = y/TILE_SIZE;
                unsigned int tile = xi+yi*PADDED_NUM_ATOMS/TILE_SIZE-yi*(yi+1)/2;
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
432
                    if (atom1 < NUM_ATOMS && y+tj < NUM_ATOMS) {
433
                        float4 posq2 = (float4) (localData[tbx+tj].x, localData[tbx+tj].y, localData[tbx+tj].z, localData[tbx+tj].q);
434
                        float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
435
#ifdef USE_PERIODIC
436
437
438
                        delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                        delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                        delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
439
440
#endif
                        float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
441
442
                        float invR = RSQRT(r2);
                        float r = RECIP(invR);
443
                        float bornRadius2 = localData[tbx+tj].bornRadius;
444
445
446
447
                        float alpha2_ij = bornRadius1*bornRadius2;
                        float D_ij = r2/(4.0f*alpha2_ij);
                        float expTerm = exp(-D_ij);
                        float denominator2 = r2 + alpha2_ij*expTerm;
448
                        float denominator = SQRT(denominator2);
449
                        float tempEnergy = (PREFACTOR*posq1.w*posq2.w)/denominator;
450
451
452
453
454
                        float Gpol = tempEnergy/denominator2;
                        float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                        force.w += dGpol_dalpha2_ij*bornRadius2;
                        float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
455
                        if (r2 > CUTOFF_SQUARED) {
456
457
458
459
460
461
462
                            dEdR = 0.0f;
                            tempEnergy  = 0.0f;
                        }
#endif
                        energy += tempEnergy;
                        delta.xyz *= dEdR;
                        force.xyz -= delta.xyz;
463
464
465
466
                        localData[tbx+tj].fx += delta.x;
                        localData[tbx+tj].fy += delta.y;
                        localData[tbx+tj].fz += delta.z;
                        localData[tbx+tj].fw += dGpol_dalpha2_ij*bornRadius1;
467
                    }
468
                    tj = (tj + 1) & (TILE_SIZE - 1);
469
470
471
472
473
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
474
475
            unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
476
#else
477
478
            unsigned int offset1 = x + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + warp*PADDED_NUM_ATOMS;
479
480
#endif
            forceBuffers[offset1].xyz += force.xyz;
481
            forceBuffers[offset2] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0);
Peter Eastman's avatar
Peter Eastman committed
482
            global_bornForce[offset1] += force.w;
483
            global_bornForce[offset2] += localData[get_local_id(0)].fw;
484
485
486
487
488
489
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}