flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_momentum_slab.cpp
Go to the documentation of this file.
1// VoF momentum flux at second order (doc/vof_momentum_second_order.md, WO-3 (c)): the device
2// advector (`WyAdvector` + `MomentumConsistentAdvector`, no solver, no projection) against the 1D
3// a-priori operator test `tests/study/vof_mom2/apriori1d.py`.
4//
5// G-M2-7 TRANSVERSE-SLAB ORACLE. x-faces carry U = 1, y-faces v(x) = 0.1 v0(x), z-faces 0, with
6// v0(s) = sin 2 pi s + 0.5 cos 6 pi s; C is a y,z-uniform liquid slab [0.3, 0.7);
7// ny = nz = 4, periodic, a_x = U dt/h = 0.2, one period (5 N steps). The field
8// (U, v(x), 0) reduces the step EXACTLY to the 1D x-sweep: the y-sweep sees y-uniform
9// data (equal fluxes, zero dilation, zero dv) and the z-sweep has a = 0. After each step
10// the advected velocity is written back as the next step's face velocity (the transported
11// v is the state). Criteria (pre-registered, §9): the L1 error of v within 1 % of
12// apriori1d.py's at every (N, ratio); order 64 -> 128 >= 1.6 at ratio 1e3;
13// L1(1e4)/L1(1e3) in [0.99, 1.01]; capped == 0; positive control: 'muscl_mixed' at 1e4,
14// N = 128 has L1 >= 3x 'muscl_phase''s.
15// G-M2-9 SINGLE-PHASE INACTIVITY. C == 1 and C == 0, a non-uniform divergence-free velocity,
16// ratio 1e3: the cap never bites (capped == 0 every step, §4.6).
17// ID The uniform-velocity identity on the device advector, every scheme, ratios 1, 1e3,
18// 1e4, a sharp sphere: max|u - U| == 0.0 bitwise (G-M2-1's direct-advector twin).
19#include <algorithm>
20#include <cmath>
21#include <cstdio>
22#include <Kokkos_Core.hpp>
23#include <stdexcept>
24#include <vector>
25
26#include "vof/colour_field.hpp"
28
29namespace {
33namespace vf = peclet::flow::vof;
34using MS = vf::MomentumScheme;
35
36int failures = 0;
37#define CHECK(cond) \
38 do { \
39 if (!(cond)) { \
40 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
41 ++failures; \
42 } \
43 } while (0)
44
45const char* schemeName(MS s) {
46 switch (s) {
47 case MS::Upwind:
48 return "upwind";
49 case MS::MusclMixed:
50 return "muscl_mixed";
51 case MS::MusclMixedCapped:
52 return "muscl_mixed_capped";
53 case MS::MusclPhase:
54 return "muscl_phase";
55 }
56 return "?";
57}
58
59double v0(double s) {
60 return std::sin(2 * M_PI * s) + 0.5 * std::cos(6 * M_PI * s);
61}
62
63// A periodic g = 3 advector of nx*ny*nz unit cells (h = 1: dt in cell units is dt/h).
64struct Box {
65 vf::WyAdvector w;
66 vf::MomentumConsistentAdvector mom;
67 I3 n, e;
68 int g = 3;
69 Box(int nx, int ny, int nz, double rhoG, double rhoL, MS scheme) {
70 w.init(nx, ny, nz, 1.0, 3);
71 n = w.inner();
72 e = w.extent();
73 g = w.ghost();
74 const I3 ee = e;
75 const int gg = g;
76 w.exchange = [ee, gg](SField f) { vf::periodicFill(f, ee, gg, true, true, true); };
77 mom.init(w, rhoG, rhoL);
78 mom.momentumScheme = scheme;
79 }
80 // Inner values from f(x, y, z) (inner indices), then the periodic ghosts.
81 template <class Fn>
82 void put(SField dst, Fn f) {
83 auto h = Kokkos::create_mirror_view(dst);
84 Kokkos::deep_copy(h, dst);
85 for (int z = 0; z < n.z; ++z)
86 for (int y = 0; y < n.y; ++y)
87 for (int x = 0; x < n.x; ++x)
88 h(L3(g + x, g + y, g + z, e)) = f(x, y, z);
89 Kokkos::deep_copy(dst, h);
90 w.exchange(dst);
91 }
92 std::vector<double> inner(SField src) const {
93 auto h = Kokkos::create_mirror_view(src);
94 Kokkos::deep_copy(h, src);
95 std::vector<double> v((std::size_t)n.x * n.y * n.z);
96 for (int z = 0; z < n.z; ++z)
97 for (int y = 0; y < n.y; ++y)
98 for (int x = 0; x < n.x; ++x)
99 v[x + (std::size_t)n.x * (y + (std::size_t)n.y * z)] = h(L3(g + x, g + y, g + z, e));
100 return v;
101 }
102 // The advected velocity becomes the next step's face velocity. Control volume i of component c
103 // carries the advector face uf_c(i - s_c) (momentum_advect.hpp "Indexing"), so the inner face j
104 // takes CV j + s_c, wrapped periodically.
105 void feedBack() {
106 for (int c = 0; c < 3; ++c) {
107 const std::vector<double> u = inner(mom.advectedVelocity(c));
108 put(w.faceVel(c), [&](int x, int y, int z) {
109 int i[3] = {x, y, z};
110 const int nn[3] = {n.x, n.y, n.z};
111 i[c] = (i[c] + 1) % nn[c];
112 return u[i[0] + (std::size_t)n.x * (i[1] + (std::size_t)n.y * i[2])];
113 });
114 }
115 }
116};
117
118struct SlabResult {
119 double l1 = 0.0;
120 long capped = 0, clamped = 0, floored = 0;
121 long diverged = -1;
122};
123
124// One period of the transverse slab at resolution N, density ratio `ratio` (rho_g = 1).
125SlabResult slab(int N, double ratio, MS scheme) {
126 Box b(N, 4, 4, 1.0, ratio, scheme);
127 const double h = 1.0 / N, A = 0.2;
128 b.put(b.w.colour(), [&](int x, int, int) { // apriori1d.py's exact cell fractions
129 const double xl = x * h;
130 const double c = (std::fmin(xl + h, 0.7) - std::fmax(xl, 0.3)) / h;
131 return c < 0.0 ? 0.0 : (c > 1.0 ? 1.0 : c);
132 });
133 b.put(b.w.faceVel(0), [](int, int, int) { return 1.0; });
134 b.put(b.w.faceVel(1), [&](int x, int, int) { return 0.1 * v0((x + 0.5) * h); });
135 b.put(b.w.faceVel(2), [](int, int, int) { return 0.0; });
136 const long nst = std::lround(1.0 / (A * h));
137 SlabResult r;
138 for (long k = 0; k < nst; ++k) {
139 try {
140 b.mom.advect(b.w, A, k); // U = 1, h = 1: a_x = U dt/h = 0.2
141 } catch (const std::runtime_error& ex) {
142 // WyAdvector's Courant check: the transported v (|v0| <= 0.15) left the CFL cap, so the
143 // one-period L1 does not exist; recorded as unbounded (only the positive control is expected
144 // here: the mixed slope's stayer kick of note §2.2 is O(1) at ratio 1e4).
145 std::printf(" [%s ratio %g N %d] step %ld: %s\n", schemeName(scheme), ratio, N, k,
146 ex.what());
147 r.diverged = k;
148 r.l1 = INFINITY;
149 return r;
150 }
151 r.capped += b.mom.cappedFaces();
152 r.clamped += b.mom.clampedFluxes();
153 r.floored += b.mom.diagnostics().floored;
154 b.feedBack();
155 }
156 const std::vector<double> v = b.inner(b.mom.advectedVelocity(1));
157 double s = 0.0;
158 for (int z = 0; z < 4; ++z)
159 for (int y = 0; y < 4; ++y)
160 for (int x = 0; x < N; ++x) {
161 const double xc = (x + 0.5) * h;
162 s += std::fabs(v[x + (std::size_t)N * (y + 4 * z)] - 0.1 * v0(xc - nst * A / N));
163 }
164 r.l1 = s / (16.0 * N);
165 return r;
166}
167
168// apriori1d.py, `run(N, ratio, 'muscl_phase', lambda s: 0.1 * v0(s))` (wisp 0, as here), L1 of v
169// after one period; regenerate with
170// python -I -c "import apriori1d as A, numpy as np; v=lambda s: 0.1*A.v0(s); \
171// x,C,w,n,b=A.run(N,R,'muscl_phase',v); print(np.abs(w-v(x-n*A.A/N)).mean())"
172// run from tests/study/vof_mom2.
173constexpr int kNs[3] = {32, 64, 128};
174constexpr double kRatios[3] = {1.0, 1e3, 1e4};
175constexpr double kApriori[3][3] = {{1.1480651489e-02, 3.3904780129e-03, 1.0287161683e-03},
176 {1.2372109717e-02, 3.9895354338e-03, 1.2389030843e-03},
177 {1.2377683982e-02, 3.9924772277e-03, 1.2398866240e-03}};
178
179void transverseSlab() {
180 double l1[3][3];
181 long capped = 0;
182 for (int r = 0; r < 3; ++r)
183 for (int k = 0; k < 3; ++k) {
184 const SlabResult s = slab(kNs[k], kRatios[r], MS::MusclPhase);
185 l1[r][k] = s.l1;
186 capped += s.capped;
187 const double rel = s.l1 / kApriori[r][k] - 1.0;
188 std::printf(
189 "G-M2-7 muscl_phase ratio %-6g N %-4d L1 %.10e apriori %.10e rel %+.2e capped %ld "
190 "clamped %ld floored %ld\n",
191 kRatios[r], kNs[k], s.l1, kApriori[r][k], rel, s.capped, s.clamped, s.floored);
192 CHECK(std::fabs(rel) <= 0.01);
193 CHECK(s.capped == 0);
194 CHECK(s.floored == 0);
195 }
196 const double ord = std::log2(l1[1][1] / l1[1][2]);
197 const double rr = l1[2][2] / l1[1][2];
198 std::printf(
199 "G-M2-7 order 64->128 at 1e3: %.3f (>= 1.6) L1(1e4)/L1(1e3) at N 128: %.5f "
200 "([0.99, 1.01]) capped total %ld\n",
201 ord, rr, capped);
202 CHECK(ord >= 1.6);
203 CHECK(rr >= 0.99 && rr <= 1.01);
204 const SlabResult ctl = slab(128, 1e4, MS::MusclMixed);
205 std::printf(
206 "G-M2-7 positive control: muscl_mixed ratio 1e4 N 128 L1 %.6e = %.2fx muscl_phase "
207 "(>= 3)%s\n",
208 ctl.l1, ctl.l1 / l1[2][2], ctl.diverged >= 0 ? " (left the Courant cap)" : "");
209 if (ctl.diverged >= 0)
210 std::printf(
211 "G-M2-7 positive control: v left the Weymouth-Yue Courant cap at step %ld of %d "
212 "(the mixed slope's ratio-1e4 stayer gain, note §2.2)\n",
213 ctl.diverged, 5 * 128);
214 CHECK(ctl.l1 >= 3.0 * l1[2][2]);
215}
216
217// A non-uniform, discretely divergence-free field: each face component is independent of its own
218// coordinate. Courant <= 0.2 at dt/h = 0.2.
219double fieldU(int c, int x, int y, int z, int n) {
220 const double t = 2 * M_PI / n;
221 if (c == 0)
222 return 0.5 + 0.4 * std::sin(t * (y + 0.5)) * std::cos(t * z);
223 if (c == 1)
224 return -0.3 + 0.5 * std::cos(t * (z + 0.5)) + 0.2 * std::sin(2 * t * x);
225 return 0.2 + 0.6 * std::sin(t * (x + 0.5) + 0.3) * std::cos(t * y);
226}
227
228void singlePhase() {
229 const int n = 16, steps = 20;
230 for (MS sch : {MS::MusclPhase, MS::MusclMixedCapped})
231 for (double cval : {1.0, 0.0}) {
232 Box b(n, n, n, 1.0, 1e3, sch);
233 b.put(b.w.colour(), [&](int, int, int) { return cval; });
234 for (int c = 0; c < 3; ++c)
235 b.put(b.w.faceVel(c), [&](int x, int y, int z) { return fieldU(c, x, y, z, n); });
236 long worst = 0, total = 0;
237 for (int k = 0; k < steps; ++k) {
238 b.mom.advect(b.w, 0.2, k);
239 worst = std::max(worst, b.mom.cappedFaces());
240 total += b.mom.cappedFaces();
241 b.feedBack();
242 }
243 std::printf(
244 "G-M2-9 %-18s C == %.0f ratio 1e3, %d steps: capped per step max %ld, total %ld\n",
245 schemeName(sch), cval, steps, worst, total);
246 if (sch == MS::MusclPhase)
247 CHECK(total == 0);
248 }
249}
250
251void identity() {
252 const int n = 16;
253 const double U[3] = {1.0, 0.6, -0.4};
254 for (MS sch : {MS::Upwind, MS::MusclMixed, MS::MusclMixedCapped, MS::MusclPhase})
255 for (double R : {1.0, 1e3, 1e4}) {
256 Box b(n, n, n, 1.0, R, sch);
257 b.put(b.w.colour(), [&](int x, int y, int z) {
258 return vf::sphereCellFraction(0.5 * n, 0.5 * n, 0.5 * n, 0.3 * n, x, y, z, 1.0, 4);
259 });
260 for (int c = 0; c < 3; ++c)
261 b.put(b.w.faceVel(c), [&](int, int, int) { return U[c]; });
262 double dev = 0.0;
263 for (int k = 0; k < 3; ++k) {
264 b.mom.advect(b.w, 0.2, k);
265 for (int c = 0; c < 3; ++c)
266 for (double v : b.inner(b.mom.advectedVelocity(c)))
267 dev = std::fmax(dev, std::fabs(v - U[c]));
268 }
269 std::printf("ID %-18s ratio %-6g 3 steps: max|u - U| = %.17g%s\n", schemeName(sch), R, dev,
270 dev == 0.0 ? " (BITWISE)" : "");
271 CHECK(dev == 0.0);
272 }
273}
274
275} // namespace
276
277int main(int argc, char** argv) {
278 std::setvbuf(stdout, nullptr, _IONBF, 0);
279 Kokkos::initialize(argc, argv);
280 {
281 identity();
282 singlePhase();
283 transverseSlab();
284 }
285 Kokkos::finalize();
286 if (failures == 0) {
287 std::printf("OK\n");
288 return 0;
289 }
290 std::fprintf(stderr, "%d failure(s)\n", failures);
291 return 1;
292}
flow — VoF rung V2a (WO-J): the bridge between the solver's G = 2 block and the colour field's own g ...
flow — VoF rung V2b (WO-K): momentum-consistent transport of rho^c u_c on the half-shifted (MAC) mome...
y
Definition cmp.py:24
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
dict A
Definition post.py:33
t
Definition post.py:21
xc
Definition post.py:21
worst
Definition stab5.py:6
static constexpr int N
static constexpr double U
static constexpr double R
#define CHECK(cond)