From 3edd39aef2c6c0f0c243556d8c563c741dbef594 Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Fri, 12 Jun 2020 17:55:32 -0400 Subject: [PATCH] #632 fixing tests --- pybamm/discretisations/discretisation.py | 42 +++++++++++-------- pybamm/expression_tree/symbol.py | 2 +- .../submodels/interface/diffusion_limited.py | 2 +- .../spatial_methods/scikit_finite_element.py | 8 ++-- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index 9fb812458e..b7958687de 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -4,7 +4,7 @@ import pybamm import numpy as np from collections import defaultdict, OrderedDict -from scipy.sparse import block_diag, csc_matrix, csr_matrix +from scipy.sparse import block_diag, csc_matrix, csr_matrix, issparse from scipy.sparse.linalg import inv @@ -1106,16 +1106,14 @@ def check_initial_conditions_rhs(self, model): y0 = model.concatenated_initial_conditions # Individual for var in model.rhs.keys(): - assert ( - model.rhs[var].shape == model.initial_conditions[var].shape - ), pybamm.ModelError( - """ - rhs and initial_conditions must have the same shape after discretisation - but rhs.shape = {} and initial_conditions.shape = {} for variable '{}'. - """.format( - model.rhs[var].shape, model.initial_conditions[var].shape, var + if not model.rhs[var].shape == model.initial_conditions[var].shape: + raise pybamm.ModelError( + "rhs and initial_conditions must have the same shape after " + "discretisation but rhs.shape = " + "{} and initial_conditions.shape = {} for variable '{}'.".format( + model.rhs[var].shape, model.initial_conditions[var].shape, var + ) ) - ) # Concatenated assert ( model.concatenated_rhs.shape[0] + model.concatenated_algebraic.shape[0] @@ -1150,17 +1148,27 @@ def check_variables(self, model): not_concatenation = not isinstance(var, pybamm.Concatenation) not_mult_by_one_vec = not ( - isinstance(var, pybamm.Multiplication) - and isinstance(var.right, pybamm.Vector) - and np.all(var.right.entries == 1) + isinstance( + var, (pybamm.Multiplication, pybamm.MatrixMultiplication) + ) + and (is_array_one(var.left) or is_array_one(var.right)) ) if different_shapes and not_concatenation and not_mult_by_one_vec: raise pybamm.ModelError( - """ - variable and its eqn must have the same shape after discretisation - but variable.shape = {} and rhs.shape = {} for variable '{}'. - """.format( + "variable and its eqn must have the same shape after " + "discretisation but variable.shape = " + "{} and rhs.shape = {} for variable '{}'. ".format( var.shape, model.rhs[rhs_var].shape, var ) ) + + +def is_array_one(symbol): + if not isinstance(symbol, pybamm.Array): + return False + entries = symbol.entries + if issparse(entries): + return np.all(entries.toarray() == 1) + else: + return np.all(entries == 1) diff --git a/pybamm/expression_tree/symbol.py b/pybamm/expression_tree/symbol.py index 2235d98209..6693d22acd 100644 --- a/pybamm/expression_tree/symbol.py +++ b/pybamm/expression_tree/symbol.py @@ -666,7 +666,7 @@ def evaluates_to_number(self): result = self.evaluate_ignoring_errors() if isinstance(result, numbers.Number) or ( - isinstance(result, np.ndarray) and result.shape == () + isinstance(result, np.ndarray) and np.prod(result.shape) == 1 ): return True else: diff --git a/pybamm/models/submodels/interface/diffusion_limited.py b/pybamm/models/submodels/interface/diffusion_limited.py index 6de23c2789..2f2890000f 100644 --- a/pybamm/models/submodels/interface/diffusion_limited.py +++ b/pybamm/models/submodels/interface/diffusion_limited.py @@ -145,7 +145,7 @@ def _get_j_diffusion_limited_first_order(self, variables): param = self.param if self.domain == "Negative": N_ox_s_p = variables["Oxygen flux"].orphans[1] - N_ox_neg_sep_interface = N_ox_s_p[0] + N_ox_neg_sep_interface = pybamm.Index(N_ox_s_p, slice(0, 1)) j = -N_ox_neg_sep_interface / param.C_e / -param.s_ox_Ox / param.l_n diff --git a/pybamm/spatial_methods/scikit_finite_element.py b/pybamm/spatial_methods/scikit_finite_element.py index 52c8d15faf..f7ab2fdc45 100644 --- a/pybamm/spatial_methods/scikit_finite_element.py +++ b/pybamm/spatial_methods/scikit_finite_element.py @@ -303,7 +303,7 @@ def integral(self, child, discretised_child, integration_dimension): return out - def definite_integral_matrix(self, domains, vector_type="row"): + def definite_integral_matrix(self, child, vector_type="row"): """ Matrix for finite-element implementation of the definite integral over the entire domain @@ -315,8 +315,8 @@ def definite_integral_matrix(self, domains, vector_type="row"): Parameters ---------- - domains : dict - The domain(s) of integration + child : :class:`pybamm.Symbol` + The symbol being integrated vector_type : str, optional Whether to return a row or column vector (default is row) @@ -326,7 +326,7 @@ def definite_integral_matrix(self, domains, vector_type="row"): The finite element integral vector for the domain """ # get primary domain mesh - domain = domains["primary"] + domain = child.domains["primary"] if isinstance(domain, list): domain = domain[0] mesh = self.mesh[domain]