source: palm/trunk/SOURCE/init_3d_model.f90 @ 4856

Last change on this file since 4856 was 4848, checked in by gronemeier, 3 years ago

bugfix: removed syn_turb_gen from restart files, replaced use_syn_turb_gen by syn_turb_gen

  • Property svn:keywords set to Id
  • Property svn:mergeinfo set to (toggle deleted branches)
    /palm/branches/chemistry/SOURCE/init_3d_model.f902047-3190,​3218-3297
    /palm/branches/rans/SOURCE/init_3d_model.f902078-3128
    /palm/branches/resler/SOURCE/init_3d_model.f902023-4492
    /palm/branches/salsa/SOURCE/init_3d_model.f902503-3581
    /palm/branches/forwind/SOURCE/init_3d_model.f901564-1913
    /palm/branches/mosaik_M2/init_3d_model.f902360-3471
    /palm/branches/palm4u/SOURCE/init_3d_model.f902540-2692
File size: 84.4 KB
RevLine 
[1682]1!> @file init_3d_model.f90
[4648]2!--------------------------------------------------------------------------------------------------!
[2696]3! This file is part of the PALM model system.
[1036]4!
[4648]5! PALM is free software: you can redistribute it and/or modify it under the terms of the GNU General
6! Public License as published by the Free Software Foundation, either version 3 of the License, or
7! (at your option) any later version.
[1036]8!
[4648]9! PALM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10! implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11! Public License for more details.
[1036]12!
[4648]13! You should have received a copy of the GNU General Public License along with PALM. If not, see
14! <http://www.gnu.org/licenses/>.
[1036]15!
[4828]16! Copyright 1997-2021 Leibniz Universitaet Hannover
[4648]17!--------------------------------------------------------------------------------------------------!
[1036]18!
[254]19! Current revisions:
[732]20! ------------------
[4648]21!
22!
[2233]23! Former revisions:
24! -----------------
25! $Id: init_3d_model.f90 4848 2021-01-21 15:51:51Z raasch $
[4848]26! replaced use_syn_turb_gen by syn_turb_gen
27!
28! 4845 2021-01-18 11:15:37Z raasch
[4845]29! removed allocation and initialization of arrays required for radiation boundary conditions
30!
31! 4828 2021-01-05 11:21:41Z Giersch
[4791]32! Add possibility to initialize surface flux of passive scalar via static driver
33!
34! 4783 2020-11-13 13:58:45Z raasch
[4783]35! bugfix for reading restart data with MPI-I/O (does not work with blockwise I/O)
36!
37! 4680 2020-09-16 10:20:34Z gronemeier
[4680]38! Add option to fix date or time of the simulation
39!
40! 4671 2020-09-09 20:27:58Z pavelkrc
[4671]41! Implementation of downward facing USM and LSM surfaces
42!
43! 4648 2020-08-25 07:52:08Z raasch
[4648]44! file re-formatted to follow the PALM coding standard
45!
46! 4548 2020-05-28 19:36:45Z suehring
[4548]47! Bugfix, move call for lsf_forcing_surf after lsf_init is called
[4648]48!
[4548]49! 4514 2020-04-30 16:29:59Z suehring
[4648]50! Add possibility to initialize surface sensible and latent heat fluxes via a static driver.
51!
[4514]52! 4493 2020-04-10 09:49:43Z pavelkrc
[4648]53! Overwrite u_init, v_init, pt_init, q_init and s_init with hom for all cyclic_fill-cases, not only
54! for turbulent_inflow = .TRUE.
55!
[4365]56! 4360 2020-01-07 11:25:50Z suehring
[4648]57! Introduction of wall_flags_total_0, which currently sets bits based on static topography
58! information used in wall_flags_static_0
59!
[4346]60! 4329 2019-12-10 15:46:36Z motisi
[4329]61! Renamed wall_flags_0 to wall_flags_static_0
[4648]62!
[4329]63! 4286 2019-10-30 16:01:14Z resler
[4227]64! implement new palm_date_time_mod
[4648]65!
[4227]66! 4223 2019-09-10 09:20:47Z gronemeier
[4648]67! Deallocate temporary string array since it may be re-used to read different input data in other
68! modules
69!
[4187]70! 4186 2019-08-23 16:06:14Z suehring
[4648]71! Design change, use variables defined in netcdf_data_input_mod to read netcd variables rather than
72! define local ones.
73!
[4186]74! 4185 2019-08-23 13:49:38Z oliver.maas
[4185]75! For initializing_actions = ' cyclic_fill':
[4648]76! Overwrite u_init, v_init, pt_init, q_init and s_init with the (temporally) and horizontally
77! averaged vertical profiles from the end of the prerun, because these profiles shall be used as the
78! basic state for the rayleigh damping and the pt_damping.
79!
[4185]80! 4182 2019-08-22 15:20:23Z scharf
[4182]81! Corrected "Former revisions" section
[4648]82!
[4182]83! 4168 2019-08-16 13:50:17Z suehring
[4168]84! Replace function get_topography_top_index by topo_top_ind
[4648]85!
[4168]86! 4151 2019-08-09 08:24:30Z suehring
[4151]87! Add netcdf directive around input calls (fix for last commit)
[4648]88!
[4151]89! 4150 2019-08-08 20:00:47Z suehring
[4648]90! Input of additional surface variables independent on land- or urban-surface model
91!
[4150]92! 4131 2019-08-02 11:06:18Z monakurppa
[4131]93! Allocate sums and sums_l to allow profile output for salsa variables.
[4648]94!
[4131]95! 4130 2019-08-01 13:04:13Z suehring
[4648]96! Effectively reduce 3D initialization to 1D initial profiles. This is because 3D initialization
97! produces structures in the w-component that are correlated with the processor grid for some
98! unknown reason
99!
[4130]100! 4090 2019-07-11 15:06:47Z Giersch
[4090]101! Unused variables removed
[4648]102!
[4090]103! 4088 2019-07-11 13:57:56Z Giersch
[4088]104! Pressure and density profile calculations revised using basic functions
[4648]105!
[4088]106! 4048 2019-06-21 21:00:21Z knoop
[4028]107! Further modularization of particle code components
[4648]108!
[4028]109! 4017 2019-06-06 12:16:46Z schwenkel
[4648]110! Convert most location messages to debug messages to reduce output in job logfile to a minimum
111!
[3939]112! unused variable removed
[4648]113!
[3939]114! 3937 2019-04-29 15:09:07Z suehring
[4648]115! Move initialization of synthetic turbulence generator behind initialization of offline nesting.
116! Remove call for stg_adjust, as this is now already done in stg_init.
117!
[3937]118! 3900 2019-04-16 15:17:43Z suehring
[3900]119! Fix problem with LOD = 2 initialization
[4648]120!
[3900]121! 3885 2019-04-11 11:29:34Z kanani
[4648]122! Changes related to global restructuring of location messages and introduction of additional debug
123! messages
124!
[3885]125! 3849 2019-04-01 16:35:16Z knoop
[3747]126! Move initialization of rmask before initializing user_init_arrays
[4648]127!
[3747]128! 3711 2019-01-31 13:44:26Z knoop
[3711]129! Introduced module_interface_init_checks for post-init checks in modules
[4648]130!
[3711]131! 3700 2019-01-26 17:03:42Z knoop
[3685]132! Some interface calls moved to module_interface + cleanup
[4648]133!
[3685]134! 3648 2019-01-02 16:35:46Z suehring
[3648]135! Rename subroutines for surface-data output
[3569]136!
[4182]137! Revision 1.1  1998/03/09 16:22:22  raasch
138! Initial revision
139!
140!
[1]141! Description:
142! ------------
[1682]143!> Allocation of arrays and initialization of the 3D model via
144!> a) pre-run the 1D model
145!> or
146!> b) pre-set constant linear profiles
147!> or
148!> c) read values of a previous run
[4648]149!--------------------------------------------------------------------------------------------------!
[1682]150 SUBROUTINE init_3d_model
[1]151
[3298]152
[667]153    USE advec_ws
[1320]154
[1]155    USE arrays_3d
[1849]156
[4648]157    USE basic_constants_and_equations_mod,                                                         &
158        ONLY:  barometric_formula, c_p, exner_function, exner_function_invers, g,                  &
159               ideal_gas_law_rho, ideal_gas_law_rho_pt, l_v, pi
[3274]160
[4648]161    USE bulk_cloud_model_mod,                                                                      &
[3685]162        ONLY:  bulk_cloud_model
[3274]163
[4648]164    USE chem_modules,                                                                              &
[3685]165        ONLY:  max_pr_cs ! ToDo: this dependency needs to be removed cause it is ugly #new_dom
[3298]166
[1]167    USE control_parameters
[3298]168
[4648]169    USE grid_variables,                                                                            &
[2037]170        ONLY:  dx, dy, ddx2_mg, ddy2_mg
[2817]171
[1]172    USE indices
[3469]173
[1320]174    USE kinds
[4648]175
176    USE lsf_nudging_mod,                                                                           &
[3685]177        ONLY:  ls_forcing_surf
[1849]178
[4648]179    USE model_1d_mod,                                                                              &
[3241]180        ONLY:  init_1d_model, l1d, u1d, v1d
[2338]181
[4648]182    USE module_interface,                                                                          &
183        ONLY:  module_interface_init_arrays,                                                       &
184               module_interface_init,                                                              &
[3711]185               module_interface_init_checks
[3685]186
[4648]187    USE multi_agent_system_mod,                                                                    &
[3159]188        ONLY:  agents_active, mas_init
189
[4648]190    USE netcdf_interface,                                                                          &
[3700]191        ONLY:  dots_max
[2696]192
[4648]193    USE netcdf_data_input_mod,                                                                     &
194        ONLY:  char_fill,                                                                          &
195               check_existence,                                                                    &
196               close_input_file,                                                                   &
197               get_attribute,                                                                      &
198               get_variable,                                                                       &
199               init_3d,                                                                            &
200               input_pids_static,                                                                  &
201               inquire_num_variables,                                                              &
202               inquire_variable_names,                                                             &
203               input_file_static,                                                                  &
204               netcdf_data_input_init_3d,                                                          &
205               num_var_pids,                                                                       &
206               open_read_file,                                                                     &
207               pids_id,                                                                            &
208               real_2d,                                                                            &
[4186]209               vars_pids
[4648]210
211    USE nesting_offl_mod,                                                                          &
[3347]212        ONLY:  nesting_offl_init
[3294]213
[4648]214    USE palm_date_time_mod,                                                                        &
[4680]215        ONLY:  init_date_time
[4227]216
[1]217    USE pegrid
[3298]218
[3524]219#if defined( __parallel )
[4648]220    USE pmc_interface,                                                                             &
[2934]221        ONLY:  nested_run
[3524]222#endif
[2934]223
[4648]224    USE random_function_mod
[3685]225
[4648]226    USE random_generator_parallel,                                                                 &
[2172]227        ONLY:  init_parallel_random_generator
[3685]228
[4648]229    USE read_restart_data_mod,                                                                     &
230        ONLY:  rrd_local, rrd_read_parts_of_global
[3685]231
[4648]232    USE statistics,                                                                                &
233        ONLY:  hom, hom_sum, mean_surface_level_height, pr_palm, rmask, statistic_regions, sums,   &
234               sums_divnew_l, sums_divold_l, sums_l, sums_l_l, sums_wsts_bc_l, ts_value,           &
[1833]235               weight_pres, weight_substep
[2259]236
[4648]237    USE synthetic_turbulence_generator_mod,                                                        &
[4848]238        ONLY:  stg_init
[3685]239
[4648]240    USE surface_layer_fluxes_mod,                                                                  &
[1691]241        ONLY:  init_surface_layer_fluxes
[2232]242
[4648]243    USE surface_mod,                                                                               &
244        ONLY :  init_single_surface_properties,                                                    &
245                init_surface_arrays,                                                               &
246                init_surfaces,                                                                     &
247                surf_def_h,                                                                        &
248                surf_def_v,                                                                        &
249                surf_lsm_h,                                                                        &
[4168]250                surf_usm_h
[3685]251
[3849]252#if defined( _OPENACC )
[4648]253    USE surface_mod,                                                                               &
[3849]254        ONLY :  bc_h
255#endif
256
[4648]257    USE surface_data_output_mod,                                                                   &
[3648]258        ONLY:  surface_data_output_init
[3685]259
[2007]260    USE transpose_indices
[1]261
[4648]262
[1]263    IMPLICIT NONE
[4648]264
[4150]265    INTEGER(iwp) ::  i                    !< grid index in x direction
266    INTEGER(iwp) ::  ind_array(1)         !< dummy used to determine start index for external pressure forcing
267    INTEGER(iwp) ::  j                    !< grid index in y direction
268    INTEGER(iwp) ::  k                    !< grid index in z direction
269    INTEGER(iwp) ::  k_surf               !< surface level index
[4648]270    INTEGER(iwp) ::  l                    !< running index over surface orientation
271    INTEGER(iwp) ::  m                    !< index of surface element in surface data type
[4150]272    INTEGER(iwp) ::  nz_u_shift           !< topography-top index on u-grid, used to vertically shift initial profiles
273    INTEGER(iwp) ::  nz_v_shift           !< topography-top index on v-grid, used to vertically shift initial profiles
274    INTEGER(iwp) ::  nz_w_shift           !< topography-top index on w-grid, used to vertically shift initial profiles
275    INTEGER(iwp) ::  nz_s_shift           !< topography-top index on scalar-grid, used to vertically shift initial profiles
276    INTEGER(iwp) ::  nz_u_shift_l         !< topography-top index on u-grid, used to vertically shift initial profiles
277    INTEGER(iwp) ::  nz_v_shift_l         !< topography-top index on v-grid, used to vertically shift initial profiles
278    INTEGER(iwp) ::  nz_w_shift_l         !< topography-top index on w-grid, used to vertically shift initial profiles
279    INTEGER(iwp) ::  nz_s_shift_l         !< topography-top index on scalar-grid, used to vertically shift initial profiles
280    INTEGER(iwp) ::  nzt_l                !< index of top PE boundary for multigrid level
281    INTEGER(iwp) ::  sr                   !< index of statistic region
[1]282
[4648]283    INTEGER(iwp), DIMENSION(:), ALLOCATABLE   ::  ngp_2dh_l  !< toal number of horizontal grid points in statistical region on
284                                                             !< subdomain
[1]285
[4648]286    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  ngp_2dh_outer_l    !< number of horizontal non-wall bounded grid points on
287                                                                     !< subdomain
288    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  ngp_2dh_s_inner_l  !< number of horizontal non-topography grid points on
289                                                                     !< subdomain
[1]290
[2037]291    REAL(wp) ::  dx_l !< grid spacing along x on different multigrid level
292    REAL(wp) ::  dy_l !< grid spacing along y on different multigrid level
293
[4648]294    REAL(wp), DIMENSION(:), ALLOCATABLE ::  init_l                       !< dummy array used for averaging 3D data to obtain
295                                                                         !< inital profiles
296    REAL(wp), DIMENSION(:), ALLOCATABLE ::  mean_surface_level_height_l  !< mean surface level height on subdomain
297    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ngp_3d_inner_l               !< total number of non-topography grid points on subdomain
298    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ngp_3d_inner_tmp             !< total number of non-topography grid points
299    REAL(wp), DIMENSION(:), ALLOCATABLE ::  p_hydrostatic                !< hydrostatic pressure
300
[3547]301    REAL(wp), DIMENSION(1:3) ::  volume_flow_area_l     !< area of lateral and top model domain surface on local subdomain
302    REAL(wp), DIMENSION(1:3) ::  volume_flow_initial_l  !< initial volume flow into model domain
[1]303
[4648]304    TYPE(real_2d) ::  tmp_2d !< temporary variable to input additional surface-data from static file
[1]305
[3987]306    CALL location_message( 'model initialization', 'start' )
[4227]307!
308!-- Set reference date-time
[4680]309    CALL init_date_time( date_time_str=origin_date_time,                                           &
310                         use_fixed_date=use_fixed_date,                                            &
311                         use_fixed_time=use_fixed_time )
[3987]312
313    IF ( debug_output )  CALL debug_message( 'allocating arrays', 'start' )
[1]314!
315!-- Allocate arrays
[4648]316    ALLOCATE( mean_surface_level_height(0:statistic_regions),                                      &
317              mean_surface_level_height_l(0:statistic_regions),                                    &
318              ngp_2dh(0:statistic_regions), ngp_2dh_l(0:statistic_regions),                        &
319              ngp_3d(0:statistic_regions),                                                         &
320              ngp_3d_inner(0:statistic_regions),                                                   &
321              ngp_3d_inner_l(0:statistic_regions),                                                 &
322              ngp_3d_inner_tmp(0:statistic_regions),                                               &
323              sums_divnew_l(0:statistic_regions),                                                  &
[1]324              sums_divold_l(0:statistic_regions) )
[1195]325    ALLOCATE( dp_smooth_factor(nzb:nzt), rdf(nzb+1:nzt), rdf_sc(nzb+1:nzt) )
[4648]326    ALLOCATE( ngp_2dh_outer(nzb:nzt+1,0:statistic_regions),                                        &
327              ngp_2dh_outer_l(nzb:nzt+1,0:statistic_regions),                                      &
328              ngp_2dh_s_inner(nzb:nzt+1,0:statistic_regions),                                      &
329              ngp_2dh_s_inner_l(nzb:nzt+1,0:statistic_regions),                                    &
330              rmask(nysg:nyng,nxlg:nxrg,0:statistic_regions),                                      &
331              sums(nzb:nzt+1,pr_palm+max_pr_user+max_pr_cs+max_pr_salsa),                          &
[4131]332              sums_l(nzb:nzt+1,pr_palm+max_pr_user+max_pr_cs+max_pr_salsa,0:threads_per_task-1),   &
[4648]333              sums_l_l(nzb:nzt+1,0:statistic_regions,0:threads_per_task-1),                        &
[3700]334              sums_wsts_bc_l(nzb:nzt+1,0:statistic_regions) )
335    ALLOCATE( ts_value(dots_max,0:statistic_regions) )
[978]336    ALLOCATE( ptdf_x(nxlg:nxrg), ptdf_y(nysg:nyng) )
[1]337
[4648]338    ALLOCATE( d(nzb+1:nzt,nys:nyn,nxl:nxr),                                                        &
339              p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                    &
[1010]340              tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
341
[4648]342    ALLOCATE( pt_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                 &
343              pt_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                 &
344              u_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                  &
345              u_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                  &
346              u_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                  &
347              v_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                  &
348              v_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                  &
349              v_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                  &
350              w_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                  &
351              w_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                                  &
[667]352              w_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
[1788]353    IF (  .NOT.  neutral )  THEN
[1032]354       ALLOCATE( pt_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
355    ENDIF
[673]356!
[3747]357!-- Pre-set masks for regional statistics. Default is the total model domain.
[4648]358!-- Ghost points are excluded because counting values at the ghost boundaries would bias the
359!-- statistics.
[3747]360    rmask = 1.0_wp
361    rmask(:,nxlg:nxl-1,:) = 0.0_wp;  rmask(:,nxr+1:nxrg,:) = 0.0_wp
362    rmask(nysg:nys-1,:,:) = 0.0_wp;  rmask(nyn+1:nyng,:,:) = 0.0_wp
363!
[4648]364!-- Following array is required for perturbation pressure within the iterative pressure solvers. For
365!-- the multistep schemes (Runge-Kutta), array p holds the weighted average of the substeps and
366!-- cannot be used in the Poisson solver.
[707]367    IF ( psolver == 'sor' )  THEN
368       ALLOCATE( p_loc(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
[1575]369    ELSEIF ( psolver(1:9) == 'multigrid' )  THEN
[707]370!
371!--    For performance reasons, multigrid is using one ghost layer only
372       ALLOCATE( p_loc(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
[673]373    ENDIF
[1]374
[1111]375!
376!-- Array for storing constant coeffficients of the tridiagonal solver
377    IF ( psolver == 'poisfft' )  THEN
[1212]378       ALLOCATE( tri(nxl_z:nxr_z,nys_z:nyn_z,0:nz-1,2) )
[1111]379       ALLOCATE( tric(nxl_z:nxr_z,nys_z:nyn_z,0:nz-1) )
380    ENDIF
381
[1960]382    IF ( humidity )  THEN
[1]383!
[1960]384!--    3D-humidity
[4648]385       ALLOCATE( q_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                               &
386                 q_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                               &
387                 q_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                               &
388                 vpt_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
389    ENDIF
390
[1960]391    IF ( passive_scalar )  THEN
[1]392
[1960]393!
394!--    3D scalar arrays
[4648]395       ALLOCATE( s_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                               &
396                 s_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                               &
[1960]397                 s_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
[3636]398
[1960]399    ENDIF
400
[1]401!
[4648]402!-- Allocate and set 1d-profiles for Stokes drift velocity. It may be set to non-zero values later
403!-- in ocean_init.
404    ALLOCATE( u_stokes_zu(nzb:nzt+1), u_stokes_zw(nzb:nzt+1),                                      &
[3302]405              v_stokes_zu(nzb:nzt+1), v_stokes_zw(nzb:nzt+1) )
406    u_stokes_zu(:) = 0.0_wp
407    u_stokes_zw(:) = 0.0_wp
408    v_stokes_zu(:) = 0.0_wp
409    v_stokes_zw(:) = 0.0_wp
410
411!
[2037]412!-- Allocation of anelastic and Boussinesq approximation specific arrays
413    ALLOCATE( p_hydrostatic(nzb:nzt+1) )
414    ALLOCATE( rho_air(nzb:nzt+1) )
415    ALLOCATE( rho_air_zw(nzb:nzt+1) )
416    ALLOCATE( drho_air(nzb:nzt+1) )
417    ALLOCATE( drho_air_zw(nzb:nzt+1) )
418!
[4648]419!-- Density profile calculation for anelastic and Boussinesq approximation.
420!-- In case of a Boussinesq approximation, a constant density is calculated mainly for output
421!-- purposes. This density does not need to be considered in the model's system of equations.
[4088]422    IF ( TRIM( approximation ) == 'anelastic' )  THEN
[2037]423       DO  k = nzb, nzt+1
[4648]424          p_hydrostatic(k) = barometric_formula(zu(k), pt_surface *                                &
425                                                exner_function(surface_pressure * 100.0_wp),       &
426                                                surface_pressure * 100.0_wp)
427
[4088]428          rho_air(k) = ideal_gas_law_rho_pt(p_hydrostatic(k), pt_init(k))
[2037]429       ENDDO
[4648]430
[2037]431       DO  k = nzb, nzt
432          rho_air_zw(k) = 0.5_wp * ( rho_air(k) + rho_air(k+1) )
433       ENDDO
[4648]434
435       rho_air_zw(nzt+1)  = rho_air_zw(nzt) + 2.0_wp * ( rho_air(nzt+1) - rho_air_zw(nzt)  )
436
[2037]437    ELSE
[2252]438       DO  k = nzb, nzt+1
[4648]439          p_hydrostatic(k) = barometric_formula(zu(nzb), pt_surface *                              &
440                                                exner_function(surface_pressure * 100.0_wp),       &
441                                                surface_pressure * 100.0_wp)
[4088]442
443          rho_air(k) = ideal_gas_law_rho_pt(p_hydrostatic(k), pt_init(nzb))
[2252]444       ENDDO
[4648]445
[2252]446       DO  k = nzb, nzt
447          rho_air_zw(k) = 0.5_wp * ( rho_air(k) + rho_air(k+1) )
448       ENDDO
[4648]449
450       rho_air_zw(nzt+1)  = rho_air_zw(nzt) + 2.0_wp * ( rho_air(nzt+1) - rho_air_zw(nzt)  )
451
[2037]452    ENDIF
[2696]453!
[4648]454!-- Compute the inverse density array in order to avoid expencive divisions
[2037]455    drho_air    = 1.0_wp / rho_air
456    drho_air_zw = 1.0_wp / rho_air_zw
457
458!
459!-- Allocation of flux conversion arrays
460    ALLOCATE( heatflux_input_conversion(nzb:nzt+1) )
461    ALLOCATE( waterflux_input_conversion(nzb:nzt+1) )
462    ALLOCATE( momentumflux_input_conversion(nzb:nzt+1) )
463    ALLOCATE( heatflux_output_conversion(nzb:nzt+1) )
464    ALLOCATE( waterflux_output_conversion(nzb:nzt+1) )
465    ALLOCATE( momentumflux_output_conversion(nzb:nzt+1) )
466
467!
[4648]468!-- Calculate flux conversion factors according to approximation and in-/output mode
[2037]469    DO  k = nzb, nzt+1
470
471        IF ( TRIM( flux_input_mode ) == 'kinematic' )  THEN
472            heatflux_input_conversion(k)      = rho_air_zw(k)
473            waterflux_input_conversion(k)     = rho_air_zw(k)
474            momentumflux_input_conversion(k)  = rho_air_zw(k)
475        ELSEIF ( TRIM( flux_input_mode ) == 'dynamic' ) THEN
[3274]476            heatflux_input_conversion(k)      = 1.0_wp / c_p
[2037]477            waterflux_input_conversion(k)     = 1.0_wp / l_v
478            momentumflux_input_conversion(k)  = 1.0_wp
479        ENDIF
480
481        IF ( TRIM( flux_output_mode ) == 'kinematic' )  THEN
482            heatflux_output_conversion(k)     = drho_air_zw(k)
483            waterflux_output_conversion(k)    = drho_air_zw(k)
484            momentumflux_output_conversion(k) = drho_air_zw(k)
485        ELSEIF ( TRIM( flux_output_mode ) == 'dynamic' ) THEN
[3274]486            heatflux_output_conversion(k)     = c_p
[2037]487            waterflux_output_conversion(k)    = l_v
488            momentumflux_output_conversion(k) = 1.0_wp
489        ENDIF
490
491        IF ( .NOT. humidity ) THEN
492            waterflux_input_conversion(k)  = 1.0_wp
493            waterflux_output_conversion(k) = 1.0_wp
494        ENDIF
495
496    ENDDO
497
498!
[4648]499!-- In case of multigrid method, compute grid lengths and grid factors for the grid levels with
500!-- respective density on each grid.
[2037]501    IF ( psolver(1:9) == 'multigrid' )  THEN
502
503       ALLOCATE( ddx2_mg(maximum_grid_level) )
504       ALLOCATE( ddy2_mg(maximum_grid_level) )
505       ALLOCATE( dzu_mg(nzb+1:nzt+1,maximum_grid_level) )
506       ALLOCATE( dzw_mg(nzb+1:nzt+1,maximum_grid_level) )
507       ALLOCATE( f1_mg(nzb+1:nzt,maximum_grid_level) )
508       ALLOCATE( f2_mg(nzb+1:nzt,maximum_grid_level) )
509       ALLOCATE( f3_mg(nzb+1:nzt,maximum_grid_level) )
510       ALLOCATE( rho_air_mg(nzb:nzt+1,maximum_grid_level) )
511       ALLOCATE( rho_air_zw_mg(nzb:nzt+1,maximum_grid_level) )
512
513       dzu_mg(:,maximum_grid_level) = dzu
514       rho_air_mg(:,maximum_grid_level) = rho_air
[4648]515!
516!--    Next line to ensure an equally spaced grid.
[2037]517       dzu_mg(1,maximum_grid_level) = dzu(2)
[4648]518       rho_air_mg(nzb,maximum_grid_level) = rho_air(nzb) + (rho_air(nzb) - rho_air(nzb+1))
[2037]519
520       dzw_mg(:,maximum_grid_level) = dzw
521       rho_air_zw_mg(:,maximum_grid_level) = rho_air_zw
522       nzt_l = nzt
523       DO  l = maximum_grid_level-1, 1, -1
524           dzu_mg(nzb+1,l) = 2.0_wp * dzu_mg(nzb+1,l+1)
525           dzw_mg(nzb+1,l) = 2.0_wp * dzw_mg(nzb+1,l+1)
[4648]526           rho_air_mg(nzb,l)    = rho_air_mg(nzb,l+1)    + ( rho_air_mg(nzb,l+1)    -              &
527                                                             rho_air_mg(nzb+1,l+1)    )
528           rho_air_zw_mg(nzb,l) = rho_air_zw_mg(nzb,l+1) + ( rho_air_zw_mg(nzb,l+1) -              &
529                                                             rho_air_zw_mg(nzb+1,l+1) )
[2037]530           rho_air_mg(nzb+1,l)    = rho_air_mg(nzb+1,l+1)
531           rho_air_zw_mg(nzb+1,l) = rho_air_zw_mg(nzb+1,l+1)
532           nzt_l = nzt_l / 2
533           DO  k = 2, nzt_l+1
534              dzu_mg(k,l) = dzu_mg(2*k-2,l+1) + dzu_mg(2*k-1,l+1)
535              dzw_mg(k,l) = dzw_mg(2*k-2,l+1) + dzw_mg(2*k-1,l+1)
536              rho_air_mg(k,l)    = rho_air_mg(2*k-1,l+1)
537              rho_air_zw_mg(k,l) = rho_air_zw_mg(2*k-1,l+1)
538           ENDDO
539       ENDDO
540
541       nzt_l = nzt
542       dx_l  = dx
543       dy_l  = dy
544       DO  l = maximum_grid_level, 1, -1
545          ddx2_mg(l) = 1.0_wp / dx_l**2
546          ddy2_mg(l) = 1.0_wp / dy_l**2
547          DO  k = nzb+1, nzt_l
548             f2_mg(k,l) = rho_air_zw_mg(k,l) / ( dzu_mg(k+1,l) * dzw_mg(k,l) )
549             f3_mg(k,l) = rho_air_zw_mg(k-1,l) / ( dzu_mg(k,l)   * dzw_mg(k,l) )
[4648]550             f1_mg(k,l) = 2.0_wp * ( ddx2_mg(l) + ddy2_mg(l) )                                     &
[2037]551                          * rho_air_mg(k,l) + f2_mg(k,l) + f3_mg(k,l)
552          ENDDO
553          nzt_l = nzt_l / 2
554          dx_l  = dx_l * 2.0_wp
555          dy_l  = dy_l * 2.0_wp
556       ENDDO
557
558    ENDIF
559
560!
[1299]561!-- 1D-array for large scale subsidence velocity
[1361]562    IF ( .NOT. ALLOCATED( w_subs ) )  THEN
563       ALLOCATE ( w_subs(nzb:nzt+1) )
564       w_subs = 0.0_wp
565    ENDIF
[1299]566
567!
[1]568!-- Initial assignment of the pointers
[1032]569    IF ( .NOT. neutral )  THEN
570       pt => pt_1;  pt_p => pt_2;  tpt_m => pt_3
571    ELSE
572       pt => pt_1;  pt_p => pt_1;  tpt_m => pt_3
573    ENDIF
[1001]574    u  => u_1;   u_p  => u_2;   tu_m  => u_3
575    v  => v_1;   v_p  => v_2;   tv_m  => v_3
576    w  => w_1;   w_p  => w_2;   tw_m  => w_3
[1]577
[1960]578    IF ( humidity )  THEN
[1001]579       q => q_1;  q_p => q_2;  tq_m => q_3
[3274]580       vpt  => vpt_1
[1001]581    ENDIF
[4648]582
[1960]583    IF ( passive_scalar )  THEN
584       s => s_1;  s_p => s_2;  ts_m => s_3
[4648]585    ENDIF
[1]586
587!
[2696]588!-- Initialize surface arrays
[2232]589    CALL init_surface_arrays
590!
[3294]591!-- Allocate arrays for other modules
[3685]592    CALL module_interface_init_arrays
[1551]593
[1914]594
[2320]595!
[4648]596!-- Allocate arrays containing the RK coefficient for calculation of perturbation pressure and
597!-- turbulent fluxes. At this point values are set for pressure calculation during initialization
598!-- (where no timestep is done). Further below the values needed within the timestep scheme will be
599!-- set.
600    ALLOCATE( weight_substep(1:intermediate_timestep_count_max),                                   &
[1878]601              weight_pres(1:intermediate_timestep_count_max) )
[1340]602    weight_substep = 1.0_wp
603    weight_pres    = 1.0_wp
[1918]604    intermediate_timestep_count = 0  ! needed when simulated_time = 0.0
[4648]605
[3987]606    IF ( debug_output )  CALL debug_message( 'allocating arrays', 'end' )
[1918]607
[673]608!
[3014]609!-- Initialize time series
610    ts_value = 0.0_wp
611
612!
[1918]613!-- Initialize local summation arrays for routine flow_statistics.
[4648]614!-- This is necessary because they may not yet have been initialized when they are called from
615!-- flow_statistics (or - depending on the chosen model run - are never initialized)
616    sums_divnew_l  = 0.0_wp
617    sums_divold_l  = 0.0_wp
618    sums_l_l       = 0.0_wp
619    sums_wsts_bc_l = 0.0_wp
620
[1918]621!
[1]622!-- Initialize model variables
[4648]623    IF ( TRIM( initializing_actions ) /= 'read_restart_data'  .AND.                                &
[328]624         TRIM( initializing_actions ) /= 'cyclic_fill' )  THEN
[1]625!
[2696]626!--    Initialization with provided input data derived from larger-scale model
627       IF ( INDEX( initializing_actions, 'inifor' ) /= 0 )  THEN
[3987]628          IF ( debug_output )  CALL debug_message( 'initializing with INIFOR', 'start' )
[2696]629!
[4648]630!--       Read initial 1D profiles or 3D data from NetCDF file, depending on the provided
631!--       level-of-detail.
632!--       At the moment, only u, v, w, pt and q are provided.
[2696]633          CALL netcdf_data_input_init_3d
634!
[4648]635!--       Please note, Inifor provides data from nzb+1 to nzt.
636!--       Bottom and top boundary conditions for Inifor profiles are already set (just after
637!--       reading), so that this is not necessary here.
638!--       Depending on the provided level-of-detail, initial Inifor data is either stored on data
639!--       type (lod=1), or directly on 3D arrays (lod=2).
640!--       In order to obtain also initial profiles in case of lod=2 (which is required for e.g.
641!--       damping), average over 3D data.
[3182]642          IF( init_3d%lod_u == 1 )  THEN
643             u_init = init_3d%u_init
[4648]644          ELSEIF( init_3d%lod_u == 2 )  THEN
645             ALLOCATE( init_l(nzb:nzt+1) )
[3182]646             DO  k = nzb, nzt+1
647                init_l(k) = SUM( u(k,nys:nyn,nxl:nxr) )
648             ENDDO
649             init_l = init_l / REAL( ( nx + 1 ) * ( ny + 1 ), KIND = wp )
[1384]650
[3182]651#if defined( __parallel )
[4648]652             CALL MPI_ALLREDUCE( init_l, u_init, nzt+1-nzb+1, MPI_REAL, MPI_SUM, comm2d, ierr )
[3182]653#else
654             u_init = init_l
655#endif
656             DEALLOCATE( init_l )
[3051]657
[2696]658          ENDIF
[4648]659
660          IF( init_3d%lod_v == 1 )  THEN
[3182]661             v_init = init_3d%v_init
[4648]662          ELSEIF( init_3d%lod_v == 2 )  THEN
663             ALLOCATE( init_l(nzb:nzt+1) )
[3182]664             DO  k = nzb, nzt+1
665                init_l(k) = SUM( v(k,nys:nyn,nxl:nxr) )
666             ENDDO
667             init_l = init_l / REAL( ( nx + 1 ) * ( ny + 1 ), KIND = wp )
[2696]668
[3182]669#if defined( __parallel )
[4648]670             CALL MPI_ALLREDUCE( init_l, v_init, nzt+1-nzb+1, MPI_REAL, MPI_SUM, comm2d, ierr )
[3182]671#else
672             v_init = init_l
673#endif
674             DEALLOCATE( init_l )
675          ENDIF
676          IF( .NOT. neutral )  THEN
677             IF( init_3d%lod_pt == 1 )  THEN
678                pt_init = init_3d%pt_init
[4648]679             ELSEIF( init_3d%lod_pt == 2 )  THEN
680                ALLOCATE( init_l(nzb:nzt+1) )
[3182]681                DO  k = nzb, nzt+1
682                   init_l(k) = SUM( pt(k,nys:nyn,nxl:nxr) )
683                ENDDO
684                init_l = init_l / REAL( ( nx + 1 ) * ( ny + 1 ), KIND = wp )
685
686#if defined( __parallel )
[4648]687                CALL MPI_ALLREDUCE( init_l, pt_init, nzt+1-nzb+1, MPI_REAL, MPI_SUM, comm2d, ierr )
[3182]688#else
689                pt_init = init_l
690#endif
691                DEALLOCATE( init_l )
692             ENDIF
693          ENDIF
694
695
696          IF( humidity )  THEN
697             IF( init_3d%lod_q == 1 )  THEN
698                q_init = init_3d%q_init
[4648]699             ELSEIF( init_3d%lod_q == 2 )  THEN
700                ALLOCATE( init_l(nzb:nzt+1) )
[3182]701                DO  k = nzb, nzt+1
702                   init_l(k) = SUM( q(k,nys:nyn,nxl:nxr) )
703                ENDDO
704                init_l = init_l / REAL( ( nx + 1 ) * ( ny + 1 ), KIND = wp )
705
706#if defined( __parallel )
[4648]707                CALL MPI_ALLREDUCE( init_l, q_init, nzt+1-nzb+1, MPI_REAL, MPI_SUM, comm2d, ierr )
[3182]708#else
709                q_init = init_l
710#endif
711                DEALLOCATE( init_l )
712             ENDIF
713          ENDIF
714
[2696]715!
[4648]716!--       Write initial profiles onto 3D arrays.
717!--       Work-around, 3D initialization of u,v,w creates artificial structures which correlate with
718!--       the processor grid. The reason for this is still unknown. To work-around this, 3D
719!--       initialization will be effectively reduce to a 1D initialization where no such artificial
720!--       structures appear.
[2696]721          DO  i = nxlg, nxrg
722             DO  j = nysg, nyng
[4648]723                IF( init_3d%lod_u == 1  .OR.  init_3d%lod_u == 2 )  u(:,j,i) = u_init(:)
724                IF( init_3d%lod_v == 1  .OR.  init_3d%lod_u == 2 )  v(:,j,i) = v_init(:)
725                IF( .NOT. neutral  .AND.  ( init_3d%lod_pt == 1  .OR.  init_3d%lod_pt == 2 ) )     &
[3051]726                   pt(:,j,i) = pt_init(:)
[4648]727                IF( humidity  .AND.  ( init_3d%lod_q == 1  .OR.  init_3d%lod_q == 2 ) )            &
[4130]728                   q(:,j,i) = q_init(:)
[2696]729             ENDDO
730          ENDDO
731!
[4648]732!--       Set geostrophic wind components.
[2938]733          IF ( init_3d%from_file_ug )  THEN
734             ug(:) = init_3d%ug_init(:)
735          ENDIF
736          IF ( init_3d%from_file_vg )  THEN
737             vg(:) = init_3d%vg_init(:)
738          ENDIF
[3404]739!
740!--       Set bottom and top boundary condition for geostrophic wind
[2938]741          ug(nzt+1) = ug(nzt)
742          vg(nzt+1) = vg(nzt)
[3404]743          ug(nzb)   = ug(nzb+1)
744          vg(nzb)   = vg(nzb+1)
[2696]745!
746!--       Set inital w to 0
747          w = 0.0_wp
748
749          IF ( passive_scalar )  THEN
750             DO  i = nxlg, nxrg
751                DO  j = nysg, nyng
752                   s(:,j,i) = s_init
753                ENDDO
754             ENDDO
755          ENDIF
756
757!
[4648]758!--       Set velocity components at non-atmospheric / oceanic grid points to zero.
[4346]759          u = MERGE( u, 0.0_wp, BTEST( wall_flags_total_0, 1 ) )
760          v = MERGE( v, 0.0_wp, BTEST( wall_flags_total_0, 2 ) )
761          w = MERGE( w, 0.0_wp, BTEST( wall_flags_total_0, 3 ) )
[2700]762!
[4648]763!--       Initialize surface variables, e.g. friction velocity, momentum fluxes, etc.
764          CALL  init_surfaces
[2696]765
[4648]766          IF ( debug_output )  CALL  debug_message( 'initializing with INIFOR', 'end' )
[2696]767!
768!--    Initialization via computed 1D-model profiles
769       ELSEIF ( INDEX( initializing_actions, 'set_1d-model_profiles' ) /= 0 )  THEN
770
[4648]771          IF ( debug_output )  CALL  debug_message( 'initializing with 1D model profiles', 'start' )
[1]772!
773!--       Use solutions of the 1D model as initial profiles,
774!--       start 1D model
775          CALL init_1d_model
776!
777!--       Transfer initial profiles to the arrays of the 3D model
[667]778          DO  i = nxlg, nxrg
779             DO  j = nysg, nyng
[1]780                pt(:,j,i) = pt_init
781                u(:,j,i)  = u1d
782                v(:,j,i)  = v1d
783             ENDDO
784          ENDDO
785
[1960]786          IF ( humidity )  THEN
[667]787             DO  i = nxlg, nxrg
788                DO  j = nysg, nyng
[1]789                   q(:,j,i) = q_init
790                ENDDO
791             ENDDO
792          ENDIF
[2292]793
[1960]794          IF ( passive_scalar )  THEN
795             DO  i = nxlg, nxrg
796                DO  j = nysg, nyng
797                   s(:,j,i) = s_init
798                ENDDO
[4648]799             ENDDO
[1960]800          ENDIF
[1]801!
802!--          Store initial profiles for output purposes etc.
[2696]803          IF ( .NOT. constant_diffusion )  THEN
[1]804             hom(:,1,25,:) = SPREAD( l1d, 2, statistic_regions+1 )
805          ENDIF
806!
[2696]807!--       Set velocities back to zero
[4346]808          u = MERGE( u, 0.0_wp, BTEST( wall_flags_total_0, 1 ) )
[4648]809          v = MERGE( v, 0.0_wp, BTEST( wall_flags_total_0, 2 ) )
[1]810!
[4648]811!--       WARNING: The extra boundary conditions set after running the 1D model impose an error on
812!--       -------- the divergence one layer below the topography; need to correct later
813!--       ATTENTION: Provisional correction for Piacsek & Williams advection scheme: keep u and v
814!--       ---------- zero one layer below the topography.
[2696]815          IF ( ibc_uv_b == 1 )  THEN
[667]816!
[2696]817!--          Neumann condition
818             DO  i = nxl-1, nxr+1
819                DO  j = nys-1, nyn+1
820                   u(nzb,j,i) = u(nzb+1,j,i)
821                   v(nzb,j,i) = v(nzb+1,j,i)
[1]822                ENDDO
[2696]823             ENDDO
[1]824
825          ENDIF
[2618]826!
[4648]827!--       Initialize surface variables, e.g. friction velocity, momentum fluxes, etc.
[2618]828          CALL init_surfaces
[1]829
[4648]830          IF ( debug_output )  CALL  debug_message( 'initializing with 1D model profiles', 'end' )
[1384]831
[4648]832       ELSEIF ( INDEX(initializing_actions, 'set_constant_profiles') /= 0 )  THEN
[1241]833
[4648]834          IF ( debug_output )  CALL  debug_message( 'initializing with constant profiles', 'start' )
[2259]835
836!
[4648]837!--       Use constructed initial profiles (velocity constant with height, temperature profile with
838!--       constant gradient)
[667]839          DO  i = nxlg, nxrg
840             DO  j = nysg, nyng
[1]841                pt(:,j,i) = pt_init
842                u(:,j,i)  = u_init
843                v(:,j,i)  = v_init
844             ENDDO
845          ENDDO
846!
[2758]847!--       Mask topography
[4346]848          u = MERGE( u, 0.0_wp, BTEST( wall_flags_total_0, 1 ) )
849          v = MERGE( v, 0.0_wp, BTEST( wall_flags_total_0, 2 ) )
[2758]850!
[4648]851!--       Set initial horizontal velocities at the lowest computational grid levels to zero in order
852!--       to avoid too small time steps caused by the diffusion limit in the initial phase of a run
853!--       (at k=1, dz/2 occurs in the limiting formula!).
854!--       Please note, in case land- or urban-surface model is used and a spinup is applied, masking
855!--       the lowest computational level is not possible as MOST as well as energy-balance
856!--       parametrizations will not work with zero wind velocity.
857          IF ( ibc_uv_b /= 1  .AND.  .NOT. spinup )  THEN
[1815]858             DO  i = nxlg, nxrg
859                DO  j = nysg, nyng
[2232]860                   DO  k = nzb, nzt
[4648]861                      u(k,j,i) = MERGE( u(k,j,i), 0.0_wp, BTEST( wall_flags_total_0(k,j,i), 20 ) )
862                      v(k,j,i) = MERGE( v(k,j,i), 0.0_wp, BTEST( wall_flags_total_0(k,j,i), 21 ) )
[2232]863                   ENDDO
[1815]864                ENDDO
865             ENDDO
866          ENDIF
[1]867
[1960]868          IF ( humidity )  THEN
[667]869             DO  i = nxlg, nxrg
870                DO  j = nysg, nyng
[1]871                   q(:,j,i) = q_init
872                ENDDO
873             ENDDO
874          ENDIF
[4648]875
[1960]876          IF ( passive_scalar )  THEN
877             DO  i = nxlg, nxrg
878                DO  j = nysg, nyng
879                   s(:,j,i) = s_init
880                ENDDO
881             ENDDO
882          ENDIF
[1]883
[1920]884!
[4648]885!--       Compute initial temperature field and other constants used in case of a sloping surface.
[1]886          IF ( sloping_surface )  CALL init_slope
[2618]887!
[4648]888!--       Initialize surface variables, e.g. friction velocity, momentum fluxes, etc.
[2618]889          CALL init_surfaces
[4648]890
[3987]891          IF ( debug_output )  CALL debug_message( 'initializing with constant profiles', 'end' )
[1384]892
[4648]893       ELSEIF ( INDEX(initializing_actions, 'by_user') /= 0 )  THEN
[1384]894
[3987]895          IF ( debug_output )  CALL debug_message( 'initializing by user', 'start' )
[46]896!
[4648]897!--       Pre-initialize surface variables, i.e. setting start- and end-indices at each
898!--       (j,i)-location. Please note, this does not supersede user-defined initialization of
899!--       surface quantities.
[2618]900          CALL init_surfaces
901!
[46]902!--       Initialization will completely be done by the user
903          CALL user_init_3d_model
904
[3987]905          IF ( debug_output )  CALL debug_message( 'initializing by user', 'end' )
[1384]906
[1]907       ENDIF
[1384]908
[4648]909       IF ( debug_output )  THEN
910          CALL debug_message( 'initializing statistics, boundary conditions, etc.', 'start' )
911       ENDIF
[1384]912
[667]913!
914!--    Bottom boundary
[4648]915       IF ( ibc_uv_b == 0  .OR.  ibc_uv_b == 2  )  THEN
[1340]916          u(nzb,:,:) = 0.0_wp
917          v(nzb,:,:) = 0.0_wp
[667]918       ENDIF
[1]919
920!
[151]921!--    Apply channel flow boundary condition
[132]922       IF ( TRIM( bc_uv_t ) == 'dirichlet_0' )  THEN
[1340]923          u(nzt+1,:,:) = 0.0_wp
924          v(nzt+1,:,:) = 0.0_wp
[132]925       ENDIF
926
927!
[1]928!--    Calculate virtual potential temperature
[1960]929       IF ( humidity )  vpt = pt * ( 1.0_wp + 0.61_wp * q )
[1]930
931!
[4648]932!--    Store initial profiles for output purposes etc.. Please note, in case of initialization of u,
933!--    v, w, pt, and q via output data derived from larger scale models, data will not be
934!--    horizontally homogeneous. Actually, a mean profile should be calculated before.
[1]935       hom(:,1,5,:) = SPREAD( u(:,nys,nxl), 2, statistic_regions+1 )
936       hom(:,1,6,:) = SPREAD( v(:,nys,nxl), 2, statistic_regions+1 )
[667]937       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2)  THEN
[1340]938          hom(nzb,1,5,:) = 0.0_wp
939          hom(nzb,1,6,:) = 0.0_wp
[1]940       ENDIF
941       hom(:,1,7,:)  = SPREAD( pt(:,nys,nxl), 2, statistic_regions+1 )
942
[75]943       IF ( humidity )  THEN
[1]944!
[4648]945!--       Store initial profile of total water content, virtual potential temperature
[1]946          hom(:,1,26,:) = SPREAD(   q(:,nys,nxl), 2, statistic_regions+1 )
947          hom(:,1,29,:) = SPREAD( vpt(:,nys,nxl), 2, statistic_regions+1 )
[2696]948!
[4648]949!--       Store initial profile of mixing ratio and potential temperature
[3274]950          IF ( bulk_cloud_model  .OR.  cloud_droplets ) THEN
[1]951             hom(:,1,27,:) = SPREAD(  q(:,nys,nxl), 2, statistic_regions+1 )
952             hom(:,1,28,:) = SPREAD( pt(:,nys,nxl), 2, statistic_regions+1 )
953          ENDIF
954       ENDIF
955
[2696]956!
957!--    Store initial scalar profile
[1]958       IF ( passive_scalar )  THEN
[2513]959          hom(:,1,121,:) = SPREAD(  s(:,nys,nxl), 2, statistic_regions+1 )
[1]960       ENDIF
961
962!
[1400]963!--    Initialize the random number generators (from numerical recipes)
964       CALL random_function_ini
[4648]965
[1400]966       IF ( random_generator == 'random-parallel' )  THEN
[3241]967          CALL init_parallel_random_generator( nx, nys, nyn, nxl, nxr )
[1400]968       ENDIF
969!
[4648]970!--    Set the reference state to be used in the buoyancy terms (for ocean runs the reference state
971!--    will be set (overwritten) in init_ocean).
[1179]972       IF ( use_single_reference_value )  THEN
[4648]973          IF ( .NOT. humidity )  THEN
[1179]974             ref_state(:) = pt_reference
975          ELSE
976             ref_state(:) = vpt_reference
977          ENDIF
978       ELSE
[4648]979          IF ( .NOT. humidity )  THEN
[1179]980             ref_state(:) = pt_init(:)
981          ELSE
982             ref_state(:) = vpt(:,nys,nxl)
983          ENDIF
984       ENDIF
[152]985
986!
[707]987!--    For the moment, vertical velocity is zero
[1340]988       w = 0.0_wp
[1]989
990!
991!--    Initialize array sums (must be defined in first call of pres)
[1340]992       sums = 0.0_wp
[1]993
994!
[707]995!--    In case of iterative solvers, p must get an initial value
[1575]996       IF ( psolver(1:9) == 'multigrid'  .OR.  psolver == 'sor' )  p = 0.0_wp
[707]997!
[1]998!--    Impose vortex with vertical axis on the initial velocity profile
999       IF ( INDEX( initializing_actions, 'initialize_vortex' ) /= 0 )  THEN
1000          CALL init_rankine
1001       ENDIF
1002
1003!
[4648]1004!--    Impose temperature anomaly (advection test only) or warm air bubble close to surface.
1005       IF ( INDEX( initializing_actions, 'initialize_ptanom' ) /= 0  .OR.                          &
[3035]1006            INDEX( initializing_actions, 'initialize_bubble' ) /= 0  )  THEN
[1]1007          CALL init_pt_anomaly
1008       ENDIF
[4648]1009
[1]1010!
1011!--    If required, change the surface temperature at the start of the 3D run
[1340]1012       IF ( pt_surface_initial_change /= 0.0_wp )  THEN
[1]1013          pt(nzb,:,:) = pt(nzb,:,:) + pt_surface_initial_change
1014       ENDIF
1015
1016!
1017!--    If required, change the surface humidity/scalar at the start of the 3D
1018!--    run
[4648]1019       IF ( humidity  .AND.  q_surface_initial_change /= 0.0_wp )                                  &
[1]1020          q(nzb,:,:) = q(nzb,:,:) + q_surface_initial_change
[4648]1021
1022       IF ( passive_scalar  .AND.  s_surface_initial_change /= 0.0_wp )                            &
[1960]1023          s(nzb,:,:) = s(nzb,:,:) + s_surface_initial_change
[1]1024
[4648]1025
[1]1026!
1027!--    Initialize old and new time levels.
[2696]1028       tpt_m = 0.0_wp; tu_m = 0.0_wp; tv_m = 0.0_wp; tw_m = 0.0_wp
1029       pt_p = pt; u_p = u; v_p = v; w_p = w
[1]1030
[1960]1031       IF ( humidity  )  THEN
[1340]1032          tq_m = 0.0_wp
[1]1033          q_p = q
1034       ENDIF
[4648]1035
[1960]1036       IF ( passive_scalar )  THEN
1037          ts_m = 0.0_wp
1038          s_p  = s
[4648]1039       ENDIF
[1]1040
[4648]1041       IF ( debug_output )  THEN
1042          CALL debug_message( 'initializing statistics, boundary conditions, etc.', 'end' )
1043       ENDIF
[94]1044
[1788]1045    ELSEIF ( TRIM( initializing_actions ) == 'read_restart_data'  .OR.         &
[2232]1046             TRIM( initializing_actions ) == 'cyclic_fill' )                   &
[1]1047    THEN
[1384]1048
[4648]1049       IF ( debug_output )  THEN
1050          CALL debug_message( 'initializing in case of restart / cyclic_fill', 'start' )
1051       ENDIF
[1]1052!
[4648]1053!--    Initialize surface elements and its attributes, e.g. heat- and momentumfluxes, roughness,
1054!--    scaling parameters. As number of surface elements might be different between runs, e.g. in
1055!--    case of cyclic fill, and not all surface elements are read, surface elements need to be
1056!--    initialized before.
1057!--    Please note, in case of cyclic fill, surfaces should be initialized after restart data is
1058!--    read, else, individual settings of surface parameters will be overwritten from data of
1059!--    precursor run, hence, init_surfaces is called a second time after reading the restart data.
1060       CALL init_surfaces
[3609]1061!
[4648]1062!--    When reading data for cyclic fill of 3D prerun data files, read some of the global variables
1063!--    from the restart file which are required for initializing the inflow
[328]1064       IF ( TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
[559]1065
[4783]1066!
1067!--       Blockwise I/O does not work together with MPI-I/O
1068          IF ( restart_data_format_input(1:3) == 'mpi' )  THEN
1069             CALL rrd_read_parts_of_global
1070          ELSE
1071             DO  i = 0, io_blocks-1
1072                IF ( i == io_group )  THEN
1073                   CALL rrd_read_parts_of_global
1074                ENDIF
1075#if defined( __parallel )
1076                CALL MPI_BARRIER( comm2d, ierr )
1077#endif
1078             ENDDO
1079          ENDIF
1080
1081       ENDIF
1082
1083!
1084!--    Read processor specific binary data from restart file.
1085!--    Blockwise I/O does not work together with MPI-I/O
1086       IF ( restart_data_format_input(1:3) == 'mpi' )  THEN
1087          CALL rrd_local
1088       ELSE
[759]1089          DO  i = 0, io_blocks-1
1090             IF ( i == io_group )  THEN
[4783]1091                CALL rrd_local
[759]1092             ENDIF
1093#if defined( __parallel )
1094             CALL MPI_BARRIER( comm2d, ierr )
1095#endif
1096          ENDDO
[767]1097       ENDIF
1098
[4648]1099
[4365]1100       IF ( TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
1101
[3608]1102!
[4648]1103!--       In case of cyclic fill, call init_surfaces a second time, so that surface properties such
1104!--       as heat fluxes are initialized as prescribed.
[3609]1105          CALL init_surfaces
[767]1106
[328]1107!
[4648]1108!--       Overwrite u_init, v_init, pt_init, q_init and s_init with the horizontally mean (hom)
1109!--       vertical profiles from the end of the prerun, because these profiles shall be used as the
1110!--       reference state for the rayleigh damping and the pt_damping. This is especially important
1111!--       for the use of large_scale_subsidence, because the reference temperature in the free
1112!--       atmosphere changes in time.
[4365]1113          u_init(:) = hom_sum(:,1,0)
1114          v_init(:) = hom_sum(:,2,0)
1115          pt_init(:) = hom_sum(:,4,0)
[4648]1116          IF ( humidity )  q_init(:) = hom_sum(:,41,0)
1117          IF ( passive_scalar )  s_init(:) = hom_sum(:,115,0)
[4365]1118       ENDIF
1119!
[4648]1120!--    In case of complex terrain and cyclic fill method as initialization, shift initial data in
1121!--    the vertical direction for each point in the x-y-plane depending on local surface height.
1122       IF ( complex_terrain  .AND.  TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
[2550]1123          DO  i = nxlg, nxrg
1124             DO  j = nysg, nyng
[4168]1125                nz_u_shift = topo_top_ind(j,i,1)
1126                nz_v_shift = topo_top_ind(j,i,2)
1127                nz_w_shift = topo_top_ind(j,i,3)
1128                nz_s_shift = topo_top_ind(j,i,0)
[2550]1129
[4648]1130                u(nz_u_shift:nzt+1,j,i)  = u(0:nzt+1-nz_u_shift,j,i)
[2550]1131
1132                v(nz_v_shift:nzt+1,j,i)  = v(0:nzt+1-nz_v_shift,j,i)
1133
1134                w(nz_w_shift:nzt+1,j,i)  = w(0:nzt+1-nz_w_shift,j,i)
1135
1136                p(nz_s_shift:nzt+1,j,i)  =  p(0:nzt+1-nz_s_shift,j,i)
1137                pt(nz_s_shift:nzt+1,j,i) = pt(0:nzt+1-nz_s_shift,j,i)
1138             ENDDO
1139          ENDDO
1140       ENDIF
1141!
[767]1142!--    Initialization of the turbulence recycling method
[4648]1143       IF ( TRIM( initializing_actions ) == 'cyclic_fill'  .AND.  turbulent_inflow )  THEN
[767]1144!
1145!--       First store the profiles to be used at the inflow.
[4648]1146!--       These profiles are the (temporally) and horizontally averaged vertical profiles from the
1147!--       prerun. Alternatively, prescribed profiles for u,v-components can be used.
[3288]1148          ALLOCATE( mean_inflow_profiles(nzb:nzt+1,1:num_mean_inflow_profiles) )
[151]1149
[767]1150          IF ( use_prescribed_profile_data )  THEN
1151             mean_inflow_profiles(:,1) = u_init            ! u
1152             mean_inflow_profiles(:,2) = v_init            ! v
1153          ELSE
[328]1154             mean_inflow_profiles(:,1) = hom_sum(:,1,0)    ! u
1155             mean_inflow_profiles(:,2) = hom_sum(:,2,0)    ! v
[767]1156          ENDIF
1157          mean_inflow_profiles(:,4) = hom_sum(:,4,0)       ! pt
[4648]1158          IF ( humidity )  mean_inflow_profiles(:,6) = hom_sum(:,41,0)          ! q
1159          IF ( passive_scalar )  mean_inflow_profiles(:,7) = hom_sum(:,115,0)   ! s
[4365]1160
[2550]1161!
[4648]1162!--       In case of complex terrain, determine vertical displacement at inflow boundary and adjust
1163!--       mean inflow profiles
[2550]1164          IF ( complex_terrain )  THEN
[4648]1165             IF ( nxlg <= 0  .AND.  nxrg >= 0  .AND.  nysg <= 0  .AND.  nyng >= 0 )  THEN
[4168]1166                nz_u_shift_l = topo_top_ind(j,i,1)
1167                nz_v_shift_l = topo_top_ind(j,i,2)
1168                nz_w_shift_l = topo_top_ind(j,i,3)
1169                nz_s_shift_l = topo_top_ind(j,i,0)
[2550]1170             ELSE
1171                nz_u_shift_l = 0
1172                nz_v_shift_l = 0
1173                nz_w_shift_l = 0
1174                nz_s_shift_l = 0
1175             ENDIF
[151]1176
[2550]1177#if defined( __parallel )
[4648]1178             CALL MPI_ALLREDUCE( nz_u_shift_l, nz_u_shift, 1, MPI_INTEGER, MPI_MAX, comm2d, ierr )
1179             CALL MPI_ALLREDUCE( nz_v_shift_l, nz_v_shift, 1, MPI_INTEGER, MPI_MAX, comm2d, ierr )
1180             CALL MPI_ALLREDUCE( nz_w_shift_l, nz_w_shift, 1, MPI_INTEGER, MPI_MAX, comm2d, ierr )
1181             CALL MPI_ALLREDUCE( nz_s_shift_l, nz_s_shift, 1, MPI_INTEGER, MPI_MAX, comm2d, ierr )
[2550]1182#else
1183             nz_u_shift = nz_u_shift_l
1184             nz_v_shift = nz_v_shift_l
1185             nz_w_shift = nz_w_shift_l
1186             nz_s_shift = nz_s_shift_l
1187#endif
1188
1189             mean_inflow_profiles(:,1) = 0.0_wp
1190             mean_inflow_profiles(nz_u_shift:nzt+1,1) = hom_sum(0:nzt+1-nz_u_shift,1,0)  ! u
1191
1192             mean_inflow_profiles(:,2) = 0.0_wp
1193             mean_inflow_profiles(nz_v_shift:nzt+1,2) = hom_sum(0:nzt+1-nz_v_shift,2,0)  ! v
1194
1195             mean_inflow_profiles(nz_s_shift:nzt+1,4) = hom_sum(0:nzt+1-nz_s_shift,4,0)  ! pt
1196
1197          ENDIF
1198
[151]1199!
[4648]1200!--       If necessary, adjust the horizontal flow field to the prescribed profiles
[767]1201          IF ( use_prescribed_profile_data )  THEN
1202             DO  i = nxlg, nxrg
[667]1203                DO  j = nysg, nyng
[328]1204                   DO  k = nzb, nzt+1
[767]1205                      u(k,j,i) = u(k,j,i) - hom_sum(k,1,0) + u_init(k)
1206                      v(k,j,i) = v(k,j,i) - hom_sum(k,2,0) + v_init(k)
[328]1207                   ENDDO
[151]1208                ENDDO
[767]1209             ENDDO
1210          ENDIF
[151]1211
1212!
[4648]1213!--       Use these mean profiles at the inflow (provided that Dirichlet conditions are used)
[3182]1214          IF ( bc_dirichlet_l )  THEN
[767]1215             DO  j = nysg, nyng
1216                DO  k = nzb, nzt+1
1217                   u(k,j,nxlg:-1)  = mean_inflow_profiles(k,1)
1218                   v(k,j,nxlg:-1)  = mean_inflow_profiles(k,2)
[1340]1219                   w(k,j,nxlg:-1)  = 0.0_wp
[767]1220                   pt(k,j,nxlg:-1) = mean_inflow_profiles(k,4)
[4648]1221                   IF ( humidity )  q(k,j,nxlg:-1)  = mean_inflow_profiles(k,6)
1222                   IF ( passive_scalar )  s(k,j,nxlg:-1)  = mean_inflow_profiles(k,7)
[767]1223                ENDDO
1224             ENDDO
1225          ENDIF
1226
[151]1227!
[4648]1228!--       Calculate the damping factors to be used at the inflow. For a turbulent inflow the
1229!--       turbulent fluctuations have to be limited vertically because otherwise the turbulent
1230!--       inflow layer will grow in time.
[1340]1231          IF ( inflow_damping_height == 9999999.9_wp )  THEN
[767]1232!
[4648]1233!--          Default: use the inversion height calculated by the prerun; if this is zero,
1234!--          inflow_damping_height must be explicitly specified.
[1340]1235             IF ( hom_sum(nzb+6,pr_palm,0) /= 0.0_wp )  THEN
[767]1236                inflow_damping_height = hom_sum(nzb+6,pr_palm,0)
1237             ELSE
[4648]1238                WRITE( message_string, * ) 'inflow_damping_height must be ',                       &
1239                                           'explicitly specified because&the inversion height ',   &
1240                                           'calculated by the prerun is zero.'
[767]1241                CALL message( 'init_3d_model', 'PA0318', 1, 2, 0, 6, 0 )
[292]1242             ENDIF
[151]1243
[767]1244          ENDIF
1245
[1340]1246          IF ( inflow_damping_width == 9999999.9_wp )  THEN
[151]1247!
[4648]1248!--          Default for the transition range: one tenth of the undamped layer
[1340]1249             inflow_damping_width = 0.1_wp * inflow_damping_height
[151]1250
[767]1251          ENDIF
[151]1252
[767]1253          ALLOCATE( inflow_damping_factor(nzb:nzt+1) )
[151]1254
[767]1255          DO  k = nzb, nzt+1
[151]1256
[767]1257             IF ( zu(k) <= inflow_damping_height )  THEN
[1340]1258                inflow_damping_factor(k) = 1.0_wp
[996]1259             ELSEIF ( zu(k) <= ( inflow_damping_height + inflow_damping_width ) )  THEN
[4648]1260                inflow_damping_factor(k) = 1.0_wp -                                                &
1261                                           ( zu(k) - inflow_damping_height ) / inflow_damping_width
[767]1262             ELSE
[1340]1263                inflow_damping_factor(k) = 0.0_wp
[767]1264             ENDIF
[151]1265
[767]1266          ENDDO
[151]1267
[147]1268       ENDIF
1269
[152]1270!
[2696]1271!--    Inside buildings set velocities back to zero
[4648]1272       IF ( TRIM( initializing_actions ) == 'cyclic_fill'  .AND.  topography /= 'flat' )  THEN
[359]1273!
[2696]1274!--       Inside buildings set velocities back to zero.
1275!--       Other scalars (pt, q, s, p, sa, ...) are ignored at present,
[359]1276!--       maybe revise later.
[1001]1277          DO  i = nxlg, nxrg
1278             DO  j = nysg, nyng
[2232]1279                DO  k = nzb, nzt
[4648]1280                   u(k,j,i) = MERGE( u(k,j,i), 0.0_wp, BTEST( wall_flags_total_0(k,j,i), 1 ) )
1281                   v(k,j,i) = MERGE( v(k,j,i), 0.0_wp, BTEST( wall_flags_total_0(k,j,i), 2 ) )
1282                   w(k,j,i) = MERGE( w(k,j,i), 0.0_wp, BTEST( wall_flags_total_0(k,j,i), 3 ) )
[2232]1283                ENDDO
[359]1284             ENDDO
[1001]1285          ENDDO
[359]1286
1287       ENDIF
1288
1289!
[4648]1290!--    Calculate initial temperature field and other constants used in case of a sloping surface
[1]1291       IF ( sloping_surface )  CALL init_slope
1292
1293!
[4648]1294!--    Initialize new time levels (only done in order to set boundary values including ghost points)
[2696]1295       pt_p = pt; u_p = u; v_p = v; w_p = w
[1960]1296       IF ( humidity )  THEN
[1053]1297          q_p = q
1298       ENDIF
[1960]1299       IF ( passive_scalar )  s_p  = s
[181]1300!
[4648]1301!--    Allthough tendency arrays are set in prognostic_equations, they have have to be predefined
1302!--    here because they are used (but multiplied with 0) there before they are set.
[2696]1303       tpt_m = 0.0_wp; tu_m = 0.0_wp; tv_m = 0.0_wp; tw_m = 0.0_wp
[1960]1304       IF ( humidity )  THEN
[1340]1305          tq_m = 0.0_wp
[1053]1306       ENDIF
[1960]1307       IF ( passive_scalar )  ts_m  = 0.0_wp
[181]1308
[4648]1309       IF ( debug_output )  THEN
1310          CALL debug_message( 'initializing in case of restart / cyclic_fill', 'end' )
1311       ENDIF
[1384]1312
[1]1313    ELSE
1314!
1315!--    Actually this part of the programm should not be reached
[254]1316       message_string = 'unknown initializing problem'
1317       CALL message( 'init_3d_model', 'PA0193', 1, 2, 0, 6, 0 )
[1]1318    ENDIF
1319
1320!
[667]1321!-- Calculate the initial volume flow at the right and north boundary
[709]1322    IF ( conserve_volume_flow )  THEN
[151]1323
[767]1324       IF ( use_prescribed_profile_data )  THEN
[667]1325
[1340]1326          volume_flow_initial_l = 0.0_wp
1327          volume_flow_area_l    = 0.0_wp
[732]1328
[667]1329          IF ( nxr == nx )  THEN
1330             DO  j = nys, nyn
[2232]1331                DO  k = nzb+1, nzt
[4648]1332                   volume_flow_initial_l(1) = volume_flow_initial_l(1) +                           &
1333                                              u_init(k) * dzw(k)                                   &
1334                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1335                                                       BTEST( wall_flags_total_0(k,j,nxr), 1 )     &
1336                                                     )
[2232]1337
[4648]1338                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzw(k)                       &
1339                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1340                                                       BTEST( wall_flags_total_0(k,j,nxr), 1 )     &
1341                                                     )
[767]1342                ENDDO
1343             ENDDO
1344          ENDIF
[4648]1345
[767]1346          IF ( nyn == ny )  THEN
1347             DO  i = nxl, nxr
[2232]1348                DO  k = nzb+1, nzt
[4648]1349                   volume_flow_initial_l(2) = volume_flow_initial_l(2) +                           &
1350                                              v_init(k) * dzw(k)                                   &
1351                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1352                                                       BTEST( wall_flags_total_0(k,nyn,i), 2 )     &
1353                                                     )
1354                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzw(k)                       &
1355                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1356                                                       BTEST( wall_flags_total_0(k,nyn,i), 2 )     &
1357                                                     )
[767]1358                ENDDO
1359             ENDDO
1360          ENDIF
1361
1362#if defined( __parallel )
[4648]1363          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1), 2, MPI_REAL,       &
1364                              MPI_SUM, comm2d, ierr )
1365          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1), 2, MPI_REAL, MPI_SUM,    &
1366                              comm2d, ierr )
[767]1367
1368#else
1369          volume_flow_initial = volume_flow_initial_l
1370          volume_flow_area    = volume_flow_area_l
[4648]1371#endif
[767]1372
1373       ELSEIF ( TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
1374
[1340]1375          volume_flow_initial_l = 0.0_wp
1376          volume_flow_area_l    = 0.0_wp
[767]1377
1378          IF ( nxr == nx )  THEN
1379             DO  j = nys, nyn
[2232]1380                DO  k = nzb+1, nzt
[4648]1381                   volume_flow_initial_l(1) = volume_flow_initial_l(1) +                           &
1382                                              hom_sum(k,1,0) * dzw(k)                              &
1383                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1384                                                       BTEST( wall_flags_total_0(k,j,nx), 1 )      &
1385                                                     )
1386                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzw(k)                       &
1387                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1388                                                       BTEST( wall_flags_total_0(k,j,nx), 1 )      &
1389                                                     )
[667]1390                ENDDO
1391             ENDDO
1392          ENDIF
[4648]1393
[667]1394          IF ( nyn == ny )  THEN
1395             DO  i = nxl, nxr
[2232]1396                DO  k = nzb+1, nzt
[4648]1397                   volume_flow_initial_l(2) = volume_flow_initial_l(2) +                           &
1398                                              hom_sum(k,2,0) * dzw(k)                              &
1399                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1400                                                       BTEST( wall_flags_total_0(k,ny,i), 2 )      &
1401                                                     )
1402                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzw(k)                       &
1403                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1404                                                       BTEST( wall_flags_total_0(k,ny,i), 2 )      &
1405                                                     )
[667]1406                ENDDO
1407             ENDDO
1408          ENDIF
1409
[732]1410#if defined( __parallel )
[4648]1411          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1), 2, MPI_REAL,       &
1412                              MPI_SUM, comm2d, ierr )
1413          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1), 2, MPI_REAL, MPI_SUM,    &
1414                              comm2d, ierr )
[732]1415
1416#else
1417          volume_flow_initial = volume_flow_initial_l
1418          volume_flow_area    = volume_flow_area_l
[4648]1419#endif
[732]1420
[667]1421       ELSEIF ( TRIM( initializing_actions ) /= 'read_restart_data' )  THEN
1422
[1340]1423          volume_flow_initial_l = 0.0_wp
1424          volume_flow_area_l    = 0.0_wp
[732]1425
[667]1426          IF ( nxr == nx )  THEN
1427             DO  j = nys, nyn
[2232]1428                DO  k = nzb+1, nzt
[4648]1429                   volume_flow_initial_l(1) = volume_flow_initial_l(1) +                           &
1430                                              u(k,j,nx) * dzw(k)                                   &
1431                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1432                                                       BTEST( wall_flags_total_0(k,j,nx), 1 )      &
1433                                                     )
1434                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzw(k)                       &
1435                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1436                                                   BTEST( wall_flags_total_0(k,j,nx), 1 )          &
1437                                                     )
[667]1438                ENDDO
1439             ENDDO
1440          ENDIF
[4648]1441
[667]1442          IF ( nyn == ny )  THEN
1443             DO  i = nxl, nxr
[2232]1444                DO  k = nzb+1, nzt
[4648]1445                   volume_flow_initial_l(2) = volume_flow_initial_l(2) +                           &
1446                                              v(k,ny,i) * dzw(k)                                   &
1447                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1448                                                       BTEST( wall_flags_total_0(k,ny,i), 2 )      &
1449                                                     )
1450                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzw(k)                       &
1451                                              * MERGE( 1.0_wp, 0.0_wp,                             &
1452                                                       BTEST( wall_flags_total_0(k,ny,i), 2 )      &
1453                                                     )
[667]1454                ENDDO
1455             ENDDO
1456          ENDIF
1457
1458#if defined( __parallel )
[4648]1459          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1), 2, MPI_REAL,       &
1460                              MPI_SUM, comm2d, ierr )
1461          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1), 2, MPI_REAL, MPI_SUM,    &
1462                              comm2d, ierr )
[667]1463
1464#else
[732]1465          volume_flow_initial = volume_flow_initial_l
1466          volume_flow_area    = volume_flow_area_l
[4648]1467#endif
[667]1468
[732]1469       ENDIF
1470
[151]1471!
[4648]1472!--    In case of 'bulk_velocity' mode, volume_flow_initial is calculated from u|v_bulk instead
[680]1473       IF ( TRIM( conserve_volume_flow_mode ) == 'bulk_velocity' )  THEN
1474          volume_flow_initial(1) = u_bulk * volume_flow_area(1)
1475          volume_flow_initial(2) = v_bulk * volume_flow_area(2)
1476       ENDIF
[667]1477
[680]1478    ENDIF
[2232]1479!
[4648]1480!-- In the following, surface properties can be further initialized with input from static driver
1481!-- file.
1482!-- At the moment this affects only default surfaces. For example, roughness length or sensible /
1483!-- latent heat fluxes can be initialized heterogeneously for default surfaces. Therefore, a generic
1484!-- routine from netcdf_data_input_mod is called to read a 2D array.
[4150]1485    IF ( input_pids_static )  THEN
1486!
[4151]1487!--    Allocate memory for possible static input
1488       ALLOCATE( tmp_2d%var(nys:nyn,nxl:nxr) )
1489       tmp_2d%var = 0.0_wp
1490!
[4150]1491!--    Open the static input file
[4151]1492#if defined( __netcdf )
[4648]1493       CALL open_read_file( TRIM( input_file_static ) //                                           &
1494                            TRIM( coupling_char ), pids_id )
1495
[4186]1496       CALL inquire_num_variables( pids_id, num_var_pids )
[4150]1497!
1498!--    Allocate memory to store variable names and read them
[4186]1499       ALLOCATE( vars_pids(1:num_var_pids) )
1500       CALL inquire_variable_names( pids_id, vars_pids )
[4150]1501!
[4648]1502!--    Input roughness length.
[4186]1503       IF ( check_existence( vars_pids, 'z0' ) )  THEN
[4150]1504!
1505!--       Read _FillValue attribute
[4648]1506          CALL get_attribute( pids_id, char_fill, tmp_2d%fill, .FALSE., 'z0' )
1507!
1508!--       Read variable
1509          CALL get_variable( pids_id, 'z0', tmp_2d%var, nxl, nxr, nys, nyn )
1510!
1511!--       Initialize roughness length. Note, z0 will be only initialized at default-type surfaces.
1512!--       At natural or urban z0 is implicitly initialized by the respective parameter lists.
1513!--       Initialize horizontal surface elements.
1514          CALL init_single_surface_properties( surf_def_h(0)%z0, tmp_2d%var, surf_def_h(0)%ns,     &
1515                                               tmp_2d%fill, surf_def_h(0)%i, surf_def_h(0)%j )
1516!
1517!--       Initialize roughness also at vertical surface elements.
1518!--       Note, the actual 2D input arrays are only defined on the subdomain. Therefore, pass the
1519!--       index arrays with their respective offset values.
1520          DO  l = 0, 3
1521             CALL init_single_surface_properties( surf_def_v(l)%z0, tmp_2d%var, surf_def_v(l)%ns,  &
1522                                                  tmp_2d%fill, surf_def_v(l)%i+surf_def_v(l)%ioff, &
1523                                                  surf_def_v(l)%j+surf_def_v(l)%joff )
[4150]1524          ENDDO
[4648]1525
[4150]1526       ENDIF
1527!
[4648]1528!--    Input surface sensible heat flux.
[4514]1529       IF ( check_existence( vars_pids, 'shf' ) )  THEN
1530!
1531!--       Read _FillValue attribute
[4648]1532          CALL get_attribute( pids_id, char_fill, tmp_2d%fill, .FALSE., 'shf' )
[4514]1533!
1534!--       Read variable
[4648]1535          CALL get_variable( pids_id, 'shf', tmp_2d%var, nxl, nxr, nys, nyn )
[4514]1536!
[4648]1537!--       Initialize heat flux. Note, shf will be only initialized at default-type surfaces. At
1538!--       natural or urban shf is implicitly initialized by the respective parameter lists.
[4514]1539!--       Initialize horizontal surface elements.
[4648]1540          CALL init_single_surface_properties( surf_def_h(0)%shf, tmp_2d%var, surf_def_h(0)%ns,    &
1541                                               tmp_2d%fill, surf_def_h(0)%i, surf_def_h(0)%j )
[4514]1542!
1543!--       Initialize heat flux also at vertical surface elements.
[4648]1544!--       Note, the actual 2D input arrays are only defined on the subdomain. Therefore, pass the
1545!--       index arrays with their respective offset values.
[4514]1546          DO  l = 0, 3
[4648]1547             CALL init_single_surface_properties( surf_def_v(l)%shf, tmp_2d%var, surf_def_v(l)%ns, &
1548                                                  tmp_2d%fill, surf_def_v(l)%i+surf_def_v(l)%ioff, &
1549                                                  surf_def_v(l)%j+surf_def_v(l)%joff )
[4514]1550          ENDDO
1551
1552       ENDIF
1553!
[4791]1554!--    Input surface latent heat flux.
1555       IF ( humidity )  THEN
1556          IF ( check_existence( vars_pids, 'qsws' ) )  THEN
[4514]1557!
[4791]1558!--          Read _FillValue attribute
1559             CALL get_attribute( pids_id, char_fill, tmp_2d%fill, .FALSE., 'qsws' )
[4514]1560!
[4791]1561!--          Read variable
1562             CALL get_variable( pids_id, 'qsws', tmp_2d%var, nxl, nxr, nys, nyn )
[4514]1563!
[4791]1564!--          Initialize latent heat flux. Note, qsws will be only initialized at default-type surfaces.
1565!--          At natural or urban qsws is implicitly initialized by the respective parameter lists.
1566!--          Initialize horizontal surface elements.
1567             CALL init_single_surface_properties( surf_def_h(0)%qsws, tmp_2d%var, surf_def_h(0)%ns,&
1568                                                  tmp_2d%fill, surf_def_h(0)%i, surf_def_h(0)%j )
[4514]1569!
[4791]1570!--          Initialize latent heat flux also at vertical surface elements.
1571!--          Note, the actual 2D input arrays are only defined on the subdomain. Therefore, pass the
1572!--          index arrays with their respective offset values.
1573             DO  l = 0, 3
1574                CALL init_single_surface_properties( surf_def_v(l)%qsws, tmp_2d%var,               &
1575                                                     surf_def_v(l)%ns, tmp_2d%fill,                &
1576                                                     surf_def_v(l)%i+surf_def_v(l)%ioff,           &
1577                                                     surf_def_v(l)%j+surf_def_v(l)%joff )
1578             ENDDO
[4514]1579
[4791]1580          ENDIF
[4514]1581       ENDIF
1582!
[4791]1583!--    Input passive scalar flux.
1584       IF ( passive_scalar )  THEN
1585          IF ( check_existence( vars_pids, 'ssws' ) )  THEN
1586!
1587!--          Read _FillValue attribute
1588             CALL get_attribute( pids_id, char_fill, tmp_2d%fill, .FALSE., 'ssws' )
1589!
1590!--          Read variable
1591             CALL get_variable( pids_id, 'ssws', tmp_2d%var, nxl, nxr, nys, nyn )
1592!
1593!--          Initialize passive scalar flux. Initialize horizontal surface elements.
1594             CALL init_single_surface_properties( surf_def_h(0)%ssws, tmp_2d%var, surf_def_h(0)%ns,&
1595                                                  tmp_2d%fill, surf_def_h(0)%i, surf_def_h(0)%j )
1596
1597             DO  l = 0, 3
1598                CALL init_single_surface_properties( surf_def_v(l)%ssws, tmp_2d%var,               &
1599                                                     surf_def_v(l)%ns, tmp_2d%fill,                &
1600                                                     surf_def_v(l)%i+surf_def_v(l)%ioff,           &
1601                                                     surf_def_v(l)%j+surf_def_v(l)%joff )
1602             ENDDO
1603
1604          ENDIF
1605       ENDIF
1606!
[4648]1607!--    Additional variables, can be initialized the
[4150]1608!--    same way.
[4514]1609
[4150]1610!
[4187]1611!--    Finally, close the input file and deallocate temporary arrays
1612       DEALLOCATE( vars_pids )
[4648]1613
[4186]1614       CALL close_input_file( pids_id )
[4151]1615#endif
1616       DEALLOCATE( tmp_2d%var )
[4150]1617    ENDIF
1618!
[4648]1619!-- Finally, if random_heatflux is set, disturb shf at horizontal surfaces. Actually, this should be
1620!-- done in surface_mod, where all other initializations of surface quantities are done. However,
1621!-- this would create a ring dependency, hence, it is done here. Maybe delete disturb_heatflux and
1622!-- tranfer the respective code directly into the initialization in surface_mod.
1623    IF ( TRIM( initializing_actions ) /= 'read_restart_data'  .AND.                                &
[2232]1624         TRIM( initializing_actions ) /= 'cyclic_fill' )  THEN
[4648]1625
1626       IF ( use_surface_fluxes  .AND.  constant_heatflux  .AND.  random_heatflux )  THEN
[2232]1627          IF ( surf_def_h(0)%ns >= 1 )  CALL disturb_heatflux( surf_def_h(0) )
[4671]1628          IF ( surf_lsm_h(0)%ns >= 1 )  CALL disturb_heatflux( surf_lsm_h(0) )
1629          IF ( surf_usm_h(0)%ns >= 1 )  CALL disturb_heatflux( surf_usm_h(0) )
[2232]1630       ENDIF
1631    ENDIF
[680]1632
[787]1633!
[4648]1634!-- Compute total sum of grid points and the mean surface level height for each statistic region.
1635!-- These are mainly used for horizontal averaging of turbulence statistics.
1636!-- ngp_2dh: number of grid points of a horizontal cross section through the respective statistic
1637!--          region
[3747]1638!-- ngp_3d:  number of grid points of the respective statistic region
[2696]1639    ngp_2dh_outer_l   = 0
1640    ngp_2dh_outer     = 0
1641    ngp_2dh_s_inner_l = 0
1642    ngp_2dh_s_inner   = 0
1643    ngp_2dh_l         = 0
1644    ngp_2dh           = 0
1645    ngp_3d_inner_l    = 0.0_wp
1646    ngp_3d_inner      = 0
1647    ngp_3d            = 0
1648    ngp_sums          = ( nz + 2 ) * ( pr_palm + max_pr_user )
1649
1650    mean_surface_level_height   = 0.0_wp
1651    mean_surface_level_height_l = 0.0_wp
1652!
1653!-- To do: New concept for these non-topography grid points!
1654    DO  sr = 0, statistic_regions
1655       DO  i = nxl, nxr
1656          DO  j = nys, nyn
1657             IF ( rmask(j,i,sr) == 1.0_wp )  THEN
1658!
1659!--             All xy-grid points
1660                ngp_2dh_l(sr) = ngp_2dh_l(sr) + 1
1661!
[4648]1662!--             Determine mean surface-level height. In case of downward-facing walls are present,
1663!--             more than one surface level exist.
1664!--             In this case, use the lowest surface-level height.
1665                IF ( surf_def_h(0)%start_index(j,i) <= surf_def_h(0)%end_index(j,i) )  THEN
[2696]1666                   m = surf_def_h(0)%start_index(j,i)
1667                   k = surf_def_h(0)%k(m)
[4648]1668                   mean_surface_level_height_l(sr) = mean_surface_level_height_l(sr) + zw(k-1)
[2696]1669                ENDIF
[4671]1670                IF ( surf_lsm_h(0)%start_index(j,i) <= surf_lsm_h(0)%end_index(j,i) )  THEN
1671                   m = surf_lsm_h(0)%start_index(j,i)
1672                   k = surf_lsm_h(0)%k(m)
[4648]1673                   mean_surface_level_height_l(sr) = mean_surface_level_height_l(sr) + zw(k-1)
[2696]1674                ENDIF
[4671]1675                IF ( surf_usm_h(0)%start_index(j,i) <= surf_usm_h(0)%end_index(j,i) )  THEN
1676                   m = surf_usm_h(0)%start_index(j,i)
1677                   k = surf_usm_h(0)%k(m)
[4648]1678                   mean_surface_level_height_l(sr) = mean_surface_level_height_l(sr) + zw(k-1)
[2696]1679                ENDIF
1680
1681                k_surf = k - 1
1682
1683                DO  k = nzb, nzt+1
1684!
1685!--                xy-grid points above topography
[4648]1686                   ngp_2dh_outer_l(k,sr) = ngp_2dh_outer_l(k,sr)     +                             &
1687                                           MERGE( 1, 0, BTEST( wall_flags_total_0(k,j,i), 24 ) )
[2696]1688
[4648]1689                   ngp_2dh_s_inner_l(k,sr) = ngp_2dh_s_inner_l(k,sr) +                             &
1690                                             MERGE( 1, 0, BTEST( wall_flags_total_0(k,j,i), 22 ) )
[2696]1691
1692                ENDDO
1693!
1694!--             All grid points of the total domain above topography
1695                ngp_3d_inner_l(sr) = ngp_3d_inner_l(sr) + ( nz - k_surf + 2 )
1696
1697
1698
1699             ENDIF
1700          ENDDO
1701       ENDDO
1702    ENDDO
[3747]1703
[2696]1704    sr = statistic_regions + 1
1705#if defined( __parallel )
1706    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
[4648]1707    CALL MPI_ALLREDUCE( ngp_2dh_l(0), ngp_2dh(0), sr, MPI_INTEGER, MPI_SUM, comm2d, ierr )
1708    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1709    CALL MPI_ALLREDUCE( ngp_2dh_outer_l(0,0), ngp_2dh_outer(0,0), (nz+2)*sr, MPI_INTEGER, MPI_SUM, &
[2696]1710                        comm2d, ierr )
1711    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
[4648]1712    CALL MPI_ALLREDUCE( ngp_2dh_s_inner_l(0,0), ngp_2dh_s_inner(0,0), (nz+2)*sr, MPI_INTEGER,      &
1713                        MPI_SUM, comm2d, ierr )
[2696]1714    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
[4648]1715    CALL MPI_ALLREDUCE( ngp_3d_inner_l(0), ngp_3d_inner_tmp(0), sr, MPI_REAL, MPI_SUM, comm2d,     &
1716                        ierr )
[2696]1717    ngp_3d_inner = INT( ngp_3d_inner_tmp, KIND = SELECTED_INT_KIND( 18 ) )
1718    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
[4648]1719    CALL MPI_ALLREDUCE( mean_surface_level_height_l(0), mean_surface_level_height(0), sr, MPI_REAL,&
[2696]1720                        MPI_SUM, comm2d, ierr )
1721    mean_surface_level_height = mean_surface_level_height / REAL( ngp_2dh )
1722#else
1723    ngp_2dh         = ngp_2dh_l
1724    ngp_2dh_outer   = ngp_2dh_outer_l
1725    ngp_2dh_s_inner = ngp_2dh_s_inner_l
1726    ngp_3d_inner    = INT( ngp_3d_inner_l, KIND = SELECTED_INT_KIND( 18 ) )
1727    mean_surface_level_height = mean_surface_level_height_l / REAL( ngp_2dh_l )
1728#endif
1729
[4648]1730    ngp_3d = INT ( ngp_2dh, KIND = SELECTED_INT_KIND( 18 ) ) *                                     &
[2696]1731             INT ( (nz + 2 ), KIND = SELECTED_INT_KIND( 18 ) )
1732
1733!
[4648]1734!-- Set a lower limit of 1 in order to avoid zero divisions in flow_statistics, buoyancy, etc. A
1735!-- zero value will occur for cases where all grid points of the respective subdomain lie below the
1736!-- surface topography
1737    ngp_2dh_outer   = MAX( 1, ngp_2dh_outer(:,:)   )
1738    ngp_3d_inner    = MAX( INT(1, KIND = SELECTED_INT_KIND( 18 )), ngp_3d_inner(:) )
1739    ngp_2dh_s_inner = MAX( 1, ngp_2dh_s_inner(:,:) )
[2696]1740
[4648]1741    DEALLOCATE( mean_surface_level_height_l, ngp_2dh_l, ngp_2dh_outer_l, ngp_3d_inner_l,           &
1742                ngp_3d_inner_tmp )
[4548]1743
[2696]1744!
[4648]1745!-- Initializae 3D offline nesting in COSMO model and read data from
[3347]1746!-- external NetCDF file.
1747    IF ( nesting_offline )  CALL nesting_offl_init
1748!
[787]1749!-- Initialize quantities for special advections schemes
1750    CALL init_advec
[680]1751
[667]1752!
[680]1753!-- Impose random perturbation on the horizontal velocity field and then
1754!-- remove the divergences from the velocity field at the initial stage
[4648]1755    IF ( create_disturbances  .AND.  disturbance_energy_limit /= 0.0_wp  .AND.                     &
1756         TRIM( initializing_actions ) /= 'read_restart_data'  .AND.                                &
[680]1757         TRIM( initializing_actions ) /= 'cyclic_fill' )  THEN
1758
[4648]1759       IF ( debug_output )  THEN
1760          CALL debug_message( 'creating disturbances + applying pressure solver', 'start' )
1761       ENDIF
[3849]1762!
1763!--    Needed for both disturb_field and pres
1764!$ACC DATA &
1765!$ACC CREATE(tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
1766!$ACC COPY(u(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
1767!$ACC COPY(v(nzb:nzt+1,nysg:nyng,nxlg:nxrg))
1768
[2232]1769       CALL disturb_field( 'u', tend, u )
1770       CALL disturb_field( 'v', tend, v )
[1384]1771
[3849]1772!$ACC DATA &
1773!$ACC CREATE(d(nzb+1:nzt,nys:nyn,nxl:nxr)) &
1774!$ACC COPY(w(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
1775!$ACC COPY(p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
1776!$ACC COPYIN(rho_air(nzb:nzt+1), rho_air_zw(nzb:nzt+1)) &
1777!$ACC COPYIN(ddzu(1:nzt+1), ddzw(1:nzt+1)) &
[4346]1778!$ACC COPYIN(wall_flags_total_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
[3849]1779!$ACC COPYIN(bc_h(0:1)) &
1780!$ACC COPYIN(bc_h(0)%i(1:bc_h(0)%ns)) &
1781!$ACC COPYIN(bc_h(0)%j(1:bc_h(0)%ns)) &
1782!$ACC COPYIN(bc_h(0)%k(1:bc_h(0)%ns)) &
1783!$ACC COPYIN(bc_h(1)%i(1:bc_h(1)%ns)) &
1784!$ACC COPYIN(bc_h(1)%j(1:bc_h(1)%ns)) &
1785!$ACC COPYIN(bc_h(1)%k(1:bc_h(1)%ns))
1786
[680]1787       n_sor = nsor_ini
1788       CALL pres
1789       n_sor = nsor
[1384]1790
[3849]1791!$ACC END DATA
1792!$ACC END DATA
1793
[4648]1794       IF ( debug_output )  THEN
1795          CALL debug_message( 'creating disturbances + applying pressure solver', 'end' )
1796       ENDIF
[3987]1797
[680]1798    ENDIF
1799
[3294]1800    IF ( .NOT. ocean_mode )  THEN
[3274]1801
1802       ALLOCATE( hyp(nzb:nzt+1) )
1803       ALLOCATE( d_exner(nzb:nzt+1) )
1804       ALLOCATE( exner(nzb:nzt+1) )
1805       ALLOCATE( hyrho(nzb:nzt+1) )
[1849]1806!
[3274]1807!--    Check temperature in case of too large domain height
1808       DO  k = nzb, nzt+1
[4648]1809          IF ( ( pt_surface * exner_function( surface_pressure * 100.0_wp ) - g/c_p * zu(k) )      &
1810                 < 0.0_wp )  THEN
1811             WRITE( message_string, * )  'absolute temperature < 0.0 at zu(', k, ') = ', zu(k)
[3685]1812             CALL message( 'init_3d_model', 'PA0142', 1, 2, 0, 6, 0 )
[3274]1813          ENDIF
1814       ENDDO
1815
1816!
1817!--    Calculate vertical profile of the hydrostatic pressure (hyp)
[4648]1818       hyp    = barometric_formula( zu, pt_surface * exner_function( surface_pressure * 100.0_wp ),&
1819                                    surface_pressure * 100.0_wp )
1820       d_exner = exner_function_invers( hyp )
1821       exner = 1.0_wp / exner_function_invers( hyp )
1822       hyrho  = ideal_gas_law_rho_pt( hyp, pt_init )
[3274]1823!
1824!--    Compute reference density
[4648]1825       rho_surface = ideal_gas_law_rho( surface_pressure * 100.0_wp,                               &
1826                                        pt_surface * exner_function( surface_pressure * 100.0_wp ) )
[3274]1827
[96]1828    ENDIF
[1]1829
1830!
1831!-- If required, initialize particles
[3159]1832    IF ( agents_active )  CALL mas_init
1833!
[3937]1834!-- Initialization of synthetic turbulence generator
[4848]1835    IF ( syn_turb_gen )  CALL stg_init
[2696]1836!
[3685]1837!-- Initializing actions for all other modules
1838    CALL module_interface_init
[2696]1839!
[4648]1840!-- Initialize surface layer (done after LSM as roughness length are required for initialization
[3685]1841    IF ( constant_flux_layer )  CALL init_surface_layer_fluxes
[2977]1842!
[3421]1843!-- Initialize surface data output
[3685]1844    IF ( surface_output )  CALL surface_data_output_init
[3472]1845!
[4648]1846!-- Initialize the ws-scheme.
1847    IF ( ws_scheme_sca  .OR.  ws_scheme_mom )  CALL ws_init
[3711]1848!
1849!-- Perform post-initializing checks for all other modules
1850    CALL module_interface_init_checks
[1]1851
1852!
[4648]1853!-- Initialize surface forcing corresponding to large-scale forcing. Therein,
[4548]1854!-- initialize heat-fluxes, etc. via datatype. Revise it later!
[4648]1855    IF ( large_scale_forcing  .AND.  lsf_surf )  THEN
[4548]1856       IF ( use_surface_fluxes  .AND.  constant_heatflux )  THEN
[4648]1857          CALL ls_forcing_surf( simulated_time )
[4548]1858       ENDIF
1859    ENDIF
1860!
[4648]1861!-- Setting weighting factors for calculation of perturbation pressure and turbulent quantities from
1862!-- the RK substeps.
1863    IF ( TRIM( timestep_scheme ) == 'runge-kutta-3' )  THEN      ! for RK3-method
[709]1864
[1322]1865       weight_substep(1) = 1._wp/6._wp
1866       weight_substep(2) = 3._wp/10._wp
1867       weight_substep(3) = 8._wp/15._wp
[709]1868
[1322]1869       weight_pres(1)    = 1._wp/3._wp
1870       weight_pres(2)    = 5._wp/12._wp
1871       weight_pres(3)    = 1._wp/4._wp
[709]1872
[4648]1873    ELSEIF ( TRIM( timestep_scheme ) == 'runge-kutta-2' )  THEN  ! for RK2-method
[709]1874
[1322]1875       weight_substep(1) = 1._wp/2._wp
1876       weight_substep(2) = 1._wp/2._wp
[4648]1877
[1322]1878       weight_pres(1)    = 1._wp/2._wp
[4648]1879       weight_pres(2)    = 1._wp/2._wp
[709]1880
[1001]1881    ELSE                                     ! for Euler-method
[709]1882
[4648]1883       weight_substep(1) = 1.0_wp
1884       weight_pres(1)    = 1.0_wp
[709]1885
[673]1886    ENDIF
1887
1888!
[1]1889!-- Initialize Rayleigh damping factors
[1340]1890    rdf    = 0.0_wp
1891    rdf_sc = 0.0_wp
1892    IF ( rayleigh_damping_factor /= 0.0_wp )  THEN
[3294]1893
1894       IF (  .NOT.  ocean_mode )  THEN
[108]1895          DO  k = nzb+1, nzt
1896             IF ( zu(k) >= rayleigh_damping_height )  THEN
[4648]1897                rdf(k) = rayleigh_damping_factor *                                                 &
1898                         ( SIN( pi * 0.5_wp * ( zu(k) - rayleigh_damping_height )                  &
1899                                / ( zu(nzt) - rayleigh_damping_height ) )                          &
1900                         )**2
[108]1901             ENDIF
1902          ENDDO
1903       ELSE
[3294]1904!
[4648]1905!--       In ocean mode, rayleigh damping is applied in the lower part of the model domain
[108]1906          DO  k = nzt, nzb+1, -1
1907             IF ( zu(k) <= rayleigh_damping_height )  THEN
[4648]1908                rdf(k) = rayleigh_damping_factor *                                                 &
1909                         ( SIN( pi * 0.5_wp * ( rayleigh_damping_height - zu(k) )                  &
1910                                / ( rayleigh_damping_height - zu(nzb+1) ) )                        &
1911                         )**2
[108]1912             ENDIF
1913          ENDDO
1914       ENDIF
[3294]1915
[1]1916    ENDIF
[785]1917    IF ( scalar_rayleigh_damping )  rdf_sc = rdf
[1]1918
1919!
[4648]1920!-- Initialize the starting level and the vertical smoothing factor used for the external pressure
1921!-- gradient
[1340]1922    dp_smooth_factor = 1.0_wp
[240]1923    IF ( dp_external )  THEN
1924!
[4648]1925!--    Set the starting level dp_level_ind_b only if it has not been set before (e.g. in init_grid).
[240]1926       IF ( dp_level_ind_b == 0 )  THEN
1927          ind_array = MINLOC( ABS( dp_level_b - zu ) )
[4648]1928          dp_level_ind_b = ind_array(1) - 1 + nzb
[240]1929                                        ! MINLOC uses lower array bound 1
1930       ENDIF
1931       IF ( dp_smooth )  THEN
[1340]1932          dp_smooth_factor(:dp_level_ind_b) = 0.0_wp
[240]1933          DO  k = dp_level_ind_b+1, nzt
[4648]1934             dp_smooth_factor(k) = 0.5_wp * ( 1.0_wp + SIN( pi *                                   &
1935                                             ( REAL( k - dp_level_ind_b, KIND=wp ) /               &
1936                                               REAL( nzt - dp_level_ind_b, KIND=wp ) - 0.5_wp ) ) )
[240]1937          ENDDO
1938       ENDIF
1939    ENDIF
1940
1941!
[4648]1942!-- Initialize damping zone for the potential temperature in case of non-cyclic lateral boundaries.
1943!-- The damping zone has the maximum value at the inflow boundary and decreases to zero at
1944!-- pt_damping_width.
[1340]1945    ptdf_x = 0.0_wp
1946    ptdf_y = 0.0_wp
[1159]1947    IF ( bc_lr_dirrad )  THEN
[996]1948       DO  i = nxl, nxr
[978]1949          IF ( ( i * dx ) < pt_damping_width )  THEN
[4648]1950             ptdf_x(i) = pt_damping_factor * ( SIN( pi * 0.5_wp *                                  &
1951                                                    REAL( pt_damping_width - i * dx, KIND=wp ) /   &
1952                                                    REAL( pt_damping_width, KIND=wp ) ) )**2
1953                                  ENDIF
[73]1954       ENDDO
[1159]1955    ELSEIF ( bc_lr_raddir )  THEN
[996]1956       DO  i = nxl, nxr
[978]1957          IF ( ( i * dx ) > ( nx * dx - pt_damping_width ) )  THEN
[4648]1958             ptdf_x(i) = pt_damping_factor * SIN( pi * 0.5_wp *                                    &
1959                                                  ( ( i - nx ) * dx + pt_damping_width ) /         &
1960                                                  REAL( pt_damping_width, KIND=wp ) )**2
[73]1961          ENDIF
[4648]1962       ENDDO
[1159]1963    ELSEIF ( bc_ns_dirrad )  THEN
[996]1964       DO  j = nys, nyn
[978]1965          IF ( ( j * dy ) > ( ny * dy - pt_damping_width ) )  THEN
[4648]1966             ptdf_y(j) = pt_damping_factor * SIN( pi * 0.5_wp *                                    &
1967                                                  ( ( j - ny ) * dy + pt_damping_width ) /         &
1968                                                  REAL( pt_damping_width, KIND=wp ) )**2
[1]1969          ENDIF
[4648]1970       ENDDO
[1159]1971    ELSEIF ( bc_ns_raddir )  THEN
[996]1972       DO  j = nys, nyn
[978]1973          IF ( ( j * dy ) < pt_damping_width )  THEN
[4648]1974             ptdf_y(j) = pt_damping_factor * SIN( pi * 0.5_wp *                                    &
1975                                                  ( pt_damping_width - j * dy ) /                  &
1976                                                  REAL( pt_damping_width, KIND=wp ) )**2
[1]1977          ENDIF
[73]1978       ENDDO
[1]1979    ENDIF
[51]1980
[1]1981!
[4648]1982!-- Input binary data file is not needed anymore. This line must be placed after call of user_init!
[1]1983    CALL close_file( 13 )
[2934]1984!
[4648]1985!-- In case of nesting, put an barrier to assure that all parent and child domains finished
1986!-- initialization.
[2934]1987#if defined( __parallel )
1988    IF ( nested_run )  CALL MPI_BARRIER( MPI_COMM_WORLD, ierr )
1989#endif
[1]1990
[2934]1991
[3987]1992    CALL location_message( 'model initialization', 'finished' )
[1]1993
1994 END SUBROUTINE init_3d_model
Note: See TracBrowser for help on using the repository browser.