1 subroutine da_vtovv_spectral_adj(max_wavenumber, sizec, lenr, lenwrk, lensav, inc, &
2 alp_size, alp, wsave, power, rcv, field)
4 !----------------------------------------------------------------------
5 ! Purpose: Performs Adjoint of spectral to grid transformation on a sphere.
6 !----------------------------------------------------------------------
10 integer, intent(in) :: max_wavenumber ! Max total wavenumber.
11 integer, intent(in) :: sizec ! Size of packed spectral array.
12 integer, intent(in) :: lenr ! FFT info.
13 integer, intent(in) :: lenwrk ! FFT info.
14 integer, intent(in) :: lensav ! FFT info.
15 integer, intent(in) :: inc ! FFT info.
16 integer, intent(in) :: alp_size ! Size of alp array.
17 real, intent(in) :: alp(1:alp_size) ! Associated Legendre Polynomials.
18 real, intent(in) :: wsave(1:lensav) ! Primes for FFT.
19 real*8, intent(in) :: power(0:max_wavenumber) ! Power spectrum
20 real, intent(out) :: rcv(1:2*sizec) ! Spectral modes.
21 real, intent(in) :: field(its:ite,jts:jte) ! Gridpoint field.
23 integer :: j, m, n ! Loop counters.
24 integer :: index_start ! Position markers in cv.
26 integer :: index_end ! Position markers in cv.
28 integer :: index_r, index_c ! Array index for complex v_fou.
30 real :: r_fou(1:lenr) ! FFT array.
31 complex :: v_fou(its:ite,0:max_wavenumber)! Intermediate Fourier state.
32 complex :: ccv(1:sizec) ! Spectral modes.
33 complex :: ccv_local(1:sizec) ! Spectral modes.
35 integer :: l, js, je ! Loop counters.
36 integer :: index_m, index_j ! Markers.
37 complex :: sum_legtra ! Summation scalars.
39 integer :: jc, iequator, temp
42 real :: work(1:lenwrk) ! FFT work array.
45 if (trace_use) call da_trace_entry("da_vtovv_spectral_adj")
47 !----------------------------------------------------------------------
48 ! [1] Perform Adjoint of inverse Fourier decomposition in E-W direction:
49 !----------------------------------------------------------------------
53 r_fou(its:ite) = field(its:ite,j)
55 call rfft1f(ide, inc, r_fou, lenr, wsave, lensav, work, lenwrk, ierr)
57 call da_error(__FILE__,__LINE__,(/"Must compile with FFTPACK"/))
60 !----------------------------------------------------------------------
61 ! Adjust the output for adjoint test
62 !----------------------------------------------------------------------
63 r_fou = real(ite)/2.0 * r_fou
64 r_fou(its) = r_fou(its) * 2.0
66 ! if(.not. odd_longitudes) r_fou(ite) = 2.0*r_fou(ite)
67 ! make r_fou(ide) zero as there is no power computed for this wavenumber
70 v_fou(j,0) = CMPLX(r_fou(its), r_fou(ite))
72 do m = 1, max_wavenumber
75 v_fou(j,m) = v_fou(j,m) + cmplx(r_fou(index_r),r_fou(index_c))
79 !----------------------------------------------------------------------
80 ! [2] Perform adjoint of inverse Legendre decomposition in N-S direction:
81 !----------------------------------------------------------------------
85 do m = 0, max_wavenumber
86 index_start = m * (max_wavenumber + 1 - m) + m * (m + 1) / 2 +1
88 index_m = m * (max_wavenumber + 1 - m) + m * (m + 1) / 2 + 1 - m
92 iequator = mod(jde-jds+1, 2)
94 js = max(jts, jc+iequator+1)
95 je = min(jc+iequator, jte)
97 temp = (max_wavenumber + 1) * (max_wavenumber + 2) / 2
99 do l = m, max_wavenumber
100 sum_legtra = da_zero_complex
102 if (mod(l+m,2) == 1) then
104 index_j = (jds+jde - j - 1) * temp
105 sum_legtra = sum_legtra - v_fou(j,m) * alp(index_j + index_m + l)
109 index_j = (jds+jde - j - 1) * temp
110 sum_legtra = sum_legtra + v_fou(j,m) * alp(index_j + index_m + l)
115 index_j = (j - 1) * temp
116 sum_legtra = sum_legtra + v_fou(j,m) * alp(index_j + index_m + l)
119 ccv_local(index_start+l-m) = sum_legtra
125 index_end = max_wavenumber + &
126 max_wavenumber * (max_wavenumber + 1) / 2 + 1
128 n = index_end - index_start + 1
129 call mpi_allreduce(ccv_local(index_start:index_end), &
130 ccv(index_start:index_end), n, true_mpi_complex, mpi_sum, &
133 ccv(:) = ccv_local(:)
136 !----------------------------------------------------------------------
137 ! [2] Apply Power spectrum
138 !-------------------------------------------------------------------------
140 if (.not. test_transforms) call da_apply_power(power, max_wavenumber, ccv, sizec)
143 rcv(2*n - 1) = real (ccv(n))
144 rcv(2*n ) = aimag(ccv(n))
147 if (trace_use) call da_trace_exit("da_vtovv_spectral_adj")
149 end subroutine da_vtovv_spectral_adj