4adb858b04784a17bf9535c555614647673e2432
[LbmBenchmarkKernelsPublic.git] / src / BenchKernelD3Q19List.c
1 // --------------------------------------------------------------------------
2 //
3 // Copyright
4 //   Markus Wittmann, 2016-2017
5 //   RRZE, University of Erlangen-Nuremberg, Germany
6 //   markus.wittmann -at- fau.de or hpc -at- rrze.fau.de
7 //
8 //   Viktor Haag, 2016
9 //   LSS, University of Erlangen-Nuremberg, Germany
10 //
11 //  This file is part of the Lattice Boltzmann Benchmark Kernels (LbmBenchKernels).
12 //
13 //  LbmBenchKernels is free software: you can redistribute it and/or modify
14 //  it under the terms of the GNU General Public License as published by
15 //  the Free Software Foundation, either version 3 of the License, or
16 //  (at your option) any later version.
17 //
18 //  LbmBenchKernels is distributed in the hope that it will be useful,
19 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 //  GNU General Public License for more details.
22 //
23 //  You should have received a copy of the GNU General Public License
24 //  along with LbmBenchKernels.  If not, see <http://www.gnu.org/licenses/>.
25 //
26 // --------------------------------------------------------------------------
27 #include "BenchKernelD3Q19ListCommon.h"
28
29 #include "Memory.h"
30 #include "Vtk.h"
31 #include "LikwidIf.h"
32
33 #include <inttypes.h>
34 #include <math.h>
35
36
37 void FNAME(D3Q19ListKernel)(LatticeDesc * ld, KernelData * kernelData, CaseData * cd)
38 {
39         Assert(ld != NULL);
40         Assert(kernelData != NULL);
41         Assert(cd != NULL);
42
43         Assert(cd->Omega > F(0.0));
44         Assert(cd->Omega < F(2.0));
45
46         KernelData * kd = (KernelData *)kernelData;
47         KernelDataList * kdl = (KernelDataList *)kernelData;
48
49         PdfT omega = cd->Omega;
50         PdfT omegaEven = omega;
51         PdfT magicParam = F(1.0) / F(12.0);
52         PdfT omegaOdd   = F(1.0) /(F(0.5) + magicParam / (F(1.0) / omega - F(0.5)));
53
54         PdfT evenPart = F(0.0);
55         PdfT oddPart  = F(0.0);
56         PdfT dir_indep_trm = F(0.0);
57
58         PdfT w_0 = F(1.0) / F( 3.0);
59         PdfT w_1 = F(1.0) / F(18.0);
60         PdfT w_2 = F(1.0) / F(36.0);
61
62         PdfT w_1_x3 = w_1 * F(3.0);     PdfT w_1_nine_half = w_1 * F(9.0) / F(2.0);     PdfT w_1_indep = F(0.0);
63         PdfT w_2_x3 = w_2 * F(3.0);     PdfT w_2_nine_half = w_2 * F(9.0) / F(2.0);     PdfT w_2_indep = F(0.0);
64
65         PdfT ux, uy, uz, ui;
66         PdfT dens;
67
68         // Declare pdf_N, pdf_E, pdf_S, pdf_W, ...
69         #define X(name, idx, idxinv, x, y, z)   PdfT JOIN(pdf_,name);
70         D3Q19_LIST
71         #undef X
72
73         PdfT * src = kd->Pdfs[0];
74         PdfT * dst = kd->Pdfs[1];
75         PdfT * tmp;
76
77         int maxIterations = cd->MaxIterations;
78         int nFluid = kdl->nFluid;
79         int nCells = kdl->nCells;
80
81         uint32_t adjListIndex;
82         uint32_t * adjList = kdl->AdjList;
83
84         #ifdef VTK_OUTPUT
85                 if (cd->VtkOutput) {
86                         kd->PdfsActive = src;
87                         VtkWrite(ld, kd, cd, 0);
88                 }
89         #endif
90
91         #ifdef STATISTICS
92                 kd->PdfsActive = src;
93                 KernelStatistics(kd, ld, cd, 0);
94         #endif
95
96         // TODO: outer openmp parallel
97         for(int iter = 0; iter < maxIterations; ++iter) {
98
99
100         X_LIKWID_START("list-os");
101
102         #ifdef _OPENMP
103                 #pragma omp parallel for default(none) \
104                                 shared(nFluid, nCells, kd, kdl, adjList, src, dst, w_0, w_1, w_2, omegaEven, omegaOdd, \
105                                 w_1_x3, w_2_x3, w_1_nine_half, w_2_nine_half, cd) \
106                                 private(ux, uy, uz, ui, dens, dir_indep_trm, adjListIndex, \
107                                         pdf_C, \
108                                         pdf_N, pdf_E, pdf_S, pdf_W, \
109                                         pdf_NE, pdf_SE, pdf_SW, pdf_NW, \
110                                         pdf_T, pdf_TN, pdf_TE, pdf_TS, pdf_TW, \
111                                         pdf_B, pdf_BN, pdf_BE, pdf_BS, pdf_BW, \
112                                         evenPart, oddPart, w_1_indep, w_2_indep)
113         #endif
114         #ifdef INTEL_OPT_DIRECTIVES
115                 #pragma ivdep
116         #endif
117         for (int index = 0; index < nFluid; ++index) {
118
119                         #define I(index, dir)   P_INDEX_3((nCells), (index), (dir))
120
121 #ifdef PROP_MODEL_PUSH
122
123                         // Load PDFs of local cell: pdf_N = src[I(x, y, z, D3Q19_N)]; ...
124                         #define X(name, idx, idxinv, _x, _y, _z)        JOIN(pdf_,name) = src[I(index, idx)];
125                         D3Q19_LIST
126                         #undef X
127
128 #elif PROP_MODEL_PULL
129
130                         adjListIndex = index * N_D3Q19_IDX;
131
132                         pdf_C = src[P_INDEX_3(nCells, index, D3Q19_C)];
133
134                         // Load PDFs of local cell: pdf_N = src[adjList[adjListIndex + D3Q19_N]]; ...
135                         #define X(name, idx, idxinv, _x, _y, _z)        JOIN(pdf_,name) = src[adjList[adjListIndex + idx]];
136                         D3Q19_LIST_WO_C
137                         #undef X
138
139 #else
140         #error No implementation for PROP_MODEL_NAME.
141 #endif
142
143 // #define LID_DRIVEN_CAVITY
144
145 #ifdef LID_DRIVEN_CAVITY
146                         int nX = kd->Dims[0];
147                         int nY = kd->Dims[1];
148                         int nZ = kd->Dims[2];
149
150                         int x = kdl->Coords[C_INDEX_X(index)];
151                         int y = kdl->Coords[C_INDEX_Y(index)];
152                         int z = kdl->Coords[C_INDEX_Z(index)];
153
154                         if (z == nZ - 4 && x > 3 && x < (nX - 4) && y > 3 && y < (nY - 4)) {
155                                 ux = 0.1 * 0.577;
156                                 uy = 0.0;
157                                 uz = 0.0;
158                         } else {
159 #endif
160                                 ux = pdf_E + pdf_NE + pdf_SE + pdf_TE + pdf_BE -
161                                          pdf_W - pdf_NW - pdf_SW - pdf_TW - pdf_BW;
162                                 uy = pdf_N + pdf_NE + pdf_NW + pdf_TN + pdf_BN -
163                                          pdf_S - pdf_SE - pdf_SW - pdf_TS - pdf_BS;
164                                 uz = pdf_T + pdf_TE + pdf_TW + pdf_TN + pdf_TS -
165                                          pdf_B - pdf_BE - pdf_BW - pdf_BN - pdf_BS;
166 #ifdef LID_DRIVEN_CAVITY
167                         }
168 #endif
169
170                         dens = pdf_C +
171                                    pdf_N  + pdf_E  + pdf_S  + pdf_W  +
172                                    pdf_NE + pdf_SE + pdf_SW + pdf_NW +
173                                    pdf_T  + pdf_TN + pdf_TE + pdf_TS + pdf_TW +
174                                    pdf_B  + pdf_BN + pdf_BE + pdf_BS + pdf_BW;
175
176                         dir_indep_trm = dens - (ux * ux + uy * uy + uz * uz) * F(3.0) / F(2.0);
177
178 #ifdef PROP_MODEL_PUSH
179
180                         adjListIndex = index * N_D3Q19_IDX;
181
182                         // direction: w_0
183                         dst[I(index, D3Q19_C)             ]  = pdf_C - omegaEven*(pdf_C - w_0*dir_indep_trm);
184
185                         // direction: w_1
186                         w_1_indep = w_1*dir_indep_trm;
187
188                         ui = uy;
189                         evenPart = omegaEven*( F(0.5)*(pdf_N + pdf_S) - ui*ui*w_1_nine_half - w_1_indep );
190                         oddPart = omegaOdd*(F(0.5)*(pdf_N - pdf_S) - ui*w_1_x3 );
191                         dst[adjList[adjListIndex + D3Q19_N]]  = pdf_N - evenPart - oddPart;
192                         dst[adjList[adjListIndex + D3Q19_S]]  = pdf_S - evenPart + oddPart;
193
194                         ui = ux;
195                         evenPart = omegaEven*( F(0.5)*(pdf_E + pdf_W) - ui*ui*w_1_nine_half - w_1_indep );
196                         oddPart = omegaOdd*(F(0.5)*(pdf_E - pdf_W) - ui*w_1_x3 );
197                         dst[adjList[adjListIndex + D3Q19_E]]  = pdf_E - evenPart - oddPart;
198                         dst[adjList[adjListIndex + D3Q19_W]]  = pdf_W - evenPart + oddPart;
199
200                         ui = uz;
201                         evenPart = omegaEven*( F(0.5)*(pdf_T + pdf_B) - ui*ui*w_1_nine_half - w_1_indep );
202                         oddPart = omegaOdd*(F(0.5)*(pdf_T - pdf_B) - ui*w_1_x3 );
203                         dst[adjList[adjListIndex + D3Q19_T]]  = pdf_T - evenPart - oddPart;
204                         dst[adjList[adjListIndex + D3Q19_B]]  = pdf_B - evenPart + oddPart;
205
206                         // direction: w_2
207                         w_2_indep = w_2*dir_indep_trm;
208
209                         ui = -ux + uy;
210                         evenPart = omegaEven*( F(0.5)*(pdf_NW + pdf_SE) - ui*ui*w_2_nine_half - w_2_indep );
211                         oddPart = omegaOdd*(F(0.5)*(pdf_NW - pdf_SE) - ui*w_2_x3 );
212                         dst[adjList[adjListIndex + D3Q19_NW]] = pdf_NW - evenPart - oddPart;
213                         dst[adjList[adjListIndex + D3Q19_SE]] = pdf_SE - evenPart + oddPart;
214
215                         ui = ux + uy;
216                         evenPart = omegaEven*( F(0.5)*(pdf_NE + pdf_SW) - ui*ui*w_2_nine_half - w_2_indep );
217                         oddPart = omegaOdd*(F(0.5)*(pdf_NE - pdf_SW) - ui*w_2_x3 );
218                         dst[adjList[adjListIndex + D3Q19_NE]] = pdf_NE - evenPart - oddPart;
219                         dst[adjList[adjListIndex + D3Q19_SW]] = pdf_SW - evenPart + oddPart;
220
221                         ui = -ux + uz;
222                         evenPart = omegaEven*( F(0.5)*(pdf_TW + pdf_BE) - ui*ui*w_2_nine_half - w_2_indep );
223                         oddPart = omegaOdd*(F(0.5)*(pdf_TW - pdf_BE) - ui*w_2_x3 );
224                         dst[adjList[adjListIndex + D3Q19_TW]] = pdf_TW - evenPart - oddPart;
225                         dst[adjList[adjListIndex + D3Q19_BE]] = pdf_BE - evenPart + oddPart;
226
227                         ui = ux + uz;
228                         evenPart = omegaEven*( F(0.5)*(pdf_TE + pdf_BW) - ui*ui*w_2_nine_half - w_2_indep );
229                         oddPart = omegaOdd*(F(0.5)*(pdf_TE - pdf_BW) - ui*w_2_x3 );
230                         dst[adjList[adjListIndex + D3Q19_TE]] = pdf_TE - evenPart - oddPart;
231                         dst[adjList[adjListIndex + D3Q19_BW]] = pdf_BW - evenPart + oddPart;
232
233                         ui = -uy + uz;
234                         evenPart = omegaEven*( F(0.5)*(pdf_TS + pdf_BN) - ui*ui*w_2_nine_half - w_2_indep );
235                         oddPart = omegaOdd*(F(0.5)*(pdf_TS - pdf_BN) - ui*w_2_x3 );
236                         dst[adjList[adjListIndex + D3Q19_TS]] = pdf_TS - evenPart - oddPart;
237                         dst[adjList[adjListIndex + D3Q19_BN]] = pdf_BN - evenPart + oddPart;
238
239                         ui = uy + uz;
240                         evenPart = omegaEven*( F(0.5)*(pdf_TN + pdf_BS) - ui*ui*w_2_nine_half - w_2_indep );
241                         oddPart = omegaOdd*(F(0.5)*(pdf_TN - pdf_BS) - ui*w_2_x3 );
242                         dst[adjList[adjListIndex + D3Q19_TN]] = pdf_TN - evenPart - oddPart;
243                         dst[adjList[adjListIndex + D3Q19_BS]] = pdf_BS - evenPart + oddPart;
244
245 #elif PROP_MODEL_PULL
246
247                         // direction: w_0
248                         dst[I(index, D3Q19_C )]  = pdf_C - omegaEven*(pdf_C - w_0*dir_indep_trm);
249
250                         // direction: w_1
251                         w_1_indep = w_1*dir_indep_trm;
252
253                         ui = uy;
254                         evenPart = omegaEven*( F(0.5)*(pdf_N + pdf_S) - ui*ui*w_1_nine_half - w_1_indep );
255                         oddPart = omegaOdd*(F(0.5)*(pdf_N - pdf_S) - ui*w_1_x3 );
256                         dst[I(index, D3Q19_N )]  = pdf_N - evenPart - oddPart;
257                         dst[I(index, D3Q19_S )]  = pdf_S - evenPart + oddPart;
258
259                         ui = ux;
260                         evenPart = omegaEven*( F(0.5)*(pdf_E + pdf_W) - ui*ui*w_1_nine_half - w_1_indep );
261                         oddPart = omegaOdd*(F(0.5)*(pdf_E - pdf_W) - ui*w_1_x3 );
262                         dst[I(index, D3Q19_E )]  = pdf_E - evenPart - oddPart;
263                         dst[I(index, D3Q19_W )]  = pdf_W - evenPart + oddPart;
264
265                         ui = uz;
266                         evenPart = omegaEven*( F(0.5)*(pdf_T + pdf_B) - ui*ui*w_1_nine_half - w_1_indep );
267                         oddPart = omegaOdd*(F(0.5)*(pdf_T - pdf_B) - ui*w_1_x3 );
268                         dst[I(index, D3Q19_T )]  = pdf_T - evenPart - oddPart;
269                         dst[I(index, D3Q19_B )]  = pdf_B - evenPart + oddPart;
270
271                         // direction: w_2
272                         w_2_indep = w_2*dir_indep_trm;
273
274                         ui = -ux + uy;
275                         evenPart = omegaEven*( F(0.5)*(pdf_NW + pdf_SE) - ui*ui*w_2_nine_half - w_2_indep );
276                         oddPart = omegaOdd*(F(0.5)*(pdf_NW - pdf_SE) - ui*w_2_x3 );
277                         dst[I(index, D3Q19_NW)] = pdf_NW - evenPart - oddPart;
278                         dst[I(index, D3Q19_SE)] = pdf_SE - evenPart + oddPart;
279
280                         ui = ux + uy;
281                         evenPart = omegaEven*( F(0.5)*(pdf_NE + pdf_SW) - ui*ui*w_2_nine_half - w_2_indep );
282                         oddPart = omegaOdd*(F(0.5)*(pdf_NE - pdf_SW) - ui*w_2_x3 );
283                         dst[I(index, D3Q19_NE)] = pdf_NE - evenPart - oddPart;
284                         dst[I(index, D3Q19_SW)] = pdf_SW - evenPart + oddPart;
285
286                         ui = -ux + uz;
287                         evenPart = omegaEven*( F(0.5)*(pdf_TW + pdf_BE) - ui*ui*w_2_nine_half - w_2_indep );
288                         oddPart = omegaOdd*(F(0.5)*(pdf_TW - pdf_BE) - ui*w_2_x3 );
289                         dst[I(index, D3Q19_TW)] = pdf_TW - evenPart - oddPart;
290                         dst[I(index, D3Q19_BE)] = pdf_BE - evenPart + oddPart;
291
292                         ui = ux + uz;
293                         evenPart = omegaEven*( F(0.5)*(pdf_TE + pdf_BW) - ui*ui*w_2_nine_half - w_2_indep );
294                         oddPart = omegaOdd*(F(0.5)*(pdf_TE - pdf_BW) - ui*w_2_x3 );
295                         dst[I(index, D3Q19_TE)] = pdf_TE - evenPart - oddPart;
296                         dst[I(index, D3Q19_BW)] = pdf_BW - evenPart + oddPart;
297
298                         ui = -uy + uz;
299                         evenPart = omegaEven*( F(0.5)*(pdf_TS + pdf_BN) - ui*ui*w_2_nine_half - w_2_indep );
300                         oddPart = omegaOdd*(F(0.5)*(pdf_TS - pdf_BN) - ui*w_2_x3 );
301                         dst[I(index, D3Q19_TS)] = pdf_TS - evenPart - oddPart;
302                         dst[I(index, D3Q19_BN)] = pdf_BN - evenPart + oddPart;
303
304                         ui = uy + uz;
305                         evenPart = omegaEven*( F(0.5)*(pdf_TN + pdf_BS) - ui*ui*w_2_nine_half - w_2_indep );
306                         oddPart = omegaOdd*(F(0.5)*(pdf_TN - pdf_BS) - ui*w_2_x3 );
307                         dst[I(index, D3Q19_TN)] = pdf_TN - evenPart - oddPart;
308                         dst[I(index, D3Q19_BS)] = pdf_BS - evenPart + oddPart;
309
310 #endif
311                         #undef I
312                 } // loop over fluid nodes
313
314                 X_LIKWID_STOP("list-os");
315
316                 #ifdef VERIFICATION
317                         kd->PdfsActive = dst;
318                         KernelAddBodyForce(kd, ld, cd);
319                 #endif
320
321                 #ifdef VTK_OUTPUT
322                         if (cd->VtkOutput && (iter % cd->VtkModulus) == 0) {
323                                 kd->PdfsActive = dst;
324                                 VtkWrite(ld, kd, cd, iter);
325                         }
326                 #endif
327
328                 #ifdef STATISTICS
329                         kd->PdfsActive = dst;
330                         KernelStatistics(kd, ld, cd, iter);
331                 #endif
332
333                 // swap grids
334                 tmp = src;
335                 src = dst;
336                 dst = tmp;
337
338         } // for (int iter = 0; ...
339
340 #ifdef VTK_OUTPUT
341         if (cd->VtkOutput) {
342                 kd->PdfsActive = src;
343                 VtkWrite(ld, kd, cd, maxIterations);
344         }
345 #endif
346
347 #ifdef STATISTICS
348         kd->PdfsActive = src;
349         KernelStatistics(kd, ld, cd, maxIterations);
350 #endif
351
352         return;
353 }
This page took 0.061814 seconds and 3 git commands to generate.