From 2db150e2571eb25bbe8b16e7e493de120750220a Mon Sep 17 00:00:00 2001 From: medha-14 Date: Wed, 23 Oct 2024 18:41:20 +0530 Subject: [PATCH] added gradient squared method --- src/pybamm/spatial_methods/finite_volume.py | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/pybamm/spatial_methods/finite_volume.py b/src/pybamm/spatial_methods/finite_volume.py index 5aaf7ea123..34cb437c04 100644 --- a/src/pybamm/spatial_methods/finite_volume.py +++ b/src/pybamm/spatial_methods/finite_volume.py @@ -128,6 +128,33 @@ def gradient_matrix(self, domain, domains): return pybamm.Matrix(matrix) + 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 + ------- + :class:`pybamm.Vector` + 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 = gradient_squared_matrix @ discretised_symbol + + return gradient_squared_result + def divergence(self, symbol, discretised_symbol, boundary_conditions): """Matrix-vector multiplication to implement the divergence operator. See :meth:`pybamm.SpatialMethod.divergence`