| |
1 /*! |
| |
2 Wrappers to Python objects. |
| |
3 */ |
| |
4 |
| |
5 use pyo3::types::{PyModule, PyModuleMethods, PyTracebackMethods}; |
| |
6 use pyo3::{pymodule, Bound, PyResult, Python}; |
| |
7 |
| |
8 use alg_tools::error::DynResult; |
| |
9 use anyhow::anyhow; |
| |
10 |
| |
11 mod diff_mapping; |
| |
12 mod function; |
| |
13 mod numpy_array; |
| |
14 mod plot; |
| |
15 mod prox_mapping; |
| |
16 |
| |
17 //mod measures; |
| |
18 |
| |
19 #[allow(unused_imports)] |
| |
20 pub use diff_mapping::{ |
| |
21 Basic, Differentiable, DolfinxPyFunctionMarker, NumpyArrayMarker, PythonMapping, |
| |
22 }; |
| |
23 //pub use pointsource_algs::measures::python::{DiscreteMeasure_1_f64, DiscreteMeasure_2_f64}; |
| |
24 // |
| |
25 |
| |
26 #[allow(unused_imports)] |
| |
27 pub use prox_mapping::HasProx; |
| |
28 |
| |
29 #[allow(unused_imports)] |
| |
30 pub use numpy_array::{NumpyArray_f64, NumpyMatrix_f64, NumpyVector_f64}; |
| |
31 |
| |
32 #[allow(unused_imports)] |
| |
33 pub use plot::PythonPlotFactory; |
| |
34 |
| |
35 /// Populates the Python module. |
| |
36 /// |
| |
37 /// Needs to be called with [`pyo3::append_to_inittab`]: |
| |
38 /// ``` |
| |
39 /// append_to_inittab!(pymod_pointsource_pde); |
| |
40 /// ``` |
| |
41 /// before initialising the intepreter. |
| |
42 #[pymodule] |
| |
43 #[pyo3(name = "pointsource_pde")] |
| |
44 pub(crate) fn pymod_pointsource_pde(m: &Bound<'_, PyModule>) -> PyResult<()> { |
| |
45 m.add_class::<super::experiments::Experiments>()?; |
| |
46 m.add_class::<super::experiments::Problem>()?; |
| |
47 m.add_class::<super::experiments::RegTermPy>()?; |
| |
48 Ok(()) |
| |
49 } |
| |
50 |
| |
51 /// Converts a [`PyErr`] error in a [`Results`] error into a string with traceback. |
| |
52 pub fn process_error<'py, T>(fname: &str, py: Python<'py>, res: PyResult<T>) -> DynResult<T> |
| |
53 where |
| |
54 { |
| |
55 match res { |
| |
56 Ok(res) => Ok(res), |
| |
57 Err(e) => Err(match e.traceback(py) { |
| |
58 None => anyhow!("Failed Python traceback in {fname}; original error {e}"), |
| |
59 Some(r) => match r.format() { |
| |
60 Err(e) => anyhow!("Failed Python traceback formatting in {fname}: {e}"), |
| |
61 Ok(o) => anyhow!("Python-side error in {fname}: {e}.\n{o}"), |
| |
62 }, |
| |
63 }), |
| |
64 } |
| |
65 } |