flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_scalar_cutcell_operator.cpp
Go to the documentation of this file.
1// Cut-cell scalar operator and solve (doc/scalar_ibm_design.md §1.4, §1.6, §4, §5.1; WO-3),
2// single rank. Structural properties of the assembled operator, independent of the accuracy gates
3// (tests/python/test_scalar_cutcell_gates.py):
4//
5// guard — ruling D-WO3-4: a face with a snapped aperture > 0 between an unknown and an
6// interior neighbour always has an unknown neighbour, so the band guard toward a
7// non-unknown cell is a no-op away from the domain boundary;
8// symmetry — the 7-point part is symmetric: AE(i) == AW(i + 1) (and N/S, T/B) BITWISE;
9// rows — identity rows are AC = 1, bands 0, rhs 0; steady Neumann rows sum to 0;
10// constants — Dirichlet walls at g = 1, steady: c == 1 (the probe weights sum to 1); transient,
11// insulating walls: a constant stays constant (§1.6.2);
12// singular — steady, insulating walls + flux + source: the incompatibility is reported, the
13// projected problem converges and the gauge keeps sum kappa V c (§5.1);
14// budget — transient and steady, with Robin walls, a source and Dirichlet domain faces: the
15// identity of §1.6.1 closes to round-off;
16// refusals — a scalar periodic face against a flow wall, and the reverse.
17#include <cmath>
18#include <cstdio>
19#include <Kokkos_Core.hpp>
20#include <stdexcept>
21#include <vector>
22
23#include "flow_ibm.hpp"
24
25namespace {
26int failures = 0;
27#define CHECK(cond) \
28 do { \
29 if (!(cond)) { \
30 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
31 ++failures; \
32 } \
33 } while (0)
34
36constexpr int G = IbmSolver::G;
37constexpr int N = 24;
38
39std::vector<double> sphereSdf(double cx, double cy, double cz, double R, bool solidInside) {
40 std::vector<double> s((std::size_t)N * N * N);
41 for (int z = 0; z < N; ++z)
42 for (int y = 0; y < N; ++y)
43 for (int x = 0; x < N; ++x) {
44 const double r = std::sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy) + (z - cz) * (z - cz));
45 s[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * N] =
46 solidInside ? r - R : R - r;
47 }
48 return s;
49}
50
51using HV = Kokkos::View<double*, Kokkos::HostSpace>;
52HV host(const peclet::flow::CCField& f) {
53 return Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
54}
55
56// A solver with the off-centre sphere; `walls` puts no-slip walls on +-x (else all periodic).
57void setup(IbmSolver& s, bool walls) {
58 s.setRho(1.0);
59 s.setMu(1.0);
60 s.setDt(0.8);
61 if (walls) {
62 s.setDomainBc(0, 1, 0, 0, 0);
63 s.setDomainBc(1, 1, 0, 0, 0);
64 }
65 s.setSolid(sphereSdf(11.3, 12.6, 11.9, 6.4, true), false);
66}
67
68void testStructure() {
69 IbmSolver s(N, N, N);
70 s.setAdvection(false);
71 setup(s, false);
72 s.addScalar("c", 1.3, 1, 50, true);
73 s.solveScalarSteady("c"); // insulating walls, no source: b = 0, assembled, nothing to solve
74 const auto& gm = s.scalarCutGeometry();
75 auto unk = host(gm.unknown);
76 const HV sa[3] = {host(gm.sax), host(gm.say), host(gm.saz)};
77 auto& sc = s.scalarField("c");
78 auto AC = host(sc.AC), b = host(sc.b);
79 const HV lo[3] = {host(sc.AW), host(sc.AS), host(sc.AB)};
80 const HV hi[3] = {host(sc.AE), host(sc.AN), host(sc.AT)};
81 const int ex = N + 2 * G;
82 const long st[3] = {1, ex, (long)ex * ex};
83 long guarded = 0, asym = 0, badId = 0, badSum = 0, nUnk = 0;
84 double maxRow = 0.0;
85 for (int z = G; z < N + G; ++z)
86 for (int y = G; y < N + G; ++y)
87 for (int x = G; x < N + G; ++x) {
88 const long i = (long)x + (long)y * st[1] + (long)z * st[2];
89 if (!(unk(i) > 0.5)) {
90 bool id = AC(i) == 1.0 && b(i) == 0.0;
91 for (int a = 0; a < 3; ++a)
92 id = id && lo[a](i) == 0.0 && hi[a](i) == 0.0;
93 badId += id ? 0 : 1;
94 continue;
95 }
96 ++nUnk;
97 double row = AC(i);
98 for (int a = 0; a < 3; ++a) {
99 row += lo[a](i) + hi[a](i);
100 // the periodic box: every neighbour is an interior (or periodic-image) cell
101 if (sa[a](i) > 0.0 && !(unk(i - st[a]) > 0.5))
102 ++guarded;
103 if (sa[a](i + st[a]) > 0.0 && !(unk(i + st[a]) > 0.5))
104 ++guarded;
105 const long j = i + st[a];
106 const int c[3] = {x, y, z};
107 if (c[a] + 1 < N + G && unk(j) > 0.5 && hi[a](i) != lo[a](j))
108 ++asym;
109 }
110 maxRow = std::fmax(maxRow, std::fabs(row) / AC(i));
111 if (std::fabs(row) > 1e-14 * AC(i))
112 ++badSum;
113 }
114 std::printf(
115 "structure: %ld unknowns, guard fires on %ld interior faces, %ld asymmetric pairs, "
116 "%ld bad identity rows, max |row sum|/AC %.1e\n",
117 nUnk, guarded, asym, badId, maxRow);
118 CHECK(nUnk > 0);
119 CHECK(guarded == 0);
120 CHECK(asym == 0);
121 CHECK(badId == 0);
122 CHECK(badSum == 0);
123}
124
125double maxDevUnknown(IbmSolver& s, double v) {
126 auto unk = host(s.scalarCutGeometry().unknown);
127 auto c = host(s.scalarField("c").c);
128 const int ex = N + 2 * G;
129 double m = 0.0;
130 for (int z = G; z < N + G; ++z)
131 for (int y = G; y < N + G; ++y)
132 for (int x = G; x < N + G; ++x) {
133 const long i = (long)x + (long)y * ex + (long)z * ex * ex;
134 if (unk(i) > 0.5)
135 m = std::fmax(m, std::fabs(c(i) - v));
136 }
137 return m;
138}
139
140void testConstants() {
141 { // Dirichlet walls at g = 1, steady: A 1 = b exactly up to round-off
142 IbmSolver s(N, N, N);
143 s.setAdvection(false);
144 setup(s, false);
145 s.addScalar("c", 0.9, 1, 50, true);
146 s.setScalarWall("c", 1, 1.0, 0.0, -1);
147 s.setScalarResidualTolerance("c", 1e-12);
148 s.setScalarMaxIterations("c", 3000);
149 s.solveScalarSteady("c");
150 const double d = maxDevUnknown(s, 1.0);
151 std::printf("constants: steady Dirichlet g = 1 -> max|c - 1| = %.2e (%d iterations)\n", d,
152 s.scalarField("c").cut->iterations);
153 CHECK(d <= 1e-9);
154 CHECK(s.scalarField("c").cut->converged);
155 }
156 { // transient, insulating walls: c == 5 stays
157 IbmSolver s(N, N, N);
158 s.setAdvection(false);
159 setup(s, false);
160 s.addScalar("c", 0.9, 1, 50, true);
161 std::vector<double> five((std::size_t)N * N * N, 5.0);
162 s.setField("c", five);
163 for (int k = 0; k < 3; ++k)
164 s.advanceScalars();
165 const double d = maxDevUnknown(s, 5.0);
166 std::printf("constants: transient insulating, c = 5 -> max|c - 5| = %.2e\n", d);
167 CHECK(d <= 1e-12);
168 }
169}
170
171void testSingular() {
172 IbmSolver s(N, N, N);
173 s.setAdvection(false);
174 setup(s, false);
175 s.addScalar("c", 0.9, 1, 50, true);
176 s.setScalarWall("c", 0, 0.1, 0.0, -1); // a flux into the fluid
177 s.setScalarSource("c", 0.05); // and a source: incompatible in a periodic box
178 s.setScalarMaxIterations("c", 3000);
179 s.solveScalarSteady("c");
180 const auto& st = *s.scalarField("c").cut;
181 auto unk = host(s.scalarCutGeometry().unknown);
182 auto kap = host(s.scalarCutGeometry().kappa);
183 auto c = host(s.scalarField("c").c);
184 double kc = 0.0, kac = 0.0;
185 const int ex = N + 2 * G;
186 for (int z = G; z < N + G; ++z)
187 for (int y = G; y < N + G; ++y)
188 for (int x = G; x < N + G; ++x) {
189 const long i = (long)x + (long)y * ex + (long)z * ex * ex;
190 if (unk(i) > 0.5) {
191 kc += kap(i) * c(i);
192 kac += kap(i) * std::fabs(c(i));
193 }
194 }
195 std::printf(
196 "singular: incompatibility %.3e, %d iterations, converged %d, residual %.2e, "
197 "sum kappa c = %.2e (sum kappa |c| = %.2e)\n",
198 st.incompatibility, st.iterations, (int)st.converged, st.residual, kc, kac);
199 CHECK(st.singular);
200 CHECK(st.incompatibility > 0.1);
201 CHECK(st.converged);
202 CHECK(std::fabs(kc) <= 1e-10 * kac);
203}
204
205void testBudget() {
206 for (int steady = 0; steady < 2; ++steady) {
207 IbmSolver s(N, N, N);
208 s.setAdvection(false);
209 setup(s, true);
210 s.addScalar("c", 1.1, 1, 50, true);
211 s.setScalarBc("c", 0, 2, 0.25);
212 s.setScalarBc("c", 1, 2, 1.5);
213 s.setScalarWall("c", 2, 2.0, 0.5, -1); // robin: k = 0.5, g = 2
214 s.setScalarSource("c", 0.3);
215 std::vector<double> init((std::size_t)N * N * N);
216 for (std::size_t k = 0; k < init.size(); ++k)
217 init[k] = 0.5 + 0.25 * std::sin(0.37 * (double)k);
218 s.setField("c", init);
219 if (steady)
220 s.solveScalarSteady("c");
221 else
222 for (int k = 0; k < 2; ++k)
223 s.advanceScalars();
224 const auto b = s.scalarBudget("c");
225 // ruling D-WO6-3 (the D-WO5c-1 convention): the identity relative to the GROSS budget, the
226 // sum of the terms' absolute values (the source is uniform: |source_in|), bound 1e-11 — it is
227 // reduction-order round-off and must not depend on the thread count. Cell units: the rates
228 // are internal, and a transient budget is in mass units (dt times the rates).
229 const double dtB = steady ? 1.0 : s.scalarField("c").cut->dt;
230 const double scale =
231 std::fabs(b.dMass) +
232 dtB * ((std::fabs(b.wallIn) + std::fabs(b.boundaryIn)) + std::fabs(b.sourceIn));
233 std::printf(
234 "budget (%s): d_mass %.6e wall %.6e boundary %.6e source %.6e defect %.2e "
235 "identity %.2e (rel %.1e)\n",
236 steady ? "steady" : "transient", b.dMass, b.wallIn, b.boundaryIn, b.sourceIn, b.defect,
237 b.identityError, std::fabs(b.identityError) / scale);
238 CHECK(std::fabs(b.identityError) <= 1e-11 * scale);
239 CHECK(std::fabs(b.wallIn) > 0.0 && std::fabs(b.boundaryIn) > 0.0 && b.sourceIn > 0.0);
240 // the per-body wall flux is the budget's wall term
241 const auto wf = s.scalarWallFlux("c");
242 CHECK(wf.size() == 1 && std::fabs(wf[0] - b.wallIn) <= 1e-13 * std::fabs(b.wallIn));
243 }
244}
245
246void testRefusals() {
247 { // scalar Dirichlet face against a periodic flow face
248 IbmSolver s(N, N, N);
249 s.setAdvection(false);
250 setup(s, false);
251 s.addScalar("c", 1.0, 1, 50, true);
252 s.setScalarBc("c", 2, 2, 1.0);
253 bool threw = false;
254 try {
255 s.advanceScalars();
256 } catch (const std::runtime_error&) {
257 threw = true;
258 }
259 CHECK(threw);
260 }
261 { // flow wall on -x, scalar left periodic
262 IbmSolver s(N, N, N);
263 s.setAdvection(false);
264 setup(s, true);
265 s.addScalar("c", 1.0, 1, 50, true);
266 bool threw = false;
267 try {
268 s.solveScalarSteady("c");
269 } catch (const std::runtime_error&) {
270 threw = true;
271 }
272 CHECK(threw);
273 }
274}
275} // namespace
276
277int main(int argc, char** argv) {
278 Kokkos::initialize(argc, argv);
279 {
280 testStructure();
281 testConstants();
282 testSingular();
283 testBudget();
284 testRefusals();
285 }
286 Kokkos::finalize();
287 std::printf(failures ? "%d failure(s)\n" : "OK\n", failures);
288 return failures ? 1 : 0;
289}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
Kokkos::View< double *, CCMem > CCField
Solver< Staggered > IbmSolver
int m
Definition post.py:35
steady(s, probe, maxit=3000, tol=1e-9)
static constexpr int N
static std::vector< double > sphereSdf()
static constexpr int G
static constexpr double AC
peclet::flow::IbmSolver IbmSolver
static constexpr double R
#define CHECK(cond)