pnm 1.0.3
peclet.pnm — pore-network extraction from SDF geometry (Kokkos)
Loading...
Searching...
No Matches
test_pnm_flow_mpi.cpp
Go to the documentation of this file.
1// peclet-pnm — distributed network-flow extraction vs the single-rank oracle.
2//
3// A sphere-lattice SDF + deterministic smooth MAC fields (u,v,w,p and openness — not a physical
4// flow; the bookkeeping only needs consistency) are extracted (a) via extract_network_flow_mpi
5// distributed over the ORB blocks and (b) via the single-rank extract_network_flow_k on rank 0.
6// The distributed result is global and identical on every rank; it must match the oracle: throat
7// list and pore ids exactly; Q/A/dp/pressures/residuals and the momentum/energy/volume-mean
8// extension to accumulation-order tolerance (both paths sum with atomics); pore volumes and
9// volume centroids bitwise (exact integer sums); pore positions to the CUDA FMA-contraction
10// tolerance. Two configs:
11// with openness fields and fully open. np = 1, 2, 4, any Kokkos backend.
12#include <mpi.h>
13
14#include <algorithm>
15#include <array>
16#include <cmath>
17#include <cstdio>
18#include <Kokkos_Core.hpp>
19#include <vector>
20
21#include "../synthetic_sdf.hpp"
22#include "pore_extraction.hpp"
24
25using pnm::Pore;
26
27namespace {
28
29double fld(int c, int gx, int gy, int gz, std::array<int, 3> gd) {
30 const double x = 2.0 * M_PI * gx / gd[0], y = 2.0 * M_PI * gy / gd[1],
31 z = 2.0 * M_PI * gz / gd[2];
32 switch (c) {
33 case 0:
34 return std::sin(x) * std::cos(y) + 0.3 * std::cos(2 * z);
35 case 1:
36 return std::cos(x) * std::sin(z) - 0.2 * std::sin(y);
37 case 2:
38 return std::sin(y) * std::sin(z) + 0.1 * std::cos(x);
39 case 3:
40 return std::cos(x + y) + 0.5 * std::sin(z - x); // p
41 default: {
42 const double s = 0.5 + 0.5 * std::sin(x + 2 * y - z); // openness in [0,1]
43 return s < 0.02 ? 0.0 : s;
44 }
45 }
46}
47
48bool close(double a, double b, double tol) {
49 return std::fabs(a - b) <= tol;
50}
51
52// Momentum/volume extension arrays: same length, and every entry within rtol * max|oracle| of the
53// oracle (atomic accumulation order); rtol = 0 demands bitwise equality (the order-independent
54// integer sums: volumes, volume centroids). Prints the measured max deviation.
55bool closeArray(const char* cfg, const char* key, const std::vector<double>& d,
56 const std::vector<double>& o, double rtol) {
57 if (d.size() != o.size()) {
58 std::printf("[%s] FAIL %s: size %zu vs %zu\n", cfg, key, d.size(), o.size());
59 return false;
60 }
61 double scale = 1e-300, dev = 0.0;
62 for (double x : o)
63 scale = std::max(scale, std::fabs(x));
64 bool ok = true;
65 for (std::size_t i = 0; i < o.size(); ++i) {
66 dev = std::max(dev, std::fabs(d[i] - o[i]));
67 ok = ok && (rtol == 0.0 ? d[i] == o[i] : close(d[i], o[i], rtol * scale));
68 }
69 std::printf("[%s] %s %s: n=%zu max|dev|/max|ref| = %.3e%s\n", cfg, ok ? "ok " : "FAIL", key,
70 o.size(), dev / scale, rtol == 0.0 ? " (bitwise)" : "");
71 return ok;
72}
73
74int runConfig(const char* name, bool withOpen, MPI_Comm comm) {
75 const std::array<int, 3> gd{36, 30, 24};
76 const std::array<float, 3> org{0.5f, -1.0f, 2.0f};
77 const std::array<float, 3> spc{0.5f, 1.0f, 1.5f};
78 const std::array<double, 3> gp{1.0e-3, -2.0e-3, 0.5e-3};
79 int rank = 0;
80 MPI_Comm_rank(comm, &rank);
81
82 std::array<int, 3> bo{}, bs{};
83 pnm::mpi_block_of(gd, comm, bo, bs);
84 const std::size_t nl = std::size_t(bs[0]) * bs[1] * bs[2];
85 std::vector<float> sdfL(nl);
86 std::vector<double> fldL[7];
87 for (int c = 0; c < 7; ++c)
88 fldL[c].resize(nl);
89 for (int z = 0; z < bs[2]; ++z)
90 for (int y = 0; y < bs[1]; ++y)
91 for (int x = 0; x < bs[0]; ++x) {
92 const std::size_t i = (std::size_t(z) * bs[1] + y) * bs[0] + x;
93 const int gx = bo[0] + x, gy = bo[1] + y, gz = bo[2] + z;
94 sdfL[i] = pnm::test::sphereLatticeSdf(gx, gy, gz, gd);
95 for (int c = 0; c < 4; ++c)
96 fldL[c][i] = fld(c, gx, gy, gz, gd);
97 for (int c = 4; c < 7; ++c)
98 fldL[c][i] = fld(4, gx + c, gy, gz, gd); // three distinct openness fields
99 }
100 std::vector<double> none;
101 auto dist = pnm::extract_network_flow_mpi(sdfL, gd, org, spc, fldL[0], fldL[1], fldL[2], fldL[3],
102 withOpen ? fldL[4] : none, withOpen ? fldL[5] : none,
103 withOpen ? fldL[6] : none, gp, comm);
104
105 int fail = 0;
106 if (rank == 0) {
107 const std::size_t n = std::size_t(gd[0]) * gd[1] * gd[2];
108 std::vector<float> sdfG(n);
109 std::vector<double> fldG[7];
110 for (int c = 0; c < 7; ++c)
111 fldG[c].resize(n);
112 for (int z = 0; z < gd[2]; ++z)
113 for (int y = 0; y < gd[1]; ++y)
114 for (int x = 0; x < gd[0]; ++x) {
115 const std::size_t i = (std::size_t(z) * gd[1] + y) * gd[0] + x;
116 sdfG[i] = pnm::test::sphereLatticeSdf(x, y, z, gd);
117 for (int c = 0; c < 4; ++c)
118 fldG[c][i] = fld(c, x, y, z, gd);
119 for (int c = 4; c < 7; ++c)
120 fldG[c][i] = fld(4, x + c, y, z, gd);
121 }
122 auto orc = pnm::extract_network_flow_k(sdfG, gd, org, spc, fldG[0], fldG[1], fldG[2], fldG[3],
123 withOpen ? fldG[4] : none, withOpen ? fldG[5] : none,
124 withOpen ? fldG[6] : none, gp);
125 double qs = 1e-30;
126 for (double q : orc.throat_flow)
127 qs = std::max(qs, std::fabs(q));
128 if (dist.throats != orc.throats) {
129 std::printf("[%s] FAIL throat list: %zu vs %zu\n", name, dist.throats.size(),
130 orc.throats.size());
131 fail = 1;
132 } else {
133 for (std::size_t t = 0; t < orc.throats.size() && !fail; ++t) {
134 if (!close(dist.throat_flow[t], orc.throat_flow[t], 1e-10 * qs) ||
135 !close(dist.throat_area[t], orc.throat_area[t], 1e-10 * qs) ||
136 !close(dist.throat_dp[t], orc.throat_dp[t], 1e-8)) {
137 std::printf("[%s] FAIL throat %zu: Q %.15e vs %.15e A %.15e vs %.15e dp %g vs %g\n",
138 name, t, dist.throat_flow[t], orc.throat_flow[t], dist.throat_area[t],
139 orc.throat_area[t], dist.throat_dp[t], orc.throat_dp[t]);
140 fail = 1;
141 }
142 }
143 }
144 if (dist.pores.size() != orc.pores.size()) {
145 std::printf("[%s] FAIL pore count %zu vs %zu\n", name, dist.pores.size(), orc.pores.size());
146 fail = 1;
147 } else {
148 for (std::size_t k = 0; k < orc.pores.size() && !fail; ++k) {
149 const Pore &a = dist.pores[k], &b = orc.pores[k];
150 if (a.radius != b.radius || !close(a.x, b.x, 1e-5 * spc[0]) ||
151 !close(a.y, b.y, 1e-5 * spc[1]) || !close(a.z, b.z, 1e-5 * spc[2]) ||
152 !close(dist.pore_pressure[k], orc.pore_pressure[k], 1e-9) ||
153 !close(dist.pore_residual[k], orc.pore_residual[k], 1e-10 * qs)) {
154 std::printf("[%s] FAIL pore %zu (r %.9g vs %.9g, p %.15e vs %.15e)\n", name, k, a.radius,
155 b.radius, dist.pore_pressure[k], orc.pore_pressure[k]);
156 fail = 1;
157 }
158 }
159 }
160 if (!fail) {
161 // the extension: atomic-order tolerance like Q/A, bitwise for the integer volume sums
162 const double rt = 1e-10;
163 bool ok = closeArray(name, "throat_momentum_flux_zyx", dist.throat_momentum_flux_zyx,
164 orc.throat_momentum_flux_zyx, rt);
165 ok &= closeArray(name, "throat_kinetic_energy_flux", dist.throat_kinetic_energy_flux,
166 orc.throat_kinetic_energy_flux, rt);
167 ok &= closeArray(name, "throat_pressure_work_flux", dist.throat_pressure_work_flux,
168 orc.throat_pressure_work_flux, rt);
169 ok &=
170 closeArray(name, "throat_normal_zyx", dist.throat_normal_zyx, orc.throat_normal_zyx, rt);
171 ok &= closeArray(name, "throat_centroid_zyx", dist.throat_centroid_zyx,
172 orc.throat_centroid_zyx, rt);
173 ok &= closeArray(name, "throat_dp_mean", dist.throat_dp_mean, orc.throat_dp_mean, rt);
174 ok &= closeArray(name, "pore_volume", dist.pore_volume, orc.pore_volume, 0.0);
175 ok &= closeArray(name, "pore_mean_pressure", dist.pore_mean_pressure, orc.pore_mean_pressure,
176 rt);
177 ok &= closeArray(name, "pore_mean_kinetic_energy", dist.pore_mean_kinetic_energy,
178 orc.pore_mean_kinetic_energy, rt);
179 ok &= closeArray(name, "pore_mean_velocity_zyx", dist.pore_mean_velocity_zyx,
180 orc.pore_mean_velocity_zyx, rt);
181 ok &= closeArray(name, "pore_volume_centroid_zyx", dist.pore_volume_centroid_zyx,
182 orc.pore_volume_centroid_zyx, 0.0);
183 fail = ok ? 0 : 1;
184 }
185 if (!fail)
186 std::printf("[%s] PASS: %zu pores, %zu throats match the single-rank oracle\n", name,
187 orc.pores.size(), orc.throats.size());
188 }
189 MPI_Bcast(&fail, 1, MPI_INT, 0, comm);
190 return fail;
191}
192
193} // namespace
194
195int main(int argc, char** argv) {
196 MPI_Init(&argc, &argv);
197 int fail = 0;
198 {
199 Kokkos::ScopeGuard kokkos(argc, argv);
200 fail += runConfig("openness", true, MPI_COMM_WORLD);
201 fail += runConfig("fully_open", false, MPI_COMM_WORLD);
202 }
203 MPI_Finalize();
204 return fail ? 1 : 0;
205}
float sphereLatticeSdf(int gx, int gy, int gz, std::array< int, 3 > gd)
2x2x2 lattice of SOLID spheres in a periodic box (min-image distance); pores at the interstices.
NetworkFlow extract_network_flow_k(const std::vector< float > &sdf_h, std::array< int, 3 > resolution, std::array< float, 3 > origin, std::array< float, 3 > spacing, const std::vector< double > &u_h, const std::vector< double > &v_h, const std::vector< double > &w_h, const std::vector< double > &p_h, const std::vector< double > &ox_h, const std::vector< double > &oy_h, const std::vector< double > &oz_h, std::array< double, 3 > grad_p)
NetworkFlow extract_network_flow_mpi(const std::vector< float > &sdf_local, std::array< int, 3 > gdims, std::array< float, 3 > origin, std::array< float, 3 > spacing, const std::vector< double > &u_h, const std::vector< double > &v_h, const std::vector< double > &w_h, const std::vector< double > &p_h, const std::vector< double > &ox_h, const std::vector< double > &oy_h, const std::vector< double > &oz_h, std::array< double, 3 > grad_p, MPI_Comm comm)
void mpi_block_of(std::array< int, 3 > gdims, MPI_Comm comm, std::array< int, 3 > &offset, std::array< int, 3 > &size)
This rank's ORB block of the global grid (deterministic; same partition as flow/dem): offset = the bl...
peclet.pnm — portable (Kokkos) single-rank pore-network extraction from an SDF.
peclet.pnm — distributed (MPI) pore-network extraction on the core block decomposition.
int main(int argc, char **argv)