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

Last change on this file since 4171 was 4069, checked in by Giersch, 5 years ago

Bugfix for masked output, compiler warning removed, test case for wind turbine model revised

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