Fix ustarn calculation: initialization and FFT shear formula bugs (#265) #272
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Initial plan
Fix ustars0 and ustarn0 initialization bug in wind.py
Fixed bug where ustars0 and ustarn0 were incorrectly set to ustar magnitude instead of their respective directional components ustars and ustarn.
input files for debugging
Fix missing division in dtauy FFT shear calculation
The dtauy_t formula in the FFT shear method was missing the division by sc_kv(0., 2.*sqrt(2.)*sigma), causing incorrect y-direction shear stress perturbations. This resulted in non-zero ustarn values even when the bed had no y-direction variability.
The formula now matches the structure of dtaux_t calculation.
The kx and ky frequency arrays were misaligned with the FFT output. The code was creating frequency arrays with fftfreq(n+1)[1:] which skips the DC component, but the FFT includes all frequencies. This caused incorrect mapping between frequencies and FFT coefficients, leading to wrong shear perturbations.
Fixed by using fftfreq(n) which properly matches the FFT output dimensions and includes the DC component at the correct index.
The previous fix included the DC component (kx=0, ky=0) in the frequency arrays, which caused division by zero errors in the shear calculations.
Fixed by:
The DC component represents the mean value and doesn't contribute to perturbations, so setting it to zero is physically correct.
The previous fix still allowed invalid values when kx=0 (but ky≠0) because sigma depends on kx. When kx=0, sigma=0 which causes issues in Bessel function ratios.
New approach:
This is physically correct: perturbations at kx=0 (pure y-direction frequencies) should be zero for the x-direction flow formulation.
Simplified the masked computation approach to a cleaner implementation:
This achieves the same result with much simpler, more readable code.
reverted the suggested division in dtauy_t calculation
deleted wind tests
shear patch