Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for integral computations when using overintegration #216

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions grudge/reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@
import grudge.dof_desc as dof_desc


def _quadrature_summand(
dcoll: DiscretizationCollection, dd, vec) -> ArrayOrContainerT:
if not isinstance(vec, DOFArray):
return map_array_container(partial(_quadrature_summand, dcoll, dd), vec)

dd = dof_desc.as_dofdesc(dd)
discr = dcoll.discr_from_dd(dd)
actx = vec.array_context

if dd.uses_quadrature():
from grudge.geometry import area_element
from meshmode.transform_metadata import FirstAxisIsElementsTag

jacobians = area_element(
actx, dcoll, dd=dd,
_use_geoderiv_connection=actx.supports_nonscalar_broadcasting)
return DOFArray(
actx,
data=tuple(
actx.einsum("ei,i,ei->ei",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
actx.einsum("ei,i,ei->ei",
# FIXME: Using einsum only for the benefit of eager eval.
# With proper broadcasting, this should likely just be
# a multiplication.
actx.einsum("ei,i,ei->ei",

vec_i,
actx.from_numpy(grp.quadrature_rule().weights),
jac_i,
arg_names=("vec", "weights", "jac"),
tagged=(FirstAxisIsElementsTag(),))
for grp, vec_i, jac_i in zip(discr.groups, vec, jacobians)
)
)

from grudge.op import _apply_mass_operator

return vec * _apply_mass_operator(dcoll, dd, dd, discr.zeros(actx) + 1.0)


# {{{ Nodal reductions

def norm(dcoll: DiscretizationCollection, vec, p, dd=None) -> "DeviceScalar":
Expand Down Expand Up @@ -242,23 +276,28 @@ def nodal_max_loc(dcoll: DiscretizationCollection, dd, vec) -> "DeviceScalar":
vec, actx.from_numpy(np.array(-np.inf)))


def integral(dcoll: DiscretizationCollection, dd, vec) -> "DeviceScalar":
def integral(dcoll: DiscretizationCollection, *args) -> "DeviceScalar":
"""Numerically integrates a function represented by a
:class:`~meshmode.dof_array.DOFArray` of degrees of freedom.

May be called with ``(vec)`` or ``(dd, vec)``.

:arg dd: a :class:`~grudge.dof_desc.DOFDesc`, or a value convertible to one.
:arg vec: a :class:`~meshmode.dof_array.DOFArray` or an
:class:`~arraycontext.container.ArrayContainer` of them.
:returns: a device scalar denoting the evaluated integral.
"""
from grudge.op import _apply_mass_operator
if len(args) == 1:
vec, = args
dd = dof_desc.DOFDesc("vol", dof_desc.DISCR_TAG_BASE)
elif len(args) == 2:
dd, vec = args
else:
raise TypeError("invalid number of arguments")

dd = dof_desc.as_dofdesc(dd)

ones = dcoll.discr_from_dd(dd).zeros(vec.array_context) + 1.0
return nodal_sum(
dcoll, dd, vec * _apply_mass_operator(dcoll, dd, dd, ones)
)
return nodal_sum(dcoll, dd, _quadrature_summand(dcoll, dd, vec))

# }}}

Expand Down Expand Up @@ -462,12 +501,7 @@ def elementwise_integral(

dd = dof_desc.as_dofdesc(dd)

from grudge.op import _apply_mass_operator

ones = dcoll.discr_from_dd(dd).zeros(vec.array_context) + 1.0
return elementwise_sum(
dcoll, dd, vec * _apply_mass_operator(dcoll, dd, dd, ones)
)
return elementwise_sum(dcoll, dd, _quadrature_summand(dcoll, dd, vec))

# }}}

Expand Down