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

Last change on this file since 2000 was 2000, checked in by knoop, 8 years ago

Forced header and separation lines into 80 columns

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