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

Last change on this file since 1431 was 1431, checked in by suehring, 10 years ago

Spectra coincide with respective variance

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