From ce4a6b3925de0b015ea56365d3226164fc61e8ca Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Sat, 26 Nov 2022 17:00:01 -0500 Subject: [PATCH] #2418 fixing tests, unit tests should finish now --- pybamm/models/base_model.py | 7 +++++++ .../test_models/standard_output_comparison.py | 6 ++++-- tests/integration/test_solvers/test_idaklu.py | 2 +- tests/unit/test_solvers/test_idaklu_solver.py | 2 +- .../test_finite_volume/test_extrapolation.py | 12 +++++++----- .../test_finite_volume/test_finite_volume.py | 8 ++++---- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/pybamm/models/base_model.py b/pybamm/models/base_model.py index 47686191dc..2fc6e8bc02 100644 --- a/pybamm/models/base_model.py +++ b/pybamm/models/base_model.py @@ -1106,6 +1106,13 @@ def process_parameters_and_discretise(self, symbol, parameter_values, disc): # Set y slices if disc.y_slices == {}: variables = list(self.rhs.keys()) + list(self.algebraic.keys()) + for variable in variables: + variable.bounds = tuple( + [ + parameter_values.process_symbol(bound) + for bound in variable.bounds + ] + ) disc.set_variable_slices(variables) # Set boundary conditions (also requires setting parameter values) diff --git a/tests/integration/test_models/standard_output_comparison.py b/tests/integration/test_models/standard_output_comparison.py index f3d803f4c4..925fbfc508 100644 --- a/tests/integration/test_models/standard_output_comparison.py +++ b/tests/integration/test_models/standard_output_comparison.py @@ -98,10 +98,12 @@ def test_all(self): self.compare("X-averaged open circuit voltage [V]") # Currents self.compare( - "X-averaged negative electrode volumetric interfacial current density [A.m-3]" + "X-averaged negative electrode volumetric " + "interfacial current density [A.m-3]" ) self.compare( - "X-averaged positive electrode volumetric interfacial current density [A.m-3]" + "X-averaged positive electrode volumetric " + "interfacial current density [A.m-3]" ) # Concentration self.compare("X-averaged electrolyte concentration [mol.m-3]") diff --git a/tests/integration/test_solvers/test_idaklu.py b/tests/integration/test_solvers/test_idaklu.py index e97547424f..760f3e01bc 100644 --- a/tests/integration/test_solvers/test_idaklu.py +++ b/tests/integration/test_solvers/test_idaklu.py @@ -75,7 +75,7 @@ def test_set_tol_by_variable(self): t_eval = np.linspace(0, 3600, 100) solver = pybamm.IDAKLUSolver() - variable_tols = {"Porosity times concentration": 1e-3} + variable_tols = {"Porosity times concentration [mol.m-3]": 1e-3} solver.set_atol_by_variable(variable_tols, model) solver.solve(model, t_eval) diff --git a/tests/unit/test_solvers/test_idaklu_solver.py b/tests/unit/test_solvers/test_idaklu_solver.py index b080f14ca5..e7b33bea97 100644 --- a/tests/unit/test_solvers/test_idaklu_solver.py +++ b/tests/unit/test_solvers/test_idaklu_solver.py @@ -341,7 +341,7 @@ def test_set_atol(self): disc.process_model(model) solver = pybamm.IDAKLUSolver() - variable_tols = {"Porosity times concentration": 1e-3} + variable_tols = {"Porosity times concentration [mol.m-3]": 1e-3} solver.set_atol_by_variable(variable_tols, model) model = pybamm.BaseModel() diff --git a/tests/unit/test_spatial_methods/test_finite_volume/test_extrapolation.py b/tests/unit/test_spatial_methods/test_finite_volume/test_extrapolation.py index 0fec605695..f17a158892 100644 --- a/tests/unit/test_spatial_methods/test_finite_volume/test_extrapolation.py +++ b/tests/unit/test_spatial_methods/test_finite_volume/test_extrapolation.py @@ -268,7 +268,9 @@ def test_linear_extrapolate_left_right(self): # check linear variable extrapolates correctly linear_y = macro_submesh.nodes - self.assertEqual(extrap_left_disc.evaluate(None, linear_y), 0) + np.testing.assert_array_almost_equal( + extrap_left_disc.evaluate(None, linear_y), 0 + ) np.testing.assert_array_almost_equal( extrap_right_disc.evaluate(None, linear_y), 3 ) @@ -280,12 +282,12 @@ def test_linear_extrapolate_left_right(self): extrap_flux_right_disc = disc.process_symbol(extrap_flux_right) # check constant extrapolates to constant - self.assertEqual(extrap_flux_left_disc.evaluate(None, constant_y), 0) - self.assertEqual(extrap_flux_right_disc.evaluate(None, constant_y), 0) + np.testing.assert_allclose(extrap_flux_left_disc.evaluate(y=constant_y), 0) + np.testing.assert_allclose(extrap_flux_right_disc.evaluate(y=constant_y), 0) # check linear variable extrapolates correctly - self.assertEqual(extrap_flux_left_disc.evaluate(None, linear_y), 2) - self.assertEqual(extrap_flux_right_disc.evaluate(None, linear_y), -1) + np.testing.assert_allclose(extrap_flux_left_disc.evaluate(y=linear_y), 2) + np.testing.assert_allclose(extrap_flux_right_disc.evaluate(y=linear_y), -1) # Microscale # create variable diff --git a/tests/unit/test_spatial_methods/test_finite_volume/test_finite_volume.py b/tests/unit/test_spatial_methods/test_finite_volume/test_finite_volume.py index f90497793a..29d6eedd77 100644 --- a/tests/unit/test_spatial_methods/test_finite_volume/test_finite_volume.py +++ b/tests/unit/test_spatial_methods/test_finite_volume/test_finite_volume.py @@ -258,11 +258,11 @@ def test_jacobian(self): grad_matrix = spatial_method.gradient_matrix( whole_cell, {"primary": whole_cell} ).entries - np.testing.assert_array_equal(jacobian.toarray()[1:-1], grad_matrix.toarray()) - np.testing.assert_array_equal( + np.testing.assert_allclose(jacobian.toarray()[1:-1], grad_matrix.toarray()) + np.testing.assert_allclose( jacobian.toarray()[0, 0], grad_matrix.toarray()[0][0] * -2 ) - np.testing.assert_array_almost_equal( + np.testing.assert_allclose( jacobian.toarray()[-1, -1], grad_matrix.toarray()[-1][-1] * -2 ) @@ -360,7 +360,7 @@ def test_delta_function(self): var_disc = disc.process_symbol(var) x = pybamm.standard_spatial_vars.x_n delta_fn_int_disc = disc.process_symbol(pybamm.Integral(delta_fn_left, x)) - np.testing.assert_array_equal( + np.testing.assert_allclose( var_disc.evaluate(y=y) * mesh["negative electrode"].edges[-1], np.sum(delta_fn_int_disc.evaluate(y=y)), )