kCalculateCDLJEwaldFastReciprocal.h 8.58 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* -------------------------------------------------------------------------- *
 *                                   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/>.      *
 * -------------------------------------------------------------------------- */

  /* 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;
  
  }
  
  __device__ float2 FloatMultFloat2(float r, float2 a)
  {
       float2 b;
       b.x = r*a.x;
       b.y = r*a.y;
       return b;
  
  }
   
  __device__ float2 FloatMultConjFloat2(float r, float2 a)
  {
       float2 b;
       b.x = r*a.y;
       b.y = r*a.x;
       return b;
  
  }
  
__global__ void kCalculateEwaldFastEikr_kernel()
{

    int kmax               = cSim.kmax;
    float4 apos;

    unsigned int atom    = threadIdx.x + blockIdx.x * blockDim.x;

    while (atom < cSim.atoms)
    {

76
77
          apos                         = cSim.pPosq[atom];

78
79
80
81
82
//generic form of the array
// pEikr[ atomID*kmax*3 + k*3 + m]


// k = 0, explicitly
83
        for(unsigned int m = 0; (m < 3); m++) {
84
85
          cSim.pEikr[atom*kmax*3 + 0 + m].x = 1;
          cSim.pEikr[atom*kmax*3 + 0 + m].y = 0;
86
        }
87
// k = 1, explicitly
88
89
          cSim.pEikr[atom*kmax*3 + 3 + 0].x = cos(apos.x*cSim.recipBoxSizeX);
          cSim.pEikr[atom*kmax*3 + 3 + 0].y = sin(apos.x*cSim.recipBoxSizeX);
90

91
92
          cSim.pEikr[atom*kmax*3 + 3 + 1].x = cos(apos.y*cSim.recipBoxSizeY);
          cSim.pEikr[atom*kmax*3 + 3 + 1].y = sin(apos.y*cSim.recipBoxSizeY);
93

94
95
          cSim.pEikr[atom*kmax*3 + 3 + 2].x = cos(apos.z*cSim.recipBoxSizeZ);
          cSim.pEikr[atom*kmax*3 + 3 + 2].y = sin(apos.z*cSim.recipBoxSizeZ);
96
97
98
99
100
101
102
103
104
105
106
107

// k > 1, by recursion
        for(unsigned int k=2; (k<kmax); k++) {
          for(unsigned int m=0; (m<3); m++) {
            cSim.pEikr[atom*kmax*3 + k*3 + m] = MultofFloat2 (cSim.pEikr[atom*kmax*3 + (k-1)*3 + m] , cSim.pEikr[atom*kmax*3 + 3 + m]);
          }
        }

        atom                            += blockDim.x * gridDim.x;
    }
}

108
__global__ void kCalculateEwaldFastStructureFactors_kernel()
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{

// hard-coded maximum k-vectors, no interface yet
    int kmax               = cSim.kmax;

    float4 apos;
    int lowry = 0;
    int lowrz = 1;
    int numRx = 20+1;
    int numRy = 20+1;
    int numRz = 20+1;
    unsigned int totalK  = (numRx*2 - 1) * (numRy*2 - 1) * (numRz*2 - 1);

    float2 tab_xy;
    int index;

    unsigned int atom    = threadIdx.x + blockIdx.x * blockDim.x;

    while (atom < cSim.atoms)
    {
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

           apos                         = cSim.pPosq[atom];

           // cSim.pEikr[atom*kmax*3 + k*3 + m] 

           for(int rx = 0; rx < numRx; rx++) 
           {

             for(int ry = lowry; ry < numRy; ry++) 
             {

               if(ry >= 0) 
               {
                   tab_xy = MultofFloat2 (cSim.pEikr[atom*kmax*3 + rx*3 + 0] , cSim.pEikr[atom*kmax*3 + ry*3 + 1]);
               }

               else 
               {
                   tab_xy = ConjMultofFloat2 (cSim.pEikr[atom*kmax*3 + rx*3 + 0] , cSim.pEikr[atom*kmax*3 - ry*3 + 1]);
               }

               for (int rz = lowrz; rz < numRz; rz++) 
               {

                   index = rx * (numRy*2 - 1 ) * (numRz*2 - 1) + (ry + numRy -1 ) * (numRz * 2 - 1) + (rz + numRz -1 );

                   if( rz >= 0) 
                   {
                      cSim.pStructureFactor[atom*totalK + index] = FloatMultFloat2 ( (apos.w ), MultofFloat2 (tab_xy ,cSim.pEikr[atom*kmax*3 + rz*3 + 2] ));
                   }

                   else 
                   {
                      cSim.pStructureFactor[atom*totalK + index] = FloatMultFloat2 ( ( apos.w ), ConjMultofFloat2 (tab_xy ,cSim.pEikr[atom*kmax*3 - rz*3 + 2] ));
                   }

                     cSim.pCosSinSum[index].x = 0.0;
                     cSim.pCosSinSum[index].y = 0.0;
       
                   lowrz = 1 - numRz;
               }

               lowry = 1 - numRy;
             }

           }

        atom                            += blockDim.x * gridDim.x;
    }
}


__global__ void kCalculateEwaldFastCosSinSums_kernel()
{

//    float2 eikr;
    int lowry = 0;
    int lowrz = 1;
    int numRx = 20+1;
    int numRy = 20+1;
    int numRz = 20+1;
    unsigned int totalK  = (numRx*2 - 1) * (numRy*2 - 1) * (numRz*2 - 1);

    int index;

    unsigned int rx    = threadIdx.x + blockIdx.x * blockDim.x;

    while (rx < numRx)
    {
198
199
200
201
// **********************************************************************

// cSim.pEikr[atom*kmax*3 + k*3 + m] 

202
//    for(int rx = 0; rx < numRx; rx++) {
203
204
205
206
207
208
209

      for(int ry = lowry; ry < numRy; ry++) {

        for (int rz = lowrz; rz < numRz; rz++) {

          index = rx * (numRy*2 - 1 ) * (numRz*2 - 1) + (ry + numRy -1 ) * (numRz * 2 - 1) + (rz + numRz -1 );

210
211
212
213
214
             for (int atom = 0; atom < cSim.atoms; atom++)
             {
                 cSim.pCosSinSum[index].x += cSim.pStructureFactor[atom*totalK + index].x;
                 cSim.pCosSinSum[index].y += cSim.pStructureFactor[atom*totalK + index].y;
             }
215
216
217
218
219

          lowrz = 1 - numRz;
        }
        lowry = 1 - numRy;
      }
220
221

        rx                            += blockDim.x * gridDim.x;
222
    }
223

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
}

__global__ void kCalculateEwaldFastForces_kernel()
{

    float PI               = 3.14159265358979323846f;
    const float epsilon     =  1.0;
    float recipCoeff = (4*PI/cSim.V/epsilon);

    int lowry = 0;
    int lowrz = 1;
    int numRx = 20+1;
    int numRy = 20+1;
    int numRz = 20+1;
    unsigned int totalK  = (numRx*2 - 1) * (numRy*2 - 1) * (numRz*2 - 1);
    int index;

    unsigned int atom    = threadIdx.x + blockIdx.x * blockDim.x;

    while (atom < cSim.atoms)
    {

    for(int rx = 0; rx < numRx; rx++) {

      float kx = rx * cSim.recipBoxSizeX;

      for(int ry = lowry; ry < numRy; ry++) {

      float ky = ry * cSim.recipBoxSizeY;

        for (int rz = lowrz; rz < numRz; rz++) {

        float kz = rz * cSim.recipBoxSizeZ;

       // next one is scary!
          index = rx * (numRy*2 - 1 ) * (numRz*2 - 1) + (ry + numRy -1 ) * (numRz * 2 - 1) + (rz + numRz -1 );

          float k2 = kx * kx + ky * ky + kz * kz;
          float ak = exp(k2*cSim.factorEwald) / k2;

          float dEdR = ak * (cSim.pCosSinSum[index].x  * cSim.pStructureFactor[atom*totalK + index].y - cSim.pCosSinSum[index].y * cSim.pStructureFactor[atom*totalK + index].x);
265
 
266
267
268
269
270
271
272
273
274
          cSim.pForce4[atom].x += 2 * recipCoeff * dEdR * kx ;
          cSim.pForce4[atom].y += 2 * recipCoeff * dEdR * ky ;
          cSim.pForce4[atom].z += 2 * recipCoeff * dEdR * kz ;

          lowrz = 1 - numRz;
        }
        lowry = 1 - numRy;
      }
    }
275
276
277



278
279
280
        atom                            += blockDim.x * gridDim.x;
    }
}