Skip to content

Commit 023d7a5

Browse files
authored
Merge pull request #51 from fortran-lang/complex-fft-example
Add complex-fft example; fix comment & rename real-fft example
2 parents b4ed3dd + d646b90 commit 023d7a5

File tree

4 files changed

+78
-15
lines changed

4 files changed

+78
-15
lines changed

.github/workflows/fpm.yml

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ jobs:
88
strategy:
99
fail-fast: false
1010
matrix:
11-
os: [ubuntu-latest, macos-11]
12-
gcc_v: [10] # Version of GFortran we want to use.
11+
os: [ubuntu-latest, macos-12]
12+
gcc_v: [11] # Version of GFortran we want to use.
1313
include:
1414
- os: ubuntu-latest
1515
os-arch: linux-x86_64
1616

17-
- os: macos-11
17+
- os: macos-12
1818
os-arch: macos-x86_64
1919

2020
env:
@@ -25,24 +25,35 @@ jobs:
2525
- name: Checkout code
2626
uses: actions/checkout@v1
2727

28-
- name: Install GFortran macOS
29-
if: contains(matrix.os, 'macos')
30-
run: |
31-
ln -s /usr/local/bin/gfortran-${GCC_V} /usr/local/bin/gfortran
32-
which gfortran-${GCC_V}
33-
which gfortran
34-
3528
- name: Install GFortran Linux
3629
if: contains(matrix.os, 'ubuntu')
3730
run: |
3831
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-${GCC_V} 100 \
3932
--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-${GCC_V} \
4033
--slave /usr/bin/gcov gcov /usr/bin/gcov-${GCC_V}
41-
34+
35+
# Backport gfortran shared libraries to version 9 folder. This is necessary because the macOS release of fpm
36+
# 0.10.0 used for bootstrapping has these paths hardcoded in the executable.
37+
# See https://github.com/fortran-lang/fpm/pull/1061
38+
- name: MacOS patch libgfortran
39+
if: contains(matrix.os, 'macos')
40+
run: |
41+
ln -s /usr/local/bin/gfortran-${GCC_V} /usr/local/bin/gfortran
42+
which gfortran-${GCC_V}
43+
which gfortran
44+
mkdir /usr/local/opt/gcc@10
45+
mkdir /usr/local/opt/gcc@10/lib
46+
mkdir /usr/local/opt/gcc@10/lib/gcc
47+
mkdir /usr/local/opt/gcc@10/lib/gcc/10
48+
mkdir /usr/local/lib/gcc/10
49+
ln -fs /usr/local/opt/gcc@${GCC_V}/lib/gcc/${GCC_V}/libquadmath.0.dylib /usr/local/opt/gcc@10/lib/gcc/10/libquadmath.0.dylib
50+
ln -fs /usr/local/opt/gcc@${GCC_V}/lib/gcc/${GCC_V}/libgfortran.5.dylib /usr/local/opt/gcc@10/lib/gcc/10/libgfortran.5.dylib
51+
ln -fs /usr/local/lib/gcc/${GCC_V}/libgcc_s.1.dylib /usr/local/lib/gcc/10/libgcc_s.1.dylib
52+
4253
- name: Install fpm
43-
uses: fortran-lang/setup-fpm@v3
54+
uses: fortran-lang/setup-fpm@v5
4455
with:
45-
fpm-version: 'v0.8.2'
56+
fpm-version: 'v0.9.0'
4657

4758
- name: Build fftpack
4859
run: |

example/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ target_link_libraries(bench3 fftpack)
99

1010
add_executable(rfft_example)
1111
target_link_libraries(rfft_example fftpack)
12+
13+
add_executable(complex_transforms)
14+
target_link_libraries(complex_transforms fftpack)
15+
16+
add_executable(real_transforms)
17+
target_link_libraries(real_transforms fftpack)

example/complex_transforms.f90

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
program complex_transforms
2+
!! This program invokes fftpack's fft function to compute the forward transform of a complex function
3+
!! and the inverse transform of the result. An assertion verifies the expected result of the forward
4+
!! transform according to the element layout described at [1]. A second assertion checks that the
5+
!! inverse transform recovers the original function.
6+
!!
7+
!! [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.fft.html#scipy.fftpack.fft
8+
use fftpack, only: fft, ifft
9+
implicit none
10+
integer j, k
11+
integer, parameter :: N = 8
12+
double precision, parameter :: two_pi = 2.D0*acos(-1.D0), tolerance = 1.0D-06, f_avg = 3.D0, zero=0.D0
13+
double precision, parameter :: x(0:N-1) = [(two_pi*dble(j)/dble(N), j=0,N-1)]
14+
integer, parameter :: rk = kind(two_pi)
15+
complex(rk), parameter :: f(0:N-1) = f_avg + cos(x)
16+
!! sample f(x) = 3 + cos(x) uniformly on [0,2*pi)
17+
!! = 3 + (exp(i*x) - exp(-i*x))/2
18+
!! which yields the Fourier coefficients
19+
!! { 3, k = 0
20+
!! f_hat = { 1/2, k = 1
21+
!! { 1/2, k = -1
22+
!! { 0, otherwise
23+
complex(rk), dimension(0:N-1) :: f_round_trip, fft_f
24+
character(len=*), parameter :: real_format = "(a,*(g10.4,:,1x))" !! space-separated values
25+
character(len=*), parameter :: complex_format= "(a,*('(',g10.4,',',g10.4,')',:,1x)))" !! space-separated complex values
26+
27+
call assert(mod(N,2)==0, "the algorithm below requires even N")
28+
29+
fft_f(:) = fft(f)/dble(N)
30+
f_round_trip(:) = ifft(fft_f)
31+
32+
print complex_format, "f = ", f
33+
print complex_format, "fft_f = ", fft_f
34+
print complex_format, "f_round_trip = ",f_round_trip
35+
36+
!call assert(all(abs(f_round_trip - f) < tolerance), "inverse of forward FFT must yield the original function")
37+
38+
contains
39+
40+
pure subroutine assert(assertion, description)
41+
logical, intent(in) :: assertion
42+
character(len=*), intent(in) :: description
43+
if (.not. assertion) error stop description
44+
end subroutine
45+
46+
end program

example/real-forward-transform.f90 renamed to example/real_transforms.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
program forward_transform_of_real_function
1+
program real_transforms
22
!! This program invokes fftpack's rrft function to compute the forward transform of a real function
33
!! and constructs the resulting complex Fourier coefficients by (re)organizing and normalizing the
44
!! rfft result according to array element layout described at [1]. The program also demonstrates
5-
!! the inverse transform of the raw rrft result to recover the original function.
5+
!! the inverse transform of the normalized rrft result to recover the original function.
66
!!
77
!! [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.rfft.html#scipy.fftpack.rfft
88
use fftpack, only: rfft, irfft

0 commit comments

Comments
 (0)