From af198f95b5a4e935555f7b43a24a08eb2b461cd4 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Mon, 20 May 2024 14:50:31 +0100 Subject: [PATCH 01/15] #3959 updated scipy and jax versions to fix deprecation error --- pyproject.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 54ec27e2bd..cd27374461 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,7 @@ classifiers = [ ] dependencies = [ "numpy>=1.23.5", - "scipy>=1.9.3,<1.13.0", + "scipy>=1.11.0", "casadi>=3.6.5", "xarray>=2022.6.0", "anytree>=2.8.0", @@ -113,8 +113,8 @@ dev = [ ] # For the Jax solver. Note: these must be kept in sync with the versions defined in pybamm/util.py. jax = [ - "jax==0.4.20; python_version >= '3.9'", - "jaxlib==0.4.20; python_version >= '3.9'", + "jax==0.4.28; python_version >= '3.9'", + "jaxlib==0.4.28; python_version >= '3.9'", ] # Contains all optional dependencies, except for jax and dev dependencies all = [ From 0fe4ed2764de1c3a9409a69b080cc89767cdd58c Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Mon, 20 May 2024 15:31:34 +0100 Subject: [PATCH 02/15] #3959 fix issue with vstack array dimensions --- pybamm/spatial_methods/spectral_volume.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pybamm/spatial_methods/spectral_volume.py b/pybamm/spatial_methods/spectral_volume.py index b8bee93b1d..a44eadd363 100644 --- a/pybamm/spatial_methods/spectral_volume.py +++ b/pybamm/spatial_methods/spectral_volume.py @@ -391,7 +391,7 @@ def penalty_matrix(self, domains): e = np.zeros(n - 1) e[d - 1 :: d] = 1 / submesh.d_nodes[d - 1 :: d] sub_matrix = vstack( - [np.zeros(n), diags([-e, e], [0, 1], shape=(n - 1, n)), np.zeros(n)] + [np.zeros((1, n)), diags([-e, e], [0, 1], shape=(n - 1, n)), np.zeros((1, n))] ) # number of repeats From 0d7e635f9cd8c9849e47faaba13f02df1aaaf6fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 14:32:25 +0000 Subject: [PATCH 03/15] style: pre-commit fixes --- pybamm/spatial_methods/spectral_volume.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pybamm/spatial_methods/spectral_volume.py b/pybamm/spatial_methods/spectral_volume.py index a44eadd363..11c6dfd6d2 100644 --- a/pybamm/spatial_methods/spectral_volume.py +++ b/pybamm/spatial_methods/spectral_volume.py @@ -391,7 +391,11 @@ def penalty_matrix(self, domains): e = np.zeros(n - 1) e[d - 1 :: d] = 1 / submesh.d_nodes[d - 1 :: d] sub_matrix = vstack( - [np.zeros((1, n)), diags([-e, e], [0, 1], shape=(n - 1, n)), np.zeros((1, n))] + [ + np.zeros((1, n)), + diags([-e, e], [0, 1], shape=(n - 1, n)), + np.zeros((1, n)), + ] ) # number of repeats From 9362c7ac0fa5c63aa59a00a7ec007774af02f758 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Mon, 20 May 2024 17:19:59 +0100 Subject: [PATCH 04/15] #3959 use direct solver for interpolant --- pybamm/expression_tree/interpolant.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pybamm/expression_tree/interpolant.py b/pybamm/expression_tree/interpolant.py index dd0980fb46..71cb79f406 100644 --- a/pybamm/expression_tree/interpolant.py +++ b/pybamm/expression_tree/interpolant.py @@ -4,6 +4,7 @@ from __future__ import annotations import numpy as np from scipy import interpolate +from scipy.sparse.linalg import spsolve from collections.abc import Sequence import numbers @@ -150,12 +151,17 @@ def __init__( fill_value = None else: fill_value = np.nan + if interpolator == "cubic": + solver = spsolve + else: + solver = None interpolating_function = interpolate.RegularGridInterpolator( (x1, x2), y, method=interpolator, bounds_error=False, fill_value=fill_value, + solver=solver, ) elif len(x) == 3: @@ -173,12 +179,17 @@ def __init__( for 3D interpolation""" ) else: + if interpolator == "cubic": + solver = spsolve + else: + solver = None interpolating_function = interpolate.RegularGridInterpolator( (x1, x2, x3), y, method=interpolator, bounds_error=False, fill_value=fill_value, + solver=solver, ) else: raise ValueError(f"Invalid dimension of x: {len(x)}") From a01daabd526aeb216593471a5e75e84a6599b5f4 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Mon, 20 May 2024 18:42:30 +0100 Subject: [PATCH 05/15] Update pyproject.toml Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cd27374461..13a0251141 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -113,8 +113,8 @@ dev = [ ] # For the Jax solver. Note: these must be kept in sync with the versions defined in pybamm/util.py. jax = [ - "jax==0.4.28; python_version >= '3.9'", - "jaxlib==0.4.28; python_version >= '3.9'", + "jax==0.4.28", + "jaxlib==0.4.28", ] # Contains all optional dependencies, except for jax and dev dependencies all = [ From 3abc48281cd7a008dc5375f885f53e4031d218c2 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Mon, 20 May 2024 18:43:32 +0100 Subject: [PATCH 06/15] #3959 use jax and jaxlib 0.4.27 --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 13a0251141..957f07f6ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -113,8 +113,8 @@ dev = [ ] # For the Jax solver. Note: these must be kept in sync with the versions defined in pybamm/util.py. jax = [ - "jax==0.4.28", - "jaxlib==0.4.28", + "jax==0.4.27", + "jaxlib==0.4.27", ] # Contains all optional dependencies, except for jax and dev dependencies all = [ From fc3aa8e6835698ab483824cd529b6ae599b5a399 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Mon, 20 May 2024 18:49:45 +0100 Subject: [PATCH 07/15] #3959 revert to iterative solver for interpolator and relax test tolerances --- pybamm/expression_tree/interpolant.py | 10 ---------- tests/unit/test_expression_tree/test_interpolant.py | 6 ++++-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/pybamm/expression_tree/interpolant.py b/pybamm/expression_tree/interpolant.py index 71cb79f406..61fddccf9d 100644 --- a/pybamm/expression_tree/interpolant.py +++ b/pybamm/expression_tree/interpolant.py @@ -151,17 +151,12 @@ def __init__( fill_value = None else: fill_value = np.nan - if interpolator == "cubic": - solver = spsolve - else: - solver = None interpolating_function = interpolate.RegularGridInterpolator( (x1, x2), y, method=interpolator, bounds_error=False, fill_value=fill_value, - solver=solver, ) elif len(x) == 3: @@ -179,17 +174,12 @@ def __init__( for 3D interpolation""" ) else: - if interpolator == "cubic": - solver = spsolve - else: - solver = None interpolating_function = interpolate.RegularGridInterpolator( (x1, x2, x3), y, method=interpolator, bounds_error=False, fill_value=fill_value, - solver=solver, ) else: raise ValueError(f"Invalid dimension of x: {len(x)}") diff --git a/tests/unit/test_expression_tree/test_interpolant.py b/tests/unit/test_expression_tree/test_interpolant.py index 86f52c1870..a10673bea4 100644 --- a/tests/unit/test_expression_tree/test_interpolant.py +++ b/tests/unit/test_expression_tree/test_interpolant.py @@ -133,7 +133,8 @@ def f(x, y): # check also works for cubic interp = pybamm.Interpolant(x_in, data, (var1, var2), interpolator="cubic") value = interp.evaluate(y=np.array([1, 5])) - np.testing.assert_equal(value, f(1, 5)) + # relaxed tolerance as from Scipy 1.13 it uses iterative solver + np.testing.assert_almost_equal(value, f(1, 5), decimal=3) # Test raising error if data is not 2D data_3d = np.zeros((11, 22, 33)) @@ -231,7 +232,8 @@ def f(x, y, z): x_in, data, (var1, var2, var3), interpolator="cubic" ) value = interp.evaluate(y=np.array([1, 5, 8])) - np.testing.assert_equal(value, f(1, 5, 8)) + # relaxed tolerance as from Scipy 1.13 it uses iterative solver + np.testing.assert_almost_equal(value, f(1, 5, 8), decimal=3) # Test raising error if data is not 3D data_4d = np.zeros((11, 22, 33, 5)) From e15aafa35c28ff6a0c292b63911c47ed76304bfd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 17:50:30 +0000 Subject: [PATCH 08/15] style: pre-commit fixes --- pybamm/expression_tree/interpolant.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pybamm/expression_tree/interpolant.py b/pybamm/expression_tree/interpolant.py index 61fddccf9d..dd0980fb46 100644 --- a/pybamm/expression_tree/interpolant.py +++ b/pybamm/expression_tree/interpolant.py @@ -4,7 +4,6 @@ from __future__ import annotations import numpy as np from scipy import interpolate -from scipy.sparse.linalg import spsolve from collections.abc import Sequence import numbers From b5b7e43e81aeb6ff2c1637a201ca46427a57e741 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Mon, 20 May 2024 18:50:55 +0100 Subject: [PATCH 09/15] ruff --- pybamm/expression_tree/interpolant.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pybamm/expression_tree/interpolant.py b/pybamm/expression_tree/interpolant.py index 61fddccf9d..dd0980fb46 100644 --- a/pybamm/expression_tree/interpolant.py +++ b/pybamm/expression_tree/interpolant.py @@ -4,7 +4,6 @@ from __future__ import annotations import numpy as np from scipy import interpolate -from scipy.sparse.linalg import spsolve from collections.abc import Sequence import numbers From 786385ca76f36d734e3bf43c3dd064a3204061f9 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Mon, 20 May 2024 18:52:28 +0100 Subject: [PATCH 10/15] #3959 Eric's comments --- tests/unit/test_expression_tree/test_interpolant.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/unit/test_expression_tree/test_interpolant.py b/tests/unit/test_expression_tree/test_interpolant.py index a10673bea4..23343fe72a 100644 --- a/tests/unit/test_expression_tree/test_interpolant.py +++ b/tests/unit/test_expression_tree/test_interpolant.py @@ -133,7 +133,6 @@ def f(x, y): # check also works for cubic interp = pybamm.Interpolant(x_in, data, (var1, var2), interpolator="cubic") value = interp.evaluate(y=np.array([1, 5])) - # relaxed tolerance as from Scipy 1.13 it uses iterative solver np.testing.assert_almost_equal(value, f(1, 5), decimal=3) # Test raising error if data is not 2D @@ -232,7 +231,6 @@ def f(x, y, z): x_in, data, (var1, var2, var3), interpolator="cubic" ) value = interp.evaluate(y=np.array([1, 5, 8])) - # relaxed tolerance as from Scipy 1.13 it uses iterative solver np.testing.assert_almost_equal(value, f(1, 5, 8), decimal=3) # Test raising error if data is not 3D From 6b9f3782fcb5ccd7136cc021428016b0a9cc6015 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 21 May 2024 11:22:30 +0100 Subject: [PATCH 11/15] #3959 reduce tolerances to fix macos-14 unit tests --- .../test_finite_volume/test_integration.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_spatial_methods/test_finite_volume/test_integration.py b/tests/unit/test_spatial_methods/test_finite_volume/test_integration.py index f1908b1238..494e68cb1b 100644 --- a/tests/unit/test_spatial_methods/test_finite_volume/test_integration.py +++ b/tests/unit/test_spatial_methods/test_finite_volume/test_integration.py @@ -339,7 +339,7 @@ def test_indefinite_integral(self): phi_exact = np.ones((submesh.npts, 1)) phi_approx = int_grad_phi_disc.evaluate(None, phi_exact) phi_approx += 1 # add constant of integration - np.testing.assert_array_equal(phi_exact, phi_approx) + np.testing.assert_array_almost_equal(phi_exact, phi_approx) self.assertEqual(left_boundary_value_disc.evaluate(y=phi_exact), 0) # linear case phi_exact = submesh.nodes[:, np.newaxis] @@ -488,7 +488,7 @@ def test_backward_indefinite_integral(self): phi_exact = np.ones((submesh.npts, 1)) phi_approx = int_grad_phi_disc.evaluate(None, phi_exact) phi_approx += 1 # add constant of integration - np.testing.assert_array_equal(phi_exact, phi_approx) + np.testing.assert_array_almost_equal(phi_exact, phi_approx) self.assertEqual(right_boundary_value_disc.evaluate(y=phi_exact), 0) # linear case From fbfbdebd0fdd279058465fabbf7e225d7f865fe6 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 21 May 2024 11:25:34 +0100 Subject: [PATCH 12/15] Update pyproject.toml Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 957f07f6ab..b13a09933b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,7 @@ classifiers = [ ] dependencies = [ "numpy>=1.23.5", - "scipy>=1.11.0", + "scipy>=1.11.4", "casadi>=3.6.5", "xarray>=2022.6.0", "anytree>=2.8.0", From 71b5f43972bfca6b2fb2c7e705b6d245048b922b Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 21 May 2024 11:36:25 +0100 Subject: [PATCH 13/15] #3959 relax some more tolerances --- .../test_finite_volume/test_integration.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_spatial_methods/test_finite_volume/test_integration.py b/tests/unit/test_spatial_methods/test_finite_volume/test_integration.py index 494e68cb1b..57113259c1 100644 --- a/tests/unit/test_spatial_methods/test_finite_volume/test_integration.py +++ b/tests/unit/test_spatial_methods/test_finite_volume/test_integration.py @@ -379,7 +379,7 @@ def test_indefinite_integral(self): phi_exact = np.ones((submesh.npts, 1)) phi_approx = int_grad_phi_disc.evaluate(None, phi_exact) phi_approx += 1 # add constant of integration - np.testing.assert_array_equal(phi_exact, phi_approx) + np.testing.assert_array_almost_equal(phi_exact, phi_approx) self.assertEqual(left_boundary_value_disc.evaluate(y=phi_exact), 0) # linear case @@ -440,7 +440,7 @@ def test_indefinite_integral(self): c_exact = np.ones((submesh.npts, 1)) c_approx = c_integral_disc.evaluate(None, c_exact) c_approx += 1 # add constant of integration - np.testing.assert_array_equal(c_exact, c_approx) + np.testing.assert_array_almost_equal(c_exact, c_approx) self.assertEqual(left_boundary_value_disc.evaluate(y=c_exact), 0) # linear case @@ -561,7 +561,7 @@ def test_indefinite_integral_on_nodes(self): phi_exact = np.ones((submesh.npts, 1)) int_phi_exact = submesh.edges int_phi_approx = int_phi_disc.evaluate(None, phi_exact).flatten() - np.testing.assert_array_equal(int_phi_exact, int_phi_approx) + np.testing.assert_array_almost_equal(int_phi_exact, int_phi_approx) # linear case phi_exact = submesh.nodes int_phi_exact = submesh.edges**2 / 2 From c30f82b91c16444f0b309a2fb02fcae6d6b8d9fa Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 21 May 2024 12:18:10 +0100 Subject: [PATCH 14/15] #3959 reduce solve time in test to avoid overdischarge (aiming to fix macos failing test) --- .../test_lithium_ion/test_compare_outputs_two_phase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs_two_phase.py b/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs_two_phase.py index 6a62faeeb9..9ed81fbf25 100644 --- a/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs_two_phase.py +++ b/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs_two_phase.py @@ -144,7 +144,7 @@ def compare_outputs_two_phase_silicon_graphite(self, model_class): ) sim = pybamm.Simulation(model, parameter_values=param) - t_eval = np.linspace(0, 9000, 1000) + t_eval = np.linspace(0, 7200, 1000) inputs = [{"x": 0.01}, {"x": 0.1}] sol = sim.solve(t_eval, inputs=inputs) From 5a237eaee1c75dbf2245168140d9957f311c6f62 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 21 May 2024 12:36:46 +0100 Subject: [PATCH 15/15] #3959 extend solving time to reach end of discharge --- .../test_lithium_ion/test_compare_outputs_two_phase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs_two_phase.py b/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs_two_phase.py index 9ed81fbf25..b0c0fe5898 100644 --- a/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs_two_phase.py +++ b/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs_two_phase.py @@ -144,7 +144,7 @@ def compare_outputs_two_phase_silicon_graphite(self, model_class): ) sim = pybamm.Simulation(model, parameter_values=param) - t_eval = np.linspace(0, 7200, 1000) + t_eval = np.linspace(0, 8000, 1000) inputs = [{"x": 0.01}, {"x": 0.1}] sol = sim.solve(t_eval, inputs=inputs)