use crate::aggregate::Aggregate;
use crate::containers::element_field::{ElementField, SubElementField};
use crate::containers::field::Field;
use crate::containers::finite_element_space::{FiniteElementSpace, SubFiniteElementSpace};
use crate::containers::node_field::{NodeField, NodeFieldView};
use crate::coords::Coords;
use crate::error::{PyrucastError, Result};
use crate::store::{insert, read, Handle};
const COMPONENTS_2D: &[&str] = &["eps", "kappa", "gamma"];
const COMPONENTS_3D: &[&str] = &["eps", "kappa_y", "kappa_z", "torsion", "gamma_y", "gamma_z"];
const DOFS_2D: &[&str] = &["u_x", "u_y", "rz"];
const DOFS_3D: &[&str] = &["u_x", "u_y", "u_z", "r_x", "r_y", "r_z"];
pub fn frame_deformation(field: &NodeField, fespace: &FiniteElementSpace) -> Result<ElementField> {
let components = Field::components(field)?;
let view = field.view()?;
let mut out = ElementField::empty();
for sub in fespace {
out.add_sub(insert(subspace_frame_deformation(sub, &view, &components)?))?;
}
Ok(out)
}
fn subspace_frame_deformation(
fespace: &Handle<SubFiniteElementSpace>,
view: &NodeFieldView,
components: &[String],
) -> Result<SubElementField> {
let s = read(fespace)?;
let space_dim = s.space_dim();
let (dofs, out_comps): (&[&str], &[&str]) = match space_dim {
2 => (DOFS_2D, COMPONENTS_2D),
3 => (DOFS_3D, COMPONENTS_3D),
d => {
return Err(PyrucastError::Message(format!(
"frame_deformation: frame element requires a 2-D or 3-D configuration, got {d}-D"
)))
}
};
let n_nodes = s.nodes_per_cell()?;
let n_g = s.gauss_count();
if n_nodes != 2 {
return Err(PyrucastError::Message(format!(
"frame_deformation: frame element must be SEG2 (2 nodes/cell), got {n_nodes}"
)));
}
for required in dofs {
if !components.iter().any(|c| c == required) {
return Err(PyrucastError::Message(format!(
"frame_deformation: the field must carry a '{required}' component (has: [{}])",
components.join(", ")
)));
}
}
let submesh = s.submesh();
let mesh = read(&submesh)?;
let conn = mesh.connectivity();
let coords_h: Handle<Coords> = mesh.coords();
let coords = read(&coords_h)?;
let n_comp = out_comps.len();
let mut field = SubElementField::new(
fespace.clone(),
out_comps.iter().map(|s| s.to_string()).collect(),
)?;
let n_cells = conn.len() / n_nodes;
for cell in 0..n_cells {
let ids = &conn[cell * n_nodes..(cell + 1) * n_nodes];
let xa = coords.position(ids[0])?;
let xb = coords.position(ids[1])?;
let da: Vec<f64> = dofs
.iter()
.map(|c| view.value(ids[0], c))
.collect::<Result<_>>()?;
let db: Vec<f64> = dofs
.iter()
.map(|c| view.value(ids[1], c))
.collect::<Result<_>>()?;
let strains = if space_dim == 2 {
strains_2d(xa, xb, &da, &db)
} else {
strains_3d(xa, xb, &da, &db)
};
for g in 0..n_g {
for (c, &v) in strains.iter().enumerate() {
field.set(cell, g, c, v)?;
}
}
debug_assert_eq!(strains.len(), n_comp);
}
Ok(field)
}
fn strains_2d(xa: &[f64], xb: &[f64], da: &[f64], db: &[f64]) -> Vec<f64> {
let (dx, dy) = (xb[0] - xa[0], xb[1] - xa[1]);
let l = (dx * dx + dy * dy).sqrt();
let (c, s) = (dx / l, dy / l);
let ua = c * da[0] + s * da[1];
let wa = -s * da[0] + c * da[1];
let ub = c * db[0] + s * db[1];
let wb = -s * db[0] + c * db[1];
let (ta, tb) = (da[2], db[2]);
let eps = (ub - ua) / l;
let kappa = (tb - ta) / l;
let gamma = (wb - wa) / l - 0.5 * (ta + tb);
vec![eps, kappa, gamma]
}
fn strains_3d(xa: &[f64], xb: &[f64], da: &[f64], db: &[f64]) -> Vec<f64> {
let d = [xb[0] - xa[0], xb[1] - xa[1], xb[2] - xa[2]];
let l = norm(d);
let r = local_axes(d); let ua = rotate(&r, &da[0..3]);
let ra = rotate(&r, &da[3..6]);
let ub = rotate(&r, &db[0..3]);
let rb = rotate(&r, &db[3..6]);
let eps = (ub[0] - ua[0]) / l; let torsion = (rb[0] - ra[0]) / l; let kappa_y = (rb[1] - ra[1]) / l; let kappa_z = (rb[2] - ra[2]) / l; let gamma_y = (ub[1] - ua[1]) / l - 0.5 * (ra[2] + rb[2]);
let gamma_z = (ub[2] - ua[2]) / l + 0.5 * (ra[1] + rb[1]);
vec![eps, kappa_y, kappa_z, torsion, gamma_y, gamma_z]
}
fn cross(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
}
fn norm(a: [f64; 3]) -> f64 {
(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]).sqrt()
}
fn normalize(a: [f64; 3]) -> [f64; 3] {
let n = norm(a);
[a[0] / n, a[1] / n, a[2] / n]
}
fn local_axes(d: [f64; 3]) -> [[f64; 3]; 3] {
let x = normalize(d);
let z_ref = if x[2].abs() > 0.999 {
[0.0, 1.0, 0.0]
} else {
[0.0, 0.0, 1.0]
};
let y = normalize(cross(z_ref, x));
let z = cross(x, y);
[x, y, z]
}
fn rotate(r: &[[f64; 3]; 3], v: &[f64]) -> [f64; 3] {
[
r[0][0] * v[0] + r[0][1] * v[1] + r[0][2] * v[2],
r[1][0] * v[0] + r[1][1] * v[1] + r[1][2] * v[2],
r[2][0] * v[0] + r[2][1] * v[1] + r[2][2] * v[2],
]
}
#[cfg(test)]
mod tests {
use super::*;
use crate::atoms::{ElementType, Node};
use crate::containers::field::SubField;
use crate::containers::mesh::{Mesh, SubMesh};
use crate::containers::node_field::SubNodeField;
use crate::coords::Coords;
use crate::store::insert;
fn one_element(
space_dim: u8,
a: &[f64],
b: &[f64],
) -> (FiniteElementSpace, Node, Node, Handle<SubMesh>) {
let coords = insert(Coords::new(space_dim).unwrap());
let na = Node::create_in(coords.clone(), a).unwrap();
let nb = Node::create_in(coords.clone(), b).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
mesh.add_cell(&[na.id(), nb.id()]).unwrap();
let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
let support = insert(SubMesh::poi1_from_nodes(&[na.clone(), nb.clone()]).unwrap());
(fes, na, nb, support)
}
#[test]
fn horizontal_2d_strains() {
let l = 2.0;
let (fes, a, b, support) = one_element(2, &[0.0, 0.0], &[l, 0.0]);
let mut f =
SubNodeField::from_poi1(&support, vec!["u_x".into(), "u_y".into(), "rz".into()])
.unwrap();
f.set_value(a.id(), "u_x", 0.0).unwrap();
f.set_value(b.id(), "u_x", 0.5 * l).unwrap();
f.set_value(a.id(), "rz", 0.0).unwrap();
f.set_value(b.id(), "rz", 0.25 * l).unwrap();
let f = NodeField::from_sub(f);
let def = frame_deformation(&f, &fes).unwrap();
let s = read(&def.get(0).unwrap()).unwrap();
assert_eq!(s.components(), &["eps", "kappa", "gamma"]);
for g in 0..s.gauss_count() {
assert!((s.value(0, g, "eps").unwrap() - 0.5).abs() < 1e-12);
assert!((s.value(0, g, "kappa").unwrap() - 0.25).abs() < 1e-12);
assert!((s.value(0, g, "gamma").unwrap() + 0.25).abs() < 1e-12);
}
}
#[test]
fn vertical_2d_strains_are_rotated() {
let l = 2.0;
let (fes, a, b, support) = one_element(2, &[0.0, 0.0], &[0.0, l]);
let mut f =
SubNodeField::from_poi1(&support, vec!["u_x".into(), "u_y".into(), "rz".into()])
.unwrap();
f.set_value(a.id(), "u_y", 0.0).unwrap();
f.set_value(b.id(), "u_y", 0.5 * l).unwrap();
let f = NodeField::from_sub(f);
let def = frame_deformation(&f, &fes).unwrap();
let s = read(&def.get(0).unwrap()).unwrap();
assert!((s.value(0, 0, "eps").unwrap() - 0.5).abs() < 1e-12);
assert!(s.value(0, 0, "kappa").unwrap().abs() < 1e-12);
assert!(s.value(0, 0, "gamma").unwrap().abs() < 1e-12);
}
#[test]
fn horizontal_3d_strains() {
let l = 2.0;
let (fes, a, b, support) = one_element(3, &[0.0, 0.0, 0.0], &[l, 0.0, 0.0]);
let dofs: Vec<String> = DOFS_3D.iter().map(|s| s.to_string()).collect();
let mut f = SubNodeField::from_poi1(&support, dofs).unwrap();
f.set_value(a.id(), "u_x", 0.0).unwrap();
f.set_value(b.id(), "u_x", 0.5 * l).unwrap(); f.set_value(a.id(), "r_x", 0.0).unwrap();
f.set_value(b.id(), "r_x", 0.3 * l).unwrap(); f.set_value(a.id(), "r_z", 0.0).unwrap();
f.set_value(b.id(), "r_z", 0.2 * l).unwrap(); let f = NodeField::from_sub(f);
let def = frame_deformation(&f, &fes).unwrap();
let s = read(&def.get(0).unwrap()).unwrap();
assert_eq!(
s.components(),
&["eps", "kappa_y", "kappa_z", "torsion", "gamma_y", "gamma_z"]
);
assert!((s.value(0, 0, "eps").unwrap() - 0.5).abs() < 1e-12);
assert!((s.value(0, 0, "torsion").unwrap() - 0.3).abs() < 1e-12);
assert!((s.value(0, 0, "kappa_z").unwrap() - 0.2).abs() < 1e-12);
assert!(s.value(0, 0, "kappa_y").unwrap().abs() < 1e-12);
assert!((s.value(0, 0, "gamma_y").unwrap() + 0.2).abs() < 1e-12);
}
#[test]
fn rejects_missing_dof() {
let (fes, _a, _b, support) = one_element(2, &[0.0, 0.0], &[1.0, 0.0]);
let f = NodeField::from_sub(
SubNodeField::from_poi1(&support, vec!["u_x".into(), "u_y".into()]).unwrap(),
);
let err = frame_deformation(&f, &fes).unwrap_err();
assert!(format!("{err}").contains("must carry a 'rz'"));
}
}