use eunomia::{NumericElement, RealField};
use leto::{Array3, ArrayView3, ArrayViewMut3, Result};
use super::kernels::{divergence, divergence_row, gradient, gradient_row, plane_reads};
use super::{assert_grid_shaped, Axis, StaggeredLeapfrog3D};
use crate::infrastructure::parallel::for_each_plane_mut_many_with;
impl<T: RealField> StaggeredLeapfrog3D<T> {
pub fn map_gradient_into<const M: usize, F>(
&self,
axis: Axis,
field: ArrayView3<'_, T>,
pointwise: [ArrayView3<'_, T>; M],
dst: &mut ArrayViewMut3<'_, T>,
combine: F,
) -> Result<()>
where
F: Fn(T, [T; M], T) -> T + Send + Sync,
{
let shape = field.shape();
assert_grid_shaped(dst.shape(), shape, "map_gradient")?;
for values in &pointwise {
assert_grid_shaped(values.shape(), shape, "map_gradient pointwise input")?;
}
let values = pointwise.each_ref().map(|values| values.as_slice());
if let (Some(source), Some(target)) = (field.as_slice(), dst.as_mut_slice()) {
if values.iter().all(Option::is_some) {
let values = values
.map(|values| values.expect("invariant: every input was checked present"));
map_dense(self, axis, source, values, target, shape, &combine);
return Ok(());
}
}
map_logical(self, axis, field, pointwise, dst, shape, &combine);
Ok(())
}
pub fn map_divergence_into<const M: usize, F>(
&self,
fields: [ArrayView3<'_, T>; 3],
pointwise: [ArrayView3<'_, T>; M],
dst: &mut ArrayViewMut3<'_, T>,
combine: F,
) -> Result<()>
where
F: Fn([T; 3], [T; M], T) -> T + Send + Sync,
{
let shape = fields[0].shape();
assert_grid_shaped(dst.shape(), shape, "map_divergence")?;
for field in &fields[1..] {
assert_grid_shaped(field.shape(), shape, "map_divergence field")?;
}
for values in &pointwise {
assert_grid_shaped(values.shape(), shape, "map_divergence pointwise input")?;
}
let sources = fields.each_ref().map(|field| field.as_slice());
let values = pointwise.each_ref().map(|values| values.as_slice());
if let Some(target) = dst.as_mut_slice() {
if sources.iter().chain(&values).all(Option::is_some) {
let sources = sources
.map(|source| source.expect("invariant: every field was checked present"));
let values = values
.map(|values| values.expect("invariant: every input was checked present"));
map_divergence_dense(self, sources, values, target, shape, &combine);
return Ok(());
}
}
map_divergence_logical(self, fields, pointwise, dst, shape, &combine);
Ok(())
}
}
const COMPONENT_AXES: [Axis; 3] = [Axis::X, Axis::Y, Axis::Z];
fn map_divergence_dense<T, const M: usize, F>(
op: &StaggeredLeapfrog3D<T>,
sources: [&[T]; 3],
values: [&[T]; M],
target: &mut [T],
shape: [usize; 3],
combine: &F,
) where
T: RealField,
F: Fn([T; 3], [T; M], T) -> T + Send + Sync,
{
let [_, ny, nz] = shape;
let plane = ny * nz;
if plane == 0 || target.is_empty() {
return;
}
let reads: usize = COMPONENT_AXES
.iter()
.map(|&axis| plane_reads(op, axis.index()))
.sum();
let element_bytes = (reads + 2 + M) * size_of::<T>();
for_each_plane_mut_many_with(
[target],
plane,
element_bytes,
|| vec![<T as NumericElement>::ZERO; 3 * nz],
|divergences, x, [out]| {
for (y, out) in out.chunks_exact_mut(nz).enumerate() {
for ((axis, source), row) in COMPONENT_AXES
.into_iter()
.zip(sources)
.zip(divergences.chunks_exact_mut(nz))
{
divergence_row(op, axis, source, row, [x, y], shape);
}
let (dx, rest) = divergences.split_at(nz);
let (dy, dz) = rest.split_at(nz);
let start = (x * ny + y) * nz;
let values: [&[T]; M] = core::array::from_fn(|k| &values[k][start..start + nz]);
for (i, held) in out.iter_mut().enumerate() {
*held = combine(
[dx[i], dy[i], dz[i]],
core::array::from_fn(|k| values[k][i]),
*held,
);
}
}
},
);
}
fn map_divergence_logical<T, const M: usize, F>(
op: &StaggeredLeapfrog3D<T>,
fields: [ArrayView3<'_, T>; 3],
pointwise: [ArrayView3<'_, T>; M],
dst: &mut ArrayViewMut3<'_, T>,
shape: [usize; 3],
combine: &F,
) where
T: RealField,
F: Fn([T; 3], [T; M], T) -> T + Send + Sync,
{
let swept = COMPONENT_AXES.map(|axis| {
let mut swept = Array3::from_elem(shape, <T as NumericElement>::ZERO);
divergence(op, axis, fields[axis.index()], &mut swept.view_mut(), shape);
swept
});
for i in 0..shape[0] {
for j in 0..shape[1] {
for k in 0..shape[2] {
let at = [i, j, k];
dst[at] = combine(
swept.each_ref().map(|swept| swept[at]),
pointwise.each_ref().map(|values| values[at]),
dst[at],
);
}
}
}
}
fn map_dense<T, const M: usize, F>(
op: &StaggeredLeapfrog3D<T>,
axis: Axis,
source: &[T],
values: [&[T]; M],
target: &mut [T],
shape: [usize; 3],
combine: &F,
) where
T: RealField,
F: Fn(T, [T; M], T) -> T + Send + Sync,
{
let [_, ny, nz] = shape;
let plane = ny * nz;
if plane == 0 || source.is_empty() {
return;
}
let element_bytes = (plane_reads(op, axis.index()) + 2 + M) * size_of::<T>();
for_each_plane_mut_many_with(
[target],
plane,
element_bytes,
|| vec![<T as NumericElement>::ZERO; nz],
|gradient, x, [out]| {
for (y, out) in out.chunks_exact_mut(nz).enumerate() {
gradient_row(op, axis, source, gradient, [x, y], shape);
let start = (x * ny + y) * nz;
let values: [&[T]; M] = core::array::from_fn(|k| &values[k][start..start + nz]);
for (i, (held, &g)) in out.iter_mut().zip(gradient.iter()).enumerate() {
*held = combine(g, core::array::from_fn(|k| values[k][i]), *held);
}
}
},
);
}
fn map_logical<T, const M: usize, F>(
op: &StaggeredLeapfrog3D<T>,
axis: Axis,
field: ArrayView3<'_, T>,
pointwise: [ArrayView3<'_, T>; M],
dst: &mut ArrayViewMut3<'_, T>,
shape: [usize; 3],
combine: &F,
) where
T: RealField,
F: Fn(T, [T; M], T) -> T + Send + Sync,
{
let mut swept = Array3::from_elem(shape, <T as NumericElement>::ZERO);
gradient(op, axis, field, &mut swept.view_mut(), shape);
for i in 0..shape[0] {
for j in 0..shape[1] {
for k in 0..shape[2] {
let at = [i, j, k];
dst[at] = combine(
swept[at],
pointwise.each_ref().map(|values| values[at]),
dst[at],
);
}
}
}
}
#[cfg(test)]
mod tests;