Skip to content

Commit

Permalink
Merge pull request #45 from mredenti/calculus-deriv-dft
Browse files Browse the repository at this point in the history
Fix bug in computation of derivative with DFT
  • Loading branch information
ZedThree authored Apr 26, 2023
2 parents 0554ff6 + fbb0a5c commit 0641c91
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions boututils/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,20 @@ def deriv(*args, **kwargs):

n = var.size
if periodic:
# assume uniform grid
dx = x[1] - x[0]
# Use FFTs to take derivatives
f = rfft(var)
f[0] = 0.0 # Zero constant term
if n % 2 == 0:
# Even n
for i in arange(1, old_div(n, 2)):
f[i] *= 2.0j * pi * float(i) / float(n)
f[i] *= 2.0j * pi * float(i) / (float(n)*dx)
f[-1] = 0.0 # Nothing from Nyquist frequency
else:
# Odd n
for i in arange(1, old_div((n - 1), 2) + 1):
f[i] *= 2.0j * pi * float(i) / float(n)
f[i] *= 2.0j * pi * float(i) / (float(n)*dx)
return irfft(f)
else:
# Non-periodic function
Expand Down

0 comments on commit 0641c91

Please sign in to comment.