From 5f0529809650e02ef3a59cf97318f51c344817b4 Mon Sep 17 00:00:00 2001 From: benSepanski Date: Tue, 2 Mar 2021 12:15:34 -0600 Subject: [PATCH 1/8] Update connection to allow tuples of valid bdy ids like in firedrake --- meshmode/interop/firedrake/connection.py | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index da42c960..ac7b7385 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -672,11 +672,29 @@ PolynomialWarpAndBlendGroupFactory` is used. if restrict_to_boundary is not None: uniq_markers = fdrake_fspace.mesh().exterior_facets.unique_markers allowable_bdy_ids = list(uniq_markers) + ["on_boundary"] - if restrict_to_boundary not in allowable_bdy_ids: - raise ValueError("'restrict_to_boundary' must be one of" - " the following allowable boundary ids: " - f"{allowable_bdy_ids}, not " - f"'{restrict_to_boundary}'") + # make sure restrict_to_boundary is of correct type + typeCheck = isinstance(restrict_to_boundary, int) + if not typeCheck: + isTuple = isinstance(restrict_to_boundary, tuple) + if isTuple: + typeCheck = all([isinstance(x, int) for x in restrict_to_boundary]) + else: + typeCheck = restrict_to_boundary == "on_boundary" + if not typeCheck: + raise TypeError("restrict_to_boundary must be an int, a tuple" + " of ints, or the string \"on_boundary\", not" + f" of type {type(restrict_to_boundary)}") + # convert int to tuple to avoid corner cases + else: + restrict_to_boundary = (restrict_to_boundary,) + # make sure all markers are valid + if restrict_to_boundary != "on_boundary": + for marker in restrict_to_boundary: + if marker not in allowable_bdy_ids: + raise ValueError("'restrict_to_boundary' must be one of" + " the following allowable boundary ids: " + f"{allowable_bdy_ids}, not " + f"'{restrict_to_boundary}'") # If only converting a portion of the mesh near the boundary, get # *cells_to_use* as described in -- GitLab From dc7a67775635cbb5f7c84382269a840f3b1771a3 Mon Sep 17 00:00:00 2001 From: benSepanski Date: Tue, 2 Mar 2021 12:18:18 -0600 Subject: [PATCH 2/8] Allow conversion of external operators --- meshmode/interop/firedrake/connection.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index ac7b7385..113ffbd0 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -283,9 +283,18 @@ class FiredrakeConnection: """ # Validate that *function* is convertible from firedrake.function import Function - if not isinstance(function, Function): - raise TypeError(f"'{function_name} must be a firedrake Function" - f" but is of unexpected type '{type(function)}'") + # FIXME : Once ExternalOperator is fully implemented, we don't need + # to try/except this block + try: + from firedrake.pointwise_operators import ExternalOperator + if not isinstance(function, (Function, ExternalOperator)): + raise TypeError(f"'{function_name} must be a firedrake Function" + " or ExternalOperator, " + f" but is of unexpected type '{type(function)}'") + except ImportError: + if not isinstance(function, Function): + raise TypeError(f"'{function_name} must be a firedrake Function" + f" but is of unexpected type '{type(function)}'") ufl_elt = function.function_space().ufl_element() if ufl_elt.family() != self._ufl_element.family(): raise ValueError(f"'{function_name}.function_space().ufl_element()" -- GitLab From da0d4d3ce05abbaab1963bf2583cf5a5e8b94a07 Mon Sep 17 00:00:00 2001 From: benSepanski Date: Tue, 2 Mar 2021 17:00:00 -0600 Subject: [PATCH 3/8] flake8 fix --- meshmode/interop/firedrake/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index 113ffbd0..45990359 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -691,7 +691,7 @@ PolynomialWarpAndBlendGroupFactory` is used. typeCheck = restrict_to_boundary == "on_boundary" if not typeCheck: raise TypeError("restrict_to_boundary must be an int, a tuple" - " of ints, or the string \"on_boundary\", not" + " of ints, or the string 'on_boundary', not" f" of type {type(restrict_to_boundary)}") # convert int to tuple to avoid corner cases else: -- GitLab From 8e7ce440a8c63b5639f865381580465b0101dbfd Mon Sep 17 00:00:00 2001 From: benSepanski Date: Tue, 2 Mar 2021 17:01:43 -0600 Subject: [PATCH 4/8] flake8 fix --- meshmode/interop/firedrake/connection.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index 45990359..80caf089 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -682,14 +682,14 @@ PolynomialWarpAndBlendGroupFactory` is used. uniq_markers = fdrake_fspace.mesh().exterior_facets.unique_markers allowable_bdy_ids = list(uniq_markers) + ["on_boundary"] # make sure restrict_to_boundary is of correct type - typeCheck = isinstance(restrict_to_boundary, int) - if not typeCheck: + type_check = isinstance(restrict_to_boundary, int) + if not type_check: isTuple = isinstance(restrict_to_boundary, tuple) if isTuple: - typeCheck = all([isinstance(x, int) for x in restrict_to_boundary]) + type_check = all([isinstance(x, int) for x in restrict_to_boundary]) else: - typeCheck = restrict_to_boundary == "on_boundary" - if not typeCheck: + type_check = restrict_to_boundary == "on_boundary" + if not type_check: raise TypeError("restrict_to_boundary must be an int, a tuple" " of ints, or the string 'on_boundary', not" f" of type {type(restrict_to_boundary)}") -- GitLab From 9f2b9ff2027c4411c6e7506cf3648707ef03b0be Mon Sep 17 00:00:00 2001 From: benSepanski Date: Tue, 2 Mar 2021 17:02:40 -0600 Subject: [PATCH 5/8] flake8 fix --- meshmode/interop/firedrake/connection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index 80caf089..ad52602b 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -684,8 +684,8 @@ PolynomialWarpAndBlendGroupFactory` is used. # make sure restrict_to_boundary is of correct type type_check = isinstance(restrict_to_boundary, int) if not type_check: - isTuple = isinstance(restrict_to_boundary, tuple) - if isTuple: + is_tuple = isinstance(restrict_to_boundary, tuple) + if is_tuple: type_check = all([isinstance(x, int) for x in restrict_to_boundary]) else: type_check = restrict_to_boundary == "on_boundary" -- GitLab From 9491e2f35c57773255be35e2ee329c0503c28166 Mon Sep 17 00:00:00 2001 From: benSepanski Date: Fri, 12 Mar 2021 09:58:06 -0600 Subject: [PATCH 6/8] Make try/catch block smaller --- meshmode/interop/firedrake/connection.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index ad52602b..c58a9e08 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -283,18 +283,19 @@ class FiredrakeConnection: """ # Validate that *function* is convertible from firedrake.function import Function + function_is_function_type = isinstance(function, Function) # FIXME : Once ExternalOperator is fully implemented, we don't need # to try/except this block - try: - from firedrake.pointwise_operators import ExternalOperator - if not isinstance(function, (Function, ExternalOperator)): - raise TypeError(f"'{function_name} must be a firedrake Function" - " or ExternalOperator, " - f" but is of unexpected type '{type(function)}'") - except ImportError: - if not isinstance(function, Function): - raise TypeError(f"'{function_name} must be a firedrake Function" - f" but is of unexpected type '{type(function)}'") + if not function_is_function_type: + try: + from firedrake.pointwise_operators import ExternalOperator + function_is_extop_type = isinstance(function, ExternalOperator) + except ImportError: + function_is_extop_type = False + if not function_is_function_type and not function_is_extop_type: + raise TypeError(f"'{function_name} must be a firedrake Function" + " or ExternalOperator, " + f" but is of unexpected type '{type(function)}'") ufl_elt = function.function_space().ufl_element() if ufl_elt.family() != self._ufl_element.family(): raise ValueError(f"'{function_name}.function_space().ufl_element()" -- GitLab From ec7a6455cb02c5b2ef8f3f71af9dcdb540fbb48d Mon Sep 17 00:00:00 2001 From: benSepanski Date: Fri, 12 Mar 2021 10:02:58 -0600 Subject: [PATCH 7/8] improve/fix docs --- meshmode/interop/firedrake/connection.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index c58a9e08..26cd02f7 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -131,6 +131,7 @@ class FiredrakeConnection: .. automethod:: __init__ .. automethod:: from_meshmode .. automethod:: from_firedrake + .. automethod:: firedrake_fspace """ def __init__(self, discr, fdrake_fspace, mm2fd_node_mapping, group_nr=None): """ @@ -630,8 +631,14 @@ PolynomialRecursiveNodesGroupFactory` with ``"lgl"`` nodes is used. imported, a :class:`~meshmode.discretization.poly_element.\ PolynomialWarpAndBlendGroupFactory` is used. :arg restrict_to_boundary: (optional) - If not *None*, then must be a valid boundary marker for - ``fdrake_fspace.mesh()``. In this case, creates a + If not *None*, then must be one of the following: + + * A valid boundary marker for ``fdrake_fspace.mesh()`` + * A tuple of valid boundary markers for ``fdrake_fspace.mesh()`` + * The string ``"on_boundary"`` (:mod:`firedrake` equivalent + to :class:`~meshmode.mesh.BTAG_ALL`). + + If not *None*, creates a :class:`~meshmode.discretization.Discretization` on a submesh of ``fdrake_fspace.mesh()`` created from the cells with at least one vertex on a facet marked with the marker -- GitLab From 05adf1c5c8c94f34a1b6484f926c974b799956f2 Mon Sep 17 00:00:00 2001 From: benSepanski Date: Fri, 12 Mar 2021 10:16:02 -0600 Subject: [PATCH 8/8] Convert to if-elif --- meshmode/interop/firedrake/connection.py | 52 +++++++++++++----------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/meshmode/interop/firedrake/connection.py b/meshmode/interop/firedrake/connection.py index 26cd02f7..bbdf8369 100644 --- a/meshmode/interop/firedrake/connection.py +++ b/meshmode/interop/firedrake/connection.py @@ -686,32 +686,36 @@ PolynomialWarpAndBlendGroupFactory` is used. except ImportError: # If cannot be imported, uses warp-and-blend nodes grp_factory = PolynomialWarpAndBlendGroupFactory(degree) + # validate restrict_to_boundary, if present if restrict_to_boundary is not None: - uniq_markers = fdrake_fspace.mesh().exterior_facets.unique_markers - allowable_bdy_ids = list(uniq_markers) + ["on_boundary"] - # make sure restrict_to_boundary is of correct type - type_check = isinstance(restrict_to_boundary, int) - if not type_check: - is_tuple = isinstance(restrict_to_boundary, tuple) - if is_tuple: - type_check = all([isinstance(x, int) for x in restrict_to_boundary]) - else: - type_check = restrict_to_boundary == "on_boundary" - if not type_check: - raise TypeError("restrict_to_boundary must be an int, a tuple" - " of ints, or the string 'on_boundary', not" - f" of type {type(restrict_to_boundary)}") - # convert int to tuple to avoid corner cases + firedrake_bdy_ids = fdrake_fspace.mesh().exterior_facets.unique_markers + # Integer case, just make sure it is a valid bdy id + if isinstance(restrict_to_boundary, int): + if restrict_to_boundary not in firedrake_bdy_ids: + raise ValueError("Unrecognized boundary id " + f"{restrict_to_boundary}. Valid boundary " + f"ids: {firedrake_bdy_ids}") + # Tuple case, make sure it is a tuple of valid bdy ids + elif isinstance(restrict_to_boundary, tuple): + for bdy_id in restrict_to_boundary: + if not isinstance(bdy_id, int): + raise TypeError(f"Invalid type {type(bdy_id)} in " + "restrict_to_boundary. When " + "restrict_to_boundary is a tuple, each " + "entry must be of type int") + if bdy_id not in firedrake_bdy_ids: + raise ValueError(f"Unrecognized boundary id {bdy_id}. Valid" + f" boundary ids: {firedrake_bdy_ids}.") + # String case, must be "on_boundary" + elif isinstance(restrict_to_boundary, str): + if restrict_to_boundary != "on_boundary": + raise ValueError(f"Unexpected value '{restrict_to_boundary}' " + "for restrict_to_boundary. Only valid string " + "is 'on_boundary'") else: - restrict_to_boundary = (restrict_to_boundary,) - # make sure all markers are valid - if restrict_to_boundary != "on_boundary": - for marker in restrict_to_boundary: - if marker not in allowable_bdy_ids: - raise ValueError("'restrict_to_boundary' must be one of" - " the following allowable boundary ids: " - f"{allowable_bdy_ids}, not " - f"'{restrict_to_boundary}'") + raise TypeError(f"Invalid type {type(restrict_to_boundary)} for " + "restrict_to_boundary. Must be an int, a tuple " + "of ints, or the string 'on_boundary'.") # If only converting a portion of the mesh near the boundary, get # *cells_to_use* as described in -- GitLab