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

Last change on this file since 1682 was 1682, checked in by knoop, 8 years ago

Code annotations made doxygen readable

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