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

Last change on this file since 3359 was 3294, checked in by raasch, 5 years ago

modularization of the ocean code

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