diff --git a/examples/coremem.rs b/examples/coremem.rs index 63f706d..c1aea32 100644 --- a/examples/coremem.rs +++ b/examples/coremem.rs @@ -4,7 +4,7 @@ use coremem::consts; use std::{thread, time}; fn main() { - let mut state = SimState::new(101, 101); + let mut state = SimState::new(101, 101, 1e-3 /* feature size */); for y in 70..100 { for x in 0..100 { diff --git a/src/lib.rs b/src/lib.rs index bad5781..1057a92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,23 +34,35 @@ pub mod consts { pub fn MU0() -> R64 { super::MU0.into() } + pub fn HALF() -> R64 { + 0.5.into() + } } } #[derive(Default)] pub struct SimState { cells: Array2>, + feature_size: R64, + step_no: u64, } impl SimState { - pub fn new(width: usize, height: usize) -> Self { + pub fn new(width: usize, height: usize, feature_size: f64) -> Self { Self { cells: Array2::default((height, width)), + feature_size: feature_size.into(), + ..Default::default() } } + + pub fn time(&self) -> f64 { + (self.timestep() * self.step_no as f64).into() + } pub fn step(&mut self) { - // feature size: 1mm. + use consts::real::*; + let half_time_step = HALF() * self.timestep(); let half_time_step = 0.0005 / consts::C; let mut working_cells = Array2::default((self.height(), self.width())); // first advance all the magnetic fields @@ -74,6 +86,7 @@ impl SimState { } } std::mem::swap(&mut working_cells, &mut self.cells); + self.step_no += 1; } pub fn impulse_ex(&mut self, x: usize, y: usize, ex: f64) { @@ -98,6 +111,11 @@ impl SimState { pub fn get_mut(&mut self, x: usize, y: usize) -> &mut Cell { &mut self.cells[[y, x]] } + + + fn timestep(&self) -> R64 { + self.feature_size / consts::real::C() + } } /// Conceptually, one cell looks like this: diff --git a/src/render.rs b/src/render.rs index 81c074b..aa37f7a 100644 --- a/src/render.rs +++ b/src/render.rs @@ -60,6 +60,6 @@ impl ColorTermRenderer { } write!(&mut buf, "\n"); } - println!("{}", buf); + println!("{}\ntime: {:.3e}", buf, state.time()); } }