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

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

overlapping execution of fft and transpositions (MPI_ALLTOALL), but real overlapping is not activated so far,
fftw implemented for 1D-decomposition
resorting of arrays moved to separate routines resort_for_...
bugfix in mbuild concerning Makefile_check

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