Skip to content

Fix UnboundLocalError in compute_tke_spectrum when length is given - #2213

Open
udsy19 wants to merge 1 commit into
NVIDIA:mainfrom
udsy19:fix/tke-spectrum-length
Open

udsy19 wants to merge 1 commit into
NVIDIA:mainfrom
udsy19:fix/tke-spectrum-length

Conversation

@udsy19

@udsy19 udsy19 commented Oct 31, 2026

Copy link
Copy Markdown

Bug

compute_tke_spectrum(field, length=None) (physicsnemo/metrics/cae/cfd.py) only sets the domain lengths when length is omitted:

if length is None:
    lx, ly, lz = 2 * np.pi, 2 * np.pi, 2 * np.pi

so passing the documented length argument crashes a few lines later at kx = np.fft.fftfreq(nx, lx / nx):

compute_tke_spectrum(np.random.rand(3, 8, 8, 8), length=1.0)
# UnboundLocalError: cannot access local variable 'lx' where it is not associated with a value

The existing test only calls it with the default, so this went unnoticed.

Fix

Default length to 2 * np.pi and always set lx = ly = lz = length. Output without length is unchanged.

Tests

test_compute_tke_spectrum_length in test/metrics/test_metrics_cfd.py checks that length=2π reproduces the default output exactly and that wavenumbers scale with 1 / length (length=1.0 → 2π × the default). It fails on main with the UnboundLocalError and passes with the fix; test/metrics/test_metrics_cfd.py passes locally (CPU, macOS).

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.
  • The CHANGELOG.md is up to date with these changes.
  • An issue is linked to this pull request. (No existing issue; small self-contained fix.)

compute_tke_spectrum only assigned the domain lengths lx, ly, lz when
length was None, so passing an explicit length raised UnboundLocalError
at the first fftfreq call. Use the given length (defaulting to 2*pi) for
all three axes.

Signed-off-by: Udaya Tejas <udayatejas2004@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions

Copy link
Copy Markdown
Contributor

CODEOWNERS review map

Current for commit 3f16e360a5d6. An approval covers every file listed for that owner; one owner is sufficient for shared files.

⏳ @mnabian — 1 file(s)
  • physicsnemo/metrics/cae/cfd.py
⏳ @peterdsharpe — 1 file(s)
  • physicsnemo/metrics/cae/cfd.py

No CODEOWNER

  • CHANGELOG.md
  • test/metrics/test_metrics_cfd.py

Comment /codeowners-info to refresh.

@greptile-apps

greptile-apps Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in Greptile

[Low impact] The change is limited to the optional domain-length path of one CFD metric.

The binned wavenumber axis should respect the supplied domain length before merging.

Findings

  1. P1 Binned axis ignores domain size ▶

Summary

Fixes compute_tke_spectrum crashing when callers supply length.

  • The spectrum uses the domain length passed by the caller.

Reviews (1) · Last reviewed commit: "Fix UnboundLocalError in compute_tke_spe..." · Reviewed by Greptile

if length is None:
lx, ly, lz = 2 * np.pi, 2 * np.pi, 2 * np.pi
length = 2 * np.pi
lx, ly, lz = length, length, length

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Binned axis ignores domain size

Passing a different length now changes mk, but the returned wave_numbers still uses the fixed spacing 2 * np.pi. Doubling length halves the raw wavenumbers but leaves the binned spectrum’s axis unchanged. Users plotting wave_numbers against tke_spectrum therefore get an axis that ignores their domain length.

Make the binned axis scale with length, and check it in the new test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant