From f8a506e841220dbc0f3d093ce01996c3c8de2af7 Mon Sep 17 00:00:00 2001 From: medha-14 Date: Mon, 28 Oct 2024 16:16:06 +0530 Subject: [PATCH] added test for gradient squared method --- .../test_scikit_finite_element.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/unit/test_spatial_methods/test_scikit_finite_element.py b/tests/unit/test_spatial_methods/test_scikit_finite_element.py index 42b282e08a..e22734fe99 100644 --- a/tests/unit/test_spatial_methods/test_scikit_finite_element.py +++ b/tests/unit/test_spatial_methods/test_scikit_finite_element.py @@ -157,6 +157,36 @@ def test_gradient(self): ans = eqn_disc.evaluate(None, 3 * y**2) np.testing.assert_array_less(0, ans) + def test_gradient_squared(self): + mesh = get_unit_2p1D_mesh_for_testing(ypts=32, zpts=32, include_particles=False) + spatial_methods = { + "macroscale": pybamm.FiniteVolume(), + "current collector": pybamm.ScikitFiniteElement(), + } + disc = pybamm.Discretisation(mesh, spatial_methods) + + var = pybamm.Variable("var", domain="current collector") + disc.set_variable_slices([var]) + + y = mesh["current collector"].coordinates[0, :] + z = mesh["current collector"].coordinates[1, :] + + gradient_squared = pybamm.grad_squared(var) + grad_squared_disc = disc.process_symbol(gradient_squared) + evaluated_gradient_squared = grad_squared_disc.evaluate( + None, 3 * y**2 + 2 * z**2 + ) + + expected_result = 36 * y**2 + 16 * z**2 + expected_result = expected_result[:, np.newaxis] + + try: + np.testing.assert_array_almost_equal( + evaluated_gradient_squared, expected_result + ) + except AssertionError as e: + print("AssertionError:", e) + def test_manufactured_solution(self): mesh = get_unit_2p1D_mesh_for_testing(ypts=32, zpts=32, include_particles=False) spatial_methods = {