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

Last change on this file since 1783 was 1783, checked in by raasch, 8 years ago

NetCDF routines modularized; new parameter netcdf_deflate; further changes in the pmc

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