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

Last change on this file since 1003 was 1003, checked in by raasch, 12 years ago

subdomains must have identical size, i.e. grid_matching = "match" not allowed any more
parameter grid_matching removed
some obsolete variables removed

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