flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_scalar_cutcell_solve_mpi.cpp
Go to the documentation of this file.
1// Cut-cell scalar solve under MPI (doc/scalar_ibm_design.md §1.5, §5.1, §5.2, §5.4, §6; WO-3,
2// WO-4, WO-5, WO-5b, WO-5c, WO-6).
3//
4// np = 1, 2, 4 on the production ORB. Each problem is solved on the distributed solver and on a
5// single-rank reference of the same problem; the parallel contract of §5.4 (G10) is checked at
6// rtol 1e-13 (ruling Q-D of WO-4, the vardensity_mpi precedent): np = 1 in the MPI build is BITWISE
7// equal to the single-rank build (field, iterations, wall flux); np > 1: max |c - c_1| <= 1e-10
8// max |c_1|, iterations within +-1 per solve, the wall flux to the same bound; the budget identity
9// closes on every rank count. At the default rtol 1e-10 the np > 1 gap is the BiCGStab stopping
10// error (the V-cycle is decomposition-independent): `g1` is also run there and printed for
11// information only. Every solve runs the ScalarMG V-cycle of WO-4 (the transient steps have
12// dt D/h^2 > 1).
13//
14// mixed — an off-centre solid sphere, no-slip flow walls on +-x carrying scalar Dirichlet
15// values (one a per-face profile), y and z periodic: one steady solve with Dirichlet
16// walls (c = 1), then three backward-Euler steps with Robin walls and a source;
17// g1 — G1 at R/h = 16 (box 4R = 64, walls everywhere): a Dirichlet sphere c = 1, the
18// exact field R/r as the box Dirichlet profile on all six faces, steady;
19// singular — the periodic box, insulating walls with a flux and a source, steady: the
20// singular case of §5.1 (mean projection on every ScalarMG level, gauge kept);
21// g9 — G9 (WO-5): an annulus carrying a Gaussian blob by solid-body rotation, koren,
22// 50 backward-Euler steps with the explicit-implicit split of §6.3 (the small-cell
23// counts compared exactly across decompositions);
24// g9c — G9c (WO-5b): a channel with inflow / walls / outflow, a sphere and a cap cutting
25// the outflow face across the rank boundaries, the scalar advected by the full NS
26// step on the captured open-face flux, fou, 30 steps (small-cell counts exact);
27// g8 — G8 (WO-6): the Taylor-Aris closure in the mean-gradient mode (a pipe, Poiseuille
28// face velocities, insulating walls, G = e_z), steady, advecting and singular; the
29// compared output is the box-averaged flux along G (scalar_mean_flux).
30// g6 case 1 — G6 (WO-7): the conjugate composite sphere (a scene: a conjugate core inside an
31// immersed Dirichlet sphere), 20 backward-Euler steps; both fields and both bodies'
32// fluxes compared per step.
33// steady_adv — G-adv(a) (WO-5c, design Amendment A2): the periodic C3 problem (a Dirichlet sphere
34// R/h = 16 in a 64^3 box with a source) advected by a projected Stokes field
35// rescaled to census Pe_h 1, steady: the advective V-cycle. Plus its z = M^-1 r
36// for a fixed global-index r, bitwise equal to the single-rank build at np 1, 2, 4.
37//
38// Plus the level table: ScalarMG's distributed table equals VelocityMG::initMpi's (in place).
39#include <mpi.h>
40
41#include <algorithm>
42#include <cmath>
43#include <cstdio>
44#include <cstring>
45#include <functional>
46#include <Kokkos_Core.hpp>
47#include <vector>
48
49#include "flow_ibm.hpp"
50#include "peclet/core/decomp/block_decomposer.hpp"
51
53static constexpr int G = IbmSolver::G;
54
55static int failures = 0;
56#define CHECK(cond) \
57 do { \
58 if (!(cond)) { \
59 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
60 ++failures; \
61 } \
62 } while (0)
63
64static bool same(double a, double b) {
65 return std::memcmp(&a, &b, sizeof(double)) == 0;
66}
67
68static int rank_ = 0, size_ = 1;
69
70struct Block {
71 int o[3], l[3];
72};
73
74struct Run {
75 std::vector<int> iters;
76 std::vector<double> flux; // the body flux after each recorded stage
77 std::vector<double> identity;
78};
79
80using FlowSetup = std::function<void(IbmSolver&)>;
81using ScalarSetup = std::function<void(IbmSolver&, const Block&)>;
82using Solve = std::function<Run(IbmSolver&)>;
83
84static std::vector<double> sphereSdf(int n, double cx, double cy, double cz, double R) {
85 std::vector<double> s((std::size_t)n * n * n);
86 for (int z = 0; z < n; ++z)
87 for (int y = 0; y < n; ++y)
88 for (int x = 0; x < n; ++x) {
89 const double dx = x - cx, dy = y - cy, dz = z - cz;
90 s[(std::size_t)x + (std::size_t)y * n + (std::size_t)z * n * n] =
91 std::sqrt(dx * dx + dy * dy + dz * dz) - R;
92 }
93 return s;
94}
95
96static void record(IbmSolver& s, Run& r, bool transient) {
97 r.iters.push_back(s.scalarField("c").cut->iterations);
98 r.flux.push_back(s.scalarWallFlux("c")[0]);
99 auto b = s.scalarBudget("c");
100 r.identity.push_back(b.identityError / std::fabs(transient ? b.dMass : b.wallIn));
101}
102
103using Post = std::function<void(IbmSolver&, IbmSolver&, const Block&)>;
104
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];
121 IbmSolver sd(B.l[0], B.l[1], B.l[2]);
122 // pre-flip default (Stokes) kept; NS physics: CHANGELOG "advection default"
123 sd.setAdvection(false);
124 flow(sd);
125 sd.initMpi(n, n, n, MPI_COMM_WORLD);
126 sd.setSolid(lsdf, cutcellPressure);
127 scalar(sd, B);
128 IbmSolver sr(n, n, n);
129 // pre-flip default (Stokes) kept; NS physics: CHANGELOG "advection default"
130 sr.setAdvection(false);
131 flow(sr);
132 sr.setSolid(gsdf, cutcellPressure);
133 scalar(sr, Block{{0, 0, 0}, {n, n, n}});
134 if (gated) { // ruling Q-D: G10 is gated at rtol 1e-13
135 sd.setScalarResidualTolerance("c", 1e-13);
136 sr.setScalarResidualTolerance("c", 1e-13);
137 }
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;
145 long notSame = 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);
151 maxDiff = std::fmax(maxDiff, std::fabs(a - b));
152 maxRef = std::fmax(maxRef, std::fabs(b));
153 notSame += same(a, b) ? 0 : 1;
154 }
155 double g[2] = {maxDiff, maxRef}, gg[2];
156 MPI_Allreduce(g, gg, 2, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
157 long ns = 0;
158 MPI_Allreduce(&notSame, &ns, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
159 const int lvD = sd.scalarField("c").cut->mgLevels, lvR = sr.scalarField("c").cut->mgLevels;
160 if (rank_ == 0) {
161 std::printf(
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,
165 lvR);
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());
173 double idm = 0.0;
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);
177 }
178 if (post)
179 post(sd, sr, B);
180 if (!gated)
181 return;
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);
186 }
187 if (size_ == 1) {
188 CHECK(ns == 0);
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)
192 CHECK(same(D.flux[k], R.flux[k]));
193 } else {
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]));
199 }
200 if (checkLevels) {
201 // ScalarMG's distributed level table == VelocityMG::initMpi(dec, ., comm, inPlace)'s
202 const auto& mg = *sd.scalarField("c").cut->mg;
204 const double w[3] = {1.0, 1.0, 1.0}, hp[3] = {1.0, 1.0, 1.0};
205 vmg.setMetric(w, hp);
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;
214 }
215 int all = eq ? 1 : 0, allMin = 0;
216 MPI_Allreduce(&all, &allMin, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
217 if (rank_ == 0)
218 std::printf(" level table: %d levels, %s VelocityMG::initMpi\n", mg.levels(),
219 allMin ? "==" : "DIFFERS from");
220 CHECK(allMin == 1);
221 }
222}
223
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);
229 {
230 // ---- mixed (the WO-3 problem) ----
231 {
232 const int N = 32;
233 auto profile = [](int y, int z) { return 0.4 + 0.1 * std::sin(0.3 * y) * std::cos(0.2 * z); };
234 compare(
235 "mixed", N, sphereSdf(N, 15.3, 16.1, 14.7, 9.3),
236 [](IbmSolver& s) {
237 s.setRho(1.0);
238 s.setMu(1.0);
239 s.setDt(2.5);
240 s.setDomainBc(0, 1, 0.0, 0.0, 0.0);
241 s.setDomainBc(1, 1, 0.0, 0.0, 0.0);
242 },
243 [&](IbmSolver& s, const Block& B) {
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]);
251 },
252 [](IbmSolver& s) {
253 Run r;
254 s.setScalarWall("c", 1, 1.0, 0.0, -1);
255 s.solveScalarSteady("c");
256 record(s, r, false);
257 s.setScalarWall("c", 2, 0.3, 0.7, -1);
258 s.setScalarSource("c", 0.1);
259 for (int k = 0; k < 3; ++k) {
260 s.advanceScalars();
261 record(s, r, true);
262 }
263 return r;
264 },
265 true);
266 }
267 // ---- G10 on G1 (R/h = 16, box 4R) ----
268 {
269 const int N = 64;
270 const double R = 16.0, c0[3] = {31.87, 32.21, 31.66};
271 for (const bool gated : {true, false})
272 compare(
273 "g1", N, sphereSdf(N, c0[0], c0[1], c0[2], R),
274 [](IbmSolver& s) {
275 s.setRho(1.0);
276 s.setMu(1.0);
277 s.setDt(1.0);
278 for (int f = 0; f < 6; ++f)
279 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
280 },
281 [&](IbmSolver& s, const Block& B) {
282 s.addScalar("c", 0.7, 1, 50, true);
283 for (int f = 0; f < 6; ++f) {
284 const int a = f / 2;
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) {
290 double p[3];
291 p[a] = xa;
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;
298 }
299 s.setScalarBc("c", f, 2, 0.0);
300 s.setScalarBcProfile("c", f, prof, B.l[t1], B.l[t2]);
301 }
302 s.setScalarWall("c", 1, 1.0, 0.0, -1);
303 },
304 [](IbmSolver& s) {
305 Run r;
306 s.solveScalarSteady("c");
307 record(s, r, false);
308 return r;
309 },
310 false, gated);
311 }
312 // ---- G10 on the singular case (periodic, insulating + flux + source, steady) ----
313 {
314 const int N = 32;
315 compare(
316 "singular", N, sphereSdf(N, 15.3, 16.1, 14.7, 9.3),
317 [](IbmSolver& s) {
318 s.setRho(1.0);
319 s.setMu(1.0);
320 s.setDt(1.0);
321 },
322 [](IbmSolver& s, const Block&) {
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);
326 },
327 [](IbmSolver& s) {
328 Run r;
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);
333 return r;
334 });
335 }
336 // ---- G10 on G9 (WO-5): the annulus, solid-body rotation, koren, 50 steps ----
337 {
338 // z-invariant annulus R_i = 6.4 < r < R_o = 16 cells in a 36^3 periodic box; the face
339 // velocities are node-potential differences of the clamped stream function, set to the
340 // wall value on both nodes of every face the projection closes (the Python G9's field), so
341 // the flux is discretely divergence-free in the predicate's openness. D = 0, bulk Courant
342 // 0.60 (Koren: 50 steps stay bounded; forward Euler + Koren is TVD only to 1/2).
343 const int N = 36;
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);
352 }
353 auto flowG9 = [](IbmSolver& s) {
354 s.setRho(1.0);
355 s.setMu(1.0);
356 s.setDt(1.0);
357 };
358 // the global face velocities, from the projection openness of a single-rank build (a pure
359 // function of the SDF, bitwise equal across decompositions)
360 std::vector<double> vx((std::size_t)N * N * N), vy((std::size_t)N * N * N);
361 {
362 IbmSolver g(N, N, N);
363 g.setAdvection(false);
364 flowG9(g);
365 g.setSolid(gsdf, true); // the projection's openness needs the cut-cell operator
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;
369 };
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) { // node (x - 1/2, y - 1/2)
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);
379 }
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;
387 }
388 }
389 compare(
390 "g9", N, gsdf, flowG9,
391 [&](IbmSolver& s, const Block& B) {
392 s.addScalar("c", 0.0, 1, 50, true); // koren
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;
403 u[l] = vx[k];
404 v[l] = vy[k];
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;
407 }
408 s.setField("u", u);
409 s.setField("v", v);
410 s.setField("w", w);
411 s.setField("c", c);
412 },
413 [](IbmSolver& s) {
414 Run r;
415 for (int k = 0; k < 50; ++k) {
416 s.advanceScalars();
417 const auto& st = *s.scalarField("c").cut;
418 r.iters.push_back(st.iterations);
419 // the classification must not depend on the decomposition: compared exactly
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);
423 }
424 const auto& st = *s.scalarField("c").cut;
425 if (rank_ == 0)
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);
429 return r;
430 },
431 false, true, true);
432 }
433 // ---- G10 on G9c (WO-5b): open domain faces, the scalar on flow's own projection ----
434 {
435 // A channel along z in a 32^3 box: inflow w = 1 on -z, outflow on +z, no-slip x and y walls;
436 // a sphere off the axis and a cap (a sphere centred just outside the outlet, cutting the
437 // outflow face) whose slivers straddle the rank boundaries of np = 2 and 4 (the ORB splits x
438 // first, then y), so the captured outflow plane's transverse ghost rows enter the small flags
439 // of ghost layer 1 (§6.3). fou at bulk Courant ~1.1 (small cells occur, in the outlet layer
440 // too: the cap was chosen for it by a scan of its centre and radius), D = 0.3, c0 = 0.2,
441 // Dirichlet 1 at the inlet, zero-gradient exit; 30 steps of the full NS step.
442 const int N = 32;
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)) -
449 7.3;
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)) -
452 8.1;
453 gsdf[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * N] = std::fmin(a, b);
454 }
455 auto flowG9c = [](IbmSolver& s) {
456 s.setRho(1.0);
457 s.setMu(1.6);
458 s.setDt(0.45);
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); // -z inflow
462 s.setDomainBc(5, 3, 0.0, 0.0, 0.0); // +z outflow
463 };
464 int call = 0; // compare() solves the distributed run first, then the single-rank reference
465 compare(
466 "g9c", N, gsdf, flowG9c,
467 [&](IbmSolver& s, const Block& B) {
468 s.addScalar("c", 0.3, 0, 50, true); // fou
469 for (int f = 0; f < 4; ++f)
470 s.setScalarBc("c", f, 1, 0.0); // neumann
471 s.setScalarBc("c", 4, 2, 1.0); // dirichlet inflow
472 s.setScalarBc("c", 5, 1, 0.0); // zero-gradient exit
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;
477 (void)B;
478 s.setField("c", c);
479 },
480 [&](IbmSolver& s) {
481 const bool ref = call++ == 1;
482 Run r;
483 long outletSmall = 0, small = 0, impl = 0;
484 for (int k = 0; k < 30; ++k) {
485 s.step();
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);
493 // small cells in the outlet layer (the implicit open-face rows of §1.4 are exercised)
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;
500 }
501 if (rank_ == 0)
502 std::printf(
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);
507 if (ref) // the implicit open-face rows (an outflow face of a small cell) are exercised
508 CHECK(outletSmall > 0);
509 return r;
510 },
511 false, true, true);
512 }
513 // ---- G10 on G-adv(a) (WO-5c, Amendment A2): steady advection, the advective V-cycle ----
514 {
515 // The C3 problem: a periodic 64^3 box, a Dirichlet sphere R = 16 with a source, D = 0.7;
516 // the face velocities are the projected Stokes field of a SINGLE-RANK build (20 steps under
517 // a body force: WO-5's harness), rescaled so that the census max_cell_peclet is 1, and set
518 // on every block (identical input on every decomposition).
519 const int N = 64;
520 const std::vector<double> gsdf = sphereSdf(N, 31.87, 31.28, 32.11, 16.0);
521 auto flowAdv = [](IbmSolver& s) {
522 s.setRho(1.0);
523 s.setMu(1.0);
524 s.setDt(1.0);
525 };
526 auto scalarAdv = [](IbmSolver& s) {
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);
530 };
531 std::vector<double> gv[3];
532 const char* nm[3] = {"u", "v", "w"};
533 {
534 IbmSolver g(N, N, N);
535 g.setAdvection(false);
536 flowAdv(g);
537 g.setSolid(gsdf, true);
538 g.setBodyForce(1e-3, 4e-4, 2e-4);
539 for (int k = 0; k < 20; ++k)
540 g.step();
541 scalarAdv(g);
542 g.solveScalarSteady("c"); // the probe: census max_cell_peclet of the unscaled field
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])
547 v *= k;
548 }
549 }
550 compare(
551 "steady_adv", N, gsdf, flowAdv,
552 [&](IbmSolver& s, const Block& B) {
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);
563 }
564 scalarAdv(s);
565 },
566 [](IbmSolver& s) {
567 Run r;
568 s.solveScalarSteady("c");
569 record(s, r, false);
570 // ruling D-WO5c-1: the steady identity relative to the GROSS budget (the sum of the
571 // terms' absolute values; the source is uniform, so its gross is |source_in|),
572 // bound 1e-11 (compare()'s identityBound below)
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;
577 if (rank_ == 0)
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);
581 return r;
582 },
583 false, true, true,
584 // z = M^-1 r of the advective V-cycle for a fixed global-index r: bitwise equal to the
585 // single-rank build on every decomposition (the in-place coarsening keeps children
586 // rank-local, the bands are pointwise, the colours come from global parity)
587 [&](IbmSolver& sd, IbmSolver& sr, const Block& B) {
588 auto zOf = [&](IbmSolver& s, const Block& b) {
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];
596 const double gk =
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))
599 : 0.0;
600 }
601 auto& sc = s.scalarField("c");
602 s.setField("c", rv);
603 const std::size_t n = sc.c.extent(0);
604 peclet::flow::CCField r("r", n), z("z", n);
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);
608 };
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);
618 const double b =
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;
621 ++cells;
622 }
623 long t[2] = {notSame, cells}, tt[2];
624 MPI_Allreduce(t, tt, 2, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
625 if (rank_ == 0)
626 std::printf(
627 " [steady_adv] advective V-cycle z = M^-1 r: %ld of %ld cells not "
628 "bitwise vs single rank\n",
629 tt[0], tt[1]);
630 CHECK(tt[0] == 0 && tt[1] == (long)N * N * N);
631 },
632 1e-11);
633 }
634 // ---- G10 on G8 (WO-6): the Taylor-Aris closure, steady, advecting, singular ----
635 {
636 // G8's pipe in a cubic box (z-invariant, so the cube is the same problem): R = 16 cells,
637 // insulating walls, x/y no-slip flow walls, z periodic; frozen Poiseuille z-face velocities
638 // at Pe = U R/D = 10 (census Pe_h 1.25) set on every block; mean gradient e_z.
639 const int N = 40;
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));
647 compare(
648 "g8", N, gsdf,
649 [](IbmSolver& s) {
650 s.setRho(1.0);
651 s.setMu(1.0);
652 s.setDt(1.0);
653 for (int f = 0; f < 4; ++f)
654 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
655 },
656 [&](IbmSolver& s, const Block& B) {
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);
664 }
665 s.setField("w", w);
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});
670 },
671 [](IbmSolver& s) {
672 Run r;
673 s.solveScalarSteady("c");
674 r.iters.push_back(s.scalarField("c").cut->iterations);
675 // the compared "flux" is the closure's output: the box-averaged flux along G
676 r.flux.push_back(s.scalarMeanFlux("c")[2]);
677 // ruling D-WO5c-1: the steady identity relative to the GROSS budget; every net term
678 // is 0 here (insulating, no source, periodic along G), the gross is V sum |b| (b the
679 // mean-gradient terms of the right-hand side; V = 1 in cell units)
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};
683 double g = 0.0;
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]));
688 double gg = 0.0;
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;
692 if (rank_ == 0)
693 std::printf(
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);
698 return r;
699 },
700 false, true, true, nullptr, 1e-11);
701 }
702 // ---- G10 on G6 case 1 (WO-7): the conjugate composite sphere, 20 backward-Euler steps ----
703 // A conjugate core (instance 0: Lam_s = 10, C_s K = 1, K = 1, R_c = 0) inside an immersed
704 // Dirichlet sphere (instance 1, psi = 0), walls on every face, cell units, R = 8, Ro = 16,
705 // dt mu = 1. Both fields and both bodies' fluxes compared per step (the two-field Krylov, the
706 // coupled two-field V-cycle, the solid probes and the material halo under the decomposition).
707 {
708 const int N = 40;
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); // case 1, test_scalar_cutcell_gates.g6_exact
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;
716 }
717 nr[0] = R;
718 nr[16 + 0] = nr[16 + 1] = nr[16 + 2] = 1e3;
719 nr[32 + 0] = Ro;
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;
727 }
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]}};
733 auto build = [&](IbmSolver& s, const Block& b, bool dist) {
734 s.setRho(1.0);
735 s.setMu(1.0);
736 s.setDt(1.0 / mu);
737 for (int f = 0; f < 6; ++f)
738 s.setDomainBc(f, 1, 0.0, 0.0, 0.0);
739 if (dist)
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);
749 // a smooth positive initial state in both fields: 1 - (r/Ro)^2
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);
761 }
762 Kokkos::deep_copy(f, h);
763 }
764 };
765 IbmSolver sd(B.l[0], B.l[1], B.l[2]);
766 sd.setAdvection(false);
767 build(sd, B, true);
768 IbmSolver sr(N, N, N);
769 sr.setAdvection(false);
770 build(sr, Block{{0, 0, 0}, {N, N, N}}, false);
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) {
775 IbmSolver& s = w == 0 ? sd : sr;
776 s.advanceScalars();
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));
783 }
784 }
785 double gg[4] = {0, 0, 0, 0};
786 long ns = 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;
794 long nsl = 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);
799 const double b =
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;
804 }
805 double g2[2] = {md, mr}, o2[2];
806 MPI_Allreduce(g2, o2, 2, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
807 gg[2 * ph] = o2[0];
808 gg[2 * ph + 1] = o2[1];
809 long o = 0;
810 MPI_Allreduce(&nsl, &o, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
811 ns += o;
812 }
813 double idm = 0.0;
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])));
816 const auto& st = *sd.scalarField("c").cut;
817 if (rank_ == 0) {
818 std::printf(
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:");
823 for (int it : itD)
824 std::printf(" %d", it);
825 std::printf(" single:");
826 for (int it : itR)
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);
831 }
832 CHECK(st.conj && st.numSolidUnknowns > 0);
833 CHECK(itD.size() == itR.size());
834 CHECK(idm <= 1e-12);
835 if (size_ == 1) {
836 CHECK(ns == 0);
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)
840 CHECK(same(fxD[k], fxR[k]));
841 } else {
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));
848 }
849 }
850 }
851 Kokkos::finalize();
852 int all = 0;
853 MPI_Allreduce(&failures, &all, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
854 if (rank_ == 0)
855 std::printf(all ? "%d failure(s)\n" : "OK\n", all);
856 MPI_Finalize();
857 return all ? 1 : 0;
858}
void setSolid(const std::vector< double > &sdfInner, bool cutcellPressure)
Host entry point: upload the inner SDF once and delegate.
ScalarField & scalarField(const std::string &name)
void setAdvection(bool on)
void setScalarResidualTolerance(const std::string &name, double rtol)
CCField fieldView(const std::string &name)
void setMetric(const double w[3], const double hp[3])
Per-axis metric of the physical domain (doc/anisotropic_metric.md §1.1): the weights w_a = 1/h_a'^2,...
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Kokkos::View< double *, CCMem > CCField
Solver< Staggered > IbmSolver
Definition post.py:1
Definition prof.py:1
std::vector< double > flux
std::vector< double > identity
std::shared_ptr< ScalarCutState > cut
static constexpr int N
static std::vector< double > sphereSdf()
static constexpr int G
static constexpr double U
peclet::flow::IbmSolver IbmSolver
static constexpr double R
std::function< void(IbmSolver &)> FlowSetup
static void record(IbmSolver &s, Run &r, bool transient)
static void compare(const char *tag, int n, const std::vector< double > &gsdf, FlowSetup flow, ScalarSetup scalar, Solve solve, bool checkLevels=false, bool gated=true, bool cutcellPressure=false, Post post=nullptr, double identityBound=1e-12)
#define CHECK(cond)
std::function< void(IbmSolver &, IbmSolver &, const Block &)> Post
std::function< void(IbmSolver &, const Block &)> ScalarSetup
static bool same(double a, double b)
static void build(IbmSolver &s, std::size_t cells)
static double maxDiff(const std::vector< double > &a, const std::vector< double > &b)