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

Last change on this file since 2215 was 2215, checked in by suehring, 7 years ago

Normalization of spectra output

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