source: palm/trunk/SOURCE/spectra_mod.f90 @ 4181

Last change on this file since 4181 was 4180, checked in by scharf, 5 years ago

removed comments in 'Former revisions' section that are older than 01.01.2019

  • Property svn:keywords set to Id
File size: 26.1 KB
Line 
1!> @file spectra_mod.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2019 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: spectra_mod.f90 4180 2019-08-21 14:37:54Z raasch $
27! unused variable removed
28!
29! 3655 2019-01-07 16:51:22Z knoop
30! Renamed output variables
31!
32!
33! Description:
34! ------------
35!> Calculate horizontal spectra along x and y.
36!> ATTENTION: 1d-decomposition along y still needs improvement, because in that
37!>            case the gridpoint number along z still depends on the PE number
38!>            because transpose_xz has to be used (and possibly also
39!>            transpose_zyd needs modification).
40!------------------------------------------------------------------------------!
41 MODULE spectra_mod
42
43    USE kinds
44
45    PRIVATE
46
47    CHARACTER (LEN=2),  DIMENSION(10) ::  spectra_direction = 'x'
48    CHARACTER (LEN=10), DIMENSION(10) ::  data_output_sp  = ' '
49
50    INTEGER(iwp) ::  average_count_sp = 0
51    INTEGER(iwp) ::  dosp_time_count = 0
52    INTEGER(iwp) ::  n_sp_x = 0, n_sp_y = 0
53
54    INTEGER(iwp) ::  comp_spectra_level(100) = 999999
55
56    LOGICAL ::  calculate_spectra   = .FALSE.  !< internal switch that spectra are calculated
57    LOGICAL ::  spectra_initialized = .FALSE.  !< internal switch that spectra related quantities are initialized
58
59    REAL(wp) ::  averaging_interval_sp = 9999999.9_wp  !< averaging interval for spectra output
60    REAL(wp) ::  dt_dosp = 9999999.9_wp                !< time interval for spectra output
61    REAL(wp) ::  skip_time_dosp = 9999999.9_wp         !< no output of spectra data before this interval has passed
62
63    REAL(wp), DIMENSION(:), ALLOCATABLE ::  var_d
64
65    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  spectrum_x, spectrum_y
66
67    SAVE
68
69    INTERFACE calc_spectra
70       MODULE PROCEDURE calc_spectra
71    END INTERFACE calc_spectra
72
73    INTERFACE preprocess_spectra
74       MODULE PROCEDURE preprocess_spectra
75    END INTERFACE preprocess_spectra
76
77    INTERFACE calc_spectra_x
78       MODULE PROCEDURE calc_spectra_x
79    END INTERFACE calc_spectra_x
80
81    INTERFACE calc_spectra_y
82       MODULE PROCEDURE calc_spectra_y
83    END INTERFACE calc_spectra_y
84
85    INTERFACE spectra_check_parameters
86       MODULE PROCEDURE spectra_check_parameters
87    END INTERFACE spectra_check_parameters
88
89    INTERFACE spectra_header
90       MODULE PROCEDURE spectra_header
91    END INTERFACE spectra_header
92
93    INTERFACE spectra_init
94       MODULE PROCEDURE spectra_init
95    END INTERFACE spectra_init
96
97    INTERFACE spectra_parin
98       MODULE PROCEDURE spectra_parin
99    END INTERFACE spectra_parin
100
101    PUBLIC average_count_sp, averaging_interval_sp, calc_spectra,              &
102           calculate_spectra, comp_spectra_level, data_output_sp,              &
103           dosp_time_count, dt_dosp, n_sp_x, n_sp_y,                           &
104           skip_time_dosp, spectra_check_parameters, spectra_direction,        &
105           spectra_header, spectra_init, spectra_parin, spectrum_x,            &
106           spectrum_y, var_d
107
108
109 CONTAINS
110
111!------------------------------------------------------------------------------!
112! Description:
113! ------------
114!> Parin for &spectra_par for calculating spectra
115!------------------------------------------------------------------------------!
116    SUBROUTINE spectra_parin
117
118       USE control_parameters,                                                 &
119           ONLY:  dt_data_output, message_string
120
121       IMPLICIT NONE
122
123       CHARACTER (LEN=80) ::  line  !< dummy string that contains the current  &
124                                    !< line of the parameter file
125
126       NAMELIST /spectra_par/  averaging_interval_sp, comp_spectra_level,      &
127                               data_output_sp, dt_dosp, skip_time_dosp,        &
128                               spectra_direction
129
130       NAMELIST /spectra_parameters/                                           &
131                               averaging_interval_sp, comp_spectra_level,      &
132                               data_output_sp, dt_dosp, skip_time_dosp,        &
133                               spectra_direction
134!
135!--    Position the namelist-file at the beginning (it was already opened in
136!--    parin), search for the namelist-group of the package and position the
137!--    file at this line.
138       line = ' '
139
140!
141!--    Try to find the spectra package
142       REWIND ( 11 )
143       line = ' '
144       DO WHILE ( INDEX( line, '&spectra_parameters' ) == 0 )
145          READ ( 11, '(A)', END=12 )  line
146       ENDDO
147       BACKSPACE ( 11 )
148
149!
150!--    Read namelist
151       READ ( 11, spectra_parameters, ERR = 10 )
152
153!
154!--    Default setting of dt_dosp here (instead of check_parameters), because
155!--    its current value is needed in init_pegrid
156       IF ( dt_dosp == 9999999.9_wp )  dt_dosp = dt_data_output
157
158!
159!--    Set general switch that spectra shall be calculated
160       calculate_spectra = .TRUE.
161
162       GOTO 14
163
164 10    BACKSPACE( 11 )
165       READ( 11 , '(A)') line
166       CALL parin_fail_message( 'spectra_parameters', line )
167!
168!--    Try to find the old namelist
169 12    REWIND ( 11 )
170       line = ' '
171       DO WHILE ( INDEX( line, '&spectra_par' ) == 0 )
172          READ ( 11, '(A)', END=14 )  line
173       ENDDO
174       BACKSPACE ( 11 )
175
176!
177!--    Read namelist
178       READ ( 11, spectra_par, ERR = 13, END = 14 )
179
180       
181       message_string = 'namelist spectra_par is deprecated and will be ' // &
182                     'removed in near future. Please use namelist ' //       &
183                     'spectra_parameters instead'
184       CALL message( 'spectra_parin', 'PA0487', 0, 1, 0, 6, 0 )
185!
186!--    Default setting of dt_dosp here (instead of check_parameters), because
187!--    its current value is needed in init_pegrid
188       IF ( dt_dosp == 9999999.9_wp )  dt_dosp = dt_data_output
189
190!
191!--    Set general switch that spectra shall be calculated
192       calculate_spectra = .TRUE.
193       
194       GOTO 14
195
196 13    BACKSPACE( 11 )
197       READ( 11 , '(A)') line
198       CALL parin_fail_message( 'spectra_par', line )
199       
200       
201 14    CONTINUE
202
203    END SUBROUTINE spectra_parin
204
205
206
207!------------------------------------------------------------------------------!
208! Description:
209! ------------
210!> Initialization of spectra related variables
211!------------------------------------------------------------------------------!
212    SUBROUTINE spectra_init
213
214       USE indices,                                                            &
215           ONLY:  nx, ny, nzb, nzt
216
217       IMPLICIT NONE
218
219       IF ( spectra_initialized )  RETURN
220
221       IF ( dt_dosp /= 9999999.9_wp )  THEN
222
223          IF ( .NOT. ALLOCATED( spectrum_x ) )  THEN
224             ALLOCATE( spectrum_x( 1:nx/2, 1:100, 1:10 ) )
225             spectrum_x = 0.0_wp
226          ENDIF
227
228          IF ( .NOT. ALLOCATED( spectrum_y ) )  THEN
229             ALLOCATE( spectrum_y( 1:ny/2, 1:100, 1:10 ) )
230             spectrum_y = 0.0_wp
231          ENDIF
232
233          ALLOCATE( var_d(nzb:nzt+1) )
234          var_d = 0.0_wp
235       ENDIF
236
237       spectra_initialized = .TRUE.
238
239    END SUBROUTINE spectra_init
240
241
242
243!------------------------------------------------------------------------------!
244! Description:
245! ------------
246!> Check spectra related quantities
247!------------------------------------------------------------------------------!
248    SUBROUTINE spectra_check_parameters
249
250       USE control_parameters,                                                 &
251           ONLY:  averaging_interval, message_string, skip_time_data_output
252
253       IMPLICIT NONE
254
255!
256!--    Check the average interval
257       IF ( averaging_interval_sp == 9999999.9_wp )  THEN
258          averaging_interval_sp = averaging_interval
259       ENDIF
260
261       IF ( averaging_interval_sp > dt_dosp )  THEN
262          WRITE( message_string, * )  'averaging_interval_sp = ',              &
263                averaging_interval_sp, ' must be <= dt_dosp = ', dt_dosp
264          CALL message( 'spectra_check_parameters', 'PA0087', 1, 2, 0, 6, 0 )
265       ENDIF
266
267!
268!--    Set the default skip time interval for data output, if necessary
269       IF ( skip_time_dosp == 9999999.9_wp )                                   &
270                                          skip_time_dosp = skip_time_data_output
271
272    END SUBROUTINE spectra_check_parameters
273
274
275
276!------------------------------------------------------------------------------!
277! Description:
278! ------------
279!> Header output for spectra
280!>
281!> @todo Output of netcdf data format and compression level
282!------------------------------------------------------------------------------!
283    SUBROUTINE spectra_header ( io )
284
285       USE control_parameters,                                                 &
286           ONLY:  dt_averaging_input_pr
287
288!       USE netcdf_interface,                                                  &
289!           ONLY:  netcdf_data_format_string, netcdf_deflate
290
291       IMPLICIT NONE
292
293!       CHARACTER (LEN=40) ::  output_format       !< internal string
294
295       INTEGER(iwp) ::  i                         !< internal counter
296       INTEGER(iwp), INTENT(IN) ::  io            !< Unit of the output file
297
298!
299!--    Spectra output
300       IF ( dt_dosp /= 9999999.9_wp )  THEN
301          WRITE ( io, 1 )
302
303!          output_format = netcdf_data_format_string
304!          IF ( netcdf_deflate == 0 )  THEN
305!             WRITE ( io, 2 )  output_format
306!          ELSE
307!             WRITE ( io, 3 )  TRIM( output_format ), netcdf_deflate
308!          ENDIF
309          WRITE ( io, 2 )  'see profiles or other quantities'
310          WRITE ( io, 4 )  dt_dosp
311          IF ( skip_time_dosp /= 0.0_wp )  WRITE ( io, 5 )  skip_time_dosp
312          WRITE ( io, 6 )  ( data_output_sp(i), i = 1,10 ),     &
313                           ( spectra_direction(i), i = 1,10 ),  &
314                           ( comp_spectra_level(i), i = 1,100 ), &
315                           averaging_interval_sp, dt_averaging_input_pr
316       ENDIF
317
318     1 FORMAT ('    Spectra:')
319     2 FORMAT ('       Output format: ',A/)
320!     3 FORMAT ('       Output format: ',A, '   compressed with level: ',I1/)
321     4 FORMAT ('       Output every ',F7.1,' s'/)
322     5 FORMAT ('       No output during initial ',F8.2,' s')
323     6 FORMAT ('       Arrays:     ', 10(A5,',')/                         &
324               '       Directions: ', 10(A5,',')/                         &
325               '       height levels  k = ', 20(I3,',')/                  &
326               '                          ', 20(I3,',')/                  &
327               '                          ', 20(I3,',')/                  &
328               '                          ', 20(I3,',')/                  &
329               '                          ', 19(I3,','),I3,'.'/           &
330               '       Time averaged over ', F7.1, ' s,' /                &
331               '       Profiles for the time averaging are taken every ', &
332                    F6.1,' s')
333
334    END SUBROUTINE spectra_header
335
336
337
338    SUBROUTINE calc_spectra
339
340       USE arrays_3d,                                                          &
341           ONLY:  d, tend
342
343       USE control_parameters,                                                 &
344           ONLY:  bc_lr_cyc, bc_ns_cyc, message_string, psolver
345
346       USE cpulog,                                                             &
347           ONLY:  cpu_log, log_point
348
349       USE fft_xy,                                                             &
350           ONLY:  fft_init
351
352       USE indices,                                                            &
353           ONLY:  nxl, nxr, nyn, nys, nzb, nzt
354
355       USE kinds
356
357       USE pegrid,                                                             &
358           ONLY:  myid, pdims
359
360       IMPLICIT NONE
361
362       INTEGER(iwp) ::  m  !<
363       INTEGER(iwp) ::  pr !<
364
365
366!
367!--    Check if user gave any levels for spectra to be calculated
368       IF ( comp_spectra_level(1) == 999999 )  RETURN
369
370       CALL cpu_log( log_point(30), 'calc_spectra', 'start' )
371
372!
373!--    Initialize spectra related quantities
374       CALL spectra_init
375
376!
377!--    Initialize ffts
378       CALL fft_init
379
380!
381!--    Reallocate array d in required size
382       IF ( psolver(1:9) == 'multigrid' )  THEN
383          DEALLOCATE( d )
384          ALLOCATE( d(nzb+1:nzt,nys:nyn,nxl:nxr) )
385       ENDIF
386
387       m = 1
388       DO WHILE ( data_output_sp(m) /= ' '  .AND.  m <= 10 )
389!
390!--       Transposition from z --> x  ( y --> x in case of a 1d-decomposition
391!--       along x)
392          IF ( INDEX( spectra_direction(m), 'x' ) /= 0 )  THEN
393
394!
395!--          Calculation of spectra works for cyclic boundary conditions only
396             IF ( .NOT. bc_lr_cyc )  THEN
397
398                message_string = 'non-cyclic lateral boundaries along x do'//  &
399                                 ' not &  allow calculation of spectra along x'
400                CALL message( 'calc_spectra', 'PA0160', 1, 2, 0, 6, 0 )
401             ENDIF
402
403             CALL preprocess_spectra( m, pr )
404
405#if defined( __parallel )
406             IF ( pdims(2) /= 1 )  THEN
407                CALL resort_for_zx( d, tend )
408                CALL transpose_zx( tend, d )
409             ELSE
410                CALL transpose_yxd( d, d )
411             ENDIF
412             CALL calc_spectra_x( d, m )
413#else
414             message_string = 'sorry, calculation of spectra in non paral' //  &
415                              'lel mode& is still not realized'
416             CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 )
417#endif
418
419          ENDIF
420
421!
422!--       Transposition from z --> y (d is rearranged only in case of a
423!--       1d-decomposition along x)
424          IF ( INDEX( spectra_direction(m), 'y' ) /= 0 )  THEN
425
426!
427!--          Calculation of spectra works for cyclic boundary conditions only
428             IF ( .NOT. bc_ns_cyc )  THEN
429                IF ( myid == 0 )  THEN
430                   message_string = 'non-cyclic lateral boundaries along y' // &
431                                    ' do not & allow calculation of spectra' //&
432                                    ' along y'
433                   CALL message( 'calc_spectra', 'PA0162', 1, 2, 0, 6, 0 )
434                ENDIF
435                CALL local_stop
436             ENDIF
437
438             CALL preprocess_spectra( m, pr )
439
440#if defined( __parallel )
441             CALL transpose_zyd( d, d )
442             CALL calc_spectra_y( d, m )
443#else
444             message_string = 'sorry, calculation of spectra in non paral' //  &
445                              'lel mode& is still not realized'
446             CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 )
447#endif
448
449          ENDIF
450
451!
452!--       Increase counter for next spectrum
453          m = m + 1
454         
455       ENDDO
456
457!
458!--    Increase counter for averaging process in routine plot_spectra
459       average_count_sp = average_count_sp + 1
460
461       CALL cpu_log( log_point(30), 'calc_spectra', 'stop' )
462
463    END SUBROUTINE calc_spectra
464
465
466!------------------------------------------------------------------------------!
467! Description:
468! ------------
469!> @todo Missing subroutine description.
470!------------------------------------------------------------------------------!
471    SUBROUTINE preprocess_spectra( m, pr )
472
473       USE arrays_3d,                                                          &
474           ONLY:  d, pt, q, s, u, v, w
475
476       USE indices,                                                            &
477           ONLY:  ngp_2dh, nxl, nxr, nyn, nys, nzb, nzt
478
479       USE kinds
480
481#if defined( __parallel )
482#if !defined( __mpifh )
483       USE MPI
484#endif
485#endif
486
487       USE pegrid,                                                             &
488           ONLY:  collective_wait, comm2d, ierr
489
490       USE statistics,                                                         &
491           ONLY:  hom
492
493
494       IMPLICIT NONE
495
496#if defined( __parallel )
497#if defined( __mpifh )
498       INCLUDE "mpif.h"
499#endif
500#endif
501
502       INTEGER(iwp) :: i  !<
503       INTEGER(iwp) :: j  !<
504       INTEGER(iwp) :: k  !<
505       INTEGER(iwp) :: m  !<
506       INTEGER(iwp) :: pr !<
507
508       REAL(wp), DIMENSION(nzb:nzt+1) :: var_d_l
509
510       SELECT CASE ( TRIM( data_output_sp(m) ) )
511         
512       CASE ( 'u' )
513          pr = 1
514          d(nzb+1:nzt,nys:nyn,nxl:nxr) = u(nzb+1:nzt,nys:nyn,nxl:nxr)
515       
516       CASE ( 'v' )
517          pr = 2
518          d(nzb+1:nzt,nys:nyn,nxl:nxr) = v(nzb+1:nzt,nys:nyn,nxl:nxr)
519       
520       CASE ( 'w' )
521          pr = 3
522          d(nzb+1:nzt,nys:nyn,nxl:nxr) = w(nzb+1:nzt,nys:nyn,nxl:nxr)
523       
524       CASE ( 'theta' )
525          pr = 4
526          d(nzb+1:nzt,nys:nyn,nxl:nxr) = pt(nzb+1:nzt,nys:nyn,nxl:nxr)
527       
528       CASE ( 'q' )
529          pr = 41
530          d(nzb+1:nzt,nys:nyn,nxl:nxr) = q(nzb+1:nzt,nys:nyn,nxl:nxr)
531         
532       CASE ( 's' )
533          pr = 117
534          d(nzb+1:nzt,nys:nyn,nxl:nxr) = s(nzb+1:nzt,nys:nyn,nxl:nxr)
535       
536       CASE DEFAULT
537!
538!--       The DEFAULT case is reached either if the parameter data_output_sp(m)
539!--       contains a wrong character string or if the user has coded a special
540!--       case in the user interface. There, the subroutine user_spectra
541!--       checks which of these two conditions applies.
542          CALL user_spectra( 'preprocess', m, pr )
543         
544       END SELECT
545
546!
547!--    Subtract horizontal mean from the array, for which spectra have to be
548!--    calculated. Moreover, calculate variance of the respective quantitiy,
549!--    later used for normalizing spectra output.
550       var_d_l(:) = 0.0_wp
551       DO  i = nxl, nxr
552          DO  j = nys, nyn
553             DO  k = nzb+1, nzt
554                d(k,j,i)   = d(k,j,i) - hom(k,1,pr,0)
555                var_d_l(k) = var_d_l(k) + d(k,j,i) * d(k,j,i)
556             ENDDO
557          ENDDO
558       ENDDO
559!
560!--    Compute total variance from local variances
561       var_d(:) = 0.0_wp
562#if defined( __parallel ) 
563       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
564       CALL MPI_ALLREDUCE( var_d_l(0), var_d(0), nzt+1-nzb, MPI_REAL, MPI_SUM, &
565                           comm2d, ierr )
566#else
567       var_d(:) = var_d_l(:)
568#endif
569       var_d(:) = var_d(:) / ngp_2dh(0)
570
571    END SUBROUTINE preprocess_spectra
572
573
574!------------------------------------------------------------------------------!
575! Description:
576! ------------
577!> @todo Missing subroutine description.
578!------------------------------------------------------------------------------!
579    SUBROUTINE calc_spectra_x( ddd, m )
580
581       USE fft_xy,                                                             &
582           ONLY:  fft_x_1d
583
584       USE grid_variables,                                                     &
585           ONLY:  dx
586
587       USE indices,                                                            &
588           ONLY:  nx, ny
589
590       USE kinds
591
592#if defined( __parallel )
593#if !defined( __mpifh )
594       USE MPI
595#endif
596#endif
597
598       USE pegrid,                                                             &
599           ONLY:  comm2d, ierr, myid
600
601       USE transpose_indices,                                                  &
602           ONLY:  nyn_x, nys_x, nzb_x, nzt_x
603
604
605       IMPLICIT NONE
606
607#if defined( __parallel )
608#if defined( __mpifh )
609       INCLUDE "mpif.h"
610#endif
611#endif
612
613       INTEGER(iwp) ::  i         !<
614       INTEGER(iwp) ::  j         !<
615       INTEGER(iwp) ::  k         !<
616       INTEGER(iwp) ::  m         !<
617       INTEGER(iwp) ::  n         !<
618
619       REAL(wp) ::  exponent     !<
620       REAL(wp) ::  sum_spec_dum !< wavenumber-integrated spectrum
621   
622       REAL(wp), DIMENSION(0:nx) ::  work !<
623   
624       REAL(wp), DIMENSION(0:nx/2) ::  sums_spectra_l !<
625   
626       REAL(wp), DIMENSION(0:nx/2,100) ::  sums_spectra !<
627   
628       REAL(wp), DIMENSION(0:nx,nys_x:nyn_x,nzb_x:nzt_x) ::  ddd !<
629
630!
631!--    Exponent for geometric average
632       exponent = 1.0_wp / ( ny + 1.0_wp )
633
634!
635!--    Loop over all levels defined by the user
636       n = 1
637       DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
638
639          k = comp_spectra_level(n)
640
641!
642!--       Calculate FFT only if the corresponding level is situated on this PE
643          IF ( k >= nzb_x  .AND.  k <= nzt_x )  THEN
644         
645             DO  j = nys_x, nyn_x
646
647                work = ddd(0:nx,j,k)
648                CALL fft_x_1d( work, 'forward' )
649
650                ddd(0,j,k) = dx * work(0)**2
651                DO  i = 1, nx/2
652                   ddd(i,j,k) = dx * ( work(i)**2 + work(nx+1-i)**2 )
653                ENDDO
654
655             ENDDO
656
657!
658!--          Local sum and geometric average of these spectra
659!--          (WARNING: no global sum should be performed, because floating
660!--          point overflow may occur)
661             DO  i = 0, nx/2
662
663                sums_spectra_l(i) = 1.0_wp
664                DO  j = nys_x, nyn_x
665                   sums_spectra_l(i) = sums_spectra_l(i) * ddd(i,j,k)**exponent
666                ENDDO
667
668             ENDDO
669         
670          ELSE
671
672             sums_spectra_l = 1.0_wp
673
674          ENDIF
675
676!
677!--       Global sum of spectra on PE0 (from where they are written on file)
678          sums_spectra(:,n) = 0.0_wp
679#if defined( __parallel )   
680          CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
681          CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), nx/2+1,       &
682                           MPI_REAL, MPI_PROD, 0, comm2d, ierr )
683#else
684          sums_spectra(:,n) = sums_spectra_l
685#endif
686!
687!--       Normalize spectra by variance
688          sum_spec_dum = SUM( sums_spectra(1:nx/2,n) )
689
690          IF ( sum_spec_dum /= 0.0_wp )  THEN
691             sums_spectra(1:nx/2,n) = sums_spectra(1:nx/2,n) *                 &
692                                      var_d(k) / sum_spec_dum
693          ENDIF
694          n = n + 1
695
696       ENDDO
697       n = n - 1
698
699       IF ( myid == 0 )  THEN
700!
701!--       Sum of spectra for later averaging (see routine data_output_spectra)
702          DO  i = 1, nx/2
703             DO k = 1, n
704                spectrum_x(i,k,m) = spectrum_x(i,k,m) + sums_spectra(i,k)
705             ENDDO
706          ENDDO
707
708       ENDIF
709!
710!--    n_sp_x is needed by data_output_spectra_x
711       n_sp_x = n
712
713    END SUBROUTINE calc_spectra_x
714
715
716!------------------------------------------------------------------------------!
717! Description:
718! ------------
719!> @todo Missing subroutine description.
720!------------------------------------------------------------------------------!
721    SUBROUTINE calc_spectra_y( ddd, m )
722
723       USE fft_xy,                                                             &
724           ONLY:  fft_y_1d
725
726       USE grid_variables,                                                     &
727           ONLY:  dy
728
729       USE indices,                                                            &
730           ONLY:  nx, ny
731
732       USE kinds
733
734#if defined( __parallel )
735#if !defined( __mpifh )
736       USE MPI
737#endif
738#endif
739
740       USE pegrid,                                                             &
741           ONLY:  comm2d, ierr, myid
742
743       USE transpose_indices,                                                  &
744           ONLY:  nxl_yd, nxr_yd, nzb_yd, nzt_yd
745
746
747       IMPLICIT NONE
748
749#if defined( __parallel )
750#if defined( __mpifh )
751       INCLUDE "mpif.h"
752#endif
753#endif
754
755       INTEGER(iwp) ::  i         !<
756       INTEGER(iwp) ::  j         !<
757       INTEGER(iwp) ::  k         !<
758       INTEGER(iwp) ::  m         !<
759       INTEGER(iwp) ::  n         !<
760
761       REAL(wp) ::  exponent !<
762       REAL(wp) ::  sum_spec_dum !< wavenumber-integrated spectrum
763   
764       REAL(wp), DIMENSION(0:ny) ::  work !<
765   
766       REAL(wp), DIMENSION(0:ny/2) ::  sums_spectra_l !<
767   
768       REAL(wp), DIMENSION(0:ny/2,100) ::  sums_spectra !<
769   
770       REAL(wp), DIMENSION(0:ny,nxl_yd:nxr_yd,nzb_yd:nzt_yd) :: ddd !<
771
772
773!
774!--    Exponent for geometric average
775       exponent = 1.0_wp / ( nx + 1.0_wp )
776
777!
778!--    Loop over all levels defined by the user
779       n = 1
780       DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
781
782          k = comp_spectra_level(n)
783
784!
785!--       Calculate FFT only if the corresponding level is situated on this PE
786          IF ( k >= nzb_yd  .AND.  k <= nzt_yd )  THEN
787         
788             DO  i = nxl_yd, nxr_yd
789
790                work = ddd(0:ny,i,k)
791                CALL fft_y_1d( work, 'forward' )
792
793                ddd(0,i,k) = dy * work(0)**2
794                DO  j = 1, ny/2
795                   ddd(j,i,k) = dy * ( work(j)**2 + work(ny+1-j)**2 )
796                ENDDO
797
798             ENDDO
799
800!
801!--          Local sum and geometric average of these spectra
802!--          (WARNING: no global sum should be performed, because floating
803!--          point overflow may occur)
804             DO  j = 0, ny/2
805
806                sums_spectra_l(j) = 1.0_wp
807                DO  i = nxl_yd, nxr_yd
808                   sums_spectra_l(j) = sums_spectra_l(j) * ddd(j,i,k)**exponent
809                ENDDO
810
811             ENDDO
812         
813          ELSE
814
815             sums_spectra_l = 1.0_wp
816
817          ENDIF
818
819!
820!--       Global sum of spectra on PE0 (from where they are written on file)
821          sums_spectra(:,n) = 0.0_wp
822#if defined( __parallel )   
823          CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
824          CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), ny/2+1,       &
825                           MPI_REAL, MPI_PROD, 0, comm2d, ierr )
826#else
827          sums_spectra(:,n) = sums_spectra_l
828#endif
829!
830!--       Normalize spectra by variance
831          sum_spec_dum = SUM( sums_spectra(1:ny/2,n) )
832          IF ( sum_spec_dum /= 0.0_wp )  THEN
833             sums_spectra(1:ny/2,n) = sums_spectra(1:ny/2,n) *                 &
834                                      var_d(k) / sum_spec_dum
835          ENDIF
836          n = n + 1
837
838       ENDDO
839       n = n - 1
840
841
842       IF ( myid == 0 )  THEN
843!
844!--       Sum of spectra for later averaging (see routine data_output_spectra)
845          DO  j = 1, ny/2
846             DO k = 1, n
847                spectrum_y(j,k,m) = spectrum_y(j,k,m) + sums_spectra(j,k)
848             ENDDO
849          ENDDO
850
851       ENDIF
852
853!
854!--    n_sp_y is needed by data_output_spectra_y
855       n_sp_y = n
856
857    END SUBROUTINE calc_spectra_y
858
859 END MODULE spectra_mod
Note: See TracBrowser for help on using the repository browser.