sim: spirv: remove apply_stimulus test-only function

This commit is contained in:
2022-08-24 01:51:32 -07:00
parent dd6f598e44
commit 488d0fe62a

View File

@@ -1,7 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use log::{info, trace, warn}; use log::{info, warn};
use crate::diagnostics::SyncDiagnostics; use crate::diagnostics::SyncDiagnostics;
use crate::geom::Index; use crate::geom::Index;
@@ -67,8 +67,8 @@ impl<R: Clone, M: Clone, B: Default> Clone for SpirvSim<R, M, B> {
mat: self.mat.clone(), mat: self.mat.clone(),
step_no: self.step_no.clone(), step_no: self.step_no.clone(),
// backends can be expensive to clone. // backends can be expensive to clone.
// we require that the caller explicitly init the backend if they need this. // instead we default-init them and they're expected to acquire their resources on
// TODO: this probably shouldn't be a `Clone` method, but like "read_only_clone()". // first query.
backend: Default::default(), backend: Default::default(),
diag: self.diag.clone(), diag: self.diag.clone(),
} }
@@ -197,18 +197,6 @@ where
M: Send + Sync + Material<R> + Clone + Into<FullyGenericMaterial<R>>, M: Send + Sync + Material<R> + Clone + Into<FullyGenericMaterial<R>>,
B: Send + Sync + SimBackend<R, M>, 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().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();
});
trace!("apply_stimulus end");
}
fn eval_stimulus<'a, S: Stimulus<R>>(&self, stim: &'a S) fn eval_stimulus<'a, S: Stimulus<R>>(&self, stim: &'a S)
-> Cow<'a, RenderedStimulus<R>> -> Cow<'a, RenderedStimulus<R>>
{ {
@@ -226,27 +214,6 @@ where
} }
} }
// used for test
fn iterate_stim<R, S, F>(
stim: &S, dim: Index, feature_size: f32, t_sec: f32, timestep: R, mut f: F
)
where
R: Real,
S: Stimulus<R> + ?Sized,
F: FnMut(Index, Vec3<R>, Vec3<R>),
{
// TODO: parallelize
for z in 0..dim.z() {
for y in 0..dim.y() {
for x in 0..dim.x() {
let pos_idx = Index::new(x, y, z);
let densities = stim.at(t_sec.cast(), feature_size.cast(), pos_idx);
let (value_e, value_h) = (densities.e() * timestep, densities.h() * timestep);
f(pos_idx, value_e, value_h)
}
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
@@ -559,10 +526,10 @@ mod test {
} }
} }
dut_state.apply_stimulus(&RngStimulus::new(seed)); let stim = RngStimulus::new(seed);
for step in 0..steps { for step in 0..steps {
println!("step {}", step); println!("step {}", step);
dut_state.step(); dut_state.step_multiple(1, &stim);
} }
} }
@@ -585,33 +552,33 @@ mod test {
mod same { mod same {
use super::*; use super::*;
use crate::stim::{NoopStimulus, RngStimulus}; use crate::stim::{NoopStimulus, RngStimulus};
fn test_same_explicit( fn test_same_explicit<S: Stimulus<R32>>(
mut cpu_state: SpirvSim<R32, FullyGenericMaterial<R32>, CpuBackend>, mut cpu_state: SpirvSim<R32, FullyGenericMaterial<R32>, CpuBackend>,
mut wgpu_state: SpirvSim<R32, FullyGenericMaterial<R32>, WgpuBackend>, mut wgpu_state: SpirvSim<R32, FullyGenericMaterial<R32>, WgpuBackend>,
stim: &S,
step_iters: u64, step_iters: u64,
steps_per_iter: u32 steps_per_iter: u32
) { ) {
for _ in 0..step_iters { for _ in 0..step_iters {
cpu_state.step_multiple(steps_per_iter, &NoopStimulus); cpu_state.step_multiple(steps_per_iter, stim);
wgpu_state.step_multiple(steps_per_iter, &NoopStimulus); wgpu_state.step_multiple(steps_per_iter, stim);
assert_simstate_eq(&wgpu_state, &cpu_state); assert_simstate_eq(&wgpu_state, &cpu_state);
} }
} }
fn test_same(seed: u64, step_iters: u64, steps_per_iter: u32, size: Index) { fn test_same(seed: u64, step_iters: u64, steps_per_iter: u32, size: Index) {
let mut cpu_state = SpirvSim::new_with_backend(size, 1e-3, CpuBackend::default()); let cpu_state = SpirvSim::new_with_backend(size, 1e-3, CpuBackend::default());
cpu_state.apply_stimulus(&RngStimulus::new(seed));
let mut wgpu_state = SpirvSim::new_with_backend(size, 1e-3, WgpuBackend::default()); let wgpu_state = SpirvSim::new_with_backend(size, 1e-3, WgpuBackend::default());
wgpu_state.apply_stimulus(&RngStimulus::new(seed)); let stim = RngStimulus::new(seed);
test_same_explicit(cpu_state, wgpu_state, step_iters, steps_per_iter); test_same_explicit(cpu_state, wgpu_state, &stim, step_iters, steps_per_iter);
} }
#[test] #[test]
fn same_no_step_no_stim() { fn same_no_step_no_stim() {
let cpu_state = SpirvSim::new_with_backend(Index::new(8, 8, 8), 1e-3, CpuBackend::default()); let cpu_state = SpirvSim::new_with_backend(Index::new(8, 8, 8), 1e-3, CpuBackend::default());
let wgpu_state = SpirvSim::new_with_backend(Index::new(8, 8, 8), 1e-3, WgpuBackend::default()); let wgpu_state = SpirvSim::new_with_backend(Index::new(8, 8, 8), 1e-3, WgpuBackend::default());
test_same_explicit(cpu_state, wgpu_state, 1, 1); test_same_explicit(cpu_state, wgpu_state, &NoopStimulus, 1, 1);
} }
#[test] #[test]
@@ -661,11 +628,9 @@ mod test {
} }
} }
// TODO: we should apply this on every frame? let stim = RngStimulus::new(seed);
cpu_state.apply_stimulus(&RngStimulus::new(seed));
wgpu_state.apply_stimulus(&RngStimulus::new(seed));
test_same_explicit(cpu_state, wgpu_state, step_iters, steps_per_iter); test_same_explicit(cpu_state, wgpu_state, &stim, step_iters, steps_per_iter);
} }
#[test] #[test]
@@ -708,10 +673,9 @@ mod test {
} }
} }
cpu_state.apply_stimulus(&RngStimulus::new(seed)); let stim = RngStimulus::new(seed);
wgpu_state.apply_stimulus(&RngStimulus::new(seed));
test_same_explicit(cpu_state, wgpu_state, step_iters, steps_per_iter); test_same_explicit(cpu_state, wgpu_state, &stim, step_iters, steps_per_iter);
} }
#[test] #[test]
@@ -736,10 +700,9 @@ mod test {
ref_state.put_material(Index::new(1, 1, 1), mat.clone()); ref_state.put_material(Index::new(1, 1, 1), mat.clone());
dut_state.put_material(Index::new(1, 1, 1), mat.clone()); dut_state.put_material(Index::new(1, 1, 1), mat.clone());
ref_state.apply_stimulus(&RngStimulus::new(seed)); let stim = RngStimulus::new(seed);
dut_state.apply_stimulus(&RngStimulus::new(seed));
test_same_explicit(ref_state, dut_state, steps, 1); test_same_explicit(ref_state, dut_state, &stim, steps, 1);
} }
#[test] #[test]
@@ -753,10 +716,9 @@ mod test {
ref_state.put_material(Index::new(1, 1, 0), mat.clone()); ref_state.put_material(Index::new(1, 1, 0), mat.clone());
dut_state.put_material(Index::new(1, 1, 0), mat.clone()); dut_state.put_material(Index::new(1, 1, 0), mat.clone());
ref_state.apply_stimulus(&RngStimulus::new(seed)); let stim = RngStimulus::new(seed);
dut_state.apply_stimulus(&RngStimulus::new(seed));
test_same_explicit(ref_state, dut_state, steps, 1); test_same_explicit(ref_state, dut_state, &stim, steps, 1);
} }