2020-04-03 22:29:02 +00:00
|
|
|
use super::grid::{Grid, Metrics};
|
2020-01-29 20:16:09 +00:00
|
|
|
use super::integrate;
|
2019-12-12 19:32:38 +00:00
|
|
|
use super::operators::{SbpOperator, UpwindOperator};
|
2020-02-27 19:26:43 +00:00
|
|
|
use crate::Float;
|
2020-01-29 20:06:50 +00:00
|
|
|
use ndarray::azip;
|
2019-12-11 19:36:12 +00:00
|
|
|
use ndarray::prelude::*;
|
2019-08-13 18:43:31 +00:00
|
|
|
|
2019-12-11 19:36:12 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-02-27 19:26:43 +00:00
|
|
|
pub struct Field(pub(crate) Array3<Float>);
|
2019-12-11 19:36:12 +00:00
|
|
|
|
|
|
|
impl std::ops::Deref for Field {
|
2020-02-27 19:26:43 +00:00
|
|
|
type Target = Array3<Float>;
|
2019-12-11 19:36:12 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::DerefMut for Field {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
2019-08-13 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 19:36:12 +00:00
|
|
|
impl Field {
|
2020-01-29 21:01:12 +00:00
|
|
|
pub fn new(height: usize, width: usize) -> Self {
|
2019-12-11 19:36:12 +00:00
|
|
|
let field = Array3::zeros((3, height, width));
|
|
|
|
|
|
|
|
Self(field)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nx(&self) -> usize {
|
|
|
|
self.0.shape()[2]
|
|
|
|
}
|
|
|
|
pub fn ny(&self) -> usize {
|
|
|
|
self.0.shape()[1]
|
|
|
|
}
|
|
|
|
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn ex(&self) -> ArrayView2<Float> {
|
2019-12-11 19:36:12 +00:00
|
|
|
self.slice(s![0, .., ..])
|
|
|
|
}
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn hz(&self) -> ArrayView2<Float> {
|
2019-12-11 19:36:12 +00:00
|
|
|
self.slice(s![1, .., ..])
|
|
|
|
}
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn ey(&self) -> ArrayView2<Float> {
|
2019-12-11 19:36:12 +00:00
|
|
|
self.slice(s![2, .., ..])
|
|
|
|
}
|
|
|
|
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn ex_mut(&mut self) -> ArrayViewMut2<Float> {
|
2019-12-11 19:36:12 +00:00
|
|
|
self.slice_mut(s![0, .., ..])
|
|
|
|
}
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn hz_mut(&mut self) -> ArrayViewMut2<Float> {
|
2019-12-11 19:36:12 +00:00
|
|
|
self.slice_mut(s![1, .., ..])
|
|
|
|
}
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn ey_mut(&mut self) -> ArrayViewMut2<Float> {
|
2019-12-11 19:36:12 +00:00
|
|
|
self.slice_mut(s![2, .., ..])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn components_mut(
|
|
|
|
&mut self,
|
2020-02-27 19:26:43 +00:00
|
|
|
) -> (
|
|
|
|
ArrayViewMut2<Float>,
|
|
|
|
ArrayViewMut2<Float>,
|
|
|
|
ArrayViewMut2<Float>,
|
|
|
|
) {
|
2019-12-14 13:51:19 +00:00
|
|
|
let mut iter = self.0.outer_iter_mut();
|
2019-08-13 18:43:31 +00:00
|
|
|
|
2019-12-14 13:51:19 +00:00
|
|
|
let ex = iter.next().unwrap();
|
|
|
|
let hz = iter.next().unwrap();
|
|
|
|
let ey = iter.next().unwrap();
|
|
|
|
assert_eq!(iter.next(), None);
|
2019-12-11 19:36:12 +00:00
|
|
|
|
2019-12-14 13:51:19 +00:00
|
|
|
(ex, hz, ey)
|
2019-08-13 18:43:31 +00:00
|
|
|
}
|
2019-12-14 12:59:20 +00:00
|
|
|
}
|
2019-08-13 18:43:31 +00:00
|
|
|
|
2020-01-29 21:01:12 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-01-29 20:06:50 +00:00
|
|
|
pub struct System<SBP: SbpOperator> {
|
|
|
|
sys: (Field, Field),
|
|
|
|
wb: WorkBuffers,
|
2020-04-03 22:29:02 +00:00
|
|
|
grid: Grid,
|
|
|
|
metrics: Metrics<SBP>,
|
2020-01-29 20:06:50 +00:00
|
|
|
}
|
2019-12-14 12:59:20 +00:00
|
|
|
|
2020-01-29 20:06:50 +00:00
|
|
|
impl<SBP: SbpOperator> System<SBP> {
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn new(x: Array2<Float>, y: Array2<Float>) -> Self {
|
2020-01-29 21:01:12 +00:00
|
|
|
assert_eq!(x.shape(), y.shape());
|
|
|
|
let ny = x.shape()[0];
|
|
|
|
let nx = x.shape()[1];
|
|
|
|
|
|
|
|
let grid = Grid::new(x, y).unwrap();
|
2020-04-03 22:29:02 +00:00
|
|
|
let metrics = grid.metrics().unwrap();
|
2019-12-14 12:59:20 +00:00
|
|
|
|
2020-01-29 20:06:50 +00:00
|
|
|
Self {
|
2020-01-29 21:01:12 +00:00
|
|
|
sys: (Field::new(ny, nx), Field::new(ny, nx)),
|
2020-01-29 20:06:50 +00:00
|
|
|
grid,
|
2020-04-03 22:29:02 +00:00
|
|
|
metrics,
|
2020-01-29 21:01:12 +00:00
|
|
|
wb: WorkBuffers::new(ny, nx),
|
2020-01-29 20:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 19:32:38 +00:00
|
|
|
|
2020-01-29 20:06:50 +00:00
|
|
|
pub fn field(&self) -> &Field {
|
|
|
|
&self.sys.0
|
2019-12-12 19:32:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn set_gaussian(&mut self, x0: Float, y0: Float) {
|
2020-01-29 20:06:50 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
2019-08-13 18:43:31 +00:00
|
|
|
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn advance(&mut self, dt: Float) {
|
2020-04-10 15:36:01 +00:00
|
|
|
fn rhs_adaptor<SBP: SbpOperator>(
|
|
|
|
fut: &mut Field,
|
|
|
|
prev: &Field,
|
|
|
|
_time: Float,
|
|
|
|
c: &(&Grid, &Metrics<SBP>),
|
|
|
|
m: &mut (Array2<Float>, Array2<Float>, Array2<Float>, Array2<Float>),
|
|
|
|
) {
|
|
|
|
let (grid, metrics) = c;
|
|
|
|
RHS(fut, prev, grid, metrics, m);
|
|
|
|
}
|
|
|
|
let mut _time = 0.0;
|
2020-04-11 17:56:44 +00:00
|
|
|
integrate::integrate::<integrate::Rk4, _, _, _, _>(
|
2020-04-10 15:36:01 +00:00
|
|
|
rhs_adaptor,
|
2020-01-29 20:06:50 +00:00
|
|
|
&self.sys.0,
|
|
|
|
&mut self.sys.1,
|
2020-04-10 15:36:01 +00:00
|
|
|
&mut _time,
|
2020-01-29 20:06:50 +00:00
|
|
|
dt,
|
|
|
|
&mut self.wb.k,
|
2020-04-10 15:36:01 +00:00
|
|
|
&(&self.grid, &self.metrics),
|
2020-01-29 20:06:50 +00:00
|
|
|
&mut self.wb.tmp,
|
|
|
|
);
|
|
|
|
std::mem::swap(&mut self.sys.0, &mut self.sys.1);
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 12:59:20 +00:00
|
|
|
|
2020-01-29 20:06:50 +00:00
|
|
|
impl<UO: UpwindOperator> System<UO> {
|
|
|
|
/// Using artificial dissipation with the upwind operator
|
2020-02-27 19:26:43 +00:00
|
|
|
pub fn advance_upwind(&mut self, dt: Float) {
|
2020-04-10 15:36:01 +00:00
|
|
|
fn rhs_adaptor<UO: UpwindOperator>(
|
|
|
|
fut: &mut Field,
|
|
|
|
prev: &Field,
|
|
|
|
_time: Float,
|
|
|
|
c: &(&Grid, &Metrics<UO>),
|
|
|
|
m: &mut (Array2<Float>, Array2<Float>, Array2<Float>, Array2<Float>),
|
|
|
|
) {
|
|
|
|
let (grid, metrics) = c;
|
|
|
|
RHS_upwind(fut, prev, grid, metrics, m);
|
|
|
|
}
|
|
|
|
let mut _time = 0.0;
|
2020-04-11 17:56:44 +00:00
|
|
|
integrate::integrate::<integrate::Rk4, _, _, _, _>(
|
2020-04-10 15:36:01 +00:00
|
|
|
rhs_adaptor,
|
2020-01-29 20:06:50 +00:00
|
|
|
&self.sys.0,
|
|
|
|
&mut self.sys.1,
|
2020-04-10 15:36:01 +00:00
|
|
|
&mut _time,
|
2020-01-29 20:06:50 +00:00
|
|
|
dt,
|
|
|
|
&mut self.wb.k,
|
2020-04-10 15:36:01 +00:00
|
|
|
&(&self.grid, &self.metrics),
|
2020-01-29 20:06:50 +00:00
|
|
|
&mut self.wb.tmp,
|
|
|
|
);
|
|
|
|
std::mem::swap(&mut self.sys.0, &mut self.sys.1);
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 12:59:20 +00:00
|
|
|
|
2020-02-27 19:26:43 +00:00
|
|
|
fn gaussian(x: Float, x0: Float, y: Float, y0: Float) -> Float {
|
2020-02-27 21:27:11 +00:00
|
|
|
use crate::consts::PI;
|
2020-02-27 19:26:43 +00:00
|
|
|
|
2020-01-29 20:06:50 +00:00
|
|
|
let x = x - x0;
|
|
|
|
let y = y - y0;
|
2019-12-12 18:24:22 +00:00
|
|
|
|
2020-01-29 20:06:50 +00:00
|
|
|
let sigma = 0.05;
|
2019-12-14 12:59:20 +00:00
|
|
|
|
2020-02-27 19:26:43 +00:00
|
|
|
1.0 / (2.0 * PI * sigma * sigma) * (-(x * x + y * y) / (2.0 * sigma * sigma)).exp()
|
2019-08-13 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 20:39:29 +00:00
|
|
|
#[allow(non_snake_case)]
|
2020-01-29 20:06:50 +00:00
|
|
|
/// Solving (Au)_x + (Bu)_y
|
|
|
|
/// with:
|
|
|
|
/// A B
|
|
|
|
/// [ 0, 0, 0] [ 0, 1, 0]
|
|
|
|
/// [ 0, 0, -1] [ 1, 0, 0]
|
|
|
|
/// [ 0, -1, 0] [ 0, 0, 0]
|
|
|
|
///
|
2019-12-11 20:39:29 +00:00
|
|
|
/// This flux is rotated by the grid metrics
|
|
|
|
/// (Au)_x + (Bu)_y = 1/J [
|
|
|
|
/// (J xi_x Au)_xi + (J eta_x Au)_eta
|
|
|
|
/// (J xi_y Bu)_xi + (J eta_y Bu)_eta
|
|
|
|
/// ]
|
|
|
|
/// where J is the grid determinant
|
|
|
|
///
|
|
|
|
/// This is used both in fluxes and SAT terms
|
|
|
|
fn RHS<SBP: SbpOperator>(
|
|
|
|
k: &mut Field,
|
|
|
|
y: &Field,
|
2020-04-03 22:29:02 +00:00
|
|
|
_grid: &Grid,
|
|
|
|
metrics: &Metrics<SBP>,
|
2020-02-27 19:26:43 +00:00
|
|
|
tmp: &mut (Array2<Float>, Array2<Float>, Array2<Float>, Array2<Float>),
|
2019-12-11 20:39:29 +00:00
|
|
|
) {
|
2020-04-03 22:29:02 +00:00
|
|
|
fluxes(k, y, metrics, tmp);
|
2019-12-11 20:39:29 +00:00
|
|
|
|
2020-01-29 20:06:50 +00:00
|
|
|
let boundaries = BoundaryTerms {
|
|
|
|
north: Boundary::This,
|
|
|
|
south: Boundary::This,
|
|
|
|
west: Boundary::This,
|
|
|
|
east: Boundary::This,
|
|
|
|
};
|
2020-04-03 22:29:02 +00:00
|
|
|
SAT_characteristics(k, y, metrics, &boundaries);
|
2019-12-11 20:39:29 +00:00
|
|
|
|
|
|
|
azip!((k in &mut k.0,
|
2020-04-03 22:29:02 +00:00
|
|
|
&detj in &metrics.detj.broadcast((3, y.ny(), y.nx())).unwrap()) {
|
2019-12-11 20:39:29 +00:00
|
|
|
*k /= detj;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:32:38 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn RHS_upwind<UO: UpwindOperator>(
|
|
|
|
k: &mut Field,
|
|
|
|
y: &Field,
|
2020-04-03 22:29:02 +00:00
|
|
|
_grid: &Grid,
|
|
|
|
metrics: &Metrics<UO>,
|
2020-02-27 19:26:43 +00:00
|
|
|
tmp: &mut (Array2<Float>, Array2<Float>, Array2<Float>, Array2<Float>),
|
2019-12-12 19:32:38 +00:00
|
|
|
) {
|
2020-04-03 22:29:02 +00:00
|
|
|
fluxes(k, y, metrics, tmp);
|
|
|
|
dissipation(k, y, metrics, tmp);
|
2019-12-12 19:32:38 +00:00
|
|
|
|
2020-01-29 20:06:50 +00:00
|
|
|
let boundaries = BoundaryTerms {
|
|
|
|
north: Boundary::This,
|
|
|
|
south: Boundary::This,
|
|
|
|
west: Boundary::This,
|
|
|
|
east: Boundary::This,
|
|
|
|
};
|
2020-04-03 22:29:02 +00:00
|
|
|
SAT_characteristics(k, y, metrics, &boundaries);
|
2019-12-12 19:32:38 +00:00
|
|
|
|
|
|
|
azip!((k in &mut k.0,
|
2020-04-03 22:29:02 +00:00
|
|
|
&detj in &metrics.detj.broadcast((3, y.ny(), y.nx())).unwrap()) {
|
2019-12-12 19:32:38 +00:00
|
|
|
*k /= detj;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-11 20:39:29 +00:00
|
|
|
fn fluxes<SBP: SbpOperator>(
|
|
|
|
k: &mut Field,
|
|
|
|
y: &Field,
|
2020-04-03 22:29:02 +00:00
|
|
|
metrics: &Metrics<SBP>,
|
2020-02-27 19:26:43 +00:00
|
|
|
tmp: &mut (Array2<Float>, Array2<Float>, Array2<Float>, Array2<Float>),
|
2019-12-11 20:39:29 +00:00
|
|
|
) {
|
|
|
|
// ex = hz_y
|
|
|
|
{
|
|
|
|
ndarray::azip!((a in &mut tmp.0,
|
2020-04-03 22:29:02 +00:00
|
|
|
&dxi_dy in &metrics.detj_dxi_dy,
|
2019-12-11 20:39:29 +00:00
|
|
|
&hz in &y.hz())
|
|
|
|
*a = dxi_dy * hz
|
|
|
|
);
|
|
|
|
SBP::diffxi(tmp.0.view(), tmp.1.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((b in &mut tmp.2,
|
2020-04-03 22:29:02 +00:00
|
|
|
&deta_dy in &metrics.detj_deta_dy,
|
2019-12-11 20:39:29 +00:00
|
|
|
&hz in &y.hz())
|
|
|
|
*b = deta_dy * hz
|
|
|
|
);
|
|
|
|
SBP::diffeta(tmp.2.view(), tmp.3.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((flux in &mut k.ex_mut(), &ax in &tmp.1, &by in &tmp.3)
|
|
|
|
*flux = ax + by
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// hz = -ey_x + ex_y
|
|
|
|
ndarray::azip!((a in &mut tmp.0,
|
2020-04-03 22:29:02 +00:00
|
|
|
&dxi_dx in &metrics.detj_dxi_dx,
|
|
|
|
&dxi_dy in &metrics.detj_dxi_dy,
|
2019-12-11 20:39:29 +00:00
|
|
|
&ex in &y.ex(),
|
|
|
|
&ey in &y.ey())
|
|
|
|
*a = dxi_dx * -ey + dxi_dy * ex
|
|
|
|
);
|
|
|
|
SBP::diffxi(tmp.0.view(), tmp.1.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((b in &mut tmp.2,
|
2020-04-03 22:29:02 +00:00
|
|
|
&deta_dx in &metrics.detj_deta_dx,
|
|
|
|
&deta_dy in &metrics.detj_deta_dy,
|
2019-12-11 20:39:29 +00:00
|
|
|
&ex in &y.ex(),
|
|
|
|
&ey in &y.ey())
|
|
|
|
*b = deta_dx * -ey + deta_dy * ex
|
|
|
|
);
|
|
|
|
SBP::diffeta(tmp.2.view(), tmp.3.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((flux in &mut k.hz_mut(), &ax in &tmp.1, &by in &tmp.3)
|
|
|
|
*flux = ax + by
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ey = -hz_x
|
|
|
|
{
|
|
|
|
ndarray::azip!((a in &mut tmp.0,
|
2020-04-03 22:29:02 +00:00
|
|
|
&dxi_dx in &metrics.detj_dxi_dx,
|
2019-12-11 20:39:29 +00:00
|
|
|
&hz in &y.hz())
|
|
|
|
*a = dxi_dx * -hz
|
|
|
|
);
|
|
|
|
SBP::diffxi(tmp.0.view(), tmp.1.view_mut());
|
|
|
|
|
|
|
|
azip!((b in &mut tmp.2,
|
2020-04-03 22:29:02 +00:00
|
|
|
&deta_dx in &metrics.detj_deta_dx,
|
2019-12-11 20:39:29 +00:00
|
|
|
&hz in &y.hz())
|
|
|
|
*b = deta_dx * -hz
|
|
|
|
);
|
|
|
|
SBP::diffeta(tmp.2.view(), tmp.3.view_mut());
|
|
|
|
|
|
|
|
azip!((flux in &mut k.ey_mut(), &ax in &tmp.1, &by in &tmp.3)
|
|
|
|
*flux = ax + by
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:32:38 +00:00
|
|
|
fn dissipation<UO: UpwindOperator>(
|
|
|
|
k: &mut Field,
|
|
|
|
y: &Field,
|
2020-04-03 22:29:02 +00:00
|
|
|
metrics: &Metrics<UO>,
|
2020-02-27 19:26:43 +00:00
|
|
|
tmp: &mut (Array2<Float>, Array2<Float>, Array2<Float>, Array2<Float>),
|
2019-12-12 19:32:38 +00:00
|
|
|
) {
|
|
|
|
// ex component
|
|
|
|
{
|
|
|
|
ndarray::azip!((a in &mut tmp.0,
|
2020-04-03 22:29:02 +00:00
|
|
|
&kx in &metrics.detj_dxi_dx,
|
|
|
|
&ky in &metrics.detj_dxi_dy,
|
2019-12-12 19:32:38 +00:00
|
|
|
&ex in &y.ex(),
|
|
|
|
&ey in &y.ey()) {
|
2020-02-27 19:26:43 +00:00
|
|
|
let r = Float::hypot(kx, ky);
|
2019-12-12 19:32:38 +00:00
|
|
|
*a = ky*ky/r * ex + -kx*ky/r*ey;
|
|
|
|
});
|
|
|
|
UO::dissxi(tmp.0.view(), tmp.1.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((b in &mut tmp.2,
|
2020-04-03 22:29:02 +00:00
|
|
|
&kx in &metrics.detj_deta_dx,
|
|
|
|
&ky in &metrics.detj_deta_dy,
|
2019-12-12 19:32:38 +00:00
|
|
|
&ex in &y.ex(),
|
|
|
|
&ey in &y.ey()) {
|
2020-02-27 19:26:43 +00:00
|
|
|
let r = Float::hypot(kx, ky);
|
2019-12-12 19:32:38 +00:00
|
|
|
*b = ky*ky/r * ex + -kx*ky/r*ey;
|
|
|
|
});
|
|
|
|
UO::disseta(tmp.2.view(), tmp.3.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((flux in &mut k.ex_mut(), &ax in &tmp.1, &by in &tmp.3)
|
|
|
|
*flux += ax + by
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// hz component
|
|
|
|
{
|
|
|
|
ndarray::azip!((a in &mut tmp.0,
|
2020-04-03 22:29:02 +00:00
|
|
|
&kx in &metrics.detj_dxi_dx,
|
|
|
|
&ky in &metrics.detj_dxi_dy,
|
2019-12-12 19:32:38 +00:00
|
|
|
&hz in &y.hz()) {
|
2020-02-27 19:26:43 +00:00
|
|
|
let r = Float::hypot(kx, ky);
|
2019-12-12 19:32:38 +00:00
|
|
|
*a = r * hz;
|
|
|
|
});
|
|
|
|
UO::dissxi(tmp.0.view(), tmp.1.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((b in &mut tmp.2,
|
2020-04-03 22:29:02 +00:00
|
|
|
&kx in &metrics.detj_deta_dx,
|
|
|
|
&ky in &metrics.detj_deta_dy,
|
2019-12-12 19:32:38 +00:00
|
|
|
&hz in &y.hz()) {
|
2020-02-27 19:26:43 +00:00
|
|
|
let r = Float::hypot(kx, ky);
|
2019-12-12 19:32:38 +00:00
|
|
|
*b = r * hz;
|
|
|
|
});
|
|
|
|
UO::disseta(tmp.2.view(), tmp.3.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((flux in &mut k.hz_mut(), &ax in &tmp.1, &by in &tmp.3)
|
|
|
|
*flux += ax + by
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ey
|
|
|
|
{
|
|
|
|
ndarray::azip!((a in &mut tmp.0,
|
2020-04-03 22:29:02 +00:00
|
|
|
&kx in &metrics.detj_dxi_dx,
|
|
|
|
&ky in &metrics.detj_dxi_dy,
|
2019-12-12 19:32:38 +00:00
|
|
|
&ex in &y.ex(),
|
|
|
|
&ey in &y.ey()) {
|
2020-02-27 19:26:43 +00:00
|
|
|
let r = Float::hypot(kx, ky);
|
2019-12-12 19:32:38 +00:00
|
|
|
*a = -kx*ky/r * ex + kx*kx/r*ey;
|
|
|
|
});
|
|
|
|
UO::dissxi(tmp.0.view(), tmp.1.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((b in &mut tmp.2,
|
2020-04-03 22:29:02 +00:00
|
|
|
&kx in &metrics.detj_deta_dx,
|
|
|
|
&ky in &metrics.detj_deta_dy,
|
2019-12-12 19:32:38 +00:00
|
|
|
&ex in &y.ex(),
|
|
|
|
&ey in &y.ey()) {
|
2020-02-27 19:26:43 +00:00
|
|
|
let r = Float::hypot(kx, ky);
|
2019-12-12 19:32:38 +00:00
|
|
|
*b = -kx*ky/r * ex + kx*kx/r*ey;
|
|
|
|
});
|
|
|
|
UO::disseta(tmp.2.view(), tmp.3.view_mut());
|
|
|
|
|
|
|
|
ndarray::azip!((flux in &mut k.hz_mut(), &ax in &tmp.1, &by in &tmp.3)
|
|
|
|
*flux += ax + by
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:24:22 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Boundary {
|
|
|
|
This,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct BoundaryTerms {
|
|
|
|
pub north: Boundary,
|
|
|
|
pub south: Boundary,
|
|
|
|
pub east: Boundary,
|
|
|
|
pub west: Boundary,
|
|
|
|
}
|
|
|
|
|
2019-12-11 20:39:29 +00:00
|
|
|
#[allow(non_snake_case)]
|
2019-12-12 18:24:22 +00:00
|
|
|
/// Boundary conditions (SAT)
|
|
|
|
fn SAT_characteristics<SBP: SbpOperator>(
|
|
|
|
k: &mut Field,
|
|
|
|
y: &Field,
|
2020-04-03 22:29:02 +00:00
|
|
|
metrics: &Metrics<SBP>,
|
2019-12-12 18:24:22 +00:00
|
|
|
boundaries: &BoundaryTerms,
|
|
|
|
) {
|
2019-12-11 20:39:29 +00:00
|
|
|
let ny = y.ny();
|
|
|
|
let nx = y.nx();
|
|
|
|
|
2020-02-27 19:26:43 +00:00
|
|
|
fn positive_flux(kx: Float, ky: Float) -> [[Float; 3]; 3] {
|
2019-12-11 20:39:29 +00:00
|
|
|
let r = (kx * kx + ky * ky).sqrt();
|
|
|
|
[
|
|
|
|
[ky * ky / r / 2.0, ky / 2.0, -kx * ky / r / 2.0],
|
|
|
|
[ky / 2.0, r / 2.0, -kx / 2.0],
|
|
|
|
[-kx * ky / r / 2.0, -kx / 2.0, kx * kx / r / 2.0],
|
|
|
|
]
|
|
|
|
}
|
2020-02-27 19:26:43 +00:00
|
|
|
fn negative_flux(kx: Float, ky: Float) -> [[Float; 3]; 3] {
|
2019-12-11 20:39:29 +00:00
|
|
|
let r = (kx * kx + ky * ky).sqrt();
|
|
|
|
[
|
|
|
|
[-ky * ky / r / 2.0, ky / 2.0, kx * ky / r / 2.0],
|
|
|
|
[ky / 2.0, -r / 2.0, -kx / 2.0],
|
|
|
|
[kx * ky / r / 2.0, -kx / 2.0, -kx * kx / r / 2.0],
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-12-12 18:24:22 +00:00
|
|
|
let g = match boundaries.east {
|
|
|
|
Boundary::This => y.slice(s![.., .., 0]),
|
|
|
|
};
|
2019-12-11 20:39:29 +00:00
|
|
|
// East boundary
|
2020-02-27 19:26:43 +00:00
|
|
|
let hinv = 1.0 / (SBP::h()[0] / (nx - 1) as Float);
|
2019-12-12 17:41:41 +00:00
|
|
|
for ((((mut k, v), g), &kx), &ky) in k
|
|
|
|
.slice_mut(s![.., .., nx - 1])
|
|
|
|
.gencolumns_mut()
|
|
|
|
.into_iter()
|
|
|
|
.zip(y.slice(s![.., .., nx - 1]).gencolumns())
|
|
|
|
.zip(g.gencolumns())
|
2020-04-03 22:29:02 +00:00
|
|
|
.zip(metrics.detj_dxi_dx.slice(s![.., nx - 1]))
|
|
|
|
.zip(metrics.detj_dxi_dy.slice(s![.., nx - 1]))
|
2019-12-12 17:41:41 +00:00
|
|
|
{
|
2019-12-11 20:39:29 +00:00
|
|
|
// East boundary, positive flux
|
|
|
|
let tau = -1.0;
|
|
|
|
|
2019-12-12 17:41:41 +00:00
|
|
|
let v = (v[0], v[1], v[2]);
|
|
|
|
let g = (g[0], g[1], g[2]);
|
2019-12-11 20:39:29 +00:00
|
|
|
|
|
|
|
let plus = positive_flux(kx, ky);
|
|
|
|
|
2019-12-12 17:41:41 +00:00
|
|
|
k[0] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (plus[0][0] * (v.0 - g.0) + plus[0][1] * (v.1 - g.1) + plus[0][2] * (v.2 - g.2));
|
2019-12-12 17:41:41 +00:00
|
|
|
k[1] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (plus[1][0] * (v.0 - g.0) + plus[1][1] * (v.1 - g.1) + plus[1][2] * (v.2 - g.2));
|
2019-12-12 17:41:41 +00:00
|
|
|
k[2] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (plus[2][0] * (v.0 - g.0) + plus[2][1] * (v.1 - g.1) + plus[2][2] * (v.2 - g.2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// West boundary, negative flux
|
2019-12-12 18:24:22 +00:00
|
|
|
let g = match boundaries.east {
|
|
|
|
Boundary::This => y.slice(s![.., .., nx - 1]),
|
|
|
|
};
|
2020-02-27 19:26:43 +00:00
|
|
|
let hinv = 1.0 / (SBP::h()[0] / (nx - 1) as Float);
|
2019-12-12 17:41:41 +00:00
|
|
|
for ((((mut k, v), g), &kx), &ky) in k
|
|
|
|
.slice_mut(s![.., .., 0])
|
|
|
|
.gencolumns_mut()
|
|
|
|
.into_iter()
|
|
|
|
.zip(y.slice(s![.., .., 0]).gencolumns())
|
|
|
|
.zip(g.gencolumns())
|
2020-04-03 22:29:02 +00:00
|
|
|
.zip(metrics.detj_dxi_dx.slice(s![.., 0]))
|
|
|
|
.zip(metrics.detj_dxi_dy.slice(s![.., 0]))
|
2019-12-12 17:41:41 +00:00
|
|
|
{
|
2019-12-11 20:39:29 +00:00
|
|
|
let tau = 1.0;
|
|
|
|
|
2019-12-12 17:41:41 +00:00
|
|
|
let v = (v[0], v[1], v[2]);
|
|
|
|
let g = (g[0], g[1], g[2]);
|
2019-12-11 20:39:29 +00:00
|
|
|
|
|
|
|
let minus = negative_flux(kx, ky);
|
|
|
|
|
2019-12-12 17:41:41 +00:00
|
|
|
k[0] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (minus[0][0] * (v.0 - g.0)
|
|
|
|
+ minus[0][1] * (v.1 - g.1)
|
|
|
|
+ minus[0][2] * (v.2 - g.2));
|
2019-12-12 17:41:41 +00:00
|
|
|
k[1] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (minus[1][0] * (v.0 - g.0)
|
|
|
|
+ minus[1][1] * (v.1 - g.1)
|
|
|
|
+ minus[1][2] * (v.2 - g.2));
|
2019-12-12 17:41:41 +00:00
|
|
|
k[2] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (minus[2][0] * (v.0 - g.0)
|
|
|
|
+ minus[2][1] * (v.1 - g.1)
|
|
|
|
+ minus[2][2] * (v.2 - g.2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-12-12 18:24:22 +00:00
|
|
|
let g = match boundaries.north {
|
|
|
|
Boundary::This => y.slice(s![.., 0, ..]),
|
|
|
|
};
|
2020-02-27 19:26:43 +00:00
|
|
|
let hinv = 1.0 / (SBP::h()[0] / (ny - 1) as Float);
|
2019-12-12 17:41:41 +00:00
|
|
|
for ((((mut k, v), g), &kx), &ky) in k
|
|
|
|
.slice_mut(s![.., ny - 1, ..])
|
|
|
|
.gencolumns_mut()
|
|
|
|
.into_iter()
|
|
|
|
.zip(y.slice(s![.., ny - 1, ..]).gencolumns())
|
|
|
|
.zip(g.gencolumns())
|
2020-04-03 22:29:02 +00:00
|
|
|
.zip(metrics.detj_deta_dx.slice(s![ny - 1, ..]))
|
|
|
|
.zip(metrics.detj_deta_dy.slice(s![ny - 1, ..]))
|
2019-12-12 17:41:41 +00:00
|
|
|
{
|
2019-12-11 20:39:29 +00:00
|
|
|
// North boundary, positive flux
|
|
|
|
let tau = -1.0;
|
2019-12-12 17:41:41 +00:00
|
|
|
let v = (v[0], v[1], v[2]);
|
|
|
|
let g = (g[0], g[1], g[2]);
|
2019-12-11 20:39:29 +00:00
|
|
|
|
|
|
|
let plus = positive_flux(kx, ky);
|
|
|
|
|
2019-12-12 17:41:41 +00:00
|
|
|
k[0] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (plus[0][0] * (v.0 - g.0) + plus[0][1] * (v.1 - g.1) + plus[0][2] * (v.2 - g.2));
|
2019-12-12 17:41:41 +00:00
|
|
|
k[1] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (plus[1][0] * (v.0 - g.0) + plus[1][1] * (v.1 - g.1) + plus[1][2] * (v.2 - g.2));
|
2019-12-12 17:41:41 +00:00
|
|
|
k[2] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (plus[2][0] * (v.0 - g.0) + plus[2][1] * (v.1 - g.1) + plus[2][2] * (v.2 - g.2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-12-12 18:24:22 +00:00
|
|
|
let g = match boundaries.south {
|
|
|
|
Boundary::This => y.slice(s![.., ny - 1, ..]),
|
|
|
|
};
|
2020-02-27 19:26:43 +00:00
|
|
|
let hinv = 1.0 / (SBP::h()[0] / (ny - 1) as Float);
|
2019-12-12 17:41:41 +00:00
|
|
|
for ((((mut k, v), g), &kx), &ky) in k
|
|
|
|
.slice_mut(s![.., 0, ..])
|
|
|
|
.gencolumns_mut()
|
|
|
|
.into_iter()
|
|
|
|
.zip(y.slice(s![.., 0, ..]).gencolumns())
|
|
|
|
.zip(g.gencolumns())
|
2020-04-03 22:29:02 +00:00
|
|
|
.zip(metrics.detj_deta_dx.slice(s![0, ..]))
|
|
|
|
.zip(metrics.detj_deta_dy.slice(s![0, ..]))
|
2019-12-12 17:41:41 +00:00
|
|
|
{
|
2019-12-11 20:39:29 +00:00
|
|
|
// South boundary, negative flux
|
|
|
|
|
2019-12-12 17:41:41 +00:00
|
|
|
let tau = 1.0;
|
|
|
|
let v = (v[0], v[1], v[2]);
|
|
|
|
let g = (g[0], g[1], g[2]);
|
2019-12-11 20:39:29 +00:00
|
|
|
|
|
|
|
let minus = negative_flux(kx, ky);
|
|
|
|
|
2019-12-12 17:41:41 +00:00
|
|
|
k[0] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (minus[0][0] * (v.0 - g.0)
|
|
|
|
+ minus[0][1] * (v.1 - g.1)
|
|
|
|
+ minus[0][2] * (v.2 - g.2));
|
2019-12-12 17:41:41 +00:00
|
|
|
k[1] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (minus[1][0] * (v.0 - g.0)
|
|
|
|
+ minus[1][1] * (v.1 - g.1)
|
|
|
|
+ minus[1][2] * (v.2 - g.2));
|
2019-12-12 17:41:41 +00:00
|
|
|
k[2] += tau
|
2019-12-11 20:39:29 +00:00
|
|
|
* hinv
|
|
|
|
* (minus[2][0] * (v.0 - g.0)
|
|
|
|
+ minus[2][1] * (v.1 - g.1)
|
|
|
|
+ minus[2][2] * (v.2 - g.2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 21:01:12 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2019-08-13 18:43:31 +00:00
|
|
|
pub struct WorkBuffers {
|
2020-01-29 20:06:50 +00:00
|
|
|
k: [Field; 4],
|
2020-02-27 19:26:43 +00:00
|
|
|
tmp: (Array2<Float>, Array2<Float>, Array2<Float>, Array2<Float>),
|
2019-08-13 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkBuffers {
|
2020-01-29 21:01:12 +00:00
|
|
|
pub fn new(ny: usize, nx: usize) -> Self {
|
2019-12-11 19:36:12 +00:00
|
|
|
let arr2 = Array2::zeros((ny, nx));
|
2020-01-29 21:01:12 +00:00
|
|
|
let arr3 = Field::new(ny, nx);
|
2019-08-13 18:43:31 +00:00
|
|
|
Self {
|
2020-01-29 20:06:50 +00:00
|
|
|
k: [arr3.clone(), arr3.clone(), arr3.clone(), arr3],
|
2019-12-11 19:36:12 +00:00
|
|
|
tmp: (arr2.clone(), arr2.clone(), arr2.clone(), arr2),
|
2019-08-13 18:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|