module m_omp_spectral use m_common, only: dp use m_logger, only: abort_run implicit none ! Threshold below which a spectral coefficient is treated as zero. Must ! scale with the working precision: single-precision roundoff leaves ! ~1e-13 noise in wave numbers that should vanish (e.g. Nyquist modes), ! and dividing by that noise corrupts the pressure field. real(dp), parameter :: eps_wave = epsilon(1._dp) private :: guarded_neg_div contains pure elemental function guarded_neg_div(num, den) result(res) !! Return -num/den, or zero if the magnitude of den is below eps_wave. real(dp), intent(in) :: num, den real(dp) :: res if (abs(den) < eps_wave) then res = 0._dp else res = -num/den end if end function guarded_neg_div subroutine process_spectral_000( & div_u, waves, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, & nx, ny, nz, ax, bx, ay, by, az, bz & ) !! Post-process div U* in spectral space for all periodic BCs. !! !! 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 complex(dp), intent(in), dimension(:, :, :) :: waves 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 real(dp) :: tmp_r, tmp_c, div_r, div_c !$omp parallel do private(div_r, div_c, ix, iy, iz, tmp_r, tmp_c) collapse(3) do k = 1, nz_spec do j = 1, ny_spec do i = 1, nx_spec ! normalisation div_r = real(div_u(i, j, k), kind=dp)/nx/ny/nz div_c = aimag(div_u(i, j, k))/nx/ny/nz ix = i + x_sp_st iy = j + y_sp_st iz = k + z_sp_st ! post-process forward ! post-process in z tmp_r = div_r tmp_c = div_c div_r = tmp_r*bz(iz) + tmp_c*az(iz) div_c = tmp_c*bz(iz) - tmp_r*az(iz) if (iz > nz/2 + 1) div_r = -div_r if (iz > nz/2 + 1) div_c = -div_c ! post-process in y tmp_r = div_r tmp_c = div_c div_r = tmp_r*by(iy) + tmp_c*ay(iy) div_c = tmp_c*by(iy) - tmp_r*ay(iy) if (iy > ny/2 + 1) div_r = -div_r if (iy > ny/2 + 1) div_c = -div_c ! post-process in x tmp_r = div_r tmp_c = div_c div_r = tmp_r*bx(ix) + tmp_c*ax(ix) div_c = tmp_c*bx(ix) - tmp_r*ax(ix) ! Solve Poisson tmp_r = real(waves(i, j, k), kind=dp) tmp_c = aimag(waves(i, j, k)) if ((tmp_r < eps_wave) .or. (tmp_c < eps_wave)) then div_r = 0._dp; div_c = 0._dp else div_r = -div_r/tmp_r div_c = -div_c/tmp_c end if ! post-process backward ! post-process in z tmp_r = div_r tmp_c = div_c div_r = tmp_r*bz(iz) - tmp_c*az(iz) div_c = -tmp_c*bz(iz) - tmp_r*az(iz) if (iz > nz/2 + 1) div_r = -div_r if (iz > nz/2 + 1) div_c = -div_c ! post-process in y tmp_r = div_r tmp_c = div_c div_r = tmp_r*by(iy) + tmp_c*ay(iy) div_c = tmp_c*by(iy) - tmp_r*ay(iy) if (iy > ny/2 + 1) div_r = -div_r if (iy > ny/2 + 1) div_c = -div_c ! post-process in x tmp_r = div_r tmp_c = div_c div_r = tmp_r*bx(ix) + tmp_c*ax(ix) div_c = -tmp_c*bx(ix) + tmp_r*ax(ix) ! update the entry div_u(i, j, k) = cmplx(div_r, div_c, kind=dp) end do end do end do !$omp end parallel do end subroutine process_spectral_000 subroutine process_spectral_010( & div_u, waves, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, & nx, ny, nz, ax, bx, ay, by, az, bz & ) !! Post-process div U* in spectral space, for non-periodic BC in y-dir. !! !! 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 complex(dp), intent(in), dimension(:, :, :) :: waves 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 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, 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 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 !$omp parallel do private(div_r, div_c, ix, iz, tmp_r, tmp_c) collapse(3) do k = 1, nz_spec do j = 1, ny_spec do i = 1, nx_spec ix = i + x_sp_st iz = k + z_sp_st ! normalisation div_r = real(div_u(i, j, k), kind=dp)/nx/ny/nz div_c = aimag(div_u(i, j, k))/nx/ny/nz ! postprocess in z tmp_r = div_r tmp_c = div_c div_r = tmp_r*bz(iz) + tmp_c*az(iz) div_c = tmp_c*bz(iz) - tmp_r*az(iz) if (iz > nz/2 + 1) div_r = -div_r if (iz > nz/2 + 1) div_c = -div_c ! postprocess in x tmp_r = div_r tmp_c = div_c div_r = tmp_r*bx(ix) + tmp_c*ax(ix) div_c = tmp_c*bx(ix) - tmp_r*ax(ix) if (ix > nx/2 + 1) div_r = -div_r if (ix > nx/2 + 1) div_c = -div_c ! update the entry div_u(i, j, k) = cmplx(div_r, div_c, kind=dp) end do end do end do !$omp end parallel do !$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 iy = j + y_sp_st iy_r = ny_spec - j + 2 + y_sp_st l_r = real(div_u(i, j, k), kind=dp) l_c = aimag(div_u(i, j, k)) r_r = real(div_u(i, ny_spec - j + 2, k), kind=dp) r_c = aimag(div_u(i, ny_spec - j + 2, k)) ! update the entry div_u(i, j, k) = 0.5_dp*cmplx( & !& l_r*by(iy) + l_c*ay(iy) + r_r*by(iy) - r_c*ay(iy), & -l_r*ay(iy) + l_c*by(iy) + r_r*ay(iy) + r_c*by(iy), kind=dp & ) 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 & ) end do end do end do !$omp end parallel do end subroutine process_spectral_010_fw subroutine factor_spectral_010_poisson(a_re, a_im, nx_spec, n, nz_spec) !! Factor the pentadiagonal matrices of the stretched 010 case, once at !! init. The elimination overwrites a_re and a_im. Slot 3 and slots 4, 5 !! hold the factored diagonals. Slot 2 of row j+1 holds the multiplier !! that removes diag-1 at step j, and slot 1 of row j+2 the multiplier !! that removes diag-2. Slot 2 of row n holds the multiplier of the last !! row. !! !! Ref. JCP 228 (2009), 5989–6015, Sec 4 implicit none !> Pentadiagonal matrices, overwritten by the elimination real(dp), intent(inout), dimension(:, :, :, :) :: a_re, a_im !> Grid size in spectral space. n is the number of rows of the matrix integer, intent(in) :: nx_spec, n, nz_spec integer :: i, j, k real(dp) :: tmp_r, tmp_c !$omp parallel do private(j, tmp_r, tmp_c) collapse(2) do k = 1, nz_spec do i = 1, nx_spec do j = 1, n - 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 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) ! keep the multiplier, the row j+1 slot 2 is final here a_re(i, j + 1, k, 2) = tmp_r a_im(i, j + 1, k, 2) = tmp_c ! 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 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) ! keep the multiplier, the row j+2 slot 1 is never modified a_re(i, j + 2, k, 1) = tmp_r a_im(i, j + 2, k, 1) = tmp_c end do ! 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 a_re(i, n, k, 3) = a_re(i, n, k, 3) - tmp_r*a_re(i, n - 1, k, 4) a_im(i, n, k, 3) = a_im(i, n, k, 3) - tmp_c*a_im(i, n - 1, k, 4) a_re(i, n, k, 2) = tmp_r a_im(i, n, k, 2) = tmp_c end do end do !$omp end parallel do end subroutine factor_spectral_010_poisson 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, nz & ) !! Solve the Poisson equation with the pentadiagonal matrices of the !! stretched 010 case, along y. !! !! The matrices are factored once at init by factor_spectral_010_poisson, !! so this routine only substitutes into div_u. !! !! Ref. JCP 228 (2009), 5989–6015, Sec 4 implicit none !> Divergence of velocity in spectral space complex(dp), intent(inout), dimension(:, :, :) :: div_u !> Factored pentadiagonal matrices real(dp), intent(in), 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, 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 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 = a_re(i, j + 1, k, 2) tmp_c = a_im(i, j + 1, k, 2) 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) ! eliminate diag-2 tmp_r = a_re(i, j + 2, k, 1) tmp_c = a_im(i, j + 2, k, 1) 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) end do ! handle the last row, its multiplier and pivot are factored tmp_r = a_re(i, n, k, 2) tmp_c = a_im(i, n, k, 2) div_r = a_re(i, n, k, 3) div_c = a_im(i, n, k, 3) ! 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 Nyquist line, tested on the global spectral indices. It ! exists only for even nx and nz. The zero mode is handled in ! fill_stretching_sym. if (i + x_sp_st == nx/2 + 1 .and. k + z_sp_st == nz/2 + 1 & .and. mod(nx, 2) == 0 .and. mod(nz, 2) == 0) 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 tmp_r = 0._dp end if if (abs(a_im(i, j, k, 3)) > eps_wave) then tmp_c = 1._dp/a_im(i, j, k, 3) else tmp_c = 0._dp end if 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 & .and. mod(nx, 2) == 0 .and. mod(nz, 2) == 0) & div_u(i, jm, k) = 0._dp end do end do end do !$omp end parallel do 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, nz, ax, bx, ay, by, az, bz & ) !! Backward third of the spectral post-process for the 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, 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 iy = j + y_sp_st iy_r = ny_spec - j + 2 + y_sp_st l_r = real(div_u(i, j, k), kind=dp) l_c = aimag(div_u(i, j, k)) r_r = real(div_u(i, ny_spec - j + 2, k), kind=dp) r_c = aimag(div_u(i, ny_spec - j + 2, k)) ! update the entry div_u(i, j, k) = cmplx( & !& l_r*by(iy) - l_c*ay(iy) + r_r*ay(iy) + r_c*by(iy), & l_r*ay(iy) + l_c*by(iy) - r_r*by(iy) + r_c*ay(iy), kind=dp & ) div_u(i, ny_spec - j + 2, k) = cmplx( & !& r_r*by(iy_r) - r_c*ay(iy_r) + l_r*ay(iy_r) + l_c*by(iy_r), & r_r*ay(iy_r) + r_c*by(iy_r) - l_r*by(iy_r) + l_c*ay(iy_r), & kind=dp & ) end do end do end do !$omp end parallel do !$omp parallel do private(div_r, div_c, ix, iz, tmp_r, tmp_c) collapse(3) do k = 1, nz_spec do j = 1, ny_spec do i = 1, nx_spec ix = i + x_sp_st iz = k + z_sp_st div_r = real(div_u(i, j, k), kind=dp) div_c = aimag(div_u(i, j, k)) ! post-process in z tmp_r = div_r tmp_c = div_c div_r = tmp_r*bz(iz) - tmp_c*az(iz) div_c = tmp_c*bz(iz) + tmp_r*az(iz) if (iz > nz/2 + 1) div_r = -div_r if (iz > nz/2 + 1) div_c = -div_c ! post-process in x tmp_r = div_r tmp_c = div_c div_r = tmp_r*bx(ix) - tmp_c*ax(ix) div_c = tmp_c*bx(ix) + tmp_r*ax(ix) if (ix > nx/2 + 1) div_r = -div_r if (ix > nx/2 + 1) div_c = -div_c ! update the entry div_u(i, j, k) = cmplx(div_r, div_c, kind=dp) end do end do end do !$omp end parallel do 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, & nx, ny, nz, ax, bx, ay, by, az, bz & ) !! Post-process div U* in spectral space, for non-periodic BC in x-dir. !! !! This is the process_spectral_010 arrangement with x and y swapped: !! the caller passes the transposed buffer (dim1 = y modes, dim2 = x !! modes, dim3 = z), so this wrapper maps the global sizes nx <-> ny !! and the coefficient pairs ax, bx <-> ay, by onto process_spectral_010's !! non-periodic-y convention before delegating to it. implicit none !> Divergence of velocity in spectral space complex(dp), intent(inout), dimension(:, :, :) :: div_u !> Spectral equivalence constants complex(dp), intent(in), dimension(:, :, :) :: waves 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 call process_spectral_010(div_u=div_u, waves=waves, nx_spec=nx_spec, & ny_spec=ny_spec, nz_spec=nz_spec, & x_sp_st=x_sp_st, y_sp_st=y_sp_st, & z_sp_st=z_sp_st, nx=ny, ny=nx, nz=nz, & ax=ay, bx=by, ay=ax, by=bx, az=az, bz=bz) end subroutine process_spectral_100 subroutine process_spectral_110_fw( & div_u, n1, n2, n3, st1, st2, nx, ny, nz, ax, bx, az, bz & ) !! Forward third of the spectral post-process for the 110 case !! (non-periodic x and y, periodic z, R2C Z-transpose layout): !! normalise, Z periodic post-process forward, then the X paired !! even/odd split. !! !! Layout: div_u(n1, n2, n3) with dim1 = Z r2c modes (az, bz), dim2 = !! X modes (ax, bx), dim3 = Y modes. Dim2 must span the whole global !! x range (nx), which the caller guarantees, since the X pairing !! writes both a mode and its mirror in the same pass. implicit none !> Divergence of velocity in spectral space, (nz/2+1, nx, ny) layout complex(dp), intent(inout), dimension(:, :, :) :: div_u real(dp), intent(in), dimension(:) :: ax, bx, az, bz !> Local extents of div_u integer, intent(in) :: n1, n2, n3 !> Offsets of the local block within the global spectral array integer, intent(in) :: st1, st2 !> Global cell size integer, intent(in) :: nx, ny, nz integer :: i, j, k, iz, ix, ix_pair real(dp) :: tmp_r, tmp_c, div_r, div_c, l_r, l_c, r_r, r_c ! normalise + Z periodic post-process (forward) for the X mode 1, which ! has no mirror. No sign flip: dim1 never exceeds nz/2+1. !$omp parallel do private(iz, div_r, div_c, tmp_r, tmp_c) collapse(2) do k = 1, n3 do i = 1, n1 iz = i + st1 div_r = real(div_u(i, 1, k), kind=dp)/nx/ny/nz div_c = aimag(div_u(i, 1, k))/nx/ny/nz tmp_r = div_r tmp_c = div_c div_r = tmp_r*bz(iz) + tmp_c*az(iz) div_c = tmp_c*bz(iz) - tmp_r*az(iz) div_u(i, 1, k) = cmplx(div_r, div_c, kind=dp) end do end do !$omp end parallel do ! Fused normalise, Z post-process and X paired even/odd split (forward). ! Both members of a pair are read and post-processed in registers before ! either is written. !$omp parallel do private(iz, ix, ix_pair, tmp_r, tmp_c, l_r, l_c, r_r, r_c) collapse(3) do k = 1, n3 do j = 2, n2/2 + 1 do i = 1, n1 iz = i + st1 ix = j + st2 ix_pair = n2 - j + 2 l_r = real(div_u(i, j, k), kind=dp)/nx/ny/nz l_c = aimag(div_u(i, j, k))/nx/ny/nz r_r = real(div_u(i, ix_pair, k), kind=dp)/nx/ny/nz r_c = aimag(div_u(i, ix_pair, k))/nx/ny/nz tmp_r = l_r tmp_c = l_c l_r = tmp_r*bz(iz) + tmp_c*az(iz) l_c = tmp_c*bz(iz) - tmp_r*az(iz) tmp_r = r_r tmp_c = r_c r_r = tmp_r*bz(iz) + tmp_c*az(iz) r_c = tmp_c*bz(iz) - tmp_r*az(iz) div_u(i, j, k) = 0.5_dp*cmplx( & !& l_r*bx(ix) + l_c*ax(ix) + r_r*bx(ix) - r_c*ax(ix), & -l_r*ax(ix) + l_c*bx(ix) + r_r*ax(ix) + r_c*bx(ix), kind=dp & ) div_u(i, ix_pair, k) = 0.5_dp*cmplx( & !& r_r*bx(ix_pair + st2) + r_c*ax(ix_pair + st2) & + l_r*bx(ix_pair + st2) - l_c*ax(ix_pair + st2), & -r_r*ax(ix_pair + st2) + r_c*bx(ix_pair + st2) & + l_r*ax(ix_pair + st2) + l_c*bx(ix_pair + st2), & kind=dp & ) end do end do end do !$omp end parallel do end subroutine process_spectral_110_fw subroutine process_spectral_110_solve( & div_u, waves, n1, n2, n3, st3, ay, by & ) !! Middle third of the spectral post-process for the 110 case: the Y !! paired even/odd split, the Poisson divide, and the Y paired !! even/odd recombine. x and y are cosine transformed, so neither has a !! Nyquist mode and only the mean is singular (handled by eps_wave); the !! x Nyquist zeroing of the 010 case does not apply here (#465). !! !! Layout: div_u(n1, n2, n3) with dim1 = Z r2c modes, dim2 = X modes, !! dim3 = Y modes (ay, by). Dim3 must span the whole global y range !! (ny), which the caller guarantees, since the Y pairing writes both !! a mode and its mirror in the same pass. implicit none !> Divergence of velocity in spectral space, (nz/2+1, nx, ny) layout complex(dp), intent(inout), dimension(:, :, :) :: div_u !> Spectral equivalence constants, (nz/2+1, nx, ny) layout complex(dp), intent(in), dimension(:, :, :) :: waves real(dp), intent(in), dimension(:) :: ay, by !> Local extents of div_u integer, intent(in) :: n1, n2, n3 !> Offset of the local block in dim3 within the global spectral array integer, intent(in) :: st3 integer :: i, j, k, iy, iy_pair real(dp) :: div_r, div_c, tmp_r, tmp_c, l_r, l_c, r_r, r_c real(dp) :: w_r, w_c ! Poisson divide for the Y mode 1, which has no mirror. !$omp parallel do private(div_r, div_c, tmp_r, tmp_c) collapse(2) do j = 1, n2 do i = 1, n1 div_r = real(div_u(i, j, 1), kind=dp) div_c = aimag(div_u(i, j, 1)) tmp_r = real(waves(i, j, 1), kind=dp) tmp_c = aimag(waves(i, j, 1)) div_r = guarded_neg_div(div_r, tmp_r) div_c = guarded_neg_div(div_c, tmp_c) div_u(i, j, 1) = cmplx(div_r, div_c, kind=dp) end do end do !$omp end parallel do ! Fused Y paired even/odd split (forward), Poisson divide and Y paired ! even/odd recombine (backward). Both members of a pair are read before ! either is written, and the split, divide and recombine stay in ! registers. A self-paired mode (iy_pair == k) gives equal split values ! for both members, as in the unfused passes. !$omp parallel do private(iy, iy_pair, l_r, l_c, r_r, r_c, div_r, div_c, tmp_r, tmp_c, w_r, w_c) collapse(3) do k = 2, n3/2 + 1 do j = 1, n2 do i = 1, n1 iy = k + st3 iy_pair = n3 - k + 2 l_r = real(div_u(i, j, k), kind=dp) l_c = aimag(div_u(i, j, k)) r_r = real(div_u(i, j, iy_pair), kind=dp) r_c = aimag(div_u(i, j, iy_pair)) ! split div_r = 0.5_dp*(l_r*by(iy) + l_c*ay(iy) + r_r*by(iy) - r_c*ay(iy)) div_c = 0.5_dp*(-l_r*ay(iy) + l_c*by(iy) + r_r*ay(iy) + r_c*by(iy)) w_r = 0.5_dp*( & r_r*by(iy_pair + st3) + r_c*ay(iy_pair + st3) & + l_r*by(iy_pair + st3) - l_c*ay(iy_pair + st3)) w_c = 0.5_dp*( & -r_r*ay(iy_pair + st3) + r_c*by(iy_pair + st3) & + l_r*ay(iy_pair + st3) + l_c*by(iy_pair + st3)) ! Poisson divide, first member tmp_r = real(waves(i, j, k), kind=dp) tmp_c = aimag(waves(i, j, k)) div_r = guarded_neg_div(div_r, tmp_r) div_c = guarded_neg_div(div_c, tmp_c) ! Poisson divide, second member tmp_r = real(waves(i, j, iy_pair), kind=dp) tmp_c = aimag(waves(i, j, iy_pair)) w_r = guarded_neg_div(w_r, tmp_r) w_c = guarded_neg_div(w_c, tmp_c) ! recombine l_r = div_r l_c = div_c r_r = w_r r_c = w_c div_u(i, j, k) = cmplx( & !& l_r*by(iy) - l_c*ay(iy) + r_r*ay(iy) + r_c*by(iy), & l_r*ay(iy) + l_c*by(iy) - r_r*by(iy) + r_c*ay(iy), kind=dp & ) div_u(i, j, iy_pair) = cmplx( & !& r_r*by(iy_pair + st3) - r_c*ay(iy_pair + st3) & + l_r*ay(iy_pair + st3) + l_c*by(iy_pair + st3), & r_r*ay(iy_pair + st3) + r_c*by(iy_pair + st3) & - l_r*by(iy_pair + st3) + l_c*ay(iy_pair + st3), & kind=dp & ) end do end do end do !$omp end parallel do end subroutine process_spectral_110_solve subroutine process_spectral_110_bw( & div_u, n1, n2, n3, st1, st2, nx, ny, nz, ax, bx, az, bz & ) !! Backward third of the spectral post-process for the 110 case: the !! X paired even/odd recombine, then the Z periodic post-process !! backward. !! !! Layout: div_u(n1, n2, n3) with dim1 = Z r2c modes (az, bz), dim2 = !! X modes (ax, bx), dim3 = Y modes. Dim2 must span the whole global !! x range (nx), which the caller guarantees, since the X pairing !! writes both a mode and its mirror in the same pass. implicit none !> Divergence of velocity in spectral space, (nz/2+1, nx, ny) layout complex(dp), intent(inout), dimension(:, :, :) :: div_u real(dp), intent(in), dimension(:) :: ax, bx, az, bz !> Local extents of div_u integer, intent(in) :: n1, n2, n3 !> Offsets of the local block within the global spectral array integer, intent(in) :: st1, st2 !> Global cell size integer, intent(in) :: nx, ny, nz integer :: i, j, k, iz, ix, ix_pair real(dp) :: tmp_r, tmp_c, div_r, div_c, l_r, l_c, r_r, r_c ! Z periodic post-process (backward) for the X mode 1, which has no ! mirror. No sign flip: dim1 never exceeds nz/2+1. !$omp parallel do private(iz, div_r, div_c, tmp_r, tmp_c) collapse(2) do k = 1, n3 do i = 1, n1 iz = i + st1 div_r = real(div_u(i, 1, k), kind=dp) div_c = aimag(div_u(i, 1, k)) tmp_r = div_r tmp_c = div_c div_r = tmp_r*bz(iz) - tmp_c*az(iz) div_c = tmp_c*bz(iz) + tmp_r*az(iz) div_u(i, 1, k) = cmplx(div_r, div_c, kind=dp) end do end do !$omp end parallel do ! Fused X paired even/odd recombine (backward) and Z post-process ! (backward). Both members of a pair are read before either is written. !$omp parallel do private(iz, ix, ix_pair, tmp_r, tmp_c, div_r, div_c, l_r, l_c, r_r, r_c) collapse(3) do k = 1, n3 do j = 2, n2/2 + 1 do i = 1, n1 iz = i + st1 ix = j + st2 ix_pair = n2 - j + 2 l_r = real(div_u(i, j, k), kind=dp) l_c = aimag(div_u(i, j, k)) r_r = real(div_u(i, ix_pair, k), kind=dp) r_c = aimag(div_u(i, ix_pair, k)) div_r = l_r*bx(ix) - l_c*ax(ix) + r_r*ax(ix) + r_c*bx(ix) div_c = l_r*ax(ix) + l_c*bx(ix) - r_r*bx(ix) + r_c*ax(ix) tmp_r = div_r tmp_c = div_c div_u(i, j, k) = cmplx( & tmp_r*bz(iz) - tmp_c*az(iz), & tmp_c*bz(iz) + tmp_r*az(iz), kind=dp) div_r = r_r*bx(ix_pair + st2) - r_c*ax(ix_pair + st2) & + l_r*ax(ix_pair + st2) + l_c*bx(ix_pair + st2) div_c = r_r*ax(ix_pair + st2) + r_c*bx(ix_pair + st2) & - l_r*bx(ix_pair + st2) + l_c*ax(ix_pair + st2) tmp_r = div_r tmp_c = div_c div_u(i, ix_pair, k) = cmplx( & tmp_r*bz(iz) - tmp_c*az(iz), & tmp_c*bz(iz) + tmp_r*az(iz), kind=dp) end do end do end do !$omp end parallel do end subroutine process_spectral_110_bw subroutine process_spectral_110( & div_u, waves, n1, n2, n3, st1, st2, st3, nx, ny, nz, & ax, bx, ay, by, az, bz & ) !! Post-process div U* in spectral space, for non-periodic BC in both !! x- and y-directions (R2C Z-transpose layout: dim1 = Z r2c modes, !! dim2 = X modes, dim3 = Y modes). implicit none !> Divergence of velocity in spectral space, (nz/2+1, nx, ny) layout complex(dp), intent(inout), dimension(:, :, :) :: div_u !> Spectral equivalence constants, (nz/2+1, nx, ny) layout complex(dp), intent(in), dimension(:, :, :) :: waves real(dp), intent(in), dimension(:) :: ax, bx, ay, by, az, bz !> Local extents of div_u integer, intent(in) :: n1, n2, n3 !> Offsets of the local block within the global spectral array integer, intent(in) :: st1, st2, st3 !> Global cell size integer, intent(in) :: nx, ny, nz ! The paired x/y splits index the mirror mode globally, so dims 2 and 3 ! must hold the full x and y mode ranges (single rank only for now). if (n2 /= nx .or. n3 /= ny .or. st2 /= 0 .or. st3 /= 0) & call abort_run('process_spectral_110: dims 2 and 3 must span full nx, & &ny') call process_spectral_110_fw(div_u=div_u, n1=n1, n2=n2, n3=n3, & st1=st1, st2=st2, nx=nx, ny=ny, nz=nz, & ax=ax, bx=bx, az=az, bz=bz) call process_spectral_110_solve(div_u=div_u, waves=waves, n1=n1, n2=n2, & n3=n3, st3=st3, ay=ay, by=by) call process_spectral_110_bw(div_u=div_u, n1=n1, n2=n2, n3=n3, & st1=st1, st2=st2, nx=nx, ny=ny, nz=nz, & ax=ax, bx=bx, az=az, bz=bz) end subroutine process_spectral_110 end module m_omp_spectral