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

Last change on this file since 1324 was 1324, checked in by suehring, 10 years ago

Bugfixes in ONLY statements

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