This page was generated from unit-2.6-stokes/stokes.ipynb.

2.6 Stokes equation

Find \(u \in [H^1_D]^2\) and \(p \in L_2\) such that

\[\begin{split}\DeclareMathOperator{\Div}{div} \begin{array}{ccccll} \int \nabla u : \nabla v & + & \int \Div v \, p & = & \int f v & \forall \, v \\ \int \Div u \, q & & & = & 0 & \forall \, q \end{array}\end{split}\]

Define channel geometry and mesh it:

[1]:
from ngsolve import *
import netgen.gui

from netgen.geom2d import SplineGeometry
geo = SplineGeometry()
geo.AddRectangle( (0, 0), (2, 0.41), bcs = ("wall", "outlet", "wall", "inlet"))
geo.AddCircle ( (0.2, 0.2), r=0.05, leftdomain=0, rightdomain=1, bc="cyl")
mesh = Mesh( geo.GenerateMesh(maxh=0.05))
mesh.Curve(3)
Draw (mesh)

Use Taylor Hood finite element pairing: Continuous \(P^2\) elements for velocity, and continuous \(P^1\) for pressure:

[2]:
V = H1(mesh, order=2, dirichlet="wall|inlet|cyl")
Q = H1(mesh, order=1)
X = FESpace([V,V,Q])

Setup bilinear-form for Stokes. We give names for all scalar field components. The divergence is constructed from partial derivatives of the velocity components.

[3]:
ux,uy,p = X.TrialFunction()
vx,vy,q = X.TestFunction()

div_u = grad(ux)[0]+grad(uy)[1]
div_v = grad(vx)[0]+grad(vy)[1]

a = BilinearForm(X)
a += (grad(ux)*grad(vx)+grad(uy)*grad(vy) + div_u*q + div_v*p) * dx
a.Assemble()

Set inhomogeneous Dirichlet boundary condition only on inlet boundary:

[4]:
gfu = GridFunction(X)
uin = 1.5*4*y*(0.41-y)/(0.41*0.41)
gfu.components[0].Set(uin, definedon=mesh.Boundaries("inlet"))
velocity = CoefficientFunction(gfu.components[0:2])
Draw(velocity, mesh, "vel")
Draw(Norm(velocity), mesh, "|vel|")
SetVisualization(max=2)

Solve equation:

[5]:
res = gfu.vec.CreateVector()
res.data = -a.mat * gfu.vec
inv = a.mat.Inverse(freedofs=X.FreeDofs(), inverse="umfpack")
gfu.vec.data += inv * res
Redraw()

Testing different velocity-pressure pairs

Now we define a Stokes setup function to test different spaces:

[6]:
def SolveStokes(X):
    ux,uy,p = X.TrialFunction()
    vx,vy,q = X.TestFunction()
    div_u = grad(ux)[0]+grad(uy)[1]
    div_v = grad(vx)[0]+grad(vy)[1]
    a = BilinearForm(X)
    a += (grad(ux)*grad(vx)+grad(uy)*grad(vy) + div_u*q + div_v*p)*dx
    a.Assemble()
    gfu = GridFunction(X)
    uin = 1.5*4*y*(0.41-y)/(0.41*0.41)
    gfu.components[0].Set(uin, definedon=mesh.Boundaries("inlet"))
    res = gfu.vec.CreateVector()
    res.data = -a.mat * gfu.vec
    inv = a.mat.Inverse(freedofs=X.FreeDofs(), inverse="umfpack")
    gfu.vec.data += inv * res


    velocity = CoefficientFunction(gfu.components[0:2])
    Draw(velocity, mesh, "vel")
    Draw(Norm(velocity), mesh, "|vel|")
    SetVisualization(max=2)

    return gfu

Higher order Taylor-Hood elements:

[7]:
V = H1(mesh, order=4, dirichlet="wall|inlet|cyl")
Q = H1(mesh, order=3)
X = FESpace([V,V,Q])

gfu = SolveStokes(X)

With discontinuous pressure elements P2-P1 is unstable:

[8]:
V = H1(mesh, order=2, dirichlet="wall|inlet|cyl")
Q = L2(mesh, order=1)
print ("V.ndof =", V.ndof, ", Q.ndof =", Q.ndof)
X = FESpace([V,V,Q])

gfu = SolveStokes(X)
V.ndof = 1664 , Q.ndof = 2334
---------------------------------------------------------------------------
NgException                               Traceback (most recent call last)
<ipython-input-8-4e71f02dc134> in <module>
      4 X = FESpace([V,V,Q])
      5
----> 6 gfu = SolveStokes(X)

<ipython-input-6-abe06cca85c3> in SolveStokes(X)
     12     res = gfu.vec.CreateVector()
     13     res.data = -a.mat * gfu.vec
---> 14     inv = a.mat.Inverse(freedofs=X.FreeDofs(), inverse="umfpack")
     15     gfu.vec.data += inv * res
     16

NgException: UmfpackInverse: Numeric factorization failed.

\(P^{2,+} \times P^{1,dc}\) elements:

[9]:
V = H1(mesh, order=2, dirichlet="wall|inlet|cyl")
V.SetOrder(TRIG,3)
V.Update()
Q = L2(mesh, order=1)
X = FESpace([V,V,Q])
print ("V.ndof =", V.ndof, ", Q.ndof =", Q.ndof)

gfu = SolveStokes(X)
V.ndof = 2442 , Q.ndof = 2334

the mini element:

[10]:
V = H1(mesh, order=1, dirichlet="wall|inlet|cyl")
V.SetOrder(TRIG,3)
V.Update()
Q = H1(mesh, order=1)
X = FESpace([V,V,Q])

gfu = SolveStokes(X)

VectorH1

A vector-valued \(H^1\)-space: Less to type and more possibilities to explore structure and optimize.

[11]:
V = VectorH1(mesh, order=2, dirichlet="wall|inlet|cyl")
V.SetOrder(TRIG,3)
V.Update()
Q = L2(mesh, order=1)
X = FESpace([V,Q])

u,p = X.TrialFunction()
v,q = X.TestFunction()

a = BilinearForm(X)
a += (InnerProduct(grad(u),grad(v))+div(u)*q+div(v)*p)*dx
a.Assemble()

gfu = GridFunction(X)
uin = CoefficientFunction( (1.5*4*y*(0.41-y)/(0.41*0.41), 0) )
gfu.components[0].Set(uin, definedon=mesh.Boundaries("inlet"))

res = gfu.vec.CreateVector()
res.data = -a.mat * gfu.vec
inv = a.mat.Inverse(freedofs=X.FreeDofs(), inverse="umfpack")
gfu.vec.data += inv * res
Draw(gfu.components[0], mesh, "vel")
Draw(Norm(gfu.components[0]), mesh, "|vel|")
SetVisualization(max=2)

Stokes as a block-system

We can now define separate bilinear-form and matrices for A and B, and combine them to a block-system:

[12]:
V = VectorH1(mesh, order=3, dirichlet="wall|inlet|cyl")
Q = H1(mesh, order=2)

u,v = V.TnT()
p,q = Q.TnT()

a = BilinearForm(V)
a += InnerProduct(Grad(u),Grad(v))*dx

b = BilinearForm(trialspace=V, testspace=Q)
b += div(u)*q*dx

a.Assemble()
b.Assemble()

Needed as preconditioner for the pressure:

[13]:
mp = BilinearForm(Q)
mp += SymbolicBFI(p*q)
mp.Assemble()

Two right hand sides for the two spaces:

[14]:
f = LinearForm(V)
f += CoefficientFunction((0,x-0.5)) * v * dx
f.Assemble()

g = LinearForm(Q)
g.Assemble()

Two GridFunctions for velocity and pressure:

[15]:
gfu = GridFunction(V, name="u")
gfp = GridFunction(Q, name="p")
uin = CoefficientFunction( (1.5*4*y*(0.41-y)/(0.41*0.41), 0) )
gfu.Set(uin, definedon=mesh.Boundaries("inlet"))

Combine everything to a block-system. BlockMatrix and BlockVector store references to the original matrices and vectors, no new large matrices are allocated. The same for the transpose matrix b.mat.T. It stores a wrapper for the original matrix, and replaces the call of the Mult function by MultTrans.

[16]:
K = BlockMatrix( [ [a.mat, b.mat.T], [b.mat, None] ] )
C = BlockMatrix( [ [a.mat.Inverse(V.FreeDofs()), None], [None, mp.mat.Inverse()] ] )

rhs = BlockVector ( [f.vec, g.vec] )
sol = BlockVector( [gfu.vec, gfp.vec] )

solvers.MinRes (mat=K, pre=C, rhs=rhs, sol=sol, initialize=False)
it =  0  err =  4.2831916188823405
it =  1  err =  2.2528773428799926
it =  2  err =  1.8912623750393163
it =  3  err =  1.5266344210724205
it =  4  err =  1.4769403404550825
it =  5  err =  1.273980625789727
it =  6  err =  1.2267903137452287
it =  7  err =  1.0789374009715231
it =  8  err =  0.9924621944813976
it =  9  err =  0.8695500319639947
it =  10  err =  0.8166114003790829
it =  11  err =  0.7256695659723982
it =  12  err =  0.7041516510901927
it =  13  err =  0.6435700344170293
it =  14  err =  0.6270264097404598
it =  15  err =  0.5917402967076032
it =  16  err =  0.5779269147010139
it =  17  err =  0.5490852224638537
it =  18  err =  0.5321971427754211
it =  19  err =  0.5052077166325496
it =  20  err =  0.49403911435585673
it =  21  err =  0.4775045706658109
it =  22  err =  0.4599175452649691
it =  23  err =  0.4364003917551297
it =  24  err =  0.4145366596802775
it =  25  err =  0.3796222731122138
it =  26  err =  0.34688314643468915
it =  27  err =  0.3042742066224347
it =  28  err =  0.25808278756103575
it =  29  err =  0.19363875280799248
it =  30  err =  0.14913352783592562
it =  31  err =  0.09689047985658827
it =  32  err =  0.08231715365848459
it =  33  err =  0.047933852547070235
it =  34  err =  0.04660405092258501
it =  35  err =  0.024150763309553014
it =  36  err =  0.023949493623723794
it =  37  err =  0.013756611485447112
it =  38  err =  0.013752003715191651
it =  39  err =  0.009244585198913581
it =  40  err =  0.009139299552014488
it =  41  err =  0.006942884430257957
it =  42  err =  0.006885819957494805
it =  43  err =  0.004346352508280099
it =  44  err =  0.004326382358068149
it =  45  err =  0.0025357072287832162
it =  46  err =  0.0025334872923504777
it =  47  err =  0.001640050374687981
it =  48  err =  0.0016127539938019034
it =  49  err =  0.0010513788958569058
it =  50  err =  0.0010338708220353828
it =  51  err =  0.0005951248443777509
it =  52  err =  0.0005939594900701774
it =  53  err =  0.00039836168334434966
it =  54  err =  0.0003960466810400156
it =  55  err =  0.00025383957069783204
it =  56  err =  0.0002528758971194699
it =  57  err =  0.00013318813940194028
it =  58  err =  0.0001318624337366139
it =  59  err =  5.040358389150922e-05
it =  60  err =  5.0192981406831293e-05
it =  61  err =  2.4302123737278337e-05
it =  62  err =  2.406837620670656e-05
it =  63  err =  1.0875898578731561e-05
it =  64  err =  1.0874469185844444e-05
it =  65  err =  4.174224416979829e-06
it =  66  err =  4.167494990074168e-06
it =  67  err =  1.8101110873613084e-06
it =  68  err =  1.8098542481917512e-06
it =  69  err =  6.478119047235048e-07
it =  70  err =  6.47743831950252e-07
it =  71  err =  1.9731177756319453e-07
it =  72  err =  1.9718401936993724e-07
it =  73  err =  9.447140149261653e-08
[16]:
basevector
[17]:
Draw (gfu)
[ ]: