flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_sharp_viscous.cpp
Go to the documentation of this file.
1// flow — the sharp viscous stress at a VoF interface (src/vof/sharp_viscous.hpp,
2// doc/vof_sharp_viscous_stress.md §10 WO-2):
3//
4// (a) 3-D extrusion of the 2-D harness (n_z = 0, z-independent fields, exact planes injected):
5// tau_hat, D_nn, the cell C, both midsection fractions, both face fractions and both link
6// fractions of every node with a mixed cell equal mac2d (TAU_RULE 'mixed-avg') to 1e-12
7// relative; fit fallbacks 0. Reference: tests/kokkos/sharp_viscous_mac2d.txt
8// (tests/study/sharp_viscous/wo2_reference.py);
9// (b) exact tau and D_nn for kinked-linear fields (traceless L_lo, [u] = 0, [P sigma n] = 0) at
10// the 9 orientations (beta, gamma in {0, 22.5, 45} deg), offsets 0 and 0.3, mu ratio 1e-2 /
11// 1e2 / 1e-3 / 1e3, isotropic and h ratio 1 : 1.5 : 2: <= 1e-11; fallbacks 0;
12// (c) virtual planes (§5.7 Q-B): a 1-cell film normal to each axis and an ABAB stack: every edge
13// on a film face is owned at distance 0, and (m, alpha) follows the low-face rule;
14// (d) a periodic seam inside the band (45 deg slab, offset-order ties): classes, fallbacks and
15// tau/D_nn are bitwise invariant under a circular shift of the block by (5, 3, 2) cells;
16// (e) WO-3, the implicit band (sphere of tangent planes, mu ratio 1e-2 / 1e3, isotropic and
17// 1 : 1.5 : 2): every band-edge entry = -mu(F_c(E)) w_a of a host oracle (owner + PLIC face
18// fraction), every non-band entry and every cell/diagonal of a row without a band edge
19// bitwise 'averaged' (VarFaceProps), per-component symmetry bitwise, M-matrix;
20// (f) WO-4, the explicit kernel against a host transcription (sample arrays, then the
21// divergence; random u, tau_hat, D_nn, row scale): <= 1e-13 relative;
22// (g) WO-4, the full operator (implicit band + band transpose + tau-terms + midsection - grad p,
23// p the exact cell-average pressure) on exact face averages of kinked-linear Couette and
24// extension at the 9 orientations, offsets 0 / 0.3, 4 ratios, both metrics: residual
25// <= 1e-11 of the stress scale;
26// (h) WO-9.2 (§5.8), the owner-set mean: a vertex-centred sphere is reflection self-symmetric
27// (F, theta, T over every band edge) to 1e-13 of the scale, with ties;
28// (i) WO-9.2: a tie-free case is bitwise the f01a251 single-owner operator (host digest).
29// The host oracles of (e) and (f) transcribe the owner set and its mean (§5.8) independently.
30#include <algorithm>
31#include <array>
32#include <cmath>
33#include <cstdint>
34#include <cstdio>
35#include <cstring>
36#include <fstream>
37#include <Kokkos_Core.hpp>
38#include <limits>
39#include <random>
40#include <sstream>
41#include <string>
42#include <vector>
43
44#include "cut_cell_ibm.hpp"
45#include "full_stress.hpp"
46#include "vof/sharp_viscous.hpp"
47
48#ifndef SHARP_VISCOUS_REF
49#define SHARP_VISCOUS_REF "sharp_viscous_mac2d.txt"
50#endif
51
52namespace {
53int failures = 0;
54#define CHECK(cond, ...) \
55 do { \
56 if (!(cond)) { \
57 std::fprintf(stderr, "CHECK failed: %s at %s:%d ", #cond, __FILE__, __LINE__); \
58 std::fprintf(stderr, __VA_ARGS__); \
59 std::fprintf(stderr, "\n"); \
60 ++failures; \
61 } \
62 } while (0)
63
64using namespace peclet::flow;
65namespace sv = peclet::flow::vof;
66using DV = Kokkos::View<double*, CCMem>;
67constexpr double kPi = 3.14159265358979323846;
68constexpr int kBig = 1 << 28;
69
70DV toDev(const std::vector<double>& h, const char* name = "t") {
71 DV d(name, h.size());
72 auto m = Kokkos::create_mirror_view(d);
73 for (std::size_t i = 0; i < h.size(); ++i)
74 m(i) = h[i];
75 Kokkos::deep_copy(d, m);
76 return d;
77}
78std::vector<double> toHost(DV d) {
79 auto m = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), d);
80 return std::vector<double>(m.data(), m.data() + m.extent(0));
81}
82std::vector<int> toHost(sv::SvByte d) {
83 auto m = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), d);
84 std::vector<int> o(m.extent(0));
85 for (std::size_t i = 0; i < o.size(); ++i)
86 o[i] = m(i);
87 return o;
88}
89
90// Solver block (ghost 2) and colour block (ghost 3) of n inner cells.
91struct Blk {
92 int n[3], G = 2, e[3], e3[3];
93 long st[3], st3[3], cells, cells3;
94 Blk(int nx, int ny, int nz) : n{nx, ny, nz} {
95 for (int a = 0; a < 3; ++a) {
96 e[a] = n[a] + 2 * G;
97 e3[a] = n[a] + 2 * G + 2;
98 }
99 st[0] = 1;
100 st[1] = e[0];
101 st[2] = (long)e[0] * e[1];
102 st3[0] = 1;
103 st3[1] = e3[0];
104 st3[2] = (long)e3[0] * e3[1];
105 cells = st[2] * e[2];
106 cells3 = st3[2] * e3[2];
107 }
108 long id(int x, int y, int z) const { return x + y * st[1] + z * st[2]; }
109 // colour-block index of SOLVER coordinates (x, y, z), x in [-1, e]
110 long id3(int x, int y, int z) const { return (x + 1) + (y + 1) * st3[1] + (z + 1) * st3[2]; }
111};
112
113// The device side of one configuration.
114struct Geo {
115 DV col, pm[3], pa, tau[3], dnn, v[3];
116 sv::SvByte cls, fb;
117 sv::SharpGeom g;
118};
119
120// Colour + planes on the colour block (host vectors), class pass, fit over the inner cells.
121Geo makeGeo(const Blk& b, const std::vector<double>& col, const std::vector<double> (&pm)[3],
122 const std::vector<double>& pa, const std::vector<double> (&v)[3], const int dlo[3],
123 const int dhi[3], double eps, double muA, double muB, const double ell[3]) {
124 Geo o;
125 o.col = toDev(col, "col");
126 for (int a = 0; a < 3; ++a) {
127 o.pm[a] = toDev(pm[a], "pm");
128 o.v[a] = toDev(v[a], "v");
129 o.tau[a] = DV("tau", b.cells);
130 }
131 o.pa = toDev(pa, "pa");
132 o.dnn = DV("dnn", b.cells);
133 o.cls = sv::SvByte("cls", b.cells);
134 o.fb = sv::SvByte("fb", b.cells);
135 sv::SharpGeom& g = o.g;
136 for (int a = 0; a < 3; ++a) {
137 g.e[a] = b.e[a];
138 g.st[a] = b.st[a];
139 g.st3[a] = b.st3[a];
140 g.dlo[a] = dlo[a];
141 g.dhi[a] = dhi[a];
142 g.ell[a] = ell[a];
143 g.gm.h[a] = ell[a];
144 g.pm[a] = o.pm[a];
145 }
146 g.d = 1;
147 g.col = o.col;
148 g.pa = o.pa;
149 g.cls = o.cls;
150 g.eps = eps;
151 g.p0 = muB;
152 g.p1 = muA - muB;
153 g.muA = muA;
154 g.muB = muB;
155 g.lowA = muA < muB;
156 g.mulo = g.lowA ? muA : muB;
157 g.r = g.lowA ? muA / muB : muB / muA;
158 sv::sharpClassify(g, o.cls);
159 const CCConst vv[3] = {o.v[0], o.v[1], o.v[2]};
160 sv::sharpFit(g, vv, o.tau[0], o.tau[1], o.tau[2], o.dnn, o.fb, C3{b.G, b.G, b.G},
161 C3{b.e[0] - b.G, b.e[1] - b.G, b.e[2] - b.G});
162 Kokkos::fence();
163 return o;
164}
165
166// Edge queries on the device (§5.8): n_E (0 = not a band edge), the largest member distance, and
167// the owner-set means F_row(c), F_row(a), L_ac, L_ca.
168struct EdgeQ {
169 int k[3], a, c;
170};
171std::vector<std::array<double, 6>> queryEdges(const Geo& o, const std::vector<EdgeQ>& qs) {
172 const int nq = (int)qs.size();
173 Kokkos::View<int*, CCMem> qd("q", 5 * nq);
174 auto qh = Kokkos::create_mirror_view(qd);
175 for (int t = 0; t < nq; ++t) {
176 for (int s = 0; s < 3; ++s)
177 qh(5 * t + s) = qs[t].k[s];
178 qh(5 * t + 3) = qs[t].a;
179 qh(5 * t + 4) = qs[t].c;
180 }
181 Kokkos::deep_copy(qd, qh);
182 DV out("out", 6 * nq);
183 const sv::SharpGeom g = o.g;
184 Kokkos::parallel_for(
185 "test::edges", Kokkos::RangePolicy<CCExec>(0, nq), KOKKOS_LAMBDA(int t) {
186 const int k[3] = {qd(5 * t), qd(5 * t + 1), qd(5 * t + 2)};
187 const int a = qd(5 * t + 3), c = qd(5 * t + 4), b = 3 - a - c;
188 double dmax = 0.0;
189 const double n =
190 g.forEachOwner(k, a, c, [&](const int ow[3], const double m[3], double al, double) {
191 double xi[3];
192 xi[a] = k[a] - ow[a];
193 xi[c] = k[c] - ow[c];
194 xi[b] = 0.5;
195 dmax =
196 Kokkos::fmax(dmax, Kokkos::fabs(m[0] * xi[0] + m[1] * xi[1] + m[2] * xi[2] - al) /
197 Kokkos::sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]));
198 });
199 out(6 * t) = n;
200 if (n == 0.0)
201 return;
202 out(6 * t + 1) = dmax;
203 double f = 0.0, tac = 0.0, tca = 0.0;
204 g.edgeFaceFraction(k, a, c, c, f);
205 out(6 * t + 2) = f;
206 g.edgeFaceFraction(k, a, c, a, f);
207 out(6 * t + 3) = f;
208 g.edgeLinkFractions(k, a, c, tac, tca);
209 out(6 * t + 4) = tac;
210 out(6 * t + 5) = tca;
211 });
212 auto oh = toHost(out);
213 std::vector<std::array<double, 6>> r(nq);
214 for (int t = 0; t < nq; ++t)
215 for (int s = 0; s < 6; ++s)
216 r[t][s] = oh[6 * t + s];
217 return r;
218}
219
220// Cell queries: class, plane (m, alpha), midsection fractions c = 0, 1, 2.
221std::vector<std::array<double, 8>> queryCells(const Geo& o,
222 const std::vector<std::array<int, 3>>& qs) {
223 const int nq = (int)qs.size();
224 Kokkos::View<int*, CCMem> qd("q", 3 * nq);
225 auto qh = Kokkos::create_mirror_view(qd);
226 for (int t = 0; t < nq; ++t)
227 for (int s = 0; s < 3; ++s)
228 qh(3 * t + s) = qs[t][s];
229 Kokkos::deep_copy(qd, qh);
230 DV out("out", 8 * nq);
231 const sv::SharpGeom g = o.g;
232 Kokkos::parallel_for(
233 "test::cells", Kokkos::RangePolicy<CCExec>(0, nq), KOKKOS_LAMBDA(int t) {
234 const int q[3] = {qd(3 * t), qd(3 * t + 1), qd(3 * t + 2)};
235 const int k = g.cellClass(q);
236 out(8 * t) = k;
237 if (k == sv::kSvNone)
238 return;
239 double m[3], al;
240 g.plane(q, k, m, al);
241 out(8 * t + 1) = m[0];
242 out(8 * t + 2) = m[1];
243 out(8 * t + 3) = m[2];
244 out(8 * t + 4) = al;
245 for (int c = 0; c < 3; ++c)
246 out(8 * t + 5 + c) = sv::plicMidsectionFraction(m, al, c);
247 });
248 auto oh = toHost(out);
249 std::vector<std::array<double, 8>> r(nq);
250 for (int t = 0; t < nq; ++t)
251 for (int s = 0; s < 8; ++s)
252 r[t][s] = oh[8 * t + s];
253 return r;
254}
255
256// ---------------------------------------------------------------------------------------------
257// (a) the 2-D harness, extruded
258void testExtrusion() {
259 std::ifstream in(SHARP_VISCOUS_REF);
260 CHECK(in.good(), "cannot open %s", SHARP_VISCOUS_REF);
261 if (!in.good())
262 return;
263 std::string tag;
264 int ncases = 0;
265 in >> tag >> ncases;
266 double wTau = 0, wDnn = 0, wFrac = 0, wC = 0;
267 long nCells = 0, nNodes = 0, nFb = 0, nNoOwner = 0, nClass = 0;
268 for (int cs = 0; cs < ncases; ++cs) {
269 int N, gen;
270 double n0, n1, t0, t1, c0, c1, s, muA, muB;
271 in >> tag >> N >> n0 >> n1 >> t0 >> t1 >> c0 >> c1 >> s >> muA >> muB >> gen;
272 const double h = 1.0 / N;
273 struct Cell {
274 int i, j, fit;
275 double C, cbu, cbv, tau, dnn;
276 };
277 struct Node {
278 int i, j;
279 double fu, fv, thy, thx;
280 };
281 int nc;
282 in >> tag >> nc;
283 std::vector<Cell> cells(nc);
284 for (auto& c : cells)
285 in >> c.i >> c.j >> c.C >> c.cbu >> c.cbv >> c.fit >> c.tau >> c.dnn;
286 int nn;
287 in >> tag >> nn;
288 std::vector<Node> nodes(nn);
289 for (auto& d : nodes)
290 in >> d.i >> d.j >> d.fu >> d.fv >> d.thy >> d.thx;
291 Blk b(N, N, 3);
292 const int G = b.G;
293 std::vector<double> vel[3] = {std::vector<double>(b.cells, 0.0),
294 std::vector<double>(b.cells, 0.0),
295 std::vector<double>(b.cells, 0.0)};
296 for (int comp = 0; comp < 2; ++comp) {
297 int nv;
298 in >> tag >> nv;
299 for (int t = 0; t < nv; ++t) {
300 int i, j;
301 double val;
302 in >> i >> j >> val;
303 for (int z = 0; z < b.e[2]; ++z)
304 vel[comp][b.id(i + G, j + G, z)] = val / h; // index velocity (t_ref = 1, h' = 1)
305 }
306 }
307 // Colour block: mixed cells from the file, pure by the sign of phi at the centre.
308 auto phi = [&](double x, double y) { return n0 * (x - c0) + n1 * (y - c1) - s; };
309 std::vector<double> col(b.cells3),
310 pm[3] = {std::vector<double>(b.cells3, 0.0), std::vector<double>(b.cells3, 0.0),
311 std::vector<double>(b.cells3, 0.0)},
312 pa(b.cells3, 0.0);
313 for (int z = -1; z <= b.e[2]; ++z)
314 for (int y = -1; y <= b.e[1]; ++y)
315 for (int x = -1; x <= b.e[0]; ++x)
316 col[b.id3(x, y, z)] = phi((x - G + 0.5) * h, (y - G + 0.5) * h) < 0 ? 1.0 : 0.0;
317 for (const auto& c : cells)
318 for (int z = -1; z <= b.e[2]; ++z) {
319 const long j3 = b.id3(c.i + G, c.j + G, z);
320 col[j3] = c.C;
321 pm[0][j3] = n0;
322 pm[1][j3] = n1;
323 pm[2][j3] = 0.0;
324 pa[j3] = (s + n0 * c0 + n1 * c1) / h - (n0 * c.i + n1 * c.j);
325 }
326 const int dlo[3] = {G, G, -kBig}, dhi[3] = {N + G, N + G, kBig};
327 const double ell[3] = {1.0, 1.0, 1.0};
328 Geo o = makeGeo(b, col, pm, pa, vel, dlo, dhi, 0.0, muA, muB, ell);
329 auto tau0 = toHost(o.tau[0]), tau1 = toHost(o.tau[1]), tau2 = toHost(o.tau[2]),
330 dnn = toHost(o.dnn);
331 auto fb = toHost(o.fb);
332 const double mulo = std::min(muA, muB);
333 double tmax = 0.0;
334 for (const auto& c : cells)
335 tmax = std::max(tmax, std::fabs(c.tau));
336 std::vector<std::array<int, 3>> cq;
337 for (const auto& c : cells)
338 cq.push_back({c.i + G, c.j + G, G + 1});
339 auto cr = queryCells(o, cq);
340 for (std::size_t t = 0; t < cells.size(); ++t) {
341 const auto& c = cells[t];
342 const bool in = c.i >= 0 && c.i < N && c.j >= 0 && c.j < N;
343 CHECK((cr[t][0] == sv::kSvPlane) == in, "class of (%d,%d) = %g", c.i, c.j, cr[t][0]);
344 if (!in)
345 continue;
346 nClass += (cr[t][0] == sv::kSvPlane);
347 const double mm[3] = {cr[t][1], cr[t][2], cr[t][3]};
348 wC = std::max(wC, std::fabs(sv::plicVolume(mm[0], mm[1], mm[2], cr[t][4]) - c.C));
349 wFrac = std::max({wFrac, std::fabs(cr[t][5] - c.cbu), std::fabs(cr[t][6] - c.cbv)});
350 if (!c.fit)
351 continue;
352 ++nCells;
353 const long i = b.id(c.i + G, c.j + G, G + 1);
354 nFb += fb[i];
355 const double et = std::max(
356 {std::fabs(tau0[i] - c.tau * t0), std::fabs(tau1[i] - c.tau * t1), std::fabs(tau2[i])});
357 wTau = std::max(wTau, et / tmax);
358 wDnn = std::max(wDnn, std::fabs(dnn[i] - c.dnn) / (tmax / mulo));
359 }
360 std::vector<EdgeQ> eq;
361 for (const auto& d : nodes)
362 eq.push_back(EdgeQ{{d.i + G, d.j + G, G + 1}, 0, 1});
363 auto er = queryEdges(o, eq);
364 for (std::size_t t = 0; t < nodes.size(); ++t) {
365 const auto& d = nodes[t];
366 if (er[t][0] < 1.0) {
367 ++nNoOwner;
368 continue;
369 }
370 ++nNodes;
371 // E_xy: row c = y is the v row (F_y = fv), row a = x the u row (F_x = fu); L_ac = d_x u_y
372 // (thx), L_ca = d_y u_x (thy).
373 wFrac = std::max({wFrac, std::fabs(er[t][2] - d.fv), std::fabs(er[t][3] - d.fu),
374 std::fabs(er[t][4] - d.thx), std::fabs(er[t][5] - d.thy)});
375 }
376 }
377 std::printf(
378 "(a) extrusion: %d cases, %ld fits, %ld band nodes: tau %.2e, D_nn %.2e, fractions "
379 "%.2e, C %.2e; fallbacks %ld, ownerless %ld\n",
380 ncases, nCells, nNodes, wTau, wDnn, wFrac, wC, nFb, nNoOwner);
381 CHECK(nCells > 100 && nNodes > 100 && nClass >= nCells, "coverage");
382 CHECK(wTau <= 1e-12 && wDnn <= 1e-12, "tau/D_nn vs mac2d");
383 CHECK(wFrac <= 1e-12 && wC <= 1e-12, "fractions vs mac2d");
384 CHECK(nFb == 0 && nNoOwner == 0, "fallbacks / ownerless band nodes");
385}
386
387// ---------------------------------------------------------------------------------------------
388// Exact kinked-linear fields. Internal coordinates X_a = ell_a * (index coordinate).
389struct Kinked {
390 double n[3], xp[3], Llo[9], j[3], muA, muB;
391 bool lowA;
392 double phi(const double X[3]) const {
393 return n[0] * (X[0] - xp[0]) + n[1] * (X[1] - xp[1]) + n[2] * (X[2] - xp[2]);
394 }
395 bool hi(double ph) const { return lowA ? (ph >= 0.0) : (ph < 0.0); } // A: phi < 0
396};
397
398// int over [0,L1]x[0,L2] of max(0, f0 + g1 s + g2 t), exact (clipped polygon, centroid rule).
399double posInt(double f0, double g1, double g2, double L1, double L2) {
400 double P[4][2] = {{0, 0}, {L1, 0}, {L1, L2}, {0, L2}};
401 std::vector<std::array<double, 2>> poly;
402 for (int k = 0; k < 4; ++k) {
403 const double* A = P[k];
404 const double* B = P[(k + 1) % 4];
405 const double fa = f0 + g1 * A[0] + g2 * A[1], fb = f0 + g1 * B[0] + g2 * B[1];
406 if (fa > 0)
407 poly.push_back({A[0], A[1]});
408 if ((fa > 0) != (fb > 0)) {
409 const double w = fa / (fa - fb);
410 poly.push_back({A[0] + w * (B[0] - A[0]), A[1] + w * (B[1] - A[1])});
411 }
412 }
413 if (poly.size() < 3)
414 return 0.0;
415 double ar = 0, cx = 0, cy = 0;
416 for (std::size_t k = 0; k < poly.size(); ++k) {
417 const auto& p = poly[k];
418 const auto& q = poly[(k + 1) % poly.size()];
419 const double cr = p[0] * q[1] - q[0] * p[1];
420 ar += cr;
421 cx += (p[0] + q[0]) * cr;
422 cy += (p[1] + q[1]) * cr;
423 }
424 ar *= 0.5;
425 if (std::fabs(ar) < 1e-300)
426 return 0.0;
427 cx /= (6 * ar);
428 cy /= (6 * ar);
429 return std::fabs(ar) * (f0 + g1 * cx + g2 * cy);
430}
431
432// Face average of u_q over the -c face of cell (x, y, z) (index coordinates of the cell corner).
433double faceAvg(const Kinked& K, const double ell[3], int c, int x, int y, int z, int q) {
434 const int I[3] = {x, y, z};
435 const int p1 = (c == 0) ? 1 : 0, p2 = (c == 2) ? 1 : 2;
436 const double L1 = ell[p1], L2 = ell[p2];
437 double X0[3];
438 for (int a = 0; a < 3; ++a)
439 X0[a] = ell[a] * I[a];
440 double cen[3] = {X0[0], X0[1], X0[2]};
441 cen[p1] += 0.5 * L1;
442 cen[p2] += 0.5 * L2;
443 double lin = 0.0;
444 for (int p = 0; p < 3; ++p)
445 lin += K.Llo[3 * p + q] * (cen[p] - K.xp[p]);
446 const double f0 = K.phi(X0), g1 = K.n[p1], g2 = K.n[p2];
447 const double A = L1 * L2;
448 const double pos = posInt(f0, g1, g2, L1, L2) / A;
449 const double all = f0 + 0.5 * (g1 * L1 + g2 * L2); // mean of phi over the face
450 // the mean of phi over the hi side: phi >= 0 (lowA) -> mean max(phi, 0); phi < 0 -> mean
451 // min(phi, 0) = mean(phi) - mean max(phi, 0)
452 const double kink = K.lowA ? pos : (all - pos);
453 return lin + K.j[q] * kink;
454}
455
456struct KCase {
457 Blk b;
458 Kinked K;
459 double ell[3];
460 std::vector<double> col, pm[3], pa, v[3];
461};
462
463// Exact planes + colour on the colour block, exact face averages on the solver block.
464void buildKinked(KCase& kc) {
465 const Blk& b = kc.b;
466 const Kinked& K = kc.K;
467 kc.col.assign(b.cells3, 0.0);
468 kc.pa.assign(b.cells3, 0.0);
469 for (int a = 0; a < 3; ++a) {
470 kc.pm[a].assign(b.cells3, 0.0);
471 kc.v[a].assign(b.cells, 0.0);
472 }
473 for (int z = -1; z <= b.e[2]; ++z)
474 for (int y = -1; y <= b.e[1]; ++y)
475 for (int x = -1; x <= b.e[0]; ++x) {
476 const long j = b.id3(x, y, z);
477 double m[3];
478 for (int a = 0; a < 3; ++a)
479 m[a] = K.n[a] * kc.ell[a];
480 const double al = K.n[0] * K.xp[0] + K.n[1] * K.xp[1] + K.n[2] * K.xp[2] -
481 (m[0] * x + m[1] * y + m[2] * z);
482 const double C = sv::plicVolume(m[0], m[1], m[2], al);
483 kc.col[j] = C;
484 if (C > 0.0 && C < 1.0) {
485 for (int a = 0; a < 3; ++a)
486 kc.pm[a][j] = m[a];
487 kc.pa[j] = al;
488 }
489 }
490 for (int z = 0; z < b.e[2]; ++z)
491 for (int y = 0; y < b.e[1]; ++y)
492 for (int x = 0; x < b.e[0]; ++x)
493 for (int c = 0; c < 3; ++c)
494 kc.v[c][b.id(x, y, z)] = faceAvg(K, kc.ell, c, x, y, z, c) / kc.ell[c];
495}
496
497// L_lo traceless and its kink: L_hi = L_lo + n (x) j, j = (r - 1) P S_lo n.
498void setKink(Kinked& K, double ratio) {
499 K.muA = ratio;
500 K.muB = 1.0;
501 K.lowA = K.muA < K.muB;
502 const double r = K.lowA ? K.muA / K.muB : K.muB / K.muA;
503 double sn[3], nsn = 0;
504 for (int p = 0; p < 3; ++p) {
505 sn[p] = 0;
506 for (int q = 0; q < 3; ++q)
507 sn[p] += (K.Llo[3 * p + q] + K.Llo[3 * q + p]) * K.n[q];
508 nsn += K.n[p] * sn[p];
509 }
510 for (int p = 0; p < 3; ++p)
511 K.j[p] = (r - 1.0) * (sn[p] - K.n[p] * nsn);
512}
513
514void testKinked() {
515 std::mt19937 rng(7);
516 std::uniform_real_distribution<double> U(-1.0, 1.0);
517 const double angs[3] = {0.0, 22.5, 45.0};
518 double wTau = 0, wDnn = 0;
519 long nFit = 0, nFb = 0;
520 for (int an = 0; an < 2; ++an)
521 for (int ib = 0; ib < 3; ++ib)
522 for (int ig = 0; ig < 3; ++ig)
523 for (double off : {0.0, 0.3})
524 for (double ratio : {1e-2, 1e2, 1e-3, 1e3}) {
525 KCase kc{Blk(8, 8, 8), {}, {1.0, 1.0, 1.0}, {}, {}, {}, {}};
526 if (an == 1) {
527 kc.ell[1] = 1.5;
528 kc.ell[2] = 2.0;
529 }
530 const double be = angs[ib] * kPi / 180, ga = angs[ig] * kPi / 180;
531 Kinked& K = kc.K;
532 K.n[0] = std::cos(be) * std::cos(ga);
533 K.n[1] = std::cos(be) * std::sin(ga);
534 K.n[2] = std::sin(be);
535 for (int a = 0; a < 3; ++a) // through a cell corner + off along n (index units)
536 K.xp[a] = kc.ell[a] * 6.0 + off * K.n[a];
537 double tr = 0;
538 for (int s = 0; s < 9; ++s)
539 K.Llo[s] = U(rng);
540 for (int p = 0; p < 3; ++p)
541 tr += K.Llo[4 * p];
542 for (int p = 0; p < 3; ++p)
543 K.Llo[4 * p] -= tr / 3.0;
544 setKink(K, ratio);
545 buildKinked(kc);
546 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
547 Geo o = makeGeo(kc.b, kc.col, kc.pm, kc.pa, kc.v, dlo, dhi, 0.0, K.muA, K.muB, kc.ell);
548 auto t0 = toHost(o.tau[0]), t1 = toHost(o.tau[1]), t2 = toHost(o.tau[2]),
549 dn = toHost(o.dnn);
550 auto fb = toHost(o.fb), cl = toHost(o.cls);
551 // exact: tau = mu_lo P S_lo n, D_nn = n.L n
552 double sn[3], nsn = 0, dex = 0, tex[3], lmax = 0;
553 for (int p = 0; p < 3; ++p) {
554 sn[p] = 0;
555 for (int q = 0; q < 3; ++q) {
556 sn[p] += (K.Llo[3 * p + q] + K.Llo[3 * q + p]) * K.n[q];
557 dex += K.n[p] * K.Llo[3 * p + q] * K.n[q];
558 lmax = std::max(lmax, std::fabs(K.Llo[3 * p + q]));
559 }
560 nsn += K.n[p] * sn[p];
561 }
562 const double mulo = std::min(K.muA, K.muB);
563 for (int p = 0; p < 3; ++p)
564 tex[p] = mulo * (sn[p] - K.n[p] * nsn);
565 const Blk& b = kc.b;
566 for (int z = b.G; z < b.e[2] - b.G; ++z)
567 for (int y = b.G; y < b.e[1] - b.G; ++y)
568 for (int x = b.G; x < b.e[0] - b.G; ++x) {
569 const long i = b.id(x, y, z);
570 if (cl[i] == sv::kSvNone)
571 continue;
572 ++nFit;
573 nFb += fb[i];
574 const double e = std::max({std::fabs(t0[i] - tex[0]), std::fabs(t1[i] - tex[1]),
575 std::fabs(t2[i] - tex[2])});
576 wTau = std::max(wTau, e / (mulo * lmax));
577 wDnn = std::max(wDnn, std::fabs(dn[i] - dex) / lmax);
578 }
579 }
580 std::printf("(b) kinked-linear: %ld fits, tau %.2e, D_nn %.2e (relative), fallbacks %ld\n", nFit,
581 wTau, wDnn, nFb);
582 CHECK(nFit > 1000, "coverage");
583 CHECK(wTau <= 1e-11 && wDnn <= 1e-11, "kinked-linear exactness");
584 CHECK(nFb == 0, "fallbacks");
585}
586
587// ---------------------------------------------------------------------------------------------
588// (c) virtual planes: films and the ABAB stack, periodic box, colour only.
589void testVirtual() {
590 long nEdge = 0, nBad = 0, nPlaneBad = 0;
591 double wDist = 0;
592 for (int ax = 0; ax < 3; ++ax)
593 for (int stack = 0; stack < 2; ++stack) {
594 Blk b(6, 6, 6);
595 std::vector<double> col(b.cells3, 0.0), pm[3], pa(b.cells3, 0.0), v[3];
596 for (int a = 0; a < 3; ++a) {
597 pm[a].assign(b.cells3, 0.0);
598 v[a].assign(b.cells, 0.0);
599 }
600 // global inner coordinate along ax: X = x - G; the film is X == 2 (A, C = 1) in B; the
601 // stack alternates A, B starting with A at X = 0 (6 cells: periodic-consistent).
602 auto phase = [&](int X) {
603 const int w = ((X % 6) + 6) % 6;
604 return stack ? (w % 2 == 0) : (w == 2);
605 };
606 for (int z = -1; z <= b.e[2]; ++z)
607 for (int y = -1; y <= b.e[1]; ++y)
608 for (int x = -1; x <= b.e[0]; ++x) {
609 const int I[3] = {x, y, z};
610 col[b.id3(x, y, z)] = phase(I[ax] - b.G) ? 1.0 : 0.0;
611 }
612 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
613 const double ell[3] = {1.0, 1.0, 1.0};
614 Geo o = makeGeo(b, col, pm, pa, v, dlo, dhi, 1e-8, 0.01, 1.0, ell);
615 // planes: every cell with an opposite face-neighbour along ax is virtual on ax, low face
616 // first
617 std::vector<std::array<int, 3>> cq;
618 for (int X = 0; X < 6; ++X)
619 cq.push_back({ax == 0 ? X + b.G : 3, ax == 1 ? X + b.G : 3, ax == 2 ? X + b.G : 3});
620 auto cr = queryCells(o, cq);
621 for (int X = 0; X < 6; ++X) {
622 const bool me = phase(X), lo = phase(X - 1), hi = phase(X + 1);
623 int side = -1;
624 if (lo != me)
625 side = 0;
626 else if (hi != me)
627 side = 1;
628 const int kExp = side < 0 ? sv::kSvNone : sv::kSvVirtual0 + 2 * ax + side;
629 bool ok = (int)cr[X][0] == kExp;
630 if (ok && side >= 0) {
631 // sigma = +1 iff C = 1 lies at xi_ax < f, f = side
632 const double sg = (side == 0) ? (lo ? 1.0 : -1.0) : (me ? 1.0 : -1.0);
633 for (int a = 0; a < 3; ++a)
634 ok = ok && cr[X][1 + a] == (a == ax ? sg : 0.0);
635 ok = ok && cr[X][4] == sg * side;
636 }
637 nPlaneBad += ok ? 0 : 1;
638 }
639 // every edge on a phase face: (k_ax = face) for the two orientations containing ax
640 std::vector<EdgeQ> eq;
641 for (int X = 0; X <= 6; ++X) {
642 if (phase(X) == phase(X - 1))
643 continue; // not a phase face
644 for (int c = 0; c < 3; ++c) {
645 if (c == ax)
646 continue;
647 for (int p = 3; p <= 4; ++p)
648 for (int s = 3; s <= 4; ++s) {
649 EdgeQ q;
650 const int bb = 3 - ax - c;
651 q.k[ax] = X + b.G;
652 q.k[c] = p;
653 q.k[bb] = s;
654 q.a = std::min(ax, c);
655 q.c = std::max(ax, c);
656 eq.push_back(q);
657 }
658 }
659 }
660 auto er = queryEdges(o, eq);
661 for (const auto& r : er) {
662 ++nEdge;
663 if (r[0] < 1.0)
664 ++nBad;
665 else
666 wDist = std::max(wDist, r[1]);
667 }
668 }
669 std::printf(
670 "(c) virtual planes: %ld face edges, %ld ownerless, max owner distance %.2e, %ld bad "
671 "planes\n",
672 nEdge, nBad, wDist, nPlaneBad);
673 CHECK(nEdge > 0 && nBad == 0 && wDist == 0.0, "film-face edges owned at distance 0");
674 CHECK(nPlaneBad == 0, "virtual planes follow the low-face rule");
675}
676
677// ---------------------------------------------------------------------------------------------
678// (d) a periodic seam in the band: circular-shift invariance (bitwise).
679void testSeam() {
680 const int n = 8, sh[3] = {5, 3, 2};
681 std::mt19937 rng(11);
682 std::uniform_real_distribution<double> U(-1.0, 1.0);
683 std::vector<double> ug[3]; // global periodic face values, n^3
684 for (int c = 0; c < 3; ++c) {
685 ug[c].resize(n * n * n);
686 for (auto& x : ug[c])
687 x = U(rng);
688 }
689 auto wrap = [&](int i) { return ((i % n) + n) % n; };
690 // the slab 2.3 <= (X + Y) / sqrt(2) < 5.1 (45 deg; ties between the two planes' owners)
691 const double s2 = std::sqrt(0.5);
692 auto run = [&](int shift, std::vector<double>& tauOut, std::vector<int>& clsOut) {
693 Blk b(n, n, n);
694 std::vector<double> col(b.cells3), pm[3], pa(b.cells3, 0.0), v[3];
695 for (int a = 0; a < 3; ++a) {
696 pm[a].assign(b.cells3, 0.0);
697 v[a].assign(b.cells, 0.0);
698 }
699 for (int z = -1; z <= b.e[2]; ++z)
700 for (int y = -1; y <= b.e[1]; ++y)
701 for (int x = -1; x <= b.e[0]; ++x) {
702 // global cell of this local cell under the shift
703 const int X = wrap(x - b.G + shift * sh[0]), Y = wrap(y - b.G + shift * sh[1]);
704 const int W = (X + Y) % n; // the slab is a function of (X + Y) mod n on the torus
705 // two planes: phi1 = s2 (X + Y) - 2.3 (A below), phi2 = 5.1 - s2 (X + Y) (A above)
706 // -- A between them, on the periodic torus of period n in X + Y: use the cell-local
707 // planes of whichever plane cuts the cell
708 const long j = b.id3(x, y, z);
709 const double m[3] = {s2, s2, 0.0};
710 double C1 = sv::plicVolume(-m[0], -m[1], 0.0, -(2.3 - s2 * W)); // side phi1 > 0
711 double C2 = sv::plicVolume(m[0], m[1], 0.0, 5.1 - s2 * W); // side phi2 > 0
712 const double C = C1 + C2 - 1.0 < 0.0 ? 0.0 : C1 + C2 - 1.0;
713 col[j] = C;
714 if (C > 0.0 && C < 1.0) {
715 const bool first = C1 < 1.0;
716 for (int a = 0; a < 2; ++a)
717 pm[a][j] = first ? -m[a] : m[a];
718 pa[j] = first ? -(2.3 - s2 * W) : 5.1 - s2 * W;
719 }
720 }
721 for (int z = 0; z < b.e[2]; ++z)
722 for (int y = 0; y < b.e[1]; ++y)
723 for (int x = 0; x < b.e[0]; ++x)
724 for (int c = 0; c < 3; ++c) {
725 const int X = wrap(x - b.G + shift * sh[0]), Y = wrap(y - b.G + shift * sh[1]),
726 Z = wrap(z - b.G + shift * sh[2]);
727 v[c][b.id(x, y, z)] = ug[c][X + n * (Y + n * Z)];
728 }
729 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
730 const double ell[3] = {1.0, 1.0, 1.0};
731 Geo o = makeGeo(b, col, pm, pa, v, dlo, dhi, 1e-12, 0.01, 1.0, ell);
732 auto t0 = toHost(o.tau[0]), t1 = toHost(o.tau[1]), t2 = toHost(o.tau[2]), dn = toHost(o.dnn);
733 auto cl = toHost(o.cls), fb = toHost(o.fb);
734 tauOut.assign(4 * n * n * n, 0.0);
735 clsOut.assign(2 * n * n * n, 0);
736 for (int z = 0; z < n; ++z)
737 for (int y = 0; y < n; ++y)
738 for (int x = 0; x < n; ++x) {
739 const long i = b.id(x + b.G, y + b.G, z + b.G);
740 const int X = wrap(x + shift * sh[0]), Y = wrap(y + shift * sh[1]),
741 Z = wrap(z + shift * sh[2]);
742 const long gi = X + n * (Y + n * Z);
743 tauOut[4 * gi] = t0[i];
744 tauOut[4 * gi + 1] = t1[i];
745 tauOut[4 * gi + 2] = t2[i];
746 tauOut[4 * gi + 3] = dn[i];
747 clsOut[2 * gi] = cl[i];
748 clsOut[2 * gi + 1] = fb[i];
749 }
750 };
751 std::vector<double> ta, tb;
752 std::vector<int> ca, cb;
753 run(0, ta, ca);
754 run(1, tb, cb);
755 long diff = 0, band = 0;
756 for (std::size_t i = 0; i < ta.size(); ++i)
757 diff += (ta[i] != tb[i]);
758 for (std::size_t i = 0; i < ca.size(); ++i)
759 diff += (ca[i] != cb[i]);
760 for (std::size_t i = 0; i < ca.size(); i += 2)
761 band += (ca[i] != 0);
762 std::printf("(d) periodic seam: %ld band cells, %ld differing values under the shift\n", band,
763 diff);
764 CHECK(band > 50 && diff == 0, "shift invariance across the seam");
765}
766
767// ---------------------------------------------------------------------------------------------
768// Host oracle of the band geometry: an independent transcription of §5.2/§5.4 on host arrays.
769struct HostGeo {
770 const Blk* b;
771 std::vector<int> cls;
772 std::vector<double> col, pm[3], pa;
773 double eps, muA, muB, ell[3];
774 int cl(const int q[3]) const { return cls[b->id(q[0], q[1], q[2])]; }
775 double C(const int q[3]) const { return col[b->id3(q[0], q[1], q[2])]; }
776 double mu(double f) const { return muB + (muA - muB) * f; }
777 double res(double f) const { return f / muA + (1.0 - f) / muB; }
778 void plane(const int q[3], double m[3], double& al) const {
779 const int k = cl(q);
780 if (k == 1) {
781 const long j = b->id3(q[0], q[1], q[2]);
782 for (int a = 0; a < 3; ++a)
783 m[a] = pm[a][j];
784 al = pa[j];
785 return;
786 }
787 const int v = k - 2, ax = v / 2, side = v % 2;
788 const bool one = C(q) >= 1.0 - eps;
789 const double s = side == 0 ? (one ? -1.0 : 1.0) : (one ? 1.0 : -1.0);
790 m[0] = m[1] = m[2] = 0.0;
791 m[ax] = s;
792 al = side == 0 ? 0.0 : s;
793 }
794 void normal(const double m[3], double n[3]) const {
795 double p[3], s = 0;
796 for (int a = 0; a < 3; ++a) {
797 p[a] = m[a] / ell[a];
798 s += p[a] * p[a];
799 }
800 s = std::sqrt(s);
801 for (int a = 0; a < 3; ++a)
802 n[a] = p[a] / s;
803 }
804 // the four cells of E_ac(k), sorted by local linear index
805 std::vector<std::array<int, 3>> cells4(const int k[3], int a, int c) const {
806 std::vector<std::pair<long, std::array<int, 3>>> v;
807 for (int da = 0; da < 2; ++da)
808 for (int dc = 0; dc < 2; ++dc) {
809 std::array<int, 3> q = {k[0], k[1], k[2]};
810 q[a] -= da;
811 q[c] -= dc;
812 v.push_back({b->id(q[0], q[1], q[2]), q});
813 }
814 std::sort(v.begin(), v.end());
815 std::vector<std::array<int, 3>> o;
816 for (auto& x : v)
817 o.push_back(x.second);
818 return o;
819 }
820 // the owner set of E_ac(k) (§5.8): the interfacial cells whose plane distance from the edge
821 // midpoint is within 1e-6 of the closest, in local-linear-index order (empty: not a band edge)
822 struct Member {
823 int o[3];
824 double m[3], al;
825 };
826 std::vector<Member> owners(const int k[3], int a, int c) const {
827 std::vector<Member> all;
828 std::vector<double> d;
829 for (auto& q : cells4(k, a, c)) {
830 if (cl(q.data()) == 0)
831 continue;
832 Member mb;
833 plane(q.data(), mb.m, mb.al);
834 double xi[3];
835 for (int s = 0; s < 3; ++s) {
836 mb.o[s] = q[s];
837 xi[s] = k[s] - q[s];
838 }
839 xi[3 - a - c] = 0.5;
840 const double* mm = mb.m;
841 d.push_back(std::fabs(mm[0] * xi[0] + mm[1] * xi[1] + mm[2] * xi[2] - mb.al) /
842 std::sqrt(mm[0] * mm[0] + mm[1] * mm[1] + mm[2] * mm[2]));
843 all.push_back(mb);
844 }
845 std::vector<Member> o;
846 if (all.empty())
847 return o;
848 const double dmin = *std::min_element(d.begin(), d.end());
849 for (std::size_t t = 0; t < all.size(); ++t)
850 if (d[t] <= dmin + 1e-6)
851 o.push_back(all[t]);
852 return o;
853 }
854 // box x box fraction in the plane x_p = xiP over [lo_s, lo_s + 1] on the two other axes s
855 static double boxFrac(const double m[3], double al, int p, double xiP, const double lo[3]) {
856 const int q = p == 0 ? 1 : 0, bb = p == 2 ? 1 : 2;
857 return sv::plicVolume(m[q], m[bb], 0.0, al - m[p] * xiP - m[q] * lo[q] - m[bb] * lo[bb]);
858 }
859 // hat (half-width 1, centred qc on axis q) x box (b from b0) in the plane x_p = xiP
860 static double hatFrac(const double m[3], double al, int p, double xiP, int q, double qc,
861 double b0) {
862 const int bb = 3 - p - q;
863 return sv::plicVolume(m[q], m[q], m[bb], al - m[p] * xiP - m[q] * (qc - 1.0) - m[bb] * b0);
864 }
865 // row r's face at E_ac(k)
866 double rowFace(const int k[3], int a, int c, int r, const int o[3], const double m[3],
867 double al) const {
868 const int p = (r == c) ? a : c, bb = 3 - a - c;
869 double lo[3];
870 lo[r] = k[r] - o[r] - 0.5;
871 lo[bb] = k[bb] - o[bb];
872 lo[p] = 0;
873 return boxFrac(m, al, p, k[p] - o[p], lo);
874 }
875 double link(const int k[3], int dd, int cc, const int o[3], const double m[3], double al) const {
876 return hatFrac(m, al, cc, k[cc] - o[cc], dd, k[dd] - o[dd], k[3 - dd - cc] - o[3 - dd - cc]);
877 }
878};
879
880HostGeo hostGeo(const Blk& b, const Geo& o, double eps, double muA, double muB,
881 const double ell[3]) {
882 HostGeo h;
883 h.b = &b;
884 h.cls = toHost(o.cls);
885 h.col = toHost(o.col);
886 for (int a = 0; a < 3; ++a) {
887 h.pm[a] = toHost(o.pm[a]);
888 h.ell[a] = ell[a];
889 }
890 h.pa = toHost(o.pa);
891 h.eps = eps;
892 h.muA = muA;
893 h.muB = muB;
894 return h;
895}
896
897// A sphere of tangent planes (one plane per cut cell, so owners choose between planes).
898// Centre `cen` (internal coordinates) and radius R; by default the (e)/(f) sphere.
899void sphereGeom(const Blk& b, const double ell[3], std::vector<double>& col,
900 std::vector<double> (&pm)[3], std::vector<double>& pa,
901 const double* centre = nullptr, double R = 3.1) {
902 const double cen[3] = {centre ? centre[0] : 7.3 * ell[0], centre ? centre[1] : 6.7 * ell[1],
903 centre ? centre[2] : 7.1 * ell[2]};
904 col.assign(b.cells3, 0.0);
905 pa.assign(b.cells3, 0.0);
906 for (int a = 0; a < 3; ++a)
907 pm[a].assign(b.cells3, 0.0);
908 for (int z = -1; z <= b.e[2]; ++z)
909 for (int y = -1; y <= b.e[1]; ++y)
910 for (int x = -1; x <= b.e[0]; ++x) {
911 const int I[3] = {x, y, z};
912 double n[3], s = 0;
913 for (int a = 0; a < 3; ++a) {
914 n[a] = ell[a] * (I[a] + 0.5) - cen[a];
915 s += n[a] * n[a];
916 }
917 s = std::sqrt(s);
918 double m[3], al = R;
919 for (int a = 0; a < 3; ++a) {
920 n[a] /= s;
921 m[a] = n[a] * ell[a];
922 al += n[a] * cen[a] - m[a] * I[a];
923 }
924 const long j = b.id3(x, y, z);
925 const double C = sv::plicVolume(m[0], m[1], m[2], al);
926 col[j] = C;
927 if (C > 1e-12 && C < 1.0 - 1e-12) {
928 for (int a = 0; a < 3; ++a)
929 pm[a][j] = m[a];
930 pa[j] = al;
931 }
932 }
933}
934
935DV muField(const Blk& b, const std::vector<double>& col, double muA, double muB) {
936 std::vector<double> mu(b.cells);
937 for (int z = 0; z < b.e[2]; ++z)
938 for (int y = 0; y < b.e[1]; ++y)
939 for (int x = 0; x < b.e[0]; ++x)
940 mu[b.id(x, y, z)] = muB + (muA - muB) * col[b.id3(x, y, z)];
941 return toDev(mu, "mu");
942}
943
944struct Bands {
945 DV A[7]; // AC, AW, AE, AS, AN, AB, AT
946 explicit Bands(long n) {
947 for (auto& x : A)
948 x = DV("band", n);
949 }
950};
951template <class FP>
952std::vector<std::vector<double>> buildBands(const Blk& b, FP fp, const double w[3]) {
953 Bands B(b.cells);
954 ibmBuildDiffusionVar(B.A[0], B.A[1], B.A[2], B.A[3], B.A[4], B.A[5], B.A[6], b.e[0], b.e[1],
955 b.e[2], b.G, fp, w[0], w[1], w[2]);
956 Kokkos::fence();
957 std::vector<std::vector<double>> o;
958 for (auto& x : B.A)
959 o.push_back(toHost(x));
960 return o;
961}
962
963sv::SharpFaceProps sharpFP(const Geo& o, DV mu, int c, double rhoIdt,
966 base.haveMu = true;
967 base.mu = CCConst(mu);
968 base.msc = o.g.st[c];
969 base.rhoIdtC = rhoIdt;
971 sp.base = base;
972 sp.geo = o.g;
973 sp.c = c;
974 sp.role = role;
975 return sp;
976}
977
978// (e) WO-3: the implicit band. WO-5b (§6.1): the cell link along c of an interfacial cell carries
979// 2μ(C) in the implicit role and 0 in the explicit one; implicit + explicit = 2 × 'averaged' there
980// and on every other cell link both roles equal 'averaged'.
981void testBand() {
982 long nBand = 0, nTie = 0, nNon = 0, badNon = 0, badSym = 0, badM = 0, badCell = 0, nCellIf = 0,
983 badSplit = 0;
984 double wOr = 0;
985 for (int an = 0; an < 2; ++an)
986 for (double ratio : {1e-2, 1e3}) {
987 Blk b(12, 12, 12);
988 double ell[3] = {1.0, 1.0, 1.0};
989 if (an) {
990 ell[1] = 1.5;
991 ell[2] = 2.0;
992 }
993 const double w[3] = {1.0 / (ell[0] * ell[0]), 1.0 / (ell[1] * ell[1]),
994 1.0 / (ell[2] * ell[2])};
995 std::vector<double> col, pm[3], pa, v[3];
996 sphereGeom(b, ell, col, pm, pa);
997 for (auto& x : v)
998 x.assign(b.cells, 0.0);
999 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
1000 const double muA = ratio, muB = 1.0;
1001 Geo o = makeGeo(b, col, pm, pa, v, dlo, dhi, 1e-12, muA, muB, ell);
1002 HostGeo hg = hostGeo(b, o, 1e-12, muA, muB, ell);
1003 DV mu = muField(b, col, muA, muB);
1004 for (int c = 0; c < 3; ++c) {
1005 sv::SharpFaceProps sp = sharpFP(o, mu, c, 0.7);
1006 auto Av = buildBands(b, sp.base, w);
1007 auto As = buildBands(b, sp, w);
1008 auto Ae = buildBands(b, sharpFP(o, mu, c, 0.7, sv::SvRole::kExplicit), w);
1009 for (int z = b.G; z < b.e[2] - b.G; ++z)
1010 for (int y = b.G; y < b.e[1] - b.G; ++y)
1011 for (int x = b.G; x < b.e[0] - b.G; ++x) {
1012 const long i = b.id(x, y, z);
1013 bool rowBand = false, rowCell = false;
1014 double sum = 0;
1015 for (int f = 0; f < 6; ++f) {
1016 const int a = f / 2, sg = f % 2; // AW(-x) AE(+x) AS AN AB AT
1017 const double sv_ = As[1 + f][i], av = Av[1 + f][i];
1018 sum += -sv_;
1019 badM += (sv_ > 0.0);
1020 if (a == c) {
1021 int q[3] = {x, y, z}; // the cell between the two u_c faces: the lower one
1022 if (!sg)
1023 q[c] -= 1;
1024 const bool itf = hg.cl(q) != 0;
1025 nCellIf += itf;
1026 rowCell = rowCell || itf;
1027 badCell += (sv_ != (itf ? 2.0 * av : av));
1028 badSplit += (sv_ + Ae[1 + f][i] != 2.0 * av) || (!itf && Ae[1 + f][i] != av) ||
1029 (itf && Ae[1 + f][i] != 0.0);
1030 continue;
1031 }
1032 badSplit += (Ae[1 + f][i] != sv_);
1033 int k[3] = {x, y, z};
1034 if (sg)
1035 k[a] += 1;
1036 const auto ow = hg.owners(k, a, c);
1037 if (ow.empty()) {
1038 ++nNon;
1039 badNon += (sv_ != av);
1040 continue;
1041 }
1042 ++nBand;
1043 nTie += ow.size() > 1;
1044 rowBand = true;
1045 double F = 0.0;
1046 for (const auto& mb : ow)
1047 F += hg.rowFace(k, a, c, c, mb.o, mb.m, mb.al);
1048 const double ex = -hg.mu(F / ow.size()) * w[a];
1049 wOr = std::max(wOr, std::fabs(sv_ - ex) / std::fabs(ex));
1050 }
1051 if (!rowBand && !rowCell)
1052 badCell += (As[0][i] != Av[0][i]);
1053 badM += !(std::fabs(As[0][i] - 0.7 - sum) <= 1e-13 * As[0][i]);
1054 // per-component symmetry with the +a neighbour (inner)
1055 for (int a = 0; a < 3; ++a) {
1056 int q[3] = {x, y, z};
1057 q[a] += 1;
1058 if (q[a] >= b.e[a] - b.G)
1059 continue;
1060 badSym += (As[2 + 2 * a][i] != As[1 + 2 * a][b.id(q[0], q[1], q[2])]);
1061 }
1062 }
1063 }
1064 }
1065 std::printf(
1066 "(e) implicit band: %ld band entries (%ld tied; oracle %.2e), %ld non-band (%ld differ), "
1067 "cells/"
1068 "diag %ld differ, symmetry %ld, M-matrix %ld; split: %ld interfacial cell links, %ld "
1069 "wrong\n",
1070 nBand, nTie, wOr, nNon, badNon, badCell, badSym, badM, nCellIf, badSplit);
1071 CHECK(nBand > 500 && wOr <= 1e-14, "band entries vs oracle");
1072 CHECK(badNon == 0 && badCell == 0, "non-band entries bitwise 'averaged'");
1073 CHECK(badSym == 0 && badM == 0, "symmetric M-matrix");
1074 CHECK(nCellIf > 100 && badSplit == 0,
1075 "WO-5b split: 2mu(C) implicit, 0 explicit on interfacial cells");
1076}
1077
1078// Host transcription of the explicit band terms of the u_c rows (§5.4): sample arrays, then the
1079// divergence.
1080double hostCellExtra(const HostGeo& h, const int q[3], int c, const std::vector<double> (&v)[3],
1081 const std::vector<double> (&tau)[3], const std::vector<double>& dnn) {
1082 if (h.cl(q) == 0)
1083 return 0.0;
1084 double m[3], al, n[3];
1085 h.plane(q, m, al);
1086 h.normal(m, n);
1087 double lo[3] = {0, 0, 0};
1088 const double cb = HostGeo::boxFrac(m, al, c, 0.5, lo), C = h.C(q);
1089 const long j = h.b->id(q[0], q[1], q[2]);
1090 const double mcc = v[c][j + h.b->st[c]] - v[c][j];
1091 return 2.0 * (h.muA - h.muB) * (cb - C) * (mcc - dnn[j]) +
1092 2.0 * n[c] * tau[c][j] * (1.0 - h.mu(cb) * h.res(C));
1093}
1094double hostEdgeY(const HostGeo& h, const int k[3], int a, int c,
1095 const std::vector<double> (&tau)[3]) {
1096 const auto ow = h.owners(k, a, c);
1097 if (ow.empty())
1098 return 0.0;
1099 double t[3] = {0, 0, 0};
1100 int nI = 0;
1101 for (auto& q : h.cells4(k, a, c))
1102 nI += h.cl(q.data()) != 0;
1103 for (auto& q : h.cells4(k, a, c))
1104 if (h.cl(q.data()) != 0)
1105 for (int s = 0; s < 3; ++s)
1106 t[s] += tau[s][h.b->id(q[0], q[1], q[2])] / nI;
1107 double y = 0.0;
1108 for (const auto& mb : ow) {
1109 double n[3];
1110 h.normal(mb.m, n);
1111 const double mub = h.mu(h.rowFace(k, a, c, c, mb.o, mb.m, mb.al));
1112 const double rac = h.res(h.link(k, a, c, mb.o, mb.m, mb.al)),
1113 rca = h.res(h.link(k, c, a, mb.o, mb.m, mb.al));
1114 y += n[a] * t[c] * (1.0 - mub * rac) + n[c] * t[a] * (1.0 - mub * rca);
1115 }
1116 return y / ow.size();
1117}
1118
1119// (h) WO-9.2 (§5.8): reflection self-symmetry of the owner-set mean. A sphere of radius 5.3 cells
1120// centred on a grid vertex of a 24^3 periodic box (tangent planes), mu ratio 1e3, u^n the pure
1121// strain u = (X, Y, -2Z) about the centre (wrapped at the box faces: its jump lies outside every
1122// fit block). For each axis reflection R about the centre and every band edge E,
1123// |G(E) - s G(R E)| <= 1e-13 max|G| for G in {F_a, F_c, th_ac, th_ca, T_a, T_c}; s = -1 for T
1124// iff R reflects a or c. The case ties (census), and n_E is reflection-invariant.
1125void testReflection() {
1126 Blk b(24, 24, 24);
1127 const double ell[3] = {1.0, 1.0, 1.0};
1128 const int X0 = b.G + 12; // the centre, a grid vertex (solver index coordinates)
1129 const double cen[3] = {(double)X0, (double)X0, (double)X0};
1130 std::vector<double> col, pm[3], pa, v[3];
1131 sphereGeom(b, ell, col, pm, pa, cen, 5.3);
1132 for (auto& x : v)
1133 x.assign(b.cells, 0.0);
1134 for (int z = 0; z < b.e[2]; ++z)
1135 for (int y = 0; y < b.e[1]; ++y)
1136 for (int x = 0; x < b.e[0]; ++x) {
1137 const int q[3] = {x, y, z};
1138 for (int a = 0; a < 3; ++a) {
1139 const double r = (double)(((q[a] - b.G) % 24 + 24) % 24) - 12.0; // face a - centre
1140 v[a][b.id(x, y, z)] = (a == 2) ? -2.0 * r : r;
1141 }
1142 }
1143 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
1144 Geo o = makeGeo(b, col, pm, pa, v, dlo, dhi, 1e-12, 1e3, 1.0, ell);
1145 const auto census =
1146 sv::sharpCensus(o.g, o.fb, C3{b.G, b.G, b.G}, C3{b.e[0] - b.G, b.e[1] - b.G, b.e[2] - b.G});
1147 DV out("refl", 21 * b.cells);
1148 const sv::SharpGeom g = o.g;
1149 const CCConst tc[3] = {o.tau[0], o.tau[1], o.tau[2]};
1150 const int G = b.G;
1151 Kokkos::parallel_for(
1152 "test::reflection", Kokkos::RangePolicy<CCExec>(0, b.cells), KOKKOS_LAMBDA(long i) {
1153 const int z = (int)(i / g.st[2]), rr = (int)(i - z * g.st[2]), y = rr / (int)g.st[1],
1154 x = rr - y * (int)g.st[1];
1155 for (int ac = 0; ac < 3; ++ac)
1156 out(21 * i + 7 * ac) = 0.0;
1157 if (x < G || y < G || z < G || x >= g.e[0] - G || y >= g.e[1] - G || z >= g.e[2] - G)
1158 return;
1159 const int k[3] = {x, y, z};
1160 for (int ac = 0; ac < 3; ++ac) {
1161 const int a = (ac == 2) ? 1 : 0, c = (ac == 0) ? 1 : 2;
1162 const long o7 = 21 * i + 7 * ac;
1163 const double W =
1164 g.forEachOwner(k, a, c, [](const int*, const double*, double, double) {});
1165 if (W == 0.0)
1166 continue;
1167 double sp = 0.0, gp = 0.0;
1168 bool bl = false;
1169 out(o7) = g.edgeTieKeys(k, ac, sp, gp, bl); // n_E: the plateau members (census)
1170 double f = 0.0, tac = 0.0, tca = 0.0;
1171 g.edgeFaceFraction(k, a, c, a, f);
1172 out(o7 + 1) = f;
1173 g.edgeFaceFraction(k, a, c, c, f);
1174 out(o7 + 2) = f;
1175 g.edgeLinkFractions(k, a, c, tac, tca);
1176 out(o7 + 3) = tac;
1177 out(o7 + 4) = tca;
1178 out(o7 + 5) = g.edgeTraction(k, a, c, a, tc);
1179 out(o7 + 6) = g.edgeTraction(k, a, c, c, tc);
1180 }
1181 });
1182 const auto h = toHost(out);
1183 double mx[6] = {0, 0, 0, 0, 0, 0}, wd[6] = {0, 0, 0, 0, 0, 0};
1184 long nBand = 0, nTie = 0, badN = 0;
1185 for (int r = 0; r < 3; ++r)
1186 for (int z = G; z < b.e[2] - G; ++z)
1187 for (int y = G; y < b.e[1] - G; ++y)
1188 for (int x = G; x < b.e[0] - G; ++x)
1189 for (int ac = 0; ac < 3; ++ac) {
1190 const int a = (ac == 2) ? 1 : 0, c = (ac == 0) ? 1 : 2, bb = 3 - a - c;
1191 const double* e = &h[21 * b.id(x, y, z) + 7 * ac];
1192 if (e[0] == 0.0)
1193 continue;
1194 int kr[3] = {x, y, z};
1195 kr[r] = (r == bb) ? 2 * X0 - 1 - kr[r] : 2 * X0 - kr[r]; // the reflected edge
1196 const double* f = &h[21 * b.id(kr[0], kr[1], kr[2]) + 7 * ac];
1197 if (r == 0) {
1198 ++nBand;
1199 nTie += e[0] > 1.0;
1200 }
1201 if (f[0] != e[0]) {
1202 ++badN;
1203 continue;
1204 }
1205 for (int q = 0; q < 6; ++q) {
1206 const double s = (q >= 4 && r != bb) ? -1.0 : 1.0;
1207 mx[q] = std::max(mx[q], std::fabs(e[1 + q]));
1208 wd[q] = std::max(wd[q], std::fabs(e[1 + q] - s * f[1 + q]));
1209 }
1210 }
1211 double worst = 0.0;
1212 for (int q = 0; q < 6; ++q)
1213 worst = std::max(worst, mx[q] > 0.0 ? wd[q] / mx[q] : 0.0);
1214 std::printf(
1215 "(h) reflection self-symmetry: %ld band edges, %ld tied (census %ld, spread %.2e, gap "
1216 "%.2e); worst |G(E) - s G(RE)| / max|G|: F_a %.1e F_c %.1e th_ac %.1e th_ca %.1e T_a %.1e "
1217 "T_c %.1e; n_E mismatches %ld; fallbacks %ld\n",
1218 nBand, nTie, census.tied, census.tieSpread, census.untiedGap, wd[0] / mx[0], wd[1] / mx[1],
1219 wd[2] / mx[2], wd[3] / mx[3], wd[4] / mx[4], wd[5] / mx[5], badN, census.fallback);
1220 CHECK(nBand > 1000 && nTie > 0 && census.tied == nTie, "a band with ties (%ld / %ld)", nTie,
1221 census.tied);
1222 CHECK(badN == 0, "n_E reflection-invariant");
1223 CHECK(worst <= 1e-13, "reflection self-symmetry %.2e", worst);
1224}
1225
1226// (j) §5.9 item 7: the owner weight -- plateau, C¹ ramp, zero; evaluated in the execution space.
1227void testOwnerWeight() {
1228 constexpr double t0 = sv::kSvOwnerTieTol, t1 = sv::kSvOwnerRampTol;
1229 constexpr int nS = 100000;
1230 const double K = 1.5 / (t1 - t0), hh = 1e-9;
1231 DV out("ownerWeight", nS + 1 + 7);
1232 Kokkos::parallel_for(
1233 "test::ownerWeight", Kokkos::RangePolicy<CCExec>(0, nS + 1), KOKKOS_LAMBDA(int i) {
1234 out(i) = sv::ownerWeight(2.0 * t1 * (double)i / (double)nS);
1235 if (i == 0) {
1236 out(nS + 1) = sv::ownerWeight(0.0);
1237 out(nS + 2) = sv::ownerWeight(t0);
1238 out(nS + 3) = sv::ownerWeight(t1);
1239 out(nS + 4) = sv::ownerWeight(2.0 * t1);
1240 out(nS + 5) = sv::ownerWeight(Kokkos::Experimental::quiet_NaN<double>::value);
1241 out(nS + 6) = sv::ownerWeight(t0 + hh);
1242 out(nS + 7) = sv::ownerWeight(t1 - hh);
1243 }
1244 });
1245 const auto h = toHost(out);
1246 long nInc = 0;
1247 double slope = 0.0;
1248 for (int i = 0; i < nS; ++i) {
1249 nInc += h[i + 1] > h[i];
1250 slope = std::max(slope, std::fabs(h[i + 1] - h[i]) / (2.0 * t1 / nS));
1251 }
1252 const double e2 = 3.0 * (hh / (t1 - t0)) * (hh / (t1 - t0));
1253 std::printf(
1254 "(j) owner weight: w(0) %g w(t0) %g w(t1) %g w(2t1) %g w(NaN) %g; %ld increases on %d "
1255 "points, max slope %.6e (K %.6e); 1 - w(t0 + h) %.3e, w(t1 - h) %.3e (bound %.3e)\n",
1256 h[nS + 1], h[nS + 2], h[nS + 3], h[nS + 4], h[nS + 5], nInc, nS + 1, slope, K,
1257 1.0 - h[nS + 6], h[nS + 7], e2);
1258 CHECK(h[nS + 1] == 1.0 && h[nS + 2] == 1.0, "w = 1 exactly on the plateau");
1259 CHECK(h[nS + 3] == 0.0 && h[nS + 4] == 0.0 && h[nS + 5] == 0.0, "w = 0 from t1 on and at NaN");
1260 CHECK(nInc == 0, "w non-increasing (%ld increases)", nInc);
1261 CHECK(slope <= K * (1.0 + 1e-9), "|dw/dg| <= K (%.6e)", slope);
1262 CHECK(std::fabs(1.0 - h[nS + 6]) <= e2 && std::fabs(h[nS + 7]) <= e2, "C1 at both ends");
1263}
1264
1265// FNV-1a over the bytes of a double array (the tie-free identity's digest).
1266std::uint64_t fnv(std::uint64_t h, const std::vector<double>& x) {
1267 for (double d : x) {
1268 std::uint64_t u;
1269 std::memcpy(&u, &d, sizeof u);
1270 for (int s = 0; s < 8; ++s) {
1271 h ^= (u >> (8 * s)) & 0xffu;
1272 h *= 0x100000001b3ULL;
1273 }
1274 }
1275 return h;
1276}
1277
1278// (i) WO-9.2 (§5.8): a tie-free case is bitwise the single-owner operator of f01a251 (host). The
1279// (e)/(f) sphere at mu ratio 1e3 and h ratio 1 : 1.5 : 2, random u^n: its census reads 0 tied
1280// edges (the isotropic sphere has 7: not this case), and every band-op output -- the fit (tau_hat,
1281// D_nn, fallbacks), the implicit and the explicit bands of the three components, the explicit band
1282// RHS -- is hashed (FNV-1a over the bytes). The digests were recorded once from the f01a251
1283// operator (the WO-9.1 tree, host OpenMP, 2026-11-04). On a device backend FMA contraction may
1284// differ: the digest is printed, not checked.
1285void testTieFree() {
1286 const std::uint64_t want = 0x453f2865e4cc0381ULL;
1287 constexpr bool host = Kokkos::SpaceAccessibility<Kokkos::HostSpace, CCMem>::accessible;
1288 {
1289 Blk b(12, 12, 12);
1290 const double ell[3] = {1.0, 1.5, 2.0};
1291 const double w[3] = {1.0 / (ell[0] * ell[0]), 1.0 / (ell[1] * ell[1]), 1.0 / (ell[2] * ell[2])};
1292 std::vector<double> col, pm[3], pa, v[3];
1293 sphereGeom(b, ell, col, pm, pa);
1294 std::mt19937 rng(11);
1295 std::uniform_real_distribution<double> U(-1.0, 1.0);
1296 for (auto& x : v) {
1297 x.resize(b.cells);
1298 for (auto& y : x)
1299 y = U(rng);
1300 }
1301 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
1302 const double muA = 1e3, muB = 1.0;
1303 Geo o = makeGeo(b, col, pm, pa, v, dlo, dhi, 1e-12, muA, muB, ell);
1304 const auto cen =
1305 sv::sharpCensus(o.g, o.fb, C3{b.G, b.G, b.G}, C3{b.e[0] - b.G, b.e[1] - b.G, b.e[2] - b.G});
1306 std::uint64_t h = 0xcbf29ce484222325ULL;
1307 for (int a = 0; a < 3; ++a)
1308 h = fnv(h, toHost(o.tau[a]));
1309 h = fnv(h, toHost(o.dnn));
1310 {
1311 const auto fb = toHost(o.fb);
1312 h = fnv(h, std::vector<double>(fb.begin(), fb.end()));
1313 }
1314 DV mu = muField(b, col, muA, muB);
1315 for (int c = 0; c < 3; ++c) {
1316 for (const auto& x : buildBands(b, sharpFP(o, mu, c, 0.7), w))
1317 h = fnv(h, x);
1318 for (const auto& x : buildBands(b, sharpFP(o, mu, c, 0.7, sv::SvRole::kExplicit), w))
1319 h = fnv(h, x);
1320 }
1321 DV rd("rs", b.cells);
1322 Kokkos::deep_copy(rd, 1.0);
1323 const CCConst tc[3] = {o.tau[0], o.tau[1], o.tau[2]}, vc[3] = {o.v[0], o.v[1], o.v[2]};
1324 for (int c = 0; c < 3; ++c) {
1325 DV bb("bb", b.cells);
1326 sv::addSharpViscousRhs(c, bb, CCConst(rd), vc, o.g, tc, CCConst(o.dnn), b.G, w[c], false);
1327 Kokkos::fence();
1328 h = fnv(h, toHost(bb));
1329 }
1330 std::printf("(i) tie-free identity: %ld band cells, %ld tied edges, digest 0x%016llx%s\n",
1331 cen.band, cen.tied, (unsigned long long)h,
1332 host ? (h == want ? " (= f01a251)" : " (MISMATCH)") : " (device: not checked)");
1333 CHECK(cen.band > 50 && cen.tied == 0, "a tie-free case");
1334 if (host)
1335 CHECK(h == want, "tie-free band-op outputs bitwise f01a251");
1336 }
1337}
1338
1339// (f) WO-4: the explicit kernel against the host transcription.
1340void testRhsOracle() {
1341 std::mt19937 rng(5);
1342 std::uniform_real_distribution<double> U(-1.0, 1.0);
1343 double worst = 0;
1344 long nRow = 0;
1345 for (int an = 0; an < 2; ++an)
1346 for (double ratio : {1e-2, 1e3}) {
1347 Blk b(12, 12, 12);
1348 double ell[3] = {1.0, 1.0, 1.0};
1349 if (an) {
1350 ell[1] = 1.5;
1351 ell[2] = 2.0;
1352 }
1353 std::vector<double> col, pm[3], pa, v[3], tau[3], dnn(b.cells), rs(b.cells);
1354 sphereGeom(b, ell, col, pm, pa);
1355 for (int a = 0; a < 3; ++a) {
1356 v[a].resize(b.cells);
1357 tau[a].resize(b.cells);
1358 for (long i = 0; i < b.cells; ++i) {
1359 v[a][i] = U(rng);
1360 tau[a][i] = U(rng);
1361 }
1362 }
1363 for (long i = 0; i < b.cells; ++i) {
1364 dnn[i] = U(rng);
1365 rs[i] = 1.0 + 0.25 * U(rng);
1366 }
1367 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
1368 const double muA = ratio, muB = 1.0;
1369 Geo o = makeGeo(b, col, pm, pa, v, dlo, dhi, 1e-12, muA, muB, ell);
1370 HostGeo hg = hostGeo(b, o, 1e-12, muA, muB, ell);
1371 DV td[3] = {toDev(tau[0]), toDev(tau[1]), toDev(tau[2])}, dd = toDev(dnn), rd = toDev(rs);
1372 const CCConst tc[3] = {td[0], td[1], td[2]}, vc[3] = {o.v[0], o.v[1], o.v[2]};
1373 for (int c = 0; c < 3; ++c) {
1374 DV bb("bb", b.cells);
1375 const double wc = 1.0 / (ell[c] * ell[c]);
1376 sv::addSharpViscousRhs(c, bb, CCConst(rd), vc, o.g, tc, CCConst(dd), b.G, wc, false);
1377 Kokkos::fence();
1378 auto bh = toHost(bb);
1379 const int a1 = c == 0 ? 1 : 0, a2 = c == 2 ? 1 : 2;
1380 double mx = 0, md = 0;
1381 for (int z = b.G; z < b.e[2] - b.G; ++z)
1382 for (int y = b.G; y < b.e[1] - b.G; ++y)
1383 for (int x = b.G; x < b.e[0] - b.G; ++x) {
1384 const int q[3] = {x, y, z};
1385 int qm[3] = {x, y, z}, p1[3] = {x, y, z}, p2[3] = {x, y, z};
1386 qm[c] -= 1;
1387 p1[a1] += 1;
1388 p2[a2] += 1;
1389 const double X =
1390 hostCellExtra(hg, q, c, v, tau, dnn) - hostCellExtra(hg, qm, c, v, tau, dnn);
1391 const double Y1 = hostEdgeY(hg, p1, a1, c, tau) - hostEdgeY(hg, q, a1, c, tau);
1392 const double Y2 = hostEdgeY(hg, p2, a2, c, tau) - hostEdgeY(hg, q, a2, c, tau);
1393 const long i = b.id(x, y, z);
1394 const double ex =
1395 rs[i] * (wc * X + Y1 / (ell[a1] * ell[c]) + Y2 / (ell[a2] * ell[c]));
1396 mx = std::max(mx, std::fabs(ex));
1397 md = std::max(md, std::fabs(bh[i] - ex));
1398 nRow += (ex != 0.0);
1399 }
1400 worst = std::max(worst, md / mx);
1401 }
1402 }
1403 std::printf("(f) explicit kernel vs host oracle: %ld nonzero rows, worst %.2e relative\n", nRow,
1404 worst);
1405 CHECK(nRow > 500 && worst <= 1e-13, "explicit kernel vs host oracle");
1406}
1407
1408// (g) WO-4: the full operator on exact face averages of kinked-linear fields (Couette and
1409// extension, 9 orientations) -- implicit band + band transpose + tau-terms + midsection - grad p.
1410// Review F3: the fit fallback. The normal equations depend on the geometry only (a NaN VELOCITY
1411// datum gives a non-finite tau_hat, not a fallback), so a NaN in the PLIC normal of one
1412// interfacial cell q* is what makes its fit singular (fitSolve's pivot test is NaN-safe): q* and
1413// only interfacial cells within the fit's reach fall back, each with tau_hat = D_nn = 0 exactly,
1414// every other fit stays finite, the census counts the fallbacks, and the band terms on the CLEAN
1415// geometry with that fit are finite (a fallback's tau_hat = 0 enters the edge mean, §5.2).
1416void testFallback() {
1417 Blk b(12, 12, 12);
1418 const double ell[3] = {1.0, 1.0, 1.0};
1419 std::vector<double> col, pm[3], pa, v[3];
1420 sphereGeom(b, ell, col, pm, pa);
1421 std::mt19937 rng(11);
1422 std::uniform_real_distribution<double> U(-1.0, 1.0);
1423 for (int a = 0; a < 3; ++a) {
1424 v[a].resize(b.cells);
1425 for (long i = 0; i < b.cells; ++i)
1426 v[a][i] = U(rng);
1427 }
1428 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
1429 const double muA = 1e-2, muB = 1.0;
1430 Geo clean = makeGeo(b, col, pm, pa, v, dlo, dhi, 1e-12, muA, muB, ell);
1431 const auto cls = toHost(clean.cls);
1432 const auto fb0 = toHost(clean.fb);
1433 long qs = -1, nFb0 = 0;
1434 for (int z = b.G; z < b.e[2] - b.G; ++z)
1435 for (int y = b.G; y < b.e[1] - b.G; ++y)
1436 for (int x = b.G; x < b.e[0] - b.G; ++x) {
1437 const long i = b.id(x, y, z);
1438 nFb0 += fb0[i];
1439 if (qs < 0 && cls[i] == sv::kSvPlane && x > 5 && y > 5 && z > 5)
1440 qs = i;
1441 }
1442 CHECK(qs >= 0 && nFb0 == 0, "an interior plane cell (%ld), no fallback on the clean field (%ld)",
1443 qs, nFb0);
1444 if (qs < 0)
1445 return;
1446 const int qz = (int)(qs / b.st[2]), qy = (int)((qs - qz * b.st[2]) / b.st[1]),
1447 qx = (int)(qs - qz * b.st[2] - qy * b.st[1]);
1448 std::vector<double> pmn[3] = {pm[0], pm[1], pm[2]};
1449 pmn[0][b.id3(qx, qy, qz)] = std::numeric_limits<double>::quiet_NaN();
1450 Geo o = makeGeo(b, col, pmn, pa, v, dlo, dhi, 1e-12, muA, muB, ell);
1451 const auto fb = toHost(o.fb);
1452 const std::vector<double> tau[3] = {toHost(o.tau[0]), toHost(o.tau[1]), toHost(o.tau[2])};
1453 const auto dnn = toHost(o.dnn);
1454 long nFb = 0, nFar = 0, nNonZero = 0, nNonFinite = 0, reach = 0;
1455 for (int z = b.G; z < b.e[2] - b.G; ++z)
1456 for (int y = b.G; y < b.e[1] - b.G; ++y)
1457 for (int x = b.G; x < b.e[0] - b.G; ++x) {
1458 const long i = b.id(x, y, z);
1459 const bool f = fb[i] != 0;
1460 nFb += f;
1461 const int d = std::max({std::abs(x - qx), std::abs(y - qy), std::abs(z - qz)});
1462 if (f) {
1463 reach = std::max<long>(reach, d);
1464 nFar += (d > 2 || cls[i] == sv::kSvNone);
1465 nNonZero += (tau[0][i] != 0.0 || tau[1][i] != 0.0 || tau[2][i] != 0.0 || dnn[i] != 0.0);
1466 } else
1467 nNonFinite += !(std::isfinite(tau[0][i]) && std::isfinite(tau[1][i]) &&
1468 std::isfinite(tau[2][i]) && std::isfinite(dnn[i]));
1469 }
1470 const auto cen =
1471 sv::sharpCensus(o.g, o.fb, C3{b.G, b.G, b.G}, C3{b.e[0] - b.G, b.e[1] - b.G, b.e[2] - b.G});
1472 long nBad = 0;
1473 {
1474 const CCConst tc[3] = {o.tau[0], o.tau[1], o.tau[2]},
1475 vc[3] = {clean.v[0], clean.v[1], clean.v[2]};
1476 DV rs("rs", b.cells);
1477 Kokkos::deep_copy(rs, 1.0);
1478 for (int c = 0; c < 3; ++c) {
1479 DV bb("bb", b.cells);
1480 sv::addSharpViscousRhs(c, bb, CCConst(rs), vc, clean.g, tc, CCConst(o.dnn), b.G, 1.0, false);
1481 Kokkos::fence();
1482 for (double x : toHost(bb))
1483 nBad += !std::isfinite(x);
1484 }
1485 }
1486 std::printf(
1487 "fallback: NaN normal at (%d,%d,%d): %ld fallbacks (reach %ld), census %ld, tau/D_nn nonzero "
1488 "on a fallback %ld, beyond reach 2 or off the band %ld, non-finite fits %ld, non-finite RHS "
1489 "%ld\n",
1490 qx, qy, qz, nFb, reach, cen.fallback, nNonZero, nFar, nNonFinite, nBad);
1491 CHECK(fb[qs] == 1 && nFb >= 1, "q* falls back");
1492 CHECK(cen.fallback == nFb, "census %ld vs %ld", cen.fallback, nFb);
1493 CHECK(nNonZero == 0 && nFar == 0 && nNonFinite == 0, "fallback state");
1494 CHECK(nBad == 0, "finite band terms");
1495}
1496
1497void testPatch() {
1498 const double angs[3] = {0.0, 22.5, 45.0};
1499 double worst = 0;
1500 long nCase = 0;
1501 for (int an = 0; an < 2; ++an)
1502 for (int ib = 0; ib < 3; ++ib)
1503 for (int ig = 0; ig < 3; ++ig)
1504 for (double off : {0.0, 0.3})
1505 for (double ratio : {1e-2, 1e2, 1e-3, 1e3})
1506 for (int kind = 0; kind < 2; ++kind) {
1507 KCase kc{Blk(8, 8, 8), {}, {1.0, 1.0, 1.0}, {}, {}, {}, {}};
1508 if (an) {
1509 kc.ell[1] = 1.5;
1510 kc.ell[2] = 2.0;
1511 }
1512 const double be = angs[ib] * kPi / 180, ga = angs[ig] * kPi / 180;
1513 Kinked& K = kc.K;
1514 K.n[0] = std::cos(be) * std::cos(ga);
1515 K.n[1] = std::cos(be) * std::sin(ga);
1516 K.n[2] = std::sin(be);
1517 double t1[3] = {-std::sin(ga), std::cos(ga), 0.0};
1518 for (int a = 0; a < 3; ++a)
1519 K.xp[a] = kc.ell[a] * 6.0 + off * K.n[a];
1520 K.muA = ratio;
1521 K.muB = 1.0;
1522 const double mulo = std::min(K.muA, K.muB), muhi = std::max(K.muA, K.muB);
1523 for (int p = 0; p < 3; ++p)
1524 for (int q = 0; q < 3; ++q)
1525 K.Llo[3 * p + q] =
1526 kind == 0 ? K.n[p] * t1[q] / mulo : K.n[p] * K.n[q] - t1[p] * t1[q];
1527 setKink(K, ratio);
1528 buildKinked(kc);
1529 const Blk& b = kc.b;
1530 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
1531 Geo o = makeGeo(b, kc.col, kc.pm, kc.pa, kc.v, dlo, dhi, 0.0, K.muA, K.muB, kc.ell);
1532 DV mu = muField(b, kc.col, K.muA, K.muB);
1533 const double w[3] = {1.0 / (kc.ell[0] * kc.ell[0]), 1.0 / (kc.ell[1] * kc.ell[1]),
1534 1.0 / (kc.ell[2] * kc.ell[2])};
1535 const double dnnEx = kind == 0 ? 0.0 : 1.0;
1536 const double jump = 2.0 * (K.muA - K.muB) * dnnEx; // p_A - p_B
1537 const double scale = kind == 0 ? 1.0 : 2.0 * muhi;
1538 const CCConst vc[3] = {o.v[0], o.v[1], o.v[2]};
1539 const CCConst tc[3] = {o.tau[0], o.tau[1], o.tau[2]};
1540 DV ones("rs", b.cells);
1541 Kokkos::deep_copy(ones, 1.0);
1542 double md = 0;
1543 for (int c = 0; c < 3; ++c) {
1544 sv::SharpFaceProps sp = sharpFP(o, mu, c, 0.0);
1545 auto A = buildBands(b, sp, w);
1546 DV bb("bb", b.cells);
1548 c, bb, CCConst(ones), vc, sharpFP(o, mu, c, 0.0, sv::SvRole::kExplicit),
1549 C3{b.e[0], b.e[1], b.e[2]}, b.G, w[c], false);
1550 sv::addSharpViscousRhs(c, bb, CCConst(ones), vc, o.g, tc, CCConst(o.dnn), b.G, w[c],
1551 false);
1552 Kokkos::fence();
1553 auto bh = toHost(bb);
1554 const auto& vh = kc.v[c];
1555 for (int z = b.G + 1; z < b.e[2] - b.G - 1; ++z)
1556 for (int y = b.G + 1; y < b.e[1] - b.G - 1; ++y)
1557 for (int x = b.G + 1; x < b.e[0] - b.G - 1; ++x) {
1558 const long i = b.id(x, y, z);
1559 double Ku = A[0][i] * vh[i];
1560 for (int f = 0; f < 6; ++f)
1561 Ku += A[1 + f][i] * vh[i + (f % 2 ? 1 : -1) * b.st[f / 2]];
1562 int qm[3] = {x, y, z};
1563 qm[c] -= 1;
1564 const double pi = jump * kc.col[b.id3(x, y, z)],
1565 pm_ = jump * kc.col[b.id3(qm[0], qm[1], qm[2])];
1566 const double R = -Ku + bh[i] - w[c] * (pi - pm_);
1567 md = std::max(md, std::fabs(R));
1568 }
1569 }
1570 ++nCase;
1571 worst = std::max(worst, md / scale);
1572 }
1573 std::printf("(g) operator patch test: %ld cases, worst residual %.2e of the stress scale\n",
1574 nCase, worst);
1575 CHECK(worst <= 1e-11, "kinked-linear patch test");
1576}
1577} // namespace
1578
1579int main(int argc, char** argv) {
1580 Kokkos::initialize(argc, argv);
1581 {
1582 testExtrusion();
1583 testKinked();
1584 testVirtual();
1585 testSeam();
1586 testBand();
1587 testRhsOracle();
1588 testFallback();
1589 testPatch();
1590 testReflection();
1591 testTieFree();
1592 testOwnerWeight();
1593 }
1594 Kokkos::finalize();
1595 if (failures)
1596 std::printf("FAILED (%d)\n", failures);
1597 else
1598 std::printf("PASS\n");
1599 return failures ? 1 : 0;
1600}
flow — portable (Kokkos) Robust-Scaled cut-cell IBM primitives + per-cut-cell overlay build.
flow — the transpose half of the full deviatoric viscous stress on the staggered grid.
mm(p, q)
Definition apriori1d.py:20
y
Definition cmp.py:24
dict res
Definition mgd_case.py:488
void buildBands(CCField AC, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, CCConst kappa, CCConst unk, CCConst sax, CCConst say, CCConst saz, double lam, double idt, const double w[3], C3 e, int g)
The 7 bands (§4.1): AC = kappa idt + sum of the six face terms Lam w_a a (guarded toward a cell that ...
void sharpFit(const SharpGeom &gg, const CCConst(&v)[3], CCField t0, CCField t1, CCField t2, CCField dnn, SvByte fb, C3 lo, C3 hi, SvList list)
The τ̂/D̂_nn fit (§5.3) over the cells [lo, hi) of the solver block: interfacial cells get their fit ...
constexpr double kSvOwnerTieTol
The owner-set tolerance τ_own (§5.8), index units, absolute: the candidates of an edge whose plane di...
SharpCensus sharpCensus(const SharpGeom &gg, SvByteConst fb, C3 lo, C3 hi)
Kokkos::View< std::uint8_t *, CCMem > SvByte
Per-cell byte fields on the solver block: the band class and the fit-fallback flag.
constexpr std::uint8_t kSvVirtual0
void sharpClassify(const SharpGeom &gg, SvByte cls)
The band classes over the WHOLE solver block (§5.2, §5.7 Q-B).
constexpr double kSvOwnerRampTol
The end of the owner-weight ramp τ₁ (§5.9), index units, absolute: a candidate whose distance exceeds...
static double ownerWeight(double g)
The owner weight of a candidate whose plane distance exceeds the closest by g (§5....
constexpr std::uint8_t kSvNone
Band class of a cell.
void addSharpViscousRhs(int c, CCField bb, CCConst rs, const CCConst(&v)[3], const SharpGeom &gg, const CCConst(&tau)[3], CCConst dnn, int g, double wc, bool skipLoC, SvList list, long nl)
The explicit band terms of the u_c rows over the inner cells [g, e−g)³ (§5.4): the τ-terms of the fou...
constexpr std::uint8_t kSvPlane
SvRole
Which half of the time split a SharpFaceProps feeds (§6.1, WO-5b): the implicit band (ibmBuildDiffusi...
Kokkos::View< const double *, CCMem > CCConst
void ibmBuildDiffusionVar(MV AC, MV AW, MV AE, MV AS, MV AN, MV AB, MV AT, int ex, int ey, int ez, int g, FaceProps fp, double wx=1.0, double wy=1.0, double wz=1.0)
void addFullStressRhs(int c, CCField bb, CCConst rs, const CCConst(&v)[3], FaceProps fp, C3 e, int g, double wc, bool skipLoC, const RegConst(&reg)[3]={RegConst(), RegConst(), RegConst()})
Add rs_c · T_c(uⁿ) to the momentum RHS bb of component c over the inner cells [g, e−g)³.
std::vector< typename V::non_const_value_type > toHost(const V &f)
dict A
Definition post.py:33
int m
Definition post.py:35
ta
Definition post.py:36
B
Definition post.py:34
t
Definition post.py:21
one(n, rho_ratio, mu_ratio, stress, steps, adv, dtf, grav=False, sigma="none")
vel
Definition prof.py:67
fit(a, t, t0=40.0, t1=160.0)
worst
Definition stab5.py:6
P
Definition stab5.py:16
col(path, ncol)
g1(thetas=(30.0, 60.0, 90.0, 120.0, 150.0), ratio=1.0, steps=600, R=12.0, nx=64, nz=40, zw=4.25, sigma=1.0, oh=0.1, pivot=0, init_offset=0.0, init_abs=None)
flow — the sharp conservative viscous stress at a VoF interface on the staggered MAC grid: band geome...
The face viscosity under the sharp interface (§5.4 a, §6): a band edge across a (a ≠ c) takes μ̄_c,...
The band geometry and the closure constants, by value into every kernel.
static double val(long gi)
Definition symconv.cpp:27
static constexpr int N
static int wrap(int a, int n)
static constexpr int G
static constexpr double F
Kokkos::View< double *, IMem > DV
static int run(int bc)
static double X0()
static constexpr double U
static constexpr double R
#define CHECK(cond,...)
#define SHARP_VISCOUS_REF