source: palm/trunk/SOURCE/spectrum.f90 @ 1815

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

cpp-switches removed + cpp-bugfixes + zero-settings for velocities inside topography re-activated

  • Property svn:keywords set to Id
File size: 20.8 KB
Line 
1!> @file spectrum.f90
2!--------------------------------------------------------------------------------!
3! This file is part of PALM.
4!
5! PALM is free software: you can redistribute it and/or modify it under the terms
6! of the GNU General Public License as published by the Free Software Foundation,
7! either version 3 of the License, or (at your option) any later version.
8!
9! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
10! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12!
13! You should have received a copy of the GNU General Public License along with
14! PALM. If not, see <http://www.gnu.org/licenses/>.
15!
16! Copyright 1997-2014 Leibniz Universitaet Hannover
17!--------------------------------------------------------------------------------!
18!
19! Current revisions:
20! -----------------
21! bugfix: preprocessor directives included for the non-parallel case
22!
23! Former revisions:
24! -----------------
25! $Id: spectrum.f90 1815 2016-04-06 13:49:59Z raasch $
26!
27! 1808 2016-04-05 19:44:00Z raasch
28! MPI module used by default on all machines
29!
30! 1786 2016-03-08 05:49:27Z raasch
31! routine is modularized, filename renamed from calc_spectra to spectrum,
32! privious data module spectrum moved from modules.f90 to here,
33! cpp-direktives for spectra removed, immediate return if no spectra levels are
34! given
35!
36! 1682 2015-10-07 23:56:08Z knoop
37! Code annotations made doxygen readable
38!
39! 1575 2015-03-27 09:56:27Z raasch
40! adjustments for psolver-queries
41!
42! 1511 2014-12-16 15:54:16Z suehring
43! Bugfix concerning spectra normalization
44!
45! 1431 2014-07-15 14:47:17Z suehring
46! Wavenumber-integrated spectra coincide with respective variance.
47!
48! 1342 2014-03-26 17:04:47Z kanani
49! REAL constants defined as wp-kinds
50!
51! 1324 2014-03-21 09:13:16Z suehring
52! Bugfix: nzb_x, nzb_yd, nyn_x, nyn_x, nzt_x, nzt_yd belong to transpose_indices
53!
54! 1320 2014-03-20 08:40:49Z raasch
55! ONLY-attribute added to USE-statements,
56! kind-parameters added to all INTEGER and REAL declaration statements,
57! kinds are defined in new module kinds,
58! revision history before 2012 removed,
59! comment fields (!:) to be used for variable explanations added to
60! all variable declaration statements
61!
62! 1318 2014-03-17 13:35:16Z raasch
63! module interfaces removed
64!
65! 1216 2013-08-26 09:31:42Z raasch
66! resorting of array moved to separate routine resort_for_zx,
67! one argument removed from the transpose_..d routines
68!
69! 1120 2013-04-05 15:11:35Z raasch
70! bugfix: calls of fft_x|y replaced by fft_x|y_1d
71!
72! 1036 2012-10-22 13:43:42Z raasch
73! code put under GPL (PALM 3.9)
74!
75! 1003 2012-09-14 14:35:53Z raasch
76! adjustment of array tend for cases with unequal subdomain sizes removed
77!
78! Revision 1.1  2001/01/05 15:08:07  raasch
79! Initial revision
80!
81!
82! Description:
83! ------------
84!> Calculate horizontal spectra along x and y.
85!> ATTENTION: 1d-decomposition along y still needs improvement, because in that
86!>            case the gridpoint number along z still depends on the PE number
87!>            because transpose_xz has to be used (and possibly also
88!>            transpose_zyd needs modification).
89!------------------------------------------------------------------------------!
90 MODULE spectrum
91
92    USE kinds
93
94    PRIVATE
95
96    CHARACTER (LEN=6),  DIMENSION(1:5) ::  header_char = (/ 'PS(u) ', 'PS(v) ',&
97                                           'PS(w) ', 'PS(pt)', 'PS(q) ' /)
98    CHARACTER (LEN=2),  DIMENSION(10)  ::  spectra_direction = 'x'
99    CHARACTER (LEN=10), DIMENSION(10)  ::  data_output_sp  = ' '
100    CHARACTER (LEN=25), DIMENSION(1:5) ::  utext_char =                    &
101                                           (/ '-power spectrum of u     ', &
102                                              '-power spectrum of v     ', &
103                                              '-power spectrum of w     ', &
104                                              '-power spectrum of ^1185 ', &
105                                              '-power spectrum of q     ' /)
106    CHARACTER (LEN=39), DIMENSION(1:5) ::  ytext_char =                        &
107                                 (/ 'k ^2236 ^2566^2569<u(k) in m>2s>->2    ', &
108                                    'k ^2236 ^2566^2569<v(k) in m>2s>->2    ', &
109                                    'k ^2236 ^2566^2569<w(k) in m>2s>->2    ', &
110                                    'k ^2236 ^2566^2569<^1185(k) in m>2s>->2', &
111                                    'k ^2236 ^2566^2569<q(k) in m>2s>->2    ' /)
112
113    INTEGER(iwp) ::  klist_x = 0, klist_y = 0, n_sp_x = 0, n_sp_y = 0
114
115    INTEGER(iwp) ::  comp_spectra_level(100) = 999999,                   &
116                     lstyles(100) = (/ 0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
117                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
118                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
119                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
120                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
121                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
122                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
123                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
124                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
125                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8 /), &
126                     plot_spectra_level(100) = 999999
127
128    REAL(wp)    ::  time_to_start_sp = 0.0_wp
129
130    PUBLIC  comp_spectra_level, data_output_sp, header_char, klist_x, klist_y, &
131            lstyles, n_sp_x, n_sp_y, plot_spectra_level, spectra_direction,    &
132            utext_char, ytext_char
133
134    SAVE
135
136    INTERFACE calc_spectra
137       MODULE PROCEDURE calc_spectra
138    END INTERFACE calc_spectra
139
140    INTERFACE preprocess_spectra
141       MODULE PROCEDURE preprocess_spectra
142    END INTERFACE preprocess_spectra
143
144    INTERFACE calc_spectra_x
145       MODULE PROCEDURE calc_spectra_x
146    END INTERFACE calc_spectra_x
147
148    INTERFACE calc_spectra_y
149       MODULE PROCEDURE calc_spectra_y
150    END INTERFACE calc_spectra_y
151
152    PUBLIC calc_spectra
153
154
155 CONTAINS
156
157    SUBROUTINE calc_spectra
158
159       USE arrays_3d,                                                          &
160           ONLY:  d, tend
161
162       USE control_parameters,                                                 &
163           ONLY:  average_count_sp, bc_lr_cyc, bc_ns_cyc, message_string, psolver
164
165       USE cpulog,                                                             &
166           ONLY:  cpu_log, log_point
167
168       USE fft_xy,                                                             &
169           ONLY:  fft_init
170
171       USE indices,                                                            &
172           ONLY:  nxl, nxr, nyn, nys, nzb, nzt
173
174       USE kinds
175
176       USE pegrid,                                                             &
177           ONLY:  myid, pdims
178
179       IMPLICIT NONE
180
181       INTEGER(iwp) ::  m  !<
182       INTEGER(iwp) ::  pr !<
183
184
185!
186!--    Check if user gave any levels for spectra to be calculated
187       IF ( comp_spectra_level(1) == 999999 )  RETURN
188
189       CALL cpu_log( log_point(30), 'calc_spectra', 'start' )
190
191!
192!--    Initialize ffts
193       CALL fft_init
194
195!
196!--    Reallocate array d in required size
197       IF ( psolver(1:9) == 'multigrid' )  THEN
198          DEALLOCATE( d )
199          ALLOCATE( d(nzb+1:nzt,nys:nyn,nxl:nxr) )
200       ENDIF
201
202       m = 1
203       DO WHILE ( data_output_sp(m) /= ' '  .AND.  m <= 10 )
204!
205!--       Transposition from z --> x  ( y --> x in case of a 1d-decomposition
206!--       along x)
207          IF ( INDEX( spectra_direction(m), 'x' ) /= 0 )  THEN
208
209!
210!--          Calculation of spectra works for cyclic boundary conditions only
211             IF ( .NOT. bc_lr_cyc )  THEN
212
213                message_string = 'non-cyclic lateral boundaries along x do'//  &
214                                 ' not & allow calculation of spectra along x'
215                CALL message( 'calc_spectra', 'PA0160', 1, 2, 0, 6, 0 )
216             ENDIF
217
218             CALL preprocess_spectra( m, pr )
219
220#if defined( __parallel )
221             IF ( pdims(2) /= 1 )  THEN
222                CALL resort_for_zx( d, tend )
223                CALL transpose_zx( tend, d )
224             ELSE
225                CALL transpose_yxd( d, d )
226             ENDIF
227             CALL calc_spectra_x( d, pr, m )
228#else
229             message_string = 'sorry, calculation of spectra in non paral' //  &
230                              'lel mode& is still not realized'
231             CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 )
232#endif
233
234          ENDIF
235
236!
237!--       Transposition from z --> y (d is rearranged only in case of a
238!--       1d-decomposition along x)
239          IF ( INDEX( spectra_direction(m), 'y' ) /= 0 )  THEN
240
241!
242!--          Calculation of spectra works for cyclic boundary conditions only
243             IF ( .NOT. bc_ns_cyc )  THEN
244                IF ( myid == 0 )  THEN
245                   message_string = 'non-cyclic lateral boundaries along y' // &
246                                    ' do not & allow calculation of spectr' // &
247                                    'a along y'
248                   CALL message( 'calc_spectra', 'PA0162', 1, 2, 0, 6, 0 )
249                ENDIF
250                CALL local_stop
251             ENDIF
252
253             CALL preprocess_spectra( m, pr )
254
255#if defined( __parallel )
256             CALL transpose_zyd( d, d )
257             CALL calc_spectra_y( d, pr, m )
258#else
259             message_string = 'sorry, calculation of spectra in non paral' //  &
260                              'lel mode& is still not realized'
261             CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 )
262#endif
263
264          ENDIF
265
266!
267!--       Increase counter for next spectrum
268          m = m + 1
269         
270       ENDDO
271
272!
273!--    Increase counter for averaging process in routine plot_spectra
274       average_count_sp = average_count_sp + 1
275
276       CALL cpu_log( log_point(30), 'calc_spectra', 'stop' )
277
278    END SUBROUTINE calc_spectra
279
280
281!------------------------------------------------------------------------------!
282! Description:
283! ------------
284!> @todo Missing subroutine description.
285!------------------------------------------------------------------------------!
286    SUBROUTINE preprocess_spectra( m, pr )
287
288       USE arrays_3d,                                                          &
289           ONLY:  d, pt, q, u, v, w
290
291       USE indices,                                                            &
292           ONLY:  ngp_2dh, nxl, nxr, nyn, nys, nzb, nzt
293
294       USE kinds
295
296#if defined( __parallel )
297#if defined( __mpifh )
298       INCLUDE "mpif.h"
299#else
300       USE MPI
301#endif
302#endif
303       USE pegrid,                                                             &
304           ONLY:  collective_wait, comm2d, ierr
305
306       USE statistics,                                                         &
307           ONLY:  hom, var_d
308
309
310       IMPLICIT NONE
311
312       INTEGER(iwp) :: i  !<
313       INTEGER(iwp) :: j  !<
314       INTEGER(iwp) :: k  !<
315       INTEGER(iwp) :: m  !<
316       INTEGER(iwp) :: pr !<
317
318       REAL(wp), DIMENSION(nzb:nzt+1) :: var_d_l
319
320       SELECT CASE ( TRIM( data_output_sp(m) ) )
321         
322       CASE ( 'u' )
323          pr = 1
324          d(nzb+1:nzt,nys:nyn,nxl:nxr) = u(nzb+1:nzt,nys:nyn,nxl:nxr)
325       
326       CASE ( 'v' )
327          pr = 2
328          d(nzb+1:nzt,nys:nyn,nxl:nxr) = v(nzb+1:nzt,nys:nyn,nxl:nxr)
329       
330       CASE ( 'w' )
331          pr = 3
332          d(nzb+1:nzt,nys:nyn,nxl:nxr) = w(nzb+1:nzt,nys:nyn,nxl:nxr)
333       
334       CASE ( 'pt' )
335          pr = 4
336          d(nzb+1:nzt,nys:nyn,nxl:nxr) = pt(nzb+1:nzt,nys:nyn,nxl:nxr)
337       
338       CASE ( 'q' )
339          pr = 41
340          d(nzb+1:nzt,nys:nyn,nxl:nxr) = q(nzb+1:nzt,nys:nyn,nxl:nxr)
341       
342       CASE DEFAULT
343!
344!--       The DEFAULT case is reached either if the parameter data_output_sp(m)
345!--       contains a wrong character string or if the user has coded a special
346!--       case in the user interface. There, the subroutine user_spectra
347!--       checks which of these two conditions applies.
348          CALL user_spectra( 'preprocess', m, pr )
349         
350       END SELECT
351
352!
353!--    Subtract horizontal mean from the array, for which spectra have to be
354!--    calculated
355       var_d_l(:) = 0.0_wp
356       DO  i = nxl, nxr
357          DO  j = nys, nyn
358             DO  k = nzb+1, nzt
359                d(k,j,i)   = d(k,j,i) - hom(k,1,pr,0)
360                var_d_l(k) = var_d_l(k) + d(k,j,i) * d(k,j,i)
361             ENDDO
362          ENDDO
363       ENDDO
364!
365!--    Compute total variance from local variances
366       var_d(:) = 0.0_wp
367#if defined( __parallel ) 
368       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
369       CALL MPI_ALLREDUCE( var_d_l(0), var_d(0), nzt+1-nzb, MPI_REAL, MPI_SUM, &
370                           comm2d, ierr )
371#else
372       var_d(:) = var_d_l(:)
373#endif
374       var_d(:) = var_d(:) / ngp_2dh(0)
375
376    END SUBROUTINE preprocess_spectra
377
378
379!------------------------------------------------------------------------------!
380! Description:
381! ------------
382!> @todo Missing subroutine description.
383!------------------------------------------------------------------------------!
384    SUBROUTINE calc_spectra_x( ddd, pr, m )
385
386       USE control_parameters,                                                 &
387           ONLY:  fft_method
388
389       USE fft_xy,                                                             &
390           ONLY:  fft_x_1d
391
392       USE grid_variables,                                                     &
393           ONLY:  dx
394
395       USE indices,                                                            &
396           ONLY:  nx, ny
397
398       USE kinds
399
400#if defiend( __parallel )
401#if defined( __mpifh )
402       INCLUDE "mpif.h"
403#else
404       USE MPI
405#endif
406#endif
407       USE pegrid,                                                             &
408           ONLY:  comm2d, ierr, myid
409
410       USE statistics,                                                         &
411           ONLY:  spectrum_x, var_d
412
413       USE transpose_indices,                                                  &
414           ONLY:  nyn_x, nys_x, nzb_x, nzt_x
415
416
417       IMPLICIT NONE
418
419       INTEGER(iwp) ::  i         !<
420       INTEGER(iwp) ::  ishape(1) !<
421       INTEGER(iwp) ::  j         !<
422       INTEGER(iwp) ::  k         !<
423       INTEGER(iwp) ::  m         !<
424       INTEGER(iwp) ::  n         !<
425       INTEGER(iwp) ::  pr        !<
426
427       REAL(wp) ::  exponent     !<
428       REAL(wp) ::  sum_spec_dum !< wavenumber-integrated spectrum
429   
430       REAL(wp), DIMENSION(0:nx) ::  work !<
431   
432       REAL(wp), DIMENSION(0:nx/2) ::  sums_spectra_l !<
433   
434       REAL(wp), DIMENSION(0:nx/2,100) ::  sums_spectra !<
435   
436       REAL(wp), DIMENSION(0:nx,nys_x:nyn_x,nzb_x:nzt_x) ::  ddd !<
437
438!
439!--    Exponent for geometric average
440       exponent = 1.0_wp / ( ny + 1.0_wp )
441
442!
443!--    Loop over all levels defined by the user
444       n = 1
445       DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
446
447          k = comp_spectra_level(n)
448
449!
450!--       Calculate FFT only if the corresponding level is situated on this PE
451          IF ( k >= nzb_x  .AND.  k <= nzt_x )  THEN
452         
453             DO  j = nys_x, nyn_x
454
455                work = ddd(0:nx,j,k)
456                CALL fft_x_1d( work, 'forward' )
457
458                ddd(0,j,k) = dx * work(0)**2
459                DO  i = 1, nx/2
460                   ddd(i,j,k) = dx * ( work(i)**2 + work(nx+1-i)**2 )
461                ENDDO
462
463             ENDDO
464
465!
466!--          Local sum and geometric average of these spectra
467!--          (WARNING: no global sum should be performed, because floating
468!--          point overflow may occur)
469             DO  i = 0, nx/2
470
471                sums_spectra_l(i) = 1.0_wp
472                DO  j = nys_x, nyn_x
473                   sums_spectra_l(i) = sums_spectra_l(i) * ddd(i,j,k)**exponent
474                ENDDO
475
476             ENDDO
477         
478          ELSE
479
480             sums_spectra_l = 1.0_wp
481
482          ENDIF
483
484!
485!--       Global sum of spectra on PE0 (from where they are written on file)
486          sums_spectra(:,n) = 0.0_wp
487#if defined( __parallel )   
488          CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
489          CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), nx/2+1,       &
490                           MPI_REAL, MPI_PROD, 0, comm2d, ierr )
491#else
492          sums_spectra(:,n) = sums_spectra_l
493#endif
494!
495!--       Normalize spectra by variance
496          sum_spec_dum = SUM( sums_spectra(:,n) )
497          IF ( sum_spec_dum /= 0.0_wp )  THEN
498             sums_spectra(:,n) = sums_spectra(:,n) * var_d(k) / sum_spec_dum
499          ENDIF
500          n = n + 1
501
502       ENDDO
503       n = n - 1
504
505       IF ( myid == 0 )  THEN
506!
507!--       Sum of spectra for later averaging (see routine data_output_spectra)
508          DO  i = 1, nx/2
509             DO k = 1, n
510                spectrum_x(i,k,m) = spectrum_x(i,k,m) + sums_spectra(i,k)
511             ENDDO
512          ENDDO
513
514       ENDIF
515!
516!--    n_sp_x is needed by data_output_spectra_x
517       n_sp_x = n
518
519    END SUBROUTINE calc_spectra_x
520
521
522!------------------------------------------------------------------------------!
523! Description:
524! ------------
525!> @todo Missing subroutine description.
526!------------------------------------------------------------------------------!
527    SUBROUTINE calc_spectra_y( ddd, pr, m )
528
529       USE control_parameters,                                                 &
530           ONLY:  fft_method
531
532       USE fft_xy,                                                             &
533           ONLY:  fft_y_1d
534
535       USE grid_variables,                                                     &
536           ONLY:  dy
537
538       USE indices,                                                            &
539           ONLY:  nx, ny
540
541       USE kinds
542
543#if defined( __parallel )
544#if defined( __mpifh )
545       INCLUDE "mpif.h"
546#else
547       USE MPI
548#endif
549#endif
550       USE pegrid,                                                             &
551           ONLY:  comm2d, ierr, myid
552
553       USE statistics,                                                         &
554           ONLY:  spectrum_y, var_d
555
556       USE transpose_indices,                                                  &
557           ONLY:  nxl_yd, nxr_yd, nzb_yd, nzt_yd
558
559
560       IMPLICIT NONE
561
562       INTEGER(iwp) ::  i         !<
563       INTEGER(iwp) ::  j         !<
564       INTEGER(iwp) ::  jshape(1) !<
565       INTEGER(iwp) ::  k         !<
566       INTEGER(iwp) ::  m         !<
567       INTEGER(iwp) ::  n         !<
568       INTEGER(iwp) ::  pr        !<
569
570       REAL(wp) ::  exponent !<
571       REAL(wp) ::  sum_spec_dum !< wavenumber-integrated spectrum
572   
573       REAL(wp), DIMENSION(0:ny) ::  work !<
574   
575       REAL(wp), DIMENSION(0:ny/2) ::  sums_spectra_l !<
576   
577       REAL(wp), DIMENSION(0:ny/2,100) ::  sums_spectra !<
578   
579       REAL(wp), DIMENSION(0:ny,nxl_yd:nxr_yd,nzb_yd:nzt_yd) :: ddd !<
580
581
582!
583!--    Exponent for geometric average
584       exponent = 1.0_wp / ( nx + 1.0_wp )
585
586!
587!--    Loop over all levels defined by the user
588       n = 1
589       DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
590
591          k = comp_spectra_level(n)
592
593!
594!--       Calculate FFT only if the corresponding level is situated on this PE
595          IF ( k >= nzb_yd  .AND.  k <= nzt_yd )  THEN
596         
597             DO  i = nxl_yd, nxr_yd
598
599                work = ddd(0:ny,i,k)
600                CALL fft_y_1d( work, 'forward' )
601
602                ddd(0,i,k) = dy * work(0)**2
603                DO  j = 1, ny/2
604                   ddd(j,i,k) = dy * ( work(j)**2 + work(ny+1-j)**2 )
605                ENDDO
606
607             ENDDO
608
609!
610!--          Local sum and geometric average of these spectra
611!--          (WARNING: no global sum should be performed, because floating
612!--          point overflow may occur)
613             DO  j = 0, ny/2
614
615                sums_spectra_l(j) = 1.0_wp
616                DO  i = nxl_yd, nxr_yd
617                   sums_spectra_l(j) = sums_spectra_l(j) * ddd(j,i,k)**exponent
618                ENDDO
619
620             ENDDO
621         
622          ELSE
623
624             sums_spectra_l = 1.0_wp
625
626          ENDIF
627
628!
629!--       Global sum of spectra on PE0 (from where they are written on file)
630          sums_spectra(:,n) = 0.0_wp
631#if defined( __parallel )   
632          CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
633          CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), ny/2+1,       &
634                           MPI_REAL, MPI_PROD, 0, comm2d, ierr )
635#else
636          sums_spectra(:,n) = sums_spectra_l
637#endif
638!
639!--       Normalize spectra by variance
640          sum_spec_dum = SUM( sums_spectra(:,n) )
641          IF ( SUM(sums_spectra(:,n)) /= 0.0_wp )  THEN
642             sums_spectra(:,n) = sums_spectra(:,n) *                           &
643                                 var_d(k) / SUM(sums_spectra(:,n))
644          ENDIF
645          n = n + 1
646
647       ENDDO
648       n = n - 1
649
650
651       IF ( myid == 0 )  THEN
652!
653!--       Sum of spectra for later averaging (see routine data_output_spectra)
654          DO  j = 1, ny/2
655             DO k = 1, n
656                spectrum_y(j,k,m) = spectrum_y(j,k,m) + sums_spectra(j,k)
657             ENDDO
658          ENDDO
659
660       ENDIF
661
662!
663!--    n_sp_y is needed by data_output_spectra_y
664       n_sp_y = n
665
666    END SUBROUTINE calc_spectra_y
667
668 END MODULE spectrum
Note: See TracBrowser for help on using the repository browser.