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

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

last commit documented

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