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

Implement gradient_squared for FiniteVolume #2979

Open
brosaplanella opened this issue May 23, 2023 · 3 comments · May be fixed by #4540
Open

Implement gradient_squared for FiniteVolume #2979

brosaplanella opened this issue May 23, 2023 · 3 comments · May be fixed by #4540
Assignees
Labels
difficulty: easy A good issue for someone new. Can be done in a few hours feature priority: low No existing plans to resolve

Comments

@brosaplanella
Copy link
Member

brosaplanella commented May 23, 2023

Description

At the moment FiniteVolume does not have a gradient_squared method, so it needs to be computed as the inner product of the gradient with itself.

Motivation

Implementing it would allow some code simplifications in the heat sources (Ohmic & heat of mixing).

Possible Implementation

Main issue is to find how we want to implement it (mathematically). If the discretised gradient of u is L u, then the gradient squared is just u^T L^T L u, where superscript T means transpose. Is this too naive?

Additional context

No Response

@brosaplanella brosaplanella added feature difficulty: easy A good issue for someone new. Can be done in a few hours priority: low No existing plans to resolve labels May 23, 2023
@arunavabasucom
Copy link

@brosaplanella can you assign it to me ?

@medha-14
Copy link
Contributor

I have tried to implemented the gradient square using the previously discussed method. I am open to exploring other implementations as well. If you have any suggestions.

def gradient_squared(self, symbol, discretised_symbol, boundary_conditions):
        """
        Compute 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
        -------
        :class:`pybamm.Vector`
            The gradient squared of the symbol.
        """
        # Compute the gradient of the symbol
        gradient = self.gradient(symbol, discretised_symbol, boundary_conditions)
        
        # Get the gradient matrix
        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 = gradient_squared_matrix @ discretised_symbol
        
        return gradient_squared_result

@brosaplanella
Copy link
Member Author

Hi! Could you please open a PR? It will be easier to discuss there as we will be able to see all the changes & tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: easy A good issue for someone new. Can be done in a few hours feature priority: low No existing plans to resolve
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants