use super::{ElementKind, Facet, Interpolation};
use crate::atoms::ElementType;
pub struct Pyra5;
const BASE: [(f64, f64); 4] = [(-1.0, -1.0), (1.0, -1.0), (1.0, 1.0), (-1.0, 1.0)];
const APEX_EPS: f64 = 1e-12;
const EDGES: &[[usize; 2]] = &[
[0, 1],
[1, 2],
[2, 3],
[3, 0],
[0, 4],
[1, 4],
[2, 4],
[3, 4],
];
pub(super) const CORNER_FACES: [&[usize]; 5] = [
&[0, 3, 2, 1],
&[0, 1, 4],
&[1, 2, 4],
&[2, 3, 4],
&[3, 0, 4],
];
const FACETS: &[Facet] = &[
Facet {
element_type: ElementType::QUA4,
nodes: CORNER_FACES[0],
},
Facet {
element_type: ElementType::TRI3,
nodes: CORNER_FACES[1],
},
Facet {
element_type: ElementType::TRI3,
nodes: CORNER_FACES[2],
},
Facet {
element_type: ElementType::TRI3,
nodes: CORNER_FACES[3],
},
Facet {
element_type: ElementType::TRI3,
nodes: CORNER_FACES[4],
},
];
impl ElementKind for Pyra5 {
fn element_type(&self) -> ElementType {
ElementType::PYRA5
}
fn ref_nodes(&self) -> &'static [&'static [f64]] {
&[
&[-1.0, -1.0, 0.0],
&[1.0, -1.0, 0.0],
&[1.0, 1.0, 0.0],
&[-1.0, 1.0, 0.0],
&[0.0, 0.0, 1.0],
]
}
fn reversal_permutation(&self) -> &'static [usize] {
&[0, 3, 2, 1, 4]
}
fn corner_count(&self) -> usize {
5
}
fn facets(&self) -> &'static [Facet] {
FACETS
}
fn edges(&self) -> &'static [[usize; 2]] {
EDGES
}
fn ref_centroid(&self) -> &'static [f64] {
&[0.0, 0.0, 0.25]
}
fn ref_measure(&self) -> f64 {
4.0 / 3.0
}
fn contains_ref(&self, xi: &[f64], tol: f64) -> bool {
let (a, b, c) = (xi[0], xi[1], xi[2]);
let half = 1.0 - c + tol;
c >= -tol && c <= 1.0 + tol && a.abs() <= half && b.abs() <= half
}
fn clamp_ref(&self, _xi: &mut [f64]) {}
fn degree(&self) -> Option<Interpolation> {
Some(Interpolation::Lagrange1)
}
fn shape_into(&self, xi: &[f64], out: &mut [f64]) {
let (a, b, c) = (xi[0], xi[1], xi[2]);
let m = 1.0 - c;
if m <= APEX_EPS {
out.copy_from_slice(&[0.0, 0.0, 0.0, 0.0, 1.0]);
return;
}
let (u, v) = (a / m, b / m);
for (i, &(si, ti)) in BASE.iter().enumerate() {
out[i] = 0.25 * m * (1.0 + si * u) * (1.0 + ti * v);
}
out[4] = c;
}
fn dshape_into(&self, xi: &[f64], out: &mut [f64]) {
let (a, b, c) = (xi[0], xi[1], xi[2]);
let m = 1.0 - c;
if m <= APEX_EPS {
out.copy_from_slice(&[
0.0, 0.0, -0.25, 0.0, 0.0, -0.25, 0.0, 0.0, -0.25, 0.0, 0.0, -0.25, 0.0, 0.0, 1.0,
]);
return;
}
let (u, v) = (a / m, b / m);
for (i, &(si, ti)) in BASE.iter().enumerate() {
out[3 * i] = 0.25 * si * (1.0 + ti * v);
out[3 * i + 1] = 0.25 * ti * (1.0 + si * u);
out[3 * i + 2] = 0.25 * (-1.0 + si * ti * u * v);
}
out[12] = 0.0;
out[13] = 0.0;
out[14] = 1.0;
}
fn gauss(&self) -> (Vec<f64>, Vec<f64>) {
let a = 1.0 / 3.0_f64.sqrt();
let spread = 10.0_f64.sqrt() / 15.0;
let (z0, z1) = (1.0 / 3.0 - spread, 1.0 / 3.0 + spread);
let w1 = (1.0 / 12.0 - z0 / 3.0) / (z1 - z0);
let w0 = 1.0 / 3.0 - w1;
let mut xi = Vec::with_capacity(8 * 3);
let mut w = Vec::with_capacity(8);
for &sa in &[-a, a] {
for &sb in &[-a, a] {
for &(z, wz) in &[(z0, w0), (z1, w1)] {
xi.push(sa * (1.0 - z));
xi.push(sb * (1.0 - z));
xi.push(z);
w.push(wz);
}
}
}
(xi, w)
}
fn vtk_code(&self) -> u8 {
14 }
fn gmsh_code(&self) -> u32 {
7
}
fn med_permutation(&self) -> &'static [usize] {
&[0, 3, 2, 1, 4]
}
}