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

Last change on this file since 1960 was 1960, checked in by suehring, 8 years ago

Separate balance equations for humidity and passive_scalar

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