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

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

Code annotations made doxygen readable

  • Property svn:keywords set to Id
File size: 16.5 KB
Line 
1!> @file calc_spectra.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! Code annotations made doxygen readable
22!
23! Former revisions:
24! -----------------
25! $Id: calc_spectra.f90 1682 2015-10-07 23:56:08Z knoop $
26!
27! 1575 2015-03-27 09:56:27Z raasch
28! adjustments for psolver-queries
29!
30! 1511 2014-12-16 15:54:16Z suehring
31! Bugfix concerning spectra normalization
32!
33! 1431 2014-07-15 14:47:17Z suehring
34! Wavenumber-integrated spectra coincide with respective variance.
35!
36! 1342 2014-03-26 17:04:47Z kanani
37! REAL constants defined as wp-kinds
38!
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!
42! 1320 2014-03-20 08:40:49Z raasch
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
49!
50! 1318 2014-03-17 13:35:16Z raasch
51! module interfaces removed
52!
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!
57! 1120 2013-04-05 15:11:35Z raasch
58! bugfix: calls of fft_x|y replaced by fft_x|y_1d
59!
60! 1036 2012-10-22 13:43:42Z raasch
61! code put under GPL (PALM 3.9)
62!
63! 1003 2012-09-14 14:35:53Z raasch
64! adjustment of array tend for cases with unequal subdomain sizes removed
65!
66! Revision 1.1  2001/01/05 15:08:07  raasch
67! Initial revision
68!
69!
70! Description:
71! ------------
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).
77!------------------------------------------------------------------------------!
78 SUBROUTINE calc_spectra
79 
80
81#if defined( __spectra )
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,                                                               &
95        ONLY:  nxl, nxr, nyn, nys, nzb, nzt
96
97    USE kinds
98
99    USE pegrid
100
101    USE spectrum,                                                              &
102        ONLY:  data_output_sp, spectra_direction
103
104
105    IMPLICIT NONE
106
107    INTEGER(iwp) ::  m  !<
108    INTEGER(iwp) ::  pr !<
109
110
111    CALL cpu_log( log_point(30), 'calc_spectra', 'start' )
112
113!
114!-- Initialize ffts
115    CALL fft_init
116
117!
118!-- Reallocate array d in required size
119    IF ( psolver(1:9) == 'multigrid' )  THEN
120       DEALLOCATE( d )
121       ALLOCATE( d(nzb+1:nzt,nys:nyn,nxl:nxr) )
122    ENDIF
123
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
133          IF ( .NOT. bc_lr_cyc )  THEN
134
135             message_string = 'non-cyclic lateral boundaries along x do not'// &
136                              '& allow calculation of spectra along x'
137             CALL message( 'calc_spectra', 'PA0160', 1, 2, 0, 6, 0 )
138          ENDIF
139
140          CALL preprocess_spectra( m, pr )
141
142#if defined( __parallel )
143          IF ( pdims(2) /= 1 )  THEN
144             CALL resort_for_zx( d, tend )
145             CALL transpose_zx( tend, d )
146          ELSE
147             CALL transpose_yxd( d, d )
148          ENDIF
149          CALL calc_spectra_x( d, pr, m )
150#else
151          message_string = 'sorry, calculation of spectra in non parallel ' // &
152                           'mode& is still not realized'
153          CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 )     
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
165          IF ( .NOT. bc_ns_cyc )  THEN
166             IF ( myid == 0 )  THEN
167                message_string = 'non-cyclic lateral boundaries along y do' // &
168                                 ' not & allow calculation of spectra along y' 
169                CALL message( 'calc_spectra', 'PA0162', 1, 2, 0, 6, 0 )
170             ENDIF
171             CALL local_stop
172          ENDIF
173
174          CALL preprocess_spectra( m, pr )
175
176#if defined( __parallel )
177          CALL transpose_zyd( d, d )
178          CALL calc_spectra_y( d, pr, m )
179#else
180          message_string = 'sorry, calculation of spectra in non parallel' //  &
181                           'mode& is still not realized'
182          CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 )
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 )
204!------------------------------------------------------------------------------!
205! Description:
206! ------------
207!> @todo Missing subroutine description.
208!------------------------------------------------------------------------------!
209 SUBROUTINE preprocess_spectra( m, pr )
210
211    USE arrays_3d,                                                             &
212        ONLY:  d, pt, q, u, v, w
213
214    USE indices,                                                               &
215        ONLY:  ngp_2dh, nxl, nxr, nyn, nys, nzb, nzt
216
217    USE kinds
218
219    USE pegrid
220
221    USE spectrum,                                                              &
222        ONLY:  data_output_sp
223
224    USE statistics,                                                            &
225        ONLY:  hom, var_d
226
227
228    IMPLICIT NONE
229
230    INTEGER(iwp) :: i  !<
231    INTEGER(iwp) :: j  !<
232    INTEGER(iwp) :: k  !<
233    INTEGER(iwp) :: m  !<
234    INTEGER(iwp) :: pr !<
235
236    REAL(wp), DIMENSION(nzb:nzt+1) :: var_d_l
237
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
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 )
267         
268    END SELECT
269
270!
271!-- Subtract horizontal mean from the array, for which spectra have to be
272!-- calculated
273    var_d_l(:) = 0.0_wp
274    DO  i = nxl, nxr
275       DO  j = nys, nyn
276          DO  k = nzb+1, nzt
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)
279          ENDDO
280       ENDDO
281    ENDDO
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)
293
294 END SUBROUTINE preprocess_spectra
295
296
297!------------------------------------------------------------------------------!
298! Description:
299! ------------
300!> @todo Missing subroutine description.
301!------------------------------------------------------------------------------!
302 SUBROUTINE calc_spectra_x( ddd, pr, m )
303
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,                                                               &
317        ONLY:  nx, ny
318
319    USE kinds
320
321    USE pegrid
322
323    USE spectrum,                                                              &
324        ONLY:  comp_spectra_level, n_sp_x
325
326    USE statistics,                                                            &
327        ONLY:  spectrum_x, var_d
328
329    USE transpose_indices,                                                     &
330        ONLY:  nyn_x, nys_x, nzb_x, nzt_x
331
332
333    IMPLICIT NONE
334
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        !<
342
343    REAL(wp) ::  exponent     !<
344    REAL(wp) ::  sum_spec_dum !< wavenumber-integrated spectrum
345   
346    REAL(wp), DIMENSION(0:nx) ::  work !<
347   
348    REAL(wp), DIMENSION(0:nx/2) ::  sums_spectra_l !<
349   
350    REAL(wp), DIMENSION(0:nx/2,100) ::  sums_spectra !<
351   
352    REAL(wp), DIMENSION(0:nx,nys_x:nyn_x,nzb_x:nzt_x) ::  ddd !<
353
354!
355!-- Exponent for geometric average
356    exponent = 1.0_wp / ( ny + 1.0_wp )
357
358!
359!-- Loop over all levels defined by the user
360    n = 1
361    DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
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)
372             CALL fft_x_1d( work, 'forward' )
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
387             sums_spectra_l(i) = 1.0_wp
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
396          sums_spectra_l = 1.0_wp
397
398       ENDIF
399
400!
401!--    Global sum of spectra on PE0 (from where they are written on file)
402       sums_spectra(:,n) = 0.0_wp
403#if defined( __parallel )   
404       CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
405       CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), nx/2+1,          &
406                        MPI_REAL, MPI_PROD, 0, comm2d, ierr )
407#else
408       sums_spectra(:,n) = sums_spectra_l
409#endif
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
416       n = n + 1
417
418    ENDDO
419    n = n - 1
420
421    IF ( myid == 0 )  THEN
422!
423!--    Sum of spectra for later averaging (see routine data_output_spectra)
424       DO  i = 1, nx/2
425          DO k = 1, n
426             spectrum_x(i,k,m) = spectrum_x(i,k,m) + sums_spectra(i,k)
427          ENDDO
428       ENDDO
429
430    ENDIF
431!
432!-- n_sp_x is needed by data_output_spectra_x
433    n_sp_x = n
434
435 END SUBROUTINE calc_spectra_x
436
437
438!------------------------------------------------------------------------------!
439! Description:
440! ------------
441!> @todo Missing subroutine description.
442!------------------------------------------------------------------------------!
443 SUBROUTINE calc_spectra_y( ddd, pr, m )
444
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,                                                               &
458        ONLY:  nx, ny
459
460    USE kinds
461
462    USE pegrid
463
464    USE spectrum,                                                              &
465        ONLY:  comp_spectra_level, n_sp_y
466
467    USE statistics,                                                            &
468        ONLY:  spectrum_y, var_d
469
470    USE transpose_indices,                                                     &
471        ONLY:  nxl_yd, nxr_yd, nzb_yd, nzt_yd
472
473
474    IMPLICIT NONE
475
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        !<
483
484    REAL(wp) ::  exponent !<
485    REAL(wp) ::  sum_spec_dum !< wavenumber-integrated spectrum
486   
487    REAL(wp), DIMENSION(0:ny) ::  work !<
488   
489    REAL(wp), DIMENSION(0:ny/2) ::  sums_spectra_l !<
490   
491    REAL(wp), DIMENSION(0:ny/2,100) ::  sums_spectra !<
492   
493    REAL(wp), DIMENSION(0:ny,nxl_yd:nxr_yd,nzb_yd:nzt_yd) :: ddd !<
494
495
496!
497!-- Exponent for geometric average
498    exponent = 1.0_wp / ( nx + 1.0_wp )
499
500!
501!-- Loop over all levels defined by the user
502    n = 1
503    DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
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)
514             CALL fft_y_1d( work, 'forward' )
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
529             sums_spectra_l(j) = 1.0_wp
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
538          sums_spectra_l = 1.0_wp
539
540       ENDIF
541
542!
543!--    Global sum of spectra on PE0 (from where they are written on file)
544       sums_spectra(:,n) = 0.0_wp
545#if defined( __parallel )   
546       CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
547       CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), ny/2+1,          &
548                        MPI_REAL, MPI_PROD, 0, comm2d, ierr )
549#else
550       sums_spectra(:,n) = sums_spectra_l
551#endif
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
558       n = n + 1
559
560    ENDDO
561    n = n - 1
562
563
564    IF ( myid == 0 )  THEN
565!
566!--    Sum of spectra for later averaging (see routine data_output_spectra)
567       DO  j = 1, ny/2
568          DO k = 1, n
569             spectrum_y(j,k,m) = spectrum_y(j,k,m) + sums_spectra(j,k)
570          ENDDO
571       ENDDO
572
573    ENDIF
574
575!
576!-- n_sp_y is needed by data_output_spectra_y
577    n_sp_y = n
578
579 END SUBROUTINE calc_spectra_y
580#endif
Note: See TracBrowser for help on using the repository browser.