| 1 |
# Copyright (C) 2011 Marie E. Rognes |
| 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: 2011-11-09 |
| 19 |
# Last changed: 2012-08-06 |
| 20 |
|
| 21 |
# Begin demo |
| 22 |
|
| 23 |
|
| 24 |
from dolfin import * |
| 25 |
|
| 26 |
# Create classes for defining parts of the boundaries and the interior |
| 27 |
# of the domain |
| 28 |
class Left(SubDomain): |
| 29 |
def inside(self, x, on_boundary): |
| 30 |
return near(x[0], 0.0) |
| 31 |
|
| 32 |
class Right(SubDomain): |
| 33 |
def inside(self, x, on_boundary): |
| 34 |
return near(x[0], 1.0) |
| 35 |
|
| 36 |
class Bottom(SubDomain): |
| 37 |
def inside(self, x, on_boundary): |
| 38 |
return near(x[1], 0.0) |
| 39 |
|
| 40 |
class Top(SubDomain): |
| 41 |
def inside(self, x, on_boundary): |
| 42 |
return near(x[1], 1.0) |
| 43 |
|
| 44 |
class Obstacle(SubDomain): |
| 45 |
def inside(self, x, on_boundary): |
| 46 |
return (between(x[1], (0.5, 0.7)) and between(x[0], (0.2, 1.0))) |
| 47 |
|
| 48 |
# Initialize sub-domain instances |
| 49 |
left = Left() |
| 50 |
top = Top() |
| 51 |
right = Right() |
| 52 |
bottom = Bottom() |
| 53 |
obstacle = Obstacle() |
| 54 |
|
| 55 |
# Define mesh |
| 56 |
mesh = UnitSquareMesh(64, 64) |
| 57 |
|
| 58 |
# Initialize mesh function for interior domains |
| 59 |
domains = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 60 |
domains.set_all(0) |
| 61 |
obstacle.mark(domains, 1) |
| 62 |
|
| 63 |
# Initialize mesh function for boundary domains |
| 64 |
boundaries = MeshFunction("size_t", mesh, mesh.topology().dim()-1) |
| 65 |
boundaries.set_all(0) |
| 66 |
left.mark(boundaries, 1) |
| 67 |
top.mark(boundaries, 2) |
| 68 |
right.mark(boundaries, 3) |
| 69 |
bottom.mark(boundaries, 4) |
| 70 |
|
| 71 |
# Define input data |
| 72 |
a0 = Constant(1.0) |
| 73 |
a1 = Constant(0.01) |
| 74 |
g_L = Expression("- 10*exp(- pow(x[1] - 0.5, 2))", degree=2) |
| 75 |
g_R = Constant(1.0) |
| 76 |
f = Constant(1.0) |
| 77 |
|
| 78 |
# Define function space and basis functions |
| 79 |
V = FunctionSpace(mesh, "CG", 2) |
| 80 |
u = TrialFunction(V) |
| 81 |
v = TestFunction(V) |
| 82 |
|
| 83 |
# Define Dirichlet boundary conditions at top and bottom boundaries |
| 84 |
bcs = [DirichletBC(V, 5.0, boundaries, 2), |
| 85 |
DirichletBC(V, 0.0, boundaries, 4)] |
| 86 |
|
| 87 |
# Define new measures associated with the interior domains and |
| 88 |
# exterior boundaries |
| 89 |
dx = Measure('dx', domain=mesh, subdomain_data=domains) |
| 90 |
ds = Measure('ds', domain=mesh, subdomain_data=boundaries) |
| 91 |
|
| 92 |
# Define variational form |
| 93 |
F = (inner(a0*grad(u), grad(v))*dx(0) + inner(a1*grad(u), grad(v))*dx(1) |
| 94 |
- g_L*v*ds(1) - g_R*v*ds(3) |
| 95 |
- f*v*dx(0) - f*v*dx(1)) |
| 96 |
|
| 97 |
# Separate left and right hand sides of equation |
| 98 |
a, L = lhs(F), rhs(F) |
| 99 |
|
| 100 |
# Solve problem |
| 101 |
u = Function(V) |
| 102 |
solve(a == L, u, bcs) |
| 103 |
|
| 104 |
# Evaluate integral of normal gradient over top boundary |
| 105 |
n = FacetNormal(mesh) |
| 106 |
m1 = dot(grad(u), n)*ds(2) |
| 107 |
v1 = assemble(m1) |
| 108 |
print("\int grad(u) * n ds(2) = ", v1) |
| 109 |
|
| 110 |
# Evaluate integral of u over the obstacle |
| 111 |
m2 = u*dx(1) |
| 112 |
v2 = assemble(m2) |
| 113 |
print("\int u dx(1) = ", v2) |
| 114 |
|
| 115 |
# Plot solution |
| 116 |
import matplotlib.pyplot as plt |
| 117 |
plt.figure() |
| 118 |
plot(u, title="Solution u") |
| 119 |
|
| 120 |
# Plot solution and gradient |
| 121 |
plt.figure() |
| 122 |
plot(grad(u), title="Projected grad(u)") |
| 123 |
|
| 124 |
# Show plots |
| 125 |
plt.show() |