mod kernels;
mod map;
#[cfg(test)]
mod tests;
use eunomia::{NumericElement, RealField};
use leto::{ArrayView3, ArrayViewMut3, LetoError, Result};
use super::coefficients::{staggered_first_derivative_coefficients, TapCoefficients};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Axis {
X,
Y,
Z,
}
impl Axis {
#[must_use]
#[inline]
pub fn index(self) -> usize {
match self {
Self::X => 0,
Self::Y => 1,
Self::Z => 2,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StaggeredLeapfrog3D<T> {
coefficients: TapCoefficients<T>,
spacing: [T; 3],
inverse_spacing: [T; 3],
}
impl<T: RealField> StaggeredLeapfrog3D<T> {
pub fn new(order: usize, dx: T, dy: T, dz: T) -> Result<Self> {
if order == 0 || !order.is_multiple_of(2) {
return Err(LetoError::InvalidInput(format!(
"StaggeredLeapfrog3D needs an even, non-zero order, got {order}"
)));
}
let spacing = [dx, dy, dz];
require_finite_positive(spacing, "dx, dy, dz")?;
Ok(Self {
coefficients: staggered_first_derivative_coefficients(order / 2)?,
spacing,
inverse_spacing: spacing.map(T::recip),
})
}
pub fn from_parts(coefficients: TapCoefficients<T>, inverse_spacing: [T; 3]) -> Result<Self> {
require_finite_positive(inverse_spacing, "1/dx, 1/dy, 1/dz")?;
Ok(Self {
coefficients,
spacing: inverse_spacing.map(T::recip),
inverse_spacing,
})
}
#[must_use]
#[inline]
pub fn order(&self) -> usize {
self.coefficients.order()
}
#[must_use]
#[inline]
pub fn halo_width(&self) -> usize {
self.coefficients.half_order()
}
#[must_use]
#[inline]
pub fn coefficients(&self) -> &TapCoefficients<T> {
&self.coefficients
}
#[must_use]
#[inline]
pub fn spacing(&self) -> (T, T, T) {
(self.spacing[0], self.spacing[1], self.spacing[2])
}
#[must_use]
pub fn cfl_limit(&self, dimensions: usize) -> T {
let dimensions = T::from_f64(dimensions as f64);
(dimensions.sqrt() * self.coefficients.absolute_sum()).recip()
}
pub fn gradient_into(
&self,
axis: Axis,
field: ArrayView3<'_, T>,
dst: &mut ArrayViewMut3<'_, T>,
) -> Result<()> {
let shape = field.shape();
assert_grid_shaped(dst.shape(), shape, "gradient")?;
kernels::gradient(self, axis, field, dst, shape);
Ok(())
}
pub fn divergence_into(
&self,
axis: Axis,
field: ArrayView3<'_, T>,
dst: &mut ArrayViewMut3<'_, T>,
) -> Result<()> {
let shape = field.shape();
assert_grid_shaped(dst.shape(), shape, "divergence")?;
kernels::divergence(self, axis, field, dst, shape);
Ok(())
}
fn axis_geometry(&self, axis: Axis, shape: [usize; 3]) -> (usize, isize, T) {
let index = axis.index();
let extent = isize::try_from(shape[index]).unwrap_or(isize::MAX);
(index, extent, self.inverse_spacing[index])
}
}
fn require_finite_positive<T: RealField>(values: [T; 3], names: &str) -> Result<()> {
let zero = <T as NumericElement>::ZERO;
if values
.iter()
.all(|&value| value.is_finite() && value > zero)
{
Ok(())
} else {
Err(LetoError::InvalidInput(format!(
"StaggeredLeapfrog3D: {names} must all be finite and positive"
)))
}
}
#[inline]
fn assert_grid_shaped(actual: [usize; 3], expected: [usize; 3], what: &str) -> Result<()> {
if actual != expected {
return Err(LetoError::InvalidInput(format!(
"StaggeredLeapfrog3D {what}: dst shape {actual:?} does not match field shape {expected:?}"
)));
}
Ok(())
}