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

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

last commit documented

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