use crate::atoms::Band;
use crate::containers::element_field::{ElementField, SubElementField};
use crate::containers::field::SubField;
use crate::handle::Handle;
use crate::py::finite_element_space::PyFiniteElementSpace;
use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::pyclass::CompareOp;
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "SubElementField")]
pub struct PySubElementField {
pub(crate) handle: Handle<SubElementField>,
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PySubElementField {
fn cell_count(&self) -> PyResult<usize> {
Ok(self.handle.read().cell_count())
}
fn gauss_count(&self) -> PyResult<usize> {
Ok(self.handle.read().gauss_count())
}
fn component_count(&self) -> PyResult<usize> {
Ok(self.handle.read().component_count())
}
fn components(&self) -> PyResult<Vec<String>> {
Ok(self.handle.read().components().to_vec())
}
fn component_index(&self, name: &str) -> PyResult<Option<usize>> {
Ok(self.handle.read().component_index(name))
}
fn get(&self, cell: usize, gauss: usize, comp: usize) -> PyResult<f64> {
Ok(self.handle.read().get(cell, gauss, comp)?)
}
fn set(&self, cell: usize, gauss: usize, comp: usize, value: f64) -> PyResult<()> {
self.handle.write().set(cell, gauss, comp, value)?;
Ok(())
}
fn value(&self, cell: usize, gauss: usize, component: &str) -> PyResult<f64> {
Ok(self.handle.read().value(cell, gauss, component)?)
}
fn set_value(&self, cell: usize, gauss: usize, component: &str, value: f64) -> PyResult<()> {
self.handle
.write()
.set_value(cell, gauss, component, value)?;
Ok(())
}
fn point_values(&self, cell: usize, gauss: usize) -> PyResult<Vec<f64>> {
Ok(self.handle.read().point_values(cell, gauss)?.to_vec())
}
fn set_uniform(&self, component: &str, value: f64) -> PyResult<()> {
self.handle.write().set_uniform(component, value)?;
Ok(())
}
fn set_cell_uniform(&self, cell: usize, component: &str, value: f64) -> PyResult<()> {
self.handle
.write()
.set_cell_uniform(cell, component, value)?;
Ok(())
}
fn min(&self, component: &str) -> PyResult<f64> {
use crate::containers::field::SubField;
Ok(SubField::min(&*self.handle.read(), component)?)
}
fn max(&self, component: &str) -> PyResult<f64> {
use crate::containers::field::SubField;
Ok(SubField::max(&*self.handle.read(), component)?)
}
fn sum(&self, component: &str) -> PyResult<f64> {
use crate::containers::field::SubField;
Ok(SubField::sum(&*self.handle.read(), component)?)
}
fn add_to_component(&self, component: &str, scalar: f64) -> PyResult<()> {
self.handle.write().add_to_component(component, scalar)?;
Ok(())
}
fn sub_to_component(&self, component: &str, scalar: f64) -> PyResult<()> {
self.handle.write().sub_to_component(component, scalar)?;
Ok(())
}
fn mul_to_component(&self, component: &str, scalar: f64) -> PyResult<()> {
self.handle.write().mul_to_component(component, scalar)?;
Ok(())
}
fn div_to_component(&self, component: &str, scalar: f64) -> PyResult<()> {
self.handle.write().div_to_component(component, scalar)?;
Ok(())
}
fn __add__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PySubElementField> {
self.scalar_or_combine(rhs, |a, b| a + b)
}
fn __sub__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PySubElementField> {
self.scalar_or_combine(rhs, |a, b| a - b)
}
fn __mul__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PySubElementField> {
self.scalar_or_combine(rhs, |a, b| a * b)
}
fn __truediv__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PySubElementField> {
self.scalar_or_combine(rhs, |a, b| a / b)
}
fn __pow__(
&self,
exponent: &Bound<'_, PyAny>,
modulo: &Bound<'_, PyAny>,
) -> PyResult<PySubElementField> {
if !modulo.is_none() {
return Err(PyTypeError::new_err(
"field ** exponent does not support a modulo argument",
));
}
self.scalar_or_combine(exponent, |a, b| a.powf(b))
}
fn __setitem__(&self, key: (usize, usize, String), value: f64) -> PyResult<()> {
let (cell, gauss, comp) = key;
self.handle.write().set_value(cell, gauss, &comp, value)?;
Ok(())
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("{:?}", &*self.handle.read()))
}
fn __str__(&self) -> PyResult<String> {
Ok(format!("{}", &*self.handle.read()))
}
}
impl PySubElementField {
fn scalar_or_combine(
&self,
rhs: &Bound<'_, PyAny>,
op: fn(f64, f64) -> f64,
) -> PyResult<PySubElementField> {
if let Ok(s) = rhs.extract::<f64>() {
let out = self.handle.read().map_all(|v| op(v, s));
Ok(PySubElementField {
handle: Handle::new(out),
})
} else if let Ok(other) = rhs.extract::<PyRef<PySubElementField>>() {
let a = (*self.handle.read()).clone();
let b = (*other.handle.read()).clone();
Ok(PySubElementField {
handle: Handle::new(a.merge_components(&b, op)?),
})
} else {
Err(PyTypeError::new_err(
"unsupported operand: expected a float or a SubElementField",
))
}
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "ElementField")]
pub struct PyElementField {
pub(crate) inner: ElementField,
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyElementField {
#[new]
fn py_new(fespace: PyRef<PyFiniteElementSpace>, components: Vec<String>) -> PyResult<Self> {
let ef = ElementField::new(&fespace.inner, components)?;
Ok(Self { inner: ef })
}
#[classmethod]
fn with_components_per_subspace(
_cls: &pyo3::Bound<'_, pyo3::types::PyType>,
fespace: PyRef<PyFiniteElementSpace>,
components_per_subspace: Vec<Vec<String>>,
) -> PyResult<Self> {
let ef = ElementField::with(&fespace.inner, &components_per_subspace)?;
Ok(Self { inner: ef })
}
fn components(&self) -> PyResult<Vec<String>> {
use crate::containers::field::Field;
Ok(Field::components(&self.inner)?)
}
#[cfg(feature = "viz")]
#[pyo3(signature = (view=None, save=None, show_axes=true, component=None, vmin=None, vmax=None, cmap=None, smooth=4, revolve=false, revolve_angle=360.0, title=None))]
#[allow(clippy::too_many_arguments)]
fn plot(
&self,
view: Option<(f64, f64, f64)>,
save: Option<std::path::PathBuf>,
show_axes: bool,
component: Option<String>,
vmin: Option<f64>,
vmax: Option<f64>,
cmap: Option<String>,
smooth: usize,
revolve: bool,
revolve_angle: f64,
title: Option<String>,
) -> PyResult<()> {
let view = crate::py::build_view(view, show_axes, revolve, revolve_angle)?;
let scale = crate::viz::ColorScale {
cmap: crate::py::mesh::parse_cmap(cmap)?,
vmin,
vmax,
};
self.inner.plot(
Some(view),
save.as_deref(),
component.as_deref(),
scale,
smooth,
title.as_deref(),
)?;
Ok(())
}
fn min(&self, component: &str) -> PyResult<f64> {
use crate::containers::field::Field;
Ok(Field::min(&self.inner, component)?)
}
fn max(&self, component: &str) -> PyResult<f64> {
use crate::containers::field::Field;
Ok(Field::max(&self.inner, component)?)
}
fn sum(&self, component: &str) -> PyResult<f64> {
use crate::containers::field::Field;
Ok(Field::sum(&self.inner, component)?)
}
fn add_to_component(&self, component: &str, scalar: f64) -> PyResult<()> {
use crate::containers::field::Field;
self.inner.add_to_component(component, scalar)?;
Ok(())
}
fn sub_to_component(&self, component: &str, scalar: f64) -> PyResult<()> {
use crate::containers::field::Field;
self.inner.sub_to_component(component, scalar)?;
Ok(())
}
fn mul_to_component(&self, component: &str, scalar: f64) -> PyResult<()> {
use crate::containers::field::Field;
self.inner.mul_to_component(component, scalar)?;
Ok(())
}
fn div_to_component(&self, component: &str, scalar: f64) -> PyResult<()> {
use crate::containers::field::Field;
self.inner.div_to_component(component, scalar)?;
Ok(())
}
fn __add__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PyElementField> {
self.binary(rhs, |a, b| a + b)
}
fn __sub__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PyElementField> {
self.binary(rhs, |a, b| a - b)
}
fn __mul__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PyElementField> {
self.binary(rhs, |a, b| a * b)
}
fn __truediv__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PyElementField> {
self.binary(rhs, |a, b| a / b)
}
fn __pow__(
&self,
exponent: &Bound<'_, PyAny>,
modulo: &Bound<'_, PyAny>,
) -> PyResult<PyElementField> {
if !modulo.is_none() {
return Err(PyTypeError::new_err(
"field ** exponent does not support a modulo argument",
));
}
self.binary(exponent, |a, b| a.powf(b))
}
}
impl PyElementField {
fn binary(&self, rhs: &Bound<'_, PyAny>, op: fn(f64, f64) -> f64) -> PyResult<PyElementField> {
use crate::containers::field::Field;
if let Ok(s) = rhs.extract::<f64>() {
Ok(PyElementField {
inner: self.inner.combine_scalar(op, s)?,
})
} else if let Ok(other) = rhs.extract::<PyRef<PyElementField>>() {
Ok(PyElementField {
inner: self.inner.merge_field(&other.inner, op)?,
})
} else if let Ok(sub) = rhs.extract::<PyRef<PySubElementField>>() {
let s = (*sub.handle.read()).clone();
Ok(PyElementField {
inner: self.inner.merge_subfield(&s, op)?,
})
} else {
Err(PyTypeError::new_err(
"unsupported operand: expected a float, an ElementField, or a SubElementField",
))
}
}
}
#[pymethods]
impl PySubElementField {
fn __getitem__(&self, py: Python<'_>, key: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok((cell, gauss, comp)) = key.extract::<(usize, usize, String)>() {
let v = self
.handle
.read()
.value(cell, gauss, &comp)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
return Ok(v.into_pyobject(py)?.into_any().unbind());
}
let names = crate::py::ops::field::extract_names(key)?;
let out =
crate::containers::field::SubField::select_components(&*self.handle.read(), names)?;
Ok(Py::new(
py,
PySubElementField {
handle: Handle::new(out),
},
)?
.into_any())
}
fn __richcmp__(
&self,
py: Python<'_>,
other: &Bound<'_, PyAny>,
op: CompareOp,
) -> PyResult<Py<PyAny>> {
let Ok(x) = other.extract::<f64>() else {
return Ok(py.NotImplemented());
};
let band = match op {
CompareOp::Ge => Band::new(Some(x), None, None, None),
CompareOp::Gt => Band::new(None, Some(x), None, None),
CompareOp::Le => Band::new(None, None, Some(x), None),
CompareOp::Lt => Band::new(None, None, None, Some(x)),
CompareOp::Eq | CompareOp::Ne => return Ok(py.NotImplemented()),
}?;
let out = crate::ops::element_field::mask_sub(&self.handle.read(), &band, None);
Ok(Py::new(
py,
PySubElementField {
handle: Handle::new(out),
},
)?
.into_any())
}
}
#[pymethods]
impl PyElementField {
fn __richcmp__(
&self,
py: Python<'_>,
other: &Bound<'_, PyAny>,
op: CompareOp,
) -> PyResult<Py<PyAny>> {
let Ok(x) = other.extract::<f64>() else {
return Ok(py.NotImplemented());
};
let band = match op {
CompareOp::Ge => Band::new(Some(x), None, None, None),
CompareOp::Gt => Band::new(None, Some(x), None, None),
CompareOp::Le => Band::new(None, None, Some(x), None),
CompareOp::Lt => Band::new(None, None, None, Some(x)),
CompareOp::Eq | CompareOp::Ne => return Ok(py.NotImplemented()),
}?;
let out = crate::ops::element_field::mask(&self.inner, &band, None)?;
Ok(Py::new(py, PyElementField { inner: out })?.into_any())
}
}
#[cfg(feature = "stub-gen")]
pyo3_stub_gen::inventory::submit! {
pyo3_stub_gen::derive::gen_methods_from_python! { r#"
class PyElementField:
def __ge__(self, other: float) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`field >= x` → a fresh `ElementField` of per-component 0/1 flags, one
value per Gauss point (see `mask`), not a boolean."""
def __gt__(self, other: float) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`field > x` → a 0/1 mask field (see `__ge__`)."""
def __le__(self, other: float) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`field <= x` → a 0/1 mask field (see `__ge__`)."""
def __lt__(self, other: float) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`field < x` → a 0/1 mask field (see `__ge__`)."""
"# }
}
#[cfg(feature = "stub-gen")]
pyo3_stub_gen::inventory::submit! {
pyo3_stub_gen::derive::gen_methods_from_python! { r#"
class PySubElementField:
@overload
def __getitem__(self, key: tuple[int, int, str]) -> float:
"""`field[cell, gauss, "name"]` → the value at that Gauss point of that
cell, for that component. `ValueError` if any of the three is unknown."""
@overload
def __getitem__(self, key: str) -> pyo3_stub_gen.RustType["PySubElementField"]:
"""`field["sig_xx"]` → a fresh `SubElementField` on the same support,
keeping only that component."""
@overload
def __getitem__(self, key: list[str]) -> pyo3_stub_gen.RustType["PySubElementField"]:
"""`field[["sig_xx", "sig_yy"]]` → a fresh `SubElementField` keeping only
those components, so `u1[u2.components()]` reprojects `u1` onto `u2`'s
component set."""
def __ge__(self, other: float) -> pyo3_stub_gen.RustType["PySubElementField"]:
"""`subfield >= x` → a fresh `SubElementField` of per-component 0/1
flags, one value per Gauss point (see `mask`), not a boolean."""
def __gt__(self, other: float) -> pyo3_stub_gen.RustType["PySubElementField"]:
"""`subfield > x` → a 0/1 mask field (see `__ge__`)."""
def __le__(self, other: float) -> pyo3_stub_gen.RustType["PySubElementField"]:
"""`subfield <= x` → a 0/1 mask field (see `__ge__`)."""
def __lt__(self, other: float) -> pyo3_stub_gen.RustType["PySubElementField"]:
"""`subfield < x` → a 0/1 mask field (see `__ge__`)."""
"# }
}
crate::impl_aggregate_pymethods!(
PyElementField,
PySubElementField,
"ElementField",
subfield,
ElementField,
r#"
class PyElementField:
@overload
def __getitem__(self, key: int) -> pyo3_stub_gen.RustType["PySubElementField"]:
"""`field[i]` → the `SubElementField` of zone i: the values carried by
the Gauss points of one support."""
@overload
def __getitem__(self, key: slice) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`field[i:j:k]` → a fresh `ElementField` over the sliced zones, shared
with this one (no deep copy)."""
@overload
def __getitem__(self, key: str) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`field["sig_xx"]` → a fresh `ElementField` keeping only that
component, on every zone that carries it."""
@overload
def __getitem__(self, key: list[str]) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`field[["sig_xx", "sig_yy"]]` → a fresh `ElementField` keeping only
those components, on every zone that carries them."""
def __or__(self, other: pyo3_stub_gen.RustType["PyElementField"] | pyo3_stub_gen.RustType["PySubElementField"]) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`field | other` → a fresh `ElementField` holding the zones of both.
Component-disjoint zones on one support stay **side by side** (unlike
`NodeField`, which fuses them); a component carried by two zones on the
same support is rejected — fuse those explicitly with `pyrucast.element_field.consolidate`."""
def __ror__(self, other: pyo3_stub_gen.RustType["PySubElementField"]) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`subfield | field` — the mirror of `field | subfield`, differing only
in that the lone zone comes first."""
"#,
r#"
class PySubElementField:
def __or__(self, other: pyo3_stub_gen.RustType["PySubElementField"]) -> pyo3_stub_gen.RustType["PyElementField"]:
"""`subfield | subfield` → a fresh `ElementField` holding both zones,
left side by side (never fused)."""
"#,
field_components
);
crate::impl_dump_pymethod!(handle PySubElementField, handle);