|
| 1 | +import pytest |
| 2 | +import numpy as np |
| 3 | +from firedrake import * |
| 4 | +from firedrake.petsc import PETSc |
| 5 | + |
| 6 | + |
| 7 | +def assert_local_equality(A, B): |
| 8 | + i0, j0, v0 = A.getValuesCSR() |
| 9 | + i1, j1, v1 = B.getValuesCSR() |
| 10 | + j0 -= A.createVecs()[0].getOwnershipRange()[0] |
| 11 | + j1 -= B.createVecs()[0].getOwnershipRange()[0] |
| 12 | + assert np.array_equal(i0, i1) |
| 13 | + assert np.array_equal(j0, j1) |
| 14 | + assert np.allclose(v0, v1) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parallel([1, 3]) |
| 18 | +def test_create_submesh_comm_self(): |
| 19 | + subdomain_id = None |
| 20 | + nx = 4 |
| 21 | + mesh = UnitSquareMesh(nx, nx, quadrilateral=True, reorder=False) |
| 22 | + submesh = Submesh(mesh, mesh.topological_dimension, subdomain_id, ignore_halo=True, reorder=False, comm=COMM_SELF) |
| 23 | + assert submesh.submesh_parent is mesh |
| 24 | + assert submesh.comm.size == 1 |
| 25 | + assert submesh.cell_set.size == mesh.cell_set.size |
| 26 | + assert np.allclose(mesh.coordinates.dat.data_ro, submesh.coordinates.dat.data_ro) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parallel([1, 3]) |
| 30 | +def test_assemble_submesh_comm_self(): |
| 31 | + subdomain_id = None |
| 32 | + nx = 6 |
| 33 | + ny = 5 |
| 34 | + px = -np.cos(np.linspace(0, np.pi, nx)) |
| 35 | + py = -np.cos(np.linspace(0, np.pi, ny)) |
| 36 | + mesh = TensorRectangleMesh(px, py, reorder=False) |
| 37 | + submesh = Submesh(mesh, mesh.topological_dimension, subdomain_id, ignore_halo=True, reorder=False, comm=COMM_SELF) |
| 38 | + |
| 39 | + Vsub = FunctionSpace(submesh, "DG", 0) |
| 40 | + Asub = assemble(inner(TrialFunction(Vsub), TestFunction(Vsub))*dx) |
| 41 | + |
| 42 | + V = FunctionSpace(mesh, "DG", 0) |
| 43 | + A = assemble(inner(TrialFunction(V), TestFunction(V))*dx) |
| 44 | + assert_local_equality(A.petscmat, Asub.petscmat) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parallel([1, 3]) |
| 48 | +@pytest.mark.parametrize("label", ["some", "all"]) |
| 49 | +def test_label_submesh_comm_self(label): |
| 50 | + subdomain_id = 999 |
| 51 | + nx = 8 |
| 52 | + mesh = UnitSquareMesh(nx, nx, reorder=False) |
| 53 | + |
| 54 | + M = FunctionSpace(mesh, "DG", 0) |
| 55 | + marker = Function(M) |
| 56 | + if label == "some": |
| 57 | + x, y = SpatialCoordinate(mesh) |
| 58 | + marker.interpolate(conditional(Or(x > 0.5, y > 0.5), 1, 0)) |
| 59 | + elif label == "all": |
| 60 | + marker.assign(1) |
| 61 | + else: |
| 62 | + raise ValueError(f"Unrecognized label {label}") |
| 63 | + |
| 64 | + mesh = RelabeledMesh(mesh, [marker], [subdomain_id]) |
| 65 | + submesh = Submesh(mesh, mesh.topological_dimension, subdomain_id, ignore_halo=True, reorder=False, comm=COMM_SELF) |
| 66 | + |
| 67 | + Vsub = FunctionSpace(submesh, "DG", 0) |
| 68 | + Asub = assemble(inner(TrialFunction(Vsub), TestFunction(Vsub)) * dx) |
| 69 | + |
| 70 | + V = FunctionSpace(mesh, "DG", 0) |
| 71 | + A = assemble(inner(TrialFunction(V), TestFunction(V)) * dx) |
| 72 | + if label == "all": |
| 73 | + assert_local_equality(A.petscmat, Asub.petscmat) |
| 74 | + else: |
| 75 | + lgmap = V.dof_dset.lgmap |
| 76 | + indices = PETSc.IS().createGeneral(lgmap.apply(np.flatnonzero(marker.dat.data).astype(PETSc.IntType))) |
| 77 | + Amat = A.petscmat.createSubMatrix(indices, indices) |
| 78 | + assert_local_equality(Amat, Asub.petscmat) |
0 commit comments