flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_march_state.cpp
Go to the documentation of this file.
1
13#include <algorithm>
14#include <array>
15#include <cmath>
16#include <cstdio>
17#include <Kokkos_Core.hpp>
18#include <stdexcept>
19#include <string>
20#include <vector>
21
22#include "flow_ibm.hpp"
23#include "peclet/core/geom/scene_builder.hpp"
24
25using peclet::core::solver::AndersonRole;
28
29namespace {
30
31int failures = 0;
32
33void check(bool ok, const std::string& what) {
34 std::printf(" [%s] %s\n", ok ? "ok" : "FAIL", what.c_str());
35 if (!ok)
36 ++failures;
37}
38
39constexpr int N = 12;
40
41std::vector<double> sphereSdf(double radius) {
42 std::vector<double> sdf((std::size_t)N * N * N);
43 const double c = 0.5 * N;
44 for (int z = 0; z < N; ++z)
45 for (int y = 0; y < N; ++y)
46 for (int x = 0; x < N; ++x) {
47 const double dx = x + 0.5 - c, dy = y + 0.5 - c, dz = z + 0.5 - c;
48 sdf[(std::size_t)x + (std::size_t)N * (y + (std::size_t)N * z)] =
49 std::sqrt(dx * dx + dy * dy + dz * dz) - radius;
50 }
51 return sdf;
52}
53
54std::vector<double> allFluid() {
55 return std::vector<double>((std::size_t)N * N * N, 1.0);
56}
57
58template <class S>
59void basics(S& s) {
60 s.setAdvection(false);
61 s.setRho(1.0);
62 s.setMu(1.0);
63 s.setDt(6.0);
64 s.setBodyForce(1e-3, 0.0, 0.0);
65 s.setPressurePcg(true, 200, 1e-8);
66}
67
69template <class S>
70void checkFields(S& s, const typename S::MarchState& ms, const std::vector<std::string>& names,
71 int carriedFrom, const std::string& tag) {
72 check(ms.fields.size() == names.size() && ms.roles.size() == names.size(),
73 tag + ": " + std::to_string(names.size()) + " fields and roles");
74 if (ms.fields.size() != names.size() || ms.roles.size() != names.size())
75 return;
76 const std::size_t nPad = (std::size_t)ms.e.x * ms.e.y * ms.e.z;
77 check(ms.G == 2 && ms.e.x == N + 4 && ms.e.y == N + 4 && ms.e.z == N + 4,
78 tag + ": e = n + 2G, G = 2");
79 for (std::size_t f = 0; f < names.size(); ++f) {
80 const AndersonRole want = (int)f < carriedFrom ? AndersonRole::Velocity : AndersonRole::Carried;
81 const bool same = ms.fields[f].data() == s.fieldView(names[f]).data();
82 check(ms.roles[f] == want && ms.fields[f].extent(0) == nPad && same,
83 tag + ": field " + std::to_string(f) + " is '" + names[f] + "' (" +
84 (want == AndersonRole::Velocity ? "Velocity" : "Carried") + ", padded)");
85 }
86}
87
88void gateRows() {
89 std::printf("rows of §3.1\n");
90 {
91 Stag s(N, N, N);
92 basics(s);
93 s.setAdvection(true);
94 s.setSolid(sphereSdf(3.0), true);
95 const auto ms = s.marchState();
96 checkFields(s, ms, {"u", "v", "w", "p"}, 3, "staggered + advection");
97 check(ms.signature[0] == 6.0 && ms.signature[1] == 1.0 && ms.signature[2] == 1.0 &&
98 ms.signature[3] == 1e-3 && ms.signature[4] == 0.0 && ms.signature[5] == 0.0,
99 "staggered: signature (dt, rho, mu, F) in cell units");
100 check(ms.signature[6] == 1.0 && ms.signature[7] == 0.0 && ms.signature[8] == 0.0,
101 "staggered: signature advection (on, scheme SOU, explicit)");
102 s.setAdvectionScheme(1);
103 s.setImplicitAdvection(true);
104 const auto ms2 = s.marchState();
105 check(ms2.signature[7] == 1.0 && ms2.signature[8] == 1.0,
106 "staggered: signature follows set_advection_scheme / set_implicit_advection");
107 }
108 {
109 Colo s(N, N, N); // AUTO scheme, Stokes: resolves to 'ghost' at set_solid
110 basics(s);
111 s.setAdvection(false);
112 s.setSolid(sphereSdf(3.0), true);
113 checkFields(s, s.marchState(), {"u", "v", "w", "p"}, 3, "collocated AUTO ghost, Stokes");
114 }
115 {
116 Colo s(N, N, N);
117 basics(s);
118 s.setCollocatedScheme("ghost");
119 s.setAdvection(true);
120 s.setUfAdvection(false);
121 s.setSolid(sphereSdf(3.0), true);
122 checkFields(s, s.marchState(), {"u", "v", "w", "p"}, 3,
123 "collocated ghost + advection, set_uf_advection(False)");
124 }
125 {
126 Colo s(N, N, N);
127 basics(s);
128 s.setCollocatedScheme("ghost");
129 s.setAdvection(true);
130 s.setSolid(sphereSdf(3.0), true);
131 const auto ms = s.marchState();
132 // uf/vf/wf are not in the field registry; compare them by position after u, v, w, p.
133 auto first4 = ms;
134 first4.fields.resize(std::min<std::size_t>(4, ms.fields.size()));
135 first4.roles.resize(std::min<std::size_t>(4, ms.roles.size()));
136 checkFields(s, first4, {"u", "v", "w", "p"}, 3,
137 "collocated ghost + projected-face advection (first 4)");
138 bool faces = ms.fields.size() == 7;
139 for (std::size_t f = 4; faces && f < 7; ++f)
140 faces = ms.roles[f] == AndersonRole::Carried &&
141 ms.fields[f].extent(0) == (std::size_t)ms.e.x * ms.e.y * ms.e.z;
142 if (faces) {
143 bool distinct = true; // three distinct buffers, none of them u, v, w or p
144 for (std::size_t f = 4; f < 7; ++f)
145 for (std::size_t g = 0; g < f; ++g)
146 distinct = distinct && ms.fields[f].data() != ms.fields[g].data();
147 faces = distinct;
148 }
149 check(faces, "collocated ghost + projected-face advection: + uf, vf, wf (Carried, padded)");
150 }
151}
152
155template <class S, class Make>
156void refusal(const std::string& tag, const std::string& token, Make make) {
157 S s(N, N, N);
158 basics(s);
159 std::string what;
160 try {
161 make(s);
162 (void)s.marchState();
163 } catch (const std::runtime_error& e) {
164 what = e.what();
165 }
166 const bool own = what.find(token) != std::string::npos;
167 const bool hint = what.find("pass accelerate=False") != std::string::npos;
168 check(own && hint, "refusal " + tag + ": \"" + what + "\"");
169}
170
171void sphereScene(Stag& s, double radius) {
172 namespace g = peclet::core::geom;
173 std::vector<int> ni(g::kNodeIntStride, 0), ii(g::kInstanceIntStride, 0);
174 std::vector<double> nr(g::kNodeRealStride, 0.0), ir(g::kInstanceRealStride, 0.0);
175 ni[0] = g::kSphere;
176 ni[1] = -1;
177 ni[2] = -1;
178 nr[0] = radius;
179 nr[14] = 1.0;
180 nr[15] = 1.0;
181 ii[0] = 0;
182 ii[1] = -1;
183 ir[0] = ir[1] = ir[2] = 0.5 * N;
184 ir[6] = 1.0;
185 ir[7] = 1.0;
186 s.setScene(ni, nr, ii, ir, /*periodic=*/true);
187 s.setSolidFromScene(true);
188}
189
190void gateRefusals() {
191 std::printf("refusals (§5.3)\n");
192 refusal<Colo>("1 collocated gauge-exact", "not the fluid-only 'ghost' projection", [](Colo& s) {
193 s.setCollocatedScheme("gauge-exact");
194 s.setSolid(sphereSdf(3.0), true);
195 });
196 refusal<Colo>("1 collocated embed", "not the fluid-only 'ghost' projection", [](Colo& s) {
197 s.setCollocatedScheme("embed");
198 s.setSolid(sphereSdf(3.0), true);
199 });
200 refusal<Stag>("2 VoF", "VoF", [](Stag& s) {
201 s.setPressureGeometry(allFluid());
202 s.enableVof();
203 });
204 refusal<Stag>("3 phase change", "phase change", [](Stag& s) {
205 s.setPressureGeometry(allFluid());
206 s.enablePhaseChange(1.0, 10.0, 1.0);
207 });
208 refusal<Stag>("4 scalar", "transported scalar", [](Stag& s) { s.addScalar("T", 0.1, 0, 1); });
209 refusal<Stag>("5 porous", "porous", [](Stag& s) { s.setPorousContinuity(true); });
210 refusal<Stag>("6 variable rho (closure)", "variable density or viscosity", [](Stag& s) {
211 s.addField("phase");
212 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "phase", "", {1.0, 0.0});
213 });
214 refusal<Stag>("6 mu field", "variable density or viscosity", [](Stag& s) { s.addField("mu"); });
215 refusal<Stag>("7 drag", "drag_beta", [](Stag& s) { s.enableDrag(); });
216 refusal<Stag>("8 cell force", "cell force", [](Stag& s) { s.enableCellForce(); });
217 refusal<Stag>("9 moving scene", "moving scene", [](Stag& s) {
218 sphereScene(s, 3.0);
219 s.setInstanceMotion(0, {1e-3, 0.0, 0.0}, {0.0, 0.0, 0.0}, nullptr);
220 });
221 refusal<Stag>("10 superficial velocity", "set_superficial_velocity",
222 [](Stag& s) { s.setSuperficialVelocity(true, 0, 1e-3); });
223 refusal<Stag>("11 pressure warm start", "warm start",
224 [](Stag& s) { s.setPressureWarmstart(true); });
225 refusal<Stag>("12 balanced-force projection", "balanced-force projection",
226 [](Stag& s) { s.setBalancedForceProjection(true); });
227 // A static scene is allowed.
228 {
229 Stag s(N, N, N);
230 basics(s);
231 sphereScene(s, 3.0);
232 bool ok = true;
233 try {
234 (void)s.marchState();
235 } catch (const std::exception&) {
236 ok = false;
237 }
238 check(ok, "a static scene is allowed");
239 }
240}
241
242} // namespace
243
244int main(int argc, char** argv) {
245 Kokkos::initialize(argc, argv);
246 {
247 gateRows();
248 gateRefusals();
249 }
250 Kokkos::finalize();
251 if (failures) {
252 std::fprintf(stderr, "\n%d CHECK(s) failed\n", failures);
253 return 1;
254 }
255 std::printf("\nAll march-state checks passed.\n");
256 return 0;
257}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
static constexpr int N
static std::vector< double > sphereSdf()
static void check(bool ok, const std::string &what)
peclet::flow::Solver< peclet::flow::Staggered > Stag
peclet::flow::Solver< peclet::flow::Colocated > Colo
static void sphereScene(std::vector< int > &ni, std::vector< double > &nr, std::vector< int > &ii, std::vector< double > &ir)
static bool same(double a, double b)