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

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

last commit documented

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