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

Last change on this file since 4725 was 4648, checked in by raasch, 4 years ago

files re-formatted to follow the PALM coding standard

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