Move input validation of expressions to evalexpr

This commit is contained in:
Magnus Ulimoen 2021-07-01 18:47:56 +02:00
parent 891cee3160
commit 6c6e11c1e3
3 changed files with 68 additions and 63 deletions

View File

@ -1,5 +1,6 @@
use ndarray::{ArrayView, ArrayViewMut, Dimension};
use sbp::Float;
use std::convert::{TryFrom, TryInto};
pub mod evalexpr;
@ -24,3 +25,10 @@ impl<D: Dimension> euler::eval::Evaluator<D> for Evaluator {
}
}
}
impl TryFrom<crate::input::Expressions> for Evaluator {
type Error = ();
fn try_from(expr: crate::input::Expressions) -> Result<Self, Self::Error> {
Ok(Self::EvalExpr(expr.try_into()?))
}
}

View File

@ -2,6 +2,9 @@ use euler::GAMMA;
use evalexpr::*;
use ndarray::{azip, ArrayView, ArrayViewMut, Dimension};
use sbp::Float;
use std::convert::TryFrom;
use crate::input;
#[derive(Clone, Debug)]
pub enum Evaluator {
@ -9,6 +12,63 @@ pub enum Evaluator {
Conservation(EvaluatorConservation),
}
impl TryFrom<input::Expressions> for Evaluator {
type Error = ();
fn try_from(expr: input::Expressions) -> Result<Self, Self::Error> {
let mut context = default_context();
match expr {
input::Expressions::Pressure(input::ExpressionsPressure {
globals,
rho,
u,
v,
p,
}) => {
if let Some(globals) = &globals {
evalexpr::eval_with_context_mut(globals, &mut context).unwrap();
}
let [rho, u, v, p] = [
evalexpr::build_operator_tree(&rho).unwrap(),
evalexpr::build_operator_tree(&u).unwrap(),
evalexpr::build_operator_tree(&v).unwrap(),
evalexpr::build_operator_tree(&p).unwrap(),
];
Ok(Evaluator::Pressure(EvaluatorPressure {
ctx: context,
rho,
u,
v,
p,
}))
}
input::Expressions::Conservation(input::ExpressionsConservation {
globals,
rho,
rhou,
rhov,
e,
}) => {
if let Some(globals) = &globals {
evalexpr::eval_with_context_mut(globals, &mut context).unwrap();
}
let [rho, rhou, rhov, e] = [
evalexpr::build_operator_tree(&rho).unwrap(),
evalexpr::build_operator_tree(&rhou).unwrap(),
evalexpr::build_operator_tree(&rhov).unwrap(),
evalexpr::build_operator_tree(&e).unwrap(),
];
Ok(Evaluator::Conservation(EvaluatorConservation {
ctx: context,
rho,
rhou,
rhov,
e,
}))
}
}
}
}
#[derive(Debug, Clone)]
pub struct EvaluatorConservation {
pub(crate) ctx: HashMapContext,

View File

@ -6,69 +6,6 @@ use sbp::Float;
use crate::eval;
use crate::input;
impl TryFrom<input::Expressions> for eval::Evaluator {
type Error = ();
fn try_from(expr: input::Expressions) -> Result<Self, Self::Error> {
let mut context = eval::evalexpr::default_context();
match expr {
input::Expressions::Pressure(input::ExpressionsPressure {
globals,
rho,
u,
v,
p,
}) => {
if let Some(globals) = &globals {
evalexpr::eval_with_context_mut(globals, &mut context).unwrap();
}
let [rho, u, v, p] = [
evalexpr::build_operator_tree(&rho).unwrap(),
evalexpr::build_operator_tree(&u).unwrap(),
evalexpr::build_operator_tree(&v).unwrap(),
evalexpr::build_operator_tree(&p).unwrap(),
];
Ok(eval::Evaluator::EvalExpr(
eval::evalexpr::Evaluator::Pressure(eval::evalexpr::EvaluatorPressure {
ctx: context,
rho,
u,
v,
p,
}),
))
}
input::Expressions::Conservation(input::ExpressionsConservation {
globals,
rho,
rhou,
rhov,
e,
}) => {
if let Some(globals) = &globals {
evalexpr::eval_with_context_mut(globals, &mut context).unwrap();
}
let [rho, rhou, rhov, e] = [
evalexpr::build_operator_tree(&rho).unwrap(),
evalexpr::build_operator_tree(&rhou).unwrap(),
evalexpr::build_operator_tree(&rhov).unwrap(),
evalexpr::build_operator_tree(&e).unwrap(),
];
Ok(eval::Evaluator::EvalExpr(
eval::evalexpr::Evaluator::Conservation(
eval::evalexpr::EvaluatorConservation {
ctx: context,
rho,
rhou,
rhov,
e,
},
),
))
}
}
}
}
impl TryFrom<input::InitialConditions> for InitialConditions {
type Error = ();
fn try_from(v: input::InitialConditions) -> Result<Self, Self::Error> {