Repository navigation
Add implementation of Hessian recovery using Clement interpolant #23
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a0b8ab5
Add recover_hessian_clement
joewallwork 1c4bbc9
Add tests for Hessian recovery
joewallwork 1d56a17
Only allow Clement Hessians for P1
joewallwork 13fa774
Add recover_gradient(_clement)
joewallwork 3e3afd2
Add tests for catching errors
joewallwork 0a7f29c
Fail-fast
joewallwork 7834dd4
Test Clement gradient recovery
joewallwork 8e543c3
Use dimension ID for fixtures
joewallwork 3b0688f
Use recover_gradient_clement twice for Hessian computation
joewallwork 895a80c
Extend rank check test to Clement, too
joewallwork c944a8a
Drop outdated TODO
joewallwork 2faf53c
Drop another outdated TODO
joewallwork 1dd1a0c
Mention continuous Lagrange specifically in error message
joewallwork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """Unit tests for Hessian recovery based on Clement interpolation.""" | ||
|
|
||
| import firedrake as fd | ||
| import pytest | ||
| import ufl | ||
|
|
||
| from adapt_common.norms import errornorm, norm | ||
| from adapt_common.recovery import recover_hessian_clement | ||
|
|
||
|
|
||
| @pytest.fixture(params=[1, 2, 3], ids=["1D", "2D", "3D"]) | ||
| def dim(request): | ||
| """Set the spatial dimension.""" | ||
| return request.param | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mesh(dim): | ||
| """Create a uniform simplex mesh of [-1, 1]^dim centred on the origin.""" | ||
| n = 20 | ||
| length = 2.0 | ||
| base = { | ||
| 1: fd.IntervalMesh(n, length), | ||
| 2: fd.SquareMesh(n, n, length), | ||
| 3: fd.CubeMesh(n, n, n, length), | ||
| }[dim] | ||
| coords = fd.Function(base.coordinates) | ||
| coords.interpolate(2 * (coords - fd.as_vector([0.5 * length] * dim))) | ||
| return fd.Mesh(coords) | ||
|
|
||
|
|
||
| def bowl(mesh): | ||
| """Quadratic bowl sensor function.""" | ||
| return 0.5 * sum([xi**2 for xi in fd.SpatialCoordinate(mesh)]) | ||
|
|
||
|
|
||
| def relative_error(approx, mesh, ignore_boundary=False): | ||
| """Relative L2 error of the recovered Hessian against the identity.""" | ||
| d = mesh.topological_dimension | ||
| identity = fd.Function(approx.function_space()).interpolate(ufl.Identity(d)) | ||
| cond = fd.Constant(1.0) | ||
| if ignore_boundary: | ||
| x = fd.SpatialCoordinate(mesh) | ||
| cond = ufl.And(x[0] > -0.8, x[0] < 0.8) | ||
| for i in range(1, d): | ||
| cond = ufl.And(cond, ufl.And(x[i] > -0.8, x[i] < 0.8)) | ||
| cond = ufl.conditional(cond, 1, 0) | ||
| err = errornorm(identity, approx, norm_type="L2", condition=cond) | ||
| err /= norm(identity, norm_type="L2", condition=cond) | ||
| return err | ||
|
|
||
|
|
||
| def test_recover_hessian_clement_linear(mesh): | ||
| """Test Clement Hessian recovery for a P1 scalar field (linear bowl).""" | ||
| f = fd.Function(fd.FunctionSpace(mesh, "CG", 1)).interpolate(bowl(mesh)) | ||
|
|
||
| g, H = recover_hessian_clement(f) | ||
|
|
||
| # Check the function spaces of the recovered gradient and Hessian | ||
| g_element = g.function_space().ufl_element() | ||
| assert g_element.family() == "Lagrange" | ||
| assert g_element.degree() == 1 | ||
| assert g_element.num_sub_elements == mesh.geometric_dimension | ||
| H_element = H.function_space().ufl_element() | ||
| assert H_element.family() == "Lagrange" | ||
| assert H_element.degree() == 1 | ||
| assert H_element.num_sub_elements == mesh.geometric_dimension**2 | ||
|
|
||
| # Verify the accuracy of the recovered Hessian away from the boundary | ||
| assert relative_error(H, mesh, ignore_boundary=True) < 1.0e-05 | ||
|
|
||
|
|
||
| def test_recover_hessian_clement_function_error(mesh): | ||
| """Test that an error is raised for non-Function input.""" | ||
| val_err = ( | ||
| "Clement interpolation can only be used to compute gradients of" | ||
| " Lagrange Functions of degree 1." | ||
| ) | ||
| with pytest.raises(ValueError, match=val_err): | ||
| recover_hessian_clement(bowl(mesh)) | ||
|
|
||
|
|
||
| def test_recover_hessian_clement_family_error(mesh): | ||
| """Test that an error is raised for non-Lagrange input.""" | ||
| f = fd.Function(fd.VectorFunctionSpace(mesh, "DG", 1)).assign(1.0) | ||
| val_err = ( | ||
| "Clement interpolation can only be used to compute gradients of" | ||
| " Lagrange Functions of degree 1." | ||
| ) | ||
| with pytest.raises(ValueError, match=val_err): | ||
| recover_hessian_clement(f) | ||
|
|
||
|
|
||
| def test_recover_hessian_clement_degree_error(mesh): | ||
| """Test that an error is raised for degree 0 input.""" | ||
| f = fd.Function(fd.FunctionSpace(mesh, "CG", 2)).assign(1.0) | ||
| val_err = ( | ||
| "Clement interpolation can only be used to compute gradients of" | ||
| " Lagrange Functions of degree 1." | ||
| ) | ||
| with pytest.raises(ValueError, match=val_err): | ||
| recover_hessian_clement(f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add the word "continuous" - if you supply a DG1 field it's not immediately obvious what's wrong (especially if you're used to
"DG", 1instead of"Discontinuous Lagrange", 1)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Done in 1dd1a0c.