| 1 |
# Copyright (C) 2015 Anders Logg |
| 2 |
# |
| 3 |
# This file is part of DOLFIN. |
| 4 |
# |
| 5 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU Lesser General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# DOLFIN is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public License |
| 16 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
# First added: 2015-11-11 |
| 19 |
# Last changed: 2017-05-25 |
| 20 |
# |
| 21 |
# This demo program solves the Stokes equations on a domain defined |
| 22 |
# by three overlapping and non-matching meshes. |
| 23 |
|
| 24 |
from dolfin import * |
| 25 |
|
| 26 |
|
| 27 |
class InflowBoundary(SubDomain): |
| 28 |
def inside(self, x, on_boundary): |
| 29 |
return on_boundary and near(x[0], 0.0) |
| 30 |
|
| 31 |
class OutflowBoundary(SubDomain): |
| 32 |
def inside(self, x, on_boundary): |
| 33 |
return on_boundary and near(x[0], 1.0) |
| 34 |
|
| 35 |
class NoslipBoundary(SubDomain): |
| 36 |
def inside(self, x, on_boundary): |
| 37 |
return on_boundary and (near(x[1], 0.0) or near(x[1], 1.0)) |
| 38 |
|
| 39 |
if MPI.size(MPI.comm_world) > 1: |
| 40 |
info("Sorry, this demo does not (yet) run in parallel.") |
| 41 |
exit(0) |
| 42 |
|
| 43 |
# Create meshes |
| 44 |
mesh_0 = UnitSquareMesh(16, 16) |
| 45 |
mesh_1 = RectangleMesh(Point(0.2, 0.2), Point(0.6, 0.6), 8, 8) |
| 46 |
mesh_2 = RectangleMesh(Point(0.4, 0.4), Point(0.8, 0.8), 8, 8) |
| 47 |
|
| 48 |
# Build multimesh |
| 49 |
multimesh = MultiMesh() |
| 50 |
multimesh.add(mesh_0) |
| 51 |
multimesh.add(mesh_1) |
| 52 |
multimesh.add(mesh_2) |
| 53 |
multimesh.build() |
| 54 |
|
| 55 |
# Create function space |
| 56 |
P2 = VectorElement("Lagrange", triangle, 2) |
| 57 |
P1 = FiniteElement("Lagrange", triangle, 1) |
| 58 |
TH = P2 * P1 |
| 59 |
W = MultiMeshFunctionSpace(multimesh, TH) |
| 60 |
|
| 61 |
# Define trial and test functions and right-hand side |
| 62 |
(u, p) = TrialFunctions(W) |
| 63 |
(v, q) = TestFunctions(W) |
| 64 |
f = Constant((0, 0)) |
| 65 |
|
| 66 |
# Define facet normal and mesh size |
| 67 |
n = FacetNormal(multimesh) |
| 68 |
h = 2.0*Circumradius(multimesh) |
| 69 |
|
| 70 |
# Parameters |
| 71 |
alpha = 20.0 |
| 72 |
beta = 5.0 |
| 73 |
|
| 74 |
def tensor_jump(v, n): |
| 75 |
return outer(v('+'), n('+')) + outer(v('-'), n('-')) |
| 76 |
|
| 77 |
def a_h(v, w): |
| 78 |
return inner(grad(v), grad(w))*dX \ |
| 79 |
- inner(avg(grad(v)), tensor_jump(w, n))*dI \ |
| 80 |
- inner(avg(grad(w)), tensor_jump(v, n))*dI \ |
| 81 |
+ alpha/avg(h) * inner(jump(v), jump(w))*dI |
| 82 |
|
| 83 |
def b_h(v, q): |
| 84 |
return -div(v)*q*dX + jump(v, n)*avg(q)*dI |
| 85 |
|
| 86 |
def l_h(v, q, f): |
| 87 |
return inner(f, v)*dX |
| 88 |
|
| 89 |
def s_O(v, q, w, r): |
| 90 |
return (beta/avg(h)**2) * inner(jump(v), jump(w))*dO \ |
| 91 |
+ beta * inner(jump(q), jump(r))*dO |
| 92 |
|
| 93 |
def s_C(v, q, w, r): |
| 94 |
return h*h*inner(-div(grad(v)) + grad(q), -div(grad(w)) - grad(r))*dC |
| 95 |
|
| 96 |
def l_C(v, q, f): |
| 97 |
return h*h*inner(f, -div(grad(v)) - grad(q))*dC |
| 98 |
|
| 99 |
# Define bilinear form |
| 100 |
a = a_h(u, v) + b_h(v, p) + b_h(u, q) + s_O(u, p, v, q) + s_C(u, p, v, q) |
| 101 |
|
| 102 |
# Define linear form |
| 103 |
L = l_h(v, q, f) + l_C(v, q, f) |
| 104 |
|
| 105 |
# Assemble linear system |
| 106 |
A = assemble_multimesh(a) |
| 107 |
b = assemble_multimesh(L) |
| 108 |
|
| 109 |
# Create boundary values |
| 110 |
inflow_value = Expression(("sin(x[1]*DOLFIN_PI)", "0.0"), degree=2) |
| 111 |
outflow_value = Constant(0) |
| 112 |
noslip_value = Constant((0, 0)) |
| 113 |
|
| 114 |
# Create subdomains for boundary conditions |
| 115 |
inflow_boundary = InflowBoundary() |
| 116 |
outflow_boundary = OutflowBoundary() |
| 117 |
noslip_boundary = NoslipBoundary() |
| 118 |
|
| 119 |
# Create subspaces for boundary conditions |
| 120 |
V = MultiMeshSubSpace(W, 0) |
| 121 |
Q = MultiMeshSubSpace(W, 1) |
| 122 |
|
| 123 |
# Create boundary conditions |
| 124 |
bc0 = MultiMeshDirichletBC(V, noslip_value, noslip_boundary) |
| 125 |
bc1 = MultiMeshDirichletBC(V, inflow_value, inflow_boundary) |
| 126 |
bc2 = MultiMeshDirichletBC(Q, outflow_value, outflow_boundary) |
| 127 |
|
| 128 |
# Apply boundary conditions |
| 129 |
bc0.apply(A, b) |
| 130 |
bc1.apply(A, b) |
| 131 |
bc2.apply(A, b) |
| 132 |
W.lock_inactive_dofs(A, b) |
| 133 |
|
| 134 |
# Compute solution |
| 135 |
w = MultiMeshFunction(W) |
| 136 |
solve(A, w.vector(), b) |
| 137 |
|
| 138 |
# Save solution parts and components to file |
| 139 |
for part in range(3): |
| 140 |
ufile = XDMFFile("output/u%d.xdmf" % part) |
| 141 |
pfile = XDMFFile("output/p%d.xdmf" % part) |
| 142 |
ufile.write(w.part(part).sub(0)) |
| 143 |
pfile.write(w.part(part).sub(1)) |
| 144 |
ufile.close() |
| 145 |
pfile.close() |