Skip to content

refactor: use pytest fixtures to parameterize tests for lead acid battery model #4723

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 9 commits into from
Feb 22, 2025
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
#
# Tests for the lead-acid Full model
#
import pybamm
import tests

import pytest
import numpy as np


class TestLeadAcidFull:
def test_basic_processing(self):
options = {"thermal": "isothermal"}
model = pybamm.lead_acid.Full(options)
modeltest = tests.StandardModelTest(model)
modeltest.test_all(t_eval=np.linspace(0, 3600 * 17))
@pytest.fixture
def optimtest():
options = {"thermal": "isothermal"}
model = pybamm.lead_acid.Full(options)
return tests.OptimisationsTest(model)

def test_basic_processing_with_convection(self):
options = {"thermal": "isothermal", "convection": "uniform transverse"}
model = pybamm.lead_acid.Full(options)
# var_pts = {"x_n": 10, "x_s": 10, "x_p": 10}
modeltest = tests.StandardModelTest(model)
modeltest.test_all(t_eval=np.linspace(0, 3600 * 10))

def test_optimisations(self):
options = {"thermal": "isothermal"}
class TestLeadAcidFull:
@pytest.mark.parametrize(
"options, t_eval",
[
({"thermal": "isothermal"}, np.linspace(0, 3600 * 17)),
(
{"thermal": "isothermal", "convection": "uniform transverse"},
np.linspace(0, 3600 * 10),
),
],
ids=["basic_processing", "basic_processing_with_convection"],
)
def test_basic_processing(self, options, t_eval):
model = pybamm.lead_acid.Full(options)
optimtest = tests.OptimisationsTest(model)
modeltest = tests.StandardModelTest(model)
modeltest.test_all(t_eval=t_eval)

def test_optimisations(self, optimtest):
original = optimtest.evaluate_model()
to_python = optimtest.evaluate_model(to_python=True)
np.testing.assert_allclose(original, to_python, rtol=1e-7, atol=1e-6)

def test_set_up(self):
options = {"thermal": "isothermal"}
model = pybamm.lead_acid.Full(options)
optimtest = tests.OptimisationsTest(model)
def test_set_up(self, optimtest):
optimtest.set_up_model(to_python=True)
optimtest.set_up_model(to_python=False)

def test_basic_processing_1plus1D(self):
options = {"current collector": "potential pair", "dimensionality": 1}
model = pybamm.lead_acid.Full(options)
@pytest.mark.parametrize(
"options",
[
{"current collector": "potential pair", "dimensionality": 1},
{
"current collector": "potential pair",
"dimensionality": 1,
"convection": "full transverse",
},
],
ids=["basic_1plus1D", "1plus1D_with_convection"],
)
def test_basic_processing_1plus1D(self, options):
var_pts = {"x_n": 5, "x_s": 5, "x_p": 5, "y": 5, "z": 5}
modeltest = tests.StandardModelTest(model, var_pts=var_pts)
modeltest.test_all(skip_output_tests=True)

options = {
"current collector": "potential pair",
"dimensionality": 1,
"convection": "full transverse",
}
model = pybamm.lead_acid.Full(options)
modeltest = tests.StandardModelTest(model, var_pts=var_pts)
modeltest.test_all(skip_output_tests=True)


class TestLeadAcidFullSurfaceForm:
def test_basic_processing_differential(self):
options = {"surface form": "differential"}
model = pybamm.lead_acid.Full(options)
modeltest = tests.StandardModelTest(model)
modeltest.test_all()

def test_basic_processing_algebraic(self):
options = {"surface form": "algebraic"}
@pytest.mark.parametrize(
"surface_form",
["differential", "algebraic"],
ids=["differential", "algebraic"],
)
def test_basic_processing(self, surface_form):
options = {"surface form": surface_form}
model = pybamm.lead_acid.Full(options)
modeltest = tests.StandardModelTest(model)
modeltest.test_all()
Expand All @@ -74,15 +75,18 @@ def test_set_up(self):
optimtest.set_up_model(to_python=True)
optimtest.set_up_model(to_python=False)

def test_thermal(self):
options = {"thermal": "lumped"}
@pytest.mark.parametrize(
"options, param_update",
[
({"thermal": "lumped"}, {"Current function [A]": 1.7}),
({"thermal": "x-full"}, None),
],
ids=["lumped_with_current_function", "x_full"],
)
def test_thermal(self, options, param_update):
model = pybamm.lead_acid.Full(options)
param = model.default_parameter_values
param["Current function [A]"] = 1.7
if param_update:
param.update(param_update)
modeltest = tests.StandardModelTest(model, parameter_values=param)
modeltest.test_all()

options = {"thermal": "x-full"}
model = pybamm.lead_acid.Full(options)
modeltest = tests.StandardModelTest(model)
modeltest.test_all()
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#
# Tests for the lead-acid LOQS model
#
import pybamm
import tests

import pytest
import numpy as np


Expand All @@ -16,7 +13,6 @@ def test_basic_processing(self):
def test_optimisations(self):
model = pybamm.lead_acid.LOQS()
optimtest = tests.OptimisationsTest(model)

original = optimtest.evaluate_model()
to_python = optimtest.evaluate_model(to_python=True)
np.testing.assert_allclose(original, to_python, rtol=1e-7, atol=1e-6)
Expand All @@ -27,17 +23,11 @@ def test_set_up(self):
optimtest.set_up_model(to_python=True)
optimtest.set_up_model(to_python=False)

def test_charge(self):
model = pybamm.lead_acid.LOQS()
parameter_values = model.default_parameter_values
parameter_values.update({"Current function [A]": -1})
modeltest = tests.StandardModelTest(model, parameter_values=parameter_values)
modeltest.test_all()

def test_zero_current(self):
@pytest.mark.parametrize("current_value", [-1, 0], ids=["charge", "zero_current"])
def test_current(self, current_value):
model = pybamm.lead_acid.LOQS()
parameter_values = model.default_parameter_values
parameter_values.update({"Current function [A]": 0})
parameter_values.update({"Current function [A]": current_value})
modeltest = tests.StandardModelTest(model, parameter_values=parameter_values)
modeltest.test_all()

Expand All @@ -46,29 +36,30 @@ def test_basic_processing_with_convection(self):
modeltest = tests.StandardModelTest(model)
modeltest.test_all()

def test_thermal(self):
options = {"thermal": "lumped"}
@pytest.mark.parametrize(
"options",
[{"thermal": "lumped"}, {"thermal": "x-full"}],
ids=["lumped", "x_full"],
)
def test_thermal(self, options):
model = pybamm.lead_acid.LOQS(options)
modeltest = tests.StandardModelTest(model)
modeltest.test_all()

options = {"thermal": "x-full"}
model = pybamm.lead_acid.LOQS(options)
modeltest = tests.StandardModelTest(model)
modeltest.test_all()

def test_basic_processing_1plus1D(self):
options = {"current collector": "potential pair", "dimensionality": 1}
model = pybamm.lead_acid.LOQS(options)
@pytest.mark.parametrize(
"options",
[
{"current collector": "potential pair", "dimensionality": 1},
{
"current collector": "potential pair",
"dimensionality": 1,
"convection": "full transverse",
},
],
ids=["basic_1plus1D", "1plus1D_with_convection"],
)
def test_basic_processing_1plus1D(self, options):
var_pts = {"x_n": 5, "x_s": 5, "x_p": 5, "y": 5, "z": 5}
modeltest = tests.StandardModelTest(model, var_pts=var_pts)
modeltest.test_all(skip_output_tests=True)

options = {
"current collector": "potential pair",
"dimensionality": 1,
"convection": "full transverse",
}
model = pybamm.lead_acid.LOQS(options)
modeltest = tests.StandardModelTest(model, var_pts=var_pts)
modeltest.test_all(skip_output_tests=True)
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#
# Tests for the lead-acid LOQS model with capacitance
#
import pybamm
import tests
import pytest
import numpy as np


class TestLeadAcidLoqsSurfaceForm:
def test_basic_processing(self):
options = {"surface form": "algebraic"}
model = pybamm.lead_acid.LOQS(options)
modeltest = tests.StandardModelTest(model)
modeltest.test_all()
@pytest.fixture
def optimtest():
options = {"surface form": "differential"}
model = pybamm.lead_acid.LOQS(options)
optimtest_instance = tests.OptimisationsTest(model)
return optimtest_instance


def test_basic_processing_with_capacitance(self):
options = {"surface form": "differential"}
class TestLeadAcidLoqsSurfaceForm:
@pytest.mark.parametrize(
"surface_form",
["algebraic", "differential"],
ids=["basic_processing", "basic_processing_with_capacitance"],
)
def test_basic_processing(self, surface_form):
options = {"surface form": surface_form}
model = pybamm.lead_acid.LOQS(options)
modeltest = tests.StandardModelTest(model)
modeltest.test_all()
Expand All @@ -41,18 +45,11 @@ def test_basic_processing_1p1D_algebraic(self):
modeltest = tests.StandardModelTest(model)
modeltest.test_all(skip_output_tests=True)

def test_optimisations(self):
options = {"surface form": "differential"}
model = pybamm.lead_acid.LOQS(options)
optimtest = tests.OptimisationsTest(model)

def test_optimisations(self, optimtest):
original = optimtest.evaluate_model()
to_python = optimtest.evaluate_model(to_python=True)
np.testing.assert_allclose(original, to_python, rtol=1e-6, atol=1e-5)

def test_set_up(self):
options = {"surface form": "differential"}
model = pybamm.lead_acid.LOQS(options)
optimtest = tests.OptimisationsTest(model)
def test_set_up(self, optimtest):
optimtest.set_up_model(to_python=True)
optimtest.set_up_model(to_python=False)
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
#
# Tests for the lead-acid Full model
#
import pybamm
import pytest


class TestLeadAcidFull:
def test_well_posed(self):
model = pybamm.lead_acid.Full()
model.check_well_posedness()

def test_well_posed_with_convection(self):
options = {"convection": "uniform transverse"}
model = pybamm.lead_acid.Full(options)
model.check_well_posedness()

options = {"dimensionality": 1, "convection": "full transverse"}
@pytest.mark.parametrize(
"options",
[
{},
{"convection": "uniform transverse"},
{"dimensionality": 1, "convection": "full transverse"},
],
ids=["well_posed", "with_convention", "with_convention_1plus1d"],
)
def test_well_posed(self, options):
model = pybamm.lead_acid.Full(options)
model.check_well_posedness()


class TestLeadAcidFullSurfaceForm:
def test_well_posed_differential(self):
options = {"surface form": "differential"}
model = pybamm.lead_acid.Full(options)
model.check_well_posedness()

def test_well_posed_differential_1plus1d(self):
options = {"surface form": "differential", "dimensionality": 1}
model = pybamm.lead_acid.Full(options)
model.check_well_posedness()

def test_well_posed_algebraic(self):
options = {"surface form": "algebraic"}
@pytest.mark.parametrize(
"options",
[
{"surface form": "differential"},
{"surface form": "differential", "dimensionality": 1},
{"surface form": "algebraic"},
],
ids=["differential", "differential_1plus1d", "algebraic"],
)
def test_well_posed(self, options):
model = pybamm.lead_acid.Full(options)
model.check_well_posedness()


class TestLeadAcidFullSideReactions:
def test_well_posed(self):
def test_model_well_posedness(self):
options = {"hydrolysis": "true"}
model = pybamm.lead_acid.Full(options)
model.check_well_posedness()

def test_well_posed_surface_form_differential(self):
options = {"hydrolysis": "true", "surface form": "differential"}
model = pybamm.lead_acid.Full(options)
model.check_well_posedness()
assert isinstance(model.default_solver, pybamm.CasadiSolver)

def test_well_posed_surface_form_algebraic(self):
options = {"hydrolysis": "true", "surface form": "algebraic"}
@pytest.mark.parametrize(
"options, expected_solver",
[
(
{"hydrolysis": "true", "surface form": "differential"},
pybamm.CasadiSolver,
),
({"hydrolysis": "true", "surface form": "algebraic"}, pybamm.CasadiSolver),
],
ids=[
"surface_form_differential",
"surface_form_algebraic",
],
)
def test_well_posed(self, options, expected_solver):
model = pybamm.lead_acid.Full(options)
model.check_well_posedness()
assert isinstance(model.default_solver, pybamm.CasadiSolver)
if expected_solver is not None:
assert isinstance(model.default_solver, expected_solver)
Loading