Skip to content

Commit 7597712

Browse files
#492 tests and coverage
1 parent e638b1b commit 7597712

File tree

6 files changed

+49
-42
lines changed

6 files changed

+49
-42
lines changed

pybamm/models/full_battery_models/lead_acid/loqs.py

+5-18
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,11 @@ def set_external_circuit_submodel(self):
5757
Define how the external circuit defines the boundary conditions for the model,
5858
e.g. (not necessarily constant-) current, voltage, etc
5959
"""
60-
if self.options["operating mode"] == "current":
61-
self.submodels[
62-
"leading order external circuit"
63-
] = pybamm.external_circuit.LeadingOrderCurrentControl(self.param)
64-
elif self.options["operating mode"] == "voltage":
65-
self.submodels[
66-
"leading order external circuit"
67-
] = pybamm.external_circuit.VoltageFunctionControl(self.param)
68-
elif self.options["operating mode"] == "power":
69-
self.submodels[
70-
"leading order external circuit"
71-
] = pybamm.external_circuit.PowerFunctionControl(self.param)
72-
elif callable(self.options["operating mode"]):
73-
self.submodels[
74-
"leading order external circuit"
75-
] = pybamm.external_circuit.FunctionControl(
76-
self.param, self.options["operating mode"]
77-
)
60+
super().set_external_circuit_submodel()
61+
# replace 'external circuit' with 'leading-order external circuit'
62+
self.submodels["leading order external circuit"] = self.submodels.pop(
63+
"external circuit"
64+
)
7865

7966
def set_current_collector_submodel(self):
8067

pybamm/models/submodels/external_circuit/function_control_external_circuit.py

+10-23
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ def __init__(self, param, external_circuit_class):
1212
super().__init__(param)
1313
self.external_circuit_class = external_circuit_class
1414

15+
def _get_current_variable(self):
16+
return pybamm.Variable("Total current density")
17+
1518
def get_fundamental_variables(self):
1619
# Current is a variable
17-
i_cell = pybamm.Variable("Total current density")
20+
i_cell = self._get_current_variable()
1821
variables = self._get_current_variables(i_cell)
1922

2023
# Add discharge capacity variable
@@ -24,9 +27,9 @@ def get_fundamental_variables(self):
2427
# These are not implemented yet but can be used later with the Experiment class
2528
# to simulate different external circuit conditions sequentially within a
2629
# single model (for example Constant Current - Constant Voltage)
27-
for i in range(self.external_circuit_class.num_switches):
28-
s = pybamm.Parameter("Switch {}".format(i + 1))
29-
variables["Switch {}".format(i + 1)] = s
30+
# for i in range(self.external_circuit_class.num_switches):
31+
# s = pybamm.Parameter("Switch {}".format(i + 1))
32+
# variables["Switch {}".format(i + 1)] = s
3033

3134
return variables
3235

@@ -81,26 +84,10 @@ class LeadingOrderFunctionControl(FunctionControl, LeadingOrderBaseModel):
8184
"""External circuit with an arbitrary function, at leading order. """
8285

8386
def __init__(self, param, external_circuit_class):
84-
super().__init__(param)
85-
self.external_circuit_class = external_circuit_class
86-
87-
def get_fundamental_variables(self):
88-
# Current is a variable
89-
i_cell = pybamm.Variable("Leading-order total current density")
90-
variables = self._get_current_variables(i_cell)
91-
92-
# Add discharge capacity variable
93-
variables.update(super().get_fundamental_variables())
94-
95-
# Add switches
96-
# These are not implemented yet but can be used later with the Experiment class
97-
# to simulate different external circuit conditions sequentially within a
98-
# single model (for example Constant Current - Constant Voltage)
99-
for i in range(self.external_circuit_class.num_switches):
100-
s = pybamm.Parameter("Switch {}".format(i + 1))
101-
variables["Switch {}".format(i + 1)] = s
87+
super().__init__(param, external_circuit_class)
10288

103-
return variables
89+
def _get_current_variable(self):
90+
return pybamm.Variable("Leading-order total current density")
10491

10592

10693
class LeadingOrderVoltageFunctionControl(LeadingOrderFunctionControl):

tests/unit/test_models/test_full_battery_models/test_base_battery_model.py

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def test_bad_options(self):
124124
pybamm.BaseBatteryModel({"particle": "bad particle"})
125125
with self.assertRaisesRegex(pybamm.OptionError, "option set external"):
126126
pybamm.BaseBatteryModel({"current collector": "set external potential"})
127+
with self.assertRaisesRegex(pybamm.OptionError, "operating mode"):
128+
pybamm.BaseBatteryModel({"operating mode": "bad operating mode"})
127129

128130
def test_build_twice(self):
129131
model = pybamm.lithium_ion.SPM() # need to pick a model to set vars and build

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

+12
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ def test_default_geometry(self):
155155
self.assertIn("current collector", model.default_geometry)
156156

157157

158+
class TestLeadAcidLOQSExternalCircuits(unittest.TestCase):
159+
def test_well_posed_voltage(self):
160+
options = {"operating mode": "voltage"}
161+
model = pybamm.lead_acid.LOQS(options)
162+
model.check_well_posedness()
163+
164+
def test_well_posed_power(self):
165+
options = {"operating mode": "power"}
166+
model = pybamm.lead_acid.LOQS(options)
167+
model.check_well_posedness()
168+
169+
158170
if __name__ == "__main__":
159171
print("Add -v for more debug output")
160172
import sys

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

+12
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ def test_particle_fast_diffusion(self):
178178
model.check_well_posedness()
179179

180180

181+
class TestSPMExternalCircuits(unittest.TestCase):
182+
def test_well_posed_voltage(self):
183+
options = {"operating mode": "voltage"}
184+
model = pybamm.lithium_ion.SPM(options)
185+
model.check_well_posedness()
186+
187+
def test_well_posed_power(self):
188+
options = {"operating mode": "power"}
189+
model = pybamm.lithium_ion.SPM(options)
190+
model.check_well_posedness()
191+
192+
181193
if __name__ == "__main__":
182194
print("Add -v for more debug output")
183195
import sys

tests/unit/test_parameters/test_update_parameters.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ def test_update_geometry(self):
9494
# test on simple lead-acid model
9595
model1 = pybamm.lead_acid.LOQS()
9696
modeltest1 = tests.StandardModelTest(model1)
97+
parameter_values = pybamm.ParameterValues(
98+
chemistry=pybamm.parameter_sets.Sulzer2019
99+
)
100+
parameter_values.update({"C-rate": 0.05})
97101
t_eval = np.linspace(0, 0.5)
98-
modeltest1.test_all(t_eval=t_eval, skip_output_tests=True)
102+
modeltest1.test_all(
103+
param=parameter_values, t_eval=t_eval, skip_output_tests=True
104+
)
99105

100106
T1, Y1 = modeltest1.solution.t, modeltest1.solution.y
101107

@@ -105,6 +111,7 @@ def test_update_geometry(self):
105111
)
106112
parameter_values_update.update(
107113
{
114+
"C-rate": 0.05,
108115
"Negative electrode thickness [m]": 0.0002,
109116
"Separator thickness [m]": 0.0003,
110117
"Positive electrode thickness [m]": 0.0004,

0 commit comments

Comments
 (0)