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

Last change on this file since 1318 was 1318, checked in by raasch, 8 years ago

former files/routines cpu_log and cpu_statistics combined to one module,
which also includes the former data module cpulog from the modules-file,
module interfaces removed

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