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

Last change on this file since 1668 was 1576, checked in by raasch, 9 years ago

last commit documented

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