use crate::atoms::ElementType;
use crate::error::{PyrucastError, Result};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub enum QuadratureRule {
Gauss,
Reduced,
}
impl QuadratureRule {
pub fn name(self) -> &'static str {
match self {
Self::Gauss => "GAUSS",
Self::Reduced => "REDUCED",
}
}
pub fn from_name(s: &str) -> Option<Self> {
match s.to_ascii_uppercase().as_str() {
"GAUSS" => Some(Self::Gauss),
"REDUCED" => Some(Self::Reduced),
_ => None,
}
}
pub fn is_compatible_with(self, element_type: ElementType) -> bool {
element_type.topological_dim() > 0
}
pub fn point_count(self, element_type: ElementType) -> Result<usize> {
Ok(self.points(element_type)?.1.len())
}
pub fn points(self, element_type: ElementType) -> Result<(Vec<f64>, Vec<f64>)> {
self.check_compat(element_type)?;
let k = element_type.as_kind();
Ok(match self {
Self::Gauss => k.gauss(),
Self::Reduced => k.reduced(),
})
}
fn check_compat(self, element_type: ElementType) -> Result<()> {
if !self.is_compatible_with(element_type) {
return Err(PyrucastError::Message(format!(
"quadrature {} is not defined for {}",
self, element_type
)));
}
Ok(())
}
}
impl fmt::Display for QuadratureRule {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
impl crate::dump::Dump for QuadratureRule {
fn render(&self, _opts: &crate::dump::DumpOptions) -> String {
self.to_string()
}
}
pub(super) fn gauss3_1d() -> ([f64; 3], [f64; 3]) {
let a = (3.0_f64 / 5.0).sqrt();
([-a, 0.0, a], [5.0 / 9.0, 8.0 / 9.0, 5.0 / 9.0])
}
pub(super) fn tri6_gauss() -> (Vec<f64>, Vec<f64>) {
let b1 = 0.445_948_490_915_965;
let b2 = 0.091_576_213_509_771;
let w1 = 0.223_381_589_678_011 / 2.0;
let w2 = 0.109_951_743_655_322 / 2.0;
let orbit = |b: f64| {
let a = 1.0 - 2.0 * b;
[(b, b), (a, b), (b, a)]
};
let mut xi = Vec::with_capacity(6 * 2);
let mut w = Vec::with_capacity(6);
for (b, weight) in [(b1, w1), (b2, w2)] {
for (x, y) in orbit(b) {
xi.push(x);
xi.push(y);
w.push(weight);
}
}
(xi, w)
}
#[cfg(test)]
mod tests {
use super::*;
fn all_fe_types() -> impl Iterator<Item = ElementType> {
ElementType::ALL
.iter()
.copied()
.filter(|et| et.topological_dim() > 0)
}
#[test]
fn weights_sum_to_reference_volume() {
for et in all_fe_types() {
let (_xi, w) = QuadratureRule::Gauss.points(et).unwrap();
let s: f64 = w.iter().sum();
let expected = et.as_kind().ref_measure();
assert!(
(s - expected).abs() < 1e-12,
"{}: weights sum = {} ≠ {}",
et,
s,
expected
);
}
}
#[test]
fn buffer_layouts() {
for et in all_fe_types() {
let n_g = QuadratureRule::Gauss.point_count(et).unwrap();
let (xi, w) = QuadratureRule::Gauss.points(et).unwrap();
assert_eq!(w.len(), n_g);
assert_eq!(xi.len(), n_g * et.topological_dim());
}
}
#[test]
fn gauss_seg2_exact_up_to_degree_3() {
let (xi, w) = QuadratureRule::Gauss.points(ElementType::SEG2).unwrap();
for p in 0..=3 {
let mut sum = 0.0;
for g in 0..w.len() {
sum += w[g] * xi[g].powi(p);
}
let expected = if p % 2 == 0 {
2.0 / (p + 1) as f64
} else {
0.0
};
assert!(
(sum - expected).abs() < 1e-12,
"SEG2 Gauss not exact for ξ^{}: got {}, expected {}",
p,
sum,
expected
);
}
}
#[test]
fn gauss_tri3_exact_for_degree_2() {
let (xi, w) = QuadratureRule::Gauss.points(ElementType::TRI3).unwrap();
let mut sum = 0.0;
for g in 0..w.len() {
let a = xi[g * 2];
sum += w[g] * a * a;
}
let expected = 1.0 / 12.0;
assert!(
(sum - expected).abs() < 1e-12,
"TRI3 Gauss: ∫ξ² = {} ≠ {}",
sum,
expected
);
}
fn fact(n: u32) -> f64 {
(1..=n).map(|k| k as f64).product::<f64>().max(1.0)
}
#[test]
fn gauss_tri6_exact_up_to_degree_4() {
let (xi, w) = QuadratureRule::Gauss.points(ElementType::TRI6).unwrap();
for p in 0..=4u32 {
for q in 0..=(4 - p) {
let mut sum = 0.0;
for g in 0..w.len() {
sum += w[g] * xi[2 * g].powi(p as i32) * xi[2 * g + 1].powi(q as i32);
}
let expected = fact(p) * fact(q) / fact(p + q + 2);
assert!(
(sum - expected).abs() < 1e-12,
"TRI6: ∫ξ^{p}η^{q} = {sum} ≠ {expected}"
);
}
}
}
#[test]
fn gauss_tet10_exact_up_to_degree_4() {
let (xi, w) = QuadratureRule::Gauss.points(ElementType::TET10).unwrap();
for p in 0..=4u32 {
for q in 0..=(4 - p) {
for r in 0..=(4 - p - q) {
let mut sum = 0.0;
for g in 0..w.len() {
sum += w[g]
* xi[3 * g].powi(p as i32)
* xi[3 * g + 1].powi(q as i32)
* xi[3 * g + 2].powi(r as i32);
}
let expected = fact(p) * fact(q) * fact(r) / fact(p + q + r + 3);
assert!(
(sum - expected).abs() < 1e-12,
"TET10: ∫ξ^{p}η^{q}ζ^{r} = {sum} ≠ {expected}"
);
}
}
}
}
#[test]
fn rejects_poi1() {
assert!(!QuadratureRule::Gauss.is_compatible_with(ElementType::POI1));
assert!(QuadratureRule::Gauss.points(ElementType::POI1).is_err());
assert!(QuadratureRule::Gauss
.point_count(ElementType::POI1)
.is_err());
}
#[test]
fn display_and_parsing() {
assert_eq!(format!("{}", QuadratureRule::Gauss), "GAUSS");
assert_eq!(
QuadratureRule::from_name("gauss"),
Some(QuadratureRule::Gauss)
);
assert_eq!(QuadratureRule::from_name("unknown"), None);
}
}