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

Last change on this file since 4895 was 4895, checked in by suehring, 3 years ago

Remove offset in terrain-following masked output and allow only mask_k_over_surface >= 1

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