泊松方程光谱法
我写了泊松方程。谱法求解器。然而,所得结果与周期性边界条件差分法的结果并不相符。 我认为我使用 FFTW 是错误的。 你能告诉我以下代码的哪一部分有错误吗? 谢谢。
program main
implicit none
include 'fftw3.f'
integer(8) :: plan
integer, parameter :: j_max = 100, k_max = 100, m_max = j_max/2 + 1, n_max = k_max
integer :: j, k, m, n, mm, nn
real(8) :: v(1:j_max, 1:k_max), f(1:j_max, 1:k_max)
real(8) :: x_max, y_max, dx, dy, x, y, t_max, pi
complex(8), parameter :: im = (0.d0, 1.d0)
complex(8) :: vk(1:m_max, 1:n_max), fk(1:m_max, 1:n_max)
pi = 4.d0*atan(1.d0)
x_max = 2.d0*pi
y_max = 2.d0*pi
dx = x_max/j_max
dy = y_max/k_max
!*-- Initial Condition ---
do j = 1, j_max
x = dx*j
do k = 1, k_max
y = dy*k
f(j, k) = dexp(-(x - x_max/2)**2 -(y - y_max/2)**2)
enddo
enddo
!*-- FFT forward ---
call dfftw_plan_dft_r2c_2d(plan, j_max, k_max, v, vk, FFTW_ESTIMATE)
call dfftw_execute(plan)
call dfftw_plan_dft_r2c_2d(plan, j_max, k_max, f, fk, FFTW_ESTIMATE)
call dfftw_execute(plan)
do m = 1, m_max
do n = 1, n_max
if(m <= m_max/2 + 1) then
mm = m - 1
else
mm = m - 1 - m_max
endif
if(n <= n_max/2 + 1) then
nn = n - 1
else
nn = n - 1 - n_max
endif
if(mm == 0 .and. nn == 0) then
else
vk(m, n) = fk(m, n)/(mm**2 + nn**2)
endif
enddo
enddo
!*-- FFT backward ---
call dfftw_plan_dft_c2r_2d(plan, j_max, k_max, vk, v, FFTW_ESTIMATE)
call dfftw_execute(plan)
!*-- normalization ---
v = v/j_max/k_max
call dfftw_destroy_plan(plan)
end program main
I wrote poisson eq. solver with spectral method. However, the obtained result does not coincide with the result of difference method with periodic boundary condition.
I think I am mistaken in the use of FFTW.
Could you tell me which part of the following code contains errors?
Thank you.
program main
implicit none
include 'fftw3.f'
integer(8) :: plan
integer, parameter :: j_max = 100, k_max = 100, m_max = j_max/2 + 1, n_max = k_max
integer :: j, k, m, n, mm, nn
real(8) :: v(1:j_max, 1:k_max), f(1:j_max, 1:k_max)
real(8) :: x_max, y_max, dx, dy, x, y, t_max, pi
complex(8), parameter :: im = (0.d0, 1.d0)
complex(8) :: vk(1:m_max, 1:n_max), fk(1:m_max, 1:n_max)
pi = 4.d0*atan(1.d0)
x_max = 2.d0*pi
y_max = 2.d0*pi
dx = x_max/j_max
dy = y_max/k_max
!*-- Initial Condition ---
do j = 1, j_max
x = dx*j
do k = 1, k_max
y = dy*k
f(j, k) = dexp(-(x - x_max/2)**2 -(y - y_max/2)**2)
enddo
enddo
!*-- FFT forward ---
call dfftw_plan_dft_r2c_2d(plan, j_max, k_max, v, vk, FFTW_ESTIMATE)
call dfftw_execute(plan)
call dfftw_plan_dft_r2c_2d(plan, j_max, k_max, f, fk, FFTW_ESTIMATE)
call dfftw_execute(plan)
do m = 1, m_max
do n = 1, n_max
if(m <= m_max/2 + 1) then
mm = m - 1
else
mm = m - 1 - m_max
endif
if(n <= n_max/2 + 1) then
nn = n - 1
else
nn = n - 1 - n_max
endif
if(mm == 0 .and. nn == 0) then
else
vk(m, n) = fk(m, n)/(mm**2 + nn**2)
endif
enddo
enddo
!*-- FFT backward ---
call dfftw_plan_dft_c2r_2d(plan, j_max, k_max, vk, v, FFTW_ESTIMATE)
call dfftw_execute(plan)
!*-- normalization ---
v = v/j_max/k_max
call dfftw_destroy_plan(plan)
end program main
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里你有一个代码可以满足你的要求,考虑到原始数据在我的例子中是“f1”和“f2”,重要的注释是英语的,其他一些是西班牙语的,如果你有理解上的问题告诉我吧:)
Here you have a code that do what you ask for, take into account that the original data was in 'f1' and 'f2' in my case, the important comments are in english, some other in spanish, if you have problems to understand just tell me :)
在FFT正向过程中,'2d_c2r'函数可以修改输入值,然后,如果您稍后使用then,结果将不正确,您可以在执行该函数之前复制数据。
In the FFT Forward process the '2d_c2r' function can modify the input values, then, if you use then later the results will not be correct, you can do a copy of the data before executing that function.