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

Adds gradient_squared method to FiniteVolume #4540

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

Conversation

medha-14
Copy link
Contributor

Description

Fixes #2979

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

@medha-14 medha-14 changed the title Adds gradient_squared method to FiniteVolume` Adds gradient_squared method to FiniteVolume Oct 23, 2024
Copy link

codecov bot commented Oct 23, 2024

Codecov Report

Attention: Patch coverage is 7.14286% with 13 lines in your changes missing coverage. Please review.

Project coverage is 99.21%. Comparing base (9ac0b5b) to head (91aeea5).

Files with missing lines Patch % Lines
src/pybamm/spatial_methods/finite_volume.py 7.14% 13 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4540      +/-   ##
===========================================
- Coverage    99.27%   99.21%   -0.06%     
===========================================
  Files          300      300              
  Lines        22795    22809      +14     
===========================================
+ Hits         22630    22631       +1     
- Misses         165      178      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kratman
Copy link
Contributor

kratman commented Oct 23, 2024

@medha-14 This PR should also add tests and use the gradient squared function where appropriate

@medha-14
Copy link
Contributor Author

I have written a test for the gradient_squared method . Could you please advise on where I should incorporate the use of the gradient_squared method ?

Copy link
Member

@brosaplanella brosaplanella left a comment

Choose a reason for hiding this comment

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

Just some changes, mostly to improve coverages, but getting there!

src/pybamm/spatial_methods/finite_volume.py Outdated Show resolved Hide resolved
src/pybamm/spatial_methods/finite_volume.py Outdated Show resolved Hide resolved
Comment on lines 131 to 158
def gradient_squared(self, symbol, discretised_symbol, boundary_conditions):
"""
Computes the square of the gradient of a symbol.

Parameters
----------
symbol : :class:`pybamm.Symbol`
The symbol for which to compute the gradient squared.
discretised_symbol : :class:`pybamm.Vector`
The discretised variable for which to compute the gradient squared.
boundary_conditions : dict
Boundary conditions for the symbol.

Returns
-------
float
The gradient squared of the symbol.
"""
domain = symbol.domain
gradient_matrix = self.gradient_matrix(domain, symbol.domains)

# Compute gradient squared: (∇u)^2 = u^T (L^T L) u
gradient_squared_matrix = gradient_matrix.T @ gradient_matrix
gradient_squared_result = (
discretised_symbol.T @ gradient_squared_matrix @ discretised_symbol
)

return gradient_squared_result.item()
Copy link
Member

Choose a reason for hiding this comment

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

Here, boundary_conditions is mentioned in the function's signature, but it doesn't look like it's used in the logic of the function anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the feedback. I've updated the gradient_squared function to correctly handle boundary conditions, and I've adjusted the tests accordingly to verify these changes.

Copy link
Member

@agriyakhetarpal agriyakhetarpal left a comment

Choose a reason for hiding this comment

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

Thanks, @medha-14!

src/pybamm/spatial_methods/finite_volume.py Outdated Show resolved Hide resolved
src/pybamm/spatial_methods/finite_volume.py Show resolved Hide resolved
src/pybamm/spatial_methods/finite_volume.py Outdated Show resolved Hide resolved
@agriyakhetarpal
Copy link
Member

I noticed that in the gradient_matrix method above the lines you touched, we could try building the gradient matrix directly in sparse format instead of converting to it later as a potential further improvement – by just pre-allocating all indices at once, and passing them to scipy.sparse.block_diag in favour of the current Kronecker product. Some part of the time there is spent in the array creation alongside the matrix construction, so maybe we can get a 2-4x improvement for larger matrices. Would you like to try that (not in this PR but in a separate one)? I'll be able to help, shall you need it.

@medha-14
Copy link
Contributor Author

I noticed that in the gradient_matrix method above the lines you touched, we could try building the gradient matrix directly in sparse format instead of converting to it later as a potential further improvement – by just pre-allocating all indices at once, and passing them to scipy.sparse.block_diag in favour of the current Kronecker product. Some part of the time there is spent in the array creation alongside the matrix construction, so maybe we can get a 2-4x improvement for larger matrices. Would you like to try that (not in this PR but in a separate one)? I'll be able to help, shall you need it.

@agriyakhetarpal, I would like to give this a go and explore the implementation further. I’d be grateful for any additional insights or recommendations you might have as I move forward.

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.

Implement gradient_squared for FiniteVolume
4 participants