11import pytest
22import numpy as np
33from 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 )
415
516
617@pytest .mark .parallel ([1 , 3 ])
718def test_create_submesh_comm_self ():
8- comm = COMM_SELF
919 subdomain_id = None
1020 nx = 4
1121 mesh = UnitSquareMesh (nx , nx , quadrilateral = True , reorder = False )
12- submesh = Submesh (mesh , mesh .topological_dimension , subdomain_id , ignore_halo = True , comm = comm , reorder = False )
22+ submesh = Submesh (mesh , mesh .topological_dimension , subdomain_id , ignore_halo = True , reorder = False , comm = COMM_SELF )
1323 assert submesh .submesh_parent is mesh
1424 assert submesh .comm .size == 1
1525 assert submesh .cell_set .size == mesh .cell_set .size
@@ -18,24 +28,51 @@ def test_create_submesh_comm_self():
1828
1929@pytest .mark .parallel ([1 , 3 ])
2030def test_assemble_submesh_comm_self ():
21- comm = COMM_SELF
2231 subdomain_id = None
2332 nx = 6
2433 ny = 5
2534 px = - np .cos (np .linspace (0 , np .pi , nx ))
2635 py = - np .cos (np .linspace (0 , np .pi , ny ))
2736 mesh = TensorRectangleMesh (px , py , reorder = False )
28- submesh = Submesh (mesh , mesh .topological_dimension , subdomain_id , ignore_halo = True , comm = comm , 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 )
2941
3042 V = FunctionSpace (mesh , "DG" , 0 )
3143 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 )
3266
3367 Vsub = FunctionSpace (submesh , "DG" , 0 )
34- Asub = assemble (inner (TrialFunction (Vsub ), TestFunction (Vsub ))* dx )
68+ Asub = assemble (inner (TrialFunction (Vsub ), TestFunction (Vsub )) * dx )
3569
36- i0 , j0 , v0 = A .petscmat .getValuesCSR ()
37- j0 -= V .dof_dset .layout_vec .getOwnershipRange ()[0 ]
38- i1 , j1 , v1 = Asub .petscmat .getValuesCSR ()
39- assert np .array_equal (i0 , i1 )
40- assert np .array_equal (j0 , j1 )
41- assert np .allclose (v0 , v1 )
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