move the dimensioned operations out of the sim adapters and into step.rs

This commit is contained in:
2022-07-26 18:43:41 -07:00
parent 7d16e87b6e
commit f6a585852e
3 changed files with 85 additions and 100 deletions

View File

@@ -1,7 +1,7 @@
use coremem_types::dim::DimensionedSlice;
use coremem_types::mat::Material;
use coremem_types::real::Real;
use coremem_types::step::{SimMeta, StepEContext, StepHContext, VolumeSampleNeg, VolumeSamplePos};
use coremem_types::step::{SimMeta, StepEContext, StepHContext};
use coremem_types::vec::{Vec3, Vec3u};
use super::SimBackend;
@@ -64,27 +64,7 @@ fn step_e_cell<R: Real, M: Material<R>>(
h: &[Vec3<R>],
m: &[Vec3<R>],
) {
let dim = meta.dim;
let stim_e_matrix = DimensionedSlice::new(dim, stim_e);
let mat_matrix = DimensionedSlice::new(dim, mat);
let mut e_matrix = DimensionedSlice::new(dim, e);
let h_matrix = DimensionedSlice::new(dim, h);
let stim_e = stim_e_matrix[idx];
let mat = &mat_matrix[idx];
let in_e = e_matrix[idx];
let in_h = VolumeSampleNeg::from_indexable(&h_matrix, idx);
let update_state = StepEContext {
inv_feature_size: meta.inv_feature_size,
time_step: meta.time_step,
stim_e,
mat,
in_h,
in_e,
};
let new_e = update_state.step_e();
e_matrix[idx] = new_e;
StepEContext::step_flat_view(meta, mat, stim_e, e, h, idx);
}
fn step_h_cell<R: Real, M: Material<R>>(
idx: Vec3u,
@@ -95,32 +75,7 @@ fn step_h_cell<R: Real, M: Material<R>>(
h: &mut [Vec3<R>],
m: &mut [Vec3<R>],
) {
let dim = meta.dim;
let stim_h_matrix = DimensionedSlice::new(dim, stim_h);
let mat_matrix = DimensionedSlice::new(dim, mat);
let e_matrix = DimensionedSlice::new(dim, e);
let mut h_matrix = DimensionedSlice::new(dim, h);
let mut m_matrix = DimensionedSlice::new(dim, m);
let stim_h = stim_h_matrix[idx];
let mat = &mat_matrix[idx];
let in_e = VolumeSamplePos::from_indexable(&e_matrix, dim, idx);
let in_h = h_matrix[idx];
let in_m = m_matrix[idx];
let update_state = StepHContext {
inv_feature_size: meta.inv_feature_size,
time_step: meta.time_step,
stim_h,
mat,
in_e,
in_h,
in_m,
};
let (new_h, new_m) = update_state.step_h();
h_matrix[idx] = new_h;
m_matrix[idx] = new_m;
StepHContext::step_flat_view(meta, mat, stim_h, e, h, m, idx);
}
fn apply_all_cells<F: FnMut(Vec3u)>(dim: Vec3u, mut f: F) {

View File

@@ -1,9 +1,8 @@
use spirv_std::RuntimeArray;
use coremem_types::dim::DimensionedSlice;
use coremem_types::mat::Material;
use coremem_types::real::Real;
use coremem_types::step::{SimMeta, StepEContext, StepHContext, VolumeSampleNeg, VolumeSamplePos};
use coremem_types::step::{SimMeta, StepEContext, StepHContext};
use coremem_types::vec::{Vec3, Vec3u};
use crate::support::SizedArray;
@@ -22,38 +21,12 @@ pub(crate) fn step_h<R: Real, M: Material<R>>(
let len = dim.product_sum_usize();
let stim_h_array = unsafe { SizedArray::new(stimulus_h, len) };
let stim_h_matrix = DimensionedSlice::new(dim, &stim_h_array);
let mat_array = unsafe { SizedArray::new(material, len) };
let mat_matrix = DimensionedSlice::new(dim, &mat_array);
let e_array = unsafe { SizedArray::new(e, len) };
let e_matrix = DimensionedSlice::new(dim, &e_array);
let mut h_array = unsafe { SizedArray::new(h, len) };
let mut h_matrix = DimensionedSlice::new(dim, &mut h_array);
let mut m_array = unsafe { SizedArray::new(m, len) };
let mut m_matrix = DimensionedSlice::new(dim, &mut m_array);
let stim_h = stim_h_matrix[idx];
let mat = &mat_matrix[idx];
let in_e = VolumeSamplePos::from_indexable(&e_matrix, dim, idx);
let in_h = h_matrix[idx];
let in_m = m_matrix[idx];
let update_state = StepHContext {
inv_feature_size: meta.inv_feature_size,
time_step: meta.time_step,
stim_h,
mat,
in_e,
in_h,
in_m,
};
let (new_h, new_m) = update_state.step_h();
h_matrix[idx] = new_h;
m_matrix[idx] = new_m;
StepHContext::step_flat_view(*meta, &mat_array, &stim_h_array, &e_array, &mut h_array, &mut m_array, idx);
}
}
@@ -70,32 +43,11 @@ pub(crate) fn step_e<R: Real, M: Material<R>>(
let len = dim.product_sum_usize();
let stim_e_array = unsafe { SizedArray::new(stimulus_e, len) };
let stim_e_matrix = DimensionedSlice::new(dim, &stim_e_array);
let mat_array = unsafe { SizedArray::new(material, len) };
let mat_matrix = DimensionedSlice::new(dim, &mat_array);
let mut e_array = unsafe { SizedArray::new(e, len) };
let mut e_matrix = DimensionedSlice::new(dim, &mut e_array);
let h_array = unsafe { SizedArray::new(h, len) };
let h_matrix = DimensionedSlice::new(dim, &h_array);
let stim_e = stim_e_matrix[idx];
let mat = &mat_matrix[idx];
let in_e = e_matrix[idx];
let in_h = VolumeSampleNeg::from_indexable(&h_matrix, idx);
let update_state = StepEContext {
inv_feature_size: meta.inv_feature_size,
time_step: meta.time_step,
stim_e,
mat,
in_h,
in_e,
};
let new_e = update_state.step_e();
e_matrix[idx] = new_e;
StepEContext::step_flat_view(*meta, &mat_array, &stim_e_array, &mut e_array, &h_array, idx);
}
}

View File

@@ -1,6 +1,7 @@
use core::ops::Index;
use core::ops::{Index, IndexMut};
use crate::compound::Optional;
use crate::dim::DimensionedSlice;
use crate::mat::Material;
use crate::real::Real;
use crate::vec::{Vec3, Vec3u};
@@ -236,6 +237,42 @@ pub struct StepEContext<'a, R, M> {
}
impl<'a, R: Real, M: Material<R>> StepEContext<'a, R, M> {
pub fn step_flat_view<RM, RF, WF>(
meta: SimMeta<R>,
mat: &RM,
stim_e: &RF,
e: &mut WF,
h: &RF,
idx: Vec3u,
)
where
RM: Index<usize, Output=M> + ?Sized,
RF: Index<usize, Output=Vec3<R>> + ?Sized,
WF: Index<usize, Output=Vec3<R>> + IndexMut<usize> + ?Sized,
{
let dim = meta.dim;
let stim_e_matrix = DimensionedSlice::new(dim, stim_e);
let mat_matrix = DimensionedSlice::new(dim, mat);
let mut e_matrix = DimensionedSlice::new(dim, e);
let h_matrix = DimensionedSlice::new(dim, h);
let stim_e = stim_e_matrix[idx];
let mat = &mat_matrix[idx];
let in_e = e_matrix[idx];
let in_h = VolumeSampleNeg::from_indexable(&h_matrix, idx);
let update_state = StepEContext {
inv_feature_size: meta.inv_feature_size,
time_step: meta.time_step,
stim_e,
mat,
in_h,
in_e,
};
let new_e = update_state.step_e();
e_matrix[idx] = new_e;
}
pub fn step_e(self) -> Vec3<R> {
let twice_eps0 = R::twice_eps0();
let deltas = self.in_h.delta_h();
@@ -267,6 +304,47 @@ pub struct StepHContext<'a, R, M> {
}
impl<'a, R: Real, M: Material<R>> StepHContext<'a, R, M> {
pub fn step_flat_view<RM, RF, WF>(
meta: SimMeta<R>,
mat: &RM,
stim_h: &RF,
e: &RF,
h: &mut WF,
m: &mut WF,
idx: Vec3u,
)
where
RM: Index<usize, Output=M> + ?Sized,
RF: Index<usize, Output=Vec3<R>> + ?Sized,
WF: Index<usize, Output=Vec3<R>> + IndexMut<usize> + ?Sized,
{
let dim = meta.dim;
let stim_h_matrix = DimensionedSlice::new(dim, stim_h);
let mat_matrix = DimensionedSlice::new(dim, mat);
let e_matrix = DimensionedSlice::new(dim, e);
let mut h_matrix = DimensionedSlice::new(dim, h);
let mut m_matrix = DimensionedSlice::new(dim, m);
let stim_h = stim_h_matrix[idx];
let mat = &mat_matrix[idx];
let in_e = VolumeSamplePos::from_indexable(&e_matrix, dim, idx);
let in_h = h_matrix[idx];
let in_m = m_matrix[idx];
let update_state = StepHContext {
inv_feature_size: meta.inv_feature_size,
time_step: meta.time_step,
stim_h,
mat,
in_e,
in_h,
in_m,
};
let (new_h, new_m) = update_state.step_h();
h_matrix[idx] = new_h;
m_matrix[idx] = new_m;
}
pub fn step_h(self) -> (Vec3<R>, Vec3<R>) {
let mu0 = R::mu0();
let mu0_inv = R::mu0_inv();