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::owned_components;
use crate::models::symmetry::{self, MaterialSymmetry};
use crate::models::{CellGeom, Domain, MatrixLayout, Physics, SubModelKind};
use serde::{Deserialize, Serialize};
pub const PRIMAL_VAR: &str = "T";
pub const DUAL_VAR: &str = "q";
pub const MATERIAL_COMPONENT: &str = "k";
const MATERIAL_COMPONENTS: &[&str] = &[MATERIAL_COMPONENT];
const ORTHOTROPIC_2D: &[&str] = &["k_1", "k_2", "k_3", "V1X", "V1Y"];
const ORTHOTROPIC_3D: &[&str] = &[
"k_1", "k_2", "k_3", "V1X", "V1Y", "V1Z", "V2X", "V2Y", "V2Z",
];
const ANISOTROPIC_2D: &[&str] = &["k_11", "k_12", "k_13", "k_22", "k_23", "k_33", "V1X", "V1Y"];
const ANISOTROPIC_3D: &[&str] = &[
"k_11", "k_12", "k_13", "k_22", "k_23", "k_33", "V1X", "V1Y", "V1Z", "V2X", "V2Y", "V2Z",
];
fn material_contract(symmetry: MaterialSymmetry, space_dim: usize) -> &'static [&'static str] {
match (symmetry, space_dim) {
(MaterialSymmetry::Isotropic, _) => MATERIAL_COMPONENTS,
(MaterialSymmetry::Orthotropic, 2) => ORTHOTROPIC_2D,
(MaterialSymmetry::Orthotropic, _) => ORTHOTROPIC_3D,
(MaterialSymmetry::Anisotropic, 2) => ANISOTROPIC_2D,
(MaterialSymmetry::Anisotropic, _) => ANISOTROPIC_3D,
}
}
const CAPACITY_COMPONENTS: &[&str] = &["rho", "cp"];
const AXES: [&str; 3] = ["x", "y", "z"];
fn deformation_components(space_dim: usize) -> Vec<String> {
(0..space_dim)
.map(|a| format!("grad_{PRIMAL_VAR}_{}", AXES[a]))
.collect()
}
fn flux_components(space_dim: usize) -> Vec<String> {
(0..space_dim)
.map(|a| format!("flux_{}", AXES[a]))
.collect()
}
#[derive(Clone, Serialize, Deserialize)]
pub struct HeatConduction {
pub(crate) fespace: Handle<SubFiniteElementSpace>,
pub(crate) support: Handle<SubMesh>,
pub(crate) space_dim: usize,
pub(crate) symmetry: MaterialSymmetry,
}
impl HeatConduction {
pub fn new(fespace: Handle<SubFiniteElementSpace>) -> Result<Self> {
Self::with_symmetry(fespace, MaterialSymmetry::Isotropic)
}
pub fn with_symmetry(
fespace: Handle<SubFiniteElementSpace>,
symmetry: MaterialSymmetry,
) -> Result<Self> {
let (submesh, space_dim) = {
let s = fespace.read();
(s.submesh(), s.space_dim())
};
let support = submesh.read().to_poi1()?;
Ok(Self {
fespace,
support,
space_dim,
symmetry,
})
}
}
impl SubModelKind for HeatConduction {
fn primal_vars(&self) -> Vec<String> {
vec![PRIMAL_VAR.to_string()]
}
fn dual_vars(&self) -> Vec<String> {
vec![DUAL_VAR.to_string()]
}
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 mass_layout(&self) -> Option<MatrixLayout> {
self.stiffness_layout()
}
fn element_matrix(
&self,
geoms: &[CellGeom],
material: Option<&SubElementField>,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
element_stiffness(
geom,
material.expect("HeatConduction requires a material field"),
self.symmetry,
ke,
)
}
fn element_mass(
&self,
geoms: &[CellGeom],
material: Option<&SubElementField>,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
element_capacity(
geom,
material.expect("HeatConduction requires a material field"),
ke,
)
}
fn internal_force_element(
&self,
geoms: &[CellGeom],
stress: &SubElementField,
fe: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
let d = geom.space_dim;
let flux_names = flux_components(d);
for g in 0..geom.n_gauss {
let dn = geom.dn_dx(g)?; let w = geom.det_j_w(g)?;
for i in 0..geom.n_nodes {
let mut s = 0.0;
for a in 0..d {
s += dn[i * d + a] * stress.value(geom.cell, g, &flux_names[a])?;
}
fe[i] += s * w;
}
}
Ok(())
}
fn physics(&self) -> &'static [Physics] {
&[Physics::Thermal]
}
fn label(&self) -> &'static str {
"HeatConduction"
}
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<HeatConduction({})>\n primal var(s): {primal}\n \
dual var(s): {dual}\n support: {n} node(s)",
self.symmetry
)
}
}
impl Domain for HeatConduction {
fn material_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn material_components(&self) -> Option<Vec<String>> {
Some(owned_components(material_contract(
self.symmetry,
self.space_dim,
)))
}
fn optional_material_components(&self) -> &'static [&'static str] {
CAPACITY_COMPONENTS
}
fn behavior_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn behavior_output_components(&self) -> Result<Vec<String>> {
Ok(flux_components(self.fespace.read().space_dim()))
}
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("HeatConduction declares a material_fespace ⇒ material is supplied");
let (cell, space_dim) = (geom.cell, geom.space_dim);
let grad_names = deformation_components(space_dim);
let k3 =
symmetry::transport_tensor(mat, cell, g, self.symmetry, space_dim, MATERIAL_COMPONENT)?;
for a in 0..space_dim {
let mut acc = 0.0;
for b in 0..space_dim {
acc += k3[(a, b)] * input.value(cell, g, &grad_names[b])?;
}
out[a] = acc;
}
Ok(())
}
}
pub fn element_stiffness(
geom: &CellGeom,
material: &SubElementField,
symmetry: MaterialSymmetry,
ke: &mut [f64],
) -> Result<()> {
let n_nodes = geom.n_nodes;
let space_dim = geom.space_dim;
let tensor = if symmetry.has_frame() {
Some(symmetry::transport_tensor(
material,
geom.cell,
0,
symmetry,
space_dim,
MATERIAL_COMPONENT,
)?)
} else {
None
};
for g in 0..geom.n_gauss {
let dn = geom.dn_dx(g)?;
let det_j_w = geom.det_j_w(g)?;
match &tensor {
None => {
let k = material.value(geom.cell, g, MATERIAL_COMPONENT)?;
for i in 0..n_nodes {
for j in 0..n_nodes {
let mut grad_dot = 0.0;
for a in 0..space_dim {
grad_dot += dn[i * space_dim + a] * dn[j * space_dim + a];
}
ke[i * n_nodes + j] += k * grad_dot * det_j_w;
}
}
}
Some(k3) => {
for i in 0..n_nodes {
for j in 0..n_nodes {
let mut acc = 0.0;
for a in 0..space_dim {
for b in 0..space_dim {
acc += dn[i * space_dim + a] * k3[(a, b)] * dn[j * space_dim + b];
}
}
ke[i * n_nodes + j] += acc * det_j_w;
}
}
}
}
}
Ok(())
}
pub fn element_capacity(geom: &CellGeom, material: &SubElementField, ke: &mut [f64]) -> Result<()> {
let n_nodes = geom.n_nodes;
let rho = material.value(geom.cell, 0, "rho").map_err(|_| {
crate::error::PyrucastError::Message(
"HeatConduction capacity matrix: material component `rho` (density) is required".into(),
)
})?;
let cp = material.value(geom.cell, 0, "cp").map_err(|_| {
crate::error::PyrucastError::Message(
"HeatConduction capacity matrix: material component `cp` (specific heat) is required"
.into(),
)
})?;
let rho_cp = rho * cp;
for g in 0..geom.n_gauss {
let n = geom.n_at_g(g)?;
let w = geom.det_j_w(g)? * rho_cp;
for i in 0..n_nodes {
for j in 0..n_nodes {
ke[i * n_nodes + j] += n[i] * n[j] * w;
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, Node};
use crate::containers::field::SubField;
use crate::containers::finite_element_space::FiniteElementSpace;
use crate::containers::mesh::Mesh;
use crate::coords::Coords;
use crate::handle::Handle;
fn seg2_hc(length: f64) -> HeatConduction {
let coords = Handle::new(Coords::new(1).unwrap());
let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[length]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
mesh.add_cell(&[a.id(), b.id()]).unwrap();
let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
HeatConduction::new(fes.get(0).unwrap()).unwrap()
}
#[test]
fn behavior_fespace_is_the_physics_fespace() {
let hc = seg2_hc(1.0);
let fe = hc.behavior_fespace();
assert!(fe.same_object(&hc.fespace));
}
#[test]
fn integrate_behavior_returns_weak_form_flux() {
let hc = seg2_hc(2.0);
let grad = 1.5; let k = 1.5;
let mut def = SubElementField::new(hc.fespace.clone(), deformation_components(1)).unwrap();
def.set_uniform("grad_T_x", grad).unwrap();
let def = Handle::new(def);
let mut mat =
SubElementField::new(hc.fespace.clone(), vec![MATERIAL_COMPONENT.to_string()]).unwrap();
mat.set_uniform(MATERIAL_COMPONENT, k).unwrap();
let mat = Handle::new(mat);
let flux = hc.integrate_behavior(&def, None, Some(&mat), None).unwrap();
assert_eq!(flux.components(), &["flux_x".to_string()]);
let expected = k * grad;
for g in 0..flux.gauss_count() {
assert!((flux.value(0, g, "flux_x").unwrap() - expected).abs() < 1e-12);
}
}
}