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

Generalised symbol copying #4023

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

pipliggins
Copy link
Contributor

@pipliggins pipliggins commented Apr 19, 2024

Description

Provides a general new_copy() function for pybamm symbols which can accept new children.
Some symbols are copied within pybamm using private functions like _unary_new_copy, usually to use overloaded or convenience functions to perform simplifications before creating a new symbol.
As this behaviour can sometimes result in a different symbol class to the one originally copied when new children are provided, this can be overridden using the perform_simplifications argument.

Fixes #3798

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All tests pass: $ python run-tests.py --all (or $ nox -s tests)
  • The documentation builds: $ python run-tests.py --doctest (or $ nox -s doctests)

You can run integration tests, unit tests, and doctests together at once, using $ python run-tests.py --quick (or $ nox -s quick).

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

Abstract out checks for if new children are provided
Improve _unary_new_copy doc strings

This comment was marked as outdated.

@pipliggins pipliggins marked this pull request as ready for review May 9, 2024 22:03
Copy link
Contributor

@martinjrobins martinjrobins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @pipliggins, this looks great. I had a quick look through and had a couple of questions, see below. Also I was thinking it might be good to put in an integration test that copies a larger expression tree, e.g. the rhs of the DFN model, and evaluates it a couple of times to check the output is the same?

@@ -874,7 +874,7 @@ def _process_symbol(self, symbol):

elif isinstance(symbol, pybamm.Function):
disc_children = [self.process_symbol(child) for child in symbol.children]
return symbol._function_new_copy(disc_children)
return symbol.new_copy(disc_children)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this new_copy replace the _unary_new_copy and _binary_new_copy calls above?

return x_average(child)
else:
raise NotImplementedError(
f"{self.__class__.__name__} should always be copied using "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have to use simplification checks here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and others below....

children = [child.create_copy() for child in self.children]
return children

def create_copy(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the difference between create_copy and new_copy? The docs say that new_copy adds attributes, but not sure what this means?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_copy used the print_name setter to ensure the print name was run through the pretty-print functions. No tests broke when I took this out, so I've deprecated new_copy in favour of just using create_copy() (to avoid confusion with model.new_copy())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor: Generalise copy for node of expression tree
2 participants