193 "pnm — pore-network extraction from SDF geometry (Kokkos).\n\n"
194 "Arrays are (Nz,Ny,Nx) C-order and every describing triple is z-y-x with a `_zyx` suffix. "
195 "Precision: the SDF is float32 and the geometry kernels compute in float32; origin_zyx / "
196 "spacing_zyx are doubles narrowed to float32 (pore centres and radii are float32 in the "
198 "unit system); the network-flow MAC fields (u, v, w, p, ox, oy, oz) are float64 and their "
199 "sums are double. SDF sign: negative inside the solid, positive in the pore space.";
204 peclet::core::python::install(m);
207 nb::class_<SDFReader>(m,
"SDFReader",
208 "VTI (VTK ImageData) reader for a signed-distance-field volume. Stateless: "
209 "the class exists only to hold read_vti as a static method, not to be "
210 "instantiated — call SDFReader.read_vti(filename) directly.")
213 [](
const std::string& filename) {
215 std::size_t shape[3]{(std::size_t)data->resolution[2], (std::size_t)data->resolution[1],
216 (std::size_t)data->resolution[0]};
217 std::vector<double> org{data->origin[2], data->origin[1], data->origin[0]};
218 std::vector<double> spc{data->spacing[2], data->spacing[1], data->spacing[0]};
221 nb::capsule owner(data, [](
void* p)
noexcept {
delete static_cast<SDFData*
>(p); });
222 nb::ndarray<nb::numpy, float> sdf_3d(data->sdf_values.data(), 3, shape, owner);
223 return nb::make_tuple(sdf_3d, org, spc);
225 "Reads a VTI (VTK ImageData) SDF volume. Returns the tuple (sdf, origin_zyx, "
226 "spacing_zyx), in that order: sdf a float32 (Nz,Ny,Nx) C-order array over the "
227 "reader's buffer (no copy), origin_zyx and spacing_zyx the z-y-x lists of the grid "
228 "origin and cell size (float64).");
230 nb::class_<Pore>(m,
"Pore",
231 "One detected pore: a strict local maximum of the SDF over its 26-neighbourhood "
232 "(periodic in all three directions). The centre is the peak voxel's position "
233 "plus a squared-SDF-weighted sub-voxel offset over the 3x3x3 stencil, in the "
234 "unit system of origin_zyx / spacing_zyx; the radius is the SDF value at the "
235 "peak, i.e. the physical inscribed-sphere radius. All fields are float32. "
236 "The centre is exposed as three self-named scalars x, y, z (not a triple), so "
237 "no axis order is implied and no _zyx suffix applies — the documented "
238 "exception to NAMING.md 1.7.")
240 .def(
"__init__", &
chk::initPore, nb::arg(
"x"), nb::arg(
"y"), nb::arg(
"z"), nb::arg(
"radius"))
241 .def_rw(
"x", &
Pore::x,
"Centre x coordinate (physical units of origin_zyx / spacing_zyx).")
242 .def_rw(
"y", &
Pore::y,
"Centre y coordinate (physical units of origin_zyx / spacing_zyx).")
243 .def_rw(
"z", &
Pore::z,
"Centre z coordinate (physical units of origin_zyx / spacing_zyx).")
245 "Inscribed-sphere radius = the SDF value at the peak voxel (physical units).")
246 .def(
"__repr__", [](
const Pore& p) {
248 std::snprintf(buf,
sizeof buf,
"Pore(x=%g, y=%g, z=%g, radius=%g)", (
double)p.x,
249 (
double)p.y, (
double)p.z, (
double)p.radius);
250 return std::string(buf);
255 [](nb::ndarray<float, nb::c_contig> sdf, std::vector<double> origin_zyx,
256 std::vector<double> spacing_zyx) {
257 chk::grid(
"extract_pores", origin_zyx, spacing_zyx);
258 std::array<int, 3> res;
259 auto v =
to_sdf(sdf, res);
260 std::array<float, 3> org{(float)origin_zyx[2], (
float)origin_zyx[1], (float)origin_zyx[0]};
261 std::array<float, 3> spc{(float)spacing_zyx[2], (
float)spacing_zyx[1],
262 (float)spacing_zyx[0]};
265 nb::arg(
"sdf"), nb::arg(
"origin_zyx"), nb::arg(
"spacing_zyx"),
266 "Pore detection: every voxel with sdf > 0 that is a strict local maximum of the SDF over its "
267 "26 neighbours (periodic wrap in x, y, z; ties broken towards the higher flat index) becomes "
268 "a Pore. sdf is a float32 (Nz,Ny,Nx) C-order array; origin_zyx / spacing_zyx are the grid's "
269 "z-y-x origin and cell size (narrowed to float32). Returns the list of Pore(x, y, z, radius) "
270 "in the input unit system, in device-completion order (NOT sorted; sort by (z, y, x) for a "
271 "reproducible order). Capped at 1e6 pores.");
275 [](nb::ndarray<float, nb::c_contig> sdf, std::vector<double> spacing_zyx) {
277 std::array<int, 3> res;
278 auto v =
to_sdf(sdf, res);
279 std::array<float, 3> spc{(float)spacing_zyx[2], (
float)spacing_zyx[1],
280 (float)spacing_zyx[0]};
283 nb::arg(
"sdf"), nb::arg(
"spacing_zyx"),
284 "Marker-controlled watershed segmentation of the SDF grid. Returns an int32 (Nz,Ny,Nx) "
285 "C-order label array (sdf.shape; the same memory as the kernels' flat x-fastest label "
286 "vector — no copy, no reshape needed): pore voxels (sdf > 0) carry the "
287 "id of the pore basin they belong to, 1, 2, ... in first-encounter (flat-index) order of the "
288 "basin peaks — the same peaks extract_pores finds — assigned by a gradient-ascent path to "
289 "the local SDF maximum; solid voxels (sdf <= 0) carry the NEGATIVE id -1, -2, ... of their "
290 "connected solid grain (26-connected components of the deep solid, sdf < -1.5 * min "
291 "spacing, flooded outwards by a Jacobi min-label sweep), and 0 marks solid debris no grain "
292 "reached. Periodic in all three directions. spacing_zyx (the cell size per axis, in the "
293 "SDF's length units) only sets the deep-solid marker threshold.");
297 [](nb::ndarray<const int, nb::c_contig> segmentation,
298 std::optional<std::vector<int>> shape_zyx) {
300 std::array<int, 3> res;
301 if (segmentation.ndim() == 3) {
302 res = {(int)segmentation.shape(2), (int)segmentation.shape(1),
303 (int)segmentation.shape(0)};
304 if (shape_zyx && (shape_zyx->size() != 3 || (*shape_zyx)[0] != res[2] ||
305 (*shape_zyx)[1] != res[1] || (*shape_zyx)[2] != res[0]))
306 throw std::runtime_error(
"shape_zyx does not match the (Nz,Ny,Nx) segmentation");
307 }
else if (segmentation.ndim() == 1) {
309 throw std::runtime_error(
"a flat segmentation needs shape_zyx = (Nz, Ny, Nx)");
310 if (shape_zyx->size() != 3)
311 throw std::runtime_error(
"shape_zyx must be (Nz, Ny, Nx)");
312 res = {(*shape_zyx)[2], (*shape_zyx)[1], (*shape_zyx)[0]};
313 if (segmentation.size() != std::size_t(res[0]) * res[1] * res[2])
314 throw std::runtime_error(
"segmentation length does not match shape_zyx");
316 throw std::runtime_error(
"segmentation must be (Nz,Ny,Nx) or flat");
321 nb::arg(
"segmentation"), nb::arg(
"shape_zyx") = nb::none(),
322 "Label adjacency of a segment_volume result: the sorted, unique (a, b) pairs with a < b of "
323 "labels that share a voxel face (+x, +y, +z, periodic wrap), as an (M,2) int32 array. "
324 "segmentation is the int32 (Nz,Ny,Nx) array segment_volume returned (read in place, no "
325 "copy); a flat x-fastest label vector is accepted too, then shape_zyx = (Nz, Ny, Nx) of "
326 "the grid it was made on (= sdf.shape) is required. Rows with a label <= 0 are pore-solid "
327 "(or grain-grain / debris) contacts; the pore-to-pore throats are the rows with both "
328 "labels > 0. One row per label pair (a per-PATCH throat list, which can repeat a pair, is "
329 "what extract_network_flow returns).");
333 "extract_pore_network",
334 [](nb::ndarray<float, nb::c_contig> sdf, std::vector<double> origin_zyx,
335 std::vector<double> spacing_zyx) {
336 chk::grid(
"extract_pore_network", origin_zyx, spacing_zyx);
337 std::array<int, 3> res;
338 auto v =
to_sdf(sdf, res);
339 std::array<float, 3> org{(float)origin_zyx[2], (
float)origin_zyx[1], (float)origin_zyx[0]};
340 std::array<float, 3> spc{(float)spacing_zyx[2], (
float)spacing_zyx[1],
341 (float)spacing_zyx[0]};
346 nb::arg(
"sdf"), nb::arg(
"origin_zyx"), nb::arg(
"spacing_zyx"),
347 "Fused extraction (SDF uploaded once, segmentation device-resident across stages): returns "
348 "the tuple (pores, segmentation, connections), in that order, each element exactly what "
349 "extract_pores, segment_volume and extract_topology return (list[Pore]; int32 (Nz,Ny,Nx) "
350 "array; (M,2) int32 array).\n\n"
351 "UNITS: everything is in the system `origin_zyx` / `spacing_zyx` and the SDF are stated in "
352 "(the VTI's own, straight from SDFReader.read_vti) — pore centres are physical coordinates "
353 "and a pore radius is the physical inscribed radius, not a voxel count. Pass "
354 "spacing_zyx = [1,1,1] and origin_zyx = [0,0,0] to work in voxels. Measured on "
355 "flow/data/packing_ring.vti: doubling the spacing, the origin and the SDF together leaves "
356 "the pore count (7199) and the throat-connection count (53020) unchanged and doubles every "
357 "radius BITWISE. One sub-voxel caveat: the centre's intra-cell offset is guarded by an "
358 "absolute `sw > 1e-6` on a squared-distance weight sum, so a near-degenerate peak's centre "
359 "can shift by a fraction of a cell (measured up to 0.37 cells) under a change of unit "
360 "system. Radii, counts and topology are unaffected.");
365 static const std::string kNetworkFlowDoc =
367 "Pore-network FLOW data from a MAC field on the same grid as the SDF: segments the SDF, "
368 "then returns per-pore-label centers/pressures (trilinear p at the basin peak, periodic) "
369 "and per-throat flow rates (sum of openness-weighted MAC face fluxes o*u*A over the "
370 "label-interface faces; positive from the lower to the higher label). All arrays are "
371 "(Nz,Ny,Nx) C-order on the SDF grid: pass flow's fields as get_uf().T, get_p().T, "
372 "get_ox().T, ... u(i,j,k) is the -x face velocity of cell (i,j,k) (flow's MAC layout); "
373 "omit ox/oy/oz for a fully open grid. grad_p_zyx adds the macroscopic gradient along "
374 "the min-image pore-to-pore vector to throat_dp (= P_i - P_j, drives flow i->j when "
375 "positive). pore_residual is the signed flux sum over each pore's whole boundary — "
376 "~solver tolerance when u is flow's projected divergence-free field. Throats are "
377 "PER-PATCH (a connected patch of interface faces): two disjoint interfaces between the "
378 "same two pores are separate parallel throats, so the throat list can repeat a label "
382 "extract_network_flow",
383 [](nb::ndarray<float, nb::c_contig> sdf, std::vector<double> origin_zyx,
384 std::vector<double> spacing_zyx, nb::ndarray<double, nb::c_contig> u,
385 nb::ndarray<double, nb::c_contig> v, nb::ndarray<double, nb::c_contig> w,
386 nb::ndarray<double, nb::c_contig> p, std::optional<nb::ndarray<double, nb::c_contig>> ox,
387 std::optional<nb::ndarray<double, nb::c_contig>> oy,
388 std::optional<nb::ndarray<double, nb::c_contig>> oz, std::vector<double> grad_p_zyx) {
389 std::array<int, 3> res;
390 auto sv =
to_sdf(sdf, res);
391 if ((
bool)ox != (
bool)oy || (
bool)ox != (
bool)oz)
392 throw std::runtime_error(
"pass all three openness arrays (ox,oy,oz) or none");
393 std::array<float, 3> org{(float)origin_zyx[2], (
float)origin_zyx[1], (float)origin_zyx[0]};
394 std::array<float, 3> spc{(float)spacing_zyx[2], (
float)spacing_zyx[1],
395 (float)spacing_zyx[0]};
396 std::array<double, 3> gp{grad_p_zyx[2], grad_p_zyx[1], grad_p_zyx[0]};
398 sv, res, org, spc,
to_field(u, res,
"u"),
to_field(v, res,
"v"),
to_field(w, res,
"w"),
399 to_field(p, res,
"p"), ox ?
to_field(*ox, res,
"ox") : std::vector<double>{},
400 oy ?
to_field(*oy, res,
"oy") : std::vector<double>{},
401 oz ?
to_field(*oz, res,
"oz") : std::vector<double>{}, gp);
404 nb::arg(
"sdf"), nb::arg(
"origin_zyx"), nb::arg(
"spacing_zyx"), nb::arg(
"u"), nb::arg(
"v"),
405 nb::arg(
"w"), nb::arg(
"p"), nb::arg(
"ox") = nb::none(), nb::arg(
"oy") = nb::none(),
406 nb::arg(
"oz") = nb::none(), nb::arg(
"grad_p_zyx") = std::vector<double>{0.0, 0.0, 0.0},
407 kNetworkFlowDoc.c_str());
416 [](std::vector<int> global_shape_zyx) {
418 if (global_shape_zyx.size() != 3)
419 throw std::runtime_error(
"global_shape_zyx must be (Nz, Ny, Nx)");
421 std::array<int, 3> gd{global_shape_zyx[2], global_shape_zyx[1], global_shape_zyx[0]};
422 std::array<int, 3> o{}, s{};
424 return nb::make_tuple(nb::make_tuple(o[2], o[1], o[0]), nb::make_tuple(s[2], s[1], s[0]));
426 nb::arg(
"global_shape_zyx"),
427 "This rank's ORB block of the global (Nz,Ny,Nx) grid (collective; MPI_Init is called if "
428 "needed): returns the tuple (offset_zyx, shape_zyx), in that order — two int 3-tuples, the "
429 "block's first VOXEL index per axis and its voxel count per axis. offset_zyx is an integer "
430 "grid offset, NOT the physical origin_zyx the extraction functions take (that stays the "
431 "global grid's). Slice the global SDF as sdf[oz:oz+sz, oy:oy+sy, ox:ox+sx] and pass the "
432 "block to extract_pore_network_mpi / extract_network_flow_mpi.");
434 "extract_pore_network_mpi",
435 [](nb::ndarray<float, nb::c_contig> sdf_local, std::vector<int> global_shape_zyx,
436 std::vector<double> origin_zyx, std::vector<double> spacing_zyx) {
438 chk::grid(
"extract_pore_network_mpi", origin_zyx, spacing_zyx);
440 std::array<int, 3> res;
441 auto v =
to_sdf(sdf_local, res);
442 std::array<int, 3> gd{global_shape_zyx[2], global_shape_zyx[1], global_shape_zyx[0]};
443 std::array<int, 3> bo{}, bs{};
446 throw std::runtime_error(
"sdf_local shape does not match this rank's mpi_block");
447 std::array<float, 3> org{(float)origin_zyx[2], (
float)origin_zyx[1], (float)origin_zyx[0]};
448 std::array<float, 3> spc{(float)spacing_zyx[2], (
float)spacing_zyx[1],
449 (float)spacing_zyx[0]};
454 nb::arg(
"sdf_local"), nb::arg(
"global_shape_zyx"), nb::arg(
"origin_zyx"),
455 nb::arg(
"spacing_zyx"),
456 "Distributed fused extraction (collective over MPI_COMM_WORLD). sdf_local is this rank's "
457 "ORB block (Nz,Ny,Nx C-order, from mpi_block); origin_zyx / spacing_zyx are the GLOBAL "
458 "grid's, in the SDF's length units (as for extract_pore_network). Returns the tuple "
459 "(pores, segmentation, connections), in that order: the pores "
460 "whose peak this rank owns (list[Pore]), this rank's block of the segmentation (int32 "
461 "array of sdf_local.shape, global label ids), and the GLOBAL connection list ((M,2) int32, "
462 "identical on every rank). Bit-exact to the single-rank extract_pore_network on the "
464 static const std::string kNetworkFlowMpiDoc =
466 "Distributed network-flow extraction (collective over MPI_COMM_WORLD): all arrays are "
467 "this rank's ORB block (Nz,Ny,Nx C-order, from mpi_block; MAC fields from a distributed "
468 "peclet.flow run on the SAME BlockDecomposer). Returns the same dict as "
469 "extract_network_flow, GLOBAL and identical on every rank.") +
472 "extract_network_flow_mpi",
473 [](nb::ndarray<float, nb::c_contig> sdf_local, std::vector<int> global_shape_zyx,
474 std::vector<double> origin_zyx, std::vector<double> spacing_zyx,
475 nb::ndarray<double, nb::c_contig> u, nb::ndarray<double, nb::c_contig> v,
476 nb::ndarray<double, nb::c_contig> w, nb::ndarray<double, nb::c_contig> p,
477 std::optional<nb::ndarray<double, nb::c_contig>> ox,
478 std::optional<nb::ndarray<double, nb::c_contig>> oy,
479 std::optional<nb::ndarray<double, nb::c_contig>> oz, std::vector<double> grad_p_zyx) {
481 std::array<int, 3> res;
482 auto sv =
to_sdf(sdf_local, res);
483 std::array<int, 3> gd{global_shape_zyx[2], global_shape_zyx[1], global_shape_zyx[0]};
484 std::array<int, 3> bo{}, bs{};
487 throw std::runtime_error(
"sdf_local shape does not match this rank's mpi_block");
488 if ((
bool)ox != (
bool)oy || (
bool)ox != (
bool)oz)
489 throw std::runtime_error(
"pass all three openness arrays (ox,oy,oz) or none");
490 std::array<float, 3> org{(float)origin_zyx[2], (
float)origin_zyx[1], (float)origin_zyx[0]};
491 std::array<float, 3> spc{(float)spacing_zyx[2], (
float)spacing_zyx[1],
492 (float)spacing_zyx[0]};
493 std::array<double, 3> gp{grad_p_zyx[2], grad_p_zyx[1], grad_p_zyx[0]};
495 sv, gd, org, spc,
to_field(u, res,
"u"),
to_field(v, res,
"v"),
to_field(w, res,
"w"),
496 to_field(p, res,
"p"), ox ?
to_field(*ox, res,
"ox") : std::vector<double>{},
497 oy ?
to_field(*oy, res,
"oy") : std::vector<double>{},
498 oz ?
to_field(*oz, res,
"oz") : std::vector<double>{}, gp, MPI_COMM_WORLD);
501 nb::arg(
"sdf_local"), nb::arg(
"global_shape_zyx"), nb::arg(
"origin_zyx"),
502 nb::arg(
"spacing_zyx"), nb::arg(
"u"), nb::arg(
"v"), nb::arg(
"w"), nb::arg(
"p"),
503 nb::arg(
"ox") = nb::none(), nb::arg(
"oy") = nb::none(), nb::arg(
"oz") = nb::none(),
504 nb::arg(
"grad_p_zyx") = std::vector<double>{0.0, 0.0, 0.0}, kNetworkFlowMpiDoc.c_str());