flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_mg_line_relax.cpp
Go to the documentation of this file.
1
21#include <cmath>
22#include <cstdio>
23#include <cstring>
24#include <Kokkos_Core.hpp>
25#include <random>
26#include <string>
27#include <vector>
28
29#include "mac_cutcell_mg.hpp"
30#include "mg_fp32_problems.hpp"
31
32using namespace peclet::flow;
33using namespace peclet_flow_fp32_problems;
34
35namespace {
36
37int fails = 0;
38void check(bool ok, const std::string& what) {
39 std::printf(" %-78s %s\n", what.c_str(), ok ? "PASS" : "FAIL");
40 std::fflush(stdout);
41 if (!ok)
42 ++fails;
43}
44std::string num(double v) {
45 char b[32];
46 std::snprintf(b, sizeof b, "%.3g", v);
47 return b;
48}
49
50constexpr int NX = 32, NY = 32;
51constexpr int SX = 5, SY = 7; // the sealed column (0-based inner coordinates)
52
53Problem makeLineProblem(bool walls, int NZ) {
54 Problem P;
55 P.name = walls ? "z walls" : "z periodic";
56 P.nx = NX;
57 P.ny = NY;
58 P.nz = NZ;
59 P.levels = 2;
60 if (walls)
61 P.bc[4] = P.bc[5] = 1;
62 const C3 e = P.ext();
63 const std::size_t n = (std::size_t)e.x * e.y * e.z;
64 P.ox.assign(n, 0.0);
65 P.oy.assign(n, 0.0);
66 P.oz.assign(n, 0.0);
67 std::mt19937 rng(20261026u);
68 std::uniform_real_distribution<double> open(0.05, 1.0), u01(0.0, 1.0);
69 auto face = [&] { return u01(rng) < 0.05 ? 0.0 : open(rng); };
70 for (int z = 0; z < NZ; ++z)
71 for (int y = 0; y < NY; ++y)
72 for (int x = 0; x < NX; ++x) {
73 const long i = P.idx(x, y, z);
74 P.ox[i] = face();
75 P.oy[i] = face();
76 P.oz[i] = face();
77 }
78 for (int z = 0; z < NZ; ++z) { // seal the column (SX, SY) laterally (x, y periodic)
79 P.ox[P.idx(SX, SY, z)] = 0.0;
80 P.ox[P.idx((SX + 1) % NX, SY, z)] = 0.0;
81 P.oy[P.idx(SX, SY, z)] = 0.0;
82 P.oy[P.idx(SX, (SY + 1) % NY, z)] = 0.0;
83 }
84 return P;
85}
86
87// Dense solve of M d = r (m x m, long double, partial pivoting). false if singular.
88bool denseSolve(std::vector<long double> M, std::vector<long double> r, int m,
89 std::vector<long double>& d) {
90 for (int c = 0; c < m; ++c) {
91 int p = c;
92 for (int k = c + 1; k < m; ++k)
93 if (std::fabs(M[k * m + c]) > std::fabs(M[p * m + c]))
94 p = k;
95 if (M[p * m + c] == 0.0L)
96 return false;
97 if (p != c) {
98 for (int j = 0; j < m; ++j)
99 std::swap(M[c * m + j], M[p * m + j]);
100 std::swap(r[c], r[p]);
101 }
102 for (int k = c + 1; k < m; ++k) {
103 const long double f = M[k * m + c] / M[c * m + c];
104 for (int j = c; j < m; ++j)
105 M[k * m + j] -= f * M[c * m + j];
106 r[k] -= f * r[c];
107 }
108 }
109 d.assign(m, 0.0L);
110 for (int k = m - 1; k >= 0; --k) {
111 long double s = r[k];
112 for (int j = k + 1; j < m; ++j)
113 s -= M[k * m + j] * d[j];
114 d[k] = s / M[k * m + k];
115 }
116 return true;
117}
118
119// The host operator of one level: row coefficients of every inner cell, with wrapped reads.
120struct HostOp {
121 C3 e, n;
122 long sy, sz;
123 std::vector<long double> AC, AF[3]; // FP64: AC, AF; FP32: D (as fp32Diag), -W
124 long idx(int lx, int ly, int lz) const { return (long)lx + (long)ly * sy + (long)lz * sz; }
125 bool decoupled(long i, bool fp32) const { return fp32 ? AC[i] == 0.0L : AC[i] < 1e-30L; }
126};
127
128// The reference pass: on every column (lx, ly) of `color`, x_k + delta_k with A_col delta = r.
129// x, b: host copies (level extent). Returns the max relative error over the columns compared;
130// sealed: the sealed column's comparison is skipped (its system is singular) -- finiteness only.
131template <class XT>
132double comparePass(const HostOp& H, const std::vector<XT>& b, const std::vector<XT>& x0,
133 const std::vector<XT>& x1, bool periodic, bool fp32, int color, bool& otherSame,
134 bool& finite, int& nCols) {
135 const C3 e = H.e, n = H.n;
136 const int g = 1, m = n.z;
137 const long t = H.sz;
138 double worst = 0.0;
139 otherSame = true;
140 finite = true;
141 nCols = 0;
142 for (std::size_t i = 0; i < x1.size(); ++i)
143 finite = finite && std::isfinite((double)x1[i]);
144 for (int ly = g; ly < e.y - g; ++ly)
145 for (int lx = g; lx < e.x - g; ++lx) {
146 if (((lx + ly) & 1) != color) { // og = {0, 0, 0} single rank; the column colour drops z
147 for (int lz = g; lz < e.z - g; ++lz) {
148 const long i = H.idx(lx, ly, lz);
149 otherSame = otherSame && std::memcmp(&x0[i], &x1[i], sizeof(XT)) == 0;
150 }
151 continue;
152 }
153 if (lx - g == SX && ly - g == SY)
154 continue; // the sealed column: singular column system
155 std::vector<long double> M((std::size_t)m * m, 0.0L), r(m, 0.0L), d;
156 for (int k = 0; k < m; ++k) {
157 const int lz = g + k;
158 const long i = H.idx(lx, ly, lz);
159 if (H.decoupled(i, fp32)) {
160 M[k * m + k] = 1.0L;
161 continue;
162 }
163 const CcNbrs w = ccWrapNbrs(lx, ly, lz, n, g, i, H.sy, H.sz);
164 long double Ax;
165 if (fp32) { // the flux form: D x_i + sum_f (-w_f) x_j = sum_f w_f (x_i - x_j)
166 Ax = H.AC[i] * x0[i] + H.AF[0][i] * x0[w.xm] + H.AF[0][i + 1] * x0[w.xp] +
167 H.AF[1][i] * x0[w.ym] + H.AF[1][i + H.sy] * x0[w.yp] + H.AF[2][i] * x0[w.zm] +
168 H.AF[2][i + t] * x0[w.zp];
169 } else {
170 Ax = H.AC[i] * x0[i] + H.AF[0][i + 1] * x0[w.xp] + H.AF[0][i] * x0[w.xm] +
171 H.AF[1][i + H.sy] * x0[w.yp] + H.AF[1][i] * x0[w.ym] + H.AF[2][i + t] * x0[w.zp] +
172 H.AF[2][i] * x0[w.zm];
173 }
174 r[k] = (long double)b[i] - Ax;
175 M[k * m + k] = H.AC[i];
176 const long double lo = H.AF[2][i], up = H.AF[2][i + t];
177 if (k > 0)
178 M[k * m + k - 1] += lo;
179 else if (periodic)
180 M[k * m + m - 1] += lo;
181 if (k < m - 1)
182 M[k * m + k + 1] += up;
183 else if (periodic)
184 M[k * m + 0] += up;
185 }
186 if (!denseSolve(M, r, m, d)) {
187 worst = 1e300;
188 continue;
189 }
190 long double dmax = 0.0L, emax = 0.0L;
191 for (int k = 0; k < m; ++k) {
192 const long i = H.idx(lx, ly, g + k);
193 const long double got = (long double)x1[i] - (long double)x0[i];
194 dmax = std::max(dmax, std::fabs(d[k]));
195 emax = std::max(emax, std::fabs(got - d[k]));
196 }
197 ++nCols;
198 if (dmax > 0.0L)
199 worst = std::max(worst, (double)(emax / dmax));
200 }
201 return worst;
202}
203
204void runCase(bool walls, int nz) {
205 const Problem P = makeLineProblem(walls, nz);
206 std::printf("%s (32x32x%d, s = z)\n", P.name.c_str(), nz);
207 CutcellMG mg;
208 mg.init(P.nx, P.ny, P.nz, P.levels);
211 mg.setOpenness(CCConst(toDevice(P.ox, "ox")), CCConst(toDevice(P.oy, "oy")),
212 CCConst(toDevice(P.oz, "oz")), 1.0, 1.0, 1.0);
213 CutcellMG::Level& lv = mg.level(0);
214 const C3 e = lv.ext;
215 const std::size_t n = lv.n;
216 const bool periodic = !walls;
217 check(mg.linePeriodic(2) == periodic, "linePeriodic(z) follows the domain BC");
218 if (lv.rhs.extent(0) != n)
219 lv.rhs = CCField("rhs", n);
220 lv.lnc = CCField("c", n);
221 lv.lny = CCField("y", n);
222 lv.lnz = CCField("z", n);
223
224 HostOp H;
225 H.e = e;
226 H.n = lv.inner;
227 H.sy = e.x;
228 H.sz = (long)e.x * e.y;
229 {
230 const auto ac = toHost(lv.AC);
231 const auto fx = toHost(lv.AFX), fy = toHost(lv.AFY), fz = toHost(lv.AFZ);
232 H.AC.assign(ac.begin(), ac.end());
233 H.AF[0].assign(fx.begin(), fx.end());
234 H.AF[1].assign(fy.begin(), fy.end());
235 H.AF[2].assign(fz.begin(), fz.end());
236 if (walls) {
237 bool zero = true;
238 for (int ly = 1; ly < e.y - 1; ++ly)
239 for (int lx = 1; lx < e.x - 1; ++lx)
240 zero = zero && fz[H.idx(lx, ly, 1)] == 0 && fz[H.idx(lx, ly, e.z - 1)] == 0;
241 check(zero, "FP64 wall faces: AF_z = 0 on both wall planes");
242 }
243 }
244
245 std::mt19937 rng(7u);
246 std::uniform_real_distribution<double> U(-1.0, 1.0);
247 // ---- FP64
248 for (int color = 0; color < 2; ++color) {
249 std::vector<double> hx(n, 0.0), hb(n, 0.0);
250 for (std::size_t i = 0; i < n; ++i) {
251 hx[i] = U(rng);
252 hb[i] = U(rng);
253 }
254 Kokkos::deep_copy(lv.x, Kokkos::View<double*, Kokkos::HostSpace>(hx.data(), n));
255 Kokkos::deep_copy(lv.rhs, Kokkos::View<double*, Kokkos::HostSpace>(hb.data(), n));
256 mg.lineSmoothColor(lv, 2, color);
257 Kokkos::fence();
258 const auto x1 = toHost(lv.x);
259 // the ghosts were refilled by the pass's own fill; compare from the filled pre-pass x
260 std::vector<double> x0 = hx;
261 for (int lz = 0; lz < e.z; ++lz)
262 for (int ly = 0; ly < e.y; ++ly)
263 for (int lx = 0; lx < e.x; ++lx) {
264 const bool inner =
265 lx >= 1 && lx < e.x - 1 && ly >= 1 && ly < e.y - 1 && lz >= 1 && lz < e.z - 1;
266 if (!inner)
267 x0[H.idx(lx, ly, lz)] = x1[H.idx(lx, ly, lz)];
268 }
269 bool same = false, finite = false;
270 int cols = 0;
271 const double err = comparePass(H, hb, x0, x1, periodic, false, color, same, finite, cols);
272 check(err <= 1e-12, "FP64 colour " + std::to_string(color) + ": " + std::to_string(cols) +
273 " columns = dense solve, max rel err " + num(err) + " <= 1e-12");
274 check(same, "FP64 colour " + std::to_string(color) + ": the other colour bitwise unchanged");
275 check(finite, "FP64 colour " + std::to_string(color) + ": all finite (sealed column included)");
276 }
277
278 // ---- FP32: w_f = fl32(t_f) = fl32(-AF_f) (AF = -t_f in FP64)
279 {
280 std::vector<VReal> W[3];
281 for (int a = 0; a < 3; ++a) {
282 W[a].resize(n);
283 for (std::size_t i = 0; i < n; ++i)
284 W[a][i] = static_cast<VReal>(-(double)H.AF[a][i]);
285 }
286 auto toDevF = [&](const std::vector<VReal>& v) {
287 VField f("w", v.size());
288 auto h = Kokkos::create_mirror_view(f);
289 for (std::size_t i = 0; i < v.size(); ++i)
290 h(i) = v[i];
291 Kokkos::deep_copy(f, h);
292 return f;
293 };
294 lv.WX = toDevF(W[0]);
295 lv.WY = toDevF(W[1]);
296 lv.WZ = toDevF(W[2]);
297 lv.xf = VField("xf", n);
298 lv.rhsf = VField("rhsf", n);
299 lv.lncf = VField("cf", n);
300 lv.lnyf = VField("yf", n);
301 lv.lnzf = VField("zf", n);
302 if (walls) {
303 bool zero = true;
304 for (int ly = 1; ly < e.y - 1; ++ly)
305 for (int lx = 1; lx < e.x - 1; ++lx)
306 zero = zero && W[2][H.idx(lx, ly, 1)] == 0.0f && W[2][H.idx(lx, ly, e.z - 1)] == 0.0f;
307 check(zero, "FP32 wall weights: W_z = 0 on both wall planes");
308 }
309 HostOp H32 = H;
310 for (int lz = 0; lz < e.z; ++lz)
311 for (int ly = 0; ly < e.y; ++ly)
312 for (int lx = 0; lx < e.x; ++lx) {
313 const long i = H.idx(lx, ly, lz);
314 const bool inner =
315 lx >= 1 && lx < e.x - 1 && ly >= 1 && ly < e.y - 1 && lz >= 1 && lz < e.z - 1;
316 // fp32Diag's order, in FP32
317 const VReal D =
318 inner ? ((((W[0][i + 1] + W[0][i]) + W[1][i + H.sy]) + W[1][i]) + W[2][i + H.sz]) +
319 W[2][i]
320 : 0.0f;
321 H32.AC[i] = (long double)D;
322 for (int a = 0; a < 3; ++a)
323 H32.AF[a][i] = -(long double)W[a][i];
324 }
325 for (int color = 0; color < 2; ++color) {
326 std::vector<VReal> hx(n, 0.0f), hb(n, 0.0f);
327 for (std::size_t i = 0; i < n; ++i) {
328 hx[i] = static_cast<VReal>(U(rng));
329 hb[i] = static_cast<VReal>(U(rng));
330 }
331 Kokkos::deep_copy(lv.xf, Kokkos::View<VReal*, Kokkos::HostSpace>(hx.data(), n));
332 Kokkos::deep_copy(lv.rhsf, Kokkos::View<VReal*, Kokkos::HostSpace>(hb.data(), n));
333 mg.lineSmoothColorFp32(lv, 2, color);
334 Kokkos::fence();
335 const auto x1 = toHost(lv.xf);
336 bool same = false, finite = false;
337 int cols = 0;
338 const double err = comparePass(H32, hb, hx, x1, periodic, true, color, same, finite, cols);
339 check(err <= 1e-4, "FP32 colour " + std::to_string(color) + ": " + std::to_string(cols) +
340 " columns = dense solve, max rel err " + num(err) + " <= 1e-4");
341 check(same, "FP32 colour " + std::to_string(color) + ": the other colour bitwise unchanged");
342 check(finite,
343 "FP32 colour " + std::to_string(color) + ": all finite (sealed column included)");
344 }
345 for (const VReal c : {1.0f, -3.7f, 1e5f}) {
346 std::vector<VReal> hx(n, c), hb(n, 0.0f);
347 Kokkos::deep_copy(lv.xf, Kokkos::View<VReal*, Kokkos::HostSpace>(hx.data(), n));
348 Kokkos::deep_copy(lv.rhsf, Kokkos::View<VReal*, Kokkos::HostSpace>(hb.data(), n));
349 mg.lineSmoothColorFp32(lv, 2, 0);
350 mg.lineSmoothColorFp32(lv, 2, 1);
351 Kokkos::fence();
352 const auto x1 = toHost(lv.xf);
353 const bool same = std::memcmp(x1.data(), hx.data(), n * sizeof(VReal)) == 0;
354 check(same, "FP32 x = " + num(c) + " * 1, b = 0: both colours bitwise unchanged");
355 }
356 }
357}
358
359} // namespace
360
361int main(int argc, char** argv) {
362 Kokkos::initialize(argc, argv);
363 {
364 for (int nz : {25, 2, 3}) { // the A2.5 level, and the shortest columns (m = 2, 3)
365 runCase(false, nz);
366 runCase(true, nz);
367 }
368 }
369 Kokkos::finalize();
370 std::printf("mg_line_relax: %s (%d failure%s)\n", fails ? "FAIL" : "PASS", fails,
371 fails == 1 ? "" : "s");
372 return fails ? 1 : 0;
373}
void lineSmoothColorFp32(Level &lv, int s, int color)
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void lineSmoothColor(Level &lv, int s, int color)
void setBoundaryConditions(const int bc[6])
bool linePeriodic(int s) const
void init(int nx, int ny, int nz, int nLevels)
void setVcyclePrecision(int mode)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
The FP32 V-cycle test problems (column, cyl, slab, pack) and their right-hand side,...
y
Definition cmp.py:24
M
Definition conj.py:180
dict fx
Definition mgd_case.py:457
void finite(const char *who, const std::string &name, double v, const char *dim)
void zero(const ScalarVec &y)
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< VReal *, CCMem > VField
CcNbrs ccWrapNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz)
Kokkos::View< double *, CCMem > CCField
CCField toDevice(const std::vector< double > &v, const char *name)
std::vector< typename V::non_const_value_type > toHost(const V &f)
int m
Definition post.py:35
dict cols
Definition post.py:24
t
Definition post.py:21
worst
Definition stab5.py:6
P
Definition stab5.py:16
static constexpr int NY
static constexpr int NX
static constexpr int NZ
static int runCase(const Config &c, const peclet::core::decomp::BlockDecomposer< 3 > &dec, int rank, int size)
static void check(bool ok, const std::string &what)
static constexpr double U
static int fails
static bool same(double a, double b)
static constexpr int SX