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

Last change on this file since 1822 was 1822, checked in by hoffmann, 8 years ago

changes in LPM and bulk cloud microphysics

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