revisit SBP Traits

This commit is contained in:
2021-01-17 15:37:45 +01:00
parent 6f3a810cd3
commit 1f15bcc056
13 changed files with 229 additions and 219 deletions

View File

@@ -13,7 +13,6 @@ rayon = "1.3.0"
indicatif = "0.15.0"
structopt = "0.3.14"
ndarray = { version = "0.13.1", features = ["serde"] }
either = "1.5.3"
serde = { version = "1.0.115", features = ["derive"] }
json5 = "0.2.8"
indexmap = { version = "1.5.2", features = ["serde-1"] }

View File

@@ -1,15 +1,12 @@
use either::*;
use structopt::StructOpt;
use sbp::operators::{SbpOperator2d, UpwindOperator2d};
use sbp::operators::SbpOperator2d;
use sbp::*;
mod file;
mod parsing;
use file::*;
pub(crate) type DiffOp = Either<Box<dyn SbpOperator2d>, Box<dyn UpwindOperator2d>>;
struct System {
fnow: Vec<euler::Field>,
fnext: Vec<euler::Field>,
@@ -20,14 +17,14 @@ struct System {
bt: Vec<euler::BoundaryCharacteristics>,
eb: Vec<euler::BoundaryStorage>,
time: Float,
operators: Vec<DiffOp>,
operators: Vec<Box<dyn SbpOperator2d>>,
}
impl System {
fn new(
grids: Vec<grid::Grid>,
bt: Vec<euler::BoundaryCharacteristics>,
operators: Vec<DiffOp>,
operators: Vec<Box<dyn SbpOperator2d>>,
) -> Self {
let fnow = grids
.iter()
@@ -42,10 +39,7 @@ impl System {
let metrics = grids
.iter()
.zip(&operators)
.map(|(g, op)| {
let sbpop: &dyn SbpOperator2d = op.as_ref().either(|op| &**op, |uo| uo.as_sbp());
g.metrics(sbpop).unwrap()
})
.map(|(g, op)| g.metrics(&**op).unwrap())
.collect::<Vec<_>>();
let eb = bt
@@ -97,13 +91,10 @@ impl System {
{
s.spawn(move |_| {
let bc = euler::boundary_extracts(prev_all, bt, prev, grid, eb, time);
match op.as_ref() {
Left(sbp) => {
euler::RHS_trad(&**sbp, fut, prev, metrics, &bc, &mut wb.0);
}
Right(uo) => {
euler::RHS_upwind(&**uo, fut, prev, metrics, &bc, &mut wb.0);
}
if op.upwind().is_some() {
euler::RHS_upwind(&**op, fut, prev, metrics, &bc, &mut wb.0);
} else {
euler::RHS_trad(&**op, fut, prev, metrics, &bc, &mut wb.0);
}
})
}
@@ -130,12 +121,10 @@ impl System {
/// Suggested maximum dt for this problem
fn max_dt(&self) -> Float {
let is_h2 = self.operators.iter().any(|op| {
op.as_ref().either(
|op| op.is_h2xi() || op.is_h2eta(),
|op| op.is_h2xi() || op.is_h2eta(),
)
});
let is_h2 = self
.operators
.iter()
.any(|op| op.is_h2xi() || op.is_h2eta());
let c_max = if is_h2 { 0.5 } else { 1.0 };
let mut max_dt: Float = Float::INFINITY;
@@ -283,8 +272,7 @@ fn main() {
for ((fmod, grid), op) in sys.fnow.iter().zip(&sys.grids).zip(&sys.operators) {
let mut fvort = fmod.clone();
fvort.vortex(grid.x(), grid.y(), time, &vortexparams);
let sbpop: &dyn SbpOperator2d = op.as_ref().either(|op| &**op, |uo| uo.as_sbp());
e += fmod.h2_err(&fvort, sbpop);
e += fmod.h2_err(&fvort, &**op);
}
println!("Total error: {:e}", e);
}

View File

@@ -1,5 +1,4 @@
use super::DiffOp;
use either::*;
use sbp::operators::SbpOperator2d;
use sbp::utils::h2linspace;
use sbp::Float;
@@ -148,7 +147,7 @@ pub struct RuntimeConfiguration {
pub names: Vec<String>,
pub grids: Vec<sbp::grid::Grid>,
pub bc: Vec<euler::BoundaryCharacteristics>,
pub op: Vec<DiffOp>,
pub op: Vec<Box<dyn SbpOperator2d>>,
pub integration_time: Float,
pub vortex: euler::VortexParameters,
}
@@ -223,32 +222,19 @@ impl Configuration {
use sbp::operators::*;
use Operator as op;
match (eta, xi) {
(op::Upwind4, op::Upwind4) => {
Right(Box::new(Upwind4) as Box<dyn UpwindOperator2d>)
let matcher = |op| -> Box<dyn SbpOperator2d> {
match op {
op::Upwind4 => Box::new(Upwind4),
op::Upwind4h2 => Box::new(Upwind4h2),
op::Upwind9 => Box::new(Upwind9),
op::Upwind9h2 => Box::new(Upwind9h2),
op::Sbp4 => Box::new(SBP4),
op::Sbp8 => Box::new(SBP8),
}
(op::Upwind4h2, op::Upwind4h2) => {
Right(Box::new(Upwind4h2) as Box<dyn UpwindOperator2d>)
}
(op::Upwind9, op::Upwind9) => {
Right(Box::new(Upwind9) as Box<dyn UpwindOperator2d>)
}
(op::Upwind9h2, op::Upwind9h2) => {
Right(Box::new(Upwind9h2) as Box<dyn UpwindOperator2d>)
}
(op::Upwind4, op::Upwind4h2) => {
Right(Box::new((&Upwind4, &Upwind4h2)) as Box<dyn UpwindOperator2d>)
}
(op::Upwind9, op::Upwind9h2) => {
Right(Box::new((&Upwind9, &Upwind9h2)) as Box<dyn UpwindOperator2d>)
}
(op::Upwind9h2, op::Upwind9) => {
Right(Box::new((&Upwind9h2, &Upwind9)) as Box<dyn UpwindOperator2d>)
}
(op::Sbp4, op::Sbp4) => Left(Box::new(SBP4) as Box<dyn SbpOperator2d>),
(op::Sbp8, op::Sbp8) => Left(Box::new(SBP8) as Box<dyn SbpOperator2d>),
_ => todo!("Combination {:?}, {:?} not implemented", eta, xi),
}
};
Box::new((matcher(eta), matcher(xi))) as Box<dyn SbpOperator2d>
})
.collect();
let bc = self