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

Last change on this file since 1805 was 1805, checked in by maronga, 8 years ago

last commit documented

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