Skip to content

Commit 7a7d73a

Browse files
add petsc_raises to tests (#4443)
* add `petsc_raises` to tests * Increase permissiveness of petsc_raises Previously issues were encountered if the garbage collection routines threw an error. This should stop being an issue when the original error stops getting destroyed too early (should be fixed in later Python versions). --------- Co-authored-by: Connor Ward <c.ward20@imperial.ac.uk>
1 parent a90df34 commit 7a7d73a

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

tests/firedrake/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ def __enter__(self):
205205
pass
206206

207207
def __exit__(self, exc_type, exc_val, traceback):
208-
# There is a bug where 'exc_val' is occasionally 'None'. In my tests
209-
# this error only exists for Python < 3.12.11.
208+
# There is a bug where 'exc_val' is occasionally the wrong thing,
209+
# either 'None' or some unrelated garbage collection error. In my
210+
# tests this error only exists for Python < 3.12.11.
210211
if exc_type is PETSc.Error:
211212
if sys.version_info < (3, 12, 11):
212-
if exc_val is None or isinstance(exc_val.__cause__, self.exc_type):
213-
return True
213+
return True
214214
else:
215215
if isinstance(exc_val.__cause__, self.exc_type):
216216
return True

tests/firedrake/vertexonly/test_vertex_only_fs.py

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def pseudo_random_coords(size):
6868

6969
# Function Space Generation Tests
7070

71-
def functionspace_tests(vm):
71+
def functionspace_tests(vm, petsc_raises):
7272
# Prep
7373
num_cells = len(vm.coordinates.dat.data_ro)
7474
num_cells_mpi_global = MPI.COMM_WORLD.allreduce(num_cells, op=MPI.SUM)
@@ -153,34 +153,28 @@ def functionspace_tests(vm):
153153
h_star = h.riesz_representation(riesz_map="l2")
154154
g = assemble(I_io.interpolate(h_star, adjoint=True))
155155
assert np.allclose(g.dat.data_ro_with_halos, np.prod(vm.coordinates.dat.data_ro_with_halos.reshape(-1, vm.geometric_dimension()), axis=1))
156-
with pytest.raises(NotImplementedError):
156+
with petsc_raises(NotImplementedError):
157157
# Can't use adjoint on interpolators with expressions yet
158-
try:
159-
g2 = assemble(I2_io.interpolate(h_star, adjoint=True))
160-
assert np.allclose(g2.dat.data_ro_with_halos, 2*np.prod(vm.coordinates.dat.data_ro_with_halos.reshape(-1, vm.geometric_dimension()), axis=1))
161-
except PETSc.Error as e:
162-
raise e.__cause__ from None
158+
g2 = assemble(I2_io.interpolate(h_star, adjoint=True))
159+
assert np.allclose(g2.dat.data_ro_with_halos, 2*np.prod(vm.coordinates.dat.data_ro_with_halos.reshape(-1, vm.geometric_dimension()), axis=1))
163160

164161
I_io_adjoint = Interpolator(TestFunction(W), V)
165162
I2_io_adjoint = Interpolator(2*TestFunction(W), V)
166163
h_star = assemble(I_io_adjoint.interpolate(g, adjoint=True))
167164
h = h_star.riesz_representation(riesz_map="l2")
168165
assert np.allclose(h.dat.data_ro_with_halos[idxs_to_include], np.prod(vm.input_ordering.coordinates.dat.data_ro_with_halos[idxs_to_include].reshape(-1, vm.input_ordering.geometric_dimension()), axis=1))
169166
assert np.all(h.dat.data_ro_with_halos[~idxs_to_include] == 0)
170-
with pytest.raises(NotImplementedError):
167+
with petsc_raises(NotImplementedError):
171168
# Can't use adjoint on interpolators with expressions yet
172-
try:
173-
h2 = assemble(I2_io_adjoint.interpolate(g, adjoint=True))
174-
assert np.allclose(h2.dat.data_ro_with_halos[idxs_to_include], 2*np.prod(vm.input_ordering.coordinates.dat.data_ro_with_halos[idxs_to_include].reshape(-1, vm.input_ordering.geometric_dimension()), axis=1))
175-
except PETSc.Error as e:
176-
raise e.__cause__ from None
169+
h2 = assemble(I2_io_adjoint.interpolate(g, adjoint=True))
170+
assert np.allclose(h2.dat.data_ro_with_halos[idxs_to_include], 2*np.prod(vm.input_ordering.coordinates.dat.data_ro_with_halos[idxs_to_include].reshape(-1, vm.input_ordering.geometric_dimension()), axis=1))
177171
g = assemble(I_io_adjoint.interpolate(h))
178172
assert np.allclose(g.dat.data_ro_with_halos, np.prod(vm.coordinates.dat.data_ro_with_halos.reshape(-1, vm.geometric_dimension()), axis=1))
179173
g2 = assemble(I2_io_adjoint.interpolate(h))
180174
assert np.allclose(g2.dat.data_ro_with_halos, 2*np.prod(vm.coordinates.dat.data_ro_with_halos.reshape(-1, vm.geometric_dimension()), axis=1))
181175

182176

183-
def vectorfunctionspace_tests(vm):
177+
def vectorfunctionspace_tests(vm, petsc_raises):
184178
# Prep
185179
gdim = vm.geometric_dimension()
186180
num_cells = len(vm.coordinates.dat.data_ro)
@@ -263,26 +257,20 @@ def vectorfunctionspace_tests(vm):
263257
h_star = h.riesz_representation(riesz_map="l2")
264258
g = assemble(I_io.interpolate(h_star, adjoint=True))
265259
assert np.allclose(g.dat.data_ro_with_halos, 2*vm.coordinates.dat.data_ro_with_halos)
266-
with pytest.raises(NotImplementedError):
260+
with petsc_raises(NotImplementedError):
267261
# Can't use adjoint on interpolators with expressions yet
268-
try:
269-
g2 = assemble(I2_io.interpolate(h_star, adjoint=True))
270-
assert np.allclose(g2.dat.data_ro_with_halos, 4*vm.coordinates.dat.data_ro_with_halos)
271-
except PETSc.Error as e:
272-
raise e.__cause__ from None
262+
g2 = assemble(I2_io.interpolate(h_star, adjoint=True))
263+
assert np.allclose(g2.dat.data_ro_with_halos, 4*vm.coordinates.dat.data_ro_with_halos)
273264

274265
I_io_adjoint = Interpolator(TestFunction(W), V)
275266
I2_io_adjoint = Interpolator(2*TestFunction(W), V)
276267
h_star = assemble(I_io_adjoint.interpolate(g, adjoint=True))
277268
assert np.allclose(h_star.dat.data_ro[idxs_to_include], 2*vm.input_ordering.coordinates.dat.data_ro_with_halos[idxs_to_include])
278269
assert np.all(h_star.dat.data_ro_with_halos[~idxs_to_include] == 0)
279-
with pytest.raises(NotImplementedError):
280-
try:
281-
# Can't use adjoint on interpolators with expressions yet
282-
h2 = assemble(I2_io_adjoint.interpolate(g, adjoint=True))
283-
assert np.allclose(h2.dat.data_ro[idxs_to_include], 4*vm.input_ordering.coordinates.dat.data_ro_with_halos[idxs_to_include])
284-
except PETSc.Error as e:
285-
raise e.__cause__ from None
270+
with petsc_raises(NotImplementedError):
271+
# Can't use adjoint on interpolators with expressions yet
272+
h2 = assemble(I2_io_adjoint.interpolate(g, adjoint=True))
273+
assert np.allclose(h2.dat.data_ro[idxs_to_include], 4*vm.input_ordering.coordinates.dat.data_ro_with_halos[idxs_to_include])
286274

287275
h = h_star.riesz_representation(riesz_map="l2")
288276
g = assemble(I_io_adjoint.interpolate(h))
@@ -292,12 +280,12 @@ def vectorfunctionspace_tests(vm):
292280

293281

294282
@pytest.mark.parallel([1, 3])
295-
def test_functionspaces(parentmesh, vertexcoords):
283+
def test_functionspaces(parentmesh, vertexcoords, petsc_raises):
296284
vm = VertexOnlyMesh(parentmesh, vertexcoords, missing_points_behaviour=None)
297-
functionspace_tests(vm)
298-
vectorfunctionspace_tests(vm)
299-
functionspace_tests(vm.input_ordering)
300-
vectorfunctionspace_tests(vm.input_ordering)
285+
functionspace_tests(vm, petsc_raises)
286+
vectorfunctionspace_tests(vm, petsc_raises)
287+
functionspace_tests(vm.input_ordering, petsc_raises)
288+
vectorfunctionspace_tests(vm.input_ordering, petsc_raises)
301289

302290

303291
@pytest.mark.parallel(nprocs=2)

0 commit comments

Comments
 (0)