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

Last change on this file since 3274 was 3274, checked in by knoop, 6 years ago

Modularization of all bulk cloud physics code components

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