use pyrucast::aggregate::Aggregate;
use pyrucast::atoms::{ElementType, Node};
use pyrucast::containers::finite_element_space::FiniteElementSpace;
use pyrucast::containers::mesh::{Mesh, SubMesh};
use pyrucast::containers::model::Model;
use pyrucast::coords::Coords;
use pyrucast::handle::Handle;
use pyrucast::models::symmetry::MaterialSymmetry;
use pyrucast::models::tensor::Kinematics;
use pyrucast::models::Physics;
use pyrucast::ops::element_field::StrainMeasure;
use pyrucast::ops::model;
use pyrucast::ops::solver::solve;
use pyrucast::Result;
const E: f64 = 210_000.0;
const NU: f64 = 0.0;
const L: f64 = 1.0;
const Q: f64 = 100.0;
#[test]
fn a_boundary_exchange_on_displacements_is_an_elastic_foundation() -> Result<()> {
for h in [0.0, 1e3, 1e4, 1e5, 1e6] {
let u = free_face_displacement(4, h)?;
let exact = Q / (E / L + h);
assert!(
(u - exact).abs() < 1e-9 * exact.abs().max(1e-12),
"h = {h}: face displacement {u}, exact {exact}"
);
}
Ok(())
}
#[test]
fn the_foundation_interpolates_between_a_free_and_a_held_face() -> Result<()> {
let free = free_face_displacement(4, 0.0)?;
assert!(
(free - Q * L / E).abs() < 1e-9 * free,
"with no foundation the bar is free: {free}"
);
let held = free_face_displacement(4, 1e12)?;
assert!(
held < 1e-6 * free,
"a very stiff foundation must hold the face: {held} against {free}"
);
Ok(())
}
#[test]
fn each_direction_carries_its_own_stiffness() -> Result<()> {
let (n, h_x, h_y) = (4, 1e4, 1e6);
let (grid, fes, coords, _) = square(n)?;
let idx = |i: usize, j: usize| j * (n + 1) + i;
let right = edge_fespace(&grid, &coords, n, idx)?;
let elastic = model::elasticity(
&fes,
Kinematics::PlaneStress,
MaterialSymmetry::Isotropic,
StrainMeasure::Linear,
)?;
let mut model = elastic.union(&model::boundary_transfer(
&right,
&elastic,
vec!["u_x".into(), "u_y".into()],
)?)?;
let left: Vec<Node> = (0..=n).map(|j| grid[idx(0, j)].clone()).collect();
for var in ["u_x", "u_y"] {
model = model.union(&clamp(&model, &left, var)?)?;
}
for dual in ["f_x", "f_y"] {
model = model.union(&model::flux(&right, &model, dual)?)?;
}
let materials = pyrucast::ops::element_field::material_field(
&model,
&[
("E", E),
("nu", NU),
("h_u_x", h_x),
("h_u_y", h_y),
("a_ext_u_x", 0.0),
("a_ext_u_y", 0.0),
("phi_f_x", Q),
("phi_f_y", Q),
],
)?;
let load = pyrucast::ops::node_field::external_forces(&model, &materials)?;
let k = pyrucast::ops::matrix::stiffness(&model, &materials)?;
let solution = solve(&k, &load)?;
let u_x = solution.value(grid[idx(n, n / 2)].id(), "u_x")?;
let u_y = solution.value(grid[idx(n, n / 2)].id(), "u_y")?;
assert!(
(u_x - Q / (E / L + h_x)).abs() < 1e-9 * u_x,
"along the bar: {u_x}"
);
assert!(
u_y < u_x,
"the stiffer direction must move less: u_y = {u_y}, u_x = {u_x}"
);
Ok(())
}
#[test]
fn the_foundation_takes_its_nature_from_the_elasticity() -> Result<()> {
let n = 2;
let (grid, fes, coords, _) = square(n)?;
let idx = |i: usize, j: usize| j * (n + 1) + i;
let right = edge_fespace(&grid, &coords, n, idx)?;
let elastic = model::elasticity(
&fes,
Kinematics::PlaneStress,
MaterialSymmetry::Isotropic,
StrainMeasure::Linear,
)?;
let appui = model::boundary_transfer(&right, &elastic, vec!["u_x".into()])?;
assert_eq!(appui.filter(Physics::Mechanical).len(), 1);
assert!(appui.filter(Physics::Thermal).is_empty());
assert_eq!(appui.dual_vars(), vec!["f_x".to_string()]);
let conduction = model::heat_conduction(&fes, MaterialSymmetry::Isotropic)?;
let err = model::boundary_transfer(&right, &conduction, vec!["u_x".into()])
.unwrap_err()
.to_string();
assert!(err.contains("`u_x` is not an unknown"), "unexpected: {err}");
let both = elastic.union(&conduction)?;
let err = model::boundary_transfer(&right, &both, vec!["u_x".into(), "T".into()])
.unwrap_err()
.to_string();
assert!(err.contains("single nature"), "unexpected: {err}");
Ok(())
}
#[allow(clippy::type_complexity)]
fn square(
n: usize,
) -> Result<(
Vec<Node>,
FiniteElementSpace,
pyrucast::handle::Handle<Coords>,
usize,
)> {
let coords = Handle::new(Coords::new(2)?);
let step = L / n as f64;
let mut grid = Vec::new();
for j in 0..=n {
for i in 0..=n {
grid.push(Node::create_in(
coords.clone(),
&[i as f64 * step, j as f64 * step],
)?);
}
}
let idx = |i: usize, j: usize| j * (n + 1) + i;
let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::QUA4));
for j in 0..n {
for i in 0..n {
mesh.add_cell(&[
grid[idx(i, j)].id(),
grid[idx(i + 1, j)].id(),
grid[idx(i + 1, j + 1)].id(),
grid[idx(i, j + 1)].id(),
])?;
}
}
let fes = FiniteElementSpace::lagrange1(&mesh)?;
Ok((grid, fes, coords, n))
}
fn edge_fespace(
grid: &[Node],
coords: &pyrucast::handle::Handle<Coords>,
n: usize,
idx: impl Fn(usize, usize) -> usize,
) -> Result<FiniteElementSpace> {
let mut edge = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
for j in 0..n {
edge.add_cell(&[grid[idx(n, j)].id(), grid[idx(n, j + 1)].id()])?;
}
FiniteElementSpace::lagrange1(&edge)
}
fn clamp(target: &Model, nodes: &[Node], var: &str) -> Result<Model> {
let imposed = Mesh::from_submesh(SubMesh::poi1_from_nodes(nodes)?);
model::dirichlet(&imposed, target, var, Default::default())
}
fn free_face_displacement(n: usize, h: f64) -> Result<f64> {
let (grid, fes, coords, _) = square(n)?;
let idx = |i: usize, j: usize| j * (n + 1) + i;
let right = edge_fespace(&grid, &coords, n, idx)?;
let elastic = model::elasticity(
&fes,
Kinematics::PlaneStress,
MaterialSymmetry::Isotropic,
StrainMeasure::Linear,
)?;
let mut model = elastic.union(&model::boundary_transfer(
&right,
&elastic,
vec!["u_x".into()],
)?)?;
let left: Vec<Node> = (0..=n).map(|j| grid[idx(0, j)].clone()).collect();
model = model.union(&clamp(&model, &left, "u_x")?)?;
let bottom: Vec<Node> = (0..=n).map(|i| grid[idx(i, 0)].clone()).collect();
model = model.union(&clamp(&model, &bottom, "u_y")?)?;
model = model.union(&model::flux(&right, &model, "f_x")?)?;
let materials = pyrucast::ops::element_field::material_field(
&model,
&[
("E", E),
("nu", NU),
("h_u_x", h),
("a_ext_u_x", 0.0),
("phi_f_x", Q),
],
)?;
let traction = pyrucast::ops::node_field::external_forces(&model, &materials)?;
let k = pyrucast::ops::matrix::stiffness(&model, &materials)?;
let solution = solve(&k, &traction)?;
solution.value(grid[idx(n, n / 2)].id(), "u_x")
}