Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fft notebook #409

Merged
merged 33 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
06e5633
Write fft notebook
samjrholt Aug 3, 2023
cc50d1d
extra explanation
samjrholt Aug 3, 2023
251c566
Spelling
samjrholt Aug 3, 2023
228983c
no fftshift on last dim for real ffts
samjrholt Aug 3, 2023
646e4b3
add test
samjrholt Aug 3, 2023
5b92708
comment
samjrholt Aug 7, 2023
7cac320
Update docs/field-fft.ipynb
samjrholt Aug 7, 2023
44187af
Update docs/field-fft.ipynb
samjrholt Aug 7, 2023
a1cf6c3
Revert "Update docs/field-fft.ipynb"
samjrholt Aug 7, 2023
9f1c19a
Revert "Update docs/field-fft.ipynb"
samjrholt Aug 7, 2023
344ef05
martin edits
samjrholt Aug 7, 2023
8fec5b6
New plots
samjrholt Aug 7, 2023
a891ab6
Angular frequency
samjrholt Aug 7, 2023
765a350
Scipy comment
samjrholt Aug 7, 2023
ed227c9
Remove duplicate tests
samjrholt Aug 7, 2023
1517cd8
Merge pull request #410 from ubermag/fft_bug
samjrholt Aug 7, 2023
99379d3
example
samjrholt Aug 7, 2023
bac194d
text
samjrholt Aug 8, 2023
c2760f0
shift r
samjrholt Aug 8, 2023
085a664
spelling
samjrholt Aug 9, 2023
bfbb689
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
5c5036a
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
2f56038
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
b364d2f
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
cad8777
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
b8f628e
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
327bcee
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
2942684
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
d62c88b
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
8a6af6d
Update docs/field-fft.ipynb
samjrholt Aug 17, 2023
7f51498
Apply suggestions from code review
samjrholt Aug 22, 2023
7386240
review changes
samjrholt Aug 22, 2023
ca18d08
review changes
samjrholt Aug 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions discretisedfield/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -3436,6 +3436,7 @@ def fftn(self, **kwargs):
"""
mesh = self.mesh.fftn()

# Use scipy as faster than numpy
axes = range(self.mesh.region.ndim)
ft = spfft.fftshift(
spfft.fftn(self.array, axes=axes, **kwargs),
Expand Down Expand Up @@ -3520,7 +3521,7 @@ def rfftn(self, **kwargs):
axes = range(self.mesh.region.ndim)
ft = spfft.fftshift(
spfft.rfftn(self.array, axes=axes, **kwargs),
axes=axes,
axes=axes[:-1],
)

return self._fftn(mesh=mesh, array=ft, ifftn=False)
Expand Down Expand Up @@ -3566,7 +3567,7 @@ def irfftn(self, shape=None, **kwargs):

axes = range(self.mesh.region.ndim)
ft = spfft.irfftn(
spfft.ifftshift(self.array, axes=axes),
spfft.ifftshift(self.array, axes=axes[:-1]),
axes=axes,
s=shape,
**kwargs,
Expand Down
1 change: 1 addition & 0 deletions discretisedfield/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,7 @@ def fftn(self, rfft=False):
freqs = spfft.fftfreq(self.n[i], self.cell[i])
# Shift the region boundaries to get the correct coordinates of
# mesh cells.
# This effectively does the same as using fftshift
dfreq = (freqs[1] - freqs[0]) / 2
p1.append(min(freqs) - dfreq)
p2.append(max(freqs) + dfreq)
Expand Down
26 changes: 18 additions & 8 deletions discretisedfield/tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import matplotlib.pyplot as plt
import numpy as np
import pytest
import scipy.fft as spfft
import xarray as xr

import discretisedfield as df
Expand Down Expand Up @@ -2661,17 +2662,11 @@ def _init_random(p):
f = df.Field(mesh, nvdim=3, value=_init_random, norm=1)

# 3d fft
assert f.allclose(f.fftn().ifftn().real)
assert df.Field(mesh, nvdim=3).allclose(f.fftn().ifftn().imag)

assert f.allclose(f.rfftn().irfftn())

# 2d fft
for i in ["x", "y", "z"]:
plane = f.sel(i)
assert plane.allclose(plane.fftn().ifftn().real)
assert df.Field(mesh, nvdim=3).sel(i).allclose(plane.fftn().ifftn().imag)

assert plane.allclose(plane.rfftn().irfftn())

# Fourier slice theoreme
Expand Down Expand Up @@ -2704,11 +2699,26 @@ def _init_random(p):

f = df.Field(mesh, nvdim=1, value=np.random.rand(*mesh.n, 1), norm=1)

assert f.allclose(f.fftn().ifftn().real)
assert df.Field(mesh, nvdim=1).allclose(f.fftn().ifftn().imag)
assert f.allclose(f.rfftn().irfftn(shape=f.mesh.n))

# test 1d rfft
assert f.allclose(f.rfftn().irfftn(shape=f.mesh.n))

# test rfft no shift last dim
a = np.zeros((5, 5))
a[2, 3] = 1

p1 = (0, 0)
p2 = (10, 10)
cell = (2.0, 2.0)
mesh = df.Mesh(p1=p1, p2=p2, cell=cell)
f = df.Field(mesh, nvdim=1, value=a)

field_ft = f.rfftn()
ft = spfft.fftshift(spfft.rfftn(a), axes=[0])

assert np.array_equal(field_ft.array[..., 0], ft)


def test_mpl_scalar(test_field):
# No axes
Expand Down
Loading
Loading