|
| 1 | +import pytest |
| 2 | +import numpy as np |
| 3 | +from firedrake import * |
| 4 | + |
| 5 | + |
| 6 | +def test_submesh_subdomain_id_tuple(): |
| 7 | + mesh = UnitSquareMesh(4, 4) |
| 8 | + x, y = SpatialCoordinate(mesh) |
| 9 | + M = FunctionSpace(mesh, "DG", 0) |
| 10 | + m1 = Function(M).interpolate(conditional(lt(x, 0.5), 1, 0)) |
| 11 | + m2 = Function(M).interpolate(conditional(lt(y, 0.5), 1, 0)) |
| 12 | + mesh.mark_entities(m1, 111) |
| 13 | + mesh.mark_entities(m2, 222) |
| 14 | + |
| 15 | + subdomain_id = [111, 222] |
| 16 | + submesh1 = Submesh(mesh, mesh.topological_dimension, subdomain_id=subdomain_id) |
| 17 | + |
| 18 | + m3 = Function(M).interpolate(m1 + m2 - m1 * m2) |
| 19 | + expected = assemble(m3*dx) |
| 20 | + assert abs(assemble(1*dx(domain=submesh1)) - expected) < 1E-12 |
| 21 | + |
| 22 | + mesh.mark_entities(m3, 333) |
| 23 | + submesh2 = Submesh(mesh, mesh.topological_dimension, 333) |
| 24 | + assert submesh2.cell_set.size == submesh1.cell_set.size |
| 25 | + assert np.allclose(submesh2.coordinates.dat.data, submesh1.coordinates.dat.data) |
| 26 | + |
| 27 | + |
| 28 | +def test_submesh_subdomain_id_nested_tuple(): |
| 29 | + mesh = UnitSquareMesh(4, 4) |
| 30 | + x, y = SpatialCoordinate(mesh) |
| 31 | + M = FunctionSpace(mesh, "DG", 0) |
| 32 | + m1 = Function(M).interpolate(conditional(lt(x, 0.5), 1, 0)) |
| 33 | + m2 = Function(M).interpolate(conditional(lt(y, 0.5), 1, 0)) |
| 34 | + mesh.mark_entities(m1, 111) |
| 35 | + mesh.mark_entities(m2, 222) |
| 36 | + |
| 37 | + subdomain_id = [(111, 222)] |
| 38 | + submesh1 = Submesh(mesh, mesh.topological_dimension, subdomain_id=subdomain_id) |
| 39 | + |
| 40 | + m3 = Function(M).interpolate(m1 * m2) |
| 41 | + expected = assemble(m3*dx) |
| 42 | + assert abs(assemble(1*dx(domain=submesh1)) - expected) < 1E-12 |
| 43 | + |
| 44 | + mesh.mark_entities(m3, 333) |
| 45 | + submesh2 = Submesh(mesh, mesh.topological_dimension, 333) |
| 46 | + assert submesh2.cell_set.size == submesh1.cell_set.size |
| 47 | + assert np.allclose(submesh2.coordinates.dat.data, submesh1.coordinates.dat.data) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("subdomain_id", ["on_boundary", (1, 3, 6)]) |
| 51 | +def test_submesh_facet_subdomain_id_tuple(subdomain_id): |
| 52 | + mesh = UnitCubeMesh(2, 2, 2) |
| 53 | + submesh1 = Submesh(mesh, mesh.topological_dimension - 1, subdomain_id=subdomain_id) |
| 54 | + if subdomain_id == "on_boundary": |
| 55 | + area = assemble(1*ds(domain=mesh)) |
| 56 | + else: |
| 57 | + area = assemble(1*ds(subdomain_id, domain=mesh)) |
| 58 | + assert abs(assemble(1*dx(domain=submesh1)) - area) < 1E-12 |
| 59 | + |
| 60 | + V = FunctionSpace(mesh, "HDiv Trace", 0) |
| 61 | + facet_function = Function(V) |
| 62 | + DirichletBC(V, 1, subdomain_id).apply(facet_function) |
| 63 | + facet_value = 999 |
| 64 | + rmesh = RelabeledMesh(mesh, [facet_function], [facet_value]) |
| 65 | + submesh2 = Submesh(rmesh, mesh.topological_dimension - 1, facet_value) |
| 66 | + assert submesh2.cell_set.size == submesh1.cell_set.size |
| 67 | + assert np.allclose(submesh2.coordinates.dat.data, submesh1.coordinates.dat.data) |
| 68 | + |
| 69 | + |
| 70 | +def test_submesh_facet_subdomain_id_nested_tuple(): |
| 71 | + mesh = UnitSquareMesh(4, 4) |
| 72 | + x, y = SpatialCoordinate(mesh) |
| 73 | + M = FunctionSpace(mesh, "DG", 0) |
| 74 | + m1 = Function(M).interpolate(conditional(lt(x, 0.5), 1, 0)) |
| 75 | + m2 = Function(M).interpolate(conditional(lt(x, 0.5), 0, 1)) |
| 76 | + mesh.mark_entities(m1, 111) |
| 77 | + mesh.mark_entities(m2, 222) |
| 78 | + |
| 79 | + subdomain_id = [(111, 222)] |
| 80 | + submesh1 = Submesh(mesh, mesh.topological_dimension - 1, subdomain_id=subdomain_id, label_name="Cell Sets") |
| 81 | + |
| 82 | + expected = 1 |
| 83 | + assert abs(assemble(1*dx(domain=submesh1)) - expected) < 1E-12 |
| 84 | + |
| 85 | + x, y = SpatialCoordinate(mesh) |
| 86 | + V = FunctionSpace(mesh, "HDiv Trace", 0) |
| 87 | + facet_function = Function(V) |
| 88 | + facet_function.interpolate(conditional(lt(abs(x-0.5), 1E-8), 1, 0)) |
| 89 | + facet_value = 999 |
| 90 | + rmesh = RelabeledMesh(mesh, [facet_function], [facet_value]) |
| 91 | + submesh2 = Submesh(rmesh, mesh.topological_dimension - 1, facet_value) |
| 92 | + assert submesh2.cell_set.size == submesh1.cell_set.size |
| 93 | + assert np.allclose(submesh2.coordinates.dat.data, submesh1.coordinates.dat.data) |
0 commit comments