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

Last change on this file since 2209 was 2193, checked in by raasch, 7 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 26.7 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!
23!
24! Former revisions:
25! -----------------
26! $Id: spectra_mod.f90 2193 2017-03-22 04:21:28Z kanani $
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
562       var_d_l(:) = 0.0_wp
563       DO  i = nxl, nxr
564          DO  j = nys, nyn
565             DO  k = nzb+1, nzt
566                d(k,j,i)   = d(k,j,i) - hom(k,1,pr,0)
567                var_d_l(k) = var_d_l(k) + d(k,j,i) * d(k,j,i)
568             ENDDO
569          ENDDO
570       ENDDO
571!
572!--    Compute total variance from local variances
573       var_d(:) = 0.0_wp
574#if defined( __parallel ) 
575       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
576       CALL MPI_ALLREDUCE( var_d_l(0), var_d(0), nzt+1-nzb, MPI_REAL, MPI_SUM, &
577                           comm2d, ierr )
578#else
579       var_d(:) = var_d_l(:)
580#endif
581       var_d(:) = var_d(:) / ngp_2dh(0)
582
583    END SUBROUTINE preprocess_spectra
584
585
586!------------------------------------------------------------------------------!
587! Description:
588! ------------
589!> @todo Missing subroutine description.
590!------------------------------------------------------------------------------!
591    SUBROUTINE calc_spectra_x( ddd, pr, m )
592
593       USE control_parameters,                                                 &
594           ONLY:  fft_method
595
596       USE fft_xy,                                                             &
597           ONLY:  fft_x_1d
598
599       USE grid_variables,                                                     &
600           ONLY:  dx
601
602       USE indices,                                                            &
603           ONLY:  nx, ny
604
605       USE kinds
606
607#if defined( __parallel )
608#if defined( __mpifh )
609       INCLUDE "mpif.h"
610#else
611       USE MPI
612#endif
613#endif
614       USE pegrid,                                                             &
615           ONLY:  comm2d, ierr, myid
616
617       USE transpose_indices,                                                  &
618           ONLY:  nyn_x, nys_x, nzb_x, nzt_x
619
620
621       IMPLICIT NONE
622
623       INTEGER(iwp) ::  i         !<
624       INTEGER(iwp) ::  ishape(1) !<
625       INTEGER(iwp) ::  j         !<
626       INTEGER(iwp) ::  k         !<
627       INTEGER(iwp) ::  m         !<
628       INTEGER(iwp) ::  n         !<
629       INTEGER(iwp) ::  pr        !<
630
631       REAL(wp) ::  exponent     !<
632       REAL(wp) ::  sum_spec_dum !< wavenumber-integrated spectrum
633   
634       REAL(wp), DIMENSION(0:nx) ::  work !<
635   
636       REAL(wp), DIMENSION(0:nx/2) ::  sums_spectra_l !<
637   
638       REAL(wp), DIMENSION(0:nx/2,100) ::  sums_spectra !<
639   
640       REAL(wp), DIMENSION(0:nx,nys_x:nyn_x,nzb_x:nzt_x) ::  ddd !<
641
642!
643!--    Exponent for geometric average
644       exponent = 1.0_wp / ( ny + 1.0_wp )
645
646!
647!--    Loop over all levels defined by the user
648       n = 1
649       DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
650
651          k = comp_spectra_level(n)
652
653!
654!--       Calculate FFT only if the corresponding level is situated on this PE
655          IF ( k >= nzb_x  .AND.  k <= nzt_x )  THEN
656         
657             DO  j = nys_x, nyn_x
658
659                work = ddd(0:nx,j,k)
660                CALL fft_x_1d( work, 'forward' )
661
662                ddd(0,j,k) = dx * work(0)**2
663                DO  i = 1, nx/2
664                   ddd(i,j,k) = dx * ( work(i)**2 + work(nx+1-i)**2 )
665                ENDDO
666
667             ENDDO
668
669!
670!--          Local sum and geometric average of these spectra
671!--          (WARNING: no global sum should be performed, because floating
672!--          point overflow may occur)
673             DO  i = 0, nx/2
674
675                sums_spectra_l(i) = 1.0_wp
676                DO  j = nys_x, nyn_x
677                   sums_spectra_l(i) = sums_spectra_l(i) * ddd(i,j,k)**exponent
678                ENDDO
679
680             ENDDO
681         
682          ELSE
683
684             sums_spectra_l = 1.0_wp
685
686          ENDIF
687
688!
689!--       Global sum of spectra on PE0 (from where they are written on file)
690          sums_spectra(:,n) = 0.0_wp
691#if defined( __parallel )   
692          CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
693          CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), nx/2+1,       &
694                           MPI_REAL, MPI_PROD, 0, comm2d, ierr )
695#else
696          sums_spectra(:,n) = sums_spectra_l
697#endif
698!
699!--       Normalize spectra by variance
700          sum_spec_dum = SUM( sums_spectra(:,n) )
701          IF ( sum_spec_dum /= 0.0_wp )  THEN
702             sums_spectra(:,n) = sums_spectra(:,n) * var_d(k) / sum_spec_dum
703          ENDIF
704          n = n + 1
705
706       ENDDO
707       n = n - 1
708
709       IF ( myid == 0 )  THEN
710!
711!--       Sum of spectra for later averaging (see routine data_output_spectra)
712          DO  i = 1, nx/2
713             DO k = 1, n
714                spectrum_x(i,k,m) = spectrum_x(i,k,m) + sums_spectra(i,k)
715             ENDDO
716          ENDDO
717
718       ENDIF
719!
720!--    n_sp_x is needed by data_output_spectra_x
721       n_sp_x = n
722
723    END SUBROUTINE calc_spectra_x
724
725
726!------------------------------------------------------------------------------!
727! Description:
728! ------------
729!> @todo Missing subroutine description.
730!------------------------------------------------------------------------------!
731    SUBROUTINE calc_spectra_y( ddd, pr, m )
732
733       USE control_parameters,                                                 &
734           ONLY:  fft_method
735
736       USE fft_xy,                                                             &
737           ONLY:  fft_y_1d
738
739       USE grid_variables,                                                     &
740           ONLY:  dy
741
742       USE indices,                                                            &
743           ONLY:  nx, ny
744
745       USE kinds
746
747#if defined( __parallel )
748#if defined( __mpifh )
749       INCLUDE "mpif.h"
750#else
751       USE MPI
752#endif
753#endif
754       USE pegrid,                                                             &
755           ONLY:  comm2d, ierr, myid
756
757       USE transpose_indices,                                                  &
758           ONLY:  nxl_yd, nxr_yd, nzb_yd, nzt_yd
759
760
761       IMPLICIT NONE
762
763       INTEGER(iwp) ::  i         !<
764       INTEGER(iwp) ::  j         !<
765       INTEGER(iwp) ::  jshape(1) !<
766       INTEGER(iwp) ::  k         !<
767       INTEGER(iwp) ::  m         !<
768       INTEGER(iwp) ::  n         !<
769       INTEGER(iwp) ::  pr        !<
770
771       REAL(wp) ::  exponent !<
772       REAL(wp) ::  sum_spec_dum !< wavenumber-integrated spectrum
773   
774       REAL(wp), DIMENSION(0:ny) ::  work !<
775   
776       REAL(wp), DIMENSION(0:ny/2) ::  sums_spectra_l !<
777   
778       REAL(wp), DIMENSION(0:ny/2,100) ::  sums_spectra !<
779   
780       REAL(wp), DIMENSION(0:ny,nxl_yd:nxr_yd,nzb_yd:nzt_yd) :: ddd !<
781
782
783!
784!--    Exponent for geometric average
785       exponent = 1.0_wp / ( nx + 1.0_wp )
786
787!
788!--    Loop over all levels defined by the user
789       n = 1
790       DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
791
792          k = comp_spectra_level(n)
793
794!
795!--       Calculate FFT only if the corresponding level is situated on this PE
796          IF ( k >= nzb_yd  .AND.  k <= nzt_yd )  THEN
797         
798             DO  i = nxl_yd, nxr_yd
799
800                work = ddd(0:ny,i,k)
801                CALL fft_y_1d( work, 'forward' )
802
803                ddd(0,i,k) = dy * work(0)**2
804                DO  j = 1, ny/2
805                   ddd(j,i,k) = dy * ( work(j)**2 + work(ny+1-j)**2 )
806                ENDDO
807
808             ENDDO
809
810!
811!--          Local sum and geometric average of these spectra
812!--          (WARNING: no global sum should be performed, because floating
813!--          point overflow may occur)
814             DO  j = 0, ny/2
815
816                sums_spectra_l(j) = 1.0_wp
817                DO  i = nxl_yd, nxr_yd
818                   sums_spectra_l(j) = sums_spectra_l(j) * ddd(j,i,k)**exponent
819                ENDDO
820
821             ENDDO
822         
823          ELSE
824
825             sums_spectra_l = 1.0_wp
826
827          ENDIF
828
829!
830!--       Global sum of spectra on PE0 (from where they are written on file)
831          sums_spectra(:,n) = 0.0_wp
832#if defined( __parallel )   
833          CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
834          CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), ny/2+1,       &
835                           MPI_REAL, MPI_PROD, 0, comm2d, ierr )
836#else
837          sums_spectra(:,n) = sums_spectra_l
838#endif
839!
840!--       Normalize spectra by variance
841          sum_spec_dum = SUM( sums_spectra(:,n) )
842          IF ( SUM(sums_spectra(:,n)) /= 0.0_wp )  THEN
843             sums_spectra(:,n) = sums_spectra(:,n) *                           &
844                                 var_d(k) / SUM(sums_spectra(:,n))
845          ENDIF
846          n = n + 1
847
848       ENDDO
849       n = n - 1
850
851
852       IF ( myid == 0 )  THEN
853!
854!--       Sum of spectra for later averaging (see routine data_output_spectra)
855          DO  j = 1, ny/2
856             DO k = 1, n
857                spectrum_y(j,k,m) = spectrum_y(j,k,m) + sums_spectra(j,k)
858             ENDDO
859          ENDDO
860
861       ENDIF
862
863!
864!--    n_sp_y is needed by data_output_spectra_y
865       n_sp_y = n
866
867    END SUBROUTINE calc_spectra_y
868
869 END MODULE spectra_mod
Note: See TracBrowser for help on using the repository browser.