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

Last change on this file since 4540 was 4521, checked in by schwenkel, 4 years ago

add error number

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