use crate::aggregate::Aggregate;
use crate::atoms::Band;
use crate::atoms::NodeId;
use crate::containers::field::SubField;
use crate::containers::node_field::{NodeField, SubNodeField};
use crate::handle::Handle;
use crate::py::mesh::{PyMesh, PySubMesh};
use crate::py::node::PyNode;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::pyclass::CompareOp;
fn extract_node_ids(obj: &Bound<'_, PyAny>) -> PyResult<Vec<NodeId>> {
if let Ok(sm) = obj.extract::<PyRef<PySubMesh>>() {
return Ok(sm.handle.read().connectivity().to_vec());
}
if let Ok(mesh) = obj.extract::<PyRef<PyMesh>>() {
let mut ids: Vec<NodeId> = Vec::new();
for h in &mesh.inner {
ids.extend_from_slice(h.read().connectivity());
}
return Ok(ids);
}
if let Ok(nodes) = obj.extract::<Vec<PyRef<'_, PyNode>>>() {
return Ok(nodes.iter().map(|n| n.as_node().id()).collect());
}
Err(PyTypeError::new_err(
"expected a list of Node, a SubMesh, or a Mesh",
))
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "SubNodeField")]
pub struct PySubNodeField {
pub(crate) handle: Handle<SubNodeField>,
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PySubNodeField {
fn node_count(&self) -> PyResult<usize> {
Ok(self.handle.read().node_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 get(&self, node_idx: usize, comp_idx: usize) -> PyResult<f64> {
Ok(self.handle.read().get(node_idx, comp_idx)?)
}
fn set(&self, node_idx: usize, comp_idx: usize, value: f64) -> PyResult<()> {
self.handle.write().set(node_idx, comp_idx, value)?;
Ok(())
}
fn get_by_node(&self, node: PyRef<'_, PyNode>, comp_idx: usize) -> PyResult<f64> {
let nid = node.as_node().id();
Ok(self.handle.read().get_by_node(nid, comp_idx)?)
}
fn set_by_node(&self, node: PyRef<'_, PyNode>, comp_idx: usize, value: f64) -> PyResult<()> {
let nid = node.as_node().id();
self.handle.write().set_by_node(nid, comp_idx, value)?;
Ok(())
}
fn component_index(&self, name: &str) -> PyResult<Option<usize>> {
Ok(self.handle.read().component_index(name))
}
fn node_values(&self, node_idx: usize) -> PyResult<Vec<f64>> {
Ok(self.handle.read().node_values(node_idx)?.to_vec())
}
fn support_submesh(&self) -> PyResult<PySubMesh> {
let sm = self.handle.read().support_submesh()?;
Ok(PySubMesh {
handle: Handle::new(sm),
})
}
fn support_mesh(&self) -> PyResult<PyMesh> {
let mesh = self.handle.read().support_mesh()?;
Ok(PyMesh { inner: mesh })
}
fn value(&self, node: PyRef<'_, PyNode>, component: &str) -> PyResult<f64> {
let nid = node.as_node().id();
Ok(self.handle.read().value(nid, component)?)
}
fn set_value(&self, node: PyRef<'_, PyNode>, component: &str, value: f64) -> PyResult<()> {
let nid = node.as_node().id();
self.handle.write().set_value(nid, 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<PySubNodeField> {
self.scalar_or_combine(rhs, |a, b| a + b)
}
fn __sub__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PySubNodeField> {
self.scalar_or_combine(rhs, |a, b| a - b)
}
fn __mul__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PySubNodeField> {
self.scalar_or_combine(rhs, |a, b| a * b)
}
fn __truediv__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PySubNodeField> {
self.scalar_or_combine(rhs, |a, b| a / b)
}
fn __pow__(
&self,
exponent: &Bound<'_, PyAny>,
modulo: &Bound<'_, PyAny>,
) -> PyResult<PySubNodeField> {
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: (PyRef<'_, PyNode>, String), value: f64) -> PyResult<()> {
let (node, comp) = key;
let nid = node.as_node().id();
self.handle.write().set_value(nid, &comp, value)?;
Ok(())
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("{:?}", &*self.handle.read()))
}
fn __str__(&self) -> PyResult<String> {
Ok(format!("{}", &*self.handle.read()))
}
}
impl PySubNodeField {
fn scalar_or_combine(
&self,
rhs: &Bound<'_, PyAny>,
op: fn(f64, f64) -> f64,
) -> PyResult<PySubNodeField> {
if let Ok(s) = rhs.extract::<f64>() {
let out = self.handle.read().map_all(|v| op(v, s));
Ok(PySubNodeField {
handle: Handle::new(out),
})
} else if let Ok(other) = rhs.extract::<PyRef<PySubNodeField>>() {
let a = (*self.handle.read()).clone();
let b = (*other.handle.read()).clone();
Ok(PySubNodeField {
handle: Handle::new(a.merge_components(&b, op)?),
})
} else {
Err(PyTypeError::new_err(
"unsupported operand: expected a float or a SubNodeField",
))
}
}
}
crate::impl_dump_pymethod!(handle PySubNodeField, handle);
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "NodeField")]
pub struct PyNodeField {
pub(crate) inner: NodeField,
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyNodeField {
#[new]
fn py_new(support: &Bound<'_, PyAny>, components: Vec<String>) -> PyResult<Self> {
let inner = if let Ok(mesh) = support.extract::<PyRef<PyMesh>>() {
NodeField::new(&mesh.inner, components)?
} else if let Ok(sm) = support.extract::<PyRef<PySubMesh>>() {
NodeField::from_submesh(&sm.handle, components)?
} else {
return Err(PyTypeError::new_err("expected a Mesh or a SubMesh"));
};
Ok(Self { inner })
}
#[classmethod]
fn with_components_per_submesh(
_cls: &pyo3::Bound<'_, pyo3::types::PyType>,
mesh: PyRef<PyMesh>,
components_per_submesh: Vec<Vec<String>>,
) -> PyResult<Self> {
let inner = NodeField::with(&mesh.inner, &components_per_submesh)?;
Ok(Self { inner })
}
fn node_count(&self) -> PyResult<usize> {
Ok(self.inner.node_count()?)
}
fn components(&self) -> PyResult<Vec<String>> {
use crate::containers::field::Field;
Ok(Field::components(&self.inner)?)
}
fn value(&self, node: PyRef<'_, PyNode>, component: &str) -> PyResult<f64> {
let nid = node.as_node().id();
Ok(self.inner.value(nid, component)?)
}
fn values(&self, nodes: &Bound<'_, PyAny>, component: &str) -> PyResult<Vec<f64>> {
let ids = extract_node_ids(nodes)?;
Ok(self.inner.values_at(&ids, component)?)
}
fn check(&self) -> PyResult<()> {
Ok(self.inner.check()?)
}
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)?)
}
#[cfg(feature = "viz")]
#[pyo3(signature = (view=None, save=None, show_axes=true, component=None, vmin=None, vmax=None, cmap=None, 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>,
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,
title.as_deref(),
)?;
Ok(())
}
fn support_mesh(&self) -> PyResult<PyMesh> {
let mut mesh = crate::containers::mesh::Mesh::empty();
for h in &self.inner {
let sm = h.read().support();
mesh.add_sub(sm)?;
}
Ok(PyMesh { inner: mesh })
}
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<PyNodeField> {
self.binary(rhs, |a, b| a + b)
}
fn __sub__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PyNodeField> {
self.binary(rhs, |a, b| a - b)
}
fn __mul__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PyNodeField> {
self.binary(rhs, |a, b| a * b)
}
fn __truediv__(&self, rhs: &Bound<'_, PyAny>) -> PyResult<PyNodeField> {
self.binary(rhs, |a, b| a / b)
}
fn __pow__(
&self,
exponent: &Bound<'_, PyAny>,
modulo: &Bound<'_, PyAny>,
) -> PyResult<PyNodeField> {
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 PyNodeField {
fn binary(&self, rhs: &Bound<'_, PyAny>, op: fn(f64, f64) -> f64) -> PyResult<PyNodeField> {
use crate::containers::field::Field;
if let Ok(s) = rhs.extract::<f64>() {
Ok(PyNodeField {
inner: self.inner.combine_scalar(op, s)?,
})
} else if let Ok(other) = rhs.extract::<PyRef<PyNodeField>>() {
Ok(PyNodeField {
inner: self.inner.merge_field(&other.inner, op)?,
})
} else if let Ok(sub) = rhs.extract::<PyRef<PySubNodeField>>() {
let s = (*sub.handle.read()).clone();
Ok(PyNodeField {
inner: self.inner.merge_subfield(&s, op)?,
})
} else {
Err(PyTypeError::new_err(
"unsupported operand: expected a float, a NodeField, or a SubNodeField",
))
}
}
}
#[pymethods]
impl PySubNodeField {
fn __getitem__(&self, py: Python<'_>, key: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok((node, comp)) = key.extract::<(PyRef<'_, PyNode>, String)>() {
let nid = node.as_node().id();
let v = self.handle.read().value(nid, &comp)?;
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,
PySubNodeField {
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::node_field::mask_sub(&self.handle.read(), &band, None);
Ok(Py::new(
py,
PySubNodeField {
handle: Handle::new(out),
},
)?
.into_any())
}
}
#[pymethods]
impl PyNodeField {
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::node_field::mask(&self.inner, &band, None)?;
Ok(Py::new(py, PyNodeField { inner: out })?.into_any())
}
}
#[cfg(feature = "stub-gen")]
pyo3_stub_gen::inventory::submit! {
pyo3_stub_gen::derive::gen_methods_from_python! { r#"
class PyNodeField:
def __ge__(self, other: float) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`field >= x` → a fresh `NodeField` of per-component 0/1 flags (see
`mask`), not a boolean. Combine masks with `*` and use them to weight or
select values."""
def __gt__(self, other: float) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`field > x` → a 0/1 mask field (see `__ge__`)."""
def __le__(self, other: float) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`field <= x` → a 0/1 mask field (see `__ge__`)."""
def __lt__(self, other: float) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`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 PySubNodeField:
@overload
def __getitem__(self, key: tuple[pyo3_stub_gen.RustType["PyNode"], str]) -> float:
"""`subfield[node, "UX"]` → the value carried by that node for that
component. Raises if either is absent from this zone."""
@overload
def __getitem__(self, key: str) -> pyo3_stub_gen.RustType["PySubNodeField"]:
"""`subfield["UX"]` → a fresh `SubNodeField` on the same support,
keeping only that component."""
@overload
def __getitem__(self, key: list[str]) -> pyo3_stub_gen.RustType["PySubNodeField"]:
"""`subfield[["UX", "UY"]]` → a fresh `SubNodeField` keeping only those
components, so `u1[u2.components()]` reprojects `u1` onto `u2`'s
component set."""
def __ge__(self, other: float) -> pyo3_stub_gen.RustType["PySubNodeField"]:
"""`subfield >= x` → a fresh `SubNodeField` of per-component 0/1 flags
(see `mask`), not a boolean."""
def __gt__(self, other: float) -> pyo3_stub_gen.RustType["PySubNodeField"]:
"""`subfield > x` → a 0/1 mask field (see `__ge__`)."""
def __le__(self, other: float) -> pyo3_stub_gen.RustType["PySubNodeField"]:
"""`subfield <= x` → a 0/1 mask field (see `__ge__`)."""
def __lt__(self, other: float) -> pyo3_stub_gen.RustType["PySubNodeField"]:
"""`subfield < x` → a 0/1 mask field (see `__ge__`)."""
"# }
}
crate::impl_aggregate_pymethods!(
PyNodeField,
PySubNodeField,
"NodeField",
subfield,
NodeField,
r#"
class PyNodeField:
@overload
def __getitem__(self, key: int) -> pyo3_stub_gen.RustType["PySubNodeField"]:
"""`field[i]` → the `SubNodeField` of zone i: the values carried by the
nodes of one support."""
@overload
def __getitem__(self, key: slice) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`field[i:j:k]` → a fresh `NodeField` over the sliced zones, shared
with this one (no deep copy)."""
@overload
def __getitem__(self, key: str) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`field["u_x"]` → a fresh `NodeField` keeping only that component, on
every zone that carries it."""
@overload
def __getitem__(self, key: list[str]) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`field[["u_x", "u_y"]]` → a fresh `NodeField` keeping only those
components, on every zone that carries them."""
def __or__(self, other: pyo3_stub_gen.RustType["PyNodeField"] | pyo3_stub_gen.RustType["PySubNodeField"]) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`field | other` → a fresh `NodeField` holding the zones of both.
Zones sharing the same support are **fused** (union of their
components) — unlike `ElementField`, which juxtaposes them."""
def __ror__(self, other: pyo3_stub_gen.RustType["PySubNodeField"]) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`subfield | field` — the mirror of `field | subfield`, differing only
in that the lone zone comes first."""
"#,
r#"
class PySubNodeField:
def __or__(self, other: pyo3_stub_gen.RustType["PySubNodeField"]) -> pyo3_stub_gen.RustType["PyNodeField"]:
"""`subfield | subfield` → a fresh `NodeField` holding both zones, fused
if they share the same support."""
"#,
field_components
);