37#include <Kokkos_Core.hpp>
48#ifndef SHARP_VISCOUS_REF
49#define SHARP_VISCOUS_REF "sharp_viscous_mac2d.txt"
54#define CHECK(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"); \
66using DV = Kokkos::View<double*, CCMem>;
67constexpr double kPi = 3.14159265358979323846;
68constexpr int kBig = 1 << 28;
70DV toDev(
const std::vector<double>& h,
const char* name =
"t") {
72 auto m = Kokkos::create_mirror_view(d);
73 for (std::size_t i = 0; i < h.size(); ++i)
75 Kokkos::deep_copy(d, m);
79 auto m = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), d);
80 return std::vector<double>(
m.data(),
m.data() +
m.extent(0));
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)
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) {
97 e3[
a] = n[
a] + 2 * G + 2;
101 st[2] = (long)e[0] * e[1];
104 st3[2] = (long)e3[0] * e3[1];
105 cells = st[2] * e[2];
106 cells3 = st3[2] * e3[2];
108 long id(
int x,
int y,
int z)
const {
return x +
y * st[1] +
z * st[2]; }
110 long id3(
int x,
int y,
int z)
const {
return (x + 1) + (
y + 1) * st3[1] + (z + 1) * st3[2]; }
115 DV col, pm[3], pa, tau[3], dnn, v[3];
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]) {
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);
131 o.pa = toDev(pa,
"pa");
132 o.dnn =
DV(
"dnn",
b.cells);
136 for (
int a = 0;
a < 3; ++
a) {
156 g.mulo =
g.lowA ? muA : muB;
157 g.r =
g.lowA ? muA / muB : muB / muA;
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});
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;
181 Kokkos::deep_copy(qd, qh);
182 DV out(
"out", 6 * nq);
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;
190 g.forEachOwner(k, a, c, [&](
const int ow[3],
const double m[3],
double al,
double) {
192 xi[
a] =
k[
a] - ow[
a];
193 xi[
c] =
k[
c] - ow[
c];
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]));
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);
206 g.edgeFaceFraction(k, a, c, a, f);
208 g.edgeLinkFractions(k, a, c, tac, tca);
209 out(6 * t + 4) = tac;
210 out(6 * t + 5) = tca;
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];
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);
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);
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];
245 for (
int c = 0;
c < 3; ++
c)
246 out(8 * t + 5 + c) = sv::plicMidsectionFraction(m, al, c);
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];
258void testExtrusion() {
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) {
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;
275 double C, cbu, cbv, tau, dnn;
279 double fu, fv, thy, thx;
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;
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;
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) {
299 for (
int t = 0;
t < nv; ++
t) {
303 for (
int z = 0;
z <
b.e[2]; ++
z)
304 vel[comp][
b.id(i +
G, j +
G, z)] =
val /
h;
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)},
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);
324 pa[j3] = (
s + n0 * c0 + n1 *
c1) / h - (n0 *
c.i + n1 *
c.j);
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);
332 const double mulo = std::min(muA, muB);
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;
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)});
353 const long i =
b.id(
c.i +
G,
c.j +
G,
G + 1);
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));
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) {
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)});
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");
390 double n[3], xp[3], Llo[9], j[3], muA, muB;
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]);
395 bool hi(
double ph)
const {
return lowA ? (
ph >= 0.0) : (
ph < 0.0); }
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];
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])});
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];
421 cx += (
p[0] +
q[0]) * cr;
422 cy += (
p[1] +
q[1]) * cr;
425 if (std::fabs(ar) < 1e-300)
429 return std::fabs(ar) * (f0 +
g1 * cx +
g2 * cy);
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];
438 for (
int a = 0;
a < 3; ++
a)
440 double cen[3] = {
X0[0],
X0[1],
X0[2]};
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);
452 const double kink =
K.lowA ?
pos : (
all -
pos);
453 return lin +
K.j[
q] * kink;
460 std::vector<double> col, pm[3], pa, v[3];
464void buildKinked(KCase& kc) {
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);
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);
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);
484 if (C > 0.0 && C < 1.0) {
485 for (
int a = 0;
a < 3; ++
a)
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];
498void setKink(Kinked& K,
double ratio) {
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) {
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];
510 for (
int p = 0;
p < 3; ++
p)
511 K.j[p] = (r - 1.0) * (sn[
p] -
K.n[
p] * nsn);
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 : {1
e-2, 1
e2, 1
e-3, 1
e3}) {
525 KCase
kc{Blk(8, 8, 8), {}, {1.0, 1.0, 1.0}, {}, {}, {}, {}};
530 const double be = angs[ib] * kPi / 180, ga = angs[ig] * kPi / 180;
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)
536 K.xp[a] =
kc.ell[a] * 6.0 + off *
K.n[a];
538 for (
int s = 0;
s < 9; ++
s)
540 for (
int p = 0;
p < 3; ++
p)
542 for (
int p = 0;
p < 3; ++
p)
543 K.Llo[4 * p] -= tr / 3.0;
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);
552 double sn[3], nsn = 0, dex = 0, tex[3],
lmax = 0;
553 for (
int p = 0;
p < 3; ++
p) {
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]));
560 nsn +=
K.n[
p] * sn[
p];
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);
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);
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);
580 std::printf(
"(b) kinked-linear: %ld fits, tau %.2e, D_nn %.2e (relative), fallbacks %ld\n", nFit,
582 CHECK(nFit > 1000,
"coverage");
583 CHECK(wTau <= 1e-11 && wDnn <= 1e-11,
"kinked-linear exactness");
584 CHECK(nFb == 0,
"fallbacks");
590 long nEdge = 0, nBad = 0, nPlaneBad = 0;
592 for (
int ax = 0;
ax < 3; ++
ax)
593 for (
int stack = 0; stack < 2; ++stack) {
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);
602 auto phase = [&](
int X) {
603 const int w = ((
X % 6) + 6) % 6;
604 return stack ? (
w % 2 == 0) : (
w == 2);
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;
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);
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);
629 bool ok = (
int)cr[X][0] == kExp;
630 if (ok && side >= 0) {
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);
637 nPlaneBad +=
ok ? 0 : 1;
640 std::vector<EdgeQ>
eq;
641 for (
int X = 0;
X <= 6; ++
X) {
642 if (phase(X) == phase(X - 1))
644 for (
int c = 0;
c < 3; ++
c) {
647 for (
int p = 3;
p <= 4; ++
p)
648 for (
int s = 3;
s <= 4; ++
s) {
650 const int bb = 3 -
ax -
c;
654 q.a = std::min(ax, c);
655 q.c = std::max(ax, c);
660 auto er = queryEdges(o, eq);
661 for (
const auto& r : er) {
666 wDist = std::max(wDist, r[1]);
670 "(c) virtual planes: %ld face edges, %ld ownerless, max owner distance %.2e, %ld bad "
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");
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];
684 for (
int c = 0;
c < 3; ++
c) {
685 ug[
c].resize(n * n * n);
686 for (
auto& x : ug[c])
689 auto wrap = [&](
int i) {
return ((i % n) + n) %
n; };
691 const double s2 = std::sqrt(0.5);
692 auto run = [&](
int shift, std::vector<double>& tauOut, std::vector<int>& clsOut) {
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);
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) {
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;
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));
711 double C2 = sv::plicVolume(m[0], m[1], 0.0, 5.1 - s2 * W);
712 const double C = C1 + C2 - 1.0 < 0.0 ? 0.0 : C1 + C2 - 1.0;
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;
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)];
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);
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];
751 std::vector<double>
ta,
tb;
752 std::vector<int>
ca, 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,
764 CHECK(band > 50 && diff == 0,
"shift invariance across the seam");
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 {
781 const long j = b->id3(q[0], q[1], q[2]);
782 for (
int a = 0;
a < 3; ++
a)
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;
792 al =
side == 0 ? 0.0 :
s;
794 void normal(
const double m[3],
double n[3])
const {
796 for (
int a = 0;
a < 3; ++
a) {
797 p[
a] =
m[
a] / ell[
a];
801 for (
int a = 0;
a < 3; ++
a)
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]};
812 v.push_back({b->id(q[0], q[1], q[2]),
q});
814 std::sort(
v.begin(),
v.end());
815 std::vector<std::array<int, 3>>
o;
817 o.push_back(
x.second);
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)
833 plane(
q.data(), mb.m, mb.al);
835 for (
int s = 0;
s < 3; ++
s) {
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]));
845 std::vector<Member>
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)
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]);
860 static double hatFrac(
const double m[3],
double al,
int p,
double xiP,
int q,
double qc,
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);
866 double rowFace(
const int k[3],
int a,
int c,
int r,
const int o[3],
const double m[3],
868 const int p = (
r ==
c) ? a :
c, bb = 3 -
a -
c;
871 lo[bb] =
k[bb] -
o[bb];
873 return boxFrac(m, al, p, k[p] - o[p], lo);
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]);
880HostGeo hostGeo(
const Blk& b,
const Geo& o,
double eps,
double muA,
double muB,
881 const double ell[3]) {
886 for (
int a = 0;
a < 3; ++
a) {
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};
913 for (
int a = 0;
a < 3; ++
a) {
914 n[
a] = ell[
a] * (
I[
a] + 0.5) - cen[a];
919 for (
int a = 0;
a < 3; ++
a) {
921 m[
a] =
n[
a] * ell[
a];
922 al +=
n[
a] * cen[
a] -
m[
a] *
I[
a];
924 const long j =
b.id3(x, y, z);
925 const double C = sv::plicVolume(m[0], m[1], m[2], al);
927 if (C > 1e-12 && C < 1.0 - 1e-12) {
928 for (
int a = 0;
a < 3; ++
a)
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");
946 explicit Bands(
long n) {
952std::vector<std::vector<double>>
buildBands(
const Blk& b, FP fp,
const double w[3]) {
955 b.e[2],
b.G, fp, w[0], w[1], w[2]);
957 std::vector<std::vector<double>>
o;
969 base.rhoIdtC = rhoIdt;
982 long nBand = 0, nTie = 0, nNon = 0, badNon = 0, badSym = 0, badM = 0, badCell = 0, nCellIf = 0,
985 for (
int an = 0; an < 2; ++an)
986 for (
double ratio : {1
e-2, 1
e3}) {
988 double ell[3] = {1.0, 1.0, 1.0};
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);
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) {
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;
1015 for (
int f = 0;
f < 6; ++
f) {
1016 const int a =
f / 2,
sg =
f % 2;
1017 const double sv_ = As[1 +
f][
i], av = Av[1 +
f][
i];
1019 badM += (sv_ > 0.0);
1021 int q[3] = {
x,
y,
z};
1024 const bool itf = hg.cl(q) != 0;
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);
1032 badSplit += (Ae[1 +
f][
i] != sv_);
1033 int k[3] = {
x,
y,
z};
1036 const auto ow = hg.owners(k, a, c);
1039 badNon += (sv_ != av);
1043 nTie += ow.size() > 1;
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));
1051 if (!rowBand && !rowCell)
1052 badCell += (As[0][
i] != Av[0][
i]);
1053 badM += !(std::fabs(As[0][i] - 0.7 - sum) <= 1
e-13 * As[0][
i]);
1055 for (
int a = 0;
a < 3; ++
a) {
1056 int q[3] = {
x,
y,
z};
1058 if (q[a] >=
b.e[a] -
b.G)
1060 badSym += (As[2 + 2 *
a][
i] != As[1 + 2 *
a][
b.id(q[0], q[1], q[2])]);
1066 "(e) implicit band: %ld band entries (%ld tied; oracle %.2e), %ld non-band (%ld differ), "
1068 "diag %ld differ, symmetry %ld, M-matrix %ld; split: %ld interfacial cell links, %ld "
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");
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) {
1084 double m[3], al,
n[3];
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));
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);
1099 double t[3] = {0, 0, 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;
1108 for (
const auto& mb : ow) {
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);
1116 return y / ow.size();
1125void testReflection() {
1127 const double ell[3] = {1.0, 1.0, 1.0};
1128 const int X0 =
b.G + 12;
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);
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;
1140 v[
a][
b.id(x, y, z)] = (
a == 2) ? -2.0 * r :
r;
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);
1147 DV out(
"refl", 21 *
b.cells);
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)
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;
1164 g.forEachOwner(k, a, c, [](
const int*,
const double*,
double,
double) {});
1167 double sp = 0.0, gp = 0.0;
1169 out(o7) =
g.edgeTieKeys(k, ac, sp, gp, bl);
1170 double f = 0.0, tac = 0.0, tca = 0.0;
1171 g.edgeFaceFraction(k, a, c, a, f);
1173 g.edgeFaceFraction(k, a, c, c, f);
1175 g.edgeLinkFractions(k, a, c, tac, tca);
1178 out(o7 + 5) =
g.edgeTraction(k, a, c, a, tc);
1179 out(o7 + 6) =
g.edgeTraction(k, a, c, c, tc);
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];
1194 int kr[3] = {
x,
y,
z};
1195 kr[
r] = (
r == bb) ? 2 *
X0 - 1 - kr[
r] : 2 *
X0 - kr[
r];
1196 const double*
f = &
h[21 *
b.id(kr[0], kr[1], kr[2]) + 7 * ac];
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]));
1212 for (
int q = 0;
q < 6; ++
q)
1213 worst = std::max(worst, mx[q] > 0.0 ? wd[q] / mx[q] : 0.0);
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,
1222 CHECK(badN == 0,
"n_E reflection-invariant");
1223 CHECK(worst <= 1e-13,
"reflection self-symmetry %.2e", worst);
1227void testOwnerWeight() {
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) {
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));
1252 const double e2 = 3.0 * (hh / (t1 -
t0)) * (hh / (t1 - t0));
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");
1266std::uint64_t fnv(std::uint64_t h,
const std::vector<double>& x) {
1267 for (
double d : x) {
1269 std::memcpy(&u, &d,
sizeof u);
1270 for (
int s = 0;
s < 8; ++
s) {
1271 h ^= (
u >> (8 *
s)) & 0xffu;
1272 h *= 0x100000001b3ULL;
1286 const std::uint64_t
want = 0x453f2865e4cc0381ULL;
1287 constexpr bool host = Kokkos::SpaceAccessibility<Kokkos::HostSpace, CCMem>::accessible;
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);
1301 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
1302 const double muA = 1
e3, muB = 1.0;
1303 Geo
o = makeGeo(b, col, pm, pa, v, dlo, dhi, 1e-12, muA, muB, ell);
1306 std::uint64_t
h = 0xcbf29ce484222325ULL;
1307 for (
int a = 0;
a < 3; ++
a)
1312 h = fnv(h, std::vector<double>(
fb.begin(),
fb.end()));
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))
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);
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");
1335 CHECK(h == want,
"tie-free band-op outputs bitwise f01a251");
1340void testRhsOracle() {
1341 std::mt19937
rng(5);
1342 std::uniform_real_distribution<double>
U(-1.0, 1.0);
1345 for (
int an = 0; an < 2; ++an)
1346 for (
double ratio : {1
e-2, 1
e3}) {
1348 double ell[3] = {1.0, 1.0, 1.0};
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) {
1363 for (
long i = 0;
i <
b.cells; ++
i) {
1365 rs[
i] = 1.0 + 0.25 *
U(rng);
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);
1373 for (
int c = 0;
c < 3; ++
c) {
1374 DV bb(
"bb",
b.cells);
1375 const double wc = 1.0 / (ell[
c] * ell[
c]);
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};
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);
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);
1400 worst = std::max(worst, md / mx);
1403 std::printf(
"(f) explicit kernel vs host oracle: %ld nonzero rows, worst %.2e relative\n", nRow,
1405 CHECK(nRow > 500 && worst <= 1e-13,
"explicit kernel vs host oracle");
1416void testFallback() {
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)
1428 const int dlo[3] = {-kBig, -kBig, -kBig}, dhi[3] = {kBig, kBig, kBig};
1429 const double muA = 1
e-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);
1439 if (qs < 0 && cls[i] ==
sv::kSvPlane && x > 5 && y > 5 && z > 5)
1442 CHECK(qs >= 0 && nFb0 == 0,
"an interior plane cell (%ld), no fallback on the clean field (%ld)",
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);
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;
1461 const int d = std::max({std::abs(x - qx), std::abs(y - qy), std::abs(z - qz)});
1463 reach = std::max<long>(reach, d);
1465 nNonZero += (tau[0][
i] != 0.0 || tau[1][
i] != 0.0 || tau[2][
i] != 0.0 || dnn[
i] != 0.0);
1467 nNonFinite += !(std::isfinite(tau[0][i]) && std::isfinite(tau[1][i]) &&
1468 std::isfinite(tau[2][i]) && std::isfinite(dnn[i]));
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);
1482 for (
double x :
toHost(bb))
1483 nBad += !std::isfinite(x);
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 "
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");
1498 const double angs[3] = {0.0, 22.5, 45.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 : {1
e-2, 1
e2, 1
e-3, 1
e3})
1506 for (
int kind = 0; kind < 2; ++kind) {
1507 KCase
kc{Blk(8, 8, 8), {}, {1.0, 1.0, 1.0}, {}, {}, {}, {}};
1512 const double be = angs[ib] * kPi / 180, ga = angs[ig] * kPi / 180;
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];
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)
1526 kind == 0 ?
K.n[p] * t1[q] / mulo :
K.
n[
p] *
K.
n[
q] - t1[
p] * t1[
q];
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;
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]};
1540 DV ones(
"rs",
b.cells);
1541 Kokkos::deep_copy(ones, 1.0);
1543 for (
int c = 0;
c < 3; ++
c) {
1546 DV bb(
"bb",
b.cells);
1549 C3{
b.e[0],
b.e[1],
b.e[2]},
b.G,
w[
c],
false);
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};
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));
1571 worst = std::max(worst, md / scale);
1573 std::printf(
"(g) operator patch test: %ld cases, worst residual %.2e of the stress scale\n",
1575 CHECK(worst <= 1e-11,
"kinked-linear patch test");
1580 Kokkos::initialize(argc, argv);
1596 std::printf(
"FAILED (%d)\n",
failures);
1598 std::printf(
"PASS\n");
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.
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(®)[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)
one(n, rho_ratio, mu_ratio, stress, steps, adv, dtf, grav=False, sigma="none")
fit(a, t, t0=40.0, t1=160.0)
g2(path, dt, walls=False)
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)
static int wrap(int a, int n)
static constexpr double F
Kokkos::View< double *, IMem > DV
static constexpr double U
static constexpr double R
#define SHARP_VISCOUS_REF