/
test_42.py
109 lines (85 loc) · 3.12 KB
/
test_42.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from dolfin import *
# ------------------------
# BEGIN: class definitions
class LowerBoundaryOfUnitSquare(SubDomain):
""" This class represents and manipulates
the lower boundary of the unit square
[0,1]x[0,1]. """
def inside(self, x, on_boundary):
tol = 1E-14 # tolerance for coordinate comparisons
return on_boundary and abs(x[1]) < tol
class UpperBoundaryOfUnitSquare(SubDomain):
""" This class represents and manipulates
the upper boundary of the unit square
[0,1]x[0,1]. """
def inside(self, x, on_boundary):
tol = 1E-14 # tolerance for coordinate comparisons
return on_boundary and abs(x[1] - 1) < tol
class LeftBoundaryOfUnitSquare(SubDomain):
""" This class represents and manipulates
the left boundary of the unit square
[0,1]x[0,1]. """
def inside(self, x, on_boundary):
tol = 1E-14 # tolerance for coordinate comparisons
return on_boundary and abs(x[0]) < tol
class RightBoundaryOfUnitSquare(SubDomain):
""" This class represents and manipulates
the right boundary of the unit square
[0,1]x[0,1]. """
def inside(self, x, on_boundary):
tol = 1E-14 # tolerance for coordinate comparisons
return on_boundary and abs(x[0] - 1) < tol
# END: class definitions
# ----------------------
# --------------------
# BEGIN: Main function
nx = 64
ny = 64
mesh = UnitSquareMesh(nx, ny)
# create a mesh function over cell facets
mesh_fn_marking_bdry_parts = MeshFunction("size_t", mesh, mesh.topology().dim()-1)
Gamma_0 = LowerBoundaryOfUnitSquare()
Gamma_0.mark(mesh_fn_marking_bdry_parts, 0)
Gamma_1 = RightBoundaryOfUnitSquare()
Gamma_1.mark(mesh_fn_marking_bdry_parts, 1)
Gamma_2 = UpperBoundaryOfUnitSquare()
Gamma_2.mark(mesh_fn_marking_bdry_parts, 2)
Gamma_3 = LeftBoundaryOfUnitSquare()
Gamma_3.mark(mesh_fn_marking_bdry_parts, 3)
# set up and solve the weak formulation of the Neumann
# boundary-value problem for the Laplacian
V = FunctionSpace(mesh, "CG", 1)
R = FunctionSpace(mesh, "R", 0)
W = V * R
(u, c) = TrialFunction(W)
(v, d) = TestFunction(W)
f = Constant("0")
g_0 = Constant("0")
g_1 = Constant("1")
g_2 = Constant("0")
g_3 = Constant("-1")
(u, c) = TrialFunction(W)
(v, d) = TestFunction(W)
a = ( inner(grad(u),grad(v)) + c*v + d*u )*dx
L = f*v*dx + g_0*v*ds(0, subdomain_data=mesh_fn_marking_bdry_parts) \
+ g_1*v*ds(1, subdomain_data=mesh_fn_marking_bdry_parts) \
+ g_2*v*ds(2, subdomain_data=mesh_fn_marking_bdry_parts) \
+ g_3*v*ds(3, subdomain_data=mesh_fn_marking_bdry_parts)
A = assemble(a)
b = assemble(L)
w = Function(W)
solve(A, w.vector(), b, 'lu')
(u, c) = w.split()
# plot the numerically obtained solution
plot(u, title = 'The solution u')
plot(Expression('x[0]-0.5'), mesh=mesh)
plot(grad(u))
interactive()
# compute and print the flux through the boundary
unitNormal = FacetNormal(mesh)
for i in range(0,4):
flux = dot(grad(u), unitNormal)*ds(i,subdomain_data=mesh_fn_marking_bdry_parts)
flux = assemble(flux)
print "outward flux of u through Gamma_{0}: ".format(i), flux
# END: Main function
# --------------------------