use crate::aggregate::Aggregate;
use crate::containers::element_field::ElementField;
use crate::containers::field::SubField;
use crate::containers::finite_element_space::FiniteElementSpace;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::models::kernel;
use crate::ops::element_field::gradient::AXES;
const TEMPERATURE: &str = "T";
const ALPHA: &str = "alpha";
pub fn thermal_strain(
temperature: &ElementField,
material: &ElementField,
fespace: &FiniteElementSpace,
t_ref: f64,
) -> Result<ElementField> {
let mut out = ElementField::empty();
for sub in fespace {
let temp_sub = temperature.sub_for_fespace(sub)?;
let mat_sub = material.sub_for_fespace_with(sub, &[ALPHA.to_string()])?;
let (space_dim, axisymmetric) = {
let s = sub.read();
(s.space_dim(), s.is_axisymmetric())
};
if !temp_sub
.read()
.components()
.iter()
.any(|c| c == TEMPERATURE)
{
return Err(PyrucastError::Message(format!(
"thermal_strain: the temperature field carries no '{TEMPERATURE}' component"
)));
}
let mut names = Vec::with_capacity(space_dim * (space_dim + 1) / 2);
let mut pairs = Vec::with_capacity(space_dim * (space_dim + 1) / 2);
for i in 0..space_dim {
for j in i..space_dim {
names.push(format!("eps_{}{}", AXES[i], AXES[j]));
pairs.push((i, j));
}
}
if axisymmetric {
names.push("eps_zz".to_string());
}
let n_diag = names.len();
let sf = kernel::element_pointwise(
sub,
&temp_sub,
Some(&mat_sub),
names,
|geom, input, material, g, out| {
let cell = geom.cell;
let material = material.expect("thermal_strain supplies a material field");
let d_temp = input.value(cell, g, TEMPERATURE)? - t_ref;
let eps_th = material.value(cell, g, ALPHA)? * d_temp;
for (c, &(i, j)) in pairs.iter().enumerate() {
out[c] = if i == j { eps_th } else { 0.0 };
}
if axisymmetric {
out[n_diag - 1] = eps_th; }
Ok(())
},
)?;
out.add_sub(Handle::new(sf))?;
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::atoms::{ElementType, Node};
use crate::containers::finite_element_space::FiniteElementSpace;
use crate::containers::mesh::{Mesh, SubMesh};
use crate::coords::Coords;
use crate::handle::Handle;
fn tri3_fes_2d() -> FiniteElementSpace {
let coords = Handle::new(Coords::new(2).unwrap());
let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
let c = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::TRI3));
mesh.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
FiniteElementSpace::lagrange1(&mesh).unwrap()
}
#[test]
fn uniform_heating_gives_isotropic_normal_strain() {
let fes = tri3_fes_2d();
let temp = ElementField::new(&fes, vec!["T".into()]).unwrap();
temp.get(0).unwrap().write().set_uniform("T", 70.0).unwrap();
let mat = ElementField::new(&fes, vec!["alpha".into()]).unwrap();
mat.get(0)
.unwrap()
.write()
.set_uniform("alpha", 1e-5)
.unwrap();
let eps = thermal_strain(&temp, &mat, &fes, 20.0).unwrap();
let s = eps.get(0).unwrap().read();
assert_eq!(
s.components(),
&[
"eps_xx".to_string(),
"eps_xy".to_string(),
"eps_yy".to_string()
]
);
for g in 0..s.gauss_count() {
assert!((s.value(0, g, "eps_xx").unwrap() - 5e-4).abs() < 1e-15);
assert!((s.value(0, g, "eps_yy").unwrap() - 5e-4).abs() < 1e-15);
assert!(s.value(0, g, "eps_xy").unwrap().abs() < 1e-18);
}
}
#[test]
fn no_temperature_change_gives_zero_strain() {
let fes = tri3_fes_2d();
let temp = ElementField::new(&fes, vec!["T".into()]).unwrap();
temp.get(0).unwrap().write().set_uniform("T", 20.0).unwrap();
let mat = ElementField::new(&fes, vec!["alpha".into()]).unwrap();
mat.get(0)
.unwrap()
.write()
.set_uniform("alpha", 1e-5)
.unwrap();
let eps = thermal_strain(&temp, &mat, &fes, 20.0).unwrap();
let s = eps.get(0).unwrap().read();
for g in 0..s.gauss_count() {
for comp in ["eps_xx", "eps_xy", "eps_yy"] {
assert!(s.value(0, g, comp).unwrap().abs() < 1e-18);
}
}
}
#[test]
fn missing_alpha_errors() {
let fes = tri3_fes_2d();
let temp = ElementField::new(&fes, vec!["T".into()]).unwrap();
temp.get(0).unwrap().write().set_uniform("T", 50.0).unwrap();
let mat = ElementField::new(&fes, vec!["E".into()]).unwrap();
let err = thermal_strain(&temp, &mat, &fes, 20.0).unwrap_err();
assert!(format!("{err}").contains("alpha"), "unexpected: {err}");
}
}