Debian Codegraph @

python/demo/documented/subdomains/documentation.rst

raw
1 .. Documentation for subdomains demo from DOLFIN.
2
3 .. _demo_pde_subdomains_python_documentation:
4
5 Marking subdomains of a mesh
6 ============================
7
8 This demo is implemented in a single Python file,
9 :download:`demo_subdomains.py`.
10
11
12 .. include:: ../common.txt
13
14 Implementation
15 --------------
16
17 First, the :py:mod:`dolfin` module is imported:
18
19 .. code-block:: python
20
21 from dolfin import *
22
23 It may be useful to get information about what is going on behind the
24 scenes when the program is running. In DOLFIN, we can control which
25 messages routed through the logging system will be printed. By
26 calling the function :py:class:`set_log_level()
27 <dolfin.cpp.common.set_log_level>` we specify the log level. An
28 optional integer argument may be given to specify the level, the
29 default level is 20. Only messages on a level higher than or equal to
30 the current log level will be printed. To get as much information as
31 possible we will set the log level to 1:
32
33 .. code-block:: python
34
35 set_log_level(1)
36
37 Before we can mark the boundaries, we must specify the boundary
38 conditions. We do this by defining three classes, one for each
39 boundary condition. We start by defining the whole boundary as a
40 noslip boundary, and then overwrite the parts of the boundary that is
41 inflow and outflow.
42
43 .. code-block:: python
44
45 # Sub domain for no-slip (mark whole boundary, inflow and outflow will overwrite)
46 class Noslip(SubDomain):
47 def inside(self, x, on_boundary):
48 return on_boundary
49
50 # Sub domain for inflow (right)
51 class Inflow(SubDomain):
52 def inside(self, x, on_boundary):
53 return x[0] > 1.0 - DOLFIN_EPS and on_boundary
54
55 # Sub domain for outflow (left)
56 class Outflow(SubDomain):
57 def inside(self, x, on_boundary):
58 return x[0] < DOLFIN_EPS and on_boundary
59
60 Then, we import the mesh:
61
62 .. code-block:: python
63
64 # Read mesh
65 mesh = Mesh("../dolfin_fine.xml.gz")
66
67 We create a :py:class:`MeshFunction <dolfin.cpp.MeshFunction>` to
68 store the numbering of the subdomains. When creating a MeshFunction
69 an argument specifying the type of the MeshFunction must be given.
70 Allowed types are ‘int’, ‘size_t’, ‘double’ and ‘bool’. To illustrate
71 the difference of these we will use both ‘size_t’, ‘double’ and
72 ‘bool’:
73
74 .. code-block:: python
75
76 # Create mesh functions over the cell facets
77 sub_domains = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
78 sub_domains_bool = MeshFunction("bool", mesh, mesh.topology().dim() - 1)
79 sub_domains_double = MeshFunction("double", mesh, mesh.topology().dim() - 1)
80
81 The second and third arguments are optional. The second argument
82 specifies our mesh, while the third argument gives the topological
83 dimension of the :py:class:`MeshFunction <dolfin.cpp.MeshFunction>`,
84 which is the topological dimension of our mesh minus 1.
85
86 We are now ready to mark the mesh. We will create four subdomains of
87 the mesh; one subdomain for each boundary condition and a fourth
88 subdomain for the interior. First, we mark all facets in the mesh as
89 part of subdomain 3. Since we want to illustrate the possible types
90 of mesh functions, this is done in three different ways. The first
91 type, the :py:class:`MeshFunction <dolfin.cpp.MeshFunctionSizet>`,
92 takes an integer argument and all facets will be given this index.
93 The second type, the :py:class:`MeshFunction
94 <dolfin.cpp.MeshFunctionBool>`, takes a boolean as argument and all
95 facets will be marked by this boolean. The third type , the
96 :py:class:`MeshFunction <dolfin.cpp.MeshFunctionDouble>`, takes a
97 floating point number as argument and this number will be the index of
98 all the facets.
99
100 .. code-block:: python
101
102 # Mark all facets as sub domain 3
103 sub_domains.set_all(3)
104 sub_domains_bool.set_all(False)
105 sub_domains_double.set_all(0.3)
106
107 When all facets are marked, we mark only the boundary facets. We give
108 the noslip boundary the index 0. To mark the facets of the noslip
109 boundary, we first make an instance of the class ``Noslip``, and then
110 mark these facets with the correct value (either an integer, boolean
111 or double).
112
113 .. code-block:: python
114
115 # Mark no-slip facets as sub domain 0, 0.0
116 noslip = Noslip()
117 noslip.mark(sub_domains, 0)
118 noslip.mark(sub_domains_double, 0.0)
119
120 The same must be done for the inflow and outflow boundaries. The
121 inflow is marked as subdomain 1 and the outflow is marked as subdomain
122 2:
123
124 .. code-block:: python
125
126 # Mark inflow as sub domain 1, 01
127 inflow = Inflow()
128 inflow.mark(sub_domains, 1)
129 inflow.mark(sub_domains_double, 0.1)
130
131 # Mark outflow as sub domain 2, 0.2, True
132 outflow = Outflow()
133 outflow.mark(sub_domains, 2)
134 outflow.mark(sub_domains_double, 0.2)
135 outflow.mark(sub_domains_bool, True)
136
137 Finally, to be able to use these subdomains together with the mesh in
138 other programs, we save the subdomains to file, both as XML and VTK
139 files:
140
141 .. code-block:: python
142
143 # Save sub domains to file
144 file = File("subdomains.xml")
145 file << sub_domains
146
147 # FIXME: Not implemented
148 #file_bool = File("subdomains_bool.xml")
149 #file_bool << sub_domains_bool
150
151 file_double = File("subdomains_double.xml")
152 file_double << sub_domains_double
153
154 # Save sub domains to VTK files
155 file = File("subdomains.pvd")
156 file << sub_domains
157
158 file = File("subdomains_double.pvd")
159 file << sub_domains_double
160
161 Complete code
162 -------------
163
164 .. literalinclude:: demo_subdomains.py
165 :start-after: # Begin demo