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

Last change on this file since 1004 was 1004, checked in by raasch, 9 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 10.9 KB
Line 
1 SUBROUTINE calc_spectra
2
3!------------------------------------------------------------------------------!
4! Current revisions:
5! -----------------
6!
7!
8! Former revisions:
9! -----------------
10! $Id: calc_spectra.f90 1004 2012-09-14 14:56:50Z raasch $
11!
12! 1003 2012-09-14 14:35:53Z raasch
13! adjustment of array tend for cases with unequal subdomain sizes removed
14!
15! 707 2011-03-29 11:39:40Z raasch
16! bc_lr/ns replaced by bc_lr/ns_cyc
17!
18! 667 2010-12-23 12:06:00Z suehring/gryschka
19! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng for allocation
20! of tend
21!
22! 274 2009-03-26 15:11:21Z heinze
23! Output of messages replaced by message handling routine
24!
25! 225 2009-01-26 14:44:20Z raasch
26! Bugfix: array d is reallocated in case that multigrid is used
27!
28! 192 2008-08-27 16:51:49Z letzel
29! bugfix in calc_spectra_x: exponent = 1.0 / ( ny + 1.0 )
30! allow 100 spectra levels instead of 10 for consistency with
31! define_netcdf_header
32! user-defined spectra, arguments removed from transpose routines
33!
34! February 2007
35! RCS Log replace by Id keyword, revision history cleaned up
36!
37! Revision 1.9  2006/04/11 14:56:00  raasch
38! pl_spectra renamed data_output_sp
39!
40! Revision 1.1  2001/01/05 15:08:07  raasch
41! Initial revision
42!
43!
44! Description:
45! ------------
46! Calculate horizontal spectra along x and y.
47! ATTENTION: 1d-decomposition along y still needs improvement, because in that
48!            case the gridpoint number along z still depends on the PE number
49!            because transpose_xz has to be used (and possibly also
50!            transpose_zyd needs modification).
51!------------------------------------------------------------------------------!
52
53#if defined( __spectra )
54    USE arrays_3d
55    USE control_parameters
56    USE cpulog
57    USE fft_xy
58    USE indices
59    USE interfaces
60    USE pegrid
61    USE spectrum
62
63    IMPLICIT NONE
64
65    INTEGER ::  m, pr
66
67
68    CALL cpu_log( log_point(30), 'calc_spectra', 'start' )
69
70!
71!-- Initialize ffts
72    CALL fft_init
73
74!
75!-- Reallocate array d in required size
76    IF ( psolver == 'multigrid' )  THEN
77       DEALLOCATE( d )
78       ALLOCATE( d(nzb+1:nzt,nys:nyn,nxl:nxr) )
79    ENDIF
80
81    m = 1
82    DO WHILE ( data_output_sp(m) /= ' '  .AND.  m <= 10 )
83!
84!--    Transposition from z --> x  ( y --> x in case of a 1d-decomposition
85!--    along x)
86       IF ( INDEX( spectra_direction(m), 'x' ) /= 0 )  THEN
87
88!
89!--       Calculation of spectra works for cyclic boundary conditions only
90          IF ( .NOT. bc_lr_cyc )  THEN
91
92             message_string = 'non-cyclic lateral boundaries along x do not'// &
93                              '& allow calculation of spectra along x'
94             CALL message( 'calc_spectra', 'PA0160', 1, 2, 0, 6, 0 )
95          ENDIF
96
97          CALL preprocess_spectra( m, pr )
98
99#if defined( __parallel )
100          IF ( pdims(2) /= 1 )  THEN
101             CALL transpose_zx( d, tend, d )
102          ELSE
103             CALL transpose_yxd( d, tend, d )
104          ENDIF
105          CALL calc_spectra_x( d, pr, m )
106#else
107          message_string = 'sorry, calculation of spectra in non parallel ' // &
108                           'mode& is still not realized'
109          CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 )     
110#endif
111
112       ENDIF
113
114!
115!--    Transposition from z --> y (d is rearranged only in case of a
116!--    1d-decomposition along x)
117       IF ( INDEX( spectra_direction(m), 'y' ) /= 0 )  THEN
118
119!
120!--       Calculation of spectra works for cyclic boundary conditions only
121          IF ( .NOT. bc_ns_cyc )  THEN
122             IF ( myid == 0 )  THEN
123                message_string = 'non-cyclic lateral boundaries along y do' // &
124                                 ' not & allow calculation of spectra along y' 
125                CALL message( 'calc_spectra', 'PA0162', 1, 2, 0, 6, 0 )
126             ENDIF
127             CALL local_stop
128          ENDIF
129
130          CALL preprocess_spectra( m, pr )
131
132#if defined( __parallel )
133          CALL transpose_zyd( d, tend, d )
134          CALL calc_spectra_y( d, pr, m )
135#else
136          message_string = 'sorry, calculation of spectra in non parallel' // &
137                           'mode& is still not realized'
138          CALL message( 'calc_spectra', 'PA0161', 1, 2, 0, 6, 0 )
139#endif
140
141       ENDIF
142
143!
144!--    Increase counter for next spectrum
145       m = m + 1
146         
147    ENDDO
148
149!
150!-- Increase counter for averaging process in routine plot_spectra
151    average_count_sp = average_count_sp + 1
152
153    CALL cpu_log( log_point(30), 'calc_spectra', 'stop' )
154
155#endif
156 END SUBROUTINE calc_spectra
157
158
159#if defined( __spectra )
160 SUBROUTINE preprocess_spectra( m, pr )
161
162    USE arrays_3d
163    USE indices
164    USE pegrid
165    USE spectrum
166    USE statistics
167
168    IMPLICIT NONE
169
170    INTEGER :: i, j, k, m, pr
171
172    SELECT CASE ( TRIM( data_output_sp(m) ) )
173         
174    CASE ( 'u' )
175       pr = 1
176       d(nzb+1:nzt,nys:nyn,nxl:nxr) = u(nzb+1:nzt,nys:nyn,nxl:nxr)
177       
178    CASE ( 'v' )
179       pr = 2
180       d(nzb+1:nzt,nys:nyn,nxl:nxr) = v(nzb+1:nzt,nys:nyn,nxl:nxr)
181       
182    CASE ( 'w' )
183       pr = 3
184       d(nzb+1:nzt,nys:nyn,nxl:nxr) = w(nzb+1:nzt,nys:nyn,nxl:nxr)
185       
186    CASE ( 'pt' )
187       pr = 4
188       d(nzb+1:nzt,nys:nyn,nxl:nxr) = pt(nzb+1:nzt,nys:nyn,nxl:nxr)
189       
190    CASE ( 'q' )
191       pr = 41
192       d(nzb+1:nzt,nys:nyn,nxl:nxr) = q(nzb+1:nzt,nys:nyn,nxl:nxr)
193       
194    CASE DEFAULT
195!
196!--    The DEFAULT case is reached either if the parameter data_output_sp(m)
197!--    contains a wrong character string or if the user has coded a special
198!--    case in the user interface. There, the subroutine user_spectra
199!--    checks which of these two conditions applies.
200       CALL user_spectra( 'preprocess', m, pr )
201         
202    END SELECT
203
204!
205!-- Subtract horizontal mean from the array, for which spectra have to be
206!-- calculated
207    DO  i = nxl, nxr
208       DO  j = nys, nyn
209          DO  k = nzb+1, nzt
210             d(k,j,i) = d(k,j,i) - sums(k,pr)
211          ENDDO
212       ENDDO
213    ENDDO
214
215 END SUBROUTINE preprocess_spectra
216
217
218 SUBROUTINE calc_spectra_x( ddd, pr, m )
219
220    USE arrays_3d
221    USE constants
222    USE control_parameters
223    USE fft_xy
224    USE grid_variables
225    USE indices
226    USE pegrid
227    USE spectrum
228    USE statistics
229    USE transpose_indices
230
231    IMPLICIT NONE
232
233    INTEGER                    ::  i, ishape(1), j, k, m, n, pr
234
235    REAL                       ::  fac, exponent
236    REAL, DIMENSION(0:nx)      ::  work
237    REAL, DIMENSION(0:nx/2)    ::  sums_spectra_l
238    REAL, DIMENSION(0:nx/2,100)::  sums_spectra
239
240    REAL, DIMENSION(0:nx,nys_x:nyn_x,nzb_x:nzt_x) ::  ddd
241
242!
243!-- Exponent for geometric average
244    exponent = 1.0 / ( ny + 1.0 )
245
246!
247!-- Loop over all levels defined by the user
248    n = 1
249    DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
250
251       k = comp_spectra_level(n)
252
253!
254!--    Calculate FFT only if the corresponding level is situated on this PE
255       IF ( k >= nzb_x  .AND.  k <= nzt_x )  THEN
256         
257          DO  j = nys_x, nyn_x
258
259             work = ddd(0:nx,j,k)
260             CALL fft_x( work, 'forward' )
261
262             ddd(0,j,k) = dx * work(0)**2
263             DO  i = 1, nx/2
264                ddd(i,j,k) = dx * ( work(i)**2 + work(nx+1-i)**2 )
265             ENDDO
266
267          ENDDO
268
269!
270!--       Local sum and geometric average of these spectra
271!--       (WARNING: no global sum should be performed, because floating
272!--       point overflow may occur)
273          DO  i = 0, nx/2
274
275             sums_spectra_l(i) = 1.0
276             DO  j = nys_x, nyn_x
277                sums_spectra_l(i) = sums_spectra_l(i) * ddd(i,j,k)**exponent
278             ENDDO
279
280          ENDDO
281         
282       ELSE
283
284          sums_spectra_l = 1.0
285
286       ENDIF
287
288!
289!--    Global sum of spectra on PE0 (from where they are written on file)
290       sums_spectra(:,n) = 0.0
291#if defined( __parallel )   
292       CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
293       CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), nx/2+1, &
294                        MPI_REAL, MPI_PROD, 0, comm2d, ierr )
295#else
296       sums_spectra(:,n) = sums_spectra_l
297#endif
298
299       n = n + 1
300
301    ENDDO
302    n = n - 1
303
304    IF ( myid == 0 )  THEN
305!
306!--    Sum of spectra for later averaging (see routine data_output_spectra)
307!--    Temperton fft results need to be normalized
308       IF ( fft_method == 'temperton-algorithm' )  THEN
309          fac = nx + 1.0
310       ELSE
311          fac = 1.0
312       ENDIF
313       DO  i = 1, nx/2
314          DO k = 1, n
315             spectrum_x(i,k,m) = spectrum_x(i,k,m) + sums_spectra(i,k) * fac
316          ENDDO
317       ENDDO
318
319    ENDIF
320
321!
322!-- n_sp_x is needed by data_output_spectra_x
323    n_sp_x = n
324
325 END SUBROUTINE calc_spectra_x
326
327
328 SUBROUTINE calc_spectra_y( ddd, pr, m )
329
330    USE arrays_3d
331    USE constants
332    USE control_parameters
333    USE fft_xy
334    USE grid_variables
335    USE indices
336    USE pegrid
337    USE spectrum
338    USE statistics
339    USE transpose_indices
340
341    IMPLICIT NONE
342
343    INTEGER :: i, j, jshape(1), k, m, n, pr
344
345    REAL                       ::  fac, exponent
346    REAL, DIMENSION(0:ny)      ::  work
347    REAL, DIMENSION(0:ny/2)    ::  sums_spectra_l
348    REAL, DIMENSION(0:ny/2,100)::  sums_spectra
349
350    REAL, DIMENSION(0:ny,nxl_yd:nxr_yd,nzb_yd:nzt_yd) :: ddd
351
352
353!
354!-- Exponent for geometric average
355    exponent = 1.0 / ( nx + 1.0 )
356
357!
358!-- Loop over all levels defined by the user
359    n = 1
360    DO WHILE ( comp_spectra_level(n) /= 999999  .AND.  n <= 100 )
361
362       k = comp_spectra_level(n)
363
364!
365!--    Calculate FFT only if the corresponding level is situated on this PE
366       IF ( k >= nzb_yd  .AND.  k <= nzt_yd )  THEN
367         
368          DO  i = nxl_yd, nxr_yd
369
370             work = ddd(0:ny,i,k)
371             CALL fft_y( work, 'forward' )
372
373             ddd(0,i,k) = dy * work(0)**2
374             DO  j = 1, ny/2
375                ddd(j,i,k) = dy * ( work(j)**2 + work(ny+1-j)**2 )
376             ENDDO
377
378          ENDDO
379
380!
381!--       Local sum and geometric average of these spectra
382!--       (WARNING: no global sum should be performed, because floating
383!--       point overflow may occur)
384          DO  j = 0, ny/2
385
386             sums_spectra_l(j) = 1.0
387             DO  i = nxl_yd, nxr_yd
388                sums_spectra_l(j) = sums_spectra_l(j) * ddd(j,i,k)**exponent
389             ENDDO
390
391          ENDDO
392         
393       ELSE
394
395          sums_spectra_l = 1.0
396
397       ENDIF
398
399!
400!--    Global sum of spectra on PE0 (from where they are written on file)
401       sums_spectra(:,n) = 0.0
402#if defined( __parallel )   
403       CALL MPI_BARRIER( comm2d, ierr )  ! Necessary?
404       CALL MPI_REDUCE( sums_spectra_l(0), sums_spectra(0,n), ny/2+1, &
405                        MPI_REAL, MPI_PROD, 0, comm2d, ierr )
406#else
407       sums_spectra(:,n) = sums_spectra_l
408#endif
409
410       n = n + 1
411
412    ENDDO
413    n = n - 1
414
415
416    IF ( myid == 0 )  THEN
417!
418!--    Sum of spectra for later averaging (see routine data_output_spectra)
419!--    Temperton fft results need to be normalized
420       IF ( fft_method == 'temperton-algorithm' )  THEN
421          fac = ny + 1.0
422       ELSE
423          fac = 1.0
424       ENDIF
425       DO  j = 1, ny/2
426          DO k = 1, n
427             spectrum_y(j,k,m) = spectrum_y(j,k,m) + sums_spectra(j,k) * fac
428          ENDDO
429       ENDDO
430
431    ENDIF
432
433!
434!-- n_sp_y is needed by data_output_spectra_y
435    n_sp_y = n
436
437 END SUBROUTINE calc_spectra_y
438#endif
Note: See TracBrowser for help on using the repository browser.