ewald.cl 4.09 KB
Newer Older
1
2
real2 multofReal2(real2 a, real2 b) {
    return (real2) (a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
3
4
5
6
7
8
}

/**
 * Precompute the cosine and sine sums which appear in each force term.
 */

9
__kernel void calculateEwaldCosSinSums(__global mixed* restrict energyBuffer, __global const real4* restrict posq, __global real2* restrict cosSinSum, real4 reciprocalPeriodicBoxSize, real reciprocalCoefficient) {
10
11
12
13
14
    const unsigned int ksizex = 2*KMAX_X-1;
    const unsigned int ksizey = 2*KMAX_Y-1;
    const unsigned int ksizez = 2*KMAX_Z-1;
    const unsigned int totalK = ksizex*ksizey*ksizez;
    unsigned int index = get_global_id(0);
15
    mixed energy = 0;
16
17
18
19
20
21
22
23
24
25
    while (index < (KMAX_Y-1)*ksizez+KMAX_Z)
        index += get_global_size(0);
    while (index < totalK) {
        // Find the wave vector (kx, ky, kz) this index corresponds to.

        int rx = index/(ksizey*ksizez);
        int remainder = index - rx*ksizey*ksizez;
        int ry = remainder/ksizez;
        int rz = remainder - ry*ksizez - KMAX_Z + 1;
        ry += -KMAX_Y + 1;
26
27
28
        real kx = rx*reciprocalPeriodicBoxSize.x;
        real ky = ry*reciprocalPeriodicBoxSize.y;
        real kz = rz*reciprocalPeriodicBoxSize.z;
29
30
31

        // Compute the sum for this wave vector.

32
        real2 sum = 0.0f;
33
        for (int atom = 0; atom < NUM_ATOMS; atom++) {
34
35
36
            real4 apos = posq[atom];
            real phase = apos.x*kx;
            real2 structureFactor = (real2) (cos(phase), sin(phase));
37
            phase = apos.y*ky;
38
            structureFactor = multofReal2(structureFactor, (real2) (cos(phase), sin(phase)));
39
            phase = apos.z*kz;
40
            structureFactor = multofReal2(structureFactor, (real2) (cos(phase), sin(phase)));
41
42
43
44
45
46
            sum += apos.w*structureFactor;
        }
        cosSinSum[index] = sum;

        // Compute the contribution to the energy.

47
48
        real k2 = kx*kx + ky*ky + kz*kz;
        real ak = EXP(k2*EXP_COEFFICIENT) / k2;
49
        energy += reciprocalCoefficient*ak*(sum.x*sum.x + sum.y*sum.y);
50
51
52
53
54
55
56
57
58
59
        index += get_global_size(0);
    }
    energyBuffer[get_global_id(0)] += energy;
}

/**
 * Compute the reciprocal space part of the Ewald force, using the precomputed sums from the
 * previous routine.
 */

60
__kernel void calculateEwaldForces(__global real4* restrict forceBuffers, __global const real4* restrict posq, __global const real2* restrict cosSinSum, real4 reciprocalPeriodicBoxSize, real reciprocalCoefficient) {
61
62
    unsigned int atom = get_global_id(0);
    while (atom < NUM_ATOMS) {
63
64
        real4 force = forceBuffers[atom];
        real4 apos = posq[atom];
65
66
67
68
69
70

        // Loop over all wave vectors.

        int lowry = 0;
        int lowrz = 1;
        for (int rx = 0; rx < KMAX_X; rx++) {
71
            real kx = rx*reciprocalPeriodicBoxSize.x;
72
            for (int ry = lowry; ry < KMAX_Y; ry++) {
73
74
75
                real ky = ry*reciprocalPeriodicBoxSize.y;
                real phase = apos.x*kx;
                real2 tab_xy = (real2) (cos(phase), sin(phase));
76
                phase = apos.y*ky;
77
                tab_xy = multofReal2(tab_xy, (real2) (cos(phase), sin(phase)));
78
                for (int rz = lowrz; rz < KMAX_Z; rz++) {
79
                    real kz = rz*reciprocalPeriodicBoxSize.z;
80
81
82
83

                    // Compute the force contribution of this wave vector.

                    int index = rx*(KMAX_Y*2-1)*(KMAX_Z*2-1) + (ry+KMAX_Y-1)*(KMAX_Z*2-1) + (rz+KMAX_Z-1);
84
85
                    real k2 = kx*kx + ky*ky + kz*kz;
                    real ak = EXP(k2*EXP_COEFFICIENT)/k2;
86
                    phase = apos.z*kz;
87
88
89
                    real2 structureFactor = multofReal2(tab_xy, (real2) (cos(phase), sin(phase)));
                    real2 sum = cosSinSum[index];
                    real dEdR = 2*reciprocalCoefficient*ak*apos.w*(sum.x*structureFactor.y - sum.y*structureFactor.x);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
                    force.x += dEdR*kx;
                    force.y += dEdR*ky;
                    force.z += dEdR*kz;
                    lowrz = 1 - KMAX_Z;
                }
                lowry = 1 - KMAX_Y;
            }
        }

        // Record the force on the atom.

        forceBuffers[atom] = force;
        atom += get_global_size(0);
    }
}