|
| 1 | +"""Unit tests for derivative recovery based on L2-projection.""" |
| 2 | + |
| 3 | +import firedrake as fd |
| 4 | +import pytest |
| 5 | + |
| 6 | +from adapt_common.recovery import recover_gradient_l2 |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(params=[1, 2, 3]) |
| 10 | +def dim(request): |
| 11 | + """Set the spatial dimension.""" |
| 12 | + return request.param |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def mesh(dim): |
| 17 | + """Create a uniform simplex mesh.""" |
| 18 | + n = 4 |
| 19 | + return { |
| 20 | + 1: fd.UnitIntervalMesh(n), |
| 21 | + 2: fd.UnitSquareMesh(n, n), |
| 22 | + 3: fd.UnitCubeMesh(n, n, n), |
| 23 | + }[dim] |
| 24 | + |
| 25 | + |
| 26 | +def test_recover_gradient_p2_scalar(mesh): |
| 27 | + """Test gradient recovery for a P2 scalar field.""" |
| 28 | + # Define a scalar function in P2 space |
| 29 | + f = fd.Function(fd.FunctionSpace(mesh, "CG", 2)) |
| 30 | + x = fd.SpatialCoordinate(mesh) |
| 31 | + f.interpolate(sum([0.5 * xi**2 for xi in x])) |
| 32 | + |
| 33 | + # Recovery its gradient using L2 projection |
| 34 | + grad_f = recover_gradient_l2(f) |
| 35 | + |
| 36 | + # Check the function space of the recovered gradient |
| 37 | + element = grad_f.function_space().ufl_element() |
| 38 | + assert element.family() == "Lagrange" |
| 39 | + assert element.degree() == 1 |
| 40 | + assert element.num_sub_elements == mesh.geometric_dimension |
| 41 | + |
| 42 | + # Verify the accuracy of the recovered gradient |
| 43 | + expected = x |
| 44 | + assert fd.errornorm(expected, grad_f, norm_type="L2") == pytest.approx(0, abs=1e-8) |
| 45 | + |
| 46 | + |
| 47 | +def test_recover_gradient_p2_vector(mesh): |
| 48 | + """Test gradient recovery for a P2 vector field.""" |
| 49 | + # Define a vector function in P2 space |
| 50 | + f = fd.Function(fd.VectorFunctionSpace(mesh, "CG", 2)) |
| 51 | + x = fd.SpatialCoordinate(mesh) |
| 52 | + f.interpolate(fd.as_vector([0.5 * xi**2 for xi in fd.SpatialCoordinate(mesh)])) |
| 53 | + |
| 54 | + # Recovery its gradient using L2 projection |
| 55 | + grad_f = recover_gradient_l2(f) |
| 56 | + |
| 57 | + # Check the function space of the recovered gradient |
| 58 | + element = grad_f.function_space().ufl_element() |
| 59 | + assert element.family() == "Lagrange" |
| 60 | + assert element.degree() == 1 |
| 61 | + assert element.num_sub_elements == mesh.geometric_dimension**2 |
| 62 | + |
| 63 | + # Verify the accuracy of the recovered gradient |
| 64 | + expected = fd.Function(fd.TensorFunctionSpace(mesh, "CG", 1)) |
| 65 | + expected.interpolate( |
| 66 | + fd.as_tensor( |
| 67 | + [ |
| 68 | + [xi if i == j else 0 for j in range(mesh.geometric_dimension)] |
| 69 | + for i, xi in enumerate(x) |
| 70 | + ] |
| 71 | + ) |
| 72 | + ) |
| 73 | + assert fd.errornorm(expected, grad_f, norm_type="L2") == pytest.approx(0, abs=1e-8) |
| 74 | + |
| 75 | + |
| 76 | +def test_recover_gradient_p1_scalar(mesh): |
| 77 | + """Test gradient recovery for a P1 scalar field.""" |
| 78 | + # Define a scalar function in P1 space |
| 79 | + f = fd.Function(fd.FunctionSpace(mesh, "CG", 1)) |
| 80 | + x = fd.SpatialCoordinate(mesh) |
| 81 | + f.interpolate(sum(x)) |
| 82 | + |
| 83 | + # Recovery its gradient using L2 projection |
| 84 | + grad_f = recover_gradient_l2(f) |
| 85 | + |
| 86 | + # Check the function space of the recovered gradient |
| 87 | + element = grad_f.function_space().ufl_element() |
| 88 | + assert element.family() == "Discontinuous Lagrange" |
| 89 | + assert element.degree() == 0 |
| 90 | + assert element.num_sub_elements == mesh.geometric_dimension |
| 91 | + |
| 92 | + # Verify the accuracy of the recovered gradient |
| 93 | + expected = fd.Function(grad_f.function_space()).assign(1.0) |
| 94 | + assert fd.errornorm(expected, grad_f, norm_type="L2") == pytest.approx(0, abs=1e-8) |
| 95 | + |
| 96 | + |
| 97 | +def test_recover_gradient_invalid_input(): |
| 98 | + """Test that an error is raised for invalid input.""" |
| 99 | + val_err = "If a target space is not provided then the input must be a Function." |
| 100 | + with pytest.raises(ValueError, match=val_err): |
| 101 | + recover_gradient_l2("not_a_function") |
| 102 | + |
| 103 | + |
| 104 | +def test_recover_gradient_degree_error(mesh): |
| 105 | + """Test that an error is raised for degree below 1.""" |
| 106 | + val_err = "Input Function must be at least degree 1." |
| 107 | + with pytest.raises(ValueError, match=val_err): |
| 108 | + recover_gradient_l2(fd.Function(fd.FunctionSpace(mesh, "DG", 0))) |
| 109 | + |
| 110 | + |
| 111 | +def test_recover_gradient_rank_error(mesh): |
| 112 | + """Test that an error is raised for unsupported function ranks.""" |
| 113 | + f = fd.Function(fd.TensorFunctionSpace(mesh, "CG", 2)) |
| 114 | + val_err = ( |
| 115 | + "L2 projection can only be used to compute gradients of scalar or vector" |
| 116 | + " Functions, not Functions of rank 2." |
| 117 | + ) |
| 118 | + with pytest.raises(ValueError, match=val_err): |
| 119 | + recover_gradient_l2(f) |
0 commit comments