105static void compare(
const char* tag,
int n,
const std::vector<double>& gsdf,
FlowSetup flow,
106 ScalarSetup scalar, Solve solve,
bool checkLevels =
false,
bool gated =
true,
107 bool cutcellPressure =
false,
Post post =
nullptr,
108 double identityBound = 1e-12) {
109 peclet::core::decomp::BlockDecomposer<3> dec =
110 peclet::flow::CutcellMG::decomposition(
static_cast<std::size_t
>(
size_), n, n, n);
111 const auto blk = dec.block(
rank_);
112 Block B{{(int)blk.origin[0], (
int)blk.origin[1], (int)blk.origin[2]},
113 {(int)blk.size[0], (
int)blk.size[1], (int)blk.size[2]}};
114 std::vector<double> lsdf((std::size_t)B.l[0] * B.l[1] * B.l[2]);
115 for (
int z = 0; z < B.l[2]; ++z)
116 for (
int y = 0; y < B.l[1]; ++y)
117 for (
int x = 0; x < B.l[0]; ++x)
118 lsdf[(std::size_t)x + (std::size_t)y * B.l[0] + (std::size_t)z * B.l[0] * B.l[1]] =
119 gsdf[(std::size_t)(x + B.o[0]) + (std::size_t)(y + B.o[1]) * n +
120 (std::size_t)(z + B.o[2]) * n * n];
125 sd.initMpi(n, n, n, MPI_COMM_WORLD);
133 scalar(sr,
Block{{0, 0, 0}, {n, n, n}});
138 const Run D = solve(sd);
139 const Run
R = solve(sr);
140 auto cd = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sd.
scalarField(
"c").
c);
141 auto cr = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sr.
scalarField(
"c").
c);
142 const long dsy = B.l[0] + 2 *
G, dsz = dsy * (B.l[1] + 2 *
G), rsy = n + 2 *
G,
143 rsz = rsy * (n + 2 *
G);
144 double maxDiff = 0.0, maxRef = 0.0;
146 for (
int z = 0; z < B.l[2]; ++z)
147 for (
int y = 0; y < B.l[1]; ++y)
148 for (
int x = 0; x < B.l[0]; ++x) {
149 const double a = cd((x +
G) + (y +
G) * dsy + (z +
G) * dsz);
150 const double b = cr((x + B.o[0] +
G) + (y + B.o[1] +
G) * rsy + (z + B.o[2] +
G) * rsz);
152 maxRef = std::fmax(maxRef, std::fabs(b));
153 notSame +=
same(a, b) ? 0 : 1;
155 double g[2] = {
maxDiff, maxRef}, gg[2];
156 MPI_Allreduce(g, gg, 2, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
158 MPI_Allreduce(¬Same, &ns, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
162 "[%s%s] np=%d: max|c - c_1| = %.2e (max|c_1| %.3f), %ld cells not bitwise; "
163 "mg levels np %d single %d\n",
164 tag, gated ?
"" :
" @ default rtol 1e-10, information only",
size_, gg[0], gg[1], ns, lvD,
166 std::printf(
" iterations np:");
167 for (
int it : D.iters)
168 std::printf(
" %d", it);
169 std::printf(
" single:");
170 for (
int it :
R.iters)
171 std::printf(
" %d", it);
172 std::printf(
"\n wall flux np: %.15e single: %.15e\n", D.flux.back(),
R.flux.back());
174 for (std::size_t k = 0; k < D.identity.size(); ++k)
175 idm = std::fmax(idm, std::fmax(std::fabs(D.identity[k]), std::fabs(
R.identity[k])));
176 std::printf(
" budget identity (rel) <= %.1e\n", idm);
182 CHECK(D.iters.size() ==
R.iters.size());
183 for (std::size_t k = 0; k < D.identity.size(); ++k) {
184 CHECK(std::fabs(D.identity[k]) <= identityBound);
185 CHECK(std::fabs(
R.identity[k]) <= identityBound);
189 for (std::size_t k = 0; k < D.iters.size(); ++k)
190 CHECK(D.iters[k] ==
R.iters[k]);
191 for (std::size_t k = 0; k < D.flux.size(); ++k)
194 CHECK(gg[0] <= 1e-10 * gg[1]);
195 for (std::size_t k = 0; k < D.iters.size(); ++k)
196 CHECK(std::abs(D.iters[k] -
R.iters[k]) <= 1);
197 for (std::size_t k = 0; k < D.flux.size(); ++k)
198 CHECK(std::fabs(D.flux[k] -
R.flux[k]) <= 1e-10 * std::fabs(
R.flux[k]));
204 const double w[3] = {1.0, 1.0, 1.0}, hp[3] = {1.0, 1.0, 1.0};
206 vmg.initMpi(dec, 64, MPI_COMM_WORLD);
207 bool eq = vmg.
levels() == mg.levels();
208 for (
int L = 0; eq && L < mg.levels(); ++L) {
209 const auto& a = vmg.
level(L);
210 const auto& b = mg.level(L);
211 eq = a.inner.x == b.inner.x && a.inner.y == b.inner.y && a.inner.z == b.inner.z &&
212 a.ratio.x == b.ratio.x && a.ratio.y == b.ratio.y && a.ratio.z == b.ratio.z &&
213 a.og.x == b.og.x && a.og.y == b.og.y && a.og.z == b.og.z;
215 int all = eq ? 1 : 0, allMin = 0;
216 MPI_Allreduce(&all, &allMin, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
218 std::printf(
" level table: %d levels, %s VelocityMG::initMpi\n", mg.levels(),
219 allMin ?
"==" :
"DIFFERS from");
224int main(
int argc,
char** argv) {
225 MPI_Init(&argc, &argv);
226 MPI_Comm_rank(MPI_COMM_WORLD, &
rank_);
227 MPI_Comm_size(MPI_COMM_WORLD, &
size_);
228 Kokkos::initialize(argc, argv);
233 auto profile = [](
int y,
int z) {
return 0.4 + 0.1 * std::sin(0.3 * y) * std::cos(0.2 * z); };
240 s.setDomainBc(0, 1, 0.0, 0.0, 0.0);
241 s.setDomainBc(1, 1, 0.0, 0.0, 0.0);
244 s.addScalar(
"c", 1.2, 1, 50,
true);
245 s.setScalarBc(
"c", 0, 2, 0.15);
246 std::vector<double>
prof((std::size_t)B.l[1] * B.l[2]);
247 for (
int z = 0; z < B.l[2]; ++z)
248 for (
int y = 0; y < B.l[1]; ++y)
249 prof[(std::size_t)y + (std::size_t)z * B.l[1]] = profile(y + B.o[1], z + B.o[2]);
250 s.setScalarBcProfile(
"c", 1,
prof, B.l[1], B.l[2]);
254 s.setScalarWall(
"c", 1, 1.0, 0.0, -1);
255 s.solveScalarSteady(
"c");
257 s.setScalarWall(
"c", 2, 0.3, 0.7, -1);
258 s.setScalarSource(
"c", 0.1);
259 for (
int k = 0; k < 3; ++k) {
270 const double R = 16.0, c0[3] = {31.87, 32.21, 31.66};
271 for (
const bool gated : {
true,
false})
278 for (
int f = 0; f < 6; ++f)
279 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
282 s.addScalar(
"c", 0.7, 1, 50,
true);
283 for (
int f = 0; f < 6; ++f) {
285 const int t1 = (a == 0) ? 1 : 0, t2 = (a == 2) ? 1 : 2;
286 const double xa = (f % 2 == 0) ? -0.5 :
N - 0.5;
287 std::vector<double>
prof((std::size_t)B.l[t1] * B.l[t2]);
288 for (
int j2 = 0; j2 < B.l[t2]; ++j2)
289 for (
int j1 = 0; j1 < B.l[t1]; ++j1) {
292 p[t1] = j1 + B.o[t1];
293 p[t2] = j2 + B.o[t2];
294 const double r = std::sqrt((p[0] - c0[0]) * (p[0] - c0[0]) +
295 (p[1] - c0[1]) * (p[1] - c0[1]) +
296 (p[2] - c0[2]) * (p[2] - c0[2]));
297 prof[(std::size_t)j1 + (std::size_t)j2 * B.l[t1]] =
R / r;
299 s.setScalarBc(
"c", f, 2, 0.0);
300 s.setScalarBcProfile(
"c", f,
prof, B.l[t1], B.l[t2]);
302 s.setScalarWall(
"c", 1, 1.0, 0.0, -1);
306 s.solveScalarSteady(
"c");
316 "singular",
N,
sphereSdf(
N, 15.3, 16.1, 14.7, 9.3),
323 s.addScalar(
"c", 0.9, 1, 50,
true);
324 s.setScalarWall(
"c", 0, 0.4, 0.0, -1);
325 s.setScalarSource(
"c", -0.05);
329 s.solveScalarSteady(
"c");
330 r.iters.push_back(s.scalarField(
"c").cut->iterations);
331 r.flux.push_back(s.scalarWallFlux(
"c")[0]);
332 CHECK(s.scalarField(
"c").cut->singular);
344 const double cx = 17.78, cy = 18.13, Ri = 6.4, Ro = 16.0, Om = 0.5 / (std::sqrt(2.0) * Ro);
345 std::vector<double> gsdf((std::size_t)
N *
N *
N);
346 for (
int z = 0; z <
N; ++z)
347 for (
int y = 0; y <
N; ++y)
348 for (
int x = 0; x <
N; ++x) {
349 const double r = std::hypot(x - cx, y - cy);
350 gsdf[(std::size_t)x + (std::size_t)y *
N + (std::size_t)z *
N *
N] =
351 std::fmin(r - Ri, Ro - r);
360 std::vector<double> vx((std::size_t)
N *
N *
N), vy((std::size_t)
N *
N *
N);
363 g.setAdvection(
false);
365 g.setSolid(gsdf,
true);
366 const std::vector<double> ox = g.getOpennessProj(0), oy = g.getOpennessProj(1);
367 auto I = [&](
int x,
int y) {
368 return (std::size_t)((x +
N) %
N) + (std::size_t)((y +
N) %
N) *
N;
370 std::vector<double> P((std::size_t)
N *
N);
371 for (
int y = 0; y <
N; ++y)
372 for (
int x = 0; x <
N; ++x) {
373 const double r = std::hypot(x - 0.5 - cx, y - 0.5 - cy);
374 const double rc = std::fmin(std::fmax(r, Ri), Ro);
375 const double wall = r < 0.5 * (Ri + Ro) ? Ri : Ro;
376 const bool mark = !(ox[I(x, y)] > 0.0) || !(ox[I(x, y - 1)] > 0.0) ||
377 !(oy[I(x, y)] > 0.0) || !(oy[I(x - 1, y)] > 0.0);
378 P[I(x, y)] = -0.5 * Om * (mark ? wall * wall : rc * rc);
380 for (
int z = 0; z <
N; ++z)
381 for (
int y = 0; y <
N; ++y)
382 for (
int x = 0; x <
N; ++x) {
383 const std::size_t k = (std::size_t)x + (std::size_t)y *
N + (std::size_t)z *
N *
N;
384 const double fx = P[I(x, y + 1)] - P[I(x, y)], fy = -(P[I(x + 1, y)] - P[I(x, y)]);
385 vx[k] = ox[k] > 0.0 ? fx / ox[k] : 0.0;
386 vy[k] = oy[k] > 0.0 ? fy / oy[k] : 0.0;
390 "g9",
N, gsdf, flowG9,
392 s.addScalar(
"c", 0.0, 1, 50,
true);
393 const std::size_t nl = (std::size_t)B.l[0] * B.l[1] * B.l[2];
394 std::vector<double> u(nl), v(nl), w(nl, 0.0), c(nl);
395 for (
int z = 0; z < B.l[2]; ++z)
396 for (
int y = 0; y < B.l[1]; ++y)
397 for (
int x = 0; x < B.l[0]; ++x) {
398 const std::size_t l =
399 (std::size_t)x + (std::size_t)y * B.l[0] + (std::size_t)z * B.l[0] * B.l[1];
400 const int gx = x + B.o[0], gy = y + B.o[1], gz = z + B.o[2];
401 const std::size_t k =
402 (std::size_t)gx + (std::size_t)gy *
N + (std::size_t)gz *
N *
N;
405 const double dx = gx - cx - 0.7 * Ro, dy = gy - cy, sg = 0.08 * Ro;
406 c[l] = gsdf[k] > 0.0 ? std::exp(-(dx * dx + dy * dy) / (2.0 * sg * sg)) : 0.0;
415 for (
int k = 0; k < 50; ++k) {
417 const auto& st = *s.scalarField(
"c").cut;
418 r.iters.push_back(st.iterations);
420 r.flux.push_back((
double)st.numSmall + 1e4 * (
double)st.numImplicitFaces);
421 const auto b = s.scalarBudget(
"c");
422 r.identity.push_back(b.identityError / b.mass);
424 const auto& st = *s.scalarField(
"c").cut;
426 std::printf(
" [g9] C_bulk %.3f, small cells %ld, implicit faces %ld of %ld\n",
427 st.bulkCourant, st.numSmall, st.numImplicitFaces, st.numFluxFaces);
428 CHECK(st.numSmall > 0 && st.numImplicitFaces > 0);
443 std::vector<double> gsdf((std::size_t)
N *
N *
N);
444 for (
int z = 0; z <
N; ++z)
445 for (
int y = 0; y <
N; ++y)
446 for (
int x = 0; x <
N; ++x) {
447 const double a = std::sqrt((x - 13.3) * (x - 13.3) + (y - 17.1) * (y - 17.1) +
448 (z - 11.4) * (z - 11.4)) -
450 const double b = std::sqrt((x - 15.6) * (x - 15.6) + (y - 16.5) * (y - 16.5) +
451 (z - 34.6) * (z - 34.6)) -
453 gsdf[(std::size_t)x + (std::size_t)y *
N + (std::size_t)z *
N *
N] = std::fmin(a, b);
459 for (
int f = 0; f < 4; ++f)
460 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
461 s.setDomainBc(4, 2, 0.0, 0.0, 1.0);
462 s.setDomainBc(5, 3, 0.0, 0.0, 0.0);
466 "g9c",
N, gsdf, flowG9c,
468 s.addScalar(
"c", 0.3, 0, 50,
true);
469 for (
int f = 0; f < 4; ++f)
470 s.setScalarBc(
"c", f, 1, 0.0);
471 s.setScalarBc(
"c", 4, 2, 1.0);
472 s.setScalarBc(
"c", 5, 1, 0.0);
473 const std::vector<double> unk = s.scalarGeometryField(4);
474 std::vector<double> c(unk.size());
475 for (std::size_t k = 0; k < c.size(); ++k)
476 c[k] = unk[k] > 0.5 ? 0.2 : 0.0;
481 const bool ref = call++ == 1;
483 long outletSmall = 0, small = 0, impl = 0;
484 for (
int k = 0; k < 30; ++k) {
486 const auto& st = *s.scalarField(
"c").cut;
487 small = std::max(small, st.numSmall);
488 impl = std::max(impl, st.numImplicitFaces);
489 r.iters.push_back(st.iterations);
490 r.flux.push_back((
double)st.numSmall + 1e4 * (
double)st.numImplicitFaces);
491 const auto b = s.scalarBudget(
"c");
492 r.identity.push_back(b.identityError / b.mass);
494 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), st.small);
495 const int lx = s.nx(), ly = s.ny(), lz = s.nz();
496 const long sy = lx + 2 *
G, sz = sy * (ly + 2 *
G);
497 for (
int y = 0; y < ly; ++y)
498 for (
int x = 0; x < lx; ++x)
499 outletSmall += h((x +
G) + (y +
G) * sy + (lz - 1 +
G) * sz) > 0.5 ? 1 : 0;
503 " [g9c %s] small cells <= %ld (in the outlet layer, summed over the "
504 "steps, rank 0: %ld), implicit faces <= %ld\n",
505 ref ?
"single" :
"np", small, outletSmall, impl);
506 CHECK(small > 0 && impl > 0);
508 CHECK(outletSmall > 0);
520 const std::vector<double> gsdf =
sphereSdf(
N, 31.87, 31.28, 32.11, 16.0);
527 s.addScalar(
"c", 0.7, 1, 50,
true);
528 s.setScalarWall(
"c", 1, 1.0, 0.0, -1);
529 s.setScalarSource(
"c", -0.3);
531 std::vector<double> gv[3];
532 const char* nm[3] = {
"u",
"v",
"w"};
535 g.setAdvection(
false);
537 g.setSolid(gsdf,
true);
538 g.setBodyForce(1e-3, 4e-4, 2e-4);
539 for (
int k = 0; k < 20; ++k)
542 g.solveScalarSteady(
"c");
543 const double k = 1.0 / g.scalarField(
"c").cut->maxCellPeclet;
544 for (
int a = 0; a < 3; ++a) {
545 gv[a] = g.getField(nm[a]);
546 for (
double& v : gv[a])
551 "steady_adv",
N, gsdf, flowAdv,
553 const std::size_t nl = (std::size_t)B.l[0] * B.l[1] * B.l[2];
554 for (
int a = 0; a < 3; ++a) {
555 std::vector<double> f(nl);
556 for (
int z = 0; z < B.l[2]; ++z)
557 for (
int y = 0; y < B.l[1]; ++y)
558 for (
int x = 0; x < B.l[0]; ++x)
559 f[(std::size_t)x + (std::size_t)y * B.l[0] + (std::size_t)z * B.l[0] * B.l[1]] =
560 gv[a][(std::size_t)(x + B.o[0]) + (std::size_t)(y + B.o[1]) *
N +
561 (std::size_t)(z + B.o[2]) *
N *
N];
562 s.setField(nm[a], f);
568 s.solveScalarSteady(
"c");
573 const auto b = s.scalarBudget(
"c");
574 r.identity.back() = b.identityError / (std::fabs(b.wallIn) + std::fabs(b.sourceIn) +
575 std::fabs(b.boundaryIn));
576 const auto& st = *s.scalarField(
"c").cut;
578 std::printf(
" [steady_adv] census max_cell_peclet %.6g, %d levels, %d iterations\n",
579 st.maxCellPeclet, st.mgLevels, st.iterations);
580 CHECK(st.advecting && st.steady && std::fabs(st.maxCellPeclet - 1.0) <= 1e-12);
589 const std::vector<double> unk = s.scalarGeometryField(4);
590 std::vector<double> rv(unk.size());
591 for (
int z = 0; z < b.l[2]; ++z)
592 for (
int y = 0; y < b.l[1]; ++y)
593 for (
int x = 0; x < b.l[0]; ++x) {
594 const std::size_t l =
595 (std::size_t)x + (std::size_t)y * b.l[0] + (std::size_t)z * b.l[0] * b.l[1];
597 (double)((x + b.o[0]) + (y + b.o[1]) *
N + (
long)(z + b.o[2]) *
N *
N);
598 rv[l] = unk[l] > 0.5 ? std::sin(0.37 * gk) * (1.0 + 0.2 * std::cos(1e-3 * gk))
601 auto& sc = s.scalarField(
"c");
603 const std::size_t n = sc.c.extent(0);
605 Kokkos::deep_copy(r, sc.c);
606 sc.cut->mg->apply(z, r);
607 return Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), z);
609 const auto zd = zOf(sd, B);
610 const auto zr = zOf(sr,
Block{{0, 0, 0}, {
N,
N,
N}});
611 const long dsy = B.l[0] + 2 *
G, dsz = dsy * (B.l[1] + 2 *
G), rsy =
N + 2 *
G,
612 rsz = rsy * (
N + 2 *
G);
613 long notSame = 0, cells = 0;
614 for (
int z = 0; z < B.l[2]; ++z)
615 for (
int y = 0; y < B.l[1]; ++y)
616 for (
int x = 0; x < B.l[0]; ++x) {
617 const double a = zd((x +
G) + (y +
G) * dsy + (z +
G) * dsz);
619 zr((x + B.o[0] +
G) + (y + B.o[1] +
G) * rsy + (z + B.o[2] +
G) * rsz);
620 notSame +=
same(a, b) ? 0 : 1;
623 long t[2] = {notSame, cells}, tt[2];
624 MPI_Allreduce(t, tt, 2, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
627 " [steady_adv] advective V-cycle z = M^-1 r: %ld of %ld cells not "
628 "bitwise vs single rank\n",
630 CHECK(tt[0] == 0 && tt[1] == (
long)
N *
N *
N);
640 const double R = 16.0, D = 0.7,
U = 10.0 * D /
R, cx = 19.83, cy = 20.27;
641 std::vector<double> gsdf((std::size_t)
N *
N *
N);
642 for (
int z = 0; z <
N; ++z)
643 for (
int y = 0; y <
N; ++y)
644 for (
int x = 0; x <
N; ++x)
645 gsdf[(std::size_t)x + (std::size_t)y *
N + (std::size_t)z *
N *
N] =
646 R - std::sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
653 for (
int f = 0; f < 4; ++f)
654 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
657 std::vector<double> w((std::size_t)B.l[0] * B.l[1] * B.l[2]);
658 for (
int z = 0; z < B.l[2]; ++z)
659 for (
int y = 0; y < B.l[1]; ++y)
660 for (
int x = 0; x < B.l[0]; ++x) {
661 const double dx = x + B.o[0] - cx, dy = y + B.o[1] - cy;
662 w[(std::size_t)x + (std::size_t)y * B.l[0] + (std::size_t)z * B.l[0] * B.l[1]] =
663 std::fmax(2.0 *
U * (1.0 - (dx * dx + dy * dy) / (
R *
R)), 0.0);
666 s.addScalar(
"c", D, 1, 50,
true);
667 for (
int f = 0; f < 4; ++f)
668 s.setScalarBc(
"c", f, 1, 0.0);
669 s.setScalarMeanGradient(
"c", {0.0, 0.0, 1.0});
673 s.solveScalarSteady(
"c");
674 r.iters.push_back(s.scalarField(
"c").cut->iterations);
676 r.flux.push_back(s.scalarMeanFlux(
"c")[2]);
680 auto& sc = s.scalarField(
"c");
681 auto hb = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sc.b);
682 const int ext[3] = {s.nx() + 2 *
G, s.ny() + 2 *
G, s.nz() + 2 *
G};
684 for (
int z =
G; z < ext[2] -
G; ++z)
685 for (
int y =
G; y < ext[1] -
G; ++y)
686 for (
int x =
G; x < ext[0] -
G; ++x)
687 g += std::fabs(hb(x + (
long)y * ext[0] + (
long)z * ext[0] * ext[1]));
689 MPI_Allreduce(&g, &gg, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
690 r.identity.push_back(s.scalarBudget(
"c").identityError / gg);
691 const auto& st = *sc.cut;
694 " [g8] census max_cell_peclet %.6g, singular %d, %d levels, %d iterations, "
695 "mean flux z %.15e\n",
696 st.maxCellPeclet, (
int)st.singular, st.mgLevels, st.iterations, r.flux.back());
697 CHECK(st.advecting && st.steady && st.singular && st.meanGradOn);
700 false,
true,
true,
nullptr, 1e-11);
709 const double R = 8.0, Ro = 16.0, c0[3] = {19.83, 20.17, 19.71};
710 const double mu = 2.6588142379399775 / (
R *
R);
711 std::vector<int> ni = {1, -1, -1, 3, -1, -1, 1, -1, -1, 34, 1, 2};
712 std::vector<double> nr(4 * 16, 0.0);
713 for (
int k = 0; k < 4; ++k) {
714 nr[16 * k + 14] = 1.0;
715 nr[16 * k + 15] = 1.0;
718 nr[16 + 0] = nr[16 + 1] = nr[16 + 2] = 1e3;
720 std::vector<int> ii = {0, -1, 3, -1};
721 std::vector<double> ir(2 * 18, 0.0);
722 for (
int k = 0; k < 2; ++k) {
723 for (
int d = 0; d < 3; ++d)
724 ir[18 * k + d] = c0[d];
725 ir[18 * k + 6] = 1.0;
726 ir[18 * k + 7] = 1.0;
728 peclet::core::decomp::BlockDecomposer<3> dec =
729 peclet::flow::CutcellMG::decomposition(
static_cast<std::size_t
>(
size_),
N,
N,
N);
730 const auto blk = dec.block(
rank_);
731 const Block B{{(int)blk.origin[0], (
int)blk.origin[1], (int)blk.origin[2]},
732 {(int)blk.size[0], (
int)blk.size[1], (int)blk.size[2]}};
737 for (
int f = 0; f < 6; ++f)
738 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
740 s.initMpi(
N,
N,
N, MPI_COMM_WORLD);
741 s.setScene(ni, nr, ii, ir,
false);
742 s.setSolidFromScene(
false);
743 s.addScalar(
"c", 1.0, 1, 50,
true);
744 for (
int f = 0; f < 6; ++f)
745 s.setScalarBc(
"c", f, 1, 0.0);
746 s.setScalarWall(
"c", 1, 0.0, 0.0, 1);
747 s.setScalarSolid(
"c", 10.0, 1.0, 1.0, 0.0, 0);
748 s.setScalarResidualTolerance(
"c", 1e-13);
750 for (
const char* nm : {
"c",
"c_solid"}) {
751 auto f = s.fieldView(nm);
752 auto h = Kokkos::create_mirror_view(f);
753 const long sy = b.l[0] + 2 *
G, sz = sy * (b.l[1] + 2 *
G);
754 for (
int z = 0; z < b.l[2]; ++z)
755 for (
int y = 0; y < b.l[1]; ++y)
756 for (
int x = 0; x < b.l[0]; ++x) {
757 const double dx = x + b.o[0] - c0[0], dy = y + b.o[1] - c0[1],
758 dz = z + b.o[2] - c0[2];
759 h((x +
G) + (y +
G) * sy + (z +
G) * sz) =
760 1.0 - (dx * dx + dy * dy + dz * dz) / (Ro * Ro);
762 Kokkos::deep_copy(f, h);
771 std::vector<int> itD, itR;
772 std::vector<double> fxD, fxR, idD, idR;
773 for (
int k = 0; k < 20; ++k) {
774 for (
int w = 0; w < 2; ++w) {
777 const auto fl = s.scalarWallFlux(
"c");
778 const auto bu = s.scalarBudget(
"c");
779 (w == 0 ? itD : itR).push_back(s.scalarField(
"c").cut->iterations);
780 (w == 0 ? fxD : fxR).push_back(fl[0]);
781 (w == 0 ? fxD : fxR).push_back(fl[1]);
782 (w == 0 ? idD : idR).push_back(bu.identityError / std::fabs(bu.dMass));
785 double gg[4] = {0, 0, 0, 0};
787 for (
int ph = 0; ph < 2; ++ph) {
788 const char* nm = ph == 0 ?
"c" :
"c_solid";
789 auto cd = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sd.
fieldView(nm));
790 auto cr = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sr.
fieldView(nm));
791 const long dsy = B.l[0] + 2 *
G, dsz = dsy * (B.l[1] + 2 *
G), rsy =
N + 2 *
G,
792 rsz = rsy * (
N + 2 *
G);
793 double md = 0.0, mr = 0.0;
795 for (
int z = 0; z < B.l[2]; ++z)
796 for (
int y = 0; y < B.l[1]; ++y)
797 for (
int x = 0; x < B.l[0]; ++x) {
798 const double a = cd((x +
G) + (y +
G) * dsy + (z +
G) * dsz);
800 cr((x + B.o[0] +
G) + (y + B.o[1] +
G) * rsy + (z + B.o[2] +
G) * rsz);
801 md = std::fmax(md, std::fabs(a - b));
802 mr = std::fmax(mr, std::fabs(b));
803 nsl +=
same(a, b) ? 0 : 1;
805 double g2[2] = {md, mr}, o2[2];
806 MPI_Allreduce(g2, o2, 2, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
808 gg[2 * ph + 1] = o2[1];
810 MPI_Allreduce(&nsl, &o, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
814 for (std::size_t k = 0; k < idD.size(); ++k)
815 idm = std::fmax(idm, std::fmax(std::fabs(idD[k]), std::fabs(idR[k])));
819 "[g6 case 1] np=%d: max|c - c_1| = %.2e (max %.3f), max|psi_s - psi_s1| = %.2e "
820 "(max %.3f), %ld cells not bitwise; %ld solid unknowns, %d levels\n",
821 size_, gg[0], gg[1], gg[2], gg[3], ns, st.numSolidUnknowns, st.mgLevels);
822 std::printf(
" iterations np:");
824 std::printf(
" %d", it);
825 std::printf(
" single:");
827 std::printf(
" %d", it);
828 std::printf(
"\n last fluxes np: %.15e %.15e single: %.15e %.15e\n", fxD[fxD.size() - 2],
829 fxD.back(), fxR[fxR.size() - 2], fxR.back());
830 std::printf(
" budget identity (rel to |d_mass|) <= %.1e\n", idm);
832 CHECK(st.conj && st.numSolidUnknowns > 0);
833 CHECK(itD.size() == itR.size());
837 for (std::size_t k = 0; k < itD.size(); ++k)
838 CHECK(itD[k] == itR[k]);
839 for (std::size_t k = 0; k < fxD.size(); ++k)
842 CHECK(gg[0] <= 1e-10 * gg[1]);
843 CHECK(gg[2] <= 1e-10 * gg[3]);
844 for (std::size_t k = 0; k < itD.size(); ++k)
845 CHECK(std::abs(itD[k] - itR[k]) <= 1);
846 for (std::size_t k = 0; k < fxD.size(); ++k)
847 CHECK(std::fabs(fxD[k] - fxR[k]) <= 1e-10 * std::fmax(std::fabs(fxR[k]), 1e-300));
853 MPI_Allreduce(&
failures, &all, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
855 std::printf(all ?
"%d failure(s)\n" :
"OK\n", all);