merge with kernels from MH's master thesis
[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         X_KERNEL_START(kernelData);
97
98         X_LIKWID_START("list-os");
99
100         // TODO: outer openmp parallel
101         #ifdef _OPENMP
102                 #pragma omp parallel default(none) \
103                                 shared(nFluid, nCells, kd, kdl, adjList, src, dst, w_0, w_1, w_2, omegaEven, omegaOdd, \
104                                 w_1_x3, w_2_x3, w_1_nine_half, w_2_nine_half, cd, ld, tmp, maxIterations ) \
105                                 private(ux, uy, uz, ui, dens, dir_indep_trm, adjListIndex, \
106                                         pdf_C, \
107                                         pdf_N, pdf_E, pdf_S, pdf_W, \
108                                         pdf_NE, pdf_SE, pdf_SW, pdf_NW, \
109                                         pdf_T, pdf_TN, pdf_TE, pdf_TS, pdf_TW, \
110                                         pdf_B, pdf_BN, pdf_BE, pdf_BS, pdf_BW, \
111                                         evenPart, oddPart, w_1_indep, w_2_indep)
112         #endif
113 {
114         for(int iter = 0; iter < maxIterations; ++iter) {
115
116
117
118         #ifdef INTEL_OPT_DIRECTIVES
119         #endif
120         #pragma omp for
121         #pragma novector
122         for (int index = 0; index < nFluid; ++index) { // LOOP list-os
123
124                         #define I(index, dir)   P_INDEX_3((nCells), (index), (dir))
125
126 #ifdef PROP_MODEL_PUSH
127
128                         // Load PDFs of local cell: pdf_N = src[I(x, y, z, D3Q19_N)]; ...
129                         #define X(name, idx, idxinv, _x, _y, _z)        JOIN(pdf_,name) = src[I(index, idx)];
130                         D3Q19_LIST
131                         #undef X
132
133 #elif PROP_MODEL_PULL
134
135                         adjListIndex = index * N_D3Q19_IDX;
136
137                         pdf_C = src[P_INDEX_3(nCells, index, D3Q19_C)];
138
139                         // Load PDFs of local cell: pdf_N = src[adjList[adjListIndex + D3Q19_N]]; ...
140                         #define X(name, idx, idxinv, _x, _y, _z)        JOIN(pdf_,name) = src[adjList[adjListIndex + idx]];
141                         D3Q19_LIST_WO_C
142                         #undef X
143
144 #else
145         #error No implementation for PROP_MODEL_NAME.
146 #endif
147
148 // #define LID_DRIVEN_CAVITY
149
150 #ifdef LID_DRIVEN_CAVITY
151                         int nX = kd->Dims[0];
152                         int nY = kd->Dims[1];
153                         int nZ = kd->Dims[2];
154
155                         int x = kdl->Coords[C_INDEX_X(index)];
156                         int y = kdl->Coords[C_INDEX_Y(index)];
157                         int z = kdl->Coords[C_INDEX_Z(index)];
158
159                         if (z == nZ - 4 && x > 3 && x < (nX - 4) && y > 3 && y < (nY - 4)) {
160                                 ux = 0.1 * 0.577;
161                                 uy = 0.0;
162                                 uz = 0.0;
163                         } else {
164 #endif
165                                 ux = pdf_E + pdf_NE + pdf_SE + pdf_TE + pdf_BE -
166                                          pdf_W - pdf_NW - pdf_SW - pdf_TW - pdf_BW;
167                                 uy = pdf_N + pdf_NE + pdf_NW + pdf_TN + pdf_BN -
168                                          pdf_S - pdf_SE - pdf_SW - pdf_TS - pdf_BS;
169                                 uz = pdf_T + pdf_TE + pdf_TW + pdf_TN + pdf_TS -
170                                          pdf_B - pdf_BE - pdf_BW - pdf_BN - pdf_BS;
171 #ifdef LID_DRIVEN_CAVITY
172                         }
173 #endif
174
175                         dens = pdf_C +
176                                    pdf_N  + pdf_E  + pdf_S  + pdf_W  +
177                                    pdf_NE + pdf_SE + pdf_SW + pdf_NW +
178                                    pdf_T  + pdf_TN + pdf_TE + pdf_TS + pdf_TW +
179                                    pdf_B  + pdf_BN + pdf_BE + pdf_BS + pdf_BW;
180
181                         dir_indep_trm = dens - (ux * ux + uy * uy + uz * uz) * F(3.0) / F(2.0);
182
183 #ifdef PROP_MODEL_PUSH
184
185                         adjListIndex = index * N_D3Q19_IDX;
186
187                         // direction: w_0
188                         dst[I(index, D3Q19_C)             ]  = pdf_C - omegaEven*(pdf_C - w_0*dir_indep_trm);
189
190                         // direction: w_1
191                         w_1_indep = w_1*dir_indep_trm;
192
193                         ui = uy;
194                         evenPart = omegaEven*( F(0.5)*(pdf_N + pdf_S) - ui*ui*w_1_nine_half - w_1_indep );
195                         oddPart = omegaOdd*(F(0.5)*(pdf_N - pdf_S) - ui*w_1_x3 );
196                         dst[adjList[adjListIndex + D3Q19_N]]  = pdf_N - evenPart - oddPart;
197                         dst[adjList[adjListIndex + D3Q19_S]]  = pdf_S - evenPart + oddPart;
198
199                         ui = ux;
200                         evenPart = omegaEven*( F(0.5)*(pdf_E + pdf_W) - ui*ui*w_1_nine_half - w_1_indep );
201                         oddPart = omegaOdd*(F(0.5)*(pdf_E - pdf_W) - ui*w_1_x3 );
202                         dst[adjList[adjListIndex + D3Q19_E]]  = pdf_E - evenPart - oddPart;
203                         dst[adjList[adjListIndex + D3Q19_W]]  = pdf_W - evenPart + oddPart;
204
205                         ui = uz;
206                         evenPart = omegaEven*( F(0.5)*(pdf_T + pdf_B) - ui*ui*w_1_nine_half - w_1_indep );
207                         oddPart = omegaOdd*(F(0.5)*(pdf_T - pdf_B) - ui*w_1_x3 );
208                         dst[adjList[adjListIndex + D3Q19_T]]  = pdf_T - evenPart - oddPart;
209                         dst[adjList[adjListIndex + D3Q19_B]]  = pdf_B - evenPart + oddPart;
210
211                         // direction: w_2
212                         w_2_indep = w_2*dir_indep_trm;
213
214                         ui = -ux + uy;
215                         evenPart = omegaEven*( F(0.5)*(pdf_NW + pdf_SE) - ui*ui*w_2_nine_half - w_2_indep );
216                         oddPart = omegaOdd*(F(0.5)*(pdf_NW - pdf_SE) - ui*w_2_x3 );
217                         dst[adjList[adjListIndex + D3Q19_NW]] = pdf_NW - evenPart - oddPart;
218                         dst[adjList[adjListIndex + D3Q19_SE]] = pdf_SE - evenPart + oddPart;
219
220                         ui = ux + uy;
221                         evenPart = omegaEven*( F(0.5)*(pdf_NE + pdf_SW) - ui*ui*w_2_nine_half - w_2_indep );
222                         oddPart = omegaOdd*(F(0.5)*(pdf_NE - pdf_SW) - ui*w_2_x3 );
223                         dst[adjList[adjListIndex + D3Q19_NE]] = pdf_NE - evenPart - oddPart;
224                         dst[adjList[adjListIndex + D3Q19_SW]] = pdf_SW - evenPart + oddPart;
225
226                         ui = -ux + uz;
227                         evenPart = omegaEven*( F(0.5)*(pdf_TW + pdf_BE) - ui*ui*w_2_nine_half - w_2_indep );
228                         oddPart = omegaOdd*(F(0.5)*(pdf_TW - pdf_BE) - ui*w_2_x3 );
229                         dst[adjList[adjListIndex + D3Q19_TW]] = pdf_TW - evenPart - oddPart;
230                         dst[adjList[adjListIndex + D3Q19_BE]] = pdf_BE - evenPart + oddPart;
231
232                         ui = ux + uz;
233                         evenPart = omegaEven*( F(0.5)*(pdf_TE + pdf_BW) - ui*ui*w_2_nine_half - w_2_indep );
234                         oddPart = omegaOdd*(F(0.5)*(pdf_TE - pdf_BW) - ui*w_2_x3 );
235                         dst[adjList[adjListIndex + D3Q19_TE]] = pdf_TE - evenPart - oddPart;
236                         dst[adjList[adjListIndex + D3Q19_BW]] = pdf_BW - evenPart + oddPart;
237
238                         ui = -uy + uz;
239                         evenPart = omegaEven*( F(0.5)*(pdf_TS + pdf_BN) - ui*ui*w_2_nine_half - w_2_indep );
240                         oddPart = omegaOdd*(F(0.5)*(pdf_TS - pdf_BN) - ui*w_2_x3 );
241                         dst[adjList[adjListIndex + D3Q19_TS]] = pdf_TS - evenPart - oddPart;
242                         dst[adjList[adjListIndex + D3Q19_BN]] = pdf_BN - evenPart + oddPart;
243
244                         ui = uy + uz;
245                         evenPart = omegaEven*( F(0.5)*(pdf_TN + pdf_BS) - ui*ui*w_2_nine_half - w_2_indep );
246                         oddPart = omegaOdd*(F(0.5)*(pdf_TN - pdf_BS) - ui*w_2_x3 );
247                         dst[adjList[adjListIndex + D3Q19_TN]] = pdf_TN - evenPart - oddPart;
248                         dst[adjList[adjListIndex + D3Q19_BS]] = pdf_BS - evenPart + oddPart;
249
250 #elif PROP_MODEL_PULL
251
252                         // direction: w_0
253                         dst[I(index, D3Q19_C )]  = pdf_C - omegaEven*(pdf_C - w_0*dir_indep_trm);
254
255                         // direction: w_1
256                         w_1_indep = w_1*dir_indep_trm;
257
258                         ui = uy;
259                         evenPart = omegaEven*( F(0.5)*(pdf_N + pdf_S) - ui*ui*w_1_nine_half - w_1_indep );
260                         oddPart = omegaOdd*(F(0.5)*(pdf_N - pdf_S) - ui*w_1_x3 );
261                         dst[I(index, D3Q19_N )]  = pdf_N - evenPart - oddPart;
262                         dst[I(index, D3Q19_S )]  = pdf_S - evenPart + oddPart;
263
264                         ui = ux;
265                         evenPart = omegaEven*( F(0.5)*(pdf_E + pdf_W) - ui*ui*w_1_nine_half - w_1_indep );
266                         oddPart = omegaOdd*(F(0.5)*(pdf_E - pdf_W) - ui*w_1_x3 );
267                         dst[I(index, D3Q19_E )]  = pdf_E - evenPart - oddPart;
268                         dst[I(index, D3Q19_W )]  = pdf_W - evenPart + oddPart;
269
270                         ui = uz;
271                         evenPart = omegaEven*( F(0.5)*(pdf_T + pdf_B) - ui*ui*w_1_nine_half - w_1_indep );
272                         oddPart = omegaOdd*(F(0.5)*(pdf_T - pdf_B) - ui*w_1_x3 );
273                         dst[I(index, D3Q19_T )]  = pdf_T - evenPart - oddPart;
274                         dst[I(index, D3Q19_B )]  = pdf_B - evenPart + oddPart;
275
276                         // direction: w_2
277                         w_2_indep = w_2*dir_indep_trm;
278
279                         ui = -ux + uy;
280                         evenPart = omegaEven*( F(0.5)*(pdf_NW + pdf_SE) - ui*ui*w_2_nine_half - w_2_indep );
281                         oddPart = omegaOdd*(F(0.5)*(pdf_NW - pdf_SE) - ui*w_2_x3 );
282                         dst[I(index, D3Q19_NW)] = pdf_NW - evenPart - oddPart;
283                         dst[I(index, D3Q19_SE)] = pdf_SE - evenPart + oddPart;
284
285                         ui = ux + uy;
286                         evenPart = omegaEven*( F(0.5)*(pdf_NE + pdf_SW) - ui*ui*w_2_nine_half - w_2_indep );
287                         oddPart = omegaOdd*(F(0.5)*(pdf_NE - pdf_SW) - ui*w_2_x3 );
288                         dst[I(index, D3Q19_NE)] = pdf_NE - evenPart - oddPart;
289                         dst[I(index, D3Q19_SW)] = pdf_SW - evenPart + oddPart;
290
291                         ui = -ux + uz;
292                         evenPart = omegaEven*( F(0.5)*(pdf_TW + pdf_BE) - ui*ui*w_2_nine_half - w_2_indep );
293                         oddPart = omegaOdd*(F(0.5)*(pdf_TW - pdf_BE) - ui*w_2_x3 );
294                         dst[I(index, D3Q19_TW)] = pdf_TW - evenPart - oddPart;
295                         dst[I(index, D3Q19_BE)] = pdf_BE - evenPart + oddPart;
296
297                         ui = ux + uz;
298                         evenPart = omegaEven*( F(0.5)*(pdf_TE + pdf_BW) - ui*ui*w_2_nine_half - w_2_indep );
299                         oddPart = omegaOdd*(F(0.5)*(pdf_TE - pdf_BW) - ui*w_2_x3 );
300                         dst[I(index, D3Q19_TE)] = pdf_TE - evenPart - oddPart;
301                         dst[I(index, D3Q19_BW)] = pdf_BW - evenPart + oddPart;
302
303                         ui = -uy + uz;
304                         evenPart = omegaEven*( F(0.5)*(pdf_TS + pdf_BN) - ui*ui*w_2_nine_half - w_2_indep );
305                         oddPart = omegaOdd*(F(0.5)*(pdf_TS - pdf_BN) - ui*w_2_x3 );
306                         dst[I(index, D3Q19_TS)] = pdf_TS - evenPart - oddPart;
307                         dst[I(index, D3Q19_BN)] = pdf_BN - evenPart + oddPart;
308
309                         ui = uy + uz;
310                         evenPart = omegaEven*( F(0.5)*(pdf_TN + pdf_BS) - ui*ui*w_2_nine_half - w_2_indep );
311                         oddPart = omegaOdd*(F(0.5)*(pdf_TN - pdf_BS) - ui*w_2_x3 );
312                         dst[I(index, D3Q19_TN)] = pdf_TN - evenPart - oddPart;
313                         dst[I(index, D3Q19_BS)] = pdf_BS - evenPart + oddPart;
314
315 #endif
316                         #undef I
317                 } // loop over fluid nodes
318
319                 #pragma omp single
320                 {
321                         #ifdef VERIFICATION
322                                 kd->PdfsActive = dst;
323                                 KernelAddBodyForce(kd, ld, cd);
324                         #endif
325
326                         #ifdef VTK_OUTPUT
327                                 if (cd->VtkOutput && (iter % cd->VtkModulus) == 0) {
328                                         kd->PdfsActive = dst;
329                                         VtkWrite(ld, kd, cd, iter);
330                                 }
331                         #endif
332
333                         #ifdef STATISTICS
334                                 kd->PdfsActive = dst;
335                                 KernelStatistics(kd, ld, cd, iter);
336                         #endif
337
338                         // swap grids
339                         tmp = src;
340                         src = dst;
341                         dst = tmp;
342                 }
343         }
344         } // for (int iter = 0; ...
345
346         X_LIKWID_STOP("list-os");
347
348         X_KERNEL_END(kernelData);
349
350 #ifdef VTK_OUTPUT
351         if (cd->VtkOutput) {
352                 kd->PdfsActive = src;
353                 VtkWrite(ld, kd, cd, maxIterations);
354         }
355 #endif
356
357 #ifdef STATISTICS
358         kd->PdfsActive = src;
359         KernelStatistics(kd, ld, cd, maxIterations);
360 #endif
361
362         return;
363 }
This page took 0.061417 seconds and 4 git commands to generate.