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

Last change on this file since 1342 was 1342, checked in by kanani, 10 years ago

REAL constants defined as wp-kind

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