Debian Codegraph @

python/demo/documented/built-in-meshes/demo_built-in-meshes.py.rst

raw
1 Built-in meshes
2 ===============
3
4 This demo is implemented in a single Python file,
5 :download:`demo_built-in-meshes.py`, and demonstrates use of the
6 built-in meshes in DOLFIN.
7
8 This demo illustrates:
9
10 * How to define some of the different built-in meshes in DOLFIN
11
12
13 Problem definition
14 ------------------
15
16 The demo focuses on the built-in meshes. We will look at the following
17 meshes:
18
19 * :py:class:`UnitIntervalMesh <dolfin.cpp.mesh.UnitIntervalMesh>`
20 * :py:class:`UnitSquareMesh <dolfin.cpp.mesh.UnitSquareMesh>`
21 * :py:class:`RectangleMesh <dolfin.cpp.mesh.RectangleMesh>`
22 * :py:class:`UnitCubeMesh <dolfin.cpp.mesh.UnitCubeMesh>`
23 * :py:class:`BoxMesh <dolfin.cpp.mesh.BoxMesh>`
24
25
26 Implementation
27 --------------
28
29 First, the :py:mod:`dolfin` module is imported::
30
31 from dolfin import *
32 import matplotlib.pyplot as plt
33
34 The first mesh we make is a mesh over the unit interval
35 :math:`(0,1)`. :py:class:`UnitIntervalMesh
36 <dolfin.cpp.mesh.UnitIntervalMesh>` takes the number of intervals
37 :math:`(n_x)` as input argument, and the total number of vertices is
38 therefore :math:`(n_x+1)`. ::
39
40 mesh = UnitIntervalMesh(10)
41 print("Plotting a UnitIntervalMesh")
42 plt.figure()
43 plot(mesh, title="Unit interval")
44
45 This produces a mesh looking as follows:
46
47 .. image:: unitintervalmesh.png
48 :scale: 75 %
49
50 We then make our first version of a mesh on the unit square
51 :math:`[0,1] \times [0,1]`. We must give the number of cells in the
52 horizontal and vertical directions as the first two arguments to
53 :py:class:`UnitSquareMesh
54 <dolfin.cpp.generation.UnitSquareMesh>`. There is a third optional
55 argument that indicates the direction of the diagonals. This can be
56 set to "left", "right", "right/left", "left/right", or "crossed". We
57 can also omit this argument and thereby use the default direction
58 "right". ::
59
60 mesh = UnitSquareMesh(10, 10)
61 print("Plotting a UnitSquareMesh")
62 plt.figure()
63 plot(mesh, title="Unit square")
64
65 .. image:: unitsquaremesh.png
66 :scale: 75 %
67
68 Our second version of a mesh on the unit square has diagonals to the
69 left, the third version has crossed diagonals and our final version
70 has diagonals to both left and right::
71
72 mesh = UnitSquareMesh(10, 10, "left")
73 print("Plotting a UnitSquareMesh")
74 plot(mesh, title="Unit square (left)")
75
76 mesh = UnitSquareMesh(10, 10, "crossed")
77 print("Plotting a UnitSquareMesh")
78 plot(mesh, title="Unit square (crossed)")
79
80 mesh = UnitSquareMesh(10, 10, "right/left")
81 print("Plotting a UnitSquareMesh")
82 plt.figure()
83 plot(mesh, title="Unit square (right/left)")
84
85 .. image:: unitsquaremesh_left.png
86 :scale: 65 %
87
88 .. image:: unitsquaremesh_crossed.png
89 :scale: 65 %
90
91 .. image:: unitsquaremesh_left_right.png
92 :scale: 65 %
93
94 The class :py:class:`RectangleMesh
95 <dolfin.cpp.generation.RectangleMesh>` creates a mesh of a 2D
96 rectangle spanned by two points (opposing corners) of the rectangle.
97 Three additional arguments specify the number of divisions in the
98 :math:`x`- and :math:`y`-directions, and as above the direction of the
99 diagonals is given as a final optional argument ("left", "right",
100 "left/right", or "crossed"). In the first mesh we use the default
101 direction ("right") of the diagonal, and in the second mesh we use
102 diagonals to both left and right. ::
103
104 mesh = RectangleMesh(Point(0.0, 0.0), Point(10.0, 4.0), 10, 10)
105 print("Plotting a RectangleMesh")
106 plt.figure()
107 plot(mesh, title="Rectangle")
108
109 mesh = RectangleMesh(Point(-3.0, 2.0), Point(7.0, 6.0), 10, 10, "right/left")
110 print("Plotting a RectangleMesh")
111 plt.figure()
112 plot(mesh, title="Rectangle (right/left)")
113
114 .. image:: rectanglemesh.png
115 :scale: 75 %
116
117 .. image:: rectanglemesh_left_right.png
118 :scale: 75 %
119
120 To make a mesh of the 3D unit cube :math:`[0,1] \times [0,1] \times
121 [0,1]`, we use :py:class:`UnitCubeMesh
122 <dolfin.cpp.generation.UnitCubeMesh>`. :py:class:`UnitCubeMesh
123 <dolfin.cpp.generation.UnitCubeMesh>` takes the number of cells in the
124 :math:`x`-, :math:`y`- and :math:`z`-direction as the only three
125 arguments. ::
126
127 mesh = UnitCubeMesh(10, 10, 10)
128 print("Plotting a UnitCubeMesh")
129 plt.figure()
130 plot(mesh, title="Unit cube")
131
132 .. image:: unitcubemesh.png
133 :scale: 75 %
134
135 Finally we will demonstrate a mesh on a rectangular prism in 3D. The
136 prism is specified by two points (opposing corners) of the
137 prism. Three additional arguments specify the number of divisions in
138 the :math:`x`-, :math:`y`- and :math:`z`-directions.
139
140 Meshes for more complex geometries may be created using the mshr
141 library, which functions as a plugin to DOLFIN, providing support for
142 Constructive Solid Geometry (CSG) and mesh generation. For more
143 details, refer to the mshr documentation. ::
144
145 mesh = BoxMesh(Point(0.0, 0.0, 0.0), Point(10.0, 4.0, 2.0), 10, 10, 10)
146 print("Plotting a BoxMesh")
147 plt.figure()
148 plot(mesh, title="Box")
149 plt.show()
150
151 .. image:: boxmesh.png
152 :scale: 75 %