flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_full_stress.cpp
Go to the documentation of this file.
1// flow — the transpose half of the full viscous stress (src/full_stress.hpp): kernel vs a host
2// oracle, and the a-priori operator gate G1 of doc/full_viscous_stress.md §13:
3//
4// (o) the kernel against a straightforward host transcription of §5.1/§5.4 (random fields,
5// isotropic + anisotropic metric, all-fluid + random solid mask, BC row skip): bitwise on a
6// host backend, <= 1e-13 relative on a device backend (FMA contraction);
7// (a) rigid rotation, h ratio 1 : 1 : 1 and 1 : 1.5 : 2, mu sharp (cell-sampled sphere,
8// kappa = 0.1) and smeared (tanh, width 2h): max|L+T| <= 1e-12 max|L| and
9// max|L| > 1e3 max|L+T| (the defect exists and is cancelled);
10// (b) pure strain u = (g x, -g y, 0): (L+T)_x = 2 w_x g' (mu(i) - mu(i-s_x)), (L+T)_y likewise,
11// L alone half of it, to a relative 1e-13;
12// (c) simple shear u_x = g y: T_y = w_y g' (h_y/h_x)(beta_y(i,i+s_x) - beta_y(i,i-s_x)), L_y = 0;
13// (d) 100 random periodic (u, mu), mu ratio up to 1e3: telescoping |sum T_c| <= 1e-13 sum|T_c|,
14// W-symmetry |<v,Tu> - <u,Tv>| <= 1e-12 |<v,Tu>|, <u,(K+-T)u> >= -1e-12 <u,Ku> with K the
15// ibmBuildDiffusionVar bands at idiag = 0; with a random solid mask, T symmetry only;
16// (e) smooth mu and a solenoidal trigonometric u on the unit periodic cube: max-norm truncation
17// error of (L+T) against the analytic div(2 mu D), order >= 1.9 for 32->64 and 64->128; the
18// Laplacian alone does not converge (order < 0.5 on both pairs);
19// (f) uniform mu: muRangeInner returns min == max.
20//
21// L = -K u is the existing Laplacian part (the 7-band kernel, metric weights w_a); T carries the
22// weight of the component axis w_c. Index velocity v_a = u_a t_ref / h_a (t_ref = 1), w_a =
23// (h_ref/h_a)^2 with h_ref = min h.
24#include <algorithm>
25#include <cmath>
26#include <cstdio>
27#include <Kokkos_Core.hpp>
28#include <random>
29#include <type_traits>
30#include <vector>
31
32#include "cut_cell_ibm.hpp"
33#include "face_props.hpp"
34#include "full_stress.hpp"
35
36namespace {
37int failures = 0;
38#define CHECK(cond, ...) \
39 do { \
40 if (!(cond)) { \
41 std::fprintf(stderr, "CHECK failed: %s at %s:%d ", #cond, __FILE__, __LINE__); \
42 std::fprintf(stderr, __VA_ARGS__); \
43 std::fprintf(stderr, "\n"); \
44 ++failures; \
45 } \
46 } while (0)
47
48using namespace peclet::flow;
49using DV = Kokkos::View<double*, CCMem>;
50constexpr bool kHost = std::is_same_v<CCMem, Kokkos::HostSpace>;
51constexpr double kPi = 3.14159265358979323846;
52
53struct Box {
54 int n[3]; // inner cells
55 int g = 2;
56 double h[3];
57 int e[3];
58 long st[3];
59 long cells;
60 double w[3];
61 Box(int nx, int ny, int nz, double hx, double hy, double hz) {
62 n[0] = nx, n[1] = ny, n[2] = nz;
63 h[0] = hx, h[1] = hy, h[2] = hz;
64 for (int a = 0; a < 3; ++a)
65 e[a] = n[a] + 2 * g;
66 st[0] = 1, st[1] = e[0], st[2] = (long)e[0] * e[1];
67 cells = (long)e[0] * e[1] * e[2];
68 const double hr = std::min(hx, std::min(hy, hz));
69 for (int a = 0; a < 3; ++a)
70 w[a] = (hr / h[a]) * (hr / h[a]);
71 }
72 long idx(int x, int y, int z) const { return (long)x + (long)y * st[1] + (long)z * st[2]; }
73 bool inner(int x, int y, int z) const {
74 return x >= g && x < e[0] - g && y >= g && y < e[1] - g && z >= g && z < e[2] - g;
75 }
76 C3 c3() const { return C3{e[0], e[1], e[2]}; }
77 // Physical position of node (x, y, z) of component c (c = 3: the cell centre).
78 void pos(int c, int x, int y, int z, double* X) const {
79 const int q[3] = {x, y, z};
80 for (int a = 0; a < 3; ++a)
81 X[a] = (q[a] - g + ((a == c) ? 0.0 : 0.5)) * h[a];
82 }
83 // Periodic wrap of the ghost cells (host vector).
84 void wrap(std::vector<double>& f) const {
85 for (int z = 0; z < e[2]; ++z)
86 for (int y = 0; y < e[1]; ++y)
87 for (int x = 0; x < e[0]; ++x) {
88 if (inner(x, y, z))
89 continue;
90 auto wr = [&](int q, int a) { return g + (((q - g) % n[a]) + n[a]) % n[a]; };
91 f[idx(x, y, z)] = f[idx(wr(x, 0), wr(y, 1), wr(z, 2))];
92 }
93 }
94};
95
96DV toDev(const std::vector<double>& h, const char* name) {
97 DV d(name, h.size());
98 auto m = Kokkos::create_mirror_view(d);
99 for (std::size_t i = 0; i < h.size(); ++i)
100 m(i) = h[i];
101 Kokkos::deep_copy(d, m);
102 return d;
103}
104std::vector<double> toHost(const DV& d) {
105 auto m = Kokkos::create_mirror_view(d);
106 Kokkos::deep_copy(m, d);
107 std::vector<double> h(d.extent(0));
108 for (std::size_t i = 0; i < h.size(); ++i)
109 h[i] = m(i);
110 return h;
111}
112
113VarFaceProps props(const Box& b, const DV& mu, int c) {
114 VarFaceProps fp;
115 fp.haveMu = true;
116 fp.mu = CCConst(mu);
117 fp.msc = b.st[c];
118 fp.harmMu = false;
119 fp.haveRho = false;
120 fp.rhoIdtC = 0.0;
121 return fp;
122}
123
124// T_c from the kernel (rs = 1, bb = 0 on entry).
125std::vector<double> kernelT(const Box& b, int c, const std::vector<double>* v, const DV& mu,
126 const std::vector<std::uint8_t>* reg = nullptr, bool skipLo = false) {
127 DV bb("bb", b.cells), rs("rs", b.cells);
128 Kokkos::deep_copy(rs, 1.0);
129 const CCConst vv[3] = {CCConst(toDev(v[0], "v0")), CCConst(toDev(v[1], "v1")),
130 CCConst(toDev(v[2], "v2"))};
131 if (reg) {
132 RegConst rr[3];
133 for (int a = 0; a < 3; ++a) {
134 RegMask r("reg", b.cells);
135 auto m = Kokkos::create_mirror_view(r);
136 for (long i = 0; i < b.cells; ++i)
137 m(i) = reg[a][i];
138 Kokkos::deep_copy(r, m);
139 rr[a] = r;
140 }
141 addFullStressRhs<VarFaceProps, true>(c, bb, CCConst(rs), vv, props(b, mu, c), b.c3(), b.g,
142 b.w[c], skipLo, rr);
143 } else {
144 addFullStressRhs<VarFaceProps, false>(c, bb, CCConst(rs), vv, props(b, mu, c), b.c3(), b.g,
145 b.w[c], skipLo);
146 }
147 Kokkos::fence();
148 return toHost(bb);
149}
150
151// Host oracle: §5.1 / §5.4 transcribed literally.
152std::vector<double> oracleT(const Box& b, int c, const std::vector<double>* v,
153 const std::vector<double>& mu,
154 const std::vector<std::uint8_t>* reg = nullptr, bool skipLo = false) {
155 std::vector<double> T(b.cells, 0.0);
156 const long sc = b.st[c];
157 auto beta = [&](long i, long j) {
158 if (j - i == sc)
159 return mu[i];
160 if (i - j == sc)
161 return mu[j];
162 return 0.5 * (0.5 * (mu[i] + mu[j]) + 0.5 * (mu[i - sc] + mu[j - sc]));
163 };
164 auto R = [&](int a, long i) { return reg ? (bool)reg[a][i] : true; };
165 auto N = [&](long k) {
166 return R(c, k) && R(c, k + sc) ? beta(k, k + sc) * (v[c][k + sc] - v[c][k]) : 0.0;
167 };
168 auto Sp = [&](long i, int a) {
169 const long sa = b.st[a];
170 const bool inc = R(c, i) && R(c, i + sa) && R(a, i + sa) && R(a, i + sa - sc);
171 return inc ? beta(i, i + sa) * (v[a][i + sa] - v[a][i + sa - sc]) : 0.0;
172 };
173 auto Sm = [&](long i, int a) {
174 const long sa = b.st[a];
175 const bool inc = R(c, i) && R(c, i - sa) && R(a, i) && R(a, i - sc);
176 return inc ? beta(i, i - sa) * (v[a][i] - v[a][i - sc]) : 0.0;
177 };
178 const int a1 = (c == 0) ? 1 : 0, a2 = (c == 2) ? 1 : 2;
179 for (int z = b.g; z < b.e[2] - b.g; ++z)
180 for (int y = b.g; y < b.e[1] - b.g; ++y)
181 for (int x = b.g; x < b.e[0] - b.g; ++x) {
182 const int q[3] = {x, y, z};
183 if (skipLo && q[c] == b.g)
184 continue;
185 const long i = b.idx(x, y, z);
186 if (!R(c, i))
187 continue;
188 T[i] = b.w[c] * (((N(i) - N(i - sc)) + (Sp(i, a1) - Sm(i, a1))) + (Sp(i, a2) - Sm(i, a2)));
189 }
190 return T;
191}
192
193// L_c = -K_c v_c from the production 7-band kernel at idiag = 0 (metric weights on the bands).
194std::vector<double> laplacian(const Box& b, int c, const std::vector<double>& vc, const DV& mu) {
195 DV AC("AC", b.cells), AW("AW", b.cells), AE("AE", b.cells), AS("AS", b.cells), AN("AN", b.cells),
196 AB("AB", b.cells), AT("AT", b.cells);
197 ibmBuildDiffusionVar(AC, AW, AE, AS, AN, AB, AT, b.e[0], b.e[1], b.e[2], b.g, props(b, mu, c),
198 b.w[0], b.w[1], b.w[2]);
199 Kokkos::fence();
200 const auto ac = toHost(AC), aw = toHost(AW), ae = toHost(AE), as = toHost(AS), an = toHost(AN),
201 ab = toHost(AB), at = toHost(AT);
202 std::vector<double> L(b.cells, 0.0);
203 for (int z = b.g; z < b.e[2] - b.g; ++z)
204 for (int y = b.g; y < b.e[1] - b.g; ++y)
205 for (int x = b.g; x < b.e[0] - b.g; ++x) {
206 const long i = b.idx(x, y, z);
207 L[i] = -(ac[i] * vc[i] + aw[i] * vc[i - 1] + ae[i] * vc[i + 1] + as[i] * vc[i - b.st[1]] +
208 an[i] * vc[i + b.st[1]] + ab[i] * vc[i - b.st[2]] + at[i] * vc[i + b.st[2]]);
209 }
210 return L;
211}
212
213double maxAbsInner(const Box& b, const std::vector<double>& f) {
214 double m = 0.0;
215 for (int z = b.g; z < b.e[2] - b.g; ++z)
216 for (int y = b.g; y < b.e[1] - b.g; ++y)
217 for (int x = b.g; x < b.e[0] - b.g; ++x)
218 m = std::max(m, std::fabs(f[b.idx(x, y, z)]));
219 return m;
220}
221
222// --- (o) kernel vs oracle ------------------------------------------------------------------------
223void testOracle() {
224 std::mt19937_64 rng(7);
225 std::uniform_real_distribution<double> U(-1.0, 1.0), M(0.0, 1.0);
226 double worst = 0.0;
227 long bitDiff = 0;
228 for (int aniso = 0; aniso < 2; ++aniso)
229 for (int solid = 0; solid < 2; ++solid)
230 for (int skip = 0; skip < 2; ++skip) {
231 Box b(7, 6, 5, 1.0, aniso ? 1.5 : 1.0, aniso ? 2.0 : 1.0);
232 std::vector<double> v[3], mu(b.cells);
233 std::vector<std::uint8_t> reg[3];
234 for (int a = 0; a < 3; ++a) {
235 v[a].resize(b.cells);
236 reg[a].resize(b.cells);
237 for (long i = 0; i < b.cells; ++i) {
238 v[a][i] = U(rng);
239 reg[a][i] = M(rng) < 0.8 ? 1 : 0;
240 }
241 }
242 for (long i = 0; i < b.cells; ++i)
243 mu[i] = std::pow(10.0, 3.0 * M(rng));
244 const DV muD = toDev(mu, "mu");
245 for (int c = 0; c < 3; ++c) {
246 const auto k = kernelT(b, c, v, muD, solid ? reg : nullptr, skip);
247 const auto o = oracleT(b, c, v, mu, solid ? reg : nullptr, skip);
248 const double s = maxAbsInner(b, o);
249 for (long i = 0; i < b.cells; ++i) {
250 if (k[i] != o[i])
251 ++bitDiff;
252 worst = std::max(worst, std::fabs(k[i] - o[i]) / s);
253 }
254 }
255 }
256 std::printf("(o) kernel vs host oracle: %ld differing values, max rel %.3e (%s)\n", bitDiff,
257 worst, kHost ? "host: bitwise required" : "device: <= 1e-13");
258 if (kHost)
259 CHECK(bitDiff == 0, "kernel != oracle bitwise on host");
260 else
261 CHECK(worst <= 1e-13, "kernel vs oracle %.3e", worst);
262}
263
264// mu fields on a non-periodic box: sharp sphere (cell-sampled) or tanh, ratio kappa = 0.1.
265std::vector<double> muSphere(const Box& b, bool sharp) {
266 std::vector<double> mu(b.cells);
267 const double L[3] = {b.n[0] * b.h[0], b.n[1] * b.h[1], b.n[2] * b.h[2]};
268 const double R = 0.3 * std::min(L[0], std::min(L[1], L[2]));
269 const double hm = std::max(b.h[0], std::max(b.h[1], b.h[2]));
270 const double mo = 1.0, mi = 0.1;
271 for (int z = 0; z < b.e[2]; ++z)
272 for (int y = 0; y < b.e[1]; ++y)
273 for (int x = 0; x < b.e[0]; ++x) {
274 double X[3];
275 b.pos(3, x, y, z, X);
276 const double r = std::sqrt((X[0] - 0.47 * L[0]) * (X[0] - 0.47 * L[0]) +
277 (X[1] - 0.52 * L[1]) * (X[1] - 0.52 * L[1]) +
278 (X[2] - 0.5 * L[2]) * (X[2] - 0.5 * L[2]));
279 const double C =
280 sharp ? (r < R ? 1.0 : 0.0) : 0.5 * (1.0 - std::tanh((r - R) / (2.0 * hm)));
281 mu[b.idx(x, y, z)] = mi * C + mo * (1.0 - C);
282 }
283 return mu;
284}
285
286template <class F>
287void fillVel(const Box& b, std::vector<double>* v, F u) {
288 for (int c = 0; c < 3; ++c) {
289 v[c].assign(b.cells, 0.0);
290 for (int z = 0; z < b.e[2]; ++z)
291 for (int y = 0; y < b.e[1]; ++y)
292 for (int x = 0; x < b.e[0]; ++x) {
293 double X[3];
294 b.pos(c, x, y, z, X);
295 v[c][b.idx(x, y, z)] = u(c, X) / b.h[c]; // t_ref = 1
296 }
297 }
298}
299
300// --- (a) rigid rotation -------------------------------------------------------------------------
301void testRotation() {
302 for (int aniso = 0; aniso < 2; ++aniso)
303 for (int sharp = 0; sharp < 2; ++sharp) {
304 Box b(20, 18, 16, 0.1, aniso ? 0.15 : 0.1, aniso ? 0.2 : 0.1);
305 const auto mu = muSphere(b, sharp);
306 const DV muD = toDev(mu, "mu");
307 const double Om[3] = {0.7, -1.1, 1.3}, X0[3] = {0.9, 1.1, 0.7};
308 std::vector<double> v[3];
309 fillVel(b, v, [&](int c, const double* X) {
310 const double r[3] = {X[0] - X0[0], X[1] - X0[1], X[2] - X0[2]};
311 const double u[3] = {Om[1] * r[2] - Om[2] * r[1], Om[2] * r[0] - Om[0] * r[2],
312 Om[0] * r[1] - Om[1] * r[0]};
313 return u[c];
314 });
315 double mL = 0.0, mS = 0.0;
316 for (int c = 0; c < 3; ++c) {
317 const auto L = laplacian(b, c, v[c], muD), T = kernelT(b, c, v, muD);
318 std::vector<double> S(b.cells);
319 for (long i = 0; i < b.cells; ++i)
320 S[i] = L[i] + T[i];
321 mL = std::max(mL, maxAbsInner(b, L));
322 mS = std::max(mS, maxAbsInner(b, S));
323 }
324 std::printf("(a) rotation %s %s: max|L| %.3e max|L+T| %.3e ratio %.3e\n",
325 aniso ? "aniso 1:1.5:2" : "iso", sharp ? "sharp" : "tanh ", mL, mS, mS / mL);
326 CHECK(mS <= 1e-12 * mL && mL > 1e3 * mS, "rotation not cancelled");
327 }
328}
329
330// --- (b) pure strain, (c) simple shear ----------------------------------------------------------
331void testStrainShear() {
332 for (int aniso = 0; aniso < 2; ++aniso) {
333 Box b(12, 10, 8, 0.1, aniso ? 0.15 : 0.1, aniso ? 0.2 : 0.1);
334 const auto mu = muSphere(b, true);
335 const DV muD = toDev(mu, "mu");
336 const double gam = 0.375;
337 // (b) u = (g x, -g y, 0)
338 std::vector<double> v[3];
339 fillVel(b, v, [&](int c, const double* X) {
340 return c == 0 ? gam * X[0] : (c == 1 ? -gam * X[1] : 0.0);
341 });
342 double errS = 0.0, errL = 0.0;
343 for (int c = 0; c < 2; ++c) {
344 const auto L = laplacian(b, c, v[c], muD), T = kernelT(b, c, v, muD);
345 const double sg = (c == 0) ? gam : -gam;
346 double scale = 0.0;
347 for (int z = b.g; z < b.e[2] - b.g; ++z)
348 for (int y = b.g; y < b.e[1] - b.g; ++y)
349 for (int x = b.g; x < b.e[0] - b.g; ++x) {
350 const long i = b.idx(x, y, z);
351 const double ex = b.w[c] * sg * (mu[i] - mu[i - b.st[c]]);
352 scale = std::max(scale, std::fabs(ex));
353 errS = std::max(errS, std::fabs((L[i] + T[i]) - 2.0 * ex));
354 errL = std::max(errL, std::fabs(L[i] - ex));
355 }
356 errS /= scale;
357 errL /= scale;
358 }
359 // (c) u_x = g y
360 fillVel(b, v, [&](int c, const double* X) { return c == 0 ? gam * X[1] : 0.0; });
361 const auto Ly = laplacian(b, 1, v[1], muD), Ty = kernelT(b, 1, v, muD);
362 double errT = 0.0, scale = 0.0, mLy = 0.0;
363 for (int z = b.g; z < b.e[2] - b.g; ++z)
364 for (int y = b.g; y < b.e[1] - b.g; ++y)
365 for (int x = b.g; x < b.e[0] - b.g; ++x) {
366 const long i = b.idx(x, y, z);
367 const VarFaceProps fp = props(b, muD, 1); // host-side beta via the same formula
368 (void)fp;
369 const long sy = b.st[1];
370 auto beta = [&](long p, long q) {
371 return 0.5 * (0.5 * (mu[p] + mu[q]) + 0.5 * (mu[p - sy] + mu[q - sy]));
372 };
373 const double ex = b.w[1] * gam * (b.h[1] / b.h[0]) * (beta(i, i + 1) - beta(i, i - 1));
374 scale = std::max(scale, std::fabs(ex));
375 errT = std::max(errT, std::fabs(Ty[i] - ex));
376 mLy = std::max(mLy, std::fabs(Ly[i]));
377 }
378 errT /= scale;
379 std::printf(
380 "(b) strain %s: |L+T - 2x|/s %.3e |L - x|/s %.3e (c) shear: |T_y - id|/s %.3e "
381 "max|L_y| %.3e\n",
382 aniso ? "aniso" : "iso ", errS, errL, errT, mLy);
383 CHECK(errS <= 1e-13 && errL <= 1e-13, "pure strain identity");
384 CHECK(errT <= 1e-13 && mLy == 0.0, "simple shear identity");
385 }
386}
387
388// --- (d) random periodic: telescoping, W-symmetry, energy bounds ---------------------------------
389void testRandom() {
390 std::mt19937_64 rng(2026);
391 std::uniform_real_distribution<double> U(-1.0, 1.0), M(0.0, 1.0);
392 double wTel = 0.0, wSym = 0.0, wEn = 1e300, wSymS = 0.0;
393 for (int t = 0; t < 100; ++t) {
394 const bool aniso = (t % 2) == 1;
395 Box b(6, 8, 6, 1.0, aniso ? 1.5 : 1.0, aniso ? 2.0 : 1.0);
396 const double ratio = std::pow(10.0, 3.0 * M(rng)); // up to 1e3
397 std::vector<double> mu(b.cells), u[3], v[3];
398 for (long i = 0; i < b.cells; ++i)
399 mu[i] = 1.0 + (ratio - 1.0) * (M(rng) < 0.5 ? M(rng) : 0.0);
400 b.wrap(mu);
401 for (int c = 0; c < 3; ++c) {
402 u[c].resize(b.cells);
403 v[c].resize(b.cells);
404 for (long i = 0; i < b.cells; ++i) {
405 u[c][i] = U(rng);
406 v[c][i] = U(rng);
407 }
408 b.wrap(u[c]);
409 b.wrap(v[c]);
410 }
411 const DV muD = toDev(mu, "mu");
412 double vTu = 0.0, uTv = 0.0, uKu = 0.0, uTu = 0.0, sAbs = 0.0;
413 for (int c = 0; c < 3; ++c) {
414 const auto Tu = kernelT(b, c, u, muD), Tv = kernelT(b, c, v, muD);
415 const auto Lu = laplacian(b, c, u[c], muD);
416 double sum = 0.0, sab = 0.0;
417 for (long i = 0; i < b.cells; ++i) {
418 sum += Tu[i];
419 sab += std::fabs(Tu[i]);
420 vTu += v[c][i] * Tu[i] / b.w[c];
421 uTv += u[c][i] * Tv[i] / b.w[c];
422 uTu += u[c][i] * Tu[i] / b.w[c];
423 uKu += -u[c][i] * Lu[i] / b.w[c];
424 }
425 wTel = std::max(wTel, std::fabs(sum) / sab);
426 sAbs += sab;
427 }
428 wSym = std::max(wSym, std::fabs(vTu - uTv) / std::fabs(vTu));
429 wEn = std::min(wEn, std::min(uKu + uTu, uKu - uTu) / uKu);
430 // with a random solid mask: symmetry only
431 std::vector<std::uint8_t> reg[3];
432 for (int a = 0; a < 3; ++a) {
433 reg[a].resize(b.cells);
434 std::vector<double> r(b.cells);
435 for (long i = 0; i < b.cells; ++i)
436 r[i] = M(rng) < 0.85 ? 1.0 : 0.0;
437 b.wrap(r);
438 for (long i = 0; i < b.cells; ++i)
439 reg[a][i] = (std::uint8_t)r[i];
440 }
441 double svTu = 0.0, suTv = 0.0;
442 for (int c = 0; c < 3; ++c) {
443 const auto Tu = kernelT(b, c, u, muD, reg), Tv = kernelT(b, c, v, muD, reg);
444 for (long i = 0; i < b.cells; ++i) {
445 svTu += v[c][i] * Tu[i] / b.w[c];
446 suTv += u[c][i] * Tv[i] / b.w[c];
447 }
448 }
449 wSymS = std::max(wSymS, std::fabs(svTu - suTv) / std::fabs(svTu));
450 }
451 std::printf(
452 "(d) 100 random periodic: telescoping %.3e (<=1e-13) W-symmetry %.3e (<=1e-12) "
453 "min <u,(K+-T)u>/<u,Ku> %.3e (>=-1e-12) solid-mask symmetry %.3e (<=1e-12)\n",
454 wTel, wSym, wEn, wSymS);
455 CHECK(wTel <= 1e-13, "telescoping");
456 CHECK(wSym <= 1e-12, "W-symmetry");
457 CHECK(wEn >= -1e-12, "energy bound");
458 CHECK(wSymS <= 1e-12, "solid-mask symmetry");
459}
460
461// --- (e) smooth convergence ---------------------------------------------------------------------
462struct Smooth {
463 static constexpr double mu0 = 1.0, k = 2.0 * kPi;
464 // mu = mu0 (1 + 0.5 sin kx sin ky cos kz)
465 static double mu(const double* X) {
466 return mu0 * (1.0 + 0.5 * std::sin(k * X[0]) * std::sin(k * X[1]) * std::cos(k * X[2]));
467 }
468 static void gradMu(const double* X, double* g) {
469 const double sx = std::sin(k * X[0]), cx = std::cos(k * X[0]), sy = std::sin(k * X[1]),
470 cy = std::cos(k * X[1]), sz = std::sin(k * X[2]), cz = std::cos(k * X[2]);
471 g[0] = mu0 * 0.5 * k * cx * sy * cz;
472 g[1] = mu0 * 0.5 * k * sx * cy * cz;
473 g[2] = -mu0 * 0.5 * k * sx * sy * sz;
474 }
475 // u_c = A_c * (sin along c, cos along the others), A = (1, 1, -2): div u = 0.
476 static constexpr double A[3] = {1.0, 1.0, -2.0};
477 static double u(int c, const double* X) {
478 double p = A[c];
479 for (int a = 0; a < 3; ++a)
480 p *= (a == c) ? std::sin(k * X[a]) : std::cos(k * X[a]);
481 return p;
482 }
483 // d u_c / d x_a
484 static double du(int c, int a, const double* X) {
485 double p = A[c] * k;
486 for (int b = 0; b < 3; ++b) {
487 if (b == a)
488 p *= (b == c) ? std::cos(k * X[b]) : -std::sin(k * X[b]);
489 else
490 p *= (b == c) ? std::sin(k * X[b]) : std::cos(k * X[b]);
491 }
492 return p;
493 }
494 // div(2 mu D)_c = mu Lap u_c + sum_a d_a mu (d_a u_c + d_c u_a) (div u = 0, Lap u = -3k^2 u)
495 static double full(int c, const double* X) {
496 double g[3];
497 gradMu(X, g);
498 double f = mu(X) * (-3.0 * k * k) * u(c, X);
499 for (int a = 0; a < 3; ++a)
500 f += g[a] * (du(c, a, X) + du(a, c, X));
501 return f;
502 }
503};
504
505void testConvergence() {
506 const int Ns[3] = {32, 64, 128};
507 double eF[3], eL[3];
508 for (int r = 0; r < 3; ++r) {
509 const int N = Ns[r];
510 const double h = 1.0 / N;
511 Box b(N, N, N, h, h, h);
512 std::vector<double> mu(b.cells), v[3];
513 for (int z = 0; z < b.e[2]; ++z)
514 for (int y = 0; y < b.e[1]; ++y)
515 for (int x = 0; x < b.e[0]; ++x) {
516 double X[3];
517 b.pos(3, x, y, z, X);
518 mu[b.idx(x, y, z)] = Smooth::mu(X);
519 }
520 fillVel(b, v,
521 [](int c, const double* X) { return Smooth::u(c, X); }); // periodic by construction
522 const DV muD = toDev(mu, "mu");
523 eF[r] = eL[r] = 0.0;
524 for (int c = 0; c < 3; ++c) {
525 const auto L = laplacian(b, c, v[c], muD), T = kernelT(b, c, v, muD);
526 for (int z = b.g; z < b.e[2] - b.g; ++z)
527 for (int y = b.g; y < b.e[1] - b.g; ++y)
528 for (int x = b.g; x < b.e[0] - b.g; ++x) {
529 const long i = b.idx(x, y, z);
530 double X[3];
531 b.pos(c, x, y, z, X);
532 const double ex = Smooth::full(c, X);
533 // index units: (L+T) = h * div(2 mu D) with t_ref = 1, w = 1
534 eF[r] = std::max(eF[r], std::fabs((L[i] + T[i]) / h - ex));
535 eL[r] = std::max(eL[r], std::fabs(L[i] / h - ex));
536 }
537 }
538 }
539 const double oF1 = std::log2(eF[0] / eF[1]), oF2 = std::log2(eF[1] / eF[2]);
540 const double oL1 = std::log2(eL[0] / eL[1]), oL2 = std::log2(eL[1] / eL[2]);
541 std::printf(
542 "(e) smooth: full err %.3e %.3e %.3e order %.3f %.3f (>=1.9); Laplacian err %.3e "
543 "%.3e %.3e order %.3f %.3f (<0.5)\n",
544 eF[0], eF[1], eF[2], oF1, oF2, eL[0], eL[1], eL[2], oL1, oL2);
545 CHECK(oF1 >= 1.9 && oF2 >= 1.9, "full-stress order");
546 CHECK(oL1 < 0.5 && oL2 < 0.5, "Laplacian alone converges?");
547}
548
549// --- (f) uniform mu -----------------------------------------------------------------------------
550void testUniform() {
551 Box b(6, 5, 4, 1.0, 1.0, 1.0);
552 std::vector<double> mu(b.cells, 0.37);
553 double lo = 0.0, hi = 0.0;
554 muRangeInner(CCConst(toDev(mu, "mu")), b.c3(), b.g, lo, hi);
555 // a value in the read ring (g-1) and one outside it (0) must be seen / ignored
556 mu[b.idx(b.g - 1, b.g, b.g)] = 0.5;
557 mu[b.idx(0, 0, 0)] = 9.0;
558 double lo2 = 0.0, hi2 = 0.0;
559 muRangeInner(CCConst(toDev(mu, "mu")), b.c3(), b.g, lo2, hi2);
560 std::printf("(f) uniform: min %.17g max %.17g (equal: %s); ring value seen: max %.3g\n", lo, hi,
561 lo == hi ? "yes" : "NO", hi2);
562 CHECK(lo == hi && lo == 0.37, "uniform range");
563 CHECK(lo2 == 0.37 && hi2 == 0.5, "range covers [g-1, e-g+1) only");
564}
565} // namespace
566
567int main(int argc, char** argv) {
568 Kokkos::initialize(argc, argv);
569 {
570 testOracle();
571 testRotation();
572 testStrainShear();
573 testRandom();
574 testConvergence();
575 testUniform();
576 }
577 Kokkos::finalize();
578 std::printf("full_stress_operator: %s (%d failures)\n", failures ? "FAIL" : "PASS", failures);
579 return failures ? 1 : 0;
580}
flow — portable (Kokkos) Robust-Scaled cut-cell IBM primitives + per-cut-cell overlay build.
flow — face/cell material-property accessors for the variable-coefficient momentum operator.
flow — the transpose half of the full deviatoric viscous stress on the staggered grid.
y
Definition cmp.py:24
M
Definition conj.py:180
float solid
Definition owner_gate.py:49
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)
Kokkos::View< const std::uint8_t *, CCMem > RegConst
void addFullStressRhs(int c, CCField bb, CCConst rs, const CCConst(&v)[3], FaceProps fp, C3 e, int g, double wc, bool skipLoC, const RegConst(&reg)[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)³.
double maxAbsInner(CCConst a, C3 e, int g)
void muRangeInner(CCConst mu, C3 e, int g, double &lo, double &hi)
The local min and max of mu over [g−1, e−g+1)³ — the inner cells plus the one ghost ring the transpos...
Kokkos::View< std::uint8_t *, CCMem > RegMask
Per-component regular-node mask (§5.4): 1 = a velocity node with neither a solid nor a cut row.
std::vector< typename V::non_const_value_type > toHost(const V &f)
int m
Definition post.py:35
t
Definition post.py:21
worst
Definition stab5.py:6
static constexpr int N
static int wrap(int a, int n)
static constexpr double AC
#define CHECK(cond,...)
static constexpr double F
Kokkos::View< double *, IMem > DV
static double X0()
static constexpr double U
static constexpr double R