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

Last change on this file since 3046 was 3046, checked in by Giersch, 6 years ago

Remaining error messages revised, comments extended

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