source: palm/trunk/SOURCE/init_masks.f90 @ 2014

Last change on this file since 2014 was 2001, checked in by knoop, 8 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 28.9 KB
Line 
1!> @file init_masks.f90
2!------------------------------------------------------------------------------!
3! This file is part of PALM.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! 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-2016 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: init_masks.f90 2001 2016-08-20 18:41:22Z suehring $
27!
28! 2000 2016-08-20 18:09:15Z knoop
29! Forced header and separation lines into 80 columns
30!
31! 1822 2016-04-07 07:49:42Z hoffmann
32! icloud_scheme replaced by microphysics_*
33!
34! 1804 2016-04-05 16:30:18Z maronga
35! Removed code for parameter file check (__check)
36!
37! 1783 2016-03-06 18:36:17Z raasch
38! netcdf module name changed + related changes
39!
40! 1682 2015-10-07 23:56:08Z knoop
41! Code annotations made doxygen readable
42!
43! 1438 2014-07-22 14:14:06Z heinze
44! +nr, qc, qr
45!
46! 1414 2014-05-31 11:19:48Z gronemeier
47! Bugfix: first and last grid points as they appear in 3D volume data can now
48! be included to masked data output
49!
50! 1353 2014-04-08 15:21:23Z heinze
51! REAL constants provided with KIND-attribute
52!
53! 1324 2014-03-21 09:13:16Z suehring
54! Bugfix: ONLY statement for module netcdf_control removed
55!
56! 1320 2014-03-20 08:40:49Z raasch
57! ONLY-attribute added to USE-statements,
58! kind-parameters added to all INTEGER and REAL declaration statements,
59! kinds are defined in new module kinds,
60! revision history before 2012 removed,
61! comment fields (!:) to be used for variable explanations added to
62! all variable declaration statements
63!
64! 1186 2013-06-18 06:22:52Z raasch
65! bugfix: 0.0 replaced by zu(nzb) as the lowest default height level for masks,
66! because a zero value does not work in case of ocean runs
67!
68! 1036 2012-10-22 13:43:42Z raasch
69! code put under GPL (PALM 3.9)
70!
71! 1032 2012-10-21 13:03:21Z letzel
72! mask locations determined based on scalar positions
73!
74! 1031 2012-10-19 14:35:30Z raasch
75! netCDF4 without parallel file support implemented
76!
77! 996 2012-09-07 10:41:47Z raasch
78! little reformatting
79!
80! 978 2012-08-09 08:28:32Z fricke
81! +z0h*
82!
83! 809 2012-01-30 13:32:58Z maronga
84! Bugfix: replaced .AND. and .NOT. with && and ! in the preprocessor directives
85!
86! 807 2012-01-25 11:53:51Z maronga
87! New cpp directive "__check" implemented which is used by check_namelist_files
88!
89! 410 2009-12-04 17:05:40Z letzel
90! Initial revision
91!
92!
93! Description:
94! ------------
95!> Initialize masked data output
96!------------------------------------------------------------------------------!
97 SUBROUTINE init_masks
98
99    USE arrays_3d,                                                             &
100        ONLY:  zu, zw
101
102    USE control_parameters,                                                    &
103        ONLY:  constant_diffusion, cloud_droplets, cloud_physics,              &
104               data_output_masks, data_output_masks_user,                      &
105               doav, doav_n, domask, domask_no, dz, dz_stretch_level, humidity,&
106               mask, masks, mask_scale, mask_i,                                &
107               mask_i_global, mask_j, mask_j_global, mask_k, mask_k_global,    &
108               mask_loop, mask_size, mask_size_l, mask_start_l, mask_x,        &
109               mask_x_loop, mask_xyz_dimension, mask_y, mask_y_loop, mask_z,   &
110               mask_z_loop, max_masks,  message_string, mid,                   &
111               microphysics_seifert, passive_scalar, ocean
112
113    USE grid_variables,                                                        &
114        ONLY:  dx, dy
115
116    USE indices,                                                               &
117        ONLY:  nx, nxl, nxr, ny, nyn, nys, nz, nzb, nzt
118
119    USE kinds
120
121    USE netcdf_interface,                                                      &
122        ONLY:  domask_unit, netcdf_data_format
123
124    USE particle_attributes,                                                   &
125        ONLY:  particle_advection
126
127    USE pegrid
128
129    IMPLICIT NONE
130
131    CHARACTER (LEN=6) ::  var  !<
132    CHARACTER (LEN=7) ::  unit !<
133   
134    CHARACTER (LEN=10), DIMENSION(max_masks,100) ::  do_mask      !<
135    CHARACTER (LEN=10), DIMENSION(max_masks,100) ::  do_mask_user !<
136
137    INTEGER(iwp) ::  i            !<
138    INTEGER(iwp) ::  ilen         !<
139    INTEGER(iwp) ::  ind(6)       !<
140    INTEGER(iwp) ::  ind_array(1) !<
141    INTEGER(iwp) ::  j            !<
142    INTEGER(iwp) ::  k            !<
143    INTEGER(iwp) ::  n            !<
144    INTEGER(iwp) ::  sender       !<
145   
146    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  tmp_array !<
147
148    LOGICAL ::  found !<
149!
150!-- Allocation and initialization
151    ALLOCATE( tmp_array( MAX(nx,ny,nz)+2 ) )
152
153    ALLOCATE( mask_i(max_masks,nxr-nxl+2), &
154              mask_j(max_masks,nyn-nys+2), &
155              mask_k(max_masks,nzt-nzb+2) )
156!
157!-- internal mask arrays ("mask,dimension,selection")
158    ALLOCATE( mask(max_masks,3,mask_xyz_dimension), &
159              mask_loop(max_masks,3,3) )
160   
161!
162!-- Parallel mask output not yet tested
163    IF ( netcdf_data_format > 4 )  THEN
164       message_string = 'netCDF file formats '//                         &
165                        '3 (netCDF 4) and 4 (netCDF 4 Classic model)'//  &
166                        '&are currently not supported (not yet tested).'
167       CALL message( 'init_masks', 'PA0328', 1, 2, 0, 6, 0 )
168    ENDIF
169
170!
171!-- Store data output parameters for masked data output in few shared arrays
172    DO  mid = 1, masks
173   
174       do_mask     (mid,:) = data_output_masks(mid,:)
175       do_mask_user(mid,:) = data_output_masks_user(mid,:)
176       mask      (mid,1,:) = mask_x(mid,:) 
177       mask      (mid,2,:) = mask_y(mid,:)
178       mask      (mid,3,:) = mask_z(mid,:) 
179       
180       IF ( mask_x_loop(mid,1) == -1.0_wp  .AND.  mask_x_loop(mid,2) == -1.0_wp&
181            .AND.  mask_x_loop(mid,3) == -1.0_wp )  THEN
182          mask_loop(mid,1,1:2) = -1.0_wp
183          mask_loop(mid,1,3)   =  0.0_wp
184       ELSE
185          mask_loop(mid,1,:) = mask_x_loop(mid,:)
186       ENDIF
187       IF ( mask_y_loop(mid,1) == -1.0_wp  .AND.  mask_y_loop(mid,2) == -1.0_wp&
188            .AND.  mask_y_loop(mid,3) == -1.0_wp )  THEN
189          mask_loop(mid,2,1:2) = -1.0_wp
190          mask_loop(mid,2,3)   =  0.0_wp
191       ELSE
192          mask_loop(mid,2,:) = mask_y_loop(mid,:)
193       ENDIF
194       IF ( mask_z_loop(mid,1) == -1.0_wp  .AND.  mask_z_loop(mid,2) == -1.0_wp&
195            .AND.  mask_z_loop(mid,3) == -1.0_wp )  THEN
196          mask_loop(mid,3,1:2) = -1.0_wp
197          mask_loop(mid,3,3)   =  0.0_wp
198       ELSE
199          mask_loop(mid,3,:) = mask_z_loop(mid,:)
200       ENDIF
201       
202    ENDDO
203   
204    mask_i = -1; mask_j = -1; mask_k = -1
205   
206!
207!-- Global arrays are required by define_netcdf_header.
208    IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
209       ALLOCATE( mask_i_global(max_masks,nx+2), &
210                 mask_j_global(max_masks,ny+2), &
211                 mask_k_global(max_masks,nz+2) )
212       mask_i_global = -1; mask_j_global = -1; mask_k_global = -1
213    ENDIF
214
215!
216!-- Determine variable names for each mask
217    DO  mid = 1, masks
218!
219!--    Append user-defined data output variables to the standard data output
220       IF ( do_mask_user(mid,1) /= ' ' )  THEN
221          i = 1
222          DO  WHILE ( do_mask(mid,i) /= ' '  .AND.  i <= 100 )
223             i = i + 1
224          ENDDO
225          j = 1
226          DO  WHILE ( do_mask_user(mid,j) /= ' '  .AND.  j <= 100 )
227             IF ( i > 100 )  THEN
228                WRITE ( message_string, * ) 'number of output quantitities ', &
229                     'given by data_output_mask and data_output_mask_user ', &
230                     'exceeds the limit of 100'
231                CALL message( 'init_masks', 'PA0329', 1, 2, 0, 6, 0 )
232             ENDIF
233             do_mask(mid,i) = do_mask_user(mid,j)
234             i = i + 1
235             j = j + 1
236          ENDDO
237       ENDIF
238
239!
240!--    Check and set steering parameters for mask data output and averaging
241       i   = 1
242       DO WHILE ( do_mask(mid,i) /= ' '  .AND.  i <= 100 )
243!
244!--       Check for data averaging
245          ilen = LEN_TRIM( do_mask(mid,i) )
246          j = 0                                              ! no data averaging
247          IF ( ilen > 3 )  THEN
248             IF ( do_mask(mid,i)(ilen-2:ilen) == '_av' )  THEN
249                j = 1                                           ! data averaging
250                do_mask(mid,i) = do_mask(mid,i)(1:ilen-3)
251             ENDIF
252          ENDIF
253          var = TRIM( do_mask(mid,i) )
254!
255!--       Check for allowed value and set units
256          SELECT CASE ( TRIM( var ) )
257
258             CASE ( 'e' )
259                IF ( constant_diffusion )  THEN
260                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
261                        '" requires constant_diffusion = .FALSE.'
262                   CALL message( 'init_masks', 'PA0103', 1, 2, 0, 6, 0 )
263                ENDIF
264                unit = 'm2/s2'
265
266             CASE ( 'lpt' )
267                IF ( .NOT. cloud_physics )  THEN
268                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
269                        '" requires cloud_physics = .TRUE.'
270                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
271                ENDIF
272                unit = 'K'
273
274             CASE ( 'nr' )
275                IF ( .NOT. cloud_physics )  THEN
276                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
277                        '" requires cloud_physics = .TRUE.'
278                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
279                 ELSEIF ( .NOT. microphysics_seifert ) THEN
280                   message_string = 'output of "' // TRIM( var ) // '" requi' //  &
281                         'res cloud_scheme = seifert_beheng'
282                   CALL message( 'check_parameters', 'PA0359', 1, 2, 0, 6, 0 )
283                ENDIF
284                unit = '1/m3'
285
286             CASE ( 'pc', 'pr' )
287                IF ( .NOT. particle_advection )  THEN
288                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
289                        '" requires a "particles_par"-NAMELIST in the ', &
290                        'parameter file (PARIN)'
291                   CALL message( 'init_masks', 'PA0104', 1, 2, 0, 6, 0 )
292                ENDIF
293                IF ( TRIM( var ) == 'pc' )  unit = 'number'
294                IF ( TRIM( var ) == 'pr' )  unit = 'm'
295
296             CASE ( 'q', 'vpt' )
297                IF ( .NOT. humidity )  THEN
298                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
299                        '" requires humidity = .TRUE.'
300                   CALL message( 'init_masks', 'PA0105', 1, 2, 0, 6, 0 )
301                ENDIF
302                IF ( TRIM( var ) == 'q'   )  unit = 'kg/kg'
303                IF ( TRIM( var ) == 'vpt' )  unit = 'K'
304
305             CASE ( 'qc' )
306                IF ( .NOT. cloud_physics )  THEN
307                   message_string = 'output of "' // TRIM( var ) // '" requi' //  &
308                            'res cloud_physics = .TRUE.'
309                   CALL message( 'check_parameters', 'PA0108', 1, 2, 0, 6, 0 )
310                ENDIF
311                unit = 'kg/kg'
312
313             CASE ( 'ql' )
314                IF ( .NOT. ( cloud_physics  .OR.  cloud_droplets ) )  THEN
315                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
316                        '" requires cloud_physics = .TRUE. or cloud_droplets', &
317                        ' = .TRUE.'
318                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
319                ENDIF
320                unit = 'kg/kg'
321
322             CASE ( 'ql_c', 'ql_v', 'ql_vp' )
323                IF ( .NOT. cloud_droplets )  THEN
324                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
325                        '" requires cloud_droplets = .TRUE.'
326                   CALL message( 'init_masks', 'PA0107', 1, 2, 0, 6, 0 )
327                ENDIF
328                IF ( TRIM( var ) == 'ql_c'  )  unit = 'kg/kg'
329                IF ( TRIM( var ) == 'ql_v'  )  unit = 'm3'
330                IF ( TRIM( var ) == 'ql_vp' )  unit = 'none'
331
332             CASE ( 'qv' )
333                IF ( .NOT. cloud_physics )  THEN
334                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
335                        '" requires cloud_physics = .TRUE.'
336                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
337                ENDIF
338                unit = 'kg/kg'
339
340             CASE ( 'qr' )
341                IF ( .NOT. cloud_physics )  THEN
342                   message_string = 'output of "' // TRIM( var ) // '" requi' //  &
343                            'res cloud_physics = .TRUE.'
344                   CALL message( 'check_parameters', 'PA0108', 1, 2, 0, 6, 0 )
345                ELSEIF ( .NOT. microphysics_seifert ) THEN
346                   message_string = 'output of "' // TRIM( var ) // '" requi' //  &
347                            'res cloud_scheme = seifert_beheng'
348                   CALL message( 'check_parameters', 'PA0359', 1, 2, 0, 6, 0 )
349                ENDIF
350                unit = 'kg/kg'
351
352             CASE ( 'rho' )
353                IF ( .NOT. ocean )  THEN
354                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
355                        '" requires ocean = .TRUE.'
356                   CALL message( 'init_masks', 'PA0109', 1, 2, 0, 6, 0 )
357                ENDIF
358                unit = 'kg/m3'
359
360             CASE ( 's' )
361                IF ( .NOT. passive_scalar )  THEN
362                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
363                        '" requires passive_scalar = .TRUE.'
364                   CALL message( 'init_masks', 'PA0110', 1, 2, 0, 6, 0 )
365                ENDIF
366                unit = 'conc'
367
368             CASE ( 'sa' )
369                IF ( .NOT. ocean )  THEN
370                   WRITE ( message_string, * ) 'output of "', TRIM( var ), &
371                        '" requires ocean = .TRUE.'
372                   CALL message( 'init_masks', 'PA0109', 1, 2, 0, 6, 0 )
373                ENDIF
374                unit = 'psu'
375
376             CASE ( 'u*', 't*', 'lwp*', 'pra*', 'prr*', 'z0*', 'z0h*' )
377                WRITE ( message_string, * ) 'illegal value for data_',&
378                     'output: "', TRIM( var ), '" is only allowed', &
379                     'for horizontal cross section'
380                CALL message( 'init_masks', 'PA0111', 1, 2, 0, 6, 0 )
381
382             CASE ( 'p', 'pt', 'u', 'v', 'w' )
383                IF ( TRIM( var ) == 'p'  )  unit = 'Pa'
384                IF ( TRIM( var ) == 'pt' )  unit = 'K'
385                IF ( TRIM( var ) == 'u'  )  unit = 'm/s'
386                IF ( TRIM( var ) == 'v'  )  unit = 'm/s'
387                IF ( TRIM( var ) == 'w'  )  unit = 'm/s'
388                CONTINUE
389
390             CASE DEFAULT
391                CALL user_check_data_output( var, unit )
392
393                IF ( unit == 'illegal' )  THEN
394                   IF ( do_mask_user(mid,1) /= ' ' )  THEN
395                      WRITE ( message_string, * ) 'illegal value for data_',&
396                           'output or data_output_user: "', &
397                           TRIM( do_mask(mid,i) ), '"'
398                      CALL message( 'init_masks', 'PA0114', 1, 2, 0, 6, 0 )
399                   ELSE
400                      WRITE ( message_string, * ) 'illegal value for data_',&
401                           'output: "', TRIM( do_mask(mid,i) ), '"'
402                      CALL message( 'init_masks', 'PA0330', 1, 2, 0, 6, 0 )
403                   ENDIF
404                ENDIF
405
406          END SELECT
407!
408!--       Set the internal steering parameters appropriately
409          domask_no(mid,j)                    = domask_no(mid,j) + 1
410          domask(mid,j,domask_no(mid,j))      = do_mask(mid,i)
411          domask_unit(mid,j,domask_no(mid,j)) = unit
412
413          IF ( j == 1 )  THEN
414!
415!--          Check, if variable is already subject to averaging
416             found = .FALSE.
417             DO  k = 1, doav_n
418                IF ( TRIM( doav(k) ) == TRIM( var ) )  found = .TRUE.
419             ENDDO
420
421             IF ( .NOT. found )  THEN
422                doav_n = doav_n + 1
423                doav(doav_n) = var
424             ENDIF
425          ENDIF
426
427          i = i + 1
428
429       ENDDO   ! do_mask(mid,i)
430    ENDDO   ! mid
431
432
433!
434!-- Determine mask locations for each mask
435    DO  mid = 1, masks
436!
437!--    Set local masks for each subdomain along all three dimensions
438       CALL set_mask_locations( 1, dx, 'dx', nx, 'nx', nxl, nxr )
439       CALL set_mask_locations( 2, dy, 'dy', ny, 'ny', nys, nyn )
440       CALL set_mask_locations( 3, dz, 'dz', nz, 'nz', nzb, nzt )
441!
442!--    Set global masks along all three dimensions (required by
443!--    define_netcdf_header).
444#if defined( __parallel )
445!
446!--    PE0 receives partial arrays from all processors of the respective mask
447!--    and outputs them. Here a barrier has to be set, because otherwise
448!--    "-MPI- FATAL: Remote protocol queue full" may occur.
449
450       CALL MPI_BARRIER( comm2d, ierr )
451
452       IF ( myid == 0 )  THEN
453!
454!--       Local arrays can be relocated directly.
455          mask_i_global(mid,mask_start_l(mid,1): &
456                       mask_start_l(mid,1)+mask_size_l(mid,1)-1) = &
457                       mask_i(mid,:mask_size_l(mid,1))
458          mask_j_global(mid,mask_start_l(mid,2): &
459                       mask_start_l(mid,2)+mask_size_l(mid,2)-1) = &
460                       mask_j(mid,:mask_size_l(mid,2))
461          mask_k_global(mid,mask_start_l(mid,3): &
462                       mask_start_l(mid,3)+mask_size_l(mid,3)-1) = &
463                       mask_k(mid,:mask_size_l(mid,3))
464!
465!--       Receive data from all other PEs.
466          DO  n = 1, numprocs-1
467!
468!--          Receive index limits first, then arrays.
469!--          Index limits are received in arbitrary order from the PEs.
470             CALL MPI_RECV( ind(1), 6, MPI_INTEGER, MPI_ANY_SOURCE, 0,  &
471                  comm2d, status, ierr )
472!
473!--          Not all PEs have data for the mask.
474             IF ( ind(1) /= -9999 )  THEN
475                sender = status(MPI_SOURCE)
476                CALL MPI_RECV( tmp_array(ind(1)), ind(2)-ind(1)+1,  &
477                               MPI_INTEGER, sender, 1, comm2d, status, ierr )
478                mask_i_global(mid,ind(1):ind(2)) = tmp_array(ind(1):ind(2))
479                CALL MPI_RECV( tmp_array(ind(3)), ind(4)-ind(3)+1,  &
480                               MPI_INTEGER, sender, 2, comm2d, status, ierr )
481                mask_j_global(mid,ind(3):ind(4)) = tmp_array(ind(3):ind(4))
482                CALL MPI_RECV( tmp_array(ind(5)), ind(6)-ind(5)+1,  &
483                               MPI_INTEGER, sender, 3, comm2d, status, ierr )
484                mask_k_global(mid,ind(5):ind(6)) = tmp_array(ind(5):ind(6))
485             ENDIF
486          ENDDO
487
488       ELSE
489!
490!--       If at least part of the mask resides on the PE, send the index limits
491!--       for the target array, otherwise send -9999 to PE0.
492          IF ( mask_size_l(mid,1) > 0  .AND.  mask_size_l(mid,2) > 0  .AND.  &
493               mask_size_l(mid,3) > 0  )  THEN
494             ind(1) = mask_start_l(mid,1)
495             ind(2) = mask_start_l(mid,1) + mask_size_l(mid,1) - 1
496             ind(3) = mask_start_l(mid,2)
497             ind(4) = mask_start_l(mid,2) + mask_size_l(mid,2) - 1
498             ind(5) = mask_start_l(mid,3)
499             ind(6) = mask_start_l(mid,3) + mask_size_l(mid,3) - 1
500          ELSE
501             ind(1) = -9999; ind(2) = -9999
502             ind(3) = -9999; ind(4) = -9999
503             ind(5) = -9999; ind(6) = -9999
504          ENDIF
505          CALL MPI_SEND( ind(1), 6, MPI_INTEGER, 0, 0, comm2d, ierr )
506!
507!--       If applicable, send data to PE0.
508          IF ( ind(1) /= -9999 )  THEN
509             tmp_array(:mask_size_l(mid,1)) = mask_i(mid,:mask_size_l(mid,1))
510             CALL MPI_SEND( tmp_array(1), mask_size_l(mid,1),  &
511                            MPI_INTEGER, 0, 1, comm2d, ierr )
512             tmp_array(:mask_size_l(mid,2)) = mask_j(mid,:mask_size_l(mid,2))
513             CALL MPI_SEND( tmp_array(1), mask_size_l(mid,2),  &
514                            MPI_INTEGER, 0, 2, comm2d, ierr )
515             tmp_array(:mask_size_l(mid,3)) = mask_k(mid,:mask_size_l(mid,3))
516             CALL MPI_SEND( tmp_array(1), mask_size_l(mid,3),  &
517                            MPI_INTEGER, 0, 3, comm2d, ierr )
518          ENDIF
519       ENDIF
520!
521!--    A barrier has to be set, because otherwise some PEs may proceed too fast
522!--    so that PE0 may receive wrong data on tag 0.
523       CALL MPI_BARRIER( comm2d, ierr )
524       
525       IF ( netcdf_data_format > 4 )  THEN
526         
527          CALL MPI_BCAST( mask_i_global(mid,:), nx+2, MPI_INTEGER, 0, comm2d, &
528                          ierr )
529          CALL MPI_BCAST( mask_j_global(mid,:), ny+2, MPI_INTEGER, 0, comm2d, &
530                          ierr )
531          CALL MPI_BCAST( mask_k_global(mid,:), nz+2, MPI_INTEGER, 0, comm2d, &
532                          ierr ) 
533     
534       ENDIF
535
536#else
537!
538!--    Local arrays can be relocated directly.
539       mask_i_global(mid,:) = mask_i(mid,:)
540       mask_j_global(mid,:) = mask_j(mid,:)
541       mask_k_global(mid,:) = mask_k(mid,:)
542#endif
543    ENDDO   ! mid
544
545    DEALLOCATE( tmp_array )
546!
547!-- Internal mask arrays cannot be deallocated on PE 0 because they are
548!-- required for header output on PE 0.
549    IF ( myid /= 0 )  DEALLOCATE( mask, mask_loop )
550
551 CONTAINS
552
553!------------------------------------------------------------------------------!
554! Description:
555! ------------
556!> Set local mask for each subdomain along 'dim' direction.
557!------------------------------------------------------------------------------!
558    SUBROUTINE set_mask_locations( dim, dxyz, dxyz_string, nxyz, nxyz_string, &
559                                   lb, ub )
560
561       IMPLICIT NONE
562
563       CHARACTER (LEN=2) ::  dxyz_string !<
564       CHARACTER (LEN=2) ::  nxyz_string !<
565       
566       INTEGER(iwp)  ::  count       !<
567       INTEGER(iwp)  ::  count_l     !<
568       INTEGER(iwp)  ::  dim         !<
569       INTEGER(iwp)  ::  m           !<
570       INTEGER(iwp)  ::  loop_begin  !<
571       INTEGER(iwp)  ::  loop_end    !<
572       INTEGER(iwp)  ::  loop_stride !<
573       INTEGER(iwp)  ::  lb          !<
574       INTEGER(iwp)  ::  nxyz        !<
575       INTEGER(iwp)  ::  ub          !<
576       
577       REAL(wp)      ::  dxyz  !<
578       REAL(wp)      ::  ddxyz !<
579       REAL(wp)      ::  tmp1  !<
580       REAL(wp)      ::  tmp2  !<
581
582       count = 0;  count_l = 0 
583       ddxyz = 1.0_wp / dxyz 
584       tmp1  = 0.0_wp
585       tmp2  = 0.0_wp
586
587       IF ( mask(mid,dim,1) >= 0.0_wp )  THEN
588!
589!--       use predefined mask_* array
590          DO  WHILE ( mask(mid,dim,count+1) >= 0.0_wp )
591             count = count + 1
592             IF ( dim == 1 .OR. dim == 2 )  THEN
593                m = NINT( mask(mid,dim,count) * mask_scale(dim) * ddxyz - 0.5_wp )
594                IF ( m < 0 )  m = 0  ! avoid negative values
595             ELSEIF ( dim == 3 )  THEN
596                ind_array =  &
597                     MINLOC( ABS( mask(mid,dim,count) * mask_scale(dim) - zu ) )
598                m = ind_array(1) - 1 + nzb  ! MINLOC uses lower array bound 1
599             ENDIF
600             IF ( m > (nxyz+1) )  THEN
601                WRITE ( message_string, '(I3,A,I3,A,I1,3A,I3)' )  &
602                     m,' in mask ',mid,' along dimension ',dim,  &
603                     ' exceeds (',nxyz_string,'+1) = ',nxyz+1
604                CALL message( 'init_masks', 'PA0331', 1, 2, 0, 6, 0 )
605             ENDIF
606             IF ( ( m >= lb .AND. m <= ub ) .OR.     &
607                  ( m == (nxyz+1) .AND. ub == nxyz )  )  THEN
608                IF ( count_l == 0 )  mask_start_l(mid,dim) = count
609                count_l = count_l + 1
610                IF ( dim == 1 )  THEN
611                   mask_i(mid,count_l) = m
612                ELSEIF ( dim == 2 )  THEN
613                   mask_j(mid,count_l) = m
614                ELSEIF ( dim == 3 )  THEN
615                   mask_k(mid,count_l) = m
616                ENDIF
617             ENDIF
618             IF ( count == mask_xyz_dimension )  EXIT
619          ENDDO
620          mask_size(mid,dim)   = count
621          mask_size_l(mid,dim) = count_l
622
623       ELSE
624!
625!--       use predefined mask_loop_* array, or use the default (all grid points
626!--       along this direction)
627          IF ( mask_loop(mid,dim,1) < 0.0_wp )  THEN
628             tmp1 = mask_loop(mid,dim,1)
629             mask_loop(mid,dim,1) = zw(nzb)  !   lowest level  (default)
630          ENDIF
631          IF ( dim == 1 .OR. dim == 2 )  THEN
632             IF ( mask_loop(mid,dim,2) < 0.0_wp )  THEN
633                tmp2 = mask_loop(mid,dim,2)
634                mask_loop(mid,dim,2) = (nxyz+1)*dxyz / mask_scale(dim)   ! (default)
635             ENDIF
636             IF ( MAXVAL( mask_loop(mid,dim,1:2) )  &
637                  > (nxyz+1) * dxyz / mask_scale(dim) )  THEN
638                WRITE ( message_string, '(2(A,I3,A,I1,A,F9.3),5A,I1,A,F9.3)' ) &
639                     'mask_loop(',mid,',',dim,',1)=',mask_loop(mid,dim,1), &
640                     ' and/or mask_loop(',mid,',',dim,',2)=', &
641                     mask_loop(mid,dim,2),'&exceed (', &
642                     nxyz_string,'+1)*',dxyz_string,'/mask_scale(',dim,')=', &
643                     (nxyz+1)*dxyz/mask_scale(dim)
644                CALL message( 'init_masks', 'PA0332', 1, 2, 0, 6, 0 )
645             ENDIF
646             loop_begin  = NINT( mask_loop(mid,dim,1) * mask_scale(dim) &
647                  * ddxyz - 0.5_wp )
648             loop_end    = NINT( mask_loop(mid,dim,2) * mask_scale(dim) &
649                  * ddxyz - 0.5_wp )
650             loop_stride = NINT( mask_loop(mid,dim,3) * mask_scale(dim) &
651                  * ddxyz )
652             IF ( loop_begin == -1 )  loop_begin = 0  ! avoid negative values
653          ELSEIF ( dim == 3 )  THEN
654             IF ( mask_loop(mid,dim,2) < 0.0_wp )  THEN
655                tmp2 = mask_loop(mid,dim,2)
656                mask_loop(mid,dim,2) = zu(nz+1) / mask_scale(dim)   ! (default)
657             ENDIF
658             IF ( MAXVAL( mask_loop(mid,dim,1:2) )  &
659                  > zu(nz+1) / mask_scale(dim) )  THEN
660                WRITE ( message_string, '(2(A,I3,A,I1,A,F9.3),A,I1,A,F9.3)' ) &
661                     'mask_loop(',mid,',',dim,',1)=',mask_loop(mid,dim,1), &
662                     ' and/or mask_loop(',mid,',',dim,',2)=', &
663                     mask_loop(mid,dim,2),'&exceed zu(nz+1)/mask_scale(',dim, &
664                     ')=',zu(nz+1)/mask_scale(dim)
665                CALL message( 'init_masks', 'PA0333', 1, 2, 0, 6, 0 )
666             ENDIF
667             ind_array =  &
668                  MINLOC( ABS( mask_loop(mid,dim,1) * mask_scale(dim) - zu ) )
669             loop_begin =  &
670                  ind_array(1) - 1 + nzb ! MINLOC uses lower array bound 1
671             ind_array =  &
672                  MINLOC( ABS( mask_loop(mid,dim,2) * mask_scale(dim) - zu ) )
673             loop_end = ind_array(1) - 1 + nzb ! MINLOC uses lower array bound 1
674!
675!--          The following line assumes a constant vertical grid spacing within
676!--          the vertical mask range; it fails for vertical grid stretching.
677!--          Maybe revise later. Issue warning but continue execution.
678             loop_stride = NINT( mask_loop(mid,dim,3) * mask_scale(dim) * ddxyz )
679
680             IF ( mask_loop(mid,dim,2) * mask_scale(dim) > dz_stretch_level )  &
681                  THEN
682                WRITE ( message_string, '(A,I3,A,I1,A,F9.3,A,F8.2,3A)' ) &
683                     'mask_loop(',mid,',',dim,',2)=', mask_loop(mid,dim,2),&
684                     ' exceeds dz_stretch_level=',dz_stretch_level, &
685                     '.&Vertical mask locations will not ', &
686                     'match the desired heights&within the stretching ', &
687                     'region. Recommendation: use mask instead of mask_loop.'
688                CALL message( 'init_masks', 'PA0334', 0, 1, 0, 6, 0 )
689             ENDIF
690
691          ENDIF
692!
693!--       If necessary, reset mask_loop(mid,dim,1) and mask_loop(mid,dim,2).
694          IF ( tmp1 < 0.0_wp )  mask_loop(mid,dim,1) = tmp1
695          IF ( tmp2 < 0.0_wp )  mask_loop(mid,dim,2) = tmp2
696!
697!--       The default stride +/-1 (every grid point) applies if
698!--       mask_loop(mid,dim,3) is not specified (its default is zero).
699          IF ( loop_stride == 0 )  THEN
700             IF ( loop_end >= loop_begin )  THEN
701                loop_stride =  1
702             ELSE
703                loop_stride = -1
704             ENDIF
705          ENDIF
706          DO  m = loop_begin, loop_end, loop_stride
707             count = count + 1
708             IF ( ( m >= lb  .AND.  m <= ub ) .OR.   &
709                  ( m == (nxyz+1) .AND. ub == nxyz )  )  THEN
710                IF ( count_l == 0 )  mask_start_l(mid,dim) = count
711                count_l = count_l + 1
712                IF ( dim == 1 )  THEN
713                   mask_i(mid,count_l) = m
714                ELSEIF ( dim == 2 )  THEN
715                   mask_j(mid,count_l) = m
716                ELSEIF ( dim == 3 )  THEN
717                   mask_k(mid,count_l) = m
718                ENDIF
719             ENDIF
720          ENDDO
721          mask_size(mid,dim)   = count
722          mask_size_l(mid,dim) = count_l
723
724       ENDIF
725
726    END SUBROUTINE set_mask_locations
727
728 END SUBROUTINE init_masks
Note: See TracBrowser for help on using the repository browser.