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
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-2018 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: init_masks.f90 3274 2018-09-24 15:42:55Z knoop $
27! Modularization of all bulk cloud physics code components
28!
29! 3065 2018-06-12 07:03:02Z Giersch
30! dz_stretch_level was replaced by dz_stretch_level_start
31!
32! 3049 2018-05-29 13:52:36Z Giersch
33! Error messages revised
34!
35! 3045 2018-05-28 07:55:41Z Giersch
36! Error messages revised
37!
38! 2718 2018-01-02 08:49:38Z maronga
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
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
49! Bugfix: set variable name length to global value
50!
51!
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
57! Changed error message
58!
59! 2101 2017-01-05 16:42:31Z suehring
60!
61! 2031 2016-10-21 15:11:58Z knoop
62! renamed variable rho to rho_ocean
63!
64! 2000 2016-08-20 18:09:15Z knoop
65! Forced header and separation lines into 80 columns
66!
67! 1822 2016-04-07 07:49:42Z hoffmann
68! icloud_scheme replaced by microphysics_*
69!
70! 1804 2016-04-05 16:30:18Z maronga
71! Removed code for parameter file check (__check)
72!
73! 1783 2016-03-06 18:36:17Z raasch
74! netcdf module name changed + related changes
75!
76! 1682 2015-10-07 23:56:08Z knoop
77! Code annotations made doxygen readable
78!
79! 1438 2014-07-22 14:14:06Z heinze
80! +nr, qc, qr
81!
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!
86! 1353 2014-04-08 15:21:23Z heinze
87! REAL constants provided with KIND-attribute
88!
89! 1324 2014-03-21 09:13:16Z suehring
90! Bugfix: ONLY statement for module netcdf_control removed
91!
92! 1320 2014-03-20 08:40:49Z raasch
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
99!
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!
104! 1036 2012-10-22 13:43:42Z raasch
105! code put under GPL (PALM 3.9)
106!
107! 1032 2012-10-21 13:03:21Z letzel
108! mask locations determined based on scalar positions
109!
110! 1031 2012-10-19 14:35:30Z raasch
111! netCDF4 without parallel file support implemented
112!
113! 996 2012-09-07 10:41:47Z raasch
114! little reformatting
115!
116! 978 2012-08-09 08:28:32Z fricke
117! +z0h*
118!
119! 809 2012-01-30 13:32:58Z maronga
120! Bugfix: replaced .AND. and .NOT. with && and ! in the preprocessor directives
121!
122! 807 2012-01-25 11:53:51Z maronga
123! New cpp directive "__check" implemented which is used by check_namelist_files
124!
125! 410 2009-12-04 17:05:40Z letzel
126! Initial revision
127!
128!
129! Description:
130! ------------
131!> Initialize masked data output
132!------------------------------------------------------------------------------!
133 SUBROUTINE init_masks
134
135    USE arrays_3d,                                                             &
136        ONLY:  zu, zw
137
138    USE control_parameters,                                                    &
139        ONLY:  constant_diffusion, cloud_droplets,                             &
140               data_output_masks, data_output_masks_user,                      &
141               doav, doav_n, domask, domask_no, dz, dz_stretch_level_start,    &
142               humidity, mask, masks, mask_scale, mask_i,                      &
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,                   &
147               passive_scalar, ocean, varnamelength
148
149
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
158    USE bulk_cloud_model_mod,                                                  &
159        ONLY: bulk_cloud_model, microphysics_morrison, microphysics_seifert
160
161    USE netcdf_interface,                                                      &
162        ONLY:  domask_unit, netcdf_data_format
163
164    USE particle_attributes,                                                   &
165        ONLY:  particle_advection
166
167    USE pegrid
168
169    IMPLICIT NONE
170
171    CHARACTER (LEN=varnamelength) ::  var  !<
172    CHARACTER (LEN=7) ::  unit !<
173   
174    CHARACTER (LEN=varnamelength), DIMENSION(max_masks,100) ::  do_mask      !<
175    CHARACTER (LEN=varnamelength), DIMENSION(max_masks,100) ::  do_mask_user !<
176
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       !<
185   
186    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  tmp_array !<
187
188    LOGICAL ::  found !<
189
190!
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!
197!-- Allocation and initialization
198    ALLOCATE( tmp_array( MAX(nx,ny,nz)+2 ) )
199
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) )
203!
204!-- internal mask arrays ("mask,dimension,selection")
205    ALLOCATE( mask(max_masks,3,mask_xyz_dimension), &
206              mask_loop(max_masks,3,3) )
207   
208!
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.
212    IF ( netcdf_data_format > 4 )  THEN
213       message_string = 'netCDF file formats '//                               &
214                        '5 and 6 (with parallel I/O support)'//                &
215                        ' are currently not supported.'
216       CALL message( 'init_masks', 'PA0328', 1, 2, 0, 6, 0 )
217    ENDIF
218
219!
220!-- Store data output parameters for masked data output in few shared arrays
221    DO  mid = 1, masks
222   
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,:) 
228       
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
233       ELSE
234          mask_loop(mid,1,:) = mask_x_loop(mid,:)
235       ENDIF
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
240       ELSE
241          mask_loop(mid,2,:) = mask_y_loop(mid,:)
242       ENDIF
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
247       ELSE
248          mask_loop(mid,3,:) = mask_z_loop(mid,:)
249       ENDIF
250       
251    ENDDO
252   
253    mask_i = -1; mask_j = -1; mask_k = -1
254   
255!
256!-- Global arrays are required by define_netcdf_header.
257    IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
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) )
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
277                WRITE ( message_string, * ) 'number of output quantitities ',  &
278                     'given by data_output_mask and data_output_mask_user ',   &
279                     'exceeds the limit of 100'
280                CALL message( 'init_masks', 'PA0329', 1, 2, 0, 6, 0 )
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
291       DO WHILE ( do_mask(mid,i) /= ' '  .AND.  i <= 100 )
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
309                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
310                        '" requires constant_diffusion = .FALSE.'
311                   CALL message( 'init_masks', 'PA0103', 1, 2, 0, 6, 0 )
312                ENDIF
313                unit = 'm2/s2'
314
315             CASE ( 'lpt' )
316                IF ( .NOT. bulk_cloud_model )  THEN
317                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
318                        '" requires bulk_cloud_model = .TRUE.'
319                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
320                ENDIF
321                unit = 'K'
322
323             CASE ( 'nc' )
324                IF ( .NOT. bulk_cloud_model )  THEN
325                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
326                        '" requires bulk_cloud_model = .TRUE.'
327                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
328                 ELSEIF ( .NOT. microphysics_morrison ) THEN
329                   message_string = 'output of "' // TRIM( var ) // '" ' //    &
330                         'requires  = morrison'
331                   CALL message( 'check_parameters', 'PA0359', 1, 2, 0, 6, 0 )
332                ENDIF
333                unit = '1/m3'
334
335             CASE ( 'nr' )
336                IF ( .NOT. bulk_cloud_model )  THEN
337                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
338                        '" requires bulk_cloud_model = .TRUE.'
339                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
340                 ELSEIF ( .NOT. microphysics_seifert ) THEN
341                   message_string = 'output of "' // TRIM( var ) // '"' //     &
342                         'requires cloud_scheme = seifert_beheng'
343                   CALL message( 'check_parameters', 'PA0359', 1, 2, 0, 6, 0 )
344                ENDIF
345                unit = '1/m3'
346
347             CASE ( 'pc', 'pr' )
348                IF ( .NOT. particle_advection )  THEN
349                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
350                        '" requires a "particles_par"-NAMELIST in the ',       &
351                        'parameter file (PARIN)'
352                   CALL message( 'init_masks', 'PA0104', 1, 2, 0, 6, 0 )
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
359                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
360                        '" requires humidity = .TRUE.'
361                   CALL message( 'init_masks', 'PA0105', 1, 2, 0, 6, 0 )
362                ENDIF
363                IF ( TRIM( var ) == 'q'   )  unit = 'kg/kg'
364                IF ( TRIM( var ) == 'vpt' )  unit = 'K'
365
366             CASE ( 'qc' )
367                IF ( .NOT. bulk_cloud_model )  THEN
368                   message_string = 'output of "' // TRIM( var ) // '"' //     &
369                            'requires bulk_cloud_model = .TRUE.'
370                   CALL message( 'check_parameters', 'PA0108', 1, 2, 0, 6, 0 )
371                ENDIF
372                unit = 'kg/kg'
373
374             CASE ( 'ql' )
375                IF ( .NOT. ( bulk_cloud_model  .OR.  cloud_droplets ) )  THEN
376                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
377                        '" requires bulk_cloud_model = .TRUE. or ',            &
378                        'cloud_droplets = .TRUE.'
379                   CALL message( 'init_masks', 'PA0106', 1, 2, 0, 6, 0 )
380                ENDIF
381                unit = 'kg/kg'
382
383             CASE ( 'ql_c', 'ql_v', 'ql_vp' )
384                IF ( .NOT. cloud_droplets )  THEN
385                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
386                        '" requires cloud_droplets = .TRUE.'
387                   CALL message( 'init_masks', 'PA0107', 1, 2, 0, 6, 0 )
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' )
394                IF ( .NOT. bulk_cloud_model )  THEN
395                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
396                        '" requires bulk_cloud_model = .TRUE.'
397                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
398                ENDIF
399                unit = 'kg/kg'
400
401             CASE ( 'qr' )
402                IF ( .NOT. bulk_cloud_model )  THEN
403                   message_string = 'output of "' // TRIM( var ) // '" ' //    &
404                            'requires bulk_cloud_model = .TRUE.'
405                   CALL message( 'check_parameters', 'PA0108', 1, 2, 0, 6, 0 )
406                ELSEIF ( .NOT. microphysics_seifert ) THEN
407                   message_string = 'output of "' // TRIM( var ) // '" ' //    &
408                            'requires cloud_scheme = seifert_beheng'
409                   CALL message( 'check_parameters', 'PA0359', 1, 2, 0, 6, 0 )
410                ENDIF
411                unit = 'kg/kg'
412
413             CASE ( 'rho_ocean' )
414                IF ( .NOT. ocean )  THEN
415                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
416                        '" requires ocean = .TRUE.'
417                   CALL message( 'init_masks', 'PA0109', 1, 2, 0, 6, 0 )
418                ENDIF
419                unit = 'kg/m3'
420
421             CASE ( 's' )
422                IF ( .NOT. passive_scalar )  THEN
423                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
424                        '" requires passive_scalar = .TRUE.'
425                   CALL message( 'init_masks', 'PA0110', 1, 2, 0, 6, 0 )
426                ENDIF
427                unit = 'conc'
428
429             CASE ( 'sa' )
430                IF ( .NOT. ocean )  THEN
431                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
432                        '" requires ocean = .TRUE.'
433                   CALL message( 'init_masks', 'PA0109', 1, 2, 0, 6, 0 )
434                ENDIF
435                unit = 'psu'
436
437             CASE ( 'u*', 't*', 'lwp*', 'pra*', 'prr*', 'z0*', 'z0h*' )
438                WRITE ( message_string, * ) 'illegal value for data_',         &
439                     'output: "', TRIM( var ), '" is only allowed',            &
440                     'for horizontal cross section'
441                CALL message( 'init_masks', 'PA0111', 1, 2, 0, 6, 0 )
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
456                      WRITE ( message_string, * ) 'illegal value for data_',   &
457                           'output_masks or data_output_masks_user: "',        &
458                           TRIM( do_mask(mid,i) ), '"'
459                      CALL message( 'init_masks', 'PA0018', 1, 2, 0, 6, 0 )
460                   ELSE
461                      WRITE ( message_string, * ) 'illegal value for data_',   &
462                           ' output_masks : "', TRIM( do_mask(mid,i) ), '"'
463                      CALL message( 'init_masks', 'PA0330', 1, 2, 0, 6, 0 )
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 )
501       CALL set_mask_locations( 3, dz(1), 'dz', nz, 'nz', nzb, nzt )
502!
503!--    Set global masks along all three dimensions (required by
504!--    define_netcdf_header).
505#if defined( __parallel )
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.
510
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,  &
538                               MPI_INTEGER, sender, 1, comm2d, status, ierr )
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,  &
541                               MPI_INTEGER, sender, 2, comm2d, status, ierr )
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,  &
544                               MPI_INTEGER, sender, 3, comm2d, status, ierr )
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.
553          IF ( mask_size_l(mid,1) > 0  .AND.  mask_size_l(mid,2) > 0  .AND.  &
554               mask_size_l(mid,3) > 0  )  THEN
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),  &
572                            MPI_INTEGER, 0, 1, comm2d, ierr )
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),  &
575                            MPI_INTEGER, 0, 2, comm2d, ierr )
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),  &
578                            MPI_INTEGER, 0, 3, comm2d, ierr )
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 )
585       
586       IF ( netcdf_data_format > 4 )  THEN
587         
588          CALL MPI_BCAST( mask_i_global(mid,:), nx+2, MPI_INTEGER, 0, comm2d, &
589                          ierr )
590          CALL MPI_BCAST( mask_j_global(mid,:), ny+2, MPI_INTEGER, 0, comm2d, &
591                          ierr )
592          CALL MPI_BCAST( mask_k_global(mid,:), nz+2, MPI_INTEGER, 0, comm2d, &
593                          ierr ) 
594     
595       ENDIF
596
597#else
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
613
614!------------------------------------------------------------------------------!
615! Description:
616! ------------
617!> Set local mask for each subdomain along 'dim' direction.
618!------------------------------------------------------------------------------!
619    SUBROUTINE set_mask_locations( dim, dxyz, dxyz_string, nxyz, nxyz_string, &
620                                   lb, ub )
621
622       IMPLICIT NONE
623
624       CHARACTER (LEN=2) ::  dxyz_string !<
625       CHARACTER (LEN=2) ::  nxyz_string !<
626       
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          !<
637       
638       REAL(wp)      ::  dxyz  !<
639       REAL(wp)      ::  ddxyz !<
640       REAL(wp)      ::  tmp1  !<
641       REAL(wp)      ::  tmp2  !<
642
643       count = 0;  count_l = 0 
644       ddxyz = 1.0_wp / dxyz 
645       tmp1  = 0.0_wp
646       tmp2  = 0.0_wp
647
648       IF ( mask(mid,dim,1) >= 0.0_wp )  THEN
649!
650!--       use predefined mask_* array
651          DO  WHILE ( mask(mid,dim,count+1) >= 0.0_wp )
652             count = count + 1
653             IF ( dim == 1 .OR. dim == 2 )  THEN
654                m = NINT( mask(mid,dim,count) * mask_scale(dim) * ddxyz - 0.5_wp )
655                IF ( m < 0 )  m = 0  ! avoid negative values
656             ELSEIF ( dim == 3 )  THEN
657                ind_array =  &
658                     MINLOC( ABS( mask(mid,dim,count) * mask_scale(dim) - zu ) )
659                m = ind_array(1) - 1 + nzb  ! MINLOC uses lower array bound 1
660             ENDIF
661             IF ( m > (nxyz+1) )  THEN
662                WRITE ( message_string, '(I3,A,I3,A,I1,3A,I3)' )               &
663                     m,' in mask ',mid,' along dimension ',dim,                &
664                     ' exceeds (',nxyz_string,'+1) = ',nxyz+1
665                CALL message( 'init_masks', 'PA0331', 1, 2, 0, 6, 0 )
666             ENDIF
667             IF ( ( m >= lb .AND. m <= ub ) .OR.     &
668                  ( m == (nxyz+1) .AND. ub == nxyz )  )  THEN
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)
688          IF ( mask_loop(mid,dim,1) < 0.0_wp )  THEN
689             tmp1 = mask_loop(mid,dim,1)
690             mask_loop(mid,dim,1) = zw(nzb)  !   lowest level  (default)
691          ENDIF
692          IF ( dim == 1 .OR. dim == 2 )  THEN
693             IF ( mask_loop(mid,dim,2) < 0.0_wp )  THEN
694                tmp2 = mask_loop(mid,dim,2)
695                mask_loop(mid,dim,2) = (nxyz+1)*dxyz / mask_scale(dim)   ! (default)
696             ENDIF
697             IF ( MAXVAL( mask_loop(mid,dim,1:2) )  &
698                  > (nxyz+1) * dxyz / mask_scale(dim) )  THEN
699                WRITE ( message_string, '(2(A,I3,A,I1,A,F9.3),5A,I1,A,F9.3)' ) &
700                     'mask_loop(',mid,',',dim,',1)=',mask_loop(mid,dim,1),     &
701                     ' and/or mask_loop(',mid,',',dim,',2)=', &
702                     mask_loop(mid,dim,2),' exceed (', &
703                     nxyz_string,'+1)*',dxyz_string,'/mask_scale(',dim,')=',   &
704                     (nxyz+1)*dxyz/mask_scale(dim)
705                CALL message( 'init_masks', 'PA0332', 1, 2, 0, 6, 0 )
706             ENDIF
707             loop_begin  = NINT( mask_loop(mid,dim,1) * mask_scale(dim)        &
708                  * ddxyz - 0.5_wp )
709             loop_end    = NINT( mask_loop(mid,dim,2) * mask_scale(dim)        &
710                  * ddxyz - 0.5_wp )
711             loop_stride = NINT( mask_loop(mid,dim,3) * mask_scale(dim)        &
712                  * ddxyz )
713             IF ( loop_begin == -1 )  loop_begin = 0  ! avoid negative values
714          ELSEIF ( dim == 3 )  THEN
715             IF ( mask_loop(mid,dim,2) < 0.0_wp )  THEN
716                tmp2 = mask_loop(mid,dim,2)
717                mask_loop(mid,dim,2) = zu(nz+1) / mask_scale(dim)   ! (default)
718             ENDIF
719             IF ( MAXVAL( mask_loop(mid,dim,1:2) )  &
720                  > zu(nz+1) / mask_scale(dim) )  THEN
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),     &
723                     ' and/or mask_loop(',mid,',',dim,',2)=', &
724                     mask_loop(mid,dim,2),' exceed zu(nz+1)/mask_scale(',dim,  &
725                     ')=',zu(nz+1)/mask_scale(dim)
726                CALL message( 'init_masks', 'PA0333', 1, 2, 0, 6, 0 )
727             ENDIF
728             ind_array =  &
729                  MINLOC( ABS( mask_loop(mid,dim,1) * mask_scale(dim) - zu ) )
730             loop_begin =  &
731                  ind_array(1) - 1 + nzb ! MINLOC uses lower array bound 1
732             ind_array =  &
733                  MINLOC( ABS( mask_loop(mid,dim,2) * mask_scale(dim) - zu ) )
734             loop_end = ind_array(1) - 1 + nzb ! MINLOC uses lower array bound 1
735!
736!--          The following line assumes a constant vertical grid spacing within
737!--          the vertical mask range; it fails for vertical grid stretching.
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.
741             loop_stride = NINT( mask_loop(mid,dim,3) * mask_scale(dim) * ddxyz )
742
743             IF ( mask_loop(mid,dim,2) * mask_scale(dim) >                     &
744                  ABS( dz_stretch_level_start(1) ) )  THEN
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),    &
747                     ' exceeds dz_stretch_level=',dz_stretch_level_start(1),   &
748                     '.&Vertical mask locations will not ',                    &
749                     'match the desired heights within the stretching ',       &
750                     'region.'
751                CALL message( 'init_masks', 'PA0334', 0, 1, 0, 6, 0 )
752             ENDIF
753
754          ENDIF
755!
756!--       If necessary, reset mask_loop(mid,dim,1) and mask_loop(mid,dim,2).
757          IF ( tmp1 < 0.0_wp )  mask_loop(mid,dim,1) = tmp1
758          IF ( tmp2 < 0.0_wp )  mask_loop(mid,dim,2) = tmp2
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
771             IF ( ( m >= lb  .AND.  m <= ub ) .OR.   &
772                  ( m == (nxyz+1) .AND. ub == nxyz )  )  THEN
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
786
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.