src/dolfinx_access.rs

changeset 3
c3a4f4bb87f7
parent 1
a4137aedcb3a
equal deleted inserted replaced
1:a4137aedcb3a 3:c3a4f4bb87f7
1 /*!
2 Utility functions accessing C++ side Dolfinx.
3 */
1 use alg_tools::error::DynResult; 4 use alg_tools::error::DynResult;
2 use alg_tools::fe_model::{ 5 use alg_tools::fe_model::{
3 base::RealLocalModel, 6 base::RealLocalModel,
4 p2_local_model::{P2LocalModel, Simplex}, 7 p2_local_model::{P2LocalModel, Simplex},
5 }; 8 };
7 use anyhow::bail; 10 use anyhow::bail;
8 use pyo3::ffi::PyImport_AppendInittab; 11 use pyo3::ffi::PyImport_AppendInittab;
9 use std::ffi::CString; 12 use std::ffi::CString;
10 use std::mem::MaybeUninit; 13 use std::mem::MaybeUninit;
11 14
12 // These are required for the linking to the sys crates to actually happen. 15 /// Import helper required for the linking to the sys crates to actually happen.
13 #[allow(unused_imports)] 16 #[allow(unused_imports)]
14 mod dummy_import { 17 mod dummy_import {
15 use dolfinx_sys; 18 use dolfinx_sys;
16 use nanobind_sys; 19 use nanobind_sys;
17 } 20 }
18 21
19 mod function; 22 mod function;
20 pub use function::DolfinxPyFunction_f64; 23 pub use function::DolfinxPyFunction_f64;
21 24
25 /// C++ side routines
22 #[allow(dead_code)] 26 #[allow(dead_code)]
23 #[cxx::bridge(namespace = "dolfinx_access")] 27 #[cxx::bridge(namespace = "dolfinx_access")]
24 pub mod ffi { 28 pub mod ffi {
29 /// Structure for coordinte-value pairs
25 #[derive(Copy, Clone, Debug)] 30 #[derive(Copy, Clone, Debug)]
26 struct CoordValuePair { 31 struct CoordValuePair {
32 /// 2D coordinate
27 x: [f64; 2], 33 x: [f64; 2],
34 /// Value
28 v: f64, 35 v: f64,
29 } 36 }
30 37
38 /// Structure for information about a dolfinx `Function`
31 #[derive(Copy, Clone, Debug)] 39 #[derive(Copy, Clone, Debug)]
32 struct FunctionInfo { 40 struct FunctionInfo {
41 /// Domain dimension
33 domain_dim: u32, 42 domain_dim: u32,
43 /// Codomain dimension
34 codomain_dim: u32, 44 codomain_dim: u32,
45 /// Element order
35 order: u32, 46 order: u32,
47 /// Whether the mesh is triangular
36 triangular_mesh: bool, 48 triangular_mesh: bool,
37 } 49 }
38 50
39 unsafe extern "C++" { 51 unsafe extern "C++" {
40 include!("pointsource_pde/include/dolfinx_access/nanobind_helpers.h"); 52 include!("pointsource_pde/include/dolfinx_access/nanobind_helpers.h");
41 include!("pointsource_pde/include/dolfinx_access/function.h"); 53 include!("pointsource_pde/include/dolfinx_access/function.h");
42 include!("pointsource_pde/include/dolfinx_access/minmax_p2.h"); 54 include!("pointsource_pde/include/dolfinx_access/minmax_p2.h");
43 55
56 /// C++ side Dolfinx `Function<double>`
44 type Function_f64; 57 type Function_f64;
58 /// C-side python object.
45 type PyObject; 59 type PyObject;
46 60
47 /// Find the cell containing `x` (in 1D, 2D, or 3D, following Fenics weirdness 61 /// Find the cell containing `x` (in 1D, 2D, or 3D, following Fenics weirdness
48 /// of hard-coding vectors to 3D). 62 /// of hard-coding vectors to 3D).
49 pub unsafe fn cell_Function_f64(f: *const Function_f64, x: &[f64; 3]) -> i32; 63 pub unsafe fn cell_Function_f64(f: *const Function_f64, x: &[f64; 3]) -> i32;
64 f: *const Function_f64, 78 f: *const Function_f64,
65 all_coords: &[f64; 18], // 6*3 79 all_coords: &[f64; 18], // 6*3
66 cell: i32, 80 cell: i32,
67 ) -> [f64; 6]; 81 ) -> [f64; 6];
68 82
83 /// Get information about a dolfinx `Function_f64`.
69 pub unsafe fn info_Function_f64(f: *const Function_f64) -> FunctionInfo; 84 pub unsafe fn info_Function_f64(f: *const Function_f64) -> FunctionInfo;
70 85
86 /// Returns the minimum value of the Dolfinx `Function`,
87 /// if supported (P2 elements on triangular mesh)
71 pub unsafe fn min_Function_f64_p2(f: *const Function_f64) -> Result<CoordValuePair>; 88 pub unsafe fn min_Function_f64_p2(f: *const Function_f64) -> Result<CoordValuePair>;
89
90 /// Returns the maximum value of the Dolfinx `Function`,
91 /// if supported (P2 elements on triangular mesh)
72 pub unsafe fn max_Function_f64_p2(f: *const Function_f64) -> Result<CoordValuePair>; 92 pub unsafe fn max_Function_f64_p2(f: *const Function_f64) -> Result<CoordValuePair>;
93
94 /// Returns the minimum or maximum value of the Dolfinx `Function`,
95 /// if supported (P2 elements on triangular mesh)
73 pub unsafe fn minmax_Function_f64_p2( 96 pub unsafe fn minmax_Function_f64_p2(
74 f: *const Function_f64, 97 f: *const Function_f64,
75 max: bool, 98 max: bool,
76 ) -> Result<CoordValuePair>; 99 ) -> Result<CoordValuePair>;
77 100
101 /// Checks if a Dolfinx `Function` is supported (P2 elements on triangular mesh)
78 pub unsafe fn check_Function_f64(o: *const PyObject) -> bool; 102 pub unsafe fn check_Function_f64(o: *const PyObject) -> bool;
103
104 /// Casts a Python object into a Dolfinx `Function` wrapper.
79 pub unsafe fn cast_Function_f64(o: *const PyObject) -> Result<*const Function_f64>; 105 pub unsafe fn cast_Function_f64(o: *const PyObject) -> Result<*const Function_f64>;
106
107 /// Casts a Python object into a mutable Dolfinx `Function` wrapper.
80 pub unsafe fn cast_mut_Function_f64(o: *mut PyObject) -> Result<*mut Function_f64>; 108 pub unsafe fn cast_mut_Function_f64(o: *mut PyObject) -> Result<*mut Function_f64>;
109
110 /// Gets the 3D coordinates of the nodes of a triangular cell
81 pub unsafe fn cell_coords_Function_f64_triangle( 111 pub unsafe fn cell_coords_Function_f64_triangle(
82 f: *const Function_f64, 112 f: *const Function_f64,
83 cell: i32, 113 cell: i32,
84 ) -> [f64; 9]; 114 ) -> [f64; 9];
85 115
93 pub unsafe fn check_compat_Function_f64( 123 pub unsafe fn check_compat_Function_f64(
94 f: *const Function_f64, 124 f: *const Function_f64,
95 g: *const Function_f64, 125 g: *const Function_f64,
96 ) -> Result<()>; 126 ) -> Result<()>;
97 127
98 /// Create a new similar function 128 /// Create a new similar Dolfinx `Function`
99 pub unsafe fn similar_Function_f64(f: *const Function_f64) -> *mut Function_f64; 129 pub unsafe fn similar_Function_f64(f: *const Function_f64) -> *mut Function_f64;
100 130
101 /// Clone the function 131 /// Clone the Dolfinx `Function`
102 pub unsafe fn clone_Function_f64(f: *const Function_f64) -> *mut Function_f64; 132 pub unsafe fn clone_Function_f64(f: *const Function_f64) -> *mut Function_f64;
103 133
134 /// Wraps a Dolfinx `Function` into a Python object
104 pub unsafe fn wrap_Function_f64(f: *mut Function_f64) -> *mut PyObject; 135 pub unsafe fn wrap_Function_f64(f: *mut Function_f64) -> *mut PyObject;
105 136
137 /// Drop helper
106 pub unsafe fn drop_Function_f64(f: *mut Function_f64); 138 pub unsafe fn drop_Function_f64(f: *mut Function_f64);
139
140 /// Scatter helper. Not truly supported.
141 pub unsafe fn scatter_fwd_Function_f64(f: *mut Function_f64);
107 } 142 }
108 143
109 extern "Rust" { 144 extern "Rust" {
145 /// Rust-side helper for minimising P2 elements. This bypasses poorly documented basix,
146 /// and does things at a low level directly.
147 /// `f` is the function, `simplex_coords` the coorindates of the triangle
148 /// see [`cell_coords_Function_f64_triangle`], `cell` is the cell number,
149 /// and `max` indicates whether to maximise or minimise.
110 unsafe fn minmax_dolfinx_p2_cell( 150 unsafe fn minmax_dolfinx_p2_cell(
111 f: *const Function_f64, 151 f: *const Function_f64,
112 simplex_coords: &[f64; 9], 152 simplex_coords: &[f64; 9],
113 cell: i32, 153 cell: i32,
114 max: bool, 154 max: bool,

mercurial