pub mod elastic;
pub(crate) mod finite_strain;
pub mod internal_force;
pub mod material;
pub mod voigt;
use crate::containers::element_field::SubElementField;
use crate::containers::field::ABSENT_COMPONENT;
use crate::containers::finite_element_space::SubFiniteElementSpace;
use crate::containers::mesh::SubMesh;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::models::kernel::MAX_CELL_DOFS;
use crate::models::symmetry::{self, MaterialSymmetry};
use crate::models::tensor::{voigt_names, Kinematics};
use crate::models::{CellGeom, ElementLayout, MatrixKind};
use crate::ops::element_field::StrainMeasure;
use elastic::RHO_SLOT;
use voigt::{b_matrix_into, voigt_size, VoigtRows};
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, serde::Serialize, serde::Deserialize)]
pub struct Continuum {
pub(crate) fespace: Handle<SubFiniteElementSpace>,
pub(crate) support: Handle<SubMesh>,
pub(crate) space_dim: usize,
pub(crate) kinematics: Kinematics,
pub(crate) measure: StrainMeasure,
}
impl Continuum {
pub fn new(
fespace: Handle<SubFiniteElementSpace>,
kinematics: Kinematics,
label: &str,
) -> 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(label, 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!(
"{label}: 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!(
"{label}: kinematics {kinematics:?} on an axisymmetric geometry — a body of \
revolution requires the `axisymmetric` kinematics (its integrals already \
carry the 2πr factor)"
)
} else {
format!(
"{label}: the `axisymmetric` kinematics requires an axisymmetric geometry \
(build the Coords with Coords::axisymmetric)"
)
}));
}
let support = submesh.read().to_poi1()?;
Ok(Self {
fespace,
support,
space_dim,
kinematics,
measure: StrainMeasure::Linear,
})
}
pub fn with_measure(self, measure: StrainMeasure, label: &str) -> Result<Self> {
match measure {
StrainMeasure::Linear | StrainMeasure::GreenLagrange => {}
StrainMeasure::Hencky if self.kinematics == Kinematics::PlaneStress => {
return Err(PyrucastError::Message(format!(
"{label}: the Hencky measure ½ ln C needs C_zz, which plane stress leaves \
to the law — use plane_strain, or green_lagrange under plane stress"
)));
}
StrainMeasure::Hencky => {}
other => {
return Err(PyrucastError::Message(format!(
"{label}: `{}` is not a strain a law reads — the measures are `linear`, \
`green_lagrange` and `hencky`",
crate::named::Named::name(other)
)));
}
}
Ok(Self { measure, ..self })
}
#[inline]
pub fn measure(&self) -> StrainMeasure {
self.measure
}
pub(crate) fn stress_prefix(&self) -> &'static str {
match self.measure {
StrainMeasure::GreenLagrange => "S",
StrainMeasure::Hencky => "T",
_ => "sigma",
}
}
pub fn fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
pub fn support(&self) -> Handle<SubMesh> {
self.support.clone()
}
#[inline]
pub fn space_dim(&self) -> usize {
self.space_dim
}
#[inline]
pub fn kinematics(&self) -> Kinematics {
self.kinematics
}
pub fn strain_reads(&self) -> Vec<String> {
voigt::strain_reads(self.measure.prefix(), self.space_dim, self.kinematics)
}
pub fn stress_names(&self) -> Vec<String> {
voigt_names(self.stress_prefix(), self.space_dim, self.kinematics)
}
pub fn element_state_reads(&self, kind: MatrixKind) -> Vec<String> {
match kind {
MatrixKind::Geometric => {
let mut names = internal_force::voigt_matrix_reads("sigma", self.space_dim);
if self.kinematics.is_axisymmetric() {
names.push("sigma_zz".to_string());
}
names
}
MatrixKind::Stiffness | MatrixKind::Mass | MatrixKind::Tangent => Vec::new(),
}
}
pub fn element_stiffness(
&self,
geom: &CellGeom,
material: &SubElementField,
lay: &ElementLayout,
symmetry: MaterialSymmetry,
ke: &mut [f64],
) -> Result<()> {
self.element_tangent_with(
geom,
|g, d| {
symmetry::elastic_constitutive_into(
material.row(geom.cell, g),
&lay.material,
symmetry,
self.kinematics,
geom.space_dim,
d,
)?;
Ok(())
},
ke,
)
}
pub fn element_mass(
&self,
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(
"Continuum mass matrix: material component `rho` (density) is required".into(),
));
}
for g in 0..geom.n_gauss {
let n = geom.n_at_g(g);
let w = geom.det_j_w(g) * material.row(geom.cell, g)[i_rho as usize];
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(
&self,
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]; internal_force::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(())
}
#[allow(clippy::too_many_arguments)]
pub fn element_tangent_of<B: crate::models::Behavior + ?Sized>(
&self,
behavior: &B,
geoms: &[CellGeom],
lay: &crate::models::ZoneLayout,
deformation: &SubElementField,
prev: &SubElementField,
material: &SubElementField,
dt: f64,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
self.element_tangent_with(
geom,
|g, d| {
behavior.tangent_point(
geom,
g,
lay,
deformation.row(geom.cell, g),
prev.row(geom.cell, g),
material.row(geom.cell, g),
dt,
d,
)
},
ke,
)
}
pub(crate) fn element_tangent_with<F>(
&self,
geom: &CellGeom,
mut d_at: F,
ke: &mut [f64],
) -> Result<()>
where
F: FnMut(usize, &mut [[f64; 6]; 6]) -> 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, self.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 self.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,
);
d_at(g, &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(())
}
}