use crate::containers::element_field::SubElementField;
use crate::containers::field::SubField;
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::{CellGeom, Domain, MatrixLayout, Physics, SubModelKind};
use serde::{Deserialize, Serialize};
pub const PRIMAL_VAR: &str = "T";
pub const DUAL_VAR: &str = "q";
const MATERIAL_COMPONENTS: &[&str] = &["emis", "T_inf"];
const OPTIONAL_COMPONENTS: &[&str] = &["sigma"];
pub const STEFAN_BOLTZMANN: f64 = 5.670_374_419e-8;
const INPUT_COMPONENT: &str = PRIMAL_VAR;
const OUTPUT_FLUX: &str = "flux";
const OUTPUT_TANGENT: &str = "ktan";
#[derive(Clone, Serialize, Deserialize)]
pub struct Radiation {
pub(crate) fespace: Handle<SubFiniteElementSpace>,
pub(crate) support: Handle<SubMesh>,
}
impl Radiation {
pub fn new(fespace: Handle<SubFiniteElementSpace>) -> Result<Self> {
let submesh = fespace.read().submesh();
let support = submesh.read().to_poi1()?;
Ok(Self { fespace, support })
}
fn layout(&self) -> MatrixLayout {
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,
}
}
}
impl SubModelKind for Radiation {
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(self.layout())
}
fn tangent_layout(&self) -> Option<MatrixLayout> {
Some(self.layout())
}
fn element_matrix(
&self,
geoms: &[CellGeom],
material: Option<&SubElementField>,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
let mat = material.expect("Radiation declares a material_fespace");
surface_mass(geom, ke, |g| {
let (sigma, emis) = (
sigma_of(mat, geom.cell, g)?,
mat.value(geom.cell, g, "emis")?,
);
let t_inf = mat.value(geom.cell, g, "T_inf")?;
Ok(4.0 * sigma * emis * t_inf.powi(3))
})
}
fn element_tangent(
&self,
geoms: &[CellGeom],
_material: Option<&SubElementField>,
state: Option<&SubElementField>,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
let st = state.expect("the radiation tangent requires the current state field");
surface_mass(geom, ke, |g| st.value(geom.cell, g, OUTPUT_TANGENT))
}
fn internal_force_element(
&self,
geoms: &[CellGeom],
stress: &SubElementField,
fe: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
for g in 0..geom.n_gauss {
let shape = geom.n_at_g(g)?;
let w = geom.det_j_w(g)?;
let flux = stress.value(geom.cell, g, OUTPUT_FLUX)?;
for i in 0..geom.n_nodes {
fe[i] += shape[i] * flux * w;
}
}
Ok(())
}
fn physics(&self) -> &'static [Physics] {
&[Physics::Thermal, Physics::Radiation]
}
fn label(&self) -> &'static str {
"Radiation"
}
fn render(&self, _opts: &DumpOptions) -> String {
let n = self.support.read().cell_count();
format!(
"SubModel<Radiation>\n primal var(s): {PRIMAL_VAR}\n \
dual var(s): {DUAL_VAR}\n support: {n} node(s)"
)
}
}
impl Domain for Radiation {
fn material_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn material_components(&self) -> Option<Vec<String>> {
Some(owned_components(MATERIAL_COMPONENTS))
}
fn optional_material_components(&self) -> &'static [&'static str] {
OPTIONAL_COMPONENTS
}
fn behavior_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn behavior_output_components(&self) -> Result<Vec<String>> {
Ok(vec![OUTPUT_FLUX.to_string(), OUTPUT_TANGENT.to_string()])
}
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("Radiation declares a material_fespace");
let cell = geom.cell;
let sigma = sigma_of(mat, cell, g)?;
let emis = mat.value(cell, g, "emis")?;
let t_inf = mat.value(cell, g, "T_inf")?;
let t = input.value(cell, g, INPUT_COMPONENT)?;
out[0] = sigma * emis * (t.powi(4) - t_inf.powi(4));
out[1] = 4.0 * sigma * emis * t.powi(3);
Ok(())
}
}
fn sigma_of(material: &SubElementField, cell: usize, g: usize) -> Result<f64> {
Ok(match material.component_index("sigma") {
Some(_) => material.value(cell, g, "sigma")?,
None => STEFAN_BOLTZMANN,
})
}
fn surface_mass(
geom: &CellGeom,
ke: &mut [f64],
coeff: impl Fn(usize) -> Result<f64>,
) -> Result<()> {
let n_nodes = geom.n_nodes;
for g in 0..geom.n_gauss {
let shape = geom.n_at_g(g)?;
let w = geom.det_j_w(g)? * coeff(g)?;
for i in 0..n_nodes {
for j in 0..n_nodes {
ke[i * n_nodes + j] += shape[i] * shape[j] * w;
}
}
}
Ok(())
}