Skip to content

Commit c13d8b9

Browse files
#492 coverage for parameter values
1 parent 7eb60ec commit c13d8b9

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

pybamm/parameters/parameter_values.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ def check_and_update_parameter_values(self, values):
223223
value = CrateToCurrent(values["C-rate"], capacity)
224224
elif isinstance(values["C-rate"], tuple):
225225
data = values["C-rate"][1]
226-
data[:, 1] *= capacity
227-
value = (values["C-rate"][0] + "_toCrate", data)
226+
data[:, 1] = data[:, 1] * capacity
227+
value = (values["C-rate"][0] + "_to_Crate", data)
228228
else:
229229
value = values["C-rate"] * capacity
230230
super().__setitem__("Current function [A]", value)
@@ -233,8 +233,8 @@ def check_and_update_parameter_values(self, values):
233233
value = CurrentToCrate(values["Current function [A]"], capacity)
234234
elif isinstance(values["Current function [A]"], tuple):
235235
data = values["Current function [A]"][1]
236-
data[:, 1] /= capacity
237-
value = (values["Current function [A]"][0] + "_toCurrent", data)
236+
data[:, 1] = data[:, 1] / capacity
237+
value = (values["Current function [A]"][0] + "_to_current", data)
238238
else:
239239
value = values["Current function [A]"] / capacity
240240
super().__setitem__("C-rate", value)

tests/unit/test_models/test_full_battery_models/test_lithium_ion/test_spm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def __call__(self, variables):
199199
return V + I - pybamm.FunctionParameter("Function", pybamm.t)
200200

201201
options = {"operating mode": ExternalCircuitFunction()}
202-
model = pybamm.lead_acid.LOQS(options)
202+
model = pybamm.lithium_ion.SPM(options)
203203
model.check_well_posedness()
204204

205205

tests/unit/test_parameters/test_parameter_values.py

+33
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ def test_check_and_update_parameter_values(self):
8686
param = pybamm.ParameterValues(values)
8787
self.assertEqual(param["C-rate"](5).evaluate(), np.exp(5) / 10)
8888

89+
# With data
90+
# if only C-rate and capacity provided, update current
91+
x = np.linspace(0, 10)[:, np.newaxis]
92+
linear = np.hstack([x, 2 * x])
93+
values = {"C-rate": ("linear", linear), "Cell capacity [A.h]": 10}
94+
param = pybamm.ParameterValues(values)
95+
self.assertEqual(param["Current function [A]"][0], "linear_to_Crate")
96+
np.testing.assert_array_equal(
97+
param["Current function [A]"][1], np.hstack([x, 20 * x])
98+
)
99+
# if only current and capacity provided, update C-rate
100+
x = np.linspace(0, 10)[:, np.newaxis]
101+
linear = np.hstack([x, 2 * x])
102+
values = {"Current function [A]": ("linear", linear), "Cell capacity [A.h]": 10}
103+
param = pybamm.ParameterValues(values)
104+
self.assertEqual(param["C-rate"][0], "linear_to_current")
105+
np.testing.assert_array_almost_equal(
106+
param["C-rate"][1], np.hstack([x, 0.2 * x])
107+
)
108+
89109
def test_process_symbol(self):
90110
parameter_values = pybamm.ParameterValues({"a": 1, "b": 2, "c": 3})
91111
# process parameter
@@ -226,6 +246,11 @@ def test_process_symbol(self):
226246
with self.assertRaises(NotImplementedError):
227247
parameter_values.process_symbol(sym)
228248

249+
# not found
250+
with self.assertRaises(KeyError):
251+
x = pybamm.Parameter("x")
252+
parameter_values.process_symbol(x)
253+
229254
def test_process_input_parameter(self):
230255
parameter_values = pybamm.ParameterValues({"a": "[input]", "b": 3})
231256
# process input parameter
@@ -461,6 +486,14 @@ def test_process_model(self):
461486
isinstance(model.variables["d_var1"].children[1], pybamm.Variable)
462487
)
463488

489+
# bad boundary conditions
490+
model = pybamm.BaseModel()
491+
model.algebraic = {var1: var1}
492+
x = pybamm.Parameter("x")
493+
model.boundary_conditions = {var1: {"left": (x, "Dirichlet")}}
494+
with self.assertRaises(KeyError):
495+
parameter_values.process_model(model)
496+
464497
def test_process_empty_model(self):
465498
model = pybamm.BaseModel()
466499
parameter_values = pybamm.ParameterValues({"a": 1, "b": 2, "c": 3, "d": 42})

tests/unit/test_solvers/test_casadi_solver.py

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import unittest
77
import numpy as np
88
from tests import get_mesh_for_testing, get_discretisation_for_testing
9-
import warnings
109

1110

1211
class TestCasadiSolver(unittest.TestCase):

0 commit comments

Comments
 (0)