SUBROUTINE init_grid !------------------------------------------------------------------------------! ! Current revisions: ! ----------------- ! ! ! Former revisions: ! ----------------- ! $Id: init_grid.f90 392 2009-09-24 10:39:14Z maronga $ ! ! 274 2009-03-26 15:11:21Z heinze ! Output of messages replaced by message handling routine. ! new topography case 'single_street_canyon' ! ! 217 2008-12-09 18:00:48Z letzel ! +topography_grid_convention ! ! 134 2007-11-21 07:28:38Z letzel ! Redefine initial nzb_local as the actual total size of topography (later the ! extent of topography in nzb_local is reduced by 1dx at the E topography walls ! and by 1dy at the N topography walls to form the basis for nzb_s_inner); ! for consistency redefine 'single_building' case. ! Calculation of wall flag arrays ! ! 94 2007-06-01 15:25:22Z raasch ! Grid definition for ocean version ! ! 75 2007-03-22 09:54:05Z raasch ! storage of topography height arrays zu_s_inner and zw_s_inner, ! 2nd+3rd argument removed from exchange horiz ! ! 19 2007-02-23 04:53:48Z raasch ! Setting of nzt_diff ! ! RCS Log replace by Id keyword, revision history cleaned up ! ! Revision 1.17 2006/08/22 14:00:05 raasch ! +dz_max to limit vertical stretching, ! bugfix in index array initialization for line- or point-like topography ! structures ! ! Revision 1.1 1997/08/11 06:17:45 raasch ! Initial revision (Testversion) ! ! ! Description: ! ------------ ! Creating grid depending constants !------------------------------------------------------------------------------! USE arrays_3d USE control_parameters USE grid_variables USE indices USE pegrid IMPLICIT NONE INTEGER :: bh, blx, bly, bxl, bxr, byn, bys, ch, cwx, cwy, cxl, cxr, cyn, & cys, gls, i, inc, i_center, j, j_center, k, l, nxl_l, nxr_l, & nyn_l, nys_l, nzb_si, nzt_l, vi INTEGER, DIMENSION(:), ALLOCATABLE :: vertical_influence INTEGER, DIMENSION(:,:), ALLOCATABLE :: corner_nl, corner_nr, corner_sl, & corner_sr, wall_l, wall_n, wall_r,& wall_s, nzb_local, nzb_tmp REAL :: dx_l, dy_l, dz_stretched REAL, DIMENSION(0:ny,0:nx) :: topo_height REAL, DIMENSION(:,:,:), ALLOCATABLE :: distance ! !-- Allocate grid arrays ALLOCATE( ddzu(1:nzt+1), ddzw(1:nzt+1), dd2zu(1:nzt), dzu(1:nzt+1), & dzw(1:nzt+1), l_grid(1:nzt), zu(0:nzt+1), zw(0:nzt+1) ) ! !-- Compute height of u-levels from constant grid length and dz stretch factors IF ( dz == -1.0 ) THEN message_string = 'missing dz' CALL message( 'init_grid', 'PA0200', 1, 2, 0, 6, 0 ) ELSEIF ( dz <= 0.0 ) THEN WRITE( message_string, * ) 'dz=',dz,' <= 0.0' CALL message( 'init_grid', 'PA0201', 1, 2, 0, 6, 0 ) ENDIF ! !-- Define the vertical grid levels IF ( .NOT. ocean ) THEN ! !-- Grid for atmosphere with surface at z=0 (k=0, w-grid). !-- Since the w-level lies on the surface, the first u-level (staggered!) !-- lies below the surface (used for "mirror" boundary condition). !-- The first u-level above the surface corresponds to the top of the !-- Prandtl-layer. zu(0) = - dz * 0.5 zu(1) = dz * 0.5 dz_stretch_level_index = nzt+1 dz_stretched = dz DO k = 2, nzt+1 IF ( dz_stretch_level <= zu(k-1) .AND. dz_stretched < dz_max ) THEN dz_stretched = dz_stretched * dz_stretch_factor dz_stretched = MIN( dz_stretched, dz_max ) IF ( dz_stretch_level_index == nzt+1 ) dz_stretch_level_index = k-1 ENDIF zu(k) = zu(k-1) + dz_stretched ENDDO ! !-- Compute the w-levels. They are always staggered half-way between the !-- corresponding u-levels. The top w-level is extrapolated linearly. zw(0) = 0.0 DO k = 1, nzt zw(k) = ( zu(k) + zu(k+1) ) * 0.5 ENDDO zw(nzt+1) = zw(nzt) + 2.0 * ( zu(nzt+1) - zw(nzt) ) ELSE ! !-- Grid for ocean with solid surface at z=0 (k=0, w-grid). The free water !-- surface is at k=nzt (w-grid). !-- Since the w-level lies always on the surface, the first/last u-level !-- (staggered!) lies below the bottom surface / above the free surface. !-- It is used for "mirror" boundary condition. !-- The first u-level above the bottom surface corresponds to the top of the !-- Prandtl-layer. zu(nzt+1) = dz * 0.5 zu(nzt) = - dz * 0.5 dz_stretch_level_index = 0 dz_stretched = dz DO k = nzt-1, 0, -1 IF ( dz_stretch_level <= ABS( zu(k+1) ) .AND. & dz_stretched < dz_max ) THEN dz_stretched = dz_stretched * dz_stretch_factor dz_stretched = MIN( dz_stretched, dz_max ) IF ( dz_stretch_level_index == 0 ) dz_stretch_level_index = k+1 ENDIF zu(k) = zu(k+1) - dz_stretched ENDDO ! !-- Compute the w-levels. They are always staggered half-way between the !-- corresponding u-levels. !-- The top w-level (nzt+1) is not used but set for consistency, since !-- w and all scalar variables are defined up tp nzt+1. zw(nzt+1) = dz zw(nzt) = 0.0 DO k = 0, nzt zw(k) = ( zu(k) + zu(k+1) ) * 0.5 ENDDO ENDIF ! !-- Compute grid lengths. DO k = 1, nzt+1 dzu(k) = zu(k) - zu(k-1) ddzu(k) = 1.0 / dzu(k) dzw(k) = zw(k) - zw(k-1) ddzw(k) = 1.0 / dzw(k) ENDDO DO k = 1, nzt dd2zu(k) = 1.0 / ( dzu(k) + dzu(k+1) ) ENDDO ! !-- In case of multigrid method, compute grid lengths and grid factors for the !-- grid levels IF ( psolver == 'multigrid' ) THEN ALLOCATE( ddx2_mg(maximum_grid_level), ddy2_mg(maximum_grid_level), & dzu_mg(nzb+1:nzt+1,maximum_grid_level), & dzw_mg(nzb+1:nzt+1,maximum_grid_level), & f1_mg(nzb+1:nzt,maximum_grid_level), & f2_mg(nzb+1:nzt,maximum_grid_level), & f3_mg(nzb+1:nzt,maximum_grid_level) ) dzu_mg(:,maximum_grid_level) = dzu dzw_mg(:,maximum_grid_level) = dzw nzt_l = nzt DO l = maximum_grid_level-1, 1, -1 dzu_mg(nzb+1,l) = 2.0 * dzu_mg(nzb+1,l+1) dzw_mg(nzb+1,l) = 2.0 * dzw_mg(nzb+1,l+1) nzt_l = nzt_l / 2 DO k = 2, nzt_l+1 dzu_mg(k,l) = dzu_mg(2*k-2,l+1) + dzu_mg(2*k-1,l+1) dzw_mg(k,l) = dzw_mg(2*k-2,l+1) + dzw_mg(2*k-1,l+1) ENDDO ENDDO nzt_l = nzt dx_l = dx dy_l = dy DO l = maximum_grid_level, 1, -1 ddx2_mg(l) = 1.0 / dx_l**2 ddy2_mg(l) = 1.0 / dy_l**2 DO k = nzb+1, nzt_l f2_mg(k,l) = 1.0 / ( dzu_mg(k+1,l) * dzw_mg(k,l) ) f3_mg(k,l) = 1.0 / ( dzu_mg(k,l) * dzw_mg(k,l) ) f1_mg(k,l) = 2.0 * ( ddx2_mg(l) + ddy2_mg(l) ) + & f2_mg(k,l) + f3_mg(k,l) ENDDO nzt_l = nzt_l / 2 dx_l = dx_l * 2.0 dy_l = dy_l * 2.0 ENDDO ENDIF ! !-- Compute the reciprocal values of the horizontal grid lengths. ddx = 1.0 / dx ddy = 1.0 / dy dx2 = dx * dx dy2 = dy * dy ddx2 = 1.0 / dx2 ddy2 = 1.0 / dy2 ! !-- Compute the grid-dependent mixing length. DO k = 1, nzt l_grid(k) = ( dx * dy * dzw(k) )**0.33333333333333 ENDDO ! !-- Allocate outer and inner index arrays for topography and set !-- defaults. !-- nzb_local has to contain additional layers of ghost points for calculating !-- the flag arrays needed for the multigrid method gls = 2**( maximum_grid_level ) ALLOCATE( corner_nl(nys:nyn,nxl:nxr), corner_nr(nys:nyn,nxl:nxr), & corner_sl(nys:nyn,nxl:nxr), corner_sr(nys:nyn,nxl:nxr), & nzb_local(-gls:ny+gls,-gls:nx+gls), nzb_tmp(-1:ny+1,-1:nx+1), & wall_l(nys:nyn,nxl:nxr), wall_n(nys:nyn,nxl:nxr), & wall_r(nys:nyn,nxl:nxr), wall_s(nys:nyn,nxl:nxr) ) ALLOCATE( fwxm(nys-1:nyn+1,nxl-1:nxr+1), fwxp(nys-1:nyn+1,nxl-1:nxr+1), & fwym(nys-1:nyn+1,nxl-1:nxr+1), fwyp(nys-1:nyn+1,nxl-1:nxr+1), & fxm(nys-1:nyn+1,nxl-1:nxr+1), fxp(nys-1:nyn+1,nxl-1:nxr+1), & fym(nys-1:nyn+1,nxl-1:nxr+1), fyp(nys-1:nyn+1,nxl-1:nxr+1), & nzb_s_inner(nys-1:nyn+1,nxl-1:nxr+1), & nzb_s_outer(nys-1:nyn+1,nxl-1:nxr+1), & nzb_u_inner(nys-1:nyn+1,nxl-1:nxr+1), & nzb_u_outer(nys-1:nyn+1,nxl-1:nxr+1), & nzb_v_inner(nys-1:nyn+1,nxl-1:nxr+1), & nzb_v_outer(nys-1:nyn+1,nxl-1:nxr+1), & nzb_w_inner(nys-1:nyn+1,nxl-1:nxr+1), & nzb_w_outer(nys-1:nyn+1,nxl-1:nxr+1), & nzb_diff_s_inner(nys-1:nyn+1,nxl-1:nxr+1), & nzb_diff_s_outer(nys-1:nyn+1,nxl-1:nxr+1), & nzb_diff_u(nys-1:nyn+1,nxl-1:nxr+1), & nzb_diff_v(nys-1:nyn+1,nxl-1:nxr+1), & nzb_2d(nys-1:nyn+1,nxl-1:nxr+1), & wall_e_x(nys-1:nyn+1,nxl-1:nxr+1), & wall_e_y(nys-1:nyn+1,nxl-1:nxr+1), & wall_u(nys-1:nyn+1,nxl-1:nxr+1), & wall_v(nys-1:nyn+1,nxl-1:nxr+1), & wall_w_x(nys-1:nyn+1,nxl-1:nxr+1), & wall_w_y(nys-1:nyn+1,nxl-1:nxr+1) ) ALLOCATE( l_wall(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) ) nzb_s_inner = nzb; nzb_s_outer = nzb nzb_u_inner = nzb; nzb_u_outer = nzb nzb_v_inner = nzb; nzb_v_outer = nzb nzb_w_inner = nzb; nzb_w_outer = nzb ! !-- Define vertical gridpoint from (or to) which on the usual finite difference !-- form (which does not use surface fluxes) is applied IF ( prandtl_layer .OR. use_surface_fluxes ) THEN nzb_diff = nzb + 2 ELSE nzb_diff = nzb + 1 ENDIF IF ( use_top_fluxes ) THEN nzt_diff = nzt - 1 ELSE nzt_diff = nzt ENDIF nzb_diff_s_inner = nzb_diff; nzb_diff_s_outer = nzb_diff nzb_diff_u = nzb_diff; nzb_diff_v = nzb_diff wall_e_x = 0.0; wall_e_y = 0.0; wall_u = 0.0; wall_v = 0.0 wall_w_x = 0.0; wall_w_y = 0.0 fwxp = 1.0; fwxm = 1.0; fwyp = 1.0; fwym = 1.0 fxp = 1.0; fxm = 1.0; fyp = 1.0; fym = 1.0 ! !-- Initialize near-wall mixing length l_wall only in the vertical direction !-- for the moment, !-- multiplication with wall_adjustment_factor near the end of this routine l_wall(nzb,:,:) = l_grid(1) DO k = nzb+1, nzt l_wall(k,:,:) = l_grid(k) ENDDO l_wall(nzt+1,:,:) = l_grid(nzt) ALLOCATE ( vertical_influence(nzb:nzt) ) DO k = 1, nzt vertical_influence(k) = MIN ( INT( l_grid(k) / & ( wall_adjustment_factor * dzw(k) ) + 0.5 ), nzt - k ) ENDDO DO k = 1, MAXVAL( nzb_s_inner ) IF ( l_grid(k) > 1.5 * dx * wall_adjustment_factor .OR. & l_grid(k) > 1.5 * dy * wall_adjustment_factor ) THEN WRITE( message_string, * ) 'grid anisotropy exceeds ', & 'threshold given by only local', & ' &horizontal reduction of near_wall ', & 'mixing length l_wall', & ' &starting from height level k = ', k, '.' CALL message( 'init_grid', 'PA0202', 0, 1, 0, 6, 0 ) EXIT ENDIF ENDDO vertical_influence(0) = vertical_influence(1) DO i = nxl-1, nxr+1 DO j = nys-1, nyn+1 DO k = nzb_s_inner(j,i) + 1, & nzb_s_inner(j,i) + vertical_influence(nzb_s_inner(j,i)) l_wall(k,j,i) = zu(k) - zw(nzb_s_inner(j,i)) ENDDO ENDDO ENDDO ! !-- Set outer and inner index arrays for non-flat topography. !-- Here consistency checks concerning domain size and periodicity are !-- necessary. !-- Within this SELECT CASE structure only nzb_local is initialized !-- individually depending on the chosen topography type, all other index !-- arrays are initialized further below. SELECT CASE ( TRIM( topography ) ) CASE ( 'flat' ) ! !-- No actions necessary CASE ( 'single_building' ) ! !-- Single rectangular building, by default centered in the middle of the !-- total domain blx = NINT( building_length_x / dx ) bly = NINT( building_length_y / dy ) bh = NINT( building_height / dz ) IF ( building_wall_left == 9999999.9 ) THEN building_wall_left = ( nx + 1 - blx ) / 2 * dx ENDIF bxl = NINT( building_wall_left / dx ) bxr = bxl + blx IF ( building_wall_south == 9999999.9 ) THEN building_wall_south = ( ny + 1 - bly ) / 2 * dy ENDIF bys = NINT( building_wall_south / dy ) byn = bys + bly ! !-- Building size has to meet some requirements IF ( ( bxl < 1 ) .OR. ( bxr > nx-1 ) .OR. ( bxr < bxl+3 ) .OR. & ( bys < 1 ) .OR. ( byn > ny-1 ) .OR. ( byn < bys+3 ) ) THEN WRITE( message_string, * ) 'inconsistent building parameters:', & '& bxl=', bxl, 'bxr=', bxr, 'bys=', bys, & 'byn=', byn, 'nx=', nx, 'ny=', ny CALL message( 'init_grid', 'PA0203', 1, 2, 0, 6, 0 ) ENDIF ! !-- Define the building. nzb_local = 0 nzb_local(bys:byn,bxl:bxr) = bh CASE ( 'single_street_canyon' ) ! !-- Single quasi-2D street canyon of infinite length in x or y direction. !-- The canyon is centered in the other direction by default. IF ( canyon_width_x /= 9999999.9 ) THEN ! !-- Street canyon in y direction cwx = NINT( canyon_width_x / dx ) IF ( canyon_wall_left == 9999999.9 ) THEN canyon_wall_left = ( nx + 1 - cwx ) / 2 * dx ENDIF cxl = NINT( canyon_wall_left / dx ) cxr = cxl + cwx ELSEIF ( canyon_width_y /= 9999999.9 ) THEN ! !-- Street canyon in x direction cwy = NINT( canyon_width_y / dy ) IF ( canyon_wall_south == 9999999.9 ) THEN canyon_wall_south = ( ny + 1 - cwy ) / 2 * dy ENDIF cys = NINT( canyon_wall_south / dy ) cyn = cys + cwy ELSE message_string = 'no street canyon width given' CALL message( 'init_grid', 'PA0204', 1, 2, 0, 6, 0 ) ENDIF ch = NINT( canyon_height / dz ) dp_level_ind_b = ch ! !-- Street canyon size has to meet some requirements IF ( canyon_width_x /= 9999999.9 ) THEN IF ( ( cxl < 1 ) .OR. ( cxr > nx-1 ) .OR. ( cwx < 3 ) .OR. & ( ch < 3 ) ) THEN WRITE( message_string, * ) 'inconsistent canyon parameters:', & '&cxl=', cxl, 'cxr=', cxr, & 'cwx=', cwx, & 'ch=', ch, 'nx=', nx, 'ny=', ny CALL message( 'init_grid', 'PA0205', 1, 2, 0, 6, 0 ) ENDIF ELSEIF ( canyon_width_y /= 9999999.9 ) THEN IF ( ( cys < 1 ) .OR. ( cyn > ny-1 ) .OR. ( cwy < 3 ) .OR. & ( ch < 3 ) ) THEN WRITE( message_string, * ) 'inconsistent canyon parameters:', & '&cys=', cys, 'cyn=', cyn, & 'cwy=', cwy, & 'ch=', ch, 'nx=', nx, 'ny=', ny CALL message( 'init_grid', 'PA0206', 1, 2, 0, 6, 0 ) ENDIF ENDIF IF ( canyon_width_x /= 9999999.9 .AND. canyon_width_y /= 9999999.9 ) & THEN message_string = 'inconsistent canyon parameters:' // & '&street canyon can only be oriented' // & '&either in x- or in y-direction' CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 ) ENDIF nzb_local = ch IF ( canyon_width_x /= 9999999.9 ) THEN nzb_local(:,cxl+1:cxr-1) = 0 ELSEIF ( canyon_width_y /= 9999999.9 ) THEN nzb_local(cys+1:cyn-1,:) = 0 ENDIF CASE ( 'read_from_file' ) ! !-- Arbitrary irregular topography data in PALM format (exactly matching !-- the grid size and total domain size) OPEN( 90, FILE='TOPOGRAPHY_DATA', STATUS='OLD', FORM='FORMATTED', & ERR=10 ) DO j = ny, 0, -1 READ( 90, *, ERR=11, END=11 ) ( topo_height(j,i), i = 0, nx ) ENDDO ! !-- Calculate the index height of the topography DO i = 0, nx DO j = 0, ny nzb_local(j,i) = NINT( topo_height(j,i) / dz ) ENDDO ENDDO ! !-- Add cyclic boundaries (additional layers are for calculating flag !-- arrays needed for the multigrid sover) nzb_local(-gls:-1,0:nx) = nzb_local(ny-gls+1:ny,0:nx) nzb_local(ny+1:ny+gls,0:nx) = nzb_local(0:gls-1,0:nx) nzb_local(:,-gls:-1) = nzb_local(:,nx-gls+1:nx) nzb_local(:,nx+1:nx+gls) = nzb_local(:,0:gls-1) GOTO 12 10 message_string = 'file TOPOGRAPHY_DATA does not exist' CALL message( 'init_grid', 'PA0208', 1, 2, 0, 6, 0 ) 11 message_string = 'errors in file TOPOGRAPHY_DATA' CALL message( 'init_grid', 'PA0209', 1, 2, 0, 6, 0 ) 12 CLOSE( 90 ) CASE DEFAULT ! !-- The DEFAULT case is reached either if the parameter topography !-- contains a wrong character string or if the user has defined a special !-- case in the user interface. There, the subroutine user_init_grid !-- checks which of these two conditions applies. CALL user_init_grid( gls, nzb_local ) END SELECT ! !-- Test output of nzb_local -1:ny+1,-1:nx+1 ! WRITE (9,*) '*** nzb_local ***' ! DO j = ny+1, -1, -1 ! WRITE (9,'(194(1X,I2))') ( nzb_local(j,i), i = -1, nx+1 ) ! ENDDO ! !-- Consistency checks and index array initialization are only required for !-- non-flat topography, also the initialization of topography height arrays !-- zu_s_inner and zw_w_inner IF ( TRIM( topography ) /= 'flat' ) THEN ! !-- Consistency checks IF ( MINVAL( nzb_local ) < 0 .OR. MAXVAL( nzb_local ) > nz + 1 ) THEN WRITE( message_string, * ) 'nzb_local values are outside the', & 'model domain', & '&MINVAL( nzb_local ) = ', MINVAL(nzb_local), & '&MAXVAL( nzb_local ) = ', MAXVAL(nzb_local) CALL message( 'init_grid', 'PA0210', 1, 2, 0, 6, 0 ) ENDIF IF ( bc_lr == 'cyclic' ) THEN IF ( ANY( nzb_local(:,-1) /= nzb_local(:,nx) ) .OR. & ANY( nzb_local(:,0) /= nzb_local(:,nx+1) ) ) THEN message_string = 'nzb_local does not fulfill cyclic' // & ' boundary condition in x-direction' CALL message( 'init_grid', 'PA0211', 1, 2, 0, 6, 0 ) ENDIF ENDIF IF ( bc_ns == 'cyclic' ) THEN IF ( ANY( nzb_local(-1,:) /= nzb_local(ny,:) ) .OR. & ANY( nzb_local(0,:) /= nzb_local(ny+1,:) ) ) THEN message_string = 'nzb_local does not fulfill cyclic' // & ' boundary condition in y-direction' CALL message( 'init_grid', 'PA0212', 1, 2, 0, 6, 0 ) ENDIF ENDIF IF ( topography_grid_convention == 'cell_edge' ) THEN ! !-- The array nzb_local as defined using the 'cell_edge' convention !-- describes the actual total size of topography which is defined at the !-- cell edges where u=0 on the topography walls in x-direction and v=0 !-- on the topography walls in y-direction. However, PALM uses individual !-- arrays nzb_u|v|w|s_inner|outer that are based on nzb_s_inner. !-- Therefore, the extent of topography in nzb_local is now reduced by !-- 1dx at the E topography walls and by 1dy at the N topography walls !-- to form the basis for nzb_s_inner. DO j = -gls, ny + gls DO i = -gls, nx nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j,i+1) ) ENDDO ENDDO !-- apply cyclic boundary conditions in x-direction !(ist das erforderlich? Ursache von Seung Bus Fehler?) nzb_local(:,nx+1:nx+gls) = nzb_local(:,0:gls-1) DO i = -gls, nx + gls DO j = -gls, ny nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j+1,i) ) ENDDO ENDDO !-- apply cyclic boundary conditions in y-direction !(ist das erforderlich? Ursache von Seung Bus Fehler?) nzb_local(ny+1:ny+gls,:) = nzb_local(0:gls-1,:) ENDIF ! !-- Initialize index arrays nzb_s_inner and nzb_w_inner nzb_s_inner = nzb_local(nys-1:nyn+1,nxl-1:nxr+1) nzb_w_inner = nzb_local(nys-1:nyn+1,nxl-1:nxr+1) ! !-- Initialize remaining index arrays: !-- first pre-initialize them with nzb_s_inner... nzb_u_inner = nzb_s_inner nzb_u_outer = nzb_s_inner nzb_v_inner = nzb_s_inner nzb_v_outer = nzb_s_inner nzb_w_outer = nzb_s_inner nzb_s_outer = nzb_s_inner ! !-- ...then extend pre-initialized arrays in their according directions !-- based on nzb_local using nzb_tmp as a temporary global index array ! !-- nzb_s_outer: !-- extend nzb_local east-/westwards first, then north-/southwards nzb_tmp = nzb_local(-1:ny+1,-1:nx+1) DO j = -1, ny + 1 DO i = 0, nx nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i), & nzb_local(j,i+1) ) ENDDO ENDDO DO i = nxl, nxr DO j = nys, nyn nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i), & nzb_tmp(j+1,i) ) ENDDO ! !-- non-cyclic boundary conditions (overwritten by call of !-- exchange_horiz_2d_int below in case of cyclic boundary conditions) IF ( nys == 0 ) THEN j = -1 nzb_s_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) ) ENDIF IF ( nys == ny ) THEN j = ny + 1 nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) ) ENDIF ENDDO ! !-- nzb_w_outer: !-- identical to nzb_s_outer nzb_w_outer = nzb_s_outer ! !-- nzb_u_inner: !-- extend nzb_local rightwards only nzb_tmp = nzb_local(-1:ny+1,-1:nx+1) DO j = -1, ny + 1 DO i = 0, nx + 1 nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i) ) ENDDO ENDDO nzb_u_inner = nzb_tmp(nys-1:nyn+1,nxl-1:nxr+1) ! !-- nzb_u_outer: !-- extend current nzb_tmp (nzb_u_inner) north-/southwards DO i = nxl, nxr DO j = nys, nyn nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i), & nzb_tmp(j+1,i) ) ENDDO ! !-- non-cyclic boundary conditions (overwritten by call of !-- exchange_horiz_2d_int below in case of cyclic boundary conditions) IF ( nys == 0 ) THEN j = -1 nzb_u_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) ) ENDIF IF ( nys == ny ) THEN j = ny + 1 nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) ) ENDIF ENDDO ! !-- nzb_v_inner: !-- extend nzb_local northwards only nzb_tmp = nzb_local(-1:ny+1,-1:nx+1) DO i = -1, nx + 1 DO j = 0, ny + 1 nzb_tmp(j,i) = MAX( nzb_local(j-1,i), nzb_local(j,i) ) ENDDO ENDDO nzb_v_inner = nzb_tmp(nys-1:nyn+1,nxl-1:nxr+1) ! !-- nzb_v_outer: !-- extend current nzb_tmp (nzb_v_inner) right-/leftwards DO j = nys, nyn DO i = nxl, nxr nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i), & nzb_tmp(j,i+1) ) ENDDO ! !-- non-cyclic boundary conditions (overwritten by call of !-- exchange_horiz_2d_int below in case of cyclic boundary conditions) IF ( nxl == 0 ) THEN i = -1 nzb_v_outer(j,i) = MAX( nzb_tmp(j,i+1), nzb_tmp(j,i) ) ENDIF IF ( nxr == nx ) THEN i = nx + 1 nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i) ) ENDIF ENDDO ! !-- Exchange of lateral boundary values (parallel computers) and cyclic !-- boundary conditions, if applicable. !-- Since nzb_s_inner and nzb_w_inner are derived directly from nzb_local !-- they do not require exchange and are not included here. CALL exchange_horiz_2d_int( nzb_u_inner ) CALL exchange_horiz_2d_int( nzb_u_outer ) CALL exchange_horiz_2d_int( nzb_v_inner ) CALL exchange_horiz_2d_int( nzb_v_outer ) CALL exchange_horiz_2d_int( nzb_w_outer ) CALL exchange_horiz_2d_int( nzb_s_outer ) ! !-- Allocate and set the arrays containing the topography height IF ( myid == 0 ) THEN ALLOCATE( zu_s_inner(0:nx+1,0:ny+1), zw_w_inner(0:nx+1,0:ny+1) ) DO i = 0, nx + 1 DO j = 0, ny + 1 zu_s_inner(i,j) = zu(nzb_local(j,i)) zw_w_inner(i,j) = zw(nzb_local(j,i)) ENDDO ENDDO ENDIF ENDIF ! !-- Preliminary: to be removed after completion of the topography code! !-- Set the former default k index arrays nzb_2d nzb_2d = nzb ! !-- Set the individual index arrays which define the k index from which on !-- the usual finite difference form (which does not use surface fluxes) is !-- applied IF ( prandtl_layer .OR. use_surface_fluxes ) THEN nzb_diff_u = nzb_u_inner + 2 nzb_diff_v = nzb_v_inner + 2 nzb_diff_s_inner = nzb_s_inner + 2 nzb_diff_s_outer = nzb_s_outer + 2 ELSE nzb_diff_u = nzb_u_inner + 1 nzb_diff_v = nzb_v_inner + 1 nzb_diff_s_inner = nzb_s_inner + 1 nzb_diff_s_outer = nzb_s_outer + 1 ENDIF ! !-- Calculation of wall switches and factors required by diffusion_u/v.f90 and !-- for limitation of near-wall mixing length l_wall further below corner_nl = 0 corner_nr = 0 corner_sl = 0 corner_sr = 0 wall_l = 0 wall_n = 0 wall_r = 0 wall_s = 0 DO i = nxl, nxr DO j = nys, nyn ! !-- u-component IF ( nzb_u_outer(j,i) > nzb_u_outer(j+1,i) ) THEN wall_u(j,i) = 1.0 ! north wall (location of adjacent fluid) fym(j,i) = 0.0 fyp(j,i) = 1.0 ELSEIF ( nzb_u_outer(j,i) > nzb_u_outer(j-1,i) ) THEN wall_u(j,i) = 1.0 ! south wall (location of adjacent fluid) fym(j,i) = 1.0 fyp(j,i) = 0.0 ENDIF ! !-- v-component IF ( nzb_v_outer(j,i) > nzb_v_outer(j,i+1) ) THEN wall_v(j,i) = 1.0 ! rigth wall (location of adjacent fluid) fxm(j,i) = 0.0 fxp(j,i) = 1.0 ELSEIF ( nzb_v_outer(j,i) > nzb_v_outer(j,i-1) ) THEN wall_v(j,i) = 1.0 ! left wall (location of adjacent fluid) fxm(j,i) = 1.0 fxp(j,i) = 0.0 ENDIF ! !-- w-component, also used for scalars, separate arrays for shear !-- production of tke IF ( nzb_w_outer(j,i) > nzb_w_outer(j+1,i) ) THEN wall_e_y(j,i) = 1.0 ! north wall (location of adjacent fluid) wall_w_y(j,i) = 1.0 fwym(j,i) = 0.0 fwyp(j,i) = 1.0 ELSEIF ( nzb_w_outer(j,i) > nzb_w_outer(j-1,i) ) THEN wall_e_y(j,i) = -1.0 ! south wall (location of adjacent fluid) wall_w_y(j,i) = 1.0 fwym(j,i) = 1.0 fwyp(j,i) = 0.0 ENDIF IF ( nzb_w_outer(j,i) > nzb_w_outer(j,i+1) ) THEN wall_e_x(j,i) = 1.0 ! right wall (location of adjacent fluid) wall_w_x(j,i) = 1.0 fwxm(j,i) = 0.0 fwxp(j,i) = 1.0 ELSEIF ( nzb_w_outer(j,i) > nzb_w_outer(j,i-1) ) THEN wall_e_x(j,i) = -1.0 ! left wall (location of adjacent fluid) wall_w_x(j,i) = 1.0 fwxm(j,i) = 1.0 fwxp(j,i) = 0.0 ENDIF ! !-- Wall and corner locations inside buildings for limitation of !-- near-wall mixing length l_wall IF ( nzb_s_inner(j,i) > nzb_s_inner(j+1,i) ) THEN wall_n(j,i) = nzb_s_inner(j+1,i) + 1 ! North wall IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) ) THEN corner_nl(j,i) = MAX( nzb_s_inner(j+1,i), & ! Northleft corner nzb_s_inner(j,i-1) ) + 1 ENDIF IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) ) THEN corner_nr(j,i) = MAX( nzb_s_inner(j+1,i), & ! Northright corner nzb_s_inner(j,i+1) ) + 1 ENDIF ENDIF IF ( nzb_s_inner(j,i) > nzb_s_inner(j-1,i) ) THEN wall_s(j,i) = nzb_s_inner(j-1,i) + 1 ! South wall IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) ) THEN corner_sl(j,i) = MAX( nzb_s_inner(j-1,i), & ! Southleft corner nzb_s_inner(j,i-1) ) + 1 ENDIF IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) ) THEN corner_sr(j,i) = MAX( nzb_s_inner(j-1,i), & ! Southright corner nzb_s_inner(j,i+1) ) + 1 ENDIF ENDIF IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) ) THEN wall_l(j,i) = nzb_s_inner(j,i-1) + 1 ! Left wall ENDIF IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) ) THEN wall_r(j,i) = nzb_s_inner(j,i+1) + 1 ! Right wall ENDIF ENDDO ENDDO ! !-- Calculate wall flag arrays for the multigrid method IF ( psolver == 'multigrid' ) THEN ! !-- Gridpoint increment of the current level inc = 1 DO l = maximum_grid_level, 1 , -1 nxl_l = nxl_mg(l) nxr_l = nxr_mg(l) nys_l = nys_mg(l) nyn_l = nyn_mg(l) nzt_l = nzt_mg(l) ! !-- Assign the flag level to be calculated SELECT CASE ( l ) CASE ( 1 ) flags => wall_flags_1 CASE ( 2 ) flags => wall_flags_2 CASE ( 3 ) flags => wall_flags_3 CASE ( 4 ) flags => wall_flags_4 CASE ( 5 ) flags => wall_flags_5 CASE ( 6 ) flags => wall_flags_6 CASE ( 7 ) flags => wall_flags_7 CASE ( 8 ) flags => wall_flags_8 CASE ( 9 ) flags => wall_flags_9 CASE ( 10 ) flags => wall_flags_10 END SELECT ! !-- Depending on the grid level, set the respective bits in case of !-- neighbouring walls !-- Bit 0: wall to the bottom !-- Bit 1: wall to the top (not realized in remaining PALM code so far) !-- Bit 2: wall to the south !-- Bit 3: wall to the north !-- Bit 4: wall to the left !-- Bit 5: wall to the right !-- Bit 6: inside building flags = 0 DO i = nxl_l-1, nxr_l+1 DO j = nys_l-1, nyn_l+1 DO k = nzb, nzt_l+1 ! !-- Inside/outside building (inside building does not need !-- further tests for walls) IF ( k*inc <= nzb_local(j*inc,i*inc) ) THEN flags(k,j,i) = IBSET( flags(k,j,i), 6 ) ELSE ! !-- Bottom wall IF ( (k-1)*inc <= nzb_local(j*inc,i*inc) ) THEN flags(k,j,i) = IBSET( flags(k,j,i), 0 ) ENDIF ! !-- South wall IF ( k*inc <= nzb_local((j-1)*inc,i*inc) ) THEN flags(k,j,i) = IBSET( flags(k,j,i), 2 ) ENDIF ! !-- North wall IF ( k*inc <= nzb_local((j+1)*inc,i*inc) ) THEN flags(k,j,i) = IBSET( flags(k,j,i), 3 ) ENDIF ! !-- Left wall IF ( k*inc <= nzb_local(j*inc,(i-1)*inc) ) THEN flags(k,j,i) = IBSET( flags(k,j,i), 4 ) ENDIF ! !-- Right wall IF ( k*inc <= nzb_local(j*inc,(i+1)*inc) ) THEN flags(k,j,i) = IBSET( flags(k,j,i), 5 ) ENDIF ENDIF ENDDO ENDDO ENDDO ! !-- Test output of flag arrays ! i = nxl_l ! WRITE (9,*) ' ' ! WRITE (9,*) '*** mg level ', l, ' ***', mg_switch_to_pe0_level ! WRITE (9,*) ' inc=', inc, ' i =', nxl_l ! WRITE (9,*) ' nxl_l',nxl_l,' nxr_l=',nxr_l,' nys_l=',nys_l,' nyn_l=',nyn_l ! DO k = nzt_l+1, nzb, -1 ! WRITE (9,'(194(1X,I2))') ( flags(k,j,i), j = nys_l-1, nyn_l+1 ) ! ENDDO inc = inc * 2 ENDDO ENDIF ! !-- In case of topography: limit near-wall mixing length l_wall further: !-- Go through all points of the subdomain one by one and look for the closest !-- surface IF ( TRIM(topography) /= 'flat' ) THEN DO i = nxl, nxr DO j = nys, nyn nzb_si = nzb_s_inner(j,i) vi = vertical_influence(nzb_si) IF ( wall_n(j,i) > 0 ) THEN ! !-- North wall (y distance) DO k = wall_n(j,i), nzb_si l_wall(k,j+1,i) = MIN( l_wall(k,j+1,i), 0.5 * dy ) ENDDO ! !-- Above North wall (yz distance) DO k = nzb_si + 1, nzb_si + vi l_wall(k,j+1,i) = MIN( l_wall(k,j+1,i), & SQRT( 0.25 * dy**2 + & ( zu(k) - zw(nzb_si) )**2 ) ) ENDDO ! !-- Northleft corner (xy distance) IF ( corner_nl(j,i) > 0 ) THEN DO k = corner_nl(j,i), nzb_si l_wall(k,j+1,i-1) = MIN( l_wall(k,j+1,i-1), & 0.5 * SQRT( dx**2 + dy**2 ) ) ENDDO ! !-- Above Northleft corner (xyz distance) DO k = nzb_si + 1, nzb_si + vi l_wall(k,j+1,i-1) = MIN( l_wall(k,j+1,i-1), & SQRT( 0.25 * (dx**2 + dy**2) + & ( zu(k) - zw(nzb_si) )**2 ) ) ENDDO ENDIF ! !-- Northright corner (xy distance) IF ( corner_nr(j,i) > 0 ) THEN DO k = corner_nr(j,i), nzb_si l_wall(k,j+1,i+1) = MIN( l_wall(k,j+1,i+1), & 0.5 * SQRT( dx**2 + dy**2 ) ) ENDDO ! !-- Above northright corner (xyz distance) DO k = nzb_si + 1, nzb_si + vi l_wall(k,j+1,i+1) = MIN( l_wall(k,j+1,i+1), & SQRT( 0.25 * (dx**2 + dy**2) + & ( zu(k) - zw(nzb_si) )**2 ) ) ENDDO ENDIF ENDIF IF ( wall_s(j,i) > 0 ) THEN ! !-- South wall (y distance) DO k = wall_s(j,i), nzb_si l_wall(k,j-1,i) = MIN( l_wall(k,j-1,i), 0.5 * dy ) ENDDO ! !-- Above south wall (yz distance) DO k = nzb_si + 1, & nzb_si + vi l_wall(k,j-1,i) = MIN( l_wall(k,j-1,i), & SQRT( 0.25 * dy**2 + & ( zu(k) - zw(nzb_si) )**2 ) ) ENDDO ! !-- Southleft corner (xy distance) IF ( corner_sl(j,i) > 0 ) THEN DO k = corner_sl(j,i), nzb_si l_wall(k,j-1,i-1) = MIN( l_wall(k,j-1,i-1), & 0.5 * SQRT( dx**2 + dy**2 ) ) ENDDO ! !-- Above southleft corner (xyz distance) DO k = nzb_si + 1, nzb_si + vi l_wall(k,j-1,i-1) = MIN( l_wall(k,j-1,i-1), & SQRT( 0.25 * (dx**2 + dy**2) + & ( zu(k) - zw(nzb_si) )**2 ) ) ENDDO ENDIF ! !-- Southright corner (xy distance) IF ( corner_sr(j,i) > 0 ) THEN DO k = corner_sr(j,i), nzb_si l_wall(k,j-1,i+1) = MIN( l_wall(k,j-1,i+1), & 0.5 * SQRT( dx**2 + dy**2 ) ) ENDDO ! !-- Above southright corner (xyz distance) DO k = nzb_si + 1, nzb_si + vi l_wall(k,j-1,i+1) = MIN( l_wall(k,j-1,i+1), & SQRT( 0.25 * (dx**2 + dy**2) + & ( zu(k) - zw(nzb_si) )**2 ) ) ENDDO ENDIF ENDIF IF ( wall_l(j,i) > 0 ) THEN ! !-- Left wall (x distance) DO k = wall_l(j,i), nzb_si l_wall(k,j,i-1) = MIN( l_wall(k,j,i-1), 0.5 * dx ) ENDDO ! !-- Above left wall (xz distance) DO k = nzb_si + 1, nzb_si + vi l_wall(k,j,i-1) = MIN( l_wall(k,j,i-1), & SQRT( 0.25 * dx**2 + & ( zu(k) - zw(nzb_si) )**2 ) ) ENDDO ENDIF IF ( wall_r(j,i) > 0 ) THEN ! !-- Right wall (x distance) DO k = wall_r(j,i), nzb_si l_wall(k,j,i+1) = MIN( l_wall(k,j,i+1), 0.5 * dx ) ENDDO ! !-- Above right wall (xz distance) DO k = nzb_si + 1, nzb_si + vi l_wall(k,j,i+1) = MIN( l_wall(k,j,i+1), & SQRT( 0.25 * dx**2 + & ( zu(k) - zw(nzb_si) )**2 ) ) ENDDO ENDIF ENDDO ENDDO ENDIF ! !-- Multiplication with wall_adjustment_factor l_wall = wall_adjustment_factor * l_wall ! !-- Need to set lateral boundary conditions for l_wall CALL exchange_horiz( l_wall ) DEALLOCATE( corner_nl, corner_nr, corner_sl, corner_sr, nzb_local, & nzb_tmp, vertical_influence, wall_l, wall_n, wall_r, wall_s ) END SUBROUTINE init_grid