Debian Codegraph @

python/demo/undocumented/poisson1D/demo_poisson1D.py

raw
1 """This demo program solves Poisson's equation
2
3 - div grad u(x) = f(x)
4
5 on the unit interval with source f given by
6
7 f(x) = 9*pi^2*sin(3*pi*x[0])
8
9 and boundary conditions given by
10
11 u(x) = 0 for x = 0
12 du/dx = 0 for x = 1
13 """
14
15 # Copyright (C) 2007 Kristian B. Oelgaard
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 # Modified by Anders Logg 2011
33 #
34 # First added: 2007-11-28
35 # Last changed: 2012-11-12
36
37 from dolfin import *
38 import matplotlib.pyplot as plt
39
40
41 # Create mesh and function space
42 mesh = UnitIntervalMesh(50)
43 V = FunctionSpace(mesh, "CG", 1)
44
45 # Sub domain for Dirichlet boundary condition
46 class DirichletBoundary(SubDomain):
47 def inside(self, x, on_boundary):
48 return on_boundary and x[0] < DOLFIN_EPS
49
50 # Define variational problem
51 u = TrialFunction(V)
52 v = TestFunction(V)
53 f = Expression("9.0*pi*pi*sin(3.0*pi*x[0])", degree=2)
54 g = Expression("3.0*pi*cos(3.0*pi*x[0])", degree=2)
55
56 a = dot(grad(u), grad(v))*dx
57 L = f*v*dx + g*v*ds
58
59 # Define boundary condition
60 u0 = Constant(0.0)
61 bc = DirichletBC(V, u0, DirichletBoundary())
62
63 # Compute solution
64 u = Function(V)
65 solve(a == L, u, bc)
66
67 # Save solution to file
68 file = File("poisson.pvd")
69 file << u
70
71 # Plot solution
72 plot(u)
73 plt.show()