Skip to content

Commit

Permalink
Merge pull request #3757 from lesserwhirls/tke
Browse files Browse the repository at this point in the history
BUG: Fix turbulence kinetic energy calculation (Fixes #3756)
  • Loading branch information
dopplershift authored Feb 12, 2025
2 parents 489b87c + c3737e3 commit ca236e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/metpy/calc/turbulence.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def tke(u, v, w, perturbation=False, axis=-1):
-----
Turbulence Kinetic Energy is computed as:
.. math:: e = 0.5 \sqrt{\overline{u^{\prime2}} +
\overline{v^{\prime2}} +
\overline{w^{\prime2}}},
.. math:: e = \frac{1}{2} \left(\overline{u^{\prime2}} +
\overline{v^{\prime2}} +
\overline{w^{\prime2}} \right),
where the velocity components
Expand All @@ -107,7 +107,7 @@ def tke(u, v, w, perturbation=False, axis=-1):
v_cont = np.mean(v**2, axis=axis)
w_cont = np.mean(w**2, axis=axis)

return 0.5 * np.sqrt(u_cont + v_cont + w_cont)
return 0.5 * (u_cont + v_cont + w_cont)


@exporter.export
Expand Down
20 changes: 16 additions & 4 deletions tests/calc/test_turbulence.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
@pytest.fixture()
def uvw_and_known_tke():
"""Provide a set of u,v,w with a known tke value."""
u = np.array([-2, -1, 0, 1, 2])
u = np.array([-7, -1, 0, 1, 2])
v = -u
w = 2 * u
# 0.5 * sqrt(2 + 2 + 8)
e_true = np.sqrt(12) / 2.
# average u: -1
# average v: 1
# average w: -2
# average(u'^2): 10
# average(v'^2): 10
# average(w'^2): 40
# 0.5 * (10 + 10 + 40) = 30
e_true = 30
return u, v, w, e_true


Expand Down Expand Up @@ -65,6 +71,13 @@ def test_known_tke(uvw_and_known_tke):
assert_array_equal(e_true, tke(u, v, w))


def test_known_tke_using_perturbation_velocities(uvw_and_known_tke):
"""Test basic behavior of tke with known values."""
u, v, w, e_true = uvw_and_known_tke
assert_array_equal(e_true, tke(u - u.mean(), v - v.mean(), w - w.mean(),
perturbation=True))


def test_known_tke_2d_axis_last(uvw_and_known_tke):
"""Test array with shape (3, 5) [pretend time axis is -1]."""
u, v, w, e_true = uvw_and_known_tke
Expand All @@ -83,7 +96,6 @@ def test_known_tke_2d_axis_first(uvw_and_known_tke):
w = np.array([w, w, w]).transpose()
e_true = e_true * np.ones(3).transpose()
assert_array_equal(e_true, tke(u, v, w, axis=0))
assert_array_equal(e_true, tke(u, v, w, axis=0, perturbation=True))


#
Expand Down

0 comments on commit ca236e5

Please sign in to comment.