Skip to content

Commit

Permalink
Merge branch 'develop' into fix/citations2
Browse files Browse the repository at this point in the history
  • Loading branch information
kratman authored Oct 1, 2024
2 parents 4c8dbdd + 78c2410 commit 17be7e8
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 101 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ extend-select = [
"YTT", # flake8-2020
"TID252", # relative-imports
"S101", # to identify use of assert statement
"PT027", # remove unittest style assertion
"PT009", # Use pytest.raises instead of unittest-style
]
ignore = [
"E741", # Ambiguous variable name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

import pybamm
import numpy as np
import unittest
from tests import StandardOutputComparison


class TestCompareOutputs(unittest.TestCase):
class TestCompareOutputs:
def test_compare_outputs_surface_form(self):
# load models
options = [
Expand Down Expand Up @@ -142,12 +141,3 @@ def test_compare_narrow_size_distribution(self):
# compare outputs
comparison = StandardOutputComparison(solutions)
comparison.test_all(skip_first_timestep=True)


if __name__ == "__main__":
print("Add -v for more debug output")
import sys

if "-v" in sys.argv:
debug = True
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# Tests for the electrode-electrolyte interface equations
#

import pytest
import pybamm
from tests import get_discretisation_for_testing

import unittest
import numpy as np


class TestButlerVolmer(unittest.TestCase):
def setUp(self):
class TestButlerVolmer:
def setup_method(self):
self.delta_phi_s_n = pybamm.Variable(
"surface potential difference [V]",
["negative electrode"],
Expand Down Expand Up @@ -73,7 +73,7 @@ def setUp(self):
"reaction source terms [A.m-3]": 1,
}

def tearDown(self):
def teardown_method(self):
del self.variables
del self.c_e_n
del self.c_e_p
Expand Down Expand Up @@ -114,12 +114,12 @@ def test_creation(self):
]

# negative electrode Butler-Volmer is Multiplication
self.assertIsInstance(j_n, pybamm.Multiplication)
self.assertEqual(j_n.domain, ["negative electrode"])
assert isinstance(j_n, pybamm.Multiplication)
assert j_n.domain == ["negative electrode"]

# positive electrode Butler-Volmer is Multiplication
self.assertIsInstance(j_p, pybamm.Multiplication)
self.assertEqual(j_p.domain, ["positive electrode"])
assert isinstance(j_p, pybamm.Multiplication)
assert j_p.domain == ["positive electrode"]

def test_set_parameters(self):
param = pybamm.LithiumIonParameters()
Expand Down Expand Up @@ -159,9 +159,9 @@ def test_set_parameters(self):
j_p = parameter_values.process_symbol(j_p)
# Test
for x in j_n.pre_order():
self.assertNotIsInstance(x, pybamm.Parameter)
assert not isinstance(x, pybamm.Parameter)
for x in j_p.pre_order():
self.assertNotIsInstance(x, pybamm.Parameter)
assert not isinstance(x, pybamm.Parameter)

def test_discretisation(self):
param = pybamm.LithiumIonParameters()
Expand Down Expand Up @@ -219,17 +219,13 @@ def test_discretisation(self):
[mesh["negative electrode"].nodes, mesh["positive electrode"].nodes]
)
y = np.concatenate([submesh**2, submesh**3, submesh**4])
self.assertEqual(
j_n.evaluate(None, y).shape, (mesh["negative electrode"].npts, 1)
)
self.assertEqual(
j_p.evaluate(None, y).shape, (mesh["positive electrode"].npts, 1)
)
assert j_n.evaluate(None, y).shape == (mesh["negative electrode"].npts, 1)
assert j_p.evaluate(None, y).shape == (mesh["positive electrode"].npts, 1)

# test concatenated butler-volmer
whole_cell = ["negative electrode", "separator", "positive electrode"]
whole_cell_mesh = disc.mesh[whole_cell]
self.assertEqual(j.evaluate(None, y).shape, (whole_cell_mesh.npts, 1))
assert j.evaluate(None, y).shape == (whole_cell_mesh.npts, 1)

def test_diff_c_e_lead_acid(self):
# With intercalation
Expand Down Expand Up @@ -361,27 +357,12 @@ def j_p(delta_phi):
j_n_FD = parameter_values.process_symbol(
(j_n(delta_phi + h) - j_n(delta_phi - h)) / (2 * h)
)
self.assertAlmostEqual(
j_n_diff.evaluate(inputs={"delta_phi": 0.5})
/ j_n_FD.evaluate(inputs={"delta_phi": 0.5}),
1,
places=5,
)
assert j_n_diff.evaluate(inputs={"delta_phi": 0.5}) / j_n_FD.evaluate(
inputs={"delta_phi": 0.5}
) == pytest.approx(1, abs=1e-05)
j_p_FD = parameter_values.process_symbol(
(j_p(delta_phi + h) - j_p(delta_phi - h)) / (2 * h)
)
self.assertAlmostEqual(
j_p_diff.evaluate(inputs={"delta_phi": 0.5})
/ j_p_FD.evaluate(inputs={"delta_phi": 0.5}),
1,
places=5,
)


if __name__ == "__main__":
print("Add -v for more debug output")
import sys

if "-v" in sys.argv:
debug = True
unittest.main()
assert j_p_diff.evaluate(inputs={"delta_phi": 0.5}) / j_p_FD.evaluate(
inputs={"delta_phi": 0.5}
) == pytest.approx(1, abs=1e-05)
17 changes: 0 additions & 17 deletions tests/testcase.py

This file was deleted.

7 changes: 3 additions & 4 deletions tests/unit/test_expression_tree/test_concatenations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Tests for the Concatenation class and subclasses
#
import pytest
import unittest.mock as mock
from tests import assert_domain_equal


Expand Down Expand Up @@ -377,7 +376,7 @@ def test_to_equation(self):
# Test concat_sym
assert pybamm.Concatenation(a, b).to_equation() == func_symbol

def test_to_from_json(self):
def test_to_from_json(self, mocker):
# test DomainConcatenation
mesh = get_mesh_for_testing()
a = pybamm.Symbol("a", domain=["negative electrode"])
Expand All @@ -386,7 +385,7 @@ def test_to_from_json(self):

json_dict = {
"name": "domain_concatenation",
"id": mock.ANY,
"id": mocker.ANY,
"domains": {
"primary": ["negative electrode", "separator", "positive electrode"],
"secondary": [],
Expand Down Expand Up @@ -429,7 +428,7 @@ def test_to_from_json(self):

np_json = {
"name": "numpy_concatenation",
"id": mock.ANY,
"id": mocker.ANY,
"domains": {
"primary": [],
"secondary": [],
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/test_expression_tree/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#

import pytest
import unittest.mock as mock

import numpy as np
from scipy import special
Expand Down Expand Up @@ -399,7 +398,7 @@ def test_tanh(self):
abs=1e-05,
)

def test_erf(self):
def test_erf(self, mocker):
a = pybamm.InputParameter("a")
fun = pybamm.erf(a)
assert fun.evaluate(inputs={"a": 3}) == special.erf(3)
Expand All @@ -416,7 +415,7 @@ def test_erf(self):
# test creation from json
input_json = {
"name": "erf",
"id": mock.ANY,
"id": mocker.ANY,
"function": "erf",
"children": [a],
}
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/test_expression_tree/test_input_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy as np
import pybamm
import pytest
import unittest.mock as mock


class TestInputParameter:
Expand Down Expand Up @@ -49,12 +48,12 @@ def test_errors(self):
with pytest.raises(KeyError):
a.evaluate()

def test_to_from_json(self):
def test_to_from_json(self, mocker):
a = pybamm.InputParameter("a")

json_dict = {
"name": "a",
"id": mock.ANY,
"id": mocker.ANY,
"domain": [],
"expected_size": 1,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pybamm

from tests import get_discretisation_for_testing, get_1p1d_discretisation_for_testing
import unittest
import numpy as np
import scipy.sparse
from collections import OrderedDict
Expand Down Expand Up @@ -746,13 +745,3 @@ def test_jax_coo_matrix(self):

with pytest.raises(NotImplementedError):
A.multiply(v)


if __name__ == "__main__":
print("Add -v for more debug output")
import sys

if "-v" in sys.argv:
debug = True
pybamm.settings.debug_mode = True
unittest.main()
18 changes: 4 additions & 14 deletions tests/unit/test_parameters/test_parameter_sets/test_Ecker2015.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Tests for O'Kane (2022) parameter set
#

import pytest
import pybamm
import unittest


class TestEcker2015(unittest.TestCase):
class TestEcker2015:
def test_functions(self):
param = pybamm.ParameterValues("Ecker2015")
sto = pybamm.Scalar(0.5)
Expand Down Expand Up @@ -40,16 +40,6 @@ def test_functions(self):
}

for name, value in fun_test.items():
self.assertAlmostEqual(
param.evaluate(param[name](*value[0])), value[1], places=4
assert param.evaluate(param[name](*value[0])) == pytest.approx(
value[1], abs=0.0001
)


if __name__ == "__main__":
print("Add -v for more debug output")
import sys

if "-v" in sys.argv:
debug = True
pybamm.settings.debug_mode = True
unittest.main()

0 comments on commit 17be7e8

Please sign in to comment.