flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
mac_stencils.hpp
Go to the documentation of this file.
1
11#ifndef PECLET_FLOW_MAC_STENCILS_HPP
12#define PECLET_FLOW_MAC_STENCILS_HPP
13
14#include <Kokkos_Core.hpp>
15#include <type_traits>
16
17#include "policy.hpp"
18
19namespace peclet::flow {
20
21using SExec = Kokkos::DefaultExecutionSpace;
22using SMem = SExec::memory_space;
23using SField = Kokkos::View<double*, SMem>;
24using SConst = Kokkos::View<const double*, SMem>;
25
26struct I3 {
27 int x, y, z;
28};
29
30KOKKOS_INLINE_FUNCTION long L3(int x, int y, int z, I3 e) {
31 return static_cast<long>(x) + static_cast<long>(y) * e.x +
32 static_cast<long>(z) * static_cast<long>(e.x) * e.y;
33}
34
35// One Red-Black sweep colour of the implicit-diffusion smoother:
36// c[i] = (b[i] + beta*sum_neighbours) / (Ac + dcorr[i]). Call for colour 0 then 1.
37// Host backends take the line-sweep form (one (y,z) pencil per task, stride-2 x-loop at the
38// colour's parity — bit-identical, same-colour cells are independent); device keeps MDRange.
39//
40// ANISO DISPATCH (doc/anisotropic_metric.md §2, trap 4). With per-axis viscous coefficients the
41// numerator is `bx*(sW+sE) + by*(sS+sN) + bz*(sB+sT)`, which SUMS IN A DIFFERENT ORDER from the
42// isotropic `beta*(sW+sE+sS+sN+sB+sT)` and is therefore not bit-identical at bx==by==bz. The
43// isotropic path is the reference: `Aniso == false` runs today's expression LITERALLY, and the
44// per-axis body exists only for a genuinely stretched domain.
45template <bool Aniso>
46inline void diffSmoothColorT(SField c, SConst b, I3 e, I3 og, int g, double bx, double by,
47 double bz, double Ac, int color, SConst dcorr) {
48 SExec space;
49 const bool hasD = (dcorr.extent(0) != 0);
50 if constexpr (std::is_same_v<typename SExec::memory_space, Kokkos::HostSpace>) {
51 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
52 Kokkos::parallel_for(
53 "peclet::flow::diff", Kokkos::RangePolicy<SExec>(space, 0, (long)nyi * nzi),
54 KOKKOS_LAMBDA(long t) {
55 const int y = g + (int)(t % nyi), z = g + (int)(t / nyi);
56 const long sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
57 const int P = (color + og.x + og.y + y + og.z + z) & 1;
58 for (int x = g + ((P ^ (g & 1)) & 1); x < e.x - g; x += 2) {
59 const long i = L3(x, y, z, e);
60 // nvcc forbids an extended lambda FIRST-capturing a variable inside an
61 // `if constexpr`, so name the coefficients and read the six neighbours here. Both
62 // are exact: the copies are copies and the sums below keep the legacy operand order.
63 const double b0 = bx, b1 = by, b2 = bz;
64 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy),
65 nT = c(i + sz), nB = c(i - sz);
66 double s;
67 if constexpr (Aniso)
68 s = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
69 else
70 s = b0 * (nE + nW + nN + nS + nT + nB);
71 c(i) = (b(i) + s) / (Ac + (hasD ? dcorr(i) : 0.0));
72 }
73 });
74 return;
75 }
76 using MD = MDRange3<SExec>;
77 Kokkos::parallel_for(
78 "peclet::flow::diff", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
79 KOKKOS_LAMBDA(int x, int y, int z) {
80 if ((((x + og.x) + (y + og.y) + (z + og.z)) & 1) != color)
81 return;
82 const long i = L3(x, y, z, e), sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
83 const double b0 = bx, b1 = by, b2 = bz; // first-capture outside the constexpr-if (nvcc)
84 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy), nT = c(i + sz),
85 nB = c(i - sz);
86 double s;
87 if constexpr (Aniso)
88 s = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
89 else
90 s = b0 * (nE + nW + nN + nS + nT + nB);
91 c(i) = (b(i) + s) / (Ac + (hasD ? dcorr(i) : 0.0));
92 });
93}
94
95// Per-axis entry point: `aniso` selects the per-axis body, `!aniso` the legacy one (bx is beta).
96inline void diffSmoothColor(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz,
97 double Ac, int color, SConst dcorr, bool aniso) {
98 if (aniso)
99 diffSmoothColorT<true>(c, b, e, og, g, bx, by, bz, Ac, color, dcorr);
100 else
101 diffSmoothColorT<false>(c, b, e, og, g, bx, by, bz, Ac, color, dcorr);
102}
103
104// Isotropic spelling (the legacy signature).
105inline void diffSmoothColor(SField c, SConst b, I3 e, I3 og, int g, double beta, double Ac,
106 int color, SConst dcorr) {
107 diffSmoothColorT<false>(c, b, e, og, g, beta, beta, beta, Ac, color, dcorr);
108}
109
110// Residual r = b - A c of the constant-coefficient folded diffusion operator over the inner
111// cells (both colours): A c = (Ac + dcorr) c - beta * sum(neighbours). The residual-based
112// momentum stop for the all-fluid domain-BC path (velSweepLoop's `resid` functor).
113template <bool Aniso>
114inline void diffResidualT(SField r, SConst c, SConst b, I3 e, int g, double bx, double by,
115 double bz, double Ac, SConst dcorr) {
116 SExec space;
117 const bool hasD = (dcorr.extent(0) != 0);
118 using MD = MDRange3<SExec>;
119 Kokkos::parallel_for(
120 "peclet::flow::diff_resid", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
121 KOKKOS_LAMBDA(int x, int y, int z) {
122 const long i = L3(x, y, z, e), sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
123 const double b0 = bx, b1 = by, b2 = bz; // first-capture outside the constexpr-if (nvcc)
124 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy), nT = c(i + sz),
125 nB = c(i - sz);
126 double sum;
127 if constexpr (Aniso)
128 sum = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
129 else
130 sum = b0 * (nE + nW + nN + nS + nT + nB);
131 r(i) = b(i) - ((Ac + (hasD ? dcorr(i) : 0.0)) * c(i) - sum);
132 });
133}
134
135// F-1 (doc/vof_projection_cost_design.md §7, velSweepLoop's constant-coefficient stop functor):
136// max|r| and max|b - r| with diffResidualT's VERBATIM cell body, r taken as 0 on the held plane
137// (index `holdIdx` along `holdAxis`; < 0 = none), no write of r. Max is order-free: bitwise to
138// diffResidual + zeroPlane + the two max reductions; `bmax` = max|b|.
139template <bool Aniso>
140inline void diffResidualMaxT(SConst c, SConst b, I3 e, int g, double bx, double by, double bz,
141 double Ac, SConst dcorr, int holdAxis, int holdIdx, double& rmax,
142 double& bdmax, double& bmax) {
143 SExec space;
144 const bool hasD = (dcorr.extent(0) != 0);
145 using MD = MDRange3<SExec>;
146 Kokkos::parallel_reduce(
147 "peclet::flow::diff_resid_max", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
148 KOKKOS_LAMBDA(int x, int y, int z, double& m, double& d, double& bm) {
149 const long i = L3(x, y, z, e), sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
150 const double b0 = bx, b1 = by, b2 = bz; // first-capture outside the constexpr-if (nvcc)
151 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy), nT = c(i + sz),
152 nB = c(i - sz);
153 double sum;
154 if constexpr (Aniso)
155 sum = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
156 else
157 sum = b0 * (nE + nW + nN + nS + nT + nB);
158 double r = b(i) - ((Ac + (hasD ? dcorr(i) : 0.0)) * c(i) - sum);
159 if (holdAxis >= 0 && (holdAxis == 0 ? x : (holdAxis == 1 ? y : z)) == holdIdx)
160 r = 0.0;
161 const double ar = Kokkos::fabs(r);
162 if (ar > m)
163 m = ar;
164 const double ad = Kokkos::fabs(b(i) - r);
165 if (ad > d)
166 d = ad;
167 const double ab = Kokkos::fabs(b(i));
168 if (ab > bm)
169 bm = ab;
170 },
171 Kokkos::Max<double>(rmax), Kokkos::Max<double>(bdmax), Kokkos::Max<double>(bmax));
172}
173inline void diffResidualMax(SConst c, SConst b, I3 e, int g, double bx, double by, double bz,
174 double Ac, SConst dcorr, bool aniso, int holdAxis, int holdIdx,
175 double& rmax, double& bdmax, double& bmax) {
176 if (aniso)
177 diffResidualMaxT<true>(c, b, e, g, bx, by, bz, Ac, dcorr, holdAxis, holdIdx, rmax, bdmax, bmax);
178 else
179 diffResidualMaxT<false>(c, b, e, g, bx, by, bz, Ac, dcorr, holdAxis, holdIdx, rmax, bdmax,
180 bmax);
181}
182
183// Per-axis entry point (see diffSmoothColor's ANISO DISPATCH note: the summation order differs).
184inline void diffResidual(SField r, SConst c, SConst b, I3 e, int g, double bx, double by, double bz,
185 double Ac, SConst dcorr, bool aniso) {
186 if (aniso)
187 diffResidualT<true>(r, c, b, e, g, bx, by, bz, Ac, dcorr);
188 else
189 diffResidualT<false>(r, c, b, e, g, bx, by, bz, Ac, dcorr);
190}
191
192// Isotropic spelling (the legacy signature).
193inline void diffResidual(SField r, SConst c, SConst b, I3 e, int g, double beta, double Ac,
194 SConst dcorr) {
195 diffResidualT<false>(r, c, b, e, g, beta, beta, beta, Ac, dcorr);
196}
197
198// diffSmoothColor + fused max|Δ| reduction over the swept colour (see ibmRbgsStencilColorDu):
199// runs as the second colour of a sweep when the momentum tolerance stop is active.
200template <bool Aniso>
201inline double diffSmoothColorDuT(SField c, SConst b, I3 e, I3 og, int g, double bx, double by,
202 double bz, double Ac, int color, SConst dcorr) {
203 SExec space;
204 const bool hasD = (dcorr.extent(0) != 0);
205 double du = 0.0;
206 if constexpr (std::is_same_v<typename SExec::memory_space, Kokkos::HostSpace>) {
207 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
208 Kokkos::parallel_reduce(
209 "peclet::flow::diff_du", Kokkos::RangePolicy<SExec>(space, 0, (long)nyi * nzi),
210 KOKKOS_LAMBDA(long t, double& m) {
211 const int y = g + (int)(t % nyi), z = g + (int)(t / nyi);
212 const long sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
213 const int P = (color + og.x + og.y + y + og.z + z) & 1;
214 for (int x = g + ((P ^ (g & 1)) & 1); x < e.x - g; x += 2) {
215 const long i = L3(x, y, z, e);
216 // nvcc forbids an extended lambda FIRST-capturing a variable inside an
217 // `if constexpr`, so name the coefficients and read the six neighbours here. Both
218 // are exact: the copies are copies and the sums below keep the legacy operand order.
219 const double b0 = bx, b1 = by, b2 = bz;
220 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy),
221 nT = c(i + sz), nB = c(i - sz);
222 double s;
223 if constexpr (Aniso)
224 s = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
225 else
226 s = b0 * (nE + nW + nN + nS + nT + nB);
227 const double cn = (b(i) + s) / (Ac + (hasD ? dcorr(i) : 0.0));
228 const double d = Kokkos::fabs(cn - c(i));
229 if (d > m)
230 m = d;
231 c(i) = cn;
232 }
233 },
234 Kokkos::Max<double>(du));
235 return du;
236 }
237 using MD = MDRange3<SExec>;
238 Kokkos::parallel_reduce(
239 "peclet::flow::diff_du", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
240 KOKKOS_LAMBDA(int x, int y, int z, double& m) {
241 if ((((x + og.x) + (y + og.y) + (z + og.z)) & 1) != color)
242 return;
243 const long i = L3(x, y, z, e), sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
244 const double b0 = bx, b1 = by, b2 = bz; // first-capture outside the constexpr-if (nvcc)
245 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy), nT = c(i + sz),
246 nB = c(i - sz);
247 double s;
248 if constexpr (Aniso)
249 s = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
250 else
251 s = b0 * (nE + nW + nN + nS + nT + nB);
252 const double cn = (b(i) + s) / (Ac + (hasD ? dcorr(i) : 0.0));
253 const double d = Kokkos::fabs(cn - c(i));
254 if (d > m)
255 m = d;
256 c(i) = cn;
257 },
258 Kokkos::Max<double>(du));
259 return du;
260}
261
262// Per-axis entry point (see diffSmoothColor's ANISO DISPATCH note).
263inline double diffSmoothColorDu(SField c, SConst b, I3 e, I3 og, int g, double bx, double by,
264 double bz, double Ac, int color, SConst dcorr, bool aniso) {
265 return aniso ? diffSmoothColorDuT<true>(c, b, e, og, g, bx, by, bz, Ac, color, dcorr)
266 : diffSmoothColorDuT<false>(c, b, e, og, g, bx, by, bz, Ac, color, dcorr);
267}
268
269// Isotropic spelling (the legacy signature).
270inline double diffSmoothColorDu(SField c, SConst b, I3 e, I3 og, int g, double beta, double Ac,
271 int color, SConst dcorr) {
272 return diffSmoothColorDuT<false>(c, b, e, og, g, beta, beta, beta, Ac, color, dcorr);
273}
274
275// One Red-Black sweep colour of the (unit-coefficient) Poisson smoother: phi[i] = (sum - d[i]) / 6.
276inline void poisSmoothColor(SField phi, SConst d, I3 e, I3 og, int g, int color) {
277 SExec space;
278 using MD = MDRange3<SExec>;
279 Kokkos::parallel_for(
280 "peclet::flow::pois", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
281 KOKKOS_LAMBDA(int x, int y, int z) {
282 if ((((x + og.x) + (y + og.y) + (z + og.z)) & 1) != color)
283 return;
284 const long i = L3(x, y, z, e), sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
285 const double s =
286 phi(i + sx) + phi(i - sx) + phi(i + sy) + phi(i - sy) + phi(i + sz) + phi(i - sz);
287 phi(i) = (s - d(i)) / 6.0;
288 });
289}
290
291// MAC divergence d[i] = (u[i+sx]-u[i]) + (v[i+sy]-v[i]) + (w[i+sz]-w[i]) over inner cells.
292inline void divergence(SConst u, SConst v, SConst w, SField d, I3 e, int g) {
293 SExec space;
294 using MD = MDRange3<SExec>;
295 Kokkos::parallel_for(
296 "peclet::flow::diverg", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
297 KOKKOS_LAMBDA(int x, int y, int z) {
298 const long i = L3(x, y, z, e), sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
299 d(i) = (u(i + sx) - u(i)) + (v(i + sy) - v(i)) + (w(i + sz) - w(i));
300 });
301}
302
303// Full Red-Black Gauss-Seidel sweep (both colours) of the Poisson smoother.
304inline void poisSweep(SField phi, SConst d, I3 e, I3 og, int g) {
305 poisSmoothColor(phi, d, e, og, g, 0);
306 poisSmoothColor(phi, d, e, og, g, 1);
307}
308
309} // namespace peclet::flow
310
311#endif // PECLET_FLOW_MAC_STENCILS_HPP
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange3
3-D range, x (the first index) fastest (both the tile order and the order within a tile).
Definition policy.hpp:36
Kokkos::View< double *, SMem > SField
void diffResidualT(SField r, SConst c, SConst b, I3 e, int g, double bx, double by, double bz, double Ac, SConst dcorr)
long L3(int x, int y, int z, I3 e)
void divergence(SConst u, SConst v, SConst w, SField d, I3 e, int g)
void diffSmoothColorT(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz, double Ac, int color, SConst dcorr)
double diffSmoothColorDu(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz, double Ac, int color, SConst dcorr, bool aniso)
double diffSmoothColorDuT(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz, double Ac, int color, SConst dcorr)
void diffResidual(SField r, SConst c, SConst b, I3 e, int g, double bx, double by, double bz, double Ac, SConst dcorr, bool aniso)
Kokkos::DefaultExecutionSpace SExec
SExec::memory_space SMem
void poisSmoothColor(SField phi, SConst d, I3 e, I3 og, int g, int color)
Kokkos::View< const double *, SMem > SConst
void diffResidualMax(SConst c, SConst b, I3 e, int g, double bx, double by, double bz, double Ac, SConst dcorr, bool aniso, int holdAxis, int holdIdx, double &rmax, double &bdmax, double &bmax)
void diffSmoothColor(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz, double Ac, int color, SConst dcorr, bool aniso)
void diffResidualMaxT(SConst c, SConst b, I3 e, int g, double bx, double by, double bz, double Ac, SConst dcorr, int holdAxis, int holdIdx, double &rmax, double &bdmax, double &bmax)
void poisSweep(SField phi, SConst d, I3 e, I3 og, int g)
flow — the multidimensional iteration policies: iteration order follows storage.