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::dump::DumpOptions;
use crate::error::Result;
use crate::handle::Handle;
use crate::models::transfer::{
coefficient_indices, coefficient_name, exchange_matrix, flux_name, internal_force,
material_contract, physics_slice,
};
use crate::models::{CellGeom, Domain, MatrixLayout, Physics, SubModelKind};
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)]
pub struct BoundaryTransfer {
pub(crate) fespace: Handle<SubFiniteElementSpace>,
pub(crate) support: Handle<SubMesh>,
pub(crate) components: Vec<(String, String)>,
pub(crate) physics: Physics,
}
impl BoundaryTransfer {
pub fn new(
fespace: Handle<SubFiniteElementSpace>,
components: Vec<(String, String)>,
physics: Physics,
) -> Result<Self> {
material_contract("BoundaryTransfer", &components)?;
let submesh = fespace.read().submesh();
let support = submesh.read().to_poi1()?;
Ok(Self {
fespace,
support,
components,
physics,
})
}
}
impl SubModelKind for BoundaryTransfer {
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 as_domain(&self) -> Option<&dyn Domain> {
Some(self)
}
fn stiffness_layout(&self) -> Option<MatrixLayout> {
Some(MatrixLayout {
fespaces: vec![self.fespace.clone()],
support: self.support.clone(),
dual_vars: self.dual_vars(),
primal_vars: self.primal_vars(),
ordering: DofOrdering::NodesThenVars,
symmetric: true,
})
}
fn element_matrix(
&self,
geoms: &[CellGeom],
material: Option<&SubElementField>,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
let mat = material.expect("BoundaryTransfer requires a material field");
let coefficients = coefficient_indices(mat, &self.components)?;
exchange_matrix(geom, geom, mat, &coefficients, 1.0, ke)
}
fn internal_force_element(
&self,
geoms: &[CellGeom],
stress: &SubElementField,
fe: &mut [f64],
) -> Result<()> {
internal_force(&geoms[0], stress, &self.components, fe)
}
fn physics(&self) -> &'static [Physics] {
physics_slice(self.physics)
}
fn label(&self) -> &'static str {
"BoundaryTransfer"
}
fn render(&self, _opts: &DumpOptions) -> String {
let primal = self.primal_vars().join(", ");
let dual = self.dual_vars().join(", ");
let n = self.support.read().cell_count();
format!(
"SubModel<BoundaryTransfer>\n primal var(s): {primal}\n \
dual var(s): {dual}\n support: {n} node(s)"
)
}
}
impl Domain for BoundaryTransfer {
fn material_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn material_components(&self) -> Option<Vec<String>> {
Some(
self.components
.iter()
.map(|(p, _)| coefficient_name(p))
.collect(),
)
}
fn behavior_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn behavior_output_components(&self) -> Result<Vec<String>> {
Ok(self.components.iter().map(|(p, _)| flux_name(p)).collect())
}
fn integrate_point(
&self,
geom: &CellGeom,
input: &SubElementField,
_prev: Option<&SubElementField>,
material: Option<&SubElementField>,
g: usize,
_dt: Option<f64>,
out: &mut [f64],
) -> Result<()> {
let mat = material.expect("BoundaryTransfer declares a material_fespace");
let cell = geom.cell;
for (v, (primal, _)) in self.components.iter().enumerate() {
let h = mat.value(cell, g, &coefficient_name(primal))?;
out[v] = h * input.value(cell, g, primal)?;
}
Ok(())
}
}