| 1 |
"""This demo program solves the incompressible Navier-Stokes equations |
| 2 |
on an L-shaped domain using Chorin's splitting method.""" |
| 3 |
|
| 4 |
# Copyright (C) 2010-2011 Anders Logg |
| 5 |
# |
| 6 |
# This file is part of DOLFIN. |
| 7 |
# |
| 8 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU Lesser General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# DOLFIN is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public License |
| 19 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
# Modified by Mikael Mortensen 2011 |
| 22 |
# |
| 23 |
# First added: 2010-08-30 |
| 24 |
# Last changed: 2011-06-30 |
| 25 |
|
| 26 |
# Begin demo |
| 27 |
|
| 28 |
|
| 29 |
import matplotlib.pyplot as plt |
| 30 |
from dolfin import * |
| 31 |
|
| 32 |
# Print log messages only from the root process in parallel |
| 33 |
parameters["std_out_all_processes"] = False; |
| 34 |
|
| 35 |
# Load mesh from file |
| 36 |
mesh = Mesh("../lshape.xml.gz") |
| 37 |
|
| 38 |
# Define function spaces (P2-P1) |
| 39 |
V = VectorFunctionSpace(mesh, "Lagrange", 2) |
| 40 |
Q = FunctionSpace(mesh, "Lagrange", 1) |
| 41 |
|
| 42 |
# Define trial and test functions |
| 43 |
u = TrialFunction(V) |
| 44 |
p = TrialFunction(Q) |
| 45 |
v = TestFunction(V) |
| 46 |
q = TestFunction(Q) |
| 47 |
|
| 48 |
# Set parameter values |
| 49 |
dt = 0.01 |
| 50 |
T = 3 |
| 51 |
nu = 0.01 |
| 52 |
|
| 53 |
# Define time-dependent pressure boundary condition |
| 54 |
p_in = Expression("sin(3.0*t)", t=0.0, degree=2) |
| 55 |
|
| 56 |
# Define boundary conditions |
| 57 |
noslip = DirichletBC(V, (0, 0), |
| 58 |
"on_boundary && \ |
| 59 |
(x[0] < DOLFIN_EPS | x[1] < DOLFIN_EPS | \ |
| 60 |
(x[0] > 0.5 - DOLFIN_EPS && x[1] > 0.5 - DOLFIN_EPS))") |
| 61 |
inflow = DirichletBC(Q, p_in, "x[1] > 1.0 - DOLFIN_EPS") |
| 62 |
outflow = DirichletBC(Q, 0, "x[0] > 1.0 - DOLFIN_EPS") |
| 63 |
bcu = [noslip] |
| 64 |
bcp = [inflow, outflow] |
| 65 |
|
| 66 |
# Create functions |
| 67 |
u0 = Function(V) |
| 68 |
u1 = Function(V) |
| 69 |
p1 = Function(Q) |
| 70 |
|
| 71 |
# Define coefficients |
| 72 |
k = Constant(dt) |
| 73 |
f = Constant((0, 0)) |
| 74 |
|
| 75 |
# Tentative velocity step |
| 76 |
F1 = (1/k)*inner(u - u0, v)*dx + inner(grad(u0)*u0, v)*dx + \ |
| 77 |
nu*inner(grad(u), grad(v))*dx - inner(f, v)*dx |
| 78 |
a1 = lhs(F1) |
| 79 |
L1 = rhs(F1) |
| 80 |
|
| 81 |
# Pressure update |
| 82 |
a2 = inner(grad(p), grad(q))*dx |
| 83 |
L2 = -(1/k)*div(u1)*q*dx |
| 84 |
|
| 85 |
# Velocity update |
| 86 |
a3 = inner(u, v)*dx |
| 87 |
L3 = inner(u1, v)*dx - k*inner(grad(p1), v)*dx |
| 88 |
|
| 89 |
# Assemble matrices |
| 90 |
A1 = assemble(a1) |
| 91 |
A2 = assemble(a2) |
| 92 |
A3 = assemble(a3) |
| 93 |
|
| 94 |
# Use amg preconditioner if available |
| 95 |
prec = "amg" if has_krylov_solver_preconditioner("amg") else "default" |
| 96 |
|
| 97 |
# Use nonzero guesses - essential for CG with non-symmetric BC |
| 98 |
parameters['krylov_solver']['nonzero_initial_guess'] = True |
| 99 |
|
| 100 |
# Create files for storing solution |
| 101 |
ufile = File("results/velocity.pvd") |
| 102 |
pfile = File("results/pressure.pvd") |
| 103 |
|
| 104 |
# Time-stepping |
| 105 |
t = dt |
| 106 |
while t < T + DOLFIN_EPS: |
| 107 |
|
| 108 |
# Update pressure boundary condition |
| 109 |
p_in.t = t |
| 110 |
|
| 111 |
# Compute tentative velocity step |
| 112 |
b1 = assemble(L1) |
| 113 |
[bc.apply(A1, b1) for bc in bcu] |
| 114 |
solve(A1, u1.vector(), b1, "bicgstab", "default") |
| 115 |
|
| 116 |
# Pressure correction |
| 117 |
b2 = assemble(L2) |
| 118 |
[bc.apply(A2, b2) for bc in bcp] |
| 119 |
[bc.apply(p1.vector()) for bc in bcp] |
| 120 |
solve(A2, p1.vector(), b2, "bicgstab", prec) |
| 121 |
|
| 122 |
# Velocity correction |
| 123 |
b3 = assemble(L3) |
| 124 |
[bc.apply(A3, b3) for bc in bcu] |
| 125 |
solve(A3, u1.vector(), b3, "bicgstab", "default") |
| 126 |
|
| 127 |
# Save to file |
| 128 |
ufile << u1 |
| 129 |
pfile << p1 |
| 130 |
|
| 131 |
# Move to next time step |
| 132 |
u0.assign(u1) |
| 133 |
t += dt |
| 134 |
|
| 135 |
# Plot solution |
| 136 |
plt.figure() |
| 137 |
plot(p1, title="Pressure") |
| 138 |
|
| 139 |
plt.figure() |
| 140 |
plot(u1, title="Velocity") |
| 141 |
|
| 142 |
plt.show() |