SUBROUTINE calc_spectra !--------------------------------------------------------------------------------! ! This file is part of PALM. ! ! PALM is free software: you can redistribute it and/or modify it under the terms ! of the GNU General Public License as published by the Free Software Foundation, ! either version 3 of the License, or (at your option) any later version. ! ! PALM is distributed in the hope that it will be useful, but WITHOUT ANY ! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ! A PARTICULAR PURPOSE. See the GNU General Public License for more details. ! ! You should have received a copy of the GNU General Public License along with ! PALM. If not, see . ! ! Copyright 1997-2014 Leibniz Universitaet Hannover !--------------------------------------------------------------------------------! ! ! Current revisions: ! ----------------- ! ! ! Former revisions: ! ----------------- ! $Id: calc_spectra.f90 1432 2014-07-15 14:51:17Z kanani $ ! ! 1431 2014-07-15 14:47:17Z suehring ! Wavenumber-integrated spectra coincide with respective variance. ! ! 1342 2014-03-26 17:04:47Z kanani ! REAL constants defined as wp-kinds ! ! 1324 2014-03-21 09:13:16Z suehring ! Bugfix: nzb_x, nzb_yd, nyn_x, nyn_x, nzt_x, nzt_yd belong to transpose_indices ! ! 1320 2014-03-20 08:40:49Z raasch ! ONLY-attribute added to USE-statements, ! kind-parameters added to all INTEGER and REAL declaration statements, ! kinds are defined in new module kinds, ! revision history before 2012 removed, ! comment fields (!:) to be used for variable explanations added to ! all variable declaration statements ! ! 1318 2014-03-17 13:35:16Z raasch ! module interfaces removed ! ! 1216 2013-08-26 09:31:42Z raasch ! resorting of array moved to separate routine resort_for_zx, ! one argument removed from the transpose_..d routines ! ! 1120 2013-04-05 15:11:35Z raasch ! bugfix: calls of fft_x|y replaced by fft_x|y_1d ! ! 1036 2012-10-22 13:43:42Z raasch ! code put under GPL (PALM 3.9) ! ! 1003 2012-09-14 14:35:53Z raasch ! adjustment of array tend for cases with unequal subdomain sizes removed ! ! Revision 1.1 2001/01/05 15:08:07 raasch ! Initial revision ! ! ! Description: ! ------------ ! Calculate horizontal spectra along x and y. ! ATTENTION: 1d-decomposition along y still needs improvement, because in that ! case the gridpoint number along z still depends on the PE number ! because transpose_xz has to be used (and possibly also ! transpose_zyd needs modification). !------------------------------------------------------------------------------! #if defined( __spectra ) USE arrays_3d, & ONLY: d, tend USE control_parameters, & ONLY: average_count_sp, bc_lr_cyc, bc_ns_cyc, message_string, psolver USE cpulog, & ONLY: cpu_log, log_point USE fft_xy, & ONLY: fft_init USE indices, & ONLY: nxl, nxr, nyn, nys, nzb, nzt USE kinds USE pegrid USE spectrum, & ONLY: data_output_sp, spectra_direction IMPLICIT NONE INTEGER(iwp) :: m !: INTEGER(iwp) :: pr !: CALL cpu_log( log_point(30), 'calc_spectra', 'start' ) ! !-- Initialize ffts CALL fft_init ! !-- Reallocate array d in required size IF ( psolver == 'multigrid' ) THEN DEALLOCATE( d ) ALLOCATE( d(nzb+1:nzt,nys:nyn,nxl:nxr) ) ENDIF m = 1 DO WHILE ( data_output_sp(m) /= ' ' .AND. m <= 10 ) ! !-- Transposition from z --> x ( y --> x in case of a 1d-decomposition !-- along x) IF ( INDEX( spectra_direction(m), 'x' ) /= 0 ) THEN ! !-- Calculation of spectra works for cyclic boundary conditions only IF ( .NOT. bc_lr_cyc ) THEN message_string = 'non-cyclic lateral boundaries along x do not'// & '& allow calculation of spectra along x' CALL message( 'calc_spectra', 'PA0160', 1, 2, 0, 6, 0 ) ENDIF CALL preprocess_spectra( m, pr ) #if defined( __parallel ) IF ( pdims(2) /= 1 ) THEN CALL resort_for_zx( d, tend ) CALL transpose_zx( tend, d ) ELSE CALL transpose_yxd( d, d ) ENDIF CALL calc_spectra_x( d, pr, m ) #else message_string = 'sorry, calculation of spectra in non parallel ' // & 'mode& is still not realized' CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 ) #endif ENDIF ! !-- Transposition from z --> y (d is rearranged only in case of a !-- 1d-decomposition along x) IF ( INDEX( spectra_direction(m), 'y' ) /= 0 ) THEN ! !-- Calculation of spectra works for cyclic boundary conditions only IF ( .NOT. bc_ns_cyc ) THEN IF ( myid == 0 ) THEN message_string = 'non-cyclic lateral boundaries along y do' // & ' not & allow calculation of spectra along y' CALL message( 'calc_spectra', 'PA0162', 1, 2, 0, 6, 0 ) ENDIF CALL local_stop ENDIF CALL preprocess_spectra( m, pr ) #if defined( __parallel ) CALL transpose_zyd( d, d ) CALL calc_spectra_y( d, pr, m ) #else message_string = 'sorry, calculation of spectra in non parallel' // & 'mode& is still not realized' CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 ) #endif ENDIF ! !-- Increase counter for next spectrum m = m + 1 ENDDO ! !-- Increase counter for averaging process in routine plot_spectra average_count_sp = average_count_sp + 1 CALL cpu_log( log_point(30), 'calc_spectra', 'stop' ) #endif END SUBROUTINE calc_spectra #if defined( __spectra ) SUBROUTINE preprocess_spectra( m, pr ) USE arrays_3d, & ONLY: d, pt, q, u, v, w USE indices, & ONLY: ngp_2dh, nxl, nxr, nyn, nys, nzb, nzt USE kinds USE pegrid USE spectrum, & ONLY: data_output_sp USE statistics, & ONLY: hom, var_d IMPLICIT NONE INTEGER(iwp) :: i !: INTEGER(iwp) :: j !: INTEGER(iwp) :: k !: INTEGER(iwp) :: m !: INTEGER(iwp) :: pr !: REAL(wp), DIMENSION(nzb:nzt+1) :: var_d_l SELECT CASE ( TRIM( data_output_sp(m) ) ) CASE ( 'u' ) pr = 1 d(nzb+1:nzt,nys:nyn,nxl:nxr) = u(nzb+1:nzt,nys:nyn,nxl:nxr) CASE ( 'v' ) pr = 2 d(nzb+1:nzt,nys:nyn,nxl:nxr) = v(nzb+1:nzt,nys:nyn,nxl:nxr) CASE ( 'w' ) pr = 3 d(nzb+1:nzt,nys:nyn,nxl:nxr) = w(nzb+1:nzt,nys:nyn,nxl:nxr) CASE ( 'pt' ) pr = 4 d(nzb+1:nzt,nys:nyn,nxl:nxr) = pt(nzb+1:nzt,nys:nyn,nxl:nxr) CASE ( 'q' ) pr = 41 d(nzb+1:nzt,nys:nyn,nxl:nxr) = q(nzb+1:nzt,nys:nyn,nxl:nxr) CASE DEFAULT ! !-- The DEFAULT case is reached either if the parameter data_output_sp(m) !-- contains a wrong character string or if the user has coded a special !-- case in the user interface. There, the subroutine user_spectra !-- checks which of these two conditions applies. CALL user_spectra( 'preprocess', m, pr ) END SELECT ! !-- Subtract horizontal mean from the array, for which spectra have to be !-- calculated var_d_l(:) = 0.0_wp DO i = nxl, nxr DO j = nys, nyn DO k = nzb+1, nzt d(k,j,i) = d(k,j,i) - hom(k,1,pr,0) var_d_l(k) = var_d_l(k) + d(k,j,i) * d(k,j,i) ENDDO ENDDO ENDDO ! !-- Compute total variance from local variances var_d(:) = 0.0_wp #if defined( __parallel ) IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr ) CALL MPI_ALLREDUCE( var_d_l(0), var_d(0), nzt+1-nzb, MPI_REAL, & MPI_SUM, comm2d, ierr ) #else var_d(:) = var_d_l(:) #endif var_d(:) = var_d(:) / ngp_2dh(0) END SUBROUTINE preprocess_spectra SUBROUTINE calc_spectra_x( ddd, pr, m ) USE arrays_3d, & ONLY: USE control_parameters, & ONLY: fft_method USE fft_xy, & ONLY: fft_x_1d USE grid_variables, & ONLY: dx USE indices, & ONLY: nx, ny USE kinds USE pegrid USE spectrum, & ONLY: comp_spectra_level, n_sp_x USE statistics, & ONLY: spectrum_x, var_d USE transpose_indices, & ONLY: nyn_x, nys_x, nzb_x, nzt_x IMPLICIT NONE INTEGER(iwp) :: i !: INTEGER(iwp) :: ishape(1) !: INTEGER(iwp) :: j !: INTEGER(iwp) :: k !: INTEGER(iwp) :: m !: INTEGER(iwp) :: n !: INTEGER(iwp) :: pr !: REAL(wp) :: fac !: REAL(wp) :: exponent !: REAL(wp) :: sum_spec_dum !: wavenumber-integrated spectrum REAL(wp), DIMENSION(0:nx) :: work !: REAL(wp), DIMENSION(0:nx/2) :: sums_spectra_l !: REAL(wp), DIMENSION(0:nx/2,100) :: sums_spectra !: REAL(wp), DIMENSION(0:nx,nys_x:nyn_x,nzb_x:nzt_x) :: ddd !: ! !-- Exponent for geometric average exponent = 1.0_wp / ( ny + 1.0_wp ) ! !-- Loop over all levels defined by the user n = 1 DO WHILE ( comp_spectra_level(n) /= 999999 .AND. n <= 100 ) k = comp_spectra_level(n) ! !-- Calculate FFT only if the corresponding level is situated on this PE IF ( k >= nzb_x .AND. k <= nzt_x ) THEN DO j = nys_x, nyn_x work = ddd(0:nx,j,k) CALL fft_x_1d( work, 'forward' ) ddd(0,j,k) = dx * work(0)**2 DO i = 1, nx/2 ddd(i,j,k) = dx * ( work(i)**2 + work(nx+1-i)**2 ) ENDDO ENDDO ! !-- Local sum and geometric average of these spectra !-- (WARNING: no global sum should be performed, because floating !-- point overflow may occur) DO i = 0, nx/2 sums_spectra_l(i) = 1.0_wp DO j = nys_x, nyn_x sums_spectra_l(i) = sums_spectra_l(i) * ddd(i,j,k)**exponent ENDDO ENDDO ELSE sums_spectra_l = 1.0_wp ENDIF ! !-- Global sum of spectra on PE0 (from where they are written on file) sums_spectra(:,n) = 0.0_wp #if defined( __parallel ) CALL MPI_BARRIER( comm2d, ierr ) ! Necessary? CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), nx/2+1, & MPI_REAL, MPI_PROD, 0, comm2d, ierr ) #else sums_spectra(:,n) = sums_spectra_l #endif ! !-- Normalize spectra by variance sum_spec_dum = SUM( sums_spectra(:,n) ) IF ( sum_spec_dum /= 0.0_wp ) THEN sums_spectra(:,n) = sums_spectra(:,n) * var_d(k) / sum_spec_dum ENDIF n = n + 1 ENDDO n = n - 1 IF ( myid == 0 ) THEN ! !-- Sum of spectra for later averaging (see routine data_output_spectra) !-- Temperton fft results need to be normalized IF ( fft_method == 'temperton-algorithm' ) THEN fac = nx + 1.0_wp ELSE fac = 1.0_wp ENDIF DO i = 1, nx/2 DO k = 1, n spectrum_x(i,k,m) = spectrum_x(i,k,m) + sums_spectra(i,k) * fac ENDDO ENDDO ENDIF ! !-- n_sp_x is needed by data_output_spectra_x n_sp_x = n END SUBROUTINE calc_spectra_x SUBROUTINE calc_spectra_y( ddd, pr, m ) USE arrays_3d, & ONLY: USE control_parameters, & ONLY: fft_method USE fft_xy, & ONLY: fft_y_1d USE grid_variables, & ONLY: dy USE indices, & ONLY: nx, ny USE kinds USE pegrid USE spectrum, & ONLY: comp_spectra_level, n_sp_y USE statistics, & ONLY: spectrum_y, var_d USE transpose_indices, & ONLY: nxl_yd, nxr_yd, nzb_yd, nzt_yd IMPLICIT NONE INTEGER(iwp) :: i !: INTEGER(iwp) :: j !: INTEGER(iwp) :: jshape(1) !: INTEGER(iwp) :: k !: INTEGER(iwp) :: m !: INTEGER(iwp) :: n !: INTEGER(iwp) :: pr !: REAL(wp) :: fac !: REAL(wp) :: exponent !: REAL(wp) :: sum_spec_dum !: wavenumber-integrated spectrum REAL(wp), DIMENSION(0:ny) :: work !: REAL(wp), DIMENSION(0:ny/2) :: sums_spectra_l !: REAL(wp), DIMENSION(0:ny/2,100) :: sums_spectra !: REAL(wp), DIMENSION(0:ny,nxl_yd:nxr_yd,nzb_yd:nzt_yd) :: ddd !: ! !-- Exponent for geometric average exponent = 1.0_wp / ( nx + 1.0_wp ) ! !-- Loop over all levels defined by the user n = 1 DO WHILE ( comp_spectra_level(n) /= 999999 .AND. n <= 100 ) k = comp_spectra_level(n) ! !-- Calculate FFT only if the corresponding level is situated on this PE IF ( k >= nzb_yd .AND. k <= nzt_yd ) THEN DO i = nxl_yd, nxr_yd work = ddd(0:ny,i,k) CALL fft_y_1d( work, 'forward' ) ddd(0,i,k) = dy * work(0)**2 DO j = 1, ny/2 ddd(j,i,k) = dy * ( work(j)**2 + work(ny+1-j)**2 ) ENDDO ENDDO ! !-- Local sum and geometric average of these spectra !-- (WARNING: no global sum should be performed, because floating !-- point overflow may occur) DO j = 0, ny/2 sums_spectra_l(j) = 1.0_wp DO i = nxl_yd, nxr_yd sums_spectra_l(j) = sums_spectra_l(j) * ddd(j,i,k)**exponent ENDDO ENDDO ELSE sums_spectra_l = 1.0_wp ENDIF ! !-- Global sum of spectra on PE0 (from where they are written on file) sums_spectra(:,n) = 0.0_wp #if defined( __parallel ) CALL MPI_BARRIER( comm2d, ierr ) ! Necessary? CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), ny/2+1, & MPI_REAL, MPI_PROD, 0, comm2d, ierr ) #else sums_spectra(:,n) = sums_spectra_l #endif ! !-- Normalize spectra by variance sum_spec_dum = SUM( sums_spectra(:,n) ) IF ( SUM(sums_spectra(:,n)) /= 0.0_wp ) THEN sums_spectra(:,n) = sums_spectra(:,n) * var_d(k) / SUM(sums_spectra(:,n)) ENDIF n = n + 1 ENDDO n = n - 1 IF ( myid == 0 ) THEN ! !-- Sum of spectra for later averaging (see routine data_output_spectra) !-- Temperton fft results need to be normalized IF ( fft_method == 'temperton-algorithm' ) THEN fac = ny + 1.0_wp ELSE fac = 1.0_wp ENDIF DO j = 1, ny/2 DO k = 1, n spectrum_y(j,k,m) = spectrum_y(j,k,m) + sums_spectra(j,k) * fac ENDDO ENDDO ENDIF ! !-- n_sp_y is needed by data_output_spectra_y n_sp_y = n END SUBROUTINE calc_spectra_y #endif