1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Python wrapper for [`crate::atoms::Node`].
use crate::atoms::Node;
use crate::atoms::NodeId;
use crate::coords::Coords;
use crate::handle::Handle;
use crate::py::coords::PyCoords;
use crate::py::mesh::PyMesh;
use pyo3::prelude::*;
/// A node of a `Coords`: a stable identifier that carries the
/// `Coords` it belongs to (and therefore its coordinates).
///
/// Created via `Coords.add_node([x, y, ...])`; passed wherever an
/// API needs a node.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "Node")]
pub struct PyNode {
node: Node,
}
impl PyNode {
/// Build a `PyNode` from a handle and an id that have **already
/// been incremented** on the Coords side. For internal use
/// by `PyCoords::add_node` / `acquire`.
pub(crate) fn from_raw(handle: Handle<Coords>, id: NodeId) -> Self {
Self {
node: Node::from_parts(handle, id),
}
}
pub(crate) fn from_node(node: Node) -> Self {
Self { node }
}
pub(crate) fn as_node(&self) -> &Node {
&self.node
}
/// This node's id, to index a container whose nodes live in `coords`:
/// refused if the node belongs to another `Coords` ([`Node::id_in`]).
/// `None` is an empty aggregate, which has no `Coords` and no node to
/// mistake this one for: the id passes, and the container answers as it
/// would for any absent node.
pub(crate) fn id_in(&self, coords: Option<&Handle<Coords>>) -> PyResult<NodeId> {
match coords {
Some(c) => Ok(self.node.id_in(c)?),
None => Ok(self.node.id()),
}
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyNode {
/// Stable integer id of this node within its `Coords`.
#[getter]
fn id(&self) -> u32 {
self.node.id().0
}
/// This node's coordinates in the active coordinate set.
fn position(&self) -> PyResult<Vec<f64>> {
Ok(self.node.position()?)
}
/// Overwrite this node's coordinates.
fn set_position(&self, coords: Vec<f64>) -> PyResult<()> {
self.node.set_position(&coords)?;
Ok(())
}
/// The `Coords` this node belongs to — the same safety net as
/// `Mesh.coords()`, to get the handle back when it has been dropped
/// on the Python side.
fn coords(&self) -> PyCoords {
PyCoords {
handle: self.node.coords(),
}
}
}
// Polymorphic union — **closed block**, undecorated on purpose (see
// `impl_aggregate_pymethods!`): its `.pyi` entries are the hand-written
// signatures submitted just below.
#[pymethods]
impl PyNode {
/// `node | node` → a unitary POI1 `Mesh` over both nodes (the same
/// union `|` as the aggregates). Returns `NotImplemented` for any other
/// right-hand type.
fn __or__(&self, py: Python<'_>, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok(o) = other.extract::<PyRef<'_, PyNode>>() {
let mesh = self.as_node().union(o.as_node())?;
return Ok(Py::new(py, PyMesh { inner: mesh })?.into_any());
}
Ok(py.NotImplemented())
}
/// Right-hand `mesh | node`: append this node to a **unitary POI1**
/// `Mesh`, yielding a new one. Reached when the left operand's `__or__`
/// returns `NotImplemented` (a `Mesh` doesn't know how to union a `Node`).
fn __ror__(&self, py: Python<'_>, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok(m) = other.extract::<PyRef<'_, PyMesh>>() {
let mesh = m.inner.union_node(self.as_node())?;
return Ok(Py::new(py, PyMesh { inner: mesh })?.into_any());
}
Ok(py.NotImplemented())
}
}
#[cfg(feature = "stub-gen")]
pyo3_stub_gen::inventory::submit! {
pyo3_stub_gen::derive::gen_methods_from_python! { r#"
class PyNode:
def __or__(self, other: pyo3_stub_gen.RustType["PyNode"]) -> pyo3_stub_gen.RustType["PyMesh"]:
"""`node | node` → a unitary POI1 `Mesh` over both nodes — the usual way
to build the support of a point load or a boundary condition."""
def __ror__(self, other: pyo3_stub_gen.RustType["PyMesh"]) -> pyo3_stub_gen.RustType["PyMesh"]:
"""`mesh | node` → a fresh POI1 `Mesh` with this node appended. The
left-hand `Mesh` must be unitary POI1."""
"# }
}
crate::impl_dump_pymethod!(value PyNode, node);