flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_sharp_viscous_mpi.cpp
Go to the documentation of this file.
1// flow — the sharp viscous interface is decomposition-invariant BITWISE
2// (doc/vof_sharp_viscous_stress.md §2 "np 1/2/4 bitwise", §7, WO-5 / G-SV6 at the operator level).
3//
4// Everything the sharp interface adds is a local stencil with a fixed summation order: the band
5// classes (7-point colour stencil inside the depth-3 colour ghosts), the edge owners and fractions
6// (planes at distance <= 2), the tau_hat / D_nn fit of the owned interfacial cells on u^n (velocity
7// ghosts at depth 2 incl. edges), ONE exchange of tau_hat / D_nn, the implicit band and the
8// explicit band terms. So: a colour (a sphere, cell fractions from 4^3 samples of the GLOBAL
9// coordinates) and a u^n (a hash of the global index) identical for every decomposition are
10// written on the inner cells of every rank (NaN-poisoned ghosts), the production pieces run in the
11// order step() runs them (updateProperties, resolveSharpViscous, sharpViscousGeometry,
12// fullStressPrelude, sharpViscousFit, the bands of makeSharpFaceProps, addFullStressRhs with the
13// sharp face props + addSharpViscousRhs), and every inner value -- class, fallback flag, tau_hat,
14// D_nn, the 7 band coefficients and the explicit RHS of the 3 components -- is gathered and
15// compared bit for bit with a single-rank solver on the whole box, together with the owner-set
16// means of every edge of the owned cells (doc §5.8: F_a, F_c, theta_ac, theta_ca, T_a, T_c of the
17// three orientations). Three passes: triply periodic, no-slip walls on both y faces (the
18// domain-face rules), and (WO-9.2) triply periodic with a sphere centred on a grid VERTEX that lies
19// on the decomposition's first cut of every cut axis (a rank boundary on a symmetry plane) and on
20// the periodic z seam, so that tied edge owners straddle both; that pass must tie. np 4 must cut
21// two axes.
22#include <mpi.h>
23
24#include <cmath>
25#include <cstdint>
26#include <cstdio>
27#include <cstring>
28#include <Kokkos_Core.hpp>
29#include <limits>
30#include <vector>
31
32#include "flow_ibm.hpp"
33#include "peclet/core/decomp/block_decomposer.hpp"
34
40namespace sv = peclet::flow::vof;
41
42static constexpr int NX = 16, NY = 12, NZ = 10;
43static constexpr int G = IbmSolver::G;
44// per inner cell: cls, fb, tau(3), dnn, 3 x 7 bands, 3 rhs, 3 orientations x 6 edge means
45static constexpr int kVals = 48;
46
47static double urand(int id, int gx, int gy, int gz) {
48 gx = ((gx % NX) + NX) % NX;
49 gy = ((gy % NY) + NY) % NY;
50 gz = ((gz % NZ) + NZ) % NZ;
51 std::uint64_t z =
52 0x9E3779B97F4A7C15ULL * (std::uint64_t)(1 + id) + (std::uint64_t)(gx + NX * (gy + NY * gz));
53 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
54 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
55 z ^= z >> 31;
56 return (double)(z >> 11) * 0x1.0p-53;
57}
58
59// The sphere of the pass: centre (global index coordinates) and radius.
60static double gCen[3] = {7.3, 5.9, 4.6}, gR = 3.7;
61
62// The colour of global cell (gx, gy, gz): a sphere, 4^3 samples (a plane crossing the walls too),
63// periodic images included (the vertex-centred sphere straddles the z seam).
64static double colourValue(int gx, int gy, int gz) {
65 const double* c = gCen;
66 const double r = gR;
67 const double L[3] = {NX, NY, NZ};
68 int in = 0;
69 for (int k = 0; k < 4; ++k)
70 for (int j = 0; j < 4; ++j)
71 for (int i = 0; i < 4; ++i) {
72 double p[3] = {gx + (i + 0.5) / 4 - c[0], gy + (j + 0.5) / 4 - c[1],
73 gz + (k + 0.5) / 4 - c[2]};
74 for (int a = 0; a < 3; ++a)
75 p[a] -= L[a] * std::round(p[a] / L[a]);
76 const double x = p[0], y = p[1], z = p[2];
77 in += (x * x + y * y + z * z < r * r) ? 1 : 0;
78 }
79 return in / 64.0;
80}
81
82static std::vector<double> evalSharp(IbmSolver& s, const int o[3], const int ln[3]) {
83 const int e[3] = {ln[0] + 2 * G, ln[1] + 2 * G, ln[2] + 2 * G};
84 const long n = (long)e[0] * e[1] * e[2];
85 std::vector<double> col((long)ln[0] * ln[1] * ln[2]);
86 for (int z = 0; z < ln[2]; ++z)
87 for (int y = 0; y < ln[1]; ++y)
88 for (int x = 0; x < ln[0]; ++x)
89 col[x + (long)ln[0] * (y + (long)ln[1] * z)] = colourValue(o[0] + x, o[1] + y, o[2] + z);
90 s.setVof(col);
91 s.setPropertyModel("mu", ClosureKind::LinearMix, "C", "", {1.0, 1e-2 - 1.0});
92 s.setViscousStress("full");
93 s.setVofViscousInterface("sharp");
94 s.updateProperties();
95 s.resolveSharpViscous();
96 s.sharpViscousGeometry();
97 const auto st = s.sharpViscousState();
98 for (int c = 0; c < 3; ++c) {
99 auto h = Kokkos::create_mirror_view(st.old[c]);
100 for (int z = 0; z < e[2]; ++z)
101 for (int y = 0; y < e[1]; ++y)
102 for (int x = 0; x < e[0]; ++x) {
103 const bool inner =
104 x >= G && x < e[0] - G && y >= G && y < e[1] - G && z >= G && z < e[2] - G;
105 h((long)x + (long)y * e[0] + (long)z * e[0] * e[1]) =
106 inner ? 2.0 * urand(1 + c, o[0] + x - G, o[1] + y - G, o[2] + z - G) - 1.0
107 : std::numeric_limits<double>::quiet_NaN();
108 }
109 Kokkos::deep_copy(st.old[c], h);
110 }
111 s.fullStressPrelude(); // mu uniformity + the u^n ghosts incl. edges
112 s.sharpViscousFit();
113 s.fillMuGhosts();
114 Kokkos::fence();
115 const auto sv2 = s.sharpViscousState(); // after the fit: the geometry copy is final
116 std::vector<std::vector<double>> cols;
117 {
118 auto cl = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sv2.cls);
119 auto fb = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sv2.fb);
120 std::vector<double> a(n), b(n);
121 for (long i = 0; i < n; ++i) {
122 a[i] = cl(i);
123 b[i] = fb(i);
124 }
125 cols.push_back(a);
126 cols.push_back(b);
127 }
128 auto pull = [&](CCField f) {
129 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
130 cols.emplace_back(h.data(), h.data() + n);
131 };
132 for (int a = 0; a < 3; ++a)
133 pull(sv2.tau[a]);
134 pull(sv2.dnn);
135 CCField rs("sv_mpi_rs", n);
136 Kokkos::deep_copy(rs, 1.0);
137 const CCConst v[3] = {CCConst(sv2.old[0]), CCConst(sv2.old[1]), CCConst(sv2.old[2])};
138 const CCConst t[3] = {CCConst(sv2.tau[0]), CCConst(sv2.tau[1]), CCConst(sv2.tau[2])};
139 for (int c = 0; c < 3; ++c) {
140 CCField B[7];
141 for (auto& x : B)
142 x = CCField("sv_mpi_band", n);
143 peclet::flow::ibmBuildDiffusionVar(B[0], B[1], B[2], B[3], B[4], B[5], B[6], e[0], e[1], e[2],
144 G, s.makeSharpFaceProps(c, sv::SvRole::kImplicit), 1.0, 1.0,
145 1.0);
146 Kokkos::fence();
147 for (auto& x : B)
148 pull(x);
149 }
150 for (int c = 0; c < 3; ++c) {
151 CCField bb("sv_mpi_bb", n);
153 c, bb, CCConst(rs), v, s.makeSharpFaceProps(c, sv::SvRole::kExplicit), C3{e[0], e[1], e[2]},
154 G, 1.0, false);
155 sv::addSharpViscousRhs(c, bb, CCConst(rs), v, sv2.geo, t, CCConst(sv2.dnn), G, 1.0, false);
156 Kokkos::fence();
157 pull(bb);
158 }
159 { // the owner-set means of the three edges E_ac(k) of every cell k (§5.8)
160 CCField em("sv_mpi_edges", 18 * n);
161 const sv::SharpGeom g = sv2.geo;
162 Kokkos::parallel_for(
163 "sv_mpi_edges", Kokkos::RangePolicy<peclet::flow::CCExec>(0, n), KOKKOS_LAMBDA(long i) {
164 const int z = (int)(i / g.st[2]), rr = (int)(i - z * g.st[2]), y = rr / (int)g.st[1],
165 x = rr - y * (int)g.st[1];
166 const bool in =
167 x >= G && y >= G && z >= G && x < g.e[0] - G && y < g.e[1] - G && z < g.e[2] - G;
168 const int k[3] = {x, y, z};
169 for (int ac = 0; ac < 3; ++ac) {
170 const int a = (ac == 2) ? 1 : 0, c = (ac == 0) ? 1 : 2;
171 double r6[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
172 if (in && g.edgeFaceFraction(k, a, c, a, r6[0])) {
173 g.edgeFaceFraction(k, a, c, c, r6[1]);
174 g.edgeLinkFractions(k, a, c, r6[2], r6[3]);
175 r6[4] = g.edgeTraction(k, a, c, a, t);
176 r6[5] = g.edgeTraction(k, a, c, c, t);
177 }
178 for (int q = 0; q < 6; ++q)
179 em((long)(6 * ac + q) * n + i) = r6[q];
180 }
181 });
182 Kokkos::fence();
183 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), em);
184 for (int q = 0; q < 18; ++q)
185 cols.emplace_back(h.data() + (long)q * n, h.data() + (long)(q + 1) * n);
186 }
187 std::vector<double> out;
188 out.reserve((long)kVals * ln[0] * ln[1] * ln[2]);
189 for (const auto& f : cols)
190 for (int z = G; z < e[2] - G; ++z)
191 for (int y = G; y < e[1] - G; ++y)
192 for (int x = G; x < e[0] - G; ++x)
193 out.push_back(f[(long)x + (long)y * e[0] + (long)z * e[0] * e[1]]);
194 return out;
195}
196
197static void configure(IbmSolver& s, bool walls) {
198 if (walls) {
199 s.setDomainBc(2, 1, 0.0, 0.0, 0.0); // -y wall
200 s.setDomainBc(3, 1, 0.0, 0.0, 0.0); // +y wall
201 }
202}
203
204int main(int argc, char** argv) {
205 MPI_Init(&argc, &argv);
206 Kokkos::initialize(argc, argv);
207 int fail = 0;
208 {
209 int rank = 0, size = 1;
210 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
211 MPI_Comm_size(MPI_COMM_WORLD, &size);
212 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
213 const int gn[3] = {NX, NY, NZ};
214 int ncut = 0;
215 for (int a = 0; a < 3; ++a) {
216 bool cut = false;
217 for (const auto& sz : dec.sizes())
218 cut = cut || ((int)sz[a] != gn[a]);
219 ncut += cut ? 1 : 0;
220 }
221 const int needCut = (size >= 4) ? 2 : (size >= 2 ? 1 : 0);
222 if (rank == 0)
223 std::printf("SHARP_VISCOUS_MPI np=%d grid %dx%dx%d, %d cut axes (need >= %d)\n", size, NX, NY,
224 NZ, ncut, needCut);
225 if (ncut < needCut)
226 fail = 1;
227 auto blk = dec.block(rank);
228 const int o[3] = {(int)blk.origin[0], (int)blk.origin[1], (int)blk.origin[2]};
229 const int ln[3] = {(int)blk.size[0], (int)blk.size[1], (int)blk.size[2]};
230 // The vertex-centred sphere (pass 2): on the first cut of each cut axis, else the box middle;
231 // z = 0 (the periodic seam) unless z is cut.
232 double vcen[3] = {NX / 2, NY / 2, 0};
233 for (int a = 0; a < 3; ++a) {
234 int cut = gn[a];
235 for (int r = 0; r < size; ++r) {
236 const int org = (int)dec.block(r).origin[a];
237 if (org > 0 && org < cut)
238 cut = org;
239 }
240 if (cut < gn[a])
241 vcen[a] = cut;
242 }
243 if (rank == 0)
244 std::printf(" vertex sphere centre (%g, %g, %g), radius 3.4\n", vcen[0], vcen[1], vcen[2]);
245 for (int pass = 0; pass < 3; ++pass) {
246 const bool walls = (pass == 1);
247 if (pass == 2) {
248 for (int a = 0; a < 3; ++a)
249 gCen[a] = vcen[a];
250 gR = 3.4;
251 }
252 std::vector<double> mine;
253 {
254 IbmSolver s(ln[0], ln[1], ln[2]);
255 configure(s, walls);
256 s.initMpi(dec, MPI_COMM_WORLD);
257 mine = evalSharp(s, o, ln);
258 }
259 std::vector<int> counts(size), displs(size);
260 long total = 0;
261 for (int r = 0; r < size; ++r) {
262 const auto b = dec.block(r);
263 counts[r] = kVals * (int)(b.size[0] * b.size[1] * b.size[2]);
264 displs[r] = (int)total;
265 total += counts[r];
266 }
267 std::vector<double> all(rank == 0 ? total : 0);
268 MPI_Gatherv(mine.data(), (int)mine.size(), MPI_DOUBLE, all.data(), counts.data(),
269 displs.data(), MPI_DOUBLE, 0, MPI_COMM_WORLD);
270 if (rank == 0) {
271 const int zo[3] = {0, 0, 0}, gl[3] = {NX, NY, NZ};
272 IbmSolver ref(NX, NY, NZ);
273 configure(ref, walls);
274 const std::vector<double> want = evalSharp(ref, zo, gl);
275 const long tied = ref.vofViscousInterfaceCensus().tied;
276 long bad = 0, nonfinite = 0, band = 0, fallbacks = 0, rhsNz = 0;
277 const long NN = (long)NX * NY * NZ;
278 for (int r = 0; r < size; ++r) {
279 const auto b = dec.block(r);
280 const int bs[3] = {(int)b.size[0], (int)b.size[1], (int)b.size[2]};
281 const long bn = (long)bs[0] * bs[1] * bs[2];
282 for (int f = 0; f < kVals; ++f)
283 for (int z = 0; z < bs[2]; ++z)
284 for (int y = 0; y < bs[1]; ++y)
285 for (int x = 0; x < bs[0]; ++x) {
286 const double got =
287 all[displs[r] + f * bn + x + (long)bs[0] * (y + (long)bs[1] * z)];
288 const long gi =
289 ((int)b.origin[0] + x) +
290 (long)NX * (((int)b.origin[1] + y) + (long)NY * ((int)b.origin[2] + z));
291 const double w = want[(long)f * NN + gi];
292 if (std::memcmp(&got, &w, sizeof(double)) != 0)
293 ++bad;
294 if (!std::isfinite(got))
295 ++nonfinite;
296 if (f == 0 && got != 0.0)
297 ++band;
298 if (f == 1 && got != 0.0)
299 ++fallbacks;
300 if (f >= kVals - 3 && got != 0.0)
301 ++rhsNz;
302 }
303 }
304 const bool ok = bad == 0 && nonfinite == 0 && band > 100 && fallbacks == 0 && rhsNz > 0 &&
305 (pass != 2 || tied > 0);
306 std::printf(
307 " %-9s %ld/%ld bitwise mismatches vs np 1, %ld non-finite, %ld band cells, "
308 "%ld fallbacks, %ld nonzero RHS, %ld tied edges %s\n",
309 pass == 2 ? "vertex" : (walls ? "y walls" : "periodic"), bad, total, nonfinite, band,
310 fallbacks, rhsNz, tied, ok ? "PASS" : "FAIL");
311 if (!ok)
312 fail = 1;
313 }
314 }
315 }
316 int gfail = 0;
317 MPI_Allreduce(&fail, &gfail, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
318 Kokkos::finalize();
319 MPI_Finalize();
320 return gfail;
321}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
void addSharpViscousRhs(int c, CCField bb, CCConst rs, const CCConst(&v)[3], const SharpGeom &gg, const CCConst(&tau)[3], CCConst dnn, int g, double wc, bool skipLoC, SvList list, long nl)
The explicit band terms of the u_c rows over the inner cells [g, e−g)³ (§5.4): the τ-terms of the fou...
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)
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)³.
Kokkos::View< double *, CCMem > CCField
Solver< Staggered > IbmSolver
static constexpr int NY
static constexpr int NX
static constexpr int NZ
The band geometry and the closure constants, by value into every kernel.
static constexpr int G
static constexpr int kVals
static double gCen[3]
static std::vector< double > evalSharp(IbmSolver &s, const int o[3], const int ln[3])
static double gR
static double colourValue(int gx, int gy, int gz)
static double urand(int id, int gx, int gy, int gz)
static void configure(IbmSolver &s, bool walls)