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

Last change on this file since 4457 was 4429, checked in by raasch, 4 years ago

serial (non-MPI) test case added, several bugfixes for the serial mode

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