SimMeta: make the fields private

This commit is contained in:
2022-08-23 23:23:49 -07:00
parent 17446cdc6b
commit 8e48414d68
6 changed files with 27 additions and 28 deletions

View File

@@ -235,10 +235,10 @@ pub trait AbstractSim: Sync {
}
fn size(&self) -> Index {
Index(self.meta().dim)
Index(self.meta().dim())
}
fn feature_size(&self) -> f32 {
self.meta().feature_size
self.meta().feature_size()
}
fn feature_volume(&self) -> f32 {
let f = self.feature_size();
@@ -249,7 +249,7 @@ pub trait AbstractSim: Sync {
s.x() * s.y() * s.z()
}
fn timestep(&self) -> f32 {
self.meta().time_step
self.meta().time_step()
}
fn width(&self) -> u32 {

View File

@@ -25,12 +25,12 @@ impl<R: Real, M: Material<R>> SimBackend<R, M> for CpuBackend {
) {
for _ in 0..num_steps {
// step E field
apply_all_cells(meta.dim, |idx| {
apply_all_cells(meta.dim(), |idx| {
StepEContext::step_flat_view(meta, mat, stim_e, e, h, idx);
});
// step H field
apply_all_cells(meta.dim, |idx| {
apply_all_cells(meta.dim(), |idx| {
StepHContext::step_flat_view(meta, mat, stim_h, e, h, m, idx);
});
}

View File

@@ -67,12 +67,13 @@ impl<R: Copy, M: Send + Sync + HasEntryPoints<R>> SimBackend<R, M> for WgpuBacke
m: &mut [Vec3<R>],
num_steps: u32,
) {
let field_bytes = meta.dim.product_sum() as usize * std::mem::size_of::<Vec3<f32>>();
let dim = meta.dim();
let field_bytes = dim.product_sum() as usize * std::mem::size_of::<Vec3<f32>>();
let (step_h, step_e, handles) = self.handles.get_or_insert_with(|| (
M::step_h(),
M::step_e(),
WgpuHandles::open::<R, M>(meta.dim)
WgpuHandles::open::<R, M>(dim)
));
// if device is opened, make sure we're open for the right types
assert_eq!(*step_h, M::step_h());
@@ -186,7 +187,7 @@ impl<R: Copy, M: Send + Sync + HasEntryPoints<R>> SimBackend<R, M> for WgpuBacke
],
});
let workgroups = ((meta.dim.x()+3) / 4, (meta.dim.y()+3) / 4, (meta.dim.z()+3) / 4);
let workgroups = ((dim.x()+3) / 4, (dim.y()+3) / 4, (dim.z()+3) / 4);
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

View File

@@ -161,12 +161,7 @@ impl<R: Real, M: Default, B> SpirvSim<R, M, B>
let mut mat = Vec::new();
mat.resize_with(flat_size, Default::default);
Self {
meta: SimMeta {
dim: *size,
inv_feature_size: R::one()/feature_size,
time_step,
feature_size,
},
meta: SimMeta::new(*size, feature_size, time_step),
e: vec![Vec3::zero(); flat_size],
h: vec![Vec3::zero(); flat_size],
m: vec![Vec3::zero(); flat_size],
@@ -187,7 +182,7 @@ impl<R: Real, M: Default, B: Default> SpirvSim<R, M, B>
impl<R, M, B> SpirvSim<R, M, B> {
fn flat_index(&self, idx: Index) -> Option<usize> {
let dim = self.meta.dim;
let dim = self.meta.dim();
if idx.x() < dim.x() && idx.y() < dim.y() && idx.z() < dim.z() {
Some(((idx.z()*dim.y() + idx.y())*dim.x() + idx.x()) as usize)
} else {
@@ -202,10 +197,11 @@ where
M: Send + Sync + Material<R> + Clone + Into<FullyGenericMaterial<R>>,
B: Send + Sync + SimBackend<R, M>,
{
// TODO: remove this test-only backdoor
#[allow(unused)] // used for test
fn apply_stimulus(&mut self, stim: &dyn Stimulus<R>) {
trace!("apply_stimulus begin");
iterate_stim(stim, self.size(), self.feature_size(), self.time(), self.meta.time_step, |pos_idx, value_e, value_h| {
iterate_stim(stim, self.size(), self.feature_size(), self.time(), self.meta().time_step().cast(), |pos_idx, value_e, value_h| {
let flat_idx = self.flat_index(pos_idx).unwrap();
self.e[flat_idx] += value_e.cast();
self.h[flat_idx] += value_h.cast();
@@ -220,9 +216,9 @@ where
let dim = self.size();
let feature_size = self.feature_size();
let t_sec = self.time();
let timestep = self.meta.time_step;
let timestep = self.meta().time_step();
let stim = stim.rendered(timestep, t_sec.cast(), feature_size.cast(), dim.into());
let stim = stim.rendered(timestep.cast(), t_sec.cast(), feature_size.cast(), dim.into());
assert_eq!(stim.e().dim(), dim.into());
assert_eq!(stim.h().dim(), dim.into());
stim

View File

@@ -13,11 +13,10 @@ use serde::{Serialize, Deserialize};
#[cfg_attr(feature = "fmt", derive(Debug))]
#[derive(Copy, Clone, Default, PartialEq)]
pub struct SimMeta<R> {
// TODO: make these private?
pub dim: Vec3u,
pub inv_feature_size: R,
pub time_step: R,
pub feature_size: R,
dim: Vec3u,
inv_feature_size: R,
time_step: R,
feature_size: R,
}
impl<R: Real> SimMeta<R> {
@@ -31,10 +30,13 @@ impl<R: Real> SimMeta<R> {
}
}
impl<R: Copy> SimMeta<R> {
impl<R> SimMeta<R> {
pub fn dim(&self) -> Vec3u {
self.dim
}
}
impl<R: Copy> SimMeta<R> {
pub fn inv_feature_size(&self) -> R {
self.inv_feature_size
}

View File

@@ -16,8 +16,8 @@ pub(crate) fn step_h<R: Real, M: Material<R>>(
h: &mut RuntimeArray<Vec3<R>>,
m: &mut RuntimeArray<Vec3<R>>,
) {
if idx.x() < meta.dim.x() && idx.y() < meta.dim.y() && idx.z() < meta.dim.z() {
let dim = meta.dim;
let dim = meta.dim();
if idx.x() < dim.x() && idx.y() < dim.y() && idx.z() < dim.z() {
let len = dim.product_sum_usize();
let stim_h_array = unsafe { SizedArray::new(stimulus_h, len) };
@@ -38,8 +38,8 @@ pub(crate) fn step_e<R: Real, M: Material<R>>(
e: &mut RuntimeArray<Vec3<R>>,
h: &RuntimeArray<Vec3<R>>,
) {
if idx.x() < meta.dim.x() && idx.y() < meta.dim.y() && idx.z() < meta.dim.z() {
let dim = meta.dim;
let dim = meta.dim();
if idx.x() < dim.x() && idx.y() < dim.y() && idx.z() < dim.z() {
let len = dim.product_sum_usize();
let stim_e_array = unsafe { SizedArray::new(stimulus_e, len) };