use crate::containers::element_field::SubElementField;
use crate::containers::field::SubField;
use crate::error::Result;
use crate::models::shell::{
accumulate, local_derivatives, local_frame, membrane_and_drilling, to_global,
};
use crate::models::CellGeom;
pub fn membrane_law(e: f64, nu: f64, h: f64) -> [[f64; 3]; 3] {
let c = e * h / (1.0 - nu * nu);
[
[c, c * nu, 0.0],
[c * nu, c, 0.0],
[0.0, 0.0, c * (1.0 - nu) / 2.0],
]
}
pub fn bending_law(e: f64, nu: f64, h: f64) -> [[f64; 3]; 3] {
let m = membrane_law(e, nu, h);
let s = h * h / 12.0;
std::array::from_fn(|i| std::array::from_fn(|j| m[i][j] * s))
}
pub fn shear_law(e: f64, nu: f64, h: f64, k_s: f64) -> f64 {
k_s * e / (2.0 * (1.0 + nu)) * h
}
pub fn shear_factor(material: &SubElementField, cell: usize) -> f64 {
match material.component_index("k_s") {
Some(_) => material.value(cell, 0, "k_s").unwrap_or(5.0 / 6.0),
None => 5.0 / 6.0,
}
}
pub fn element_stiffness(
full: &CellGeom,
reduced: &CellGeom,
material: &SubElementField,
ke: &mut [f64],
) -> Result<()> {
let n = full.n_nodes;
let side = 6 * n;
let cell = full.cell;
let (e, nu, h) = (
material.value(cell, 0, "E")?,
material.value(cell, 0, "nu")?,
material.value(cell, 0, "h")?,
);
let db = bending_law(e, nu, h);
let ds = shear_law(e, nu, h, shear_factor(material, cell));
let frame = local_frame(full)?;
let mut local = vec![vec![0.0_f64; side]; side];
membrane_and_drilling(full, &frame, e, nu, h, &mut local)?;
for g in 0..full.n_gauss {
let dn = local_derivatives(full, &frame, g)?;
let w = full.det_j_w(g)?;
let mut bb = vec![vec![0.0; side]; 3];
for i in 0..n {
let (dx, dy) = (dn[i][0], dn[i][1]);
let (tx, ty) = (6 * i + 3, 6 * i + 4);
bb[0][ty] = dx;
bb[1][tx] = -dy;
bb[2][ty] = dy;
bb[2][tx] = -dx;
}
accumulate(&mut local, &bb, &db, w, side);
}
for g in 0..reduced.n_gauss {
let dn = local_derivatives(reduced, &frame, g)?;
let shape = reduced.n_at_g(g)?;
let w = reduced.det_j_w(g)?;
let mut bs = vec![vec![0.0; side]; 2];
for i in 0..n {
let (dx, dy) = (dn[i][0], dn[i][1]);
let (wz, tx, ty) = (6 * i + 2, 6 * i + 3, 6 * i + 4);
bs[0][wz] = dx;
bs[0][ty] = shape[i];
bs[1][wz] = dy;
bs[1][tx] = -shape[i];
}
for a in 0..side {
for row in bs.iter() {
if row[a] == 0.0 {
continue;
}
for b in 0..side {
local[a][b] += ds * row[a] * row[b] * w;
}
}
}
}
to_global(&local, &frame, n, ke);
Ok(())
}