flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_sdflow_mpi.cpp
Go to the documentation of this file.
1// cfd-gpu — the ASSEMBLED multi-rank IbmSolver step (the capstone of the cfd MPI port).
2//
3// Solves creeping (Stokes) flow through a periodic 2x2x2 sphere packing with the REAL IbmSolver
4// solver, two ways: single-rank (the validated single-GPU path) on the full grid, and distributed
5// -- each rank constructs IbmSolver with its ORB block dims, calls initMpi (g=2 velocity-block halo
6// + global-origin red-black parity
7// + the distributed pressure MG via CutcellMG::initMpi), and setSolid with its LOCAL SDF block
8// (ghosts halo- exchanged). The superficial velocity <u> (hence the permeability k = mu*<u>/F) is
9// reduced over ranks. The distributed solve must equal single-rank: np=1 bit-exact, np>1 to the
10// MG-PCG reduction-order floor (the inner-product Allreduce reorders the Krylov path -- same as
11// test_cutcellmg_mpi). Build with -DCFD_MPI.
12#include <mpi.h>
13
14#include <cmath>
15#include <cstdio>
16#include <Kokkos_Core.hpp>
17#include <stdexcept>
18#include <vector>
19
20#include "flow_ibm.hpp"
21#include "peclet/core/common/types.hpp"
22#include "peclet/core/decomp/block_decomposer.hpp"
23
24using peclet::core::IVec;
26
27static constexpr int N = 32, STEPS = 120;
28static constexpr double RHO = 1.0, MU = 0.1, F = 1e-3, DT = 60.0;
29
30// global sphere-packing SDF (flat x-fastest, negative inside), 2x2x2 spheres, periodic
31// min-distance.
32static std::vector<double> packingSdf(double rfrac = 0.18) {
33 const double R = rfrac * N;
34 std::vector<double> sdf((std::size_t)N * N * N);
35 const double cs[2] = {0.25 * N, 0.75 * N};
36 for (int z = 0; z < N; ++z)
37 for (int y = 0; y < N; ++y)
38 for (int x = 0; x < N; ++x) {
39 double best = 1e30;
40 for (double sx : cs)
41 for (double sy : cs)
42 for (double sz : cs) {
43 auto wrap = [](double d) { return d - N * std::round(d / N); };
44 const double dx = wrap(x - sx), dy = wrap(y - sy), dz = wrap(z - sz);
45 best = std::min(best, std::sqrt(dx * dx + dy * dy + dz * dz) - R);
46 }
47 sdf[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * N] = best;
48 }
49 return sdf;
50}
51
52static void configure(IbmSolver& s) {
53 s.setRho(RHO);
54 s.setMu(MU);
55 s.setDt(DT);
56 s.setBodyForce(F, 0, 0);
57 s.setAdvection(false);
58 s.setVelocityIterations(80);
59 s.setPressureLevels(4);
60 s.setPressurePcg(true, 200, 1e-9);
61}
62
63// local sum of u over this solver's inner block (getVelocity(0) returns the inner block,
64// x-fastest).
65static double localUSum(IbmSolver& s) {
66 auto u = s.getVelocity(0);
67 double sum = 0;
68 for (double v : u)
69 sum += v;
70 return sum;
71}
72
73int main(int argc, char** argv) {
74 MPI_Init(&argc, &argv);
75 Kokkos::initialize(argc, argv);
76 int fail = 0, size = 1, rank = 0;
77 {
78 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
79 MPI_Comm_size(MPI_COMM_WORLD, &size);
80 const std::vector<double> gsdf = packingSdf();
81 const double gcells = (double)N * N * N;
82
83 // --- distributed solve ---
84 // size the block from the Solver's own factory: initMpi(N, N, N) re-derives this partition
85 // and checks the local block against it
86 const peclet::core::decomp::BlockDecomposer<3> dec =
87 peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), N, N, N);
88 auto blk = dec.block(rank);
89 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
90 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
91 std::vector<double> lsdf((std::size_t)lnx * lny * lnz);
92 for (int z = 0; z < lnz; ++z)
93 for (int y = 0; y < lny; ++y)
94 for (int x = 0; x < lnx; ++x)
95 lsdf[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
96 gsdf[(std::size_t)(x + ox) + (std::size_t)(y + oy) * N +
97 (std::size_t)(z + oz) * N * N];
98
99 IbmSolver sd(lnx, lny, lnz);
100 sd.initMpi(N, N, N, MPI_COMM_WORLD);
101 configure(sd);
102 sd.setSolid(lsdf, /*cutcell_pressure=*/true);
103 for (int it = 0; it < STEPS; ++it)
104 sd.step();
105 double lsum = localUSum(sd), gsum = 0;
106 MPI_Allreduce(&lsum, &gsum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
107 const double k_dist = MU * (gsum / gcells) / F;
108 const double div_dist = sd.maxOpenDivergence();
109
110 // --- single-rank reference (full grid, the validated single-GPU path) on rank 0 ---
111 double k_ref = 0.0;
112 if (rank == 0) {
113 IbmSolver ref(N, N, N);
114 configure(ref);
115 ref.setSolid(gsdf, true);
116 for (int it = 0; it < STEPS; ++it)
117 ref.step();
118 double rsum = 0;
119 {
120 auto u = ref.getVelocity(0);
121 for (double v : u)
122 rsum += v;
123 }
124 k_ref = MU * (rsum / gcells) / F;
125 }
126 MPI_Bcast(&k_ref, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
127
128 const double reld = std::fabs(k_dist - k_ref) / (std::fabs(k_ref) + 1e-30);
129 const double tol =
130 (size == 1) ? 1e-12 : 2e-5; // np=1 bit-exact; np>1 the MG-PCG reduction-order floor
131 if (rank == 0)
132 std::printf(" k_dist=%.8e k_ref=%.8e rel=%.2e div=%.2e (np=%d, tol %.0e)\n", k_dist,
133 k_ref, reld, div_dist, size, tol);
134 if (reld > tol || !(div_dist < 1e-5))
135 fail = 1;
136
137 // initMpi(dec) refuses a block that does not match the decomposition -- on EVERY rank, even
138 // when only rank 0's block is wrong (one collective agreement; review 2026-10-20 #2).
139 {
140 int threw = 0, allThrew = 0;
141 IbmSolver bad(lnx + (rank == 0 ? 2 : 0), lny, lnz);
142 bad.setAdvection(false);
143 try {
144 bad.initMpi(N, N, N, MPI_COMM_WORLD);
145 } catch (const std::runtime_error&) {
146 threw = 1;
147 }
148 MPI_Allreduce(&threw, &allThrew, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
149 if (rank == 0)
150 std::printf(" mis-sized block on rank 0: init_mpi raised on every rank: %s\n",
151 allThrew ? "yes" : "NO");
152 if (!allThrew)
153 fail = 1;
154 }
155 }
156 int totalFail = 0;
157 MPI_Allreduce(&fail, &totalFail, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
158 if (rank == 0) {
159 if (totalFail == 0)
160 std::printf("OK (np=%d): distributed IbmSolver Stokes permeability == single-rank\n", size);
161 else
162 std::fprintf(stderr, "FAILED (np=%d)\n", size);
163 }
164 Kokkos::finalize();
165 MPI_Finalize();
166 return totalFail == 0 ? 0 : 1;
167}
void setSolid(const std::vector< double > &sdfInner, bool cutcellPressure)
Host entry point: upload the inner SDF once and delegate.
void setAdvection(bool on)
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Solver< Staggered > IbmSolver
static constexpr int STEPS
static constexpr int N
static constexpr double DT
static int wrap(int a, int n)
static constexpr double RHO
static constexpr double MU
static constexpr double F
static constexpr double R
static double localUSum(IbmSolver &s)
static void configure(IbmSolver &s)
static std::vector< double > packingSdf(double rfrac=0.18)