From cd138164db29d0ecdcc99b03ca006b00a81350e9 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 26 Jun 2020 15:58:27 -0500 Subject: [PATCH 1/7] DOFArray: Permit variable final dimension sizes --- meshmode/dof_array.py | 46 ++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/meshmode/dof_array.py b/meshmode/dof_array.py index 3136484c..7edfc2a9 100644 --- a/meshmode/dof_array.py +++ b/meshmode/dof_array.py @@ -21,7 +21,7 @@ THE SOFTWARE. """ import numpy as np -from typing import Optional, TYPE_CHECKING +from typing import Optional, Iterable, TYPE_CHECKING from functools import partial from pytools import single_valued, memoize_in @@ -53,11 +53,15 @@ class DOFArray(np.ndarray): :class:`~meshmode.discretization.ElementGroupBase`. The arrays contained within a :class:`DOFArray` are expected to be logically two-dimensional, with shape - ``(nelements, nunit_dofs)``, using - :class:`~meshmode.discretization.ElementGroupBase.nelements` - and :class:`~meshmode.discretization.ElementGroupBase.nunit_dofs`. - It is derived from :class:`numpy.ndarray` with dtype object ("an object array"). - The entries in this array are further arrays managed by :attr:`array_context`. + ``(nelements, ndofs_per_element)``, where ``nelements`` is the same as + :attr:`~meshmode.discretization.ElementGroupBase.nelements` + of the associated group. + ``ndofs_per_element`` is typically, but not necessarily, the same as + :attr:`~meshmode.discretization.ElementGroupBase.nunit_dofs` + of the associated group. + This array is derived from :class:`numpy.ndarray` with dtype object ("an + object array"). The entries in this array are further arrays managed by + :attr:`array_context`. One main purpose of this class is to describe the data structure, i.e. when a :class:`DOFArray` occurs inside of further numpy object array, @@ -74,6 +78,7 @@ class DOFArray(np.ndarray): contained in this instance. .. automethod:: from_list + """ # Follows https://numpy.org/devdocs/user/basics.subclassing.html @@ -190,7 +195,7 @@ def flatten(ary: np.ndarray) -> np.ndarray: @memoize_in(actx, (flatten, "flatten_prg")) def prg(): return make_loopy_program( - "{[iel,idof]: 0<=iel np.ndarray: return result -def unflatten(actx: ArrayContext, discr: "_Discretization", ary) -> np.ndarray: +def unflatten(actx: ArrayContext, discr: "_Discretization", ary, + ndofs_per_element_per_group: Optional[Iterable[int]] = None) -> np.ndarray: r"""Convert a 'flat' array returned by :func:`flatten` back to a :class:`DOFArray`. Vectorizes over object arrays of :class:`DOFArray`\ s. @@ -211,17 +217,26 @@ def unflatten(actx: ArrayContext, discr: "_Discretization", ary) -> np.ndarray: and ary.dtype.char == "O" and not isinstance(ary, DOFArray)): return obj_array_vectorize( - lambda subary: unflatten(actx, discr, subary), + lambda subary: unflatten( + actx, discr, subary, ndofs_per_element_per_group), ary) @memoize_in(actx, (unflatten, "unflatten_prg")) def prg(): return make_loopy_program( - "{[iel,idof]: 0<=iel np.ndarray: prg(), grp_start=grp_start, ary=ary, nelements=grp.nelements, - nunit_dofs=grp.nunit_dofs, + ndofs_per_element=ndofs_per_element, )["result"]) - for grp_start, grp in zip(group_starts, discr.groups)]) + for grp_start, grp, ndofs_per_element in zip( + group_starts, + discr.groups, + ndofs_per_element_per_group)]) def flat_norm(ary: DOFArray, ord=2): -- GitLab From 47619d048906cc7f8a9aa9936956f6765f7b6c37 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 26 Jun 2020 23:00:23 +0200 Subject: [PATCH 2/7] Whitespace fix --- meshmode/dof_array.py | 1 - 1 file changed, 1 deletion(-) diff --git a/meshmode/dof_array.py b/meshmode/dof_array.py index 7edfc2a9..63c108c1 100644 --- a/meshmode/dof_array.py +++ b/meshmode/dof_array.py @@ -78,7 +78,6 @@ class DOFArray(np.ndarray): contained in this instance. .. automethod:: from_list - """ # Follows https://numpy.org/devdocs/user/basics.subclassing.html -- GitLab From 83acfcfdc423d50912d2488f801fbff0351192f5 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 26 Jun 2020 16:05:39 -0500 Subject: [PATCH 3/7] Fix indentation --- meshmode/dof_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meshmode/dof_array.py b/meshmode/dof_array.py index 63c108c1..b26732c3 100644 --- a/meshmode/dof_array.py +++ b/meshmode/dof_array.py @@ -109,7 +109,7 @@ class DOFArray(np.ndarray): (one per :class:`~meshmode.discretization.ElementGroupBase`). :arg actx: If *None*, the arrays in *res_list* must be - :meth:`~meshmode.array_context.ArrayContext.thaw`\ ed. + :meth:`~meshmode.array_context.ArrayContext.thaw`\ ed. """ if not (actx is None or isinstance(actx, ArrayContext)): raise TypeError("actx must be of type ArrayContext") -- GitLab From 542c99fa691736f493de05dbfe47baf59e4bdef7 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 26 Jun 2020 16:09:56 -0500 Subject: [PATCH 4/7] Fix a loopy kerel error --- meshmode/dof_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meshmode/dof_array.py b/meshmode/dof_array.py index b26732c3..c77fdd0f 100644 --- a/meshmode/dof_array.py +++ b/meshmode/dof_array.py @@ -195,7 +195,7 @@ def flatten(ary: np.ndarray) -> np.ndarray: def prg(): return make_loopy_program( "{[iel,idof]: 0<=iel Date: Sat, 27 Jun 2020 06:47:08 +0200 Subject: [PATCH 5/7] Use a line continuation --- meshmode/dof_array.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/meshmode/dof_array.py b/meshmode/dof_array.py index c77fdd0f..12ffe33f 100644 --- a/meshmode/dof_array.py +++ b/meshmode/dof_array.py @@ -195,7 +195,8 @@ def flatten(ary: np.ndarray) -> np.ndarray: def prg(): return make_loopy_program( "{[iel,idof]: 0<=iel Date: Sat, 27 Jun 2020 20:19:00 +0200 Subject: [PATCH 6/7] ndofs_per_element_per_group => ndofs_per_element_by_group --- meshmode/dof_array.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/meshmode/dof_array.py b/meshmode/dof_array.py index 12ffe33f..824583c8 100644 --- a/meshmode/dof_array.py +++ b/meshmode/dof_array.py @@ -208,7 +208,7 @@ def flatten(ary: np.ndarray) -> np.ndarray: def unflatten(actx: ArrayContext, discr: "_Discretization", ary, - ndofs_per_element_per_group: Optional[Iterable[int]] = None) -> np.ndarray: + ndofs_per_element_by_group: Optional[Iterable[int]] = None) -> np.ndarray: r"""Convert a 'flat' array returned by :func:`flatten` back to a :class:`DOFArray`. Vectorizes over object arrays of :class:`DOFArray`\ s. @@ -218,7 +218,7 @@ def unflatten(actx: ArrayContext, discr: "_Discretization", ary, and not isinstance(ary, DOFArray)): return obj_array_vectorize( lambda subary: unflatten( - actx, discr, subary, ndofs_per_element_per_group), + actx, discr, subary, ndofs_per_element_by_group), ary) @memoize_in(actx, (unflatten, "unflatten_prg")) @@ -228,14 +228,14 @@ def unflatten(actx: ArrayContext, discr: "_Discretization", ary, "result[iel, idof] = ary[grp_start + iel*ndofs_per_element + idof]", name="unflatten") - if ndofs_per_element_per_group is None: - ndofs_per_element_per_group = [ + if ndofs_per_element_by_group is None: + ndofs_per_element_by_group = [ grp.nunit_dofs for grp in discr.groups] group_sizes = [ grp.nelements * ndofs_per_element for grp, ndofs_per_element - in zip(discr.groups, ndofs_per_element_per_group)] + in zip(discr.groups, ndofs_per_element_by_group)] if ary.size != sum(group_sizes): raise ValueError("array has size %d, expected %d" @@ -254,7 +254,7 @@ def unflatten(actx: ArrayContext, discr: "_Discretization", ary, for grp_start, grp, ndofs_per_element in zip( group_starts, discr.groups, - ndofs_per_element_per_group)]) + ndofs_per_element_by_group)]) def flat_norm(ary: DOFArray, ord=2): -- GitLab From 372927fb0bf691486c2720239ecdf799f8606291 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Sat, 27 Jun 2020 13:21:26 -0500 Subject: [PATCH 7/7] Revert "ndofs_per_element_per_group => ndofs_per_element_by_group" This reverts commit f1825d0b0bf04e5921f2d710314cccc47fb8833a. --- meshmode/dof_array.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/meshmode/dof_array.py b/meshmode/dof_array.py index 824583c8..12ffe33f 100644 --- a/meshmode/dof_array.py +++ b/meshmode/dof_array.py @@ -208,7 +208,7 @@ def flatten(ary: np.ndarray) -> np.ndarray: def unflatten(actx: ArrayContext, discr: "_Discretization", ary, - ndofs_per_element_by_group: Optional[Iterable[int]] = None) -> np.ndarray: + ndofs_per_element_per_group: Optional[Iterable[int]] = None) -> np.ndarray: r"""Convert a 'flat' array returned by :func:`flatten` back to a :class:`DOFArray`. Vectorizes over object arrays of :class:`DOFArray`\ s. @@ -218,7 +218,7 @@ def unflatten(actx: ArrayContext, discr: "_Discretization", ary, and not isinstance(ary, DOFArray)): return obj_array_vectorize( lambda subary: unflatten( - actx, discr, subary, ndofs_per_element_by_group), + actx, discr, subary, ndofs_per_element_per_group), ary) @memoize_in(actx, (unflatten, "unflatten_prg")) @@ -228,14 +228,14 @@ def unflatten(actx: ArrayContext, discr: "_Discretization", ary, "result[iel, idof] = ary[grp_start + iel*ndofs_per_element + idof]", name="unflatten") - if ndofs_per_element_by_group is None: - ndofs_per_element_by_group = [ + if ndofs_per_element_per_group is None: + ndofs_per_element_per_group = [ grp.nunit_dofs for grp in discr.groups] group_sizes = [ grp.nelements * ndofs_per_element for grp, ndofs_per_element - in zip(discr.groups, ndofs_per_element_by_group)] + in zip(discr.groups, ndofs_per_element_per_group)] if ary.size != sum(group_sizes): raise ValueError("array has size %d, expected %d" @@ -254,7 +254,7 @@ def unflatten(actx: ArrayContext, discr: "_Discretization", ary, for grp_start, grp, ndofs_per_element in zip( group_starts, discr.groups, - ndofs_per_element_by_group)]) + ndofs_per_element_per_group)]) def flat_norm(ary: DOFArray, ord=2): -- GitLab