From 946caf5e1bec8060b39db5b2cf7178efc621898c Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Sat, 14 Dec 2019 12:56:20 +0100 Subject: [PATCH] move init to root --- src/lib.rs | 24 +++++++++++++++++++++++- src/maxwell.rs | 28 ---------------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c45475d..f2b411e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,8 +43,20 @@ impl Universe { } } + fn set_gaussian(&mut self, x0: f32, y0: f32) { + let (ex, hz, ey) = self.sys.0.components_mut(); + ndarray::azip!( + (ex in ex, hz in hz, ey in ey, + &x in &self.grid.x, &y in &self.grid.y) + { + *ex = 0.0; + *ey = 0.0; + *hz = gaussian(x, x0, y, y0)/32.0; + }); + } + pub fn init(&mut self, x0: f32, y0: f32) { - self.sys.0.set_gaussian(x0, y0); + self.set_gaussian(x0, y0); } /// Using artifical dissipation with the upwind operator @@ -82,3 +94,13 @@ impl Universe { self.sys.0.hz().as_ptr() as *const u8 } } + +fn gaussian(x: f32, x0: f32, y: f32, y0: f32) -> f32 { + use std::f32; + let x = x - x0; + let y = y - y0; + + let sigma = 0.05; + + 1.0 / (2.0 * f32::consts::PI * sigma * sigma) * (-(x * x + y * y) / (2.0 * sigma * sigma)).exp() +} diff --git a/src/maxwell.rs b/src/maxwell.rs index f2e3174..5a77119 100644 --- a/src/maxwell.rs +++ b/src/maxwell.rs @@ -19,16 +19,6 @@ impl std::ops::DerefMut for Field { } } -fn gaussian(x: f32, x0: f32, y: f32, y0: f32) -> f32 { - use std::f32; - let x = x - x0; - let y = y - y0; - - let sigma = 0.05; - - 1.0 / (2.0 * f32::consts::PI * sigma * sigma) * (-(x * x + y * y) / (2.0 * sigma * sigma)).exp() -} - impl Field { pub fn new(width: usize, height: usize) -> Self { let field = Array3::zeros((3, height, width)); @@ -79,24 +69,6 @@ impl Field { ) } - pub fn set_gaussian(&mut self, x0: f32, y0: f32) { - let nx = self.nx(); - let ny = self.ny(); - - let (mut ex, mut hz, mut ey) = self.components_mut(); - for j in 0..ny { - for i in 0..nx { - // Must divice interval on nx/ny instead of nx - 1/ny-1 - // due to periodic conditions [0, 1) - let x = i as f32 / nx as f32; - let y = j as f32 / ny as f32; - ex[(j, i)] = 0.0; - ey[(j, i)] = 0.0; - hz[(j, i)] = gaussian(x, x0, y, y0) / 32.0; - } - } - } - pub(crate) fn advance_upwind( &self, fut: &mut Self,