diff --git a/CHANGELOG.md b/CHANGELOG.md index 920a292683..ebc582547b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,11 @@ # [Unreleased](https://github.com/pybamm-team/PyBaMM/) ## Features + +- Added `Symbol.post_order()` method to return an iterable that steps through the tree in post-order fashion. ([#4684](https://github.com/pybamm-team/PyBaMM/pull/4684)) - Added two more submodels (options) for the SEI: Lars von Kolzenberg (2020) model and Tunneling Limit model ([#4394](https://github.com/pybamm-team/PyBaMM/pull/4394)) + ## Breaking changes - The conda distribution (`pybamm`) now installs all optional dependencies available on conda-forge. Use the new `pybamm-base` conda diff --git a/src/pybamm/expression_tree/symbol.py b/src/pybamm/expression_tree/symbol.py index c4e0043f17..087acf8986 100644 --- a/src/pybamm/expression_tree/symbol.py +++ b/src/pybamm/expression_tree/symbol.py @@ -570,6 +570,13 @@ def pre_order(self): anytree = import_optional_dependency("anytree") return anytree.PreOrderIter(self) + def post_order(self): + """ + returns an iterable that steps through the tree in post-order fashion. + """ + anytree = import_optional_dependency("anytree") + return anytree.PostOrderIter(self) + def __str__(self): """return a string representation of the node and its children.""" return self._name diff --git a/tests/unit/test_expression_tree/test_symbol.py b/tests/unit/test_expression_tree/test_symbol.py index d7374e8da7..735724ef11 100644 --- a/tests/unit/test_expression_tree/test_symbol.py +++ b/tests/unit/test_expression_tree/test_symbol.py @@ -227,8 +227,27 @@ def test_multiple_symbols(self): "c", "a", ] + expected_postorder = [ + "a", + "c", + "*", + "a", + "b", + "*", + "c", + "*", + "a", + "+", + "c", + "a", + "*", + "-", + "*", + ] for node, expect in zip(exp.pre_order(), expected_preorder): assert node.name == expect + for node, expect in zip(exp.post_order(), expected_postorder): + assert node.name == expect def test_symbol_diff(self): a = pybamm.Symbol("a")