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

Last change on this file since 3586 was 3582, checked in by suehring, 5 years ago

Merge branch salsa with trunk

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