Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add post_order #4684

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/pybamm/expression_tree/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_expression_tree/test_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading