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

Last change on this file since 3421 was 3421, checked in by gronemeier, 6 years ago

new surface-data output; renamed output variables (pt to theta, rho_air to rho, rho_ocean to rho_sea_water)

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