flow 0.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_sdflow_colocated_mpi.cpp
Go to the documentation of this file.
1// cfd-gpu — the assembled multi-rank COLLOCATED solver step (collocated plan phase 5c).
2//
3// The collocated counterpart of test_sdflow_mpi: solves creeping (Stokes) flow through a periodic
4// 2x2x2 sphere packing with Solver<Colocated>, two ways -- single-rank on the full grid, and
5// distributed (each rank constructs the solver with its ORB block dims, calls initMpi, setSolid
6// with its LOCAL SDF block). The collocated approximate (MAC) projection runs multi-rank on exactly
7// the same transport-core halo machinery as the staggered solver: the cell-velocity halo feeds
8// centerToFace, the projected face field is halo-exchanged, the cut-cell pressure MG is MPI-folded.
9// The superficial velocity <u> (hence the permeability k = mu*<u>/F) is reduced over ranks and must
10// equal single-rank: np=1 bit-exact, np>1 to the MG-PCG reduction-order floor (the inner-product
11// Allreduce reorders the Krylov path). Build with -DCFD_MPI.
12#include <mpi.h>
13
14#include <cmath>
15#include <cstdio>
16#include <Kokkos_Core.hpp>
17#include <vector>
18
19#include "flow_ibm.hpp"
20#include "peclet/core/common/types.hpp"
21#include "peclet/core/decomp/block_decomposer.hpp"
22
23using peclet::core::IVec;
25
26static constexpr int N = 32, STEPS = 120;
27static constexpr double RHO = 1.0, MU = 0.1, F = 1e-3, DT = 60.0;
28
29// global sphere-packing SDF (flat x-fastest, negative inside), 2x2x2 spheres, periodic
30// min-distance.
31static std::vector<double> packingSdf(double rfrac = 0.18) {
32 const double R = rfrac * N;
33 std::vector<double> sdf((std::size_t)N * N * N);
34 const double cs[2] = {0.25 * N, 0.75 * N};
35 for (int z = 0; z < N; ++z)
36 for (int y = 0; y < N; ++y)
37 for (int x = 0; x < N; ++x) {
38 double best = 1e30;
39 for (double sx : cs)
40 for (double sy : cs)
41 for (double sz : cs) {
42 auto wrap = [](double d) { return d - N * std::round(d / N); };
43 const double dx = wrap(x - sx), dy = wrap(y - sy), dz = wrap(z - sz);
44 best = std::min(best, std::sqrt(dx * dx + dy * dy + dz * dz) - R);
45 }
46 sdf[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * N] = best;
47 }
48 return sdf;
49}
50
51static void configure(Colo& s, int faceInterp) {
52 s.setRho(RHO);
53 s.setMu(MU);
54 s.setDt(DT);
55 s.setBodyForce(F, 0, 0);
56 s.setAdvection(false);
57 s.setVelocityIterations(80);
58 s.setPressureLevels(4);
59 s.setPressurePcg(true, 200, 1e-9);
60 if (faceInterp != 0)
61 s.setFaceInterp(faceInterp); // mode 9: throat-safe cutcell-ghost hybrid (aperture projection
62 // + gpCenterGrad predictor/correction) — local stencils only,
63 // must reproduce single-rank across ranks like mode 0
64}
65
66static double localUSum(Colo& s) {
67 auto u = s.getVelocity(0);
68 double sum = 0;
69 for (double v : u)
70 sum += v;
71 return sum;
72}
73
74int main(int argc, char** argv) {
75 MPI_Init(&argc, &argv);
76 Kokkos::initialize(argc, argv);
77 int fail = 0, size = 1, rank = 0;
78 {
79 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
80 MPI_Comm_size(MPI_COMM_WORLD, &size);
81 const std::vector<double> gsdf = packingSdf();
82 const double gcells = (double)N * N * N;
83
84 // --- distributed solve ---
85 peclet::core::decomp::BlockDecomposer<3> dec(static_cast<std::size_t>(size), IVec<3>{N, N, N});
86 auto blk = dec.block(rank);
87 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
88 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
89 std::vector<double> lsdf((std::size_t)lnx * lny * lnz);
90 for (int z = 0; z < lnz; ++z)
91 for (int y = 0; y < lny; ++y)
92 for (int x = 0; x < lnx; ++x)
93 lsdf[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
94 gsdf[(std::size_t)(x + ox) + (std::size_t)(y + oy) * N +
95 (std::size_t)(z + oz) * N * N];
96
97 for (int mode : {0, 9}) {
98 Colo sd(lnx, lny, lnz);
99 sd.initMpi(N, N, N, MPI_COMM_WORLD);
100 configure(sd, mode);
101 sd.setSolid(lsdf, /*cutcell_pressure=*/true);
102 for (int it = 0; it < STEPS; ++it)
103 sd.step();
104 double lsum = localUSum(sd), gsum = 0;
105 MPI_Allreduce(&lsum, &gsum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
106 const double k_dist = MU * (gsum / gcells) / F;
107 const double div_dist = sd.maxOpenDivergence();
108
109 // --- single-rank reference (full grid) on rank 0 ---
110 double k_ref = 0.0;
111 if (rank == 0) {
112 Colo ref(N, N, N);
113 configure(ref, mode);
114 ref.setSolid(gsdf, true);
115 for (int it = 0; it < STEPS; ++it)
116 ref.step();
117 double rsum = 0;
118 {
119 auto u = ref.getVelocity(0);
120 for (double v : u)
121 rsum += v;
122 }
123 k_ref = MU * (rsum / gcells) / F;
124 }
125 MPI_Bcast(&k_ref, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
126
127 const double reld = std::fabs(k_dist - k_ref) / (std::fabs(k_ref) + 1e-30);
128 const double tol =
129 (size == 1) ? 1e-12 : 2e-5; // np=1 bit-exact; np>1 the MG-PCG reduction-order floor
130 if (rank == 0)
131 std::printf(" [mode %d] k_dist=%.8e k_ref=%.8e rel=%.2e div=%.2e (np=%d, tol %.0e)\n",
132 mode, k_dist, k_ref, reld, div_dist, size, tol);
133 if (reld > tol || !(div_dist < 1e-5))
134 fail = 1;
135 }
136 }
137 int totalFail = 0;
138 MPI_Allreduce(&fail, &totalFail, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
139 if (rank == 0) {
140 if (totalFail == 0)
141 std::printf("OK (np=%d): distributed collocated Stokes permeability == single-rank\n", size);
142 else
143 std::fprintf(stderr, "FAILED (np=%d)\n", size);
144 }
145 Kokkos::finalize();
146 MPI_Finalize();
147 return totalFail == 0 ? 0 : 1;
148}
void setSolid(const std::vector< double > &sdfInner, bool cutcellPressure)
Definition flow_ibm.hpp:709
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
int main(int argc, char **argv)
static constexpr double RHO
static constexpr double MU
static void configure(Colo &s, int faceInterp)
static constexpr int STEPS
static constexpr int N
static constexpr double DT
static constexpr double F
static double localUSum(Colo &s)
static std::vector< double > packingSdf(double rfrac=0.18)