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

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

pmc-change in server-client get-put, spectra-directives removed, spectra-package modularized

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