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

Last change on this file since 1225 was 1217, checked in by raasch, 11 years ago

last commit documented

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