source: palm/trunk/SOURCE/time_integration.f90 @ 4174

Last change on this file since 4174 was 4170, checked in by gronemeier, 5 years ago

changes in turbulence_closure_mod:

  • add performance optimizations according to K. Ketelsen to diffusion_e and tcm_diffusivities_default
  • bugfix in calculating l_wall for vertical walls
  • bugfix in using l_wall in initialization (consider wall_adjustment_factor)
  • always initialize diss and save the dissipation to that array

related changes in time_integration:

  • copy diss, diss_p, tdiss_m to GPU
  • Property svn:keywords set to Id
File size: 81.0 KB
Line 
1!> @file time_integration.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2019 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: time_integration.f90 4170 2019-08-19 17:12:31Z gronemeier $
27! copy diss, diss_p, tdiss_m to GPU
28!
29! 4144 2019-08-06 09:11:47Z raasch
30! relational operators .EQ., .NE., etc. replaced by ==, /=, etc.
31!
32! 4126 2019-07-30 11:09:11Z gronemeier
33! renamed routine to calculate uv exposure
34!
35! 4111 2019-07-22 18:16:57Z suehring
36! advc_flags_1 / advc_flags_2 renamed to advc_flags_m / advc_flags_s
37!
38! 4069 2019-07-01 14:05:51Z Giersch
39! Masked output running index mid has been introduced as a local variable to
40! avoid runtime error (Loop variable has been modified) in time_integration
41!
42! 4064 2019-07-01 05:33:33Z gronemeier
43! Moved call to radiation module out of intermediate time loop
44!
45! 4048 2019-06-21 21:00:21Z knoop
46! Moved production_e_init call into turbulence_closure_mod
47!
48! 4047 2019-06-21 18:58:09Z knoop
49! Added remainings of swap_timelevel upon its dissolution
50!
51! 4043 2019-06-18 16:59:00Z schwenkel
52! Further LPM modularization
53!
54! 4039 2019-06-18 10:32:41Z suehring
55! Rename subroutines in module for diagnostic quantities
56!
57! 4029 2019-06-14 14:04:35Z raasch
58! exchange of ghost points and boundary conditions separated for chemical species and SALSA module,
59! bugfix: decycling of chemistry species after nesting data transfer
60!
61! 4022 2019-06-12 11:52:39Z suehring
62! Call synthetic turbulence generator at last RK3 substep right after boundary
63! conditions are updated in offline nesting in order to assure that
64! perturbations are always imposed
65!
66! 4017 2019-06-06 12:16:46Z schwenkel
67! Mass (volume) flux correction included to ensure global mass conservation for child domains.
68!
69! 3994 2019-05-22 18:08:09Z suehring
70! output of turbulence intensity added
71!
72! 3988 2019-05-22 11:32:37Z kanani
73! Implement steerable output interval for virtual measurements
74!
75! 3968 2019-05-13 11:04:01Z suehring
76! replace nspec_out with n_matched_vars
77!
78! 3929 2019-04-24 12:52:08Z banzhafs
79! Reverse changes back from revision 3878: use chem_boundary_conds instead of
80! chem_boundary_conds_decycle
81!
82!
83! 3885 2019-04-11 11:29:34Z kanani
84! Changes related to global restructuring of location messages and introduction
85! of additional debug messages
86!
87! 3879 2019-04-08 20:25:23Z knoop
88! Moved wtm_forces to module_interface_actions
89!
90! 3872 2019-04-08 15:03:06Z knoop
91! Modifications made for salsa:
92! - Call salsa_emission_update at each time step but do the checks within
93!   salsa_emission_update (i.e. skip_time_do_salsa >= time_since_reference_point
94!   and next_aero_emission_update <= time_since_reference_point ).
95! - Renamed nbins --> nbins_aerosol, ncc_tot --> ncomponents_mass and
96!   ngast --> ngases_salsa and loop indices b, c and sg to ib, ic and ig
97! - Apply nesting for salsa variables
98! - Removed cpu_log calls speciffic for salsa.
99!
100! 3833 2019-03-28 15:04:04Z forkel
101! added USE chem_gasphase_mod, replaced nspec by nspec since fixed compounds are not integrated
102!
103! 3820 2019-03-27 11:53:41Z forkel
104! renamed do_emiss to emissions_anthropogenic (ecc)
105!
106!
107! 3774 2019-03-04 10:52:49Z moh.hefny
108! rephrase if statement to avoid unallocated array in case of
109! nesting_offline is false (crashing during debug mode)
110!
111! 3761 2019-02-25 15:31:42Z raasch $
112! module section re-formatted and openacc required variables moved to separate section,
113! re-formatting to 100 char line width
114!
115! 3745 2019-02-15 18:57:56Z suehring
116! Call indoor model after first timestep
117!
118! 3744 2019-02-15 18:38:58Z suehring
119! - Moved call of bio_calculate_thermal_index_maps from biometeorology module to
120! time_integration to make sure averaged input is updated before calculating.
121!
122! 3739 2019-02-13 08:05:17Z dom_dwd_user
123! Removed everything related to "time_bio_results" as this is never used.
124!
125! 3724 2019-02-06 16:28:23Z kanani
126! Correct double-used log_point_s unit
127!
128! 3719 2019-02-06 13:10:18Z kanani
129! - removed wind_turbine cpu measurement, since same time is measured inside
130!   wtm_forces subroutine as special measures
131! - moved the numerous vnest cpulog to special measures
132! - extended radiation cpulog over entire radiation part,
133!   moved radiation_interactions cpulog to special measures
134! - moved some cpu_log calls to this routine for better overview
135!
136! 3705 2019-01-29 19:56:39Z suehring
137! Data output for virtual measurements added
138!
139! 3704 2019-01-29 19:51:41Z suehring
140! Rename subroutines for surface-data output
141!
142! 3647 2019-01-02 14:10:44Z kanani
143! Bugfix: add time_since_reference_point to IF clause for data_output calls
144! (otherwise skip_time_* values don't come into affect with dt_do* = 0.0).
145! Clean up indoor_model and biometeorology model call.
146!
147! 3646 2018-12-28 17:58:49Z kanani
148! Bugfix: use time_since_reference_point instead of simulated_time where
149! required (relevant when using wall/soil spinup)
150!
151! 3634 2018-12-18 12:31:28Z knoop
152! OpenACC port for SPEC
153!
154! 3597 2018-12-04 08:40:18Z maronga
155! Removed call to calculation of near air (10 cm) potential temperature (now in
156! surface layer fluxes)
157!
158! 3589 2018-11-30 15:09:51Z suehring
159! Move the control parameter "salsa" from salsa_mod to control_parameters
160! (M. Kurppa)
161!
162! 3582 2018-11-29 19:16:36Z suehring
163! dom_dwd_user, Schrempf:
164! Changes due to merge of uv exposure model into biometeorology_mod.
165!
166! 3525 2018-11-14 16:06:14Z kanani
167! Changes related to clean-up of biometeorology (dom_dwd_user)
168!
169! 3524 2018-11-14 13:36:44Z raasch
170! unused variables removed
171!
172! 3484 2018-11-02 14:41:25Z hellstea
173! pmci_ensure_nest_mass_conservation is premanently removed
174!
175! 3473 2018-10-30 20:50:15Z suehring
176! new module for virtual measurements introduced
177!
178! 3472 2018-10-30 20:43:50Z suehring
179! Add indoor model (kanani, srissman, tlang)
180!
181! 3467 2018-10-30 19:05:21Z suehring
182! Implementation of a new aerosol module salsa.
183!
184! 3448 2018-10-29 18:14:31Z kanani
185! Add biometeorology
186!
187! 3421 2018-10-24 18:39:32Z gronemeier
188! Surface data output
189!
190! 3418 2018-10-24 16:07:39Z kanani
191! call to material_heat_model now with check if spinup runs (rvtils)
192!
193! 3378 2018-10-19 12:34:59Z kanani
194! merge from radiation branch (r3362) into trunk
195! (moh.hefny):
196! Bugfix in the if statement to call radiation_interaction
197!
198! 3347 2018-10-15 14:21:08Z suehring
199! - offline nesting separated from large-scale forcing module
200! - changes for synthetic turbulence generator
201!
202! 3343 2018-10-15 10:38:52Z suehring
203! - Formatting, clean-up, comments (kanani)
204! - Added CALL to chem_emissions_setup (Russo)
205! - Code added for decycling chemistry (basit)
206!
207! 3294 2018-10-01 02:37:10Z raasch
208! changes concerning modularization of ocean option
209!
210! 3274 2018-09-24 15:42:55Z knoop
211! Modularization of all bulk cloud physics code components
212!
213! 3241 2018-09-12 15:02:00Z raasch
214! unused variables removed
215!
216! 3198 2018-08-15 09:23:10Z sward
217! Added multi_agent_system_end; defined start time for MAS relative to
218! time_since_reference_point
219!
220! 3183 2018-07-27 14:25:55Z suehring
221! Replace simulated_time by time_since_reference_point in COSMO nesting mode.
222! Rename subroutines and variables in COSMO nesting mode
223!
224! 3182 2018-07-27 13:36:03Z suehring
225! Added multi agent system
226!
227! 3042 2018-05-25 10:44:37Z schwenkel
228! Changed the name specific humidity to mixing ratio
229!
230! 3040 2018-05-25 10:22:08Z schwenkel
231! Fixed bug in IF statement
232! Ensure that the time when calling the radiation to be the time step of the
233! pre-calculated time when first calculate the positions of the sun
234!
235! 3004 2018-04-27 12:33:25Z Giersch
236! First call of flow_statistics has been removed. It is already called in
237! run_control itself
238!
239! 2984 2018-04-18 11:51:30Z hellstea
240! CALL pmci_ensure_nest_mass_conservation is removed (so far only commented out)
241! as seemingly unnecessary.
242!
243! 2941 2018-04-03 11:54:58Z kanani
244! Deduct spinup_time from RUN_CONTROL output of main 3d run
245! (use time_since_reference_point instead of simulated_time)
246!
247! 2938 2018-03-27 15:52:42Z suehring
248! Nesting of dissipation rate in case of RANS mode and TKE-e closure is applied
249!
250! 2936 2018-03-27 14:49:27Z suehring
251! Little formatting adjustment.
252!
253! 2817 2018-02-19 16:32:21Z knoop
254! Preliminary gust module interface implemented
255!
256! 2801 2018-02-14 16:01:55Z thiele
257! Changed lpm from subroutine to module.
258! Introduce particle transfer in nested models.
259!
260! 2776 2018-01-31 10:44:42Z Giersch
261! Variable use_synthetic_turbulence_generator has been abbreviated
262!
263! 2773 2018-01-30 14:12:54Z suehring
264! - Nesting for chemical species
265!
266! 2766 2018-01-22 17:17:47Z kanani
267! Removed preprocessor directive __chem
268!
269! 2718 2018-01-02 08:49:38Z maronga
270! Corrected "Former revisions" section
271!
272! 2696 2017-12-14 17:12:51Z kanani
273! - Change in file header (GPL part)
274! - Implementation of uv exposure model (FK)
275! - Moved vnest_boundary_conds_khkm from tcm_diffusivities to here (TG)
276! - renamed diffusivities to tcm_diffusivities (TG)
277! - implement prognostic equation for diss (TG)
278! - Moved/commented CALL to chem_emissions (FK)
279! - Added CALL to chem_emissions (FK)
280! - Implementation of chemistry module (FK)
281! - Calls for setting boundary conditions in USM and LSM (MS)
282! - Large-scale forcing with larger-scale models implemented (MS)
283! - Rename usm_radiation into radiation_interactions; merge with branch
284!   radiation (MS)
285! - added call for usm_green_heat_model for green building surfaces (RvT)
286! - added call for usm_temperature_near_surface for use in indoor model (RvT)
287!
288! 2617 2017-11-16 12:47:24Z suehring
289! Bugfix, assure that the reference state does not become zero.
290!
291! 2563 2017-10-19 15:36:10Z Giersch
292! Variable wind_turbine moved to module control_parameters
293!
294! 2365 2017-08-21 14:59:59Z kanani
295! Vertical grid nesting implemented (SadiqHuq)
296!
297! 2320 2017-07-21 12:47:43Z suehring
298! Set bottom boundary conditions after nesting interpolation and anterpolation
299!
300! 2299 2017-06-29 10:14:38Z maronga
301! Call of soil model adjusted
302!
303! 2292 2017-06-20 09:51:42Z schwenkel
304! Implementation of new microphysic scheme: cloud_scheme = 'morrison'
305! includes two more prognostic equations for cloud drop concentration (nc) 
306! and cloud water content (qc).
307!
308! 2271 2017-06-09 12:34:55Z sward
309! Start timestep message changed
310!
311! 2259 2017-06-08 09:09:11Z gronemeier
312! Implemented synthetic turbulence generator
313!
314! 2233 2017-05-30 18:08:54Z suehring
315!
316! 2232 2017-05-30 17:47:52Z suehring
317! Adjustments to new topography and surface concept
318! Modify passed parameters for disturb_field
319!
320! 2178 2017-03-17 11:07:39Z hellstea
321! Setting perturbations at all times near inflow boundary is removed
322! in case of nested boundaries
323!
324! 2174 2017-03-13 08:18:57Z maronga
325! Added support for nesting with cloud microphysics
326!
327! 2118 2017-01-17 16:38:49Z raasch
328! OpenACC directives and related code removed
329!
330! 2050 2016-11-08 15:00:55Z gronemeier
331! Implement turbulent outflow condition
332!
333! 2031 2016-10-21 15:11:58Z knoop
334! renamed variable rho to rho_ocean
335!
336! 2011 2016-09-19 17:29:57Z kanani
337! Flag urban_surface is now defined in module control_parameters,
338! removed commented CALLs of global_min_max.
339!
340! 2007 2016-08-24 15:47:17Z kanani
341! Added CALLs for new urban surface model
342!
343! 2000 2016-08-20 18:09:15Z knoop
344! Forced header and separation lines into 80 columns
345!
346! 1976 2016-07-27 13:28:04Z maronga
347! Simplified calls to radiation model
348!
349! 1960 2016-07-12 16:34:24Z suehring
350! Separate humidity and passive scalar
351!
352! 1957 2016-07-07 10:43:48Z suehring
353! flight module added
354!
355! 1919 2016-05-27 14:51:23Z raasch
356! Initial version of purely vertical nesting introduced.
357!
358! 1918 2016-05-27 14:35:57Z raasch
359! determination of time step moved to the end of the time step loop,
360! the first time step is now always calculated before the time step loop (i.e.
361! also in case of restart runs)
362!
363! 1914 2016-05-26 14:44:07Z witha
364! Added call for wind turbine model
365!
366! 1878 2016-04-19 12:30:36Z hellstea
367! Synchronization for nested runs rewritten
368!
369! 1853 2016-04-11 09:00:35Z maronga
370! Adjusted for use with radiation_scheme = constant
371!
372! 1849 2016-04-08 11:33:18Z hoffmann
373! Adapted for modularization of microphysics
374!
375! 1833 2016-04-07 14:23:03Z raasch
376! spectrum renamed spectra_mod, spectra related variables moved to spectra_mod
377!
378! 1831 2016-04-07 13:15:51Z hoffmann
379! turbulence renamed collision_turbulence
380!
381! 1822 2016-04-07 07:49:42Z hoffmann
382! icloud_scheme replaced by microphysics_*
383!
384! 1808 2016-04-05 19:44:00Z raasch
385! output message in case unscheduled radiation calls removed
386!
387! 1797 2016-03-21 16:50:28Z raasch
388! introduction of different datatransfer modes
389!
390! 1791 2016-03-11 10:41:25Z raasch
391! call of pmci_update_new removed
392!
393! 1786 2016-03-08 05:49:27Z raasch
394! +module spectrum
395!
396! 1783 2016-03-06 18:36:17Z raasch
397! switch back of netcdf data format for mask output moved to the mask output
398! routine
399!
400! 1781 2016-03-03 15:12:23Z raasch
401! some pmc calls removed at the beginning (before timeloop),
402! pmc initialization moved to the main program
403!
404! 1764 2016-02-28 12:45:19Z raasch
405! PMC_ACTIVE flags removed,
406! bugfix: nest synchronization after first call of timestep
407!
408! 1762 2016-02-25 12:31:13Z hellstea
409! Introduction of nested domain feature
410!
411! 1736 2015-12-04 08:56:33Z raasch
412! no perturbations added to total domain if energy limit has been set zero
413!
414! 1691 2015-10-26 16:17:44Z maronga
415! Added option for spin-ups without land surface and radiation models. Moved calls
416! for radiation and lan surface schemes.
417!
418! 1682 2015-10-07 23:56:08Z knoop
419! Code annotations made doxygen readable
420!
421! 1671 2015-09-25 03:29:37Z raasch
422! bugfix: ghostpoint exchange for array diss in case that sgs velocities are used
423! for particles
424!
425! 1585 2015-04-30 07:05:52Z maronga
426! Moved call of radiation scheme. Added support for RRTM
427!
428! 1551 2015-03-03 14:18:16Z maronga
429! Added interface for different radiation schemes.
430!
431! 1496 2014-12-02 17:25:50Z maronga
432! Added calls for the land surface model and radiation scheme
433!
434! 1402 2014-05-09 14:25:13Z raasch
435! location messages modified
436!
437! 1384 2014-05-02 14:31:06Z raasch
438! location messages added
439!
440! 1380 2014-04-28 12:40:45Z heinze
441! CALL of nudge_ref added
442! bc_pt_t_val and bc_q_t_val are updated in case nudging is used
443!
444! 1365 2014-04-22 15:03:56Z boeske
445! Reset sums_ls_l to zero at each timestep
446! +sums_ls_l
447! Calculation of reference state (previously in subroutine calc_mean_profile)
448
449! 1342 2014-03-26 17:04:47Z kanani
450! REAL constants defined as wp-kind
451!
452! 1320 2014-03-20 08:40:49Z raasch
453! ONLY-attribute added to USE-statements,
454! kind-parameters added to all INTEGER and REAL declaration statements,
455! kinds are defined in new module kinds,
456! old module precision_kind is removed,
457! revision history before 2012 removed,
458! comment fields (!:) to be used for variable explanations added to
459! all variable declaration statements
460! 1318 2014-03-17 13:35:16Z raasch
461! module interfaces removed
462!
463! 1308 2014-03-13 14:58:42Z fricke
464! +netcdf_data_format_save
465! For masked data, parallel netcdf output is not tested so far, hence
466! netcdf_data_format is switched back to non-paralell output.
467!
468! 1276 2014-01-15 13:40:41Z heinze
469! Use LSF_DATA also in case of Dirichlet bottom boundary condition for scalars
470!
471! 1257 2013-11-08 15:18:40Z raasch
472! acc-update-host directive for timestep removed
473!
474! 1241 2013-10-30 11:36:58Z heinze
475! Generalize calc_mean_profile for wider use
476! Determine shf and qsws in dependence on data from LSF_DATA
477! Determine ug and vg in dependence on data from LSF_DATA
478! 1221 2013-09-10 08:59:13Z raasch
479! host update of arrays before timestep is called
480!
481! 1179 2013-06-14 05:57:58Z raasch
482! mean profiles for reference state are only calculated if required,
483! small bugfix for background communication
484!
485! 1171 2013-05-30 11:27:45Z raasch
486! split of prognostic_equations deactivated (comment lines), for the time being
487!
488! 1128 2013-04-12 06:19:32Z raasch
489! asynchronous transfer of ghost point data realized for acc-optimized version:
490! prognostic_equations are first called two times for those points required for
491! the left-right and north-south exchange, respectively, and then for the
492! remaining points,
493! those parts requiring global communication moved from prognostic_equations to
494! here
495!
496! 1115 2013-03-26 18:16:16Z hoffmann
497! calculation of qr and nr is restricted to precipitation
498!
499! 1113 2013-03-10 02:48:14Z raasch
500! GPU-porting of boundary conditions,
501! openACC directives updated
502! formal parameter removed from routine boundary_conds
503!
504! 1111 2013-03-08 23:54:10Z raasch
505! +internal timestep counter for cpu statistics added,
506! openACC directives updated
507!
508! 1092 2013-02-02 11:24:22Z raasch
509! unused variables removed
510!
511! 1065 2012-11-22 17:42:36Z hoffmann
512! exchange of diss (dissipation rate) in case of turbulence = .TRUE. added
513!
514! 1053 2012-11-13 17:11:03Z hoffmann
515! exchange of ghost points for nr, qr added
516!
517! 1036 2012-10-22 13:43:42Z raasch
518! code put under GPL (PALM 3.9)
519!
520! 1019 2012-09-28 06:46:45Z raasch
521! non-optimized version of prognostic_equations removed
522!
523! 1015 2012-09-27 09:23:24Z raasch
524! +call of prognostic_equations_acc
525!
526! 1001 2012-09-13 14:08:46Z raasch
527! all actions concerning leapfrog- and upstream-spline-scheme removed
528!
529! 849 2012-03-15 10:35:09Z raasch
530! advec_particles renamed lpm, first_call_advec_particles renamed first_call_lpm
531!
532! 825 2012-02-19 03:03:44Z raasch
533! wang_collision_kernel renamed wang_kernel
534!
535! Revision 1.1  1997/08/11 06:19:04  raasch
536! Initial revision
537!
538!
539! Description:
540! ------------
541!> Integration in time of the model equations, statistical analysis and graphic
542!> output
543!------------------------------------------------------------------------------!
544 SUBROUTINE time_integration
545 
546
547    USE advec_ws,                                                                                  &
548        ONLY:  ws_statistics
549
550    USE arrays_3d,                                                                                 &
551        ONLY:  diss, diss_p, dzu, e, e_p, nc, nc_p, nr, nr_p, prho, pt, pt_p, pt_init, q_init, q,  &
552               qc, qc_p, ql, ql_c, ql_v, ql_vp, qr, qr_p, q_p, ref_state, rho_ocean, s, s_p, sa_p, &
553               tend, u, u_p, v, vpt, v_p, w, w_p
554
555    USE biometeorology_mod,                                                                        &
556        ONLY:  bio_calculate_thermal_index_maps, thermal_comfort, bio_calculate_uv_exposure,       &
557               uv_exposure
558
559    USE bulk_cloud_model_mod,                                                                      &
560        ONLY: bulk_cloud_model, calc_liquid_water_content, collision_turbulence,                   &
561              microphysics_morrison, microphysics_seifert
562
563    USE calc_mean_profile_mod,                                                                     &
564        ONLY:  calc_mean_profile
565
566    USE chem_emissions_mod,                                                                        &
567        ONLY:  chem_emissions_setup
568
569    USE chem_gasphase_mod,                                                                         &
570        ONLY:  nvar
571
572    USE chem_modules,                                                                              &
573        ONLY:  bc_cs_t_val, chem_species, cs_name, emissions_anthropogenic, n_matched_vars
574
575    USE chemistry_model_mod,                                                                       &
576        ONLY:  chem_boundary_conds
577
578    USE control_parameters,                                                                        &
579        ONLY:  advected_distance_x, advected_distance_y, air_chemistry, average_count_3d,          &
580               averaging_interval, averaging_interval_pr, bc_lr_cyc, bc_ns_cyc, bc_pt_t_val,       &
581               bc_q_t_val, biometeorology, call_psolver_at_all_substeps,  child_domain,            &
582               cloud_droplets, constant_flux_layer, constant_heatflux, create_disturbances,        &
583               dopr_n, constant_diffusion, coupling_mode, coupling_start_time,                     &
584               current_timestep_number, disturbance_created, disturbance_energy_limit, dist_range, &
585               do_sum, dt_3d, dt_averaging_input, dt_averaging_input_pr, dt_coupling,              &
586               dt_data_output_av, dt_disturb, dt_do2d_xy, dt_do2d_xz, dt_do2d_yz, dt_do3d,         &
587               dt_domask,dt_dopts, dt_dopr, dt_dopr_listing, dt_dots, dt_run_control,              &
588               end_time, first_call_lpm, first_call_mas, galilei_transformation, humidity,         &
589               indoor_model, intermediate_timestep_count, intermediate_timestep_count_max,         &
590               land_surface, large_scale_forcing, loop_optimization, lsf_surf, lsf_vert, masks,    &
591               multi_agent_system_end, multi_agent_system_start, nesting_offline, neutral,         &
592               nr_timesteps_this_run, nudging, ocean_mode, passive_scalar, pt_reference,           &
593               pt_slope_offset, random_heatflux, rans_mode, rans_tke_e, run_coupled, salsa,        &
594               simulated_time, simulated_time_chr, skip_time_do2d_xy, skip_time_do2d_xz,           &
595               skip_time_do2d_yz, skip_time_do3d, skip_time_domask, skip_time_dopr,                &
596               skip_time_data_output_av, sloping_surface, stop_dt, surface_output,                 &
597               terminate_coupled, terminate_run, timestep_scheme, time_coupling, time_do2d_xy,     &
598               time_do2d_xz, time_do2d_yz, time_do3d, time_domask, time_dopr, time_dopr_av,        &
599               time_dopr_listing, time_dopts, time_dosp, time_dosp_av, time_dots, time_do_av,      &
600               time_do_sla, time_disturb, time_run_control, time_since_reference_point,            &
601               turbulent_inflow, turbulent_outflow, urban_surface,                                 &
602               use_initial_profile_as_reference, use_single_reference_value, u_gtrans, v_gtrans,   &
603               virtual_flight, virtual_measurement, ws_scheme_mom, ws_scheme_sca, timestep_count
604
605    USE cpulog,                                                                                    &
606        ONLY:  cpu_log, log_point, log_point_s
607
608    USE date_and_time_mod,                                                                         &
609        ONLY:  calc_date_and_time, hour_call_emis, hour_of_year
610
611    USE diagnostic_output_quantities_mod,                                                          &
612        ONLY:  doq_calculate,                                                                      &
613               timestep_number_at_prev_calc
614
615    USE flight_mod,                                                                                &
616        ONLY:  flight_measurement
617
618    USE indices,                                                                                   &
619        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, nzb, nzt
620
621    USE indoor_model_mod,                                                                          &
622        ONLY:  dt_indoor, im_main_heatcool, time_indoor
623
624    USE interfaces
625
626    USE kinds
627
628    USE land_surface_model_mod,                                                                    &
629        ONLY:  lsm_boundary_condition, lsm_energy_balance, lsm_soil_model, skip_time_do_lsm
630
631    USE lagrangian_particle_model_mod,                                                             &
632        ONLY:  lpm_data_output_ptseries, lpm_interaction_droplets_ptq
633
634    USE lsf_nudging_mod,                                                                           &
635        ONLY:  calc_tnudge, ls_forcing_surf, ls_forcing_vert, nudge_ref
636
637    USE module_interface,                                                                          &
638        ONLY:  module_interface_actions, module_interface_swap_timelevel
639
640    USE multi_agent_system_mod,                                                                    &
641        ONLY:  agents_active, multi_agent_system
642
643    USE nesting_offl_mod,                                                                          &
644        ONLY:  nesting_offl_bc, nesting_offl_mass_conservation
645
646    USE netcdf_data_input_mod,                                                                     &
647        ONLY:  chem_emis, chem_emis_att, nest_offl, netcdf_data_input_offline_nesting
648
649    USE ocean_mod,                                                                                 &
650        ONLY:  prho_reference
651
652    USE particle_attributes,                                                                       &
653        ONLY:  particle_advection, particle_advection_start, use_sgs_for_particles, wang_kernel
654
655    USE pegrid
656
657    USE pmc_interface,                                                                             &
658        ONLY:  nested_run, nesting_mode, pmci_boundary_conds, pmci_datatrans, pmci_synchronize,    &
659        pmci_ensure_nest_mass_conservation, pmci_ensure_nest_mass_conservation_vertical,           &
660        pmci_set_swaplevel
661
662    USE progress_bar,                                                                              &
663        ONLY:  finish_progress_bar, output_progress_bar
664
665    USE prognostic_equations_mod,                                                                  &
666        ONLY:  prognostic_equations_cache, prognostic_equations_vector
667
668    USE radiation_model_mod,                                                                       &
669        ONLY: dt_radiation, force_radiation_call, radiation, radiation_control,                    &
670              radiation_interaction, radiation_interactions, skip_time_do_radiation, time_radiation
671
672    USE salsa_mod,                                                                                 &
673        ONLY: aerosol_number, aerosol_mass, bc_am_t_val, bc_an_t_val, bc_gt_t_val,                 &
674              nbins_aerosol, ncomponents_mass, ngases_salsa, salsa_boundary_conds,                 &
675              salsa_emission_update, salsa_gas, salsa_gases_from_chem, skip_time_do_salsa
676
677    USE spectra_mod,                                                                               &
678        ONLY: average_count_sp, averaging_interval_sp, calc_spectra, dt_dosp, skip_time_dosp
679
680    USE statistics,                                                                                &
681        ONLY:  flow_statistics_called, hom, pr_palm, sums_ls_l
682
683
684    USE surface_layer_fluxes_mod,                                                                  &
685        ONLY:  surface_layer_fluxes
686
687    USE surface_data_output_mod,                                                                   &
688        ONLY:  average_count_surf, averaging_interval_surf, dt_dosurf, dt_dosurf_av,               &
689               surface_data_output, surface_data_output_averaging, skip_time_dosurf,               &
690               skip_time_dosurf_av, time_dosurf, time_dosurf_av
691
692    USE surface_mod,                                                                               &
693        ONLY:  surf_def_h, surf_lsm_h, surf_usm_h
694
695    USE synthetic_turbulence_generator_mod,                                                        &
696        ONLY:  dt_stg_call, dt_stg_adjust, parametrize_inflow_turbulence, stg_adjust, stg_main,    &
697               time_stg_adjust, time_stg_call, use_syn_turb_gen
698
699    USE turbulence_closure_mod,                                                                    &
700        ONLY:  tcm_diffusivities
701
702    USE urban_surface_mod,                                                                         &
703        ONLY:  usm_boundary_condition, usm_material_heat_model, usm_material_model,                &
704               usm_surface_energy_balance, usm_green_heat_model
705
706    USE vertical_nesting_mod,                                                                      &
707        ONLY:  vnested, vnest_anterpolate, vnest_anterpolate_e, vnest_boundary_conds,              &
708               vnest_boundary_conds_khkm, vnest_deallocate, vnest_init, vnest_init_fine,           &
709               vnest_start_time
710
711    USE virtual_measurement_mod,                                                                   &
712        ONLY:  dt_virtual_measurement,                                                             &
713               time_virtual_measurement,                                                           &
714               vm_data_output,                                                                     &
715               vm_sampling,                                                                        &
716               vm_time_start
717
718
719#if defined( _OPENACC )
720    USE arrays_3d,                                                             &
721        ONLY:  d, dd2zu, ddzu, ddzw, drho_air, drho_air_zw, dzw, heatflux_output_conversion, kh,   &
722               km, momentumflux_output_conversion, p, ptdf_x, ptdf_y, rdf, rdf_sc, rho_air,        &
723               rho_air_zw, tdiss_m, te_m, tpt_m, tu_m, tv_m, tw_m, ug, u_init, u_stokes_zu, vg,    &
724               v_init, v_stokes_zu, zu
725
726    USE control_parameters,                                                                        &
727        ONLY:  tsc
728
729    USE indices,                                                                                   &
730        ONLY:  advc_flags_m, advc_flags_s, nyn, nyng, nys, nysg, nz, nzb_max, wall_flags_0
731
732    USE statistics,                                                                                &
733        ONLY:  rmask, statistic_regions, sums_l, sums_l_l, sums_us2_ws_l,                          &
734               sums_wsus_ws_l, sums_vs2_ws_l, sums_wsvs_ws_l, sums_ws2_ws_l, sums_wspts_ws_l,      &
735               sums_wsqs_ws_l, sums_wssas_ws_l, sums_wsqcs_ws_l, sums_wsqrs_ws_l, sums_wsncs_ws_l, &
736               sums_wsnrs_ws_l, sums_wsss_ws_l, weight_substep, sums_salsa_ws_l
737
738    USE surface_mod,                                                                               &
739        ONLY:  bc_h, enter_surface_arrays, exit_surface_arrays
740#endif
741
742
743    IMPLICIT NONE
744
745    CHARACTER (LEN=9) ::  time_to_string   !<
746
747    INTEGER(iwp) ::  ib        !< index for aerosol size bins
748    INTEGER(iwp) ::  ic        !< index for aerosol mass bins
749    INTEGER(iwp) ::  icc       !< additional index for aerosol mass bins
750    INTEGER(iwp) ::  ig        !< index for salsa gases
751    INTEGER(iwp) ::  lsp
752    INTEGER(iwp) ::  lsp_usr   !<
753    INTEGER(iwp) ::  mid       !< masked output running index
754    INTEGER(iwp) ::  n         !< loop counter for chemistry species
755
756    REAL(wp) ::  dt_3d_old  !< temporary storage of timestep to be used for
757                            !< steering of run control output interval
758    REAL(wp) ::  time_since_reference_point_save  !< original value of
759                                                  !< time_since_reference_point
760
761
762!
763!-- Copy data from arrays_3d
764!$ACC DATA &
765!$ACC COPY(d(nzb+1:nzt,nys:nyn,nxl:nxr)) &
766!$ACC COPY(diss(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
767!$ACC COPY(e(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
768!$ACC COPY(u(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
769!$ACC COPY(v(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
770!$ACC COPY(w(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
771!$ACC COPY(kh(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
772!$ACC COPY(km(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
773!$ACC COPY(p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
774!$ACC COPY(pt(nzb:nzt+1,nysg:nyng,nxlg:nxrg))
775
776!$ACC DATA &
777!$ACC COPY(diss_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
778!$ACC COPY(e_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
779!$ACC COPY(u_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
780!$ACC COPY(v_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
781!$ACC COPY(w_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
782!$ACC COPY(pt_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
783!$ACC COPY(tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
784!$ACC COPY(tdiss_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
785!$ACC COPY(te_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
786!$ACC COPY(tu_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
787!$ACC COPY(tv_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
788!$ACC COPY(tw_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
789!$ACC COPY(tpt_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg))
790
791!$ACC DATA &
792!$ACC COPYIN(rho_air(nzb:nzt+1), drho_air(nzb:nzt+1)) &
793!$ACC COPYIN(rho_air_zw(nzb:nzt+1), drho_air_zw(nzb:nzt+1)) &
794!$ACC COPYIN(zu(nzb:nzt+1)) &
795!$ACC COPYIN(dzu(1:nzt+1), dzw(1:nzt+1)) &
796!$ACC COPYIN(ddzu(1:nzt+1), dd2zu(1:nzt)) &
797!$ACC COPYIN(ddzw(1:nzt+1)) &
798!$ACC COPYIN(heatflux_output_conversion(nzb:nzt+1)) &
799!$ACC COPYIN(momentumflux_output_conversion(nzb:nzt+1)) &
800!$ACC COPYIN(rdf(nzb+1:nzt), rdf_sc(nzb+1:nzt)) &
801!$ACC COPYIN(ptdf_x(nxlg:nxrg), ptdf_y(nysg:nyng)) &
802!$ACC COPYIN(ref_state(0:nz+1)) &
803!$ACC COPYIN(u_init(0:nz+1), v_init(0:nz+1)) &
804!$ACC COPYIN(u_stokes_zu(nzb:nzt+1), v_stokes_zu(nzb:nzt+1)) &
805!$ACC COPYIN(pt_init(0:nz+1)) &
806!$ACC COPYIN(ug(0:nz+1), vg(0:nz+1))
807
808!
809!-- Copy data from control_parameters
810!$ACC DATA &
811!$ACC COPYIN(tsc(1:5))
812
813!
814!-- Copy data from indices
815!$ACC DATA &
816!$ACC COPYIN(advc_flags_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
817!$ACC COPYIN(advc_flags_s(nzb:nzt+1,nysg:nyng,nxlg:nxrg)) &
818!$ACC COPYIN(wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg))
819
820!
821!-- Copy data from surface_mod
822!$ACC DATA &
823!$ACC COPYIN(bc_h(0:1)) &
824!$ACC COPYIN(bc_h(0)%i(1:bc_h(0)%ns)) &
825!$ACC COPYIN(bc_h(0)%j(1:bc_h(0)%ns)) &
826!$ACC COPYIN(bc_h(0)%k(1:bc_h(0)%ns)) &
827!$ACC COPYIN(bc_h(1)%i(1:bc_h(1)%ns)) &
828!$ACC COPYIN(bc_h(1)%j(1:bc_h(1)%ns)) &
829!$ACC COPYIN(bc_h(1)%k(1:bc_h(1)%ns))
830
831!
832!-- Copy data from statistics
833!$ACC DATA &
834!$ACC COPYIN(hom(0:nz+1,1:2,1:4,0)) &
835!$ACC COPYIN(rmask(nysg:nyng,nxlg:nxrg,0:statistic_regions)) &
836!$ACC COPYIN(weight_substep(1:intermediate_timestep_count_max)) &
837!$ACC COPY(sums_l(nzb:nzt+1,1:pr_palm,0)) &
838!$ACC COPY(sums_l_l(nzb:nzt+1,0:statistic_regions,0)) &
839!$ACC COPY(sums_us2_ws_l(nzb:nzt+1,0)) &
840!$ACC COPY(sums_wsus_ws_l(nzb:nzt+1,0)) &
841!$ACC COPY(sums_vs2_ws_l(nzb:nzt+1,0)) &
842!$ACC COPY(sums_wsvs_ws_l(nzb:nzt+1,0)) &
843!$ACC COPY(sums_ws2_ws_l(nzb:nzt+1,0)) &
844!$ACC COPY(sums_wspts_ws_l(nzb:nzt+1,0)) &
845!$ACC COPY(sums_wssas_ws_l(nzb:nzt+1,0)) &
846!$ACC COPY(sums_wsqs_ws_l(nzb:nzt+1,0)) &
847!$ACC COPY(sums_wsqcs_ws_l(nzb:nzt+1,0)) &
848!$ACC COPY(sums_wsqrs_ws_l(nzb:nzt+1,0)) &
849!$ACC COPY(sums_wsncs_ws_l(nzb:nzt+1,0)) &
850!$ACC COPY(sums_wsnrs_ws_l(nzb:nzt+1,0)) &
851!$ACC COPY(sums_wsss_ws_l(nzb:nzt+1,0)) &
852!$ACC COPY(sums_salsa_ws_l(nzb:nzt+1,0))
853
854#if defined( _OPENACC )
855    CALL enter_surface_arrays
856#endif
857
858!
859!-- At beginning determine the first time step
860    CALL timestep
861!
862!-- Synchronize the timestep in case of nested run.
863    IF ( nested_run )  THEN
864!
865!--    Synchronization by unifying the time step.
866!--    Global minimum of all time-steps is used for all.
867       CALL pmci_synchronize
868    ENDIF
869
870!
871!-- Determine and print out the run control quantities before the first time
872!-- step of this run. For the initial run, some statistics (e.g. divergence)
873!-- need to be determined first --> CALL flow_statistics at the beginning of
874!-- run_control
875    CALL run_control
876!
877!-- Data exchange between coupled models in case that a call has been omitted
878!-- at the end of the previous run of a job chain.
879    IF ( coupling_mode /= 'uncoupled'  .AND.  run_coupled  .AND. .NOT. vnested )  THEN
880!
881!--    In case of model termination initiated by the local model the coupler
882!--    must not be called because this would again cause an MPI hang.
883       DO WHILE ( time_coupling >= dt_coupling  .AND.  terminate_coupled == 0 )
884          CALL surface_coupler
885          time_coupling = time_coupling - dt_coupling
886       ENDDO
887       IF (time_coupling == 0.0_wp  .AND.  time_since_reference_point < dt_coupling )  THEN
888          time_coupling = time_since_reference_point
889       ENDIF
890    ENDIF
891
892    CALL location_message( 'atmosphere (and/or ocean) time-stepping', 'start' )
893
894!
895!-- Start of the time loop
896    DO  WHILE ( simulated_time < end_time  .AND.  .NOT. stop_dt  .AND. .NOT. terminate_run )
897
898       CALL cpu_log( log_point_s(10), 'timesteps', 'start' )
899!
900!--    Vertical nesting: initialize fine grid
901       IF ( vnested ) THEN
902          IF ( .NOT. vnest_init  .AND.  simulated_time >= vnest_start_time )  THEN
903             CALL cpu_log( log_point_s(22), 'vnest_init', 'start' )
904             CALL vnest_init_fine
905             vnest_init = .TRUE.
906             CALL cpu_log( log_point_s(22), 'vnest_init', 'stop' )
907          ENDIF
908       ENDIF
909!
910!--    Determine ug, vg and w_subs in dependence on data from external file
911!--    LSF_DATA
912       IF ( large_scale_forcing .AND. lsf_vert )  THEN
913           CALL ls_forcing_vert ( simulated_time )
914           sums_ls_l = 0.0_wp
915       ENDIF
916
917!
918!--    Set pt_init and q_init to the current profiles taken from
919!--    NUDGING_DATA
920       IF ( nudging )  THEN
921           CALL nudge_ref ( simulated_time )
922!
923!--        Store temperature gradient at the top boundary for possible Neumann
924!--        boundary condition
925           bc_pt_t_val = ( pt_init(nzt+1) - pt_init(nzt) ) / dzu(nzt+1)
926           bc_q_t_val  = ( q_init(nzt+1) - q_init(nzt) ) / dzu(nzt+1)
927           IF ( air_chemistry )  THEN
928              DO  lsp = 1, nvar
929                 bc_cs_t_val = (  chem_species(lsp)%conc_pr_init(nzt+1)                            &
930                                - chem_species(lsp)%conc_pr_init(nzt) )                            &
931                               / dzu(nzt+1)
932              ENDDO
933           ENDIF
934           IF ( salsa  .AND.  time_since_reference_point >= skip_time_do_salsa )  THEN
935              DO  ib = 1, nbins_aerosol
936                 bc_an_t_val = ( aerosol_number(ib)%init(nzt+1) - aerosol_number(ib)%init(nzt) ) / &
937                               dzu(nzt+1)
938                 DO  ic = 1, ncomponents_mass
939                    icc = ( ic - 1 ) * nbins_aerosol + ib
940                    bc_am_t_val = ( aerosol_mass(icc)%init(nzt+1) - aerosol_mass(icc)%init(nzt) ) /&
941                                  dzu(nzt+1)
942                 ENDDO
943              ENDDO
944              IF ( .NOT. salsa_gases_from_chem )  THEN
945                 DO  ig = 1, ngases_salsa
946                    bc_gt_t_val = ( salsa_gas(ig)%init(nzt+1) - salsa_gas(ig)%init(nzt) ) /        &
947                                  dzu(nzt+1)
948                 ENDDO
949              ENDIF
950           ENDIF
951       ENDIF
952!
953!--    If forcing by larger-scale models is applied, check if new data
954!--    at domain boundaries need to be read.
955       IF ( nesting_offline ) THEN
956          IF ( nest_offl%time(nest_offl%tind_p) <= time_since_reference_point ) &
957               CALL netcdf_data_input_offline_nesting
958       ENDIF
959
960!
961!--    Execute all other module actions routunes
962       CALL module_interface_actions( 'before_timestep' )
963       
964!
965!--    Start of intermediate step loop
966       intermediate_timestep_count = 0
967       DO  WHILE ( intermediate_timestep_count < intermediate_timestep_count_max )
968
969          intermediate_timestep_count = intermediate_timestep_count + 1
970
971!
972!--       Set the steering factors for the prognostic equations which depend
973!--       on the timestep scheme
974          CALL timestep_scheme_steering
975
976!
977!--       Calculate those variables needed in the tendency terms which need
978!--       global communication
979          IF ( .NOT. use_single_reference_value  .AND.  .NOT. use_initial_profile_as_reference )   &
980          THEN
981!
982!--          Horizontally averaged profiles to be used as reference state in
983!--          buoyancy terms (WARNING: only the respective last call of
984!--          calc_mean_profile defines the reference state!)
985             IF ( .NOT. neutral )  THEN
986                CALL calc_mean_profile( pt, 4 )
987                ref_state(:)  = hom(:,1,4,0) ! this is used in the buoyancy term
988             ENDIF
989             IF ( ocean_mode )  THEN
990                CALL calc_mean_profile( rho_ocean, 64 )
991                ref_state(:)  = hom(:,1,64,0)
992             ENDIF
993             IF ( humidity )  THEN
994                CALL calc_mean_profile( vpt, 44 )
995                ref_state(:)  = hom(:,1,44,0)
996             ENDIF
997!
998!--          Assure that ref_state does not become zero at any level
999!--          ( might be the case if a vertical level is completely occupied
1000!--            with topography ).
1001             ref_state = MERGE( MAXVAL(ref_state), ref_state, ref_state == 0.0_wp )
1002          ENDIF
1003
1004          IF ( ( ws_scheme_mom .OR. ws_scheme_sca )  .AND.  intermediate_timestep_count == 1 )     &
1005          THEN
1006             CALL ws_statistics
1007          ENDIF
1008!
1009!--       In case of nudging calculate current nudging time scale and horizontal
1010!--       means of u, v, pt and q
1011          IF ( nudging )  THEN
1012             CALL calc_tnudge( simulated_time )
1013             CALL calc_mean_profile( u, 1 )
1014             CALL calc_mean_profile( v, 2 )
1015             CALL calc_mean_profile( pt, 4 )
1016             CALL calc_mean_profile( q, 41 )
1017          ENDIF
1018!
1019!--       Execute all other module actions routunes
1020          CALL module_interface_actions( 'before_prognostic_equations' )
1021!
1022!--       Solve the prognostic equations. A fast cache optimized version with
1023!--       only one single loop is used in case of Piascek-Williams advection
1024!--       scheme. NEC vector machines use a different version, because
1025!--       in the other versions a good vectorization is prohibited due to
1026!--       inlining problems.
1027          IF ( loop_optimization == 'cache' )  THEN
1028             CALL prognostic_equations_cache
1029          ELSEIF ( loop_optimization == 'vector' )  THEN
1030             CALL prognostic_equations_vector
1031          ENDIF
1032
1033!
1034!--       Particle transport/physics with the Lagrangian particle model
1035!--       (only once during intermediate steps, because it uses an Euler-step)
1036!--       ### particle model should be moved before prognostic_equations, in order
1037!--       to regard droplet interactions directly
1038          IF ( particle_advection  .AND.  time_since_reference_point >= particle_advection_start   &
1039               .AND.  intermediate_timestep_count == 1 )                                           &
1040          THEN
1041             CALL module_interface_actions( 'after_prognostic_equations' )
1042             first_call_lpm = .FALSE.
1043          ENDIF
1044
1045!
1046!--       Interaction of droplets with temperature and mixing ratio.
1047!--       Droplet condensation and evaporation is calculated within
1048!--       advec_particles.
1049          IF ( cloud_droplets  .AND.  intermediate_timestep_count == intermediate_timestep_count_max ) &
1050          THEN
1051             CALL lpm_interaction_droplets_ptq
1052          ENDIF
1053
1054!
1055!--       Movement of agents in multi agent system
1056          IF ( agents_active  .AND.  time_since_reference_point >= multi_agent_system_start  .AND. &
1057               time_since_reference_point <= multi_agent_system_end  .AND.                         &
1058               intermediate_timestep_count == 1 )                                                  &
1059          THEN
1060             CALL multi_agent_system
1061             first_call_mas = .FALSE.
1062          ENDIF
1063
1064!
1065!--       Exchange of ghost points (lateral boundary conditions)
1066          CALL cpu_log( log_point(26), 'exchange-horiz-progn', 'start' )
1067
1068          CALL exchange_horiz( u_p, nbgp )
1069          CALL exchange_horiz( v_p, nbgp )
1070          CALL exchange_horiz( w_p, nbgp )
1071          CALL exchange_horiz( pt_p, nbgp )
1072          IF ( .NOT. constant_diffusion )  CALL exchange_horiz( e_p, nbgp )
1073          IF ( rans_tke_e  .OR.  wang_kernel  .OR.  collision_turbulence                           &
1074               .OR.  use_sgs_for_particles )  THEN
1075             IF ( rans_tke_e )  THEN
1076                CALL exchange_horiz( diss_p, nbgp )
1077             ELSE
1078                CALL exchange_horiz( diss, nbgp )
1079             ENDIF
1080          ENDIF
1081          IF ( ocean_mode )  THEN
1082             CALL exchange_horiz( sa_p, nbgp )
1083             CALL exchange_horiz( rho_ocean, nbgp )
1084             CALL exchange_horiz( prho, nbgp )
1085          ENDIF
1086          IF ( humidity )  THEN
1087             CALL exchange_horiz( q_p, nbgp )
1088             IF ( bulk_cloud_model .AND. microphysics_morrison )  THEN
1089                CALL exchange_horiz( qc_p, nbgp )
1090                CALL exchange_horiz( nc_p, nbgp )
1091             ENDIF
1092             IF ( bulk_cloud_model .AND. microphysics_seifert )  THEN
1093                CALL exchange_horiz( qr_p, nbgp )
1094                CALL exchange_horiz( nr_p, nbgp )
1095             ENDIF
1096          ENDIF
1097          IF ( cloud_droplets )  THEN
1098             CALL exchange_horiz( ql, nbgp )
1099             CALL exchange_horiz( ql_c, nbgp )
1100             CALL exchange_horiz( ql_v, nbgp )
1101             CALL exchange_horiz( ql_vp, nbgp )
1102          ENDIF
1103          IF ( passive_scalar )  CALL exchange_horiz( s_p, nbgp )
1104          IF ( air_chemistry )  THEN
1105             DO  lsp = 1, nvar
1106                CALL exchange_horiz( chem_species(lsp)%conc_p, nbgp )
1107             ENDDO
1108          ENDIF
1109
1110          IF ( salsa  .AND.  time_since_reference_point >= skip_time_do_salsa )  THEN
1111             DO  ib = 1, nbins_aerosol
1112                CALL exchange_horiz( aerosol_number(ib)%conc_p, nbgp )
1113                DO  ic = 1, ncomponents_mass
1114                   icc = ( ic - 1 ) * nbins_aerosol + ib
1115                   CALL exchange_horiz( aerosol_mass(icc)%conc_p, nbgp )
1116                ENDDO
1117             ENDDO
1118             IF ( .NOT. salsa_gases_from_chem )  THEN
1119                DO  ig = 1, ngases_salsa
1120                   CALL exchange_horiz( salsa_gas(ig)%conc_p, nbgp )
1121                ENDDO
1122             ENDIF
1123          ENDIF
1124          CALL cpu_log( log_point(26), 'exchange-horiz-progn', 'stop' )
1125
1126!
1127!--       Boundary conditions for the prognostic quantities (except of the
1128!--       velocities at the outflow in case of a non-cyclic lateral wall)
1129          CALL boundary_conds
1130
1131!
1132!--       Boundary conditions for prognostic quantitites of other modules:
1133!--       Here, only decycling is carried out
1134          IF ( air_chemistry )  THEN
1135
1136             DO  lsp = 1, nvar
1137                lsp_usr = 1
1138                DO WHILE ( TRIM( cs_name( lsp_usr ) ) /= 'novalue' )
1139                   IF ( TRIM( chem_species(lsp)%name ) == TRIM( cs_name(lsp_usr) ) )  THEN
1140                      CALL chem_boundary_conds( chem_species(lsp)%conc_p,                          &
1141                                                chem_species(lsp)%conc_pr_init )
1142                   ENDIF
1143                   lsp_usr = lsp_usr + 1
1144                ENDDO
1145             ENDDO
1146
1147          ENDIF
1148
1149          IF ( salsa  .AND.  time_since_reference_point >= skip_time_do_salsa )  THEN
1150
1151             DO  ib = 1, nbins_aerosol
1152                CALL salsa_boundary_conds( aerosol_number(ib)%conc_p, aerosol_number(ib)%init )
1153                DO  ic = 1, ncomponents_mass
1154                   icc = ( ic - 1 ) * nbins_aerosol + ib
1155                   CALL salsa_boundary_conds( aerosol_mass(icc)%conc_p, aerosol_mass(icc)%init )
1156                ENDDO
1157             ENDDO
1158             IF ( .NOT. salsa_gases_from_chem )  THEN
1159                DO  ig = 1, ngases_salsa
1160                   CALL salsa_boundary_conds( salsa_gas(ig)%conc_p, salsa_gas(ig)%init )
1161                ENDDO
1162             ENDIF
1163
1164          ENDIF
1165
1166!
1167!--       Incrementing timestep counter
1168          timestep_count = timestep_count + 1
1169
1170          CALL cpu_log( log_point(28), 'swap_timelevel', 'start' )
1171!
1172!--       Set the swap level for all modules
1173          CALL module_interface_swap_timelevel( MOD( timestep_count, 2) )
1174!
1175!--       Set the swap level for steering the pmc data transfer
1176          IF ( nested_run )  CALL pmci_set_swaplevel( MOD( timestep_count, 2) + 1 )  !> @todo: why the +1 ?
1177
1178          CALL cpu_log( log_point(28), 'swap_timelevel', 'stop' )
1179
1180!
1181!--       Vertical nesting: Interpolate fine grid data to the coarse grid
1182          IF ( vnest_init ) THEN
1183             CALL cpu_log( log_point_s(37), 'vnest_anterpolate', 'start' )
1184             CALL vnest_anterpolate
1185             CALL cpu_log( log_point_s(37), 'vnest_anterpolate', 'stop' )
1186          ENDIF
1187
1188          IF ( nested_run )  THEN
1189
1190             CALL cpu_log( log_point(60), 'nesting', 'start' )
1191!
1192!--          Domain nesting. The data transfer subroutines pmci_parent_datatrans
1193!--          and pmci_child_datatrans are called inside the wrapper
1194!--          subroutine pmci_datatrans according to the control parameters
1195!--          nesting_mode and nesting_datatransfer_mode.
1196!--          TO_DO: why is nesting_mode given as a parameter here?
1197             CALL pmci_datatrans( nesting_mode )
1198
1199             IF ( TRIM( nesting_mode ) == 'two-way' .OR.  nesting_mode == 'vertical' )  THEN
1200
1201                CALL cpu_log( log_point_s(92), 'exchange-horiz-nest', 'start' )
1202!
1203!--             Exchange_horiz is needed for all parent-domains after the
1204!--             anterpolation
1205                CALL exchange_horiz( u, nbgp )
1206                CALL exchange_horiz( v, nbgp )
1207                CALL exchange_horiz( w, nbgp )
1208                IF ( .NOT. neutral )  CALL exchange_horiz( pt, nbgp )
1209
1210                IF ( humidity )  THEN
1211
1212                   CALL exchange_horiz( q, nbgp )
1213
1214                   IF ( bulk_cloud_model  .AND.  microphysics_morrison )  THEN
1215                       CALL exchange_horiz( qc, nbgp )
1216                       CALL exchange_horiz( nc, nbgp )
1217                   ENDIF
1218                   IF ( bulk_cloud_model  .AND.  microphysics_seifert )  THEN
1219                       CALL exchange_horiz( qr, nbgp )
1220                       CALL exchange_horiz( nr, nbgp )
1221                   ENDIF
1222
1223                ENDIF
1224
1225                IF ( passive_scalar )  CALL exchange_horiz( s, nbgp ) 
1226
1227                IF ( .NOT. constant_diffusion )  CALL exchange_horiz( e, nbgp )
1228
1229                IF ( .NOT. constant_diffusion  .AND.  rans_mode  .AND.  rans_tke_e )  THEN
1230                   CALL exchange_horiz( diss, nbgp )
1231                ENDIF
1232
1233                IF ( air_chemistry )  THEN
1234                   DO  n = 1, nvar
1235                      CALL exchange_horiz( chem_species(n)%conc, nbgp ) 
1236                   ENDDO
1237                ENDIF
1238
1239                IF ( salsa  .AND. time_since_reference_point >= skip_time_do_salsa )  THEN
1240                   DO  ib = 1, nbins_aerosol
1241                      CALL exchange_horiz( aerosol_number(ib)%conc, nbgp )
1242                      DO  ic = 1, ncomponents_mass
1243                         icc = ( ic - 1 ) * nbins_aerosol + ib
1244                         CALL exchange_horiz( aerosol_mass(icc)%conc, nbgp )
1245                      ENDDO
1246                   ENDDO
1247                   IF ( .NOT. salsa_gases_from_chem )  THEN
1248                      DO  ig = 1, ngases_salsa
1249                         CALL exchange_horiz( salsa_gas(ig)%conc, nbgp )
1250                      ENDDO
1251                   ENDIF
1252                ENDIF
1253                CALL cpu_log( log_point_s(92), 'exchange-horiz-nest', 'stop' )
1254
1255             ENDIF
1256
1257!
1258!--          Set boundary conditions again after interpolation and anterpolation.
1259             CALL pmci_boundary_conds
1260
1261!
1262!--          Set chemistry boundary conditions (decycling)
1263             IF ( air_chemistry )  THEN
1264                DO  lsp = 1, nvar
1265                   lsp_usr = 1
1266                   DO WHILE ( TRIM( cs_name( lsp_usr ) ) /= 'novalue' )
1267                      IF ( TRIM( chem_species(lsp)%name ) == TRIM( cs_name(lsp_usr) ) )  THEN
1268                         CALL chem_boundary_conds( chem_species(lsp)%conc,                         &
1269                                                   chem_species(lsp)%conc_pr_init )
1270                      ENDIF
1271                      lsp_usr = lsp_usr + 1
1272                   ENDDO
1273                ENDDO
1274             ENDIF
1275
1276!
1277!--          Set SALSA boundary conditions (decycling)
1278             IF ( salsa  .AND. time_since_reference_point >= skip_time_do_salsa )  THEN
1279                DO  ib = 1, nbins_aerosol
1280                   CALL salsa_boundary_conds( aerosol_number(ib)%conc, aerosol_number(ib)%init )
1281                   DO  ic = 1, ncomponents_mass
1282                      icc = ( ic - 1 ) * nbins_aerosol + ib
1283                      CALL salsa_boundary_conds( aerosol_mass(icc)%conc, aerosol_mass(icc)%init )
1284                   ENDDO
1285                ENDDO
1286                IF ( .NOT. salsa_gases_from_chem )  THEN
1287                   DO  ig = 1, ngases_salsa
1288                      CALL salsa_boundary_conds( salsa_gas(ig)%conc, salsa_gas(ig)%init )
1289                   ENDDO
1290                ENDIF
1291             ENDIF
1292
1293             CALL cpu_log( log_point(60), 'nesting', 'stop' )
1294
1295          ENDIF
1296
1297!
1298!--       Temperature offset must be imposed at cyclic boundaries in x-direction
1299!--       when a sloping surface is used
1300          IF ( sloping_surface )  THEN
1301             IF ( nxl ==  0 )  pt(:,:,nxlg:nxl-1) = pt(:,:,nxlg:nxl-1) - pt_slope_offset
1302             IF ( nxr == nx )  pt(:,:,nxr+1:nxrg) = pt(:,:,nxr+1:nxrg) + pt_slope_offset
1303          ENDIF
1304
1305!
1306!--       Impose a turbulent inflow using the recycling method
1307          IF ( turbulent_inflow )  CALL inflow_turbulence
1308
1309!
1310!--       Set values at outflow boundary using the special outflow condition
1311          IF ( turbulent_outflow )  CALL outflow_turbulence
1312
1313!
1314!--       Impose a random perturbation on the horizontal velocity field
1315          IF ( create_disturbances  .AND.  ( call_psolver_at_all_substeps  .AND.                   &
1316               intermediate_timestep_count == intermediate_timestep_count_max )                    &
1317               .OR. ( .NOT. call_psolver_at_all_substeps  .AND.  intermediate_timestep_count == 1 ) ) &
1318          THEN
1319             time_disturb = time_disturb + dt_3d
1320             IF ( time_disturb >= dt_disturb )  THEN
1321                IF ( disturbance_energy_limit /= 0.0_wp  .AND.                                     &
1322                     hom(nzb+5,1,pr_palm,0) < disturbance_energy_limit )  THEN
1323                   CALL disturb_field( 'u', tend, u )
1324                   CALL disturb_field( 'v', tend, v )
1325                ELSEIF ( ( .NOT. bc_lr_cyc  .OR.  .NOT. bc_ns_cyc )                                &
1326                         .AND. .NOT. child_domain  .AND.  .NOT.  nesting_offline )                 &
1327                THEN
1328!
1329!--                Runs with a non-cyclic lateral wall need perturbations
1330!--                near the inflow throughout the whole simulation
1331                   dist_range = 1
1332                   CALL disturb_field( 'u', tend, u )
1333                   CALL disturb_field( 'v', tend, v )
1334                   dist_range = 0
1335                ENDIF
1336                time_disturb = time_disturb - dt_disturb
1337             ENDIF
1338          ENDIF
1339
1340!
1341!--       Map forcing data derived from larger scale model onto domain
1342!--       boundaries.
1343          IF ( nesting_offline  .AND.  intermediate_timestep_count ==                              &
1344                                       intermediate_timestep_count_max  )                          &
1345             CALL nesting_offl_bc
1346!
1347!--       Impose a turbulent inflow using synthetic generated turbulence.
1348          IF ( use_syn_turb_gen  .AND.                                                             &
1349               intermediate_timestep_count == intermediate_timestep_count_max )  THEN
1350             CALL cpu_log( log_point(57), 'synthetic_turbulence_gen', 'start' )
1351             CALL stg_main
1352             CALL cpu_log( log_point(57), 'synthetic_turbulence_gen', 'stop' )
1353          ENDIF
1354!
1355!--       Ensure mass conservation. This need to be done after imposing
1356!--       synthetic turbulence and top boundary condition for pressure is set to
1357!--       Neumann conditions.
1358!--       Is this also required in case of Dirichlet?
1359          IF ( nesting_offline )  CALL nesting_offl_mass_conservation
1360!
1361!--       Reduce the velocity divergence via the equation for perturbation
1362!--       pressure.
1363          IF ( intermediate_timestep_count == 1  .OR. &
1364                call_psolver_at_all_substeps )  THEN
1365
1366             IF (  vnest_init ) THEN
1367!
1368!--             Compute pressure in the CG, interpolate top boundary conditions
1369!--             to the FG and then compute pressure in the FG
1370                IF ( coupling_mode == 'vnested_crse' )  CALL pres
1371
1372                CALL cpu_log( log_point_s(30), 'vnest_bc', 'start' )
1373                CALL vnest_boundary_conds
1374                CALL cpu_log( log_point_s(30), 'vnest_bc', 'stop' )
1375 
1376                IF ( coupling_mode == 'vnested_fine' )  CALL pres
1377
1378!--             Anterpolate TKE, satisfy Germano Identity
1379                CALL cpu_log( log_point_s(28), 'vnest_anter_e', 'start' )
1380                CALL vnest_anterpolate_e
1381                CALL cpu_log( log_point_s(28), 'vnest_anter_e', 'stop' )
1382
1383             ELSE
1384!               
1385!--             Mass (volume) flux correction to ensure global mass conservation for child domains.
1386                IF ( child_domain )  THEN
1387                   IF ( nesting_mode == 'vertical' )  THEN
1388                      CALL pmci_ensure_nest_mass_conservation_vertical
1389                   ELSE
1390                      CALL pmci_ensure_nest_mass_conservation
1391                   ENDIF
1392                ENDIF
1393               
1394                CALL pres
1395
1396             ENDIF
1397
1398          ENDIF
1399
1400!
1401!--       If required, compute liquid water content
1402          IF ( bulk_cloud_model )  THEN
1403             CALL calc_liquid_water_content
1404          ENDIF
1405!
1406!--       If required, compute virtual potential temperature
1407          IF ( humidity )  THEN
1408             CALL compute_vpt 
1409          ENDIF 
1410
1411!
1412!--       Compute the diffusion quantities
1413          IF ( .NOT. constant_diffusion )  THEN
1414
1415!
1416!--          Determine surface fluxes shf and qsws and surface values
1417!--          pt_surface and q_surface in dependence on data from external
1418!--          file LSF_DATA respectively
1419             IF ( ( large_scale_forcing .AND. lsf_surf ) .AND.                                     &
1420                 intermediate_timestep_count == intermediate_timestep_count_max )                  &
1421             THEN
1422                CALL ls_forcing_surf( simulated_time )
1423             ENDIF
1424
1425!
1426!--          First the vertical (and horizontal) fluxes in the surface
1427!--          (constant flux) layer are computed
1428             IF ( constant_flux_layer )  THEN
1429                CALL cpu_log( log_point(19), 'surface_layer_fluxes', 'start' )
1430                CALL surface_layer_fluxes
1431                CALL cpu_log( log_point(19), 'surface_layer_fluxes', 'stop' )
1432             ENDIF
1433!
1434!--          If required, solve the energy balance for the surface and run soil
1435!--          model. Call for horizontal as well as vertical surfaces
1436             IF ( land_surface .AND. time_since_reference_point >= skip_time_do_lsm)  THEN
1437
1438                CALL cpu_log( log_point(54), 'land_surface', 'start' )
1439!
1440!--             Call for horizontal upward-facing surfaces
1441                CALL lsm_energy_balance( .TRUE., -1 )
1442                CALL lsm_soil_model( .TRUE., -1, .TRUE. )
1443!
1444!--             Call for northward-facing surfaces
1445                CALL lsm_energy_balance( .FALSE., 0 )
1446                CALL lsm_soil_model( .FALSE., 0, .TRUE. )
1447!
1448!--             Call for southward-facing surfaces
1449                CALL lsm_energy_balance( .FALSE., 1 )
1450                CALL lsm_soil_model( .FALSE., 1, .TRUE. )
1451!
1452!--             Call for eastward-facing surfaces
1453                CALL lsm_energy_balance( .FALSE., 2 )
1454                CALL lsm_soil_model( .FALSE., 2, .TRUE. )
1455!
1456!--             Call for westward-facing surfaces
1457                CALL lsm_energy_balance( .FALSE., 3 )
1458                CALL lsm_soil_model( .FALSE., 3, .TRUE. )
1459               
1460!
1461!--             At the end, set boundary conditons for potential temperature
1462!--             and humidity after running the land-surface model. This
1463!--             might be important for the nesting, where arrays are transfered.
1464                CALL lsm_boundary_condition
1465
1466               
1467                CALL cpu_log( log_point(54), 'land_surface', 'stop' )
1468             ENDIF
1469!
1470!--          If required, solve the energy balance for urban surfaces and run
1471!--          the material heat model
1472             IF (urban_surface) THEN
1473                CALL cpu_log( log_point(74), 'urban_surface', 'start' )
1474               
1475                CALL usm_surface_energy_balance( .FALSE. )
1476                IF ( usm_material_model )  THEN
1477                   CALL usm_green_heat_model
1478                   CALL usm_material_heat_model ( .FALSE. )
1479                ENDIF
1480
1481!
1482!--             At the end, set boundary conditons for potential temperature
1483!--             and humidity after running the urban-surface model. This
1484!--             might be important for the nesting, where arrays are transfered.
1485                CALL usm_boundary_condition
1486
1487                CALL cpu_log( log_point(74), 'urban_surface', 'stop' )
1488             ENDIF
1489!
1490!--          Compute the diffusion coefficients
1491             CALL cpu_log( log_point(17), 'diffusivities', 'start' )
1492             IF ( .NOT. humidity ) THEN
1493                IF ( ocean_mode )  THEN
1494                   CALL tcm_diffusivities( prho, prho_reference )
1495                ELSE
1496                   CALL tcm_diffusivities( pt, pt_reference )
1497                ENDIF
1498             ELSE
1499                CALL tcm_diffusivities( vpt, pt_reference )
1500             ENDIF
1501             CALL cpu_log( log_point(17), 'diffusivities', 'stop' )
1502!
1503!--          Vertical nesting: set fine grid eddy viscosity top boundary condition
1504             IF ( vnest_init )  CALL vnest_boundary_conds_khkm
1505
1506          ENDIF
1507
1508       ENDDO   ! Intermediate step loop
1509
1510!
1511!--    Will be used at some point by flow_statistics.
1512       !$ACC UPDATE &
1513       !$ACC HOST(sums_l_l(nzb:nzt+1,0:statistic_regions,0)) &
1514       !$ACC HOST(sums_us2_ws_l(nzb:nzt+1,0)) &
1515       !$ACC HOST(sums_wsus_ws_l(nzb:nzt+1,0)) &
1516       !$ACC HOST(sums_vs2_ws_l(nzb:nzt+1,0)) &
1517       !$ACC HOST(sums_wsvs_ws_l(nzb:nzt+1,0)) &
1518       !$ACC HOST(sums_ws2_ws_l(nzb:nzt+1,0)) &
1519       !$ACC HOST(sums_wspts_ws_l(nzb:nzt+1,0)) &
1520       !$ACC HOST(sums_wssas_ws_l(nzb:nzt+1,0)) &
1521       !$ACC HOST(sums_wsqs_ws_l(nzb:nzt+1,0)) &
1522       !$ACC HOST(sums_wsqcs_ws_l(nzb:nzt+1,0)) &
1523       !$ACC HOST(sums_wsqrs_ws_l(nzb:nzt+1,0)) &
1524       !$ACC HOST(sums_wsncs_ws_l(nzb:nzt+1,0)) &
1525       !$ACC HOST(sums_wsnrs_ws_l(nzb:nzt+1,0)) &
1526       !$ACC HOST(sums_wsss_ws_l(nzb:nzt+1,0)) &
1527       !$ACC HOST(sums_salsa_ws_l(nzb:nzt+1,0))
1528
1529!
1530!--    If required, calculate radiative fluxes and heating rates
1531       IF ( radiation  .AND.  time_since_reference_point > skip_time_do_radiation )  THEN
1532
1533          time_radiation = time_radiation + dt_3d
1534
1535          IF ( time_radiation >= dt_radiation  .OR.  force_radiation_call )  THEN
1536
1537             CALL cpu_log( log_point(50), 'radiation', 'start' )
1538
1539             IF ( .NOT. force_radiation_call )  THEN
1540                time_radiation = time_radiation - dt_radiation
1541             ENDIF
1542
1543!
1544!--          Adjust the current time to the time step of the radiation model.
1545!--          Needed since radiation is pre-calculated and stored only on apparent
1546!--          solar positions
1547             time_since_reference_point_save = time_since_reference_point
1548             time_since_reference_point = REAL( FLOOR( time_since_reference_point /             &
1549                                                       dt_radiation ), wp ) * dt_radiation
1550
1551             CALL radiation_control
1552
1553             IF ( ( urban_surface  .OR.  land_surface )  .AND.  radiation_interactions )  THEN
1554                CALL cpu_log( log_point_s(46), 'radiation_interaction', 'start' )
1555                CALL radiation_interaction
1556                CALL cpu_log( log_point_s(46), 'radiation_interaction', 'stop' )
1557             ENDIF
1558 
1559!
1560!--          Return the current time to its original value
1561             time_since_reference_point = time_since_reference_point_save
1562
1563             CALL cpu_log( log_point(50), 'radiation', 'stop' )
1564
1565          ENDIF
1566       ENDIF
1567
1568!
1569!--    If required, consider chemical emissions
1570       IF ( air_chemistry  .AND.  emissions_anthropogenic )  THEN
1571!
1572!--       Update the time --> kanani: revise location of this CALL
1573          CALL calc_date_and_time
1574!
1575!--       Call emission routine only once an hour
1576          IF ( hour_of_year  >  hour_call_emis )  THEN
1577             CALL chem_emissions_setup( chem_emis_att, chem_emis, n_matched_vars )
1578             hour_call_emis = hour_of_year
1579          ENDIF
1580       ENDIF
1581!
1582!--    If required, consider aerosol emissions for the salsa model
1583       IF ( salsa )  THEN
1584!
1585!--       Call emission routine to update emissions if needed
1586          CALL salsa_emission_update
1587
1588       ENDIF
1589!
1590!--    If required, calculate indoor temperature, waste heat, heat flux
1591!--    through wall, etc.
1592!--    dt_indoor steers the frequency of the indoor model calculations.
1593!--    Note, at first timestep indoor model is called, in order to provide
1594!--    a waste heat flux.
1595       IF ( indoor_model )  THEN
1596
1597          time_indoor = time_indoor + dt_3d
1598
1599          IF ( time_indoor >= dt_indoor  .OR.  current_timestep_number == 0 )  THEN
1600
1601             time_indoor = time_indoor - dt_indoor
1602
1603             CALL cpu_log( log_point(76), 'indoor_model', 'start' )
1604             CALL im_main_heatcool
1605             CALL cpu_log( log_point(76), 'indoor_model', 'stop' )
1606
1607          ENDIF
1608       ENDIF
1609!
1610!--    Increase simulation time and output times
1611       nr_timesteps_this_run      = nr_timesteps_this_run + 1
1612       current_timestep_number    = current_timestep_number + 1
1613       simulated_time             = simulated_time   + dt_3d
1614       time_since_reference_point = simulated_time - coupling_start_time
1615       simulated_time_chr         = time_to_string( time_since_reference_point )
1616
1617
1618
1619
1620       IF ( time_since_reference_point >= skip_time_data_output_av )  THEN
1621          time_do_av         = time_do_av       + dt_3d
1622       ENDIF
1623       IF ( time_since_reference_point >= skip_time_do2d_xy )  THEN
1624          time_do2d_xy       = time_do2d_xy     + dt_3d
1625       ENDIF
1626       IF ( time_since_reference_point >= skip_time_do2d_xz )  THEN
1627          time_do2d_xz       = time_do2d_xz     + dt_3d
1628       ENDIF
1629       IF ( time_since_reference_point >= skip_time_do2d_yz )  THEN
1630          time_do2d_yz       = time_do2d_yz     + dt_3d
1631       ENDIF
1632       IF ( time_since_reference_point >= skip_time_do3d    )  THEN
1633          time_do3d          = time_do3d        + dt_3d
1634       ENDIF
1635       DO  mid = 1, masks
1636          IF ( time_since_reference_point >= skip_time_domask(mid) )  THEN
1637             time_domask(mid)= time_domask(mid) + dt_3d
1638          ENDIF
1639       ENDDO
1640       IF ( time_since_reference_point >= skip_time_dosp )  THEN
1641          time_dosp       = time_dosp        + dt_3d
1642       ENDIF
1643       time_dots          = time_dots        + dt_3d
1644       IF ( .NOT. first_call_lpm )  THEN
1645          time_dopts      = time_dopts       + dt_3d
1646       ENDIF
1647       IF ( time_since_reference_point >= skip_time_dopr )  THEN
1648          time_dopr       = time_dopr        + dt_3d
1649       ENDIF
1650       time_dopr_listing  = time_dopr_listing + dt_3d
1651       time_run_control   = time_run_control + dt_3d
1652!
1653!--    Increment time-counter for surface output
1654       IF ( surface_output )  THEN
1655          IF ( time_since_reference_point >= skip_time_dosurf )  THEN
1656             time_dosurf    = time_dosurf + dt_3d
1657          ENDIF
1658          IF ( time_since_reference_point >= skip_time_dosurf_av )  THEN
1659             time_dosurf_av = time_dosurf_av + dt_3d
1660          ENDIF
1661       ENDIF
1662!
1663!--    Increment time-counter for virtual measurements
1664       IF ( virtual_measurement  .AND.  vm_time_start <= time_since_reference_point )  THEN
1665          time_virtual_measurement = time_virtual_measurement + dt_3d
1666       ENDIF
1667!
1668!--    In case of synthetic turbulence generation and parametrized turbulence
1669!--    information, update the time counter and if required, adjust the
1670!--    STG to new atmospheric conditions.
1671       IF ( use_syn_turb_gen  )  THEN
1672          IF ( parametrize_inflow_turbulence )  THEN
1673             time_stg_adjust = time_stg_adjust + dt_3d
1674             IF ( time_stg_adjust >= dt_stg_adjust )  THEN
1675                CALL cpu_log( log_point(57), 'synthetic_turbulence_gen', 'start' )
1676                CALL stg_adjust
1677                CALL cpu_log( log_point(57), 'synthetic_turbulence_gen', 'stop' )
1678             ENDIF
1679          ENDIF
1680          time_stg_call = time_stg_call + dt_3d
1681       ENDIF
1682
1683!
1684!--    Data exchange between coupled models
1685       IF ( coupling_mode /= 'uncoupled'  .AND.  run_coupled  .AND.  .NOT. vnested )  THEN
1686          time_coupling = time_coupling + dt_3d
1687
1688!
1689!--       In case of model termination initiated by the local model
1690!--       (terminate_coupled > 0), the coupler must be skipped because it would
1691!--       cause an MPI intercomminucation hang.
1692!--       If necessary, the coupler will be called at the beginning of the
1693!--       next restart run.
1694          DO WHILE ( time_coupling >= dt_coupling  .AND.  terminate_coupled == 0 )
1695             CALL surface_coupler
1696             time_coupling = time_coupling - dt_coupling
1697          ENDDO
1698       ENDIF
1699
1700!
1701!--    Biometeorology calculation of stationary thermal indices
1702!--    Todo (kanani): biometeorology needs own time_... treatment.
1703!--                   It might be that time_do2d_xy differs from time_do3d,
1704!--                   and then we might get trouble with the biomet output,
1705!--                   because we can have 2d and/or 3d biomet output!!
1706       IF ( biometeorology                                                                         &
1707            .AND. ( ( time_do3d >= dt_do3d  .AND.  time_since_reference_point >= skip_time_do3d )  &
1708                  .OR.                                                                             &
1709            ( time_do2d_xy >= dt_do2d_xy  .AND.  time_since_reference_point >= skip_time_do2d_xy ) &
1710                    ) )  THEN
1711!
1712!--       If required, do thermal comfort calculations
1713          IF ( thermal_comfort )  THEN
1714             CALL bio_calculate_thermal_index_maps ( .FALSE. )
1715          ENDIF
1716!
1717!--       If required, do UV exposure calculations
1718          IF ( uv_exposure )  THEN
1719             CALL bio_calculate_uv_exposure
1720          ENDIF
1721       ENDIF
1722
1723!
1724!--    Execute alle other module actions routunes
1725       CALL module_interface_actions( 'after_integration' )
1726
1727!
1728!--    If Galilei transformation is used, determine the distance that the
1729!--    model has moved so far
1730       IF ( galilei_transformation )  THEN
1731          advected_distance_x = advected_distance_x + u_gtrans * dt_3d
1732          advected_distance_y = advected_distance_y + v_gtrans * dt_3d
1733       ENDIF
1734
1735!
1736!--    Check, if restart is necessary (because cpu-time is expiring or
1737!--    because it is forced by user) and set stop flag
1738!--    This call is skipped if the remote model has already initiated a restart.
1739       IF ( .NOT. terminate_run )  CALL check_for_restart
1740
1741!
1742!--    Carry out statistical analysis and output at the requested output times.
1743!--    The MOD function is used for calculating the output time counters (like
1744!--    time_dopr) in order to regard a possible decrease of the output time
1745!--    interval in case of restart runs
1746
1747!
1748!--    Set a flag indicating that so far no statistics have been created
1749!--    for this time step
1750       flow_statistics_called = .FALSE.
1751
1752!
1753!--    If required, call flow_statistics for averaging in time
1754       IF ( averaging_interval_pr /= 0.0_wp  .AND.                                                 &
1755            ( dt_dopr - time_dopr ) <= averaging_interval_pr  .AND.                                &
1756            time_since_reference_point >= skip_time_dopr )  THEN
1757          time_dopr_av = time_dopr_av + dt_3d
1758          IF ( time_dopr_av >= dt_averaging_input_pr )  THEN
1759             do_sum = .TRUE.
1760             time_dopr_av = MOD( time_dopr_av, MAX( dt_averaging_input_pr, dt_3d ) )
1761          ENDIF
1762       ENDIF
1763       IF ( do_sum )  CALL flow_statistics
1764
1765!
1766!--    Sum-up 3d-arrays for later output of time-averaged 2d/3d/masked data
1767       IF ( averaging_interval /= 0.0_wp  .AND.                                                    &
1768            ( dt_data_output_av - time_do_av ) <= averaging_interval  .AND.                        &
1769            time_since_reference_point >= skip_time_data_output_av )                               &
1770       THEN
1771          time_do_sla = time_do_sla + dt_3d
1772          IF ( time_do_sla >= dt_averaging_input )  THEN
1773             IF ( current_timestep_number > timestep_number_at_prev_calc )                         &
1774                CALL doq_calculate
1775
1776             CALL sum_up_3d_data
1777             average_count_3d = average_count_3d + 1
1778             time_do_sla = MOD( time_do_sla, MAX( dt_averaging_input, dt_3d ) )
1779          ENDIF
1780       ENDIF
1781!
1782!--    Average surface data
1783       IF ( surface_output )  THEN
1784          IF ( averaging_interval_surf /= 0.0_wp                                                   &
1785                .AND.  ( dt_dosurf_av - time_dosurf_av ) <= averaging_interval_surf                &
1786                .AND.  time_since_reference_point >= skip_time_dosurf_av )  THEN
1787             IF ( time_dosurf_av >= dt_averaging_input )  THEN       
1788                CALL surface_data_output_averaging
1789                average_count_surf = average_count_surf + 1
1790             ENDIF
1791          ENDIF
1792       ENDIF
1793
1794!
1795!--    Calculate spectra for time averaging
1796       IF ( averaging_interval_sp /= 0.0_wp  .AND. ( dt_dosp - time_dosp ) <= averaging_interval_sp&
1797            .AND.  time_since_reference_point >= skip_time_dosp )  THEN
1798          time_dosp_av = time_dosp_av + dt_3d
1799          IF ( time_dosp_av >= dt_averaging_input_pr )  THEN
1800             CALL calc_spectra
1801             time_dosp_av = MOD( time_dosp_av, MAX( dt_averaging_input_pr, dt_3d ) )
1802          ENDIF
1803       ENDIF
1804
1805!
1806!--    Call flight module and output data
1807       IF ( virtual_flight )  THEN
1808          CALL flight_measurement
1809          CALL data_output_flight
1810       ENDIF
1811!
1812!--    Take virtual measurements
1813       IF ( virtual_measurement  .AND.  time_virtual_measurement >= dt_virtual_measurement         &
1814                                 .AND.  vm_time_start <= time_since_reference_point )  THEN
1815          CALL vm_sampling
1816          CALL vm_data_output
1817          time_virtual_measurement = MOD(      time_virtual_measurement,                           &
1818                                          MAX( dt_virtual_measurement, dt_3d ) )
1819       ENDIF
1820!
1821!--    Profile output (ASCII) on file
1822       IF ( time_dopr_listing >= dt_dopr_listing )  THEN
1823          CALL print_1d
1824          time_dopr_listing = MOD( time_dopr_listing, MAX( dt_dopr_listing, dt_3d ) )
1825       ENDIF
1826
1827!
1828!--    Graphic output for PROFIL
1829       IF ( time_dopr >= dt_dopr  .AND.  time_since_reference_point >= skip_time_dopr )  THEN
1830          IF ( dopr_n /= 0 )  CALL data_output_profiles
1831          time_dopr = MOD( time_dopr, MAX( dt_dopr, dt_3d ) )
1832          time_dopr_av = 0.0_wp    ! due to averaging (see above)
1833       ENDIF
1834
1835!
1836!--    Graphic output for time series
1837       IF ( time_dots >= dt_dots )  THEN
1838          CALL data_output_tseries
1839          time_dots = MOD( time_dots, MAX( dt_dots, dt_3d ) )
1840       ENDIF
1841
1842!
1843!--    Output of spectra (formatted for use with PROFIL), in case of no
1844!--    time averaging, spectra has to be calculated before
1845       IF ( time_dosp >= dt_dosp  .AND.  time_since_reference_point >= skip_time_dosp )  THEN
1846          IF ( average_count_sp == 0 )  CALL calc_spectra
1847          CALL data_output_spectra
1848          time_dosp = MOD( time_dosp, MAX( dt_dosp, dt_3d ) )
1849       ENDIF
1850
1851!
1852!--    2d-data output (cross-sections)
1853       IF ( time_do2d_xy >= dt_do2d_xy  .AND.  time_since_reference_point >= skip_time_do2d_xy )  THEN
1854          IF ( current_timestep_number > timestep_number_at_prev_calc )                            &
1855             CALL doq_calculate
1856
1857          CALL data_output_2d( 'xy', 0 )
1858          time_do2d_xy = MOD( time_do2d_xy, MAX( dt_do2d_xy, dt_3d ) )
1859       ENDIF
1860       IF ( time_do2d_xz >= dt_do2d_xz  .AND.  time_since_reference_point >= skip_time_do2d_xz )  THEN
1861          IF ( current_timestep_number > timestep_number_at_prev_calc )                            &
1862
1863             CALL doq_calculate
1864          CALL data_output_2d( 'xz', 0 )
1865          time_do2d_xz = MOD( time_do2d_xz, MAX( dt_do2d_xz, dt_3d ) )
1866       ENDIF
1867       IF ( time_do2d_yz >= dt_do2d_yz  .AND.  time_since_reference_point >= skip_time_do2d_yz )  THEN
1868          IF ( current_timestep_number > timestep_number_at_prev_calc )                            &
1869             CALL doq_calculate
1870
1871          CALL data_output_2d( 'yz', 0 )
1872          time_do2d_yz = MOD( time_do2d_yz, MAX( dt_do2d_yz, dt_3d ) )
1873       ENDIF
1874
1875!
1876!--    3d-data output (volume data)
1877       IF ( time_do3d >= dt_do3d  .AND.  time_since_reference_point >= skip_time_do3d )  THEN
1878          IF ( current_timestep_number > timestep_number_at_prev_calc )                            &
1879             CALL doq_calculate
1880
1881          CALL data_output_3d( 0 )
1882          time_do3d = MOD( time_do3d, MAX( dt_do3d, dt_3d ) )
1883       ENDIF
1884
1885!
1886!--    Masked data output
1887       DO  mid = 1, masks
1888          IF ( time_domask(mid) >= dt_domask(mid)                                                  &
1889               .AND.  time_since_reference_point >= skip_time_domask(mid) )  THEN
1890             IF ( current_timestep_number > timestep_number_at_prev_calc )                         &
1891                CALL doq_calculate
1892
1893             CALL data_output_mask( 0, mid )
1894             time_domask(mid) = MOD( time_domask(mid), MAX( dt_domask(mid), dt_3d ) )
1895          ENDIF
1896       ENDDO
1897
1898!
1899!--    Output of time-averaged 2d/3d/masked data
1900       IF ( time_do_av >= dt_data_output_av                                                        &
1901            .AND.  time_since_reference_point >= skip_time_data_output_av )  THEN
1902          CALL average_3d_data
1903!
1904!--       Udate thermal comfort indices based on updated averaged input
1905          IF ( biometeorology  .AND.  thermal_comfort )  THEN
1906             CALL bio_calculate_thermal_index_maps ( .TRUE. )
1907          ENDIF
1908          CALL data_output_2d( 'xy', 1 )
1909          CALL data_output_2d( 'xz', 1 )
1910          CALL data_output_2d( 'yz', 1 )
1911          CALL data_output_3d( 1 )
1912          DO  mid = 1, masks
1913             CALL data_output_mask( 1, mid )
1914          ENDDO
1915          time_do_av = MOD( time_do_av, MAX( dt_data_output_av, dt_3d ) )
1916       ENDIF
1917!
1918!--    Output of surface data, instantaneous and averaged data
1919       IF ( surface_output )  THEN
1920          IF ( time_dosurf >= dt_dosurf  .AND.  time_since_reference_point >= skip_time_dosurf )  THEN
1921             CALL surface_data_output( 0 )
1922             time_dosurf = MOD( time_dosurf, MAX( dt_dosurf, dt_3d ) )
1923          ENDIF
1924          IF ( time_dosurf_av >= dt_dosurf_av  .AND.  time_since_reference_point >= skip_time_dosurf_av )  THEN
1925             CALL surface_data_output( 1 )
1926             time_dosurf_av = MOD( time_dosurf_av, MAX( dt_dosurf_av, dt_3d ) )
1927          ENDIF
1928       ENDIF
1929
1930!
1931!--    Output of particle time series
1932       IF ( particle_advection )  THEN
1933          IF ( time_dopts >= dt_dopts  .OR.                                                        &
1934               ( time_since_reference_point >= particle_advection_start  .AND.                     &
1935                 first_call_lpm ) )  THEN
1936             CALL lpm_data_output_ptseries
1937             time_dopts = MOD( time_dopts, MAX( dt_dopts, dt_3d ) )
1938          ENDIF
1939       ENDIF
1940
1941!
1942!--    If required, set the heat flux for the next time step to a random value
1943       IF ( constant_heatflux  .AND.  random_heatflux )  THEN
1944          IF ( surf_def_h(0)%ns >= 1 )  THEN
1945             CALL cpu_log( log_point(23), 'disturb_heatflux', 'start' )
1946             CALL disturb_heatflux( surf_def_h(0) )
1947             CALL cpu_log( log_point(23), 'disturb_heatflux', 'stop' )
1948          ENDIF
1949          IF ( surf_lsm_h%ns    >= 1 )  THEN
1950             CALL cpu_log( log_point(23), 'disturb_heatflux', 'start' )
1951             CALL disturb_heatflux( surf_lsm_h    )
1952             CALL cpu_log( log_point(23), 'disturb_heatflux', 'stop' )
1953          ENDIF
1954          IF ( surf_usm_h%ns    >= 1 )  THEN
1955             CALL cpu_log( log_point(23), 'disturb_heatflux', 'start' )
1956             CALL disturb_heatflux( surf_usm_h    )
1957             CALL cpu_log( log_point(23), 'disturb_heatflux', 'stop' )
1958          ENDIF
1959       ENDIF
1960
1961!
1962!--    Execute alle other module actions routunes
1963       CALL module_interface_actions( 'after_timestep' )
1964
1965!
1966!--    Determine size of next time step. Save timestep dt_3d because it is
1967!--    newly calculated in routine timestep, but required further below for
1968!--    steering the run control output interval
1969       dt_3d_old = dt_3d
1970       CALL timestep
1971
1972!
1973!--    Synchronize the timestep in case of nested run.
1974       IF ( nested_run )  THEN
1975!
1976!--       Synchronize by unifying the time step.
1977!--       Global minimum of all time-steps is used for all.
1978          CALL pmci_synchronize
1979       ENDIF
1980
1981!
1982!--    Computation and output of run control parameters.
1983!--    This is also done whenever perturbations have been imposed
1984       IF ( time_run_control >= dt_run_control  .OR.                                               &
1985            timestep_scheme(1:5) /= 'runge'  .OR.  disturbance_created )                           &
1986       THEN
1987          CALL run_control
1988          IF ( time_run_control >= dt_run_control )  THEN
1989             time_run_control = MOD( time_run_control, MAX( dt_run_control, dt_3d_old ) )
1990          ENDIF
1991       ENDIF
1992
1993!
1994!--    Output elapsed simulated time in form of a progress bar on stdout
1995       IF ( myid == 0 )  CALL output_progress_bar
1996
1997       CALL cpu_log( log_point_s(10), 'timesteps', 'stop' )
1998
1999
2000    ENDDO   ! time loop
2001
2002#if defined( _OPENACC )
2003    CALL exit_surface_arrays
2004#endif
2005!$ACC END DATA
2006!$ACC END DATA
2007!$ACC END DATA
2008!$ACC END DATA
2009!$ACC END DATA
2010!$ACC END DATA
2011!$ACC END DATA
2012
2013!
2014!-- Vertical nesting: Deallocate variables initialized for vertical nesting   
2015    IF ( vnest_init )  CALL vnest_deallocate
2016
2017    IF ( myid == 0 )  CALL finish_progress_bar
2018
2019    CALL location_message( 'atmosphere (and/or ocean) time-stepping', 'finished' )
2020
2021 END SUBROUTINE time_integration
Note: See TracBrowser for help on using the repository browser.