use crate::containers::element_field::SubElementField;
use crate::containers::field::ABSENT_COMPONENT;
use crate::containers::finite_element_space::SubFiniteElementSpace;
use crate::containers::matrix::DofOrdering;
use crate::containers::mesh::SubMesh;
use crate::dump::DumpOptions;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::models::kernel::MAX_CELL_DOFS;
use crate::models::owned_components;
use crate::models::symmetry::{self, MaterialSymmetry};
use crate::models::tensor::{dual_name, primal_name, stress_names, Kinematics};
use crate::models::ZoneLayout;
use crate::models::{CellGeom, Domain, MatrixLayout, Physics, SubModelKind};
use crate::models::{ElementLayout, MatrixKind};
use serde::{Deserialize, Serialize};
const MATERIAL_COMPONENTS: &[&str] = &["E", "nu"];
type VoigtRows = [[f64; MAX_CELL_DOFS]; 6];
const OPTIONAL_COMPONENTS: &[&str] = &["alpha", "rho"];
const RHO_SLOT: usize = 1;
const ORTHOTROPIC_2D: &[&str] = &[
"E_1", "E_2", "E_3", "nu_12", "nu_13", "nu_23", "G_12", "G_13", "G_23", "V1X", "V1Y",
];
const ORTHOTROPIC_3D: &[&str] = &[
"E_1", "E_2", "E_3", "nu_12", "nu_13", "nu_23", "G_12", "G_13", "G_23", "V1X", "V1Y", "V1Z",
"V2X", "V2Y", "V2Z",
];
const ANISOTROPIC_2D: &[&str] = &[
"C_11", "C_12", "C_13", "C_14", "C_15", "C_16", "C_22", "C_23", "C_24", "C_25", "C_26", "C_33",
"C_34", "C_35", "C_36", "C_44", "C_45", "C_46", "C_55", "C_56", "C_66", "V1X", "V1Y",
];
const ANISOTROPIC_3D: &[&str] = &[
"C_11", "C_12", "C_13", "C_14", "C_15", "C_16", "C_22", "C_23", "C_24", "C_25", "C_26", "C_33",
"C_34", "C_35", "C_36", "C_44", "C_45", "C_46", "C_55", "C_56", "C_66", "V1X", "V1Y", "V1Z",
"V2X", "V2Y", "V2Z",
];
pub(crate) 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,
}
}
fn voigt_size(space_dim: usize, kinematics: Kinematics) -> usize {
match (space_dim, kinematics) {
(2, Kinematics::Axisymmetric) => 4,
(2, _) => 3,
_ => 6,
}
}
pub(crate) fn check_continuum_dimensions(
label: &str,
space_dim: usize,
ref_dim: usize,
) -> Result<()> {
if ref_dim != space_dim {
return Err(PyrucastError::Message(format!(
"{label}: a {ref_dim}-D element in a {space_dim}-D space is a manifold, not a \
solid — a boundary mesh carries loads (flux, convection), and a bar or beam \
is a structural physics of its own (truss, frame, timoshenko)"
)));
}
Ok(())
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Elasticity {
pub(crate) fespace: Handle<SubFiniteElementSpace>,
pub(crate) support: Handle<SubMesh>,
pub(crate) space_dim: usize,
pub(crate) kinematics: Kinematics,
pub(crate) symmetry: MaterialSymmetry,
}
impl Elasticity {
pub fn new(fespace: Handle<SubFiniteElementSpace>, kinematics: Kinematics) -> Result<Self> {
Self::with_symmetry(fespace, kinematics, MaterialSymmetry::Isotropic)
}
pub fn with_symmetry(
fespace: Handle<SubFiniteElementSpace>,
kinematics: Kinematics,
symmetry: MaterialSymmetry,
) -> Result<Self> {
let (submesh, space_dim, ref_dim, axisymmetric) = {
let s = fespace.read();
(
s.submesh(),
s.space_dim(),
s.ref_dim()?,
s.is_axisymmetric(),
)
};
check_continuum_dimensions("Elasticity", space_dim, ref_dim)?;
#[allow(clippy::match_like_matches_macro)]
let ok = match (space_dim, kinematics) {
(2, Kinematics::PlaneStress | Kinematics::PlaneStrain) => true,
(2, Kinematics::Axisymmetric) => true,
(3, Kinematics::Full3D) => true,
_ => false,
};
if !ok {
return Err(PyrucastError::Message(format!(
"Elasticity: kinematics {kinematics:?} is incompatible with a {space_dim}-D space \
(2-D ⇒ plane_stress|plane_strain|axisymmetric, 3-D ⇒ full_3d)"
)));
}
if axisymmetric != kinematics.is_axisymmetric() {
return Err(PyrucastError::Message(if axisymmetric {
format!(
"Elasticity: kinematics {kinematics:?} on an axisymmetric geometry — a body of \
revolution requires the `axisymmetric` kinematics (its integrals already \
carry the 2πr factor)"
)
} else {
"Elasticity: the `axisymmetric` kinematics requires an axisymmetric geometry \
(build the Coords with Coords::axisymmetric)"
.into()
}));
}
let support = submesh.read().to_poi1()?;
Ok(Self {
fespace,
support,
space_dim,
kinematics,
symmetry,
})
}
}
impl SubModelKind for Elasticity {
fn primal_vars(&self) -> Vec<String> {
(0..self.space_dim).map(primal_name).collect()
}
fn dual_vars(&self) -> Vec<String> {
(0..self.space_dim).map(dual_name).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 mass_layout(&self) -> Option<MatrixLayout> {
self.stiffness_layout()
}
fn geometric_layout(&self) -> Option<MatrixLayout> {
self.stiffness_layout()
}
fn tangent_layout(&self) -> Option<MatrixLayout> {
self.stiffness_layout()
}
fn physics(&self) -> &'static [Physics] {
&[Physics::Mechanical]
}
fn label(&self) -> &'static str {
"Elasticity"
}
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<Elasticity({:?}, {})>\n primal var(s): {primal}\n dual var(s): {dual}\n \
support: {n} node(s)",
self.kinematics, self.symmetry
)
}
}
impl Domain for Elasticity {
fn material_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn material_components(&self) -> Vec<String> {
owned_components(material_contract(self.symmetry, self.space_dim))
}
fn optional_material_components(&self) -> &'static [&'static str] {
OPTIONAL_COMPONENTS
}
fn behavior_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn behavior_output_components(&self) -> Vec<String> {
stress_names(self.space_dim, self.kinematics)
}
fn deformation_reads(&self) -> Vec<String> {
strain_reads(self.space_dim, self.kinematics)
}
fn integrate_point(
&self,
_geom: &CellGeom,
_g: usize,
lay: &ZoneLayout,
deformation: &[f64],
_prev: &[f64],
material: &[f64],
_dt: f64,
out: &mut [f64],
) -> Result<()> {
let d = self.space_dim;
let mut dmat = [[0.0_f64; 6]; 6];
let v = symmetry::elastic_constitutive_into(
material,
&lay.material,
self.symmetry,
self.kinematics,
d,
&mut dmat,
)?;
let strain = read_voigt_strain(deformation, &lay.deformation, d, self.kinematics);
for r in 0..v {
out[r] = (0..v).map(|c| dmat[r][c] * strain[c]).sum();
}
Ok(())
}
fn element_state_reads(&self, kind: MatrixKind) -> Vec<String> {
match kind {
MatrixKind::Tangent => Vec::new(),
_ => continuum_element_state_reads(kind, self.space_dim, self.kinematics),
}
}
fn element_geometric(
&self,
geoms: &[CellGeom],
_material: &SubElementField,
lay: &ElementLayout,
state: &SubElementField,
ke: &mut [f64],
) -> Result<()> {
element_geometric(&geoms[0], state, lay, ke)
}
fn element_tangent(
&self,
geoms: &[CellGeom],
material: &SubElementField,
lay: &ElementLayout,
_state: &SubElementField,
ke: &mut [f64],
) -> Result<()> {
element_stiffness(&geoms[0], material, lay, self.kinematics, self.symmetry, ke)
}
fn element_matrix(
&self,
geoms: &[CellGeom],
material: &SubElementField,
lay: &ElementLayout,
ke: &mut [f64],
) -> Result<()> {
element_stiffness(&geoms[0], material, lay, self.kinematics, self.symmetry, ke)
}
fn element_mass(
&self,
geoms: &[CellGeom],
material: &SubElementField,
lay: &ElementLayout,
ke: &mut [f64],
) -> Result<()> {
element_mass(&geoms[0], material, lay, ke)
}
}
pub fn constitutive(e: f64, nu: f64, kinematics: Kinematics, space_dim: usize) -> Vec<Vec<f64>> {
let mut d = [[0.0_f64; 6]; 6];
let v = constitutive_into(e, nu, kinematics, space_dim, &mut d);
d[..v].iter().map(|r| r[..v].to_vec()).collect()
}
pub fn constitutive_into(
e: f64,
nu: f64,
kinematics: Kinematics,
space_dim: usize,
d: &mut [[f64; 6]; 6],
) -> usize {
match (space_dim, kinematics) {
(2, Kinematics::PlaneStress) => {
let c = e / (1.0 - nu * nu);
d[0][0] = c;
d[0][1] = c * nu;
d[1][0] = c * nu;
d[1][1] = c;
d[2][2] = c * (1.0 - nu) / 2.0;
3
}
(2, Kinematics::PlaneStrain) => {
let c = e / ((1.0 + nu) * (1.0 - 2.0 * nu));
d[0][0] = c * (1.0 - nu);
d[0][1] = c * nu;
d[1][0] = c * nu;
d[1][1] = c * (1.0 - nu);
d[2][2] = c * (1.0 - 2.0 * nu) / 2.0;
3
}
(2, Kinematics::Axisymmetric) => {
let c = e / ((1.0 + nu) * (1.0 - 2.0 * nu));
let (d_n, d_off) = (c * (1.0 - nu), c * nu);
for i in 0..3 {
for j in 0..3 {
d[i][j] = if i == j { d_n } else { d_off };
}
}
d[3][3] = c * (1.0 - 2.0 * nu) / 2.0;
4
}
_ => {
let c = e / ((1.0 + nu) * (1.0 - 2.0 * nu));
let g = c * (1.0 - 2.0 * nu) / 2.0;
for i in 0..3 {
for j in 0..3 {
d[i][j] = if i == j { c * (1.0 - nu) } else { c * nu };
}
}
d[3][3] = g;
d[4][4] = g;
d[5][5] = g;
6
}
}
}
pub(crate) fn strain_reads(space_dim: usize, kinematics: Kinematics) -> Vec<String> {
let names: &[&str] = if space_dim == 2 && kinematics.is_axisymmetric() {
&["eps_xx", "eps_yy", "eps_zz", "eps_xy"]
} else if space_dim == 2 {
&["eps_xx", "eps_yy", "eps_xy"]
} else {
&["eps_xx", "eps_yy", "eps_zz", "eps_yz", "eps_xz", "eps_xy"]
};
owned_components(names)
}
pub(crate) fn normal_count(space_dim: usize, kinematics: Kinematics) -> usize {
if space_dim == 2 && kinematics.is_axisymmetric() {
3
} else if space_dim == 2 {
2
} else {
3
}
}
pub(crate) fn read_voigt_strain(
deformation: &[f64],
idx: &[u32],
space_dim: usize,
kinematics: Kinematics,
) -> [f64; 6] {
let mut eps = [0.0_f64; 6];
let n = normal_count(space_dim, kinematics);
for (r, &i) in idx.iter().enumerate() {
let v = deformation[i as usize];
eps[r] = if r < n { v } else { 2.0 * v };
}
eps
}
fn b_matrix_into(
dn_dx: &[f64],
n_nodes: usize,
space_dim: usize,
hoop: Option<(&[f64], f64)>,
b: &mut VoigtRows,
) -> usize {
let v = match hoop {
Some(_) => 4,
None => voigt_size(space_dim, Kinematics::PlaneStrain),
};
let dofs = space_dim * n_nodes;
for row in b[..v].iter_mut() {
row[..dofs].fill(0.0);
}
let dn = |i: usize, a: usize| dn_dx[i * space_dim + a];
for i in 0..n_nodes {
if let Some((n, r)) = hoop {
let (cr, cz) = (2 * i, 2 * i + 1);
b[0][cr] = dn(i, 0); b[1][cz] = dn(i, 1); b[2][cr] = n[i] / r; b[3][cr] = dn(i, 1); b[3][cz] = dn(i, 0);
} else if space_dim == 2 {
let (cx, cy) = (2 * i, 2 * i + 1);
b[0][cx] = dn(i, 0); b[1][cy] = dn(i, 1); b[2][cx] = dn(i, 1); b[2][cy] = dn(i, 0);
} else {
let (cx, cy, cz) = (3 * i, 3 * i + 1, 3 * i + 2);
b[0][cx] = dn(i, 0); b[1][cy] = dn(i, 1); b[2][cz] = dn(i, 2); b[3][cy] = dn(i, 2); b[3][cz] = dn(i, 1);
b[4][cx] = dn(i, 2); b[4][cz] = dn(i, 0);
b[5][cx] = dn(i, 1); b[5][cy] = dn(i, 0);
}
}
v
}
pub fn element_stiffness(
geom: &CellGeom,
material: &SubElementField,
lay: &ElementLayout,
kinematics: Kinematics,
symmetry: MaterialSymmetry,
ke: &mut [f64],
) -> Result<()> {
let n_nodes = geom.n_nodes;
let space_dim = geom.space_dim;
let dofs = space_dim * n_nodes;
let mut d = [[0.0_f64; 6]; 6];
let v = symmetry::elastic_constitutive_into(
material.row(geom.cell, 0),
&lay.material,
symmetry,
kinematics,
space_dim,
&mut d,
)?;
let mut dn_buf = [0.0_f64; MAX_CELL_DOFS];
let mut b: VoigtRows = [[0.0; MAX_CELL_DOFS]; 6];
let mut db: VoigtRows = [[0.0; MAX_CELL_DOFS]; 6];
for g in 0..geom.n_gauss {
let hoop = if kinematics.is_axisymmetric() {
Some((geom.n_at_g(g), geom.radius(g)))
} else {
None
};
geom.dn_dx(g, &mut dn_buf[..n_nodes * space_dim])?;
b_matrix_into(
&dn_buf[..n_nodes * space_dim],
n_nodes,
space_dim,
hoop,
&mut b,
);
for r in 0..v {
for c in 0..dofs {
let mut acc = 0.0;
for w in 0..v {
acc += d[r][w] * b[w][c];
}
db[r][c] = acc;
}
}
let w = geom.det_j_w(g);
for r in 0..dofs {
for c in 0..dofs {
let mut acc = 0.0;
for vv in 0..v {
acc += b[vv][r] * db[vv][c];
}
ke[r * dofs + c] += acc * w;
}
}
}
Ok(())
}
pub fn element_mass(
geom: &CellGeom,
material: &SubElementField,
lay: &ElementLayout,
ke: &mut [f64],
) -> Result<()> {
let n_nodes = geom.n_nodes;
let space_dim = geom.space_dim;
let dofs = space_dim * n_nodes;
let i_rho = lay.optional_material[RHO_SLOT];
if i_rho == ABSENT_COMPONENT {
return Err(PyrucastError::Message(
"Elasticity mass matrix: material component `rho` (density) is required".into(),
));
}
let rho = material.row(geom.cell, 0)[i_rho as usize];
for g in 0..geom.n_gauss {
let n = geom.n_at_g(g);
let w = geom.det_j_w(g) * rho;
for i in 0..n_nodes {
for j in 0..n_nodes {
let m = n[i] * n[j] * w;
for a in 0..space_dim {
let r = i * space_dim + a;
let c = j * space_dim + a;
ke[r * dofs + c] += m;
}
}
}
}
Ok(())
}
pub fn element_geometric(
geom: &CellGeom,
stress: &SubElementField,
lay: &ElementLayout,
ke: &mut [f64],
) -> Result<()> {
let n_nodes = geom.n_nodes;
let d = geom.space_dim;
let dofs = d * n_nodes;
let lay = &lay.state;
let mut dn_buf = [0.0_f64; MAX_CELL_DOFS];
for g in 0..geom.n_gauss {
let dn = &mut dn_buf[..n_nodes * d]; geom.dn_dx(g, dn)?;
let w = geom.det_j_w(g);
let row = stress.row(geom.cell, g);
let mut sig = [0.0_f64; 9]; crate::models::voigt_stress_matrix(row, lay, d, &mut sig);
let hoop = if geom.axisymmetric {
let r = geom.radius(g);
Some((geom.n_at_g(g), row[lay[lay.len() - 1] as usize] / (r * r)))
} else {
None
};
for i in 0..n_nodes {
for j in 0..n_nodes {
let mut gij = 0.0;
for c in 0..d {
for e in 0..d {
gij += dn[i * d + c] * sig[c * d + e] * dn[j * d + e];
}
}
gij *= w;
for a in 0..d {
ke[(i * d + a) * dofs + (j * d + a)] += gij;
}
if let Some((n, s_hoop)) = hoop {
ke[(i * d) * dofs + (j * d)] += s_hoop * n[i] * n[j] * w;
}
}
}
}
Ok(())
}
pub fn continuum_element_state_reads(
kind: MatrixKind,
space_dim: usize,
kinematics: Kinematics,
) -> Vec<String> {
match kind {
MatrixKind::Geometric => {
let mut names = crate::models::stress_matrix_reads(space_dim);
if kinematics.is_axisymmetric() {
names.push("sigma_zz".to_string());
}
names
}
MatrixKind::Tangent => tangent_component_names(space_dim, kinematics),
MatrixKind::Stiffness | MatrixKind::Mass => Vec::new(),
}
}
pub fn tangent_component_names(space_dim: usize, kinematics: Kinematics) -> Vec<String> {
let v = voigt_size(space_dim, kinematics);
let mut names = Vec::with_capacity(v * (v + 1) / 2);
for i in 0..v {
for j in i..v {
names.push(format!("ktan_{i}_{j}"));
}
}
names
}
pub fn read_tangent_matrix(
state: &SubElementField,
cell: usize,
g: usize,
space_dim: usize,
kinematics: Kinematics,
) -> Result<Vec<Vec<f64>>> {
let v = voigt_size(space_dim, kinematics);
let mut d = vec![vec![0.0; v]; v];
for i in 0..v {
for j in i..v {
let val = state.value(cell, g, &format!("ktan_{i}_{j}"))?;
d[i][j] = val;
d[j][i] = val;
}
}
Ok(d)
}
fn read_tangent_matrix_into(row: &[f64], lay: &[u32], v: usize, d: &mut [[f64; 6]; 6]) {
let mut k = 0;
for i in 0..v {
for j in i..v {
let val = row[lay[k] as usize];
d[i][j] = val;
d[j][i] = val;
k += 1;
}
}
}
pub fn element_tangent_from_state(
geom: &CellGeom,
state: &SubElementField,
lay: &ElementLayout,
kinematics: Kinematics,
ke: &mut [f64],
) -> Result<()> {
let n_nodes = geom.n_nodes;
let space_dim = geom.space_dim;
let dofs = space_dim * n_nodes;
let v = voigt_size(space_dim, kinematics);
let mut dn_buf = [0.0_f64; MAX_CELL_DOFS];
let mut b: VoigtRows = [[0.0; MAX_CELL_DOFS]; 6];
let mut db: VoigtRows = [[0.0; MAX_CELL_DOFS]; 6];
let mut d = [[0.0_f64; 6]; 6];
for g in 0..geom.n_gauss {
let hoop = if kinematics.is_axisymmetric() {
Some((geom.n_at_g(g), geom.radius(g)))
} else {
None
};
geom.dn_dx(g, &mut dn_buf[..n_nodes * space_dim])?;
b_matrix_into(
&dn_buf[..n_nodes * space_dim],
n_nodes,
space_dim,
hoop,
&mut b,
);
read_tangent_matrix_into(state.row(geom.cell, g), &lay.state, v, &mut d);
for r in 0..v {
for c in 0..dofs {
let mut acc = 0.0;
for w in 0..v {
acc += d[r][w] * b[w][c];
}
db[r][c] = acc;
}
}
let w = geom.det_j_w(g);
for r in 0..dofs {
for c in 0..dofs {
let mut acc = 0.0;
for vv in 0..v {
acc += b[vv][r] * db[vv][c];
}
ke[r * dofs + c] += acc * w;
}
}
}
Ok(())
}
pub fn lame(e: f64, nu: f64) -> (f64, f64) {
let lambda = e * nu / ((1.0 + nu) * (1.0 - 2.0 * nu));
let mu = e / (2.0 * (1.0 + nu));
(lambda, mu)
}
pub fn elastic_stress(eps: &[f64; 6], lambda: f64, mu: f64) -> [f64; 6] {
let tr = eps[0] + eps[1] + eps[2];
[
lambda * tr + 2.0 * mu * eps[0],
lambda * tr + 2.0 * mu * eps[1],
lambda * tr + 2.0 * mu * eps[2],
2.0 * mu * eps[3],
2.0 * mu * eps[4],
2.0 * mu * eps[5],
]
}
pub fn elastic_tangent(lambda: f64, mu: f64) -> [[f64; 6]; 6] {
let mut c = [[0.0; 6]; 6];
for i in 0..3 {
for j in 0..3 {
c[i][j] = if i == j { lambda + 2.0 * mu } else { lambda };
}
}
for i in 3..6 {
c[i][i] = mu;
}
c
}
#[cfg(test)]
mod tests {
use super::*;
fn rest<D: Domain>(d: &D, mat: &Handle<SubElementField>) -> Handle<SubElementField> {
Handle::new(d.initial_state(&mat.read()).unwrap())
}
use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, Node, NodeId};
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 unit_quad(kinematics: Kinematics) -> Elasticity {
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(), &[1.0, 1.0]).unwrap();
let d = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::QUA4));
mesh.add_cell(&[a.id(), b.id(), c.id(), d.id()]).unwrap();
let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
Elasticity::new(fes.get(0).unwrap(), kinematics).unwrap()
}
#[test]
fn vars_and_model_validation() {
let el = unit_quad(Kinematics::PlaneStress);
assert_eq!(el.primal_vars(), vec!["u_x", "u_y"]);
assert_eq!(el.dual_vars(), vec!["f_x", "f_y"]);
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();
let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
assert!(Elasticity::new(fes.get(0).unwrap(), Kinematics::Full3D).is_err());
}
#[test]
fn plane_stress_constitutive_known_values() {
let (e, nu) = (1.0, 0.25);
let d = constitutive(e, nu, Kinematics::PlaneStress, 2);
let c = e / (1.0 - nu * nu);
assert!((d[0][0] - c).abs() < 1e-12);
assert!((d[0][1] - c * nu).abs() < 1e-12);
assert!((d[2][2] - c * (1.0 - nu) / 2.0).abs() < 1e-12);
assert!((d[2][2] - e / (2.0 * (1.0 + nu))).abs() < 1e-12); }
#[test]
fn integrate_behavior_plane_stress_uniaxial() {
let (e, nu, eps0) = (210.0, 0.3, 0.001);
let el = unit_quad(Kinematics::PlaneStress);
let mut mat =
SubElementField::new(el.fespace.clone(), vec!["E".into(), "nu".into()]).unwrap();
mat.set_uniform("E", e).unwrap();
mat.set_uniform("nu", nu).unwrap();
let mat = Handle::new(mat);
let mut strain = SubElementField::new(
el.fespace.clone(),
vec!["eps_xx".into(), "eps_xy".into(), "eps_yy".into()],
)
.unwrap();
strain.set_uniform("eps_xx", eps0).unwrap();
let strain = Handle::new(strain);
let out = el
.integrate_behavior(&strain, &rest(&el, &mat), &mat, 0.0)
.unwrap();
let c = e / (1.0 - nu * nu);
for g in 0..out.gauss_count() {
assert!((out.value(0, g, "sigma_xx").unwrap() - c * eps0).abs() < 1e-9);
assert!((out.value(0, g, "sigma_yy").unwrap() - c * nu * eps0).abs() < 1e-9);
assert!(out.value(0, g, "sigma_xy").unwrap().abs() < 1e-9);
}
}
#[test]
fn element_stiffness_symmetric_and_rigid_body_free() {
let el = unit_quad(Kinematics::PlaneStrain);
let mut mat =
SubElementField::new(el.fespace.clone(), vec!["E".into(), "nu".into()]).unwrap();
mat.set_uniform("E", 200.0).unwrap();
mat.set_uniform("nu", 0.3).unwrap();
let mat = Handle::new(mat);
let blocks = el.build_stiffness_blocks(&mat).unwrap();
let k = &blocks[0];
let nodes: Vec<NodeId> = el.support.read().connectivity().to_vec();
let tol = 1e-9;
for &ni in &nodes {
for &nj in &nodes {
for a in ["x", "y"] {
for b in ["x", "y"] {
let lhs = k.get(ni, &format!("f_{a}"), nj, &format!("u_{b}"));
let rhs = k.get(nj, &format!("f_{b}"), ni, &format!("u_{a}"));
assert!((lhs - rhs).abs() < tol);
}
}
}
}
for &ni in &nodes {
let row: f64 = nodes.iter().map(|&nj| k.get(ni, "f_x", nj, "u_x")).sum();
assert!(row.abs() < tol, "row sum {row} ≠ 0");
}
}
}