Debian Codegraph @

tests/test_meshtags.py

raw
1 from __future__ import annotations
2
3 import itertools
4
5 from mpi4py import MPI
6
7 import dolfinx
8 import numpy as np
9 import pytest
10
11 import io4dolfinx
12
13 root = 0
14 dtypes: list["str"] = ["float64", "float32"] # Mesh geometry dtypes
15 write_comm: list[MPI.Intracomm] = [
16 MPI.COMM_SELF,
17 MPI.COMM_WORLD,
18 ] # Communicators for creating mesh
19 read_modes: list[dolfinx.mesh.GhostMode] = [
20 dolfinx.mesh.GhostMode.none,
21 dolfinx.mesh.GhostMode.shared_facet,
22 ]
23 # Cell types of different dimensions
24 two_dimensional_cell_types: list[dolfinx.mesh.CellType] = [
25 dolfinx.mesh.CellType.triangle,
26 dolfinx.mesh.CellType.quadrilateral,
27 ]
28 three_dimensional_cell_types: list[dolfinx.mesh.CellType] = [
29 dolfinx.mesh.CellType.tetrahedron,
30 dolfinx.mesh.CellType.hexahedron,
31 ]
32
33 one_dim_combinations = itertools.product(dtypes, write_comm)
34 two_dim_combinations = itertools.product(dtypes, two_dimensional_cell_types, write_comm)
35 three_dim_combinations = itertools.product(dtypes, three_dimensional_cell_types, write_comm)
36
37
38 @pytest.fixture(params=one_dim_combinations, scope="module")
39 def mesh_1D(request):
40 dtype, write_comm = request.param
41 mesh = dolfinx.mesh.create_unit_interval(write_comm, 8, dtype=np.dtype(dtype))
42 return mesh
43
44
45 @pytest.fixture(params=two_dim_combinations, scope="module")
46 def mesh_2D(request):
47 dtype, cell_type, write_comm = request.param
48 mesh = dolfinx.mesh.create_unit_square(
49 write_comm, 10, 7, cell_type=cell_type, dtype=np.dtype(dtype)
50 )
51 return mesh
52
53
54 @pytest.fixture(params=three_dim_combinations, scope="module")
55 def mesh_3D(request):
56 dtype, cell_type, write_comm = request.param
57 mesh = dolfinx.mesh.create_unit_cube(
58 write_comm, 5, 7, 3, cell_type=cell_type, dtype=np.dtype(dtype)
59 )
60 return mesh
61
62
63 @pytest.mark.parametrize("read_mode", read_modes)
64 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
65 def test_checkpointing_meshtags_1D(
66 mesh_1D, read_comm, read_mode, tmp_path, backend, generate_reference_map
67 ):
68 mesh = mesh_1D
69
70 # Write unique mesh file for each combination of MPI communicator and dtype
71 hash = f"{mesh.comm.size}_{mesh.geometry.x.dtype}"
72 fname = MPI.COMM_WORLD.bcast(tmp_path, root=0)
73 suffix = ".bp" if backend == "adios" else ".h5"
74 filename = (fname / f"{backend}_meshtags_1D_{hash}").with_suffix(suffix)
75
76 # If mesh communicator is more than a self communicator or serial write on all processes.
77 # If serial or self communicator, only write on root rank
78 if mesh.comm.size != 1:
79 io4dolfinx.write_mesh(filename, mesh, backend=backend)
80 else:
81 if MPI.COMM_WORLD.rank == root:
82 io4dolfinx.write_mesh(filename, mesh, backend=backend)
83
84 # Create meshtags labeling each entity (of each co-dimension) with a
85 # unique number (their initial global index).
86 org_maps = []
87 for dim in range(mesh.topology.dim + 1):
88 mesh.topology.create_connectivity(dim, mesh.topology.dim)
89 e_map = mesh.topology.index_map(dim)
90 num_entities_local = e_map.size_local
91 entities = np.arange(num_entities_local, dtype=np.int32)
92 ft = dolfinx.mesh.meshtags(mesh, dim, entities, e_map.local_range[0] + entities)
93 ft.name = f"entity_{dim}"
94
95 # If parallel write on all processes, else write on root rank
96 if mesh.comm.size != 1:
97 io4dolfinx.write_meshtags(filename, mesh, ft, backend=backend)
98 # Create map from mesh tag value to its corresponding index and midpoint
99 org_map = generate_reference_map(mesh, ft, mesh.comm, root)
100 org_maps.append(org_map)
101 else:
102 if MPI.COMM_WORLD.rank == root:
103 io4dolfinx.write_meshtags(filename, mesh, ft, backend=backend)
104 # Create map from mesh tag value to its corresponding index and midpoint
105 org_map = generate_reference_map(mesh, ft, MPI.COMM_SELF, root)
106 org_maps.append(org_map)
107 del ft
108 del mesh
109 MPI.COMM_WORLD.Barrier()
110 # Read mesh on testing communicator
111 new_mesh = io4dolfinx.read_mesh(filename, read_comm, ghost_mode=read_mode, backend=backend)
112 for dim in range(new_mesh.topology.dim + 1):
113 # Read meshtags on all processes if testing communicator has multiple ranks
114 # else read on root 0
115 if read_comm.size != 1:
116 new_ft = io4dolfinx.read_meshtags(
117 filename,
118 new_mesh,
119 meshtag_name=f"entity_{dim}",
120 backend=backend,
121 )
122 # Generate meshtags map from mesh tag value to its corresponding index and midpoint
123 # and gather on root process
124 read_map = generate_reference_map(new_mesh, new_ft, new_mesh.comm, root)
125 else:
126 if MPI.COMM_WORLD.rank == root:
127 new_ft = io4dolfinx.read_meshtags(
128 filename,
129 new_mesh,
130 meshtag_name=f"entity_{dim}",
131 backend=backend,
132 )
133 read_map = generate_reference_map(new_mesh, new_ft, read_comm, root)
134
135 # On root process, check that midpoints are the same for each value in the meshtag
136 if MPI.COMM_WORLD.rank == root:
137 org_map = org_maps[dim]
138 assert len(org_map) == len(read_map)
139 for value, (_, midpoint) in org_map.items():
140 _, read_midpoint = read_map[value]
141 np.testing.assert_allclose(read_midpoint, midpoint)
142
143
144 @pytest.mark.parametrize("read_mode", read_modes)
145 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
146 def test_checkpointing_meshtags_2D(
147 mesh_2D, read_comm, read_mode, tmp_path, backend, generate_reference_map
148 ):
149 mesh = mesh_2D
150 hash = f"{mesh.comm.size}_{mesh.topology.cell_name()}_{mesh.geometry.x.dtype}"
151 fname = MPI.COMM_WORLD.bcast(tmp_path, root=0)
152 filename = fname / f"meshtags_1D_{hash}.bp"
153
154 if mesh.comm.size != 1:
155 io4dolfinx.write_mesh(filename, mesh, backend=backend)
156 else:
157 if MPI.COMM_WORLD.rank == root:
158 io4dolfinx.write_mesh(filename, mesh, backend=backend)
159
160 org_maps = []
161 for dim in range(mesh.topology.dim + 1):
162 mesh.topology.create_connectivity(dim, mesh.topology.dim)
163 e_map = mesh.topology.index_map(dim)
164 num_entities_local = e_map.size_local
165 entities = np.arange(num_entities_local, dtype=np.int32)
166 ft = dolfinx.mesh.meshtags(mesh, dim, entities, e_map.local_range[0] + entities)
167 ft.name = f"entity_{dim}"
168 if mesh.comm.size != 1:
169 io4dolfinx.write_meshtags(filename, mesh, ft, backend=backend)
170 org_map = generate_reference_map(mesh, ft, mesh.comm, root)
171 org_maps.append(org_map)
172 else:
173 if MPI.COMM_WORLD.rank == root:
174 io4dolfinx.write_meshtags(filename, mesh, ft, backend=backend)
175 org_map = generate_reference_map(mesh, ft, MPI.COMM_SELF, root)
176 org_maps.append(org_map)
177 del ft
178 del mesh
179 MPI.COMM_WORLD.Barrier()
180 new_mesh = io4dolfinx.read_mesh(filename, read_comm, ghost_mode=read_mode, backend=backend)
181 for dim in range(new_mesh.topology.dim + 1):
182 if read_comm.size != 1:
183 new_ft = io4dolfinx.read_meshtags(
184 filename,
185 new_mesh,
186 meshtag_name=f"entity_{dim}",
187 backend=backend,
188 )
189 read_map = generate_reference_map(new_mesh, new_ft, new_mesh.comm, root)
190 else:
191 if MPI.COMM_WORLD.rank == root:
192 new_ft = io4dolfinx.read_meshtags(
193 filename,
194 new_mesh,
195 meshtag_name=f"entity_{dim}",
196 backend=backend,
197 )
198 read_map = generate_reference_map(new_mesh, new_ft, read_comm, root)
199
200 if MPI.COMM_WORLD.rank == root:
201 org_map = org_maps[dim]
202 assert len(org_map) == len(read_map)
203 for value, (_, midpoint) in org_map.items():
204 _, read_midpoint = read_map[value]
205 np.testing.assert_allclose(read_midpoint, midpoint)
206
207
208 @pytest.mark.parametrize("read_mode", read_modes)
209 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
210 def test_checkpointing_meshtags_3D(
211 mesh_3D, read_comm, read_mode, tmp_path, backend, generate_reference_map
212 ):
213 mesh = mesh_3D
214 hash = f"{mesh.comm.size}_{mesh.topology.cell_name()}_{mesh.geometry.x.dtype}"
215 fname = MPI.COMM_WORLD.bcast(tmp_path, root=0)
216 filename = fname / f"meshtags_1D_{hash}.bp"
217 if mesh.comm.size != 1:
218 io4dolfinx.write_mesh(filename, mesh, backend=backend)
219 else:
220 if MPI.COMM_WORLD.rank == root:
221 io4dolfinx.write_mesh(filename, mesh, backend=backend)
222
223 org_maps = []
224 for dim in range(mesh.topology.dim + 1):
225 mesh.topology.create_connectivity(dim, mesh.topology.dim)
226 e_map = mesh.topology.index_map(dim)
227 num_entities_local = e_map.size_local
228 entities = np.arange(num_entities_local, dtype=np.int32)
229 ft = dolfinx.mesh.meshtags(mesh, dim, entities, e_map.local_range[0] + entities)
230 ft.name = f"entity_{dim}"
231
232 if mesh.comm.size != 1:
233 io4dolfinx.write_meshtags(filename, mesh, ft, backend=backend)
234 org_map = generate_reference_map(mesh, ft, mesh.comm, root)
235 org_maps.append(org_map)
236 else:
237 if MPI.COMM_WORLD.rank == root:
238 io4dolfinx.write_meshtags(filename, mesh, ft, backend=backend)
239 org_map = generate_reference_map(mesh, ft, MPI.COMM_SELF, root)
240 org_maps.append(org_map)
241 del ft
242 del mesh
243
244 MPI.COMM_WORLD.Barrier()
245 new_mesh = io4dolfinx.read_mesh(filename, read_comm, ghost_mode=read_mode, backend=backend)
246 for dim in range(new_mesh.topology.dim + 1):
247 if read_comm.size != 1:
248 new_ft = io4dolfinx.read_meshtags(
249 filename,
250 new_mesh,
251 meshtag_name=f"entity_{dim}",
252 backend=backend,
253 )
254 read_map = generate_reference_map(new_mesh, new_ft, new_mesh.comm, root)
255 else:
256 if MPI.COMM_WORLD.rank == root:
257 new_ft = io4dolfinx.read_meshtags(
258 filename,
259 new_mesh,
260 meshtag_name=f"entity_{dim}",
261 backend=backend,
262 )
263 read_map = generate_reference_map(new_mesh, new_ft, MPI.COMM_SELF, root)
264 if MPI.COMM_WORLD.rank == root:
265 org_map = org_maps[dim]
266 assert len(org_map) == len(read_map)
267 for value, (_, midpoint) in org_map.items():
268 _, read_midpoint = read_map[value]
269 np.testing.assert_allclose(read_midpoint, midpoint)