From 43b0da311557807dad3f51405c061e6a2b55ea7b Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Tue, 17 Dec 2024 11:18:43 +0000 Subject: [PATCH 1/3] add post_order --- CHANGELOG.md | 3 +++ src/pybamm/expression_tree/symbol.py | 7 +++++++ .../unit/test_expression_tree/test_symbol.py | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 920a292683..007fdd35c1 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. ([]()) - 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..b96a4e8cee 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") From 5c31bac9da2fb9df139b9856266ef2601331907c Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Tue, 17 Dec 2024 11:20:45 +0000 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 007fdd35c1..ebc582547b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Features -- Added `Symbol.post_order()` method to return an iterable that steps through the tree in post-order fashion. ([]()) +- 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)) From 0b257a69ee11f68582df6ca0cb635cc1e1681129 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:21:02 +0000 Subject: [PATCH 3/3] style: pre-commit fixes --- tests/unit/test_expression_tree/test_symbol.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_expression_tree/test_symbol.py b/tests/unit/test_expression_tree/test_symbol.py index b96a4e8cee..735724ef11 100644 --- a/tests/unit/test_expression_tree/test_symbol.py +++ b/tests/unit/test_expression_tree/test_symbol.py @@ -230,7 +230,7 @@ def test_multiple_symbols(self): expected_postorder = [ "a", "c", - "*", + "*", "a", "b", "*", @@ -242,7 +242,7 @@ def test_multiple_symbols(self): "a", "*", "-", - "*" + "*", ] for node, expect in zip(exp.pre_order(), expected_preorder): assert node.name == expect