aboutsummaryrefslogtreecommitdiffstats
path: root/src/modelica_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/modelica_model.rs')
-rw-r--r--src/modelica_model.rs47
1 files changed, 23 insertions, 24 deletions
diff --git a/src/modelica_model.rs b/src/modelica_model.rs
index 785b8ae..40b83d6 100644
--- a/src/modelica_model.rs
+++ b/src/modelica_model.rs
@@ -7,9 +7,10 @@ use std::collections::HashSet;
use std::iter::FromIterator;
use self::modelica_parser::*;
+use errors::Result;
-/// / Helpers
+/// Helpers
pub trait ModelicaModelExt {
fn get_constant_vars(&self) -> HashMap<String, Option<Expr>>;
@@ -17,7 +18,7 @@ pub trait ModelicaModelExt {
fn solve_for(&self,
indep_vars: Vec<String>,
dep_vars: Vec<String>)
- -> Result<ModelicaModel, String>;
+ -> Result<ModelicaModel>;
}
impl ModelicaModelExt for ModelicaModel {
@@ -74,7 +75,7 @@ impl ModelicaModelExt for ModelicaModel {
fn solve_for(&self,
indep_vars: Vec<String>,
dep_vars: Vec<String>)
- -> Result<ModelicaModel, String> {
+ -> Result<ModelicaModel> {
let indep_vars: HashSet<String> = HashSet::from_iter(indep_vars);
let dep_vars: HashSet<String> = HashSet::from_iter(dep_vars);
@@ -89,27 +90,27 @@ impl ModelicaModelExt for ModelicaModel {
passed_vars.extend(dep_vars.clone());
for var in passed_vars {
if !all_vars.contains(&var) {
- return Err(format!("Variable not found in equations: {}", var));
+ return bail!("Variable not found in equations: {}", var);
}
}
// check that V = Q + P + M
if all_vars.len() != (constants.len() + indep_vars.len() + dep_vars.len()) {
- return Err(format!("Variable counts don't add up (V={} Q={} P={} M={})",
+ return bail!("Variable counts don't add up (V={} Q={} P={} M={})",
all_vars.len(),
constants.len(),
indep_vars.len(),
- dep_vars.len()));
+ dep_vars.len());
}
// check that all constants are bound and simple
for (name, value) in &constants {
match *value {
- None => return Err(format!("UnderSpecifiedConstant: {}", name)),
+ None => bail!("UnderSpecifiedConstant: {}", name),
Some(Expr::Integer(_)) |
Some(Expr::Float(_)) => (), // Ok,
Some(_) => {
- return Err(format!("NaiveImplementation: can't handle constant: {}", name))
+ bail!("NaiveImplementation: can't handle constant: {}", name);
}
}
}
@@ -117,15 +118,14 @@ impl ModelicaModelExt for ModelicaModel {
// check that there is a depdendent variable in each equation
for eqn in &self.equations {
if dep_vars.is_disjoint(&eqn.identifiers()) {
- return Err("NaiveImplementation/OverConstrained: at least one equation is \
- missing a dependent variable"
- .to_string());
+ bail!("NaiveImplementation/OverConstrained: at least one equation is \
+ missing a dependent variable")
}
}
// check N >= M
if self.equations.len() < dep_vars.len() {
- return Err("UnderConstrained: more dependent variables than equations".to_string());
+ bail!("UnderConstrained: more dependent variables than equations");
}
println!("Soliving for {:?} in terms of params {:?} and constants {:?}, with {} equations",
@@ -142,7 +142,7 @@ impl ModelicaModelExt for ModelicaModel {
.position(|ref x| unsolved_vars.intersection(&x.identifiers()).count() == 1);
let eqn = match next_i {
None => {
- return Err("NaiveImplementation (or poor equation selection?)".to_string());
+ return bail!("NaiveImplementation (or poor equation selection?)");
}
Some(i) => unsolved_eqns.remove(i),
};
@@ -191,18 +191,18 @@ pub fn substitute_with(original: &Expr, a: &Expr, b: &Expr) -> Expr {
}
pub trait SimpleEquationExt {
- fn rebalance_for(&self, ident: String) -> Result<SimpleEquation, String>;
- fn simplify_lhs(&self, ident: &str) -> Result<SimpleEquation, String>;
+ fn rebalance_for(&self, ident: String) -> Result<SimpleEquation>;
+ fn simplify_lhs(&self, ident: &str) -> Result<SimpleEquation>;
}
impl SimpleEquationExt for SimpleEquation {
- fn rebalance_for(&self, ident: String) -> Result<SimpleEquation, String> {
+ fn rebalance_for(&self, ident: String) -> Result<SimpleEquation> {
let lvars = self.lhs.identifiers();
let rvars = self.rhs.identifiers();
let ret = match (lvars.contains(&ident), rvars.contains(&ident)) {
- (true, true) => Err("SymbolicError: NaiveImplementation".to_string()),
- (false, false) => Err("SymbolicError: VariableNotFound".to_string()),
+ (true, true) => bail!("SymbolicError: NaiveImplementation"),
+ (false, false) => bail!("SymbolicError: VariableNotFound"),
(true, false) => self.simplify_lhs(&ident),
(false, true) => {
SimpleEquation {
@@ -215,7 +215,7 @@ impl SimpleEquationExt for SimpleEquation {
match ret {
Ok(eqn) => {
if eqn.rhs.contains(&ident) {
- Err("SymbolicError: NaiveImplementation".to_string())
+ bail!("SymbolicError: NaiveImplementation")
} else {
Ok(eqn)
}
@@ -224,18 +224,18 @@ impl SimpleEquationExt for SimpleEquation {
}
}
- fn simplify_lhs(&self, ident: &str) -> Result<SimpleEquation, String> {
+ fn simplify_lhs(&self, ident: &str) -> Result<SimpleEquation> {
use modelica_parser::Expr::*;
use modelica_parser::BinOperator::*;
match self.lhs {
Ident(ref s) if s == ident => Ok((*self).clone()),
Ident(_) | Integer(_) | Float(_) | Boolean(_) | StringLiteral(_) => {
- Err("SymbolicError: InternalError: expected var on LHS".to_string())
+ bail!("SymbolicError: InternalError: expected var on LHS")
}
Der(_) |
MathUnaryExpr(_, _) |
Sign(_) |
- Array(_) => Err("SymbolicError: NaiveImplementation: can't simplify".to_string()),
+ Array(_) => bail!("SymbolicError: NaiveImplementation: can't simplify"),
// TODO: create a macro for the below...
BinExpr(Multiply, ref a, ref b) if a.contains(ident) => {
SimpleEquation {
@@ -294,8 +294,7 @@ impl SimpleEquationExt for SimpleEquation {
.simplify_lhs(&ident)
}
BinExpr(_, _, _) => {
- Err("SymbolicError: NotImplemented BinOperator (or else couldn't find var...)"
- .to_string())
+ bail!("SymbolicError: NotImplemented BinOperator (or else couldn't find var...)")
}
// in case we add opers: _ => Err("NotImplemented".to_string()),
}