Skip to content

Commit cb50363

Browse files
aabillskratmanAlexander Billspre-commit-ci[bot]
authored
Throw an error when concatenations have different number of children (#4562)
* add error and test * disable thermal half cells for now * easier way to fix it * clean up * move half cell code to function * style: pre-commit fixes * changelog * Update CHANGELOG.md Co-authored-by: Eric G. Kratz <[email protected]> --------- Co-authored-by: Eric G. Kratz <[email protected]> Co-authored-by: Alexander Bills <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent bf8f623 commit cb50363

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Features
44

5+
- Added `CoupledVariable` which provides a placeholder variable whose equation can be elsewhere in the model. ([#4556](https://github.com/pybamm-team/PyBaMM/pull/4556))
56
- Adds support to `pybamm.Experiment` for the `output_variables` option in the `IDAKLUSolver`. ([#4534](https://github.com/pybamm-team/PyBaMM/pull/4534))
67
- Adds an option "voltage as a state" that can be "false" (default) or "true". If "true" adds an explicit algebraic equation for the voltage. ([#4507](https://github.com/pybamm-team/PyBaMM/pull/4507))
78
- Improved `QuickPlot` accuracy for simulations with Hermite interpolation. ([#4483](https://github.com/pybamm-team/PyBaMM/pull/4483))
@@ -21,6 +22,8 @@
2122
- Removed the `start_step_offset` setting and disabled minimum `dt` warnings for drive cycles with the (`IDAKLUSolver`). ([#4416](https://github.com/pybamm-team/PyBaMM/pull/4416))
2223

2324
## Bug Fixes
25+
- Added error for binary operators on two concatenations with different numbers of children. Previously, the extra children were dropped. Also fixed bug where Q_rxn was dropped from the total heating term in half-cell models. ([#4562](https://github.com/pybamm-team/PyBaMM/pull/4562))
26+
- Fixed bug where Q_rxn was set to 0 for the negative electrode in half-cell models. ([#4557](https://github.com/pybamm-team/PyBaMM/pull/4557))
2427
- Fixed bug in post-processing solutions with infeasible experiments using the (`IDAKLUSolver`). ([#4541](https://github.com/pybamm-team/PyBaMM/pull/4541))
2528
- Disabled IREE on MacOS due to compatibility issues and added the CasADI
2629
path to the environment to resolve issues on MacOS and Linux. Windows

src/pybamm/expression_tree/binary_operators.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -856,12 +856,17 @@ def _simplified_binary_broadcast_concatenation(
856856
elif isinstance(right, pybamm.Concatenation) and not isinstance(
857857
right, pybamm.ConcatenationVariable
858858
):
859-
return left.create_copy(
860-
[
861-
operator(left_child, right_child)
862-
for left_child, right_child in zip(left.orphans, right.orphans)
863-
]
864-
)
859+
if len(left.orphans) == len(right.orphans):
860+
return left.create_copy(
861+
[
862+
operator(left_child, right_child)
863+
for left_child, right_child in zip(left.orphans, right.orphans)
864+
]
865+
)
866+
else:
867+
raise AssertionError(
868+
"Concatenations must have the same number of children"
869+
)
865870
if isinstance(right, pybamm.Concatenation) and not isinstance(
866871
right, pybamm.ConcatenationVariable
867872
):

src/pybamm/models/full_battery_models/base_battery_model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,6 @@ def options(self, extra_options):
989989
raise pybamm.OptionError(
990990
f"must use surface formulation to solve {self!s} with hydrolysis"
991991
)
992-
993992
self._options = options
994993

995994
def set_standard_output_variables(self):

src/pybamm/models/submodels/thermal/base_thermal.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ def _get_standard_coupled_variables(self, variables):
8888
# TODO: change full stefan-maxwell conductivity so that i_e is always
8989
# a Concatenation
9090
i_e = variables["Electrolyte current density [A.m-2]"]
91+
# Special case for half cell -- i_e has to be a concatenation for this to work due to a mismatch with Q_ohm, so we make a new i_e which is a concatenation.
92+
if (not isinstance(i_e, pybamm.Concatenation)) and (
93+
self.options.electrode_types["negative"] == "planar"
94+
):
95+
i_e = self._get_i_e_for_half_cell_thermal(variables)
96+
9197
phi_e = variables["Electrolyte potential [V]"]
9298
if isinstance(i_e, pybamm.Concatenation):
9399
# compute by domain if possible
@@ -411,3 +417,23 @@ def _yz_average(self, var):
411417
return pybamm.z_average(var)
412418
elif self.options["dimensionality"] == 2:
413419
return pybamm.yz_average(var)
420+
421+
def _get_i_e_for_half_cell_thermal(self, variables):
422+
c_e_s = variables["Separator electrolyte concentration [mol.m-3]"]
423+
c_e_p = variables["Positive electrolyte concentration [mol.m-3]"]
424+
grad_phi_e_s = variables["Gradient of separator electrolyte potential [V.m-1]"]
425+
grad_phi_e_p = variables["Gradient of positive electrolyte potential [V.m-1]"]
426+
grad_c_e_s = pybamm.grad(c_e_s)
427+
grad_c_e_p = pybamm.grad(c_e_p)
428+
T_s = variables["Separator temperature [K]"]
429+
T_p = variables["Positive electrode temperature [K]"]
430+
tor_s = variables["Separator electrolyte transport efficiency"]
431+
tor_p = variables["Positive electrolyte transport efficiency"]
432+
i_e_s = (self.param.kappa_e(c_e_s, T_s) * tor_s) * (
433+
self.param.chiRT_over_Fc(c_e_s, T_s) * grad_c_e_s - grad_phi_e_s
434+
)
435+
i_e_p = (self.param.kappa_e(c_e_p, T_p) * tor_p) * (
436+
self.param.chiRT_over_Fc(c_e_p, T_p) * grad_c_e_p - grad_phi_e_p
437+
)
438+
i_e = pybamm.concatenation(i_e_s, i_e_p)
439+
return i_e

tests/unit/test_expression_tree/test_concatenations.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,18 @@ def test_to_from_json(self, mocker):
445445

446446
# test _from_json
447447
assert pybamm.NumpyConcatenation._from_json(np_json) == conc_np
448+
449+
def test_same_number_of_children(self):
450+
a = pybamm.Variable("y", domain=["1", "2"])
451+
b = pybamm.Variable("z", domain=["3"])
452+
453+
d1 = pybamm.Variable("d1", domain=["1"])
454+
d2 = pybamm.Variable("d2", domain=["2"])
455+
d3 = pybamm.Variable("d3", domain=["3"])
456+
457+
d_concat = pybamm.concatenation(pybamm.sin(d1), pybamm.sin(d2), pybamm.sin(d3))
458+
a_concat = pybamm.concatenation(pybamm.sin(a), pybamm.sin(b))
459+
with pytest.raises(
460+
AssertionError, match="Concatenations must have the same number of children"
461+
):
462+
a_concat + d_concat

0 commit comments

Comments
 (0)