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"

Go to the source code of this file.

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::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()

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

Definition at line 83 of file pnm_bindings.cpp.

Referenced by NB_MODULE().

◆ to_field()

static 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 91 of file pnm_bindings.cpp.

Referenced by NB_MODULE().

◆ labels_to_ndarray()

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

Definition at line 103 of file pnm_bindings.cpp.

Referenced by NB_MODULE().

◆ pairs_to_ndarray()

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

Definition at line 113 of file pnm_bindings.cpp.

Referenced by NB_MODULE(), and network_flow_dict().

◆ doubles_to_ndarray()

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

Definition at line 125 of file pnm_bindings.cpp.

Referenced by network_flow_dict().

◆ network_flow_dict()

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

Definition at line 131 of file pnm_bindings.cpp.

References doubles_to_ndarray(), and pairs_to_ndarray().

Referenced by NB_MODULE().

◆ NB_MODULE()

Variable Documentation

◆ kNetworkFlowDictDoc

constexpr 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"
"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 145 of file pnm_bindings.cpp.

Referenced by NB_MODULE().