pnm 1.0.3
peclet.pnm — pore-network extraction from SDF geometry (Kokkos)
Loading...
Searching...
No Matches
pnm_bindings.cpp File Reference

nanobind module pnm — Kokkos pore-network extraction from SDF geometry. More...

#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/optional.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <Kokkos_Core.hpp>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>
#include "peclet/core/python/kokkos_teardown.hpp"
#include "peclet/core/python/ndarray_interop.hpp"
#include "pore_extraction.hpp"
#include "sdf_reader.h"
#include "pnm_bindings_checks.hpp"

Go to the source code of this file.

Classes

struct  Pore

Functions

static std::vector< float > to_sdf (nb::ndarray< float, nb::c_contig > a, std::array< int, 3 > &res)
static std::vector< double > to_field (nb::ndarray< double, nb::c_contig > a, const std::array< int, 3 > &res, const char *name)
static nb::ndarray< nb::numpy, int > labels_to_ndarray (std::vector< int > &&seg, const std::array< int, 3 > &res)
static nb::ndarray< nb::numpy, int > pairs_to_ndarray (std::vector< std::pair< int, int > > &&v)
static nb::ndarray< nb::numpy, double > doubles_to_ndarray (std::vector< double > &&v)
static nb::ndarray< nb::numpy, double > triples_to_ndarray (std::vector< double > &&v)
static nb::dict network_flow_dict (pnm::NetworkFlow &&net)
 NB_MODULE (_pnm, m)

Variables

static constexpr const char * kNetworkFlowDictDoc

Detailed Description

nanobind module pnm — Kokkos pore-network extraction from SDF geometry.

Matches the numpy convention: SDF is (Nz,Ny,Nx) C-order, and every triple that describes it (origin_zyx, spacing_zyx, shape_zyx, grad_p_zyx) is stated in that order and carries the _zyx suffix (NAMING.md §1.7). VTI reading (SDFReader) is pure C++ (sdf_reader.cpp, backend-free); the pore / segmentation / topology / network-flow compute is Kokkos (any backend). Exposes SDFReader, Pore, extract_pores, segment_volume, extract_topology, extract_pore_network, extract_network_flow (+ mpi_block and the *_mpi collectives when built with PECLET_PNM_MPI). A C-order (Nz,Ny,Nx) buffer is contiguous x-fastest, so it maps onto the flat layout directly via the shared bridge (peclet::core::python, core).

Arrays in, arrays out: a segmentation is returned as an int32 (Nz,Ny,Nx) C-order ndarray (the flat x-fastest label vector re-shaped in place — no copy, no reshape on the caller's side), a connection / throat list as an (M,2) int32 ndarray, and the network-flow scalars as float64 ndarrays; the host vectors are moved into the capsule that backs the array (vector_to_ndarray). extract_topology takes that segmentation back without a copy. Only the pores stay a Python list of Pore objects (small, and each carries four named scalars).

Precision policy: the SDF is float32 (the VTI's storage type; the kernels compute in float), origin_zyx / spacing_zyx are taken as Python floats (double) and NARROWED to float32 before the kernels, so pore centres and radii are float32 in the input unit system; the MAC fields of the network-flow extraction (u, v, w, p, openness) are float64 and the flow sums are double.

Kokkos teardown follows the suite-wide pattern of peclet/core/python/kokkos_teardown.hpp: Kokkos is initialized at import and the module's single atexit hook (also pnm.finalize()) releases every registered View owner and THEN calls Kokkos::finalize. pnm keeps no Kokkos state between calls (its registry stays empty), but the finalize is still REQUIRED on CUDA (else cudaErrorCudartUnloading at exit).

Definition in file pnm_bindings.cpp.

Function Documentation

◆ to_sdf()

std::vector< float > to_sdf ( nb::ndarray< float, nb::c_contig > a,
std::array< int, 3 > & res )
static

Definition at line 86 of file pnm_bindings.cpp.

Referenced by NB_MODULE().

◆ to_field()

std::vector< double > to_field ( nb::ndarray< double, nb::c_contig > a,
const std::array< int, 3 > & res,
const char * name )
static

Definition at line 94 of file pnm_bindings.cpp.

Referenced by NB_MODULE().

◆ labels_to_ndarray()

nb::ndarray< nb::numpy, int > labels_to_ndarray ( std::vector< int > && seg,
const std::array< int, 3 > & res )
static

Definition at line 106 of file pnm_bindings.cpp.

Referenced by NB_MODULE().

◆ pairs_to_ndarray()

nb::ndarray< nb::numpy, int > pairs_to_ndarray ( std::vector< std::pair< int, int > > && v)
static

Definition at line 116 of file pnm_bindings.cpp.

Referenced by NB_MODULE(), and network_flow_dict().

◆ doubles_to_ndarray()

nb::ndarray< nb::numpy, double > doubles_to_ndarray ( std::vector< double > && v)
static

Definition at line 128 of file pnm_bindings.cpp.

Referenced by network_flow_dict().

◆ triples_to_ndarray()

nb::ndarray< nb::numpy, double > triples_to_ndarray ( std::vector< double > && v)
static

Definition at line 134 of file pnm_bindings.cpp.

Referenced by network_flow_dict().

◆ network_flow_dict()

nb::dict network_flow_dict ( pnm::NetworkFlow && net)
static

Definition at line 140 of file pnm_bindings.cpp.

References doubles_to_ndarray(), pairs_to_ndarray(), and triples_to_ndarray().

Referenced by NB_MODULE().

◆ NB_MODULE()

Variable Documentation

◆ kNetworkFlowDictDoc

const char* kNetworkFlowDictDoc
staticconstexpr
Initial value:
=
"\n\nReturns a dict: 'pores' (list[Pore], ordered by label: pores[k] is label k+1), "
"'pore_pressure' and 'pore_residual' (float64 (N,)), 'throats' ((M,2) int32 label pairs "
"a < b, per PATCH), 'throat_flow', 'throat_area' and 'throat_dp' (float64 (M,)).\n\n"
"Momentum/volume extension (per unit density; a throat is oriented throats[t,0] -> "
"throats[t,1] as throat_flow; every *_zyx array is (count,3) float64 with columns z, y, x): "
"'throat_momentum_flux_zyx' (M,3) = sum over the patch faces of o*A*u_vec*(u.n), u_vec the "
"face-centre velocity (normal = the MAC face value, each tangential component the mean of "
"the 4 nearest staggered values); 'throat_kinetic_energy_flux' (M,) = sum o*A*|u_vec|^2/2*"
"(u.n); 'throat_pressure_work_flux' (M,) = sum o*A*p_f*(u.n), p_f the mean of the two "
"adjacent cell pressures (periodic part); 'throat_normal_zyx' (M,3) = sum o*A*n / sum o*A "
"(oriented, NOT normalised: |n| < 1 "
"on a curved patch); 'throat_centroid_zyx' (M,3), open-area weighted, in the periodic image "
"nearest pore throats[t,0]'s peak voxel (not wrapped into the box); 'throat_dp_mean' (M,) = "
"throat_dp evaluated with pore_mean_pressure at the volume centroids. Per pore, over the "
"fluid cells (sdf > 0) of its watershed segment: 'pore_volume' (N,), 'pore_mean_pressure' "
"(N,) (periodic part of p, as pore_pressure), 'pore_mean_kinetic_energy' (N,) = mean "
"|u_c|^2/2 and 'pore_mean_velocity_zyx' (N,3) with u_c the cell-centred velocity (mean of "
"the cell's two faces per component), 'pore_volume_centroid_zyx' (N,3) in the image of the "
"pore's peak voxel.\n\n"
"PRECONDITION (cross-repo, not checkable here): the openness arrays must be the face "
"openness the velocity field was projected with. For peclet.flow's cut-cell IBM that means "
"the solid was set with set_solid(..., cutcell_pressure=True) — without it every openness "
"flow reports is 0, so every throat_flow and throat_area comes back 0; for the ghost-cell "
"IBM (set_ghost_projection) pass get_ox_proj()/get_oy_proj()/get_oz_proj() instead."

Definition at line 165 of file pnm_bindings.cpp.

Referenced by NB_MODULE().