source: palm/trunk/SOURCE/calc_spectra.f90 @ 1682

Last change on this file since 1682 was 1682, checked in by knoop, 9 years ago

Code annotations made doxygen readable

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