use crate::handle::Handle;
use crate::ops::element_field::{StrainMeasure, StressMeasure};
use crate::py::element_field::{PyElementField, PySubElementField};
use crate::py::finite_element_space::PyFiniteElementSpace;
use crate::py::model::{PyModel, PySubModel};
use crate::py::node_field::PyNodeField;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyrucast_macros::py_op;
#[py_op(method_on = PyElementField)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(name = "consolidate_element")]
pub fn consolidate(field: PyRef<PyElementField>) -> PyResult<PyElementField> {
Ok(PyElementField {
inner: crate::ops::element_field::consolidate(&field.inner)?,
})
}
#[py_op(method_on = PyNodeField)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn gradient(
field: PyRef<PyNodeField>,
fespace: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::gradient(&field.inner, &fespace.inner)?;
Ok(PyElementField { inner: ef })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (u, fespace, measure=StrainMeasure::Linear))]
pub fn deformation(
u: PyRef<PyNodeField>,
fespace: PyRef<PyFiniteElementSpace>,
measure: StrainMeasure,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::deformation(&u.inner, &fespace.inner, measure)?;
Ok(PyElementField { inner: ef })
}
#[py_op(method_on = PyNodeField)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn interp_to_gauss(
field: PyRef<PyNodeField>,
fespace: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::interp_to_gauss(&field.inner, &fespace.inner)?;
Ok(PyElementField { inner: ef })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (temperature, materials, fespace, t_ref, measure=StrainMeasure::Linear))]
pub fn thermal_strain(
temperature: PyRef<PyElementField>,
materials: PyRef<PyElementField>,
fespace: PyRef<PyFiniteElementSpace>,
t_ref: f64,
measure: StrainMeasure,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::thermal_strain(
&temperature.inner,
&materials.inner,
&fespace.inner,
t_ref,
measure,
)?;
Ok(PyElementField { inner: ef })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn stress_measure(
stress: PyRef<PyElementField>,
gradient: PyRef<PyElementField>,
target: StressMeasure,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::stress_measure(&stress.inner, &gradient.inner, target)?;
Ok(PyElementField { inner: ef })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn rotate_frame(
field: PyRef<PyElementField>,
gradient: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::rotate_frame(&field.inner, &gradient.inner)?;
Ok(PyElementField { inner: ef })
}
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn elastic_stress(
model: PyRef<PyModel>,
strain: PyRef<PyElementField>,
materials: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
let ef =
crate::ops::element_field::elastic_stress(&model.inner, &strain.inner, &materials.inner)?;
Ok(PyElementField { inner: ef })
}
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn elastic_strain(
model: PyRef<PyModel>,
stress: PyRef<PyElementField>,
materials: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
let ef =
crate::ops::element_field::elastic_strain(&model.inner, &stress.inner, &materials.inner)?;
Ok(PyElementField { inner: ef })
}
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn strain(
model: PyRef<PyModel>,
solution: PyRef<PyNodeField>,
materials: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::strain(&model.inner, &solution.inner, &materials.inner)?;
Ok(PyElementField { inner: ef })
}
#[py_op(method_on = PyElementField)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn cell_integral(
field: PyRef<PyElementField>,
target: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::cell_integral(&field.inner, &target.inner)?;
Ok(PyElementField { inner: ef })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn beam_deformation(
field: PyRef<PyNodeField>,
fespace: PyRef<PyFiniteElementSpace>,
material: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
let ef =
crate::ops::element_field::beam_deformation(&field.inner, &fespace.inner, &material.inner)?;
Ok(PyElementField { inner: ef })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn shell_deformation(
field: PyRef<PyNodeField>,
fespace: PyRef<PyFiniteElementSpace>,
formulation: &str,
) -> PyResult<PyElementField> {
let formulation =
<crate::models::shell::ShellFormulation as crate::named::Named>::parse(formulation)?;
let ef =
crate::ops::element_field::shell_deformation(&field.inner, &fespace.inner, formulation)?;
Ok(PyElementField { inner: ef })
}
#[py_op(method_on = PySubModel, name = "material_field")]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn sub_material_field(
sub_model: PyRef<PySubModel>,
components_and_values: Vec<(String, f64)>,
) -> PyResult<PySubElementField> {
let pairs: Vec<(&str, f64)> = components_and_values
.iter()
.map(|(c, v)| (c.as_str(), *v))
.collect();
let sub = crate::ops::element_field::sub_material_field(&sub_model.handle.read(), &pairs)?;
Ok(PySubElementField {
handle: Handle::new(sub),
})
}
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn material_field(
model: PyRef<PyModel>,
components_and_values: Vec<(String, f64)>,
) -> PyResult<PyElementField> {
let pairs: Vec<(&str, f64)> = components_and_values
.iter()
.map(|(c, v)| (c.as_str(), *v))
.collect();
let ef = crate::ops::element_field::material_field(&model.inner, &pairs)?;
Ok(PyElementField { inner: ef })
}
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn material_field_per_sub_model(
model: PyRef<PyModel>,
components_and_values_per_sub_model: Vec<Vec<(String, f64)>>,
) -> PyResult<PyElementField> {
let owned: Vec<Vec<(&str, f64)>> = components_and_values_per_sub_model
.iter()
.map(|v| v.iter().map(|(c, x)| (c.as_str(), *x)).collect())
.collect();
let slices: Vec<&[(&str, f64)]> = owned.iter().map(|v| v.as_slice()).collect();
let ef = crate::ops::element_field::material_field_per_sub_model(&model.inner, &slices)?;
Ok(PyElementField { inner: ef })
}
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (model, deformation, materials, prev=None, dt=None))]
pub fn integrate_behavior(
model: PyRef<PyModel>,
deformation: PyRef<PyElementField>,
materials: PyRef<PyElementField>,
prev: Option<PyRef<PyElementField>>,
dt: Option<f64>,
) -> PyResult<PyElementField> {
let prev_inner = prev.as_ref().map(|p| &p.inner);
let ef = crate::ops::element_field::behavior::integrate(
&model.inner,
&deformation.inner,
prev_inner,
&materials.inner,
dt,
)?;
Ok(PyElementField { inner: ef })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(name = "mask_element")]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
#[allow(clippy::too_many_arguments)]
pub fn mask(
py: Python<'_>,
field: &Bound<'_, PyAny>,
ge: Option<f64>,
gt: Option<f64>,
le: Option<f64>,
lt: Option<f64>,
components: Option<Vec<String>>,
) -> PyResult<Py<PyAny>> {
if let Ok(f) = field.extract::<PyRef<PyElementField>>() {
Ok(Py::new(py, mask_element_field(f, ge, gt, le, lt, components)?)?.into_any())
} else if let Ok(f) = field.extract::<PyRef<PySubElementField>>() {
Ok(Py::new(py, mask_sub_element_field(f, ge, gt, le, lt, components)?)?.into_any())
} else {
Err(PyTypeError::new_err(
"expected a PyElementField or PySubElementField",
))
}
}
#[py_op(method_on = PyElementField, name = "mask")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn mask_element_field(
field: PyRef<PyElementField>,
ge: Option<f64>,
gt: Option<f64>,
le: Option<f64>,
lt: Option<f64>,
components: Option<Vec<String>>,
) -> PyResult<PyElementField> {
let band = crate::atoms::Band::new(ge, gt, le, lt)?;
Ok(PyElementField {
inner: crate::ops::element_field::mask(&field.inner, &band, components)?,
})
}
#[py_op(method_on = PySubElementField, name = "mask")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn mask_sub_element_field(
field: PyRef<PySubElementField>,
ge: Option<f64>,
gt: Option<f64>,
le: Option<f64>,
lt: Option<f64>,
components: Option<Vec<String>>,
) -> PyResult<PySubElementField> {
let band = crate::atoms::Band::new(ge, gt, le, lt)?;
let out = crate::ops::element_field::mask_sub(&field.handle.read(), &band, components);
Ok(PySubElementField {
handle: Handle::new(out),
})
}