Debian Codegraph @

python/test/unit/multimesh/test_volume.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 """Unit tests for multimesh volume computation"""
2
3 # Copyright (C) 2016 Anders Logg
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Modified by August Johansson 2016
21 # Modified by Simon Funke 2017
22 #
23 # First added: 2016-05-03
24 # Last changed: 2017-05-25
25
26 import pytest
27
28 from dolfin import *
29 from dolfin_utils.test import skip_in_parallel
30
31 def compute_volume(multimesh):
32 # Reference volume computation
33 v0 = multimesh.compute_volume()
34
35 # Create function space for DG volume computation
36 V = MultiMeshFunctionSpace(multimesh, "DG", 0)
37
38 # Create and evaluate volume functional
39 v = TestFunction(V)
40 M = v*dX
41 v1 = sum(assemble(M).get_local())
42
43 # Alternative volume computation
44 dXmm = dx(domain=multimesh) + dC(domain=multimesh)
45 M = Constant(1.0)*dXmm
46 v2 = assemble_multimesh(M)
47
48 # FIXME: We could be able to tighten the tolerance here
49 assert abs(v0 - v1) / v0 < DOLFIN_EPS_LARGE
50 assert abs(v0 - v2) / v0 < DOLFIN_EPS_LARGE
51
52 return v0
53
54 @skip_in_parallel
55 def test_volume_2d():
56 "Integrate volume of union of 2D meshes"
57
58 # Number of meshes on top of background mesh
59 num_meshes = 8
60
61 # Create background mesh so we can easily compute volume
62 mesh_0 = UnitSquareMesh(1, 1)
63 mesh_0.scale(10.0)
64 mesh_0.translate(Point(-5, -5))
65 exact_volume = 100.0
66
67 # Create meshes with centres distributed around the unit circle.
68 # Meshes are scaled by a factor 2 so that they overlap.
69 meshes = []
70 for i in range(num_meshes):
71 mesh = UnitSquareMesh(1, 1)
72 angle = 2.*pi*float(i) / float(num_meshes)
73 print("i, angle", i, angle)
74 mesh.translate(Point(-0.5, -0.5))
75 mesh.scale(2.0)
76 mesh.rotate(180.0*angle / pi)
77 mesh.translate(Point(cos(angle), sin(angle)))
78 meshes.append(mesh)
79
80 # Save meshes to file so we can examine them
81 #File('output/background_mesh.pvd') << mesh_0
82 #vtkfile = File('output/meshes.pvd')
83 #for mesh in meshes:
84 # vtkfile << mesh
85
86 # Create multimesh
87 multimesh = MultiMesh()
88 for mesh in [mesh_0] + meshes:
89 multimesh.add(mesh)
90 multimesh.build()
91
92 # Compute approximate volume
93 approximative_volume = compute_volume(multimesh)
94
95 print("exact volume ", exact_volume)
96 print("approximative volume ", approximative_volume)
97 print("relative approximate volume error %1.16e" % ((exact_volume - approximative_volume) / exact_volume))
98 assert abs(exact_volume - approximative_volume) / exact_volume < DOLFIN_EPS_LARGE
99
100 @skip_in_parallel
101 def test_volume_2d_4_meshes():
102 "Test with four meshes that previously failed"
103
104 # Create multimesh
105 multimesh = MultiMesh()
106
107 # Mesh size
108 h = 0.25
109 Nx = int(round(1 / h))
110
111 # Background mesh
112 mesh_0 = UnitSquareMesh(Nx, Nx)
113 multimesh.add(mesh_0)
114
115 # Mesh 1
116 x0 = 0.35404867974764142602
117 y0 = 0.16597416632155614913
118 x1 = 0.63997881656511634851
119 y1 = 0.68786139026650294781
120 mesh_1 = RectangleMesh(Point(x0, y0), Point(x1, y1),
121 max(int(round((x1-x0)/h)), 1), max(int(round((y1-y0)/h)), 1))
122 mesh_1.rotate(39.609407484349517858)
123 multimesh.add(mesh_1)
124
125 # Mesh 2
126 x0 = 0.33033712968711609337
127 y0 = 0.22896817104377231722
128 x1 = 0.82920109332967595339
129 y1 = 0.89337241458397931293
130 mesh_2 = RectangleMesh(Point(x0, y0), Point(x1, y1),
131 max(int(round((x1-x0)/h)), 1), max(int(round((y1-y0)/h)), 1))
132 mesh_2.rotate(31.532416069662392744)
133 multimesh.add(mesh_2)
134
135 # Mesh 3
136 x0 = 0.28105941241656401397
137 y0 = 0.30745787374091237965
138 x1 = 0.61959648394007071914
139 y1 = 0.78600209801737319637
140 mesh_3 = RectangleMesh(Point(x0, y0), Point(x1, y1),
141 max(int(round((x1-x0)/h)), 1), max(int(round((y1-y0)/h)), 1))
142 mesh_3.rotate(40.233022128340330426)
143 multimesh.add(mesh_3)
144
145 multimesh.build()
146
147 exact_volume = 1
148 approximate_volume = compute_volume(multimesh)
149
150 print("exact volume ", exact_volume)
151 print("approximative volume ", approximate_volume)
152 print("approximate volume error %1.16e" % (exact_volume - approximate_volume))
153 assert abs(exact_volume - approximate_volume) < DOLFIN_EPS_LARGE
154
155 @skip_in_parallel
156 def test_volume_2d_six_meshes():
157 "Integrate volume of six 2D meshes"
158
159 # Number of elements
160 Nx = 8
161 h = 1. / Nx
162
163 # Background mesh
164 mesh_0 = UnitSquareMesh(Nx, Nx)
165
166 # 5 meshes plus background mesh
167 num_meshes = 5
168
169 # List of points for generating the meshes on top
170 points = [[ Point(0.747427, 0.186781), Point(0.849659, 0.417130) ],
171 [ Point(0.152716, 0.471681), Point(0.455943, 0.741585) ],
172 [ Point(0.464473, 0.251876), Point(0.585051, 0.533569) ],
173 [ Point(0.230112, 0.511897), Point(0.646974, 0.892193) ],
174 [ Point(0.080362, 0.422675), Point(0.580151, 0.454286) ]]
175
176 angles = [ 88.339755, 94.547259, 144.366564, 172.579922, 95.439692 ]
177
178 # Create multimesh
179 multimesh = MultiMesh()
180 multimesh.add(mesh_0)
181
182 # Add the 5 background meshes
183 for i in range(num_meshes):
184 nx = max(int(round(abs(points[i][0].x()-points[i][1].x()) / h)), 1)
185 ny = max(int(round(abs(points[i][0].y()-points[i][1].y()) / h)), 1)
186 mesh = RectangleMesh(points[i][0], points[i][1], nx, ny)
187 mesh.rotate(angles[i])
188 multimesh.add(mesh)
189 multimesh.build()
190
191 # Save meshes to file
192 #vtkfile = File('output/test_six_meshes.pvd')
193 #for i in range(multimesh.num_parts()):
194 # vtkfile << multimesh.part(i)
195
196 exact_volume = 1.0
197 approximate_volume = compute_volume(multimesh)
198
199 print("exact volume ", exact_volume)
200 print("approximative volume ", approximate_volume)
201 print("approximate volume error %1.16e" % (exact_volume - approximate_volume))
202 assert abs(exact_volume - approximate_volume) < DOLFIN_EPS_LARGE