Skip to content

Commit

Permalink
Test high order CR and variants
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrubeck committed Jan 13, 2025
1 parent 6140d5a commit ef0d30f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tests/firedrake/regression/test_helmholtz.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def helmholtz(r, quadrilateral=False, degree=2, mesh=None):
assemble(L)
sol = Function(V)
solve(a == L, sol, solver_parameters={'ksp_type': 'cg'})
# Analytical solution
# Error norm
return sqrt(assemble(inner(sol - expect, sol - expect) * dx)), sol, expect


Expand Down
59 changes: 59 additions & 0 deletions tests/firedrake/regression/test_helmholtz_crouzeix_raviart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""This demo program solves Helmholtz's equation
- div grad u(x, y) + u(x,y) = f(x, y)
on the unit square with source f given by
f(x, y) = (1.0 + 8.0*pi**2)*cos(x[0]*2*pi)*cos(x[1]*2*pi)
and the analytical solution
u(x, y) = cos(x[0]*2*pi)*cos(x[1]*2*pi)
"""

import numpy as np
import pytest

from firedrake import *


def helmholtz(r, quadrilateral=False, degree=1, variant=None, mesh=None):
# Create mesh and define function space
if mesh is None:
mesh = UnitSquareMesh(2 ** r, 2 ** r, quadrilateral=quadrilateral)
V = FunctionSpace(mesh, "CR", degree, variant=variant)

x, y = SpatialCoordinate(mesh)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)

uex = cos(x*pi*2)*cos(y*pi*2)
f = -div(grad(uex)) + uex

a = (inner(grad(u), grad(v)) + inner(u, v))*dx
L = inner(f, v)*dx(degree=12)

params = {"snes_type": "ksponly",
"ksp_type": "preonly",
"pc_type": "lu"}

# Compute solution
sol = Function(V)
solve(a == L, sol, solver_parameters=params)
# Error norm
return sqrt(assemble(dot(sol - uex, sol - uex) * dx)), sol, uex


@pytest.mark.parametrize(('testcase', 'convrate'),
[((1, (4, 6)), 1.9),
((3, (2, 4)), 3.9),
((5, (2, 4)), 5.7)])
@pytest.mark.parametrize("variant", ("point", "integral"))
def test_firedrake_helmholtz_scalar_convergence(variant, testcase, convrate):
degree, (start, end) = testcase
l2err = np.zeros(end - start)
for ii in [i + start for i in range(len(l2err))]:
l2err[ii - start] = helmholtz(ii, degree=degree, variant=variant)[0]
assert (np.array([np.log2(l2err[i]/l2err[i+1]) for i in range(len(l2err)-1)]) > convrate).all()
7 changes: 2 additions & 5 deletions tests/firedrake/regression/test_helmholtz_serendipity.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def helmholtz(r, quadrilateral=True, degree=2, mesh=None):
uex = cos(x*pi*2)*cos(y*pi*2)
f = -div(grad(uex)) + uex

a = (inner(grad(u), grad(v)) + inner(u, v))*dx(degree=12)
a = (inner(grad(u), grad(v)) + inner(u, v))*dx
L = inner(f, v)*dx(degree=12)

params = {"snes_type": "ksponly",
Expand All @@ -45,10 +45,7 @@ def helmholtz(r, quadrilateral=True, degree=2, mesh=None):
# Compute solution
sol = Function(V)
solve(a == L, sol, solver_parameters=params)

# Analytical solution
f = Function(V)
f.project(cos(x*pi*2)*cos(y*pi*2))
# Error norm
return sqrt(assemble(dot(sol - uex, sol - uex) * dx)), sol, uex


Expand Down

0 comments on commit ef0d30f

Please sign in to comment.