-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdt_utils.py
More file actions
338 lines (271 loc) · 11.9 KB
/
dt_utils.py
File metadata and controls
338 lines (271 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""Helper functions for estimating stable time steps for RKDG methods.
Characteristic lengthscales
---------------------------
.. autofunction:: characteristic_lengthscales
Non-geometric quantities
------------------------
.. autofunction:: dt_non_geometric_factors
Mesh size utilities
-------------------
.. autofunction:: dt_geometric_factors
.. autofunction:: h_max_from_volume
.. autofunction:: h_min_from_volume
"""
__copyright__ = """
Copyright (C) 2021 University of Illinois Board of Trustees
"""
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from collections.abc import Sequence
import numpy as np
from arraycontext import ArrayContext, Scalar, tag_axes
from arraycontext.metadata import NameHint
from meshmode.dof_array import DOFArray
from meshmode.transform_metadata import (
DiscretizationDOFAxisTag,
DiscretizationElementAxisTag,
DiscretizationFaceAxisTag,
FirstAxisIsElementsTag,
)
from pytools import memoize_in, memoize_on_first_arg
import grudge.op as op
from grudge.discretization import DiscretizationCollection
from grudge.dof_desc import (
DD_VOLUME_ALL,
FACE_RESTR_ALL,
BoundaryDomainTag,
DOFDesc,
as_dofdesc,
)
def characteristic_lengthscales(
actx: ArrayContext, dcoll: DiscretizationCollection,
dd: DOFDesc | None = None) -> DOFArray:
r"""Computes the characteristic length scale :math:`h_{\text{loc}}` at
each node. The characteristic length scale is mainly useful for estimating
the stable time step size. E.g. for a hyperbolic system, an estimate of the
stable time step can be estimated as :math:`h_{\text{loc}} / c`, where
:math:`c` is the characteristic wave speed. The estimate is obtained using
the following formula:
.. math::
h_{\text{loc}} = \operatorname{min}\left(\Delta r_i\right) r_D
where :math:`\operatorname{min}\left(\Delta r_i\right)` is the minimum
node distance on the reference cell (see :func:`dt_non_geometric_factors`),
and :math:`r_D` is the inradius of the cell (see :func:`dt_geometric_factors`).
:returns: a :class:`~meshmode.dof_array.DOFArray` containing a characteristic
lengthscale for each element, at each nodal location.
.. note::
While a prediction of stability is only meaningful in relation to a given
time integrator with a known stability region, the lengthscale provided here
is not intended to be specific to any one time integrator, though the
stability region of standard four-stage, fourth-order Runge-Kutta
methods has been used as a guide. Any concrete time integrator will
likely require scaling of the values returned by this routine.
"""
@memoize_in(dcoll, (characteristic_lengthscales, dd,
"compute_characteristic_lengthscales"))
def _compute_characteristic_lengthscales():
return actx.freeze(
actx.tag(NameHint("char_lscales"),
DOFArray(
actx,
data=tuple(
# Scale each group array of geometric factors by the
# corresponding group non-geometric factor
cng * geo_facts
for cng, geo_facts in zip(
dt_non_geometric_factors(dcoll, dd),
actx.thaw(dt_geometric_factors(dcoll, dd)),
strict=True)))))
return actx.thaw(_compute_characteristic_lengthscales())
@memoize_on_first_arg
def dt_non_geometric_factors(
dcoll: DiscretizationCollection, dd: DOFDesc | None = None
) -> Sequence[float]:
r"""Computes the non-geometric scale factors following [Hesthaven_2008]_,
section 6.4, for each element group in the *dd* discretization:
.. math::
c_{ng} = \operatorname{min}\left( \Delta r_i \right),
where :math:`\Delta r_i` denotes the distance between two distinct
nodal points on the reference element.
:arg dd: a :class:`~grudge.dof_desc.DOFDesc`, or a value convertible to one.
Defaults to the base volume discretization if not provided.
:returns: a :class:`list` of :class:`float` values denoting the minimum
node distance on the reference element for each group.
"""
if dd is None:
dd = DD_VOLUME_ALL
discr = dcoll.discr_from_dd(dd)
min_delta_rs = []
for grp in discr.groups:
nodes = np.asarray(list(zip(*grp.unit_nodes, strict=True)))
nnodes = grp.nunit_dofs
# NOTE: order 0 elements have 1 node located at the centroid of
# the reference element and is equidistant from each vertex
if grp.order == 0:
assert nnodes == 1
min_delta_rs.append(
np.linalg.norm(
nodes[0] - grp.mesh_el_group.vertex_unit_coordinates()[0]
)
)
else:
min_delta_rs.append(
min(
np.linalg.norm(nodes[i] - nodes[j])
for i in range(nnodes) for j in range(nnodes) if i != j
)
)
return min_delta_rs
# {{{ Mesh size functions
@memoize_on_first_arg
def h_max_from_volume(
dcoll: DiscretizationCollection,
dim: int | None = None,
dd: DOFDesc | None = None) -> Scalar:
"""Returns a (maximum) characteristic length based on the volume of the
elements. This length may not be representative if the elements have very
high aspect ratios.
:arg dim: an integer denoting topological dimension. If *None*, the
spatial dimension specified by
:attr:`grudge.DiscretizationCollection.dim` is used.
:arg dd: a :class:`~grudge.dof_desc.DOFDesc`, or a value convertible to one.
Defaults to the base volume discretization if not provided.
:returns: a scalar denoting the maximum characteristic length.
"""
from grudge.reductions import elementwise_sum, nodal_max
if dd is None:
dd = DD_VOLUME_ALL
dd = as_dofdesc(dd)
if dim is None:
dim = dcoll.dim
ones = dcoll.discr_from_dd(dd).zeros(dcoll._setup_actx) + 1.0
return nodal_max(
dcoll,
dd,
elementwise_sum(dcoll, op.mass(dcoll, dd, ones))
) ** (1.0 / dim)
@memoize_on_first_arg
def h_min_from_volume(
dcoll: DiscretizationCollection,
dim: int | None = None,
dd: DOFDesc | None = None) -> Scalar:
"""Returns a (minimum) characteristic length based on the volume of the
elements. This length may not be representative if the elements have very
high aspect ratios.
:arg dim: an integer denoting topological dimension. If *None*, the
spatial dimension specified by
:attr:`grudge.DiscretizationCollection.dim` is used.
:arg dd: a :class:`~grudge.dof_desc.DOFDesc`, or a value convertible to one.
Defaults to the base volume discretization if not provided.
:returns: a scalar denoting the minimum characteristic length.
"""
from grudge.reductions import elementwise_sum, nodal_min
if dd is None:
dd = DD_VOLUME_ALL
dd = as_dofdesc(dd)
if dim is None:
dim = dcoll.dim
ones = dcoll.discr_from_dd(dd).zeros(dcoll._setup_actx) + 1.0
return nodal_min(
dcoll,
dd,
elementwise_sum(dcoll, op.mass(dcoll, dd, ones))
) ** (1.0 / dim)
def dt_geometric_factors(
dcoll: DiscretizationCollection, dd: DOFDesc | None = None) -> DOFArray:
r"""Computes a geometric scaling factor for each cell following
[Hesthaven_2008]_, section 6.4, defined as the inradius (radius of an
inscribed circle/sphere).
Specifically, the inradius for each element is computed using the following
formula from [Shewchuk_2002]_, Table 1, for simplicial cells
(triangles/tetrahedra):
.. math::
r_D = \frac{d V}{\sum_{i=1}^{N_{faces}} F_i},
where :math:`d` is the topological dimension, :math:`V` is the cell volume,
and :math:`F_i` are the areas of each face of the cell.
:arg dd: a :class:`~grudge.dof_desc.DOFDesc`, or a value convertible to one.
Defaults to the base volume discretization if not provided.
:returns: a frozen :class:`~meshmode.dof_array.DOFArray` containing the
geometric factors for each cell and at each nodal location.
"""
from meshmode.discretization.poly_element import SimplexElementGroupBase
if dd is None:
dd = DD_VOLUME_ALL
actx = dcoll._setup_actx
volm_discr = dcoll.discr_from_dd(dd)
if any(not isinstance(grp, SimplexElementGroupBase)
for grp in volm_discr.groups):
raise NotImplementedError(
"Geometric factors are only implemented for simplex element groups"
)
if volm_discr.dim != volm_discr.ambient_dim:
from warnings import warn
warn("The geometric factor for the characteristic length scale in "
"time step estimation is not necessarily valid for non-volume-"
"filling discretizations. Continuing anyway.", stacklevel=3)
cell_vols = abs(
op.elementwise_integral(
dcoll, dd, volm_discr.zeros(actx) + 1.0
)
)
if dcoll.dim == 1:
# Inscribed "circle" radius is half the cell size
return actx.freeze(cell_vols/2)
dd_face = dd.with_domain_tag(
BoundaryDomainTag(FACE_RESTR_ALL, dd.domain_tag.tag))
face_discr = dcoll.discr_from_dd(dd_face)
# Compute areas of each face
face_areas = abs(
op.elementwise_integral(
dcoll, dd_face, face_discr.zeros(actx) + 1.0
)
)
if not actx.supports_nonscalar_broadcasting:
raise ValueError("Array context does not allow advanced indexing. "
"This is no longer supported.")
# Compute total surface area of an element by summing over the
# individual face areas
surface_areas = DOFArray(
actx,
data=tuple(
actx.einsum(
"fej->e",
tag_axes(actx, {
0: DiscretizationFaceAxisTag(),
1: DiscretizationElementAxisTag(),
2: DiscretizationDOFAxisTag()
},
face_ae_i.reshape(
vgrp.mesh_el_group.nfaces,
vgrp.nelements,
face_ae_i.shape[-1])),
tagged=(FirstAxisIsElementsTag(),))
for vgrp, face_ae_i in zip(volm_discr.groups, face_areas, strict=True)))
return actx.freeze(
actx.tag(NameHint(f"dt_geometric_{dd.as_identifier()}"),
DOFArray(actx,
data=tuple(
actx.einsum(
"e,ei->ei",
1/sae_i,
actx.tag_axis(1, DiscretizationDOFAxisTag(), cv_i),
tagged=(FirstAxisIsElementsTag(),)) * dcoll.dim
for cv_i, sae_i in zip(cell_vols, surface_areas,
strict=True)))))
# }}}
# vim: foldmethod=marker