From 81b6e2cb59a9fa61d03dd13e8c40551decc2bf9a Mon Sep 17 00:00:00 2001 From: Colin Date: Tue, 1 Jun 2021 14:09:43 -0700 Subject: [PATCH] Cache the `timestep` field on the SimState. --- src/sim.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/sim.rs b/src/sim.rs index cf270b2..9db82b2 100644 --- a/src/sim.rs +++ b/src/sim.rs @@ -358,6 +358,7 @@ pub struct SimState { scratch: Array3>, feature_size: Real, step_no: u64, + timestep: Real, } impl SimState { @@ -381,6 +382,12 @@ impl SimState { self.feature_size.into() } pub fn timestep(&self) -> Flt { + self.timestep.into() + } +} + +impl SimState { + pub fn new(size: Index, feature_size: Flt) -> Self { // XXX this has to be multiplied by a Courant Number in order to ensure stability. // For 3d, we need 3 full timesteps in order to communicate information across the corner // of a grid. The distance traveled during that time is \sqrt{3}. Hence each timestep needs @@ -388,16 +395,13 @@ impl SimState { // In 2d, this would be \sqrt{2}/2. // It's an upper limit though; it should be safe to go lower. let courant = 0.577; - (self.feature_size / consts::real::C() * courant).into() - } -} - -impl SimState { - pub fn new(size: Index, feature_size: Flt) -> Self { + let feature_size = Real::from(feature_size); + let timestep = feature_size / consts::real::C() * courant; Self { cells: Array3::default((size.z() as _, size.y() as _, size.x() as _)), scratch: Array3::default((size.z() as _, size.y() as _, size.x() as _)), - feature_size: feature_size.into(), + feature_size, + timestep, ..Default::default() } } @@ -406,7 +410,7 @@ impl SimState { impl SimState { pub fn step(&mut self) { use consts::real::*; - let time_step = Real::from_inner(self.timestep()); + let time_step = self.timestep; let half_time_step = HALF() * time_step; let feature_size = self.feature_size; let inv_feature_size = Real::from_inner(1.0) / feature_size; @@ -843,7 +847,6 @@ impl Cell { use consts::real::{HALF, ONE, TWO, ZERO}; // let inv_feature_size = Real::from_inner(1.0) / feature_size; let twice_delta_t = TWO() * delta_t; - let half_delta_t = HALF() * delta_t; let b_prev = self.b();