This page was generated from unit-2.10-dualbasis/dualbasis.ipynb.

2.10 Dual basis functions

We use dual basis functions to define interpolation operators, define transfer operators between different finite element spaces, and auxiliar space preconditioners.

Canonical interpolation

The canonical finite element interpolation operator is defined by specifying the degrees of freedom. For low order methods these are typically nodal values, while for high order methods these are most often moments. For example, the interpolation of a function \(u\) onto the \(p^{th}\) order triangle is given by: find \(u_{hp} \in V_{hp}\) such that

\begin{eqnarray*} u_{hp} (V) & = & u(V) \quad \forall \text{ vertices } V \\ \int_E u_{hp} q & = & \int_E u q \quad \forall q \in P^{p-2}(E) \; \forall \text{ edges } E \\ \int_T u_{hp} q & = & \int_T u q \quad \forall q \in P^{p-3}(T) \; \forall \text{ triangles } T \end{eqnarray*}
[1]:
import netgen.gui
from ngsolve import *
from netgen.geom2d import unit_square
import matplotlib.pylab as plt
mesh = Mesh(unit_square.GenerateMesh(maxh=2))

The NGSolve 'Set' function does local projection, and simple averaging. In particular, this does not respect point values in mesh vertices.

[2]:
fes = H1(mesh, order=3, low_order_space=False)

func = x*x*x*x
gfu = GridFunction(fes)
gfu.Set(func)
Draw (gfu)
print (gfu.vec)
 -0.0223792
 0.991843
 0.977621
 -0.00815655
 3.34984
 2.19912
 3.34984
       2
 0.00660131
 3.06756e-15
 0.00660131
 -3.19744e-14
 3.34984
 -1.80088
 -2.17601
 1.82399


Most NGSolve finite element spaces provide now a "dual" operator, which delivers the moments (i.e. the dual space basis functions) instead of function values. The integrals over faces, edges and also vertices are defined by co-dimension 1 (=BND), co-dimension 2 (=BBND) or co-dimension 3 (=BBBND) integrals over the volume elements. We define a variational problem for canonical interpolaion:

[3]:
u,v = fes.TnT()
vdual = v.Operator("dual")

a = BilinearForm(fes)
a += u*vdual*dx + u*vdual*dx(element_vb=BND) + \
    u*vdual*dx(element_vb=BBND)
a.Assemble()

f = LinearForm(fes)
f += func*vdual*dx + func*vdual*dx(element_vb=BND) + \
    func*vdual*dx(element_vb=BBND)
f.Assemble()

# interpolation in vertices preserves values 0 and 1
gfu.vec.data = a.mat.Inverse() * f.vec
print (gfu.vec)
Draw (gfu)
       0
       1
       1
       0
     3.6
       2
     3.6
       2
       0
       0
 -6.93335e-32
 -1.66533e-15
     3.6
      -2
      -2
       2


The vertex degrees of freedom vanish for edge and element basis functions, and the edge degrees of freedom vanish for element basis functions, but not vise versa. Thus, the obtained matrix A is block-triangular:

[4]:
import scipy.sparse as sp
A = sp.csr_matrix(a.mat.CSR())
plt.rcParams['figure.figsize'] = (4,4)
plt.spy(A)
plt.show()
../../_images/i-tutorials_unit-2.10-dualbasis_dualbasis_8_0.png

We can use proper block Gauss-Seidel smoothing for solving with that block triangular matrix by blocking the dofs for the individual vertices, edges and elements. Since the NGSolve Gauss-Seidel smoother reorders the order of smoothing blocks for parallelization, we have to take care to first compute vertex values, then edge values, and finally element values by running three different Gauss-Seidel sweeps.

[5]:
vblocks = [fes.GetDofNrs(vertex) for vertex in mesh.vertices]
eblocks = [fes.GetDofNrs(edge) for edge in mesh.edges]
fblocks = [fes.GetDofNrs(face) for face in mesh.faces]

print (vblocks)
print (eblocks)
print (fblocks)

vinv = a.mat.CreateBlockSmoother(vblocks)
einv = a.mat.CreateBlockSmoother(eblocks)
finv = a.mat.CreateBlockSmoother(fblocks)

vinv.Smooth(gfu.vec, f.vec)
einv.Smooth(gfu.vec, f.vec)
finv.Smooth(gfu.vec, f.vec)
print (gfu.vec)
[(0,), (1,), (2,), (3,)]
[(4, 5), (6, 7), (8, 9), (10, 11), (12, 13)]
[(14,), (15,)]
       0
       1
       1
       0
     3.6
       2
     3.6
       2
       0
       0
 -6.93335e-32
 -1.66533e-15
     3.6
      -2
      -2
       2


Embedding Finite Element Spaces

This interpolation can be used to transform functions from one finite element space \(V_{src}\) to another one \(V_{dst}\). We use the dual space of the destination space:

\[\int_{node} u_{dst} v_{dual} = \int_{node} u_{src} v_{dual} \qquad \forall \, v_{dual} \; \forall \, \text{nodes}\]

The left hand side leads to a non-symmetric square matrix, the right hand side to a rectangular matrix.

As an example we implement the transformation from an vector valued \(H^1\) space into \(H(\operatorname{div})\):

[6]:
import netgen.gui
from ngsolve import *
from netgen.geom2d import unit_square

mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))

fesh1 = VectorH1(mesh, order=2)
feshdiv = HDiv(mesh, order=2)

gfuh1 = GridFunction(fesh1)
gfuh1.Set ( (x*x,y*y) )

gfuhdiv = GridFunction(feshdiv, name="uhdiv")

Build the matrices, and use a direct solver:

[7]:
amixed = BilinearForm(trialspace=fesh1, testspace=feshdiv)
ahdiv = BilinearForm(feshdiv)

u,v = feshdiv.TnT()
vdual = v.Operator("dual")
uh1 = fesh1.TrialFunction()

dS = dx(element_boundary=True)
ahdiv += u*vdual * dx + u*vdual * dS
ahdiv.Assemble()

amixed += uh1*vdual*dx + uh1*vdual*dS
amixed.Assemble()

# build transformation operator:
transform = ahdiv.mat.Inverse() @ amixed.mat
gfuhdiv.vec.data = transform * gfuh1.vec

Draw (gfuh1)
Draw (gfuhdiv)

We implement a linear operator performing the fast conversion by Gauss-Seidel smoothing:

[8]:
class MyBlockInverse(BaseMatrix):
    def __init__ (self, mat, eblocks, fblocks):
        super(MyBlockInverse, self).__init__()
        self.mat = mat
        self.einv = mat.CreateBlockSmoother(eblocks)
        self.finv = mat.CreateBlockSmoother(fblocks)
        self.res = self.mat.CreateColVector()

    def CreateRowVector(self):
        return self.mat.CreateColVector()
    def CreateColVector(self):
        return self.mat.CreateRowVector()

    def Mult(self, x, y):
        # y[:] = 0
        # self.einv.Smooth(y,x)    #   y = y +  A_E^-1  (x - A y)
        # self.finv.Smooth(y,x)    #   y = y +  A_E^-1  (x - A y)

        # the same, but we see how to transpose that
        y.data = self.einv * x
        self.res.data = x - self.mat * y
        y.data += finv * self.res

    def MultTrans(self, x, y):
        y.data = self.finv.T * x
        self.res.data = x - self.mat.T * y
        y.data += einv.T * self.res


eblocks = [feshdiv.GetDofNrs(edge) for edge in mesh.edges]
fblocks = [feshdiv.GetDofNrs(face) for face in mesh.faces]

transform = MyBlockInverse(ahdiv.mat, eblocks, fblocks) @ amixed.mat
gfuhdiv.vec.data = transform * gfuh1.vec

Auxiliary Space Preconditioning

Nepomnyaschikh 91, Hiptmair-Xu 07, ….

Assume we have a complicated problem with some complicated discretization, and we have good preconditioners for a nodal finite element discretization for the Laplace operator. By auxiliary space preconditioning we can reuse the simple preconditioners for the complicated problems. It is simple, and works well in many cases.

As a simple example, we precondition a DG discretization by an \(H^1\) conforming method.

[9]:
mesh = Mesh(unit_square.GenerateMesh(maxh=0.1))

The DG discretization:

[10]:
order=4
fesDG = L2(mesh, order=order, dgjumps=True)
u,v = fesDG.TnT()
aDG = BilinearForm(fesDG)
jump_u = u-u.Other()
jump_v = v-v.Other()
n = specialcf.normal(2)
mean_dudn = 0.5*n * (grad(u)+grad(u.Other()))
mean_dvdn = 0.5*n * (grad(v)+grad(v.Other()))
alpha = 4
h = specialcf.mesh_size
aDG = BilinearForm(fesDG)
aDG += grad(u)*grad(v) * dx
aDG += alpha*order**2/h*jump_u*jump_v * dx(skeleton=True)
aDG += alpha*order**2/h*u*v * ds(skeleton=True)
aDG += (-mean_dudn*jump_v -mean_dvdn*jump_u) * dx(skeleton=True)
aDG += (-n*grad(u)*v-n*grad(v)*u)* ds(skeleton=True)
aDG.Assemble()

fDG = LinearForm(fesDG)
fDG += 1*v * dx
fDG.Assemble()
gfuDG = GridFunction(fesDG)

The auxiliary \(H^1\) discretization:

[11]:
fesH1 = H1(mesh, order=2, dirichlet=".*")
u,v = fesH1.TnT()
aH1 = BilinearForm(fesH1)
aH1 += grad(u)*grad(v)*dx
preH1 = Preconditioner(aH1, "bddc")
aH1.Assemble()
[12]:
transform = fesH1.ConvertL2Operator(fesDG)
pre = transform @ preH1.mat @ transform.T + aDG.mat.CreateSmoother()

solvers.CG(mat=aDG.mat, rhs=fDG.vec, sol=gfuDG.vec, pre=pre, \
           maxsteps=200)

Draw (gfuDG)
iteration 0 error = 0.2531562087169284
iteration 1 error = 0.0335406945650694
iteration 2 error = 0.008966767436643304
iteration 3 error = 0.004244873344653288
iteration 4 error = 0.0015376161568948893
iteration 5 error = 0.0010332060879173223
iteration 6 error = 0.0009244390324201369
iteration 7 error = 0.001111804789743911
iteration 8 error = 0.0009222415374385392
iteration 9 error = 0.0007264255257248383
iteration 10 error = 0.0005929804827760201
iteration 11 error = 0.000468090379792481
iteration 12 error = 0.00035810605131049504
iteration 13 error = 0.0003379748411397384
iteration 14 error = 0.00030818615296723594
iteration 15 error = 0.000257108552224743
iteration 16 error = 0.00020758445066797486
iteration 17 error = 0.0001630135484272246
iteration 18 error = 0.00011555145351081337
iteration 19 error = 8.887847926505628e-05
iteration 20 error = 7.557505071815006e-05
iteration 21 error = 7.004297233469563e-05
iteration 22 error = 5.5349264464186554e-05
iteration 23 error = 4.7853455576338675e-05
iteration 24 error = 3.999444092604909e-05
iteration 25 error = 3.2420221040142256e-05
iteration 26 error = 2.823496936071899e-05
iteration 27 error = 2.7918573201422033e-05
iteration 28 error = 2.6159948412659122e-05
iteration 29 error = 2.251544416837618e-05
iteration 30 error = 1.8959649652892553e-05
iteration 31 error = 1.6599252337777194e-05
iteration 32 error = 1.2858237814290514e-05
iteration 33 error = 1.0780280691368843e-05
iteration 34 error = 9.81326358015328e-06
iteration 35 error = 9.511991826112328e-06
iteration 36 error = 7.736168049596286e-06
iteration 37 error = 5.8385733389326866e-06
iteration 38 error = 4.700205388534236e-06
iteration 39 error = 4.114894872877834e-06
iteration 40 error = 3.5971187294361588e-06
iteration 41 error = 3.4378511827962837e-06
iteration 42 error = 3.19971352994794e-06
iteration 43 error = 2.6052414477556717e-06
iteration 44 error = 1.977655410265143e-06
iteration 45 error = 1.6368258923881989e-06
iteration 46 error = 1.5048006323762539e-06
iteration 47 error = 1.5089149162141367e-06
iteration 48 error = 1.3005968754797115e-06
iteration 49 error = 1.0628548741850528e-06
iteration 50 error = 8.394651016687359e-07
iteration 51 error = 6.869534625109417e-07
iteration 52 error = 6.030181390217613e-07
iteration 53 error = 5.806640830972459e-07
iteration 54 error = 5.118881802522138e-07
iteration 55 error = 4.197551858599492e-07
iteration 56 error = 3.312379338139005e-07
iteration 57 error = 2.7647684492450193e-07
iteration 58 error = 2.323096974853238e-07
iteration 59 error = 1.9363597354544476e-07
iteration 60 error = 1.773990816971846e-07
iteration 61 error = 1.4835417017881775e-07
iteration 62 error = 1.1551910685775266e-07
iteration 63 error = 9.147010803256191e-08
iteration 64 error = 7.047606498655773e-08
iteration 65 error = 6.153871310481995e-08
iteration 66 error = 5.7479369665880535e-08
iteration 67 error = 5.0983948049669474e-08
iteration 68 error = 4.093094255240317e-08
iteration 69 error = 3.275633691301039e-08
iteration 70 error = 2.6619245253407175e-08
iteration 71 error = 2.1163878974863918e-08
iteration 72 error = 1.80920962069709e-08
iteration 73 error = 1.6750432558325982e-08
iteration 74 error = 1.4528858094579712e-08
iteration 75 error = 1.1849791301993889e-08
iteration 76 error = 9.140291067511009e-09
iteration 77 error = 7.167838490939565e-09
iteration 78 error = 6.091385533034801e-09
iteration 79 error = 5.681160498682556e-09
iteration 80 error = 5.388073866733192e-09
iteration 81 error = 4.2828516403015655e-09
iteration 82 error = 3.2723083252208317e-09
iteration 83 error = 2.5840034773213857e-09
iteration 84 error = 2.228831813822195e-09
iteration 85 error = 2.0695617327821083e-09
iteration 86 error = 1.8760227085517856e-09
iteration 87 error = 1.5560994909351366e-09
iteration 88 error = 1.130308087545308e-09
iteration 89 error = 8.768035731766498e-10
iteration 90 error = 7.491352171498777e-10
iteration 91 error = 6.784946641408394e-10
iteration 92 error = 6.197424365934995e-10
iteration 93 error = 5.561135384859722e-10
iteration 94 error = 4.090771593307423e-10
iteration 95 error = 3.1573825940887904e-10
iteration 96 error = 2.5868447246910465e-10
iteration 97 error = 2.483667221293898e-10
iteration 98 error = 2.338695270072709e-10
iteration 99 error = 1.9590166739430333e-10
iteration 100 error = 1.5208242502309458e-10
iteration 101 error = 1.1658643217378076e-10
iteration 102 error = 9.844076436849347e-11
iteration 103 error = 9.395137979689745e-11
iteration 104 error = 8.84353414284689e-11
iteration 105 error = 7.259765617119655e-11
iteration 106 error = 5.288009345107834e-11
iteration 107 error = 4.1990836020998684e-11
iteration 108 error = 3.6224394057660766e-11
iteration 109 error = 3.2673646593508235e-11
iteration 110 error = 3.1141127488935616e-11
iteration 111 error = 2.6954660544716957e-11
iteration 112 error = 2.0983416804683325e-11
iteration 113 error = 1.5409273709582017e-11
iteration 114 error = 1.237487206583303e-11
iteration 115 error = 1.090902664031212e-11
iteration 116 error = 1.076613147431033e-11
iteration 117 error = 9.7664211729104e-12
iteration 118 error = 7.566493930665461e-12
iteration 119 error = 5.989372950247079e-12
iteration 120 error = 4.6407134461863746e-12
iteration 121 error = 4.021875290313864e-12
iteration 122 error = 3.88670138902049e-12
iteration 123 error = 3.5382361048564624e-12
iteration 124 error = 2.7101625906075977e-12
iteration 125 error = 2.186351259606979e-12
iteration 126 error = 1.7953763906429431e-12
iteration 127 error = 1.6353600338162495e-12
iteration 128 error = 1.5375607880153341e-12
iteration 129 error = 1.3544958861346412e-12
iteration 130 error = 1.1494358940044177e-12
iteration 131 error = 9.027347730546971e-13
iteration 132 error = 7.171464929592474e-13
iteration 133 error = 6.086071692185757e-13
iteration 134 error = 5.715616354525191e-13
iteration 135 error = 5.413835890182004e-13
iteration 136 error = 4.882067032838562e-13
iteration 137 error = 4.0682579929333557e-13
iteration 138 error = 3.330718322434454e-13
iteration 139 error = 2.6981987651503277e-13
iteration 140 error = 2.4056514941467115e-13
[ ]: