|
| 1 | +# # [Mixed-order Hybrid High-Order Method for the Poisson equation](@id poisson_hho) |
| 2 | +# |
| 3 | +# In this tutorial, we will learn how to implement a mixed-order Hybrid High-Order (HHO) method |
| 4 | +# for solving the Poisson equation. HHO methods are a class of modern hybridizable finite element methods |
| 5 | +# that provide optimal convergence rates while enabling static condensation for efficient solution. |
| 6 | +# |
| 7 | +# ## Problem statement |
| 8 | +# |
| 9 | +# We consider the Poisson equation with Dirichlet boundary conditions: |
| 10 | +# |
| 11 | +# ```math |
| 12 | +# \begin{aligned} |
| 13 | +# -\Delta u &= f \quad \text{in } \Omega \\ |
| 14 | +# u &= g \quad \text{on } \partial\Omega |
| 15 | +# \end{aligned} |
| 16 | +# ``` |
| 17 | +# |
| 18 | +# where Ω is a bounded domain in R², f is a source term, and g is the prescribed boundary value. |
| 19 | +# |
| 20 | +# ## HHO discretization |
| 21 | +# |
| 22 | +# The HHO method introduces two types of unknowns: |
| 23 | +# 1. Cell unknowns defined in the volume of each mesh cell |
| 24 | +# 2. Face unknowns defined on the mesh faces/edges |
| 25 | +# |
| 26 | +# This hybrid structure allows for efficient static condensation by eliminating the cell unknowns |
| 27 | +# algebraically at the element level. |
| 28 | +# |
| 29 | +# Load the required packages |
| 30 | + |
| 31 | +using Gridap |
| 32 | +using Gridap.Geometry, Gridap.FESpaces, Gridap.MultiField |
| 33 | +using Gridap.CellData, Gridap.Fields, Gridap.Helpers |
| 34 | +using Gridap.ReferenceFEs |
| 35 | +using Gridap.Arrays |
| 36 | + |
| 37 | +# ## Local projection operator |
| 38 | +# |
| 39 | +# Define a projection operator to map functions onto local polynomial spaces |
| 40 | + |
| 41 | +function projection_operator(V, Ω, dΩ) |
| 42 | + Π(u,Ω) = change_domain(u,Ω,DomainStyle(u)) |
| 43 | + mass(u,v) = ∫(u⋅Π(v,Ω))dΩ |
| 44 | + V0 = FESpaces.FESpaceWithoutBCs(V) |
| 45 | + P = LocalOperator( |
| 46 | + LocalSolveMap(), V0, mass, mass; trian_out = Ω |
| 47 | + ) |
| 48 | + return P |
| 49 | +end |
| 50 | + |
| 51 | +# ## Reconstruction operator |
| 52 | +# |
| 53 | +# Define a reconstruction operator that maps hybrid unknowns to a higher-order polynomial space. |
| 54 | +# This operator is key for achieving optimal convergence rates. |
| 55 | + |
| 56 | +function reconstruction_operator(ptopo,order,X,Ω,Γp,dΩp,dΓp) |
| 57 | + L = FESpaces.PolytopalFESpace(Ω, Float64, order+1; space=:P) |
| 58 | + Λ = FESpaces.PolytopalFESpace(Ω, Float64, 0; space=:P) |
| 59 | + |
| 60 | + n = get_normal_vector(Γp) |
| 61 | + Πn(v) = ∇(v)⋅n |
| 62 | + Π(u,Ω) = change_domain(u,Ω,DomainStyle(u)) |
| 63 | + lhs((u,λ),(v,μ)) = ∫( (∇(u)⋅∇(v)) + (μ*u) + (λ*v) )dΩp |
| 64 | + rhs((uT,uF),(v,μ)) = ∫( (∇(uT)⋅∇(v)) + (uT*μ) )dΩp + ∫( (uF - Π(uT,Γp))*(Πn(v)) )dΓp |
| 65 | + |
| 66 | + Y = FESpaces.FESpaceWithoutBCs(X) |
| 67 | + W = MultiFieldFESpace([L,Λ];style=BlockMultiFieldStyle()) |
| 68 | + R = LocalOperator( |
| 69 | + LocalPenaltySolveMap(), ptopo, W, Y, lhs, rhs; space_out = L |
| 70 | + ) |
| 71 | + return R |
| 72 | +end |
| 73 | + |
| 74 | +# ## Problem setup |
| 75 | +# |
| 76 | +# Define the exact solution and forcing term |
| 77 | + |
| 78 | +u(x) = sin(2*π*x[1])*sin(2*π*x[2]) |
| 79 | +f(x) = -Δ(u)(x) |
| 80 | + |
| 81 | +# Setup the mesh and discretization parameters |
| 82 | + |
| 83 | +n = 10 |
| 84 | +base_model = simplexify(CartesianDiscreteModel((0,1,0,1),(n,n))) |
| 85 | +model = Geometry.voronoi(base_model) |
| 86 | + |
| 87 | +D = num_cell_dims(model) |
| 88 | +Ω = Triangulation(ReferenceFE{D}, model) |
| 89 | +Γ = Triangulation(ReferenceFE{D-1}, model) |
| 90 | + |
| 91 | +ptopo = Geometry.PatchTopology(model) |
| 92 | +Ωp = Geometry.PatchTriangulation(model,ptopo) |
| 93 | +Γp = Geometry.PatchBoundaryTriangulation(model,ptopo) |
| 94 | + |
| 95 | +order = 1 |
| 96 | +qdegree = 2*(order+1) |
| 97 | + |
| 98 | +dΩp = Measure(Ωp,qdegree) |
| 99 | +dΓp = Measure(Γp,qdegree) |
| 100 | + |
| 101 | +# ## FE spaces and operators |
| 102 | +# |
| 103 | +# Define the finite element spaces for cell and face unknowns |
| 104 | + |
| 105 | +V = FESpaces.PolytopalFESpace(Ω, Float64, order+1; space=:P) # Bulk space |
| 106 | +M = FESpaces.PolytopalFESpace(Γ, Float64, order; space=:P, dirichlet_tags="boundary") # Skeleton space |
| 107 | +N = TrialFESpace(M,u) |
| 108 | + |
| 109 | +mfs = MultiField.BlockMultiFieldStyle(2,(1,1)) |
| 110 | +X = MultiFieldFESpace([V, N];style=mfs) |
| 111 | +Y = MultiFieldFESpace([V, M];style=mfs) |
| 112 | +Xp = FESpaces.PatchFESpace(X,ptopo) |
| 113 | + |
| 114 | +# Setup projection and reconstruction operators |
| 115 | + |
| 116 | +PΓ = projection_operator(M, Γp, dΓp) |
| 117 | +R = reconstruction_operator(ptopo,order,Y,Ωp,Γp,dΩp,dΓp) |
| 118 | + |
| 119 | +# Setup assemblers |
| 120 | + |
| 121 | +global_assem = SparseMatrixAssembler(X,Y) |
| 122 | +patch_assem = FESpaces.PatchAssembler(ptopo,X,Y) |
| 123 | + |
| 124 | +# ## Bilinear and linear forms |
| 125 | +# |
| 126 | +# Define the bilinear form a(u,v) for the diffusion term |
| 127 | + |
| 128 | +function a(u,v) |
| 129 | + Ru_Ω, Ru_Γ = R(u) |
| 130 | + Rv_Ω, Rv_Γ = R(v) |
| 131 | + return ∫(∇(Ru_Ω)⋅∇(Rv_Ω) + ∇(Ru_Γ)⋅∇(Rv_Ω) + ∇(Ru_Ω)⋅∇(Rv_Γ) + ∇(Ru_Γ)⋅∇(Rv_Γ))dΩp |
| 132 | +end |
| 133 | + |
| 134 | +# Compute the inverse of local cell measure for stabilization |
| 135 | + |
| 136 | +hTinv = CellField(1 ./ collect(get_array(∫(1)dΩp)), Ωp) |
| 137 | + |
| 138 | +# Define the stabilization term s(u,v) to weakly enforce continuity |
| 139 | + |
| 140 | +function s(u,v) |
| 141 | + function SΓ(u) |
| 142 | + u_Ω, u_Γ = u |
| 143 | + return PΓ(u_Ω) - u_Γ |
| 144 | + end |
| 145 | + return ∫(hTinv * (SΓ(u)⋅SΓ(v)))dΓp |
| 146 | +end |
| 147 | + |
| 148 | +# Define the linear form l(v) for the source term |
| 149 | + |
| 150 | +l((vΩ,vΓ)) = ∫(f⋅vΩ)dΩp |
| 151 | + |
| 152 | +# ## Problem solution |
| 153 | +# |
| 154 | +# Set up the weak form and solve using direct or static condensation |
| 155 | + |
| 156 | +function weakform() |
| 157 | + u, v = get_trial_fe_basis(X), get_fe_basis(Y) |
| 158 | + data = FESpaces.collect_and_merge_cell_matrix_and_vector( |
| 159 | + (Xp, Xp, a(u,v), DomainContribution(), zero(Xp)), |
| 160 | + (X, Y, s(u,v), l(v), zero(X)) |
| 161 | + ) |
| 162 | + assemble_matrix_and_vector(global_assem,data) |
| 163 | +end |
| 164 | + |
| 165 | +function patch_weakform() |
| 166 | + u, v = get_trial_fe_basis(X), get_fe_basis(Y) |
| 167 | + data = FESpaces.collect_and_merge_cell_matrix_and_vector(patch_assem, |
| 168 | + (Xp, Xp, a(u,v), DomainContribution(), zero(Xp)), |
| 169 | + (X, Y, s(u,v), l(v), zero(X)) |
| 170 | + ) |
| 171 | + return assemble_matrix_and_vector(patch_assem,data) |
| 172 | +end |
| 173 | + |
| 174 | +# Direct monolithic solve |
| 175 | + |
| 176 | +A, b = weakform() |
| 177 | +x = A \ b |
| 178 | + |
| 179 | +ui, ub = FEFunction(X,x) |
| 180 | +eu = ui - u |
| 181 | +l2u = sqrt(sum( ∫(eu * eu)dΩp)) |
| 182 | +h1u = l2u + sqrt(sum( ∫(∇(eu) ⋅ ∇(eu))dΩp)) |
| 183 | + |
| 184 | +# Static condensation |
| 185 | + |
| 186 | +op = MultiField.StaticCondensationOperator(X,patch_assem,patch_weakform()) |
| 187 | +ui, ub = solve(op) |
| 188 | + |
| 189 | +eu = ui - u |
| 190 | +l2u = sqrt(sum( ∫(eu * eu)dΩp)) |
| 191 | +h1u = l2u + sqrt(sum( ∫(∇(eu) ⋅ ∇(eu))dΩp)) |
| 192 | + |
| 193 | +# The code above demonstrates both solution approaches: |
| 194 | +# |
| 195 | +# 1. Direct solution of the full system |
| 196 | +# 2. Static condensation to eliminate cell unknowns |
| 197 | +# |
| 198 | +# Both give the same solution but static condensation is typically more efficient |
| 199 | +# for higher orders. |
0 commit comments