source: palm/tags/release-5.0/SOURCE/init_masks.f90 @ 4418

Last change on this file since 4418 was 2696, checked in by kanani, 6 years ago

Merge of branch palm4u into trunk

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