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

Last change on this file since 1833 was 1833, checked in by raasch, 8 years ago

spectrum renamed spactra_par and further modularized, POINTER-attributes added in coupler-routines to avoid gfortran error messages

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