StokesFlowSimulation Tutorial
StokesFlowSimulation Tutorial
BRENDAN HUANG
The layout of this document is as follows. I will first introduce the general problem
of simulating Stokes flow. I will then discuss the theory and implementation of two
numerical methods. The first, the method of fundamental solutions (MFS), is easy
to implement but often less numerically precise. The second, the boundary element
method (BEM), offers better precision but is more complicated and computationally
intense to implement. The goal of this document is for users to be able to reuse as
much code as possible for their own purposes.
𝜕u 1 𝜇
(1) + (u · ∇)u = f − ∇𝑝 + ∇2 u
𝜕𝑡 𝜌 𝜌
(2) ∇·u=0
As written above, the Navier-Stokes equation is actually a compact way of writing
three coupled partial differential equations in addition to the incompressibility equa-
tion. From a mathematical perspective, in order to solve these coupled differential
equations, we need to specify:
∙ The geometry of the problem, including: the domain of where the problem is
to be solved, as well as the boundaries or edges of the domain.
∙ The boundary conditions for either 𝑢, 𝑣, and 𝑤, defined on the boundary
(Dirichlet boundary conditions), the traction f on the surface (Neumann
boundary conditions), or some combination of the two (mixed and Robin
boundary conditions).
∙ The initial conditions of the problem, if the flow is non-steady state.
From a very high level perspective, many computational fluid dynamic (CFD)
models solve Navier-Stokes (or other constitutive fluid equations) by dividing the
domain into a mesh of volume elements and then using numerical methods to solve
the resulting equations. Common techniques such as the finite difference method,
finite volume method, and finite element method work by solving the equations of
motion on the domain of the fluid. That is, for a volume of fluid flow, the volume is
discretized up into small sub-volumes. The equations of motion are solved in these
sub-volumes.
In another set of CFD schemes, special properties of Navier-Stokes are taken ad-
vantage of in order to avoid having to discretize the entire domain. The advantage
is that these methods take three-dimensional mesh structures and recast them as
STOKES FLOW SIMULATION IN MATLAB 3
(3) 𝐿𝑢 = 𝑓
𝐿𝑢1 = 𝑓1
𝐿𝑢2 = 𝑓2
𝐿𝑢1 + 𝐿𝑢2 = 𝑓1 + 𝑓2
𝐿(𝑢1 + 𝑢2 ) = (𝑓1 + 𝑓2 )
and thus we see that if 𝐿(𝑢1 + 𝑢2 ) = 𝐿𝑢1 + 𝐿𝑢2 , then 𝑢1 + 𝑢2 is also a valid solution.
Linear superposition is a very useful mathematical tool because we can solve for sim-
ple solutions to our differential equations, usually implicitly choosing a mathematical
basis, and then reconstruct any arbitrary solution in terms of that mathematical ba-
sis. Such linear superposability forms the mathematical basis for constructive and
destructive interference in Maxwell’s equations or the Schrödinger equation, for ex-
ample.
In addition to its important physical ramifications, linearity also has important
properties for mathematical analysis. Specifically, if we consider the term 𝑓 from Eq
3 often termed our source function, to be an impulse 𝛿(x − x0 ) (where 𝛿 is the Dirac
delta function), then 𝑦 is known as our Green’s function:
4 BRENDAN HUANG
(4) 𝐿𝑦 = 𝛿(x − r0 )
(5) 𝑦 ≡ 𝐺(x, r0 )
The physical interpretation of a Green’s function 𝐺(x, x0 ) is as the function that
specifies the resulting field or action in response to a point source. It is often known
as a spatial or temporal impulse response. By summing up the response to many
impulses, we can get the total resulting field from a complex distribution. For exam-
ple, in electrostatics a single point charge generates an electric field given by Gauss’
Law. If we want to know what the electric field is in a geometry with a distribution
of charges, we merely have to place a distribution of point sources in the proper loca-
tions, and sum up all of their contributions. As will be discussed shortly, properties
of Green’s functions are central to both of our computational schemes, but an impor-
tant underlying assumption is that the Green’s function solves a linear differential
equation.
The concept of linearity is important because Navier-Stokes is a non-linear differ-
ential equation. More explicitly, if we have a solution u1 and u2 , u1 + u2 is not a
solution. This property arises as a result of the convective acceleration term in Eq.
1:
𝜕u 1 𝜇
(6) = − ∇𝑝 + ∇2 u
𝜕𝑡 𝜌 𝜌
Finally, in the case of steady state flow ( 𝜕u
𝜕𝑡
= 0), we recover the Stokes equation:
(7) ∇𝑝 = 𝜇∇2 u
We have highlight these assumptions because in our numerical simulations, we
will not be solving the full Navier-Stokes equation, but rather the Stokes equation.
Our methods rely heavily on Green’s functions, which require linearity. Thus, it
STOKES FLOW SIMULATION IN MATLAB 5
is important to keep in mind that our simulations will only be valid for (a) Low
reynolds number and (b) steady state flow.
With the mathematical background introduced, we can now proceed to our first
simulation method: the method of fundamental solutions.
3. The Method of Fundamental Solutions
The underlying principle of the method of fundamental solutions, or MFS, is as
follows. We first find the solutions to a particular differential equation ignoring of
boundary conditions. (In this particular case, the fundamental solutions will be the
Green’s functions associated with a point force.) We then superpose those solutions
in a way such that that they satisfy the boundary conditions of the problem. Because
each basis function satisfies the Stokes equation, and because Stokes equation is
linear, any linear combination of functions also satisfies the Stokes equation. Adding
them in such a way that the boundary conditions are satisfied then completes the
requirements for solving our partial differential equation.
The major advantages of MFS is that it is (a) mesh-free, (b) avoids integrating over
singularities, and (c) requires no numerical integration, making it computationally
low-cost.
MFS has several disadvantages, too. First, the method fundamentally requires
the placement of fictitious forces outside the domain on which the solution is solved,
and it is unclear what physical interpretation these forces should have. Additionally,
because singular functions are not directly embedded in the boundary, the method
does not estimate higher-derivatives (i.e. traction) well. Finally, the way in which
we set up our equations to solve, we are often left with an ill-posed inverse problem
which may be sensitive to our placement of the point forces. Given that the forces are
a somewhat fictitious construct, and and their specific location is arbitrary and not
dictated by the physical problem, an ideal simulation method would be insensitive
to their location. The flow fields generated by MFS, however, can be sensitive to the
placement of these forces.
3.0.1. Theory of MFS. In this section, we will describe the application of MFS, a
general technique to numerically solve differential equations, towards Stokes flow.
For the purposes of this explanation, we will consider the 2D case, although it will
be generalizable to 3D as well.
Our goal is to solve Stokes equation, Eq 7, while satisfying specific boundary
conditions for 𝑢 and 𝑣. In order to do so, we consider the basis function associated
with a point source f , which is called a Stokeslet:
1 𝜇
(8) − ∇𝑝 + ∇2 u = f 𝛿(x − x0 )
𝜌 𝜌
6 BRENDAN HUANG
where x = (𝑥, 𝑦) is the spatial coordinate parameterizing the flow field, and x0 =
(𝑥0 , 𝑦0 ) is the spatial location of our Stokeslet point force f . The solution of this
equation tells us that if we pick a location x0 to place a Stokeslet, what the resulting
flow field and pressure will be at any other arbitrary location x.
The specific form of the Stokeslet, also called the Oseen-Burgers tensor is (in 2D):
1
(9) 𝑢𝑖 (x) = 𝐺𝑖𝑗 (x, x0 )𝑓𝑗 (x0 )
8𝜋𝜇
𝑥ˆ𝑖 𝑥ˆ𝑗
(10) 𝐺𝑖𝑗 (x, x0 ) = −𝛿𝑖𝑗 ln 𝑟 + 2
𝑟
where x̂ = x − x0 and 𝑟 ≡ |x̂|. The interpretation of 𝐺𝑖𝑗 (x, x0 ) is a tensor quantity
that describes how the flow field at a location is induced by a point force located x̂
away.
Because of the linearity of Stokes equation, we can add many of these Stokeslets
together. The basic idea of MFS, then, is to specify our boundary conditions at 𝑁
discrete locations at our boundary. We then place an equivalent number of Stokeslets
in the vicinity of each of these locations. Although all Stokeslets will influence the
flow at every location, the magnitude of influence that any given Stokeslet has on
a location at the boundary will depend on its proximity to that location. Math-
ematically speaking, if we have 𝑁 boundary points with 𝑑 values of vectorial flow
components, each of those 𝑁 Stokeslets will be coupled to every boundary point,
leading to 𝑁 × 𝑑 coupled equations. In these equations, the magnitude of each
Stokeslet unknown. We can then solve for the magnitude of each of those Stokeslets
such that the boundary conditions are satisfied.
The MFS process yields a solution because (1) each of the Stokeslets is a solution
to the Stokes equation, (2) because they can be added in a linear fashion, and
(3) because we are setting them up in a way such that our boundary conditions
are satisfied. Thus, any solution that we get out of adding the Stokeslets together
satisfies our original criteria for solving the Stokes equation.
More explicitly, we start by discretizing our boundary at a set of locations x𝑚 .
We then specify a boundary condition 𝑢(x𝑚 ) and 𝑣(x𝑚 ) at each of these locations.
For each of the 𝑁 boundary conditions, we place a nearby Stokeslet f at a location
x𝑛 . At this point in the process, we specify the location of the Stokeslet, but not
the magnitude of the force f = (𝑓, 𝑔) If we then calculate the flow field 𝑢𝑖 at a given
boundary point x𝑚 due to the contribution of all the Stokeslets, we get:
∑︁
𝑢𝑖 (x𝑚 ) = 𝐺𝑖𝑗 (x𝑚 , x𝑛 )𝑓𝑗 (x𝑛 )
𝑛
STOKES FLOW SIMULATION IN MATLAB 7
problem. This can be accomplished in exactly the same way as solving for Eq 12,
except instead of calculating the inverse problem, we compute the forward equation:
Of note, the number of Stokeslets need not be equal to the number of interior
points. If there are 𝑁 Stokeslets and 𝐿 interior points, for example, then 𝐺𝑖𝑛𝑡 will
be an 2𝐿 × 2𝑁 matrix.
4.0.1. Overall execution script. The script to run the entire MFS is given by the
“doit” script. The current implementation is only in 2D. (The 3D problem becomes
highly ill-conditioned with very boundary points specified.). The sample script sim-
ulates a problem similar to the lid-driven cavity, but with only a portion of the lid
STOKES FLOW SIMULATION IN MATLAB 9
(surface) moving.
MFS Doit Scripts
Script doit_sim_MFS.m
Inputs 2D only
Outputs Vectorial flow field, pressure, streamfunction
4.0.2. Defining geometry and boundary conditions. In the first step, the geometry
of the problem is defined. We can have open boundary conditions, where the there
is no enclosure of the fluid, or closed boundary conditions, where the fluid flow is
constrained to a chamber. We can also specify the relative distance of the Stokeslets
from the boundary. In this simulation, a Stokeslet is paired next a boundary point
and placed along a line connecting the boundary point to the center of mass of the
problem.
4.0.3. Calculating the susceptibility matrices. The next step is to calculate the effect
of each Stokeslet on each point in the boundary.
Calculating elements of susceptibility matrix
Script calc_Amat_MFS_2D.m
Inputs scx,scy location of Stokeslets; bcx, bcy location of
boundary points
Outputs Amat Susceptibility matrix relating each Stokeslet to
each boundary point; optional Apres Susceptibility ma-
trix of pressure due to Stokeslets; Apsi Susceptibility
matrix of streamfunction due to Stokeslets
4.0.4. Solving for the flow in the interior domain. To solve the inverse equation, we
use a built in function from Matlab. Once each Stokeslet has a magnitude from solv-
ing the inverse equation, a second susceptibility matrix can be calculated to compute
10 BRENDAN HUANG
the flow. Conveniently, we can also propagate the Stokeslets in order directly esti-
mate the pressure and streamline inside the domain.
Computing quantities in the domain
Script calcUV_MFS.m, calcP.m
Inputs qcoeff strength of Stokeslets; Amat Susceptibility ma-
trix relating each Stokeslet to interior points; xpts, ypts:
number of points to calculate in the interior; Apres, Apsi
Susceptibility matrices for pressure and streamfunction.
Outputs u,v vectorial flow field ; optional pres, psi pressure field,
streamfunction
is not ill-posed in the same manner as the first kind. As a result, BEM simulations
are often less sensitive to small numerical errors.
In this section, we will first motivate the underlying mathematical principle of
BEM. We will then discuss our specific implementation of the procedure.
5.1. Interlude: The Reciprocal Relations. The mathematical basis of BEM lies
in Green’s second identity, or the reciprocal identity. In this section, we will give
some underlying intuition to the procedure by considering the reciprocal identity
as applied towards a simpler problem: Laplace’s Equation. Laplace’s equation is
defined by:
(15) ∇2 𝑓 = 0
Let’s consider two separate functions 𝑓 and 𝜑 that are not the same but both
satisfy Laplace’s equation. Specifically, let’s consider the quantity 𝜑∇2 𝑓 . We can
write the identity:
(19) 𝑓 ∇2 𝜑 = ∇ · (𝑓 ∇𝜑) − ∇𝑓 · ∇𝜑
and subtracting Eq. 19 from Eq. 16, while noting that ∇2 𝑓 = 0 and ∇2 𝜑 = 0, we
get:
(22) ∇2 𝜑 = 𝑓 𝛿(x − x0 )
(23) 𝜑 ≡ 𝐺(x, x0 )
We recover the boundary integral equation:
12 BRENDAN HUANG
ˆ ˆ ˆ
(24) (𝐺∇𝑓 − 𝑓 ∇𝐺) · 𝑛
ˆ 𝑑𝐴 = 𝑓 (x)𝛿(x − x0 )
ˆ
(25) (𝐺∇𝑓 − 𝑓 ∇𝐺) · 𝑛
ˆ 𝑑𝐴 = 𝑓 (x0 )
5.2. The Reciprocal Relations in Stokes Flow. In the previous section, we gave
a simple example of Green’s second identity in order to motivate its use and provide
a physical interpretation. Using a similar analysis, we can derive the reciprocal
relations for the Stokes equation. The goal of the reciprocal relation will be to derive
the boundary integral equations, Eqs 38 and 39. Analogous to Eq. 13 in MFS,
the boundary integral equation will be the equation we actually numerically solve in
order to simulate Stokes flow in our domain.
We begin with a statement of Stokes flow in terms of the Cauchy stress tensor
𝜎 ≡ 𝜎𝑖𝑗 :
(26) 𝜕𝑖 𝜎𝑖𝑗 = 0
(27) 𝜎𝑖𝑗 = 𝑝𝛿𝑖𝑗 + 𝜇(𝜕𝑖 𝑢𝑗 + 𝜕𝑗 𝑢𝑖 )
1In
fact, the ability to reconstruct an electric field at a point in the domain by specifying the
boundary is taken advantage of in Kirchoff scalar diffraction theory. The Kirchoff integral, a con-
struct used to numerically simulate electromagnetic diffraction, takes advantage of Eq 25. Notably,
the use of Eq 25 leads to the celebrated Rayleigh-Sommerfeld diffraction integral.
STOKES FLOW SIMULATION IN MATLAB 13
and we can eliminate the pressure term 𝑝 by recalling that 𝛿𝑖𝑗 𝜕𝑗 𝑢′𝑖 = 𝜕𝑖 𝑢′𝑖 = 0, where
𝜕𝑖 𝑢𝑖 = 0 is a statement of incompressibility.
Writing equivalent equations for 𝑢′𝑖 and 𝑢𝑖 :
where now Eq 33 defines the reciprocal relation for Stokes flow. As in the case of
Laplace’s equation, we can associate u′ instead with the Green’s function of the
Stokes equation, i.e.
1
(36) 𝜎𝑖𝑘 = 𝑇𝑖𝑗𝑘 𝑓𝑗
8𝜋
Now, plugging in our Green’s functions into the Lorentz reciprocal relation, we
recover:
(︂ )︂
1 1
(37) 𝜕𝑖 𝐺𝑗𝑚 𝑓𝑚 𝜎𝑖𝑗 − 𝑢𝑖 𝑇𝑖𝑚𝑗 𝑓𝑚 = 𝑓𝑚 𝑢𝑚 𝛿(x − x0 )
8𝜋𝜇 8𝜋
14 BRENDAN HUANG
5.3. The boundary integral equations. Much like Eq. 25, in order to convert
Eq. 37 into a more useful form, we now take the volume integral of the right side
of Eq. 37. The integral will have different values in two scenarios that we must
consider: when the delta function f 𝛿(x − x0 ) is placed inside the domain, and when
it is placed on the boundary.
If we consider a point x0 on the boundary, then we have to take the principal value
𝑃 𝑉 of the integral, leading to:
(38)
ˆ ˆ 𝑃𝑉
1 1
𝑢𝑗 (x0 ) = − 𝐺𝑗𝑖 (x0 , x)𝑓𝑖 (x)𝑑𝑆(x) + 𝑢𝑖 (x)𝑇𝑖𝑗𝑘 (x0 , x)𝑛𝑘 (x)𝑑𝑆(x)
4𝜋𝜇 𝐷 4𝜋 𝐷
⎛ ⎞ ⎞ ⎛ 𝑓 (x ) ⎞
𝑢(x1 ) ⎛
1
.. .
.. ⎟
. 𝐺𝑥𝑥 (x𝑚 , x𝑛 ) 𝐺𝑥𝑦 (x𝑚 , x𝑛 ) ⎟
⎜ ⎟ ⎜ ⎜
⎜ ⎟ ⎜ ⎟⎜ ⎟
𝑢(x𝑚 ) ⎟ ⎜ 𝑓 (x𝑛 ) ⎟
⎜ ⎟ ⎜ ⎜
⎟=⎜ +
⎜ ⎟ ⎜ ⎟⎜ ⎟
𝑣(x1 ) ⎟ ⎜ 𝑔(x1 ) ⎟
⎜
⎜ ⎟ ⎜ ⎟
.. 𝐺𝑦𝑥 (x𝑚 , x𝑛 ) 𝐺𝑦𝑦 (x𝑚 , x𝑛 ) ⎠ ⎝ ... ⎟
⎜ ⎟ ⎝ ⎟⎜
⎝ . ⎠ ⎠
𝑣(x𝑚 ) 𝑔(x𝑛 )
⎛ ⎞ ⎛ 𝑢(x ) ⎞
1
⎜ .. ⎟
⎜
⎜ 𝑇𝑥𝑥 (x𝑚 , x𝑛 ) 𝑇𝑥𝑦 (x𝑚 , x𝑛 ) ⎟ ⎜ . ⎟
⎟
⎟⎜ 𝑢(x𝑛 ) ⎟
⎜ ⎟
+⎜
⎜
⎟⎜
⎟ ⎜ 𝑣(x1 ) ⎟
⎜
⎟
𝑇𝑦𝑥 (x𝑚 , x𝑛 ) 𝑇𝑦𝑦 (x𝑚 , x𝑛 ) ⎠ ⎝ ... ⎠
⎜ ⎟⎜ ⎟
⎝
𝑣(x𝑛 )
or, more compactly written:
(42) f = 𝐺−1 (1 − 𝑇 )u
On the other hand, if we specify the traction, then we solve for u, and it becomes a
Fredholm integral of the second kind with solution:
(43) u = (1 − 𝑇 )−1 𝐺f
Similarly to MFS, once we have solved for u and f on the boundary, we can then
solve for the flow field on the domain:
6.0.1. Execution script. The first script is the “doit script,” the script that runs the
entire simulation. There are two: one that runs a two-dimensional simulation, and
one that runs a three-dimensional simulation. Both are capable of outputing the
simulated vectorial flow field, pressure, and shear stress tensor. Automated compu-
tation of the streamfunction is possible but has not yet been implemented.
STOKES FLOW SIMULATION IN MATLAB 17
6.0.2. Defining geometry and boundary conditions. In the next script, we define the
geometry and boundary conditions. Again, our goal is to simulate a small patch of
moving surface that is embedded in no-slip surface (analogous to lid-driven cavity).
As described in the MFS section, we have the choice between an open or closed
boundary condition. In addition, however, we also the choice between specifying the
force (traction) or the velocity on the moving surface. In this implementation, we
finally can choose whether to simulate Stokes flow in 2D or 3D.
6.0.5. Solving the boundary integral equation. Here the boundary integral equation
is solved. The important caveat is that in the case of mixed boundary conditions (i.e.
specifying traction on the flow portion of the surface, but specifying zero velocity
STOKES FLOW SIMULATION IN MATLAB 19