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

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

Forced header and separation lines into 80 columns

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