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

Last change on this file since 3246 was 3246, checked in by sward, 6 years ago

Added error handling for wrong input parameters

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