diff -r a4137aedcb3a -r c3a4f4bb87f7 src/dolfinx_access.rs --- a/src/dolfinx_access.rs Thu Feb 26 09:32:12 2026 -0500 +++ b/src/dolfinx_access.rs Wed Apr 22 22:32:00 2026 -0500 @@ -1,3 +1,6 @@ +/*! + Utility functions accessing C++ side Dolfinx. +*/ use alg_tools::error::DynResult; use alg_tools::fe_model::{ base::RealLocalModel, @@ -9,7 +12,7 @@ use std::ffi::CString; use std::mem::MaybeUninit; -// These are required for the linking to the sys crates to actually happen. +/// Import helper required for the linking to the sys crates to actually happen. #[allow(unused_imports)] mod dummy_import { use dolfinx_sys; @@ -19,20 +22,29 @@ mod function; pub use function::DolfinxPyFunction_f64; +/// C++ side routines #[allow(dead_code)] #[cxx::bridge(namespace = "dolfinx_access")] pub mod ffi { + /// Structure for coordinte-value pairs #[derive(Copy, Clone, Debug)] struct CoordValuePair { + /// 2D coordinate x: [f64; 2], + /// Value v: f64, } + /// Structure for information about a dolfinx `Function` #[derive(Copy, Clone, Debug)] struct FunctionInfo { + /// Domain dimension domain_dim: u32, + /// Codomain dimension codomain_dim: u32, + /// Element order order: u32, + /// Whether the mesh is triangular triangular_mesh: bool, } @@ -41,7 +53,9 @@ include!("pointsource_pde/include/dolfinx_access/function.h"); include!("pointsource_pde/include/dolfinx_access/minmax_p2.h"); + /// C++ side Dolfinx `Function` type Function_f64; + /// C-side python object. type PyObject; /// Find the cell containing `x` (in 1D, 2D, or 3D, following Fenics weirdness @@ -66,18 +80,34 @@ cell: i32, ) -> [f64; 6]; + /// Get information about a dolfinx `Function_f64`. pub unsafe fn info_Function_f64(f: *const Function_f64) -> FunctionInfo; + /// Returns the minimum value of the Dolfinx `Function`, + /// if supported (P2 elements on triangular mesh) pub unsafe fn min_Function_f64_p2(f: *const Function_f64) -> Result; + + /// Returns the maximum value of the Dolfinx `Function`, + /// if supported (P2 elements on triangular mesh) pub unsafe fn max_Function_f64_p2(f: *const Function_f64) -> Result; + + /// Returns the minimum or maximum value of the Dolfinx `Function`, + /// if supported (P2 elements on triangular mesh) pub unsafe fn minmax_Function_f64_p2( f: *const Function_f64, max: bool, ) -> Result; + /// Checks if a Dolfinx `Function` is supported (P2 elements on triangular mesh) pub unsafe fn check_Function_f64(o: *const PyObject) -> bool; + + /// Casts a Python object into a Dolfinx `Function` wrapper. pub unsafe fn cast_Function_f64(o: *const PyObject) -> Result<*const Function_f64>; + + /// Casts a Python object into a mutable Dolfinx `Function` wrapper. pub unsafe fn cast_mut_Function_f64(o: *mut PyObject) -> Result<*mut Function_f64>; + + /// Gets the 3D coordinates of the nodes of a triangular cell pub unsafe fn cell_coords_Function_f64_triangle( f: *const Function_f64, cell: i32, @@ -95,18 +125,28 @@ g: *const Function_f64, ) -> Result<()>; - /// Create a new similar function + /// Create a new similar Dolfinx `Function` pub unsafe fn similar_Function_f64(f: *const Function_f64) -> *mut Function_f64; - /// Clone the function + /// Clone the Dolfinx `Function` pub unsafe fn clone_Function_f64(f: *const Function_f64) -> *mut Function_f64; + /// Wraps a Dolfinx `Function` into a Python object pub unsafe fn wrap_Function_f64(f: *mut Function_f64) -> *mut PyObject; + /// Drop helper pub unsafe fn drop_Function_f64(f: *mut Function_f64); + + /// Scatter helper. Not truly supported. + pub unsafe fn scatter_fwd_Function_f64(f: *mut Function_f64); } extern "Rust" { + /// Rust-side helper for minimising P2 elements. This bypasses poorly documented basix, + /// and does things at a low level directly. + /// `f` is the function, `simplex_coords` the coorindates of the triangle + /// see [`cell_coords_Function_f64_triangle`], `cell` is the cell number, + /// and `max` indicates whether to maximise or minimise. unsafe fn minmax_dolfinx_p2_cell( f: *const Function_f64, simplex_coords: &[f64; 9],