-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdg_discretization.jl
377 lines (330 loc) · 16.1 KB
/
dg_discretization.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# In this tutorial, we will learn
# - How to solve a simple PDE with a DG method
# - How to compute jumps and averages of quantities on the mesh skeleton
# - How to implement the method of manufactured solutions
# - How to integrate error norms
# - How to generate Cartesian meshes in arbitrary dimensions
#
# ## Problem statement
#
# The goal of this tutorial is to solve a PDE using a Discontinuous Galerkin (DG) formulation. For
# simplicity, we take the Poisson equation on the unit cube $\Omega \doteq
# (0,1)^3$ as the model problem, namely
# ```math
# \left\lbrace
# \begin{aligned}
# -\Delta u = f \ &\text{in} \ \Omega,\\
# u = g \ &\text{on}\ \Gamma \doteq \partial\Omega,\\
# \end{aligned}
# \right.
# ```
# where $f$ is the source term and $g$ is the prescribed Dirichlet boundary
# function. In this tutorial, we follow the method of manufactured solutions
# since we want to illustrate how to compute discretization errors. We take
# $u(x) = 3 x_1 + x_2^2 + 2 x_3^3 + x_1 x_2 x_3 $ as the exact solution of the
# problem, for which $f(x)= -2 - 12x_3 $ and $g(x) = u(x)$. The selected
# manufactured solution $u$ is a third order multi-variate polynomial, which
# can be represented exactly by the FE discretization that we are going to
# define below. In this scenario, the discretization error has to be close to
# the machine precision. We will use this result to validate the proposed
# implementation.
#
# ## Numerical Scheme
#
# We consider a DG formulation to approximate the problem. In particular, we
# consider the symmetric interior penalty method (see, e.g. [1], for specific
# details). For this formulation, the approximation space is made of
# discontinuous piece-wise polynomials, namely
#
# ```math
# V \doteq \{ v\in L^2(\Omega):\ v|_{T}\in Q_p(T) \text{ for all } T\in\mathcal{T} \},
# ```
# where $\mathcal{T}$ is the set of all cells $T$ of the FE mesh, and $Q_p(T)$
# is a polynomial space of degree $p$ defined on a generic cell $T$. For
# simplicity, we consider Cartesian meshes in this tutorial. In this case, the
# space $Q_p(T)$ is made of multi-variate polynomials up to degree $p$ in each
# spatial coordinate.
#
# In order to write the weak form of the problem, we need to introduce some
# notation. The sets of interior and boundary facets associated with the FE
# mesh $\mathcal{T}$ are denoted here as $\mathcal{F}_\Lambda$ and
# $\mathcal{F}_{\Gamma}$ respectively. In addition, for a given
# function $v\in V$ restricted to the interior facets $\mathcal{F}_\Lambda$, we
# introduce the well known jump and mean value operators,
# ```math
# \begin{aligned}
# \lbrack\!\lbrack v\ n \rbrack\!\rbrack &\doteq v^+\ n^+ + v^- n^-,\\
# \{\! \!\{\nabla v\}\! \!\} &\doteq \dfrac{ \nabla v^+ + \nabla v^-}{2},
# \end{aligned}
# ```
# with $v^+$, and $v^-$ being the restrictions of $v\in V$ to the cells $T^+$,
# $T^-$ that share a generic interior facet in $\mathcal{F}_\Lambda$, and $n^+$,
# and $n^-$ are the facet outward unit normals from either the perspective of
# $T^+$ and $T^-$ respectively.
#
# With this notation, the weak form associated with the interior penalty
# formulation of our problem reads: find $u\in V$ such that $a(u,v) = l(v)$ for
# all $v\in V$. The bilinear and linear forms $a(\cdot,\cdot)$ and $l(\cdot)$
# have contributions associated with the bulk of $\Omega$, the boundary facets
# $\mathcal{F}_{\Gamma}$, and the interior facets $\mathcal{F}_\Lambda$,
# namely
# ```math
# \begin{aligned}
# a(u,v) &= a_{\Omega}(u,v) + a_{\Gamma}(u,v) + a_{\Lambda}(u,v),\\
# l(v) &= l_{\Omega}(v) + l_{\Gamma}(v).
# \end{aligned}
# ```
# These contributions are defined as
# ```math
# \begin{aligned}
# a_{\Omega}(u,v) &\doteq \sum_{T\in\mathcal{T}} \int_{T} \nabla v \cdot \nabla u \ {\rm d}T,
# \\
# l_{\Omega}(v) &\doteq \int_{\Omega} v\ f \ {\rm d}\Omega,
# \end{aligned}
# ```
# for the volume,
# ```math
# \begin{aligned}
# a_{\Gamma}(u,v)
# \doteq
# & - \sum_{F\in\mathcal{F}_{\Gamma}} \int_{F} v\ (\nabla u \cdot n) \ {\rm d}F
# \\ & - \sum_{F\in\mathcal{F}_{\Gamma}} \int_{F} (\nabla v \cdot n)\ u \ {\rm d}F
# \\ & + \sum_{F\in\mathcal{F}_{\Gamma}} \dfrac{\gamma}{|F|} \int_{F} v\ u \ {\rm d}F,
# \\
# l_{\Gamma}(v)
# \doteq
# & - \sum_{F\in\mathcal{F}_{\Gamma}} \int_{F} (\nabla v \cdot n)\ g \ {\rm d}F
# \\ & + \sum_{F\in\mathcal{F}_{\Gamma}} \dfrac{\gamma}{|F|} \int_{F} v\ g \ {\rm d}F,
# \end{aligned}
# ```
# for the boundary facets and,
# ```math
# \begin{aligned}
# a_{\Lambda}(u,v)
# \doteq
# & - \sum_{F\in\mathcal{F}_{\Lambda}} \int_{F} \lbrack\!\lbrack v\ n \rbrack\!\rbrack\cdot \{\! \!\{\nabla u\}\! \!\} \ {\rm d}F
# \\ & - \sum_{F\in\mathcal{F}_{\Lambda}} \int_{F} \{\! \!\{\nabla v\}\! \!\}\cdot \lbrack\!\lbrack u\ n \rbrack\!\rbrack \ {\rm d}F
# \\ & + \sum_{F\in\mathcal{F}_{\Lambda}} \dfrac{\gamma}{|F|} \int_{F} \lbrack\!\lbrack v\ n \rbrack\!\rbrack\cdot \lbrack\!\lbrack u\ n \rbrack\!\rbrack \ {\rm d}F,
# \end{aligned}
# ```
# for the interior facets. In previous expressions, $|F|$ denotes the diameter
# of the face $F$ (in our Cartesian grid, this is equivalent to the
# characteristic mesh size $h$), and $\gamma$ is a stabilization parameter that
# should be chosen large enough such that the bilinear form $a(\cdot,\cdot)$ is
# stable and continuous. Here, we take $\gamma = p\ (p+1)$ as done in the
# numerical experiments in reference [2].
#
# ## Manufactured solution
#
# We start by loading the Gridap library and defining the manufactured solution
# $u$ and the associated source term $f$ and Dirichlet function $g$.
using Gridap
u(x) = 3*x[1] + x[2]^2 + 2*x[3]^3 + x[1]*x[2]*x[3]
f(x) = -2 - 12*x[3]
g(x) = u(x)
# We also need to define the gradient of $u$ since we will compute the $H^1$
# error norm later. In that case, the gradient is simply defined as
∇u(x) = VectorValue(3 + x[2]*x[3],
2*x[2] + x[1]*x[3],
6*x[3]^2 + x[1]*x[2])
# In addition, we need to tell the Gridap library that the gradient of the
# function `u` is available in the function `∇u` (at this moment `u` and `∇u`
# are two standard Julia functions without any connection between them). This
# is done by adding an extra method to the function `gradient` (aka `∇`)
# defined in Gridap:
import Gridap: ∇
∇(::typeof(u)) = ∇u
# Now, it is possible to recover function `∇u` from function `u` as `∇(u)`. You
# can check that the following expression evaluates to `true`.
∇(u) === ∇u
# ## Cartesian mesh generation
# In order to discretize the geometry of the unit cube, we use the Cartesian mesh generator available in Gridap.
L = 1.0
domain = (0.0, L, 0.0, L, 0.0, L)
n = 4
partition = (n,n,n)
model = CartesianDiscreteModel(domain,partition)
# The type `CartesianDiscreteModel` is a concrete type that inherits from
# `DiscreteModel`, which is specifically designed for building Cartesian
# meshes. The `CartesianDiscreteModel` constructor takes a tuple containing
# limits of the box we want to discretize plus a tuple with the number of cells
# to be generated in each direction (here $4\times4\times4$ cells). You can
# write the model in vtk format to visualize it (see next figure).
writevtk(model,"model")
# 
#
# Note that the `CaresianDiscreteModel` is implemented for arbitrary
# dimensions. For instance, the following lines build a
# `CartesianDiscreteModel` for the unit square $(0,1)^2$ with 4 cells per
# direction
domain2D = (0.0, L, 0.0, L)
partition2D = (n,n)
model2D = CartesianDiscreteModel(domain2D,partition2D)
# You could also generate a mesh for the unit tesseract $(0,1)^4$ (i.e., the
# unit cube in 4D). Look how the 2D and 3D models are built and just follow the
# sequence.
#
# ## FE spaces
#
# On top of the discrete model, we create the discontinuous space $V$ as
# follows
order = 3
V = TestFESpace(model,
ReferenceFE(lagrangian,Float64,order),
conformity=:L2)
# We have select a Lagrangian, scalar-valued interpolation of order $3$ within
# the cells of the discrete model. Since the cells are hexahedra, the resulting
# Lagrangian shape functions are tri-cubic polynomials. In contrast to previous
# tutorials, where we have constructed $H^1$-conforming (i.e., continuous) FE
# spaces, here we construct a $L^2$-conforming (i.e., discontinuous) FE space.
# That is, we do not impose any type of continuity of the shape function on the
# cell boundaries, which leads to the discontinuous FE space $V$ of the DG
# formulation. Note also that we do not pass any information about the
# Dirichlet boundary to the `TestFESpace` constructor since the Dirichlet
# boundary conditions are not imposed strongly in this example.
#
# From the `V` object we have constructed in previous code snippet, we build
# the trial FE space as usual.
U = TrialFESpace(V)
# Note that we do not pass any Dirichlet function to the `TrialFESpace`
# constructor since we do not impose Dirichlet boundary conditions strongly
# here.
#
# ## Numerical integration
#
# Once the FE spaces are ready, the next step is to set up the numerical
# integration. In this example, we need to integrate in three different
# domains: the volume covered by the cells $\mathcal{T}$ (i.e., the
# computational domain $\Omega$), the surface covered by the boundary facets
# $\mathcal{F}_{\Gamma}$ (i.e., the boundary $\Gamma = \partial \Omega$), and
# the surface covered by the interior facets $\mathcal{F}_{\Lambda}$ (i.e. the
# so-called mesh skeleton). In order to integrate in $\Omega$ and on its
# boundary $\Gamma$, we use `Triangulation` and `BoundaryTriangulation` objects
# as already discussed in previous tutorials.
Ω = Triangulation(model)
Γ = BoundaryTriangulation(model)
# Here, we do not pass any boundary identifier to the `BoundaryTriangulation`
# constructor. In this case, an integration mesh for the entire boundary
# $\Gamma$ is constructed by default (which is just what we need in this
# example).
#
# In order to generate an integration mesh for the interior facets
# $\mathcal{F}_{\Lambda}$, we use a new type of `Triangulation` referred to as
# `SkeletonTriangulation`. It can be constructed from a `DiscreteModel` object
# as follows:
Λ = SkeletonTriangulation(model)
# As any other type of `Triangulation`, an `SkeletonTriangulation` can be
# written into a vtk file for its visualization (see next figure, where the
# interior facets $\mathcal{F}_\Lambda$ are clearly observed).
writevtk(Λ,"strian")
# 
#
# Once we have constructed the triangulations needed in this example, we define
# the corresponding quadrature rules by passing the triangulations
# together with the desired degree to the `Measure` function.
degree = 2*order
dΩ = Measure(Ω,degree)
dΓ = Measure(Γ,degree)
dΛ = Measure(Λ,degree)
# We still need a way to represent the unit outward normal vector to the
# boundary $\Gamma$, and the unit normal vector on the interior faces
# $\mathcal{F}_\Lambda$. This is done with the `get_normal_vector` getter.
n_Γ = get_normal_vector(Γ)
n_Λ = get_normal_vector(Λ)
# The `get_normal_vector` getter takes either a boundary or a skeleton
# triangulation and returns an object representing the normal vector to the
# corresponding surface. For boundary triangulations, the returned normal
# vector is the unit outwards one, whereas for skeleton triangulations the
# orientation of the returned normal is arbitrary. In the current
# implementation (Gridap v0.5.0), the unit normal is outwards to the cell with
# smaller id among the two cells that share an interior facet in
# $\mathcal{F}_\Lambda$.
#
# ## Weak form
#
# With these ingredients we can define the different terms in the weak form.
# First, we start with the terms $a_\Omega(\cdot,\cdot)$ , and
# $l_\Omega(\cdot)$ associated with integrals in the volume $\Omega$. This is
# done as in the tutorial for the Poisson equation.
a_Ω(u,v) = ∫( ∇(v)⊙∇(u) )dΩ
l_Ω(v) = ∫( v*f )dΩ
# The terms $a_{\Gamma}(\cdot,\cdot)$ and $l_{\Gamma}(\cdot)$ associated with
# integrals on the boundary $\Gamma$ are defined using an analogous approach:
h = L / n
γ = order*(order+1)
a_Γ(u,v) = ∫( - v*(∇(u)⋅n_Γ) - (∇(v)⋅n_Γ)*u + (γ/h)*v*u )dΓ
l_Γ(v) = ∫( - (∇(v)⋅n_Γ)*g + (γ/h)*v*g )dΓ
# Note that in the definition of the functions `a_Γ` and `b_Γ`, we have used
# the object `n_Γ` representing the outward unit normal to the boundary
# $\Gamma$. The code definition of `a_Γ` and `b_Γ` is indeed very close to
# the mathematical definition of the forms $a_{\Gamma}(\cdot,\cdot)$ and
# $b_{\Gamma}(\cdot)$.
#
# Finally, we need to define the term $a_\Lambda(\cdot,\cdot)$ integrated on
# the interior facets $\mathcal{F}_\Lambda$,
a_Λ(u,v) = ∫( - jump(v*n_Λ)⊙mean(∇(u))
- mean(∇(v))⊙jump(u*n_Λ)
+ (γ/h)*jump(v*n_Λ)⊙jump(u*n_Λ) )dΛ
# Note that the arguments `v`, `u` of function `a_Λ` represent a test and trial
# function *restricted* to the interior facets $\mathcal{F}_\Lambda$. As
# mentioned before in the presentation of the DG formulation, the restriction
# of a function $v\in V$ to the interior faces leads to two different values
# $v^+$ and $v^-$ . In order to compute jumps and averages of the quantities
# $v^+$ and $v^-$, we use the functions `jump` and `mean`, which represent the
# jump and mean value operators $\lbrack\!\lbrack \cdot \rbrack\!\rbrack$ and
# $\{\! \!\{\cdot\}\! \!\}$ respectively. Note also that we have used the
# object `n_Λ` representing the unit normal vector on the interior facets. As a
# result, the notation used to define function `a_Λ` is very close to the
# mathematical definition of the terms in the bilinear form
# $a_\Lambda(\cdot,\cdot)$.
#
# Once the different terms of the weak form have been defined, we build and solve the FE problem.
a(u,v) = a_Ω(u,v) + a_Γ(u,v) + a_Λ(u,v)
l(v) = l_Ω(v) + l_Γ(v)
op = AffineFEOperator(a, l, U, V)
uh = solve(op)
# ## Discretization error
#
# We end this tutorial by quantifying the discretization error associated with
# the computed numerical solution `uh`. In DG methods a simple error indicator
# is the jump of the computed (discontinuous) approximation on the interior
# faces. We compute and visualize the jump of these values as follows (see next
# figure):
writevtk(Λ,"jumps",cellfields=["jump_u"=>jump(uh)])
# Note that the jump of the numerical solution is very small, close to the
# machine precision (as expected in this example with manufactured solution).
# 
#
# A more rigorous way of quantifying the error is to measure it with a norm.
# Here, we use the $L^2$ and $H^1$ norms, namely
# ```math
# \begin{aligned}
# \| w \|_{L^2}^2 & \doteq \int_{\Omega} w^2 \ \text{d}\Omega, \\
# \| w \|_{H^1}^2 & \doteq \int_{\Omega} w^2 + \nabla w \cdot \nabla w \ \text{d}\Omega.
# \end{aligned}
# ```
#
# The discretization error can be computed in this example as the difference of
# the manufactured and numerical solutions.
e = u - uh
# We compute the error norms as follows. First, we implement the integrands of
# the norms we want to compute.
l2(u) = sqrt(sum( ∫( u⊙u )*dΩ ))
h1(u) = sqrt(sum( ∫( u⊙u + ∇(u)⊙∇(u) )*dΩ ))
# Then, we compute the corresponding integrals with the `integrate` function.
el2 = l2(e)
eh1 = h1(e)
# The `integrate` function returns a lazy object representing the contribution
# to the integral of each cell in the underlying triangulation. To end up with
# the desired error norms, one has to sum these contributions and take the
# square root. You can check that the computed error norms are close to machine
# precision (as one would expect).
tol = 1.e-10
@assert el2 < tol
@assert eh1 < tol
# ## References
#
# [1] D. N. Arnold, F. Brezzi, B. Cockburn, and L. Donatella Marini. Unified analysis of discontinuous Galerkin methods for elliptic problems. *SIAM Journal on Numerical Analysis*, 39 (5):1749–1779, 2001. doi:[10.1137/S0036142901384162](http://dx.doi.org/10.1137/S0036142901384162).
#
# [2] B. Cockburn, G. Kanschat, and D. Schötzau. An equal-order DG method for the incompressible Navier-Stokes equations. *Journal of Scientific Computing*, 40(1-3):188–210, 2009. doi:[10.1007/s10915-008-9261-1](http://dx.doi.org/10.1007/s10915-008-9261-1).
#