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

/**
 * 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 periodicBoxSize) {
10
11
12
13
14
15
    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;
    real3 reciprocalBoxSize = make_real3(2*M_PI/periodicBoxSize.x, 2*M_PI/periodicBoxSize.y, 2*M_PI/periodicBoxSize.z);
    real reciprocalCoefficient = ONE_4PI_EPS0*4*M_PI/(periodicBoxSize.x*periodicBoxSize.y*periodicBoxSize.z);
16
    unsigned int index = GLOBAL_ID;
17
    mixed energy = 0;
18
    while (index < (KMAX_Y-1)*ksizez+KMAX_Z)
19
        index += GLOBAL_SIZE;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    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;
        real kx = rx*reciprocalBoxSize.x;
        real ky = ry*reciprocalBoxSize.y;
        real kz = rz*reciprocalBoxSize.z;

        // Compute the sum for this wave vector.

        real2 sum = make_real2(0);
        for (int atom = 0; atom < NUM_ATOMS; atom++) {
            real4 apos = posq[atom];
            real phase = apos.x*kx;
38
            real2 structureFactor = make_real2(COS(phase), SIN(phase));
39
            phase = apos.y*ky;
40
            structureFactor = multofReal2(structureFactor, make_real2(COS(phase), SIN(phase)));
41
            phase = apos.z*kz;
42
            structureFactor = multofReal2(structureFactor, make_real2(COS(phase), SIN(phase)));
43
44
45
46
47
48
49
50
51
            sum += apos.w*structureFactor;
        }
        cosSinSum[index] = sum;

        // Compute the contribution to the energy.

        real k2 = kx*kx + ky*ky + kz*kz;
        real ak = EXP(k2*EXP_COEFFICIENT) / k2;
        energy += reciprocalCoefficient*ak*(sum.x*sum.x + sum.y*sum.y);
52
        index += GLOBAL_SIZE;
53
    }
54
    energyBuffer[GLOBAL_ID] += energy;
55
56
57
58
59
60
61
}

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

62
KERNEL void calculateEwaldForces(GLOBAL mm_long* RESTRICT forceBuffers, GLOBAL const real4* RESTRICT posq, GLOBAL const real2* RESTRICT cosSinSum, real4 periodicBoxSize) {
63
    unsigned int atom = GLOBAL_ID;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    real3 reciprocalBoxSize = make_real3(2*M_PI/periodicBoxSize.x, 2*M_PI/periodicBoxSize.y, 2*M_PI/periodicBoxSize.z);
    real reciprocalCoefficient = ONE_4PI_EPS0*4*M_PI/(periodicBoxSize.x*periodicBoxSize.y*periodicBoxSize.z);
    while (atom < NUM_ATOMS) {
        real3 force = make_real3(0);
        real4 apos = posq[atom];

        // Loop over all wave vectors.

        int lowry = 0;
        int lowrz = 1;
        for (int rx = 0; rx < KMAX_X; rx++) {
            real kx = rx*reciprocalBoxSize.x;
            for (int ry = lowry; ry < KMAX_Y; ry++) {
                real ky = ry*reciprocalBoxSize.y;
                real phase = apos.x*kx;
79
                real2 tab_xy = make_real2(COS(phase), SIN(phase));
80
                phase = apos.y*ky;
81
                tab_xy = multofReal2(tab_xy, make_real2(COS(phase), SIN(phase)));
82
83
84
85
86
87
88
89
90
                for (int rz = lowrz; rz < KMAX_Z; rz++) {
                    real kz = rz*reciprocalBoxSize.z;

                    // 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);
                    real k2 = kx*kx + ky*ky + kz*kz;
                    real ak = EXP(k2*EXP_COEFFICIENT)/k2;
                    phase = apos.z*kz;
91
                    real2 structureFactor = multofReal2(tab_xy, make_real2(COS(phase), SIN(phase)));
92
93
94
95
96
97
98
99
100
101
102
103
104
                    real2 sum = cosSinSum[index];
                    real dEdR = 2*reciprocalCoefficient*ak*apos.w*(sum.x*structureFactor.y - sum.y*structureFactor.x);
                    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.

105
106
107
        forceBuffers[atom] += realToFixedPoint(force.x);
        forceBuffers[atom+PADDED_NUM_ATOMS] += realToFixedPoint(force.y);
        forceBuffers[atom+2*PADDED_NUM_ATOMS] += realToFixedPoint(force.z);
108
        atom += GLOBAL_SIZE;
109
110
    }
}