macro_rules! physics_operator {
(
$(#[$rust_doc:meta])*
pub fn $name:ident(fespace $(, $arg:ident : $ty:ty)* $(,)?) = $target:path, $fixed:expr;
python: $py_doc:literal
) => { ... };
(
$(#[$rust_doc:meta])*
pub fn $name:ident(fespace, target $(, $arg:ident : $ty:ty)* $(,)?) via $sub:path;
python: $py_doc:literal
) => { ... };
(
$(#[$rust_doc:meta])*
pub fn $name:ident(fespace $(, $arg:ident : $ty:ty $(= $default:expr)?)* $(,)?) via $sub:path;
python: $py_doc:literal
) => { ... };
}Expand description
Declare a physics’ parent-level operator and its Python twin, from a single place in the physics’ own file.
The author of a physics writes physics and documentation; the plumbing —
the sweep, the #[pyfunction], the stub attribute, the unwrapping of the
Python wrappers — is emitted here. They never open src/py/.
Two doc blocks, because the two audiences differ: the Rust one carries the
/// comment and its doctest, the Python one a string literal, which is
what lands in the .pyi. Sharing a single block would put a Rust doctest
into a Python docstring. Same shape as define_polymorphic_pyfunction! in
src/py/ops/field.rs, the house precedent.
What it emits for truss — declared truss(fespace, strain_measure: StrainMeasure = StrainMeasure::Linear) via SubModel::truss — is an ordinary
function: it sweeps the FE space, one sub-model per subspace, and Rust passes
the argument the Python face defaults. That default is written into the Rust
documentation, so that a Rust reader knows what the Python one gets by
saying nothing.
let fes = FiniteElementSpace::lagrange1(&bars)?;
assert_eq!(fes.len(), 2);
let cables = model::truss(&fes, StrainMeasure::GreenLagrange)?;
assert_eq!(cables.len(), fes.len());