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

Last change on this file since 1511 was 1511, checked in by suehring, 9 years ago

Bugfix concerning spectra normalization

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