use crate::containers::element_field::SubElementField;
use crate::containers::finite_element_space::SubFiniteElementSpace;
use crate::containers::matrix::DofOrdering;
use crate::containers::mesh::SubMesh;
use crate::coords::Coords;
use crate::dump::DumpOptions;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::models::transfer::{
coefficient_name, exchange_matrix, flux_name, internal_force, jump_name, material_contract,
physics_slice,
};
use crate::models::ElementLayout;
use crate::models::ZoneLayout;
use crate::models::{
CellGeom, Contribution, CouplingLayout, Domain, MatrixKind, MatrixLayout, Physics, SubModelKind,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)]
pub struct InterfaceTransfer {
pub(crate) side_a: Handle<SubFiniteElementSpace>,
pub(crate) side_b: Handle<SubFiniteElementSpace>,
pub(crate) support_a: Handle<SubMesh>,
pub(crate) support_b: Handle<SubMesh>,
pub(crate) components: Vec<(String, String)>,
pub(crate) physics: Physics,
}
impl InterfaceTransfer {
pub fn new(
side_a: Handle<SubFiniteElementSpace>,
side_b: Handle<SubFiniteElementSpace>,
components: Vec<(String, String)>,
physics: Physics,
tol: f64,
) -> Result<Self> {
material_contract("InterfaceTransfer", &components)?;
let (mesh_a, mesh_b) = (side_a.read().submesh(), side_b.read().submesh());
check_conforming_geometry(&mesh_a, &mesh_b, tol)?;
let support_a = mesh_a.read().to_poi1()?;
let support_b = mesh_b.read().to_poi1()?;
Ok(Self {
side_a,
side_b,
support_a,
support_b,
components,
physics,
})
}
fn diagonal_layout(
&self,
fespace: &Handle<SubFiniteElementSpace>,
support: &Handle<SubMesh>,
) -> MatrixLayout {
MatrixLayout {
fespaces: vec![fespace.clone()],
support: support.clone(),
dual_vars: self.dual_vars(),
primal_vars: self.primal_vars(),
ordering: DofOrdering::NodesThenVars,
symmetric: true,
}
}
fn coupling_layout(
&self,
row_fespace: &Handle<SubFiniteElementSpace>,
row_support: &Handle<SubMesh>,
col_fespace: &Handle<SubFiniteElementSpace>,
col_support: &Handle<SubMesh>,
) -> CouplingLayout {
CouplingLayout {
fespaces: vec![row_fespace.clone()],
col_fespaces: vec![col_fespace.clone()],
row_support: row_support.clone(),
col_support: col_support.clone(),
dual_vars: self.dual_vars(),
primal_vars: self.primal_vars(),
ordering: DofOrdering::NodesThenVars,
}
}
}
impl SubModelKind for InterfaceTransfer {
fn primal_vars(&self) -> Vec<String> {
self.components.iter().map(|(p, _)| p.clone()).collect()
}
fn dual_vars(&self) -> Vec<String> {
self.components.iter().map(|(_, d)| d.clone()).collect()
}
fn physics(&self) -> &'static [Physics] {
physics_slice(self.physics)
}
fn as_domain(&self) -> Option<&dyn Domain> {
Some(self)
}
fn contributions(
&self,
kind: MatrixKind,
_material: Option<&Handle<SubElementField>>,
) -> Result<Vec<Contribution>> {
if kind != MatrixKind::Stiffness {
return Ok(Vec::new());
}
Ok(vec![
Contribution::Computed(self.diagonal_layout(&self.side_a, &self.support_a)),
Contribution::Computed(self.diagonal_layout(&self.side_b, &self.support_b)),
Contribution::Coupling(self.coupling_layout(
&self.side_a,
&self.support_a,
&self.side_b,
&self.support_b,
)),
Contribution::Coupling(self.coupling_layout(
&self.side_b,
&self.support_b,
&self.side_a,
&self.support_a,
)),
])
}
fn internal_force_reads(&self) -> Vec<String> {
self.components
.iter()
.map(|(p, _)| crate::models::transfer::flux_name(p))
.collect()
}
fn internal_force_element(
&self,
geoms: &[CellGeom],
stress: &SubElementField,
lay: &[u32],
fe: &mut [f64],
) -> Result<()> {
internal_force(&geoms[0], stress, lay, fe)
}
fn label(&self) -> &'static str {
"InterfaceTransfer"
}
fn render(&self, _opts: &DumpOptions) -> String {
let primal = self.primal_vars().join(", ");
let dual = self.dual_vars().join(", ");
let cells = self.side_a.read().cell_count();
format!(
"SubModel<InterfaceTransfer>\n primal var(s): {primal}\n \
dual var(s): {dual}\n interface: {cells} facing cell pair(s)"
)
}
}
impl Domain for InterfaceTransfer {
fn material_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.side_a.clone()
}
fn material_components(&self) -> Vec<String> {
self.components
.iter()
.map(|(p, _)| coefficient_name(p))
.collect()
}
fn behavior_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.side_a.clone()
}
fn behavior_output_components(&self) -> Vec<String> {
self.components.iter().map(|(p, _)| flux_name(p)).collect()
}
fn deformation_reads(&self) -> Vec<String> {
self.components.iter().map(|(p, _)| jump_name(p)).collect()
}
fn integrate_point(
&self,
_geom: &CellGeom,
_g: usize,
lay: &ZoneLayout,
deformation: &[f64],
_prev: &[f64],
material: &[f64],
_dt: f64,
out: &mut [f64],
) -> Result<()> {
for v in 0..self.components.len() {
let h = material[lay.material[v] as usize];
out[v] = h * deformation[lay.deformation[v] as usize];
}
Ok(())
}
fn element_matrix(
&self,
geoms: &[CellGeom],
material: &SubElementField,
lay: &ElementLayout,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
let mat = material;
exchange_matrix(geom, geom, mat, lay, 1.0, ke)
}
fn coupling_element(
&self,
_kind: MatrixKind,
row_geoms: &[CellGeom],
col_geoms: &[CellGeom],
material: &SubElementField,
lay: &ElementLayout,
ke: &mut [f64],
) -> Result<()> {
let mat = material;
exchange_matrix(&row_geoms[0], &col_geoms[0], mat, lay, -1.0, ke)
}
}
fn check_conforming_geometry(
mesh_a: &Handle<SubMesh>,
mesh_b: &Handle<SubMesh>,
tol: f64,
) -> Result<()> {
let (a, b) = (mesh_a.read(), mesh_b.read());
if a.element_type() != b.element_type() {
return Err(PyrucastError::Message(format!(
"InterfaceTransfer: the two sides must carry the same element type — \
{:?} facing {:?}",
a.element_type(),
b.element_type()
)));
}
if a.cell_count() != b.cell_count() {
return Err(PyrucastError::Message(format!(
"InterfaceTransfer: the two sides must be conforming — {} cell(s) facing {}",
a.cell_count(),
b.cell_count()
)));
}
let (coords_a_h, coords_b_h) = (a.coords(), b.coords());
let (guard_a, guard_b) = (coords_a_h.read(), coords_b_h.read());
let (coords_a, coords_b): (&Coords, &Coords) = (&guard_a, &guard_b);
for (k, (&na, &nb)) in a.connectivity().iter().zip(b.connectivity()).enumerate() {
let (pa, pb) = (coords_a.position(na)?, coords_b.position(nb)?);
let d2: f64 = pa
.iter()
.zip(pb)
.map(|(x, y)| (x - y) * (x - y))
.sum::<f64>();
if d2.sqrt() > tol {
return Err(PyrucastError::Message(format!(
"InterfaceTransfer: the interface is not node-conforming — the node pair at \
connectivity slot {k} is {:.3e} apart (tolerance {tol:.3e}). Local node `k` of \
a cell must face local node `k` of its counterpart.",
d2.sqrt()
)));
}
}
Ok(())
}