use crate::containers::element_field::SubElementField;
use crate::containers::field::SubField;
use crate::error::Result;
use crate::models::kernel::MAX_CELL_DOFS;
use crate::models::CellGeom;
const VOIGT_AXES: [&str; 3] = ["x", "y", "z"];
pub(crate) fn continuum_internal_force_element(
geoms: &[CellGeom],
stress: &SubElementField,
lay: &[u32],
fe: &mut [f64],
) -> Result<()> {
tensor_internal_force(geoms, stress, lay, fe, voigt_stress_matrix)
}
pub(crate) fn continuum_internal_force_element_full(
geoms: &[CellGeom],
stress: &SubElementField,
lay: &[u32],
fe: &mut [f64],
) -> Result<()> {
tensor_internal_force(geoms, stress, lay, fe, full_stress_matrix)
}
fn tensor_internal_force(
geoms: &[CellGeom],
stress: &SubElementField,
lay: &[u32],
fe: &mut [f64],
read: impl Fn(&[f64], &[u32], usize, &mut [f64; 9]),
) -> Result<()> {
let geom = &geoms[0];
let d = geom.space_dim;
let n_nodes = geom.n_nodes;
let stride = stress.component_count();
let values = stress.values();
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 start = (geom.cell * geom.n_gauss + g) * stride;
let row = &values[start..start + stride];
let mut sig = [0.0_f64; 9]; read(row, lay, d, &mut sig);
let hoop = if geom.axisymmetric {
Some((
geom.n_at_g(g),
row[lay[lay.len() - 1] as usize] / geom.radius(g),
))
} else {
None
};
for i in 0..n_nodes {
for a in 0..d {
let mut s = 0.0;
for b in 0..d {
s += dn[i * d + b] * sig[a * d + b];
}
fe[i * d + a] += s * w;
}
if let Some((n, s_hoop)) = hoop {
fe[i * d] += n[i] * s_hoop * w;
}
}
}
Ok(())
}
pub(crate) fn voigt_matrix_reads(prefix: &str, space_dim: usize) -> Vec<String> {
let mut names = Vec::with_capacity(space_dim * (space_dim + 1) / 2);
for i in 0..space_dim {
for j in i..space_dim {
names.push(format!("{prefix}_{}{}", VOIGT_AXES[i], VOIGT_AXES[j]));
}
}
names
}
pub(crate) fn voigt_stress_matrix(row: &[f64], idx: &[u32], d: usize, sig: &mut [f64; 9]) {
let mut k = 0;
for i in 0..d {
for j in i..d {
let v = row[idx[k] as usize];
k += 1;
sig[i * d + j] = v;
sig[j * d + i] = v; }
}
}
pub(crate) fn full_matrix_reads(prefix: &str, space_dim: usize) -> Vec<String> {
let mut names = Vec::with_capacity(space_dim * space_dim);
for i in 0..space_dim {
for j in 0..space_dim {
names.push(format!("{prefix}_{}{}", VOIGT_AXES[i], VOIGT_AXES[j]));
}
}
names
}
pub(crate) fn full_stress_matrix(row: &[f64], idx: &[u32], d: usize, sig: &mut [f64; 9]) {
for k in 0..d * d {
sig[k] = row[idx[k] as usize];
}
}