From 36a9caf91d7edb144d62377c8edfd319ad537dc6 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 21 May 2024 14:04:57 +0100 Subject: [PATCH] Updated scipy and jax versions to fix `linalg.tri` deprecation (#4103) * #3959 updated scipy and jax versions to fix deprecation error * #3959 fix issue with vstack array dimensions * style: pre-commit fixes * #3959 use direct solver for interpolant * Update pyproject.toml Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> * #3959 use jax and jaxlib 0.4.27 * #3959 revert to iterative solver for interpolator and relax test tolerances * style: pre-commit fixes * ruff * #3959 Eric's comments * #3959 reduce tolerances to fix macos-14 unit tests * Update pyproject.toml Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> * #3959 relax some more tolerances * #3959 reduce solve time in test to avoid overdischarge (aiming to fix macos failing test) * #3959 extend solving time to reach end of discharge --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Co-authored-by: Eric G. Kratz --- pybamm/spatial_methods/spectral_volume.py | 6 +++++- pyproject.toml | 6 +++--- .../test_lithium_ion/test_compare_outputs_two_phase.py | 2 +- tests/unit/test_expression_tree/test_interpolant.py | 4 ++-- .../test_finite_volume/test_integration.py | 10 +++++----- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pybamm/spatial_methods/spectral_volume.py b/pybamm/spatial_methods/spectral_volume.py index b8bee93b1d..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(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 diff --git a/pyproject.toml b/pyproject.toml index 54ec27e2bd..b13a09933b 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.4", "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.27", + "jaxlib==0.4.27", ] # Contains all optional dependencies, except for jax and dev dependencies all = [ 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..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, 9000, 1000) + t_eval = np.linspace(0, 8000, 1000) inputs = [{"x": 0.01}, {"x": 0.1}] sol = sim.solve(t_eval, inputs=inputs) diff --git a/tests/unit/test_expression_tree/test_interpolant.py b/tests/unit/test_expression_tree/test_interpolant.py index 86f52c1870..23343fe72a 100644 --- a/tests/unit/test_expression_tree/test_interpolant.py +++ b/tests/unit/test_expression_tree/test_interpolant.py @@ -133,7 +133,7 @@ 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)) + 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 +231,7 @@ 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)) + 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)) 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..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 @@ -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] @@ -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 @@ -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 @@ -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