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

Last change on this file since 2296 was 2292, checked in by schwenkel, 7 years ago

implementation of new bulk microphysics scheme

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