Skip to content

Commit c4958f5

Browse files
Aswinr24medha-14
authored andcommitted
chore: switch to np.testing.assert_allclose for better precision in tests (#4789)
* chore: switch to np.testing.assert_allclose for better precision in tests * fix: adjust tolerance values in upwind/downwind tests * chore: switch to np.testing.assert_allclose for better precision in tests-v2 * fix: replace 10** notation with exponential notation for consistency * Fix: adjust absolute tolerance to a consistent value of 1e-6 * Retrigger CI
1 parent 69f9e27 commit c4958f5

File tree

12 files changed

+142
-89
lines changed

12 files changed

+142
-89
lines changed

tests/integration/test_experiments.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ def test_discharge_rest_charge(self):
2020
model, experiment=experiment, solver=pybamm.CasadiSolver()
2121
)
2222
sim.solve()
23-
np.testing.assert_array_almost_equal(
23+
np.testing.assert_allclose(
2424
sim._solution["Time [h]"].entries,
2525
np.array([0, 0.5, 1, 1 + 1e-9, 1.5, 2, 2 + 1e-9, 2.5, 3]),
26+
rtol=1e-7,
27+
atol=1e-6,
2628
)
2729
cap = model.default_parameter_values["Nominal cell capacity [A.h]"]
28-
np.testing.assert_array_almost_equal(
30+
np.testing.assert_allclose(
2931
sim._solution["Current [A]"].entries,
3032
[cap / 2] * 3 + [0] * 3 + [-cap / 2] * 3,
33+
rtol=1e-7,
34+
atol=1e-6,
3135
)
3236

3337
def test_rest_discharge_rest(self):
@@ -45,8 +49,10 @@ def test_rest_discharge_rest(self):
4549
solver=pybamm.CasadiSolver(),
4650
)
4751
sol = sim.solve()
48-
np.testing.assert_array_almost_equal(sol["Current [A]"].data[:5], 0)
49-
np.testing.assert_array_almost_equal(sol["Current [A]"].data[-29:], 0)
52+
np.testing.assert_allclose(sol["Current [A]"].data[:5], 0, rtol=1e-7, atol=1e-6)
53+
np.testing.assert_allclose(
54+
sol["Current [A]"].data[-29:], 0, rtol=1e-7, atol=1e-6
55+
)
5056

5157
def test_gitt(self):
5258
experiment = pybamm.Experiment(
@@ -58,9 +64,11 @@ def test_gitt(self):
5864
)
5965
sim.solve()
6066
cap = model.default_parameter_values["Nominal cell capacity [A.h]"]
61-
np.testing.assert_array_almost_equal(
67+
np.testing.assert_allclose(
6268
sim._solution["Current [A]"].entries,
6369
[cap / 20] * 11 + [0] * 11 + ([cap / 20] * 11 + [0] * 11) * 9,
70+
rtol=1e-7,
71+
atol=1e-6,
6472
)
6573

6674
def test_infeasible(self):

tests/integration/test_models/standard_model_tests.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ def test_serialisation(self, solver=None, t_eval=None):
167167
new_solver.rtol = 1e-8
168168
new_solver.atol = 1e-8
169169

170-
accuracy = 5
171-
172170
Crate = abs(
173171
self.parameter_values["Current function [A]"]
174172
/ self.parameter_values["Nominal cell capacity [A.h]"]
@@ -182,8 +180,11 @@ def test_serialisation(self, solver=None, t_eval=None):
182180
new_solution = new_solver.solve(new_model, t_eval)
183181

184182
for x, _ in enumerate(self.solution.all_ys):
185-
np.testing.assert_array_almost_equal(
186-
new_solution.all_ys[x], self.solution.all_ys[x], decimal=accuracy
183+
np.testing.assert_allclose(
184+
new_solution.all_ys[x],
185+
self.solution.all_ys[x],
186+
rtol=1e-6,
187+
atol=1e-6,
187188
)
188189
temp.close()
189190

tests/integration/test_models/standard_output_tests.py

Lines changed: 73 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ def test_each_reaction_overpotential(self):
163163
np.testing.assert_array_less(self.eta_r_n(t, x_n), tol)
164164
np.testing.assert_array_less(-self.eta_r_p(t, x_p), tol)
165165
elif self.operating_condition == "off":
166-
np.testing.assert_array_almost_equal(self.eta_r_n(t, x_n), 0)
167-
np.testing.assert_array_almost_equal(-self.eta_r_p(t, x_p), 0)
166+
np.testing.assert_allclose(self.eta_r_n(t, x_n), 0, rtol=1e-7, atol=1e-6)
167+
np.testing.assert_allclose(-self.eta_r_p(t, x_p), 0, rtol=1e-7, atol=1e-6)
168168

169169
def test_overpotentials(self):
170170
"""Testing that all are:
@@ -183,8 +183,8 @@ def test_overpotentials(self):
183183
np.testing.assert_array_less(-self.delta_phi_s_av(self.t), tol)
184184

185185
elif self.operating_condition == "off":
186-
np.testing.assert_array_almost_equal(self.eta_r_av(self.t), 0)
187-
np.testing.assert_array_almost_equal(self.eta_e_av(self.t), 0, decimal=11)
186+
np.testing.assert_allclose(self.eta_r_av(self.t), 0, rtol=1e-7, atol=1e-6)
187+
np.testing.assert_allclose(self.eta_e_av(self.t), 0, rtol=1e-12, atol=1e-11)
188188
np.testing.assert_allclose(
189189
self.delta_phi_s_av(self.t), 0, atol=2e-14, rtol=1e-16
190190
)
@@ -204,8 +204,8 @@ def test_ocps(self):
204204
np.testing.assert_array_less(neg_end_vs_start, 0)
205205
np.testing.assert_array_less(-pos_end_vs_start, 0)
206206
elif self.operating_condition == "off":
207-
np.testing.assert_array_almost_equal(neg_end_vs_start, 0)
208-
np.testing.assert_array_almost_equal(pos_end_vs_start, 0)
207+
np.testing.assert_allclose(neg_end_vs_start, 0, rtol=1e-7, atol=1e-6)
208+
np.testing.assert_allclose(pos_end_vs_start, 0, rtol=1e-7, atol=1e-6)
209209

210210
def test_ocv(self):
211211
"""Testing that:
@@ -221,7 +221,7 @@ def test_ocv(self):
221221
elif self.operating_condition == "charge":
222222
np.testing.assert_array_less(-end_vs_start, 0)
223223
elif self.operating_condition == "off":
224-
np.testing.assert_array_almost_equal(end_vs_start, 0)
224+
np.testing.assert_allclose(end_vs_start, 0, rtol=1e-7, atol=1e-6)
225225

226226
def test_voltage(self):
227227
"""Testing that:
@@ -236,32 +236,41 @@ def test_voltage(self):
236236
elif self.operating_condition == "charge":
237237
np.testing.assert_array_less(-end_vs_start, 0)
238238
elif self.operating_condition == "off":
239-
np.testing.assert_array_almost_equal(end_vs_start, 0)
239+
np.testing.assert_allclose(end_vs_start, 0, rtol=1e-7, atol=1e-6)
240240

241241
def test_consistent(self):
242242
"""Test voltage components are consistent with one another by ensuring they sum
243243
correctly"""
244244

245-
np.testing.assert_array_almost_equal(
246-
self.ocv(self.t), self.ocp_p(self.t) - self.ocp_n(self.t)
245+
np.testing.assert_allclose(
246+
self.ocv(self.t),
247+
self.ocp_p(self.t) - self.ocp_n(self.t),
248+
rtol=1e-7,
249+
atol=1e-6,
247250
)
248-
np.testing.assert_array_almost_equal(
249-
self.eta_r_av(self.t), self.eta_r_p_av(self.t) - self.eta_r_n_av(self.t)
251+
np.testing.assert_allclose(
252+
self.eta_r_av(self.t),
253+
self.eta_r_p_av(self.t) - self.eta_r_n_av(self.t),
254+
rtol=1e-7,
255+
atol=1e-6,
250256
)
251-
np.testing.assert_array_almost_equal(
257+
np.testing.assert_allclose(
252258
self.eta_particle(self.t),
253259
self.eta_particle_p(self.t) - self.eta_particle_n(self.t),
260+
rtol=1e-7,
261+
atol=1e-6,
254262
)
255263

256-
np.testing.assert_array_almost_equal(
264+
np.testing.assert_allclose(
257265
self.voltage(self.t),
258266
self.ocv(self.t)
259267
+ self.eta_particle(self.t)
260268
+ self.eta_r_av(self.t)
261269
+ self.eta_e_av(self.t)
262270
+ self.delta_phi_s_av(self.t)
263271
+ self.eta_sei_av(self.t),
264-
decimal=5,
272+
rtol=1e-6,
273+
atol=1e-5,
265274
)
266275

267276
def test_all(self):
@@ -385,8 +394,8 @@ def test_concentration_increase_decrease(self):
385394
np.testing.assert_array_less(0, neg_end_vs_start)
386395
np.testing.assert_array_less(pos_end_vs_start, 0)
387396
elif self.operating_condition == "off":
388-
np.testing.assert_array_almost_equal(neg_diff, 0)
389-
np.testing.assert_array_almost_equal(pos_diff, 0)
397+
np.testing.assert_allclose(neg_diff, 0, rtol=1e-7, atol=1e-6)
398+
np.testing.assert_allclose(pos_diff, 0, rtol=1e-7, atol=1e-6)
390399
np.testing.assert_allclose(neg_end_vs_start, 0, rtol=1e-16, atol=1e-5)
391400
np.testing.assert_allclose(pos_end_vs_start, 0, rtol=1e-16, atol=1e-5)
392401

@@ -455,7 +464,8 @@ def test_conservation(self):
455464
decimal = 9
456465
else:
457466
decimal = 14
458-
np.testing.assert_array_almost_equal(diff, 0, decimal=decimal)
467+
atol = 10 ** (-decimal + 1)
468+
np.testing.assert_allclose(diff, 0, rtol=1e-7, atol=atol)
459469

460470
def test_concentration_profile(self):
461471
"""Test that the concentration in the centre of the negative particles is
@@ -479,8 +489,8 @@ def test_fluxes(self):
479489
)
480490
if self.model.options["particle"] == "uniform profile":
481491
# Fluxes are zero everywhere since the concentration is uniform
482-
np.testing.assert_array_almost_equal(self.N_s_n(t, x_n, r_n), 0)
483-
np.testing.assert_array_almost_equal(self.N_s_p(t, x_p, r_p), 0)
492+
np.testing.assert_allclose(self.N_s_n(t, x_n, r_n), 0, rtol=1e-7, atol=1e-6)
493+
np.testing.assert_allclose(self.N_s_p(t, x_p, r_p), 0, rtol=1e-7, atol=1e-6)
484494
else:
485495
if self.operating_condition == "discharge":
486496
if self.model.options["particle"] == "quartic profile":
@@ -498,11 +508,15 @@ def test_fluxes(self):
498508
np.testing.assert_array_less(self.N_s_n(t[1:], x_n, r_n[1:]), 1e-16)
499509
np.testing.assert_array_less(-1e-16, self.N_s_p(t[1:], x_p, r_p[1:]))
500510
if self.operating_condition == "off":
501-
np.testing.assert_array_almost_equal(self.N_s_n(t, x_n, r_n), 0)
502-
np.testing.assert_array_almost_equal(self.N_s_p(t, x_p, r_p), 0)
511+
np.testing.assert_allclose(
512+
self.N_s_n(t, x_n, r_n), 0, rtol=1e-7, atol=1e-6
513+
)
514+
np.testing.assert_allclose(
515+
self.N_s_p(t, x_p, r_p), 0, rtol=1e-7, atol=1e-6
516+
)
503517

504-
np.testing.assert_array_almost_equal(0, self.N_s_n(t, x_n, r_n[0]), decimal=4)
505-
np.testing.assert_array_almost_equal(0, self.N_s_p(t, x_p, r_p[0]), decimal=4)
518+
np.testing.assert_allclose(0, self.N_s_n(t, x_n, r_n[0]), rtol=1e-5, atol=1e-4)
519+
np.testing.assert_allclose(0, self.N_s_p(t, x_p, r_p[0]), rtol=1e-5, atol=1e-4)
506520

507521
def test_all(self):
508522
self.test_concentration_increase_decrease()
@@ -590,8 +604,8 @@ def test_fluxes(self):
590604
models (bug in implementation of boundary conditions?)"""
591605

592606
t, x = self.t, self.x_edge
593-
np.testing.assert_array_almost_equal(self.N_e_hat(t, x[0]), 0, decimal=3)
594-
np.testing.assert_array_almost_equal(self.N_e_hat(t, x[-1]), 0, decimal=3)
607+
np.testing.assert_allclose(self.N_e_hat(t, x[0]), 0, rtol=1e-4, atol=1e-3)
608+
np.testing.assert_allclose(self.N_e_hat(t, x[-1]), 0, rtol=1e-4, atol=1e-3)
595609

596610
def test_splitting(self):
597611
"""Test that when splitting the concentrations and fluxes by negative electrode,
@@ -604,7 +618,7 @@ def test_splitting(self):
604618

605619
# Loose tolerance since the different way that c_e and c_e_n/s/p are calculated
606620
# introduces some numerical error
607-
np.testing.assert_array_almost_equal(self.c_e(t, x), c_e_combined, decimal=12)
621+
np.testing.assert_allclose(self.c_e(t, x), c_e_combined, rtol=1e-13, atol=1e-12)
608622

609623
def test_all(self):
610624
self.test_concentration_limit()
@@ -660,7 +674,7 @@ def __init__(self, model, param, disc, solution, operating_condition):
660674
def test_negative_electrode_potential_profile(self):
661675
"""Test that negative electrode potential is zero on left boundary. Test
662676
average negative electrode potential is less than or equal to zero."""
663-
np.testing.assert_array_almost_equal(self.phi_s_n(self.t, x=0), 0, decimal=5)
677+
np.testing.assert_allclose(self.phi_s_n(self.t, x=0), 0, rtol=1e-6, atol=1e-5)
664678

665679
def test_positive_electrode_potential_profile(self):
666680
"""Test average positive electrode potential is less than the positive electrode
@@ -673,25 +687,35 @@ def test_potential_differences(self):
673687
potential and electrolyte potential"""
674688
t, x_n, x_p = self.t, self.x_n, self.x_p
675689

676-
np.testing.assert_array_almost_equal(
677-
self.phi_s_n(t, x_n) - self.phi_e_n(t, x_n), self.delta_phi_n(t, x_n)
690+
np.testing.assert_allclose(
691+
self.phi_s_n(t, x_n) - self.phi_e_n(t, x_n),
692+
self.delta_phi_n(t, x_n),
693+
rtol=1e-7,
694+
atol=1e-6,
678695
)
679-
np.testing.assert_array_almost_equal(
696+
np.testing.assert_allclose(
680697
self.phi_s_p(t, x_p) - self.phi_e_p(t, x_p),
681698
self.delta_phi_p(t, x_p),
682-
decimal=5,
699+
rtol=1e-6,
700+
atol=1e-5,
683701
)
684702

685703
def test_average_potential_differences(self):
686704
"""Test that average potential differences are the difference between electrode
687705
potential and electrolyte potential"""
688706
t = self.t
689707

690-
np.testing.assert_array_almost_equal(
691-
self.phi_s_n_av(t) - self.phi_e_n_av(t), self.delta_phi_n_av(t), decimal=4
708+
np.testing.assert_allclose(
709+
self.phi_s_n_av(t) - self.phi_e_n_av(t),
710+
self.delta_phi_n_av(t),
711+
rtol=1e-5,
712+
atol=1e-4,
692713
)
693-
np.testing.assert_array_almost_equal(
694-
self.phi_s_p_av(t) - self.phi_e_p_av(t), self.delta_phi_p_av(t), decimal=4
714+
np.testing.assert_allclose(
715+
self.phi_s_p_av(t) - self.phi_e_p_av(t),
716+
self.delta_phi_p_av(t),
717+
rtol=1e-5,
718+
atol=1e-4,
695719
)
696720

697721
def test_gradient_splitting(self):
@@ -802,10 +826,10 @@ def test_current_density_boundaries(self):
802826
current_param = self.model.param.current_density_with_time
803827

804828
i_cell = self.param.process_symbol(current_param).evaluate(t=t)
805-
np.testing.assert_array_almost_equal(self.i_s_n(t, x_n[0]), i_cell, decimal=2)
806-
np.testing.assert_array_almost_equal(self.i_s_n(t, x_n[-1]), 0, decimal=4)
807-
np.testing.assert_array_almost_equal(self.i_s_p(t, x_p[-1]), i_cell, decimal=3)
808-
np.testing.assert_array_almost_equal(self.i_s_p(t, x_p[0]), 0, decimal=4)
829+
np.testing.assert_allclose(self.i_s_n(t, x_n[0]), i_cell, rtol=1e-3, atol=1e-2)
830+
np.testing.assert_allclose(self.i_s_n(t, x_n[-1]), 0, rtol=1e-5, atol=1e-4)
831+
np.testing.assert_allclose(self.i_s_p(t, x_p[-1]), i_cell, rtol=1e-4, atol=1e-3)
832+
np.testing.assert_allclose(self.i_s_p(t, x_p[0]), 0, rtol=1e-5, atol=1e-4)
809833

810834
def test_all(self):
811835
self.test_conservation()
@@ -827,8 +851,8 @@ def __init__(self, model, param, disc, solution, operating_condition):
827851
def test_velocity_boundaries(self):
828852
"""Test the boundary values of the current densities"""
829853
L_x = self.x_edge[-1]
830-
np.testing.assert_array_almost_equal(self.v_box(self.t, 0), 0, decimal=4)
831-
np.testing.assert_array_almost_equal(self.v_box(self.t, L_x), 0, decimal=4)
854+
np.testing.assert_allclose(self.v_box(self.t, 0), 0, rtol=1e-5, atol=1e-4)
855+
np.testing.assert_allclose(self.v_box(self.t, L_x), 0, rtol=1e-5, atol=1e-4)
832856

833857
def test_vertical_velocity(self):
834858
"""Test the boundary values of the current densities"""
@@ -847,11 +871,11 @@ def test_velocity_vs_current(self):
847871
DeltaV_p = self.param.evaluate(DeltaV_p)
848872
F = pybamm.constants.F.value
849873

850-
np.testing.assert_array_almost_equal(
851-
self.v_box(t, x_n), DeltaV_n * self.i_e(t, x_n) / F
874+
np.testing.assert_allclose(
875+
self.v_box(t, x_n), DeltaV_n * self.i_e(t, x_n) / F, rtol=1e-7, atol=1e-6
852876
)
853-
np.testing.assert_array_almost_equal(
854-
self.v_box(t, x_p), DeltaV_p * self.i_e(t, x_p) / F
877+
np.testing.assert_allclose(
878+
self.v_box(t, x_p), DeltaV_p * self.i_e(t, x_p) / F, rtol=1e-7, atol=1e-6
855879
)
856880

857881
def test_all(self):
@@ -889,12 +913,13 @@ def test_degradation_modes(self):
889913

890914
def test_lithium_lost(self):
891915
"""Test the two ways of measuring lithium lost give the same value"""
892-
np.testing.assert_array_almost_equal(
916+
np.testing.assert_allclose(
893917
self.n_Li_lost(self.t),
894918
self.n_Li_lost_rxn(self.t)
895919
+ self.n_Li_lost_LAM_n(self.t)
896920
+ self.n_Li_lost_LAM_p(self.t),
897-
decimal=5,
921+
rtol=1e-6,
922+
atol=1e-5,
898923
)
899924

900925
def test_all(self):

tests/integration/test_models/test_full_battery_models/test_lead_acid/test_full.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_optimisations(self):
2828

2929
original = optimtest.evaluate_model()
3030
to_python = optimtest.evaluate_model(to_python=True)
31-
np.testing.assert_array_almost_equal(original, to_python)
31+
np.testing.assert_allclose(original, to_python, rtol=1e-7, atol=1e-6)
3232

3333
def test_set_up(self):
3434
options = {"thermal": "isothermal"}

tests/integration/test_models/test_full_battery_models/test_lead_acid/test_loqs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_optimisations(self):
1919

2020
original = optimtest.evaluate_model()
2121
to_python = optimtest.evaluate_model(to_python=True)
22-
np.testing.assert_array_almost_equal(original, to_python)
22+
np.testing.assert_allclose(original, to_python, rtol=1e-7, atol=1e-6)
2323

2424
def test_set_up(self):
2525
model = pybamm.lead_acid.LOQS()

tests/integration/test_models/test_full_battery_models/test_lead_acid/test_loqs_surface_form.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_optimisations(self):
4848

4949
original = optimtest.evaluate_model()
5050
to_python = optimtest.evaluate_model(to_python=True)
51-
np.testing.assert_array_almost_equal(original, to_python, decimal=5)
51+
np.testing.assert_allclose(original, to_python, rtol=1e-6, atol=1e-5)
5252

5353
def test_set_up(self):
5454
options = {"surface form": "differential"}

tests/integration/test_models/test_full_battery_models/test_lithium_ion/base_lithium_ion_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def test_optimisations(self):
4545

4646
original = optimtest.evaluate_model()
4747
to_python = optimtest.evaluate_model(to_python=True)
48-
np.testing.assert_array_almost_equal(original, to_python)
48+
np.testing.assert_allclose(original, to_python, rtol=1e-7, atol=1e-7)
4949

5050
if pybamm.has_jax():
5151
to_jax = optimtest.evaluate_model(to_jax=True)
52-
np.testing.assert_array_almost_equal(original, to_jax)
52+
np.testing.assert_allclose(original, to_jax, rtol=1e-7, atol=1e-7)
5353

5454
def test_set_up(self):
5555
model = self.model()

tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_dfn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def test_conservation_each_electrode(self):
149149
pos_Li.append(pos)
150150

151151
# compare
152-
np.testing.assert_array_almost_equal(neg_Li[0], neg_Li[1], decimal=12)
153-
np.testing.assert_array_almost_equal(pos_Li[0], pos_Li[1], decimal=12)
152+
np.testing.assert_allclose(neg_Li[0], neg_Li[1], rtol=1e-13, atol=1e-12)
153+
np.testing.assert_allclose(pos_Li[0], pos_Li[1], rtol=1e-13, atol=1e-12)
154154

155155
def test_basic_processing_nonlinear_diffusion(self):
156156
options = {

0 commit comments

Comments
 (0)