move init to root

This commit is contained in:
Magnus Ulimoen 2019-12-14 12:56:20 +01:00
parent bf7685e453
commit 946caf5e1b
2 changed files with 23 additions and 29 deletions

View File

@ -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) { 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 /// Using artifical dissipation with the upwind operator
@ -82,3 +94,13 @@ impl Universe {
self.sys.0.hz().as_ptr() as *const u8 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()
}

View File

@ -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 { impl Field {
pub fn new(width: usize, height: usize) -> Self { pub fn new(width: usize, height: usize) -> Self {
let field = Array3::zeros((3, height, width)); 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<UO>( pub(crate) fn advance_upwind<UO>(
&self, &self,
fut: &mut Self, fut: &mut Self,