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 crate::store::{insert, read};
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
#[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)?,
})
}
#[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]
pub fn deformation(
u: PyRef<PyNodeField>,
fespace: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::deformation(&u.inner, &fespace.inner)?;
Ok(PyElementField { inner: ef })
}
#[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]
pub fn thermal_strain(
temperature: PyRef<PyElementField>,
materials: PyRef<PyElementField>,
fespace: PyRef<PyFiniteElementSpace>,
t_ref: f64,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::thermal_strain(
&temperature.inner,
&materials.inner,
&fespace.inner,
t_ref,
)?;
Ok(PyElementField { inner: ef })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn frame_deformation(
field: PyRef<PyNodeField>,
fespace: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::frame_deformation(&field.inner, &fespace.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>,
) -> PyResult<PyElementField> {
let ef = crate::ops::element_field::beam_deformation(&field.inner, &fespace.inner)?;
Ok(PyElementField { inner: ef })
}
#[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(&*read(&sub_model.handle)?, &pairs)?;
Ok(PySubElementField {
handle: insert(sub),
})
}
#[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 })
}
#[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 })
}
#[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>> {
let band = crate::atoms::Band::new(ge, gt, le, lt)?;
if let Ok(f) = field.extract::<PyRef<PyElementField>>() {
let out = crate::ops::element_field::mask(&f.inner, &band, components)?;
Ok(Py::new(py, PyElementField { inner: out })?.into_any())
} else if let Ok(f) = field.extract::<PyRef<PySubElementField>>() {
let out = crate::ops::element_field::mask_sub(&*read(&f.handle)?, &band, components);
Ok(Py::new(
py,
PySubElementField {
handle: insert(out),
},
)?
.into_any())
} else {
Err(PyTypeError::new_err(
"expected a PyElementField or PySubElementField",
))
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyElementField {
fn consolidate(slf: PyRef<'_, Self>) -> PyResult<PyElementField> {
super::element_field::consolidate(slf)
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyNodeField {
fn gradient(
slf: PyRef<'_, Self>,
fespace: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
super::element_field::gradient(slf, fespace)
}
fn interp_to_gauss(
slf: PyRef<'_, Self>,
fespace: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
super::element_field::interp_to_gauss(slf, fespace)
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyModel {
fn material_field(
slf: PyRef<'_, Self>,
components_and_values: Vec<(String, f64)>,
) -> PyResult<PyElementField> {
super::element_field::material_field(slf, components_and_values)
}
fn material_field_per_sub_model(
slf: PyRef<'_, Self>,
components_and_values_per_sub_model: Vec<Vec<(String, f64)>>,
) -> PyResult<PyElementField> {
super::element_field::material_field_per_sub_model(slf, components_and_values_per_sub_model)
}
#[pyo3(signature = (deformation, materials, prev=None, dt=None))]
fn integrate_behavior(
slf: PyRef<'_, Self>,
deformation: PyRef<PyElementField>,
materials: PyRef<PyElementField>,
prev: Option<PyRef<PyElementField>>,
dt: Option<f64>,
) -> PyResult<PyElementField> {
super::element_field::integrate_behavior(slf, deformation, materials, prev, dt)
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PySubModel {
fn material_field(
slf: PyRef<'_, Self>,
components_and_values: Vec<(String, f64)>,
) -> PyResult<PySubElementField> {
super::element_field::sub_material_field(slf, components_and_values)
}
}