FESTUNG: A MATLAB / GNU Octave toolbox for the discontinuous Galerkin
method. Part I: Diffusion operator
Florian Franka , Balthasar Reuterb , Vadym Aizingerb,∗, Peter Knabnerb
a Rice

University, Department of Computational and Applied Mathematics, 6100 Main Street – MS 134, Houston, TX 77005-1892, USA
University of Erlangen–Nürnberg, Department of Mathematics, Cauerstraße 11, 91058 Erlangen, Germany

b Friedrich-Alexander

Abstract
This is the first in a series of papers on implementing a discontinuous Galerkin method as a MATLAB / GNU Octave toolbox. The main goal is the development of techniques that deliver optimized computational performance
combined with a compact, user-friendly interface. Our implementation relies on fully vectorized matrix / vector operations and is carefully documented; in addition, a direct mapping between discretization terms and code routines
is maintained throughout. The present work focuses on a two-dimensional time-dependent diffusion equation with
space / time-varying coefficients. The spatial discretization is based on the local discontinuous Galerkin formulation
and is locally mass conservative. Approximations of orders zero through four based on orthogonal polynomials have
been implemented; more spaces of arbitrary type and order can be easily accommodated by the code structure. Time
discretization is performed using an implicit Euler method.
Keywords: MATLAB, GNU Octave, local discontinuous Galerkin method

1. Introduction
The discontinuous Galerkin (DG) methods first introduced in [1] for a hyperbolic equation started gaining in
popularity with the appearance of techniques to deal with second order terms such as Laplace operators. Two different
approaches to the discretization of second order terms are known in the literature. The first originates from the interior
penalty (IP) methods introduced in the late 1970s and early 1980s for elliptic and parabolic equations (cf. [2] for an
overview). The IP methods discretize the second order operators directly, similarly to the classical finite element
method. To produce a stable scheme, however, they need stabilization terms in the discrete formulation.
In our MATLAB [3] / GNU Octave [4] implementation FESTUNG (F inite E lement S imulation T oolbox for
UN structured G rids) available at [5], we use the second type of DG method called the local discontinuous Galerkin (LDG) method first proposed in [6] and further developed in [7, 8]. This scheme relies on a mixed formulation
that replaces each second order equation with two first order equations introducing in the process an auxiliary flux
variable. As opposed to methods from the IP family the LDG method is also consistent for piecewise constant approximation spaces.
In developing this toolbox we pursue a number of goals:
1. Design a general-purpose software package using the discontinuous Galerkin method—the diffusion operator
described here is only the first step—for a range of standard CFD applications and provide this toolbox as
a research tool in the open source format (cf. [5]).
2. Investigate performance optimized software development strategies in MATLAB / GNU Octave for discontinuous finite element methods on unstructured grids.
∗ Corresponding

author
Email addresses: florian.frank@rice.edu (Florian Frank), reuter@math.fau.de (Balthasar Reuter), aizinger@math.fau.de
(Vadym Aizinger), knabner@math.fau.de (Peter Knabner)

Preprint submitted to Elsevier

August 17, 2014

3. Combine the performance optimization with a well-documented, intuitive user-interface to ease adoption by
a wider community of application and engineering professionals.
4. Make the toolbox and performance optimizations easily transferable and extendable to enable labor-efficient
implementation of new models, terms, and discretizations.
5. Maintain throughout full compatibility with GNU Octave to support users of open source software.
The need for this kind of numerical tool appears to be very urgent right now. On the one hand, the DG methods take
a significant amount of time to implement in a computationally efficient manner—this hinders even wider adoption of
this method in the science and engineering community in spite of the many advantages of this type of discretization.
On the other hand, a number of performance optimizations, including multi-thread and GPU enhancements, combined
with a user-friendly interface make MATLAB and GNU Octave ideal candidates for a general purpose toolbox simple
enough to be used in students’ projects but powerful enough to be employed by researchers and engineers to simulate
problems of intermediate computational complexity.
1.1. Overview of existing MATLAB / GNU Octave DG codes
The authors were unable to find published DG codes running in GNU Octave, and the number of DG codes using
MATLAB is rather small: A MATLAB code for different IP discretizations of the one-dimensional Poisson equation
can be found in [9]. In [10], an IP implementation for the Poisson equation with homogeneous boundary conditions in
two dimensions is presented. A few other (unpublished) DG MATLAB programs can be found online, mostly small
educational codes.
A special mention in this context must go to the book of Hesthaven and Warburton [11] on nodal DG methods.
A large number of classic systems of partial differential equations in one, two, and three space dimensions are covered
by the collection of MATLAB codes accompanying the book. Most of the algorithms are time-explicit or matrix-free,
but the assembly of a full system is also presented. The codes are available for download from [12]. The book and
the codes also utilize the LDG framework for diffusion operators; however, the nodal basis functions used in that
implementation differ in many important ways from orthonormal modal bases adopted in the present work.
None of the DG codes cited above use vectorization in the assembly of global systems. A recent preprint [13]
discusses the vectorized assembly in some detail for an implementation of the hybridizable discontinuous Galerkin
method; however, no code has been provided either in the paper or as a separate download. A few MATLAB toolboxes
for the classical Finite Element Method exist in the literature that focus on computationally efficient application of
vectorization techniques, such as iFEM [14] or p1afem [15]. The vectorized assembly of global matrices is also
demonstrated in [16] for the case of linear continuous elements.
1.2. Structure of this article
The rest of this paper is organized as follows: We introduce the model problem in the remainder of this section,
and describe its discretization using the LDG scheme in Sec. 2. Implementation specific details such as data structures,
reformulation and assembly of matrix blocks, and performance studies follow in Sec. 3. All routines mentioned in this
work are listed and documented in Sec. 4. Some conclusions and an outlook of future work wrap up this publication.
1.3. Model problem
Let J = (0, tend ) be a finite time interval and Ω ⊂ R2 a polygonally bounded domain with boundary ∂Ω subdivided
into Dirichlet ∂ΩD and Neumann ∂ΩN parts. We consider the diffusion equation

∂t c(t, x) − ∇ · d(t, x) ∇c(t, x) = f (t, x)
in J × Ω ,
(1a)
with space / time-varying coefficients d : J × Ω → R+ and f : J × Ω → R. The prototype application of (1a) is
the diffusive transport in fluids, in which case the primary unknown c denotes the concentration of a solute, d is the
diffusion coefficient, and f accounts for generation or degradation of c, e. g., by chemical reactions. Equation (1a) is
complemented by the following boundary and initial conditions, ν denoting the outward unit normal:
on J × ∂ΩD ,

c = cD
−∇c · ν = gN
c = c

on J × ∂ΩN ,

0

0

with given initial c : Ω →

(1b)

R+0

on {0} × Ω

and boundary data cD : J × ∂ΩD →
2

(1c)
(1d)
R+0 ,

gN : J × ∂ΩN → R.

2. Discretization
2.1. Notation
Before describing the LDG scheme for (1) we introduce some notation; an overview can be found in the section “Index of notation”.
Let Th = {T } be a regular family of non-overlapping partitions of Ω into K closed triangles T of characteristic
size h such that Ω = ∪T . For T ∈ Th , let νT denote the unit normal on ∂T exterior to T . Let EΩ denote the set
of interior edges, E∂Ω the set of boundary edges, and E ≔ EΩ ∪ E∂Ω = {E} the set of all edges (the subscript h is
suppressed here). We subdivide further the boundary edges into Dirichlet ED and Neumann EN edges.
For E ∈ EΩ , an interior edge shared by triangles T − and T + , and for x ∈ E, we define the one-sided values of
a scalar quantity w = w(x) by
w− (x) ≔ lim+ w(x − ε νT − )
ε→0

and

w+ (x) ≔ lim+ w(x − ε νT + ) ,
ε→0

respectively. For a boundary edge E ∈ E∂Ω , only the definition on the left is meaningful. The one-sided values of
a vector-valued quantity v are defined analogously. The average and the jump of w on E are then given by
{|w|} ≔ (w− + w+ )/2

and

~w ≔ w− νT − + w+ νT + ,

respectively. Note that ~w is a vector-valued quantity.
2.2. Mixed formulation
To formulate an LDG scheme we first introduce an auxiliary vector-valued unknown z ≔ −∇c and re-write (1) in
the mixed form, also introducing the necessary changes to the boundary conditions:
z = −∇c

in J × Ω ,

∂t c + ∇ · (d z) = f
c = cD

in J × Ω ,
on J × ∂ΩD ,

z · ν = gN
c = c

on J × ∂ΩN ,

0

on {0} × Ω .

(2a)
(2b)
(2c)
(2d)
(2e)

2.3. Variational formulation
Due to the discontinuous nature of DG approximations, we can formulate the variational (as well as the semidiscrete) system of equations on a triangle-by-triangle basis. To do that we multiply both sides of Equations (2a), (2b)
with smooth test functions v, w, correspondingly, and integrate by parts over element T ∈ Th . This gives us
Z
Z
Z
v · νT c(t) = 0 ,
∇ · v c(t) +
v · z(t) −
∂T
T
T
Z
Z
Z
Z

w f (t).
w d(t) z(t) · νT =
∇w · d(t) z(t) +
w ∂t c(t) −
T

T

∂T

T

Placing the test functions left to the solutions has an advantage when assembling the discrete system and will be
discussed at a later time.
2.4. Semi-discrete formulation
We denote by P p (T ) the space of polynomials of degree at most p on T ∈ Th . Let
n
o
P p (Th ) ≔ wh : Ω → R ; ∀T ∈ Th , wh |T ∈ P p (T )

denote the broken polynomial space on the triangulation Th . For the semi-discrete formulation, we assume the following approximation for coefficients (for t ∈ J fixed): dh (t), fh (t), c0h ∈ P p (Th ). A specific way to compute these
approximations will be given in Sec. 3.4; here we only state that it is done using the L2 -projection into P p (T ), therefore the accuracy improves with increasing polynomial order p.
3

Problem 1 (Semi-discrete problem). Seek (zh (t), ch (t)) ∈ [P p (Th )]2 × P p (Th ) such that the following holds for t ∈ J
and ∀T − ∈ Th , ∀vh ∈ [P p (Th )]2 , ∀wh ∈ P p (Th ) :


Z
Z
Z

{|ch (t)|} on EΩ 






cD (t) on ED 
v−h · νT − 
∇ · vh ch (t) +
vh · zh (t) −
= 0,



−
−
−
 c− (t) on E 

∂T
T
T
N
h



Z
Z
{|dh (t) zh (t)|} · νT − + h η− ~ch (t) · νT − on EΩ 
Z





 Z
T





η
−
−
−
−
dh (t) zh (t) · νT − + h − ch (t) − cD (t) on ED 
wh 
∇wh · dh (t) zh (t) +
wh ∂t ch (t) −
wh fh (t)
=


T




∂T −
T−
T−
T−

dh− (t) gN (t)
on EN 

where η > 0 is a penalty coefficient.

In the second equation of Problem 1, the presence of the penalty terms is required to ensure a full rank of the system
in the absence of the time derivative [9, Lem. 2.15]. For analysis purposes, the above equations are usually summed
over all triangles T ∈ Th . In the implementation that follows, however, it is sufficient to work with local equations.
Thus far, we used an algebraic indexing style. In the remainder we switch to a mixture of algebraic and numerical
style: for instance, Ekn ∈ ∂T k ∩EΩ means all possible element indices k ∈ {1, . . . , K} and local edge indices n ∈ {1, 2, 3}
such that Ekn lies in ∂T k ∩ EΩ . This implicitly fixes the numerical indices which accordingly can be used to index
matrices or arrays.
We use a bracket notation followed by a subscript to index matrices and multidimensional arrays. Thus, for an
n-dimensional array X, the symbol [X]i1 ,...,in stands for the component of X with index il in the lth dimension. As in
MATLAB / GNU Octave, a colon is used to abbreviate all indices within a single dimension. For example, [X]:,:,i3,...,in
is a two-dimensional array / matrix.
2.4.1. Local basis representation
In contrast to globally continuous basis functions mostly used by the standard finite element method, the DG basis
functions have no continuity constraints across the triangle boundaries. Thus a basis function ϕki : Ω → R is only
supported on triangle T k ∈ Th (i. e., ϕki = 0 on Ω r T k ) and can be defined arbitrarily while ensuring
!

(p + 1)(p + 2)
p+2
where N ≔
∀k ∈ {1, . . . , K} , P p (T k ) = span ϕki i∈{1,...,N} ,
=
p
2
is the number of local degrees of freedom. Clearly, the number of global degrees of freedom equals KN. Note that N
may in general vary from triangle to triangle, but we assume here for simplicity a fixed polynomial degree p for
every triangle. Closed-form expressions for basis functions on the reference triangle T̂ (cf. Sec. 3.2) employed in our
implementation up to order two are given by:
n

√




2,
ϕ̂1 ( x̂) =
P0 (T̂ )







1

ϕ̂
(
x̂)
=
2
−
P
(
T̂
)


2
1


√ 6 x̂ , 1







ϕ̂3 ( x̂) = 2 3(1 − x̂ − 2 x̂2 ) ,

√

P2 (T̂ ) 


ϕ̂4 ( x̂) = √6 (10 x̂1 − 8) x̂1 + 1 ,







ϕ̂5 ( x̂) =
3 (5 x̂1 − 4) x̂1 + (−15 x̂2 + 12) x̂2 − 1 ,


√



ϕ̂ ( x̂) = 3 5 (3 x̂1 + 8 x̂2 − 4) x̂1 + (3 x̂2 − 4) x̂2 + 1 ,
6



P p (T̂ ) = span ϕ̂1 , . . . , ϕ̂N . Note that these functions are orthonormal with respect to the L2 -scalar product on T̂ . The
advantage of this property will become clear in the next sections. The basis functions up to order four are provided in
the routine phi and their gradients in gradPhi. Bases of even higher order can be constructed, e. g., with the Gram–
Schmidt algorithm or by using a three-term recursion relation—the latter is unfortunately not trivial to derive in the
case of triangles. Note that these so-called modal basis functions ϕ̂i do not posses interpolation properties at nodes
unlike Lagrangian / nodal basis functions, which are often used by the continuous finite element or nodal DG methods.
Local solutions for ch and zh can be represented in terms of the local basis:
#
N
N " 1
X
X
Zk j (t)
ch (t, x) T ≕
Ck j (t) ϕk j (x) ,
zh (t, x) T ≕
ϕ (x) .
k
k
Zk2 j (t) k j
j=1

j=1

4

We condense the coefficients associated with unknowns into two-dimensional arrays C(t), Z1 (t), Z2 (t), such that Ck j (t) ≔
[C(t)]k, j etc. The vectors [C]k,: and [Zm ]k,: , m ∈ {1, 2}, are called local representation vectors with respect to basis

functions ϕki i∈{1,...,N} for ch and for components of zh , correspondingly. In a similar way, we express the coefficient
functions as linear combinations of the basis functions: On T k , we use the local representation vectors [C0 ]k,: for c0h ,
[D]k,: for dh , and [F]k,: for fh .
2.4.2. System of equations
Testing the equations of Problem 1 with vh = [ϕki , 0]T , [0, ϕki]T and wh = ϕki for i ∈ {1, . . . , N} yields a timedependent system of equations whose contribution from T k (identified with T k− in boundary integrals) reads
  N


N


X



1 X






 Ck− j (t) ϕk− j +
Ck+ j (t) ϕk+ j  on EΩ 






2


Z
Z
Z


N
N
j=1
j=1


X
X




m
m
c
(t)
on
E
ϕk− i νk− 
∂ xm ϕki ϕk j +
ϕki ϕk j −
Ck j (t)
Zk j (t)

D
D






∂T k
Tk
Tk
N


X
j=1
j=1






{z
}
|
{z
} |


− j (t) ϕk− j
C
on
E


k
N




II
I


j=1
|
{z
}
III

for m ∈ {1, 2} ,
Z
Z
N
N X
2
N
X
X
X
Dkl (t)
∂ xm ϕki ϕkl ϕk j
ϕki ϕk j −
Zkmj (t)
∂tCk j (t)
= 0

Tk

j=1

l=1

j=1 m=1

(3a)

Tk

{z
}
}|
V
IV



 X
2
N
N
N
N
X
X
X
X



1




m 
m
m





νk− 
Zk− j (t) ϕk− j +
Zk+ j (t) ϕk+ j 
Dk− l (t) ϕk− l
Dk+ l (t) ϕk+ l






2




m=1
j=1
j=1
l=1
l=1










N
N


X
X






η












−
−
+
+
on
E
C
(t)
ϕ
−
C
(t)
ϕ
+


Ω

k
j
k
j
k
j
k
j

Z






h


T k− j=1


j=1


−
ϕk i 
+



N
N
2
N


X
X
X
X





∂T k
η 


m
m





−
−
−
−
−
Zk− j (t) ϕk j +
Dk l (t) ϕk l
νk−
on ED 
Ck j (t) ϕk j − cD (t)








h


T k−


j=1
l=1
m=1
j=1






N


X









−
−
g
(t)
D
(t)
ϕ
on
E
N
k l
k l
N




l=1
|
{z
}

|

{z

(3b)

VI

=

N
X

Fkl (t)

l=1

|

T

Z

ϕki ϕkl ,

Tk

{z
VII

where we abbreviated νT k by νk = [ν1k , ν2k ] . Written in matrix form, system (3) is then given by

 
 


 0  


M
·
H1 +Q1 +Q1N   Z 1 
J D1

 





·
M
H2 +Q2 +Q2N   Z 2  = 
J D2
 0  + 





M ∂t C
G1 +R1 +R1D G2 +R2 +R2D
S +S D
C
KD +KN +L
|
{z
}

}

(4)

≕ A(t)

with the representation vectors
h
iT
m
m
m
m
(t) · · · Z1N
(t) · · · · · · ZK1
(t) · · · ZKN
(t)
Z m (t) ≔ Z11
for m ∈ {1, 2} ,
h
iT
C(t) ≔ C11 (t) · · · C1N (t) · · · · · · C K1 (t) · · · C KN (t) .

The block matrices and the right-hand side of (4) are described in the following Sections 2.4.3 and 2.4.4. Note that
some of the blocks are time dependent (we have suppressed the time arguments here).
5

2.4.3. Contributions from area terms I, II, IV, V, VII
The matrices in the remainder of this section have sparse block structure; by giving definitions for non-zero blocks
we automatically assume a zero fill-in. The mass matrix M ∈ RKN×KN in terms I and IV is defined component-wise as
Z
ϕki ϕk j .
[M](k−1)N+i,(k−1)N+ j ≔
Tk

Since the basis functions ϕki , i ∈ {1, . . . , N} are supported only on T k , M has a block-diagonal structure




MT 1

Z  ϕk1 ϕk1 · · · ϕk1 ϕkN 




 ..
..  ,
..
..
M = 
with
MT k ≔


.
.
.
. 






Tk 
MT K
ϕkN ϕk1 · · · ϕkN ϕkN

(5)


i. e., it consists of K local mass matrices MT k ∈ RN×N . Henceforth we write M = diag MT 1 , . . . , MT K .
m
KN×KN
The block matrices H ∈ R
, m ∈ {1, 2} from term II are given by
Z
∂ xm ϕki ϕk j .
[Hm ](k−1)N+i,(k−1)N+ j ≔ −
Tk

Hence follows the reason for placing the test function left to the solution: otherwise, we would be assembling the
m 
transpose of Hm instead. Similarly to M, matrices Hm are block-diagonal Hm = diag Hm
T 1 , . . . , HT K with local matrices
given by


Z  ∂ xm ϕk1 ϕk1 · · · ∂ xm ϕk1 ϕkN 


..
..
..
Hm

 .
.
Tk ≔ −
.
.

Tk 
∂ xm ϕkN ϕk1 · · · ∂ xm ϕkN ϕkN

In fact, all block matrices for volume integrals have block-diagonal structure due to the local support of the integrands.
The block matrices Gm ∈ RKN×KN , m ∈ {1, 2} from term V
m

[G ](k−1)N+i,(k−1)N+ j ≔ −

N
X
l=1

Dkl (t)

Z

∂ xm ϕki ϕkl ϕk j

Tk

are similar except that we have a non-stationary and a stationary factor. The block-diagonal matrices read Gm =
m 
diag Gm
T 1 , . . . , GT K with local matrices
Gm
Tk ≔ −

N
X
l=1


Z  ∂ xm ϕk1 ϕkl ϕk1

..

Dkl (t)
.

Tk 
∂ xm ϕkN ϕkl ϕk1


· · · ∂ xm ϕk1 ϕkl ϕkN 

..
..
 .
.
.

· · · ∂ xm ϕkN ϕkl ϕkN

(6)

Vector L(t) resulting from VII is obtained by multiplication of the representation vector of fh (t) to the global mass
matrix:
h
iT
L(t) = M F11 (t) · · · F1N (t) · · · · · · F K1 (t) · · · F KN (t) .
2.4.4. Contributions from edge terms III, VI
Interior Edges EΩ . In this section, we consider a fixed triangle T k = T k− with an interior edge Ek− n− ∈ ∂T k− ∩ EΩ =
∂T k− ∩∂T k+ shared by an adjacent triangle T k+ and associated with fixed local edge indices n− , n+ ∈ {1, 2, 3} (cf. Fig. 1).
First, we consider term III in (3a). For a fixed i ∈ {1, . . . , N}, we have a contribution for ϕk− i in the block matrices
Qm , m ∈ {1, 2}
N

1 m X
Ck− j (t)
ν−−
2 k n j=1

Z

N

E k − n−

X
1
ϕk− i ϕk− j + νm
Ck+ j (t)
−
−
2 k n j=1
6

Z

E k − n−

ϕk− i ϕk+ j .

Ek− n−

T k−

T k+
νk− n−

Figure 1: Two triangles adjacent to edge Ek− n− . It holds Ek− n− = Ek+ n+ and νk− n− = −νk+ n+ .

Entries in diagonal blocks of Qm ∈ RKN×KN are then component-wise given by
Z
1 X
1 X
[Qm ](k−1)N+i,(k−1)N+ j ≔
νm
νm
ϕki ϕk j =
kn
kn [SEkn ]i, j ,
2 E ∈∂T ∩E
2
Ekn
E ∈∂T ∩E
kn

k

Ω

kn

k

(7a)

Ω

N×N

where local matrix SEkn ∈ R
corresponds to interior edge Ekn of T k , n ∈ {1, 2, 3}:


Z  ϕk1 ϕk1 · · · ϕk1 ϕkN 
 .
..  .
..
 ..
SEkn =
.
. 


Ekn 
ϕkN ϕk1 · · · ϕkN ϕkN

(7b)

Entries in off-diagonal blocks in Qm are only non-zero for pairs of triangles T k− , T k+ with ∂T k− ∩ ∂T k+ , ∅. They
consist of the mixed terms containing basis functions from both adjacent triangles and are given as
Z
1
ϕk− i ϕk+ j .
(7c)
[Qm ](k− −1)N+i,(k+ −1)N+ j ≔ νm
−
−
2 k n E k − n−
Note that the local edge index n− is given implicitly since ∂T k− ∩ ∂T k+ , ∅ consist of exactly one edge Ek− n− = Ek+ n+ .
Next, consider term VI in (3b) containing average and jump terms that produce contributions to multiple block
matrices for ϕk− i ,
N

N

X
1 m X
Dk− l (t)
νk− n−
Zkm− j (t)
2
j=1
l=1

Z

N

E k − n−

N

X
X
1
ϕk− i ϕk− l ϕk− j + νm
Dk+ l (t)
Zkm+ j (t)
k− n−
2
j=1
l=1

Z

E k − n−

ϕk− i ϕk+ l ϕk+ j

and
N
η X

hT k−

Ck− j (t)

j=1

Z

E k − n−

ϕk− i ϕk− j −

N
η X

hT k−

Ck+ j (t)

j=1

Z

E k − n−

ϕk− i ϕk+ j .

The first integrals are responsible for entries in the diagonal and off-diagonal blocks of a block matrix Rm ∈ RKN×KN
that end up in the last row of system (15). Entries in diagonal blocks are given component-wise by
m

[R ](k−1)N+i,(k−1)N+ j

Z
N
X
1 X
1
m
≔
ϕki ϕkl ϕk j =
ν
Dkl (t)
2 E ∈∂T ∩E kn l=1
2
Ekn
kn

k

Ω

X

νm
kn [REkn ]i, j

(8a)

Ekn ∈∂T k ∩EΩ

with
REkn =

N
X
l=1


 ϕk1 ϕkl ϕk1

..

Dkl (t)
.


Ekn
ϕkN ϕkl ϕk1
Z


· · · ϕk1 ϕkl ϕkN 

..
..
 .
.
.

· · · ϕkN ϕkl ϕkN

(8b)

Once again, entries in non-zero off-diagonal blocks consist of the mixed terms:
N

[Rm ](k− −1)N+i,(k+ −1)N+ j ≔

1 m X
Dk+ l (t)
ν−−
2 k n l=1

Z

E k − n−

ϕk− i ϕk+ l ϕk+ j .
7

(8c)

All off-diagonal blocks corresponding to pairs of triangles not sharing an edge are zero.
The second integral from term VI results in a block matrix S ∈ RKN×KN similar to Qm . Its entries differ only in the
coefficient and the lack of the normal. hT k from the definition of the penalty term in Problem 1 is replaced here with
the local edge length |Ekn | to ensure the uniqueness of the flux over the edge that is necessary to ensure the local mass
conservation. In the diagonal blocks we can reuse the previously defined SEkn and have
Z
X
X
1
1
[S](k−1)N+i,(k−1)N+ j ≔ η
ϕki ϕk j = η
[SEkn ]i, j ,
(9a)
|Ekn | Ekn
|Ekn |
E ∈∂T ∩E
E ∈∂T ∩E
kn

k

Ω

kn

and entries in off-diagonal blocks are given as
Z
η
ϕk− i ϕk+ j .
[S](k− −1)N+i,(k+ −1)N+ j ≔ −
|Ek− n− | Ek− n−

k

Ω

(9b)

Dirichlet Edges ED . Consider the Dirichlet boundary ∂ΩD . The contribution of term III of (3a) consists of a prescribed
data cD (t) only and consequently enters system (4) on the right-hand side as vector J Dm ∈ RKN , m ∈ {1, 2} with
Z
X
νm
ϕki cD (t) .
(10)
[J Dm ](k−1)N+i ≔ −
kn
Ekn

Ekn ∈∂T k ∩ED

Term VI of (3b) contains dependencies on zh (t), ch (t), and the prescribed data cD (t); thus, it produces three contribuKN×KN
tions to system (4): on the left-hand side the blocks Rm
, m ∈ {1, 2} (cf. (8a), (9a))
D , SD ∈ R
X
νm
(11)
[ Rm
kn [REkn ]i, j ,
D ](k−1)N+i,(k−1)N+ j ≔
Ekn ∈∂T k ∩ED

X

[SD ](k−1)N+i,(k−1)N+ j ≔ η

Ekn ∈∂T k ∩ED

1
[SEkn ]i, j ,
|Ekn |

(12)

and on the right-hand side the vector KD ∈ RKN ,
Z
X
1
ϕki cD (t) .
[KD ](k−1)N+i ≔ η
|Ekn | Ekn
E ∈∂T ∩E
kn

k

(13)

D

Neumann Edges EN . Consider the Neumann boundary ∂ΩN . Term III of (3a) replaces the average of the primary
KN×KN
variable over the edge by the interior value resulting in the block-diagonal matrix Qm
(cf. (7a), (7b)) with
N ∈R
X
νm
(14)
[Qm
kn [SEkn ]i, j .
N ](k−1)N+i,(k−1)N+ j ≔
Ekn ∈∂T k ∩EN

Term VI contributes to the right-hand side of system (4) since it contains given data only. The corresponding vector KN ∈ RKN reads
Z
N
X X
ϕki ϕkl gN (t) .
[KN ](k−1)N+i ≔ −
Dkl (t)
Ekn ∈∂T k ∩EN l=1

Ekn

2.5. Time discretization
The system (4) is equivalent to
W∂t Y(t) + A(t) Y(t) = V(t)

(15)

with A(t) as defined in (4) and solution Y(t) ∈ R3KN , right-hand-side vector V(t) ∈ R3KN , and matrix W ∈ R3KN×3KN
defined as
 1 




 Z (t)


0 0 0 
J D1 (t)
 2 




 ,
J D2 (t)
Y(t) ≔  Z (t) ,
V(t) ≔ 
W = 0 0 0  .






C(t)
KD (t)+KN (t)+L(t)
0 0 M
8

We discretize system (15) in time using for simplicity the implicit Euler method (generally, one has to note here that
higher order time discretizations such as TVB (total variation bounded) Runge–Kutta methods [17] will be needed in
the future for applications to make an efficient use of high order DG space discretizations). Let 0 = t1 < t2 < . . . < tend
be a not necessarily equidistant decomposition of the time interval J and let ∆tn ≔ tn+1 − tn denote the time step size.
One step of our time discretization is formulated as


W + ∆tn An+1 Y n+1 = W Y n + ∆tn V n+1 ,

where we abbreviated An ≔ A(tn ), etc.
3. Implementation

We obey the following implementation conventions:
1. Compute every piece of information only once. In particular, this means that stationary parts of the linear system
to be solved in a time step should be kept in the memory and not repeatedly assembled and that the evaluation
of functions at quadrature points should be carried out only once.
2. Avoid long for loops. With “long” loops we mean loops that scale with the mesh size, e. g., loops over the
triangles T k ∈ Th or edges Ek ∈ EΩ . Use vectorization instead.
3. Avoid changing the nonzero pattern of sparse matrices. Assemble global block matrices with the command
sparse( , , , , ), kron, or comparable commands.
Furthermore, we try to name variables as close to the theory as possible. Whenever we mention a non-built-in MATLAB / GNU Octave routine they are to be found in Sec. 4.
3.1. Grid / triangulation
In Sec. 2, we considered a regular family of triangulations Th that covers a polygonally bounded domain Ω. Here
we fix the mesh fineness h and simply write T to denote the grid and also the set of triangles {T }.
3.1.1. Data structures
When writing MATLAB / GNU Octave code it is natural to use list oriented data structures. Therefore, the properties of the grid T are stored in arrays in order to facilitate vectorization, in particular, by using those as index arrays.
When we deal with a stationary grid it is very beneficial to precompute those arrays in order to have access to readily
usable information in the assembly routines. All lists describing T fall in two categories: “geometric data” containing properties such as the coordinates of vertices vkn ∈ V or the areas of triangles T k ∈ T and “topological data”
describing, e. g., the global indices k+ , k− of triangles sharing an edge Ek− n− .
The most important lists are described in Tab. 1. Those and further lists are assembled by means of the routine generateGridData , and are in some cases based on those presented in [18]. All lists are stored in a variable of
type struct even though it would be more efficient to use a class (using classdef) instead that inherits from the
class handle. However, this object-oriented design strategy would go beyond the scope of this article.
3.1.2. Interfaces to grid generators
The routine generateGridData requires a list of vertex coordinates coordV and an index list V0T (cf. Tab. 1) to
generate all further lists for the topological and geometric description of the triangulation. Grid generators are a great
tool for the creation of coordV and V0T. We give two examples of possible interfaces to grid generators:
1. The routine domainCircle makes a system call to the free software Gmsh [19]. According to the geometry
description of the domain in domainCircle.geo, Gmsh generates the ASCII file domainCircle.mesh containing
the grid data and coordV and V0T can be extracted to call generateGridData .
2. MATLAB’s toolbox for partial differential equations also provides a grid generator. The usage is exemplified
by the routine domainPolygon which generates a triangulation of a polygonally bounded domain.
Additionally, we provide the routine domainSquare , which produces a Friedrichs–Keller triangulation of a square
with given mesh size and without employing any grid generators. An example for meshes produced by each routine
is shown in Fig. 2.
9

list

dimension

description

numT

scalar
scalar
scalar

number of triangles #T = K
number of edges #E
number of vertices #V

#T × 2 × 2
#T × 3
#T × 3

transformation matrices Bk according to (17a)
global edge indices of triangles
edge IDs for edges Ekn (used to identify the interior and boundary edges as well as Dirichlet
and Neumann edges)
the (n− , n+ )th entry of this cell is a sparse K×K array whose (k− , k+ )th entry is one if Ek− n− =
Ek+ n+
signs of edges (reflects the orientation of the globally defined edge normals: the (k, n)th
entry is one, if the global normal on Ekn has the same orientation as νkn and −1 otherwise)
triangle indices of edges (nth row contains the index of the triangle against direction of the
global normal on En (i. e. T − if νEn = νT ) followed by the triangle index the global normal
is pointing to (i. e. T + if νEn = νT ))
global vertex indices of edges (accounting for the global edge directions which equal the
direction of the global edge normals rotated by pi/2
global vertex indices of triangles accounting for the counter-clockwise ordering

numE
numV
B
E0T
idE0T

markE0TE0T

3 × 3 (cell)

sigE0T

#T × 3

T0E

#E × 2

V0E

#E × 2

V0T

#T × 3

areaE0T

#T
#T
#T
#T

areaT
coordV0T
nuE0T

×3
×1
×3×2
×3×2

edge lengths |Ekn |
triangle areas |T |k
vertex coordinates vkn
edge normals νkn

Table 1: Arrays generated by the routine generateGridData storing topological (top, middle) and geometric (bottom) grid descriptions.
5
63

6 2
1

7 2
1 1

6

5
35
9

1 5
2

215
2161
1
10
3
3
6 83 8
7 34 4
103
1
3 2
3
17
3
2
3 91
2
3
1
2
1132
1
114
3
12
4
3
39
1
2
2
33
2 4
1 12 1 3 1
11 222
82
1 21
220 2
1
3
2
33 1
3 113
3
1
5 3
2 2
6 73
1
2
18
19
1
2
2
1
2
3
1
2
1
2
1
1
1
10
2

2 11
3

g = domainPolygon([0 .5 .5 1 1 0], ...

3
5
6
13 1 11 5
2
9
18220 3 7 3 2
3 237 1
53
6
1
21
39
5
2
1
36
3
2 1 32 3 14
3 3 3317
232
15
238 1 8
41
72
1
3
136 2 3 1 23212
3 33
1
4 2 2
11
1 32 1
1
2
24
42
22
3
3
10
1
3
1 34 6
20
2 2
40
2 12
5 22 6
3
19
2
33
331
334 1 11 3 2 18
2 135
2 1
1
1 141 25
2 312 323
2 1 9
21 3
1
1
2
1
1 27 114
1 213 2 3 15
245
16
3
3 29 33 243
3
3 130
2
28
3315
2
8
2 212
1
218
222
3 13
3 171
1
87
8 10
46
1
2
1
2
1
19
14
53
2 13 2
3 193 31344
1
3
2
3
3
21 3262 2 24
3
29
27
2
1 3 21 225
1 2111
12
1 51
3 154 2216
2 324
1
3 49
3 150
2 203
326
281 2 30
1 23 22
3
3
97
1 316
3 48
23 223
812 52 17
2
1 2 3
1
2 13
47
2 321 2110
13 2
3
11 111 7
8
7
5
62
11

g = domainCircle(1/3)

3
12
1

4
3
3

12
313
2
110
2

4 32
3

15
323
2
120
2

1
2 1
7
2 3
3
11
311
2
7
1
2

4 22
2

4 12

5

1
1
2 1
5

3
29
1

1
2
15
2

17
3312
2
127
8

1
2 1
10
2 3
3
13
318
2
15
1
2
3
16
1

4
3
4
1

18
3332
2
130

1
2 1
11
2 3
3
14
321
2
17
1
2

3
6
1

16
2

9
3
19
1

1
2 1
6
2 3
3
10
38
2
5
1
2

1
2
3
3

3
32
1

12
2 3
3

6
3
9
1

1
3
3
3

1
1

3
22
1

8
2 3
3

3
26
1

1
2
14
2

16
3282
2
125
7

3
14
1

1
2 1
9

3
24
1

1
2
13

g = domainSquare(1/3)

[0 0 .5 .5 1 1], .5)

Figure 2: Three examples for commands to build a grid. Each is visualized using visualizeGrid(g) (cf. Sec. 3.10).

3.2. Backtransformation to the reference triangle
The computation of the volume and edge integrals in the discrete system (3) is expensive when performed for each
triangle T k of the grid Th . A common practice is to transform the integrals over physical triangles T k to a reference
triangle T̂ and then to compute the integrals either by numerical quadrature or analytically. Both approaches are
presented in this article. We use the unit reference triangle T̂ as described in Fig. 3 and define for T k ∈ Th an affine
one-to-one mapping
Fk : T̂ ∋ x̂ 7→ x ∈ T k .
10

vk3

x̂2

b

1 v̂3

Fk

b

Ê1

Ê2
0

v̂1
b

T̂

Tk

x2
b

1

b

vk1

v̂2
Ê3

0

Ek1

Ek2

x̂1

Ek3

b

vk2

x1

Figure 3: The affine mapping Fk transforms the reference triangle T̂ with vertices v̂1 = [0, 0]T , v̂2 = [1, 0]T , v̂3 = [0, 1]T to the physical triangle T k
with vertices vki , i ∈ {1, 2, 3} assumed to be ordered counter-clockwise.

Thus any function w : T k → R implies ŵ : T̂ → R by ŵ = w ◦ Fk , i. e., w(x) = ŵ( x̂) . The transformation of the
gradient is obtained by the chain rule:
"
#
∂ x1 w(x) ∂ x̂1 Fk1 ( x̂) + ∂ x2 w(x) ∂ x̂1 Fk2 ( x̂)
ˆ
ˆ k ( x̂)T ∇w(x) = ∇Fk T ∇w(x) ,
∇ŵ( x̂) = ∇w ◦ Fk ( x̂) =
= ∇F
∂ x1 w(x) ∂ x̂2 Fk1 ( x̂) + ∂ x2 w(x) ∂ x̂2 Fk2 ( x̂)
T

ˆ = [∂ x̂1 , ∂ x̂2 ]T , Fk = [F 1 , F 2 ] . In short,
where we used the notation ∇
k
k
∇ = ∇Fk

−T ˆ
∇

(16)

on T k . Since T̂ was explicitly defined, the affine mapping can be expressed explicitly in terms of the vertices vk1 , vk2 , vk3
of T k by
i
h
(17a)
Fk : T̂ ∋ x̂ 7→ Bk x̂ + vk1 ∈ T k with R2×2 ∋ Bk ≔ vk2 − vk1 vk3 − vk1 .
Clearly, ∇Fk = Bk . The inverse mapping to Fk is easily computed:
F−1
k :

T k ∋ x 7→ B−1
k (x − vk1 ) ∈ T̂ .

(17b)

Since the orientations of all physical triangles are the same as of the reference triangle (cf. Fig. 3), 0 < det Bk = 2|T k |
holds. For a function w : Ω → R, we use transformation formula
Z
Z
(18a)
w(x) dx = 2|T k | w ◦ Fk ( x̂) d x̂ .
T̂

Tk

The transformation rule for an integral over the edge Ekn ⊂ T k reads
Z
Z
|Ekn |
w(x) dx =
w ◦ Fk ( x̂) d x̂ .
|Ên | Ên
Ekn

(18b)

The rule (18b) is derived as follows: Denote by γkn : [0, 1] ∋ s 7→ γkn (s) ∈ Ekn a parametrization of the edge Ekn with
derivative γ′kn . For instance γk2 (s) ≔ (1 − s) vk3 + s vk1 . Let γ̂n : [0, 1] ∋ s 7→ γ̂n (s) ∈ Ên be defined analogously. From
Z

w(x) dx =
Ekn

Z

1

0

w ◦ γkn (s) |γ′kn (s)| ds =

Z

0

1

w ◦ γkn (s) |Ekn | ds

and
Z

Ên

w ◦ Fk ( x̂) d x̂ =

Z

1

0

w ◦ Fk ◦ γ̂n (s) |γ̂′n (s)| ds =

Z

follows the statement in (18b).
11

0

1

w ◦ γkn (s) |Ên | ds

3.3. Numerical integration
As an alternative to the symbolic integration functions provided by MATLAB we implemented a quadrature integration functionality for triangle and edge integrals. In addition, this functionality is required to produce L2 -projections
(cf. Sec. 3.4) of all nonlinear functions (initial conditions, right-hand side, etc.) used in the system.
Since we transform all integrals on T k ∈ Th to the reference triangle T̂ (cf. Sec. 3.2), it is sufficient to define the
quadrature rules on T̂ (which, of course, can be rewritten to apply for every physical triangle T = FT (T̂ )):
Z
R
X
ĝ( q̂) ≈
ωr ĝ( q̂r )
(19)
T̂

r=1

with R quadrature points q̂r ∈ T̂ and quadrature weights ωr ∈ R. The order of a quadrature rule is the largest integer s
such that (19) is exact for polynomials g ∈ P s (T̂ ). Note that we exclusively rely on quadrature rules with positive
weights and quadrature points located strictly in the interior of T̂ and not on ∂T̂ . The rules used in the implementation
are found in the routine quadRule2D. The positions of q̂r for some quadrature formulas are illustrated in Fig. 4.
An overview of quadrature rules on triangles is found in the “Encyclopaedia of Cubature Formulas” [20]. For edge
integration, we rely on standard Gauss quadrature rules of required order.
The integrals in (3) contain integrands that are polynomials of maximum order 3p−1 on triangles and of maximum
order 3p on edges. Using quadrature integration one could choose rules that integrate all such terms exactly; however,
sufficient accuracy can be achieved with quadrature rules that are exact for polynomials of order 2p on triangles and
2p + 1 on edges (see [21]).
b

b

b

b
bc

bc

bc

bc
bc

bc

bc

bc
bc

bc
bc

bc
bc

b

b

b

order 2

bc
bc
b

bc

order 3

bc
bc

b

b

b

order 4

bc

bc
b

order 5

Figure 4: Positions of the quadrature points q̂r on the reference triangle T̂ as used in the routine quadRule2D for quadrature rules of order 2 to 5.

3.4. Approximation of coefficient functions and initial conditions
In Section 2.4, we assumed the coefficient functions and initial conditions given in piecewise polynomial spaces,
for instance, dh (t) ∈ Pd (Th ) for t ∈ J. If we have an algebraic expression for a coefficient, say d, we seek the
representation matrix D(t) ∈ RK×N satisfying
dh (t, x) T =
k

N
X

Dk j (t) ϕk j (x) ,

j=1

such that dh (t) is an adequate approximation of d(t). A simple way (also used in this work) to produce dh is the
L2 -projection defined locally for T k ∈ Th by
Z
Z
wh d(t) .
wh dh (t) =
∀wh ∈ Pd (T ) ,
Tk

Tk

Choosing wh = ϕki for i ∈ {1 . . . , N} and using the affine mapping Fk we obtain
Z
Z
Z
Z
N
N
X
X

ϕ̂i ( x̂) d t, Fk ( x̂) d x̂ ,
ϕki (x) d(t, x) dx ⇔
Dk j (t) ϕ̂i ( x̂) ϕ̂ j ( x̂) d x̂ =
ϕki (x) ϕk j (x) dx =
Dk j (t)
j=1

Tk

Tk

j=1

T̂

where the factor of 2|T k | canceled out. Written in matrix form, this is equivalent to




 Dk1 
Z  ϕ̂1 ( x̂) d t, Fk ( x̂) 

 . 

..
 d x̂
M̂  ..  =

.



T̂ 
DkN
ϕ̂N ( x̂) d t, Fk ( x̂)
12

T̂

with local mass matrix on the reference triangle M̂ ∈ RN×N defined as in (21). This N × N system of equations
can be solved locally for every k ∈ {1, . . . , K}. Approximating the right-hand side by numerical quadrature (19) and
transposing the equation yields


 



 d t, F1 ( q̂r ) 
 d t, F1 ( q̂1 ) . . . d t, F1 ( q̂R )   ω1 ϕ̂1 ( q̂1 ) . . . ω1 ϕ̂N ( q̂1 ) 
R
X
h
i








 
..
..
..
..
..
..
..
 .
D(t) M̂ =
ωr 
 ϕ̂1 ( q̂r ), . . . , ϕ̂N ( q̂r ) = 
 
.
.
.
.
.
.
.





 
r=1
d t, FK ( q̂r )
d t, FK ( q̂1 ) . . . d t, FK ( q̂R )
ωR ϕ̂1 ( q̂R ) . . . ωR ϕ̂N ( q̂R )

This is the global matrix-valued (transposed) system of equations with unknown D(t) ∈ RK×N and a right-hand side
of dimension K × N. The corresponding routine is projectAlg2DG.

3.5. Computation of the discretization error
The discretization error kch (t)−c(t)kL2 (Ω) at time t ∈ J gives the L2 -norm of the difference between the approximate
ch (t) and the analytical solution c(t) with the latter usually specified as an algebraic function. This computation is
utilized in the computation of the experimental rate of convergence of the numerical scheme (cf. Sec. 3.9).
Just as in the previous section we use numerical quadrature after transforming the integral term to the reference
triangle T̂ . The arising sums are vectorized for reasons of performance. Suppressing the time argument, we have
kch − ck2L2 (Ω) =

Z X
N
X
X Z 
2
2
Ckl ϕ̂l ( x̂) − c ◦ Fk ( x̂) d x̂
|T k |
ch (x) − c(x) dx = 2

T k ∈Th

Tk

T k ∈Th

T̂

l=1


 


2  
 |T 1 |   C11 . . . C1N   ϕ̂1 ( q̂1 ) . . . ϕ̂1 ( q̂R ) 
  ω1 
N
R
X
X
X
2


 .   .



  



.
.
.
1
2



..   ..
..  − c X , X   ...  ,
Ckl ϕ̂l ( q̂r ) − c ◦ Fk ( q̂r ) = 2  ..  ·  ..
|T k |
≈2
ωr
 
 

  

l=1
r=1
T k ∈Th
|T K |
C K1 . . . C KN ϕ̂N ( q̂1 ) . . . ϕ̂N ( q̂R )
ωR

where the arguments of c,
 m
 F1 ( q̂1 ) . . .
 .
m
X ≔  ..
 m
F K ( q̂1 ) . . .


F1m ( q̂R )

..  ,
. 

F Km ( q̂R )

can be assembled using a Kronecker product. The abuse of notation c(X1 , X2 ) clearly stands for the K × R matrix
with the entry c([X1 ]k,r , [X2 ]k,r ) in the kth row and rth column. The above procedure is implemented in the routine computeL2Error .
3.6. Assembly
The aim of this section is to transform the terms required to build the block matrices in (4) to the reference
triangle T̂ and then to compute those either via numerical quadrature or analytically. The assembly of the block
matrices from the local contributions is then performed in vectorized operations.
For the implementation, we need the explicit form for the components of the mappings Fk : T̂ → T k and their
inverses F−1
k : T k → T̂ as defined in (17). Recalling that 0 < det Bk = 2|T k | (cf. Sec. 3.2), we obtain
"
#
#
" 11 1
1
1
12 2
2
2
1
1 B22
Bk x̂ + B12
−1
k (x − v1k ) − Bk (x − v1k ) .
k x̂ + v1k
(x)
=
and
F
Fk ( x̂) =
2
2
21 1
1
1
22 2
2
k
B21
2 |T k | B11
k (x − v1k ) − Bk (x − v1k )
k x̂ + Bk x̂ + v1k
From (16) we obtain the component-wise rule for the gradient in x ∈ T k :
#
"
" #
1 B22
∂ x̂1 − B21
∂ x̂2
∂ x1
k
k
.
=
12
∂ x2
2 |T k | B11
k ∂ x̂2 − Bk ∂ x̂1

(20)

In the following, we present the necessary transformation for all blocks of system (4) and name the corresponding
MATLAB / GNU Octave routines that can be found in Sec. 4.
13

3.6.1. Assembly of M
Using the transformation rule (18a) we see that for the local mass matrix MT k as defined in (5) holds

MT k = 2|T k | M̂

with


Z  ϕ̂1 ϕ̂1
 .
 ..
M̂ ≔

T̂ 
ϕ̂N ϕ̂1


· · · ϕ̂1 ϕ̂N 

..  ,
..
.
. 

· · · ϕ̂N ϕ̂N

(21)

where M̂ ∈ RN×N is the representation of the local mass matrix on the reference triangle T̂ . With (5) we see that the
global mass matrix M can be expressed as a Kronecker product of a matrix containing the areas |T k | and the local
matrix M̂:




MT 1

|T 1 |







..
..
 ⊗ M̂ .
M = 
=
2


.
.






MT K
|T K |

In the corresponding assembly routine assembleGlobM , the sparse block-diagonal matrix is generated using the command spdiags with the list g.areaT (cf. Tab. 1).
3.6.2. Assembly of Hm
The transformation rules (18a) and (20) yield
21
H1T k = −B22
k [Ĥ]:,:,1 + Bk [Ĥ]:,:,2

and

11
H2T k = B12
k [Ĥ]:,:,1 − Bk [Ĥ]:,:,2

with

[Ĥ]:,:,m


Z  ∂ x̂m ϕ̂1 ϕ̂1

..

≔
.

T̂ 
∂ x̂m ϕ̂N ϕ̂1


· · · ∂ x̂m ϕ̂1 ϕ̂N 

..
..
 ∈ RN×N
.
.

· · · ∂ x̂m ϕ̂N ϕ̂N

(22)

for m ∈ {1, 2}. Similar to M, the global matrices Hm are assembled by Kronecker products in the routine assembleGlobH .
3.6.3. Assembly of Gm
Application of the product rule, (18a), and (20) give us
Z
Z
11
21
∂ x2 ϕki ϕk j = −B12
∂ x1 ϕki ϕk j = B22
[
Ĝ
]
−
B
[
Ĝ
]
,
i,
j,l,1
i,
j,l,2
k [Ĝ]i, j,l,1 + Bk [Ĝ]i, j,l,2 ,
k
k
Tk

Tk

with a multidimensional array Ĝ ∈ RN×N×N×2 representing the transformed integral on the reference triangle T̂ :
Z
∂ x̂m ϕ̂i ϕ̂ j ϕ̂l
(23)
[Ĝ]i, j,l,m ≔
T̂

for m ∈ {1, 2}. Now we can express the local matrix G1T k from (6) as
G1T k = −

N
X

=−

N
X

l=1

l=1




Z  ∂ x̂1 ϕ̂1 ϕ̂1 ϕ̂l

 22
..

Dkl (t)  Bk
.



T̂
∂ x̂1 ϕ̂N ϕ̂1 ϕ̂l



· · · ∂ x̂1 ϕ̂1 ϕ̂N ϕ̂l 
Z  ∂ x̂2 ϕ̂1 ϕ̂1 ϕ̂l


..
..
..
 − B21

.
k
.
.



T̂
∂ x̂2 ϕ̂N ϕ̂1 ϕ̂l
· · · ∂ x̂1 ϕ̂N ϕ̂N ϕ̂l



21
Dkl (t) B22
k [Ĝ]:,:,l,1 − Bk [Ĝ]:,:,l,2


· · · ∂ x̂2 ϕ̂1 ϕ̂N ϕ̂l 

..
..

.
.

· · · ∂ x̂2 ϕ̂N ϕ̂N ϕ̂l

m
and analogously G2T k . With Gm = diag(Gm
T 1 , . . . , GT K ) we can vectorize over all triangles using the Kronecker product
as done in the routine assembleGlobG .

14

3.6.4. Assembly of S
To ease the assembly of S we split the global matrix as given in (9) into a block-diagonal part and a remainder
such that S = Sdiag + Soffdiag holds.
We first consider the block-diagonal entries of S consisting of sums of local matrices SEkn , cf. (9a) and (7b),
respectively. Our first goal is to transform SEkn to a local matrix
Ŝdiag ∈ RN×N×3 that is independent of the physical
R
triangle T k . Therefore, we transform the edge integral term E ϕki ϕk j to the nth edge of the reference triangle Ên :
kn

Z

ϕki ϕk j =
Ekn

|Ekn |
|Ên |

Z

ϕ̂i ( x̂) ϕ̂ j ( x̂) d x̂ =
Ên

|Ekn |
|Ên |

Z

0

1

ϕ̂i ◦ γ̂n (s) ϕ̂ j ◦ γ̂n (s) |γ̂′n (s)| ds = |Ekn |

Z
|

0

1

ϕ̂i ◦ γ̂n (s) ϕ̂ j ◦ γ̂n (s) ds ,
{z
}
≕[Ŝdiag ]i, j,n

(24)

where we used transformation rule (18b) and |γ̂′n (s)| = |Ên |. The explicit forms of the mappings γ̂n : [0, 1] → Ên can
be easily derived:
"
#
"
#
" #
1−s
0
s
γ̂1 (s) ≔
,
γ̂2 (s) ≔
,
γ̂3 (s) ≔
.
(25)
s
1−s
0
Thus, we have SEkn = |Ekn |[Ŝdiag ]:,:,n allowing to define the diagonal blocks of the global matrix Sdiag using the Kronecker product:



δE1n ∈EΩ
3 
X


..
 ⊗ [Ŝdiag ]:,:,n ,
Sdiag ≔ η

.

n=1 
δEKn ∈EΩ

where δEkn ∈EΩ denotes the Kronecker delta.
Next, we consider the off-diagonal blocks of S stored in Soffdiag . For an edge Ek− n− = Ek+ n+ ∈ ∂T k− ∩∂T k+ , n− , n+ ∈
{1, 2, 3}, we obtain analogously:
Z

E k − n−

ϕk− i ϕk+ j

|Ek− n− |
=
|Ên− |
= |Ek− n− |

Z
Z

=I

Ên−
1

0

Z
z }| {
|Ek− n− |
− ( x̂) d x̂ =
ϕk− i ◦ Fk− ( x̂) ϕk+ j ◦ Fk+ ◦ F−1
ϕ̂i ( x̂) ϕ̂ j ◦ F−1
◦F
+
k
k+ ◦ Fk− ( x̂) d x̂
k
|Ên− | Ên−

ϕ̂i ◦ γ̂n− (s) ϕ̂ j ◦ F−1
k+ ◦ Fk− ◦ γ̂n− (s) ds .

F−1
k+

Note that
◦ Fk− maps from T̂ to T̂ . Since we compute a line integral the integration domain is further restricted to
an edge Ên− , n− ∈ {1, 2, 3} and its co-domain to an edge Ên+ , n+ ∈ {1, 2, 3}. As a result, this integration can be boiled
down to nine possible maps between the sides of the reference triangle expressed as ϑ̂n− n+ : Ên− → Ên+ . All 9 cases
are given by
" 1#
"
#
" 1#
" #
" 1#
" 1#
x̂
1 − x̂1
x̂
0
x̂
x̂
ϑ̂11 : 2 7→
,
ϑ̂
:
→
7
,
ϑ̂
:
→
7
,
12
13
x̂
1 − x̂2
x̂2
x̂2
x̂2
0
" 1#
"
#
" 1#
"
#
" 1#
" 2#
x̂
1 − x̂2
x̂
0
x̂
x̂
ϑ̂21 : 2 7→
,
ϑ̂
:
→
7
,
ϑ̂
:
→
7
,
(26)
22
23
x̂
x̂2
x̂2
1 − x̂2
x̂2
0
" 1#
" 1 #
" 1#
" #
" 1#
"
#
x̂
x̂
x̂
0
x̂
1 − x̂1
ϑ̂31 : 2 7→
,
ϑ̂
:
→
7
,
ϑ̂
:
→
7
.
32
33
x̂
1 − x̂1
x̂2
x̂1
x̂2
0
Here one has to note that all maps ϑ̂n− n+ reverse the edge orientation because the affine mappings Fk maintain
the counter-clockwise vertex orientation for each T k ∈ Th . We define Ŝoffdiag ∈ RN×N×3×3 by
Z 1
(27)
ϕ̂i ◦ γ̂n− (s) ϕ̂ j ◦ ϑ̂n− n+ ◦ γ̂n− (s) ds ,
[Ŝoffdiag ]i, j,n− ,n+ ≔
0

15

and thus we arrive at

S

offdiag


0


 δE − =E +
3 X
3 
X
 2n . 1n

..
≔ −η

−
+

n =1 n =1 
.
..


δEKn− =EKn+

δE1n− =E2n+
0
..
.
...

...
...
..
.
..
..
.
.
..
.
0
. . . δEKn− =E(K−1)n+







offdiag
]:,:,n− ,n+ .
 ⊗ [Ŝ


δE(K−1)n− =EKn+ 

0
δE1n− =EKn+
..
.
..
.

The sparsity structure for off-diagonal blocks depends on the numbering of mesh entities and is given for each combination of n− and n+ by the list markE0TE0T (cf. Tab. 1). The routine assembleGlobS assembles the matrices Sdiag and
Soffdiag directly into S with a code very similar to the formulation above.
3.6.5. Assembly of Qm
The assembly of Qm from equations (7a), (7c) is analogous to S since both are constructed from the same terms
only differing in constant coefficients. Consequently, we can choose the same approach as described in 3.6.4. Again,
we split the matrix into diagonal and off-diagonal blocks Qm = Qm,diag + Qm,offdiag and assemble each separately
exploiting transformation rule (18b). This allows to write the diagonal blocks as follows:

  m

 ν1n |E1n |

δE1n ∈EΩ
3 
X



1



m,diag
.
.


..
 ◦ 
..
 ⊗ [Ŝdiag ]:,:,n ,
Q
≔

2 n=1 
 

δEKn ∈EΩ
νmKn |E Kn |
where “◦” is the operator for the Hadamard product.
The off-diagonal blocks are assembled as before, using the mapping ϑ̂n− n+ from (26), leading to a similar representation as for Soffdiag ,

m,offdiag

Q


0


 δE − =E +
 2n 1n
3
3 
1 X X 
..

≔
.
2 n− =1 n+ =1 

..

.

δEKn− =EKn+

δE1n− =E2n+
0
..
.
...


...
...
δE1n− =EKn+ 

..
..

.
.


..
..
..

.
.
.


..
.
0
δE(K−1)n− =EKn+ 

. . . δEKn− =E(K−1)n+
0
 m

−
 ν1n− |E1n− | . . . νm

1n− |E 1n | 


..
..
offdiag
◦ 
]:,:,n− ,n+ .
 ⊗ [Ŝ
.
.
 m

m
νKn− |E Kn− | . . . νKn− |E Kn− |

Once again, we can use a code close to the mathematical formulation to assemble the matrices Qm,diag and Qm,offdiag .
This is realized in the routine assembleGlobQ . In the implementation the Hadamard product is replaced by a call to
the built-in function bsxfun which applies a certain element-by-element operation (here: @times) to arrays. Since all
columns in the second matrix of the product are the same this makes superfluous explicitly creating this matrix and
permits the use of a single list of all required values instead.
3.6.6. Assembly of Rm
Just as before we split the block matrices Rm from (8a), (8c) into diagonal and off-diagonal parts as Rm = Rm,diag +
m,offdiag
R
. Here, integrals consist of three basis functions due to the diffusion coefficient but still can be transformed in
the same way. In diagonal blocks, this takes the form
Z 1
Z
(28)
ϕ̂i ◦ γ̂n (s) ϕ̂l ◦ γ̂n (s) ϕ̂ j ◦ γ̂n (s) ds ,
ϕki ϕkl ϕk j = |Ekn |
Ekn
|0
{z
}
≕[R̂diag ]i, j,l,n

16

which can be used to define a common multidimensional array R̂diag ∈ RN×N×N×3 . This allows to rewrite the local
block matrix from (8b) as
REkn =

N
X
l=1

Dkl (t)|Ekn |[R̂diag ]:,:,l,n .

Consequently, the assembly of Rm,diag can be formulated as

Rm,diag


δE1n ∈EΩ
3 X
N 
X
1

≔

2


..

n=1 l=1

.
δEKn ∈EΩ

  m
 ν1n |E1n |D1l (t)
 
 ◦ 
 

..

.
νmKn |E Kn |DKl (t)




 ⊗ [R̂diag ]:,:,l,n .


The off-diagonal entries consist of integrals over triples of basis functions two of which belong to the adjacent
triangle T k+ ; thus making it necessary to apply the mapping ϑ̂n− n+ from (26). Once again, this can be written as
Z

E k − n−

ϕk− i ϕk+ l ϕk+ j = |Ek− n− |

Z

|0

1

ϕ̂i ◦ γ̂n− (s) ϕ̂l ◦ ϑ̂n− n+ ◦ γ̂n− (s) ϕ̂ j ◦ ϑ̂n− n+ ◦ γ̂n− (s) ds ,
{z
}

(29)

≕ [R̂offdiag ]i, j,l,n− ,n+

with a multidimensional array R̂offdiag ∈ RN×N×N×3×3 whose help allows us to denote the assembly of Rm,offdiag
(component-wise given in (8c)) by

Rm,offdiag



0
δE1n− =E2n+ · · ·
···
δE1n− =EKn+ 



..
..

 δE − =E +
.
0
.

2n
1n
3 X
3 X
N 
X



1
..
..
..
..
..


≔
.
.
.
.
.

2 n− =1 n+ =1 l=1 


..
..

.
0
δE(K−1)n− =EKn+ 
.


δEKn− =EKn+
···
· · · δEKn− =E(K−1)n+
0

 
 m
−
 D1l (t) · · · DKl (t)
 ν1n− |E1n− | · · · νm

1n− |E 1n | 

  .

..  ⊗ [R̂offdiag ] − + .
..
..
 ◦  ..
◦ 
:,:,l,n ,n
. 
.
.
 


m
m
−
−
D1l (t) · · · DKl (t)
νKn− |E Kn | · · · νKn− |E Kn |

The corresponding code is found in assembleGlobR . Again, we make use of the function bsxfun to carry out the
Hadamard product; it is used twice, first to apply the row vector as before and then to apply the column vector of the
diffusion coefficient.
3.6.7. Assembly of Rm
D
m,diag
(cf. (8a)), the corresponding
Since the entries of Rm
D in (11) are computed in precisely the same way as for R
assembly routine assembleGlobRD consists only of the first part of assembleGlobR which is responsible for the assembly of Rm,diag . It only differs in a factor and the list of edges, namely all edges in the set ED for which non-zero entries
(here given by markE0Tbdr) are generated.
3.6.8. Assembly of SD
diag
The same as for Rm
D holds for SD in (12) which, in fact, has the same entries in the diagonal blocks as S
(cf. (9a)). Consequently, the corresponding routine assembleGlobSD is again a subset of assembleGlobS .
3.6.9. Assembly of Qm
N
For the Neumann boundary edges in the set EN only contributions in the block Qm
N in (14) are generated. The
responsible routine assembleGlobQN is also equivalent to the assembly routine for Qm,diag .
17

3.6.10. Assembly of J Dm
The entries of J Dm in (10) are transformed using transformation rule (18b)
Z
Z
X
X


m
m |E kn |
m
ϕki ◦ Fk ( x̂) cD t, Fk ( x̂) d x̂
νkn
νkn
[J D ](k−1)N+i = −
ϕki cD (t) = −
|Ên | Ên
Ekn
Ekn ∈∂T k ∩ED
Ekn ∈∂T k ∩ED
Z 1
X


t,
F
◦
γ̂
(s)
ds .
ϕ̂
◦
γ̂
(s)
c
νm
|E
|
= −
k
n
i
n
D
kn
kn
0

Ekn ∈∂T k ∩ED

This integral is then approximated using a 1D quadrature rule (19) on the interval (0, 1)
[J Dm ](k−1)N+i ≈ −

X

Ekn ∈∂T k ∩ED

νm
kn |E kn |

R
X
r=1

ωr ϕ̂i ◦ γ̂n (qr ) cD (t, Fk ◦ γ̂n (qr )) ,

allowing to vectorize the computation over all triangles and resulting in the routine assembleGlobJD .
3.6.11. Assembly of KD
The computation of KD is done similarly to the assembly of J Dm . The component-wise integrals from (13) are,
once again, transformed to the interval [0, 1] using (18b):
Z
X Z 1
X
1
ϕ̂i ◦ γ̂n (s) cD (t, Fk ◦ γ̂n (s)) ds ,
ϕki cD (t) = η
[KD ](k−1)N+i = η
|Ekn | Ekn
0
E ∈∂T ∩E
E ∈∂T ∩E
kn

k

D

kn

k

D

effectively canceling out the edge length. Using a quadrature rule and vectorization, KD is assembled in the routine assembleGlobKD .
3.6.12. Assembly of KN
In the integral terms of KN an additional basis function from the diffusion coefficient appears. As before, the
integrals are transformed using transformation rules (18b), (25) and a 1D quadrature rule (19):
[KN ](k−1)N+i = −

X

N
X

Dkl (t)

X

Ekn ∈∂T k ∩EN

ϕki ϕkl gN (t) = −

Ekn

Ekn ∈∂T k ∩EN l=1

≈ −

Z

|Ekn |

N
X
l=1

Dkl (t)

X

|Ekn |

Ekn ∈∂T k ∩EN

R
X
r=1

N
X
l=1

Dkl (t)

Z

0

1

ϕ̂i ◦ γ̂n (s) ϕ̂l ◦ γ̂n (s) gN (t, Fk ◦ γ̂n (s)) ds

ωr ϕ̂i ◦ γ̂n (qr ) ϕ̂l ◦ γ̂n (qr ) gN (t, Fk ◦ γ̂n (qr )) .

Once again, using vectorization over all triangles the assembly routine is assembleGlobKN .
3.7. Linear solver
After assembling all blocks as described in the previous section and compiling system (15) for time step tn+1 ,
a linear system has to be solved to yield the solution Y n+1 . For that we employ MATLAB / GNU Octave’s mldivide.
3.8. Computational performance
As noted at the beginning of Sec. 3, we obey a few implementation conventions to improve the computational
performance of our code, including the paradigm to avoid re-computation of already existing values. First of all, this
boils down to reassembling only those linear system blocks that are time-dependent.
Secondly, these assembly routines involve repeated evaluations of the basis functions at the quadrature points of the
reference triangle. As stated in Sec. 3.3, we use quadrature rules of order 2p on triangles and of order 2p + 1 on edges
precomputing the values of basis functions in the quadrature points. This is done in the routine computeBasesOnQuad
for ϕ̂i ( q̂r ), ∇ϕ̂i ( q̂r ), ϕ̂i ◦ γ̂n (sr ), and ϕ̂i ◦ ϑ̂n− n+ ◦ γ̂n (sr ), with q̂r ∈ T̂ , sr ∈ [0, 1] given by a 2D or 1D quadrature
rule, respectively, for all required orders. The values are stored in global cell arrays gPhi2D, gGradPhi2D, gPhi1D, and
gThetaPhi1D, allowing to write in the assembly routines, e. g., gPhi2D{qOrd}(:, i) to obtain the values of the i-th
basis function on all quadrature points of a quadrature rule of order qOrd.
18

3.8.1. Estimated memory usage
Two resources limit the problem sizes that can be solved: computational time and available memory. The first one
is a ’soft’ limit—in contrast to exceeding the amount of available memory which will cause the computation to fail.
Hence, we will give an approximate estimate of the memory requirements of the presented code to allow gauging the
problem sizes and polynomial orders one can compute with the hardware at hand.
Grid data structures. The size of the grid data structures depends on the number of mesh entities. To give a rough
estimate, we will assume certain simplifications: Each triangle has three incident edges and each edge (disregarding
boundary edges) has two incident triangles, hence it holds 3 #T ≈ 2 #E. Additionally, the number of vertices is usually
less than the number of triangles, i. e., #V . #T . Using those assumptions, the memory requirement of the grid data
structures (cf. Tab. 1 plus additional lists not shown there) amounts to ≈ 89 #T · 8 Bytes.
Degrees of freedom. The memory requirements for system (15) largely depend on the sparsity structure of the matrix
which varies with the numbering of the mesh entities, the number of boundary edges, etc. In MATLAB / GNU Octave,
the memory requirements for a sparse matrix A ∈ Rn×n on a 64-bit machine can be approximated by 16·nnz(A)+8·n+8
Bytes, with nnz(A) being the number of non-zero entries in A. Coefficients, like Ck j , Dkl , Fkl , k ∈ {1, . . . , K}, j, l ∈
{1, . . . , N}, and the right-hand side entries J Dm , KD , KN , L are stored in full vectors, each of which requires KN ·8 Bytes.
The blocks of the system matrices A(t) and W can be divided into two groups: (i) blocks built from element-wise
integrations and (ii) blocks built from edge contributions. For the first case we showed in Sec. 2.4.3 that these have
a block diagonal structure due to the local support of the basis functions. Consequently, each contains K blocks of
size N × N with nonzero entries. Blocks from edge integrals also have block diagonal entries but additionally for each
of the three edges of an element two nonzero blocks exist. We neglect the blocks for boundary edges here (i. e., QN ,
RD , SD ), since these hold only entries for edges that are not contained in the interior edge blocks.
Before solving for the next time step, these blocks are assembled into the system matrices and the right-hand side
vector, effectively doubling the memory requirement. Combining these estimates, this sums to a memory requirement
of ≈ (27KN + 82KN 2 ) · 8 Bytes. Compared to (18(KN)2 + 9KN) · 8 Bytes alone for the assembled system, when using
full matrices, this is still a reasonable number and emphasizes once more the need for sparse data structures.
Total memory usage. Note that all these values are highly dependent on the shape of the mesh and additional overhead
introduced by MATLAB / GNU Octave (e. g., for GUI, interpreter, cell-data structures, temporary storage of built-in
routines, etc.). Other blocks, e. g., blocks on the reference element, like M̂, counters, helper variables, lookup tables
for the basis functions, etc., don’t scale with the mesh size and are left out in these estimates. Hence, the numbers
given here should be understood as a lower bound. Combining the partial results for the memory usage, we obtain
the total amount of ≈ (90 + 43N + 68N 2 )K · 8 Bytes. This means, computing with quadratic basis functions on a grid
of 10 000 triangles requires at least 214 MBytes of memory and should therefore be possible on any current machine.
However, computing with a grid of half a million elements and polynomials of order 4 requires more than 60 GBytes
of memory, requiring a well-equipped workstation.
3.8.2. Computation time
An extensive performance model for our implementation of the discontinuous Galerkin method exceeds the scope
of this publication. Instead we name the most time consuming parts of our implementation and give an insight which
computation times can be expected on current hardware for different problem sizes and approximation orders.
MATLAB’s profiler is a handy tool to investigate the runtime distribution within a program. We profiled a timedependent problem on a grid with 872 triangles and 100 time steps on an Intel Core i7-860 CPU (4 cores, 8 threads)
with 8 GBytes of RAM and MATLAB R2014a (8.3.0.532). For low and moderate polynomial orders (p = 0, 1, 2)
the largest time share (50 – 70 %) was spent in the routine assembleGlobR which assembles the contributions of the
edge integrals in term VI in (3b). Due to the involvement of the time dependent diffusion coefficient it has to be
executed in every timestep and most of its time is spent in the functions bsxfun(@times,...) (applies the Hadamard
product) and kron (performs the assembly). The second most expensive part is then the solver itself, for which we
employ mldivide. When going to higher polynomial orders (p ≥ 3) this part even becomes the most expensive one,
simply due to the larger number of degrees of freedom. In this cases the routine assembleGlobG also takes a share
worth mentioning (up to 15 %), which again assembles a time dependent block due to the diffusion coefficient. Any
19

other part takes up less than 5 % of the total computation time. Although our code is not parallelized, MATLAB’s
built-in routines (in particular, mldivide and bsxfun) make extensive use of multithreading. Hence, these results are
machine dependent and, especially on machines with more cores, the runtime distribution might be different.
For large enough a number of time steps one can disregard the execution time of the initial computations (generation of grid data, computation of basis function lookup tables and reference element blocks, etc.); then the total
runtime scales linearly with the number of time steps. Therefore, we investigate the runtime behavior of the stationary test case described in Sec. 3.9 and plot the computation times with respect to the grid size and number of basis
functions in Fig. 5. This shows that the computation time increases with the number of basis functions but primarily
depends on the mesh size. We also observe that doubling the number of elements increases the computation time by
more than a factor of two which is related to the fact that some steps of the algorithm (e. g., the linear solver) have a
complexity that does not depend linearly on the number of degrees of freedom.
Computation time for different polynomial orders

Computation time for different grid sizes
104

10
p=0
p=3

p=1
p=4

K = 144
K = 9216

K = 36
K = 2304
K = 147456

p=2

105

103

K = 576
K = 36864

104

102

time [s]

time [s]

6

101

103
102
101

100

100
10−1

10−1
102

103

104

1

105

3

6

10

15

N

K

Figure 5: Computation times for varying grid size (left) and approximation order (right). Polynomial orders p = 0, 1, 2, 3, 4 correspond to
N = 1, 3, 6, 10, 15 basis functions, respectively. Measured on 4× Intel Xeon E7-4870 CPUs (each 10 cores, 20 threads) with 500 GBytes RAM and
MATLAB R2014a (8.3.0.532).

3.9. Code verification
The code is verified by showing that the numerically estimated orders of convergences match the analytically
predicted ones for prescribed smooth solutions. Since the spatial discretization is more complex than the time discretization by far, we restrict ourselves to the stationary version of (2) in this section.
We choose the exact solution c ≔ cos(7x1 ) cos(7x2 ) and the diffusion coefficient d ≔ exp(x1 + x2 ) on the domain Ω ≔ (0, 1)2 with Neumann boundaries at y = 0, 1 and Dirichlet boundaries on the others. The data cD , gN , and f
are derived algebraically by inserting c and d into (2). We then compute the solution ch j for a sequence of increasingly
finer meshes with element sizes h j , where the coarsest grid Th0 covering Ω is an irregular grid, and each finer grid is
obtained by regular refinement of its predecessor. The discretization errors are computed according to Sec. 3.5 , and
Tab. 2 contains the results demonstrating the (minimum) order of convergence α in h estimated by
!,
!
kch j−1 − ckL2 (Ω)
h j−1
α ≔ ln
ln
.
kch j − ckL2 (Ω)
hj

20

p
j
0
1
2
3
4
5
6

kch − ck
2.37E–1
7.67E–2
8.94E–2
9.39E–2
9.48E–2
9.49E–2
9.49E–2

0
α
—
1.63
– 0.22
– 0.07
– 0.01
0.00
0.00

1
α

kch − ck
7.70E–2
2.46E–2
6.67E–3
1.71E–3
4.32E–4
1.08E–4
2.71E–5

2
α

kch − ck

—
1.65
1.88
1.96
1.99
2.00
2.00

1.45E–2
1.26E–3
1.11E–4
1.10E–5
1.22E–6
1.44E–7
1.75E–8

kch − ck

—
3.52
3.51
3.33
3.18
3.09
3.04

1.97E–3
1.53E–4
1.02E–5
6.42E–7
3.94E–8
2.43E–9
1.51E–10

3
α
—
3.69
3.90
3.99
4.03
4.02
4.01

kch − ck
4.18E–4
1.26E–5
3.78E–7
1.16E–8
3.59E–10
1.11E–11
3.64E–13

Table 2: Discretization errors measured in L2 (Ω) and estimated orders of convergences using the penalty η = 1. We have h j =
triangles in the jth refinement level.

1
3·2 j

4
α
—
5.05
5.06
5.03
5.01
5.01
4.94

and K = 36 · 4 j

3.10. Visualization
In order to get a deeper insight into the data associated with a grid Th or with a discrete variable from P p (Th ), we
provide the routines visualizeGrid and visualizeData , respectively (see Sec. 4 for documentation). Since our code
accepts arbitrary basis functions, in particular the modal basis functions of Sec. 2.4.1, we have to sample those at the
Lagrangian points on each triangle (i. e. the barycenter of T for P0 (T ), the vertices of T for P1 (T ) and the vertices and
edge barycenters for P2 (T )). This mapping from the DG to the Lagrangian basis is realized in projectDG2Lagrange .
The representation of a discrete quantity in the latter basis which is as the DG representation a K × N matrix, is then
used to write a VTK-file [22]. These can be visualized and post-processed, e. g., by Paraview [23]. Unfortunately, the
current version 4.0.1 does not visualize quadratic functions as such but instead uses piecewise linear approximations
consisting of four pieces per element.
As an example, the following code generates a grid with two triangles and visualizes it using visualizeGrid ;
then a quadratic, discontinuous function is projected into the DG space for p ∈ {0, 1, 2} and written to VTK-files
using visualizeData . The resulting outputs are shown in Fig. 6 and Fig. 7.
g = g e n e r a t e G r i d D a t a([0 , -1; sqrt (3) ,0; 0 ,1; - sqrt (3) ,0] , [4 ,1 ,3; 1 ,2 ,3]);
g . idE = ( abs ( g . nuE (: ,2)) >0).*(( g . nuE (: ,1) >0)+(g . nuE (: ,2) >0)*2+1);
v i s u a l i z e G r i d( g )
fAlg = @ ( X1 , X2 ) ( X1 <0).*( X1 .^2 - X2 .^2 -1) + ( X1 >=0).*( - X1 .^2 - X2 .^2+1);
for N = [1 , 3 , 6]
p = ( sqrt (8* N +1) -3)/2; quadOrd = max (2*p , 1); c o m p u t e B a s e s O n Q u a d( N );
fDG
= p r o j e c t A l g 2 D G(g , fAlg , quadOrd , c o m p u t e H a t M( N ));
fLagr = p r o j e c t D G 2 L a g r a n g e( fDG );
v i s u a l i z e D a t a(g , fLagr , ' funname ' , [ ' fDOF ' , int2str ( N )] , 1)
end % for

3
3

3

3

4

5
2
4

1

3
1
1

1 2 2

3
4
1

2

2

2

3
1
2

1

2

1

√
√
T
T
Figure 6: Visualization of a grid with vertices v11 = [− 3, 0] , v12 = v21 = [0, −1]T , v13 = v23 = [0, 1]T , v22 = [ 3, 0] (cf. Sec. 3.10) with
triangle numbers, global and local vertex numbers, global and local edge numbers, and (scaled) edge normals. Global numbers are printed on, local
numbers next to the respective mesh entity. The boundary ID’s which are used to associate parts of the boundary with specific boundary conditions,
are printed on the exterior of each boundary edge.

21

range: [−2/3, 1/3]

range: [−8/5, 6/5]

range: [−2, 2]

codefDOF1.1.vtu

fDOF3.1.vtu

fDOF6.1.vtu

Figure 7: Visualization of a discontinuous function, represented with constant, linear, and quadratic basis functions (left to right) using Paraview (cf. Sec. 3.10). The underlying mesh is drawn in black.

4. Register of Routines
We list here all routines of our implementation in alphabetic order. For the reason of compactness, we waive
the check for correct arguments, e. g., by means of routines as assert. However, it is strongly recommended to
catch exceptions if the code is to be extended. The argument g is always a struct representing the triangulation Th
(cf. Sec. 3.1), the argument N is always the number of local basis functions N. A script that demonstrates the application of the presented routines is given in main.m.
globG = assembleGlobG(g, hatG, dDG) assembles the matrices Gm , m ∈ {1, 2} according to Sec. 3.6.3 which are returned in the 2 × 1
cell variable globG. The input argument hatG stores the local matrices Ĝ (multidimensional array) as defined in (23) and can be computed
by computeHatG . The coefficients of the projection of the algebraic diffusion coefficient d into the broken polynomial space are stored in the
input argument dDG as explained in Sec. 3.4 and can be computed by projectAlg2DG .
function globG = a s s e m b l e G l o b G(g , hatG , dDG )
[K , N ] = size ( dDG );
globG = cell (2 , 1);
globG {1} = sparse ( K *N , K * N ); globG {2} = sparse ( K *N , K * N );
for l = 1 : N
globG {1} = globG {1} + kron ( spdiags ( - dDG (: , l ) .* g . B (: ,2 ,2) ,
- kron ( spdiags ( - dDG (: , l ) .* g . B (: ,2 ,1) ,
globG {2} = globG {2} - kron ( spdiags ( - dDG (: , l ) .* g . B (: ,1 ,2) ,
+ kron ( spdiags ( - dDG (: , l ) .* g . B (: ,1 ,1) ,
end % for
end % f u n c t i o n

0 ,K , K ) ,
0 ,K , K ) ,
0 ,K , K ) ,
0 ,K , K ) ,

hatG (: ,: ,l ,1)) ...
hatG (: ,: ,l ,2));
hatG (: ,: ,l ,1)) ...
hatG (: ,: ,l ,2));

globH = assembleGlobH(g, hatH) assembles the matrices Hm , m ∈ {1, 2} according to Sec. 3.6.2 which are returned in the 2×1 cell variable globH. The input argument hatH stores the local matrices Ĥ as defined in (22) and can be computed by computeHatH.
function globH = a s s e m b l e G l o b H(g , hatH )
K = g . numT ; N = size ( hatH , 1);
globH = cell (2 , 1);
globH {1} = sparse ( K *N , K * N ); globH {2} = sparse ( K *N , K * N );
globH {1} = - kron ( spdiags ( g . B (: ,2 ,2) , 0 ,K , K ) , hatH (: ,: ,1)) ...
+ kron ( spdiags ( g . B (: ,2 ,1) , 0 ,K , K ) , hatH (: ,: ,2));
globH {2} =
kron ( spdiags ( g . B (: ,1 ,2) , 0 ,K , K ) , hatH (: ,: ,1)) ...
- kron ( spdiags ( g . B (: ,1 ,1) , 0 ,K , K ) , hatH (: ,: ,2));
end % f u n c t i o n

globJD = assembleGlobJD(g, markE0Tbdr, cDAlg, N) assembles the contributions of Dirichlet boundaries J Dm , m ∈ {1, 2} to the
right-hand side of Equation (3a) according to Sec. 3.6.10 which are returned in the 2×1 cell variable globJD. The input argument markE0Tbdr
is a K × 3 logical array marking for each triangle the local edges on the Dirichlet boundary. Assuming the Dirichlet boundary has the ID 1 then
it can be computed as markE0Tbdr = g.idE0T==1. cDAlg is a function_handle for the algebraic representation of cD (cf. main.m).
Here, Q2X1 and Q2X2 carry out mappings Fk from the reference triangle T̂ to physical triangles T k for all k ∈ {1, . . . , K} at once. This allows to
evaluate cD (t) at all quadrature points in the physical domain at the same time and, thus, vectorize the quadrature with respect to the triangle index.
function globJD = a s s e m b l e G l o b J D(g , markE0Tbdr , cDAlg , N )

22

global gPhi1D
K = g . numT ; p = ( sqrt (8* N +1) -3)/2;
qOrd = 2* p +1; [Q , W ] = q u a d R u l e 1 D( qOrd );
Q2X1 = @ ( X1 , X2 ) g . B (: ,1 ,1)* X1 + g . B (: ,1 ,2)* X2 + g . coordV0T (: ,1 ,1)* ones ( size ( X1 ));
Q2X2 = @ ( X1 , X2 ) g . B (: ,2 ,1)* X1 + g . B (: ,2 ,2)* X2 + g . coordV0T (: ,1 ,2)* ones ( size ( X1 ));
globJD = cell (2 , 1);
globJD {1} = zeros ( K , N ); globJD {2} = zeros (K , N );
for n = 1 : 3
[ Q1 , Q2 ] = gammaMap (n , Q );
cDn = cDAlg ( Q2X1 ( Q1 , Q2 ) , Q2X2 ( Q1 , Q2 ));
Jkn = - m a r k E 0 T b d r(: , n ) .* g . sigE0T (: , n ) .* g . areaE0T (: , n );
for i = 1 : N
integral = cDn * ( W .* gPhi1D { qOrd }(: , i , n ) ' ) ' ;
globJD {1}(: ,i ) = globJD {1}(: ,i ) + Jkn .* g . nuE0T (: ,n ,1) .* integral ;
globJD {2}(: ,i ) = globJD {2}(: ,i ) + Jkn .* g . nuE0T (: ,n ,2) .* integral ;
end % for
end % for
globJD {1} = reshape ( globJD {1} ' ,K *N ,1); globJD {2} = reshape ( globJD {2} ' ,K *N ,1);
end % f u n c t i o n

globKD = assembleGlobKD(g, markE0Tbdr, cDAlg, eta, N) assembles the contributions of Dirichlet boundaries KD to the righthand side of Equation (3b) according to Sec. 3.6.11 which are returned in variable globKD. Input arguments markE0Tbdr and cDAlg are as
described in assembleGlobJD . The penalty coefficient η is given in eta.
function globKD = a s s e m b l e G l o b K D(g , markE0Tbdr , cDAlg , eta , N )
global gPhi1D
p = ( sqrt (8* N +1) -3)/2;
qOrd = 2* p +1; [Q , W ] = q u a d R u l e 1 D( qOrd );
Q2X1
= @ ( X1 , X2 ) g . B (: ,1 ,1)* X1 + g . B (: ,1 ,2)* X2 + g . coordV0T (: ,1 ,1)* ones ( size ( X1 ));
Q2X2
= @ ( X1 , X2 ) g . B (: ,2 ,1)* X1 + g . B (: ,2 ,2)* X2 + g . coordV0T (: ,1 ,2)* ones ( size ( X1 ));
globKD = zeros ( g . numT , N );
for n = 1 : 3
[ Q1 , Q2 ] = gammaMap (n , Q );
cDn = cDAlg ( Q2X1 ( Q1 , Q2 ) , Q2X2 ( Q1 , Q2 ));
for i = 1 : N
globKD (: , i ) = globKD (: , i ) + eta * m a r k E 0 T b d r(: , n ) .* ( cDn * (W ' .* gPhi1D { qOrd }(: , i , n )) );
end % for
end % for
globKD = reshape ( globKD ' , g . numT *N , 1);
end % f u n c t i o n

globKN = assembleGlobKN(g, markE0Tbdr, dDG, gNAlg) assembles the contributions of Neumann boundaries KN to the righthand side of Equation (3b) according to Sec. 3.6.12 which are returned in variable globKN. The input argument markE0Tbdr is described
in assembleGlobJD , dDG is the representation of the diffusion coefficient in the polynomial space (cf. projectAlg2DG ), and gNAlg is
a function_handle for the algebraic representation of gN (cf. main.m).
function globKN = a s s e m b l e G l o b K N(g , markE0Tbdr , dDG , gNAlg )
global gPhi1D
[K , N ] = size ( dDG ); p = ( sqrt (8* N +1) -3)/2;
qOrd = 2* p +1; [Q , W ] = q u a d R u l e 1 D( qOrd );
Q2X1 = @ ( X1 , X2 ) g . B (: ,1 ,1)* X1 + g . B (: ,1 ,2)* X2 + g . coordV0T (: ,1 ,1)* ones ( size ( X1 ));
Q2X2 = @ ( X1 , X2 ) g . B (: ,2 ,1)* X1 + g . B (: ,2 ,2)* X2 + g . coordV0T (: ,1 ,2)* ones ( size ( X1 ));
globKN = zeros (K , N );
for n = 1 : 3
[ Q1 , Q2 ] = gammaMap (n , Q );
gNAlgn = gNAlg ( Q2X1 ( Q1 , Q2 ) , Q2X2 ( Q1 , Q2 ));
Kkn = - m a r k E 0 T b d r(: , n ) .* g . sigE0T (: , n ) .* g . areaE0T (: , n );
for i = 1 : N
for l = 1 : N
integral = gNAlgn * ( W .* gPhi1D { qOrd }(: , i , n ) ' .* gPhi1D { qOrd }(: , l , n ) ' ) ' ;
globKN (: , i ) = globKN (: , i ) + Kkn .* dDG (: , l ) .* integral ;
end % for
end % for
end % for
globKN = reshape ( globKN ' , K *N ,1);
end % f u n c t i o n

globM = assembleGlobM(g, hatM) assembles the global mass matrix M according to Sec. 3.6.1 which is returned in variable globM. The
input argument hatM stores the local matrices M̂ as defined in (21) and can be computed by computeHatM .
function globM = a s s e m b l e G l o b M(g , hatM )
K = g . numT ;
globM = 2* kron ( spdiags ( g . areaT , 0 , K , K ) , hatM );
end % f u n c t i o n

23

globQ = assembleGlobQ(g, markE0Tint, hatQdiag, hatQoffdiag) assembles the matrices Qm , m ∈ {1, 2} according to
Sec. 3.6.5 which are returned in the 2 × 1 cell variable globQ. The input arguments hatQdiag and hatQoffdiag store the local matrices Q̂m,diag = Ŝdiag and Q̂m,offdiag = Ŝoffdiag as given in (24) and (27), respectively. They can be computed by computeHatSdiag
and computeHatSoffdiag . Similarly to assembleGlobJD , the argument markE0Tint is a K × 3 logical array that marks each triangle’s interior edges. Note the use of bsxfun(@times,...) to carry out the Hadamard product without building the full coefficient matrix.
function globQ = a s s e m b l e G l o b Q(g , markE0Tint , hatQdiag , h a t Q o f f d i a g)
K = g . numT ; N = size ( hatQdiag , 1);
globQ = cell (2 , 1);
globQ {1} = sparse ( K *N , K * N ); globQ {2} = sparse ( K *N , K * N );
for n = 1 : 3
Qkn = 0.5 * m a r k E 0 T i n t(: , n ) .* g . areaE0T (: , n ) .* g . sigE0T (: , n );
globQ {1} = globQ {1} + kron ( spdiags ( Qkn .* g . nuE0T (: ,n ,1) , 0 ,K , K ) , hatQdiag (: ,: , n ));
globQ {2} = globQ {2} + kron ( spdiags ( Qkn .* g . nuE0T (: ,n ,2) , 0 ,K , K ) , hatQdiag (: ,: , n ));
end % for
for nn = 1 : 3
Qkn = 0.5 * g . areaE0T (: , nn ) .* g . sigE0T (: , nn );
for np = 1 : 3
m a r k T i m e s Q k n = bsxfun ( @times , g . m a r k E 0 T E 0 T{ nn , np } , Qkn .* g . nuE0T (: , nn ,1));
globQ {1} = globQ {1} + kron ( markTimesQkn , h a t Q o f f d i a g(: ,: , nn , np ));
m a r k T i m e s Q k n = bsxfun ( @times , g . m a r k E 0 T E 0 T{ nn , np } , Qkn .* g . nuE0T (: , nn ,2));
globQ {2} = globQ {2} + kron ( markTimesQkn , h a t Q o f f d i a g(: ,: , nn , np ));
end % for
end % for
end % f u n c t i o n

globQN = assembleGlobQN(g, markE0Tbdr, hatQdiag) assembles the matrices QmN , m ∈ {1, 2} according to Sec. 3.6.9. It is essentially the same routine as the diagonal part of assembleGlobQ , only with markE0Tbdr marking the Neumann boundary edges instead of
interior edges.
function globQN = a s s e m b l e G l o b Q N(g , markE0Tbdr , hatQdiag )
K = g . numT ; N = size ( hatQdiag , 1);
globQN = cell (2 , 1);
globQN {1} = sparse ( K *N , K * N ); globQN {2} = sparse ( K *N , K * N );
for n = 1 : 3
QNkn = m a r k E 0 T b d r(: , n ) .* g . areaE0T (: , n ) .* g . sigE0T (: , n );
globQN {1} = globQN {1} + kron ( spdiags ( QNkn .* g . nuE0T (: ,n ,1) , 0 ,K , K ) , hatQdiag (: ,: , n ));
globQN {2} = globQN {2} + kron ( spdiags ( QNkn .* g . nuE0T (: ,n ,2) , 0 ,K , K ) , hatQdiag (: ,: , n ));
end % for
end % f u n c t i o n

globR = assembleGlobR(g, markE0Tint, hatRdiag, hatRoffdiag, dDG) assembles the matrices Rm , m ∈ {1, 2} according to
Sec. 3.6.6 which are returned in the 2 × 1 cell variable globR. The input arguments hatRdiag and hatRoffdiag store the local matrices R̂m,diag and R̂m,offdiag as defined in (28) and (29), respectively. They can be computed by computeHatRdiag and computeHatRoffdiag .
Each triangle’s interior edges are marked in markE0Tint as in assembleGlobQ . A representation of the diffusion coefficient in the polynomial
space is stored in dDG and can be computed by projectAlg2DG . The Hadamard product is carried out by bsxfun(@times,...). Note the
transposed application of Dkl (t) in the second part of the routine as a result of the diffusion coefficient being taken from the neighboring triangle T k+ .
function globR = a s s e m b l e G l o b R(g , markE0Tint , hatRdiag , hatRoffdiag , dDG )
[K , N ] = size ( dDG ); globR = cell (2 , 1);
globR {1} = sparse ( K *N , K * N ); globR {2} = sparse ( K *N , K * N );
for n = 1 : 3
Rkn = 0.5 * m a r k E 0 T i n t(: , n ) .* g . areaE0T (: , n ) .* g . sigE0T (: , n );
for l = 1 : N
globR {1} = globR {1} + kron ( spdiags ( Rkn .* g . nuE0T (: ,n ,1).* dDG (: , l ) ,0 ,K , K ) , hatRdiag (: ,: ,l , n ));
globR {2} = globR {2} + kron ( spdiags ( Rkn .* g . nuE0T (: ,n ,2).* dDG (: , l ) ,0 ,K , K ) , hatRdiag (: ,: ,l , n ));
end % for
end % for
for nn = 1 : 3
Rkn = 0.5 * g . areaE0T (: , nn ) .* g . sigE0T (: , nn );
for np = 1 : 3
m a r k E 0 T E 0 T t i m e s R k n 1 = bsxfun ( @times , g . m a r k E 0 T E 0 T{ nn , np } , Rkn .* g . nuE0T (: , nn ,1));
m a r k E 0 T E 0 T t i m e s R k n 2 = bsxfun ( @times , g . m a r k E 0 T E 0 T{ nn , np } , Rkn .* g . nuE0T (: , nn ,2));
for l = 1 : N
m a r k E 0 T E 0 T t i m e s D t i m e s R k n = bsxfun ( @times , m a r k E 0 T E 0 T t im esR kn 1 , dDG (: , l ). ' );
globR {1} = globR {1} + kron ( m a r k E 0 T E 0 T t i m e s Dt im es R kn , h a t R o f f d i a g(: ,: ,l , nn , np ));
m a r k E 0 T E 0 T t i m e s D t i m e s R k n = bsxfun ( @times , m a r k E 0 T E 0 T t im esR kn 2 , dDG (: , l ). ' );
globR {2} = globR {2} + kron ( m a r k E 0 T E 0 T t i m e s Dt im es R kn , h a t R o f f d i a g(: ,: ,l , nn , np ));
end % for
end % for
end % for
end % f u n c t i o n

24

globRD = assembleGlobRD(g, markE0Tbdr, hatRdiag, dDG) assembles the matrices RmD , m ∈ {1, 2} according to Sec. 3.6.7. It is
essentially the same routine as the first part of assembleGlobR but carried out only for the Dirichlet boundary edges marked by markE0Tbdr.
function globRD = a s s e m b l e G l o b R D(g , markE0Tbdr , hatRdiag , dDG )
[K , N ] = size ( dDG ); globRD = cell (2 , 1);
globRD {1} = sparse ( K *N , K * N ); globRD {2} = sparse ( K *N , K * N );
for n = 1 : 3
RDkn = m a r k E 0 T b d r(: , n ) .* g . areaE0T (: , n ) .* g . sigE0T (: , n );
for l = 1 : N
globRD {1} = globRD {1} + kron ( spdiags ( RDkn .* g . nuE0T (: ,n ,1).* dDG (: , l ) ,0 ,K , K ) , hatRdiag (: ,: ,l , n ));
globRD {2} = globRD {2} + kron ( spdiags ( RDkn .* g . nuE0T (: ,n ,2).* dDG (: , l ) ,0 ,K , K ) , hatRdiag (: ,: ,l , n ));
end
end % for
end % f u n c t i o n

globS = assembleGlobS(g, markE0Tint, hatSdiag, hatSoffdiag, eta) assembles the matrix S according to Sec. 3.6.4
which is returned in the variable globS. The arguments are the same as for assembleGlobQ plus the penalty coefficient η given in eta.
function globS = a s s e m b l e G l o b S(g , markE0Tint , hatSdiag , hatSoffdiag , eta )
K = g . numT ; N = size ( hatSdiag , 1);
globS = sparse ( K *N , K * N );
for n = 1 : 3
globS = globS + eta * kron ( spdiags ( m a r k E 0 T i n t(: , n ) ,0 ,K , K ) , hatSdiag (: ,: ,n ));
end % for
for nn = 1 : 3
for np = 1 : 3
globS = globS - eta * kron ( g . m a r k E 0 T E 0 T{ nn , np } , h a t S o f f d i a g(: ,: ,nn , np ));
end % for
end % for
end % f u n c t i o n

globSD = assembleGlobSD(g, markE0Tbdr, hatSdiag, eta) assembles the matrix SD according to Sec. 3.6.8. It is similar to the
first part of assembleGlobS but carried out for Dirichlet boundary edges, which are marked in markE0Tbdr .
function globSD = a s s e m b l e G l o b S D(g , markE0Tbdr , hatSdiag , eta )
K = g . numT ; N = size ( hatSdiag , 1);
globSD = sparse ( K *N , K * N );
for n = 1 : 3
globSD = globSD + eta * kron ( spdiags ( m a r k E 0 T b d r(: , n ) ,0 ,K , K ) , hatSdiag (: ,: ,n ));
end % for
end % f u n c t i o n

computeBasesOnQuad(N) evaluates the basis functions and their gradients in the quadrature points on the reference triangle T̂ according to
Sec. 3.8 for all required orders as described in Sec. 3.3. It computes the values of ϕ̂i ( x̂), ∇ϕ̂i ( x̂), ϕ̂i ◦ γ̂(s), and ϕ̂i ◦ ϑ̂n− n+ ◦ γ̂(s) and stores them
in global arrays gPhi2D, gGradPhi2D , gPhi1D, and gThetaPhi1D, respectively, which are 2p + 1 × 1 cell variables.
function c o m p u t e B a s e s O n Q u a d( N )
global gPhi2D g G r a d P h i 2 D gPhi1D g T h e t a P h i 1 D
p = ( sqrt (8* N +1) -3)/2;
if p > 0 , r e q u i r e d O r d e r s = [2*p , 2* p +1]; else r e q u i r e d O r d e r s = 1; end
gPhi2D = cell ( max ( r e q u i r e d O r d e r s ) ,1); g G r a d P h i 2 D = cell ( max ( r e q u i r e d O r d e r s ) ,1);
gPhi1D = cell ( max ( r e q u i r e d O r d e r s ) ,1); g T h e t a P h i 1 D = cell ( max ( r e q u i r e d O r d e r s ) ,1);
for it = 1 : length ( r e q u i r e d O r d e r s)
ord = r e q u i r e d O r d e r s( it );
[ Q1 , Q2 , ~] = q u a d R u l e 2 D( ord );
gPhi2D { ord }
= zeros ( length ( Q1 ) , N );
for i = 1 : N
gPhi2D { ord }(: , i ) = phi (i , Q1 , Q2 );
end % for
g G r a d P h i 2 D{ ord } = zeros ( length ( Q1 ) , N , 2);
for m = 1 : 2
for i = 1 : N
g G r a d P h i 2 D{ ord }(: , i , m ) = gradPhi (i , m , Q1 , Q2 );
end % for
end % for
[Q , ~] = q u a d R u l e 1 D( ord );
gPhi1D { ord } = zeros ( length ( Q ) , N , 3);
for nn = 1 : 3
[ Q1 , Q2 ] = gammaMap ( nn , Q );
for i = 1 : N
gPhi1D { ord }(: , i , nn ) = phi (i , Q1 , Q2 );
end
for np = 1 : 3
[ QP1 , QP2 ] = theta ( nn , np , Q1 , Q2 );

25

for i = 1 : N
g T h e t a P h i 1 D{ ord }(: , i , nn , np ) = phi (i , QP1 , QP2 );
end % for
end % for
end % for
end % for
end % f u n c t i o n

hatG = computeHatG(N) computes the local matrix Ĝ (multidimensional array) on the reference triangle T̂ as given in (23) using quadrature.
function hatG = c o m p u t e H a t G( N )
global gPhi2D g G r a d P h i 2 D
hatG = zeros (N , N , N , 2); % [ N x N x N x 2]
if N > 1 % p > 0
p = ( sqrt (8* N +1) -3)/2; qOrd = max (2* p , 1); [~ ,~ ,W ] = q u a d R u l e 2 D( qOrd );
for i = 1 : N
for j = 1 : N
for l = 1 : N
for m = 1 : 2
hatG (i ,j ,l , m ) = sum ( W ' .* g G r a d P h i 2 D{ qOrd }(: , i , m ) .* gPhi2D { qOrd }(: , j ) .* gPhi2D { qOrd }(: , l ) );
end % for
end % for
end % for
end % for
end % f u n c t i o n

hatH = computeHatH(N) computes the local matrix Ĥ (multidimensional array) on the reference triangle T̂ as given in (22) using quadrature.
function hatH = c o m p u t e H a t H( N )
global gPhi2D g G r a d P h i 2 D
hatH = zeros (N , N , 2); % [ N x N x 2]
if N > 1 % p > 0
p = ( sqrt (8* N +1) -3)/2; qOrd = max (2* p , 1); [~ ,~ ,W ] = q u a d R u l e 2 D( qOrd );
for i = 1 : N
for j = 1 : N
for m = 1 : 2
hatH (i , j , m ) = sum ( W ' .* g G r a d P h i 2 D{ qOrd }(: , i , m ) .* gPhi2D { qOrd }(: , j ) );
end % for
end % for
end % for
end % f u n c t i o n

hatM = computeHatM(N) computes the local mass matrix M̂ on the reference triangle T̂ as given in (21) using quadrature.
function hatM = c o m p u t e H a t M( N )
global gPhi2D
p = ( sqrt (8* N +1) -3)/2; qOrd = max (2*p , 1); [~ ,~ ,W ] = q u a d R u l e 2 D( qOrd );
hatM = zeros ( N ); % [ N x N ]
for i = 1 : N
for j = 1 : N
hatM (i , j ) = sum ( W ' .* gPhi2D { qOrd }(: , i ) .* gPhi2D { qOrd }(: , j ) );
end % for
end % for
end % f u n c t i o n

hatRdiag = computeHatRdiag(N) computes the local matrix R̂diag (multidimensional array) on the reference triangle T̂ as given in (28)
using quadrature.
function hatRdiag = c o m p u t e H a t R d i a g( N )
global gPhi1D
p = ( sqrt (8* N +1) -3)/2; qOrd = max (2* p +1 ,1); [~ , W ] = q u a d R u l e 1 D( qOrd );
hatRdiag = zeros (N , N , N , 3); % [ N x N x N x 3]
for n = 1 : 3 % 3 edges
for l = 1 : N % N b a s i s f c t s for D ( t )
for i = 1 : N
for j = 1 : N
hatRdiag (i ,j ,l , n ) = sum (W ' .* gPhi1D { qOrd }(: , i , n ) .* gPhi1D { qOrd }(: , l , n ) .* gPhi1D { qOrd }(: , j , n ));
end % for
end % for
end % for
end % for
end % f u n c t i o n

26

hatRoffdiag = computeHatRoffdiag(N) computes the local matrix R̂offdiag (multidimensional array) on the reference triangle T̂ as given
in (29) using quadrature.
function h a t R o f f d i a g = c o m p u t e H a t R o f f d i a g( N )
global gPhi1D g T h e t a P h i 1 D
p = ( sqrt (8* N +1) -3)/2; qOrd = 2* p +1; [~ , W ] = q u a d R u l e 1 D( qOrd );
h a t R o f f d i a g = zeros (N ,N ,N ,3 ,3); % [ N x N x N x 3 x 3]
for nn = 1 : 3 % 3 edges
for np = 1 : 3
for l = 1 : N
for i = 1 : N
for j = 1 : N
h a t R o f f d i a g(i , j , l , nn , np ) = sum ( W ' .* gPhi1D { qOrd }(: , i , nn ) .* ...
g T h e t a P h i 1 D{ qOrd }(: , l , nn , np ) .* g T h e t a P h i 1 D{ qOrd }(: , j , nn , np ) );
end % for
end % for
end % for
end % for
end % for
end

hatSdiag = computeHatSdiag(N) computes the local matrix Ŝdiag (multidimensional array) on the reference triangle T̂ as given in (24)
using quadrature.
function hatSdiag = c o m p u t e H a t S d i a g( N )
global gPhi1D
p = ( sqrt (8* N +1) -3)/2; qOrd = 2* p +1; [~ , W ] = q u a d R u l e 1 D( qOrd );
hatSdiag = zeros (N , N , 3); % [ N x N x 3]
for n = 1 : 3 % 3 edges
for i = 1 : N
for j = 1 : N
hatSdiag (i , j , n ) = sum ( W ' .* gPhi1D { qOrd }(: , i , n ) .* gPhi1D { qOrd }(: , j , n ) );
end % for
end % for
end % for
end % f u n c t i o n

hatSoffdiag = computeHatSoffdiag(N) computes the local matrix Ŝoffdiag (multidimensional array) on the reference triangle T̂ as given
in (27) using quadrature.
function h a t S o f f d i a g = c o m p u t e H a t S o f f d i a g( N )
global gPhi1D g T h e t a P h i 1 D
p = ( sqrt (8* N +1) -3)/2; qOrd = 2* p +1; [~ , W ] = q u a d R u l e 1 D( qOrd );
h a t S o f f d i a g = zeros (N , N , 3 , 3); % [ N x N x 3 x 3]
for nn = 1 : 3
for np = 1 : 3
for i = 1 : N
for j = 1 : N
h a t S o f f d i a g(i , j , nn , np ) = sum ( W ' .* gPhi1D { qOrd }(: , i , nn ) .* g T h e t a P h i 1 D{ qOrd }(: , j , nn , np ) );
end % for
end % for
end % for
end % for
end % f u n c t i o n

err = computeL2Error(g, cDG, cAlg, qOrd) computes the L2 -error according to Sec. 3.5 using a quadrature of order qOrd, where
g is a struct generated by generateGridData , cDG is the K × N representation matrix of the approximate solution, cAlg is the algebraic
formulation of the exact solution (see example below).
Example: N = 6; h = 1/2; qOrd = 5;

g = domainSquare(h); computeBasesOnQuad(N); hatM = computeHatM(N);
cAlg = @(X1, X2) sin(X1).*sin(X2); cDG = projectAlg2DG(g,cAlg,N,qOrd,hatM);
computeL2Error(g, cDG, cAlg, qOrd)
function err = c o m p u t e L 2 E r r o r(g , cDG , cAlg , qOrd )
global gPhi2D
N = size ( cDG , 2); qOrd = max ( qOrd ,1);
[ Q1 , Q2 , W ] = q u a d R u l e 2 D( qOrd );
R = length ( W );
X1 = kron ( g . B (: ,1 ,1) , Q1 )+ kron ( g . B (: ,1 ,2) , Q2 )+ kron ( g . coordV0T (: ,1 ,1) , ones (1 , R ));
X2 = kron ( g . B (: ,2 ,1) , Q1 )+ kron ( g . B (: ,2 ,2) , Q2 )+ kron ( g . coordV0T (: ,1 ,2) , ones (1 , R ));
c E x O n Q u a d P t s = cAlg ( X1 , X2 ); % [ K x R ]
c A p p r x O n Q u a d P t s = cDG * gPhi2D { qOrd } ' ; % [ K x R ] = [ K x N ] * [ N x R ]
err = sqrt (2* dot (( c A p p r x O n Q u a d P t s - c E x O n Q u a d P t s ).^2 * W . ' , g . areaT ));

27

end % f u n c t i o n

g = domainCircle(h) This method uses the grid generator Gmsh [19].
A system call to Gmsh generates the ASCII
file domainCircle.mesh based on geometry information of the domain Ω stored in domainCircle.geo. The basic grid data is extracted
from domainCircle.mesh to call the routine generateGridData (cf. Sec. 3.1) and to set the boundary IDs (from 1 to 4).
function g = d o m a i n C i r c l e( h )
% % g e n e r a t i o n of d o m a i n C i r c l e. mesh using d o m a i n C i r c l e. geo
cmd = sprintf ( ' gmsh -2 - format mesh - clscale % f -o " d o m a i n C i r c l e. mesh " " d o m a i n C i r c l e. geo " ' , h )
system ( cmd );
% % e x t r a c t data from d o m a i n C i r c l e. mesh
fid = fopen ( ' d o m a i n C i r c l e. mesh ' , ' r ' );
tline = fgets ( fid );
while ischar ( tline )
if strfind ( tline , ' Vertices ' )
numV = fscanf ( fid , ' % d ' , [1 , 1]);
coordV = reshape ( fscanf ( fid , ' % f ' ) , 4 , numV ) ' ; coordV (: , 3:4) = [];
end % if
if strfind ( tline , ' Edges ' )
numEbdry = fscanf ( fid , ' % d ' , [1 , 1]);
tmp = reshape ( fscanf ( fid , ' % f ' ) , 3 , numEbdry ) ' ;
V0Ebdry = tmp (: , 1:2); idEbdry = tmp (: , 3);
end % if
if strfind ( tline , ' T r i a n g l e s ' )
numT = fscanf ( fid , ' % d ' , [1 , 1]);
V0T = reshape ( fscanf ( fid , ' % f ' ) , 4 , numT ) ' ; V0T (: , 4) = [];
end % if
tline = fgets ( fid );
end % while
fclose ( fid );
% % g e n e r a t e lists and set b o u n d a r y IDs
g = g e n e r a t e G r i d D a t a( coordV , V0T );
g . idE = zeros ( g . numE , 1);
g . idE ( g . V2E ( sub2ind ([ g . numV , g . numV ] , V0Ebdry (: ,1) , V0Ebdry (: ,2)))) = idEbdry ;
g . idE0T = g . idE ( g . E0T ); % local edge IDs
end % f u n c t i o n

domainCircle.geo Geometry description of a circle with center (0, 0) and radius 0.5 serving as input for the grid generator Gmsh [19] which
is called by the routine domainCircle .
cx = 0.0; cy = 0.0; r = 0.5;
Point (0) = { cx , cy ,
0.0 , 1.0}; Point (1) = { cx -r , cy , 0.0 , 1.0}; Point (2) = { cx , cy +r , 0.0 , 1.0};
Point (3) = { cx +r , cy , 0.0 , 1.0}; Point (4) = { cx , cy -r , 0.0 , 1.0};
Circle (5) = {2 , 0 , 1}; Circle (6) = {3 , 0 , 2}; Circle (7) = {4 , 0 , 3}; Circle (8) = {1 , 0 , 4};
Line Loop (9) = {5 , 6 , 7 , 8}; // boundary edge IDs will be 5 to 8
Plane Surface (10) = {9};

g = domainSquare(h) Friedrichs–Keller triangulation on the unit square. The input argument h specifies the upper bound for the heights
of the triangles (not for the diameters). The output variable g representing the triangulation Th is of type struct according to Sec. 3.1. The

boundary IDs are set from 1 to 4.

function g = d o m a i n S q u a r e( h )
dim = ceil (1/ h ); % n u m b e r of edges per side of the unit s q u a r e
h = 1/ dim ;
% coordV
[X , Y ] = meshgrid (0: h :1);
Xlist = reshape (X , length ( X )^2 , 1); Ylist = reshape (Y , length ( X )^2 , 1);
coordV = [ Xlist , Ylist ];
% V0T
pat1 = [1 , dim +2 ,2]; % p a t t e r n of " lower - left " t r i a n g l e s
V0T1 = repmat ( pat1 , dim *( dim +1) , 1) + repmat ((0: dim *( dim +1) -1) ' , 1 , 3);
V0T1 ( dim +1 : dim +1 : dim *( dim +1) , :) = [];
pat2 = [ dim +2 , dim +3 ,2];
V0T2 = repmat ( pat2 , dim *( dim +1) , 1) + repmat ((0: dim *( dim +1) -1) ' , 1 , 3);
V0T2 ( dim +1 : dim +1 : dim *( dim +1) , :) = [];
% % g e n e r a t e grid data and b o u n d a r y IDs
g = g e n e r a t e G r i d D a t a( coordV , [ V0T1 ; V0T2 ]);
g . idE = zeros ( g . numE , 1);
g . idE ( g . baryE (: , 2) == 0) = 1; % south
g . idE ( g . baryE (: , 1) == 1) = 2; % east
g . idE ( g . baryE (: , 2) == 1) = 3; % north
g . idE ( g . baryE (: , 1) == 0) = 4; % west
g . idE0T = g . idE ( g . E0T ); % local edge IDs
end % f u n c t i o n

28

g = domainPolygon(X1, X2, h) triangulates a polygonally bounded domain Ω, where X1 and X2 are lists of x1 and x2 coordinates of the
corners. The output variable g representing the triangulation Th is of type struct according to Sec. 3.1. The boundary IDs are set to the values
produced by initmesh. This method uses the MATLAB grid generator initmesh.
function g = d o m a i n P o l y g o n( X1 , X2 , h )
gd = [2; length ( X1 (:)); X1 (:); X2 (:)]; % g e o m e t r y d e s c r i p t i o n
sf = ' polygon ' ;
% set f o r m u l a
ns = double ( ' polygon ' ) ' ;
% name space
[p , e , t ] = initmesh ( decsg ( gd , sf , ns ) , ' Hmax ' , h );
g = g e n e r a t e G r i d D a t a(p ' , t (1:3 , :) ' );
g . idE = zeros ( g . numE , 1);
g . idE ( g . V2E ( sub2ind ( size ( g . V2E ) , e (1 ,:) , e (2 ,:)))) = e (5 ,:);
g . idE0T = g . idE ( g . E0T ); % local edge IDs
end % f u n c t i o n

[X1, X2] = gammaMap(n, S) evaluates the parametrization of the nth edge Ên of the reference triangle T̂ at parameter values specified by
a list of parameters S, cf. (25).
function [ X1 , X2 ] = gammaMap (n , S )
S = S (:) ' ;
switch n
case 1 , X1 = 1 - S ;
X2 = S ;
case 2 , X1 = zeros ( size ( S )); X2 = S ;
case 3 , X1 = S ;
X2 = zeros ( size ( S ));
end % s w i t c h
end % f u n c t i o n

g = generateGridData(coordV, V0T) assembles lists describing the geometric and topological properties of a triangulation Th according
to Sec. 3.1 and stores them in the output variable g of type struct. The input arguments are the array coordV of dimension #V × 2 that contains
the x1 and x2 coordinates of the grid vertices (using a global index) and the array V0T of dimension #Th × 3 storing the global vertex indices for
each triangle with a counter-clockwise ordering. A usage example is found in Sec. 3.10. Note that the lists g.idE and g.idE0T (cf. Tab. 1)
storing the global and local edge indices are not generated and have to be defined manually after calling generateGridData .
function g = g e n e r a t e G r i d D a t a( coordV , V0T )
g . coordV = coordV ;
g . V0T = V0T ;
g . numT = size ( g . V0T , 1);
g . numV = size ( g . coordV , 1);
% the f o l l o w i n g i m p l i c i t e l y d e f i n e s the signs of the edges
g . V2T = sparse ( g . V0T (: , [1 2 3 1 2 3 1 2 3]) , g . V0T (: , [2 3 1 2 3 1 2 3 1]) , ...
[(1: g . numT ) ' , zeros ( g . numT ,3) ,(1:g . numT ) ' , zeros ( g . numT ,3) ,(1: g . numT ) ' ] , g . numV , g . numV );
% the f o l l o w i n g i m p l i c i t e l y d e f i n e s the edge n u m b e r s
[r , c ] = find ( triu ( g . V2T + g . V2T ' ));
g . V2E = sparse (r , c , 1 : size (r , 1) , g . numV , g . numV );
g . V2E = g . V2E + g . V2E ' ;
idxE = full ( g . V2E ( sub2ind ([ g . numV , g . numV ] , g . V0T ( end : -1:1 ,[1 ,2 ,3]) , g . V0T ( end : -1:1 ,[2 ,3 ,1])))) ' ;
g . V0E ( idxE (:) , 1) = reshape ( g . V0T ( end : -1:1 , [1 ,2 ,3]) ' , 3* g . numT , 1);
g . V0E ( idxE (:) , 2) = reshape ( g . V0T ( end : -1:1 , [2 ,3 ,1]) ' , 3* g . numT , 1);
g . T0E ( idxE (:) , 1) = reshape ( full ( g . V2T ( sub2ind ([ g . numV , g . numV ] , ...
g . V0T ( end : -1:1 ,[1 ,2 ,3]) , g . V0T ( end : -1:1 ,[2 ,3 ,1])))) ' , 3* g . numT , 1);
g . T0E ( idxE (:) , 2) = reshape ( full ( g . V2T ( sub2ind ([ g . numV , g . numV ] , ...
g . V0T ( end : -1:1 ,[2 ,3 ,1]) , g . V0T ( end : -1:1 ,[1 ,2 ,3])))) ' , 3* g . numT , 1);
g . numE = size ( g . V0E , 1);
g . vecE = g . coordV ( g . V0E (: , 2) , :) - g . coordV ( g . V0E (: , 1) , :);
g . areaE = ( g . vecE (: , 1).^2 + g . vecE (: , 2 ) . ^ 2 ) . ^ ( 1 / 2 ) ;
g . nuE = g . vecE * [0 , -1; 1 ,0] ./ g . areaE (: , [1 , 1]);
g . areaT = ...
( g . coordV ( g . V0T (: ,1) ,1).*g . coordV ( g . V0T (: ,2) ,2) + g . coordV ( g . V0T (: ,2) ,1).*g . coordV ( g . V0T (: ,3) ,2) ...
+ g . coordV ( g . V0T (: ,3) ,1).*g . coordV ( g . V0T (: ,1) ,2) - g . coordV ( g . V0T (: ,1) ,1).*g . coordV ( g . V0T (: ,3) ,2) ...
- g . coordV ( g . V0T (: ,2) ,1).*g . coordV ( g . V0T (: ,1) ,2) - g . coordV ( g . V0T (: ,3) ,1).*g . coordV ( g . V0T (: ,2) ,2) )/2;
g . baryT = ( g . coordV ( g . V0T (: ,1) ,:)+g . coordV ( g . V0T (: ,2) ,:)+g . coordV ( g . V0T (: ,3) ,:))/3;
g . E0T = full ( g . V2E ( sub2ind ([ g . numV , g . numV ] , g . V0T (: ,[2 ,3 ,1]) , g . V0T (: ,[3 ,1 ,2]))));
g . areaE0T = g . areaE ( g . E0T );
g . sigE0T = 1 -2*( bsxfun ( @eq , reshape ( g . T0E ( g . E0T ,2) , g . numT ,3) ,(1: g . numT ) ' ));
g . baryE = 0.5 * ( g . coordV ( g . V0E (: , 1) , :) + g . coordV ( g . V0E (: , 2) , :));
for n = 1 : 3
g . coordV0T (: , n , 1) = g . coordV ( g . V0T (: , n ) , 1) ' ; g . coordV0T (: , n , 2) = g . coordV ( g . V0T (: , n ) , 2) ' ;
g . baryE0T (: , n , 1) = g . baryE ( g . E0T (: , n ) , 1) ' ;
g . baryE0T (: , n , 2) = g . baryE ( g . E0T (: , n ) , 2) ' ;
g . nuE0T (: , n , 1) = g . nuE ( g . E0T (: , n ) , 1) ' ;
g . nuE0T (: , n , 2) = g . nuE ( g . E0T (: , n ) , 2) ' ;
Tn = g . sigE0T (: , n ) == 1; Tp = ~ Tn ;
g . E0E ( g . E0T ( Tn , n ) , 1) = n ;
g . E0E ( g . E0T ( Tp , n ) , 2) = n ;
end % for
for m = 1 : 2
g . B (: , m , 1) = g . coordV0T (: , 2 , m ) - g . coordV0T (: , 1 , m );
g . B (: , m , 2) = g . coordV0T (: , 3 , m ) - g . coordV0T (: , 1 , m );

29

end % for
markEint = g . E0E (: , 2) ~= 0; % mark i n t e r i o r edges
g . m a r k E 0 T E 0 T = cell (3 , 3);
for nn = 1 : 3
for np = 1 : 3
g . m a r k E 0 T E 0 T{ nn , np } = sparse ( g . numT , g . numT );
markEn = g . E0E (: , 1) == nn ; markEp = g . E0E (: ,
idx = markEn & markEp & markEint ;
g . m a r k E 0 T E 0 T{ nn , np }( sub2ind ([ g . numT , g . numT ] ,
markEn = g . E0E (: , 2) == nn ; markEp = g . E0E (: ,
idx = markEn & markEp & markEint ;
g . m a r k E 0 T E 0 T{ nn , np }( sub2ind ([ g . numT , g . numT ] ,
end % for
end % for
end % f u n c t i o n

2) == np ;
g . T0E ( idx , 1) , g . T0E ( idx , 2))) = 1;
1) == np ;
g . T0E ( idx , 2) , g . T0E ( idx , 1))) = 1;

ret = gradPhi(i, m, X1, X2) evaluates the mth component of the gradient of the ith basis function ϕ̂i on the reference triangle T̂
(cf. Sec. 2.4.1) at points specified by a list of x̂1 coordinates X1 and x̂2 coordinates X2.
function ret =
switch m
case 1
switch i
case 1 ,
case 2 ,
case 3 ,
case 4 ,
case 5 ,
case 6 ,
case 7 ,
case 8 ,
case 9 ,
case 10 ,
case 11 ,
case 12 ,
case 13 ,

gradPhi (i , m , X1 , X2 )

ret
ret
ret
ret
ret
ret
ret
ret
ret
ret
ret
ret
ret

=
=
=
=
=
=
=
=
=
=
=
=
=

zeros ( size ( X1 ));
-6* ones ( size ( X1 ));
-2* sqrt (3)* ones ( size ( X1 ));
sqrt (6)*(20* X1 - 8);
sqrt (3)*(10* X1 - 4);
6* sqrt (5)*(3* X1 + 4* X2 - 2);
2* sqrt ( 2 ) * ( 1 5 + ( - 9 0 + 1 0 5 *X1 ).* X1 );
2* sqrt (6)*(13+( -66+63* X1 ).* X1 +( -24+84* X1 ).* X2 );
2* sqrt (10)*(9+( -30+21* X1 ).* X1 +( -48+84* X1 +42* X2 ).* X2 );
2* sqrt (14)*(3+( -6+3* X1 ).* X1 +( -24+24* X1 +30* X2 ).* X2 );
sqrt ( 1 0 ) * ( - 2 4 + ( 2 5 2 + ( - 6 7 2 + 5 0 4 *X1 ).* X1 ).* X1 );
sqrt ( 3 0 ) * ( - 2 2 + ( 2 1 0 + ( - 5 0 4 + 3 3 6 *X1 ).* X1 ).* X1 +(42+( -336+504* X1 ).* X1 ).* X2 );
5* sqrt (2)*( -18+(138+( -264+144* X1 ).* X1 ).* X1 ...
+(102+( -624+648* X1 ).* X1 +( -96+432* X1 ).* X2 ).* X2 );
case 14 , ret = sqrt (70)*( -12+(60+( -84+36* X1 ).* X1 ).* X1 ...
+(132+( -456+324* X1 ).* X1 +( -300+540* X1 +180* X2 ).* X2 ).* X2 );
case 15 , ret = 3* sqrt (10)*( -4+(12+( -12+4* X1 ).* X1 ).* X1 ...
+(60+( -120+60* X1 ).* X1 +( -180+180* X1 +140* X2 ).* X2 ).* X2 );
end
case 2
switch i
case 1 , ret = zeros ( size ( X1 ));
case 2 , ret = zeros ( size ( X1 ));
case 3 , ret = -4* sqrt (3)* ones ( size ( X1 ));
case 4 , ret = zeros ( size ( X1 ));
case 5 , ret = 2* sqrt (3)*( -15* X2 + 6);
case 6 , ret = 6* sqrt (5)*(4* X1 + 3* X2 - 2);
case 7 , ret = zeros ( size ( X1 ));
case 8 , ret = 2* sqrt (6)*(2+( -24+42* X1 ).* X1 );
case 9 , ret = 2* sqrt (10)*(6+( -48+42* X1 ).* X1 +( -12+84* X1 ).* X2 );
case 10 , ret = 2* sqrt ( 1 4 ) * ( 1 2 + (- 2 4 + 1 2 * X1 ).* X1 +( -60+60* X1 +60* X2 ).* X2 );
case 11 , ret = zeros ( size ( X1 ));
case 12 , ret = sqrt (30)*( -2+(42+( -168+168* X1 ).* X1 ).* X1 );
case 13 , ret = 5* sqrt (2)*( -6+(102+( -312+216* X1 ).* X1 ).* X1 ...
+(12+( -192+432* X1 ).* X1 ).* X2 );
case 14 , ret = sqrt ( 7 0 ) * ( - 1 2 + ( 1 3 2 + ( - 2 2 8 + 1 0 8 *X1 ).* X1 ).* X1 ...
+(60+( -600+540* X1 ).* X1 +( -60+540* X1 ).* X2 ).* X2 );
case 15 , ret = 3* sqrt (10)*( -20+(60+( -60+20* X1 ).* X1 ).* X1 ...
+(180+( -360+180* X1 ).* X1 +( -420+420* X1 +280* X2 ).* X2 ).* X2 );
end % s w i t c h
end % s w i t c h
end % f u n c t i o n

main.m This is the main script to solve Equation (2) which can be used as a template for further modifications.
function main ()
%% parameters
hmax
= 1/8;
p
= 2;
tEnd
= pi /2;
numSteps
= 10;
isVisGrid
= false ;

%
%
%
%
%

m a x i m u m edge l e n g t h of t r i a n g l e
local p o l y n o m i a l d e g r e e
end time
n u m b e r of time steps
v i s u a l i z a t i o n of grid

30

isVisSol
= true ;
% v i s u a l i z a t i o n of s o l u t i o n
eta
= 1;
% p e n a l t y p a r a m e t e r ( eta >0)
% % c o e f f i c i e n t s and b o u n d a r y data
c0
= @ ( x1 , x2 ) sin ( x1 ).* cos ( x2 );
dAlg = @ (t , x1 , x2 ) ( x1 <3/4& x1 >1/4& x2 <3/4& x2 >1/4) + 0.01;
fAlg = @ (t , x1 , x2 ) 0.1* t *( x1 == x1 );
cDAlg = @ (t , x1 , x2 ) sin (2* pi * x2 + t );
gNAlg = @ (t , x1 , x2 ) x2 ;
%% triangulation
g = d o m a i n S q u a r e( hmax );
if isVisGrid , v i s u a l i z e G r i d( g ); end
%% globally constant parameters
K
= g . numT ;
% n u m b e r of t r i a n g l e s
N
= nchoosek ( p + 2 , p );
% n u m b e r of local DOFs
tau
= tEnd / numSteps ;
% time step size
m a r k E 0 T i n t = g . idE0T == 0;
% [ K x 3] mark local edges that are i n t e r i o r
m a r k E 0 T b d r N = g . idE0T == 1 | g . idE0T == 3; % [ K x 3] mark local edges on the N e u m a n n b o u n d a r y
m a r k E 0 T b d r D = ~( m a r k E 0 T i n t | m a r k E 0 T b d r N);
% [ K x 3] mark local edges on the D i r i c h l e t b o u n d a r y
% % l o o k u p table for basis f u n c t i o n
c o m p u t e B a s e s O n Q u a d( N );
% % c o m p u t a t i o n of m a t r i c e s on the r e f e r e n c e t r i a n g l e
hatM
= c o m p u t e H a t M( N );
hatG
= c o m p u t e H a t G( N );
hatH
= c o m p u t e H a t H( N );
hatRdiag
= c o m p u t e H a t R d i a g( N );
h a t R o f f d i a g = c o m p u t e H a t R o f f d i a g( N );
hatSdiag
= c o m p u t e H a t S d i a g( N );
h a t S o f f d i a g = c o m p u t e H a t S o f f d i a g( N );
% % a s s e m b l y of time - i n d e p e n d e n t g l o b a l m a t r i c e s
globM = a s s e m b l e G l o b M(g , hatM );
globH = a s s e m b l e G l o b H(g , hatH );
globQ = a s s e m b l e G l o b Q(g , markE0Tint , hatSdiag , h a t S o f f d i a g);
globQN = a s s e m b l e G l o b Q N(g , markE0TbdrN , hatSdiag );
globS = a s s e m b l e G l o b S(g , markE0Tint , hatSdiag , hatSoffdiag , eta );
globSD = a s s e m b l e G l o b S D(g , markE0TbdrD , hatSdiag , eta );
sysW = [ sparse (2* K *N ,3* K * N ) ; sparse ( K *N ,2* K * N ) , globM ];
% % i n i t i a l data
cDG = p r o j e c t A l g 2 D G(g , c0 , 2* p , hatM );
sysY = [ zeros (2* K *N ,1) ; reshape ( cDG ' , K *N , 1) ];
% % time s t e p p i n g
for nStep = 1 : numSteps
t = nStep * tau ;
% % L2 p r o j e c t i o n s of a l g e b r a i c c o e f f i c i e n t s
dDG = p r o j e c t A l g 2 D G(g , @ ( x1 , x2 ) dAlg (t , x1 , x2 ) , 2* p , hatM );
fDG = p r o j e c t A l g 2 D G(g , @ ( x1 , x2 ) fAlg (t , x1 , x2 ) , 2* p , hatM );
% % a s s e m b l y of time - d e p e n d e n t g l o b a l m a t r i c e s
globG = a s s e m b l e G l o b G(g , hatG , dDG );
globR = a s s e m b l e G l o b R(g , markE0Tint , hatRdiag , hatRoffdiag , dDG );
% % a s s e m b l y of D i r i c h l e t b o u n d a r y c o n t r i b u t i o n s
globRD = a s s e m b l e G l o b R D(g , markE0TbdrD , hatRdiag , dDG );
globJD = a s s e m b l e G l o b J D(g , markE0TbdrD , @ ( x1 , x2 ) cDAlg (t , x1 , x2 ) , N );
globKD = a s s e m b l e G l o b K D(g , markE0TbdrD , @ ( x1 , x2 ) cDAlg (t , x1 , x2 ) , eta , N );
% % a s s e m b l y of N e u m a n n b o u n d a r y c o n t r i b u t i o n s
globKN = a s s e m b l e G l o b K N(g , markE0TbdrN , dDG , @ ( x1 , x2 ) gNAlg (t , x1 , x2 ));
% % a s s e m b l y of the s o u r c e c o n t r i b u t i o n
globL = globM * reshape ( fDG ' , K *N , 1);
% % b u i l d i n g and s o l v i n g the s y s t e m
sysA = [
globM ,
sparse ( K *N , K * N ) , globH {1}+ globQ {1}+ globQN {1};
sparse ( K *N , K * N ) ,
globM , globH {2}+ globQ {2}+ globQN {2};
globG {1}+ globR {1}+ globRD {1} , globG {2}+ globR {2}+ globRD {2} ,
globS + globSD ];
sysV = [
globJD {1};
globJD {2};
globKD + globKN + globL ];
sysY = ( sysW + tau * sysA ) \ ( sysW * sysY + tau * sysV );
%% visualization
if isVisSol && p <= 2
cDG = reshape ( sysY (2* K * N +1 : 3* K * N ) , N , K ) ' ;
c L a g r a n g e = p r o j e c t D G 2 L a g r a n g e( cDG );
v i s u a l i z e D a t a(g , cLagrange , ' c_h ' , ' solution ' , nStep )
end % if
end % for
end % f u n c t i o n

ret = phi(i, X1, X2) evaluates the ith basis function ϕ̂i on the reference triangle T̂ (cf. Sec. 2.4.1) at points specified by a list of x̂1 coordinates X1 and x̂2 coordinates X2.
function ret = phi (i , X1 , X2 )
switch i

31

case
case
case
case
case
case
case
case
case
case
case
case
case

1,
2,
3,
4,
5,
6,
7,
8,
9,
10 ,
11 ,
12 ,
13 ,

ret
ret
ret
ret
ret
ret
ret
ret
ret
ret
ret
ret
ret

=
=
=
=
=
=
=
=
=
=
=
=
=

sqrt (2)* ones ( size ( X1 ));
2 - 6* X1 ;
2* sqrt (3)*(1 - X1 - 2* X2 );
sqrt ( 6 ) * ( ( 1 0 *X1 - 8).* X1 + 1);
sqrt (3)*((5* X1 - 4).* X1 + ( -15* X2 + 12).* X2 - 1);
3* sqrt (5)*((3* X1 + 8* X2 - 4).* X1 + (3* X2 - 4).* X2 + 1);
2* sqrt (2)*( -1+(15+( -45+35* X1 ).* X1 ).* X1 );
2* sqrt (6)*( -1+(13+( -33+21* X1 ).* X1 ).* X1 +(2+( -24+42* X1 ).* X1 ).* X2 );
2* sqrt (10)*( -1+(9+( -15+7* X1 ).* X1 ).* X1 +(6+( -48+42* X1 ).* X1 +( -6+42* X1 ).* X2 ).* X2 );
2* sqrt (14)*( -1+(3+( -3+ X1 ).* X1 ).* X1 +(12+( -24+12* X1 ).* X1 +( -30+30* X1 +20* X2 ).* X2 ).* X2 );
sqrt ( 1 0 ) * ( 1 + ( - 2 4 + ( 1 2 6 + ( - 2 2 4 + 1 2 6 *X1 ).* X1 ).* X1 ).* X1 );
sqrt ( 3 0 ) * ( 1 + ( - 2 2 + ( 1 0 5 + ( - 1 6 8 + 8 4 *X1 ).* X1 ).* X1 ).* X1 +( -2+(42+( -168+168* X1 ).* X1 ).* X1 ).* X2 );
5* sqrt (2)*(1+( -18+(69+( -88+36* X1 ).* X1 ).* X1 ).* X1 +( -6+(102+( -312+216* X1 ).* X1 ).* X1 ...
+(6+( -96+216* X1 ).* X1 ).* X2 ).* X2 );
case 14 , ret = sqrt (70)*(1+( -12+(30+( -28+9* X1 ).* X1 ).* X1 ).* X1 +( -12+(132+( -228+108* X1 ).* X1 ).* X1 ...
+(30+( -300+270* X1 ).* X1 +( -20+180* X1 ).* X2 ).* X2 ).* X2 );
case 15 , ret = 3* sqrt (10)*(1+( -4+(6+( -4+ X1 ).* X1 ).* X1 ).* X1 +( -20+(60+( -60+20* X1 ).* X1 ).* X1 ...
+(90+( -180+90* X1 ).* X1 +( -140+140* X1 +70* X2 ).* X2 ).* X2 ).* X2 );
end % s w i t c h
end % f u n c t i o n

cDG = projectAlg2DG(g, cAlg, R, hatM) computes the representation matrix cDG of an algebraic function in the DG / modal basis by
performing the L2 -projection described in Sec. 3.4. The non-obvious input arguments are as follows: cAlg is of type function_handle taking
rows of coordinates and hatM the matrix M̂ computed by computeHatM.
function cDG = p r o j e c t A l g 2 D G(g , cAlg , ord , hatM )
global gPhi2D
ord = max ( ord ,1); [ Q1 , Q2 , W ] = q u a d R u l e 2 D( ord );
K = g . numT ; N = size ( hatM , 1);
F1 = @ ( X1 , X2 ) g . B (: ,1 ,1)* X1 + g . B (: ,1 ,2)* X2 + g . coordV0T (: ,1 ,1)* ones ( size ( X1 ));
F2 = @ ( X1 , X2 ) g . B (: ,2 ,1)* X1 + g . B (: ,2 ,2)* X2 + g . coordV0T (: ,1 ,2)* ones ( size ( X1 ));
rhs = cAlg ( F1 ( Q1 , Q2 ) , F2 ( Q1 , Q2 )) * ( repmat ( W . ' , 1 , N ) .* gPhi2D { ord });
cDG = rhs / hatM ;
end % f u n c t i o n

cLagr = projectDG2Lagrange(cDG) converts the representation matrix in the DG / modal basis to the respective representation matrix in
a Lagrange / nodal basis, both of dimension K × N. In this routine, the local basis functions ϕ̂i are sampled at the Lagrange nodes on the reference
triangle T̂ (cf. visualizeData ), whose x̂1 and x̂2 coordinates are stored in the variables L1 and L2. Note that this routine is defined only for
polynomial degrees p ∈ {0, 1, 2}.
function cLagr = p r o j e c t D G 2 L a g r a n g e( cDG )
[K , N ] = size ( cDG );
switch N
case 1 , L1 = 1/3;
case 3 , L1 = [0 , 1 , 0];
case 6 , L1 = [0 , 1 , 0 , 1/2 , 0 , 1/2];
end % s w i t c h
cLagr = zeros (K , N );
for i = 1 : N
cLagr = cLagr + cDG (: , i ) * phi (i , L1 ,
end % for
end % f u n c t i o n

L2 = 1/3;
% locally constant
L2 = [0 , 0 , 1];
% locally linear
L2 = [0 , 0 , 1 , 1/2 , 1/2 , 0]; % l o c a l l y q u a d r a t i c

L2 );

[Q, W] = quadRule1D(qOrd) returns a list of Gauss–Legendre quadrature points Q within the interval (0, 1) and associated (positive)
weights W. The quadrature rule is exact for polynomials of order qOrd. The length of the interval [0, 1] is incorporated in the weights. A rule with
n points is exact for polynomials up to order 2n − 1.
Example: f = @(s) s.^2; [Q, W] = quadRule1D(2); dot(W, f(Q))
function [Q , W ] = q u a d R u l e 1 D( qOrd )
switch qOrd
case {0 , 1} % R = 1 , n u m b e r of q u a d r a t u r e p o i n t s
Q = 0;
W = 2;
case {2 , 3} % R = 2
Q = sqrt (1/3)*[ -1 , 1];
W = [1 , 1];
case {4 , 5} % R = 3
Q = sqrt (3/5)*[ -1 , 0 , 1];
W = 1/9*[5 , 8 , 5];
case {6 , 7} % R = 4
Q = [ -1 , -1 ,1 ,1].* sqrt (3/7+[1 , -1 , -1 ,1]*2/7*sqrt (6/5));
W = 1/36*(18 + sqrt (30)*[ -1 ,1 ,1 , -1]);

32

case {8 , 9} % R = 5
Q = [ -1 , -1 ,0 ,1 ,1].* sqrt (5+[2 , -2 ,0 , -2 ,2]* sqrt ( 1 0 / 7 ) ) / 3 ;
W = 1 / 9 0 0 * ( 3 2 2 + 1 3 * sqrt (70)*[ -1 ,1 ,0 ,1 , -1]+[0 ,0 ,190 ,0 ,0]);
case {10 , 11} % R = 6
Q = [ 0 . 6 6 1 2 0 9 3 8 6 46 62 64 5, -0.6612093864662645 , -0.2386191860831969 , ...
0 . 2 3 8 6 1 9 1 8 6 08 31 96 9, -0.9324695142031521 , 0 . 9 3 2 4 6 9 5 1 4 2 0 3 1 5 2 1 ] ;
W = [ 0 . 3 6 0 7 6 1 5 7 3 04 81 38 6, 0 . 3 6 0 7 6 1 5 7 3 04 81 38 6, 0 . 4 6 7 9 1 3 9 3 4 57 26 91 0, ...
0 . 4 6 7 9 1 3 9 3 4 57 26 91 0, 0 . 1 7 1 3 2 4 4 9 2 37 91 70 4, 0 . 1 7 1 3 2 4 4 9 2 3 7 9 1 7 0 ] ;
case {12 , 13} % R = 7
Q = [ 0 . 0 0 0 0 0 0 0 0 0 00 00 00 0, 0 . 4 0 5 8 4 5 1 5 1 37 73 97 2, -0.4058451513773972 , ...
-0.7415311855993945 , 0 . 7 4 1 5 3 1 1 8 5 59 93 94 5, -0.9491079123427585 , ...
0.9491079123427585];
W = [ 0 . 4 1 7 9 5 9 1 8 3 67 34 69 4, 0 . 3 8 1 8 3 0 0 5 0 50 51 18 9, 0 . 3 8 1 8 3 0 0 5 0 50 51 18 9, ...
0 . 2 7 9 7 0 5 3 9 1 48 92 76 6, 0 . 2 7 9 7 0 5 3 9 1 48 92 76 6, 0 . 1 2 9 4 8 4 9 6 6 16 88 69 7, ...
0.1294849661688697];
case {14 , 15} % R = 8
Q = [ -0.1834346424956498 , 0 . 1 8 3 4 3 4 6 4 2 49 56 49 8, -0.5255324099163290 , ...
0 . 5 2 5 5 3 2 4 0 9 91 63 29 0, -0.7966664774136267 , 0 . 7 9 6 6 6 6 4 7 7 41 36 26 7, ...
-0.9602898564975363 , 0 . 9 6 0 2 8 9 8 5 6 4 9 7 5 3 6 3 ] ;
W = [ 0 . 3 6 2 6 8 3 7 8 3 37 83 62 0, 0 . 3 6 2 6 8 3 7 8 3 37 83 62 0, 0 . 3 1 3 7 0 6 6 4 5 87 78 87 3, ...
0 . 3 1 3 7 0 6 6 4 5 87 78 87 3, 0 . 2 2 2 3 8 1 0 3 4 45 33 74 5, 0 . 2 2 2 3 8 1 0 3 4 45 33 74 5, ...
0 . 1 0 1 2 2 8 5 3 6 29 03 76 3, 0 . 1 0 1 2 2 8 5 3 6 2 9 0 3 7 6 3 ] ;
case {16 , 17} % R = 9
Q = [ 0 . 0 0 0 0 0 0 0 0 0 00 00 00 0, -0.8360311073266358 , 0 . 8 3 6 0 3 1 1 0 7 32 66 35 8, ...
-0.9681602395076261 , 0 . 9 6 8 1 6 0 2 3 9 50 76 26 1, -0.3242534234038089 , ...
0 . 3 2 4 2 5 3 4 2 3 40 38 08 9, -0.6133714327005904 , 0 . 6 1 3 3 7 1 4 3 2 7 0 0 5 9 0 4 ] ;
W = [ 0 . 3 3 0 2 3 9 3 5 5 00 12 59 8, 0 . 1 8 0 6 4 8 1 6 0 69 48 57 4, 0 . 1 8 0 6 4 8 1 6 0 69 48 57 4, ...
0 . 0 8 1 2 7 4 3 8 8 36 15 74 4, 0 . 0 8 1 2 7 4 3 8 8 36 15 74 4, 0 . 3 1 2 3 4 7 0 7 7 04 00 02 9, ...
0 . 3 1 2 3 4 7 0 7 7 04 00 02 9, 0 . 2 6 0 6 1 0 6 9 6 40 29 35 4, 0 . 2 6 0 6 1 0 6 9 6 4 0 2 9 3 5 4 ] ;
end % s w i t c h
Q = ( Q + 1)/2; W = W /2; % t r a n s f o r m a t i o n [ -1; 1] -> [0 , 1]
end % f u n c t i o n

T

[Q1, Q2, W] = quadRule2D(qOrd) returns quadrature points q̂r = [q̂1r , q̂2r ] within the reference triangle T̂ in lists of x̂1 and x̂2 coordinates Q1 and Q2, respectively, and the associated weights Q (cf. Sec. 3.3). The quadrature rule is exact for polynomials of order qOrd (cf. [24] for
orders 1, 2, 5, [25] for order 3, and [26] for order 4, 6). If qOrd is greater than 6, we call the third party function triquad [27] that uses Gaussian
quadrature points on a square which is collapsed to a triangle. The area 1/2 of the reference triangle T̂ is incorporated in the weights such that the
integral over one is 1/2.
Example: [Q1, Q2, W] = quadRule2D(2); N = 3; M = eye(N);
for i=1:N,for j=1:N,M(i,j)=sum(W.*phi(i,Q1,Q2).*phi(j,Q1,Q2));end,end
function [ Q1 , Q2 , W ] = q u a d R u l e 2 D( qOrd )
switch qOrd
case {0 , 1} % R = 1
Q1 = 1/3;
Q2 = 1/3;
W = 1/2;
case 2 % R = 3
Q1 = [1/6 , 2/3 , 1/6]; Q2 = [1/6 , 1/6 , 2/3]; W = [1/6 , 1/6 , 1/6];
case 3 % R = 4
Q1 = [0.666390246 , 0.178558728 , 0.280019915 , 0 . 0 7 5 0 3 1 1 0 9 ] ;
Q2 = [0.178558728 , 0.666390246 , 0.075031109 , 0 . 2 8 0 0 1 9 9 1 5 ] ;
W = [0.159020691 , 0.159020691 , 0.090979309 , 0 . 0 9 0 9 7 9 3 0 9 ] ;
case 4 % R = 6
Q1 = [ 0 . 4 4 5 9 4 8 4 9 09 15 965 , 0 . 1 0 8 1 0 3 0 1 81 68 07 0, 0 . 4 4 5 9 4 8 4 9 09 15 965, ...
0 . 0 9 1 5 7 6 2 1 35 09 77 1, 0 . 8 1 6 8 4 7 5 7 29 80 45 8, 0 . 0 9 1 5 7 6 2 1 3 5 0 9 7 7 1 ] ;
Q2 = [ 0 . 1 0 8 1 0 3 0 1 81 68 070 , 0 . 4 4 5 9 4 8 4 9 09 15 96 5, 0 . 4 4 5 9 4 8 4 9 09 15 965, ...
0 . 8 1 6 8 4 7 5 7 29 80 45 8, 0 . 0 9 1 5 7 6 2 1 35 09 77 1, 0 . 0 9 1 5 7 6 2 1 3 5 0 9 7 7 1 ] ;
W = [ 0 . 1 1 1 6 9 0 7 9 48 39 005 , 0 . 1 1 1 6 9 0 7 9 48 39 00 5, 0 . 1 1 1 6 9 0 7 9 48 39 005, ...
0 . 0 5 4 9 7 5 8 7 18 27 66 1, 0 . 0 5 4 9 7 5 8 7 18 27 66 1, 0 . 0 5 4 9 7 5 8 7 1 8 2 7 6 6 1 ] ;
case 5 % R = 7
a1 = (6 - sqrt ( 1 5 ) ) / 2 1 ;
a2 = (6+ sqrt ( 1 5 ) ) / 2 1 ;
w1 = (155 - sqrt ( 1 5 ) ) / 2 4 0 0 ; w2 = (155+ sqrt ( 1 5 ) ) / 2 4 0 0 ;
Q1 = [1/3 ,
a1 , 1 -2* a1 ,
a1 ,
a2 , 1 -2* a2 ,
a2 ];
Q2 = [1/3 , 1 -2* a1 ,
a1 ,
a1 , 1 -2* a2 ,
a2 ,
a2 ];
W = [9/80 ,
w1 ,
w1 ,
w1 ,
w2 ,
w2 ,
w2 ];
case 6 % R = 12
Q1 = [ 0 . 0 6 3 0 8 9 0 1 44 91 502 , 0 . 8 7 3 8 2 1 9 7 10 16 99 6, 0 . 0 6 3 0 8 9 0 1 44 91 502, ...
0 . 2 4 9 2 8 6 7 4 51 70 91 0, 0 . 5 0 1 4 2 6 5 0 96 58 17 9, 0 . 2 4 9 2 8 6 7 4 51 70 910, ...
0 . 3 1 0 3 5 2 4 5 10 33 78 5, 0 . 0 5 3 1 4 5 0 4 98 44 81 6, 0 . 6 3 6 5 0 2 4 9 91 21 399, ...
0 . 0 5 3 1 4 5 0 4 98 44 81 6, 0 . 6 3 6 5 0 2 4 9 91 21 39 9, 0 . 3 1 0 3 5 2 4 5 1 0 3 3 7 8 5 ] ;
Q2 = [ 0 . 0 6 3 0 8 9 0 1 44 91 502 , 0 . 0 6 3 0 8 9 0 1 44 91 50 2, 0 . 8 7 3 8 2 1 9 7 10 16 996, ...
0 . 2 4 9 2 8 6 7 4 51 70 91 0, 0 . 2 4 9 2 8 6 7 4 51 70 91 0, 0 . 5 0 1 4 2 6 5 0 96 58 179, ...
0 . 0 5 3 1 4 5 0 4 98 44 81 6, 0 . 3 1 0 3 5 2 4 5 10 33 78 5, 0 . 0 5 3 1 4 5 0 4 98 44 816, ...
0 . 6 3 6 5 0 2 4 9 91 21 39 9, 0 . 3 1 0 3 5 2 4 5 10 33 78 5, 0 . 6 3 6 5 0 2 4 9 9 1 2 1 3 9 9 ] ;
W = [ 0 . 0 2 5 4 2 2 4 5 31 85 103 , 0 . 0 2 5 4 2 2 4 5 31 85 10 3, 0 . 0 2 5 4 2 2 4 5 31 85 103, ...
0 . 0 5 8 3 9 3 1 3 78 63 18 9, 0 . 0 5 8 3 9 3 1 3 78 63 18 9, 0 . 0 5 8 3 9 3 1 3 78 63 189, ...

33

0 . 0 4 1 4 2 5 5 3 78 09 18 7, 0 . 0 4 1 4 2 5 5 3 78 09 18 7, 0 . 0 4 1 4 2 5 5 3 78 09 187, ...
0 . 0 4 1 4 2 5 5 3 78 09 18 7, 0 . 0 4 1 4 2 5 5 3 78 09 18 7, 0 . 0 4 1 4 2 5 5 3 7 8 0 9 1 8 7 ] ;
o t h e r w i s e % use Gauss q u a d r a t u r e p o i n t s on s q u a r e
[X ,Y , Wx , Wy ] = triquad ( qOrd , [0 0; 0 1; 1 0]); % third party function , see r e f e r e n c e s
Q1 = X (:). ' ;
Q2 = Y (:). ' ;
W = Wx * Wy . ' ; W = W (:). ' ;
end % s w i t c h
end % f u n c t i o n

[XP1, XP2] = theta(nn, np, X1, X2) returns the mapped points from n− th edge to the n+ th edge of the reference triangle T̂ , cf. (26).
function [ XP1 , XP2 ] = theta ( nn , np , X1 , X2 )
switch nn
case 1
switch np
case 1 , XP1 = 1 - X1 ; XP2 = 1 - X2 ;
case 2 , XP1 = zeros ( size ( X1 )); XP2 = X2 ;
case 3 , XP1 = X1 ; XP2 = zeros ( size ( X1 ));
end % s w i t c h
case 2
switch np
case 1 , XP1 = 1 - X2 ; XP2 = X2 ;
case 2 , XP1 = zeros ( size ( X1 )); XP2 = 1 - X2 ;
case 3 , XP1 = X2 ; XP2 = zeros ( size ( X1 ));
end % s w i t c h
case 3
switch np
case 1 , XP1 = X1 ; XP2 = 1 - X1 ;
case 2 , XP1 = zeros ( size ( X1 )); XP2 = X1 ;
case 3 , XP1 = 1 - X1 ; XP2 = zeros ( size ( X1 ));
end % s w i t c h
end % s w i t c h
end % f u n c t i o n

visualizeGrid(g) visualizes the triangulation Th along with global and local indices and edge normals. The input argument g is the output
of the routine generateGridData .
function v i s u a l i z e G r i d( g )
figure ( ' Color ' , [1 , 1 , 1]); % white b a c k g r o u n d
hold ( ' on ' ) , axis ( ' off ' )
daspect ([1 , 1 , 1]) % a d j u s t a s p e c t ration , r e q u i r e s O c t a v e >= 3.8
t e x t a r r a y = @ ( x1 , x2 , s ) arrayfun ( @ (a ,b , c ) text (a ,b , int2str ( c )) , x1 , x2 , s );
%% triangle boundaries
trisurf ( g . V0T , g . coordV (: ,1) , g . coordV (: ,2) , zeros ( g . numV ,1) , ' f a c e c o l o r ' , ' none ' );
% % edge n o r m a l s ( s c a l e d by the f a c t o r of | E |/4 to fit the s c a l e s)
quiver ( g . baryE (: ,1) , g . baryE (: ,2) , ...
g . nuE (: ,1).*g . areaE (:)/4 , g . nuE (: , 2).* g . areaE (:)/4 , 0)
% % local edge n u m b e r s
w = [1/12 , 11/24 , 11/24; 11/24 , 1/12 , 11/24; 11/24 , 11/24 , 1/12];
for kE = 1 : 3
t e x t a r r a y( reshape ( g . coordV ( g . V0T ,1) , g . numT ,3)* w (: , kE ) , ...
reshape ( g . coordV ( g . V0T ,2) , g . numT ,3)* w (: , kE ) , kE * ones ( g . numT , 1))
end % for
%% global vertex numbers
t e x t a r r a y( g . coordV (: ,1) , g . coordV (: ,2) , (1: g . numV ) ' );
% % local v e r t e x n u m b e r s
w = [5/6 , 1/12 , 1/12; 1/12 , 5/6 , 1/12; 1/12 , 1/12 , 5/6];
for kV = 1 : 3
t e x t a r r a y( reshape ( g . coordV ( g . V0T ,1) , g . numT ,3)* w (: , kV ) , ...
reshape ( g . coordV ( g . V0T ,2) , g . numT ,3)* w (: , kV ) , kV * ones ( g . numT , 1))
end % for
% % g l o b a l edge n u m b e r s
t e x t a r r a y( g . baryE (: ,1) , g . baryE (: ,2) , (1: g . numE ) ' );
%% triangle numbers
t e x t a r r a y( g . baryT (: ,1) , g . baryT (: ,2) , (1: g . numT ) ' );
% % edge IDs
markEext = g . idE ~= 0; % mark b o u n d a r y edges
t e x t a r r a y( g . baryE ( markEext ,1) + g . nuE ( markEext ,1).* g . areaE ( markEext )/8 , ...
g . baryE ( markEext ,2) + g . nuE ( markEext ,2).* g . areaE ( markEext )/8 , g . idE ( markEext ))
end % f u n c t i o n

34

visualizeData(g, repLagr, varName, fileName, tLvl) writes a .vtu file for the visualization of a discrete quantity
in P p (Th ), p ∈ {0, 1, 2} defined on the triangulation g according to generateGridData .
The name of the generated file is
fileName.tLvl.vtu, where tLvl stands for time level. The name of the quantity within the file is specified by varName. The argument repLagr should be a list of dimension K × N containing the Lagragian representation of the quantity. The kth row of repLagr has to hold

the value on T k for P0 (Th ), the values on the vertices of T k for P1 (Th ), and the values on the vertices and on the edge barycenters of T k for P2 (Th ).
Note that we treat functions of P0 (Th ) as if they were in P1 (Th ) as we assign the constant value on T k to the verices vk1 , vk2 , vk3 . An alternative
was using CellData instead of PointData . A usage example is found in Sec. 3.10.
function v i s u a l i z e D a t a(g , repLagr , varName , fileName , tLvl )
[K , N ] = size ( repLagr ) ;
% % open file
fileName = [ fileName , ' . ' , num2str ( tLvl ) , ' . vtu ' ];
file
= fopen ( fileName , ' wt ' ) ; % if this file exists , then o v e r w r i t e
%% header
fprintf ( file , ' <? xml version ="1.0"? >\ n ' ) ;
fprintf ( file , ' < VTKFile type =" U n s t r u c t u r e d G r i d" version ="0.1" b y t e _ o r d e r=" L i t t l e E n d i a n" c o m p r e s s o r="
֒→v t k Z L i b D a t a C o m p r e s s o r" >\ n ' ) ;
fprintf ( file , ' < UnstructuredGrid >\ n ' ) ;
% % p o i n t s & cells
switch N
case {1 , 3}
P1
= reshape ( g . coordV0T (: , : , 1) ' , 3* K , 1) ;
P2
= reshape ( g . coordV0T (: , : , 2) ' , 3* K , 1) ;
numP
= 3; % n u m b e r of local p o i n t s
id
= 5; % vtk ID for l i n e a r p o l y n o m i a l s
case 6
P1
= reshape ([ g . coordV0T (: ,: ,1) , g . baryE0T (: ,[3 ,1 ,2] ,1) ] ' ,6*K ,1) ;
P2
= reshape ([ g . coordV0T (: ,: ,2) , g . baryE0T (: ,[3 ,1 ,2] ,2) ] ' ,6*K ,1) ;
numP
= 6; % n u m b e r of local p o i n t s
id
= 22; % vtk ID for q u a d r a t i c p o l y n o m i a l s
end % s w i t c h
fprintf ( file , '
< Piece N u m b e r O f P o i n t s ="% d " N u m b e r O f C e l l s ="% d " >\ n ' ,K * numP , K ) ;
fprintf ( file , '
< Points >\ n ' ) ;
fprintf ( file , '
< D a t a A r r a y type =" Float32 " N u m b e r O f C o m p o n e n t s ="3" format =" ascii " >\ n ' ) ;
fprintf ( file , '
%.3 e %.3 e %.3 e \ n ' , [ P1 , P2 , zeros ( numP *K , 1) ] ' ) ;
fprintf ( file , '
</ DataArray >\ n ' ) ;
fprintf ( file , '
</ Points >\ n ' ) ;
fprintf ( file , '
< Cells >\ n ' ) ;
fprintf ( file , '
< D a t a A r r a y type =" Int32 " Name =" c o n n e c t i v i t y" format =" ascii " >\ n ' ) ;
fprintf ( file , '
' ) ; fprintf ( file , ' % d ' , 0: K * numP -1) ;
fprintf ( file , ' \ n
</ DataArray >\ n ' ) ;
fprintf ( file , '
< D a t a A r r a y type =" Int32 " Name =" offsets " format =" ascii " >\ n ' ) ;
fprintf ( file , '
% d \ n ' , numP : numP : numP * K ) ;
fprintf ( file , '
</ DataArray >\ n ' ) ;
fprintf ( file , '
< D a t a A r r a y type =" UInt8 " Name =" types " format =" ascii " >\ n ' ) ;
fprintf ( file , '
% d \ n ' , id * ones (K , 1) ) ;
fprintf ( file , '
</ DataArray >\ n ' ) ;
fprintf ( file , '
</ Cells >\ n ' ) ;
% % data
switch N
case 1 % l o c a l l y c o n s t a n t
dataLagr = kron ( repLagr , [1;1;1]) ' ;
case 3 % l o c a l l y q u a d r a t i c
dataLagr = reshape ( repLagr ' , 1 , K * N ) ;
case 6 % l o c a l l y q u a d r a t i c ( p e r m u t a t i o n of local edge i n d i c e s due to vtk f o r m a t)
dataLagr = reshape ( repLagr (: , [1 ,2 ,3 ,6 ,4 ,5]) ' , 1 , K * N ) ;
end % s w i t c h
fprintf ( file , '
< P o i n t D a t a Scalars ="% s " >\ n ' , varName ) ;
fprintf ( file , '
< D a t a A r r a y type =" Float32 " Name ="% s " N u m b e r O f C o m p o n e n t s="1" format =" ascii " >\ n ' ,
֒→varName ) ;
fprintf ( file , '
%.3 e \ n ' , dataLagr ) ;
fprintf ( file , '
</ DataArray >\ n ' ) ;
fprintf ( file , '
</ PointData >\ n ' ) ;
%% footer
fprintf ( file , '
</ Piece >\ n ' ) ;
fprintf ( file , ' </ UnstructuredGrid >\ n ' ) ;
fprintf ( file , ' </ VTKFile >\ n ' ) ;
% % close file
fclose ( file ) ;
disp ([ ' Data written to ' fileName ])
end % f u n c t i o n

35

5. Conclusion and Outlook
The MATLAB / GNU Octave toolbox described in this work represents the first step of a multi-purpose package
that will include performance optimized discretizations for a range of standard problems, first, from the CFD, and then,
conceivably, from other application areas. Several important features will be added in the upcoming parts of this paper
and in the new releases of the toolbox: slope limiters based on the nodal slope limiting procedure proposed in [28, 29],
convection terms, nonlinear advection operators, and higher order time solvers. Furthermore, the object orientation
capabilites provided by MATLAB’s classdef, which are expected to be supported in future GNU Octave versions,
will be exploited to provide a more powerful and comfortable user interface.
Acknowledgments
The work of B. Reuter was supported by the German Research Foundation (DFG) under grant AI 117/1-1.
Index of notation
Symbol

Definition

{| · |}, ~·

average
and
"
# jump on an edge
A
≔
, block-diagonal matrix with blocks A, B

diag(A, B)
#M
a·b
◦
⊗
c
d
η
δm∈M
Ekn , Ên
V, E, T
ED , EN
EΩ , E∂Ω
Fk
h
hT
J
K
νT
νk
N
ωr
Ω, ∂Ω
∂ΩD , ∂ΩN
p
ϕik , ϕ̂i
P p (T )
P p (Th )
q̂r
R
R+ , R+0
t
tn
tend
ϑ̂n− n+
∆tn
T k , ∂T k
T̂
vki v̂i
x
x̂
z

B

cardinality of a set M
P
≔ 2m=1 am bm , Euclidean scalar product in R2
composition of functions or Hadamard product
Kronecker product
concentration (scalar-valued unknown)
diffusion coefficient
penalty parameter
≔ {1 if m ∈ M, 0 if m < M}, Kronecker delta
nth edge of the physical triangle T k , nth edge of the reference triangle T̂
sets of vertices, edges, and triangles
set of boundary edges, E∂Ω = ED ∪ EN
set of interior edges, set of boundary edges
affine mapping from T̂ to T k
mesh fineness
≔ diam(h), diameter of triangle T ∈ Th
≔ (0, tend ), open time interval
≔ #Th , number of triangles
unit normal on ∂T pointing outward of T
≔ νT k
≔ (p + 1)(p + 2)/2, number of local degrees of freedom
quadrature weight associated with q̂r
spatial domain in two dimensions, boundary of Ω
Dirichlet
and Neumann boundaries, ∂Ω = ∂ΩD ∪ ∂ΩN
√
= ( 8N + 1 − 3)/2, polynomial degree
ith hierarchical basis function on T k , ith hierarchical basis function on T̂
space of polynomials of degree at most p
≔ {wh : Ω → R ; ∀T ∈ Th , wh |T ∈ P p (T )}
rth quadrature point in T̂
number of quadrature points
set of (strictly) positive real numbers, set of nonnegative real numbers
time variable
nth time level
end time
mapping from Ên− to Ên+
≔ tn+1 − tn , time step size
kth physical triangle, boundary of T k
bi-unit reference triangle
ith vertex of the physical triangle T k , ith vertex of the reference triangle T̂
T
= [x1 , x2 ] , space variable in the physical domain Ω
T
= [ x̂1 , x̂2 ] , space variable in the reference triangle T̂
diffusion mass flux (vector-valued unknown)

36

[1] H. Reed, T. R. Hill, Triangular mesh methods for the neutron transport equation, Tech. Rep. LA-UR-73-479, Los Alamos Scientific Laboratory, NM (1973).
[2] D. N. Arnold, F. Brezzi, B. Cockburn, L. D. Marini, Analysis of discontinuous Galerkin methods for elliptic problems, SIAM J. Num. Anal.
39 (2002) 1749–1779.
[3] The Mathworks, Inc., Natick, MA, USA, MATLAB and Statistics Toolbox Release 2014a.
[4] Octave community, GNU Octave 3.8.1 (2014).
URL http://www.gnu.org/software/octave/
[5] F. Frank, B. Reuter, V. Aizinger (2014). [link].
URL http://www.math.fau.de/FESTUNG
[6] B. Cockburn, C. Shu, The local discontinuous Galerkin method for time-dependent convection–diffusion systems, SIAM Journal on Numerical Analysis 35 (6) (1998) 2440–2463. doi:10.1137/S0036142997316712.
[7] V. Aizinger, C. Dawson, B. Cockburn, P. Castillo, The local discontinuous Galerkin method for contaminant transport, Advances in Water
Resources 24 (1) (2000) 73–87. doi:10.1016/S0309-1708(00)00022-1.
[8] V. Aizinger, C. Dawson, A discontinuous Galerkin method for two-dimensional flow and transport in shallow water, Advances in Water
Resources 25 (1) (2002) 67–84. doi:10.1016/S0309-1708(01)00019-7.
[9] B. Rivière, Discontinuous Galerkin Methods for Solving Elliptic and Parabolic Equations: Theory and Implementation, Society for Industrial
and Applied Mathematics, 2008.
[10] M. G. Larson, F. Bengzon, The Finite Element Method: Theory, Implementation, and Applications, Vol. 10 of Texts in Computational Science
and Engineering, Springer, 2013.
[11] J. S. Hesthaven, T. Warburton, Nodal Discontinuous Galerkin Methods: Algorithms, Analysis, and Applications, Texts in Applied Mathematics, Springer, 2008.
[12] T. Warburton (2014). [link].
URL http://www.nudg.org/
[13] Z. Fu, L. F. Gatica, F.-J. Sayas, Matlab tools for HDG in three dimensions, ArXiv e-prints arXiv:1305.1489.
[14] L. Chen, iFEM: an integrated finite element methods package in MATLAB, Tech. rep., Technical Report, University of California at Irvine
(2009).
[15] S. Funken, D. Praetorius, P. Wissgott, Efficient implementation of adaptive p1-fem in matlab, Comput. Methods Appl. Math. 11 (4) (2011)
460–490.
[16] T. Rahman, J. Valdman, Fast MATLAB assembly of FEM matrices in 2d and 3d: nodal elements, Appl. Math. Comput. 219 (13) (2013)
7151–7158. doi:10.1016/j.amc.2011.08.043.
[17] B. Cockburn, C.-W. Shu, TVB Runge–Kutta local projection discontinuous Galerkin finite element method for conservation laws ii: general
framework, Mathematics of Computation 52 (186) (1989) 411–435.
[18] C. Bahriawati, C. Carstensen, Three matlab implementations of the lowest-order Raviart–Thomas MFEM with a posteriori error control,
Computational Methods in Applied Mathematics 5 (4) (2005) 333–361.
[19] C. Geuzaine, J.-F. Remacle, Gmsh: A 3-D finite element mesh generator with built-in pre-and post-processing facilities, International Journal
for Numerical Methods in Engineering 79 (11) (2009) 1309–1331.
[20] R. Cools, An encyclopaedia of cubature formulas, Journal of Complexity 19 (3) (2003) 445–453. doi:10.1016/S0885-064X(03)00011-6.
[21] B. Cockburn, C.-W. Shu, The Runge–Kutta discontinuous Galerkin method for conservation laws V: multidimensional systems, J. Comput.
Phys. 141 (2) (1998) 199–224. doi:10.1006/jcph.1998.5892.
[22] L. S. Avila, Kitware, Inc, The VTK User’s Guide, Kitware, Incorporated, 2010.
[23] A. H. Squillacote, The ParaView Guide: A Parallel Visualization Application, Kitware, 2007.
[24] A. H. Stroud, Approximate Calculation of Multiple Integrals, Prentice-Hall Series in Automatic Computation, Prentice-Hall, 1971.
[25] P. Hillion, Numerical integration on a triangle, International Journal for Numerical Methods in Engineering 11 (5) (1977) 797–815.
doi:10.1002/nme.1620110504.
[26] G. R. Cowper, Gaussian quadrature formulas for triangles, International Journal for Numerical Methods in Engineering 7 (3) (1973) 405–408.
doi:10.1002/nme.1620070316.
[27] G. von Winckel, Gaussian Quadrature for Triangles. MATLAB Central File Exchange. Retrieved July 30, 2014. (2005). [link].
URL http://www.mathworks.com/matlabcentral/fileexchange/9230-gaussian-quadrature-for-triangles
[28] V. Aizinger, A geometry independent slope limiter for the discontinuous Galerkin method, in: E. Krause, Y. Shokin, M. Resch, D. Krner,
N. Shokina (Eds.), Computational Science and High Performance Computing IV, Vol. 115 of Notes on Numerical Fluid Mechanics and
Multidisciplinary Design, Springer Berlin Heidelberg, 2011, pp. 207–217. doi:10.1007/978-3-642-17770-5_16.
[29] D. Kuzmin, A vertex-based hierarchical slope limiter for adaptive discontinuous Galerkin methods, Journal of Computational
and Applied Mathematics 233 (12) (2010) 3077–3085, Finite Element Methods in Engineering and Science (FEMTEC 2009).
doi:10.1016/j.cam.2009.05.028.

37

