flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_bottom_direct.cpp
Go to the documentation of this file.
1
45#include <algorithm>
46#include <cmath>
47#include <cstdio>
48#include <cstring>
49#include <Kokkos_Core.hpp>
50#include <random>
51#include <type_traits>
52#include <vector>
53
54#include "mac_cutcell_mg.hpp"
55
56using namespace peclet::flow;
57
58namespace {
59
60constexpr bool kHost = std::is_same_v<CCMem, Kokkos::HostSpace>;
61
62std::vector<double> toHost(CCField f) {
63 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
64 return std::vector<double>(h.data(), h.data() + h.extent(0));
65}
66std::vector<double> toHostOp(FPV f) {
67 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
68 std::vector<double> v(h.extent(0));
69 for (std::size_t i = 0; i < v.size(); ++i)
70 v[i] = (double)h(i);
71 return v;
72}
73CCField toDevice(const std::vector<double>& v, const char* name) {
74 CCField f(name, v.size());
75 auto h = Kokkos::create_mirror_view(f);
76 for (std::size_t i = 0; i < v.size(); ++i)
77 h(i) = v[i];
78 Kokkos::deep_copy(f, h);
79 return f;
80}
81template <class V>
82bool sameBits(const V& a, const V& b) {
83 auto ha = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), a);
84 auto hb = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), b);
85 return ha.span() == hb.span() &&
86 std::memcmp(ha.data(), hb.data(), ha.span() * sizeof(typename V::value_type)) == 0;
87}
88
89// The bottom level on the host: its face form, labels (union-find, an independent check of the
90// device ones), the re-summed diagonal and the scaling of §13.4.1, and the planes.
91struct HostBottom {
92 C3 e{0, 0, 0}, n{0, 0, 0};
93 int bc[6];
94 std::vector<double> AC, AX, AY, AZ;
95 std::vector<int> comp; // ext-indexed, -1 = solid
96 int nc = 0;
97 std::vector<double> d, s; // ext-indexed
98 long idx(int x, int y, int z) const { // 0-based inner coordinates
99 return (long)(x + 1) + (long)(y + 1) * e.x + (long)(z + 1) * e.x * e.y;
100 }
101 // face f6 of inner cell c: false if it crosses a non-periodic domain face
102 bool face(const int c[3], int f6, int nb[3], double& af) const {
103 const int a = f6 >> 1, side = f6 & 1, nn[3] = {n.x, n.y, n.z};
104 nb[0] = c[0];
105 nb[1] = c[1];
106 nb[2] = c[2];
107 int v = c[a] + (side ? 1 : -1);
108 if (v < 0 || v >= nn[a]) {
109 if (bc[f6] != 0)
110 return false;
111 v = (v + nn[a]) % nn[a];
112 }
113 nb[a] = v;
114 const long i = idx(c[0], c[1], c[2]);
115 const long st = (a == 0) ? 1 : (a == 1 ? e.x : (long)e.x * e.y);
116 const std::vector<double>& F = (a == 0) ? AX : (a == 1 ? AY : AZ);
117 af = F[side ? i + st : i];
118 return true;
119 }
120 template <class Fn>
121 void forCells(Fn fn) const {
122 for (int z = 0; z < n.z; ++z)
123 for (int y = 0; y < n.y; ++y)
124 for (int x = 0; x < n.x; ++x) {
125 const int c[3] = {x, y, z};
126 fn(c, idx(x, y, z));
127 }
128 }
129 void build() {
130 std::vector<long> parent(AC.size());
131 for (std::size_t i = 0; i < parent.size(); ++i)
132 parent[i] = (long)i;
133 auto find = [&](long a) {
134 while (parent[a] != a)
135 a = parent[a] = parent[parent[a]];
136 return a;
137 };
138 forCells([&](const int c[3], long i) {
139 if (!(AC[i] > 1e-30))
140 return;
141 for (int f6 = 0; f6 < 6; ++f6) {
142 int nb[3];
143 double af;
144 if (face(c, f6, nb, af) && af < 0.0) {
145 const long j = idx(nb[0], nb[1], nb[2]);
146 if (AC[j] > 1e-30)
147 parent[find(i)] = find(j);
148 }
149 }
150 });
151 comp.assign(AC.size(), -1);
152 std::vector<int> id(AC.size(), -1);
153 nc = 0;
154 forCells([&](const int*, long i) {
155 if (!(AC[i] > 1e-30))
156 return;
157 const long r = find(i);
158 if (id[r] < 0)
159 id[r] = nc++;
160 comp[i] = id[r];
161 });
162 d.assign(AC.size(), 0.0);
163 s.assign(AC.size(), 0.0);
164 forCells([&](const int c[3], long i) {
165 double sum = 0.0;
166 for (int f6 = 0; f6 < 6; ++f6) {
167 int nb[3];
168 double af;
169 if (face(c, f6, nb, af))
170 sum += af;
171 }
172 d[i] = -sum;
173 s[i] = (comp[i] >= 0 && d[i] > 0.0) ? 1.0 / std::sqrt(d[i]) : 0.0;
174 });
175 }
176 // (A x)_i with the re-summed diagonal (the matrix M factors), fluid rows
177 double applyResummed(const std::vector<double>& x, const int c[3], long i) const {
178 double v = d[i] * x[i];
179 for (int f6 = 0; f6 < 6; ++f6) {
180 int nb[3];
181 double af;
182 if (face(c, f6, nb, af))
183 v += af * x[idx(nb[0], nb[1], nb[2])];
184 }
185 return v;
186 }
187 // (A x)_i with the STORED operator, read the way the device kernels read it (wrapped indices)
188 double applyStored(const std::vector<double>& x, const int c[3], long i) const {
189 const int nn[3] = {n.x, n.y, n.z};
190 auto at = [&](int dx, int dy, int dz) {
191 const int q[3] = {(c[0] + dx + nn[0]) % nn[0], (c[1] + dy + nn[1]) % nn[1],
192 (c[2] + dz + nn[2]) % nn[2]};
193 return x[idx(q[0], q[1], q[2])];
194 };
195 const long sy = e.x, sz = (long)e.x * e.y;
196 return AC[i] * x[i] + AX[i + 1] * at(1, 0, 0) + AX[i] * at(-1, 0, 0) +
197 AY[i + sy] * at(0, 1, 0) + AY[i] * at(0, -1, 0) + AZ[i + sz] * at(0, 0, 1) +
198 AZ[i] * at(0, 0, -1);
199 }
200 // per-component means of x (long double) -> max |mean| ; also max |x| on fluid, max |x| solid
201 void means(const std::vector<double>& x, double& meanMax, double& xmax, double& xsolid) const {
202 std::vector<long double> sum(nc, 0.0L);
203 std::vector<long> cnt(nc, 0);
204 xmax = xsolid = meanMax = 0.0;
205 forCells([&](const int*, long i) {
206 if (comp[i] >= 0) {
207 sum[comp[i]] += (long double)x[i];
208 ++cnt[comp[i]];
209 xmax = std::max(xmax, std::fabs(x[i]));
210 } else {
211 xsolid = std::max(xsolid, std::fabs(x[i]));
212 }
213 });
214 for (int c = 0; c < nc; ++c)
215 meanMax = std::max(meanMax, (double)std::fabs(sum[c] / (long double)cnt[c]));
216 }
217};
218
219// A compatible rhs: random on the fluid cells, each component's mean removed (long double), 0 on
220// solids and ghosts.
221std::vector<double> compatibleRhs(const HostBottom& H, unsigned seed) {
222 std::vector<double> v(H.AC.size(), 0.0);
223 std::mt19937 rng(seed);
224 std::uniform_real_distribution<double> U(-1.0, 1.0);
225 std::vector<long double> sum(H.nc, 0.0L);
226 std::vector<long> cnt(H.nc, 0);
227 H.forCells([&](const int*, long i) {
228 const double r = U(rng);
229 if (H.comp[i] >= 0) {
230 v[i] = r;
231 sum[H.comp[i]] += r;
232 ++cnt[H.comp[i]];
233 }
234 });
235 H.forCells([&](const int*, long i) {
236 if (H.comp[i] >= 0)
237 v[i] = (double)((long double)v[i] - sum[H.comp[i]] / (long double)cnt[H.comp[i]]);
238 });
239 return v;
240}
241
242double residualResummed(const HostBottom& H, const std::vector<double>& x,
243 const std::vector<double>& r) {
244 double rn = 0.0, r0 = 0.0;
245 H.forCells([&](const int c[3], long i) {
246 if (H.comp[i] < 0)
247 return;
248 r0 = std::max(r0, std::fabs(r[i]));
249 rn = std::max(rn, std::fabs(H.applyResummed(x, c, i) - r[i]));
250 });
251 return rn / r0;
252}
253
254// x_ref = A'^{-1} r on the host in FP64: the scaled augmented matrix A~' of §13.4.1-2 assembled
255// densely from the host copies (planes from the solver; labels, k_c, m_c host-computed), a dense
256// Cholesky, x = S y, then each component's mean removed (long double).
257std::vector<double> denseReference(const HostBottom& H, const BottomPlanes& pl,
258 const std::vector<double>& r) {
259 const int nn = pl.P * pl.b;
260 auto plane = [&](const int c[3]) { return c[pl.s]; };
261 auto fidx = [&](const int c[3]) { return c[pl.s] * pl.b + c[pl.a0] + pl.n[pl.a0] * c[pl.a1]; };
262 std::vector<int> kc(H.nc, -1), mc(H.nc, 0);
263 H.forCells([&](const int c[3], long i) {
264 if (H.comp[i] >= 0)
265 kc[H.comp[i]] = std::max(kc[H.comp[i]], plane(c));
266 });
267 H.forCells([&](const int c[3], long i) {
268 if (H.comp[i] >= 0 && plane(c) == kc[H.comp[i]])
269 ++mc[H.comp[i]];
270 });
271 std::vector<double> A((std::size_t)nn * nn, 0.0), rhs(nn, 0.0);
272 std::vector<long> ext(nn);
273 H.forCells([&](const int c[3], long i) {
274 const int fi = fidx(c);
275 ext[fi] = i;
276 rhs[fi] = H.s[i] * r[i];
277 A[(std::size_t)fi * nn + fi] += 1.0;
278 for (int f6 = 0; f6 < 6; ++f6) {
279 int nb[3];
280 double af;
281 if (H.face(c, f6, nb, af)) {
282 const long j = H.idx(nb[0], nb[1], nb[2]);
283 A[(std::size_t)fi * nn + fidx(nb)] += H.s[i] * af * H.s[j];
284 }
285 }
286 });
287 for (int fi = 0; fi < nn; ++fi)
288 for (int fj = 0; fj < nn; ++fj) {
289 const long i = ext[fi], j = ext[fj];
290 const int ci = H.comp[i];
291 if (ci >= 0 && H.comp[j] == ci && fi / pl.b == kc[ci] && fj / pl.b == kc[ci])
292 A[(std::size_t)fi * nn + fj] += 1.0 / mc[ci];
293 }
294 for (int j = 0; j < nn; ++j) { // in-place Cholesky (lower)
295 double p = A[(std::size_t)j * nn + j];
296 for (int m = 0; m < j; ++m)
297 p -= A[(std::size_t)j * nn + m] * A[(std::size_t)j * nn + m];
298 const double ljj = std::sqrt(p);
299 A[(std::size_t)j * nn + j] = ljj;
300 for (int i = j + 1; i < nn; ++i) {
301 double v = A[(std::size_t)i * nn + j];
302 for (int m = 0; m < j; ++m)
303 v -= A[(std::size_t)i * nn + m] * A[(std::size_t)j * nn + m];
304 A[(std::size_t)i * nn + j] = v / ljj;
305 }
306 }
307 for (int i = 0; i < nn; ++i) { // L y = rhs
308 double v = rhs[i];
309 for (int m = 0; m < i; ++m)
310 v -= A[(std::size_t)i * nn + m] * rhs[m];
311 rhs[i] = v / A[(std::size_t)i * nn + i];
312 }
313 for (int i = nn - 1; i >= 0; --i) { // L^T y = .
314 double v = rhs[i];
315 for (int m = i + 1; m < nn; ++m)
316 v -= A[(std::size_t)m * nn + i] * rhs[m];
317 rhs[i] = v / A[(std::size_t)i * nn + i];
318 }
319 std::vector<double> x(H.AC.size(), 0.0);
320 for (int fi = 0; fi < nn; ++fi)
321 x[ext[fi]] = (H.comp[ext[fi]] >= 0) ? H.s[ext[fi]] * rhs[fi] : 0.0;
322 std::vector<long double> sum(H.nc, 0.0L);
323 std::vector<long> cnt(H.nc, 0);
324 H.forCells([&](const int*, long i) {
325 if (H.comp[i] >= 0) {
326 sum[H.comp[i]] += x[i];
327 ++cnt[H.comp[i]];
328 }
329 });
330 H.forCells([&](const int*, long i) {
331 if (H.comp[i] >= 0)
332 x[i] = (double)((long double)x[i] - sum[H.comp[i]] / (long double)cnt[H.comp[i]]);
333 });
334 return x;
335}
336
337struct Case {
338 const char* name;
339 int bc[6];
340 int nx, ny, nz;
341 bool solids, pocket;
342};
343
344int runCase(const Case& cs) {
345 const int nx = cs.nx, ny = cs.ny, nz = cs.nz;
346 CutcellMG mg;
348 mg.init(nx, ny, nz, 2);
350 const C3 e{nx + 2, ny + 2, nz + 2};
351 const std::size_t n = (std::size_t)e.x * e.y * e.z;
352 std::vector<double> hx(n), hy(n), hz(n);
353 const double lr = std::log(50.0), k = 2.0 * M_PI;
354 for (int z = 0; z < e.z; ++z)
355 for (int y = 0; y < e.y; ++y)
356 for (int x = 0; x < e.x; ++x) {
357 const std::size_t i = x + (std::size_t)y * e.x + (std::size_t)z * e.x * e.y;
358 const double s = 0.5 + 0.5 * std::sin(k * x / nx) * std::cos(k * (y + 0.5 * z) / ny);
359 const double t = 0.5 + 0.5 * std::cos(k * (x + z) / nz);
360 hx[i] = std::exp(lr * s) / 50.0; // in [0.02, 1]: ratio 50
361 hy[i] = std::exp(lr * t) / 50.0;
362 hz[i] = std::exp(lr * (1.0 - s)) / 50.0;
363 const int ix = x - 1, iy = y - 1, iz = z - 1; // the low face of fine cell (ix, iy, iz)
364 if (cs.solids) { // the B1b sheet: coarse x = 3 solid, coarse faces x = 0 and 8 closed
365 if (ix == 0 || ix == 16 || ix == 6 || ix == 8)
366 hx[i] = 0.0;
367 if (ix == 6 || ix == 7)
368 hy[i] = hz[i] = 0.0;
369 }
370 if (cs.pocket) { // fine box [20,24) x [10,12) x [6,8) = coarse cells (10..11, 5, 3)
371 const bool inX = ix >= 20 && ix < 24, inY = iy >= 10 && iy < 12, inZ = iz >= 6 && iz < 8;
372 if ((ix == 20 || ix == 24) && inY && inZ)
373 hx[i] = 0.0;
374 if ((iy == 10 || iy == 12) && inX && inZ)
375 hy[i] = 0.0;
376 if ((iz == 6 || iz == 8) && inX && inY)
377 hz[i] = 0.0;
378 }
379 }
380 CCField ox = toDevice(hx, "ox"), oy = toDevice(hy, "oy"), oz = toDevice(hz, "oz");
381 mg.setOpenness(CCConst(ox), CCConst(oy), CCConst(oz), 1.0, 1.0, 1.0);
383 CutcellMG::Level& bt = mg.level(mg.nLevels() - 1);
384 HostBottom H;
385 H.e = bt.ext;
386 H.n = bt.inner;
387 for (int f = 0; f < 6; ++f)
388 H.bc[f] = cs.bc[f];
389 H.AC = toHostOp(bt.AC);
390 H.AX = toHostOp(bt.AFX);
391 H.AY = toHostOp(bt.AFY);
392 H.AZ = toHostOp(bt.AFZ);
393 H.build();
394 const BottomPlanes pl = mg.directPlanes();
395 const char* why = mg.directBottomIneligible();
396 printf(
397 "[%s] bottom %dx%dx%d: slow axis %d, P = %d, b = %d, border %d; %d fluid component(s) "
398 "(host %d); %s\n",
399 cs.name, bt.inner.x, bt.inner.y, bt.inner.z, pl.s, pl.P, pl.b, pl.border,
400 mg.bottomComponents(), H.nc, why ? why : "eligible");
401 int fails = 0;
402 if (!pl.valid() || mg.bottomComponents() != H.nc) {
403 printf("[%s] FAIL: no plane ordering, or the device labels disagree\n", cs.name);
404 return 1;
405 }
406 const std::vector<double> r = compatibleRhs(H, 7);
407 CCField rd = toDevice(r, "r");
408
409 { // U1: FP64 exactness
410 auto D = mg.directFactorForTest<double>(0);
411 CCField z("z", bt.n);
412 mg.directApplyForTest(D, z, rd, 0);
413 const auto x = toHost(z);
414 const double res = residualResummed(H, x, r);
415 double mm, xm, xs;
416 H.means(x, mm, xm, xs);
417 const bool ok = res <= 1e-12 && mm <= 1e-15 * xm && xs == 0.0;
418 printf(
419 "[%s] U1 FP64 M: residual %.3e, max|component mean| %.2e max|x|, max|x| solid %.1e -> "
420 "%s\n",
421 cs.name, res, mm / xm, xs, ok ? "ok" : "FAIL");
422 fails += !ok;
423 }
424 std::vector<double> zf;
425 { // U2: FP32 accuracy
426 auto D = mg.directFactorForTest<float>(0);
427 CCField z("z", bt.n);
428 mg.directApplyForTest(D, z, rd, 0);
429 zf = toHost(z);
430 const double res = residualResummed(H, zf, r);
431 const bool ok = res <= 1e-3;
432 printf("[%s] U2 FP32 M: residual %.3e -> %s\n", cs.name, res, ok ? "ok" : "FAIL");
433 fails += !ok;
434 }
435 { // U3: team-size independence (factor storage; M before the mean removal)
436 std::vector<int> Ts =
437 kHost ? std::vector<int>{1, 2, 4, 8} : std::vector<int>{32, 64, 128, 256, 0};
439 CCField zref("zref", bt.n);
440 int Tref = -1;
441 bool ok = true;
442 std::vector<int> used;
443 for (int T : Ts) {
444 int Tf = 0;
445 auto D = mg.directFactorForTest<float>(T, kBottomPivotTol, &Tf);
446 CCField z("z", bt.n);
447 const int Ta = mg.directApplyForTest(D, z, rd, T, /*noMean=*/true);
448 if ((T != 0 && (Tf != T || Ta != T))) {
449 printf("[%s] U3 team size %d not available (factor %d, solve %d): skipped\n", cs.name, T,
450 Tf, Ta);
451 continue;
452 }
453 used.push_back(Ta);
454 if (Tref < 0) {
455 ref = D;
456 Kokkos::deep_copy(zref, z);
457 Tref = Ta;
458 continue;
459 }
460 const bool same = sameBits(D.Q, ref.Q) && sameBits(D.e, ref.e) && sameBits(D.s, ref.s) &&
461 (!pl.border || sameBits(D.Y, ref.Y)) && sameBits(z, zref);
462 if (!same)
463 printf("[%s] U3 team size %d differs from %d\n", cs.name, Ta, Tref);
464 ok = ok && same;
465 }
466 ok = ok && used.size() >= 2;
467 printf("[%s] U3 team sizes", cs.name);
468 for (int T : used)
469 printf(" %d", T);
470 printf(": factor + M %s -> %s\n", ok ? "bitwise identical" : "DIFFER (or < 2 sizes)",
471 ok ? "ok" : "FAIL");
472 fails += !ok;
473 }
474 if (kHost) { // U8: the host schedule against the team algorithm, its bitwise oracle
475 bool ok = true;
476 std::vector<int> used;
477 auto agree = [&](const auto& A, const auto& B) {
478 return sameBits(A.Q, B.Q) && sameBits(A.e, B.e) && sameBits(A.s, B.s) &&
479 (!pl.border || sameBits(A.Y, B.Y)) && sameBits(A.stat, B.stat);
480 };
481 for (int T : {1, 2, 4}) {
482 for (double tau : {kBottomPivotTol, 1e30}) { // 1e30: every first-attempt pivot fails
483 int Th = 0, Tt = 0, Th64 = 0, Tt64 = 0;
484 const auto Dh = mg.directFactorForTest<float>(T, tau, &Th);
485 const auto Dt = mg.directFactorForTest<float>(T, tau, &Tt, /*teamAlgorithm=*/true);
486 const auto Dh64 = mg.directFactorForTest<double>(T, tau, &Th64);
487 const auto Dt64 = mg.directFactorForTest<double>(T, tau, &Tt64, /*teamAlgorithm=*/true);
488 if (Th != T || Tt != T || Th64 != T || Tt64 != T)
489 continue;
490 if (tau == kBottomPivotTol)
491 used.push_back(T);
492 const bool same = agree(Dh, Dt) && agree(Dh64, Dt64);
493 if (!same)
494 printf("[%s] U8 team size %d, first pivot floor %.0e: host schedule differs\n", cs.name,
495 T, tau);
496 ok = ok && same;
497 }
498 }
499 ok = ok && used.size() >= 2;
500 printf(
501 "[%s] U8 host schedule vs team algorithm (FP32 + FP64, with and without a restart), "
502 "team sizes",
503 cs.name);
504 for (int T : used)
505 printf(" %d", T);
506 printf(": %s -> %s\n", ok ? "bitwise identical" : "DIFFER (or < 2 sizes)", ok ? "ok" : "FAIL");
507 fails += !ok;
508 }
509 if (kHost) { // U9: the host lane schedule of M against the team schedule, its bitwise oracle
510 std::vector<double> hr(bt.n, 0.0);
511 std::mt19937 rng(9);
512 std::uniform_real_distribution<double> U(-1.0, 1.0);
513 H.forCells([&](const int*, long i) { hr[i] = U(rng); });
514 CCField rr = toDevice(hr, "r9");
515 bool ok = true;
516 int compared = 0;
517 std::vector<int> used;
518 auto check = [&](const auto& D, double tau, bool noMean) {
519 CCField zt("zt", bt.n);
520 const int To = mg.directApplyForTest(D, zt, rr, 4, noMean, /*teamAlgorithm=*/true);
521 for (int T : {1, 2, 4}) {
522 CCField zl("zl", bt.n);
523 if (mg.directApplyForTest(D, zl, rr, T, noMean) != T)
524 continue;
525 if (tau == kBottomPivotTol && noMean &&
526 std::find(used.begin(), used.end(), T) == used.end())
527 used.push_back(T);
528 ++compared;
529 if (!sameBits(zl, zt)) {
530 printf("[%s] U9 lane M (team %d) differs from team M (team %d), floor %.0e, noMean %d\n",
531 cs.name, T, To, tau, noMean ? 1 : 0);
532 ok = false;
533 }
534 }
535 };
536 for (double tau : {kBottomPivotTol, 1e30}) { // 1e30: the shifted (restart) factor
537 const auto D = mg.directFactorForTest<float>(0, tau);
538 const auto D64 = mg.directFactorForTest<double>(0, tau);
539 auto st = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), D.stat);
540 if (st(0) != 1 || st(1) != (tau == kBottomPivotTol ? 0 : 1)) {
541 printf("[%s] U9 factor floor %.0e: ok %d, restarts %d (unexpected)\n", cs.name, tau, st(0),
542 st(1));
543 ok = false;
544 }
545 for (bool noMean : {true, false}) {
546 check(D, tau, noMean);
547 check(D64, tau, noMean);
548 }
549 }
550 ok = ok && used.size() >= 2;
551 printf(
552 "[%s] U9 host lane M vs team M (FP32 + FP64, both shift paths, +/- mean removal), %d "
553 "comparisons, team sizes",
554 cs.name, compared);
555 for (int T : used)
556 printf(" %d", T);
557 printf(": %s -> %s\n", ok ? "bitwise identical" : "DIFFER (or < 2 sizes)", ok ? "ok" : "FAIL");
558 fails += !ok;
559 }
560 if (kHost) { // U3, host: the whole FCG solve is bitwise across T (single-lane reductions)
561 std::vector<double> hb(bt.n, 0.0);
562 std::mt19937 rng(5);
563 std::uniform_real_distribution<double> U(-1.0, 1.0);
564 H.forCells([&](const int*, long i) { hb[i] = U(rng); });
565 CCField b = toDevice(hb, "b");
566 CCField xref("xref", bt.n);
567 int itRef = -1, Tref = -1;
568 bool ok = true;
569 std::vector<int> used;
570 for (int T : {1, 2, 4, 8}) {
571 Kokkos::deep_copy(bt.rhs, b);
572 int Tu = 0;
573 const int it = mg.directSolveTeamForTest(T, &Tu);
574 if (Tu != T) {
575 printf("[%s] U3 FCG team size %d not available (ran %d): skipped\n", cs.name, T, Tu);
576 continue;
577 }
578 used.push_back(T);
579 if (Tref < 0) {
580 Kokkos::deep_copy(xref, bt.x);
581 itRef = it;
582 Tref = T;
583 continue;
584 }
585 const bool same = it == itRef && sameBits(bt.x, xref);
586 if (!same)
587 printf("[%s] U3 FCG team size %d differs from %d (iterations %d vs %d)\n", cs.name, T, Tref,
588 it, itRef);
589 ok = ok && same;
590 }
591 ok = ok && used.size() >= 2 && !mg.lastSolveFailed();
592 printf("[%s] U3 FCG team sizes", cs.name);
593 for (int T : used)
594 printf(" %d", T);
595 printf(": the whole solve (%d iterations) %s -> %s\n", itRef,
596 ok ? "bitwise identical" : "DIFFERS (or < 2 sizes)", ok ? "ok" : "FAIL");
597 fails += !ok;
598 }
599 auto fcg = [&](const char* tag, int itMax, int restartsWant) {
600 std::vector<double> hb(bt.n, 0.0); // a random rhs WITH component means (the kernel removes)
601 std::mt19937 rng(11);
602 std::uniform_real_distribution<double> U(-1.0, 1.0);
603 H.forCells([&](const int*, long i) { hb[i] = U(rng); });
604 {
605 CCField b = toDevice(hb, "b");
606 Kokkos::deep_copy(bt.rhs, b);
607 }
608 const int it = mg.directSolveForTest();
609 const int restarts = mg.directRestarts();
610 const bool flag = mg.lastSolveFailed();
611 const auto x = toHost(bt.x);
612 std::vector<long double> bsum(H.nc, 0.0L);
613 std::vector<long> bcnt(H.nc, 0);
614 H.forCells([&](const int*, long i) {
615 if (H.comp[i] >= 0) {
616 bsum[H.comp[i]] += hb[i];
617 ++bcnt[H.comp[i]];
618 }
619 });
620 double r0 = 0.0, rn = 0.0;
621 H.forCells([&](const int c[3], long i) {
622 if (H.comp[i] < 0)
623 return;
624 const double bp =
625 (double)((long double)hb[i] - bsum[H.comp[i]] / (long double)bcnt[H.comp[i]]);
626 r0 = std::max(r0, std::fabs(bp));
627 rn = std::max(rn, std::fabs(bp - H.applyStored(x, c, i)));
628 });
629 double mm, xm, xs;
630 H.means(x, mm, xm, xs);
631 const bool ok = it >= 1 && it <= itMax && !flag && restarts == restartsWant &&
632 rn <= 10.0 * kBottomTau * r0 && xs == 0.0 && mm <= 4e-16 * xm;
633 printf(
634 "[%s] %s FCG: %d iteration(s), restarts %d, flag %d, true residual %.3e r0, max|x| "
635 "solid %.1e, max|component mean| %.2e max|x| -> %s\n",
636 tag, cs.name, it, restarts, flag ? 1 : 0, rn / r0, xs, mm / xm, ok ? "ok" : "FAIL");
637 return ok ? 0 : 1;
638 };
639 fails += fcg("U4", 3, 0); // U4
640 mg.directPivotTolForTest(1e30); // U5: the first attempt fails at every pivot
642 fails += fcg("U5", kBottomCap - 1, 1);
645 { // U6: a non-finite face coefficient
646 auto hA = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), bt.AFX);
647 const long i0 = 1 + (long)1 * bt.ext.x + (long)1 * bt.ext.x * bt.ext.y;
648 long i = i0;
649 while (!(hA(i) < 0) && i < (long)bt.n - 1) // the first open x face
650 ++i;
651 const auto keep = hA(i);
652 hA(i) = std::numeric_limits<MReal>::quiet_NaN();
653 Kokkos::deep_copy(bt.AFX, hA);
655 Kokkos::deep_copy(bt.rhs, rd);
657 const bool flag = mg.lastSolveFailed();
658 const auto x = toHost(bt.x);
659 double xmax = 0.0;
660 for (double v : x)
661 xmax = std::max(xmax, std::fabs(v));
662 const bool ok = flag && xmax == 0.0;
663 printf("[%s] U6 NaN coefficient: flag %d, max|x| %.1e -> %s\n", cs.name, flag ? 1 : 0, xmax,
664 ok ? "ok" : "FAIL");
665 fails += !ok;
666 hA(i) = keep;
667 Kokkos::deep_copy(bt.AFX, hA);
669 }
670 if (bt.inner.x == 16 && bt.inner.y == 12 && bt.inner.z == 8) { // U7
671 const std::vector<double> xr = denseReference(H, pl, r);
672 double dm = 0.0, am = 0.0;
673 H.forCells([&](const int*, long i) {
674 am = std::max(am, std::fabs(xr[i]));
675 dm = std::max(dm, std::fabs(zf[i] - xr[i]));
676 });
677 const bool ok = dm <= 1e-3 * am;
678 printf("[%s] U7 FP32 M vs dense FP64 A'^-1: max diff %.3e max|x_ref| -> %s\n", cs.name, dm / am,
679 ok ? "ok" : "FAIL");
680 fails += !ok;
681 }
682 return fails;
683}
684
685} // namespace
686
687int main(int argc, char** argv) {
688 Kokkos::initialize(argc, argv);
689 int fails = 0;
690 {
691 const Case cases[] = {
692 {"periodic", {0, 0, 0, 0, 0, 0}, 32, 24, 16, false, false},
693 {"periodic-P2", {0, 0, 0, 0, 0, 0}, 4, 4, 4, false, false},
694 {"walls-y", {0, 0, 1, 1, 0, 0}, 32, 24, 16, false, false},
695 {"walls-z", {0, 0, 0, 0, 1, 1}, 32, 24, 16, false, false},
696 {"solids", {0, 0, 0, 0, 0, 0}, 32, 24, 16, true, false},
697 {"solids-walls-y", {0, 0, 1, 1, 0, 0}, 32, 24, 16, true, false},
698 {"pocket", {0, 0, 0, 0, 0, 0}, 32, 24, 16, false, true},
699 };
700 for (const Case& c : cases)
701 fails += runCase(c);
702 }
703 Kokkos::finalize();
704 printf(fails ? "bottom_direct: FAIL (%d)\n" : "bottom_direct: PASS\n", fails);
705 return fails ? 1 : 0;
706}
const BottomPlanes & directPlanes() const
int directSolveTeamForTest(int team, int *Tused=nullptr)
const char * directBottomIneligible() const
void directPivotTolForTest(double t)
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setBoundaryConditions(const int bc[6])
int directApplyForTest(const BottomDirect< FR > &D, CCField z, CCField r, int team, bool noMean=false, bool teamAlgorithm=false)
void setAgglomerationMode(int mode)
void init(int nx, int ny, int nz, int nLevels)
BottomDirect< FR > directFactorForTest(int team, double tauPiv0=kBottomPivotTol, int *Tused=nullptr, bool teamAlgorithm=false)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
mc(p, q)
Definition apriori1d.py:24
mm(p, q)
Definition apriori1d.py:20
y
Definition cmp.py:24
dict res
Definition mgd_case.py:488
constexpr bool kHost
Kokkos::View< const double *, CCMem > CCConst
constexpr int kBottomCap
constexpr double kBottomTau
Kokkos::View< double *, CCMem > CCField
Kokkos::View< MReal *, CCMem > FPV
constexpr double kBottomPivotTol
CCField toDevice(const std::vector< double > &v, const char *name)
std::vector< typename V::non_const_value_type > toHost(const V &f)
dict A
Definition post.py:33
int m
Definition post.py:35
xr
Definition post.py:23
B
Definition post.py:34
t
Definition post.py:21
xref(Bi)
Definition robin.py:13
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 bool sameBits(const std::vector< double > &a, const std::vector< double > &b)
static constexpr double F
static constexpr double U
static int fails
static bool same(double a, double b)
static void build(IbmSolver &s, std::size_t cells)