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

Last change on this file since 2277 was 2271, checked in by sward, 7 years ago

error messages and numbers updated

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