-
Notifications
You must be signed in to change notification settings - Fork 0
/
harmonic_extension.py
54 lines (41 loc) · 1.27 KB
/
harmonic_extension.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
from dolfin import *
def extend(n_cells, k):
'''
Harmonic extension of f=sin(k*pi*x) by
------------------
| |
| |
| \Gamma |
|----------------|
| |
| | \partial\Omega
| |
|----------------|
-Delta u = u
u = 0 on \partial\Omega\setminus \Gamma
u = f on \Gamma
'''
assert n_cells % 2 == 0
f = Expression('sin(k*pi*x[0])', k=k)
mesh = UnitSquareMesh(n_cells, n_cells)
mesh = RectangleMesh(Point(0, 0), Point(1, 0.5), n_cells, n_cells/2)
V = FunctionSpace(mesh, 'CG', 1)
u = TrialFunction(V)
v = TestFunction(V)
gamma = FacetFunction('size_t', mesh, 0)
CompiledSubDomain('near(x[1], 0.5)').mark(gamma, 1)
bc_out = DirichletBC(V, Constant(0), 'on_boundary')
bc_in = DirichletBC(V, f, gamma, 1)
bcs = [bc_out, bc_in]
a = inner(grad(u), grad(v))*dx
L = inner(Constant(0), v)*dx
uh = Function(V)
solve(a == L, uh, bcs)
return uh
# ----------------------------------------------------------------------------
if __name__ == '__main__':
k = 2
plot(extend(n_cells=16, k=k))
plot(extend(n_cells=32, k=k))
plot(extend(n_cells=64, k=k))
interactive()