1 Introduction

In recent years, hybrid discretisation methods have received increasing attention by the applied mathematics and computational engineering community. The main interest in these methodologies is due to their reduced computational cost with respect to classical discontinuous Galerkin (DG) methods, see [135, 163, 170, 267], from which they inherit appealing stability and convergence properties as well as the flexibility to devise high-order, non-uniform degree and adaptive discretisations and the capability to efficiently exploit parallel computing architectures [42, 91, 108, 158, 226].

The purpose of the present contribution is two-fold: to present a review on the state-of-the-art of hybrid discretisation methods including both fundamental and applied contributions; to provide an educational implementation of the hybridisable discontinuous Galerkin (HDG) method in MATLAB, the so-called HDGlab library, and describe its structure, capabilities and functioning. HDGlab is an open-source library released under GNU GPL licence and designed for rapid prototyping and testing. It supports simplicial meshes and it provides a seamless 2D and 3D implementation with vectorised loops on the integration points. In addition, HDGlab presents four specific features, currently not available in existing open-source HDG implementations in MATLAB:

  1. 1.

    Availability of high-order polynomial shape functions up to degree 9, with both equally-spaced and Fekete nodal distributions.

  2. 2.

    Support of curved isoparametric simplicial elements in 2D and 3D.

  3. 3.

    Support of non-uniform degree polynomial approximations and flexibility to devise degree adaptivity strategy.

  4. 4.

    Interface with the open-source high-order mesh generator Gmsh.

The remainder of this paper is organised as follows. First, a review of the state-of-the-art on hybrid discretisation methods is presented in Sect. 2. The formulation of the HDG method for the Poisson and Stokes problems is briefly recalled in Sects. 3 and 4, respectively. Section 5 provides a description of the structure of the HDGlab library and the url of the repository available under GNU GPL licence. The data structures for the storage of the mesh information, the reference element and the reference face are presented in Sect. 6. Section 7 is devoted to the preprocessing operations, whereas the core of the HDGlab solver for the scalar Poisson equation is described in Sect. 8. Its extension to vectorial problems involving incompressible Stokes flows is discussed in Sect. 9. The visualisation library is introduced in Sect. 10. Section 11 is devoted to numerical examples, in 2D and 3D, validating the optimal convergence properties of the HDG method and showing the potentialities of the HDGlab implementation. Finally, Sect. 12 summarises the capabilities of the presented library and three appendices provide implementation details for the Poisson (Appendix A) and Stokes (Appendix B) solvers and for the interface with the mesh generator Gmsh (Appendix C).

2 Literature Review

The common idea of all hybrid discretisation methods stems from the seminal works of Guyan on static condensation of primal formulations [157] and of Fraeijs de Veubeke on hybridisation of mixed formulations [138] of the finite element method. In the context of element-by-element discontinuous approximations, these techniques allow to remedy the drawback of node duplication in DG methods by considering only the unknowns on the mesh faces (edges in 2D) as globally-coupled degrees of freedom. More precisely, the unknowns in each element are expressed as a function of the degrees of freedom on the element faces by solving a local boundary value problem with purely Dirichlet data, whereas appropriate transmission conditions are imposed to guarantee the interelement continuity of the solution and the fluxes, see [75].

Three families of hybrid numerical schemes lay within this description, namely, (1) hybrid/hybridised DG, (2) hybridisable DG, henceforth referred to as HDG, and (3) hybrid high order (HHO) methods. Stemming from classical DG primal formulations, the hybrid or hybridised DG method reduces the number of globally coupled degrees of freedom by performing static condensation [124,125,126]. In addition, improved efficiency can be achieved using polynomial spaces of degree \({\texttt {p}} + 1\) and \({\texttt {p}}\) for the primal and hybrid variables, respectively and resorting to the reduced stabilisation approach [206, 207]. The hybridisable DG method, henceforth named HDG, is derived from the mixed formulation of the local DG method [77, 87, 99] with hybridisation. The main advantage of HDG with respect to other hybridised DG methods relies in the introduction of a mixed variable approximating the gradient of the primal unknown [88, 89]. This approach is of special interest in the context of engineering problems where quantities of interest often depend on the flux of the solution or on the stress. Finally, HHO bridges the two approaches above by utilising a primal formulation and introducing a local reconstruction operator for the gradient of the solution and an appropriate stabilisation term in the static condensation problem [109, 110]. It is worth noting that many hybrid discretisation schemes can be interpreted in a unique framework as HDG-type methods via appropriate definitions of the stabilisation term, see e.g. [68, 69] for the staggered DG method and [106] for HHO.

Unified presentations of hybrid discretisation techniques and their relationship with other known numerical methods are available in [37, 89, 114]. Interested readers are also referred to the review papers [75, 149] and to the recent monograph [112]. In the following subsections, an overview of the contributions on hybrid discretisation methods according to the authors’ vision is presented.

2.1 From Linear to Nonlinear Scalar Equations

Second-order scalar elliptic problems have been extensively studied using HDG [89], HHO [110, 115] and the hybridised DG method [206], whereas their extension to linear convection–diffusion problems is discussed in [79, 113, 124, 200]. Cases of higher-order partial differential equations (PDEs) are presented in [78] and [62] for HDG discretisations of biharmonic and third-order equations, respectively, whereas an HHO approximation of the Cahn-Hilliard equation is proposed in [53]. In addition, time-fractional diffusion problems are discussed in [76, 197].

More recently, there has been growing interest towards the analysis and simulation of quasilinear and semilinear problems, including the quasilinear p-Laplace operator [95, 216] and the semilinear Grad–Shafranov equation [233, 234]. To reduce the computational cost of semilinear problems, the interpolatory HDG method was recently devised introducing an interpolation procedure for the efficient and accurate approximation of nonlinear terms [54, 100].

Concerning nonlinear problems, HDG discretisations were proposed for nonlinear convection–diffusion [201] and nonlinear Schrödinger [46] equations, whereas an HHO formulation of the nonlinear Leray–Lions equation is presented in [111]. Recent applications involving HDG approximations of nonlinear scalar equations focus on the optoelectronic simulation of photovoltaic solar cells. This problem couples a high-order HDG method for the drift-diffusion electronic model in the semiconductor layer of the solar cells with an efficient approximation of the time-harmonic Maxwell’s equations [21, 56].

2.2 Incompressible Flows

In the context of incompressible flows, HDG formulations of the Stokes equations were devised and analysed in [90, 92, 98, 199]. The corresponding analysis of the HDG method for Oseen flow is presented in [49]. In [92], it was observed that the HDG method based on Cauchy stress tensor formulation experiences suboptimal convergence of the mixed variable and loss of superconvergence of the postprocessed velocity, when low-order polynomial approximations are considered. The M-decomposition approach [84] remedies this issue by appropriately enriching the discrete local spaces of approximation. An alternative strategy imposing the symmetry of the mixed variable pointwise via Voigt notation is discussed in [148] along with a postprocessing procedure to handle translational and rotational rigid body modes. Divergence-conforming HDG [94], hybridised DG [125] and embedded-hybridised DG (EHDG) [225] discretisations were also studied for the incompressible Stokes equations, whereas a pressure-robust HHO method for viscosity-dependent Stokes flows is proposed in [116].

HDG formulations for the nonlinear incompressible Navier–Stokes equations using equal order and different order of polynomial approximations for the primal, mixed and hybrid variables are described in [50, 204] and [215], respectively. The former approach is also employed in [152] to devise a degree adaptive strategy relying on the local superconvergence of the postprocessed velocity. Stemming from the work in [117], different HHO formulations of the incompressible Navier–Stokes equations were proposed, incorporating a skew-symmetric form of the convection term [31] and a globally divergence-free velocity approximation to achieve robustness in presence of large irrotational body forces [45]. Moreover, special attention was devoted in recent years to the development of hybridised DG schemes [126] with pointwise divergence-free velocity [171, 181, 223] and with relaxed \(H({\text {div}})\)-conformity [177, 178], as well as divergence-conforming hybrid DG discretisations for incompressible flows on surfaces [179]. It is worth noting that all the above mentioned references focus on viscous laminar flows and preliminary promising results on the incompressible Reynolds averaged Navier–Stokes (RANS) equations coupled with the Spalart-Allmaras turbulence model were recently presented in [210].

Besides classical approaches to steady and unsteady Navier–Stokes equations, HDG-based space-time formulations were studied for their ability to effectively handle moving and deforming domains. More precisely, stemming from the HDG formulation introduced in [222], \(H({\text {div}})\)-conforming hybridised DG [160] and EHDG [161] methods were proposed. Hybridised DG and HDG methods with arbitrary Lagrangian Eulerian (ALE) formulations were thus presented in [134, 140] and the resulting HDG-ALE framework was applied to fluid–structure interaction (FSI) problems involving incompressible [248] and weakly-compressible flows [176].

Among the applications of hybrid discretisation methods to incompressible flows, it is also worth mentioning the recent attempts to simulate quasi-Newtonian fluids [145] and viscoplastic materials [44].

2.3 Two-Phase Flows and Heterogeneous Porous Media

HDG simulations of immiscible incompressible two-phase flows in heterogeneous porous media were first proposed in [130] and coupled with high-order diagonally implicit Runge–Kutta (DIRK) time integrators in [107]. Moreover, in [168] a linear degenerate elliptic problem modelling two-phase mixture is approximated using a hybridised DG approach. Darcy flow and two-phase flow simulations in highly heterogeneous media are performed in [270] via the so-called generalised multiscale HDG (GMsHDG) method which is connected to the mortar mixed finite element method described in [24]. GMsHDG was also employed for multiscale simulations of elliptic PDEs in heterogeneous media [70, 123] and perforated domains [65] and of parabolic PDEs in heterogeneous media [192].

An alternative to GMsHDG is the HHO framework for highly oscillatory elliptic problems introduced in [73]. Moreover, in the context of coupled problems involving porous media, HHO simulations of passive transport of a solute in a fractured medium are presented in [52], whereas nonlinear poroelastic phenomena in a saturated porous medium with a slightly compressible fluid are described in [33, 35].

Extensive research has been also devoted to coupled Stokes/Darcy and Brinkman models. In [51], an EHDG formulation of the Stokes/Darcy system is described. Concerning the Brinkman model, an analysis of its HDG approximation is presented in [22], its simulation in the context of heterogeneous media with high-contrast is discussed in [183] and an \(H({\text {div}})\)-conforming discretisation is proposed in [142]. In [30], an HHO formulation with divergence-conforming Darcy velocity and higher-order Stokes velocity is devised.

2.4 Compressible Flows and Gas Kinetics Equations

Hybrid formulations for inviscid Euler and laminar compressible Navier–Stokes equations are proposed in [209] in the context of HDG and in [205] for the embedded DG (EDG) method. Extension to viscous turbulent compressible flows using RANS equations with Spalart–Allmaras turbulence model is presented in [193], whereas a large-eddy simulation framework is introduced in [133]. In addition, an entropy-stable space-time discretisation was proposed for the compressible Navier–Stokes equations using an HDG approach in space and a discontinuous approximation in time [266]. More recently, special attention was dedicated to the development of positivity-preserving Riemann solvers in the context of hybridised DG methods [263]. For a complete review on HDG methods for compressible flows, interested readers are referred to [263], whereas the application to gas kinetics modelled by means of the linearised Bhatnagar–Gross–Krook equation is discussed in [254].

2.5 Plasma Physics and Magnetohydrodynamics

Computational physics community is showing increasing interest towards the application of hybrid discretisation methods to the simulation of magnetic plasma physics. Promising preliminary results concerning the HDG approximation of the Grad–Shafranov equation in axisymmetric confinement devices modelling fusion reactors are described in [233, 234]. In the context of magnetohydrodynamics (MHD), an HDG method for steady-state linearised incompressible MHD equations is proposed in [180]. Approximation strategies for the unsteady compressible MHD equations using HDG, EDG and the interior embedded DG (IEDG) methods with DIRK time integrators are explored in [74].

2.6 Shallow Water Equations

The shallow water equations have been extensively studied in the context of hybrid DG methods, starting from the linearised shallow water system in [38] to the nonlinear Korteweg-de Vries equation in [63, 231]. In both the above mentioned works, time integration is performed implicitly using a backward Euler method. Extension to high-order backward differentiation formulas is discussed in [128] in the context of the Benjamin–Bona–Mahony equation. To reduce the computational cost of fully-implicit procedures, in [228] an operator splitting is applied to the Green–Naghdi equation and the nonlinear hyperbolic subproblem is solved using an explicit approach, whereas the implicit time integrator is only applied to the linear dispersive subproblem. A similar idea is presented in [169] to devise an implicit–explicit (IMEX) HDG-DG scheme in which the linear part of the problem is solved using a hybridised DG method and a singly diagonally implicit Runge–Kutta (SDIRK) scheme and the nonlinear one is approximated by means of an explicit Runge–Kutta (RK) DG discretisation. A detailed comparison of explicit and implicit approaches to the nonlinear shallow water equations is provided in [229].

2.7 Wave Propagation Phenomena

The benefits of high-order methods in the simulation of wave propagation prompted extensive research on hybrid discretisation methods in the fields of electromagnetics, elastodynamics and acoustics. A detailed review on HDG and EDG approaches for these problems is available in [132].

Starting from the work in [203], research on time-harmonic Maxwell’s equations tackled the analysis and development of HDG formulations [186], including methods suitable for simulations at large wave numbers [189] and Schwarz-type domain decomposition (DD) strategies designed for HDG [18, 185]. Recent applications of HDG to time-harmonic Maxwell’s equations focus on wave propagation in heterogeneous media modelling photovoltaic cells [41], coupling with nonlocal hydrodynamic Drude and generalised nonlocal optical response models [184] and with hydrodynamic models for metals [257,258,259, 271] to simulate plasmonic nanostructures. In the context of time-domain Maxwell’s equations, HDG methods are presented and analysed in [55, 59, 122], whereas implicit hybridised DG discretisations are proposed in [67].

In the framework of elastodynamics, HDG with DIRK time integrators were introduced in [202], whereas in [256] an HDG spectral element method (HDG-SEM) is utilised to simulate wave propagation in coupled elastic-acoustic media. In the frequency-domain, HDG methods for elastodynamics are analysed and presented in [28, 164].

The first HDG solver for acoustics, introduced in [202], relied on a fully-implicit approach based on DIRK time integrators. Since then, explicit HDG formulations utilising strong stability-preserving RK (SSPRK) and explicit RK integrators were proposed in [252], wheras an explicit arbitrary derivative (ADER) approach is discussed in [236]. In addition, a comparison of implicit and explicit HDG schemes for acoustic wave propagation is performed in [173]. More recently, an HDG-based cut finite element startegy with local time stepping was presented in [237]. It is worth recalling that devising a conservative numerical scheme is a critical aspect for the accurate simulation of acoustic wave propagation. To correct the dissipative nature of the method analysed in [93], an energy-conservative HDG formulation with a two-step Stormer-Numerov time-marching is proposed in [86]. Moreover, symplectic [232] and multisymplectic [190] HDG schemes preserving the Hamiltonian structure of the PDEs under analysis were developed to achieve energy conservation.

Among the applications of HDG to wave propagation phenomena, it is also worth mentioning the degree adaptive approximation of the mild slope equation to perform harbour simulations [152] and the cardiac electrophysiology simulations of the monodomain model [159, 227].

2.8 Linear and Nonlinear Elasticity

In linear elasticity, the imposition of the symmetry of the stress tensor using HDG methods based on mixed formulations has been extensively studied in the literature. Indeed, the first formulations introduced in [141, 251] experienced suboptimal convergence of the mixed variable and a loss of superconvergence of the postprocessed displacement field. To remedy this issue, a formulation considering a weakly symmetric stress tensor was presented in [97]. The strong imposition of the symmetry can be achieved via several strategies: in [214], different degrees of polynomial approximation are considered for the primal and hybrid variables; the M-decomposition framework [82, 83, 85] is applied to the linear elastic problem [81] to enrich the discrete spaces of approximation utilised in the local problem; an alternative formulation imposing the symmetry of the mixed variable pointwise via Voigt notation is proposed for high-order and the lowest-order HDG discretisations in [245] and [244], respectively. In the context of the high-order discretisation, a novel postprocessing strategy accounting for rigid translation and rotation is also devised. It is worth noting that hybrid methods relying on primal formulations do not suffer from these issues, see e.g. HHO [109].

Timoshenko beams are discussed in [47, 48], whereas the case of Kirchhoff plates is considered in [162] using HDG and in [27] using HHO methods.

In the context of nonlinear elasticity, the first hybrid discretisation formulation was presented in [167]. In this work, it was observed that the method may not converge to the exact solution if the interelement jumps are not appropriately penalised and a detailed numerical study on the choice of the HDG stabilisation is discussed in [96]. More recently, a locking-free HDG formulation for nonlinear elasticity of thin structures subject to large deformations was proposed [255]. In addition, HHO discretisations of hyperelastic materials in small and finite deformations were presented in [34] and [15], respectively. HHO discretisations for problems involving plastic and elastoplatic simulations are discussed in [16, 17], whereas contact phenomena are addressed in [66].

2.9 Interface Problems and Immersed Discretisations

The first attempt to solve interface problems using hybrid discretisation techniques was proposed in [165] using a body-fitted mesh. In this context, a superparametric HDG formulation was considered to limit the geometric error due to the polygonal approximation of curved interfaces.

Recently, immersed methods have received special attention, both in the context of HHO and HDG formulations. More precisely, unfitted HHO methods relying on a cell agglomeration procedure to remedy small cut instabilities are analysed for scalar and vectorial second-order elliptic problems in [39, 40]. In the framework of HDG, Poisson interface problems are treated in [121] by means of an unfitted method introducing appropriately defined ansatz functions in the vicinity of the interface. An alternative approach to handle curved interfaces is proposed in [217] where a fictitious domain strategy is developed coupling a mesh of planar faces and a transferring function for the imposition of the transmission conditions on the fictitious subdomain. Inspired by the cut finite element method, in [237], a high-order HDG strategy employing a level-set function to describe the immersed interfaces and a cell agglomeration procedure is described for the wave equation. Similarly, the extended HDG (X-HDG) method introduces a framework in which the HDG local problem is modified only in the elements cut by the interface. In this context, cut instabilities are handled by displacing the mesh nodes responsible for the bad cuts [154,155,156]. Finally, an HDG-based phase-field model for brittle fracture was recently proposed in [194].

2.10 High-Order and Exact Geometry Representations

Geometry representation plays a crucial role in the capability of high-order methods to achieve optimal accuracy. In the context of HDG, high-order isoparametric approaches in presence of curved meshes are utilised in many references, see e.g. [148, 193], whereas this technique is addressed for HHO in [29]. An alternative approach relying on meshes with planar faces and the extension to a fictitious subdomain is discussed in [101, 102, 250] for several linear problems and was recently extended to the semilinear Grad-Shafranov equation [233, 234]. It is worth noting that all the techniques mentioned above introduce geometric errors due to the polynomial approximation of the boundaries. In order to exploit the exact CAD representation of the boundaries, the NURBS-enhanced finite element method (NEFEM) [241, 242] is employed in [149, 239, 247] to devise HDG formulations with exact geometry for Stokes, linear elastic and electrostatics problems.

2.11 Lowest-Order Hybrid Discretisations

Hybrid discretisation methods have been traditionally developed in the context of high-order approximations. Nonetheless, it is well-known that lowest-order discretisations, e.g. the finite volume (FV) method, are more robust than high-order techniques. In this framework, a new class of lowest-order hybrid discretisations was developed, with unknowns approximated by means of constant functions on the mesh faces. The recently proposed face-centred finite volume (FCFV) for Poisson, Stokes [243] and linear elasticity [244] can be interpreted as an HDG method of degree zero. Variants of this approach achieving optimal second-order convergence of the primal variable are discussed in [150, 262]. Stemming from HHO, lowest-order nonconforming discretisations are proposed in [32] for linear elasticity and in [72] for elliptic obstacle problems. As their high-order counterparts, the above mentioned methodologies allow the use of generic polygonal and polyhedral elements and provide a workaround to the sensitivity issues of FV methods to mesh distortion and stretching [118, 119].

2.12 Iterative Solvers and Preconditioning

Although hybrid discretisation methods are responsible for a substantial reduction of degrees of freedom with respect to classical DG methods, their applicability to realistic problems of engineering interest still rely on the development of efficient solution strategies for large-scale systems.

In [26], a DD strategy based on restricted additive Schwarz methods is proposed for hybridised DG approximations, whereas an optimised Schwarz DD approach suitable to handle the many-subdomain case is discussed in [144]. Starting from [80], several works also explored the capabilities of multigrid solvers for HDG formulations, including hierarchical scale separation [238], geometric multigrid [265], nested geometric multigrid on many-core processors [129], p-multigrid in the context of second-order elliptic problems [174] and compressible Navier–Stokes flows [139] and GPU-accelerated p-multigrid for linear elasticity [127]. Finally, iterative algorithms inspired by the Gauss-Seidel method were proposed in [196] and tested on massively parallel architectures up to 16,384 cores. A block symmetric Gauss-Seidel type preconditioner was also introduced in [224], whereas a multilevel solver coupled with a block-Jacobi fine scale solver is proposed in [195].

2.13 A Posteriori Error Estimates and Adaptivity

The quality of hybrid discretisation methods has been assessed in several works by means of a posteriori estimates of the error in the primal, mixed and hybrid variables, as well as in quantities of interest.

Starting from the seminal works [104, 105] establishing reliability and efficiency of error estimates for the HDG approximations of second-order elliptic equations, a posteriori estimates were developed for steady and unsteady scalar convection–diffusion problems [57, 182] and for the vectorial case of incompressible Oseen [23] and Brinkman [22] flows. In addition, constant-free computable a posteriori error estimates are devised in [19] for second-order elliptic problems using an equilibrated fluxes approach, whereas residual-based estimates are established for Maxwell’s equations in [58].

In the context of adaptivity, on the one hand, the analysis of HDG approximations based on non-uniform polynomial degrees [60, 61] and the superconvergence property of the postprocessed solution [77, 89] prompted the development of degree adaptive procedures based on superparametric HDG methods [152, 153] and on isoparametric HDG-NEFEM approaches [149, 239, 247]. Degree adaptivity is also applied to the simulation of cardiac electrophysiology in [159]. On the other hand, mesh adaptivity procedures to capture localised abrupt changes in the solution were devised in [234] and [194] for the Grad-Shafranov equation and the phase-field model for brittle fracture, respectively. Octree-based mesh refinement is performed in [230] for anisotropic inhomogeneous diffusion problems. Mesh adaptivity driven by local error indicators is also employed in the context of second-order FCFV approximations [150, 262]. Concerning the error in quantities of interest, an adjoint-based method allowing to achieve superconvergent approximations of linear functionals is described in [103] and goal-oriented mesh adaptation strategies are proposed in [136, 268].

2.14 Coupling HDG with Other Numerical Methods

The accuracy of high-order HDG approximations has been recently exploited to develop efficient algorithms coupling different numerical methodologies in different regions of the computational domain.

In [172], a strategy coupling HDG and a vertex-centred finite volume method is proposed to simulate transient inviscid flows using coarse meshes designed for steady-state problems. In addition, different couplings of HDG and continuous Galerkin (CG) discretisations were explored in the literature. A strategy inspired by a non-overlapping DD method is presented in [208] in the context of incompressible Navier–Stokes flows coupled with conjugate heat transfer phenomena. An alternative minimally-intrusive coupling based on a Nitsche’s formulation of the CG method was first introduced in [175] for linear elastic problems involving nearly incompressible materials and was extended to FSI problems with weakly compressible flows in [176].

2.15 HDG-Based Reduced Order Models

In recent years, the accuracy of the HDG method and its flexibility to devise high-order adaptive discretisation have been also employed to devise high-fidelity reduced and surrogate models. In [260, 261], a reduced order model to accelerate the Monte-Carlo simulation of stochastic elliptic PDEs is constructed coupling a high-order HDG method with a reduced basis and empirical interpolation approach. The combination of an HDG solver for time-harmonic Maxwell’s equations and a proper orthogonal decomposition (POD) strategy to design parametrised plasmonic nanogap structures is proposed in [259]. An HGD-POD reduced order model (ROM) is also discussed in [249] for the fast simulation of the unsteady heat equation. More recently, an a priori ROM based on HDG and the proper generalised decomposition was proposed to simulate Stokes flows in geometrically parametrised domains [147, 240].

2.16 Availability of Open-Source Implementations of Hybrid Discretisation Methods

The success of hybrid discretisation methods led to the development of targeted open-source libraries and to their implementation in existing finite element libraries available open-source. To the best of the authors’ knowledge, the hybridised DG method based on primal formulations is available in the following libraries:

whereas the libraries

provide implementations of the HDG method based on mixed formulations. Finally, the HHO method is available in

  • code_aster [1, 211]

  • Code_Saturne [2, 137]

  • DiSk++ [3, 71]

  • GetFEM [7, 221]

  • HArDCore [8]

All above mentioned libraries rely on either Fortran or C/C++ implementations, whereas open-source libraries implementing HDG in MATLAB include:

3 HDG Formulation of the Poisson Equation

In this section, the formulation of the HDG method for the Poisson equation is briefly recalled. Special attention is devoted to the identification of the building blocks of the numerical scheme whose implementation will be detailed in Sect. 8. Interested readers are referred to [89] for a complete theoretical introduction to the HDG method for Poisson equation and to [246] for a tutorial on its derivation.

Let \({\varOmega }\subset {\mathbb {R}}^{{\texttt {n}}_{\texttt {sd}}}\) be an open bounded domain in \({\texttt {n}}_{\texttt {sd}}\) spatial dimensions such that its boundary is \(\partial {\varOmega } = {\varGamma }_{_{ D}} \cup {\varGamma }_{_{ N}}\) and \({\varGamma }_{_{ D}} \cap {\varGamma }_{_{ N}} = \emptyset\). The strong form of the Poisson equation is

$$\begin{aligned} \left\{ \begin{array}{ll} -{\varvec{\nabla }}{\cdot } (\kappa {\varvec{\nabla }}u)= s &{\text {in }}\,\,{\varOmega }, \\ u = u_{_D}& {\text {on }}\,\,{\varGamma }_{_{ D}}, \\ {\varvec{n}}{\cdot } \kappa {\varvec{\nabla }}u =g& {\text {on }}\,\,{\varGamma }_{_{ N}}, \end{array} \right. \end{aligned}$$
(1)

where the unknown u represents the solution field, \(\kappa\) denotes the material parameter (e.g. conductivity in a thermal problem) and s is a volumetric source term. On the boundary, Dirichlet, \(u_{_D}\), and Neumann, g, data prescribe the values of the unknown and its flux on \({\varGamma }_{_{ D}}\) and \({\varGamma }_{_{ N}}\), respectively. The vector \({\varvec{n}}\) denotes the outward unit normal vector to the boundary.

3.1 HDG Local and Global Problems: Strong Form

Consider a partition of \({\varOmega }\) in \({\texttt {n}}_{\texttt {el}}\) disjoint subdomains such that

$$\begin{aligned} {\varOmega }= \bigcup _{e=1}^{{\texttt {n}}_{\texttt {el}}} {\varOmega }_e, \quad {\varOmega }_i \cap {\varOmega }_j = \emptyset \,\text { for }\,\, i \ne j \end{aligned}$$

and define the mesh skeleton as

$$\begin{aligned} {\varGamma }:= \left[ \bigcup _{e=1}^{{\texttt {n}}_{\texttt {el}}} \partial {\varOmega }_e \right] {\setminus} \partial {\varOmega }. \end{aligned}$$

Following the HDG rationale [89, 92, 200, 201, 204, 246], a mixed variable \({\varvec{q}} = - \sqrt{\kappa } {\varvec{\nabla }}u\) is introduced and problem (1) is rewritten as a system of first-order equations element-by-element, that is,

$$\begin{aligned} \left\{ \begin{array}{ll} {\varvec{q}}+ \sqrt{\kappa } {\varvec{\nabla }}u = {\varvec{0}} & {\text {in }}\,\,{\varOmega }_e, \, e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}, \\ {\varvec{\nabla }}{\cdot } (\sqrt{\kappa } {\varvec{q}}) = s & {\text {in }}\,\,{\varOmega }_e, \, e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}, \\ u = u_{_D}& {\text {on }}\,\,{\varGamma }_{_{ D}}, \\ {\varvec{n}}{\cdot } \sqrt{\kappa }{\varvec{q}}= -g & {\text {on }}\,\,{\varGamma }_{_{ N}}, \\ \llbracket u {\varvec{n}}\rrbracket = {\varvec{0}} & {\text {on }}\,\,{\varGamma }, \\ \llbracket {\varvec{n}}{\cdot } \sqrt{\kappa } {\varvec{q}}\rrbracket = 0 &{\text {on }}\,\,{\varGamma }, \end{array} \right. \end{aligned}$$
(2)

where the jump operator \(\llbracket \cdot \rrbracket\) is defined as

$$\llbracket \odot \rrbracket := \odot _i + \odot _j ,$$

being \(\odot _i\) and \(\odot _j\) the evaluations of the quantity \(\odot\) in two neighbouring elements \({\varOmega }_i\) and \({\varOmega }_j\) sharing a given interface [191]. The last two conditions in (2), known as transmission conditions, enforce the continuity of the solution and of its normal flux across the internal mesh skeleton \({\varGamma }\).

The HDG algorithm solves Eq. (2) in two stages. First, an independent hybrid variable \({\hat{u}}\) is introduced to represent the trace of the solution on \(\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}}\) and the primal and mixed variables \((u_e,{\varvec{q}}_e)\) in each element \({\varOmega }_e, \, e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\) are expressed as functions of the unknown \({\hat{u}}\), namely

$$\begin{aligned} \left\{ \begin{array}{ll} {\varvec{q}}_e + \sqrt{\kappa } {\varvec{\nabla }}u_e = {\varvec{0}} & {\text {in }}\,\,{\varOmega }_e, \, e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}, \\ {\varvec{\nabla }}{\cdot } (\sqrt{\kappa } {\varvec{q}}_e ) = s & {\text {in }}\,\,{\varOmega }_e, \, e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}, \\ u_e = u_{_D}& {\text {on }}\,\,\partial {\varOmega }_e \cap {\varGamma }_{_{ D}}, \\ u_e = {\hat{u}}& {\text {on }}\,\,\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}}. \end{array} \right. \end{aligned}$$
(3)

Remark 1

Equation (3) represents the \({\texttt {n}}_{\texttt {el}}\) HDG local problems. This stage corresponds to the hybridisation of the mixed problem, see [138], and is equivalent to the static condensation procedure in classical continuous Galerkin methods [157].

Second, the hybrid variable is computed by solving the HDG global problem, which accounts for the transmission conditions on the mesh skeleton \({\varGamma }\) and the Neumann boundary condition on \({\varGamma }_{_{ N}}\), that is,

$$\begin{aligned} \left\{ \begin{array}{ll} \llbracket u {\varvec{n}}\rrbracket = {\varvec{0}}&{\text {on }}\,\,{\varGamma }, \\ \llbracket {\varvec{n}}{\cdot } \sqrt{\kappa } {\varvec{q}}\rrbracket = 0&{\text {on }}\,\,{\varGamma }, \\ {\varvec{n}}{\cdot } \sqrt{\kappa } {\varvec{q}} = -g&{\text {on }}\,\,{\varGamma }_{_{ N}}. \\ \end{array} \right. \end{aligned}$$
(4)

Remark 2

The first condition is automatically fulfilled owing to the Dirichlet boundary condition \(u_e = {\hat{u}}\) on \(\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}}\) imposed in the local problem and to the uniqueness of the hybrid variable on each mesh face (respectively, edge in 2D).

The solution \((u_e,{\varvec{q}}_e)\) in each element \({\varOmega }_e, \, e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\) is thus efficiently retrieved by solving \({\texttt {n}}_{\texttt {el}}\) independent problems, see Eq. (3), element-by-element.

3.2 HDG Local and Global Problems: Weak Form

Following the rationale introduced in [246], the discrete functional spaces

$$\begin{aligned} {\mathcal {V}}^h({\varOmega })&\,{:=}\, \lbrace v \in {\mathcal {L}}_2({\varOmega }) : \, v\vert _{{\varOmega }_e} \in {\mathcal {P}}^{{\texttt {p}}}({\varOmega }_e) \\&\forall {\varOmega }_e, \, e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\rbrace , \end{aligned}$$
(5a)
$$\begin{aligned} \widehat{{\mathcal {V}}}^h(S)&\,{:=}\, \lbrace {\hat{v}}\in {\mathcal {L}}_2(S) : \, {\hat{v}}\vert _{{\varGamma }_i} \in {\mathcal {P}}^{{\texttt {p}}}({\varGamma }_i) \\&\forall {\varGamma }_i \subset S \subseteq {\varGamma }\cup \partial {\varOmega }\rbrace , \end{aligned}$$
(5b)

are defined for the approximation of the element-based and face-based variables, respectively. In (5), \({\mathcal {P}}^{{\texttt {p}}}({\varOmega }_e)\) and \({\mathcal {P}}^{{\texttt {p}}}({\varGamma }_i)\) stand for the spaces of polynomial functions of complete degree at most \({\texttt {p}}\) in \({\varOmega }_e\) and on \({\varGamma }_i\), respectively.

For \(e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\), the weak form of the HDG local problem is: given \(u_{_D}\) on \({\varGamma }_{_{ D}}\) and \({\hat{u}}^h\) on \({\varGamma }\cup {\varGamma }_{_{ N}}\), find \((u_e^h,{\varvec{q}}_e^h) \in {\mathcal {V}}^h({\varOmega }_e) {\times } \left[ {\mathcal {V}}^h({\varOmega }_e)\right] ^{{\texttt {n}}_{\texttt {sd}}}\) that satisfy

$$\begin{aligned}&\begin{aligned} -&({\varvec{w}}, {\varvec{q}}_e^h)_{{\varOmega }_e} + ({\varvec{\nabla }}{\cdot } (\sqrt{\kappa } {\varvec{w}}), u_e^h)_{{\varOmega }_e} \\&= \langle {\varvec{n}}{\cdot } \sqrt{\kappa }{\varvec{w}}, u_{_D}\rangle _{\partial {\varOmega }_e\cap {\varGamma }_{_{ D}}} + \langle {\varvec{n}}{\cdot } \sqrt{\kappa }{\varvec{w}}, {\hat{u}}^h \rangle _{\partial {\varOmega }_e{\setminus} {\varGamma }_{_{ D}}} , \end{aligned} \end{aligned}$$
(6a)
$$\begin{aligned}&\begin{aligned} (v&, {\varvec{\nabla }}{\cdot } (\sqrt{\kappa } {\varvec{q}}_e^h))_{{\varOmega }_e} + \langle v, \tau u_e^h \rangle _{\partial {\varOmega }_e} \\&= (v, s)_{{\varOmega }_e} + \langle v, \tau u_{_D}\rangle _{\partial {\varOmega }_e\cap {\varGamma }_{_{ D}}} + \langle v, \tau {\hat{u}}^h \rangle _{\partial {\varOmega }_e{\setminus} {\varGamma }_{_{ D}}} , \end{aligned} \end{aligned}$$
(6b)

for all \((v,{\varvec{w}}) \in {\mathcal {V}}^h({\varOmega }_e) {\times } \left[ {\mathcal {V}}^h({\varOmega }_e)\right] ^{{\texttt {n}}_{\texttt {sd}}}\), where \((\cdot ,\cdot )_D\) and \(\langle \cdot ,\cdot \rangle _S\) denote the \({\mathcal {L}}_2\) inner products on a generic subdomain \(D \subset {\varOmega }\) and \(S \subset {\varGamma }\cup \partial {\varOmega }\), respectively.

Remark 3

In Eq. (6), \(\tau\) represents a stabilisation parameter influencing accuracy, stability and convergence of the HDG method [89, 92, 200, 201, 204].

Similarly, the weak form of the HDG global problem is: find \({\hat{u}}^h \in \widehat{{\mathcal {V}}}^h({\varGamma }\cup {\varGamma }_{_{ N}})\) that satisfies

$$\begin{aligned} \begin{aligned}&\sum _{e=1}^{{\texttt {n}}_{\texttt {el}}} \Bigl \{ \langle {\hat{v}}, {\varvec{n}}{\cdot } \sqrt{\kappa }{\varvec{q}}_e^h \rangle _{\partial {\varOmega }_e{\setminus} {\varGamma }_{_{ D}}} + \langle {\hat{v}}, \tau u_e^h \rangle _{\partial {\varOmega }_e{\setminus} {\varGamma }_{_{ D}}} \Bigr . \\&\quad \Bigl . - \langle {\hat{v}}, \tau {\hat{u}}^h \rangle _{\partial {\varOmega }_e{\setminus} {\varGamma }_{_{ D}}} \Bigr \} = - \sum _{e=1}^{{\texttt {n}}_{\texttt {el}}} \langle {\hat{v}}, g \rangle _{\partial {\varOmega }_e\cap {\varGamma }_{_{ N}}} , \end{aligned} \end{aligned}$$
(7)

for all \({\hat{v}}\in \widehat{{\mathcal {V}}}^h({\varGamma }\cup {\varGamma }_{_{ N}})\).

3.3 HDG Local and Global Problems: Discrete Form

An isoparametric formulation is considered for the primal, mixed and hybrid variables in the discrete spaces (5), that is,

$$\begin{aligned} u^h = \sum _{i=1}^{{\texttt {n}}_{\texttt {en}}} N_i {\mathrm {u}}_i , \quad {\varvec{q}}^h = \sum _{i=1}^{{\texttt {n}}_{\texttt {en}}} N_i {\mathbf {q}}_i , \quad {\hat{u}}^h = \sum _{i=1}^{{\texttt {n}}_{\texttt {fn}}} {\hat{N}}_i \hat{{\mathrm {u}}}_i , \end{aligned}$$
(8)

where \({\mathrm {u}}_i\), \({\mathbf {q}}_i\) and \(\hat{{\mathrm {u}}}_i\) are the nodal values of the unknowns, \(N_i\) and \({\hat{N}}_i\) are the polynomial shape functions of degree \({\texttt {p}}\) defined in a reference element and on a reference face, respectively and \({\texttt {n}}_{\texttt {en}}\) and \({\texttt {n}}_{\texttt {fn}}\) denote the number of nodes per element and per face, respectively.

Hence, for each element \({\varOmega }_e, \ e=1,\ldots ,{\texttt {n}}_{\texttt {el}}\), the local problem (6) leads to the linear system of equations

$$\begin{aligned} \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{uu} & {\mathbf {A}}_{uq} \\ {\mathbf {A}}_{uq}^T & {\mathbf {A}}_{qq} \end{array} } \right] _{ e} \left\{ {\begin{array}{*{20}l} {\mathbf {u}} \\ {\mathbf {q}} \end{array} } \right\}_{ e} = \left\{ {\begin{array}{*{20}l} {\mathbf {f}}_{u} \\ {\mathbf {f}}_{q} \end{array} } \right\}_{ e} + \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{u{\hat{u}}} \\ {\mathbf {A}}_{q{\hat{u}}} \end{array} } \right] _{ e} \widehat{\mathbf{u}}_e , \end{aligned}$$
(9)

from which the following solution is computed

$$\begin{aligned} \left\{ {\begin{array}{*{20}l} {\mathbf {u}} \\ {\mathbf {q}} \end{array} } \right\}_{ e} = \left\{ {\begin{array}{*{20}l} {{\mathbf {z}}}_u^f \\ {{\mathbf {z}}}_q^f \end{array} } \right\}_{ e} + \left[ {\begin{array}{*{20}l} {\mathbf {Z}}_{u{\hat{u}}} \\ {\mathbf {Z}}_{q{\hat{u}}} \end{array} } \right] _{ e} \widehat{\mathbf{u}}_e , \end{aligned}$$
(10a)

with the matrices

$$\begin{aligned} \left[ {\begin{array}{*{20}l} {\mathbf {Z}}_{u{\hat{u}}} \\ {\mathbf {Z}}_{q{\hat{u}}} \end{array} } \right] _{ e} \,{:=}\, \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{uu} & {\mathbf {A}}_{uq} \\ {\mathbf {A}}_{uq}^T & {\mathbf {A}}_{qq} \end{array} } \right] _{ e}^{-1} \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{u{\hat{u}}} \\ {\mathbf {A}}_{q{\hat{u}}} \end{array} } \right] _{ e} \end{aligned}$$
(10b)

and the vectors

$$\begin{aligned} \left\{ {\begin{array}{*{20}l} {{\mathbf {z}}}_u^f \\ {{\mathbf {z}}}_q^f \end{array} } \right\}_{ e} \,{:=}\, \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{uu} & {\mathbf {A}}_{uq} \\ {\mathbf {A}}_{uq}^T & {\mathbf {A}}_{qq} \end{array} } \right] _{ e}^{-1} \left\{ {\begin{array}{*{20}l} {\mathbf {f}}_{u} \\ {\mathbf {f}}_{q} \end{array} } \right\}_{ e} . \end{aligned}$$
(10c)

The corresponding discretisation of the global problem (7) leads to

$$\begin{aligned} \sum _{e=1}^{{\texttt {n}}_{\texttt {el}}}\Big \{ \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{u {\hat{u}}}^T&{\mathbf {A}}_{q {\hat{u}}}^T \end{array} } \right] _{ e} \left\{ {\begin{array}{*{20}l} {\mathbf {u}} \\ {\mathbf {q}} \end{array} } \right\}_{ e} + [{\mathbf {A}}_{{\hat{u}}{\hat{u}}}]_e \, \widehat{\mathbf{u}}_e \Big \} = \sum _{e=1}^{{\texttt {n}}_{\texttt {el}}} [{\mathbf {f}}_{{\hat{u}}}]_e . \end{aligned}$$
(11)

By plugging the local elemental solution (10a) into (11), the HDG problem

$$\begin{aligned} {\mathbf {K}} \widehat{\mathbf{u}} = \widehat{\mathbf{f}} \end{aligned}$$

involving only the globally-coupled degrees of freedom is obtained, where the matrix and the right-hand side vector are obtained by assembling the elemental contributions

$$\begin{aligned} \widehat{{\mathbf {K}}}^e&:= \bigl [\begin{array}{ll} {\mathbf {A}}_{u {\hat{u}}}^T&{\mathbf {A}}_{q {\hat{u}}}^T \end{array}\bigr ]_{ e} \left[ {\begin{array}{*{20}l} {\mathbf {Z}}_{u{\hat{u}}} \\ {\mathbf {Z}}_{q{\hat{u}}} \end{array} } \right] _{ e} + [{\mathbf {A}}_{{\hat{u}}{\hat{u}}}]_e , \end{aligned}$$
(12a)
$$\begin{aligned} \widehat{\mathbf{f}}^{e}&:= [{\mathbf {f}}_{{\hat{u}}}]_e -\bigl [\begin{array}{ll} {\mathbf {A}}_{u {\hat{u}}}^T&{\mathbf {A}}_{q {\hat{u}}}^T \end{array}\bigr ]_{ e} \left\{ {\begin{array}{*{20}l} {{\mathbf {z}}}_u^f \\ {{\mathbf {z}}}_q^f \end{array} } \right\}_{ e} . \end{aligned}$$
(12b)

The expressions of the matrices and vectors introduced in this section are detailed in Appendix A.

3.4 HDG Local Postprocess

Introduce the discrete functional space

$$\begin{aligned} {\mathcal {V}}_{\star }^h({\varOmega }) \,{:=}\,&\lbrace v \in {\mathcal {L}}_2({\varOmega }) : \, v\vert _{{\varOmega }_e} \in {\mathcal {P}}^{{\texttt {p}}+1}({\varOmega }_e) \\&\forall {\varOmega }_e, \, e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\rbrace , \end{aligned}$$
(13)

where \({\mathcal {P}}^{{\texttt {p}}+1}({\varOmega }_e)\) denotes the space of polynomial functions of complete degree at most \({\texttt {p}} + 1\) in each element \({\varOmega }_e\).

The HDG postprocess procedure allows to compute a superconvergent approximation \(u_{\star }\) of the primal variable by solving an independent local problem in each element, namely

$$\begin{aligned} \left\{ \begin{array}{ll} - {\varvec{\nabla }}{\cdot } (\kappa {\varvec{\nabla }}u_{\star })= {\varvec{\nabla }}{\cdot } (\sqrt{\kappa } {\varvec{q}}_e)&{\text {in }}\,\,{\varOmega }_e, \\ {\varvec{n}}{\cdot } \kappa {\varvec{\nabla }}u_{\star }= - {\varvec{n}}{\cdot } \sqrt{\kappa } {\varvec{q}}_e&{\text {on }}\,\,\partial {\varOmega }_e, \end{array} \right. \end{aligned}$$
(14)

with the constraint

$$\begin{aligned} (u_{\star }, 1)_{{\varOmega }_e} = (u_e, 1)_{{\varOmega }_e} \end{aligned}$$
(15)

on the mean value of the solution in the element.

For each element \({\varOmega }_e, \ e=1,\ldots ,{\texttt {n}}_{\texttt {el}}\), the weak form of the postprocess procedure is: find \(u_{\star }^h \in {\mathcal {V}}_{\star }^h({\varOmega }_e)\) that satisfies

$$\begin{aligned} \left( {\varvec{\nabla }}v_{\star }, \kappa {\varvec{\nabla }}u_{\star }^h\right) _{{\varOmega }_e}&= - \left( {\varvec{\nabla }}v_{\star }, \sqrt{\kappa } {\varvec{q}}_e^h\right) _{{\varOmega }_e} , \end{aligned}$$
(16a)
$$\begin{aligned} (u_{\star }^h, 1)_{{\varOmega }_e}&= (u_e^h, 1)_{{\varOmega }_e} , \end{aligned}$$
(16b)

for all \(v_{\star }\in {\mathcal {V}}_{\star }^h({\varOmega }_e)\).

Using an isoparametric approximation for the functions in the space \({\mathcal {V}}_{\star }^h({\varOmega })\), the HDG local postprocess gives rise to the linear system

$$\begin{aligned} \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{\star \star } & {\mathbf {a}}_{\star \lambda } \\ {\mathbf {a}}_{\star \lambda }^T & 0 \end{array} } \right] _{ e} \left\{ {\begin{array}{*{20}l} {\mathbf {u}}_{\star } \\ {\lambda } \end{array} } \right\}_{ e} = \left[ {\begin{array}{*{20}l} {{\mathbf {0}}} & {\mathbf {A}}_{\star q} \\ {\mathbf {a}}_{\star \lambda }^T & {{\mathbf {0}}} \end{array} } \right] _{ e} \left\{ {\begin{array}{*{20}l} {\mathcal {I}}^{\star } {\mathbf {u}} \\ {\mathcal {I}}_{{\texttt {n}}_{\texttt {sd}}}^{\star } {\mathbf {q}} \end{array} } \right\}_{ e} , \end{aligned}$$
(17)

where the saddle-point structure of the problem follows from the imposition of the constraint (16b) via the Lagrange multiplier \(\lambda\) and \({\mathcal {I}}^{\star }: {\mathcal {V}}^h\rightarrow {\mathcal {V}}_{\star }^h\) and \({\mathcal {I}}_{{\texttt {n}}_{\texttt {sd}}}^{\star }: [{\mathcal {V}}^h]^{{\texttt {n}}_{\texttt {sd}}} \rightarrow [{\mathcal {V}}_{\star }^h]^{{\texttt {n}}_{\texttt {sd}}}\) denote the interpolation operators from the spaces of polynomial functions of degree \({\texttt {p}}\) to the ones of degree \({\texttt {p}} + 1\) for scalar and vector-valued functions.

The expressions of the matrices and vectors introduced in this section are detailed in Appendix A.

4 HDG Formulation of the Stokes Equations

This section presents the formulation of the HDG method for the Stokes equations, extending the framework previously introduced for the Poisson equation. For the sake of simplicity, the present work focuses on the velocity-pressure formulation of the Stokes equations. For the Cauchy stress tensor formulation, a tutorial to devise an HDG method based on equal order approximation for all the variables and pointwise symmetric mixed variable is presented in [151].

The open bounded domain \({\varOmega }\subset {\mathbb {R}}^{{\texttt {n}}_{\texttt {sd}}}\) is characterised now by a boundary partitioned in three portions disjoint by pairs such that \(\partial {\varOmega } = {\varGamma }_{_{ D}} \cup {\varGamma }_{_{ N}} \cup {\varGamma }_{_{ S}}\), where Dirichlet, Neumann and slip conditions are imposed. The strong form of the Stokes equations is

$$\begin{aligned} \left\{ \begin{array}{ll} -{\varvec{\nabla }}{\cdot } (\nu {\varvec{\nabla }}{\varvec{u}}- p {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}) = {\varvec{s}}& {\text {in }}\,\,{\varOmega },\\ {\varvec{\nabla }}{\cdot } {\varvec{u}} = 0&{\text {in }}\,\,{\varOmega },\\ {\varvec{u}} = {\varvec{u}}_{\!_D}&{\text {on }}\,\,{\varGamma }_{_{ D}},\\ {\varvec{n}}{\cdot } (\nu {\varvec{\nabla }}{\varvec{u}}- p {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}) = {\varvec{g}}&{\text {on }}{\varGamma }_{_{ N}},\\ {\varvec{u}}{\cdot } {\varvec{D}}+ {\varvec{n}}{\cdot } \bigl (\nu {\varvec{\nabla }}{\varvec{u}}-p{\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr ){\varvec{E}} = {\varvec{0}}&{\text {on }}\,\,{\varGamma }_{_{ S}},\\ \end{array}\right. \end{aligned}$$
(18)

where the pair \(({\varvec{u}},p)\) denotes the unknown velocity and pressure fields, \(\nu >0\) is the kinematic viscosity, \({\varvec{n}}\) is the outward unit normal to the boundary, \({\varvec{s}}\) is the vector of the body forces and \({\varvec{u}}_{\!_D}\) and \({\varvec{g}}\) represent the imposed velocity and pseudo-traction on the Dirichlet and Neumann boundaries, respectively. On the slip boundary, matrices \({\varvec{D}}\) and \({\varvec{E}}\) are defined as \({\varvec{D}}\,{:=}\, [ {\varvec{n}},\beta {\varvec{t}}_1 , \ldots , \beta {\varvec{t}}_{{\texttt {n}}_{\texttt {sd}}-1} ]\) and \({\varvec{E}}\,{:=}\, [ \alpha {\varvec{n}},{\varvec{t}}_1 , \ldots ,{\varvec{t}}_{{\texttt {n}}_{\texttt {sd}}-1} ]\), the tangential vectors \({\varvec{t}}_j\), \(j = 1, \ldots , {\texttt {n}}_{\texttt {sd}}- 1\) being such that \(\{ {\varvec{n}},{\varvec{t}}_1, \ldots , {\varvec{t}}_{{\texttt {n}}_{\texttt {sd}}-1} \}\) form an orthonormal system of vectors. Two scalars, \(\alpha\) and \(\beta\), represent the penetration and friction coefficient, respectively. For \(\alpha ,\beta \rightarrow 0\), the case of a perfectly slip condition is retrieved [151].

Remark 4

The divergence-free condition in Eq. (18) induces the following compatibility condition on the velocity field

$$\begin{aligned} \langle {\varvec{u}}_{\!_D}{\cdot } {\varvec{n}}, 1 \rangle _{{\varGamma }_{_{ D}}} + \langle {\varvec{u}}{\cdot } {\varvec{n}}, 1 \rangle _{\partial {\varOmega }{\setminus} {\varGamma }_{_{ D}}} = 0 . \end{aligned}$$
(19)

Remark 5

In case of a purely Dirichlet boundary value problem, that is \(\partial {\varOmega } = {\varGamma }_{_{ D}}\), an additional constraint needs to be introduced to retrieve uniqueness of the pressure field. A common approach relies on imposing the mean value of the pressure in the domain [120, 218]

$$\begin{aligned} \frac{1}{|{\varOmega }|} ( p,1 )_{{\varOmega }} = 0 , \end{aligned}$$
(20)

or on the boundary of the domain [88, 98, 148, 199], namely

$$\begin{aligned} \frac{1}{|\partial {\varOmega }|} \langle p,1 \rangle _{\partial {\varOmega }} = 0 . \end{aligned}$$
(21)

4.1 HDG Local and Global Problems: Strong Form

Following the rationale presented in Sect. 3, Eq. (18) is rewritten element-by-element as a system of first-order equations by introducing a mixed variable \({\varvec{L}} = -\sqrt{\nu } {\varvec{\nabla }}{\varvec{u}}\), namely

$$\begin{aligned} \left\{ \begin{array}{ll} {\varvec{L}}+ \sqrt{\nu } {\varvec{\nabla }}{\varvec{u}}= {\varvec{0}} & {\text {in }}\,\,{\varOmega }_e, \\ {\varvec{\nabla }}{\cdot } \bigl ( \sqrt{\nu } {\varvec{L}}+ p{\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr ) = {\varvec{s}} & {\text {in }}\,\,{\varOmega }_e, \\ {\varvec{\nabla }}{\cdot } {\varvec{u}}= 0 & {\text {in }}\,\,{\varOmega }_e, \\ {\varvec{u}}= {\varvec{u}}_{\!_D}& {\text {on }}\,\,{\varGamma }_{_{ D}}, \\ {\varvec{n}}{\cdot } \bigl (\sqrt{\nu } {\varvec{L}}+ p {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr ) = -{\varvec{g}} & {\text {on }}\,\,{\varGamma }_{_{ N}}, \\ {\varvec{u}}{\cdot } {\varvec{D}} - {\varvec{n}}{\cdot } \bigl ( \sqrt{\nu } {\varvec{L}}+ p {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr ){\varvec{E}} = {\varvec{0}} & {\text {on }}\,\,{\varGamma }_{_{ S}}, \\ \llbracket {\varvec{u}}\otimes {\varvec{n}}\rrbracket ={\varvec{0}} & {\text {on }}\,\,{\varGamma }, \\ \llbracket {\varvec{n}}{\cdot } \bigl ( \sqrt{\nu }{\varvec{L}}+ p {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr ) \rrbracket = {\varvec{0}} & {\text {on }}\,\,{\varGamma }, \\ \end{array} \right. \end{aligned}$$
(22)

for \(e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\).

First, the HDG algorithm performes the hybridisation step by expressing \(({\varvec{u}}_e,p_e,{\varvec{L}}_e)\) in each element \({\varOmega }_e\) as functions of the unknown trace of the velocity \({\widehat{{\varvec{u}}}}\) on the element faces via the HDG local problem

$$\begin{aligned} \left\{ \begin{array}{ll} {\varvec{L}}_e + \sqrt{\nu } {\varvec{\nabla }}{\varvec{u}}_e = {\varvec{0}} & {\text {in }}\,\,{\varOmega }_e, \\ {\varvec{\nabla }}{\cdot } \bigl ( \sqrt{\nu } {\varvec{L}}_e + p_e {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr ) = {\varvec{s}} & {\text {in }}\,\,{\varOmega }_e, \\ {\varvec{\nabla }}{\cdot } {\varvec{u}}_e = 0 & {\text {in }}\,\,{\varOmega }_e, \\ {\varvec{u}}_e = {\varvec{u}}_{\!_D}& {\text {on }}\,\,\partial {\varOmega }_e \cap {\varGamma }_{_{ D}}, \\ {\varvec{u}}_e = {\widehat{{\varvec{u}}}}& {\text {on }}\,\,\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}}, \end{array} \right. \end{aligned}$$
(23a)

for \(e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\). Note that Eq. (23a) is a purely Dirichlet boundary value problem. Hence, following Remark 5, the constraint

$$\begin{aligned} \frac{1}{|\partial {\varOmega }_e|} \langle p_e, 1 \rangle _{\partial {\varOmega }} = \rho _e , \quad {\text {for }}\,\,e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}, \end{aligned}$$
(23b)

is introduced, where \(\rho _e\) is an independent variable representing the mean value of the pressure on the boundary \(\partial {\varOmega }_e\). It is worth noting that the variable \(\rho\) was not present in the HDG approximation of the Poisson equation and its treatment in the HDGlab code will be detailed in Sect. 9.

The HDG global problem thus accounts for the transmission and non-Dirichlet boundary conditions, that is

$$\begin{aligned} \left\{ \begin{array}{ll} \llbracket {\varvec{u}}\otimes {\varvec{n}}\rrbracket={\varvec{0}}&{\text {on }}\,\,{\varGamma }, \\ \llbracket {\varvec{n}}{\cdot } \bigl ( \sqrt{\nu }{\varvec{L}}+ p {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr ) \rrbracket= {\varvec{0}}&{\text {on }}\,\,{\varGamma }, \\ {\varvec{n}}{\cdot } \bigl (\sqrt{\nu } {\varvec{L}}+ p {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr )= -{\varvec{g}}&{\text {on }}\,\,{\varGamma }_{_{ N}}, \\ {\varvec{u}}{\cdot } {\varvec{D}} - {\varvec{n}}{\cdot } \bigl ( \sqrt{\nu } {\varvec{L}}+ p {\mathbf {I}}_{{\texttt {n}}_{\texttt {sd}}}\bigr ){\varvec{E}}= {\varvec{0}}&{\text {on }}\,\,{\varGamma }_{_{ S}}, \\ \end{array} \right. \end{aligned}$$
(24a)

where, following Remark 2, the first condition is automatically fulfilled. In addition, the constraint in Remark 4 is rewritten element-by-element in terms of the hybrid variable \({\widehat{{\varvec{u}}}}\) leading to

$$\begin{aligned} \langle {\varvec{u}}_{\!_D}{\cdot } {\varvec{n}}, 1 \rangle _{\partial {\varOmega }_e \cap {\varGamma }_{_{ D}}} + \langle {\widehat{{\varvec{u}}}}{\cdot } {\varvec{n}}, 1 \rangle _{\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}}} = 0 , \end{aligned}$$
(24b)

for \(e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\).

4.2 HDG Local and Global Problems: Weak Form

The corresponding weak form of the HDG local problem (23) is: given \({\varvec{u}}_{\!_D}\) on \({\varGamma }_{_{ D}}\) and \({\widehat{{\varvec{u}}}}^h\) on \({\varGamma }\cup {\varGamma }_{_{ N}}\cup {\varGamma }_{_{ S}}\), find \(({\varvec{L}}_e^h,{\varvec{u}}_e^h,p_e^h) \in [{\mathcal {V}}^h({\varOmega }_e)]^{{\texttt {n}}_{\texttt {sd}}\times {\texttt {n}}_{\texttt {sd}}} {\times } [{\mathcal {V}}^h({\varOmega }_e)]^{{\texttt {n}}_{\texttt {sd}}} {\times } {\mathcal {V}}^h({\varOmega }_e)\) that satisfy

$$\begin{aligned} \left\{ \begin{aligned}&- ( {\varvec{W}}, {\varvec{L}}_e^h )_{{\varOmega }_e} + ( {\varvec{\nabla }}{\cdot }(\sqrt{\nu }{\varvec{W}}), {\varvec{u}}_e^h )_{{\varOmega }_e} \\&\quad = \langle {\varvec{n}}_e {\cdot } \sqrt{\nu } {\varvec{W}}, {\varvec{u}}_{\!_D}\rangle _{\partial {\varOmega }_e\cap {\varGamma }_{_{ D}}} + \langle {\varvec{n}}_e {\cdot } \sqrt{\nu } {\varvec{W}}, {\widehat{{\varvec{u}}}}^h \rangle _{\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}} } , \\& ( {\varvec{w}}, {\varvec{\nabla }}{\cdot }( \sqrt{\nu } {\varvec{L}}_e^h) )_{{\varOmega }_e} + ( {\varvec{w}}, {\varvec{\nabla }}p_e^h )_{{\varOmega }_e} + \langle {\varvec{w}}, \tau {\varvec{u}}_e^h \rangle _{\partial {\varOmega }_e} \\&\quad = ( {\varvec{w}}, {\varvec{s}} )_{{\varOmega }_e} + \langle {\varvec{w}}, \tau {\varvec{u}}_{\!_D}\rangle _{\partial {\varOmega }_e \cap {\varGamma }_{_{ D}}} + \langle {\varvec{w}}, \tau {\widehat{{\varvec{u}}}}^h \rangle _{\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}}} , \\& ( {\varvec{\nabla }}q, {\varvec{u}}_e^h )_{{\varOmega }_e} = \langle q, {\varvec{u}}_{\!_D}{\cdot } {\varvec{n}}_e \rangle _{\partial {\varOmega }_e \cap {\varGamma }_{_{ D}}} + \langle q, {\widehat{{\varvec{u}}}}^h {\cdot } {\varvec{n}}_e \rangle _{\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}} } , \\& \langle p_e^h , 1 \rangle _{\partial {\varOmega }_e} = |\partial {\varOmega }_e| \rho _e^h , \end{aligned}\right. \end{aligned}$$
(25)

for all \(({\varvec{W}},{\varvec{v}},q) \in [{\mathcal {V}}^h({\varOmega }_e)]^{{\texttt {n}}_{\texttt {sd}}\times {\texttt {n}}_{\texttt {sd}}} {\times } [{\mathcal {V}}^h({\varOmega }_e)]^{{\texttt {n}}_{\texttt {sd}}} {\times } {\mathcal {V}}^h({\varOmega }_e)\). It is worth noting that, differently from the Poisson case, Eq. (25) provides \(({\varvec{L}}_e^h,{\varvec{u}}_e^h,p_e^h)\) in terms of two global unknowns, \({\widehat{{\varvec{u}}}}^h\) and \(\rho ^h \,{:=}\, \{ \rho _1^h, \ldots , \rho _{{\texttt {n}}_{\texttt {el}}}^h \}^T\).

Similarly, the weak form of the HDG global problem (24) is: find \({\widehat{{\varvec{u}}}}^h \in [\widehat{{\mathcal {V}}}^h]^{{\texttt {n}}_{\texttt {sd}}}\) and \({\varvec{\rho }}^h \in {\mathbb {R}}^{{\texttt {n}}_{\texttt {el}}}\) such that

$$\begin{aligned} \left\{ \begin{aligned}&\sum _{e=1}^{{\texttt {n}}_{\texttt {el}}} \Big \{ \langle {\widehat{{\varvec{v}}}}, {\varvec{n}}_e {\cdot } \sqrt{\nu } {\varvec{L}}_e^h \rangle _{\partial {\varOmega }_e{\setminus} ({\varGamma }_{_{ D}} \cup {\varGamma }_{_{ S}})} \\&\qquad - \langle {\widehat{{\varvec{v}}}}, {\varvec{n}}_e {\cdot } \sqrt{\nu } {\varvec{L}}_e^h {\varvec{E}}\rangle _{\partial {\varOmega }_e\cap {\varGamma }_{_{ S}}} \\&\qquad + \langle {\widehat{{\varvec{v}}}}, p_e^h {\varvec{n}}_e \rangle _{\partial {\varOmega }_e{\setminus} ({\varGamma }_{_{ D}} \cup {\varGamma }_{_{ S}})} \\&\qquad - \langle {\widehat{{\varvec{v}}}}, p_e^h {\varvec{n}}_e {\cdot } {\varvec{E}}\rangle _{\partial {\varOmega }_e\cap {\varGamma }_{_{ S}}} \\&\qquad + \langle {\widehat{{\varvec{v}}}}, \tau {\varvec{u}}_e^h \rangle _{\partial {\varOmega }_e{\setminus} ({\varGamma }_{_{ D}} \cup {\varGamma }_{_{ S}})} \\&\qquad - \langle {\widehat{{\varvec{v}}}}, \tau {\varvec{u}}_e^h {\cdot } {\varvec{E}}\rangle _{\partial {\varOmega }_e\cap {\varGamma }_{_{ S}}} \\&\qquad - \langle {\widehat{{\varvec{v}}}}, \tau {\widehat{{\varvec{u}}}}^h \rangle _{\partial {\varOmega }_e{\setminus} ({\varGamma }_{_{ D}} \cup {\varGamma }_{_{ S}})} \\&\qquad + \langle {\widehat{{\varvec{v}}}}, {\widehat{{\varvec{u}}}}^h {\cdot } ({\varvec{D}} + \tau {\varvec{E}}) \rangle _{\partial {\varOmega }_e \cap {\varGamma }_{_{ S}}} \Big \} \\&\quad = -\sum _{e=1}^{{\texttt {n}}_{\texttt {el}}} \langle {\widehat{{\varvec{v}}}}, {\varvec{g}} \rangle _{\partial {\varOmega }_e \cap {\varGamma }_{_{ N}}} , \\& \langle {\widehat{{\varvec{u}}}}^h {\cdot } {\varvec{n}}_e , 1 \rangle _{\partial {\varOmega }_e {\setminus} {\varGamma }_{_{ D}} } = - \langle {\varvec{u}}_{\!_D}{\cdot } {\varvec{n}}_e , 1 \rangle _{\partial {\varOmega }_e \cap {\varGamma }_{_{ D}}} , \\&\qquad {\text {for }}\,\,e=1,\dotsc ,{\texttt {n}}_{\texttt {el}}\end{aligned}\right. \end{aligned}$$
(26)

for all \({\widehat{{\varvec{v}}}}\in [\widehat{{\mathcal {V}}}^h]^{{\texttt {n}}_{\texttt {sd}}}\).

4.3 HDG Local and Global Problems: Discrete Form

The discretisation of the local problem (25) leads to the following linear system of equations

$$\begin{aligned}& \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{LL} & {\mathbf {A}}_{Lu} & {{\mathbf {0}}} & {{\mathbf {0}}} \\ {\mathbf {A}}_{Lu}^T & {\mathbf {A}}_{uu} & {\mathbf {A}}_{pu}^T & {{\mathbf {0}}} \\ {{\mathbf {0}}} & {\mathbf {A}}_{pu} & {{\mathbf {0}}} & {\mathbf {a}}_{\rho p} \\ {{\mathbf {0}}} & {{\mathbf {0}}} & {\mathbf {a}}_{\rho p}^T & 0 \\ \end{array} } \right] _{ e} \left\{ {\begin{array}{*{20}l} {\mathbf {L} }\\ {\mathbf {u}} \\ {\mathbf {p}} \\ \zeta \end{array} } \right\}_{ e} \\&\quad = \left\{ {\begin{array}{*{20}l} {\mathbf {f}}_{L} \\ {\mathbf {f}}_{u} \\ {\mathbf {f}}_{p} \\ 0 \end{array} } \right\}_{ e} + \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{L{\hat{u}}} \\ {\mathbf {A}}_{u{\hat{u}}} \\ {\mathbf {A}}_{p{\hat{u}}} \\ {{\mathbf {0}}} \end{array} } \right] _{ e} \widehat{\mathbf{u}}_e + \left\{ {\begin{array}{*{20}l} {{\mathbf {0}}} \\ {{\mathbf {0}}} \\ {{\mathbf {0}}} \\ 1 \end{array} } \right\}_{ e} \rho _e, \end{aligned}$$
(27)

where the constraint (23b) is imposed using the Lagrange multiplier \(\zeta\). It is worth noting that the Lagrange multiplier is required to guarantee that Eq. (27) is well-posed and the computed pressure field is unique but is not utilised in the solution of the global HDG problem. The resulting local elemental solution is given by

$$\begin{aligned} \left\{ {\begin{array}{*{20}l} {\mathbf {L} }\\ {\mathbf {u}} \\ {\mathbf {p}} \\ \zeta \end{array} } \right\}_{ e} = \left\{ {\begin{array}{*{20}l} {{\mathbf {z}}}_L^f \\ {{\mathbf {z}}}_u^f \\ {{\mathbf {z}}}_p^f \\ {{\mathbf {z}}}_\zeta ^f \end{array} } \right\}_{ e} + \left[ {\begin{array}{*{20}l} {\mathbf {Z}}_{L{\hat{u}}} \\ {\mathbf {Z}}_{u{\hat{u}}} \\ {\mathbf {Z}}_{p{\hat{u}}} \\ {\mathbf {Z}}_{\zeta {\hat{u}}} \end{array} } \right] _{ e} \widehat{\mathbf{u}}_e + \left\{ {\begin{array}{*{20}l} {{\mathbf {z}}}_L^{\rho } \\ {{\mathbf {z}}}_u^{\rho } \\ {{\mathbf {z}}}_p^{\rho } \\ {{\mathbf {z}}}_\zeta ^{\rho } \end{array} } \right\}_{ e} \rho _e, \end{aligned}$$
(28a)

with the matrix and vectors defined as

$$\begin{aligned} \left[ {\begin{array}{*{20}l} {\mathbf {Z}}_{L{\hat{u}}} \\ {\mathbf {Z}}_{u{\hat{u}}} \\ {\mathbf {Z}}_{p{\hat{u}}} \\ {\mathbf {Z}}_{\zeta {\hat{u}}} \end{array} } \right] _{ e}&\,{:=}\, \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{LL} & {\mathbf {A}}_{Lu} & {{\mathbf {0}}} & {{\mathbf {0}}} \\ {\mathbf {A}}_{Lu}^T & {\mathbf {A}}_{uu} & {\mathbf {A}}_{pu}^T & {{\mathbf {0}}} \\ {{\mathbf {0}}} & {\mathbf {A}}_{pu} & {{\mathbf {0}}} & {\mathbf {a}}_{\rho p} \\ {{\mathbf {0}}} & {{\mathbf {0}}} & {\mathbf {a}}_{\rho p}^T & 0 \\ \end{array} } \right] _{ e}^{-1} \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{L{\hat{u}}} \\ {\mathbf {A}}_{u{\hat{u}}} \\ {\mathbf {A}}_{p{\hat{u}}} \\ {{\mathbf {0}}} \end{array} } \right] _{ e} , \end{aligned}$$
(28b)
$$\begin{aligned} \left\{ {\begin{array}{*{20}l} {{\mathbf {z}}}_L^f \\ {{\mathbf {z}}}_u^f \\ {{\mathbf {z}}}_p^f \\ {{\mathbf {z}}}_\zeta ^f \end{array} } \right\}_{ e}&\,{:=}\, \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{LL} & {\mathbf {A}}_{Lu} & {{\mathbf {0}}} & {{\mathbf {0}}} \\ {\mathbf {A}}_{Lu}^T & {\mathbf {A}}_{uu} & {\mathbf {A}}_{pu}^T & {{\mathbf {0}}} \\ {{\mathbf {0}}} & {\mathbf {A}}_{pu} & {{\mathbf {0}}} & {\mathbf {a}}_{\rho p} \\ {{\mathbf {0}}} & {{\mathbf {0}}} & {\mathbf {a}}_{\rho p}^T & 0 \\ \end{array} } \right] _{ e}^{-1} \left\{ {\begin{array}{*{20}l} {\mathbf {f}}_{L} \\ {\mathbf {f}}_{u} \\ {\mathbf {f}}_{p} \\ 0 \end{array} } \right\}_{ e} , \end{aligned}$$
(28c)
$$\begin{aligned} \left\{ {\begin{array}{*{20}l} {{\mathbf {z}}}_L^{\rho } \\ {{\mathbf {z}}}_u^{\rho } \\ {{\mathbf {z}}}_p^{\rho } \\ {{\mathbf {z}}}_\zeta ^{\rho } \end{array} } \right\}_{ e}&\,{:=}\, \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{LL} & {\mathbf {A}}_{Lu} & {{\mathbf {0}}} & {{\mathbf {0}}} \\ {\mathbf {A}}_{Lu}^T & {\mathbf {A}}_{uu} & {\mathbf {A}}_{pu}^T & {{\mathbf {0}}} \\ {{\mathbf {0}}} & {\mathbf {A}}_{pu} & {{\mathbf {0}}} & {\mathbf {a}}_{\rho p} \\ {{\mathbf {0}}} & {{\mathbf {0}}} & {\mathbf {a}}_{\rho p}^T & 0 \\ \end{array} } \right] _{ e}^{-1} \left\{ {\begin{array}{*{20}l} {{\mathbf {0}}} \\ {{\mathbf {0}}} \\ {{\mathbf {0}}} \\ 1 \end{array} } \right\}_{ e} . \end{aligned}$$
(28d)

The discrete form of the global problem (26) reads as

$$\begin{aligned}&\sum _{e=1}^{{\texttt {n}}_{\texttt {el}}}\Big \{ \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{{\hat{u}}L}&{\mathbf {A}}_{{\hat{u}}u}&{\mathbf {A}}_{{\hat{u}}p} \end{array} } \right] _{ e} \left\{ {\begin{array}{*{20}l} {\mathbf {L} }\\ {\mathbf {u}} \\ {\mathbf {p}} \end{array} } \right\}_{ e} \\&\quad + [{\mathbf {A}}_{{\hat{u}}{\hat{u}}}]_e \, \widehat{\mathbf{u}}_e \Big \} = \sum _{e=1}^{{\texttt {n}}_{\texttt {el}}} [{\mathbf {f}}_{{\hat{u}}}]_e, \\&\quad {\mathbf {1}}^T \, [{\mathbf {A}}_{p{\hat{u}}}]_e \widehat{\mathbf{u}}_e = - {\mathbf {1}}^T \, [{\mathbf {f}}_{p}]_e . \end{aligned}$$
(29)

By inserting the solution (28a) into (29), the following system involving the globally-coupled unknowns \(\widehat{\mathbf{u}}\) and \({\varvec{\rho }}\) is obtained

$$\begin{aligned} \left[ {\begin{array}{*{20}l}\widehat{{\mathbf {K}}} & {\mathbf {G}} \\ {\mathbf {G}}^T & {{\mathbf {0}}} \end{array} } \right] \left\{ {\begin{array}{*{20}l} \widehat{\mathbf{u}} \\ {\varvec{\rho }} \end{array} } \right\} = \left\{ {\begin{array}{*{20}l} \widehat{\mathbf{f}}_{{\hat{u}}} \\ \widehat{\mathbf{f}}_{\rho } \end{array} } \right\} , \end{aligned}$$
(30)

where the matrices and vectors are obtained by assembling the elemental contributions

$$\begin{aligned} \widehat{{\mathbf {K}}}^e &:= \bigl [\begin{array}{llll} {\mathbf {A}}_{{\hat{u}}L}&{\mathbf {A}}_{{\hat{u}}u}&{\mathbf {A}}_{{\hat{u}}p}&{{\mathbf {0}}} \end{array}\bigr ]_{ e} \left[ {\begin{array}{*{20}l} {\mathbf {Z}}_{L{\hat{u}}} \\ {\mathbf {Z}}_{u{\hat{u}}} \\ {\mathbf {Z}}_{p{\hat{u}}} \\ {\mathbf {Z}}_{\zeta {\hat{u}}} \end{array} } \right] _{ e} + [{\mathbf {A}}_{{\hat{u}}{\hat{u}}}]_e , \end{aligned}$$
(31a)
$$\begin{aligned} {\mathbf {G}}^T&:= \left[ {\begin{array}{*{20}l} {\mathbf {1}}^T \, [{\mathbf {A}}_{p{\hat{u}}}]_1 \\ {\mathbf {1}}^T \, [{\mathbf {A}}_{p{\hat{u}}}]_2 \\ \cdots \\ {\mathbf {1}}^T \, [{\mathbf {A}}_{p{\hat{u}}}]_{{\texttt {n}}_{\texttt {el}}} \end{array} } \right] , \end{aligned}$$
(31b)
$$\begin{aligned} \widehat{\mathbf{f}}_{{\hat{u}}}^e&:= [{\mathbf {f}}_{{\hat{u}}}]_e -\bigl [\begin{array}{llll} {\mathbf {A}}_{{\hat{u}}L}&{\mathbf {A}}_{{\hat{u}}u}&{\mathbf {A}}_{{\hat{u}}p}&{{\mathbf {0}}}\end{array}\bigr ]_{ e} \left\{ {\begin{array}{*{20}l} {{\mathbf {z}}}_L^f \\ {{\mathbf {z}}}_u^f \\ {{\mathbf {z}}}_p^f \\ {{\mathbf {z}}}_{\zeta }^f \end{array} } \right\}_{ e} , \end{aligned}$$
(31c)
$$\begin{aligned} \widehat{\mathbf{f}}_{\rho }&:= - \left[ {\begin{array}{*{20}l} {\mathbf {1}}^T \, [{\mathbf {f}}_{p}]_1 \\ {\mathbf {1}}^T \, [{\mathbf {f}}_{p}]_2 \\ \cdots \\ {\mathbf {1}}^T \, [{\mathbf {f}}_{p}]_{{\texttt {n}}_{\texttt {el}}} \end{array} } \right] . \end{aligned}$$
(31d)

The expressions of the matrices and vectors introduced above are detailed in Appendix B.

Remark 6

Differently from the Poisson case, the global problem (30) features a saddle-point structure, as classical in the context of incompressible flows [120]. The proof of the symmetry of the HDG matrix in Eq. (30) can be devised following the rationale described in [151].

4.4 HDG Local Postprocess

The HDG postprocess procedure presented in Sect. 3.4 for the Poisson equation can be extended straightforwardly to the case of the vectorial variable \({\varvec{u}}\).

Remark 7

The postprocessing procedure utilised here is inspired by the work of Stenberg [253] and was exploited in the framework of HDG to obtain an improved approximation of the velocity field via the solution of an additional inexpensive element-by-element problem [199, 247]. Nonetheless, in the context of incompressible flows, it is often of interest retrieving an \(H({\text {div}})\)-conforming and exactly divergence-free approximation of the velocity field. For this purpose, alternative postprocessing strategies inspired by the Brezzi–Douglas–Marini (BDM) projection operator [36] were proposed [90, 92]. It is worth noting that the above mentioned procedures are suitable only for the velocity-pressure formulation of the incompressible flow equations. In case a formulation based on the Cauchy stress tensor is considered, an additional constraint is required to handle the rigid rotational modes as discussed in [148, 151, 245].

A superconvergent velocity field \({\varvec{u}}_{\star }\) is thus obtained by solving an independent local problem in each element, namely

$$\begin{aligned} \left\{ \begin{array}{ll} - {\varvec{\nabla }}{\cdot } (\nu {\varvec{\nabla }}{\varvec{u}}_{\star })= {\varvec{\nabla }}{\cdot } (\sqrt{\nu } {\varvec{L}}_e)&{\text {in }}\,\,{\varOmega }_e, \\ {\varvec{n}}{\cdot } \nu {\varvec{\nabla }}{\varvec{u}}_{\star }= - {\varvec{n}}{\cdot } \sqrt{\nu } {\varvec{L}}_e&{\text {on }}\,\,\partial {\varOmega }_e, \end{array} \right. \end{aligned}$$
(32)

with the constraint

$$\begin{aligned} ({\varvec{u}}_{\star }, 1)_{{\varOmega }_e} = ({\varvec{u}}_e, 1)_{{\varOmega }_e} \end{aligned}$$
(33)

on the mean value of the velocity in the element.

Hence, for each element \({\varOmega }_e, \ e = 1,\ldots ,{\texttt {n}}_{\texttt {el}}\), the weak form of the postprocess procedure is: find \({\varvec{u}}_{\star }^h \in [{\mathcal {V}}_{\star }^h({\varOmega }_e)]^{{\texttt {n}}_{\texttt {sd}}}\) such that

$$\begin{aligned} ({\varvec{\nabla }}{\varvec{v}}_{\star }, \nu {\varvec{\nabla }}{\varvec{u}}_{\star }^h )_{{\varOmega }_e}&= - ( {\varvec{\nabla }}{\varvec{v}}_{\star }, \sqrt{\nu } {\varvec{L}}_e^h )_{{\varOmega }_e} , \end{aligned}$$
(34a)
$$\begin{aligned} ({\varvec{u}}_{\star }^h, 1)_{{\varOmega }_e}&= ({\varvec{u}}_e^h, 1)_{{\varOmega }_e} , \end{aligned}$$
(34b)

for all \({\varvec{v}}_{\star }\in [{\mathcal {V}}_{\star }^h({\varOmega }_e)]^{{\texttt {n}}_{\texttt {sd}}}\).

Using an isoparametric approximation for the functions in the space \([{\mathcal {V}}_{\star }^h({\varOmega })]^{{\texttt {n}}_{\texttt {sd}}}\), the HDG local postprocess for the Stokes equations leads to

$$\begin{aligned} \left[ {\begin{array}{*{20}l} {\mathbf {A}}_{\star \star } & {\mathbf {A}}_{\star \lambda } \\ {\mathbf {A}}_{\star \lambda }^T & {{\mathbf {0}}} \end{array} } \right] _{ e} \left\{ {\begin{array}{*{20}l} {{\mathbf {u}}_{\star }} \\ {\varvec{\lambda }} \end{array} } \right\}_{ e} = \left[ {\begin{array}{*{20}l} {{\mathbf {0}}} & {\mathbf {A}}_{\star L} \\ {\mathbf {A}}_{\star \lambda }^T & {{\mathbf {0}}} \end{array} } \right] _{ e} \left\{ {\begin{array}{*{20}l} {\mathcal {I}}_{{\texttt {n}}_{\texttt {sd}}}^{\star } {\mathbf {u}} \\ {\mathcal {I}}_{{\texttt {n}}_{\texttt {sd}}\times {\texttt {n}}_{\texttt {sd}}}^{\star } {\mathbf {L} }\end{array} } \right\}_{ e} , \end{aligned}$$
(35)

where \({\varvec{\lambda }}\) is the vector of Lagrange multipliers imposing the constraint (34b) and \({\mathcal {I}}_{{\texttt {n}}_{\texttt {sd}}\times {\texttt {n}}_{\texttt {sd}}}^{\star } {:} [{\mathcal {V}}^h]^{{\texttt {n}}_{\texttt {sd}}\times {\texttt {n}}_{\texttt {sd}}} {\rightarrow } [{\mathcal {V}}_{\star }^h]^{{\texttt {n}}_{\texttt {sd}}\times {\texttt {n}}_{\texttt {sd}}}\) denotes the interpolation operator for tensor-valued functions from the space of polynomials of degree \({\texttt {p}}\) to the one of degree \({\texttt {p}} + 1\).

The expressions of the matrices and vectors introduced above are detailed in Appendix B.

5 The HDGlab Repository

The implementation of the HDG solver for the Poisson and Stokes equations has been made available as an open-source software, released under the terms of the GNU General Public License version 3.0 or any later version (https://www.gnu.org/licenses) and is freely available from the repository: https://git.lacan.upc.edu/hybridLab/HDGlab.

The structure of the repository, illustrated in Fig. 1, is described in this section.

Fig. 1
figure 1

Structure of the HDGlab repository

The directory dat contains the data files. This includes two and three dimensional meshes in the directories meshes2D and meshes3D respectively, the pre-computed reference elements in two and three dimensions in the directory refElem and the data structure required to postprocess high-order HDG solutions in the directory postprocess.

The directory Poisson contains the HDG solver for the Poisson problem as described in Sect. 3. The directory hdgPoisson contains the core HDG library for the Poisson equation. The directory testsPoisson is used to organise the functions that describe the setup of the problems to be solved, including the definition of the boundary conditions, source term and, if known, the analytical solution. The directory resPoisson is where the results are saved after an execution of the Poisson solver.

The directory Stokes contains the HDG solver for the Stokes problem as described in Sect. 4. The structure of this directory follows the same rationale as the one corresponding to the Poisson solver.

The directory common contains a set of functions that are common to both the Poisson and the Stokes solvers.

Finally, the directory importMesh contains a library that is provided to import a mesh generated with the open source software Gmsh [146] in HDGlab. The core routines to convert a mesh from .msh to .mat format are located in the directory GMSH. The directory examples contains some test cases including .geo and .msh files, whereas the output of the imported mesh is stored in the directory meshFiles. This library is described in detail in Appendix C.

6 Data Structures

Three data structures are used to manage the mesh, the reference element and the face information required to compute the elemental contributions of the global HDG problem. These three variables are assumed to be an input of the HDG library and a detailed description is provided in this section. To make the developed software more accessible variables of type struct are used in this work rather than class types.

6.1 Mesh

The variable mesh contains the following information:

  • nsd: Number of spatial dimensions.

  • optionNodes: Type of high-order nodal distribution, being an equally-spaced (0) or a Fekete (1) nodal set.

  • nOfNodes: Number of nodes.

  • X: Array of dimension nOfNodes\(\times\)nsd containing the nodal coordinates of the mesh.

  • nOfElements: Number of elements.

  • indexT: Array of dimension nOfElements\(\times 2\) containing the connectivity indices of the mesh. The first column contains the first node of the element and the second column contains the last node of the element.

  • pElem: Array of dimension \(1 \times\)nOfElements containing the degree of approximation of each element.

  • matElem: Array of dimension \(1 \times\)nOfElements containing the material flag for each element.

  • nOfIntFaces: Number of interior faces (i.e. faces not on the boundary).

  • intFaces: Array of dimension nOfIntFaces\(\times 5\). The first two columns contain the first element sharing this face and its local face number. The next two columns contain the second element sharing this face and its local face number. The last column contains the local node number of the second face that matches with the first local node of the first face.

  • nOfExtFaces: Number of exterior faces (i.e. faces on the boundary).

  • extFaces: Array of dimension nOfIntFaces\(\times 4\). The first two columns contains the element sharing this face and its local face number. The third column contains the boundary condition flag and the last column contains the flag of the boundary curve or surface.

The information stored in the field intFaces is characteristic of a DG formulation, where integrals on interior faces need to be computed. This is in contrast with a standard CG formulation, where only the field extFaces is needed to impose the boundary conditions. The last column of intFaces is needed to account for the different orientation of an interior face as seen from the element on the left and on the right of a face. It is worth noting that in two dimensions the information could be omitted because the local node number of the second face that matches with the first local node of the first face is always equal to two, as illustrated in Fig. 2. However, in three dimensions this information is required as there are three possible rotations of the local face nodes that do not alter the orientation of the element/face, as depicted in Fig. 3.

Fig. 2
figure 2

Interface between two triangular elements, showing the orientation of each element and the local node number of each node on the face as seen from the element on the left and on the right of the interface

Fig. 3
figure 3

Interface between two tetrahedral elements, showing the orientation of each face and the local node number of each node on the face as seen from the element on the left and on the right of the interface

To illustrate the mesh data structure, a coarse mesh of the domain \({\varOmega }= [0,1]^2\), with four triangular elements is considered. Figure 4 represents the mesh with the global numbering of the mesh elements, the element nodes, the mesh faces and the face nodes.

Fig. 4
figure 4

Mesh of \({\varOmega }= [0,1]^2\) with four triangular elements with the global numbering of the mesh elements (in green), the element nodes (in black), the mesh faces (in blue) and the face nodes (in red). (Color figure online)

An overview of the data contained in the mesh data structure for the example of Fig. 4 is shown in Fig. 5, with the details of some of the arrays depicted in Fig. 6.

Fig. 5
figure 5

Overview of the data contained in the mesh data structure for the example of Fig. 4

Fig. 6
figure 6

Detail of the fields X, indexT, intFaces and extFaces, corresponding to the data structure mesh of Fig. 5

Fig. 7
figure 7

Function to retrieve the connectivity of an element

It is worth noting that the connectivity of an element is obtained by using the function shown in Fig. 7. This means that the mesh nodes in the array X are duplicated. This functionality is meant to help the handling of meshes with a non-uniform degree of approximation and to provide a seamless way to partition the mesh in case future users would like to parallelise the code. If desired, a different array can be introduced for the connectivity information and the user only needs to redefine the function getElemConnectivity.

6.2 Reference Element

As usual in an isoparametric finite element context, the information related to the approximation and numerical integration in an element is stored by means of a reference element, with local coordinates, \({\varvec{\xi }}= (\xi _1,\ldots ,\xi _{{\texttt {n}}_{\texttt {sd}}})\). To easily handle meshes with a non-uniform degree of approximation, the variable refElem is considered an array of dimension \(1 \times {\texttt {p}}_\texttt {max}\), where \({\texttt {p}}_\texttt {max}\) is the maximum degree of approximation used in all the elements. For each component of the refElem array, the following information is stored:

  • nsd: Number of spatial dimensions.

  • optionNodes: Type of high-order nodal distribution, being an equally-spaced (0) or a Fekete (1) nodal set.

  • p: Degree of approximation.

  • nOfVertices: Number of element vertices.

  • nOfNodes: Number of element nodes.

  • coordinates: Array of dimension \(\texttt {nOfNodes} \times \texttt {nsd}\) containing the local nodal coordinates.

  • nOfFaces: Number of element faces.

  • face: Array of dimension \(1 \times \texttt {nOfFaces}\). Each position is a structure containing the following information about an element face:

    • nodes: Array containing the element local number of the nodes in the current face.

    • nodesPerm: Array containing \(\texttt {nsd} + 1\) permutations of the face nodes in the field nodes, using the element local number. Each row provides the permutation as required by the field intFaces in the mesh data structure.

    • nodesPermHDG: Array containing \(\texttt {nsd} + 1\) permutations of the face nodes in the field nodes, using the face local number. Each row provides the permutation as required by the field intFaces in the mesh data structure.

  • nOfGauss: Number of integration points.

  • gaussWeights: Array of dimension nOfGauss \(\times\) 1, containing the integration weights.

  • shapeFunctions: Array of dimension \(\texttt {nOfGauss} \times \texttt {nOfNodes} \times (\texttt {nsd}+1)\). When the third index is equal to 1, the array contains the value of the shape functions, for all the nodes at all integration points. When the third index is equal to \(r>1\), the array contains the value of the derivative of the shape functions in the \(\xi _{r-1}\) direction, for all the nodes at all integration points.

  • shapeFunctionsNodesPPp1: Array of dimension

    \(\texttt {nOfNodes}^\star\) \(\times\) \(\texttt {nOfNodes}\), where \(\texttt {nOfNodes}^\star\) denotes the number of nodes of an element with degree of approximation \({\texttt {p}} + 1\). It contains the value of the shape functions of the current element at the nodes of an element with one extra degree of approximation. This array is only used to perform the HDG postprocess described in Sects. 3.4 and 4.4.

  • shapeFunctionsGaussPPp1: Array of dimension

    \(\texttt {nOfGauss}^\star \times \texttt {nOfNodes}\), where \(\texttt {nOfGauss}^\star\) denotes the number of integration points of an element with degree of approximation \({\texttt {p}} + 1\). It contains the value of the shape functions of the current element and its derivatives at the integration points of an element with one extra degree of approximation. This array is only used to perform the HDG postprocess described in Sects. 3.4 and 4.4.

To illustrate the refElem data structure, Fig. 8 depicts the reference quadratic triangular element. An overview of the data contained in the refElem data structure for the example of Fig. 8 is shown in Fig. 9.

Fig. 8
figure 8

Reference triangular element for \({\texttt {p}}=2\)

Fig. 9
figure 9

Overview of the data contained in the refElem data structure for the reference triangular quadratic element of Fig. 8

Similarly, Fig. 10 shows the reference quadratic tetrahedral element and Fig. 11 depicts and overview of the data contained in the refElem data structure.

Fig. 10
figure 10

Reference tetrahedral element for \({\texttt {p}}=2\)

Fig. 11
figure 11

Overview of the data contained in the refElem data structure for the reference tetrahedral quadratic element of Fig. 10

It is worth noting that the code provided utilises nodal basis functions for the polynomial approximation. However, it is straightforward for future users to change the basis used for the approximation by simply changing the reference element information. With minimum effort it is also possible to incorporate other element types.

6.3 Reference Face

The information related to the approximation and numerical integration on a face is stored by means of a reference face, with local coordinates, \({\varvec{\eta }}= (\eta _1,\ldots ,\eta _{{\texttt {n}}_{\texttt {sd}}-1})\). To easily handle meshes with a non-uniform degree of approximation, the variable refFace is considered an array of dimension \({\texttt {p}}_\texttt {max} \times {\texttt {p}}_\texttt {max}\), where \({\texttt {p}}_\texttt {max}\) is the maximum degree of approximation used in all the elements. For each diagonal component of the refFace array, the information stored is a subset of the information stored in the refElem data structure. As an example, Fig. 12 offers an overview of the data contained in the diagonal term of refFace corresponding to a quadratic face of a triangular element.

Fig. 12
figure 12

Overview of the data contained in the refFace data structure for the reference face of a quadratic element in two dimensions

The upper triangular portion of the refFace data structure contains the information associated with the field shapeFunctions. This is only required when a mesh with a non-uniform degree of approximation is employed. The position (rs) of the array refFace contains the shape functions of degree r evaluated at the integration points of a face with degree of approximation s. This is required when computing the integrals in an interior face where the degree of approximation of the elements sharing this face is different. It is worth noting that only the entries in the upper triangle of the array refFace contain relevant information because the degree of approximation used for the hybrid variable in a given face is defined as the maximum between the degree of approximation of the two elements sharing the face.

7 Preprocess

This section describes the preprocessing stage of the HDG solver. The implementation can be found in the function hdgPreprocess, which produces as an output an updated version of the data structures mesh and hdg.

The data structure mesh is taken as an input, containing the fields described in Sect. 6.1, and a new field, called indexTf is added. This field contains an array of dimension \(\texttt {nOfFaces} \times 2\) featuring the connectivity indices of the mesh skeleton, where \(\texttt {nOfFaces}\) is the total number of mesh faces (i.e. interior faces plus exterior faces). The first column contains the first degree of freedom of a face and the second column contains the last degree of freedom of the face. Figure 13 shows the data contained in indexTf, after the preprocessing is performed, for the mesh of Fig. 4 and for a Poisson problem (i.e. scalar unknown). The same information for a Stokes problem is shown in Fig. 14.

Fig. 13
figure 13

Data contained in mesh.indexTf data structure for the solution of the Poisson equation in the mesh of Fig. 4

Fig. 14
figure 14

Data contained in mesh.indexTf data structure for the solution of the Stokes equation in the mesh of Fig. 4

For the Poisson problem, each node has one degree of freedom associated as the hybrid variable contains an approximation of the trace of the solution, which is a scalar quantity. In contrast, for the Stokes problem the global vector of unknowns contains an approximation of the trace of the velocity and an approximation of the mean pressure. Therefore, in two dimensions each face contains \(2({\texttt {p}}+1)\) degrees of freedom for the velocity and one extra degree of freedom per element is introduced for the mean pressure.

The hdg data structure is also an input of the function hdgPreprocess, containing two user defined parameters, namely:

  • tau: Value of the constant stabilisation parameter.

  • problem: String containing the name of the problem to be solved. The code provided contains two model problems, namely ’Poisson’ and ’Stokes’.

The structure is updated in the preprocess stage with the following information:

  • faceInfo: Structure of dimension \(1 \times \texttt {nOfElements}\). For each element of the array, the following information provides a link between the element and face information of the mesh:

    • local2global: Array of dimension

      \(1 \times \texttt {nOfElementFaces}\), containing the global face number of the faces of the current element.

    • localNumFlux: Array of dimension

      \(1 \times \texttt {nOfElementFaces}\), containing a flag for the numerical flux function associated with the faces of the current element. For an interior face, a value of zero is set. For a boundary face, the number corresponds to the boundary condition to be imposed and it is linked to the third column of the array extFaces of the mesh data structure described in Sect. 6.1.

    • permutations: Array of dimension

      \(1 \times \texttt {nOfElementFaces}\), containing a flag for the permutation required to ensure that the ordering of the nodes in the global face matches the ordering of the face nodes in the current element.

    • pHat: Array of dimension \(1 \times \texttt {nOfElementFaces}\), containing the degree of approximation used for the hybrid variable in the corresponding faces of the current element.

  • nDOFglobal: Number of global degrees of freedom.

  • vDOFtoSolve: Vector of global degrees of freedom associated with nodes not on a Dirichlet boundary.

In the case of the Stokes equations, three additional fields are introduced in the hdg data structure during the preprocess routine:

  • pureDirichlet: Boolean variable identifying whether the problem has purely Dirichlet boundary conditions.

  • columnsGlobalSystem: Number of columns in the global system, corresponding to the number of unknowns given by the hybrid velocity and the mean pressure.

  • rowsGlobalSystem: Number of rows in the global system, including the constraint for the uniqueness of pressure. The value of this variable will differ from columnsGlobalSystem only in the case of purely Dirichlet boundary value problems.

The data contained in the hdg data structure, after the preprocess stage, is shown in Fig. 15 for the Poisson problem on the mesh of Fig. 4. The data contained in the field faceInfo for the first two elements is also depicted in Fig. 16.

Fig. 15
figure 15

Data contained in the hdg data structure for the mesh of Fig. 4

Fig. 16
figure 16

Data contained in the hdg.faceInfo for the first two elements of the mesh of Fig. 4

Two more simple data structures are defined at the preprocess stage, namely problemParams and ctt. The structure problemParams contains problem-specific information. The current implementation uses this data structure to carry the following information:

  • nOfMat: Number of materials in the domain.

  • charLength: A characteristic length, used to define the value of the stabilisation parameter of the HDG formulation.

  • example: An integer that points to the number of a user-defined example.

In addition, problemParams stores the information on the material parameters. For the Poisson problem:

  • conductivity: Array of dimension \(1 \times \texttt {nOfMat}\) that contains the conductivity of each material present in the domain.

For the Stokes problem:

  • viscosity: A scalar value representing the viscosity coefficient of the fluid.

  • alphaSlip: A scalar value describing the penetration coefficient for the slip boundary condition.

  • betaSlip: A scalar value describing the friction coefficient for the slip boundary condition.

Finally, the structure ctt contains the following information:

  • iBC_Interior: A flag for the numerical flux function to be used on an interior face.

  • iBC_Dirichlet: A flag for the numerical flux function to be used on an exterior face where a Dirichlet boundary condition is imposed.

  • iBC_Neumann: A flag for the numerical flux function to be used on an exterior face where a Neumann boundary condition is imposed.

  • iBC_Slip: A flag for the numerical flux function to be used on an exterior face where a slip boundary condition is imposed (only supported for the Stokes problem).

  • nOfComponents: Number of components of the primal variable.

The flags used to distinguish the type of face and numerical flux to be considered can be specified by the user and they are linked to the third column of the array extFaces of the mesh data structure described in Sect. 6.1.

8 The HDGlab Poisson Solver

A code workflow diagram of the HDGlab Poisson code is shown in Fig. 17. This section focuses on the core part of the code that involves the assembly and solution of the global system of equations, the element-by-element solution of the local problems and the local postprocess to obtain a superconvergent solution.

Fig. 17
figure 17

Code workflow diagram

8.1 Global Problem

The HDG global system of equations is assembled and solved in the function hdg_Poisson_GlobalSystem. For each element, hdg_Poisson_ElementalMatrices contains two parts corresponding to the computation of the element integrals and the face integrals respectively. An extract of this function, showing the computation of the elemental matrices \({\mathbf {A}}_{qq}\) and \({\mathbf {A}}_{uq}\) and the elemental vector \({\mathbf {f}}_u\), is displayed in Fig. 18. It is worth noting that the loop on integration points is vectorised by using the binary singleton expansion function bsxfun.

Fig. 18
figure 18

Extract of the function hdg_Poisson_ElementalMatrices that computes the element integrals of the HDG formulation for the Poisson problem

In a similar fashion, the second part of the function hdg_Poisson_ElementalMatrices performs a loop on the faces of the current element and computes the face integrals that lead to the matrices \({\mathbf {A}}_{uu}\), \({\mathbf {A}}_{u{\hat{u}}}\), \({\mathbf {A}}_{q{\hat{u}}}\) and \({\mathbf {A}}_{{\hat{u}}{\hat{u}}}\). This computation distinguishes between interior and exterior faces and, for the exterior faces, utilises the flag in hdg.faceInfo.localNumFlux to enforce the correct boundary condition. For a Dirichlet face, the vector \({\mathbf {f}}_u\) is updated and the vector \({\mathbf {f}}_q\) is computed. For a Neumann face, the vector \({\mathbf {f}}_{{\hat{u}}}\) is computed.

One of the distinctive parts of the HDG formulation is found in the loop on faces, where a vector called indexFlip is used to ensure that the ordering of the degrees of freedom corresponding to the hybrid variable, as seen from the current element, matches the global ordering of the degrees of freedom of the vector of unknowns of the global HDG system.

After all the elemental matrices and vectors are computed, the elemental contributions to the global system are prepared to be assembled, namely \(\widehat{{\mathbf {K}}}^e\) and \(\widehat{{\mathbf {f}}}^e\), as shown in the extract of Fig. 19. It is worth noting that at this stage the matrices \({\mathbf {Z}}_{u{\hat{u}}}\) and \({\mathbf {Z}}_{q{\hat{u}}}\) and the vectors \({{\mathbf {z}}}_u^f\) and \({{\mathbf {z}}}_q^f\), defined in Eq. (10), are stored in the data structure local in order to be used during the solution of the element-by-element local problems. For large problems, the user might choose to save these matrices to disk before solving the global system of equations.

Fig. 19
figure 19

Extract of the hdg_Poisson_ElementalMatrices function that computes the elemental matrix \(\widehat{{\mathbf {K}}}^e\) and vector \(\widehat{{\mathbf {f}}}^e\)

8.2 Local Problem

After the global system of equations is solved, the function hdg_Poisson_LocalProblem, shown in Fig. 20, is called to solve the local problems. This function is just the implementation of Eq. (10a). The only aspect that requires attention is the indexing of the global vectors for the primal and mixed variables. This is managed by the function hdgElemToFaceIndex, which is designed to work for variables with any number of components. It is also worth noting that this function accounts for the possibility to have a non-uniform degree of approximation.

Fig. 20
figure 20

Function hdg_Poisson_LocalProblem to solve the element-by-element local problems

8.3 Local Postprocess

As discussed in Sect. 3.4, once the primal and mixed variables are computed, it is possible to perform a local, element-by-element, postprocess procedure to obtain a more accurate, superconvergent, approximation of the solution. This is implemented in the function hdg_Poisson_LocalPostprocess, shown in Fig. 21.

Fig. 21
figure 21

Function hdg_Poisson_LocalPostprocess to perform a local, element-by-element, postprocess of the solution

A key aspect in hdg_Poisson_LocalPostprocess is the computation of the elemental matrices and vectors in Eq. (17), which is implemented in the function hdg_Poisson_LocalPostprocessElemMat.

It is worth emphasising that the implementation assumes that no extra geometric information is known at this stage. Therefore, to compute the high-order nodal distribution of degree \({\texttt {p}}+1\), the nodal distribution in the reference element is mapped to the physical space by using the isoparametric mapping of degree \({\texttt {p}}\). This implies that, for curved elements, a subparametric formulation is considered. This formulation can lead to a suboptimal rate of convergence for the postprocessed solution as demonstrated in [239, 247], where a NURBS-enhanced implementation was proposed.

9 The HDGlab Stokes Solver

In this section, the HDGlab solver for the Stokes equations is presented. It is worth noting that the code features the same structure introduced in the previous section for the Poisson case. Hence, only the differences with respect to the Poisson solver will be detailed.

9.1 A Vector-Valued Problem

The HDG global system of equations is assembled and solved in the function hdg_Stokes_GlobalSystem. More precisely, the element and face integrals are computed by the function hdg_Stokes_ElementalMatrices for each element.

The first difference with respect to the Poisson code is represented by the vectorial nature of the primal and hybrid variables and by the tensor-valued mixed variable. For the sake of computational efficiency, the representation of the second-order tensor \(\mathbf {L}\) is written as a vector by rows, namely

$$\begin{aligned} {\mathbf {L} }= {\left\{ \begin{array}{ll} {[}\mathrm {L}_{11} \ \mathrm {L}_{12} \ \mathrm {L}_{21} \ \mathrm {L}_{22}]^T , & \text {in 2D} , \\ {[}\mathrm {L}_{11} \ \mathrm {L}_{12} \ \mathrm {L}_{13} \ \mathrm {L}_{21} \ \mathrm {L}_{22} \ \mathrm {L}_{23} \ \mathrm {L}_{31} \ \mathrm {L}_{32} \ \mathrm {L}_{33}]^T , & \text {in 3D} . \end{array}\right. } \end{aligned}$$

Figure 22 reports the computation of the elemental matrices \({\mathbf {A}}_{LL}\), \({\mathbf {A}}_{Lu}\) and \({\mathbf {A}}_{pu}\) and the elemental vector \({\mathbf {f}}_{u}\). It is worth noting that the assembly of these matrices and this vector accounts for the appropriate numbering of the vector-valued and tensor-valued unknowns. Similarly to the Poisson case, the loop on integration points is vectorised via the command bsxfun.

Fig. 22
figure 22

Extract of the hdg_Stokes_ElementalMatrices function that computes the element integrals of the HDG formulation for the Stokes problem

9.2 Slip Boundary Conditions

In the second part of hdg_Stokes_ElementalMatrices, the face integrals are computed within a loop on the element faces. More precisely, the matrices \({\mathbf {A}}_{L{\hat{u}}}\), \({\mathbf {A}}_{uu}\), \({\mathbf {A}}_{u{\hat{u}}}\) and \({\mathbf {A}}_{p{\hat{u}}}\) are computed for the local problem, whereas the matrix \({\mathbf {A}}_{{\hat{u}}{\hat{u}}}\) is computed for the global problem. In addition, on the Neumann faces the vector \({\mathbf {f}}_{{\hat{u}}}\) is computed, whereas on the Dirichlet faces the vectors \({\mathbf {f}}_{L}\) and \({\mathbf {f}}_{p}\) are computed and the vector \({\mathbf {f}}_{u}\) is updated.

Remark 8

In case of Dirichlet and Neumann boundary conditions, the remaining matrices involved in the global problem are such that

$$\begin{aligned} {\mathbf {A}}_{{\hat{u}}L}&= {\mathbf {A}}_{L{\hat{u}}}^T , \end{aligned}$$
(36a)
$$\begin{aligned} {\mathbf {A}}_{{\hat{u}}u}&= {\mathbf {A}}_{u{\hat{u}}}^T , \end{aligned}$$
(36b)
$$\begin{aligned} {\mathbf {A}}_{{\hat{u}}p}&= {\mathbf {A}}_{p{\hat{u}}}^T . \end{aligned}$$
(36c)

In case slip boundary conditions are also considered, the properties in Eq. (36) no longer hold and the matrices \({\mathbf {A}}_{{\hat{u}}L}\), \({\mathbf {A}}_{{\hat{u}}u}\) and \({\mathbf {A}}_{{\hat{u}}p}\) are computed in the function hdg_Stokes_ElementalMatrices. Figure 23 displays the initialisation of the above elemental matrices which are then computed in the loop on faces when the flag in hdg.faceInfo.localNumFlux matches ctt.iBC_Slip.

Fig. 23
figure 23

Extract of the hdg_Stokes_ElementalMatrices function that initialises the elemental matrices associated with the slip boundaries in the global problem for the Stokes problem

A specific treatment of the case in which slip boundary conditions are considered is also required for the definition of the elemental matrices of the global problem with appropriate ordering of the degrees of freedom of the hybrid variable using the indexFlip vector, see Fig. 24.

Fig. 24
figure 24

Extract of the hdg_Stokes_ElementalMatrices function that defines the elemental matrices of the global problem for the Stokes case, depending on the boundary conditions imposed

9.3 Additional Constraint in the Local Problem

A major difference between the Poisson and Stokes case is the structure of the local problems (9) and (27). Despite both matrices are symmetric, the one arising from the discretisation of the Poisson equation is positive definite, whereas it is indefinite in the Stokes case. More precisely, the matrix in (27) features a saddle-point structure, as classical in the context of finite element approximations of incompressible flow problems [120]. In addition, since the HDG local problem is a purely Dirichlet boundary value problem, the constraint (23b) is introduced using a Lagrange multiplier \(\zeta\).

The structure of the symmetric indefinite matrix involved in the local problem is displayed on the left-hand side of Fig. 25. On the right-hand side, the blocks of the first and last columns are associated with the contribution of \(\widehat{\mathbf{u}}\) and \({\varvec{\rho }}\), respectively, whereas the second column is related to the independent term of the equation. The figure reports the hybridisation stage in which the elemental matrices and vectors defined in (28) are computed for each element. The output of this computation is stored in the data structure local to be successively utilised in the solution of the element-by-element local problems.

Fig. 25
figure 25

Extract of the hdg_Stokes_ElementalMatrices function that computes the matrices and vectors in Eq. (27)

Finally, hdg_Stokes_ElementalMatrices computes the elemental contribution to the matrix in the global problem (30) as reported in Fig. 26. It is worth noting that the variable hdg.pureDirichlet is utilised here to discriminate the construction of the global matrix of a purely Dirichlet boundary value problem. More precisely, besides the blocks \(\widehat{{\mathbf {K}}}\), \({\mathbf {G}}\) and \({\mathbf {G}}^T\), also the vector \({\mathbf {a}}_{{\overline{\rho }} \rho }\) (i.e. ArlExtra) arising from the imposition of the global constraint (20) is taken into account. An extract of the hdg_Stokes_ElementalMatrices function computing such a vector is displayed in Fig. 27.

Fig. 26
figure 26

Extract of the hdg_Stokes_ElementalMatrices function that computes the block matrix and vector in Eq. (30)

Fig. 27
figure 27

Extract of the hdg_Stokes_ElementalMatrices function that computes the vector \({\mathbf {a}}_{{\overline{\rho }} \rho }\) for the pressure constraint

Remark 9

The assembly of the block matrix reported in Fig. 26 does not exploit the symmetry of the terms in Eq. (30) to its full potential. Indeed, the rationale of HDGlab being educational, the matrix \({\mathbf {G}}\) is constructed by inserting the solution (28a) of the local problem into the global problem (29), leading to the assembly of the elemental contributions

$$\begin{aligned} {\mathbf {G}^{e} }:= \bigl [\begin{array}{*{20}l} {\mathbf {A}}_{{\hat{u}}L}&{\mathbf {A}}_{{\hat{u}}u}&{\mathbf {A}}_{{\hat{u}}p}&{\mathbf {0}}\end{array}\bigr ]_{ e} \left\{ {\begin{array}{*{20}l} {\mathbf {z}}_L^{\rho } \\ {\mathbf {z}}_u^{\rho } \\ {\mathbf {z}}_p^{\rho } \\ {\mathbf {z}}_{\zeta }^{\rho } \end{array} } \right\}_{ e} . \end{aligned}$$
(37)

The interested reader can thus employ the provided code to numerically verify that the matrix \({\mathbf {G}}\) obtained from the assebly in Eq. (37) is the transpose of the one introduced in (31). The formal proof can be devised following the rationale described in [151].

9.4 Assembly of the Global System

As described in Sect. 4, the global problem for the Stokes equations features a saddle-point structure. Hence, the assembly of the global system presents major differences with respect to the Poisson case previously discussed. First, Fig. 28 reports the initialisation of the structures required to perform the assembly. It is worth recalling that the value of hdg.rowsGlobalSystem differs from the one of hdg.columnsGlobalSystem only if Dirichlet conditions are imposed on all boundaries. In this case, the additional constraint (20) is considered to guarantee the well-posedness of the problem.

Fig. 28
figure 28

Extract of the hdg_Stokes_Globalystem function that initialises the structures required for the assembly of the global system

The construction of the structures to perform the assembly of the global system is displayed in Fig. 29. According to the variable hdg.pureDirichlet, the above mentioned constraint is introduced as an extra line in the system.

Fig. 29
figure 29

Extract of the hdg_Stokes_Globalystem function that constructs the structures required for the assembly of the global system

Of course, the matrix arising from the operations displayed in Fig. 29 is rectangular. In order to retrieve a square matrix, an extra column is added to the matrix and the constraint is imposed via a Lagrange multiplier (Fig. 30).

Fig. 30
figure 30

Extract of the hdg_Stokes_Globalystem function to impose the constraint in the global system for purely Dirichlet boundary value problems

9.5 Three Unknowns in the Local Problem

The structure of the code for the local problem in the Stokes case replicates the one presented in Fig. 20 for the Poisson equation and is thus omitted. The only difference lies in the computation of three variables in each element, namely the pressure \({\mathbf {p}}\), the velocity \({\mathbf {u}}\) and the gradient of velocity \(\mathbf {L}\). An extract of the function hdg_Stokes_LocalProblem is displayed in Fig. 31, focusing on the operations in Eq. (28).

Fig. 31
figure 31

Extract of the hdg_Stokes_LocalProblem function where the elemental values of the primal and mixed unknowns are computed

10 Visualisation

The use of a high-order functional approximation means that non-standard techniques are required to produce a reliable representation of the solution in each element. With the increased popularity of high-order methods in recent years, different strategies to efficiently display high-order solutions have been proposed [188, 198, 220]. The HDGlab provides the capability to accurately display high-order solutions, including curved isoparametric elements.

Three data structures are employed in the visualisation. The data structure plotOpts is used to collect all the user defined options available for the visualisation. It contains the following information:

  • resolution: Takes value 1 for a faster but less accurate representation of high-order solutions and value 2 for a slower but more accurate representation of high-order solutions.

  • fieldsWithMesh: Plots the solution whilst displaying the mesh.

  • fieldsWithNodes: Plots the solution whilst displaying the nodes.

  • componentsToPlot: A user-defined vector containing the components of the solution to be represented.

  • alphaFace: Sets the transparency between 0 and 1.

In addition, for the Stokes equation, two problem-specific options are available in the data structure plotOpts:

  • componentsU: A boolean variable allowing the predefined visualisation of all the components of the velocity vector. This functionality relies on the definition of the vector componentsToPlot.

  • moduleU: A boolean variable allowing the visualisation of the module of the velocity.

The data structure postproc is provided for triangular and tetrahedral elements with equally-spaced and Fekete nodal sets in the directory dat/postprocess. In two dimensions, the data structure postproc contains the following information:

  • nOfNodesPlot: Number of nodes used to display the high-order solution in each element.

  • nodesPlot: Array of dimension \(\texttt {nOfNodesPlot} \times 2\) containing the coordinates of the nodes, in the reference element, used to display the high-order solution.

  • nSubElemsOnePlot: Number of subelements used to display the high-order solution in each element.

  • connecNodesPlot: Array of dimension

    \(\texttt {nSubElemsOnePlot} \times 3\) accounting for the connectivity of the submesh used to display the high-order solution in each element.

  • nOfEdges: Number of edges of the element.

  • edgeNodesSplit: Array of dimension \(\texttt {5r} \times \texttt {nOfEdges}\), where r is the resolution selected by the user in the data structure plotOpts. The i-th column contains the local number of the list of nodesPlot that belong to the i-th edge.

  • elem: Array of dimension \(1 \times {\texttt {p}}_\texttt {max}\), where \({\texttt {p}}_\texttt {max}\) is the maximum degree of approximation used in all the elements. The component \({\texttt {p}}\) of elem contains a field called \(\texttt {N}\), of dimension \(\texttt {nOfNodesPlot} \times {\texttt {p}}({\texttt {p}}+1)/2\), that stores the value of the shape functions of order \({\texttt {p}}\) at the positions given by nodesPlot. This information is used to interpolate the solution at the nodes of the submesh, providing a more accurate representation of the high-order solution.

  • face: Array of dimension \(1 \times {\texttt {p}}_\texttt {max}\). The component \({\texttt {p}}\) of elem contains a field called \(\texttt {N}\), of dimension \(\texttt {nOfNodesPlot} \times ({\texttt {p}}+1)\), that stores the value of the shape functions of order \({\texttt {p}}\) at the positions of an edge. This information is used to interpolate the solution at the edges of the submesh.

The submeshes used for a triangular element with resolution=1 and resolution=2 are displayed in Fig. 32. To illustrate the effect of the user-defined parameter resolution on the visualisation, Fig. 33 depicts the shape function associated to the fourth node of the reference quadratic triangular element, shown in Fig. 8, using resolution=1 and resolution=2.

Fig. 32
figure 32

Submesh of the reference element used to provide an accurate representation of high-order solutions in each element. The left picture corresponds to resolution=1 and the right picture to resolution=2

Fig. 33
figure 33

Shape function of the fourth node of a quadratic triangular element using resolution=1 (left) and resolution=2 (right)

In three dimensions the data structure postproc contains the following information:

  • Face: Structure that contains:

    • nElemPlot: Number of subelements used to display the high-order solution in each element face.

    • connecPlot: Array of dimension \(\texttt {nElemPlot} \times 3\) accounting for the connectivity of the submesh used to display the high-order solution in each element face.

    • nNodesPlot: Number of nodes used to display the high-order solution on each element face. It is given by \((5\texttt {r}+1)(5\texttt {r}+2)/2\), where r is the resolution selected by the user in the data structure plotOpts.

    • edgeNodesPlot: Array of dimension \(1 \times (15\texttt {r}+1)\), where r is the resolution selected by the user in the data structure plotOpts. It contains the local number of the face nodes that belong to the edges of the face.

  • Elem: Array of dimension \(1 \times {\texttt {p}}_\texttt {max}\), where \({\texttt {p}}_\texttt {max}\) is the maximum degree of approximation used in all the elements. The component \({\texttt {p}}\) of Elem contains:

    • face: Array of dimension \(1 \times 4\) where the i-th component contains the local number of the vertices on the i-th face and the value of the element shape functions of order \({\texttt {p}}\) at the nodal distribution used for plotting the solution on the i-th face.

    • coord: Array of dimension \({\texttt {p}}({\texttt {p}}+1)({\texttt {p}}+2)/6\) that contains the nodal distribution on the reference tetrahedral for an approximation of degree \({\texttt {p}}\).

  • faceVertices: Array of dimension \(4 \times 3\). The i-th row contains the local number of the vertices on the i-th face.

The third data structure utilised during the postprocess stage is called visual and it is built by the function buildSubmeshPostprocess2D in two dimensions and by buildSubmeshPostprocess3D in three dimensions. This data structure contains the following information:

  • X: Physical coordinates of all the nodes of the submesh used to display the high-order solution.

  • T: Connectivities of all the subelements used to display the high-order solution.

  • Xnodes: Physical coordinates of all the vertices of the mesh. This field is only used if the user sets fieldsWithNodes=1 in the data structure plotOpts.

  • edges: Structure containing the list of nodes of the submesh that form the high-order representation of the physical edges of the mesh.

In a separate function, the data structure is updated by adding the field U that contains the interpolated values of the solution on the submesh used to display the high-order solution. This action is performed by the function called interpolateSolutionPostprocess2D and interpolateSolutionPostprocess3D in two and three dimensions respectively.

It is worth noting that the function that creates the data structure visual is independent on the field to be represented and, therefore, it is only called once. Instead, the second function that updates the data structure visual with the field U depends upon the field to be represented. Therefore, several calls can be made to the function updating visual without the need to build the submesh again. It is also important to note that the function that updates the data structure visual with the field U accepts elemental and nodal fields.

Once all the information is available in the data structure visual, the function postprocessField2D is used to plot the high-order solution.

In three dimensions there is an extra option available that consists of representing the solution only in a region of the computational domain. The user can set the value of a string, called conditionPlot, that specifies a region in the physical space. Before visual is computed, the function selectFacesToPlot3D computes the list of faces in the computational mesh that satisfy the condition given by conditionPlot. Then, the submesh and interpolation of the solution is only performed over the faces that satisfy the condition specified by the user.

Finally, the visualisation function also accounts for the need to represent the superconvergent solution obtained after the local postprocess described in Sects. 3.4 and 4.4. To simplify the implementation, the visualisation builds a mesh data structure where the degree of approximation in each element is the degree used for the computation plus one. With this information, the same functions used to display the high-order primal solution can be used to postprocess the higher order superconvergent solution.

11 Numerical Examples

In this section, several numerical examples showing the capabilities of the HDGlab solvers for the Poisson and Stokes equations are presented. As mentioned in Sects. 3 and 4, the choice of the stabilisation parameter \(\tau\) is critical for the accuracy of the HDG approximation. For the examples involving the Poisson equation, the definition \(\tau = c_{_P} \kappa /\ell\) is considered, where \(\ell\) is a characteristic length of the domain and \(c_{_P}\) a scaling factor selected equal to 1 [175]. Following [151], the stabilisation for the Stokes cases is defined as \(\tau = c_{_S} \nu /\ell\), \(\ell \) the scaling factor being \(c_{_S} = 3\).

11.1 Optimal Convergence Properties

The optimal convergence properties of the proposed HDG implementation are presented for the Stokes flow, using test cases with analytical solution, in two and three dimensions. Uniform meshes of triangular and tetrahedral elements with Fekete nodal sets are utilised.

First, the two-dimensional Wang flow [264] in the unit square domain \({\varOmega } = [0,1]^2\) is considered. The analytical velocity field is

$$\begin{aligned} {\varvec{u}}({\varvec{x}}) = \left\{ {\begin{array}{*{20}l} 2 a x_2 - b \lambda \cos (\lambda x_1) \exp \{-\lambda x_2\} \\ b \lambda \sin (\lambda x_1) \exp \{-\lambda x_2\} \end{array} } \right\} , \end{aligned}$$
(38)

whereas the pressure field is uniformly zero in \({\varOmega }\). The coefficients a, b and \(\lambda\) in (38) are selected such that \(a = b = 1\) and \(\lambda = 10\) and the kinematic viscosity \(\nu\) is set to 1. The source term \({\varvec{s}}\) and the boundary conditions are computed starting from the analytical solution above. More precisely, a pseudo-traction \({\varvec{g}}\) is applied on the bottom surface \({\varGamma }_{_{ N}} \,{:=}\, \{(x_1,x_2) \in {\varOmega }\ | \ x_2 = 0\}\) and Dirichlet data \({\varvec{u}}_{\!_D}\) are imposed on the remaining boundaries \({\varGamma }_{_{ D}} = \partial {\varOmega }{\setminus} {\varGamma }_{_{ N}}\).

Figure 34 displays the convergence history of the relative error, measured in the \({\mathcal {L}}_2({\varOmega })\) norm, of the primal, mixed and postprocessed variables as a function of the characteristic mesh size. Optimal convergence of order \({\texttt {p}} + 1\) is observed for velocity, \({\varvec{u}}\), pressure, p, and gradient of velocity, \({\varvec{L}}\), whereas superconvergence of order \({\texttt {p}} + 2\) is achieved by the postprocessed velocity \({\varvec{u}}_{\star }\).

Fig. 34
figure 34

Two-dimensional Wang flow. Convergence of the \({\mathcal {L}}_2({\varOmega })\) error of pressure, p, mixed variable, \({\varvec{L}}\) (top), primal, \({\varvec{u}}\), and postprocessed, \({\varvec{u}}_\star\), velocities (bottom) as a function of the characteristic mesh size h for polynomial degree of approxiomation \({\texttt {p}} = 1, \ldots , 5\)

The following example involves a three-dimensional Stokes flow in the unit cube \({\varOmega } = [0,1]^3\), with the following manufactured solution

$$\begin{aligned} {\varvec{u}}({\varvec{x}})&= \left\{ {\begin{array}{*{20}l} \begin{aligned} n +&(x_3 - x_2) \sin (x_1 - n) \\ m -&x_2 \biggl [ \left( x_3 - \frac{1}{2} x_2 \right) \cos (x_1 - n) \\&\quad + \left( x_1 - \frac{1}{2} x_2 \right) \cos (x_3 - n) \biggr ] \\ n +&(x_1 - x_2) \sin (x_3 - n) \end{aligned} \end{array} } \right\} , \end{aligned}$$
(39)
$$\begin{aligned} p({\varvec{x}})&= x_1 (1 - x_1) + x_2 (1 - x_2) + x_3 (1 - x_3) , \end{aligned}$$
(40)

where \(m = 1\) and \(n = \tfrac{1}{2}\). The kinematic viscosity is set to \(\nu = 1\), a Neumann datum is imposed on the boundary \({\varGamma }_{_{ N}} \,{:=}\, \{(x_1,x_2,x_3) \in {\varOmega }\ | \ x_3 = 0\}\), whereas Dirichlet conditions are prescribed on \({\varGamma }_{_{ D}} = \partial {\varOmega }{\setminus} {\varGamma }_{_{ N}}\).

The optimal convergence of order \({\texttt {p}} + 1\) of the relative \({\mathcal {L}}_2({\varOmega })\) error for velocity, pressure and gradient of velocity and the superconvergence of the postprocessed velocity are confirmed in the 3D case by the results in Fig. 35.

Fig. 35
figure 35

Three-dimensional manufactured Stokes flow. Convergence of the \({\mathcal {L}}_2({\varOmega })\) error of pressure, p, mixed variable, \({\varvec{L}}\) (top), primal, \({\varvec{u}}\), and postprocessed, \({\varvec{u}}_\star\), velocities (bottom) as a function of the characteristic mesh size h for polynomial degree of approxiomation \({\texttt {p}} = 1, \ldots , 5\)

11.2 High-Order Curved Meshes

The coaxial Couette flow [64] is considered to show the optimal convergence properties of the HDGlab solver using a high-order isoparametric approximation in a domain featuring curved boundaries.

This test consists of an incompressible viscous flow within two coaxial circular cylinders of infinite length and radius \(R_{\text {int}} = 1\) and \(R_{\text {ext}} = 5\), respectively. The computational domain is defined as a section of the 3D cylinders, that is, \({\varOmega } = \{ (x_1,x_2) \in {\mathbb {R}}^2 \ | \ R_{\text {int}}\le r \le R_{\text {ext}}\}\), where \(r \,{:=}\, \sqrt{x_1^2 + x_2^2}\) is the distance to the axis of the cylinders. Dirichlet boundary conditions enforcing the value of the angular velocities \(\omega _{\text {int}} = 0\) and \(\omega _{\text {ext}} = 1/R_{\text {ext}}\) are imposed on the internal and external boundary, respectively. The analytical expression of the azimuthal component of the velocity is

$$\begin{aligned} \begin{aligned} u_{\phi } =&\frac{ \omega _{\text {ext}} R_{\text {ext}}^2 - \omega _{\text {int}} R_{\text {int}}^2 }{ R_{\text {ext}}^2 -R_{\text {int}}^2 } r \\&- \frac{ ( \omega _{\text {ext}}- \omega _{\text {int}}) R_{\text {ext}}^2 R_{\text {int}}^2 }{ R_{\text {ext}}^2 -R_{\text {int}}^2 } \frac{1}{r} . \end{aligned} \end{aligned}$$
(41)

Of course, being a purely Dirichlet boundary value problem, the constraint on the mean value of pressure is introduced to enforce the field to be uniformly equal to 1 in the domain.

A set of high-order uniformly refined meshes with Fekete nodal distribution is constructed using the strategy described in [212]. Figure 36 displays the first level of mesh refinement featuring 128 triangular elements of polynomial degree 3 and the module of the computed velocity.

Fig. 36
figure 36

First level of refinement of the third-order mesh used for the convergence study of the two-dimensional Couette flow (top) and module of the computed velocity (bottom)

The convergence of the relative error, measured in the \({\mathcal {L}}_2({\varOmega })\) norm, of the primal, mixed and postprocessed variables is reported in Fig. 37 as a function of the characteristic mesh size. Optimal convergence of the primal and mixed variables and superconvergence of the postprocessed variable is achieved also in presence of high-order curved meshes.

Fig. 37
figure 37

Two-dimensional Couette flow. Convergence of the \({\mathcal {L}}_2({\varOmega })\) error of pressure, p, mixed variable, \({\varvec{L}}\) (top), primal, \({\varvec{u}}\), and postprocessed, \({\varvec{u}}_\star\), velocities (bottom) as a function of the characteristic mesh size h for polynomial degree of approxiomation \({\texttt {p}} = 1, \ldots , 5\)

11.3 Non-uniform Degree of Approximation

In this section, the flexibility of HDGlab to devise a non-uniform polynomial degree approximation in the domain is presented. This case, inspired by the study on micromixers in [131], consists of the flow in a microchannel with five obstacles. The problem setup features a parabolic inlet velocity profile and homogeneous Dirichlet and Neumann conditions on the top/bottom walls and on the outlet, respectively.

The channel has dimensions \([0,6.6] \times [-0.5,0.5]\) and the obstacles, attached to the top and bottom walls have thickness 0.2 and height 0.5. A mesh with local element size ranging between 0.08 and 0.19 is generated without any specific a priori refinement. It is worth noting that only two mesh elements are defined along the thickness of the obstacles.

To capture the complex flow features among the obstacles, a high-order non-uniform polynomial degree distribution is generated following the adaptivity strategy described in [247]. The resulting degree of approximation in each element is displayed in Fig. 38. High-order polynomials are employed in correspondance of the tip of the obstacles where localised flow features appear, whereas low-order approximations are utilised in region further away.

Fig. 38
figure 38

a Mesh and degree of approximation and b module of the velocity for the flow in a microchannel with obstacles using non-uniform polynomial degree \({\texttt {p}}\) between 1 and 7

The module of the velocity on the described mesh is also reported in Fig. 38. It is worth noting that the mesh structure featuring the non-uniform polynomial approximation is provided as a datum for this test case. The corresponding simulation is performed seamlessly in HDGlab and no specific intervention is required to the user.

11.4 Stokes Flow Past a Sphere

Finally, the Stokes flow past a sphere is considered. The domain \({\varOmega } = [-H,L] \times [-H,H] \times [-H,H] {\setminus} {\mathcal {B}}_{{\varvec{0}},1}\) is defined, with \(H = 5\), \(L = 10\) and \({\mathcal {B}}_{{\varvec{0}},1}\) being the sphere of radius 1 centred in the point (0, 0, 0). Exploiting the symmetry of the problem, only a quarter of the domain is meshed and slip conditions are imposed on the corresponding symmetry planes. Homogeneous Neumann and no-slip conditions are applied on the outlet and on the surface of the sphere, respectively. On the inlet and on the remaining lateral and top planes, a Dirichlet boundary conditions with the analytical velocity is enforced.

A high-order mesh featuring 1,036 tetrahedral elements is generated via the solid mechanics analogy described in [212, 269]. Figure 39 displays the module of the velocity and the pressure field computed using an isoparametric approximation of degree 6.

Fig. 39
figure 39

a Module of the velocity and b pressure field of the external flow past a quarter of a sphere using polynomial degree \({\texttt {p}} = 6\)

It is worth noting that the computations in Sects. 11.111.211.3 and 11.4 are performed using a unique HDGlab solver for the Stokes equations, independently on the number of spatial dimensions of the problem under analysis. Indeed, HDGlab provides a seamless implementation of the HDG method, in which all relevant information is extracted from the mesh structure and the user is required to specify only the physical parameters and the boundary conditions to setup a test case.

11.5 Applications of the Poisson Solver

The next example, taken from [187], shows the solution of an electrostatic problem governed by the Poisson equation in three dimensions. The domain of interest corresponds to the exterior of 11 conducting spheres and it is discretised with a mesh of 35,895 quadratic tetrahedral elements, as shown in Fig. 40. This figure shows one of the implemented capabilities of the postprocessing library to display the faces corresponding to the exterior and interior faces of the mesh separately, with different colour and transparencies in each case. The first plot in Fig. 40 is produced by selecting the faces corresponding to the far field boundary and using a transparency. In a second phase, the exterior faces corresponding to the conducting spheres are displayed with no transparency. It is worth noting that the far field boundary and the boundary corresponding to the conducting spheres could be distinguished using either the boundary condition flag or simply imposing a condition on the faces to be displayed. The second plot of Fig. 40 is also obtained in two stages. First, the interior faces satisfying a condition corresponding to a positive \(x_2\) coordinate are displayed with a transparency. Second, the exterior faces corresponding to the conducting spheres is displayed with no transparency. When representing the interior and exterior faces, a constant element field is used to assign different colours and aid the visualisation.

Fig. 40
figure 40

Two views of the quadratic tetrahedral mesh used to solve the electrostatic problem

Dirichlet boundary conditions are considered in the whole boundary of the computational domain. A positive electrostatic potential of magnitude 5 is imposed on the central sphere and five of the surrounding spheres, whereas a negative potential of magnitude \(-5\) is imposed on the remaining spheres. On the outer boundary a zero potential is imposed. Figure 41 shows the 11 conducting spheres coloured according to the boundary condition imposed. This figure is produced by using the boundary condition flag to select the first set and the second set of spheres separately. After solving the problem with HDGlab, not only the primal variable corresponding to the electrostatic potential is obtained but also its gradient, which corresponds to the electric field in this example. Figure 42 shows the magnitude of the electric field on the surface of the 11 spheres.

Fig. 41
figure 41

The 11 conducting spheres coloured according to the boundary condition imposed. Red colour is used for the spheres where a positive potential is imposed, whereas blue is used for the spheres where a negative potential is imposed

Fig. 42
figure 42

Magnitude of the electric field on the surface of the 11 conducting spheres

The following example involves the solution of a heat transfer problem in a three dimensional mechanical component. The domain is discretised with a mesh of 6465 elements with \({\texttt {p}}=4\), as illustrated in Fig. 43.

Fig. 43
figure 43

Two views of the fourth order tetrahedral mesh used to solve the heat transfer problem

The first plot in Fig. 43 includes the high-order nodal distribution and the second plot in Fig. 43 shows the possibilities offered by the postprocessing library provided within HDGlab. In fact, it shows an extra functionality that can be added to display the intersection curves of the underlying CAD geometry when the user have access to this data.

Dirichlet and Neumann boundary conditions are imposed on the blue and red portions of the boundary, respectively, as shown in Fig. 44. In the Dirichlet part of the boundary a temperature equal to 10 is imposed, whereas the Neumann boundary condition is homogeneous, imposing that part of the boundary is perfectly insulated. The temperature field obtained after solving the problem with HDGlab is shown in Fig. 45.

Fig. 44
figure 44

The mechanical component coloured according to the boundary condition imposed. Red colour denotes a homogeneous Neumann boundary condition and the blue colour a Dirichlet boundary condition

Fig. 45
figure 45

Temperature distribution on the surface of the mechanical component

The last example considers the computation of the potential flow past a generic unmanned aerial vehicle (UAV). The domain is discretised using 101,923 tetrahedra of degree \({\texttt {p}} = 2\), as represented in Fig. 46. A Neumann boundary condition, corresponding to a unit velocity, is imposed on the inflow part of the boundary and a Dirichlet boundary condition corresponding to zero potential on the outflow. On the rest of the boundary a homogeneous Neumann boundary condition is enforced to represent a physical wall. After solving the problem with HDGlab, the flow potential and the velocity field are obtained. Figure 47 shows the magnitude of the velocity field on the surface of the UAV and the pressure field, computed using the Bernoulli equation.

Fig. 46
figure 46

Surface mesh of a generic UAV

Fig. 47
figure 47

Magnitude of the velocity field and pressure field on the surface of a generic UAV

This last example shows the applicability of the developed HDGlab to problems involving complex geometries, where the resulting global system has more than one million of equations. Despite the code was not developed with computational efficiency in mind, it still enables the interested users to solve relatively large problems and, with some additional improvements in terms of performance, it can be used for larger three dimensional problems.

12 Concluding Remarks

An open-source Matlab implementation of the HDG method for elliptic problems has been presented, the so-called HDGlab. The code is capable of solving problems governed by the Poisson and Stokes equations using high-order simplicial elements, including curved elements, by means of an isoparametric formulation.

HDGlab provides a suitable environment to those interested in the programming of hybrid methods in general and the HDG method in particular. The paper describes the HDG formulation employed for the Poisson and Stokes solvers and provides a very detailed description of the code, with particular emphasis in the data structures utilised. The presentation of the code involves the preprocess, computation and postprocess stages, and includes detailed explanations of the most relevant functions. A set of examples is presented to illustrate the use and the potential of HDGlab. The examples go from simple test cases with a known analytical solution, used to demonstrate the numerical properties of the HDG method, to more complex ones such as the computation of the potential flow around a UAV using high-order curved meshes.

HDGlab is available as an open-source software, released under the terms of the GNU General Public License version 3.0 or any later version (https://www.gnu.org/licenses) and is freely available from the repository: https://git.lacan.upc.edu/hybridLab/HDGlab.