22#include <Kokkos_Core.hpp>
34using MS = vf::MomentumScheme;
40 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
45const char* schemeName(MS s) {
51 case MS::MusclMixedCapped:
52 return "muscl_mixed_capped";
60 return std::sin(2 * M_PI * s) + 0.5 * std::cos(6 * M_PI * s);
66 vf::MomentumConsistentAdvector mom;
69 Box(
int nx,
int ny,
int nz,
double rhoG,
double rhoL, MS scheme) {
70 w.init(nx, ny, nz, 1.0, 3);
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;
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);
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));
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])];
120 long capped = 0, clamped = 0, floored = 0;
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) {
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);
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));
138 for (
long k = 0;
k <
nst; ++
k) {
140 b.mom.advect(
b.w, A, k);
141 }
catch (
const std::runtime_error& ex) {
145 std::printf(
" [%s ratio %g N %d] step %ld: %s\n", schemeName(scheme), ratio,
N, k,
151 r.capped +=
b.mom.cappedFaces();
152 r.clamped +=
b.mom.clampedFluxes();
153 r.floored +=
b.mom.diagnostics().floored;
156 const std::vector<double>
v =
b.inner(
b.mom.advectedVelocity(1));
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));
164 r.l1 =
s / (16.0 *
N);
173constexpr int kNs[3] = {32, 64, 128};
174constexpr double kRatios[3] = {1.0, 1
e3, 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}};
179void transverseSlab() {
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);
187 const double rel =
s.l1 / kApriori[
r][
k] - 1.0;
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);
196 const double ord = std::log2(l1[1][1] / l1[1][2]);
197 const double rr =
l1[2][2] /
l1[1][2];
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",
203 CHECK(rr >= 0.99 && rr <= 1.01);
204 const SlabResult ctl = slab(128, 1e4, MS::MusclMixed);
206 "G-M2-7 positive control: muscl_mixed ratio 1e4 N 128 L1 %.6e = %.2fx muscl_phase "
208 ctl.l1, ctl.l1 / l1[2][2], ctl.diverged >= 0 ?
" (left the Courant cap)" :
"");
209 if (ctl.diverged >= 0)
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]);
219double fieldU(
int c,
int x,
int y,
int z,
int n) {
220 const double t = 2 * M_PI /
n;
222 return 0.5 + 0.4 * std::sin(t * (y + 0.5)) * std::cos(t * z);
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);
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();
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)
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, 1
e3, 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);
260 for (
int c = 0;
c < 3; ++
c)
261 b.put(
b.w.faceVel(c), [&](
int,
int,
int) { return U[c]; });
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]));
269 std::printf(
"ID %-18s ratio %-6g 3 steps: max|u - U| = %.17g%s\n", schemeName(sch),
R, dev,
270 dev == 0.0 ?
" (BITWISE)" :
"");
277int main(
int argc,
char** argv) {
278 std::setvbuf(stdout,
nullptr, _IONBF, 0);
279 Kokkos::initialize(argc, argv);
290 std::fprintf(stderr,
"%d failure(s)\n",
failures);
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...
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
static constexpr double U
static constexpr double R