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

Last change on this file since 3609 was 3589, checked in by suehring, 5 years ago

Remove erroneous UTF encoding; last commit documented

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