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

Last change on this file since 3667 was 3665, checked in by raasch, 5 years ago

dummy statements added to avoid compiler warnings about unused variables, unused variables removed, ssh-call for submitting batch jobs on remote systems modified again to avoid output of login messages on specific systems

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