flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
mg_line_relax.hpp
Go to the documentation of this file.
1// Zebra line relaxation on the pressure-MG levels whose unique strong axis stops coarsening
2// (doc/mg_depth_policy.md Amendment A2.4, WO-7). Included by mac_cutcell_mg.hpp after the FP32
3// V-cycle kernels; CutcellMG drives it (lineSmoothColor / lineSmoothColorFp32) on a TRIGGERED
4// smoothed level (A2.3) in place of the point red-black colour pass.
5//
6// Column = the line along s at fixed inner (a, b) (a < b the other axes, x-y-z order). Colour =
7// (og_a + l_a + og_b + l_b) & 1 (og = parityOg(lv), l local storage indices). Per column, one
8// thread solves the column's tridiagonal (s between walls) or cyclic tridiagonal (s periodic, by
9// Sherman-Morrison) system for the correction, k = 0 ... m-1 (i_k the cell, t the s-stride):
10// r_k = b_k - (A x)_k the level's full operator at the current x (FP64 cutcellApplyFaceCell |
11// FP32 fp32Flux: x = c * 1 and b = 0 give delta = 0 exactly);
12// d_k = AC(i_k) | fp32Diag(i_k); l_k = AF_s(i_k) | -W_s(i_k); u_k = AF_s(i_k + t) |
13// -W_s(i_k+t); decoupled cell (AC < 1e-30 | D = 0): d_k = 1, l_k = u_k = r_k = 0. s non-periodic:
14// l_0 = u_{m-1} := 0, Thomas. s periodic: beta = l_0, alpha = u_{m-1} (the wrap face); alpha =
15// beta = 0 -> Thomas; else Sherman-Morrison with gamma = -d_0, d'_0 = d_0 - gamma, d'_{m-1} =
16// d_{m-1} - alpha beta / gamma: T'y = r and T'z = (gamma, 0, ..., 0, alpha)^T in the same passes,
17// f = (y_0 + beta y_{m-1}/gamma) / (1 + z_0 + beta z_{m-1}/gamma) (f := 0 if that denominator's
18// magnitude < eps_p), delta = y - f z. Thomas: p_0 = d'_0; p_k = d'_k - l_k c_{k-1}, p_k := d'_k
19// if p_k < eps_p d'_k (a sealed Neumann segment); c_k = u_k / p_k; y_k = (r_k - l_k y_{k-1}) /
20// p_k (z alike); back substitution y_k -= c_k y_{k+1}, k = m-2 ... 0. eps_p = 64 eps_mach
21// (1.42e-14 | 7.63e-6). x_k += delta_k.
22// r_k is formed in the forward pass from the x the pass started with: x changes only in the final
23// update loop, so every r_k reads the pre-pass x (the column's own cells included) and the order of
24// operations is the one written above; where c, y, z live therefore changes no bit.
25//
26// Neighbours: ccMaskNbrs with the caller's wrap mask (0: the ghosts, filled by the caller; 7: the
27// single-rank in-kernel wrap of ccWrapNbrs; the level's stencil wrap axes under MPI). The
28// coefficients are read at their ghost indices as the point kernels read them.
29//
30// Launch: device MDRange2<CCExec> over the inner (a, b), x fastest, colour filter; host RangePolicy
31// over the b-rows (each row's colour columns in a loop), serial behind hostRunSerial.
32#pragma once
33
34namespace peclet::flow {
35
36// The inner (a, b) axes of a column along s (a < b, x-y-z order).
37KOKKOS_INLINE_FUNCTION void lineAxesOf(int s, int& a, int& b) {
38 a = (s == 0) ? 1 : 0;
39 b = (s == 2) ? 1 : 2;
40}
41
42// FP64 row access of the face-form operator (the point smoother's cutcellSmoothFaceCell operands).
43template <class OpV>
45 using Real = double;
46 static constexpr double kEps = 64.0 * std::numeric_limits<double>::epsilon(); // 1.42e-14
50 long sx, sy, sz, t;
51 int s;
52 KOKKOS_INLINE_FUNCTION const OpV& afs() const { return s == 0 ? AFX : (s == 1 ? AFY : AFZ); }
53 // d, l, u of row i; false (d = 1, l = u = 0) on a decoupled cell.
54 KOKKOS_INLINE_FUNCTION bool coef(long i, double& d, double& l, double& u) const {
55 const double ac = AC(i);
56 if (ac < 1e-30) {
57 d = 1.0;
58 l = 0.0;
59 u = 0.0;
60 return false;
61 }
62 d = ac;
63 l = afs()(i);
64 u = afs()(i + t);
65 return true;
66 }
67 KOKKOS_INLINE_FUNCTION double res(long i, const CcNbrs& w) const {
68 return b(i) - cutcellApplyFaceCell(x, AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
69 w.zp, w.zm);
70 }
71};
72
73// FP32 row access of the face-weight form (fp32SmoothCell's operands). No double in this body.
75 using Real = VReal;
76 static constexpr VReal kEps = 64.0f * std::numeric_limits<VReal>::epsilon(); // 7.63e-6
79 long sx, sy, sz, t;
80 int s;
81 KOKKOS_INLINE_FUNCTION const VConst& ws() const { return s == 0 ? WX : (s == 1 ? WY : WZ); }
82 KOKKOS_INLINE_FUNCTION bool coef(long i, VReal& d, VReal& l, VReal& u) const {
83 const VReal D = fp32Diag(WX, WY, WZ, i, sx, sy, sz);
84 if (D == 0.0f) {
85 d = 1.0f;
86 l = 0.0f;
87 u = 0.0f;
88 return false;
89 }
90 d = D;
91 l = -ws()(i);
92 u = -ws()(i + t);
93 return true;
94 }
95 KOKKOS_INLINE_FUNCTION VReal res(long i, const CcNbrs& w) const {
96 return b(i) - fp32Flux(x, WX, WY, WZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym, w.zp, w.zm);
97 }
98};
99
100// The column's c, y, z by k: the level-extent scratch (indexed by cell) or, for m <= kLineRegs,
101// thread-local arrays (A2.6: "hold c, y, z in registers for m <= 64"). The arithmetic and its
102// order are the same in both, so the result is bitwise the same (A2.4).
103constexpr int kLineRegs = 64;
104template <class SV>
106 SV cv, yv, zv;
107 long i0, t;
108 KOKKOS_INLINE_FUNCTION typename SV::reference_type c(int k) const { return cv(i0 + (long)k * t); }
109 KOKKOS_INLINE_FUNCTION typename SV::reference_type y(int k) const { return yv(i0 + (long)k * t); }
110 KOKKOS_INLINE_FUNCTION typename SV::reference_type z(int k) const { return zv(i0 + (long)k * t); }
111};
112template <class Real>
115 KOKKOS_INLINE_FUNCTION Real& c(int k) { return cv[k]; }
116 KOKKOS_INLINE_FUNCTION Real& y(int k) { return yv[k]; }
117 KOKKOS_INLINE_FUNCTION Real& z(int k) { return zv[k]; }
118};
119
120// One column along s from its first cell i0 (local coordinates l3 with l3[s] = g): the A2.4 solve
121// and the update x_k += delta_k, with the column's c, y, z in S.
122template <class Row, class Store>
123KOKKOS_INLINE_FUNCTION void lineColumnSolve(const Row& R, Store& S, long i0, int m, int l3[3], C3 n,
124 int g, int wrapMask, bool periodic) {
125 using Real = typename Row::Real;
126 const Real eps = Row::kEps;
127 const long t = R.t;
128 // s periodic: beta = l_0, alpha = u_{m-1} (after the decoupled-cell zeroing); both zero ->
129 // Thomas.
130 Real alpha = Real(0), beta = Real(0);
131 bool sm = false;
132 if (periodic) {
133 Real d, l, u;
134 R.coef(i0, d, l, u);
135 beta = l;
136 R.coef(i0 + (long)(m - 1) * t, d, l, u);
137 alpha = u;
138 sm = !(alpha == Real(0) && beta == Real(0));
139 }
140 Real gamma = Real(0);
141 Real cPrev = Real(0), yPrev = Real(0), zPrev = Real(0);
142 for (int k = 0; k < m; ++k) {
143 const long i = i0 + (long)k * t;
144 l3[R.s] = g + k;
145 Real d, l, u;
146 Real r = Real(0);
147 if (R.coef(i, d, l, u)) {
148 const CcNbrs w = ccMaskNbrs(l3[0], l3[1], l3[2], n, g, i, R.sy, R.sz, wrapMask);
149 r = R.res(i, w);
150 }
151 if (!periodic) {
152 if (k == 0)
153 l = Real(0);
154 if (k == m - 1)
155 u = Real(0);
156 }
157 Real dp = d;
158 if (sm) {
159 if (k == 0) {
160 gamma = -d;
161 dp = d - gamma;
162 }
163 if (k == m - 1)
164 dp = d - alpha * beta / gamma;
165 }
166 const Real zr = !sm ? Real(0) : (k == 0 ? gamma : (k == m - 1 ? alpha : Real(0)));
167 Real p, yk, zk;
168 if (k == 0) {
169 p = dp;
170 yk = r / p;
171 zk = zr / p;
172 } else {
173 p = dp - l * cPrev;
174 if (p < eps * dp)
175 p = dp; // a sealed Neumann segment
176 yk = (r - l * yPrev) / p;
177 zk = (zr - l * zPrev) / p;
178 }
179 const Real ck = u / p;
180 S.c(k) = ck;
181 S.y(k) = yk;
182 if (sm)
183 S.z(k) = zk;
184 cPrev = ck;
185 yPrev = yk;
186 zPrev = zk;
187 }
188 for (int k = m - 2; k >= 0; --k) {
189 S.y(k) -= S.c(k) * S.y(k + 1);
190 if (sm)
191 S.z(k) -= S.c(k) * S.z(k + 1);
192 }
193 Real f = Real(0);
194 if (sm) {
195 const Real den = Real(1) + S.z(0) + beta * S.z(m - 1) / gamma;
196 if (!(Kokkos::fabs(den) < eps))
197 f = (S.y(0) + beta * S.y(m - 1) / gamma) / den;
198 }
199 for (int k = 0; k < m; ++k) {
200 const long i = i0 + (long)k * t;
201 if (sm)
202 R.x(i) = R.x(i) + (S.y(k) - f * S.z(k));
203 else
204 R.x(i) = R.x(i) + S.y(k);
205 }
206}
207
208// One column (la, lb) along s. c, y, z: the level-extent scratch, used when m > kLineRegs.
209template <class Row, class SV>
210KOKKOS_INLINE_FUNCTION void lineColumn(const Row& R, const SV& c, const SV& y, const SV& z, int la,
211 int lb, C3 n, int g, int wrapMask, bool periodic) {
212 using Real = typename Row::Real;
213 int a = 0, bb = 0;
214 lineAxesOf(R.s, a, bb);
215 const int m = R.s == 0 ? n.x : (R.s == 1 ? n.y : n.z);
216 int l3[3];
217 l3[a] = la;
218 l3[bb] = lb;
219 l3[R.s] = g;
220 const long i0 = (long)l3[0] + (long)l3[1] * R.sy + (long)l3[2] * R.sz;
221 if (m <= kLineRegs) {
223 lineColumnSolve(R, S, i0, m, l3, n, g, wrapMask, periodic);
224 } else {
225 LineScratchGlobal<SV> S{c, y, z, i0, R.t};
226 lineColumnSolve(R, S, i0, m, l3, n, g, wrapMask, periodic);
227 }
228}
229
230// One colour of the line smoother over every column of the level (A2.4 launch shape).
231template <class Row, class SV>
232inline void lineRelaxColor(const Row& R, const SV& c, const SV& y, const SV& z, C3 e, C3 og, int g,
233 int wrapMask, bool periodic, int color, const char* name,
234 const char* serialName) {
235 const C3 n{e.x - 2 * g, e.y - 2 * g, e.z - 2 * g};
236 int a = 0, b = 0;
237 lineAxesOf(R.s, a, b);
238 const int ea = a == 0 ? e.x : e.y, eb = b == 1 ? e.y : e.z;
239 const int oga = a == 0 ? og.x : og.y, ogb = b == 1 ? og.y : og.z;
240 CCExec space;
241 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
242 const int nb = eb - 2 * g;
243 const long cells = (long)n.x * n.y * n.z;
244 auto row = KOKKOS_LAMBDA(long q) {
245 const int lb = g + (int)q;
246 const int P = (color + oga + ogb + lb) & 1;
247 for (int la = g + ((P ^ (g & 1)) & 1); la < ea - g; la += 2)
248 lineColumn(R, c, y, z, la, lb, n, g, wrapMask, periodic);
249 };
250 if (hostRunSerial(cells)) {
251 const HostSerialRegion serialRegion(serialName);
252 for (long q = 0; q < (long)nb; ++q)
253 row(q);
254 return;
255 }
256 Kokkos::parallel_for(name, Kokkos::RangePolicy<CCExec>(space, 0, (long)nb), row);
257 return;
258 }
259 Kokkos::parallel_for(
260 name, MDRange2<CCExec>(space, {g, g}, {ea - g, eb - g}), KOKKOS_LAMBDA(int la, int lb) {
261 if (((oga + la + ogb + lb) & 1) != color)
262 return;
263 lineColumn(R, c, y, z, la, lb, n, g, wrapMask, periodic);
264 });
265}
266
267// FP64: one colour of the line smoother on the face-form operator (x = phi, b = rhs).
268template <class OpV>
269inline void lineRelaxColorFace(CCField x, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ, CCField c,
270 CCField y, CCField z, C3 e, C3 og, int g, int s, bool periodic,
271 int wrapMask, int color) {
272 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
273 const long t = s == 0 ? sx : (s == 1 ? sy : sz);
274 const LineRowFp64<OpV> R{x, b, AC, AFX, AFY, AFZ, sx, sy, sz, t, s};
275 lineRelaxColor(R, c, y, z, e, og, g, wrapMask, periodic, color, "peclet::flow::cc_line_smooth",
276 "peclet::flow::cc_line_smooth@serial");
277}
278
279// FP32: one colour of the line smoother on the FP32 face weights.
280inline void lineRelaxColorFp32(VField x, VConst b, VConst WX, VConst WY, VConst WZ, VField c,
281 VField y, VField z, C3 e, C3 og, int g, int s, bool periodic,
282 int wrapMask, int color) {
283 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
284 const long t = s == 0 ? sx : (s == 1 ? sy : sz);
285 const LineRowFp32 R{x, b, WX, WY, WZ, sx, sy, sz, t, s};
286 lineRelaxColor(R, c, y, z, e, og, g, wrapMask, periodic, color,
287 "peclet::flow::cc_line_smooth_f32", "peclet::flow::cc_line_smooth_f32@serial");
288}
289
290} // namespace peclet::flow
Kokkos::DefaultExecutionSpace CCExec
CcNbrs ccMaskNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz, int m)
void lineColumn(const Row &R, const SV &c, const SV &y, const SV &z, int la, int lb, C3 n, int g, int wrapMask, bool periodic)
void lineRelaxColor(const Row &R, const SV &c, const SV &y, const SV &z, C3 e, C3 og, int g, int wrapMask, bool periodic, int color, const char *name, const char *serialName)
constexpr int kLineRegs
Kokkos::View< const double *, CCMem > CCConst
void lineColumnSolve(const Row &R, Store &S, long i0, int m, int l3[3], C3 n, int g, int wrapMask, bool periodic)
VReal fp32Diag(const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz)
Kokkos::View< VReal *, CCMem > VField
VReal fp32Flux(const XV &x, const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
void lineRelaxColorFace(CCField x, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ, CCField c, CCField y, CCField z, C3 e, C3 og, int g, int s, bool periodic, int wrapMask, int color)
Kokkos::View< double *, CCMem > CCField
void lineRelaxColorFp32(VField x, VConst b, VConst WX, VConst WY, VConst WZ, VField c, VField y, VField z, C3 e, C3 og, int g, int s, bool periodic, int wrapMask, int color)
void lineAxesOf(int s, int &a, int &b)
bool hostRunSerial(long cells)
double cutcellApplyFaceCell(const XV &x, const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 2, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange2
2-D range, first index fastest (both the tile order and the order within a tile).
Definition policy.hpp:31
Kokkos::View< const VReal *, CCMem > VConst
const VConst & ws() const
VReal res(long i, const CcNbrs &w) const
static constexpr VReal kEps
bool coef(long i, VReal &d, VReal &l, VReal &u) const
static constexpr double kEps
bool coef(long i, double &d, double &l, double &u) const
double res(long i, const CcNbrs &w) const
const OpV & afs() const
SV::reference_type y(int k) const
SV::reference_type z(int k) const
SV::reference_type c(int k) const
static constexpr double AC
Kokkos::View< double *, peclet::flow::CCMem > OpV
static long l3(int x, int y, int z, C3 e)
Definition test_ibm.cpp:15
static constexpr double R