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