Debian Codegraph @

python/demo/undocumented/point-integral/demo_point-integral.py

raw
1 """This demo program solves Poisson's equation with a delta dirac point source
2
3 - div grad u(x, y) = f(x, y)*dirac
4
5 on the unit square with source f given by
6
7 f(x, y) = 0.4
8
9 and boundary conditions given by
10
11 u(x, y) = 0 for x = 0 or x = 1
12 du/dn(x, y) = A*sin(5*x) for y = 0 or y = 1
13 """
14
15 # Copyright (C) 2014 Johan Hake
16 #
17 # This file is part of DOLFIN.
18 #
19 # DOLFIN is free software: you can redistribute it and/or modify
20 # it under the terms of the GNU Lesser General Public License as published by
21 # the Free Software Foundation, either version 3 of the License, or
22 # (at your option) any later version.
23 #
24 # DOLFIN is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU Lesser General Public License for more details.
28 #
29 # You should have received a copy of the GNU Lesser General Public License
30 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
31 #
32 # First added: 2014-10-20
33 # Last changed: 2014-10-20
34
35 # Begin demo
36
37 from dolfin import *
38 import matplotlib.pyplot as plt
39
40 # Create mesh and define function space
41 mesh = UnitSquareMesh(128, 128)
42 V = FunctionSpace(mesh, "Lagrange", 1)
43
44 # Define Dirichlet boundary (x = 0 or x = 1)
45 def boundary(x):
46 return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS
47
48 def center_func(x):
49 return (0.45 <= x[0] and x[0] <= 0.55 and near(x[1], 0.5)) or \
50 0.45 <= x[1] and x[1] <= 0.55 and near(x[0], 0.5)
51
52 # Define domain for point integral
53 center_domain = MeshFunction("size_t", mesh, 0, 0)
54 center = AutoSubDomain(center_func)
55 center.mark(center_domain, 1)
56 dPP = dP(subdomain_data=center_domain)
57
58 # Define boundary condition
59 u0 = Constant(0.0)
60 bc = DirichletBC(V, u0, boundary)
61
62 # Define variational problem
63 u = TrialFunction(V)
64 v = TestFunction(V)
65 f = Constant(0.4)
66 g = Expression("A*sin(5*x[0])", A=10.0, degree=2)
67 a = inner(grad(u), grad(v))*dx
68 L = f*v*dPP(1) + g*v*ds
69
70 # Compute solution
71 u = Function(V)
72 solve(a == L, u, bc)
73
74 # Save solution in VTK format
75 file = File("poisson.pvd")
76 file << u
77
78 # Plot solution
79 plot(u)
80 plt.show()