flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_scalar_mg.cpp
Go to the documentation of this file.
1// ScalarMG, the cut-cell scalar preconditioner (doc/scalar_ibm_design.md §5.2; WO-4), single rank.
2//
3// levels — the level table equals VelocityMG::init's on the same grid and metric (inner
4// dims and coarsening ratio per level), isotropic and anisotropic, odd and
5// short axes included (§5.2: "verified identical by a unit test");
6// coarse — the rediscretized coarse surrogate of a Dirichlet sphere: symmetric by
7// construction (face form), identity rows exactly at pinned cells, an M-matrix
8// (positive diagonal, non-positive faces, weakly diagonally dominant on every
9// unknown row), and the face coefficient of the constant-Lam single-phase case
10// equal to CutcellMG's rule w_a(L) <Lam a>;
11// level rule — transient with dt D / h^2 < 1 uses level 0 alone, steady the full table;
12// contraction — the guard of design Amendment A1 (the power estimate of rho(I - M^-1 S), the
13// geometric mean of the last 10 of 30): (C1) < 1 on every geometry; (C2) <= 0.35
14// on box geometries (G1, G2, G3a at every rung), Neumann geometries (G5b's array
15// on multigrid-friendly n) and no-solid geometries; (C3) <= 0.75 on a periodic
16// box whose only sink is one Dirichlet sphere. Krylov counts stay the primary
17// gate (tests/python/test_scalar_cutcell_gates.py);
18// advective — design Amendment A2 (WO-5c), the steady advective surrogate, rows u1-u6: (u1)
19// uniform flow: Qp_a = phi_a / cf_a exactly, Qm_a = 0, (2,2,1) levels included;
20// (u2) a divergence-free random field: advective row sums <= 1e-13 max Q, column
21// sums 0 to round-off, every level; (u3) the M-matrix on every level; (u4) the
22// level-0 advective applySurrogate == the operator's band matvec with SAC,
23// bitwise; (u5) Q = 0 on the domain-face planes of every non-periodic axis, open
24// faces included; (u6) C4: the C3 problem advecting at census Pe_h 0.1 / 1 / 10,
25// contraction < 1 and <= 0.90 (prov.). Prints the G-perf V-cycle time ratio.
26#include <chrono>
27#include <cmath>
28#include <cstdio>
29#include <cstring>
30#include <Kokkos_Core.hpp>
31#include <vector>
32
33#include "flow_ibm.hpp"
34
35namespace {
36int failures = 0;
37#define CHECK(cond) \
38 do { \
39 if (!(cond)) { \
40 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
41 ++failures; \
42 } \
43 } while (0)
44
50using HV = Kokkos::View<double*, Kokkos::HostSpace>;
51HV host(const CCField& f) {
52 return Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
53}
54
55void testLevels() {
56 struct Case {
57 int n[3];
58 double hp[3];
59 };
60 const Case cases[] = {
61 {{64, 64, 64}, {1, 1, 1}}, {{48, 40, 12}, {1, 1, 1}}, {{128, 128, 4}, {1, 1, 1}},
62 {{30, 64, 7}, {1, 1, 1}}, {{77, 77, 77}, {1, 1, 1}}, {{64, 64, 32}, {1, 1, 2}},
63 {{20, 24, 48}, {1, 1.5, 3}}, {{96, 32, 64}, {2, 1, 1}},
64 };
65 for (const Case& c : cases) {
66 const double w[3] = {1.0 / (c.hp[0] * c.hp[0]), 1.0 / (c.hp[1] * c.hp[1]),
67 1.0 / (c.hp[2] * c.hp[2])};
68 VelocityMG vmg;
69 vmg.setMetric(w, c.hp);
70 vmg.init(c.n[0], c.n[1], c.n[2], 64);
71 ScalarMG smg;
72 smg.setMetric(w, c.hp);
73 smg.init(c.n[0], c.n[1], c.n[2], [](CCField) {});
74 bool same = vmg.levels() == smg.levels();
75 for (int L = 0; same && L < smg.levels(); ++L) {
76 const auto& a = vmg.level(L);
77 const auto& b = smg.level(L);
78 same = a.inner.x == b.inner.x && a.inner.y == b.inner.y && a.inner.z == b.inner.z &&
79 a.ratio.x == b.ratio.x && a.ratio.y == b.ratio.y && a.ratio.z == b.ratio.z &&
80 a.cfac.x == b.cfac.x && a.cfac.y == b.cfac.y && a.cfac.z == b.cfac.z;
81 }
82 std::printf("levels %dx%dx%d h' (%g,%g,%g): %d levels, bottom %dx%dx%d %s\n", c.n[0], c.n[1],
83 c.n[2], c.hp[0], c.hp[1], c.hp[2], smg.levels(),
84 smg.level(smg.levels() - 1).inner.x, smg.level(smg.levels() - 1).inner.y,
85 smg.level(smg.levels() - 1).inner.z, same ? "== VelocityMG" : "DIFFERS");
86 CHECK(same);
87 }
88}
89
90// Sphere of radius R (cells) at c in an n^3 box; solid inside.
91std::vector<double> sphereSdf(int n, double cx, double cy, double cz, double R) {
92 std::vector<double> s((std::size_t)n * n * n);
93 for (int z = 0; z < n; ++z)
94 for (int y = 0; y < n; ++y)
95 for (int x = 0; x < n; ++x) {
96 const double dx = x + 0.5 - cx, dy = y + 0.5 - cy, dz = z + 0.5 - cz;
97 s[(std::size_t)x + (std::size_t)y * n + (std::size_t)z * n * n] =
98 std::sqrt(dx * dx + dy * dy + dz * dz) - R;
99 }
100 return s;
101}
102
103// G1's configuration on an n^3 box (cells): Dirichlet sphere R = n/8 ... set by caller.
104void g1Setup(IbmSolver& s, int n, double R, double off) {
105 s.setRho(1.0);
106 s.setMu(1.0);
107 s.setDt(1.0);
108 for (int f = 0; f < 6; ++f)
109 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
110 s.setSolid(sphereSdf(n, 0.5 * n + off, 0.5 * n - 0.6 * off, 0.5 * n + 0.3 * off, R), false);
111 s.addScalar("c", 0.7, 1, 50, true);
112 for (int f = 0; f < 6; ++f)
113 s.setScalarBc("c", f, 2, 0.25);
114 s.setScalarWall("c", 1, 1.0, 0.0, -1);
115}
116
117void testCoarse() {
118 const int n = 32;
119 IbmSolver s(n, n, n);
120 s.setAdvection(false);
121 g1Setup(s, n, 8.0, 0.37);
122 s.solveScalarSteady("c");
123 auto& st = *s.scalarField("c").cut;
124 const ScalarMG& mg = *st.mg;
125 CHECK(st.mgLevels == mg.levels() && mg.levels() == 5); // 32 -> 16 -> 8 -> 4 -> 2
126 long pinned = 0, rows = 0, badId = 0, badMM = 0, badFace = 0;
127 for (int L = 1; L < mg.levels(); ++L) {
128 const auto& c = mg.level(L);
129 const auto& f = mg.level(L - 1);
130 auto AC = host(c.AC), AX = host(c.AFX), AY = host(c.AFY), AZ = host(c.AFZ), U = host(c.unk);
131 auto PX = host(c.px);
132 const long sy = c.ext.x, sz = (long)c.ext.x * c.ext.y;
133 const double wx = 1.0 / ((double)c.cfac.x * c.cfac.x);
134 // CutcellMG's coarse face rule on the finer level's products (the recursion, checked once)
135 HV FP = L >= 2 ? host(f.px) : HV();
136 for (int z = c.g; z < c.ext.z - c.g; ++z)
137 for (int y = c.g; y < c.ext.y - c.g; ++y)
138 for (int x = c.g; x < c.ext.x - c.g; ++x) {
139 const long i = x + y * sy + z * sz;
140 ++rows;
141 if (!(U(i) > 0.5)) {
142 ++pinned;
143 if (!(AC(i) == 1.0 && AX(i) == 0.0 && AY(i) == 0.0 && AZ(i) == 0.0))
144 ++badId;
145 continue;
146 }
147 const double off = -(AX(i) + AX(i + 1) + AY(i) + AY(i + sy) + AZ(i) + AZ(i + sz));
148 const bool nonpos = AX(i) <= 0 && AX(i + 1) <= 0 && AY(i) <= 0 && AY(i + sy) <= 0 &&
149 AZ(i) <= 0 && AZ(i + sz) <= 0;
150 if (!(AC(i) > 0.0 && nonpos && AC(i) >= off * (1.0 - 1e-14)))
151 ++badMM;
152 if (!(AX(i) == -(wx * PX(i))))
153 ++badFace;
154 if (L >= 2) {
155 const long fsy = f.ext.x, fsz = (long)f.ext.x * f.ext.y;
156 const int fx = 2 * (x - c.g) + f.g, fy = 2 * (y - c.g) + f.g, fz = 2 * (z - c.g) + f.g;
157 double sxs = 0.0;
158 for (int a = 0; a < 2; ++a)
159 for (int b = 0; b < 2; ++b)
160 sxs += FP(fx + (fy + a) * fsy + (fz + b) * fsz);
161 if (!(PX(i) == sxs / 4.0))
162 ++badFace;
163 }
164 }
165 }
166 std::printf(
167 "coarse: %ld rows (%ld pinned): identity-row defects %ld, M-matrix defects %ld, "
168 "face-rule defects %ld\n",
169 rows, pinned, badId, badMM, badFace);
170 CHECK(pinned > 0 && badId == 0 && badMM == 0 && badFace == 0);
171}
172
173void testLevelRule() {
174 const int n = 32;
175 IbmSolver s(n, n, n);
176 s.setAdvection(false);
177 g1Setup(s, n, 8.0, 0.37);
178 // dt D / h^2 = F -> kappa_A = 1 + 12 F; F just below / above the switch (D-WO9-3: 25 -> F = 2)
179 const double Fsw = (ScalarMG::kFullTableKappa - 1.0) / 12.0;
180 s.setDt(0.95 * Fsw / 0.7); // kappa_A = 23.8 < 25
181 s.advanceScalars();
182 const int small = s.scalarField("c").cut->mgLevels;
183 s.setDt(1.05 * Fsw / 0.7); // kappa_A = 26.2
184 s.advanceScalars();
185 const int at = s.scalarField("c").cut->mgLevels;
186 s.solveScalarSteady("c");
187 const int steady = s.scalarField("c").cut->mgLevels;
188 std::printf("level rule: dt D/h^2 = %.3f -> %d level(s), %.3f -> %d, steady -> %d\n", 0.95 * Fsw,
189 small, 1.05 * Fsw, at, steady);
190 CHECK(small == 1 && at == 5 && steady == 5);
191}
192
193double contraction(IbmSolver& s, const char* tag) {
194 auto& st = *s.scalarField("c").cut;
195 const std::size_t n = st.SAC.extent(0);
196 CCField e("e", n), t("t", n), z("z", n);
197 const std::vector<double> r = st.mg->contraction(30, e, t, z);
198 double gm = 1.0;
199 const int tail = 10;
200 for (int k = (int)r.size() - tail; k < (int)r.size(); ++k)
201 gm *= r[(std::size_t)k];
202 gm = std::pow(gm, 1.0 / tail);
203 std::printf(
204 "%s: %d levels, ||e_k||/||e_k-1||: first %.3f, k=10 %.3f, k=30 %.3f; "
205 "geometric mean of the last %d: %.3f\n",
206 tag, st.mgLevels, r[0], r[9], r.back(), tail, gm);
207 return gm;
208}
209
210// The concentric-shells scene of G2 (cell units): instance 0 a sphere Ri, instance 1 a huge box
211// minus a sphere Ro (solid outside Ro), both centred at c.
212void shellsScene(IbmSolver& s, double c, double Ri, double Ro) {
213 const std::vector<int> ni = {1, -1, -1, 3, -1, -1, 1, -1, -1, 34, 1, 2};
214 std::vector<double> nr(4 * 16, 0.0);
215 for (int k = 0; k < 4; ++k) {
216 nr[16 * k + 14] = 1.0;
217 nr[16 * k + 15] = 1.0;
218 }
219 nr[0] = Ri;
220 nr[16 + 0] = nr[16 + 1] = nr[16 + 2] = 1e5;
221 nr[32 + 0] = Ro;
222 const std::vector<int> ii = {0, -1, 3, -1};
223 std::vector<double> ir(2 * 18, 0.0);
224 for (int k = 0; k < 2; ++k) {
225 ir[18 * k + 0] = ir[18 * k + 1] = ir[18 * k + 2] = c;
226 ir[18 * k + 6] = 1.0;
227 ir[18 * k + 7] = 1.0;
228 }
229 s.setScene(ni, nr, ii, ir, false);
230 s.setSolidFromScene(false);
231}
232
233void walls(IbmSolver& s) {
234 s.setRho(1.0);
235 s.setMu(1.0);
236 s.setDt(1.0);
237 for (int f = 0; f < 6; ++f)
238 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
239}
240
241// rho on one configured problem; class 2 -> C2 (<= 0.35), 3 -> C3 (<= 0.75), always C1 (< 1).
242void guardRow(IbmSolver& s, const char* tag, int cls) {
243 s.solveScalarSteady("c");
244 char t[160];
245 std::snprintf(t, sizeof t, "%-44s %2d it", tag, s.scalarField("c").cut->iterations);
246 const double rho = contraction(s, t);
247 CHECK(rho < 1.0);
248 if (cls == 2)
249 CHECK(rho <= 0.35);
250 if (cls == 3)
251 CHECK(rho <= 0.75);
252}
253
254void testContraction() {
255 char tag[96];
256 for (int n : {32, 64, 128}) { // G1: Dirichlet sphere R = n/4, Dirichlet box
257 IbmSolver s(n, n, n);
258 s.setAdvection(false);
259 g1Setup(s, n, 0.25 * n, 0.37);
260 std::snprintf(tag, sizeof tag, "C2 G1 R/h=%d (%d^3)", n / 4, n);
261 guardRow(s, tag, 2);
262 }
263 const int g2box[3] = {48, 80, 128};
264 for (int k = 0; k < 3; ++k) { // G2 on its multigrid-friendly boxes (ruling D-WO4-1)
265 const int Rih = 6 << k, n = g2box[k];
266 IbmSolver s(n, n, n);
267 s.setAdvection(false);
268 walls(s);
269 shellsScene(s, 0.5 * n + 0.29, Rih, 2.5 * Rih);
270 s.addScalar("c", 0.8, 1, 50, true);
271 for (int f = 0; f < 6; ++f)
272 s.setScalarBc("c", f, 1, 0.0); // the box is solid (outside Ro): Neumann
273 s.setScalarWall("c", 0, 0.3, 0.0, 0);
274 s.setScalarWall("c", 1, 0.0, 0.0, 1);
275 std::snprintf(tag, sizeof tag, "C2 G2 Ri/h=%d (%d^3)", Rih, n);
276 guardRow(s, tag, 2);
277 }
278 for (int n : {32, 64, 128}) { // G3a, Da = 1: Robin sphere, Dirichlet box
279 IbmSolver s(n, n, n);
280 s.setAdvection(false);
281 g1Setup(s, n, 0.25 * n, 0.37);
282 s.setScalarWall("c", 2, 0.0, 0.7 / (0.25 * n), -1); // k = Da D / R
283 std::snprintf(tag, sizeof tag, "C2 G3a Da=1 R/h=%d (%d^3)", n / 4, n);
284 guardRow(s, tag, 2);
285 }
286 { // Neumann sphere, Dirichlet box
287 IbmSolver s(64, 64, 64);
288 s.setAdvection(false);
289 g1Setup(s, 64, 16.0, 0.37);
290 s.setScalarWall("c", 0, 0.3, 0.0, -1);
291 guardRow(s, "C2 Neumann sphere + Dirichlet box (64^3)", 2);
292 }
293 { // no solid, Dirichlet box
294 IbmSolver s(64, 64, 64);
295 s.setAdvection(false);
296 walls(s);
297 s.setSolid(std::vector<double>((std::size_t)64 * 64 * 64, 100.0), false);
298 s.addScalar("c", 0.7, 1, 50, true);
299 for (int f = 0; f < 6; ++f)
300 s.setScalarBc("c", f, 2, 0.25);
301 s.setScalarSource("c", 0.1);
302 guardRow(s, "C2 no solid, Dirichlet box (64^3)", 2);
303 }
304 for (int n : {32, 48, 80, 77}) { // G5b's geometry: periodic SC array, c = 0.3, singular
305 IbmSolver s(n, n, n);
306 s.setAdvection(false);
307 s.setRho(1.0);
308 s.setMu(1.0);
309 s.setDt(1.0);
310 const double R = 0.5 * n * std::cbrt(0.3 * 6.0 / M_PI);
311 s.setSolid(sphereSdf(n, 0.5 * n + 0.21, 0.5 * n - 0.13, 0.5 * n + 0.07, R), false);
312 s.addScalar("c", 0.7, 1, 50, true);
313 s.setScalarWall("c", 0, 0.3, 0.0, -1);
314 s.setScalarSource("c", 0.1);
315 // n = 77 (G5b's literal ND = 64) is odd: one level, correctness-only (C1)
316 std::snprintf(tag, sizeof tag, "%s G5b geometry n=%d (ND %.1f, singular)",
317 n == 77 ? "C1" : "C2", n, 2 * R);
318 guardRow(s, tag, n == 77 ? 1 : 2);
319 CHECK(s.scalarField("c").cut->singular);
320 }
321 for (int n : {64, 128}) { // C3: periodic box, the only sink one Dirichlet sphere R = n/4
322 IbmSolver s(n, n, n);
323 s.setAdvection(false);
324 s.setRho(1.0);
325 s.setMu(1.0);
326 s.setDt(1.0);
327 s.setSolid(sphereSdf(n, 0.5 * n + 0.37, 0.5 * n - 0.22, 0.5 * n + 0.11, 0.25 * n), false);
328 s.addScalar("c", 0.7, 1, 50, true);
329 s.setScalarWall("c", 1, 1.0, 0.0, -1);
330 s.setScalarSource("c", -0.3);
331 std::snprintf(tag, sizeof tag, "C3 periodic + Dirichlet sphere R/h=%d (%d^3)", n / 4, n);
332 guardRow(s, tag, 3);
333 }
334}
335
336// ---- the advective surrogate (design Amendment A2, §6.7; WO-5c): rows u1-u6
337// ----------------------
338
339// Inner linear index of level `lv`.
340long inner(const ScalarMG::Level& lv, int x, int y, int z) {
341 return (long)(x + lv.g) + (long)(y + lv.g) * lv.ext.x + (long)(z + lv.g) * lv.ext.x * lv.ext.y;
342}
343
344// A periodic box without a solid (the cut-cell projection's all-open openness), cell units, a
345// cut-cell scalar D = 0.7 with a source; the caller sets the face velocities, then solves steady.
346void openBox(IbmSolver& s) {
347 s.setRho(1.0);
348 s.setMu(1.0);
349 s.setDt(1.0);
350 s.setSolid(std::vector<double>((std::size_t)s.nx() * s.ny() * s.nz(), 100.0), true);
351 s.addScalar("c", 0.7, 1, 50, true);
352 s.setScalarSource("c", 0.1);
353}
354
355// (u1) uniform flow along each axis, periodic box without a solid, on a grid whose table has
356// (2,2,2) and (2,2,1) levels: Qp_a = phi_a / cf_a exactly and Qm_a = 0 on every coarse level (the
357// velocity 0.75 makes every sub-face sum exact).
358void testAdvUniform() {
359 const int nn[2][3] = {{32, 32, 32}, {32, 32, 4}};
360 for (const auto& n : nn)
361 for (int ax = 0; ax < 3; ++ax) {
362 IbmSolver s(n[0], n[1], n[2]);
363 s.setAdvection(false);
364 openBox(s);
365 const std::size_t nc = (std::size_t)n[0] * n[1] * n[2];
366 const char* nm[3] = {"u", "v", "w"};
367 for (int a = 0; a < 3; ++a)
368 s.setField(nm[a], std::vector<double>(nc, a == ax ? 0.75 : 0.0));
369 s.solveScalarSteady("c");
370 const auto& mg = *s.scalarField("c").cut->mg;
371 long bad = 0, cells = 0;
372 bool aniso = false;
373 for (int L = 1; L < mg.levels(); ++L) {
374 const auto& lv = mg.level(L);
375 aniso = aniso || (lv.cfac.x != lv.cfac.z);
376 const int cf[3] = {lv.cfac.x, lv.cfac.y, lv.cfac.z};
377 for (int a = 0; a < 3; ++a) {
378 const HV qp = host(lv.qp[a]), qm = host(lv.qm[a]);
379 const double want = a == ax ? 0.75 / (double)cf[a] : 0.0;
380 for (int z = 0; z < lv.inner.z; ++z)
381 for (int y = 0; y < lv.inner.y; ++y)
382 for (int x = 0; x < lv.inner.x; ++x) {
383 const long i = inner(lv, x, y, z);
384 bad += (qp(i) == want && qm(i) == 0.0) ? 0 : 1;
385 ++cells;
386 }
387 }
388 }
389 std::printf(
390 "(u1) uniform flow along %c, %dx%dx%d (%d levels%s): %ld of %ld Qp/Qm entries "
391 "differ from phi/cf_a, 0\n",
392 "xyz"[ax], n[0], n[1], n[2], mg.levels(), aniso ? ", (2,2,1) levels" : "", bad, cells);
393 CHECK(bad == 0 && cells > 0);
394 if (n[2] == 4)
395 CHECK(aniso);
396 }
397}
398
399// (u2) a divergence-free random field (node stream-function differences, periodic): on every
400// level |sum out - sum in| <= 1e-13 max Q per row (level 0 from phi), and the column sums of the
401// band operator vanish to round-off (no solid, steady, no open face: the diffusion columns cancel
402// too, so the whole column sum is the advective one).
403void testAdvDivergenceFree() {
404 const int n = 32;
405 IbmSolver s(n, n, n);
406 s.setAdvection(false);
407 openBox(s);
408 std::vector<double> psi[3];
409 unsigned long long st = 0x2545F4914F6CDD1Dull;
410 auto rnd = [&]() {
411 st ^= st << 13;
412 st ^= st >> 7;
413 st ^= st << 17;
414 return (double)(st >> 11) * (1.0 / 9007199254740992.0) - 0.5;
415 };
416 for (auto& p : psi) {
417 p.resize((std::size_t)n * n * n);
418 for (double& v : p)
419 v = rnd();
420 }
421 auto P = [&](int c, int x, int y, int z) {
422 return psi[c][(std::size_t)((x + n) % n) + (std::size_t)((y + n) % n) * n +
423 (std::size_t)((z + n) % n) * n * n];
424 };
425 std::vector<double> u((std::size_t)n * n * n), v(u.size()), w(u.size());
426 for (int z = 0; z < n; ++z)
427 for (int y = 0; y < n; ++y)
428 for (int x = 0; x < n; ++x) {
429 const std::size_t k = (std::size_t)x + (std::size_t)y * n + (std::size_t)z * n * n;
430 u[k] = (P(2, x, y + 1, z) - P(2, x, y, z)) - (P(1, x, y, z + 1) - P(1, x, y, z));
431 v[k] = (P(0, x, y, z + 1) - P(0, x, y, z)) - (P(2, x + 1, y, z) - P(2, x, y, z));
432 w[k] = (P(1, x + 1, y, z) - P(1, x, y, z)) - (P(0, x, y + 1, z) - P(0, x, y, z));
433 }
434 s.setField("u", u);
435 s.setField("v", v);
436 s.setField("w", w);
437 s.solveScalarSteady("c");
438 const auto& sc = s.scalarField("c");
439 const auto& mg = *sc.cut->mg;
440 CHECK(sc.cut->advecting && sc.cut->steady);
441 bool ok = true;
442 for (int L = 0; L < mg.levels(); ++L) {
443 const auto& lv = mg.level(L);
444 const long sx = 1, sy = lv.ext.x, sz = (long)lv.ext.x * lv.ext.y;
445 HV q[6];
446 if (L == 0) { // level 0: the positive parts of phi (no solid: every face is a coupling)
447 for (int a = 0; a < 3; ++a) {
448 const HV ph = host(sc.cut->phi[a]);
449 q[a] = HV("qp", ph.extent(0));
450 q[3 + a] = HV("qm", ph.extent(0));
451 for (std::size_t k = 0; k < ph.extent(0); ++k) {
452 q[a](k) = std::fmax(ph(k), 0.0);
453 q[3 + a](k) = std::fmax(-ph(k), 0.0);
454 }
455 }
456 } else {
457 for (int a = 0; a < 3; ++a) {
458 q[a] = host(lv.qp[a]);
459 q[3 + a] = host(lv.qm[a]);
460 }
461 }
462 const HV AC = host(lv.AC), AW = host(lv.AW), AE = host(lv.AE), AS = host(lv.AS),
463 AN = host(lv.AN), AB = host(lv.AB), AT = host(lv.AT);
464 double maxQ = 0.0, maxAC = 0.0, row = 0.0, col = 0.0;
465 for (int z = 0; z < lv.inner.z; ++z)
466 for (int y = 0; y < lv.inner.y; ++y)
467 for (int x = 0; x < lv.inner.x; ++x) {
468 const long i = inner(lv, x, y, z);
469 for (int k = 0; k < 6; ++k)
470 maxQ = std::fmax(maxQ, q[k](i));
471 maxAC = std::fmax(maxAC, std::fabs(AC(i)));
472 const double out =
473 ((q[3](i) + q[0](i + sx)) + (q[4](i) + q[1](i + sy))) + (q[5](i) + q[2](i + sz));
474 const double in =
475 ((q[0](i) + q[3](i + sx)) + (q[1](i) + q[4](i + sy))) + (q[2](i) + q[5](i + sz));
476 row = std::fmax(row, std::fabs(out - in));
477 // column i: the diagonal plus each neighbour's coefficient on x_i (periodic: the
478 // neighbours of an inner cell are inner or wrapped ghosts; use the inner wrap)
479 auto I = [&](int dx, int dy, int dz) {
480 return inner(lv, (x + dx + lv.inner.x) % lv.inner.x, (y + dy + lv.inner.y) % lv.inner.y,
481 (z + dz + lv.inner.z) % lv.inner.z);
482 };
483 const double cs = AC(i) + AW(I(1, 0, 0)) + AE(I(-1, 0, 0)) + AS(I(0, 1, 0)) +
484 AN(I(0, -1, 0)) + AB(I(0, 0, 1)) + AT(I(0, 0, -1));
485 col = std::fmax(col, std::fabs(cs));
486 }
487 std::printf("(u2) level %d: max|out - in| = %.2e max Q, max|column sum| = %.2e max AC\n", L,
488 row / maxQ, col / maxAC);
489 ok = ok && row <= 1e-13 * maxQ && col <= 1e-13 * maxAC && maxQ > 0.0;
490 }
491 CHECK(ok);
492}
493
494// The C3 problem (periodic box 4R, a Dirichlet sphere R = n/4 with a source) with the projected
495// Stokes field of that geometry (20 steps under a body force: WO-5's harness), cell units, the
496// pressure at its default tolerance. (u3) checks the COLUMN sums, which are zero by construction
497// whatever the field's divergence (review finding 3); the field's divergence moves only the row
498// sums, which is why the former row check needed the pressure PCG at 1e-14.
499void c3Stokes(IbmSolver& s, int n) {
500 s.setAdvection(false);
501 s.setRho(1.0);
502 s.setMu(1.0);
503 s.setDt(1.0);
504 s.setSolid(sphereSdf(n, 0.5 * n + 0.37, 0.5 * n - 0.22, 0.5 * n + 0.11, 0.25 * n), true);
505 s.setBodyForce(1e-3, 4e-4, 2e-4);
506 for (int k = 0; k < 20; ++k)
507 s.step();
508 s.addScalar("c", 0.7, 1, 50, true);
509 s.setScalarWall("c", 1, 1.0, 0.0, -1);
510 s.setScalarSource("c", -0.3);
511}
512
513// Rescale the face velocities so that the census max_cell_peclet of the next steady solve is `pe`
514// (G-adv's parametrization): one probe solve at the present field, then the field times
515// pe / measured.
516void rescalePeclet(IbmSolver& s, double pe) {
517 s.solveScalarSteady("c");
518 const double k = pe / s.scalarField("c").cut->maxCellPeclet;
519 for (const char* nm : {"u", "v", "w"}) {
520 std::vector<double> f = s.getField(nm);
521 for (double& v : f)
522 v *= k;
523 s.setField(nm, f);
524 }
525}
526
527// Seconds per V-cycle (z = M^-1 r) on the current build, r a fixed pseudo-random vector on the
528// unknowns (G-perf: the advective cycle against the symmetric one on the same block).
529double vcycleSeconds(IbmSolver& s, int reps) {
530 auto& st = *s.scalarField("c").cut;
531 const std::size_t n = st.SAC.extent(0);
532 CCField r("r", n), z("z", n);
533 const std::vector<double> unk = s.scalarGeometryField(4);
534 std::vector<double> rv(unk.size());
535 for (std::size_t k = 0; k < rv.size(); ++k)
536 rv[k] = unk[k] > 0.5 ? std::sin(0.37 * (double)k) : 0.0;
537 s.setField("c", rv);
538 Kokkos::deep_copy(r, s.scalarField("c").c);
539 st.mg->apply(z, r); // warm-up
540 Kokkos::fence();
541 const auto t0 = std::chrono::steady_clock::now();
542 for (int k = 0; k < reps; ++k)
543 st.mg->apply(z, r);
544 Kokkos::fence();
545 return std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count() / reps;
546}
547
548// (u3) A2's M-matrix property on every level, by COLUMNS (review finding 3, ruling D-WOR-3): every
549// off-diagonal band <= 0, and every column sum AC(j) + sum_k band_k(j's neighbour toward j) >=
550// -1e-13 max AC. The advective couplings enter as one number per face (Q on the upwind diagonal,
551// -Q as the downwind off-diagonal), so the advection and diffusion parts have zero column sums up
552// to round-off INDEPENDENTLY of the field's divergence; the mass, wall, Dirichlet and omega_open
553// terms only add to the diagonal. A column-diagonally dominant Z-matrix is an M-matrix (by
554// columns), and the left null vector of a singular closure problem is exactly 1 on every level. The
555// C3 box is periodic on every axis: the neighbours wrap over the inner cells. Returns the defect
556// count.
557long mMatrixDefects(const ScalarMG& mg, const char* tag) {
558 long bad = 0;
559 for (int L = 0; L < mg.levels(); ++L) {
560 const auto& lv = mg.level(L);
561 const HV AC = host(lv.AC), AW = host(lv.AW), AE = host(lv.AE), AS = host(lv.AS),
562 AN = host(lv.AN), AB = host(lv.AB), AT = host(lv.AT);
563 double maxAC = 0.0;
564 for (int z = 0; z < lv.inner.z; ++z)
565 for (int y = 0; y < lv.inner.y; ++y)
566 for (int x = 0; x < lv.inner.x; ++x)
567 maxAC = std::fmax(maxAC, std::fabs(AC(inner(lv, x, y, z))));
568 double minCol = 1e300;
569 long rows = 0, positive = 0, negative = 0;
570 for (int z = 0; z < lv.inner.z; ++z)
571 for (int y = 0; y < lv.inner.y; ++y)
572 for (int x = 0; x < lv.inner.x; ++x) {
573 const long i = inner(lv, x, y, z);
574 auto I = [&](int dx, int dy, int dz) {
575 return inner(lv, (x + dx + lv.inner.x) % lv.inner.x, (y + dy + lv.inner.y) % lv.inner.y,
576 (z + dz + lv.inner.z) % lv.inner.z);
577 };
578 for (const HV* b : {&AW, &AE, &AS, &AN, &AB, &AT})
579 positive += (*b)(i) > 0.0 ? 1 : 0;
580 const double cs = AC(i) + AW(I(1, 0, 0)) + AE(I(-1, 0, 0)) + AS(I(0, 1, 0)) +
581 AN(I(0, -1, 0)) + AB(I(0, 0, 1)) + AT(I(0, 0, -1));
582 minCol = std::fmin(minCol, cs / maxAC);
583 negative += cs < -1e-13 * maxAC ? 1 : 0;
584 ++rows;
585 }
586 std::printf(
587 "(u3) %s level %d: %ld columns, min column sum = %.3e max AC, %ld below -1e-13, %ld "
588 "positive "
589 "off-diagonals\n",
590 tag, L, rows, minCol, negative, positive);
591 bad += positive + negative;
592 }
593 return bad;
594}
595
596// (u6) the C4 contraction rows (WO-5's "C3 + advection" rows, restated by A2): the C3 problem at
597// R/h 16 advecting at census Pe_h 0.1, 1, 10 — C1 (< 1) and <= 0.90 (prov.). On the Pe_h 10 row
598// also (u3) the M-matrix on every level and (u4) the level-0 advective applySurrogate == the
599// operator's band matvec with SAC as the diagonal, bitwise. G-perf: the advective V-cycle's time
600// against the symmetric one (the same problem at rest) on the same block.
601void testAdvective() {
602 const int n = 64;
603 double tRest = 0.0;
604 {
605 IbmSolver s(n, n, n);
606 c3Stokes(s, n);
607 for (const char* nm : {"u", "v", "w"})
608 s.setField(nm, std::vector<double>((std::size_t)n * n * n, 0.0));
609 s.solveScalarSteady("c");
610 CHECK(!s.scalarField("c").cut->advecting);
611 tRest = vcycleSeconds(s, 20);
612 }
613 for (const double pe : {0.1, 1.0, 10.0}) {
614 IbmSolver s(n, n, n);
615 c3Stokes(s, n);
616 rescalePeclet(s, pe);
617 const auto& st = *s.scalarField("c").cut;
618 char tag[96];
619 std::snprintf(tag, sizeof tag, "C4 C3 + advection, Pe_h=%.3g, R/h=%d (%d^3)", pe, n / 4, n);
620 s.solveScalarSteady("c");
621 std::printf(" census max_cell_peclet %.6g; %ld implicit faces of %ld carrying flux\n",
622 st.maxCellPeclet, st.numImplicitFaces, st.numFluxFaces);
623 CHECK(std::fabs(st.maxCellPeclet / pe - 1.0) <= 1e-12);
624 CHECK(st.advecting && st.numImplicitFaces == st.numFluxFaces && st.numFluxFaces > 0);
625 char t[160];
626 std::snprintf(t, sizeof t, "%-44s %2d it", tag, st.iterations);
627 const double rho = contraction(s, t);
628 CHECK(rho < 1.0);
629 CHECK(rho <= 0.90);
630 if (pe == 10.0) {
631 auto& sc = s.scalarField("c");
632 CHECK(mMatrixDefects(*st.mg, "Pe_h 10") == 0);
633 // (u4)
634 const std::size_t nn = st.SAC.extent(0);
635 CCField x("x", nn), y1("y1", nn), y2("y2", nn);
636 {
637 auto hx = Kokkos::create_mirror_view(x);
638 for (std::size_t k = 0; k < nn; ++k)
639 hx(k) = std::cos(1.3 * (double)k) * (1.0 + 0.1 * std::sin(0.01 * (double)k));
640 Kokkos::deep_copy(x, hx);
641 }
642 st.mg->applySurrogate(y1, x); // fills x's ghosts (the solver's exchange)
643 peclet::flow::applyCutcellOp(y2, peclet::flow::CCConst(x), st.SAC, sc.AW, sc.AE, sc.AS, sc.AN,
644 sc.AB, sc.AT, st.mg->level(0).ext, IbmSolver::G);
645 const HV h1 = host(y1), h2 = host(y2);
646 const auto& l0 = st.mg->level(0);
647 long diff = 0;
648 for (int z = 0; z < l0.inner.z; ++z)
649 for (int yy = 0; yy < l0.inner.y; ++yy)
650 for (int xx = 0; xx < l0.inner.x; ++xx) {
651 const long i = inner(l0, xx, yy, z);
652 diff += std::memcmp(&h1(i), &h2(i), sizeof(double)) == 0 ? 0 : 1;
653 }
654 std::printf(
655 "(u4) level-0 advective applySurrogate vs the band matvec with SAC: %ld of %d "
656 "rows not bitwise\n",
657 diff, l0.inner.x * l0.inner.y * l0.inner.z);
658 CHECK(diff == 0);
659 const double tAdv = vcycleSeconds(s, 20);
660 std::printf(
661 "G-perf: V-cycle %d^3 (%d levels): advective %.2f ms, symmetric (at rest) "
662 "%.2f ms, ratio %.2f (expected ~1.5, red flag > 2)\n",
663 n, st.mgLevels, 1e3 * tAdv, 1e3 * tRest, tAdv / tRest);
664 }
665 }
666}
667
668// (u5) Q = 0 on both domain-face planes of every non-periodic axis on every coarse level, open
669// faces included: a channel along x (inflow -x, outflow +x), no-slip y and z walls, a sphere;
670// the flow developed by step() (which captures the open-face flux), then a steady solve.
671void testAdvOpenFaces() {
672 const int nx = 64, ny = 32, nz = 32;
673 IbmSolver s(nx, ny, nz);
674 s.setAdvection(false);
675 s.setRho(1.0);
676 s.setMu(1.0);
677 s.setDt(2.0);
678 s.setDomainBc(0, 2, 0.05, 0.0, 0.0); // -x inflow
679 s.setDomainBc(1, 3, 0.0, 0.0, 0.0); // +x outflow
680 for (int f = 2; f < 6; ++f)
681 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
682 std::vector<double> sdf((std::size_t)nx * ny * nz);
683 for (int z = 0; z < nz; ++z)
684 for (int y = 0; y < ny; ++y)
685 for (int x = 0; x < nx; ++x) {
686 const double dx = x + 0.5 - 27.3, dy = y + 0.5 - 15.6, dz = z + 0.5 - 16.9;
687 sdf[(std::size_t)x + (std::size_t)y * nx + (std::size_t)z * nx * ny] =
688 std::sqrt(dx * dx + dy * dy + dz * dz) - 7.4;
689 }
690 s.setSolid(sdf, true);
691 for (int k = 0; k < 10; ++k)
692 s.step();
693 s.addScalar("c", 0.4, 0, 50, true);
694 s.setScalarBc("c", 0, 2, 1.0); // dirichlet inflow
695 for (int f = 1; f < 6; ++f)
696 s.setScalarBc("c", f, 1, 0.0); // neumann
697 s.solveScalarSteady("c");
698 const auto& st = *s.scalarField("c").cut;
699 const auto& mg = *st.mg;
700 CHECK(st.advecting && st.converged);
701 long bad = 0, checked = 0;
702 for (int L = 1; L < mg.levels(); ++L) {
703 const auto& lv = mg.level(L);
704 const int ext[3] = {lv.ext.x, lv.ext.y, lv.ext.z};
705 const long sa[3] = {1, (long)lv.ext.x, (long)lv.ext.x * lv.ext.y};
706 for (int a = 0; a < 3; ++a) {
707 const HV qp = host(lv.qp[a]), qm = host(lv.qm[a]);
708 const int b = (a + 1) % 3, c = (a + 2) % 3;
709 for (const int pl : {lv.g, ext[a] - lv.g}) // the low and the high domain-face plane
710 for (int jb = lv.g; jb < ext[b] - lv.g; ++jb)
711 for (int jc = lv.g; jc < ext[c] - lv.g; ++jc) {
712 const long i = (long)pl * sa[a] + (long)jb * sa[b] + (long)jc * sa[c];
713 bad += (qp(i) == 0.0 && qm(i) == 0.0) ? 0 : 1;
714 ++checked;
715 }
716 }
717 }
718 std::printf(
719 "(u5) channel with open x faces, walls y z: %d levels, %ld domain-face entries, %ld "
720 "nonzero; steady %d iterations, census max_cell_peclet %.3g\n",
721 mg.levels(), checked, bad, st.iterations, st.maxCellPeclet);
722 CHECK(bad == 0 && checked > 0);
723}
724} // namespace
725
726int main(int argc, char** argv) {
727 Kokkos::initialize(argc, argv);
728 {
729 const bool advOnly = argc > 1 && std::strcmp(argv[argc - 1], "advective") == 0;
730 if (!advOnly) { // (`test_scalar_mg advective` runs the A2 rows alone)
731 testLevels();
732 testCoarse();
733 testLevelRule();
734 testContraction();
735 }
736 testAdvUniform();
737 testAdvDivergenceFree();
738 testAdvective();
739 testAdvOpenFaces();
740 }
741 Kokkos::finalize();
742 std::printf(failures ? "%d failure(s)\n" : "OK\n", failures);
743 return failures ? 1 : 0;
744}
int levels() const
Level & level(int L)
void init(int nx, int ny, int nz, int nLevels)
void setMetric(const double w[3], const double hp[3])
Per-axis metric of the physical domain (doc/anisotropic_metric.md §1.1): the weights w_a = 1/h_a'^2,...
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
Definition jc.py:1
contraction(A, levels, singular, nu, its=40)
dict fx
Definition mgd_case.py:457
void positive(const char *who, const std::string &name, double v, const char *dim)
Kokkos::View< const double *, CCMem > CCConst
void applyCutcellOp(CCField y, CCConst x, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, int g)
Kokkos::View< double *, CCMem > CCField
Solver< Staggered > IbmSolver
t
Definition post.py:21
P
Definition stab5.py:16
dict rows
Definition summarize.py:38
col(path, ncol)
steady(s, probe, maxit=3000, tol=1e-9)
static std::vector< double > sphereSdf()
Kokkos::View< double *, CCMem > CCField
static constexpr double AC
static constexpr double U
peclet::flow::IbmSolver IbmSolver
static constexpr double R
static bool same(double a, double b)
#define CHECK(cond)
static constexpr double AX