|
1 | 1 | from mpi4py import MPI |
| 2 | +from packaging.version import Version |
2 | 3 | import dolfinx |
3 | 4 | import scifem.interpolation |
4 | 5 | import pytest |
@@ -203,3 +204,114 @@ def test_discrete_curl(degree, use_petsc, cell_type): |
203 | 204 | w_ref.x.scatter_forward() |
204 | 205 |
|
205 | 206 | np.testing.assert_allclose(w.x.array, w_ref.x.array, rtol=1e-10, atol=1e-11) |
| 207 | + |
| 208 | + |
| 209 | +@pytest.mark.parametrize("degree", [1, 2, 3]) |
| 210 | +@pytest.mark.parametrize("family", ["Lagrange"]) |
| 211 | +@pytest.mark.skipif( |
| 212 | + Version(dolfinx.__version__) < Version("0.10.0"), reason="Requires DOLFINx version >0.10.0" |
| 213 | +) |
| 214 | +def test_interpolate_to_interface_submesh(family, degree): |
| 215 | + # Create a unit square |
| 216 | + comm = MPI.COMM_WORLD |
| 217 | + domain = dolfinx.mesh.create_unit_square( |
| 218 | + comm, 48, 48, ghost_mode=dolfinx.mesh.GhostMode.shared_facet |
| 219 | + ) |
| 220 | + |
| 221 | + # Split unit square in two subdomains |
| 222 | + cell_map = domain.topology.index_map(domain.topology.dim) |
| 223 | + num_cells_local = cell_map.size_local + cell_map.num_ghosts |
| 224 | + markers = np.full(num_cells_local, 1, dtype=np.int32) |
| 225 | + markers[ |
| 226 | + dolfinx.mesh.locate_entities(domain, domain.topology.dim, lambda x: x[0] <= 0.5 + 1e-14) |
| 227 | + ] = 2 |
| 228 | + ct = dolfinx.mesh.meshtags( |
| 229 | + domain, domain.topology.dim, np.arange(num_cells_local, dtype=np.int32), markers |
| 230 | + ) |
| 231 | + |
| 232 | + # Create submesh for each subdomain |
| 233 | + omega_e, e_to_parent, _, _, _ = scifem.mesh.extract_submesh(domain, ct, (1,)) |
| 234 | + omega_i, i_to_parent, _, _, _ = scifem.mesh.extract_submesh(domain, ct, (2,)) |
| 235 | + |
| 236 | + # Compute submesh for the interface between omega_e and omega_i |
| 237 | + interface_facets = scifem.mesh.find_interface(ct, (1,), (2,)) |
| 238 | + ft = dolfinx.mesh.meshtags( |
| 239 | + domain, |
| 240 | + domain.topology.dim - 1, |
| 241 | + interface_facets, |
| 242 | + np.full(interface_facets.shape, 1, dtype=np.int32), |
| 243 | + ) |
| 244 | + |
| 245 | + gamma, gamma_to_parent, _, _, _ = scifem.mesh.extract_submesh(domain, ft, 1) |
| 246 | + |
| 247 | + num_facets_local = ( |
| 248 | + gamma.topology.index_map(gamma.topology.dim).size_local |
| 249 | + + gamma.topology.index_map(gamma.topology.dim).num_ghosts |
| 250 | + ) |
| 251 | + gamma_to_parent_map = gamma_to_parent.sub_topology_to_topology( |
| 252 | + np.arange(num_facets_local, dtype=np.int32), inverse=False |
| 253 | + ) |
| 254 | + |
| 255 | + # Create functions on each subdomain |
| 256 | + def fe(x): |
| 257 | + return x[0] + x[1] ** degree |
| 258 | + |
| 259 | + def fi(x): |
| 260 | + return np.sin(x[0]) + np.cos(x[1]) |
| 261 | + |
| 262 | + Ve = dolfinx.fem.functionspace(omega_e, (family, degree)) |
| 263 | + ue = dolfinx.fem.Function(Ve) |
| 264 | + ue.interpolate(fe) |
| 265 | + ue.x.scatter_forward() |
| 266 | + Vi = dolfinx.fem.functionspace(omega_i, (family, degree)) |
| 267 | + ui = dolfinx.fem.Function(Vi) |
| 268 | + ui.interpolate(fi) |
| 269 | + ui.x.scatter_forward() |
| 270 | + |
| 271 | + # Compute ordered integration entities on the interface |
| 272 | + interface_integration_entities = scifem.compute_interface_data( |
| 273 | + ct, facet_indices=gamma_to_parent_map, include_ghosts=True |
| 274 | + ) |
| 275 | + mapped_entities = interface_integration_entities.copy() |
| 276 | + |
| 277 | + # For each submesh, get the relevant integration entities |
| 278 | + parent_to_e = e_to_parent.sub_topology_to_topology( |
| 279 | + np.arange(num_cells_local, dtype=np.int32), inverse=True |
| 280 | + ) |
| 281 | + parent_to_i = i_to_parent.sub_topology_to_topology( |
| 282 | + np.arange(num_cells_local, dtype=np.int32), inverse=True |
| 283 | + ) |
| 284 | + mapped_entities[:, 0] = parent_to_e[interface_integration_entities[:, 0]] |
| 285 | + mapped_entities[:, 2] = parent_to_i[interface_integration_entities[:, 2]] |
| 286 | + assert np.all(mapped_entities[:, 0] >= 0) |
| 287 | + assert np.all(mapped_entities[:, 2] >= 0) |
| 288 | + |
| 289 | + # Create two functions on the interface submesh |
| 290 | + Q = dolfinx.fem.functionspace(gamma, (family, degree)) |
| 291 | + qe = dolfinx.fem.Function(Q, name="qe") |
| 292 | + qi = dolfinx.fem.Function(Q, name="qi") |
| 293 | + |
| 294 | + # Interpolate volume functions (on submesh) onto all cells of the interface submesh |
| 295 | + scifem.interpolation.interpolate_to_surface_submesh( |
| 296 | + ue, qe, np.arange(len(gamma_to_parent_map), dtype=np.int32), mapped_entities[:, :2] |
| 297 | + ) |
| 298 | + qe.x.scatter_forward() |
| 299 | + scifem.interpolation.interpolate_to_surface_submesh( |
| 300 | + ui, qi, np.arange(len(gamma_to_parent_map), dtype=np.int32), mapped_entities[:, 2:] |
| 301 | + ) |
| 302 | + qi.x.scatter_forward() |
| 303 | + |
| 304 | + # Compute the difference between the two interpolated functions |
| 305 | + I = dolfinx.fem.Function(Q, name="i") |
| 306 | + I.x.array[:] = qe.x.array - qi.x.array |
| 307 | + |
| 308 | + reference = dolfinx.fem.Function(Q) |
| 309 | + reference.interpolate(lambda x: fe(x) - fi(x)) |
| 310 | + |
| 311 | + qe_ref = dolfinx.fem.Function(Q) |
| 312 | + qe_ref.interpolate(fe) |
| 313 | + qi_ref = dolfinx.fem.Function(Q) |
| 314 | + qi_ref.interpolate(fi) |
| 315 | + np.testing.assert_allclose(qe.x.array, qe_ref.x.array) |
| 316 | + np.testing.assert_allclose(qi.x.array, qi_ref.x.array) |
| 317 | + np.testing.assert_allclose(I.x.array, reference.x.array, rtol=1e-14, atol=1e-14) |
0 commit comments