kCalculateCDLJEwaldFastReciprocal.h 6.77 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
/* -------------------------------------------------------------------------- *
 *                                   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) 2009 Stanford University and the Authors.           *
 * Authors: Rossen P. Apostolov, 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/>.      *
 * -------------------------------------------------------------------------- */

27
28
29
30
31
/**
 * This file contains the kernel for evaluating nonbonded forces using the
 * Ewald summation method (Reciprocal space summation).
 */

32
33
34
/* Cuda compiler on Windows does not recognized "static const float" values */
#define LOCAL_HACK_PI 3.1415926535897932384626433832795

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Define multiply operations for floats */

__device__ float2 MultofFloat2(float2 a, float2 b)
{
    float2 c;
    c.x = a.x * b.x - a.y * b.y;
    c.y = a.x * b.y + a.y * b.x;
    return c;
}

__device__ float2 ConjMultofFloat2(float2 a, float2 b)
{
    float2 c;
    c.x = a.x*b.x + a.y*b.y;
    c.y = a.y*b.x - a.x*b.y;
    return c;
}

53
54
55
/**
 * Precompute the cosine and sine sums which appear in each force term.
 */
56

57
58
__global__ void kCalculateEwaldFastCosSinSums_kernel()
{
59
    const float epsilon =  1.0;
60
    const float recipCoeff = cSim.epsfac*(4*LOCAL_HACK_PI/cSim.cellVolume/epsilon);
61
62
63
64
    const unsigned int ksizex = 2*cSim.kmaxX-1;
    const unsigned int ksizey = 2*cSim.kmaxY-1;
    const unsigned int ksizez = 2*cSim.kmaxZ-1;
    const unsigned int totalK = ksizex*ksizey*ksizez;
65
    unsigned int index = threadIdx.x + blockIdx.x * blockDim.x;
66
67
68
    float energy = 0.0f;
    while (index < (cSim.kmaxY-1)*ksizez+cSim.kmaxZ)
        index += blockDim.x * gridDim.x;
69
    while (index < totalK)
70
    {
71
72
        // Find the wave vector (kx, ky, kz) this index corresponds to.

73
74
75
76
77
        int rx = index/(ksizey*ksizez);
        int remainder = index - rx*ksizey*ksizez;
        int ry = remainder/ksizez;
        int rz = remainder - ry*ksizez - cSim.kmaxZ + 1;
        ry += -cSim.kmaxY + 1;
78
79
80
81
82
83
        float kx = rx*cSim.recipBoxSizeX;
        float ky = ry*cSim.recipBoxSizeY;
        float kz = rz*cSim.recipBoxSizeZ;

        // Compute the sum for this wave vector.

84
85
86
        float2 sum = make_float2(0.0f, 0.0f);
        for (int atom = 0; atom < cSim.atoms; atom++)
        {
87
88
89
90
91
92
93
94
95
            float4 apos = cSim.pPosq[atom];
            float phase = apos.x*kx;
            float2 structureFactor = make_float2(cos(phase), sin(phase));
            phase = apos.y*ky;
            structureFactor = MultofFloat2(structureFactor, make_float2(cos(phase), sin(phase)));
            phase = apos.z*kz;
            structureFactor = MultofFloat2(structureFactor, make_float2(cos(phase), sin(phase)));
            sum.x += apos.w*structureFactor.x;
            sum.y += apos.w*structureFactor.y;
96
        }
97
        cSim.pEwaldCosSinSum[index] = sum;
98
99
100
101
102
103

        // Compute the contribution to the energy.

        float k2 = kx*kx + ky*ky + kz*kz;
        float ak = exp(k2*cSim.factorEwald) / k2;
        energy += recipCoeff*ak*(sum.x*sum.x + sum.y*sum.y);
104
        index += blockDim.x * gridDim.x;
105
    }
106
    cSim.pEnergy[blockIdx.x*blockDim.x+threadIdx.x] += energy;
107
108
}

109
110
111
112
113
/**
 * Compute the reciprocal space part of the Ewald force, using the precomputed sums from the
 * previous routine.
 */

114
115
__global__ void kCalculateEwaldFastForces_kernel()
{
116
    const float epsilon =  1.0;
117
    float recipCoeff = cSim.epsfac*(4*LOCAL_HACK_PI/cSim.cellVolume/epsilon);
118

119
    unsigned int atom = threadIdx.x + blockIdx.x * blockDim.x;
120
121
122

    while (atom < cSim.atoms)
    {
123
        float4 force = cSim.pForce4[atom];
124
125
126
127
128
129
        float4 apos = cSim.pPosq[atom];

        // Loop over all wave vectors.

        int lowry = 0;
        int lowrz = 1;
130
        for (int rx = 0; rx < cSim.kmaxX; rx++) {
131
            float kx = rx * cSim.recipBoxSizeX;
132
            for (int ry = lowry; ry < cSim.kmaxY; ry++) {
133
                float ky = ry * cSim.recipBoxSizeY;
134
135
136
137
                float phase = apos.x*kx;
                float2 tab_xy = make_float2(cos(phase), sin(phase));
                phase = apos.y*ky;
                tab_xy = MultofFloat2(tab_xy, make_float2(cos(phase), sin(phase)));
138
                for (int rz = lowrz; rz < cSim.kmaxZ; rz++) {
139
                    float kz = rz * cSim.recipBoxSizeZ;
140
141
142

                    // Compute the force contribution of this wave vector.

143
                    int index = rx*(cSim.kmaxY*2-1)*(cSim.kmaxZ*2-1) + (ry+cSim.kmaxY-1)*(cSim.kmaxZ*2-1) + (rz+cSim.kmaxZ-1);
144
145
                    float k2 = kx*kx + ky*ky + kz*kz;
                    float ak = exp(k2*cSim.factorEwald) / k2;
146
147
                    phase = apos.z*kz;
                    float2 structureFactor = MultofFloat2(tab_xy, make_float2(cos(phase), sin(phase)));
148
                    float2 cosSinSum = cSim.pEwaldCosSinSum[index];
149
                    float dEdR = ak * apos.w * (cosSinSum.x*structureFactor.y - cosSinSum.y*structureFactor.x);
150
151
152
                    force.x += 2 * recipCoeff * dEdR * kx;
                    force.y += 2 * recipCoeff * dEdR * ky;
                    force.z += 2 * recipCoeff * dEdR * kz;
153
                    lowrz = 1 - cSim.kmaxZ;
154
                }
155
                lowry = 1 - cSim.kmaxY;
156
            }
157
        }
158
159
160

        // Record the force on the atom.

161
        cSim.pForce4[atom] = force;
162
        atom += blockDim.x * gridDim.x;
163
164
    }
}