Skip to content

Commit fa0a850

Browse files
committed
Merge branch 'master' into dham/abstract_reduced_functional
2 parents 9cb41c1 + 7a7d73a commit fa0a850

File tree

3 files changed

+31
-40
lines changed

3 files changed

+31
-40
lines changed

firedrake/preconditioners/hypre_ams.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import firedrake as fd
21
from firedrake.preconditioners.base import PCBase
32
from firedrake.petsc import PETSc
43
from firedrake.functionspace import FunctionSpace, VectorFunctionSpace
54
from firedrake.ufl_expr import TestFunction
65
from firedrake.dmhooks import get_function_space
76
from firedrake.utils import complex_mode
7+
from firedrake.interpolation import interpolate
8+
from firedrake import grad, SpatialCoordinate
89
from firedrake_citations import Citations
910
from pyop2.utils import as_tuple
1011

@@ -28,6 +29,8 @@ def chop(A, tol=1E-10):
2829

2930
class HypreAMS(PCBase):
3031
def initialize(self, obj):
32+
from firedrake.assemble import assemble
33+
3134
if complex_mode:
3235
raise NotImplementedError("HypreAMS preconditioner not yet implemented in complex mode")
3336

@@ -47,7 +50,7 @@ def initialize(self, obj):
4750
P1 = FunctionSpace(mesh, "Lagrange", 1)
4851
G_callback = appctx.get("get_gradient", None)
4952
if G_callback is None:
50-
G = chop(fd.assemble(fd.interpolate(fd.grad(TestFunction(P1)), V)).petscmat)
53+
G = chop(assemble(interpolate(grad(TestFunction(P1)), V)).petscmat)
5154
else:
5255
G = G_callback(P1, V)
5356

@@ -65,8 +68,8 @@ def initialize(self, obj):
6568
pc.setHYPRESetBetaPoissonMatrix(None)
6669

6770
VectorP1 = VectorFunctionSpace(mesh, "Lagrange", 1)
68-
interp = fd.interpolate(fd.SpatialCoordinate(mesh), VectorP1)
69-
pc.setCoordinates(fd.assemble(interp).dat.data_ro.copy())
71+
interp = interpolate(SpatialCoordinate(mesh), VectorP1)
72+
pc.setCoordinates(assemble(interp).dat.data_ro.copy())
7073
pc.setFromOptions()
7174
self.pc = pc
7275

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)