flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_full_stress_mpi.cpp
Go to the documentation of this file.
1// flow — the transpose term of the full viscous stress is decomposition-invariant BITWISE
2// (doc/full_viscous_stress.md WO-4, gate G4 "T bitwise equal at np 1, 2, 4").
3//
4// T_c (src/full_stress.hpp) is a pointwise stencil on mu and the time-base velocity u^n; what a
5// decomposition can change is only the ghost ring it reads -- the mu fill incl. edges
6// (fillPropGhosts(edges=true), the fillMuGhosts path) and the u^n fill (fillVelGhostsTo(c, 0,
7// false), the fullStressPrelude path). So: a random periodic field (mu log-uniform over a ratio of
8// 1e3, u uniform in [-1, 1], a hash of the GLOBAL index, so every decomposition sees the same
9// field) is written on the inner cells of every rank with NaN-poisoned ghosts, filled by the
10// solver's own fills, and T_c is evaluated by the production kernel with the face props
11// makeFaceProps builds for a variable mu (arithmetic mean, staggered placement). Rank 0 evaluates
12// the same field on the whole box with a single-rank solver (np 1, periodic local fill) and
13// compares the gathered T_c bit for bit, every inner cell, every component.
14//
15// Two passes: (1) no solid (the `kSolid = false` kernel); (2) a random regular-node mask (about a
16// quarter of the nodes irregular) through the `kSolid = true` kernel, its ghosts filled the way
17// buildFullStressRegMasks does (double carrier, fillPropGhosts(edges=true), packed to uint8).
18// np 4 must cut two axes (a 2x2 split), np 2 at least one.
19#include <mpi.h>
20
21#include <cmath>
22#include <cstdint>
23#include <cstdio>
24#include <cstring>
25#include <Kokkos_Core.hpp>
26#include <limits>
27#include <vector>
28
29#include "flow_ibm.hpp"
30#include "full_stress.hpp"
31#include "peclet/core/decomp/block_decomposer.hpp"
32
40
41static constexpr int NX = 16, NY = 12, NZ = 8;
42static constexpr int G = IbmSolver::G;
43
44// splitmix64 of (field id, periodic global index) -> uniform [0, 1).
45static double urand(int id, int gx, int gy, int gz) {
46 gx = ((gx % NX) + NX) % NX;
47 gy = ((gy % NY) + NY) % NY;
48 gz = ((gz % NZ) + NZ) % NZ;
49 std::uint64_t z =
50 0x9E3779B97F4A7C15ULL * (std::uint64_t)(1 + id) + (std::uint64_t)(gx + NX * (gy + NY * gz));
51 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
52 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
53 z ^= z >> 31;
54 return (double)(z >> 11) * 0x1.0p-53;
55}
56
57// Field ids: 0 = mu, 1..3 = u_0..u_2, 4..6 = solid flag of the reg mask of component 0..2.
58static double fieldValue(int id, int gx, int gy, int gz) {
59 const double r = urand(id, gx, gy, gz);
60 if (id == 0)
61 return std::pow(10.0, 3.0 * r); // mu in [1, 1e3)
62 if (id <= 3)
63 return 2.0 * r - 1.0; // u in [-1, 1)
64 return (r < 0.25) ? 0.0 : 1.0; // regular-node flag
65}
66
67// T_c(u^n) for c = 0..2 on the inner cells of a block (origin o, size ln) owned by solver s,
68// flattened x fastest over the inner cells. withReg: the kSolid kernel with the random mask.
69static std::vector<double> evalT(IbmSolver& s, const int o[3], const int ln[3], bool withReg) {
70 const int e[3] = {ln[0] + 2 * G, ln[1] + 2 * G, ln[2] + 2 * G};
71 const long n = (long)e[0] * e[1] * e[2];
72 const long st[3] = {1L, (long)e[0], (long)e[0] * e[1]};
73 auto fill = [&](CCField f, int id) {
74 auto h = Kokkos::create_mirror_view(f);
75 for (int z = 0; z < e[2]; ++z)
76 for (int y = 0; y < e[1]; ++y)
77 for (int x = 0; x < e[0]; ++x) {
78 const bool inner =
79 x >= G && x < e[0] - G && y >= G && y < e[1] - G && z >= G && z < e[2] - G;
80 h((long)x + (long)y * e[0] + (long)z * e[0] * e[1]) =
81 inner ? fieldValue(id, o[0] + x - G, o[1] + y - G, o[2] + z - G)
82 : std::numeric_limits<double>::quiet_NaN();
83 }
84 Kokkos::deep_copy(f, h);
85 };
86 CCField mu("fs_mpi_mu", n),
87 v[3] = {CCField("fs_mpi_v0", n), CCField("fs_mpi_v1", n), CCField("fs_mpi_v2", n)};
88 fill(mu, 0);
89 s.fillPropGhosts(mu, /*edges=*/true); // = fillMuGhosts
90 for (int c = 0; c < 3; ++c) {
91 fill(v[c], 1 + c);
92 s.fillVelGhostsTo(v[c], c, 0, /*doOutflow=*/false); // = fullStressPrelude step 3
93 }
94 RegMask reg[3];
95 if (withReg) {
96 CCField tmp("fs_mpi_regtmp", n);
97 for (int c = 0; c < 3; ++c) {
98 fill(tmp, 4 + c);
99 s.fillPropGhosts(tmp, /*edges=*/true); // = buildFullStressRegMasks
100 reg[c] = RegMask("fs_mpi_reg", n);
101 RegMask r = reg[c];
102 CCField t = tmp;
103 Kokkos::parallel_for(
104 "fs_mpi_reg_pack", Kokkos::RangePolicy<peclet::flow::CCExec>(0, n),
105 KOKKOS_LAMBDA(long i) { r(i) = (t(i) != 0.0) ? 1 : 0; });
106 }
107 }
108 Kokkos::fence();
109 const CCConst vc[3] = {CCConst(v[0]), CCConst(v[1]), CCConst(v[2])};
110 CCField rs("fs_mpi_rs", n);
111 Kokkos::deep_copy(rs, 1.0);
112 std::vector<double> out;
113 out.reserve(3L * ln[0] * ln[1] * ln[2]);
114 for (int c = 0; c < 3; ++c) {
115 VarFaceProps fp; // what makeFaceProps(c) builds for a variable, arithmetic mu (staggered)
116 fp.haveMu = true;
117 fp.mu = CCConst(mu);
118 fp.msc = st[c];
119 fp.harmMu = false;
120 fp.haveRho = false;
121 fp.rhoIdtC = 0.0;
122 CCField bb("fs_mpi_bb", n);
123 if (withReg) {
124 const RegConst rr[3] = {reg[0], reg[1], reg[2]};
126 C3{e[0], e[1], e[2]}, G, 1.0, false, rr);
127 } else {
129 C3{e[0], e[1], e[2]}, G, 1.0, false);
130 }
131 Kokkos::fence();
132 auto h = Kokkos::create_mirror_view(bb);
133 Kokkos::deep_copy(h, bb);
134 for (int z = G; z < e[2] - G; ++z)
135 for (int y = G; y < e[1] - G; ++y)
136 for (int x = G; x < e[0] - G; ++x)
137 out.push_back(h((long)x + (long)y * e[0] + (long)z * e[0] * e[1]));
138 }
139 return out;
140}
141
142int main(int argc, char** argv) {
143 MPI_Init(&argc, &argv);
144 Kokkos::initialize(argc, argv);
145 int fail = 0;
146 {
147 int rank = 0, size = 1;
148 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
149 MPI_Comm_size(MPI_COMM_WORLD, &size);
150 auto dec = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ);
151 const int gn[3] = {NX, NY, NZ};
152 int ncut = 0;
153 for (int a = 0; a < 3; ++a) {
154 bool cut = false;
155 for (const auto& sz : dec.sizes())
156 cut = cut || ((int)sz[a] != gn[a]);
157 ncut += cut ? 1 : 0;
158 }
159 const int needCut = (size >= 4) ? 2 : (size >= 2 ? 1 : 0);
160 if (rank == 0)
161 std::printf("FULL_STRESS_MPI np=%d grid %dx%dx%d, %d cut axes (need >= %d)\n", size, NX, NY,
162 NZ, ncut, needCut);
163 if (ncut < needCut)
164 fail = 1;
165
166 auto blk = dec.block(rank);
167 const int o[3] = {(int)blk.origin[0], (int)blk.origin[1], (int)blk.origin[2]};
168 const int ln[3] = {(int)blk.size[0], (int)blk.size[1], (int)blk.size[2]};
169 IbmSolver s(ln[0], ln[1], ln[2]);
170 s.initMpi(dec, MPI_COMM_WORLD);
171
172 for (int pass = 0; pass < 2; ++pass) {
173 const bool withReg = (pass == 1);
174 const std::vector<double> mine = evalT(s, o, ln, withReg);
175 // Gather every rank's block (3 components, inner cells x fastest) on rank 0.
176 std::vector<int> counts(size), displs(size);
177 long total = 0;
178 for (int r = 0; r < size; ++r) {
179 const auto b = dec.block(r);
180 counts[r] = 3 * (int)(b.size[0] * b.size[1] * b.size[2]);
181 displs[r] = (int)total;
182 total += counts[r];
183 }
184 std::vector<double> all(rank == 0 ? total : 0);
185 MPI_Gatherv(mine.data(), (int)mine.size(), MPI_DOUBLE, all.data(), counts.data(),
186 displs.data(), MPI_DOUBLE, 0, MPI_COMM_WORLD);
187 if (rank == 0) {
188 const int zo[3] = {0, 0, 0}, gl[3] = {NX, NY, NZ};
189 IbmSolver ref(NX, NY, NZ); // np 1: single-rank, periodic local fill
190 const std::vector<double> want = evalT(ref, zo, gl, withReg);
191 long bad = 0, nonzero = 0, nonfinite = 0;
192 double maxAbs = 0.0;
193 for (int r = 0; r < size; ++r) {
194 const auto b = dec.block(r);
195 const int bs[3] = {(int)b.size[0], (int)b.size[1], (int)b.size[2]};
196 const long bn = (long)bs[0] * bs[1] * bs[2];
197 for (int c = 0; c < 3; ++c)
198 for (int z = 0; z < bs[2]; ++z)
199 for (int y = 0; y < bs[1]; ++y)
200 for (int x = 0; x < bs[0]; ++x) {
201 const double got =
202 all[displs[r] + c * bn + x + (long)bs[0] * (y + (long)bs[1] * z)];
203 const int gx = (int)b.origin[0] + x, gy = (int)b.origin[1] + y,
204 gz = (int)b.origin[2] + z;
205 const double ref1 =
206 want[(long)c * NX * NY * NZ + gx + (long)NX * (gy + (long)NY * gz)];
207 if (std::memcmp(&got, &ref1, sizeof(double)) != 0)
208 ++bad;
209 if (!std::isfinite(got))
210 ++nonfinite;
211 if (got != 0.0)
212 ++nonzero;
213 maxAbs = std::fmax(maxAbs, std::fabs(ref1));
214 }
215 }
216 const bool ok = (bad == 0) && (nonfinite == 0) && (nonzero > total / 2);
217 std::printf(
218 " %-12s T_c vs np 1: %ld/%ld bitwise mismatches, %ld non-finite, %ld nonzero, "
219 "max|T| %.3e %s\n",
220 withReg ? "reg mask" : "no solid", bad, total, nonfinite, nonzero, maxAbs,
221 ok ? "PASS" : "FAIL");
222 if (!ok)
223 fail = 1;
224 }
225 }
226 }
227 int gfail = 0;
228 MPI_Allreduce(&fail, &gfail, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
229 Kokkos::finalize();
230 MPI_Finalize();
231 return gfail;
232}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
flow — the transpose half of the full deviatoric viscous stress on the staggered grid.
Kokkos::View< const double *, CCMem > CCConst
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)³.
Kokkos::View< double *, CCMem > CCField
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.
Solver< Staggered > IbmSolver
static constexpr int NY
static constexpr int NX
static constexpr int NZ
static double maxAbs(const std::vector< double > &a)
static constexpr int G
static std::vector< double > evalT(IbmSolver &s, const int o[3], const int ln[3], bool withReg)
static double urand(int id, int gx, int gy, int gz)
static double fieldValue(int id, int gx, int gy, int gz)