move integrator to module

This commit is contained in:
Magnus Ulimoen 2020-01-29 20:50:56 +01:00
parent 92c86c2c41
commit e957a4fff7
3 changed files with 49 additions and 44 deletions

View File

@ -1,7 +1,8 @@
use super::integrate::integrate_rk4;
use super::operators::{SbpOperator, UpwindOperator};
use super::Grid;
use ndarray::azip;
use ndarray::prelude::*;
use ndarray::{azip, Zip};
pub const GAMMA: f32 = 1.4;
@ -224,49 +225,6 @@ impl Field {
}
}
pub(crate) fn integrate_rk4<'a, F: 'a, SBP, RHS, WB>(
rhs: RHS,
prev: &F,
fut: &mut F,
dt: f32,
grid: &Grid<SBP>,
k: &mut [F; 4],
mut wb: &mut WB,
) where
F: std::ops::Deref<Target = Array3<f32>> + std::ops::DerefMut<Target = Array3<f32>>,
SBP: SbpOperator,
RHS: Fn(&mut F, &F, &Grid<SBP>, &mut WB),
{
assert_eq!(prev.shape(), fut.shape());
for i in 0..4 {
// y = y0 + c*kn
fut.assign(prev);
match i {
0 => {}
1 | 2 => {
fut.scaled_add(1.0 / 2.0 * dt, &k[i - 1]);
}
3 => {
fut.scaled_add(dt, &k[i - 1]);
}
_ => {
unreachable!();
}
};
rhs(&mut k[i], &fut, grid, &mut wb);
}
Zip::from(&mut **fut)
.and(&**prev)
.and(&*k[0])
.and(&*k[1])
.and(&*k[2])
.and(&*k[3])
.apply(|y1, &y0, &k1, &k2, &k3, &k4| *y1 = y0 + dt / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4));
}
fn pressure(gamma: f32, rho: f32, rhou: f32, rhov: f32, e: f32) -> f32 {
(gamma - 1.0) * (e - (rhou * rhou + rhov * rhov) / (2.0 * rho))
}

46
src/integrate.rs Normal file
View File

@ -0,0 +1,46 @@
use super::grid::Grid;
use super::operators::SbpOperator;
use ndarray::{Array3, Zip};
pub(crate) fn integrate_rk4<'a, F: 'a, SBP, RHS, WB>(
rhs: RHS,
prev: &F,
fut: &mut F,
dt: f32,
grid: &Grid<SBP>,
k: &mut [F; 4],
mut wb: &mut WB,
) where
F: std::ops::Deref<Target = Array3<f32>> + std::ops::DerefMut<Target = Array3<f32>>,
SBP: SbpOperator,
RHS: Fn(&mut F, &F, &Grid<SBP>, &mut WB),
{
assert_eq!(prev.shape(), fut.shape());
for i in 0..4 {
// y = y0 + c*kn
fut.assign(prev);
match i {
0 => {}
1 | 2 => {
fut.scaled_add(1.0 / 2.0 * dt, &k[i - 1]);
}
3 => {
fut.scaled_add(dt, &k[i - 1]);
}
_ => {
unreachable!();
}
};
rhs(&mut k[i], &fut, grid, &mut wb);
}
Zip::from(&mut **fut)
.and(&**prev)
.and(&*k[0])
.and(&*k[1])
.and(&*k[2])
.and(&*k[3])
.apply(|y1, &y0, &k1, &k2, &k3, &k4| *y1 = y0 + dt / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4));
}

View File

@ -2,6 +2,7 @@ use wasm_bindgen::prelude::*;
pub mod euler;
mod grid;
pub(crate) mod integrate;
mod maxwell;
pub mod operators;
pub use crate::maxwell::{Field, WorkBuffers};