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

Last change on this file since 4180 was 4180, checked in by scharf, 5 years ago

removed comments in 'Former revisions' section that are older than 01.01.2019

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