Debian Codegraph @

demo/documented/bcs/python/demo_bcs.py

raw
1 """This demo illustrates how to set boundary conditions for meshes
2 that include boundary indicators. The mesh used in this demo was
3 generated with VMTK (http://www.vmtk.org/)."""
4
5 # Copyright (C) 2008-2012 Anders Logg
6 #
7 # This file is part of DOLFIN.
8 #
9 # DOLFIN is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # DOLFIN is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
21 #
22 # Modified by Martin Alnaes 2012
23 #
24 # First added: 2008-05-23
25 # Last changed: 2012-10-16
26 # Begin demo
27
28 import matplotlib.pyplot as plt
29 from dolfin import *
30
31 # Create mesh and define function space
32 mesh = Mesh("../aneurysm.xml.gz")
33 V = FunctionSpace(mesh, "CG", 1)
34
35 # Define variational problem
36 u = TrialFunction(V)
37 v = TestFunction(V)
38 f = Constant(0.0)
39 a = dot(grad(u), grad(v))*dx
40 L = f*v*dx
41
42 # Define boundary condition values
43 u0 = Constant(0.0)
44 u1 = Constant(1.0)
45 u2 = Constant(2.0)
46 u3 = Constant(3.0)
47
48 if has_pybind11():
49 markers = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 9999)
50 for (f, v) in mesh.domains().markers(mesh.topology().dim()-1).items():
51 markers[f] = v
52
53 # Define boundary conditions
54 bc0 = DirichletBC(V, u0, markers, 0)
55 bc1 = DirichletBC(V, u1, markers, 1)
56 bc2 = DirichletBC(V, u2, markers, 2)
57 bc3 = DirichletBC(V, u3, markers, 3)
58 else:
59 # Define boundary conditions
60 bc0 = DirichletBC(V, u0, 0)
61 bc1 = DirichletBC(V, u1, 1)
62 bc2 = DirichletBC(V, u2, 2)
63 bc3 = DirichletBC(V, u3, 3)
64
65 # Set PETSc MUMPS paramter (this is required to prevent a memory error
66 # in some cases when using MUMPS LU solver).
67 if has_petsc():
68 PETScOptions.set("mat_mumps_icntl_14", 40.0)
69
70 # Compute solution
71 u = Function(V)
72 solve(a == L, u, [bc0, bc1, bc2, bc3])
73
74 # Write solution to file
75 File("u.pvd") << u
76
77 # Plot solution
78 plot(u)
79 plt.show()