From 64d59bb8940dc6aae2227d24b989f244999d763b Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 28 Jun 2023 11:55:02 -0500 Subject: [PATCH] Fix some deprecated usage in Firedrake connections --- examples/from_firedrake.py | 28 +++++++++++--------- examples/to_firedrake.py | 2 +- meshmode/interop/firedrake/connection.py | 8 +++--- meshmode/interop/firedrake/mesh.py | 8 +++--- meshmode/interop/firedrake/reference_cell.py | 4 +-- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/examples/from_firedrake.py b/examples/from_firedrake.py index c6190e0d9..f681cb207 100644 --- a/examples/from_firedrake.py +++ b/examples/from_firedrake.py @@ -28,7 +28,7 @@ # This example provides a brief template for bringing information in # from firedrake and makes some plots to help you better understand # what a FromBoundaryFiredrakeConnection does -def main(): +def main(visualize=True): # If can't import firedrake, do nothing # # filename MUST include "firedrake" (i.e. match *firedrake*.py) in order @@ -52,7 +52,7 @@ def main(): # Make connections cl_ctx = cl.create_some_context() queue = cl.CommandQueue(cl_ctx) - actx = PyOpenCLArrayContext(queue) + actx = PyOpenCLArrayContext(queue, force_device_scalars=True) fd_connection = build_connection_from_firedrake(actx, fd_fspace) fd_bdy_connection = \ @@ -84,24 +84,26 @@ def main(): vis = make_visualizer(actx, discr, discr.groups[0].order+3) field = fd_connection.from_firedrake(fd_fntn, actx=actx) - fig = plt.figure() - ax1 = fig.add_subplot(1, 2, 1, projection="3d") - ax1.set_title("cos(x+y) in\nFiredrakeConnection") - vis.show_scalar_in_matplotlib_3d(field, do_show=False) + if visualize: + fig = plt.figure() + ax1 = fig.add_subplot(1, 2, 1, projection="3d") + ax1.set_title("cos(x+y) in\nFiredrakeConnection") + vis.show_scalar_in_matplotlib_3d(field, do_show=False) # Now repeat using FiredrakeConnection restricted to "on_boundary" bdy_discr = fd_bdy_connection.discr bdy_vis = make_visualizer(actx, bdy_discr, bdy_discr.groups[0].order+3) bdy_field = fd_bdy_connection.from_firedrake(fd_fntn, actx=actx) - ax2 = fig.add_subplot(1, 2, 2, projection="3d") - plt.sca(ax2) - ax2.set_title("cos(x+y) in\nFiredrakeConnection 'on_boundary'") - bdy_vis.show_scalar_in_matplotlib_3d(bdy_field, do_show=False) + if visualize: + ax2 = fig.add_subplot(1, 2, 2, projection="3d") + plt.sca(ax2) + ax2.set_title("cos(x+y) in\nFiredrakeConnection 'on_boundary'") + bdy_vis.show_scalar_in_matplotlib_3d(bdy_field, do_show=False) - import matplotlib.cm as cm - fig.colorbar(cm.ScalarMappable(), ax=ax2) - plt.show() + import matplotlib.cm as cm + fig.colorbar(cm.ScalarMappable(), ax=ax2) + plt.show() if __name__ == "__main__": diff --git a/examples/to_firedrake.py b/examples/to_firedrake.py index c220c3b41..3f32fd020 100644 --- a/examples/to_firedrake.py +++ b/examples/to_firedrake.py @@ -51,7 +51,7 @@ def main(): cl_ctx = cl.create_some_context() queue = cl.CommandQueue(cl_ctx) - actx = PyOpenCLArrayContext(queue) + actx = PyOpenCLArrayContext(queue, force_device_scalars=True) nel_1d = 16 from meshmode.mesh.generation import generate_regular_rect_mesh diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index 28941cb8f..5b6f7fbfb 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -509,9 +509,8 @@ def from_meshmode(self, mm_field, out=None): :return: a :class:`firedrake.function.Function` holding the transported data (*out*, if *out* was not *None*) """ - # All firedrake functions are the same dtype - dtype = self.firedrake_fspace().mesh().coordinates.dat.data.dtype - self._validate_field(mm_field, "mm_field", dtype=dtype) + # Don't check dtype: if it's numpy-assignable, that's good enough for us. + self._validate_field(mm_field, "mm_field") # get the shape of mm_field from meshmode.dof_array import DOFArray @@ -523,7 +522,8 @@ def from_meshmode(self, mm_field, out=None): # make sure out is a firedrake function in an appropriate # function space if out is not None: - self._validate_function(out, "out", fspace_shape, dtype) + # Don't check dtype: if it's numpy-assignable, that's good enough for us. + self._validate_function(out, "out", fspace_shape) else: from firedrake.function import Function # Translate shape so that don't always get a TensorFunctionSpace, diff --git a/meshmode/interop/firedrake/mesh.py b/meshmode/interop/firedrake/mesh.py index 75f356ff1..35dec0b52 100644 --- a/meshmode/interop/firedrake/mesh.py +++ b/meshmode/interop/firedrake/mesh.py @@ -24,7 +24,7 @@ import logging import numpy as np -from modepy import resampling_matrix, simplex_best_available_basis +from modepy import resampling_matrix, basis_for_space, PN, Simplex from meshmode.mesh import ( BTAG_ALL, @@ -875,7 +875,7 @@ def export_mesh_to_firedrake(mesh, group_nr=None, comm=None): import firedrake.mesh as fd_mesh with _PyOp2CommDuplicator(comm) as comm: - plex = fd_mesh._from_cell_list(group.dim, cells, coords, comm) + plex = fd_mesh.plex_from_cell_list(group.dim, cells, coords, comm) # Nb : One might be tempted to pass reorder=False and thereby save some # hassle in exchange for forcing firedrake to have slightly @@ -949,8 +949,8 @@ def export_mesh_to_firedrake(mesh, group_nr=None, comm=None): fd_unit_nodes = get_finat_element_unit_nodes(coords_fspace.finat_element) fd_unit_nodes = fd_ref_cell_to_mm(fd_unit_nodes) - basis = simplex_best_available_basis(group.dim, group.order) - resampling_mat = resampling_matrix(basis, + basis = basis_for_space(PN(group.dim, group.order), Simplex(group.dim)) + resampling_mat = resampling_matrix(basis.functions, new_nodes=fd_unit_nodes, old_nodes=group.unit_nodes) # Store the meshmode data resampled to firedrake unit nodes diff --git a/meshmode/interop/firedrake/reference_cell.py b/meshmode/interop/firedrake/reference_cell.py index 327f9cd42..f303fefb8 100644 --- a/meshmode/interop/firedrake/reference_cell.py +++ b/meshmode/interop/firedrake/reference_cell.py @@ -64,11 +64,11 @@ def get_affine_reference_simplex_mapping(ambient_dim, firedrake_to_meshmode=True f"'{type(firedrake_to_meshmode)}'") from FIAT.reference_element import ufc_simplex - from modepy.tools import unit_vertices + from modepy import unit_vertices_for_shape, Simplex # Get the unit vertices from each system, # each stored with shape *(dim, nunit_vertices)* firedrake_unit_vertices = np.array(ufc_simplex(ambient_dim).vertices).T - modepy_unit_vertices = unit_vertices(ambient_dim).T + modepy_unit_vertices = unit_vertices_for_shape(Simplex(ambient_dim)) if firedrake_to_meshmode: from_verts = firedrake_unit_vertices