diff --git a/multigrid/src/eval.rs b/multigrid/src/eval.rs index c530224..8b4b903 100644 --- a/multigrid/src/eval.rs +++ b/multigrid/src/eval.rs @@ -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 euler::eval::Evaluator for Evaluator { } } } + +impl TryFrom for Evaluator { + type Error = (); + fn try_from(expr: crate::input::Expressions) -> Result { + Ok(Self::EvalExpr(expr.try_into()?)) + } +} diff --git a/multigrid/src/eval/evalexpr.rs b/multigrid/src/eval/evalexpr.rs index 939f7e7..a0511e5 100644 --- a/multigrid/src/eval/evalexpr.rs +++ b/multigrid/src/eval/evalexpr.rs @@ -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 for Evaluator { + type Error = (); + fn try_from(expr: input::Expressions) -> Result { + 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, diff --git a/multigrid/src/parsing.rs b/multigrid/src/parsing.rs index e064dd8..f0d5c14 100644 --- a/multigrid/src/parsing.rs +++ b/multigrid/src/parsing.rs @@ -6,69 +6,6 @@ use sbp::Float; use crate::eval; use crate::input; -impl TryFrom for eval::Evaluator { - type Error = (); - fn try_from(expr: input::Expressions) -> Result { - 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 for InitialConditions { type Error = (); fn try_from(v: input::InitialConditions) -> Result {