use crate::containers::mesh::Mesh;
use crate::py::element_field::PyElementField;
use crate::py::evolution::PyEvolution;
use crate::py::mesh::PyMesh;
use crate::py::node_field::PyNodeField;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use std::path::Path;
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, path, field=None, binary=false))]
pub fn export_vtk(
mesh: PyRef<PyMesh>,
path: &str,
field: Option<&Bound<'_, PyAny>>,
binary: bool,
) -> PyResult<()> {
use crate::ops::export::VtkEncoding;
let path = Path::new(path);
let encoding = if binary {
VtkEncoding::Binary
} else {
VtkEncoding::Ascii
};
match field {
None => crate::ops::export::write_vtk_mesh(&mesh.inner, path, encoding)?,
Some(obj) => {
if let Ok(nf) = obj.extract::<PyRef<PyNodeField>>() {
crate::ops::export::write_vtk_node_field(&mesh.inner, &nf.inner, path, encoding)?;
} else if let Ok(ef) = obj.extract::<PyRef<PyElementField>>() {
crate::ops::export::write_vtk_element_field(
&mesh.inner,
&ef.inner,
path,
encoding,
)?;
} else if let Ok(ev) = obj.extract::<PyRef<PyEvolution>>() {
crate::ops::export::write_vtk_series(&mesh.inner, &ev.inner, path, encoding)?;
} else {
return Err(PyTypeError::new_err(
"field must be a NodeField, an ElementField or an Evolution",
));
}
}
}
Ok(())
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (groups, *, node_fields = Vec::new(), element_fields = Vec::new(), gauss = false, order = "pyrucast", first_tag = 1))]
pub fn to_arrays<'py>(
py: Python<'py>,
groups: &Bound<'py, PyDict>,
node_fields: Vec<Py<PyNodeField>>,
element_fields: Vec<Py<PyElementField>>,
gauss: bool,
order: &str,
first_tag: i64,
) -> PyResult<Bound<'py, PyDict>> {
use crate::ops::export::arrays::{to_arrays, ElementLayout};
use crate::py::arrays::{parse_order, PyArray};
let order = parse_order(order)?;
let named: Vec<(String, PyRef<'py, PyMesh>)> = groups
.iter()
.map(|(k, v)| Ok((k.extract::<String>()?, v.extract::<PyRef<'py, PyMesh>>()?)))
.collect::<PyResult<_>>()?;
let meshes: Vec<(String, &Mesh)> = named.iter().map(|(k, m)| (k.clone(), &m.inner)).collect();
let nodes: Vec<PyRef<'py, PyNodeField>> =
node_fields.iter().map(|f| f.bind(py).borrow()).collect();
let elems: Vec<PyRef<'py, PyElementField>> =
element_fields.iter().map(|f| f.bind(py).borrow()).collect();
let layout = if gauss {
ElementLayout::Gauss
} else {
ElementLayout::Cell
};
let node_refs: Vec<_> = nodes.iter().map(|f| &f.inner).collect();
let elem_refs: Vec<_> = elems.iter().map(|f| (&f.inner, layout)).collect();
let out = py.detach(|| to_arrays(&meshes, &node_refs, &elem_refs, order, first_tag))?;
let dict = PyDict::new(py);
let node_tags = Py::new(py, PyArray::i64(out.node_tags))?;
dict.set_item("node_tags", node_tags.clone_ref(py))?;
dict.set_item("node_coords", PyArray::f64(out.node_coords))?;
dict.set_item("dim", out.dim)?;
let blocks = out
.blocks
.into_iter()
.map(|b| {
Ok((
b.element_type.name(),
Py::new(py, PyArray::i64(b.node_tags))?,
Py::new(py, PyArray::i64(b.cell_tags))?,
b.groups,
))
})
.collect::<PyResult<Vec<_>>>()?;
dict.set_item("blocks", blocks)?;
let node_out = out
.node_fields
.into_iter()
.map(|f| {
Ok((
f.components,
node_tags.clone_ref(py),
Py::new(py, PyArray::f64(f.values))?,
))
})
.collect::<PyResult<Vec<_>>>()?;
dict.set_item("node_fields", node_out)?;
let cell_out = out
.cell_fields
.into_iter()
.map(|f| {
let layout: Bound<'py, PyAny> = if gauss {
f.rules
.into_iter()
.map(|r| {
Ok((
r.element_type.name(),
Py::new(py, PyArray::f64(r.ref_nodes))?,
Py::new(py, PyArray::f64(r.xi))?,
Py::new(py, PyArray::f64(r.weights))?,
))
})
.collect::<PyResult<Vec<_>>>()?
.into_pyobject(py)?
.into_any()
} else {
"cell".into_pyobject(py)?.into_any()
};
Ok((
f.components,
Py::new(py, PyArray::i64(f.cell_tags))?,
Py::new(py, PyArray::f64(f.values))?,
layout,
))
})
.collect::<PyResult<Vec<_>>>()?;
dict.set_item("cell_fields", cell_out)?;
Ok(dict)
}