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

Last change on this file since 1321 was 1321, checked in by raasch, 10 years ago

last commit documented

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