flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_ghost_edges_mpi.cpp
Go to the documentation of this file.
1// flow — the width-1 ghost ring INCLUDING EDGES (and corners) of the two fills the full viscous
2// stress reads (doc/full_viscous_stress.md §10, WO-0, gate G0).
3//
4// The transpose term T_c (src/full_stress.hpp) reads mu at i - s_c +- s_a and the time-base
5// velocity v_a at i - s_c + s_a: EDGE ghosts of the (a, c) planes. The existing Laplacian
6// cross-face viscosity already reads the mu edge ghost (VarFaceProps::beta), so a missing edge here
7// would be a latent bug of the shipped operator too (Q7 -> WO-0b).
8//
9// Fully periodic box, a field f = gx + 1e3*gy + 1e6*gz written on the INNER cells of every rank
10// (global coordinates), the ghosts poisoned with NaN, then
11// * fillPropGhosts(f, /*edges=*/true) -- the mu path (fillMuGhosts)
12// * fillVelGhostsTo(f, comp, 0, false), comp 0-2 -- the u^n path of the §4.3 prelude
13// and every cell of the width-1 shell [G-1, e-G+1)^3 that is not inner is compared EXACTLY with
14// the periodic-wrapped global value. Mismatches are counted per class (face / edge / corner); the
15// gate is 0 in every class. np 4 must cut two axes (a 2x2 split), np 2 at least one.
16//
17// Second case (review S1, validation/REVIEW_FULLSTRESS.md): inflow -z / outflow +z, x and y
18// periodic. The u^n fill (fillVelGhostsTo(f, comp, 0, false)) keeps the projected outflow face of
19// w, the first ghost index along z. The distributed fill (np 1 included) must reproduce the
20// single-rank fill BITWISE on the whole width-1 shell: every rank also fills the same global field
21// on a single-rank solver of the whole box and memcmps its own block's shell against it. The w
22// outflow plane carries a distinct value (the "projected" face) so a wrapped inlet value shows.
23#include <mpi.h>
24
25#include <cmath>
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
37
38static constexpr int NX = 16, NY = 12, NZ = 8;
39
40static double value(int gx, int gy, int gz) {
41 gx = ((gx % NX) + NX) % NX;
42 gy = ((gy % NY) + NY) % NY;
43 gz = ((gz % NZ) + NZ) % NZ;
44 return gx + 1e3 * gy + 1e6 * gz;
45}
46
47int main(int argc, char** argv) {
48 MPI_Init(&argc, &argv);
49 Kokkos::initialize(argc, argv);
50 int fail = 0;
51 {
52 int rank = 0, size = 1;
53 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
54 MPI_Comm_size(MPI_COMM_WORLD, &size);
55 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
56 auto blk = dec.block(rank);
57 const int o[3] = {(int)blk.origin[0], (int)blk.origin[1], (int)blk.origin[2]};
58 const int ln[3] = {(int)blk.size[0], (int)blk.size[1], (int)blk.size[2]};
59 const int gn[3] = {NX, NY, NZ};
60 int ncut = 0;
61 for (int a = 0; a < 3; ++a) {
62 bool cut = false;
63 for (const auto& sz : dec.sizes())
64 cut = cut || ((int)sz[a] != gn[a]);
65 ncut += cut ? 1 : 0;
66 }
67 const int needCut = (size >= 4) ? 2 : (size >= 2 ? 1 : 0);
68 if (rank == 0)
69 std::printf("GHOST_EDGES np=%d grid %dx%dx%d, %d cut axes (need >= %d)\n", size, NX, NY, NZ,
70 ncut, needCut);
71 if (ncut < needCut)
72 fail = 1;
73
74 IbmSolver s(ln[0], ln[1], ln[2]);
75 s.initMpi(dec, MPI_COMM_WORLD);
76 constexpr int G = IbmSolver::G;
77 const int e[3] = {ln[0] + 2 * G, ln[1] + 2 * G, ln[2] + 2 * G};
78 const long n = (long)e[0] * e[1] * e[2];
79 CCField f("ghost_edges_f", n);
80 auto h = Kokkos::create_mirror_view(f);
81 const char* names[4] = {"fillPropGhosts(edges)", "fillVelGhostsTo(c=0)", "fillVelGhostsTo(c=1)",
82 "fillVelGhostsTo(c=2)"};
83 for (int path = 0; path < 4; ++path) {
84 for (int z = 0; z < e[2]; ++z)
85 for (int y = 0; y < e[1]; ++y)
86 for (int x = 0; x < e[0]; ++x) {
87 const bool inner =
88 x >= G && x < e[0] - G && y >= G && y < e[1] - G && z >= G && z < e[2] - G;
89 h((long)x + (long)y * e[0] + (long)z * e[0] * e[1]) =
90 inner ? value(o[0] + x - G, o[1] + y - G, o[2] + z - G)
91 : std::numeric_limits<double>::quiet_NaN();
92 }
93 Kokkos::deep_copy(f, h);
94 if (path == 0)
95 s.fillPropGhosts(f, /*edges=*/true);
96 else
97 s.fillVelGhostsTo(f, path - 1, 0, /*doOutflow=*/false);
98 Kokkos::fence();
99 Kokkos::deep_copy(h, f);
100 long bad[4] = {0, 0, 0, 0},
101 seen[4] = {0, 0, 0, 0}; // by #ghost axes: 1 face, 2 edge, 3 corner
102 for (int z = G - 1; z < e[2] - G + 1; ++z)
103 for (int y = G - 1; y < e[1] - G + 1; ++y)
104 for (int x = G - 1; x < e[0] - G + 1; ++x) {
105 const int k =
106 (x < G || x >= e[0] - G) + (y < G || y >= e[1] - G) + (z < G || z >= e[2] - G);
107 if (k == 0)
108 continue;
109 ++seen[k];
110 const double got = h((long)x + (long)y * e[0] + (long)z * e[0] * e[1]);
111 if (!(got == value(o[0] + x - G, o[1] + y - G, o[2] + z - G)))
112 ++bad[k];
113 }
114 long all[8] = {bad[1], bad[2], bad[3], seen[1], seen[2], seen[3], 0, 0};
115 long red[8];
116 MPI_Allreduce(all, red, 8, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
117 if (rank == 0)
118 std::printf(" %-22s mismatches: face %ld/%ld edge %ld/%ld corner %ld/%ld %s\n",
119 names[path], red[0], red[3], red[1], red[4], red[2], red[5],
120 (red[0] + red[1] + red[2]) == 0 ? "PASS" : "FAIL");
121 if (red[0] + red[1] + red[2] != 0)
122 fail = 1;
123 }
124
125 // ---- S1: outflow +z (inflow -z), distributed vs single-rank, memcmp on the shell ----
126 // Variant 0: x and y periodic. Variant 1: no-slip walls at -x/+x (the domain BCs are applied
127 // after the restored face plane). The fills are the prelude's own: fillPropGhosts(edges) for
128 // mu and fillVelGhostsTo(f, c, 0, false, /*faceEdges=*/true) for u^n. A shell cell the
129 // single-rank fill leaves unwritten (NaN: the tangential components' outflow ghost plane,
130 // which doOutflow = false does not touch and T never reads) has no single-rank value to match
131 // and is counted apart, not compared.
132 const int eg[3] = {NX + 2 * G, NY + 2 * G, NZ + 2 * G};
133 CCField fg("ghost_edges_fg", (long)eg[0] * eg[1] * eg[2]);
134 auto hg = Kokkos::create_mirror_view(fg);
135 // inner cells = value(); the w outflow face (global z index NZ + G, inner x/y) = value + 0.25
136 // (the projected face); every other ghost NaN.
137 const auto fillField = [&](auto& hh, const int* ee, const int* off, int comp) {
138 for (int z = 0; z < ee[2]; ++z)
139 for (int y = 0; y < ee[1]; ++y)
140 for (int x = 0; x < ee[0]; ++x) {
141 const bool inXY = x >= G && x < ee[0] - G && y >= G && y < ee[1] - G;
142 const bool inner = inXY && z >= G && z < ee[2] - G;
143 const int gz = off[2] + z - G;
144 const bool face = comp == 2 && inXY && gz == NZ;
145 hh((long)x + (long)y * ee[0] + (long)z * ee[0] * ee[1]) =
146 inner ? value(off[0] + x - G, off[1] + y - G, gz)
147 : face ? value(off[0] + x - G, off[1] + y - G, 0) + 0.25
148 : std::numeric_limits<double>::quiet_NaN();
149 }
150 };
151 const int o0[3] = {0, 0, 0};
152 const char* vname[2] = {"x,y periodic", "x walls"};
153 for (int variant = 0; variant < 2; ++variant) {
154 const auto setBcs = [variant](IbmSolver& q) {
155 q.setDomainBc(4, 2, 0.0, 0.0, 1e-3); // -z inflow
156 q.setDomainBc(5, 3, 0.0, 0.0, 0.0); // +z outflow
157 if (variant == 1) {
158 q.setDomainBc(0, 1, 0.0, 0.0, 0.0); // -x wall
159 q.setDomainBc(1, 1, 0.0, 0.0, 0.0); // +x wall
160 }
161 };
162 IbmSolver so(ln[0], ln[1], ln[2]);
163 setBcs(so);
164 so.initMpi(dec, MPI_COMM_WORLD);
165 IbmSolver sg(NX, NY, NZ); // the single-rank reference on the whole box (every rank, small)
166 setBcs(sg);
167 for (int path = 0; path < 4; ++path) {
168 const int comp = path - 1; // -1 = the mu path
169 fillField(h, e, o, comp);
170 Kokkos::deep_copy(f, h);
171 fillField(hg, eg, o0, comp);
172 Kokkos::deep_copy(fg, hg);
173 if (comp < 0) {
174 so.fillPropGhosts(f, /*edges=*/true);
175 sg.fillPropGhosts(fg, /*edges=*/true);
176 } else {
177 so.fillVelGhostsTo(f, comp, 0, /*doOutflow=*/false, /*faceEdges=*/true);
178 sg.fillVelGhostsTo(fg, comp, 0, /*doOutflow=*/false, /*faceEdges=*/true);
179 }
180 Kokkos::fence();
181 Kokkos::deep_copy(h, f);
182 Kokkos::deep_copy(hg, fg);
183 long bad[4] = {0, 0, 0, 0}, seen[4] = {0, 0, 0, 0}, undef = 0;
184 for (int z = G - 1; z < e[2] - G + 1; ++z)
185 for (int y = G - 1; y < e[1] - G + 1; ++y)
186 for (int x = G - 1; x < e[0] - G + 1; ++x) {
187 const int k =
188 (x < G || x >= e[0] - G) + (y < G || y >= e[1] - G) + (z < G || z >= e[2] - G);
189 if (k == 0)
190 continue;
191 const double got = h((long)x + (long)y * e[0] + (long)z * e[0] * e[1]);
192 const double ref = hg((long)(o[0] + x) + (long)(o[1] + y) * eg[0] +
193 (long)(o[2] + z) * eg[0] * eg[1]);
194 if (std::isnan(ref)) {
195 ++undef;
196 continue;
197 }
198 ++seen[k];
199 if (std::memcmp(&got, &ref, sizeof(double)) != 0)
200 ++bad[k];
201 }
202 long all[7] = {bad[1], bad[2], bad[3], seen[1], seen[2], seen[3], undef}, red[7];
203 MPI_Allreduce(all, red, 7, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
204 if (rank == 0)
205 std::printf(
206 " outflow+z (%s) %-22s vs single rank (memcmp): face %ld/%ld edge "
207 "%ld/%ld corner %ld/%ld (unwritten %ld) %s\n",
208 vname[variant], names[path], red[0], red[3], red[1], red[4], red[2], red[5], red[6],
209 (red[0] + red[1] + red[2]) == 0 ? "PASS" : "FAIL");
210 if (red[0] + red[1] + red[2] != 0)
211 fail = 1;
212 }
213 }
214 }
215 int gfail = 0;
216 MPI_Allreduce(&fail, &gfail, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
217 Kokkos::finalize();
218 MPI_Finalize();
219 return gfail;
220}
void fillPropGhosts(CCField f, bool edges=false)
void fillVelGhostsTo(CCField f, int comp, int fold, bool doOutflow=true, bool faceEdges=false)
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Kokkos::View< double *, CCMem > CCField
Solver< Staggered > IbmSolver
static constexpr int NY
static constexpr int NX
static constexpr int NZ
static constexpr int G
static double value(int gx, int gy, int gz)