Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
278 changes: 254 additions & 24 deletions src/backend/omp/kernels/spectral_processing.f90
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,71 @@ subroutine process_spectral_010( &
!> Global cell size
integer, intent(in) :: nx, ny, nz

integer :: i, j, k
real(dp) :: tmp_r, tmp_c, div_r, div_c

call process_spectral_010_fw( &
div_u, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, &
nx, ny, nz, ax, bx, ay, by, az, bz)

! Solve Poisson
!$omp parallel do private(div_r, div_c, tmp_r, tmp_c) collapse(3)
do k = 1, nz_spec
do j = 1, ny_spec
do i = 1, nx_spec
div_r = real(div_u(i, j, k), kind=dp)
div_c = aimag(div_u(i, j, k))

tmp_r = real(waves(i, j, k), kind=dp)
tmp_c = aimag(waves(i, j, k))
if (abs(tmp_r) < eps_wave) then
div_r = 0._dp
else
div_r = -div_r/tmp_r
end if
if (abs(tmp_c) < eps_wave) then
div_c = 0._dp
else
div_c = -div_c/tmp_c
end if

! update the entry
div_u(i, j, k) = cmplx(div_r, div_c, kind=dp)
if (i + x_sp_st == nx/2 + 1 .and. k + z_sp_st == nz/2 + 1) &
div_u(i, j, k) = 0._dp
end do
end do
end do
!$omp end parallel do

! post-process backward
call process_spectral_010_bw( &
div_u, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, &
nx, ny, nz, ax, bx, ay, by, az, bz)

end subroutine process_spectral_010

subroutine process_spectral_010_fw( &
div_u, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, &
nx, ny, nz, ax, bx, ay, by, az, bz &
)
!! Forward third of the spectral post-process for the stretched 010 case:
!! normalise, post-process in z and x, then pair the y modes.
!!
!! Ref. JCP 228 (2009), 5989–6015, Sec 4
implicit none

!> Divergence of velocity in spectral space
complex(dp), intent(inout), dimension(:, :, :) :: div_u
!> Spectral equivalence constants
real(dp), intent(in), dimension(:) :: ax, bx, ay, by, az, bz
!> Grid size in spectral space
integer, intent(in) :: nx_spec, ny_spec, nz_spec
!> Offsets in the permuted pencils in spectral space
integer, intent(in) :: x_sp_st, y_sp_st, z_sp_st
!> Global cell size
integer, intent(in) :: nx, ny, nz

integer :: i, j, k, ix, iy, iz, iy_r
real(dp) :: tmp_r, tmp_c, div_r, div_c, l_r, l_c, r_r, r_c

Expand Down Expand Up @@ -170,7 +235,7 @@ subroutine process_spectral_010( &
end do
!$omp end parallel do

!$omp parallel do private(div_r, div_c, iy, iy_r, l_r, l_c, r_r, r_c) collapse(3)
!$omp parallel do private(iy, iy_r, l_r, l_c, r_r, r_c) collapse(3)
do k = 1, nz_spec
do j = 2, ny_spec/2 + 1
do i = 1, nx_spec
Expand All @@ -190,45 +255,210 @@ subroutine process_spectral_010( &
div_u(i, ny_spec - j + 2, k) = 0.5_dp*cmplx( & !&
r_r*by(iy_r) + r_c*ay(iy_r) + l_r*by(iy_r) - l_c*ay(iy_r), &
-r_r*ay(iy_r) + r_c*by(iy_r) + l_r*ay(iy_r) + l_c*by(iy_r), &
kind=dp &
)
kind=dp &
)
end do
end do
end do
!$omp end parallel do

! Solve Poisson
!$omp parallel do private(div_r, div_c, tmp_r, tmp_c) collapse(3)
end subroutine process_spectral_010_fw

subroutine process_spectral_010_poisson( &
div_u, a_re, a_im, off, inc, nx_spec, n, nz_spec, x_sp_st, z_sp_st, &
nx, ny, nz &
)
!! Solve the Poisson equation with the pentadiagonal matrices of the
!! stretched 010 case, along y.
!!
!! The elimination overwrites a_re and a_im, so the caller passes
!! working copies of the matrices.
!!
!! Ref. JCP 228 (2009), 5989–6015, Sec 4
implicit none

!> Divergence of velocity in spectral space
complex(dp), intent(inout), dimension(:, :, :) :: div_u
!> Pentadiagonal matrices, overwritten by the elimination
real(dp), intent(inout), dimension(:, :, :, :) :: a_re, a_im
!> Offset and increment. The increment is 2 for only the odd or even rows
integer, intent(in) :: off, inc
!> Grid size in spectral space. n is the number of rows of the matrix
integer, intent(in) :: nx_spec, n, nz_spec
!> Offsets in the permuted pencils in spectral space
integer, intent(in) :: x_sp_st, z_sp_st
!> Global cell size
integer, intent(in) :: nx, ny, nz

integer :: i, j, k, jm, nm
real(dp) :: tmp_r, tmp_c, div_r, div_c

!$omp parallel do private(j, jm, nm, tmp_r, tmp_c, div_r, div_c) collapse(2)
do k = 1, nz_spec
do j = 1, ny_spec
do i = 1, nx_spec
div_r = real(div_u(i, j, k), kind=dp)
div_c = aimag(div_u(i, j, k))
do i = 1, nx_spec
! Forward pass for the pentadiagonal matrix
do j = 1, n - 2
! j mapping based on odd/even
! inc=2, off=0 ---> j => 2j - 1
! inc=2, off=1 ---> j => 2j
! inc=1, off=0 ---> j => j
jm = inc*j + off - inc/2
! eliminate diag-1
tmp_r = 0._dp
if (abs(a_re(i, j, k, 3)) > eps_wave) then
tmp_r = a_re(i, j + 1, k, 2)/a_re(i, j, k, 3)
end if
tmp_c = 0._dp
if (abs(a_im(i, j, k, 3)) > eps_wave) then
tmp_c = a_im(i, j + 1, k, 2)/a_im(i, j, k, 3)
end if
div_r = real(div_u(i, jm + inc, k) - tmp_r*div_u(i, jm, k), kind=dp)
div_c = aimag(div_u(i, jm + inc, k) - tmp_c*div_u(i, jm, k))
div_u(i, jm + inc, k) = cmplx(div_r, div_c, kind=dp)
! modify pentadiagonal coefficients in-place
a_re(i, j + 1, k, 3) = a_re(i, j + 1, k, 3) - tmp_r*a_re(i, j, k, 4)
a_im(i, j + 1, k, 3) = a_im(i, j + 1, k, 3) - tmp_c*a_im(i, j, k, 4)
a_re(i, j + 1, k, 4) = a_re(i, j + 1, k, 4) - tmp_r*a_re(i, j, k, 5)
a_im(i, j + 1, k, 4) = a_im(i, j + 1, k, 4) - tmp_c*a_im(i, j, k, 5)

! eliminate diag-2
tmp_r = 0._dp
if (abs(a_re(i, j, k, 3)) > eps_wave) then
tmp_r = a_re(i, j + 2, k, 1)/a_re(i, j, k, 3)
end if
tmp_c = 0._dp
if (abs(a_im(i, j, k, 3)) > eps_wave) then
tmp_c = a_im(i, j + 2, k, 1)/a_im(i, j, k, 3)
end if
div_r = real(div_u(i, jm + 2*inc, k) - tmp_r*div_u(i, jm, k), &
kind=dp)
div_c = aimag(div_u(i, jm + 2*inc, k) - tmp_c*div_u(i, jm, k))
div_u(i, jm + 2*inc, k) = cmplx(div_r, div_c, kind=dp)
! modify pentadiagonal coefficients in-place
a_re(i, j + 2, k, 2) = a_re(i, j + 2, k, 2) - tmp_r*a_re(i, j, k, 4)
a_im(i, j + 2, k, 2) = a_im(i, j + 2, k, 2) - tmp_c*a_im(i, j, k, 4)
a_re(i, j + 2, k, 3) = a_re(i, j + 2, k, 3) - tmp_r*a_re(i, j, k, 5)
a_im(i, j + 2, k, 3) = a_im(i, j + 2, k, 3) - tmp_c*a_im(i, j, k, 5)
end do

tmp_r = real(waves(i, j, k), kind=dp)
tmp_c = aimag(waves(i, j, k))
if (abs(tmp_r) < eps_wave) then
div_r = 0._dp
! handle the last row
if (abs(a_re(i, n - 1, k, 3)) > eps_wave) then
tmp_r = a_re(i, n, k, 2)/a_re(i, n - 1, k, 3)
else
tmp_r = 0._dp
end if
if (abs(a_im(i, n - 1, k, 3)) > eps_wave) then
tmp_c = a_im(i, n, k, 2)/a_im(i, n - 1, k, 3)
else
tmp_c = 0._dp
end if
div_r = a_re(i, n, k, 3) - tmp_r*a_re(i, n - 1, k, 4)
div_c = a_im(i, n, k, 3) - tmp_c*a_im(i, n - 1, k, 4)

! j mapping based on odd/even for last point j=n
nm = inc*n + off - inc/2
if (abs(div_r) > eps_wave) then
tmp_r = tmp_r/div_r
div_r = real(div_u(i, nm, k), kind=dp)/div_r &
- tmp_r*real(div_u(i, nm - inc, k), kind=dp)
else
tmp_r = 0._dp
div_r = 0._dp
end if
if (abs(div_c) > eps_wave) then
tmp_c = tmp_c/div_c
div_c = aimag(div_u(i, nm, k))/div_c &
- tmp_c*aimag(div_u(i, nm - inc, k))
else
tmp_c = 0._dp
div_c = 0._dp
end if
div_u(i, nm, k) = cmplx(div_r, div_c, kind=dp)

if (abs(a_re(i, n - 1, k, 3)) > eps_wave) then
tmp_r = 1._dp/a_re(i, n - 1, k, 3)
else
tmp_r = 0._dp
end if
if (abs(a_im(i, n - 1, k, 3)) > eps_wave) then
tmp_c = 1._dp/a_im(i, n - 1, k, 3)
else
tmp_c = 0._dp
end if
div_r = a_re(i, n - 1, k, 4)*tmp_r
div_c = a_im(i, n - 1, k, 4)*tmp_c
div_u(i, nm - inc, k) = cmplx( & !&
real(div_u(i, nm - inc, k), kind=dp)*tmp_r &
- real(div_u(i, nm, k), kind=dp)*div_r, &
aimag(div_u(i, nm - inc, k))*tmp_c &
- aimag(div_u(i, nm, k))*div_c, &
kind=dp &
)

! zero the mean mode, tested on the global spectral indices
if (i + x_sp_st == nx/2 + 1 .and. k + z_sp_st == nz/2 + 1) then
div_u(i, nm, k) = 0._dp
div_u(i, nm - inc, k) = 0._dp
end if

! backward pass
do j = n - 2, 1, -1
! j mapping based on odd/even
jm = inc*j + off - inc/2
if (abs(a_re(i, j, k, 3)) > eps_wave) then
tmp_r = 1._dp/a_re(i, j, k, 3)
else
div_r = -div_r/tmp_r
tmp_r = 0._dp
end if
if (abs(tmp_c) < eps_wave) then
div_c = 0._dp
if (abs(a_im(i, j, k, 3)) > eps_wave) then
tmp_c = 1._dp/a_im(i, j, k, 3)
else
div_c = -div_c/tmp_c
tmp_c = 0._dp
end if

! update the entry
div_u(i, j, k) = cmplx(div_r, div_c, kind=dp)
div_u(i, jm, k) = cmplx( & !&
tmp_r*(real(div_u(i, jm, k), kind=dp) &
- a_re(i, j, k, 4)*real(div_u(i, jm + inc, k), kind=dp) &
- a_re(i, j, k, 5) &
*real(div_u(i, jm + 2*inc, k), kind=dp)), &
tmp_c*(aimag(div_u(i, jm, k)) &
- a_im(i, j, k, 4)*aimag(div_u(i, jm + inc, k)) &
- a_im(i, j, k, 5)*aimag(div_u(i, jm + 2*inc, k))), &
kind=dp &
)
if (i + x_sp_st == nx/2 + 1 .and. k + z_sp_st == nz/2 + 1) &
div_u(i, j, k) = 0._dp
div_u(i, jm, k) = 0._dp
end do
end do
end do
!$omp end parallel do

! post-process backward
!$omp parallel do private(div_r, div_c, iy, iy_r, l_r, l_c, r_r, r_c) collapse(3)
end subroutine process_spectral_010_poisson

subroutine process_spectral_010_bw( &
div_u, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, &
nx, ny, nz, ax, bx, ay, by, az, bz &
)
!! Backward third of the spectral post-process for the stretched 010 case:
!! unpair the y modes, then post-process in x and z.
!!
!! Ref. JCP 228 (2009), 5989–6015, Sec 4
implicit none

!> Divergence of velocity in spectral space
complex(dp), intent(inout), dimension(:, :, :) :: div_u
!> Spectral equivalence constants
real(dp), intent(in), dimension(:) :: ax, bx, ay, by, az, bz
!> Grid size in spectral space
integer, intent(in) :: nx_spec, ny_spec, nz_spec
!> Offsets in the permuted pencils in spectral space
integer, intent(in) :: x_sp_st, y_sp_st, z_sp_st
!> Global cell size
integer, intent(in) :: nx, ny, nz

integer :: i, j, k, ix, iy, iz, iy_r
real(dp) :: tmp_r, tmp_c, div_r, div_c, l_r, l_c, r_r, r_c

!$omp parallel do private(iy, iy_r, l_r, l_c, r_r, r_c) collapse(3)
do k = 1, nz_spec
do j = 2, ny_spec/2 + 1
do i = 1, nx_spec
Expand Down Expand Up @@ -288,7 +518,7 @@ subroutine process_spectral_010( &
end do
!$omp end parallel do

end subroutine process_spectral_010
end subroutine process_spectral_010_bw

subroutine process_spectral_100( &
div_u, waves, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, &
Expand Down
Loading
Loading