source: palm/trunk/SOURCE/multi_agent_system_mod.f90 @ 4828

Last change on this file since 4828 was 4828, checked in by Giersch, 3 years ago

Copyright updated to year 2021, interface pmc_sort removed to accelarate the nesting code

  • Property svn:keywords set to Id
File size: 189.8 KB
Line 
1!> @file multi_agent_system_mod.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 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.
8!
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.
12!
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/>.
15!
16! Copyright 2016-2021 Leibniz Universitaet Hannover
17!--------------------------------------------------------------------------------------------------!
18!
19! Current revisions:
20! ------------------
21!
22!
23! Former revisions:
24! -----------------
25! $Id: multi_agent_system_mod.f90 4828 2021-01-05 11:21:41Z Giersch $
26! file re-formatted to follow the PALM coding standard
27!
28! 4623 2020-07-24 08:42:02Z raasch
29! some switches calculated explicitly to avoid compiler warnings
30!
31! 4481 2020-03-31 18:55:54Z maronga
32! bugfix: cpp-directives for serial mode added
33!
34! 4346 2019-12-18 11:55:56Z motisi
35! Removed wall_flags_static_0 from USE statements as it's not used within the module
36!
37! 4329 2019-12-10 15:46:36Z motisi
38! Renamed wall_flags_0 to wall_flags_static_0
39!
40! 4307 2019-11-26 14:12:36Z maronga
41! Activated output of iPT
42!
43! 4182 2019-08-22 15:20:23Z scharf
44! Corrected "Former revisions" section
45!
46! 4168 2019-08-16 13:50:17Z suehring
47! Replace function get_topography_top_index by topo_top_ind
48!
49! 3987 2019-05-22 09:52:13Z kanani
50! Introduce alternative switch for debug output during timestepping
51!
52! 3885 2019-04-11 11:29:34Z kanani
53! Changes related to global restructuring of location messages and introduction  of additional debug
54! messages
55!
56! 3876 2019-04-08 18:41:49Z knoop
57! replaced nspec by nvar: only variable species should bconsidered, fixed species are not relevant
58!
59! 3766 2019-02-26 16:23:41Z raasch
60! save attribute added to local targets to avoid outlive pointer target warning
61!
62! 3665 2019-01-10 08:28:24Z raasch
63! unused variables removed
64!
65! 3159 2018-07-20 11:20:01Z sward
66! Initial revision
67!
68!
69!
70! Authors:
71! --------
72! @author sward
73!
74!
75! Description:
76! ------------
77!> Multi Agent System for the simulation of pedestrian movement in urban environments
78!--------------------------------------------------------------------------------------------------!
79 MODULE multi_agent_system_mod
80
81    USE, INTRINSIC ::  ISO_C_BINDING
82
83    USE basic_constants_and_equations_mod,                                                         &
84        ONLY:  pi
85
86    USE control_parameters,                                                                        &
87        ONLY:  biometeorology,                                                                     &
88               debug_output_timestep,                                                              &
89               dt_3d,                                                                              &
90               dt_write_agent_data,                                                                &
91               message_string,                                                                     &
92               time_since_reference_point
93
94    USE cpulog,                                                                                    &
95        ONLY:  cpu_log, log_point, log_point_s
96
97    USE grid_variables,                                                                            &
98        ONLY:  ddx, ddy, dx, dy
99
100    USE indices,                                                                                   &
101        ONLY:  nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb, topo_top_ind
102
103    USE random_function_mod,                                                                       &
104        ONLY:  random_function
105
106    USE kinds
107
108    USE pegrid
109
110    INTEGER(iwp), PARAMETER ::  max_number_of_agent_groups = 100  !< maximum allowed number of agent groups
111    INTEGER(iwp), PARAMETER ::  nr_2_direction_move = 10000       !< parameter for agent exchange
112    INTEGER(iwp), PARAMETER ::  phase_init    = 1                 !< phase parameter
113    INTEGER(iwp), PARAMETER ::  phase_release = 2                 !< phase parameter
114
115    CHARACTER(LEN=15) ::  bc_mas_lr = 'absorb'  !< left/right boundary condition
116    CHARACTER(LEN=15) ::  bc_mas_ns = 'absorb'  !< north/south boundary condition
117
118    INTEGER(iwp) ::  agt_path_size = 15                !< size of agent path array
119    INTEGER(iwp) ::  deleted_agents = 0                !< number of deleted agents per time step
120    INTEGER(iwp) ::  dim_size_agtnum_manual = 9999999  !< namelist parameter (see documentation)
121    INTEGER(iwp) ::  heap_count                        !< number of items in binary heap (for pathfinding)
122    INTEGER(iwp) ::  ibc_mas_lr                        !< agent left/right boundary condition dummy
123    INTEGER(iwp) ::  ibc_mas_ns                        !< agent north/south boundary condition dummy
124!    INTEGER(iwp) ::  ind_pm10 = -9                     !< chemical species index of PM10
125!    INTEGER(iwp) ::  ind_pm25 = -9                     !< chemical species index of PM2.5
126    INTEGER(iwp) ::  iran_agent = -1234567             !< number for random generator
127    INTEGER(iwp) ::  min_nr_agent = 2                  !< namelist parameter (see documentation)
128#if defined( __parallel )
129    INTEGER(iwp) ::  ghla_count_recv                   !< number of agents in left ghost layer
130    INTEGER(iwp) ::  ghna_count_recv                   !< number of agents in north ghost layer
131    INTEGER(iwp) ::  ghra_count_recv                   !< number of agents in right ghost layer
132    INTEGER(iwp) ::  ghsa_count_recv                   !< number of agents in south ghost layer
133    INTEGER(iwp) ::  nr_move_north                     !< number of agts to move north during exchange_horiz
134    INTEGER(iwp) ::  nr_move_south                     !< number of agts to move south during exchange_horiz
135#endif
136    INTEGER(iwp) ::  maximum_number_of_agents = 0      !< maximum number of agents during run
137    INTEGER(iwp) ::  number_of_agents = 0              !< number of agents for each grid box (3d array is saved on agt_count)
138    INTEGER(iwp) ::  number_of_agent_groups = 1        !< namelist parameter (see documentation)
139    INTEGER(iwp) ::  sort_count_mas = 0                !< counter for sorting agents
140    INTEGER(iwp) ::  step_dealloc_mas = 100            !< namelist parameter (see documentation)
141    INTEGER(iwp) ::  total_number_of_agents            !< total number of agents in the whole model domain
142
143    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  agt_count         !< 3d array of number of agents of every grid box
144    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  s_measure_height  !< k-index(s-grid) for measurement
145    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  top_top_s         !< k-index of first s-gridpoint above topography
146    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  top_top_w         !< k-index of first v-gridpoint above topography
147    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  obstacle_flags    !< flags to identify corners and edges of topography that cannot
148                                                                    !< be crossed by agents
149
150    LOGICAL ::  deallocate_memory_mas = .TRUE.          !< namelist parameter (see documentation)
151    LOGICAL ::  dt_3d_reached_mas                       !< flag: agent timestep has reached model timestep
152    LOGICAL ::  dt_3d_reached_l_mas                     !< flag: agent timestep has reached model timestep
153    LOGICAL ::  agents_active = .FALSE.                 !< flag for agent system
154    LOGICAL ::  random_start_position_agents = .TRUE.   !< namelist parameter (see documentation)
155    LOGICAL ::  read_agents_from_restartfile = .FALSE.  !< namelist parameter (see documentation)
156    LOGICAL ::  agent_own_timestep = .FALSE.            !< namelist parameter (see documentation)
157
158    LOGICAL, DIMENSION(max_number_of_agent_groups) ::  a_rand_target = .FALSE. !< namelist parameter (see documentation)
159
160    REAL(wp) ::  agent_maximum_age = 9999999.9_wp          !< namelist parameter (see documentation)
161    REAL(wp) ::  agent_substep_time = 0.0_wp               !< time measurement during one LES timestep
162    REAL(wp) ::  alloc_factor_mas = 20.0_wp                !< namelist parameter (see documentation)
163    REAL(wp) ::  coll_t_0 = 3.                             !< namelist parameter (see documentation)
164    REAL(wp) ::  corner_gate_start = 0.5_wp                !< namelist parameter (see documentation)
165    REAL(wp) ::  corner_gate_width = 1.0_wp                !< namelist parameter (see documentation)
166    REAL(wp) ::  dim_size_factor_agtnum = 1.0_wp           !< namelist parameter (see documentation)
167    REAL(wp) ::  d_sigma_rep_agent                         !< inverse of sigma_rep_agent
168    REAL(wp) ::  d_sigma_rep_wall                          !< inverse of sigma_rep_wall
169    REAL(wp) ::  d_tau_accel_agent                         !< inverse of tau_accel_agent
170    REAL(wp) ::  desired_speed = 1.2_wp                    !< namelist parameter (see documentation)
171    REAL(wp) ::  des_sp_sig = .2_wp                        !< namelist parameter (see documentation)
172    REAL(wp) ::  dist_target_reached = 2.0_wp              !< distance at which target counts as reached
173    REAL(wp) ::  dist_to_int_target = .25_wp               !< namelist parameter (see documentation)
174    REAL(wp) ::  dt_agent = 0.02_wp                        !< namelist parameter (see documentation)
175    REAL(wp) ::  dt_arel = 9999999.9_wp                    !< namelist parameter (see documentation)
176    REAL(wp) ::  end_time_arel = 9999999.9_wp              !< namelist parameter (see documentation)
177    REAL(wp) ::  force_x                                   !< dummy value for force on current agent in x-direction
178    REAL(wp) ::  force_y                                   !< dummy value for force on current agent in y-direction
179    REAL(wp) ::  max_dist_from_path = 0.25_wp              !< distance from current path at which a new path is calculated
180    REAL(wp) ::  radius_agent = .25_wp                     !< namelist parameter (see documentation)
181    REAL(wp) ::  repuls_agent = 1.5_wp                     !< namelist parameter (see documentation)
182    REAL(wp) ::  repuls_wall = 7.0_wp                      !< namelist parameter (see documentation)
183    REAL(wp) ::  scan_radius_agent = 3.0_wp                !< namelist parameter (see documentation)
184    REAL(wp) ::  scan_radius_wall = 2.0_wp                 !< namelist parameter (see documentation)
185    REAL(wp) ::  sigma_rep_agent = 0.3_wp                  !< namelist parameter (see documentation)
186    REAL(wp) ::  sigma_rep_wall = 0.1_wp                   !< namelist parameter (see documentation)
187    REAL(wp) ::  tau_accel_agent = 0.5_wp                  !< namelist parameter (see documentation)
188    REAL(wp) ::  time_arel = 0.0_wp                        !< time for agent release
189    REAL(wp) ::  time_write_agent_data = 0.0_wp            !< write agent data at current time on file
190    REAL(wp) ::  v_max_agent = 1.3_wp                      !< namelist parameter (see documentation)
191
192    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dummy_path_x  !<  dummy path (x-coordinate)
193    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dummy_path_y  !<  dummy path (y-coordinate)
194
195    REAL(wp), DIMENSION(max_number_of_agent_groups) ::  adx = 9999999.9_wp  !< namelist parameter (see documentation)
196    REAL(wp), DIMENSION(max_number_of_agent_groups) ::  ady = 9999999.9_wp  !< namelist parameter (see documentation)
197    REAL(wp), DIMENSION(max_number_of_agent_groups) ::  asl = 9999999.9_wp  !< namelist parameter (see documentation)
198    REAL(wp), DIMENSION(max_number_of_agent_groups) ::  asn = 9999999.9_wp  !< namelist parameter (see documentation)
199    REAL(wp), DIMENSION(max_number_of_agent_groups) ::  asr = 9999999.9_wp  !< namelist parameter (see documentation)
200    REAL(wp), DIMENSION(max_number_of_agent_groups) ::  ass = 9999999.9_wp  !< namelist parameter (see documentation)
201    REAL(wp), DIMENSION(max_number_of_agent_groups) ::  at_x = 9999999.9_wp !< namelist parameter (see documentation)
202    REAL(wp), DIMENSION(max_number_of_agent_groups) ::  at_y = 9999999.9_wp !< namelist parameter (see documentation)
203!
204!-- Type for the definition of an agent
205    TYPE agent_type
206        INTEGER(iwp) ::  block_nr             !< number for sorting
207        INTEGER(iwp) ::  group                !< number of agent group
208        INTEGER(idp) ::  id                   !< particle ID (64 bit integer)
209        INTEGER(iwp) ::  path_counter         !< current target along path (path_x/y)
210        LOGICAL      ::  agent_mask           !< if this parameter is set to false the agent will be deleted
211        REAL(wp)     ::  age                  !< age of agent
212        REAL(wp)     ::  age_m                !< age of agent
213        REAL(wp)     ::  dt_sum               !< sum of agents subtimesteps
214        REAL(wp)     ::  clo                  !< clothing index
215        REAL(wp)     ::  energy_storage       !< energy stored by agent
216        REAL(wp)     ::  clothing_temp        !< energy stored by agent
217        REAL(wp)     ::  actlev               !< metabolic + work energy of the person
218        REAL(wp)     ::  age_years            !< physical age of the person
219        REAL(wp)     ::  weight               !< total weight of the person (kg)
220        REAL(wp)     ::  height               !< height of the person (m)
221        REAL(wp)     ::  work                 !< workload of the agent (W)
222        INTEGER(iwp) ::  sex                  !< agents gender: 1 = male, 2 = female
223        REAL(wp)     ::  force_x              !< force term x-direction
224        REAL(wp)     ::  force_y              !< force term y-direction
225        REAL(wp)     ::  origin_x             !< origin x-position of agent
226        REAL(wp)     ::  origin_y             !< origin y-position of agent
227        REAL(wp)     ::  pm10                 !< PM10 concentration at agent position
228        REAL(wp)     ::  pm25                 !< PM25 concentration at agent position
229        REAL(wp)     ::  speed_abs            !< absolute value of agent speed
230        REAL(wp)     ::  speed_e_x            !< normalized speed of agent in x
231        REAL(wp)     ::  speed_e_y            !< normalized speed of agent in y
232        REAL(wp)     ::  speed_des            !< agent's desired speed
233        REAL(wp)     ::  speed_x              !< speed of agent in x
234        REAL(wp)     ::  speed_y              !< speed of agent in y
235        REAL(wp)     ::  ipt                  !< instationary thermal index iPT (degree_C)
236        REAL(wp)     ::  windspeed            !< absolute value of windspeed at agent position
237        REAL(wp)     ::  x                    !< x-position
238        REAL(wp)     ::  y                    !< y-position
239        REAL(wp)     ::  t                    !< temperature
240        REAL(wp)     ::  t_x                  !< x-position
241        REAL(wp)     ::  t_y                  !< y-position
242        REAL(wp), DIMENSION(0:15) ::  path_x  !< agent path to target (x)
243        REAL(wp), DIMENSION(0:15) ::  path_y  !< agent path to target (y)
244    END TYPE agent_type
245
246    TYPE(agent_type)                        ::  zero_agent           !< zero agent to avoid weird thing
247
248    TYPE(agent_type), DIMENSION(:), POINTER ::  agents               !< Agent array for this grid cell
249#if defined( __parallel )
250    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_l         !< ghost layer left of pe domain
251    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_n         !< ghost layer north of pe domain
252    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_r         !< ghost layer right of pe domain
253    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_s         !< ghost layer south of pe domain
254    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_north  !< for agent exchange between PEs
255    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_south  !< for agent exchange between PEs
256#endif
257!
258!-- Type for 2D grid on which agents are stored
259    TYPE  grid_agent_def
260        INTEGER(iwp), DIMENSION(0:3)            ::  start_index        !< start agent index for current block
261        INTEGER(iwp), DIMENSION(0:3)            ::  end_index          !< end agent index for current block
262        INTEGER(iwp)                            ::  id_counter         !< agent id counter (removeable?)
263        LOGICAL                                 ::  time_loop_done     !< timestep loop for agent advection
264        TYPE(agent_type), POINTER, DIMENSION(:) ::  agents             !< Particle array for this grid cell
265    END TYPE grid_agent_def
266
267    TYPE(grid_agent_def), DIMENSION(:,:), ALLOCATABLE, TARGET ::  grid_agents !< 2D grid on which agents are stored
268!
269!-- Item in a priority queue (binary heap)
270    TYPE heap_item
271       INTEGER(iwp) ::  mesh_id       !< id of the submitted mesh point
272       REAL(wp)     ::  priority      !< priority of the mesh point (= distance so far + heuristic to goal)
273    END TYPE heap_item
274
275    TYPE(heap_item), DIMENSION(:), ALLOCATABLE ::  queue  !< priority queue realized as binary heap
276!
277!-- Type for mesh point in visibility graph
278    TYPE  mesh_point
279        INTEGER(iwp)                            ::  polygon_id          !< Polygon the point belongs to
280        INTEGER(iwp)                            ::  vertex_id           !< Vertex in the polygon
281        INTEGER(iwp)                            ::  noc                 !< number of connections
282        INTEGER(iwp)                            ::  origin_id           !< ID of previous mesh point on path (A*)
283        INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  connected_vertices  !< Index of connected vertices
284        REAL(wp)                                ::  cost_so_far         !< Cost to reach this mesh point (A*)
285        REAL(wp)                                ::  x                   !< x-coordinate
286        REAL(wp)                                ::  y                   !< y-coordinate
287        REAL(wp)                                ::  x_s                 !< corner shifted outward from building by 1m (x)
288        REAL(wp)                                ::  y_s                 !< corner shifted outward from building by 1m (y)
289        REAL(wp), DIMENSION(:), ALLOCATABLE     ::  distance_to_vertex  !< Distance to each vertex
290    END TYPE mesh_point
291
292    TYPE(mesh_point), DIMENSION(:), ALLOCATABLE ::  mesh     !< navigation mesh
293    TYPE(mesh_point), DIMENSION(:), ALLOCATABLE ::  tmp_mesh !< temporary navigation mesh
294!
295!-- Vertex of a polygon
296    TYPE  vertex_type
297        LOGICAL               ::  delete  !< Flag to mark vertex for deletion
298        REAL(wp)              ::  x       !< x-coordinate
299        REAL(wp)              ::  y       !< y-coordinate
300    END TYPE vertex_type
301!
302!-- Polygon containing a number of vertices
303    TYPE  polygon_type
304        INTEGER(iwp)                                 ::  nov       !< Number of vertices in this polygon
305        TYPE(vertex_type), DIMENSION(:), ALLOCATABLE ::  vertices  !< Array of vertices
306    END TYPE polygon_type
307
308    TYPE(polygon_type), DIMENSION(:), ALLOCATABLE ::  polygons  !< Building data in polygon form
309
310    SAVE
311
312    PRIVATE
313!
314!-- Public functions
315    PUBLIC mas_init, mas_last_actions, mas_parin, multi_agent_system
316
317!
318!-- Public parameters, constants and initial values
319    PUBLIC agents_active
320
321    INTERFACE mas_parin
322       MODULE PROCEDURE mas_parin
323    END INTERFACE mas_parin
324
325    INTERFACE mas_init
326       MODULE PROCEDURE mas_init
327    END INTERFACE mas_init
328
329    INTERFACE mas_last_actions
330       MODULE PROCEDURE mas_last_actions
331    END INTERFACE mas_last_actions
332
333    INTERFACE multi_agent_system
334       MODULE PROCEDURE multi_agent_system
335    END INTERFACE multi_agent_system
336
337    CONTAINS
338
339
340!--------------------------------------------------------------------------------------------------!
341! Description:
342! ------------
343!> Multi Agent System:
344!> executes a number of agents sub-timesteps until the model timestep is reached.
345!> The agent timestep is usually smaller than the model timestep.
346!--------------------------------------------------------------------------------------------------!
347 SUBROUTINE multi_agent_system
348
349    USE biometeorology_mod,                                                                        &
350        ONLY:  bio_calc_ipt,                                                                       &
351               bio_calculate_mrt_grid,                                                             &
352               bio_get_thermal_index_input_ij
353
354
355    IMPLICIT NONE
356
357    INTEGER(iwp)       ::  i                  !< counter
358    INTEGER(iwp)       ::  ie                 !< counter
359    INTEGER(iwp)       ::  is                 !< counter
360    INTEGER(iwp)       ::  j                  !< counter
361    INTEGER(iwp)       ::  je                 !< counter
362    INTEGER(iwp)       ::  js                 !< counter
363    INTEGER(iwp), SAVE ::  mas_count = 0      !< counts the mas-calls
364    INTEGER(iwp)       ::  a                  !< agent iterator
365!
366!-- local meteorological conditions
367    REAL(wp) ::  pair  !< air pressure                    (hPa)
368    REAL(wp) ::  ta    !< air temperature                 (degree_C)
369    REAL(wp) ::  tmrt  !< mean radiant temperature        (degree_C)
370    REAL(wp) ::  v     !< wind speed    (local level)     (m/s)
371    REAL(wp) ::  vp    !< vapour pressure                 (hPa)
372
373
374    LOGICAL       ::  first_loop_stride   !< flag for first loop stride of agent sub-timesteps
375    LOGICAL, SAVE ::  first_call = .TRUE. !< first call of mas flag for output
376
377
378    IF ( debug_output_timestep )  CALL debug_message( 'multi_agent_system', 'start' )
379
380    CALL cpu_log( log_point(9), 'mas', 'start' )
381!
382!-- Initialize variables for the next (sub-) timestep, i.e., for marking those agents to be deleted
383!-- after the timestep
384    deleted_agents = 0
385    agent_substep_time = 0.0_wp
386!
387!-- If necessary, release new set of agents
388    IF ( time_arel >= dt_arel  .AND.  end_time_arel > time_since_reference_point )  THEN
389
390       CALL mas_create_agent( phase_release )
391!
392!--    The MOD function allows for changes in the output interval with restart runs.
393       time_arel = MOD( time_arel, MAX( dt_arel, dt_3d ) )
394
395    ENDIF
396
397    first_loop_stride = .TRUE.
398    grid_agents(:,:)%time_loop_done = .TRUE.
399!
400!-- Set timestep variable
401    IF ( .NOT. agent_own_timestep ) dt_agent = dt_3d
402!
403!-- Timestep loop for agent transport.
404!-- This loop has to be repeated until the transport time of every agent (within the total domain!)
405!-- has reached the LES timestep (dt_3d).
406!-- Timestep scheme is Euler-forward.
407    DO
408!
409!--    Write agent data at current time on file.
410       time_write_agent_data = time_write_agent_data + dt_agent
411       agent_substep_time    = agent_substep_time    + dt_agent
412       IF ( time_write_agent_data >= dt_write_agent_data )  THEN
413#if defined( __netcdf )
414          IF ( first_loop_stride ) CALL mas_get_prognostic_quantities
415          CALL mas_data_output_agents ( first_call )
416#else
417          WRITE( message_string, * ) 'NetCDF is needed for agent output. ',                        &
418                                     'Set __netcdf in compiler options'
419          CALL message( 'multi_agent_system', 'PA0071', 1, 2, 0, 6, 0 )
420#endif
421          IF(first_call) first_call = .FALSE.
422          time_write_agent_data = time_write_agent_data - dt_write_agent_data
423       ENDIF
424!
425!--    Flag is true by default, will be set to false if an agent has not yet reached the model
426!--    timestep.
427       grid_agents(:,:)%time_loop_done = .TRUE.
428
429!
430!--    First part of agent transport:
431!--    Evaluate social forces for all agents at current positions
432       CALL cpu_log( log_point_s(9), 'mas_social_forces', 'start' )
433       DO  i = nxl, nxr
434          DO  j = nys, nyn
435
436             number_of_agents = agt_count(j,i)
437!
438!--          If grid cell is empty, cycle
439             IF ( number_of_agents <= 0 ) CYCLE
440
441             agents => grid_agents(j,i)%agents(1:number_of_agents)
442!
443!--          Evaluation of social forces
444             CALL mas_timestep_forces_call(i,j)
445
446          ENDDO
447       ENDDO
448       CALL cpu_log( log_point_s(9), 'mas_social_forces', 'stop' )
449!
450!--    Second part of agent transport:
451!--    timestep
452       CALL cpu_log( log_point_s(16), 'mas_timestep', 'start' )
453       DO  i = nxl, nxr
454          DO  j = nys, nyn
455
456             number_of_agents = agt_count(j,i)
457!
458!--          If grid cell is empty, flag must be true
459             IF ( number_of_agents <= 0 )  THEN
460                grid_agents(j,i)%time_loop_done = .TRUE.
461                CYCLE
462             ENDIF
463
464             agents => grid_agents(j,i)%agents(1:number_of_agents)
465
466             agents(1:number_of_agents)%agent_mask = .TRUE.
467!
468!--          Initialize the variable storing the total time that an agent has advanced within the
469!--          timestep procedure.
470             IF ( first_loop_stride )  THEN
471                agents(1:number_of_agents)%dt_sum = 0.0_wp
472             ENDIF
473!
474!--          Initialize the switch used for the loop exit condition checked at the end of this loop.
475!--          If at least one agent has failed to reach the LES timestep, this switch will be set
476!--          false in mas_transport.
477             dt_3d_reached_l_mas = .TRUE.
478!
479!--          Timestep
480             CALL mas_timestep
481!
482!--          Delete agents that have been simulated longer than allowed.
483             CALL mas_boundary_conds( 'max_sim_time' )
484!
485!--          Delete agents that have reached target area.
486             CALL mas_boundary_conds( 'target_area' )
487!
488!--          If not all agents of the actual grid cell have reached the LES timestep, this cell has
489!--          to do another loop iteration. Due to the fact that agents can move into neighboring
490!--          grid cell, these neighbor cells also have to perform another loop iteration.
491             IF ( .NOT. dt_3d_reached_l_mas )  THEN
492                js = MAX(nys,j-1)
493                je = MIN(nyn,j+1)
494                is = MAX(nxl,i-1)
495                ie = MIN(nxr,i+1)
496                grid_agents(js:je,is:ie)%time_loop_done = .FALSE.
497             ENDIF
498
499          ENDDO
500       ENDDO
501       CALL cpu_log( log_point_s(16), 'mas_timestep', 'stop' )
502
503!
504!--    Find out, if all agents on each PE have completed the LES timestep and set the switch
505!--    corespondingly.
506       dt_3d_reached_l_mas = ALL(grid_agents(:,:)%time_loop_done)
507#if defined( __parallel )
508       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
509       CALL MPI_ALLREDUCE( dt_3d_reached_l_mas, dt_3d_reached_mas, 1, MPI_LOGICAL,  MPI_LAND,      &
510                           comm2d, ierr )
511#else
512       dt_3d_reached_mas = dt_3d_reached_l_mas
513#endif
514
515!
516!--    Increment time since last release
517       IF ( dt_3d_reached_mas )  time_arel = time_arel + dt_3d
518
519!
520!--    Move Agents local to PE to a different grid cell
521       CALL cpu_log( log_point_s(18), 'mas_move_exch_sort', 'start' )
522       CALL mas_eh_move_agent
523!
524!--    Horizontal boundary conditions including exchange between subdmains
525       CALL mas_eh_exchange_horiz
526!
527!--    Pack agents (eliminate those marked for deletion), determine new number of agents
528       CALL mas_ps_sort_in_subboxes
529       CALL cpu_log( log_point_s(18), 'mas_move_exch_sort', 'stop' )
530!
531!--    Initialize variables for the next (sub-) timestep, i.e., for marking those agents to be
532!--    deleted after the timestep
533       deleted_agents = 0
534
535       IF ( biometeorology )  THEN
536!
537!--       Fill out the MRT 2D grid from appropriate source (RTM, RRTMG,...)
538          CALL bio_calculate_mrt_grid ( .FALSE. )
539!
540!--       Call of human thermal comfort mod (and UV exposure)
541          DO  i = nxl, nxr
542             DO  j = nys, nyn
543
544                number_of_agents = agt_count(j,i)
545!
546!--             If grid cell gets empty, cycle
547                IF ( number_of_agents <= 0 )  CYCLE
548
549                agents => grid_agents(j,i)%agents(1:number_of_agents)
550!
551!--             Evaluation of social forces
552!                CALL bio_dynamic( i, j )
553!
554!--             Determine local meteorological conditions
555                CALL bio_get_thermal_index_input_ij ( .FALSE., i, j, ta, vp, v, pair, tmrt )
556
557                DO  a = 1, number_of_agents
558!
559!--                Calculate instationary thermal indices based on local tmrt
560
561                   CALL bio_calc_ipt ( ta, vp, v, pair, tmrt,                                      &
562                                       agents(a)%dt_sum,                                           &
563                                       agents(a)%energy_storage,                                   &
564                                       agents(a)%clothing_temp,                                    &
565                                       agents(a)%clo,                                              &
566                                       agents(a)%actlev,                                           &
567                                       agents(a)%age_years,                                        &
568                                       agents(a)%weight,                                           &
569                                       agents(a)%height,                                           &
570                                       agents(a)%work,                                             &
571                                       agents(a)%sex,                                              &
572                                       agents(a)%ipt )
573                END DO
574
575             ENDDO
576          ENDDO
577       ENDIF
578
579       IF ( dt_3d_reached_mas )  EXIT
580
581       first_loop_stride = .FALSE.
582    ENDDO   ! timestep loop
583
584!
585!-- Deallocate unused memory
586    IF ( deallocate_memory_mas  .AND.  mas_count == step_dealloc_mas )  THEN
587       CALL mas_eh_dealloc_agents_array
588       mas_count = 0
589    ELSEIF ( deallocate_memory_mas )  THEN
590       mas_count = mas_count + 1
591    ENDIF
592
593    CALL cpu_log( log_point(9), 'mas', 'stop' )
594
595    IF ( debug_output_timestep )  CALL debug_message( 'multi_agent_system', 'end' )
596
597
598 END SUBROUTINE multi_agent_system
599
600!--------------------------------------------------------------------------------------------------!
601! Description:
602! ------------
603!> Calculation of the direction vector from each agent to its current intermittent target.
604!--------------------------------------------------------------------------------------------------!
605 SUBROUTINE mas_agent_direction
606
607    IMPLICIT NONE
608
609    LOGICAL ::  path_flag  !< true if new path must be calculated
610
611    INTEGER(iwp) ::  n   !< loop variable over all agents in a grid box
612    INTEGER(iwp) ::  pc  !< agent path counter
613
614    REAL(wp) ::  abs_dir         !< length of direction vector (for normalization)
615!    REAL(wp) ::  d_curr_target   !< rounding influence expressed as x speed component
616!    REAL(wp) ::  d_prev_target   !< rounding influence expressed as x speed component
617    REAL(wp) ::  dir_x           !< direction of agent (x)
618    REAL(wp) ::  dir_y           !< direction of agent (y)
619!    REAL(wp) ::  dist_round = 3. !< distance at which agents start rounding a corner
620    REAL(wp) ::  dtit            !< distance to intermittent target
621!    REAL(wp) ::  round_fac  = 0.2 !< factor for rounding influence
622!    REAL(wp) ::  speed_round_x   !< rounding influence expressed as x speed component
623!    REAL(wp) ::  speed_round_y   !< rounding influence expressed as x speed component
624
625!
626!-- Loop over all agents in the current grid box
627    DO  n = 1, number_of_agents
628       path_flag = .FALSE.
629       pc = agents(n)%path_counter
630!
631!--    If no path was calculated for agent yet, do it.
632       IF ( pc >= 999 )  THEN
633          CALL mas_nav_find_path(n)
634          pc = agents(n)%path_counter
635!
636!--    Check if new path must be calculated and if so, do it.
637       ELSE
638!
639!--       Case one: Agent has come close enough to intermittent target.
640!--                 -> chose new int target and calculate rest of path if no
641!--                    new intermittent targets are left
642          dtit = SQRT(   ( agents(n)%x - agents(n)%path_x(pc) )**2                                 &
643                       + ( agents(n)%y - agents(n)%path_y(pc) )**2 )
644          IF ( dtit < dist_to_int_target )  THEN
645             agents(n)%path_counter = agents(n)%path_counter + 1
646             pc = agents(n)%path_counter
647!
648!--          Path counter out of scope (each agent can store a maximum of 15 intermittent targets on
649!--          the way to its final target); new path must be calculated.
650             IF ( pc >= SIZE( agents(n)%path_x) )  THEN
651                path_flag = .TRUE.
652             ENDIF
653!
654!--          Case two: Agent too far from path
655!--                    -> set flag for new path to be calculated
656          ELSEIF ( dist_point_to_edge(agents(n)%path_x(pc-1),                                      &
657                                      agents(n)%path_y(pc-1),                                      &
658                                      agents(n)%path_x(pc),                                        &
659                                      agents(n)%path_y(pc),                                        &
660                                      agents(n)%x, agents(n)%y)                                    &
661                   > max_dist_from_path )                                                          &
662          THEN
663             path_flag = .TRUE.
664          ENDIF
665!
666!--       If one of the above two cases was true, calculate new path and reset 0th path point.
667!--       This point (the last target the agent had) is needed for the agent's rounding of corners
668!--       and the calculation of its deviation from its current path.
669          IF ( path_flag )  THEN
670             CALL mas_nav_find_path(n)
671             pc = agents(n)%path_counter
672          ENDIF
673       ENDIF
674!
675!--    Normalize direction vector
676       abs_dir             = 1.0d-12
677       dir_x               = agents(n)%path_x(pc) - agents(n)%x
678       dir_y               = agents(n)%path_y(pc) - agents(n)%y
679       abs_dir             = SQRT( dir_x**2 + dir_y**2 )+1.0d-12
680!--      needed later for corner rounding
681!        dir_x               = dir_x/abs_dir
682!        dir_y               = dir_y/abs_dir
683!        dir_x               = dir_x + speed_round_x
684!        dir_y               = dir_y + speed_round_y
685!        abs_dir             = SQRT(dir_x**2 + dir_y**2)+1.0d-12
686       agents(n)%speed_e_x = dir_x/abs_dir
687       agents(n)%speed_e_y = dir_y/abs_dir
688    ENDDO
689
690!
691!-- corner rounding; to be added
692!
693!--       Calculate direction change due to rounding of corners
694
695!           speed_round_x = 0.
696!           speed_round_y = 0.
697!
698!           d_curr_target = SQRT( (agents(n)%path_x(pc) - agents(n)%x)**2 +      &
699!                                 (agents(n)%path_y(pc) - agents(n)%y)**2 )
700!           d_prev_target = SQRT( (agents(n)%path_x(pc-1) - agents(n)%x)**2 +    &
701!                                 (agents(n)%path_y(pc-1) - agents(n)%y)**2 )
702! !
703! !--       Agent is close to next target and that target is not the final one
704!           IF ( d_curr_target < dist_round .AND. dist_round <                   &
705!                           SQRT( (agents(n)%path_x(pc) - agents(n)%t_x)**2 +    &
706!                                 (agents(n)%path_y(pc) - agents(n)%t_y)**2 ) )  &
707!           THEN
708!              speed_round_x = (agents(n)%path_x(pc+1) - agents(n)%path_x(pc)) / &
709!                              ABS( agents(n)%path_x(pc)                         &
710!                                 - agents(n)%path_x(pc+1)) * round_fac *        &
711!                              SIN( pi/dist_round*d_curr_target )
712!              speed_round_y = (agents(n)%path_y(pc+1) - agents(n)%path_y(pc)) / &
713!                              ABS( agents(n)%path_y(pc)                         &
714!                                 - agents(n)%path_y(pc+1)) * round_fac *        &
715!                              SIN( pi/dist_round*d_curr_target )
716!           ENDIF
717!
718!           IF ( d_prev_target < dist_round )  THEN
719!              IF ( agents(n)%path_x(pc) /= agents(n)%path_x(pc+1) )  THEN
720!                 speed_round_x = speed_round_x +                                   &
721!                                 (agents(n)%path_x(pc) - agents(n)%path_x(pc+1)) / &
722!                                 ABS( agents(n)%path_x(pc)                         &
723!                                    - agents(n)%path_x(pc+1)) * round_fac *        &
724!                                 SIN( pi/dist_round*d_prev_target )
725!              ENDIF
726!
727!              IF ( agents(n)%path_y(pc) /= agents(n)%path_y(pc+1) )  THEN
728!                 speed_round_y = speed_round_y +                                   &
729!                              (agents(n)%path_y(pc) - agents(n)%path_y(pc+1)) / &
730!                              ABS( agents(n)%path_y(pc)                         &
731!                                 - agents(n)%path_y(pc+1)) * round_fac *        &
732!                              SIN( pi/dist_round*d_prev_target )
733!              ENDIF
734
735!           ENDIF
736
737
738 END SUBROUTINE mas_agent_direction
739
740!--------------------------------------------------------------------------------------------------!
741! Description:
742! ------------
743!> Boundary conditions for maximum time, target reached and out of domain
744!--------------------------------------------------------------------------------------------------!
745 SUBROUTINE mas_boundary_conds( location )
746
747    IMPLICIT NONE
748
749    CHARACTER (LEN=*) ::  location  !< Identifier
750
751    INTEGER(iwp) ::  grp  !< agent group
752    INTEGER(iwp) ::  n    !< agent number
753
754    REAL(wp) ::  dist_to_target !< distance to target
755
756    IF ( location == 'max_sim_time' )  THEN
757
758!
759!--    Delete agents that have been simulated longer than allowed
760       DO  n = 1, number_of_agents
761
762          IF ( agents(n)%age > agent_maximum_age  .AND.  agents(n)%agent_mask )  THEN
763             agents(n)%agent_mask  = .FALSE.
764             deleted_agents = deleted_agents + 1
765          ENDIF
766
767       ENDDO
768    ENDIF
769
770    IF ( location == 'target_area' )  THEN
771
772!
773!--    Delete agents that entered target region
774       DO  n = 1, number_of_agents
775          grp = agents(n)%group
776          dist_to_target = SQRT( ( agents(n)%x - at_x(grp) )**2 + ( agents(n)%y - at_y(grp) )**2 )
777          IF ( dist_to_target < dist_target_reached )  THEN
778             agents(n)%agent_mask  = .FALSE.
779             deleted_agents = deleted_agents + 1
780          ENDIF
781
782       ENDDO
783    ENDIF
784
785 END SUBROUTINE mas_boundary_conds
786
787!--------------------------------------------------------------------------------------------------!
788! Description:
789! ------------
790!> Release new agents at their respective sources
791!--------------------------------------------------------------------------------------------------!
792 SUBROUTINE mas_create_agent ( phase )
793
794    IMPLICIT  NONE
795
796    INTEGER(iwp) ::  alloc_size  !< relative increase of allocated memory for agents
797    INTEGER(iwp) ::  i           !< loop variable ( agent groups )
798    INTEGER(iwp) ::  ip          !< index variable along x
799    INTEGER(iwp) ::  jp          !< index variable along y
800    INTEGER(iwp) ::  loop_stride !< loop variable for initialization
801    INTEGER(iwp) ::  n           !< loop variable ( number of agents )
802    INTEGER(iwp) ::  new_size    !< new size of allocated memory for agents
803    INTEGER(iwp) ::  rn_side     !< index of agent path
804
805    INTEGER(iwp), INTENT(IN) ::  phase       !< mode of inititialization
806
807    INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg) ::  local_count !< start address of new agent
808    INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg) ::  local_start !< start address of new agent
809
810    LOGICAL ::  first_stride !< flag for initialization
811
812    REAL(wp) ::  pos_x       !< increment for agent position in x
813    REAL(wp) ::  pos_y       !< increment for agent position in y
814    REAL(wp) ::  rand_contr  !< dummy argument for random position
815    REAL(wp) ::  rn_side_dum !< index of agent path
816
817    TYPE(agent_type),TARGET ::  tmp_agent !< temporary agent used for initialization
818
819!
820!-- Calculate agent positions and store agent attributes, if agent is situated on this PE.
821    DO  loop_stride = 1, 2
822       first_stride = (loop_stride == 1)
823       IF ( first_stride )  THEN
824          local_count = 0           ! count number of agents
825       ELSE
826          local_count = agt_count   ! Start address of new agents
827       ENDIF
828
829       DO  i = 1, number_of_agent_groups
830
831          pos_y = ass(i)
832
833          DO WHILE ( pos_y <= asn(i) )
834
835             IF ( pos_y >= nys * dy  .AND.  pos_y <  ( nyn + 1 ) * dy  )  THEN
836
837                pos_x = asl(i)
838
839         xloop: DO WHILE ( pos_x <= asr(i) )
840
841                   IF ( pos_x >= nxl * dx  .AND.  pos_x <  ( nxr + 1) * dx )  THEN
842
843                      tmp_agent%agent_mask = .TRUE.
844                      tmp_agent%group         = i
845                      tmp_agent%id            = 0_idp
846                      tmp_agent%block_nr      = -1
847                      tmp_agent%path_counter  = 999 !SIZE(tmp_agent%path_x)
848                      tmp_agent%age           = 0.0_wp
849                      tmp_agent%age_m         = 0.0_wp
850                      tmp_agent%dt_sum        = 0.0_wp
851                      tmp_agent%clo           = -999.0_wp
852                      tmp_agent%energy_storage= 0.0_wp
853                      tmp_agent%ipt           = 99999.0_wp
854                      tmp_agent%clothing_temp = -999._wp      !< energy stored by agent (W)
855                      tmp_agent%actlev        = 134.6862_wp   !< metabolic + work energy of the person
856                      tmp_agent%age_years     = 35._wp        !< physical age of the person
857                      tmp_agent%weight        = 75._wp        !< total weight of the person (kg)
858                      tmp_agent%height        = 1.75_wp       !< height of the person (m)
859                      tmp_agent%work          = 134.6862_wp   !< workload of the agent (W)
860                      tmp_agent%sex           = 1             !< agents gender: 1 = male, 2 = female
861                      tmp_agent%force_x       = 0.0_wp
862                      tmp_agent%force_y       = 0.0_wp
863                      tmp_agent%origin_x      = pos_x
864                      tmp_agent%origin_y      = pos_y
865                      tmp_agent%speed_abs     = 0.0_wp
866                      tmp_agent%speed_e_x     = 0.0_wp
867                      tmp_agent%speed_e_y     = 0.0_wp
868                      tmp_agent%speed_des     = random_normal(desired_speed,des_sp_sig)
869                      tmp_agent%speed_x       = 0.0_wp
870                      tmp_agent%speed_y       = 0.0_wp
871                      tmp_agent%x             = pos_x
872                      tmp_agent%y             = pos_y
873                      tmp_agent%path_x        = -1.0_wp
874                      tmp_agent%path_y        = -1.0_wp
875                      tmp_agent%t_x           = - pi
876                      tmp_agent%t_y           = - pi
877!
878!--                   Determine the grid indices of the agent position
879                      ip = tmp_agent%x * ddx
880                      jp = tmp_agent%y * ddy
881!
882!--                   Give each agent its target
883                      IF ( a_rand_target(i) )  THEN
884!
885!--                      Agent shall receive random target just outside simulated area
886                         rn_side_dum = random_function(iran_agent)
887                         rn_side     = FLOOR( 4.*rn_side_dum )
888                         IF ( rn_side < 2 )  THEN
889                            IF ( rn_side == 0 )  THEN
890                               tmp_agent%t_y = -2 * dy
891                            ELSE
892                               tmp_agent%t_y = ( ny + 3 ) * dy
893                            ENDIF
894                            tmp_agent%t_x = random_function(iran_agent) * ( nx + 1 ) * dx
895                         ELSE
896                            IF ( rn_side == 2 )  THEN
897                               tmp_agent%t_x = -2 * dx
898                            ELSE
899                               tmp_agent%t_x = ( nx + 3 ) * dx
900                            ENDIF
901                            tmp_agent%t_y = random_function(iran_agent) * ( ny + 1 ) * dy
902                         ENDIF
903!
904!--                   Agent gets target of her group
905                      ELSE
906                         tmp_agent%t_x = at_x(i)
907                         tmp_agent%t_y = at_y(i)
908                      ENDIF
909
910                      local_count(jp,ip) = local_count(jp,ip) + 1
911
912                      IF ( .NOT. first_stride )  THEN
913                         grid_agents(jp,ip)%agents(local_count(jp,ip)) = tmp_agent
914                      ENDIF
915
916                   ENDIF
917
918                   pos_x = pos_x + adx(i)
919
920                ENDDO xloop
921
922             ENDIF
923
924             pos_y = pos_y + ady(i)
925
926          ENDDO
927
928       ENDDO
929
930!
931!--    Allocate or reallocate agents array to new size
932       IF ( first_stride )  THEN
933          DO  ip = nxlg, nxrg
934             DO  jp = nysg, nyng
935                IF ( phase == phase_init )  THEN
936                   IF ( local_count(jp,ip) > 0 )  THEN
937                      alloc_size = MAX( INT( local_count(jp,ip) *                                  &
938                                             ( 1.0_wp + alloc_factor_mas / 100.0_wp ) ),           &
939                                        min_nr_agent )
940                   ELSE
941                      alloc_size = min_nr_agent
942                   ENDIF
943                   ALLOCATE( grid_agents(jp,ip)%agents(1:alloc_size) )
944                   DO  n = 1, alloc_size
945                      grid_agents(jp,ip)%agents(n) = zero_agent
946                   ENDDO
947                ELSEIF ( phase == phase_release )  THEN
948                   IF ( local_count(jp,ip) > 0 )  THEN
949                      new_size   = local_count(jp,ip) + agt_count(jp,ip)
950                      alloc_size = MAX( INT( new_size *                                            &
951                                             ( 1.0_wp + alloc_factor_mas / 100.0_wp ) ),           &
952                                        min_nr_agent )
953                      IF( alloc_size > SIZE( grid_agents(jp,ip)%agents) )  THEN
954                         CALL mas_eh_realloc_agents_array(ip,jp,alloc_size)
955                      ENDIF
956                   ENDIF
957                ENDIF
958             ENDDO
959          ENDDO
960       ENDIF
961
962    ENDDO
963
964    local_start = agt_count+1
965    agt_count   = local_count
966
967!
968!-- Calculate agent IDs
969    DO  ip = nxl, nxr
970       DO  jp = nys, nyn
971          number_of_agents = agt_count(jp,ip)
972          IF ( number_of_agents <= 0 )  CYCLE
973          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
974
975          DO  n = local_start(jp,ip), number_of_agents  !only new agents
976
977             agents(n)%id = 10000_idp**2 * grid_agents(jp,ip)%id_counter + 10000_idp * jp + ip
978!
979!--          Count the number of agents that have been released before
980             grid_agents(jp,ip)%id_counter = grid_agents(jp,ip)%id_counter + 1
981
982          ENDDO
983
984       ENDDO
985    ENDDO
986
987!
988!-- Add random fluctuation to agent positions.
989    IF ( random_start_position_agents )  THEN
990       DO  ip = nxl, nxr
991          DO  jp = nys, nyn
992             number_of_agents = agt_count(jp,ip)
993             IF ( number_of_agents <= 0 )  CYCLE
994             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
995!
996!--          Move only new agents. Moreover, limit random fluctuation in order to prevent that
997!--          agents move more than one grid box, which would lead to problems concerning agent
998!--          exchange  between processors in case adx/ady are larger than dx/dy, respectively.
999             DO  n = local_start(jp,ip), number_of_agents
1000                IF ( asl(agents(n)%group) /= asr(agents(n)%group) )  THEN
1001                   rand_contr = ( random_function( iran_agent ) - 0.5_wp ) * adx(agents(n)%group)
1002                   agents(n)%x = agents(n)%x + MERGE( rand_contr, SIGN( dx, rand_contr ),          &
1003                                                      ABS( rand_contr ) < dx                       &
1004                                                    )
1005                ENDIF
1006                IF ( ass(agents(n)%group) /= asn(agents(n)%group) )  THEN
1007                   rand_contr = ( random_function( iran_agent ) - 0.5_wp ) * ady(agents(n)%group)
1008                   agents(n)%y = agents(n)%y +                                                     &
1009                                 MERGE( rand_contr, SIGN( dy, rand_contr ), ABS( rand_contr ) < dy )
1010                ENDIF
1011             ENDDO
1012!
1013!--          Delete agents that have been simulated longer than allowed
1014             CALL mas_boundary_conds( 'max_sim_time' )
1015!
1016!--          Delete agents that have reached target area
1017             CALL mas_boundary_conds( 'target_area' )
1018
1019          ENDDO
1020       ENDDO
1021!
1022!--    Exchange agents between grid cells and processors
1023       CALL mas_eh_move_agent
1024       CALL mas_eh_exchange_horiz
1025
1026    ENDIF
1027!
1028!-- In case of random_start_position_agents, delete agents identified by mas_eh_exchange_horiz and
1029!-- mas_boundary_conds. Then sort agents into blocks, which is needed for a fast interpolation of
1030!-- the LES fields on the agent position.
1031    CALL mas_ps_sort_in_subboxes
1032
1033!
1034!-- Determine the current number of agents
1035    number_of_agents = 0
1036    DO  ip = nxl, nxr
1037       DO  jp = nys, nyn
1038          number_of_agents = number_of_agents + agt_count(jp,ip)
1039       ENDDO
1040    ENDDO
1041!
1042!-- Calculate the number of agents of the total domain
1043#if defined( __parallel )
1044    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1045    CALL MPI_ALLREDUCE( number_of_agents, total_number_of_agents, 1, MPI_INTEGER, MPI_SUM, comm2d, &
1046                        ierr )
1047#else
1048    total_number_of_agents = number_of_agents
1049#endif
1050
1051    RETURN
1052
1053 END SUBROUTINE mas_create_agent
1054
1055!--------------------------------------------------------------------------------------------------!
1056! Description:
1057! ------------
1058!> Creates flags that indicate if a gridbox contains edges or corners. These flags are used for
1059!> agents to check if obstacles are close to them.
1060!--------------------------------------------------------------------------------------------------!
1061 SUBROUTINE mas_create_obstacle_flags
1062
1063    USE arrays_3d,                                                                                 &
1064        ONLY:  zw
1065
1066    IMPLICIT NONE
1067
1068    INTEGER(iwp) ::  il
1069    INTEGER(iwp) ::  jl
1070
1071    ALLOCATE( obstacle_flags(nysg:nyng,nxlg:nxrg) )
1072
1073    obstacle_flags = 0
1074
1075    DO  il = nxlg, nxrg
1076       DO  jl = nysg, nyng
1077!
1078!--       Exclude cyclic topography boundary
1079          IF ( il < 0 .OR. il > nx .OR. jl < 0 .OR. jl > ny ) CYCLE
1080!
1081!--       North edge
1082          IF ( jl < nyng )  THEN
1083             IF ( ( top_top_s(jl,il) - top_top_s(jl+1,il) ) > 1  .AND.                             &
1084                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl+1,il) ) ) > 0.51_wp )                &
1085             THEN
1086                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 0 )
1087             ENDIF
1088          ENDIF
1089!
1090!--       North right corner
1091          IF ( jl < nyng  .AND.  il < nxrg )  THEN
1092             IF ( ( top_top_s(jl,il) - top_top_s(jl+1,il)   ) > 1  .AND.                           &
1093                  ( top_top_s(jl,il) - top_top_s(jl+1,il+1) ) > 1  .AND.                           &
1094                  ( top_top_s(jl,il) - top_top_s(jl,il+1)   ) > 1  .AND.                           &
1095                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl+1,il+1) ) ) > 0.51_wp )              &
1096             THEN
1097                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 1 )
1098             ENDIF
1099          ENDIF
1100!
1101!--       Right edge
1102          IF ( il < nxrg )  THEN
1103             IF ( ( top_top_s(jl,il) - top_top_s(jl,il+1) ) > 1  .AND.                             &
1104                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl,il+1) ) ) > 0.51_wp )                &
1105             THEN
1106                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 2 )
1107             ENDIF
1108          ENDIF
1109!
1110!--       South right corner
1111          IF ( jl > nysg  .AND.  il < nxrg )  THEN
1112             IF ( ( top_top_s(jl,il) - top_top_s(jl,il+1)   ) > 1  .AND.                           &
1113                  ( top_top_s(jl,il) - top_top_s(jl-1,il+1) ) > 1  .AND.                           &
1114                  ( top_top_s(jl,il) - top_top_s(jl-1,il)   ) > 1  .AND.                           &
1115                  ( zw(top_top_w(jl,il) ) - zw( top_top_w(jl-1,il+1) ) ) > 0.51_wp )               &
1116             THEN
1117                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 3 )
1118             ENDIF
1119          ENDIF
1120!
1121!--       South edge
1122          IF ( jl > nysg )  THEN
1123             IF ( ( top_top_s(jl,il) - top_top_s(jl-1,il) ) > 1  .AND.                             &
1124                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl-1,il) ) ) > 0.51_wp )                &
1125             THEN
1126                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 4 )
1127             ENDIF
1128          ENDIF
1129!
1130!--       South left corner
1131          IF ( jl > nysg  .AND.  il > nxlg )  THEN
1132             IF ( ( top_top_s(jl,il) - top_top_s(jl-1,il)   ) > 1  .AND.                           &
1133                  ( top_top_s(jl,il) - top_top_s(jl-1,il-1) ) > 1  .AND.                           &
1134                  ( top_top_s(jl,il) - top_top_s(jl,il-1)   ) > 1  .AND.                           &
1135                  ( zw( top_top_w(jl,il) ) - zw(top_top_w(jl-1,il-1) ) ) > 0.51_wp )               &
1136             THEN
1137                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 5 )
1138             ENDIF
1139          ENDIF
1140!
1141!--       Left edge
1142          IF ( il > nxlg )  THEN
1143             IF ( ( top_top_s(jl,il) - top_top_s(jl,il-1) ) > 1  .AND.                             &
1144                  ( zw(top_top_w(jl,il) ) - zw(top_top_w(jl,il-1) ) ) > 0.51_wp )                  &
1145             THEN
1146                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 6 )
1147             ENDIF
1148          ENDIF
1149!
1150!--       North left corner
1151          IF ( jl < nyng  .AND.  il > nxlg )  THEN
1152             IF ( ( top_top_s(jl,il) - top_top_s(jl,il-1)   ) > 1  .AND.                           &
1153                  ( top_top_s(jl,il) - top_top_s(jl+1,il-1) ) > 1  .AND.                           &
1154                  ( top_top_s(jl,il) - top_top_s(jl+1,il)   ) > 1  .AND.                           &
1155                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl+1,il-1) ) ) > 0.51_wp )              &
1156             THEN
1157                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 7 )
1158             ENDIF
1159          ENDIF
1160
1161       ENDDO
1162    ENDDO
1163
1164 END SUBROUTINE mas_create_obstacle_flags
1165
1166!--------------------------------------------------------------------------------------------------!
1167! Description:
1168! ------------
1169!> Write agent data in netCDF format
1170!--------------------------------------------------------------------------------------------------!
1171 SUBROUTINE mas_data_output_agents( ftest )
1172
1173    USE control_parameters,                                                                        &
1174        ONLY:  agt_time_count, biometeorology, end_time, message_string, multi_agent_system_end,   &
1175               multi_agent_system_start
1176
1177    USE netcdf_interface,                                                                          &
1178        ONLY:  nc_stat, id_set_agt, id_var_time_agt, id_var_agt, netcdf_handle_error
1179
1180    USE pegrid
1181
1182#if defined( __netcdf )
1183    USE NETCDF
1184#endif
1185    USE mas_global_attributes,                                                                     &
1186        ONLY:  dim_size_agtnum
1187
1188    IMPLICIT NONE
1189
1190#if defined( __parallel )
1191    INTEGER(iwp) ::  agt_size !< Agent size in bytes
1192    INTEGER(iwp) ::  n        !< counter (number of PEs)
1193    INTEGER(iwp) ::  noa_rcv  !< received number of agents
1194#endif
1195    INTEGER(iwp) ::  dummy    !< dummy
1196    INTEGER(iwp) ::  ii       !< counter (x)
1197    INTEGER(iwp) ::  ip       !< counter (x)
1198    INTEGER(iwp) ::  jp       !< counter (y)
1199    INTEGER(iwp) ::  noa      !< number of agents
1200    INTEGER(iwp) ::  out_noa  !< number of agents for output
1201
1202#if defined( __parallel )
1203    INTEGER(iwp), DIMENSION(0:numprocs-1) ::  noa_arr !< number of agents on each PE
1204#endif
1205!
1206!-- SAVE attribute required to avoid compiler warning about pointer outlive the pointer target
1207    TYPE(agent_type), DIMENSION(:), ALLOCATABLE, TARGET, SAVE ::  trf_agents !< all agents on current PE
1208#if defined( __parallel )
1209    TYPE(agent_type), DIMENSION(:), ALLOCATABLE, TARGET, SAVE ::  out_agents !< all agents in entire domain
1210#endif
1211
1212    LOGICAL, INTENT (INOUT) :: ftest
1213
1214    LOGICAL, SAVE :: agt_dimension_exceeded = .FALSE.
1215
1216
1217    CALL cpu_log( log_point_s(17), 'mas_data_output', 'start' )
1218!
1219!-- Get total number of agents and put all agents on one PE in one array
1220    noa = 0
1221    DO  ip = nxl, nxr
1222       DO  jp = nys, nyn
1223          noa  = noa  + agt_count(jp,ip)
1224       ENDDO
1225    ENDDO
1226    IF ( noa > 0 )  THEN
1227       ALLOCATE( trf_agents(1:noa) )
1228       dummy = 1
1229       DO  ip = nxl, nxr
1230          DO  jp = nys, nyn
1231             IF ( agt_count(jp,ip) == 0 )  CYCLE
1232             agents => grid_agents(jp,ip)%agents(1:agt_count(jp,ip))
1233             trf_agents(dummy:(dummy-1+agt_count(jp,ip))) = agents
1234             dummy = dummy + agt_count(jp,ip)
1235          ENDDO
1236       ENDDO
1237    ENDIF
1238#if defined( __parallel )
1239!
1240!-- Gather all agents on PE0 for output
1241    IF ( myid == 0 )  THEN
1242       noa_arr(0) = noa
1243!
1244!--    Receive data from all other PEs.
1245       DO  n = 1, numprocs-1
1246           CALL MPI_RECV( noa_arr(n), 1, MPI_INTEGER, n, 0, comm2d, status, ierr )
1247       ENDDO
1248    ELSE
1249       CALL MPI_SEND( noa, 1, MPI_INTEGER, 0, 0, comm2d, ierr )
1250    ENDIF
1251    CALL MPI_BARRIER( comm2d, ierr )
1252    agt_size = STORAGE_SIZE( zero_agent ) / 8
1253    IF ( myid == 0 )  THEN
1254!
1255!--    Receive data from all other PEs.
1256       out_noa = SUM( noa_arr )
1257       IF ( out_noa > 0 )  THEN
1258          ALLOCATE( out_agents(1:out_noa) )
1259          IF ( noa > 0 )  THEN
1260             out_agents(1:noa) = trf_agents
1261          ENDIF
1262          noa_rcv = noa
1263          DO  n = 1, numprocs-1
1264             IF ( noa_arr(n) > 0 )  THEN
1265                CALL MPI_RECV( out_agents(noa_rcv+1), noa_arr(n) * agt_size, MPI_BYTE, n, 0,       &
1266                               comm2d, status, ierr )
1267                noa_rcv = noa_rcv + noa_arr(n)
1268             ENDIF
1269          ENDDO
1270       ELSE
1271          ALLOCATE( out_agents(1:2) )
1272          out_agents = zero_agent
1273          out_noa    = 2
1274       ENDIF
1275    ELSE
1276       IF ( noa > 0 )  THEN
1277          CALL MPI_SEND( trf_agents(1), noa*agt_size, MPI_BYTE, 0, 0, comm2d, ierr )
1278       ENDIF
1279    ENDIF
1280!
1281!-- A barrier has to be set, because otherwise some PEs may proceed too fast so that PE0 may receive
1282!-- wrong data on tag 0.
1283    CALL MPI_BARRIER( comm2d, ierr )
1284#endif
1285    IF ( myid == 0 )  THEN
1286#if defined( __parallel )
1287       agents => out_agents
1288#else
1289       agents => trf_agents
1290#endif
1291
1292#if defined( __netcdf )
1293!
1294!--    Update maximum number of agents
1295       maximum_number_of_agents = MAX(maximum_number_of_agents, out_noa)
1296!
1297!--       Output in netCDF format
1298       IF ( ftest )  THEN
1299!
1300!--          First, define size of agent number dimension from amount of agents released, release
1301!--          interval, time of agent simulation and max age of agents.
1302          dim_size_agtnum = MIN( MIN( multi_agent_system_end, end_time )                           &
1303                                    - multi_agent_system_start,                                    &
1304                                 agent_maximum_age)
1305
1306          DO ii = 1, number_of_agent_groups
1307             dim_size_agtnum = dim_size_agtnum                                                     &
1308                                + ( FLOOR( ( asr(ii)-asl(ii) ) / adx(ii) ) + 1 )                   &
1309                                * ( FLOOR( ( asn(ii)-ass(ii) ) / ady(ii) ) + 1 )                   &
1310                                * ( FLOOR( dim_size_agtnum / dt_arel )     + 1 )                   &
1311                                * dim_size_factor_agtnum
1312             dim_size_agtnum = MIN( dim_size_agtnum, dim_size_agtnum_manual )
1313          ENDDO
1314          CALL check_open( 118 )
1315       ENDIF
1316
1317!
1318!--    Update the NetCDF time axis
1319       agt_time_count = agt_time_count + 1
1320
1321       IF ( .NOT. agt_dimension_exceeded )  THEN
1322!
1323!--       If number of agents to be output exceeds dimension, set flag and print warning.
1324          IF ( out_noa > dim_size_agtnum )  THEN
1325
1326             agt_dimension_exceeded = .TRUE.
1327             WRITE( message_string, '(A,F11.1,2(A,I8))' )                                          &
1328                                                  'Number of agents exceeds agent dimension.' //   &
1329                                                  '&Starting at time_since_reference_point = ',    &
1330                                                  time_since_reference_point,                      &
1331                                                  ' s, &data may be missing.'//                    &
1332                                                  '&Number of agents:     ', out_noa,              &
1333                                                  '&Agent dimension size: ', dim_size_agtnum
1334
1335             CALL message( 'mas_data_output_agents', 'PA0420', 0, 1, 0, 6, 0 )
1336
1337          ENDIF
1338       ENDIF
1339
1340!
1341!--    Reduce number of output agents to dimension size, if necessary.
1342       IF ( agt_dimension_exceeded )  THEN
1343
1344          out_noa = MIN( out_noa, dim_size_agtnum )
1345
1346       ENDIF
1347
1348       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_time_agt,                                        &
1349                               (/ time_since_reference_point + agent_substep_time /),              &
1350                               start = (/ agt_time_count /),                                       &
1351                               count = (/ 1 /) )
1352       CALL netcdf_handle_error( 'mas_data_output_agents', 1 )
1353
1354!
1355!--       Output agent attributes
1356
1357       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(1), agents%id,                               &
1358                               start = (/ 1, agt_time_count /),                                    &
1359                               count = (/ out_noa /) )
1360       CALL netcdf_handle_error( 'mas_data_output_agents', 2 )
1361
1362       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(2), agents%x,                                &
1363                               start = (/ 1, agt_time_count /),                                    &
1364                               count = (/ out_noa /) )
1365       CALL netcdf_handle_error( 'mas_data_output_agents', 3 )
1366
1367       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(3), agents%y,                                &
1368                               start = (/ 1, agt_time_count /),                                    &
1369                               count = (/ out_noa /) )
1370       CALL netcdf_handle_error( 'mas_data_output_agents', 4 )
1371
1372       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(4), agents%windspeed,                        &
1373                               start = (/ 1, agt_time_count /),                                    &
1374                               count = (/ out_noa /) )
1375       CALL netcdf_handle_error( 'mas_data_output_agents', 5 )
1376
1377       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(5), agents%t,                                &
1378                               start = (/ 1, agt_time_count /),                                    &
1379                               count = (/ out_noa /) )
1380       CALL netcdf_handle_error( 'mas_data_output_agents', 6 )
1381
1382       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(6), agents%group,                            &
1383                               start = (/ 1, agt_time_count /),                                    &
1384                               count = (/ out_noa /) )
1385       CALL netcdf_handle_error( 'mas_data_output_agents', 7 )
1386
1387
1388       IF ( biometeorology )  THEN
1389          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(7), agents%ipt,                           &
1390                                  start = (/ 1, agt_time_count /),                                 &
1391                                  count = (/ out_noa /) )
1392          CALL netcdf_handle_error( 'mas_data_output_agents', 8 )
1393       ENDIF
1394
1395
1396
1397!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(8), agents%pm10,                         &
1398!                                   start = (/ 1, agt_time_count /),                                &
1399!                                   count = (/ out_noa /) )
1400!           CALL netcdf_handle_error( 'mas_data_output_agents', 9 )
1401!
1402!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(9), agents%pm25,                         &
1403!                                   start = (/ 1, agt_time_count /),                                &
1404!                                   count = (/ out_noa /) )
1405!           CALL netcdf_handle_error( 'mas_data_output_agents', 10 )
1406!
1407!
1408!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(9), agents%uv,                           &
1409!                                   start = (/ 1, agt_time_count /),                                &
1410!                                   count = (/ out_noa /) )
1411!           CALL netcdf_handle_error( 'mas_data_output_agents', 10 )
1412
1413       CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
1414
1415
1416#endif
1417
1418#if defined( __parallel )
1419       IF ( ALLOCATED( out_agents ) ) DEALLOCATE( out_agents )
1420#endif
1421    ELSE
1422       CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
1423    ENDIF
1424
1425    IF ( ALLOCATED( trf_agents ) ) DEALLOCATE( trf_agents )
1426
1427 END SUBROUTINE mas_data_output_agents
1428
1429#if defined( __parallel )
1430!--------------------------------------------------------------------------------------------------!
1431! Description:
1432! ------------
1433!> If an agent moves from one processor to another, this subroutine moves the corresponding elements
1434!> from the agent arrays of the old grid cells to the agent arrays of the new grid cells.
1435!--------------------------------------------------------------------------------------------------!
1436 SUBROUTINE mas_eh_add_agents_to_gridcell (agent_array)
1437
1438    IMPLICIT NONE
1439
1440    INTEGER(iwp) ::  aindex  !< dummy argument for new number of agents per grid box
1441    INTEGER(iwp) ::  ip      !< grid index (x) of agent
1442    INTEGER(iwp) ::  jp      !< grid index (x) of agent
1443    INTEGER(iwp) ::  n       !< index variable of agent
1444
1445    LOGICAL ::  pack_done  !< flag to indicate that packing is done
1446
1447    TYPE(agent_type), DIMENSION(:), INTENT(IN)  ::  agent_array  !< new agents in a grid box
1448    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  temp_ns      !< temporary agent array for reallocation
1449
1450    pack_done     = .FALSE.
1451
1452    DO  n = 1, SIZE(agent_array)
1453
1454       IF ( .NOT. agent_array(n)%agent_mask )  CYCLE
1455
1456       ip = agent_array(n)%x * ddx
1457       jp = agent_array(n)%y * ddy
1458
1459       IF ( ip >= nxl  .AND.  ip <= nxr  .AND.  jp >= nys  .AND.  jp <= nyn )  THEN ! agent stays on processor
1460          number_of_agents = agt_count(jp,ip)
1461          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
1462
1463          aindex = agt_count(jp,ip)+1
1464          IF( aindex > SIZE( grid_agents(jp,ip)%agents ) )  THEN
1465             IF ( pack_done )  THEN
1466                CALL mas_eh_realloc_agents_array (ip,jp)
1467             ELSE
1468                CALL mas_ps_pack
1469                agt_count(jp,ip) = number_of_agents
1470                aindex = agt_count(jp,ip)+1
1471                IF ( aindex > SIZE( grid_agents(jp,ip)%agents ) )  THEN
1472                   CALL mas_eh_realloc_agents_array (ip,jp)
1473                ENDIF
1474                pack_done = .TRUE.
1475             ENDIF
1476          ENDIF
1477          grid_agents(jp,ip)%agents(aindex) = agent_array(n)
1478          agt_count(jp,ip) = aindex
1479       ELSE
1480          IF ( jp <= nys - 1 )  THEN
1481             nr_move_south = nr_move_south+1
1482!
1483!--          Before agent information is swapped to exchange-array, check if enough memory is
1484!--          allocated. If required, reallocate exchange array.
1485             IF ( nr_move_south > SIZE( move_also_south ) )  THEN
1486!
1487!--             At first, allocate further temporary array to swap agent information.
1488                ALLOCATE( temp_ns( SIZE( move_also_south ) + nr_2_direction_move ) )
1489                temp_ns(1:nr_move_south-1) = move_also_south(1:nr_move_south-1)
1490                DEALLOCATE( move_also_south )
1491                ALLOCATE( move_also_south( SIZE(temp_ns) ) )
1492                move_also_south(1:nr_move_south-1) = temp_ns(1:nr_move_south-1)
1493                DEALLOCATE( temp_ns )
1494
1495             ENDIF
1496
1497             move_also_south(nr_move_south) = agent_array(n)
1498
1499             IF ( jp == -1 )  THEN
1500!
1501!--             Apply boundary condition along y
1502                IF ( ibc_mas_ns == 0 )  THEN
1503                   move_also_south(nr_move_south)%y = move_also_south(nr_move_south)%y             &
1504                                                      + ( ny + 1 ) * dy
1505                   move_also_south(nr_move_south)%origin_y =                                       &
1506                                                         move_also_south(nr_move_south)%origin_y   &
1507                                                         + ( ny + 1 ) * dy
1508                ELSEIF ( ibc_mas_ns == 1 )  THEN
1509!
1510!--                Agent absorption
1511                   move_also_south(nr_move_south)%agent_mask = .FALSE.
1512                   deleted_agents = deleted_agents + 1
1513
1514                ENDIF
1515             ENDIF
1516          ELSEIF ( jp >= nyn+1 )  THEN
1517             nr_move_north = nr_move_north+1
1518!
1519!--          Before agent information is swapped to exchange-array, check  if enough memory is
1520!--          allocated. If required, reallocate exchange array.
1521             IF ( nr_move_north > SIZE( move_also_north ) )  THEN
1522!
1523!--             At first, allocate further temporary array to swap agent information.
1524                ALLOCATE( temp_ns( SIZE( move_also_north ) + nr_2_direction_move ) )
1525                temp_ns(1:nr_move_north-1) = move_also_south(1:nr_move_north-1)
1526                DEALLOCATE( move_also_north )
1527                ALLOCATE( move_also_north(SIZE(temp_ns)) )
1528                move_also_north(1:nr_move_north-1) = temp_ns(1:nr_move_north-1)
1529                DEALLOCATE( temp_ns )
1530
1531             ENDIF
1532
1533             move_also_north(nr_move_north) = agent_array(n)
1534             IF ( jp == ny+1 )  THEN
1535!
1536!--             Apply boundary condition along y
1537                IF ( ibc_mas_ns == 0 )  THEN
1538
1539                   move_also_north(nr_move_north)%y = move_also_north(nr_move_north)%y             &
1540                                                      - ( ny + 1 ) * dy
1541                   move_also_north(nr_move_north)%origin_y =                                       &
1542                                                        move_also_north(nr_move_north)%origin_y    &
1543                                                        - ( ny + 1 ) * dy
1544                ELSEIF ( ibc_mas_ns == 1 )  THEN
1545!
1546!--                Agent absorption
1547                   move_also_north(nr_move_north)%agent_mask = .FALSE.
1548                   deleted_agents = deleted_agents + 1
1549
1550                ENDIF
1551             ENDIF
1552          ENDIF
1553       ENDIF
1554    ENDDO
1555
1556    RETURN
1557
1558 END SUBROUTINE mas_eh_add_agents_to_gridcell
1559#endif
1560
1561
1562#if defined( __parallel )
1563!--------------------------------------------------------------------------------------------------!
1564! Description:
1565! ------------
1566!> After ghost layer agents have been received from neighboring PEs, this subroutine sorts them into
1567!> the corresponding grid cells
1568!--------------------------------------------------------------------------------------------------!
1569 SUBROUTINE mas_eh_add_ghost_agents_to_gridcell (agent_array)
1570
1571    IMPLICIT NONE
1572
1573    INTEGER(iwp) ::  aindex  !< dummy argument for new number of agents per grid box
1574    INTEGER(iwp) ::  ip      !< grid index (x) of agent
1575    INTEGER(iwp) ::  jp      !< grid index (x) of agent
1576    INTEGER(iwp) ::  n       !< index variable of agent
1577
1578    LOGICAL ::  pack_done  !< flag to indicate that packing is done
1579
1580    TYPE(agent_type), DIMENSION(:), INTENT(IN)  ::  agent_array  !< new agents in a grid box
1581
1582    pack_done     = .FALSE.
1583
1584    DO  n = 1, SIZE(agent_array)
1585
1586       IF ( .NOT. agent_array(n)%agent_mask )  CYCLE
1587
1588       ip = agent_array(n)%x * ddx
1589       jp = agent_array(n)%y * ddy
1590
1591       IF ( ip < nxl  .OR.  ip > nxr  .OR.  jp < nys  .OR.  jp > nyn )  THEN
1592          number_of_agents = agt_count(jp,ip)
1593          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
1594
1595          aindex = agt_count(jp,ip)+1
1596          IF( aindex > SIZE( grid_agents(jp,ip)%agents ) )  THEN
1597             IF ( pack_done )  THEN
1598                CALL mas_eh_realloc_agents_array (ip,jp)
1599             ELSE
1600                CALL mas_ps_pack
1601                agt_count(jp,ip) = number_of_agents
1602                aindex = agt_count(jp,ip)+1
1603                IF ( aindex > SIZE( grid_agents(jp,ip)%agents ) )  THEN
1604                   CALL mas_eh_realloc_agents_array (ip,jp)
1605                ENDIF
1606                pack_done = .TRUE.
1607             ENDIF
1608          ENDIF
1609          grid_agents(jp,ip)%agents(aindex) = agent_array(n)
1610          agt_count(jp,ip) = aindex
1611       ENDIF
1612    ENDDO
1613 END SUBROUTINE mas_eh_add_ghost_agents_to_gridcell
1614#endif
1615
1616!--------------------------------------------------------------------------------------------------!
1617! Description:
1618! ------------
1619!> Resizing of agent arrays
1620!--------------------------------------------------------------------------------------------------!
1621 SUBROUTINE mas_eh_dealloc_agents_array
1622
1623    IMPLICIT NONE
1624
1625    INTEGER(iwp) ::  i         !< grid index (x) of agent
1626    INTEGER(iwp) ::  j         !< grid index (y) of agent
1627    INTEGER(iwp) ::  new_size  !< new array size
1628    INTEGER(iwp) ::  noa       !< number of agents
1629    INTEGER(iwp) ::  old_size  !< old array size
1630
1631    LOGICAL ::  dealloc  !< flag that indicates if reallocation is necessary
1632
1633    TYPE(agent_type), DIMENSION(10) ::  tmp_agents_s  !< temporary static agent array
1634
1635    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  tmp_agents_d  !< temporary dynamic agent array
1636
1637    DO  i = nxlg, nxrg
1638       DO  j = nysg, nyng
1639!
1640!--       Determine number of active agents
1641          noa = agt_count(j,i)
1642!
1643!--       Determine allocated memory size
1644          old_size = SIZE( grid_agents(j,i)%agents )
1645!
1646!--       Check for large unused memory
1647          dealloc = ( ( noa < min_nr_agent .AND. old_size  > min_nr_agent )  .OR.                  &
1648                      ( noa > min_nr_agent .AND.                                                   &
1649                        old_size - noa * ( 1.0_wp + 0.01_wp * alloc_factor_mas ) > 0.0_wp )        &
1650                    )
1651!
1652!--       If large unused memory was found, resize the corresponding array
1653          IF ( dealloc )  THEN
1654             IF ( noa < min_nr_agent )  THEN
1655                new_size = min_nr_agent
1656             ELSE
1657                new_size = INT( noa * ( 1.0_wp + 0.01_wp * alloc_factor_mas ) )
1658             ENDIF
1659
1660             IF ( noa <= 10 )  THEN
1661
1662                tmp_agents_s(1:noa) = grid_agents(j,i)%agents(1:noa)
1663
1664                DEALLOCATE(grid_agents(j,i)%agents)
1665                ALLOCATE(grid_agents(j,i)%agents(1:new_size))
1666
1667                grid_agents(j,i)%agents(1:noa)          = tmp_agents_s(1:noa)
1668                grid_agents(j,i)%agents(noa+1:new_size) = zero_agent
1669
1670             ELSE
1671
1672                ALLOCATE(tmp_agents_d(noa))
1673                tmp_agents_d(1:noa) = grid_agents(j,i)%agents(1:noa)
1674
1675                DEALLOCATE(grid_agents(j,i)%agents)
1676                ALLOCATE(grid_agents(j,i)%agents(new_size))
1677
1678                grid_agents(j,i)%agents(1:noa)          = tmp_agents_d(1:noa)
1679                grid_agents(j,i)%agents(noa+1:new_size) = zero_agent
1680
1681                DEALLOCATE(tmp_agents_d)
1682
1683             ENDIF
1684
1685          ENDIF
1686       ENDDO
1687    ENDDO
1688
1689 END SUBROUTINE mas_eh_dealloc_agents_array
1690
1691!--------------------------------------------------------------------------------------------------!
1692! Description:
1693! ------------
1694!> Exchange between subdomains.
1695!> As soon as one agent has moved beyond the boundary of the domain, it is included in the relevant
1696!> transfer arrays and marked for subsequent deletion on this PE.
1697!> First sweep for crossings in x direction. Find out first the number of agents to be transferred
1698!> and allocate temporary arrays needed to store them.
1699!> For a one-dimensional decomposition along y, no transfer is necessary, because the agent remains
1700!> on the PE, but the agent coordinate has to be adjusted.
1701!--------------------------------------------------------------------------------------------------!
1702 SUBROUTINE mas_eh_exchange_horiz
1703
1704    IMPLICIT NONE
1705
1706    INTEGER(iwp) ::  ip               !< index variable along x
1707    INTEGER(iwp) ::  jp               !< index variable along y
1708    INTEGER(iwp) ::  n                !< agent index variable
1709
1710#if defined( __parallel )
1711
1712    INTEGER(iwp) ::  i                !< grid index (x) of agent positition
1713    INTEGER(iwp) ::  j                !< grid index (y) of agent positition
1714    INTEGER(iwp) ::  par_size         !< Agent size in bytes
1715
1716    INTEGER(iwp) ::  trla_count       !< number of agents send to left PE
1717    INTEGER(iwp) ::  trla_count_recv  !< number of agents receive from right PE
1718    INTEGER(iwp) ::  trna_count       !< number of agents send to north PE
1719    INTEGER(iwp) ::  trna_count_recv  !< number of agents receive from south PE
1720    INTEGER(iwp) ::  trra_count       !< number of agents send to right PE
1721    INTEGER(iwp) ::  trra_count_recv  !< number of agents receive from left PE
1722    INTEGER(iwp) ::  trsa_count       !< number of agents send to south PE
1723    INTEGER(iwp) ::  trsa_count_recv  !< number of agents receive from north PE
1724
1725    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvla  !< agents received from right PE
1726    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvna  !< agents received from south PE
1727    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvra  !< agents received from left PE
1728    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvsa  !< agents received from north PE
1729    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trla  !< agents send to left PE
1730    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trna  !< agents send to north PE
1731    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trra  !< agents send to right PE
1732    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trsa  !< agents send to south PE
1733
1734!
1735!-- Exchange between subdomains.
1736!-- As soon as one agent has moved beyond the boundary of the domain, it is included in the relevant
1737!-- transfer arrays and marked for subsequent deletion on this PE.
1738!-- First sweep for crossings in x direction. Find out first the number of agents to be transferred
1739!-- and allocate temporary arrays needed to store them.
1740!-- For a one-dimensional decomposition along y, no transfer is necessary, because the agent remains
1741!-- on the PE, but the agent coordinate has to be adjusted.
1742    trla_count  = 0
1743    trra_count  = 0
1744
1745    trla_count_recv   = 0
1746    trra_count_recv   = 0
1747
1748    IF ( pdims(1) /= 1 )  THEN
1749!
1750!--    First calculate the storage necessary for sending and receiving the data.
1751!--    Compute only first (nxl) and last (nxr) loop iterration.
1752       DO  ip = nxl, nxr, nxr - nxl
1753          DO  jp = nys, nyn
1754
1755             number_of_agents = agt_count(jp,ip)
1756             IF ( number_of_agents <= 0 )  CYCLE
1757             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
1758             DO  n = 1, number_of_agents
1759                IF ( agents(n)%agent_mask )  THEN
1760                   i = agents(n)%x * ddx
1761!
1762!--                Above calculation does not work for indices less than zero
1763                   IF ( agents(n)%x < 0.0_wp )  i = -1
1764
1765                   IF ( i < nxl )  THEN
1766                      trla_count = trla_count + 1
1767                   ELSEIF ( i > nxr )  THEN
1768                      trra_count = trra_count + 1
1769                   ENDIF
1770                ENDIF
1771             ENDDO
1772
1773          ENDDO
1774       ENDDO
1775
1776       IF ( trla_count  == 0 )  trla_count  = 1
1777       IF ( trra_count  == 0 )  trra_count  = 1
1778
1779       ALLOCATE( trla(trla_count), trra(trra_count) )
1780
1781       trla = zero_agent
1782       trra = zero_agent
1783
1784       trla_count  = 0
1785       trra_count  = 0
1786
1787    ENDIF
1788!
1789!-- Compute only first (nxl) and last (nxr) loop iterration
1790    DO  ip = nxl, nxr, nxr-nxl
1791       DO  jp = nys, nyn
1792          number_of_agents = agt_count(jp,ip)
1793          IF ( number_of_agents <= 0 ) CYCLE
1794          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
1795          DO  n = 1, number_of_agents
1796!
1797!--          Only those agents that have not been marked as 'deleted' may be moved.
1798             IF ( agents(n)%agent_mask )  THEN
1799
1800                i = agents(n)%x * ddx
1801!
1802!--             Above calculation does not work for indices less than zero
1803                IF ( agents(n)%x < 0.0_wp )  i = -1
1804
1805                IF ( i <  nxl )  THEN
1806                   IF ( i < 0 )  THEN
1807!
1808!--                   Apply boundary condition along x
1809                      IF ( ibc_mas_lr == 0 )  THEN
1810!
1811!--                      Cyclic condition
1812                         IF ( pdims(1) == 1 )  THEN
1813                            agents(n)%x        = ( nx + 1 ) * dx + agents(n)%x
1814                            agents(n)%origin_x = ( nx + 1 ) * dx + agents(n)%origin_x
1815                         ELSE
1816                            trla_count         = trla_count + 1
1817                            trla(trla_count)   = agents(n)
1818                            trla(trla_count)%x = ( nx + 1 ) * dx + trla(trla_count)%x
1819                            trla(trla_count)%origin_x = trla(trla_count)%origin_x + ( nx + 1 ) * dx
1820                            agents(n)%agent_mask  = .FALSE.
1821                            deleted_agents = deleted_agents + 1
1822
1823                            IF ( trla(trla_count)%x >= (nx + 1)* dx - 1.0E-12_wp )  THEN
1824                               trla(trla_count)%x = trla(trla_count)%x - 1.0E-10_wp
1825                               trla(trla_count)%origin_x = trla(trla_count)%origin_x - 1
1826                            ENDIF
1827
1828                         ENDIF
1829
1830                      ELSEIF ( ibc_mas_lr == 1 )  THEN
1831!
1832!--                      Agent absorption
1833                         agents(n)%agent_mask = .FALSE.
1834                         deleted_agents = deleted_agents + 1
1835
1836                      ENDIF
1837                   ELSE
1838!
1839!--                   Store agent data in the transfer array, which will be send to the neighbouring
1840!--                   PE.
1841                      trla_count = trla_count + 1
1842                      trla(trla_count) = agents(n)
1843                      agents(n)%agent_mask = .FALSE.
1844                      deleted_agents = deleted_agents + 1
1845
1846                   ENDIF
1847
1848                ELSEIF ( i > nxr )  THEN
1849                   IF ( i > nx )  THEN
1850!
1851!--                   Apply boundary condition along x
1852                      IF ( ibc_mas_lr == 0 )  THEN
1853!
1854!--                      Cyclic condition
1855                         IF ( pdims(1) == 1 )  THEN
1856                            agents(n)%x = agents(n)%x - ( nx + 1 ) * dx
1857                            agents(n)%origin_x = agents(n)%origin_x - ( nx + 1 ) * dx
1858                         ELSE
1859                            trra_count = trra_count + 1
1860                            trra(trra_count) = agents(n)
1861                            trra(trra_count)%x = trra(trra_count)%x - ( nx + 1 ) * dx
1862                            trra(trra_count)%origin_x = trra(trra_count)%origin_x - ( nx + 1 ) * dx
1863                            agents(n)%agent_mask = .FALSE.
1864                            deleted_agents = deleted_agents + 1
1865
1866                         ENDIF
1867
1868                      ELSEIF ( ibc_mas_lr == 1 )  THEN
1869!
1870!--                      Agent absorption
1871                         agents(n)%agent_mask = .FALSE.
1872                         deleted_agents = deleted_agents + 1
1873
1874                      ENDIF
1875                   ELSE
1876!
1877!--                   Store agent data in the transfer array, which will be send to the neighbouring
1878!--                   PE.
1879                      trra_count = trra_count + 1
1880                      trra(trra_count) = agents(n)
1881                      agents(n)%agent_mask = .FALSE.
1882                      deleted_agents = deleted_agents + 1
1883
1884                   ENDIF
1885
1886                ENDIF
1887             ENDIF
1888
1889          ENDDO
1890       ENDDO
1891    ENDDO
1892
1893!
1894!-- Allocate arrays required for north-south exchange, as these are used directly after agents are
1895!-- exchange along x-direction.
1896    ALLOCATE( move_also_north(1:nr_2_direction_move) )
1897    ALLOCATE( move_also_south(1:nr_2_direction_move) )
1898
1899    nr_move_north = 0
1900    nr_move_south = 0
1901!
1902!-- Send left boundary, receive right boundary (but first exchange how many and check if agent
1903!-- storage must be extended).
1904    IF ( pdims(1) /= 1 )  THEN
1905
1906       CALL MPI_SENDRECV( trla_count,      1, MPI_INTEGER, pleft,  0,                              &
1907                          trra_count_recv, 1, MPI_INTEGER, pright, 0,                              &
1908                          comm2d, status, ierr )
1909
1910       ALLOCATE( rvra(MAX( 1,trra_count_recv )) )
1911!
1912!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit variables in structure
1913!--    agent_type (due to the calculation of par_size)
1914       par_size = STORAGE_SIZE( trla(1) ) / 8
1915       CALL MPI_SENDRECV( trla, MAX( 1, trla_count ) * par_size,     MPI_BYTE, pleft,  1,          &
1916                          rvra, MAX( 1, trra_count_recv )* par_size, MPI_BYTE, pright, 1,          &
1917                          comm2d, status, ierr )
1918
1919       IF ( trra_count_recv > 0 )  THEN
1920          CALL mas_eh_add_agents_to_gridcell(rvra(1:trra_count_recv))
1921       ENDIF
1922
1923       DEALLOCATE(rvra)
1924
1925!
1926!--    Send right boundary, receive left boundary
1927       CALL MPI_SENDRECV( trra_count,      1, MPI_INTEGER, pright, 0,                              &
1928                          trla_count_recv, 1, MPI_INTEGER, pleft,  0,                              &
1929                          comm2d, status, ierr )
1930
1931       ALLOCATE(rvla(MAX(1,trla_count_recv)))
1932!
1933!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit variables in structure
1934!--    agent_type (due to the calculation of par_size)
1935       par_size = STORAGE_SIZE(trra(1))/8
1936       CALL MPI_SENDRECV( trra, MAX( 1, trra_count ) * par_size,   MPI_BYTE, pright, 1,            &
1937                          rvla, MAX(1,trla_count_recv) * par_size, MPI_BYTE,  pleft, 1,            &
1938                          comm2d, status, ierr )
1939
1940       IF ( trla_count_recv > 0 )  THEN
1941          CALL mas_eh_add_agents_to_gridcell(rvla(1:trla_count_recv))
1942       ENDIF
1943
1944       DEALLOCATE( rvla )
1945       DEALLOCATE( trla, trra )
1946
1947    ENDIF
1948
1949!
1950!-- Check whether agents have crossed the boundaries in y direction. Note that this case can also
1951!-- apply to agents that have just been received from the adjacent right or left PE.
1952!-- Find out first the number of agents to be transferred and allocate temporary arrays needed to
1953!-- store them.
1954!-- For a one-dimensional decomposition along y, no transfer is necessary, because the agent remains
1955!-- on the PE.
1956    trsa_count  = nr_move_south
1957    trna_count  = nr_move_north
1958
1959    trsa_count_recv   = 0
1960    trna_count_recv   = 0
1961
1962    IF ( pdims(2) /= 1 )  THEN
1963!
1964!--    First calculate the storage necessary for sending and receiving the data.
1965       DO  ip = nxl, nxr
1966          DO  jp = nys, nyn, nyn-nys    ! compute only first (nys) and last (nyn) loop iterration
1967             number_of_agents = agt_count(jp,ip)
1968             IF ( number_of_agents <= 0 )  CYCLE
1969             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
1970             DO  n = 1, number_of_agents
1971                IF ( agents(n)%agent_mask )  THEN
1972                   j = agents(n)%y * ddy
1973!
1974!--                Above calculation does not work for indices less than zero
1975                   IF ( agents(n)%y < 0.0_wp )  j = -1
1976
1977                   IF ( j < nys )  THEN
1978                      trsa_count = trsa_count + 1
1979                   ELSEIF ( j > nyn )  THEN
1980                      trna_count = trna_count + 1
1981                   ENDIF
1982                ENDIF
1983             ENDDO
1984          ENDDO
1985       ENDDO
1986
1987       IF ( trsa_count  == 0 )  trsa_count  = 1
1988       IF ( trna_count  == 0 )  trna_count  = 1
1989
1990       ALLOCATE( trsa(trsa_count), trna(trna_count) )
1991
1992       trsa = zero_agent
1993       trna = zero_agent
1994
1995       trsa_count  = nr_move_south
1996       trna_count  = nr_move_north
1997
1998       trsa(1:nr_move_south) = move_also_south(1:nr_move_south)
1999       trna(1:nr_move_north) = move_also_north(1:nr_move_north)
2000
2001    ENDIF
2002
2003    DO  ip = nxl, nxr
2004       DO  jp = nys, nyn, nyn-nys ! compute only first (nys) and last (nyn) loop iterration
2005          number_of_agents = agt_count(jp,ip)
2006          IF ( number_of_agents <= 0 )  CYCLE
2007          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
2008          DO  n = 1, number_of_agents
2009!
2010!--          Only those agents that have not been marked as 'deleted' may be moved.
2011             IF ( agents(n)%agent_mask )  THEN
2012
2013                j = agents(n)%y * ddy
2014!
2015!--             Above calculation does not work for indices less than zero
2016                IF ( agents(n)%y < 0.0_wp * dy )  j = -1
2017
2018                IF ( j < nys )  THEN
2019                   IF ( j < 0 )  THEN
2020!
2021!--                   Apply boundary condition along y
2022                      IF ( ibc_mas_ns == 0 )  THEN
2023!
2024!--                      Cyclic condition
2025                         IF ( pdims(2) == 1 )  THEN
2026                            agents(n)%y = ( ny + 1 ) * dy + agents(n)%y
2027                            agents(n)%origin_y = ( ny + 1 ) * dy + agents(n)%origin_y
2028                         ELSE
2029                            trsa_count         = trsa_count + 1
2030                            trsa(trsa_count)   = agents(n)
2031                            trsa(trsa_count)%y = ( ny + 1 ) * dy + trsa(trsa_count)%y
2032                            trsa(trsa_count)%origin_y = trsa(trsa_count)%origin_y + ( ny + 1 ) * dy
2033                            agents(n)%agent_mask = .FALSE.
2034                            deleted_agents = deleted_agents + 1
2035
2036                            IF ( trsa(trsa_count)%y >= (ny+1)* dy - 1.0E-12_wp )  THEN
2037                               trsa(trsa_count)%y = trsa(trsa_count)%y - 1.0E-10_wp
2038                               trsa(trsa_count)%origin_y = trsa(trsa_count)%origin_y - 1
2039                            ENDIF
2040
2041                         ENDIF
2042
2043                      ELSEIF ( ibc_mas_ns == 1 )  THEN
2044!
2045!--                      Agent absorption
2046                         agents(n)%agent_mask = .FALSE.
2047                         deleted_agents          = deleted_agents + 1
2048
2049                      ENDIF
2050                   ELSE
2051!
2052!--                   Store agent data in the transfer array, which will be send to the neighbouring
2053!--                   PE.
2054                      trsa_count = trsa_count + 1
2055                      trsa(trsa_count) = agents(n)
2056                      agents(n)%agent_mask = .FALSE.
2057                      deleted_agents = deleted_agents + 1
2058
2059                   ENDIF
2060
2061                ELSEIF ( j > nyn )  THEN
2062                   IF ( j > ny )  THEN
2063!
2064!--                   Apply boundary condition along y
2065                      IF ( ibc_mas_ns == 0 )  THEN
2066!
2067!--                      Cyclic condition
2068                         IF ( pdims(2) == 1 )  THEN
2069                            agents(n)%y        = agents(n)%y        - ( ny + 1 ) * dy
2070                            agents(n)%origin_y = agents(n)%origin_y - ( ny + 1 ) * dy
2071                         ELSE
2072                            trna_count         = trna_count + 1
2073                            trna(trna_count)   = agents(n)
2074                            trna(trna_count)%y = trna(trna_count)%y - ( ny + 1 ) * dy
2075                            trna(trna_count)%origin_y = trna(trna_count)%origin_y - ( ny + 1 ) * dy
2076                            agents(n)%agent_mask = .FALSE.
2077                            deleted_agents          = deleted_agents + 1
2078                         ENDIF
2079
2080                      ELSEIF ( ibc_mas_ns == 1 )  THEN
2081!
2082!--                      Agent absorption
2083                         agents(n)%agent_mask = .FALSE.
2084                         deleted_agents = deleted_agents + 1
2085
2086                      ENDIF
2087                   ELSE
2088!
2089!--                   Store agent data in the transfer array, which will be send to the neighbouring
2090!--                   PE
2091                      trna_count = trna_count + 1
2092                      trna(trna_count) = agents(n)
2093                      agents(n)%agent_mask = .FALSE.
2094                      deleted_agents = deleted_agents + 1
2095
2096                   ENDIF
2097
2098                ENDIF
2099             ENDIF
2100          ENDDO
2101       ENDDO
2102    ENDDO
2103
2104!
2105!-- Send front boundary, receive back boundary (but first exchange how many and check if agent
2106!-- storage must be extended).
2107    IF ( pdims(2) /= 1 )  THEN
2108
2109       CALL MPI_SENDRECV( trsa_count,      1, MPI_INTEGER, psouth, 0,                              &
2110                          trna_count_recv, 1, MPI_INTEGER, pnorth, 0,                              &
2111                          comm2d, status, ierr )
2112
2113       ALLOCATE( rvna( MAX( 1, trna_count_recv )) )
2114!
2115!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit variables in structure
2116!--    agent_type (due to the calculation of par_size)
2117       par_size = STORAGE_SIZE(trsa(1))/8
2118       CALL MPI_SENDRECV( trsa, trsa_count * par_size,      MPI_BYTE, psouth, 1,                   &
2119                          rvna, trna_count_recv * par_size, MPI_BYTE, pnorth, 1,                   &
2120                          comm2d, status, ierr )
2121
2122       IF ( trna_count_recv  > 0 )  THEN
2123          CALL mas_eh_add_agents_to_gridcell(rvna(1:trna_count_recv))
2124       ENDIF
2125
2126       DEALLOCATE( rvna )
2127
2128!
2129!--    Send back boundary, receive front boundary
2130       CALL MPI_SENDRECV( trna_count,      1, MPI_INTEGER, pnorth, 0,                              &
2131                          trsa_count_recv, 1, MPI_INTEGER, psouth, 0,                              &
2132                          comm2d, status, ierr )
2133
2134       ALLOCATE( rvsa( MAX( 1, trsa_count_recv )) )
2135!
2136!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit variables in structure
2137!--    agent_type (due to the calculation of par_size)
2138       par_size = STORAGE_SIZE( trna(1) ) / 8
2139       CALL MPI_SENDRECV( trna, trna_count * par_size,      MPI_BYTE, pnorth, 1,                   &
2140                          rvsa, trsa_count_recv * par_size, MPI_BYTE, psouth, 1,                   &
2141                          comm2d, status, ierr )
2142
2143       IF ( trsa_count_recv > 0 )  THEN
2144          CALL mas_eh_add_agents_to_gridcell(rvsa(1:trsa_count_recv))
2145       ENDIF
2146
2147       DEALLOCATE( rvsa )
2148
2149       number_of_agents = number_of_agents + trsa_count_recv
2150
2151       DEALLOCATE( trsa, trna )
2152
2153    ENDIF
2154
2155    DEALLOCATE( move_also_north )
2156    DEALLOCATE( move_also_south )
2157
2158!
2159!-- Accumulate the number of agents transferred between the subdomains)
2160    CALL mas_eh_ghost_exchange
2161
2162#else
2163
2164    DO  ip = nxl, nxr, nxr-nxl
2165       DO  jp = nys, nyn
2166          number_of_agents = agt_count(jp,ip)
2167          IF ( number_of_agents <= 0 )  CYCLE
2168          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
2169          DO  n = 1, number_of_agents
2170!
2171!--          Apply boundary conditions
2172             IF ( agents(n)%x < 0.0_wp )  THEN
2173
2174                IF ( ibc_mas_lr == 0 )  THEN
2175!
2176!--                Cyclic boundary. Relevant coordinate has to be changed.
2177                   agents(n)%x = ( nx + 1 ) * dx + agents(n)%x
2178                   agents(n)%origin_x = ( nx + 1 ) * dx + agents(n)%origin_x
2179                ELSEIF ( ibc_mas_lr == 1 )  THEN
2180!
2181!--                Agent absorption
2182                   agents(n)%agent_mask = .FALSE.
2183                   deleted_agents = deleted_agents + 1
2184                ENDIF
2185
2186             ELSEIF ( agents(n)%x >= ( nx + 1 ) * dx )  THEN
2187
2188                IF ( ibc_mas_lr == 0 )  THEN
2189!
2190!--                Cyclic boundary. Relevant coordinate has to be changed.
2191                   agents(n)%x = agents(n)%x - ( nx + 1 ) * dx
2192
2193                ELSEIF ( ibc_mas_lr == 1 )  THEN
2194!
2195!--                Agent absorption
2196                   agents(n)%agent_mask = .FALSE.
2197                   deleted_agents = deleted_agents + 1
2198                ENDIF
2199
2200             ENDIF
2201          ENDDO
2202       ENDDO
2203    ENDDO
2204
2205    DO  ip = nxl, nxr
2206       DO  jp = nys, nyn, nyn-nys
2207          number_of_agents = agt_count(jp,ip)
2208          IF ( number_of_agents <= 0 )  CYCLE
2209          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
2210          DO  n = 1, number_of_agents
2211
2212             IF ( agents(n)%y < 0.0_wp )  THEN
2213
2214                IF ( ibc_mas_ns == 0 )  THEN
2215!
2216!--                Cyclic boundary. Relevant coordinate has to be changed.
2217                   agents(n)%y = ( ny + 1 ) * dy + agents(n)%y
2218                   agents(n)%origin_y = ( ny + 1 ) * dy + &
2219                        agents(n)%origin_y
2220
2221                ELSEIF ( ibc_mas_ns == 1 )  THEN
2222!
2223!--                Agent absorption
2224                   agents(n)%agent_mask = .FALSE.
2225                   deleted_agents = deleted_agents + 1
2226                ENDIF
2227
2228             ELSEIF ( agents(n)%y >= ( ny + 0.5_wp ) * dy )  THEN
2229
2230                IF ( ibc_mas_ns == 0 )  THEN
2231!
2232!--                Cyclic boundary. Relevant coordinate has to be changed.
2233                   agents(n)%y = agents(n)%y - ( ny + 1 ) * dy
2234
2235                ELSEIF ( ibc_mas_ns == 1 )  THEN
2236!
2237!--                Agent absorption
2238                   agents(n)%agent_mask = .FALSE.
2239                   deleted_agents = deleted_agents + 1
2240                ENDIF
2241
2242             ENDIF
2243
2244          ENDDO
2245       ENDDO
2246    ENDDO
2247#endif
2248
2249 END SUBROUTINE mas_eh_exchange_horiz
2250
2251
2252#if defined( __parallel )
2253!--------------------------------------------------------------------------------------------------!
2254! Description:
2255! ------------
2256!> Sends the agents from the three gridcells closest to the north/south/left/right border of a PE to
2257!> the corresponding neighbors ghost layer (which is three grid boxes deep)
2258!--------------------------------------------------------------------------------------------------!
2259 SUBROUTINE mas_eh_ghost_exchange
2260
2261    IMPLICIT NONE
2262
2263    INTEGER(iwp) ::  agt_size    !< Bit size of agent datatype
2264    INTEGER(iwp) ::  ghla_count  !< ghost points left agent
2265    INTEGER(iwp) ::  ghna_count  !< ghost points north agent
2266    INTEGER(iwp) ::  ghra_count  !< ghost points right agent
2267    INTEGER(iwp) ::  ghsa_count  !< ghost points south agent
2268    INTEGER(iwp) ::  ip          !< index variable along x
2269    INTEGER(iwp) ::  jp          !< index variable along y
2270
2271    LOGICAL ::  ghla_empty      !< ghost points left agent
2272    LOGICAL ::  ghla_empty_rcv  !< ghost points left agent
2273    LOGICAL ::  ghna_empty      !< ghost points north agent
2274    LOGICAL ::  ghna_empty_rcv  !< ghost points north agent
2275    LOGICAL ::  ghra_empty      !< ghost points right agent
2276    LOGICAL ::  ghra_empty_rcv  !< ghost points right agent
2277    LOGICAL ::  ghsa_empty      !< ghost points south agent
2278    LOGICAL ::  ghsa_empty_rcv  !< ghost points south agent
2279
2280    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghla  !< agents received from right PE
2281    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghna  !< agents received from south PE
2282    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghra  !< agents received from left PE
2283    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghsa  !< agents received from north PE
2284
2285    ghla_empty = .TRUE.
2286    ghna_empty = .TRUE.
2287    ghra_empty = .TRUE.
2288    ghsa_empty = .TRUE.
2289!
2290!-- Reset ghost layer
2291    DO  ip = nxlg, nxl-1
2292       DO  jp = nysg, nyng
2293          agt_count(jp,ip) = 0
2294       ENDDO
2295    ENDDO
2296    DO  ip = nxr+1, nxrg
2297       DO  jp = nysg, nyng
2298          agt_count(jp,ip) = 0
2299       ENDDO
2300    ENDDO
2301    DO  ip = nxl, nxr
2302       DO  jp = nysg, nys-1
2303          agt_count(jp,ip) = 0
2304       ENDDO
2305    ENDDO
2306    DO  ip = nxl, nxr
2307       DO  jp = nyn+1, nyng
2308          agt_count(jp,ip) = 0
2309       ENDDO
2310    ENDDO
2311!
2312!-- Transfer of agents from left to right and vice versa
2313    IF ( pdims(1) /= 1 )  THEN
2314!
2315!--    Reset left and right ghost layers
2316       ghla_count  = 0
2317       ghra_count  = 0
2318!
2319!--    First calculate the storage necessary for sending and receiving the data.
2320       ghla_count = SUM(agt_count(nys:nyn,nxl:nxl+2))
2321       ghra_count = SUM(agt_count(nys:nyn,nxr-2:nxr))
2322!
2323!--    No cyclic boundaries for agents
2324       IF ( nxl == 0  .OR.  ghla_count == 0 )  THEN
2325          ghla_count = 1
2326       ELSE
2327          ghla_empty = .FALSE.
2328       ENDIF
2329       IF ( nxr == nx  .OR.  ghra_count == 0 )  THEN
2330          ghra_count = 1
2331       ELSE
2332          ghra_empty = .FALSE.
2333       ENDIF
2334       ALLOCATE( ghla(1:ghla_count), ghra(1:ghra_count) )
2335       ghla = zero_agent
2336       ghra = zero_agent
2337!
2338!--    Get all agents that will be sent left into one array
2339       ghla_count = 0
2340       IF ( nxl /= 0 )  THEN
2341          DO  ip = nxl, nxl+2
2342             DO jp = nys, nyn
2343
2344                number_of_agents = agt_count(jp,ip)
2345                IF ( number_of_agents <= 0 )  CYCLE
2346                ghla(ghla_count+1:ghla_count+number_of_agents)                                     &
2347                                                     = grid_agents(jp,ip)%agents(1:number_of_agents)
2348                ghla_count = ghla_count + number_of_agents
2349
2350             ENDDO
2351          ENDDO
2352       ENDIF
2353       IF ( ghla_count == 0 )  ghla_count = 1
2354!
2355!--    Get all agents that will be sent right into one array
2356       ghra_count = 0
2357       IF ( nxr /= nx )  THEN
2358          DO  ip = nxr-2, nxr
2359             DO  jp = nys, nyn
2360
2361                number_of_agents = agt_count(jp,ip)
2362                IF ( number_of_agents <= 0 )  CYCLE
2363                ghra(ghra_count+1:ghra_count+number_of_agents)                                     &
2364                                                   = grid_agents(jp,ip)%agents(1:number_of_agents)
2365                ghra_count = ghra_count + number_of_agents
2366
2367             ENDDO
2368          ENDDO
2369       ENDIF
2370       IF ( ghra_count == 0 ) ghra_count = 1
2371!
2372!--    Send/receive number of agents that will be transferred to/from left/right neighbor.
2373       CALL MPI_SENDRECV( ghla_count,      1, MPI_INTEGER, pleft,  0,                              &
2374                          ghra_count_recv, 1, MPI_INTEGER, pright, 0,                              &
2375                          comm2d, status, ierr )
2376       ALLOCATE ( agt_gh_r(1:ghra_count_recv) )
2377!
2378!--    Send/receive number of agents that will be transferred to/from right/left neighbor
2379       CALL MPI_SENDRECV( ghra_count,      1, MPI_INTEGER, pright,  0,                             &
2380                          ghla_count_recv, 1, MPI_INTEGER, pleft,   0,                             &
2381                          comm2d, status, ierr )
2382!
2383!--    Send/receive flag that indicates if there are actually any agents in ghost layer
2384       CALL MPI_SENDRECV( ghla_empty,     1, MPI_LOGICAL, pleft, 1,                                &
2385                          ghra_empty_rcv, 1, MPI_LOGICAL, pright,1,                                &
2386                          comm2d, status, ierr )
2387       CALL MPI_SENDRECV( ghra_empty,     1, MPI_LOGICAL, pright,1,                                &
2388                          ghla_empty_rcv, 1, MPI_LOGICAL, pleft, 1,                                &
2389                          comm2d, status, ierr )
2390
2391
2392       ALLOCATE ( agt_gh_l(1:ghla_count_recv) )
2393!
2394!--    Get bit size of one agent
2395       agt_size = STORAGE_SIZE(zero_agent)/8
2396!
2397!--    Send/receive agents to/from left/right neighbor
2398       CALL MPI_SENDRECV( ghla,     ghla_count      * agt_size, MPI_BYTE, pleft, 1,                &
2399                          agt_gh_r, ghra_count_recv * agt_size, MPI_BYTE, pright,1,                &
2400                          comm2d, status, ierr )
2401!
2402!--    Send/receive agents to/from left/right neighbor
2403       CALL MPI_SENDRECV( ghra,     ghra_count      * agt_size, MPI_BYTE, pright,1,                &
2404                          agt_gh_l, ghla_count_recv * agt_size, MPI_BYTE, pleft, 1,                &
2405                          comm2d, status, ierr )
2406!
2407!--    If agents were received, add them to the respective ghost layer cells
2408       IF ( .NOT. ghra_empty_rcv )  THEN
2409          CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_r)
2410       ENDIF
2411
2412       IF ( .NOT. ghla_empty_rcv )  THEN
2413          CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_l)
2414       ENDIF
2415
2416       DEALLOCATE( ghla, ghra, agt_gh_l, agt_gh_r )
2417
2418    ENDIF
2419
2420!
2421!-- Transfer of agents from south to north and vice versa
2422    IF ( pdims(2) /= 1 )  THEN
2423!
2424!--    Reset south and north ghost layers
2425       ghsa_count  = 0
2426       ghna_count  = 0
2427!
2428!--    First calculate the storage necessary for sending and receiving the data.
2429       ghsa_count = SUM( agt_count(nys:nys+2,nxlg:nxrg) )
2430       ghna_count = SUM( agt_count(nyn-2:nyn,nxlg:nxrg) )
2431!
2432!--    No cyclic boundaries for agents
2433       IF ( nys == 0  .OR.  ghsa_count == 0 )  THEN
2434          ghsa_count = 1
2435       ELSE
2436          ghsa_empty = .FALSE.
2437       ENDIF
2438       IF ( nyn == ny  .OR.  ghna_count == 0 )  THEN
2439          ghna_count = 1
2440       ELSE
2441          ghna_empty = .FALSE.
2442       ENDIF
2443       ALLOCATE( ghsa(1:ghsa_count), ghna(1:ghna_count) )
2444       ghsa = zero_agent
2445       ghna = zero_agent
2446!
2447!--    Get all agents that will be sent south into one array
2448       ghsa_count = 0
2449       IF ( nys /= 0 )  THEN
2450          DO  ip = nxlg, nxrg
2451             DO  jp = nys, nys+2
2452
2453                number_of_agents = agt_count(jp,ip)
2454                IF ( number_of_agents <= 0 )  CYCLE
2455                ghsa(ghsa_count+1:ghsa_count+number_of_agents)                                     &
2456                                                    = grid_agents(jp,ip)%agents(1:number_of_agents)
2457                ghsa_count = ghsa_count + number_of_agents
2458
2459             ENDDO
2460          ENDDO
2461       ENDIF
2462       IF ( ghsa_count == 0 )  ghsa_count = 1
2463!
2464!--    Get all agents that will be sent north into one array
2465       ghna_count = 0
2466       IF ( nyn /= ny )  THEN
2467          DO  ip = nxlg, nxrg
2468             DO  jp = nyn-2, nyn
2469
2470                number_of_agents = agt_count(jp,ip)
2471                IF ( number_of_agents <= 0 )  CYCLE
2472                ghna(ghna_count+1:ghna_count+number_of_agents)                                     &
2473                                                     = grid_agents(jp,ip)%agents(1:number_of_agents)
2474                ghna_count = ghna_count + number_of_agents
2475
2476             ENDDO
2477          ENDDO
2478       ENDIF
2479       IF ( ghna_count == 0 ) ghna_count = 1
2480!
2481!--    Send/receive number of agents that will be transferred to/from south/north neighbor
2482       CALL MPI_SENDRECV( ghsa_count,        1, MPI_INTEGER, psouth, 0,                            &
2483                          ghna_count_recv,   1, MPI_INTEGER, pnorth, 0,                            &
2484                          comm2d, status, ierr )
2485       ALLOCATE ( agt_gh_n(1:ghna_count_recv) )
2486!
2487!--    Send/receive number of agents that will be transferred to/from north/south neighbor
2488       CALL MPI_SENDRECV( ghna_count,        1, MPI_INTEGER, pnorth, 0,                            &
2489                          ghsa_count_recv,   1, MPI_INTEGER, psouth, 0,                            &
2490                          comm2d, status, ierr )
2491!
2492!--    Send/receive flag that indicates if there are actually any agents in ghost layer
2493       CALL MPI_SENDRECV( ghsa_empty,     1, MPI_LOGICAL, psouth, 1,                               &
2494                          ghna_empty_rcv, 1, MPI_LOGICAL, pnorth, 1,                               &
2495                          comm2d, status, ierr )
2496       CALL MPI_SENDRECV( ghna_empty,     1, MPI_LOGICAL, pnorth, 1,                               &
2497                          ghsa_empty_rcv, 1, MPI_LOGICAL, psouth, 1,                               &
2498                          comm2d, status, ierr )
2499
2500
2501       ALLOCATE ( agt_gh_s(1:ghsa_count_recv) )
2502!
2503!--    Get bit size of one agent
2504       agt_size = STORAGE_SIZE(zero_agent)/8
2505!
2506!--    Send/receive agents to/from south/north neighbor
2507       CALL MPI_SENDRECV( ghsa,     ghsa_count      * agt_size, MPI_BYTE, psouth,1,                &
2508                          agt_gh_n, ghna_count_recv * agt_size, MPI_BYTE, pnorth,1,                &
2509                          comm2d, status, ierr )
2510!
2511!--       Send/receive agents to/from south/north neighbor
2512       CALL MPI_SENDRECV( ghna,     ghna_count      * agt_size, MPI_BYTE, pnorth,1,                &
2513                          agt_gh_s, ghsa_count_recv * agt_size, MPI_BYTE, psouth,1,                &
2514                          comm2d, status, ierr )
2515!
2516!--    If agents were received, add them to the respective ghost layer cells
2517       IF ( .NOT. ghna_empty_rcv )  THEN
2518          CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_n)
2519       ENDIF
2520
2521       IF ( .NOT. ghsa_empty_rcv )  THEN
2522          CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_s)
2523       ENDIF
2524
2525       DEALLOCATE( ghna, ghsa, agt_gh_n, agt_gh_s )
2526
2527    ENDIF
2528
2529 END SUBROUTINE mas_eh_ghost_exchange
2530#endif
2531
2532!--------------------------------------------------------------------------------------------------!
2533! Description:
2534! ------------
2535!> If an agent moves from one grid cell to another (on the current processor!), this subroutine
2536!> moves the corresponding element from the agent array of the old grid cell to the agent array of
2537!> the new grid cell.
2538!--------------------------------------------------------------------------------------------------!
2539 SUBROUTINE mas_eh_move_agent
2540
2541    IMPLICIT NONE
2542
2543    INTEGER(iwp) ::  aindex         !< dummy argument for number of new agent per grid box
2544    INTEGER(iwp) ::  i              !< grid index (x) of agent position
2545    INTEGER(iwp) ::  ip             !< index variable along x
2546    INTEGER(iwp) ::  j              !< grid index (y) of agent position
2547    INTEGER(iwp) ::  jp             !< index variable along y
2548    INTEGER(iwp) ::  n              !< index variable for agent array
2549    INTEGER(iwp) ::  na_before_move !< number of agents per grid box before moving
2550
2551    TYPE(agent_type), DIMENSION(:), POINTER ::  agents_before_move !< agents before moving
2552
2553    DO  ip = nxl, nxr
2554       DO  jp = nys, nyn
2555
2556             na_before_move = agt_count(jp,ip)
2557             IF ( na_before_move <= 0 )  CYCLE
2558             agents_before_move => grid_agents(jp,ip)%agents(1:na_before_move)
2559
2560             DO  n = 1, na_before_move
2561                i = agents_before_move(n)%x * ddx
2562                j = agents_before_move(n)%y * ddy
2563
2564!--             For mas_eh_exchange_horiz to work properly agents need to be moved to the outermost
2565!--             gridboxes of the respective processor.
2566!--             If the agent index is inside the processor the following lines will not change the
2567!--             index.
2568                i = MIN ( i , nxr )
2569                i = MAX ( i , nxl )
2570                j = MIN ( j , nyn )
2571                j = MAX ( j , nys )
2572
2573!
2574!--             Check if agent has moved to another grid cell.
2575                IF ( i /= ip  .OR.  j /= jp )  THEN
2576!
2577!--                If the agent stays on the same processor, the agent will be added to the agent
2578!--                array of the new processor.
2579                   number_of_agents = agt_count(j,i)
2580                   agents => grid_agents(j,i)%agents(1:number_of_agents)
2581
2582                   aindex = number_of_agents+1
2583                   IF (  aindex > SIZE(grid_agents(j,i)%agents)  )  THEN
2584                      CALL mas_eh_realloc_agents_array(i,j)
2585                   ENDIF
2586
2587                   grid_agents(j,i)%agents(aindex) = agents_before_move(n)
2588                   agt_count(j,i) = aindex
2589
2590                   agents_before_move(n)%agent_mask = .FALSE.
2591                ENDIF
2592             ENDDO
2593
2594       ENDDO
2595    ENDDO
2596
2597    RETURN
2598
2599 END SUBROUTINE mas_eh_move_agent
2600
2601!--------------------------------------------------------------------------------------------------!
2602! Description:
2603! ------------
2604!> If the allocated memory for the agent array does not suffice to add arriving agents from
2605!> neighbour grid cells, this subrouting reallocates the agent array to assure enough memory is
2606!> available.
2607!--------------------------------------------------------------------------------------------------!
2608    SUBROUTINE mas_eh_realloc_agents_array (i,j,size_in)
2609
2610       IMPLICIT NONE
2611
2612       INTEGER(iwp) :: new_size  !< new array size
2613       INTEGER(iwp) :: old_size  !< old array size
2614
2615       INTEGER(iwp), INTENT(in) ::  i  !< grid index (y)
2616       INTEGER(iwp), INTENT(in) ::  j  !< grid index (y)
2617
2618       INTEGER(iwp), INTENT(in), OPTIONAL ::  size_in  !< size of input array
2619
2620       TYPE(agent_type), DIMENSION(10) :: tmp_agents_s  !< temporary static agent array
2621
2622       TYPE(agent_type), DIMENSION(:), ALLOCATABLE :: tmp_agents_d  !< temporary dynamic agent array
2623
2624       old_size = SIZE(grid_agents(j,i)%agents)
2625
2626       IF ( PRESENT( size_in ) )  THEN
2627          new_size = size_in
2628       ELSE
2629          new_size = old_size * ( 1.0_wp + alloc_factor_mas / 100.0_wp )
2630       ENDIF
2631
2632       new_size = MAX( new_size, min_nr_agent, old_size + 1 )
2633
2634       IF ( old_size <= 10 )  THEN
2635
2636          tmp_agents_s(1:old_size) = grid_agents(j,i)%agents(1:old_size)
2637
2638          DEALLOCATE( grid_agents(j,i)%agents )
2639          ALLOCATE( grid_agents(j,i)%agents(new_size) )
2640
2641          grid_agents(j,i)%agents(1:old_size)         = tmp_agents_s(1:old_size)
2642          grid_agents(j,i)%agents(old_size+1:new_size) = zero_agent
2643
2644       ELSE
2645
2646          ALLOCATE( tmp_agents_d(new_size) )
2647          tmp_agents_d(1:old_size) = grid_agents(j,i)%agents
2648
2649          DEALLOCATE( grid_agents(j,i)%agents )
2650          ALLOCATE( grid_agents(j,i)%agents(new_size) )
2651
2652          grid_agents(j,i)%agents(1:old_size)         = tmp_agents_d(1:old_size)
2653          grid_agents(j,i)%agents(old_size+1:new_size) = zero_agent
2654
2655          DEALLOCATE( tmp_agents_d )
2656
2657       ENDIF
2658       agents => grid_agents(j,i)%agents(1:number_of_agents)
2659
2660       RETURN
2661    END SUBROUTINE mas_eh_realloc_agents_array
2662
2663!--------------------------------------------------------------------------------------------------!
2664! Description:
2665! ------------
2666!> Inquires prognostic model quantities at the position of each agent and stores them in that agent
2667!> for later output
2668!--------------------------------------------------------------------------------------------------!
2669 SUBROUTINE mas_get_prognostic_quantities
2670
2671    USE arrays_3d,                                                                                 &
2672        ONLY:  exner, pt, u, v
2673
2674    IMPLICIT NONE
2675
2676    INTEGER(iwp) ::  i_offset  !<  index offset for windspeed measurement
2677    INTEGER(iwp) ::  il        !<  x-index
2678    INTEGER(iwp) ::  is        !<  subgrid box counter
2679    INTEGER(iwp) ::  j_offset  !<  index offset for windspeed measurement
2680    INTEGER(iwp) ::  jl        !<  y-index
2681    INTEGER(iwp) ::  kl        !<  z-index
2682    INTEGER(iwp) ::  nl        !<  agent counter
2683    INTEGER(iwp) ::  se        !<  subgrid box end index
2684    INTEGER(iwp) ::  si        !<  subgrid box start index
2685
2686    REAL(wp) ::  u_a  !< windspeed at agent position (x)
2687    REAL(wp) ::  v_a  !< windspeed at agent position (y)
2688
2689    DO  il = nxl, nxr
2690       DO  jl = nys, nyn
2691
2692          number_of_agents = agt_count(jl,il)
2693!
2694!--       If grid cell is empty, cycle
2695          IF ( number_of_agents <= 0 )  CYCLE
2696          kl = s_measure_height(jl,il)
2697
2698          agents => grid_agents(jl,il)%agents(1:number_of_agents)
2699!
2700!--       Loop over the four subgrid boxes
2701          DO  is = 0,3
2702!
2703!--          Set indices
2704             si = grid_agents(jl,il)%start_index(is)
2705             se = grid_agents(jl,il)%end_index(is)
2706             DO  nl = si, se
2707!
2708!--             Calculate index offset in x-direction:
2709!--             Left value if wall right of grid box
2710!--             Right value if wall left of grid box
2711!--             Else the one that is closer to the agent
2712                IF ( BTEST( obstacle_flags( jl, il+1 ), 6 ) )  THEN
2713                   i_offset = 0
2714                ELSEIF ( BTEST( obstacle_flags( jl, il-1 ), 2 ) )  THEN
2715                   i_offset = 1
2716                ELSE
2717                   i_offset = MERGE( 0, 1, BTEST(is,1) )
2718                ENDIF
2719                u_a = u( kl, jl, il + i_offset )
2720!
2721!--             Calculate index offset in y-direction:
2722!--             South value if wall north of grid box
2723!--             North value if wall south of grid box
2724!--             Else the one that is closer to the agent
2725                IF ( BTEST( obstacle_flags( jl+1, il ), 4 ) )  THEN
2726                   j_offset = 0
2727                ELSEIF ( BTEST( obstacle_flags( jl-1, il ), 0 ) )  THEN
2728                   j_offset = 1
2729                ELSE
2730                   j_offset = MERGE( 0, 1, BTEST(is,0) )
2731                ENDIF
2732                v_a = v( kl, jl + j_offset, il )
2733!
2734!--             Calculate windspeed at agent postion
2735                agents(nl)%windspeed = SQRT(u_a**2 + v_a**2)
2736!
2737!--             Calculate temperature at agent position
2738                agents(nl)%t = pt(kl,jl,il) * exner(kl)
2739
2740             ENDDO
2741
2742          ENDDO
2743
2744       ENDDO
2745    ENDDO
2746
2747 END SUBROUTINE mas_get_prognostic_quantities
2748
2749!--------------------------------------------------------------------------------------------------!
2750! Description:
2751! ------------
2752!> Adds an item to the priority queue (binary heap) at the correct position
2753!--------------------------------------------------------------------------------------------------!
2754 SUBROUTINE mas_heap_insert_item( id, priority )
2755
2756    IMPLICIT NONE
2757
2758    INTEGER(iwp) ::  cur_pos  !< current position
2759    INTEGER(iwp) ::  id       !< mesh ID of item
2760
2761    REAL(wp) ::  priority  !< item priority
2762
2763    TYPE(heap_item) ::  item  !< heap item
2764
2765    item%mesh_id  = id
2766    item%priority = priority
2767!
2768!--    Extend heap, if necessary
2769    IF ( heap_count + 1 > SIZE( queue ) )  THEN
2770       CALL mas_heap_extend
2771    ENDIF
2772!
2773!-- Insert item at first unoccupied postion (highest index) of heap
2774    cur_pos = heap_count
2775    queue(cur_pos) = item
2776!
2777!-- Sort while inserted item is not at top of heap
2778    DO WHILE ( cur_pos /= 0 )
2779!
2780!--    If priority < its parent's priority, swap them.
2781!--    Else, sorting is done.
2782       IF ( queue(cur_pos)%priority < queue(FLOOR( (cur_pos) / 2.0_wp ))%priority )  THEN
2783          item = queue(cur_pos)
2784          queue(cur_pos) = queue(FLOOR( ( cur_pos ) / 2.0_wp ))
2785          queue(FLOOR( ( cur_pos ) / 2.0_wp )) = item
2786          cur_pos = FLOOR( ( cur_pos ) / 2.0_wp )
2787       ELSE
2788          EXIT
2789       ENDIF
2790    ENDDO
2791!
2792!-- Item was added to heap, so the heap count increases
2793    heap_count = heap_count + 1
2794
2795 END SUBROUTINE mas_heap_insert_item
2796
2797!--------------------------------------------------------------------------------------------------!
2798! Description:
2799! ------------
2800!> Extends the size of the priority queue (binary heap)
2801!--------------------------------------------------------------------------------------------------!
2802 SUBROUTINE mas_heap_extend
2803
2804    IMPLICIT NONE
2805
2806    INTEGER(iwp) ::  soh  !< size of heap
2807
2808    TYPE(heap_item), DIMENSION(:), ALLOCATABLE ::  dummy_heap  !< dummy heap
2809
2810    soh = SIZE( queue ) - 1
2811    ALLOCATE( dummy_heap(0:soh) )
2812    dummy_heap = queue
2813    DEALLOCATE( queue )
2814    ALLOCATE( queue(0:2*soh+1) )
2815    queue(0:soh) = dummy_heap(0:soh)
2816
2817 END SUBROUTINE mas_heap_extend
2818
2819!--------------------------------------------------------------------------------------------------!
2820! Description:
2821! ------------
2822!> Removes first (smallest) element from the priority queue, reorders the rest and returns the ID of
2823!> the removed mesh point
2824!--------------------------------------------------------------------------------------------------!
2825 SUBROUTINE mas_heap_extract_item ( id )
2826
2827    IMPLICIT NONE
2828
2829    INTEGER(iwp) ::  child    !< child of item in heap
2830    INTEGER(iwp) ::  cur_pos  !< current position of item in heap
2831    INTEGER(iwp) ::  id       !< ID of item extracted item
2832
2833    TYPE(heap_item) ::  dummy
2834!
2835!-- Get ID of mesh point with lowest priority (extracted item: top of heap)
2836    id = queue(0)%mesh_id
2837!
2838!-- Put last item in heap at first position
2839    queue(0) = queue(heap_count-1)
2840    cur_pos = 0
2841    DO
2842!
2843!--    If current item has no children, sorting is done
2844       IF( 2*cur_pos+1 > heap_count - 1 )  THEN
2845          EXIT
2846!
2847!--    If current item has only one child, check if item and its child are ordered correctly. Else,
2848!--    swap them.
2849       ELSEIF ( 2*cur_pos+2 > heap_count - 1 )  THEN
2850          IF ( queue(cur_pos)%priority > queue(2*cur_pos+1)%priority )  THEN
2851             dummy = queue(cur_pos)
2852             queue(cur_pos) = queue(2*cur_pos+1)
2853             queue(2*cur_pos+1) = dummy
2854             cur_pos = 2*cur_pos+1
2855          ELSE
2856             EXIT
2857          ENDIF
2858       ELSE
2859!
2860!--       Determine the smaller child
2861          IF ( queue(2*cur_pos+1)%priority >= queue(2*cur_pos+2)%priority )  THEN
2862             child = 2
2863          ELSE
2864             child = 1
2865          ENDIF
2866!
2867!--       Check if item and its smaller child are ordered falsely. If so, swap them. Else, sorting
2868!--       is done.
2869          IF ( queue(cur_pos)%priority > queue(2*cur_pos+child )%priority )  THEN
2870             dummy = queue(cur_pos)
2871             queue(cur_pos) = queue(2*cur_pos+child)
2872             queue(2*cur_pos+child) = dummy
2873             cur_pos = 2*cur_pos+child
2874          ELSE
2875             EXIT
2876          ENDIF
2877       ENDIF
2878    ENDDO
2879!
2880!-- Top item was removed from heap, thus, heap_cout decreases by one
2881    heap_count = heap_count-1
2882
2883 END SUBROUTINE mas_heap_extract_item
2884
2885!--------------------------------------------------------------------------------------------------!
2886! Description:
2887! ------------
2888!> Initialization of Multi Agent System
2889!--------------------------------------------------------------------------------------------------!
2890 SUBROUTINE mas_init
2891
2892    USE control_parameters,                                                                        &
2893        ONLY:  coupling_char, initializing_actions, io_blocks, io_group
2894
2895    USE arrays_3d,                                                                                 &
2896        ONLY:  zu, zw
2897
2898    USE indices,                                                                                   &
2899        ONLY:  nzt
2900
2901    IMPLICIT NONE
2902
2903    INTEGER(iwp) ::  i             !< grid cell (x)
2904    INTEGER(iwp) ::  ii            !< io-block counter
2905    INTEGER(iwp) ::  il            !< io-block counter
2906    INTEGER(iwp) ::  ioerr         !< IOSTAT flag for IO-commands ( 0 = no error )
2907    INTEGER(iwp) ::  j             !< grid cell (y)
2908    INTEGER(iwp) ::  jl            !< io-block counter
2909    INTEGER(iwp) ::  kl            !< io-block counter
2910    INTEGER(iwp) ::  kdum          !< io-block counter
2911    INTEGER(iwp) ::  locdum        !< io-block counter
2912    INTEGER(iwp) ::  size_of_mesh  !< temporary value for read
2913    INTEGER(iwp) ::  size_of_pols  !< temporary value for read
2914
2915    LOGICAL ::  navigation_data_present  !< Flag: check for input file
2916
2917    REAL(wp) ::  zdum  !< dummy for measurement height
2918    REAL(wp) ::  avg_agt_height = 1.8_wp
2919
2920
2921!
2922!-- Check the number of agent groups.
2923    IF ( number_of_agent_groups > max_number_of_agent_groups )  THEN
2924       WRITE( message_string, * ) 'max_number_of_agent_groups =', max_number_of_agent_groups ,     &
2925                                  '&number_of_agent_groups reset to ', max_number_of_agent_groups
2926       CALL message( 'mas_init', 'PA0072', 0, 1, 0, 6, 0 )
2927       number_of_agent_groups = max_number_of_agent_groups
2928    ENDIF
2929
2930!
2931!-- Set some parameters
2932    d_sigma_rep_agent = 1.0_wp/sigma_rep_agent
2933    d_sigma_rep_wall  = 1.0_wp/sigma_rep_wall
2934    d_tau_accel_agent = 1.0_wp/tau_accel_agent
2935    IF ( dt_agent /= 999.0_wp )  THEN
2936       agent_own_timestep = .TRUE.
2937    ENDIF
2938
2939!
2940!-- Get index of first grid box above topography
2941    ALLOCATE( top_top_s(nysg:nyng,nxlg:nxrg),                                                      &
2942              top_top_w(nysg:nyng,nxlg:nxrg),                                                      &
2943              s_measure_height(nys:nyn,nxl:nxr) )
2944!
2945!-- Get first index above topography for scalar grid and last index in topography for z-component of
2946!-- wind.
2947    DO  il = nxlg, nxrg
2948       DO  jl = nysg, nyng
2949          top_top_s(jl,il) = topo_top_ind(jl,il,0) + 1
2950          top_top_w(jl,il) = topo_top_ind(jl,il,3)
2951       ENDDO
2952    ENDDO
2953!
2954!-- Create 2D array containing the index at which measurements are done by agents. The height of
2955!-- this measurement is given by avg_agt_height.
2956    DO  il = nxl, nxr
2957       DO  jl = nys, nyn
2958
2959          kdum = top_top_w(jl,il)
2960          zdum = zw(kdum)
2961          zdum = zdum + avg_agt_height
2962          locdum = 0
2963!
2964!--       Locate minimum distance from u-grid to measurement height (zdum)
2965          DO  kl = 1, nzt
2966             IF ( ABS(zu(kl)-zdum) < ABS(zu(locdum)-zdum) ) locdum = kl
2967          ENDDO
2968          s_measure_height(jl,il) = locdum
2969
2970       ENDDO
2971    ENDDO
2972
2973    CALL mas_create_obstacle_flags
2974
2975!
2976!-- Set default start positions, if necessary
2977    IF ( asl(1) == 9999999.9_wp )  asl(1) = 0.0_wp
2978    IF ( asr(1) == 9999999.9_wp )  asr(1) = ( nx + 1 ) * dx
2979    IF ( ass(1) == 9999999.9_wp )  ass(1) = 0.0_wp
2980    IF ( asn(1) == 9999999.9_wp )  asn(1) = ( ny + 1 ) * dy
2981    IF ( adx(1) == 9999999.9_wp  .OR.  adx(1) == 0.0_wp ) adx(1) = dx
2982    IF ( ady(1) == 9999999.9_wp  .OR.  ady(1) == 0.0_wp ) ady(1) = dy
2983
2984    DO  j = 2, number_of_agent_groups
2985       IF ( asl(j) == 9999999.9_wp )  asl(j) = asl(j-1)
2986       IF ( asr(j) == 9999999.9_wp )  asr(j) = asr(j-1)
2987       IF ( ass(j) == 9999999.9_wp )  ass(j) = ass(j-1)
2988       IF ( asn(j) == 9999999.9_wp )  asn(j) = asn(j-1)
2989       IF ( adx(j) == 9999999.9_wp  .OR.  adx(j) == 0.0_wp ) adx(j) = adx(j-1)
2990       IF ( ady(j) == 9999999.9_wp  .OR.  ady(j) == 0.0_wp ) ady(j) = ady(j-1)
2991    ENDDO
2992
2993!
2994!-- Check boundary condition and set internal variables
2995    SELECT CASE ( bc_mas_lr )
2996
2997       CASE ( 'cyclic' )
2998          ibc_mas_lr = 0
2999
3000       CASE ( 'absorb' )
3001          ibc_mas_lr = 1
3002
3003       CASE DEFAULT
3004          WRITE( message_string, * ) 'unknown boundary condition ',                                &
3005                                     'bc_mas_lr = "', TRIM( bc_mas_lr ), '"'
3006          CALL message( 'mas_init', 'PA0073', 1, 2, 0, 6, 0 )
3007
3008    END SELECT
3009    SELECT CASE ( bc_mas_ns )
3010
3011       CASE ( 'cyclic' )
3012          ibc_mas_ns = 0
3013
3014       CASE ( 'absorb' )
3015          ibc_mas_ns = 1
3016
3017       CASE DEFAULT
3018          WRITE( message_string, * ) 'unknown boundary condition ',                                &
3019                                     'bc_mas_ns = "', TRIM( bc_mas_ns ), '"'
3020          CALL message( 'mas_init', 'PA0074', 1, 2, 0, 6, 0 )
3021
3022    END SELECT
3023
3024!
3025!-- For the first model run of a possible job chain initialize the agents, otherwise read the agent
3026!-- data from restart file.
3027    IF ( TRIM( initializing_actions ) == 'read_restart_data'  .AND.  read_agents_from_restartfile )&
3028    THEN
3029
3030!           CALL mas_read_restart_file
3031
3032    ELSE
3033!
3034!--    Read preprocessed data of navigation mesh and building polygons for agent pathfinding
3035       DO  ii = 0, io_blocks-1
3036          IF ( ii == io_group )  THEN
3037!
3038!--          Check for naviation input file and open it
3039             INQUIRE( FILE='NAVIGATION_DATA' // TRIM( coupling_char ),                             &
3040                      EXIST=navigation_data_present )
3041             IF ( .NOT. navigation_data_present )  THEN
3042                message_string = 'Input file NAVIGATION_DATA' //                                   &
3043                                  TRIM( coupling_char ) // ' for MAS missing. ' //                 &
3044                                  '&Please run agent_preprocessing before the job to create it.'
3045                CALL message( 'mas_init', 'PA0525', 1, 2, 0, 6, 0 )
3046             ENDIF
3047             OPEN ( 119, FILE='NAVIGATION_DATA'//TRIM( coupling_char ), FORM='UNFORMATTED',        &
3048                         IOSTAT=ioerr )
3049!
3050!--             Read mesh data
3051             READ( 119 ) size_of_mesh
3052             ALLOCATE( mesh(1:size_of_mesh) )
3053             DO  i = 1, size_of_mesh
3054                READ( 119 ) mesh(i)%polygon_id, mesh(i)%vertex_id,                                 &
3055                            mesh(i)%noc, mesh(i)%origin_id,                                        &
3056                            mesh(i)%cost_so_far, mesh(i)%x,                                        &
3057                            mesh(i)%y, mesh(i)%x_s, mesh(i)%y_s
3058                ALLOCATE( mesh(i)%connected_vertices(1:mesh(i)%noc),                               &
3059                          mesh(i)%distance_to_vertex(1:mesh(i)%noc) )
3060                DO  j = 1, mesh(i)%noc
3061                   READ( 119 ) mesh(i)%connected_vertices(j),                                      &
3062                               mesh(i)%distance_to_vertex(j)
3063                ENDDO
3064             ENDDO
3065!
3066!--             Read polygon data
3067             READ( 119 ) size_of_pols
3068             ALLOCATE( polygons(1:size_of_pols) )
3069             DO  i = 1, size_of_pols
3070                READ( 119 ) polygons(i)%nov
3071                ALLOCATE( polygons(i)%vertices(0:polygons(i)%nov+1) )
3072                DO  j = 0, polygons(i)%nov+1
3073                   READ( 119 ) polygons(i)%vertices(j)%delete,                                     &
3074                               polygons(i)%vertices(j)%x,                                          &
3075                               polygons(i)%vertices(j)%y
3076                ENDDO
3077             ENDDO
3078             CLOSE(119)
3079
3080          ENDIF
3081#if defined( __parallel ) && ! defined ( __check )
3082          CALL MPI_BARRIER( comm2d, ierr )
3083#endif
3084       ENDDO
3085
3086!
3087!--    Allocate agent arrays and set attributes of the initial set of agents, which can be also
3088!--    periodically released at later times.
3089       ALLOCATE( agt_count  (nysg:nyng,nxlg:nxrg),                                                 &
3090                 grid_agents(nysg:nyng,nxlg:nxrg) )
3091!
3092!--    Allocate dummy arrays for pathfinding
3093       ALLOCATE( dummy_path_x(0:agt_path_size),                                                    &
3094                 dummy_path_y(0:agt_path_size) )
3095
3096       number_of_agents = 0
3097       sort_count_mas   = 0
3098       agt_count        = 0
3099
3100!
3101!--    Initialize counter for agent IDs
3102       grid_agents%id_counter = 1
3103
3104!
3105!--    Initialize all agents with dummy values (otherwise errors may occur within restart runs).
3106!--    The reason for this is still not clear and may be presumably caused by errors in the
3107!--    respective user-interface.
3108       zero_agent%agent_mask = .FALSE.
3109       zero_agent%block_nr      = -1
3110       zero_agent%group         = 0
3111       zero_agent%id            = 0_idp
3112       zero_agent%path_counter  = agt_path_size
3113       zero_agent%age           = 0.0_wp
3114       zero_agent%age_m         = 0.0_wp
3115       zero_agent%dt_sum        = 0.0_wp
3116       zero_agent%clo           = 0.0_wp
3117       zero_agent%energy_storage= 0.0_wp
3118       zero_agent%force_x       = 0.0_wp
3119       zero_agent%force_y       = 0.0_wp
3120       zero_agent%origin_x      = 0.0_wp
3121       zero_agent%origin_y      = 0.0_wp
3122       zero_agent%speed_abs     = 0.0_wp
3123       zero_agent%speed_e_x     = 0.0_wp
3124       zero_agent%speed_e_y     = 0.0_wp
3125       zero_agent%speed_des     = random_normal(desired_speed, des_sp_sig)
3126       zero_agent%speed_x       = 0.0_wp
3127       zero_agent%speed_y       = 0.0_wp
3128       zero_agent%ipt           = 0.0_wp
3129       zero_agent%x             = 0.0_wp
3130       zero_agent%y             = 0.0_wp
3131       zero_agent%path_x        = 0.0_wp
3132       zero_agent%path_y        = 0.0_wp
3133       zero_agent%t_x           = 0.0_wp
3134       zero_agent%t_y           = 0.0_wp
3135
3136!
3137!--    Set a seed value for the random number generator to be exclusively used for the agent code.
3138!--    The generated random numbers should be different on the different PEs.
3139       iran_agent = iran_agent + myid
3140
3141       CALL mas_create_agent( phase_init )
3142
3143    ENDIF
3144
3145!
3146!-- To avoid programm abort, assign agents array to the local version of first grid cell.
3147    number_of_agents = agt_count(nys,nxl)
3148    agents => grid_agents(nys,nxl)%agents(1:number_of_agents)
3149
3150 END SUBROUTINE mas_init
3151
3152!--------------------------------------------------------------------------------------------------!
3153! Description:
3154! ------------
3155!> Output of informative message about maximum agent number
3156!--------------------------------------------------------------------------------------------------!
3157 SUBROUTINE mas_last_actions
3158
3159    USE control_parameters,                                                                        &
3160        ONLY:  message_string
3161
3162    IMPLICIT NONE
3163
3164    WRITE(message_string,'(A,I8,A)') 'The maximumn number of agents during this run was',          &
3165                                     maximum_number_of_agents,                                     &
3166                                     '&Consider adjusting the INPUT parameter'//                   &
3167                                     '&dim_size_agtnum_manual accordingly for the next run.'
3168
3169    CALL message( 'mas_data_output_agents', 'PA0457', 0, 0, 0, 6, 0 )
3170
3171 END SUBROUTINE mas_last_actions
3172
3173!--------------------------------------------------------------------------------------------------!
3174! Description:
3175! ------------
3176!> Finds the shortest path from a start position to a target position using the A*-algorithm
3177!--------------------------------------------------------------------------------------------------!
3178 SUBROUTINE mas_nav_a_star( start_x, start_y, target_x, target_y, nsteps )
3179
3180    IMPLICIT NONE
3181
3182    INTEGER(iwp) ::  cur_node      !< current node of binary heap
3183    INTEGER(iwp) ::  il            !< counter (x)
3184    INTEGER(iwp) ::  neigh_node    !< neighbor node
3185    INTEGER(iwp) ::  node_counter  !< binary heap node counter
3186    INTEGER(iwp) ::  nsteps        !< number of steps
3187    INTEGER(iwp) ::  path_ag       !< index of agent path
3188    INTEGER(iwp) ::  som           !< size of mesh
3189    INTEGER(iwp) ::  steps         !< steps along the path
3190
3191    LOGICAL ::  target_reached  !< flag
3192
3193    REAL(wp) ::  new_cost      !< updated cost to reach node
3194    REAL(wp) ::  new_priority  !< priority of node to be added to queue
3195    REAL(wp) ::  start_x       !< x-coordinate agent
3196    REAL(wp) ::  start_y       !< y-coordinate agent
3197    REAL(wp) ::  rn_gate       !< random number for corner gate
3198    REAL(wp) ::  target_x      !< x-coordinate target
3199    REAL(wp) ::  target_y      !< y-coordinate target
3200!
3201!-- Coordinate Type
3202    TYPE coord
3203       REAL(wp) ::  x    !< x-coordinate
3204       REAL(wp) ::  x_s  !< x-coordinate (shifted)
3205       REAL(wp) ::  y    !< y-coordinate
3206       REAL(wp) ::  y_s  !< y-coordinate (shifted)
3207    END TYPE coord
3208
3209    TYPE(coord), DIMENSION(:), ALLOCATABLE, TARGET ::  path      !< path array
3210    TYPE(coord), DIMENSION(:), ALLOCATABLE, TARGET ::  tmp_path  !< temporary path for resizing
3211
3212    node_counter = 0
3213!
3214!-- Create temporary navigation mesh including agent and target positions
3215    CALL mas_nav_create_tmp_mesh( start_x, start_y, target_x, target_y, som )
3216    tmp_mesh(som)%cost_so_far = 0.0_wp
3217!
3218!-- Initialize priority queue
3219    heap_count = 0_iwp
3220    ALLOCATE( queue(0:100) )
3221    target_reached = .FALSE.
3222!
3223!-- Add starting point (agent position) to frontier (the frontier consists of all the nodes that
3224!-- are to be visited. The node with the smallest priority will be visited first. The priority
3225!-- consists of the distance from the start node to this node plus a minimal guess (direct
3226!-- distance) from this node to the goal). For the starting node, the priority is set to 0, as it's
3227!-- the only node thus far.
3228    CALL mas_heap_insert_item( som, 0.0_wp )
3229    cur_node = som
3230    DO WHILE ( heap_count > 0 )
3231!
3232!--    Step one: Pick lowest priority item from queue
3233       node_counter = node_counter + 1
3234       CALL mas_heap_extract_item(cur_node)
3235!
3236!--    Node 0 is the goal node
3237       IF ( cur_node == 0 )  THEN
3238          EXIT
3239       ENDIF
3240!
3241!--    Loop over all of cur_node's neighbors
3242       DO  il = 1, tmp_mesh(cur_node)%noc
3243          neigh_node = tmp_mesh(cur_node)%connected_vertices(il)
3244!
3245!--       Check, if the way from the start node to this neigh_node via cur_node is shorter than the
3246!--       previously found shortest path to it.
3247!--       If so, replace said cost and add neigh_node to the frontier.
3248!--       cost_so_far is initialized as 1.d12 so that all found distances should be smaller.
3249          new_cost   = tmp_mesh(cur_node)%cost_so_far + tmp_mesh(cur_node)%distance_to_vertex(il)
3250          IF ( new_cost < tmp_mesh(neigh_node)%cost_so_far )  THEN
3251             tmp_mesh(neigh_node)%cost_so_far = new_cost
3252             tmp_mesh(neigh_node)%origin_id   = cur_node
3253!
3254!--             Priority in the queue is cost_so_far + heuristic to goal
3255             new_priority = new_cost                                                               &
3256                            + heuristic(tmp_mesh(neigh_node)%x,                                    &
3257                              tmp_mesh(neigh_node)%y, tmp_mesh(0)%x,                               &
3258                              tmp_mesh(0)%y)
3259             CALL mas_heap_insert_item(neigh_node,new_priority)
3260          ENDIF
3261       ENDDO
3262    ENDDO
3263!
3264!-- Add nodes to a path array. To do this, we must backtrack from the target node to its origin and
3265!-- so on until a node is reached that has no origin (%origin_id == -1). This is the starting node.
3266    DEALLOCATE( queue )
3267    cur_node = 0
3268    steps = 0
3269    ALLOCATE( path(1:100) )
3270    DO WHILE ( cur_node /= -1 )
3271       steps = steps + 1
3272!
3273!--    Resize path array if necessary
3274       IF ( steps > SIZE(path) )  THEN
3275          ALLOCATE( tmp_path(1:steps-1) )
3276          tmp_path(1:steps-1) = path(1:steps-1)
3277          DEALLOCATE( path )
3278          ALLOCATE( path(1:2*(steps-1)) )
3279          path(1:steps-1) = tmp_path(1:steps-1)
3280          DEALLOCATE( tmp_path )
3281       ENDIF
3282       path(steps)%x = tmp_mesh(cur_node)%x
3283       path(steps)%y = tmp_mesh(cur_node)%y
3284       path(steps)%x_s = tmp_mesh(cur_node)%x_s
3285       path(steps)%y_s = tmp_mesh(cur_node)%y_s
3286       cur_node = tmp_mesh(cur_node)%origin_id
3287    ENDDO
3288!
3289!-- Add calculated intermittent targets to the path until either the target or the maximum number of
3290!-- intermittent targets is reached.
3291!-- Ignore starting point (reduce index by one), it is agent position.
3292    dummy_path_x = -1
3293    dummy_path_y = -1
3294    path_ag = 1
3295    steps = steps - 1
3296    nsteps = 0
3297    DO WHILE( steps > 0 .AND. path_ag <= agt_path_size )
3298!
3299!--    Each target point is randomly chosen along a line target along the bisector of the building
3300!--    corner that starts at corner_gate_start and has a width of corner_gate_width. This is to
3301!--    avoid clustering when opposing agent groups try to reach the same corner target.
3302       rn_gate = random_function(iran_agent) * corner_gate_width + corner_gate_start
3303       dummy_path_x(path_ag) = path(steps)%x + rn_gate * (path(steps)%x_s - path(steps)%x)
3304       dummy_path_y(path_ag) = path(steps)%y + rn_gate * (path(steps)%y_s - path(steps)%y)
3305       steps = steps - 1
3306       path_ag = path_ag + 1
3307       nsteps = nsteps + 1
3308    ENDDO
3309!
3310!-- Set current intermittent target of this agent
3311    DEALLOCATE( tmp_mesh, path )
3312
3313 END SUBROUTINE mas_nav_a_star
3314
3315!--------------------------------------------------------------------------------------------------!
3316! Description:
3317! ------------
3318!> Adds a connection between two points of the navigation mesh (one-way: in_mp1 to in_mp2)
3319!--------------------------------------------------------------------------------------------------!
3320 SUBROUTINE mas_nav_add_connection ( in_mp1, id2, in_mp2 )
3321
3322    IMPLICIT NONE
3323
3324    LOGICAL ::  connection_established  !< Flag to indicate if connection has already been established
3325
3326    INTEGER(iwp) ::  id2   !< ID of in_mp2
3327    INTEGER(iwp) ::  il    !< local counter
3328    INTEGER(iwp) ::  noc1  !< number of connections in in_mp1
3329
3330    INTEGER, DIMENSION(:), ALLOCATABLE ::  dum_cv  !< dummy array for connected_vertices
3331
3332    REAL(wp) ::  dist  !< Distance between the two points
3333
3334    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dum_dtv
3335
3336    TYPE(mesh_point) ::  in_mp1  !< mesh point that gets a new connection
3337    TYPE(mesh_point) ::  in_mp2  !< mesh point in_mp1 will be connected to
3338
3339    connection_established = .FALSE.
3340!
3341!-- Check if connection has already been established
3342    noc1 = SIZE( in_mp1%connected_vertices )
3343    DO  il = 1, in_mp1%noc
3344       IF ( in_mp1%connected_vertices(il) == id2 )  THEN
3345          connection_established = .TRUE.
3346          EXIT
3347       ENDIF
3348    ENDDO
3349
3350    IF ( .NOT. connection_established )  THEN
3351!
3352!--    Resize arrays, if necessary
3353       IF ( in_mp1%noc >= noc1 )  THEN
3354          ALLOCATE( dum_cv(1:noc1),dum_dtv(1:noc1) )
3355          dum_cv  = in_mp1%connected_vertices
3356          dum_dtv = in_mp1%distance_to_vertex
3357          DEALLOCATE( in_mp1%connected_vertices, in_mp1%distance_to_vertex )
3358          ALLOCATE( in_mp1%connected_vertices(1:2*noc1),                                           &
3359                    in_mp1%distance_to_vertex(1:2*noc1) )
3360          in_mp1%connected_vertices         = -999
3361          in_mp1%distance_to_vertex         = -999.
3362          in_mp1%connected_vertices(1:noc1) = dum_cv
3363          in_mp1%distance_to_vertex(1:noc1) = dum_dtv
3364       ENDIF
3365
3366!
3367!--    Add connection
3368       in_mp1%noc = in_mp1%noc+1
3369       dist = SQRT( (in_mp1%x - in_mp2%x)**2 + (in_mp1%y - in_mp2%y)**2 )
3370       in_mp1%connected_vertices(in_mp1%noc) = id2
3371       in_mp1%distance_to_vertex(in_mp1%noc) = dist
3372    ENDIF
3373
3374 END SUBROUTINE mas_nav_add_connection
3375
3376!--------------------------------------------------------------------------------------------------!
3377! Description:
3378! ------------
3379!> Adds a vertex (curren position of agent or target) to the existing tmp_mesh
3380!--------------------------------------------------------------------------------------------------!
3381 SUBROUTINE mas_nav_add_vertex_to_mesh ( in_mp, in_id )
3382
3383    IMPLICIT NONE
3384
3385
3386    INTEGER(iwp) ::  in_id  !< vertex id of tested mesh point
3387    INTEGER(iwp) ::  jl     !< mesh point counter
3388    INTEGER(iwp) ::  pl     !< polygon counter
3389    INTEGER(iwp) ::  vl     !< vertex counter
3390    INTEGER(iwp) ::  pid_t  !< polygon id of tested mesh point
3391    INTEGER(iwp) ::  vid_t  !< vertex id of tested mesh point
3392
3393    LOGICAL ::  intersection_found  !< flag
3394    LOGICAL ::  is_left_n           !< local switch
3395    LOGICAL ::  is_left_p           !< local switch
3396    LOGICAL ::  is_right_n          !< local switch
3397    LOGICAL ::  is_right_p          !< local switch
3398
3399    REAL(wp) ::  v1x  !< x-coordinate of test vertex 1 for intersection test
3400    REAL(wp) ::  v1y  !< y-coordinate of test vertex 1 for intersection test
3401    REAL(wp) ::  v2x  !< x-coordinate of test vertex 2 for intersection test
3402    REAL(wp) ::  v2y  !< y-coordinate of test vertex 2 for intersection test
3403    REAL(wp) ::  x    !< x-coordinate of current mesh point
3404    REAL(wp) ::  x_t  !< x-coordinate of tested mesh point
3405    REAL(wp) ::  y    !< y-coordinate of current mesh point
3406    REAL(wp) ::  y_t  !< y-coordinate of tested mesh point
3407
3408    TYPE(mesh_point) ::  in_mp  !< Input mesh point
3409!
3410!--
3411    x = in_mp%x
3412    y = in_mp%y
3413    DO  jl = 0, SIZE( tmp_mesh )-2
3414       IF ( in_id == jl )  CYCLE
3415!
3416!--    Ignore mesh points with 0 connections
3417       IF ( tmp_mesh(jl)%polygon_id /= -1 )  THEN
3418          IF ( tmp_mesh(jl)%noc == 0 )  CYCLE
3419       ENDIF
3420       x_t = tmp_mesh(jl)%x
3421       y_t = tmp_mesh(jl)%y
3422       pid_t = tmp_mesh(jl)%polygon_id
3423       vid_t = tmp_mesh(jl)%vertex_id
3424!
3425!--    If the connecting line between the target and a mesh point points into the mesh point's
3426!--    polygon, no connection will be established between the two points. This is the case if the
3427!--    previous (next, n) vertex of the polygon is right of the connecting line and the next
3428!--    (previous, p) vertex of the polygon is left of the connecting line.
3429       IF ( pid_t > 0  .AND.  pid_t <= SIZE( polygons ) )  THEN
3430          is_left_p  = is_left(  x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t-1)%x,              &
3431                                 polygons(pid_t)%vertices(vid_t-1)%y )
3432          is_left_n  = is_left(  x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t+1)%x,              &
3433                                 polygons(pid_t)%vertices(vid_t+1)%y )
3434          is_right_p = is_right( x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t-1)%x,              &
3435                                 polygons(pid_t)%vertices(vid_t-1)%y )
3436          is_right_n = is_right( x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t+1)%x,              &
3437                                 polygons(pid_t)%vertices(vid_t+1)%y )
3438          IF ( ( is_left_p .AND. is_right_n )  .OR.  ( is_right_p .AND. is_left_n ) )  CYCLE
3439       ENDIF
3440!
3441!--    For each edge of each polygon, check if it intersects with the potential connection. If at
3442!--    least one intersection is found, no connection can be made.
3443       intersection_found = .FALSE.
3444       DO  pl = 1, SIZE( polygons )
3445          DO  vl = 1, polygons(pl)%nov
3446             v1x = polygons(pl)%vertices(vl)%x
3447             v1y = polygons(pl)%vertices(vl)%y
3448             v2x = polygons(pl)%vertices(vl+1)%x
3449             v2y = polygons(pl)%vertices(vl+1)%y
3450             intersection_found = intersect(x,y,x_t,y_t,v1x,v1y,v2x,v2y)
3451             IF ( intersection_found )  THEN
3452                EXIT
3453             ENDIF
3454          ENDDO
3455          IF ( intersection_found )  EXIT
3456       ENDDO
3457       IF ( intersection_found )  CYCLE
3458!
3459!--    If neither of the above two tests was true, a connection will be established between the two
3460!--    mesh points.
3461       CALL mas_nav_add_connection(in_mp,jl, tmp_mesh(jl))
3462       CALL mas_nav_add_connection(tmp_mesh(jl),in_id, in_mp)
3463    ENDDO
3464    CALL mas_nav_reduce_connections(in_mp)
3465
3466 END SUBROUTINE mas_nav_add_vertex_to_mesh
3467
3468!--------------------------------------------------------------------------------------------------!
3469! Description:
3470! ------------
3471!> Creates a temporary copy of the navigation mesh to be used for pathfinding
3472!--------------------------------------------------------------------------------------------------!
3473 SUBROUTINE mas_nav_create_tmp_mesh( a_x, a_y, t_x, t_y, som )
3474
3475    IMPLICIT NONE
3476
3477    INTEGER(iwp) ::  im   !< local mesh point counter
3478    INTEGER(iwp) ::  noc  !< number of connetions
3479    INTEGER(iwp) ::  som  !< size of mesh
3480
3481    REAL(wp) ::  a_x  !< x-coordinate agent
3482    REAL(wp) ::  a_y  !< y-coordinate agent
3483    REAL(wp) ::  t_x  !< x-coordinate target
3484    REAL(wp) ::  t_y  !< y-coordinate target
3485!
3486!-- Give tmp_mesh the size of mesh
3487    som = SIZE( mesh ) + 1
3488    ALLOCATE( tmp_mesh(0:som) )
3489!
3490!-- Give the allocatable variables in tmp_mesh their respctive sizes
3491    DO  im = 1, som-1
3492       noc = mesh(im)%noc
3493       ALLOCATE( tmp_mesh(im)%connected_vertices(1:noc) )
3494       ALLOCATE( tmp_mesh(im)%distance_to_vertex(1:noc) )
3495    ENDDO
3496!
3497!-- Copy mesh to tmp_mesh
3498    tmp_mesh(1:som-1) = mesh(1:som-1)
3499!
3500!-- Add target point ...
3501    CALL mas_nav_init_mesh_point(tmp_mesh(0),-1_iwp,-1_iwp,t_x, t_y)
3502    CALL mas_nav_add_vertex_to_mesh(tmp_mesh(0),0_iwp)
3503!
3504!-- ... and start point to temp mesh
3505    CALL mas_nav_init_mesh_point(tmp_mesh(som),-1_iwp,-1_iwp,a_x, a_y)
3506    CALL mas_nav_add_vertex_to_mesh(tmp_mesh(som),som)
3507
3508 END SUBROUTINE mas_nav_create_tmp_mesh
3509
3510
3511!--------------------------------------------------------------------------------------------------!
3512! Description:
3513! ------------
3514!> Finds the shortest path from an agents' position to her target. As the actual pathfinding
3515!> algorithm uses the obstacle corners and then shifts them outward after pathfinding, cases can
3516!> occur in which the connection between these intermittent targets then intersect with obstacles.
3517!> To remedy this the pathfinding algorithm is then run on every two subsequent intermittent targets
3518!> iteratively and new intermittent targets may be added to the path this way.
3519!--------------------------------------------------------------------------------------------------!
3520 SUBROUTINE mas_nav_find_path( nl )
3521
3522    IMPLICIT NONE
3523
3524    INTEGER(iwp) ::  il            !< local counter
3525    INTEGER(iwp) ::  jl            !< local counter
3526    INTEGER(iwp) ::  kl            !< local counter
3527    INTEGER(iwp) ::  nl            !< local agent counter
3528    INTEGER(iwp) ::  nsteps_dummy  !< number of steps on path
3529    INTEGER(iwp) ::  nsteps_total  !< number of steps on path
3530
3531    REAL(wp), DIMENSION(0:30) ::  ld_path_x  !< local dummy agent path to target (x)
3532    REAL(wp), DIMENSION(0:30) ::  ld_path_y  !< local dummy agent path to target (y)
3533!
3534!-- Initialize agent path arrays
3535    agents(nl)%path_x    = -1
3536    agents(nl)%path_y    = -1
3537    agents(nl)%path_x(0) = agents(nl)%x
3538    agents(nl)%path_y(0) = agents(nl)%y
3539!
3540!-- Calculate initial path
3541    CALL mas_nav_a_star( agents(nl)%x, agents(nl)%y, agents(nl)%t_x, agents(nl)%t_y, nsteps_total )
3542!
3543!-- Set the rest of the agent path that was just calculated
3544    agents(nl)%path_x(1:nsteps_total) = dummy_path_x(1:nsteps_total)
3545    agents(nl)%path_y(1:nsteps_total) = dummy_path_y(1:nsteps_total)
3546!
3547!-- Iterate through found path and check more intermittent targets need to be added. For this, run
3548!-- pathfinding between every two consecutive intermittent targets.
3549    DO  il = 0, MIN( agt_path_size-1, nsteps_total-1 )
3550!
3551!--    Pathfinding between two consecutive intermittent targets
3552       CALL mas_nav_a_star( agents(nl)%path_x(il),   agents(nl)%path_y(il),                        &
3553                            agents(nl)%path_x(il+1), agents(nl)%path_y(il+1),                      &
3554                            nsteps_dummy )
3555       nsteps_dummy = nsteps_dummy - 1
3556!
3557!--    If additional intermittent targets are found, add them to the path
3558       IF ( nsteps_dummy > 0 )  THEN
3559          ld_path_x = -1
3560          ld_path_y = -1
3561          ld_path_x(il+1:il+nsteps_dummy) = dummy_path_x(1:nsteps_dummy)
3562          ld_path_y(il+1:il+nsteps_dummy) = dummy_path_y(1:nsteps_dummy)
3563          kl = 1
3564          DO  jl = il+1,nsteps_total
3565            ld_path_x( il+nsteps_dummy+kl ) = agents(nl)%path_x(jl)
3566            ld_path_y( il+nsteps_dummy+kl ) = agents(nl)%path_y(jl)
3567            kl = kl + 1
3568            IF ( kl > agt_path_size )  EXIT
3569          ENDDO
3570          nsteps_total = MIN( nsteps_total + nsteps_dummy, agt_path_size )
3571          agents(nl)%path_x(il+1:nsteps_total) = ld_path_x(il+1:nsteps_total)
3572          agents(nl)%path_y(il+1:nsteps_total) = ld_path_y(il+1:nsteps_total)
3573       ENDIF
3574
3575    ENDDO
3576!
3577!-- Reset path counter to first intermittent target
3578    agents(nl)%path_counter = 1
3579
3580 END SUBROUTINE mas_nav_find_path
3581
3582!--------------------------------------------------------------------------------------------------!
3583! Description:
3584! ------------
3585!> Reduces the size of connection array to the amount of actual connections after all connetions
3586!> were added to a mesh point
3587!--------------------------------------------------------------------------------------------------!
3588 SUBROUTINE mas_nav_reduce_connections ( in_mp )
3589
3590    IMPLICIT NONE
3591
3592    INTEGER(iwp) ::  noc  !< number of connections
3593
3594    INTEGER, DIMENSION(:), ALLOCATABLE ::  dum_cv    !< dummy connected_vertices
3595
3596    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dum_dtv  !< dummy distance_to_vertex
3597
3598    TYPE(mesh_point) ::  in_mp
3599
3600    noc = in_mp%noc
3601    ALLOCATE( dum_cv(1:noc),dum_dtv(1:noc) )
3602    dum_cv  = in_mp%connected_vertices(1:noc)
3603    dum_dtv = in_mp%distance_to_vertex(1:noc)
3604    DEALLOCATE( in_mp%connected_vertices, in_mp%distance_to_vertex )
3605    ALLOCATE( in_mp%connected_vertices(1:noc),                                                     &
3606              in_mp%distance_to_vertex(1:noc) )
3607    in_mp%connected_vertices(1:noc) = dum_cv(1:noc)
3608    in_mp%distance_to_vertex(1:noc) = dum_dtv(1:noc)
3609
3610 END SUBROUTINE mas_nav_reduce_connections
3611
3612!--------------------------------------------------------------------------------------------------!
3613! Description:
3614! ------------
3615!> Initializes a point of the navigation mesh
3616!--------------------------------------------------------------------------------------------------!
3617 SUBROUTINE mas_nav_init_mesh_point ( in_mp, pid, vid, x, y )
3618
3619    IMPLICIT NONE
3620
3621    INTEGER(iwp) ::  pid  !< polygon ID
3622    INTEGER(iwp) ::  vid  !< vertex ID
3623
3624    REAL(wp) ::  x  !< x-coordinate
3625    REAL(wp) ::  y  !< y-coordinate
3626
3627    TYPE(mesh_point) ::  in_mp  !< mesh point to be initialized
3628
3629    in_mp%origin_id          = -1
3630    in_mp%polygon_id         = pid
3631    in_mp%vertex_id          = vid
3632    in_mp%cost_so_far        = 1.d12
3633    in_mp%x                  = x
3634    in_mp%y                  = y
3635    in_mp%x_s                = x
3636    in_mp%y_s                = y
3637    ALLOCATE( in_mp%connected_vertices(1:100),                                                     &
3638              in_mp%distance_to_vertex(1:100) )
3639    in_mp%connected_vertices = -999
3640    in_mp%distance_to_vertex = -999.
3641    in_mp%noc                = 0
3642
3643 END SUBROUTINE mas_nav_init_mesh_point
3644
3645!--------------------------------------------------------------------------------------------------!
3646! Description:
3647! ------------
3648!> Reading of namlist from parin file
3649!--------------------------------------------------------------------------------------------------!
3650 SUBROUTINE mas_parin
3651
3652    USE control_parameters,                                                                        &
3653        ONLY: agent_time_unlimited, multi_agent_system_end, multi_agent_system_start
3654
3655    IMPLICIT NONE
3656
3657    CHARACTER (LEN=80) ::  line  !<
3658
3659    NAMELIST /agent_parameters/  a_rand_target,                                                    &
3660                                 adx,                                                              &
3661                                 ady,                                                              &
3662                                 agent_maximum_age,                                                &
3663                                 agent_time_unlimited,                                             &
3664                                 alloc_factor_mas,                                                 &
3665                                 asl,                                                              &
3666                                 asn,                                                              &
3667                                 asr,                                                              &
3668                                 ass,                                                              &
3669                                 at_x,                                                             &
3670                                 at_y,                                                             &
3671                                 bc_mas_lr,                                                        &
3672                                 bc_mas_ns,                                                        &
3673                                 coll_t_0,                                                         &
3674                                 corner_gate_start,                                                &
3675                                 corner_gate_width,                                                &
3676                                 deallocate_memory_mas,                                            &
3677                                 dim_size_agtnum_manual,                                           &
3678                                 dim_size_factor_agtnum,                                           &
3679                                 dist_to_int_target,                                               &
3680                                 dt_agent,                                                         &
3681                                 dt_arel,                                                          &
3682                                 dt_write_agent_data,                                              &
3683                                 end_time_arel,                                                    &
3684                                 max_dist_from_path,                                               &
3685                                 min_nr_agent,                                                     &
3686                                 multi_agent_system_end,                                           &
3687                                 multi_agent_system_start,                                         &
3688                                 number_of_agent_groups,                                           &
3689                                 radius_agent,                                                     &
3690                                 random_start_position_agents,                                     &
3691                                 read_agents_from_restartfile,                                     &
3692                                 repuls_agent,                                                     &
3693                                 repuls_wall,                                                      &
3694                                 scan_radius_agent,                                                &
3695                                 sigma_rep_agent,                                                  &
3696                                 sigma_rep_wall,                                                   &
3697                                 step_dealloc_mas,                                                 &
3698                                 tau_accel_agent
3699
3700!
3701!-- Try to find agent package
3702    REWIND ( 11 )
3703    line = ' '
3704    DO WHILE ( INDEX( line, '&agent_parameters' ) == 0 )
3705       READ ( 11, '(A)', END=20 )  line
3706    ENDDO
3707    BACKSPACE ( 11 )
3708
3709!
3710!-- Read user-defined namelist
3711    READ ( 11, agent_parameters, ERR = 10, END = 20 )
3712
3713!
3714!-- Set flag that indicates that agents are switched on
3715    agents_active = .TRUE.
3716    GOTO 20
3717
371810    BACKSPACE( 11 )
3719    READ( 11 , '(A)') line
3720    CALL parin_fail_message( 'agent_parameters', line )
3721
372220    CONTINUE
3723
3724 END SUBROUTINE mas_parin
3725
3726!--------------------------------------------------------------------------------------------------!
3727! Description:
3728! ------------
3729!> Routine for the whole processor
3730!> Sort all agents into the 4 respective subgrid boxes
3731!--------------------------------------------------------------------------------------------------!
3732 SUBROUTINE mas_ps_sort_in_subboxes
3733
3734    IMPLICIT NONE
3735
3736    INTEGER(iwp) ::  i           !< grid box (x)
3737    INTEGER(iwp) ::  ip          !< counter (x)
3738    INTEGER(iwp) ::  is          !< box counter
3739    INTEGER(iwp) ::  j           !< grid box (y)
3740    INTEGER(iwp) ::  jp          !< counter (y)
3741    INTEGER(iwp) ::  m           !< sorting index
3742    INTEGER(iwp) ::  n           !< agent index
3743    INTEGER(iwp) ::  nn          !< agent counter
3744    INTEGER(iwp) ::  sort_index  !< sorting index
3745
3746    INTEGER(iwp), DIMENSION(0:3) ::  sort_count  !< number of agents in one subbox
3747
3748    TYPE(agent_type), DIMENSION(:,:), ALLOCATABLE ::  sort_agents  !< sorted agent array
3749
3750    DO  ip = nxl, nxr
3751       DO  jp = nys, nyn
3752          number_of_agents = agt_count(jp,ip)
3753          IF ( number_of_agents <= 0 )  CYCLE
3754          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
3755
3756          nn = 0
3757          sort_count = 0
3758          ALLOCATE( sort_agents(number_of_agents, 0:3) )
3759
3760          DO  n = 1, number_of_agents
3761             sort_index = 0
3762
3763             IF ( agents(n)%agent_mask )  THEN
3764                nn = nn + 1
3765!
3766!--             Sorting agents with a binary scheme
3767!--             sort_index=11_2=3_10 -> agent at the left,south subgridbox
3768!--             sort_index=10_2=2_10 -> agent at the left,north subgridbox
3769!--             sort_index=01_2=1_10 -> agent at the right,south subgridbox
3770!--             sort_index=00_2=0_10 -> agent at the right,north subgridbox
3771!--             For this the center of the gridbox is calculated
3772                i = (agents(n)%x + 0.5_wp * dx) * ddx
3773                j = (agents(n)%y + 0.5_wp * dy) * ddy
3774
3775                IF ( i == ip )  sort_index = sort_index + 2
3776                IF ( j == jp )  sort_index = sort_index + 1
3777
3778                sort_count(sort_index) = sort_count(sort_index) + 1
3779                m = sort_count(sort_index)
3780                sort_agents(m,sort_index) = agents(n)
3781                sort_agents(m,sort_index)%block_nr = sort_index
3782             ENDIF
3783          ENDDO
3784
3785          nn = 0
3786          DO  is = 0,3
3787             grid_agents(jp,ip)%start_index(is) = nn + 1
3788             DO  n = 1,sort_count(is)
3789                nn = nn + 1
3790                agents(nn) = sort_agents(n,is)
3791             ENDDO
3792             grid_agents(jp,ip)%end_index(is) = nn
3793          ENDDO
3794
3795          number_of_agents = nn
3796          agt_count(jp,ip) = number_of_agents
3797          DEALLOCATE( sort_agents )
3798       ENDDO
3799    ENDDO
3800
3801 END SUBROUTINE mas_ps_sort_in_subboxes
3802
3803#if defined( __parallel )
3804!--------------------------------------------------------------------------------------------------!
3805! Description:
3806! ------------
3807!> Move all agents not marked for deletion to lowest indices (packing)
3808!--------------------------------------------------------------------------------------------------!
3809 SUBROUTINE mas_ps_pack
3810
3811    IMPLICIT NONE
3812
3813    INTEGER(iwp) ::  n   !< agent counter
3814    INTEGER(iwp) ::  nn  !< number of agents
3815!
3816!-- Find out elements marked for deletion and move data from highest index values to these free
3817!-- indices.
3818    nn = number_of_agents
3819
3820    DO WHILE ( .NOT. agents(nn)%agent_mask )
3821       nn = nn-1
3822       IF ( nn == 0 )  EXIT
3823    ENDDO
3824
3825    IF ( nn > 0 )  THEN
3826       DO  n = 1, number_of_agents
3827          IF ( .NOT. agents(n)%agent_mask )  THEN
3828             agents(n) = agents(nn)
3829             nn = nn - 1
3830             DO WHILE ( .NOT. agents(nn)%agent_mask )
3831                nn = nn-1
3832                IF ( n == nn )  EXIT
3833             ENDDO
3834          ENDIF
3835          IF ( n == nn )  EXIT
3836       ENDDO
3837    ENDIF
3838
3839!
3840!-- The number of deleted agents has been determined in routines mas_boundary_conds,
3841!-- mas_droplet_collision, and mas_eh_exchange_horiz.
3842    number_of_agents = nn
3843
3844 END SUBROUTINE mas_ps_pack
3845#endif
3846
3847!--------------------------------------------------------------------------------------------------!
3848! Description:
3849! ------------
3850!> Sort agents in each sub-grid box into two groups: agents that already completed the LES
3851!> timestep, and agents that need further timestepping to complete the LES timestep.
3852!--------------------------------------------------------------------------------------------------!
3853!    SUBROUTINE mas_ps_sort_timeloop_done
3854!
3855!       IMPLICIT NONE
3856!
3857!       INTEGER(iwp) :: end_index      !< agent end index for each sub-box
3858!       INTEGER(iwp) :: i              !< index of agent grid box in x-direction
3859!       INTEGER(iwp) :: j              !< index of agent grid box in y-direction
3860!       INTEGER(iwp) :: n              !< running index for number of agents
3861!       INTEGER(iwp) :: nb             !< index of subgrid boux
3862!       INTEGER(iwp) :: nf             !< indices for agents in each sub-box that already finalized their substeps
3863!       INTEGER(iwp) :: nnf            !< indices for agents in each sub-box that need further treatment
3864!       INTEGER(iwp) :: num_finalized  !< number of agents in each sub-box that already finalized their substeps
3865!       INTEGER(iwp) :: start_index    !< agent start index for each sub-box
3866!
3867!       TYPE(agent_type), DIMENSION(:), ALLOCATABLE :: sort_agents  !< temporary agent array
3868!
3869!       DO  i = nxl, nxr
3870!          DO  j = nys, nyn
3871!
3872!             number_of_agents = agt_count(j,i)
3873!             IF ( number_of_agents <= 0 )  CYCLE
3874!
3875!             agents => grid_agents(j,i)%agents(1:number_of_agents)
3876!
3877!             DO  nb = 0, 3
3878!
3879!--             Obtain start and end index for each subgrid box
3880!                start_index = grid_agents(j,i)%start_index(nb)
3881!                end_index   = grid_agents(j,i)%end_index(nb)
3882!
3883!--             Allocate temporary array used for sorting
3884!                ALLOCATE( sort_agents(start_index:end_index) )
3885!
3886!--             Determine number of agents already completed the LES
3887!--             timestep, and write them into a temporary array
3888!                nf = start_index
3889!                num_finalized = 0
3890!                DO  n = start_index, end_index
3891!                   IF ( dt_3d - agents(n)%dt_sum < 1E-8_wp )  THEN
3892!                      sort_agents(nf) = agents(n)
3893!                      nf              = nf + 1
3894!                      num_finalized   = num_finalized + 1
3895!                   ENDIF
3896!                ENDDO
3897!
3898!--             Determine number of agents that not completed the LES
3899!--             timestep, and write them into a temporary array
3900!                nnf = nf
3901!                DO  n = start_index, end_index
3902!                   IF ( dt_3d - agents(n)%dt_sum > 1E-8_wp )  THEN
3903!                      sort_agents(nnf) = agents(n)
3904!                      nnf              = nnf + 1
3905!                   ENDIF
3906!                ENDDO
3907!
3908!--             Write back sorted agents
3909!                agents(start_index:end_index) =                          &
3910!                                        sort_agents(start_index:end_index)
3911!
3912!--             Determine updated start_index, used to masked already
3913!--             completed agents.
3914!                grid_agents(j,i)%start_index(nb) =                     &
3915!                                   grid_agents(j,i)%start_index(nb)    &
3916!                                 + num_finalized
3917!
3918!--             Deallocate dummy array
3919!                DEALLOCATE ( sort_agents )
3920!
3921!--             Finally, if number of non-completed agents is non zero
3922!--             in any of the sub-boxes, set control flag appropriately.
3923!                IF ( nnf > nf )                                             &
3924!                   grid_agents(j,i)%time_loop_done = .FALSE.
3925!
3926!             ENDDO
3927!          ENDDO
3928!       ENDDO
3929!
3930!    END SUBROUTINE mas_ps_sort_timeloop_done
3931
3932!--------------------------------------------------------------------------------------------------!
3933! Description:
3934! ------------
3935!> Calls social forces calculations
3936!--------------------------------------------------------------------------------------------------!
3937 SUBROUTINE mas_timestep_forces_call ( ip, jp )
3938
3939    IMPLICIT NONE
3940
3941    INTEGER(iwp) ::  ip  !< counter, x-direction
3942    INTEGER(iwp) ::  jp  !< counter, y-direction
3943    INTEGER(iwp) ::  n   !< loop variable over all agents in a grid box
3944
3945!
3946!-- Get direction for all agents in current grid cell
3947    CALL mas_agent_direction
3948
3949    DO  n = 1, number_of_agents
3950
3951       force_x = 0.0_wp
3952       force_y = 0.0_wp
3953
3954       CALL mas_timestep_social_forces ( 'acceleration', n, ip, jp )
3955
3956       CALL mas_timestep_social_forces ( 'other_agents', n, ip, jp )
3957
3958       CALL mas_timestep_social_forces ( 'walls',        n, ip, jp )
3959!
3960!--    Update forces
3961       agents(n)%force_x = force_x
3962       agents(n)%force_y = force_y
3963    ENDDO
3964
3965 END SUBROUTINE mas_timestep_forces_call
3966
3967!--------------------------------------------------------------------------------------------------!
3968! Description:
3969! ------------
3970!> Euler timestep of agent transport
3971!--------------------------------------------------------------------------------------------------!
3972 SUBROUTINE mas_timestep
3973
3974    IMPLICIT NONE
3975
3976    INTEGER(iwp) ::  n  !< loop variable over all agents in a grid box
3977
3978    REAL(wp) ::  abs_v  !< absolute value of velocity
3979    REAL(wp) ::  abs_f  !< absolute value of force
3980
3981    DO  n = 1, number_of_agents
3982!
3983!--    Limit absolute force to a maximum to prevent unrealistic acceleration
3984       abs_f = SQRT( ( agents(n)%force_x )**2 + ( agents(n)%force_y )**2 )
3985       IF ( abs_f > 20. )  THEN
3986          agents(n)%force_x = agents(n)%force_x * 20. / abs_f
3987          agents(n)%force_y = agents(n)%force_y * 20. / abs_f
3988       ENDIF
3989!
3990!--    Update agent speed
3991       agents(n)%speed_x = agents(n)%speed_x + agents(n)%force_x * dt_agent
3992       agents(n)%speed_y = agents(n)%speed_y + agents(n)%force_y * dt_agent
3993!
3994!--    Reduction of agent speed to maximum agent speed
3995       abs_v = SQRT( ( agents(n)%speed_x )**2 + ( agents(n)%speed_y )**2 )
3996       IF ( abs_v > v_max_agent )  THEN
3997          agents(n)%speed_x = agents(n)%speed_x * v_max_agent / abs_v
3998          agents(n)%speed_y = agents(n)%speed_y * v_max_agent / abs_v
3999       ENDIF
4000!
4001!--    Update agent position
4002       agents(n)%x = agents(n)%x + agents(n)%speed_x * dt_agent
4003       agents(n)%y = agents(n)%y + agents(n)%speed_y * dt_agent
4004!
4005!--    Update absolute value of agent speed
4006       agents(n)%speed_abs = abs_v
4007!
4008!--    Increment the agent age and the total time that the agent has advanced within the agent
4009!--    timestep procedure
4010       agents(n)%age_m  = agents(n)%age
4011       agents(n)%age    = agents(n)%age    + dt_agent
4012       agents(n)%dt_sum = agents(n)%dt_sum + dt_agent
4013!
4014!--    Check whether there is still an agent that has not yet completed the total LES timestep
4015       IF ( ( dt_3d - agents(n)%dt_sum ) > 1E-8_wp )  THEN
4016          dt_3d_reached_l_mas = .FALSE.
4017       ENDIF
4018
4019    ENDDO
4020
4021 END SUBROUTINE mas_timestep
4022
4023!--------------------------------------------------------------------------------------------------!
4024! Description:
4025! ------------
4026!> Calculates the Social Forces (Helbing and Molnar, 1995) that the agent experiences due to
4027!> acceleration towards target and repulsion by obstacles
4028!--------------------------------------------------------------------------------------------------!
4029 SUBROUTINE mas_timestep_social_forces ( mode, nl, ip, jp )
4030
4031    IMPLICIT NONE
4032
4033    REAL(wp), PARAMETER ::  k_pl = 1.5  !< factor for collision avoidance
4034
4035    CHARACTER (LEN=*) ::  mode  !< identifier for the mode of calculation
4036
4037    INTEGER(iwp) ::  ij_dum      !< index of nearest wall
4038    INTEGER(iwp) ::  il          !< index variable along x
4039    INTEGER(iwp) ::  ip          !< index variable along x
4040    INTEGER(iwp) ::  jl          !< index variable along y
4041    INTEGER(iwp) ::  jp          !< index variable along y
4042    INTEGER(iwp) ::  nl          !< loop variable over all agents in a grid box
4043    INTEGER(iwp) ::  no          !< loop variable over all agents in a grid box
4044    INTEGER(iwp) ::  noa         !< amount of agents in a grid box
4045    INTEGER(iwp) ::  sc_x_end    !< index for scan for topography/other agents
4046    INTEGER(iwp) ::  sc_x_start  !< index for scan for topography/other agents
4047    INTEGER(iwp) ::  sc_y_end    !< index for scan for topography/other agents
4048    INTEGER(iwp) ::  sc_y_start  !< index for scan for topography/other agents
4049
4050    LOGICAL ::  corner_found  !< flag that indicates a corner has been found near agent
4051
4052    REAL(wp) ::  a_pl             !< factor for collision avoidance
4053    REAL(wp) ::  ax_semimaj       !< semiminor axis of repulsive ellipse
4054    REAL(wp) ::  b_pl             !< factor for collision avoidance
4055    REAL(wp) ::  c_pl             !< factor for collision avoidance
4056    REAL(wp) ::  coll_t           !< time at which the next collision would happen
4057    REAL(wp) ::  d_coll_t_0       !< inverse of collision cutoff time
4058    REAL(wp) ::  d_pl             !< factor for collision avoidance
4059    REAL(wp) ::  ddum_f           !< dummy devisor collision avoidance
4060    REAL(wp) ::  dist             !< distance to obstacle
4061    REAL(wp) ::  dist_sq          !< distance to obstacle squared
4062    REAL(wp) ::  pos_rel_x        !< relative position of two agents (x)
4063    REAL(wp) ::  pos_rel_y        !< relative position of two agents (y)
4064    REAL(wp) ::  r_sq             !< y-position
4065    REAL(wp) ::  sra              !< scan radius (agents)
4066    REAL(wp) ::  srw              !< local variable for scan radius (walls)
4067    REAL(wp) ::  v_rel_x          !< relative velocity (x); collision avoidance
4068    REAL(wp) ::  v_rel_y          !< relative velocity (y); collision avoidance
4069    REAL(wp) ::  x_a              !< x-position
4070    REAL(wp) ::  x_wall           !< x-position of wall
4071    REAL(wp) ::  y_a              !< y-position
4072    REAL(wp) ::  y_wall           !< y-position of wall
4073
4074    TYPE(agent_type), DIMENSION(:), POINTER ::  l_agts  !< agents that repulse current agent
4075
4076!
4077!-- Initialization
4078    x_a = agents(nl)%x
4079    y_a = agents(nl)%y
4080
4081    SELECT CASE ( TRIM( mode ) )
4082!
4083!--    Calculation of force due to agent trying to approach desired velocity
4084       CASE ( 'acceleration' )
4085
4086          force_x = force_x + d_tau_accel_agent                                                    &
4087                              * ( agents(nl)%speed_des*agents(nl)%speed_e_x - agents(nl)%speed_x )
4088
4089          force_y = force_y + d_tau_accel_agent                                                    &
4090                              * ( agents(nl)%speed_des*agents(nl)%speed_e_y - agents(nl)%speed_y )
4091
4092!
4093!--    Calculation of repulsive forces by other agents in a radius around the current one
4094       CASE ( 'other_agents' )
4095
4096          sra = scan_radius_agent
4097          d_coll_t_0 = 1./coll_t_0
4098!
4099!--       Find relevant gridboxes (those that could contain agents within scan radius)
4100          sc_x_start = FLOOR( (x_a - sra) * ddx )
4101          sc_x_end   = FLOOR( (x_a + sra) * ddx )
4102          sc_y_start = FLOOR( (y_a - sra) * ddx )
4103          sc_y_end   = FLOOR( (y_a + sra) * ddx )
4104          IF ( sc_x_start < nxlg ) sc_x_start = nxlg
4105          IF ( sc_x_end   > nxrg ) sc_x_end   = nxrg
4106          IF ( sc_y_start < nysg ) sc_y_start = nysg
4107          IF ( sc_y_end   > nyng ) sc_y_end   = nyng
4108
4109          sra = sra**2
4110!
4111!--       Loop over all previously found relevant gridboxes
4112          DO  il = sc_x_start, sc_x_end
4113             DO  jl = sc_y_start, sc_y_end
4114                noa = agt_count(jl,il)
4115                IF ( noa <= 0 )  CYCLE
4116                l_agts => grid_agents(jl,il)%agents(1:noa)
4117                DO  no = 1, noa
4118!
4119!--                Skip self
4120                   IF ( jl == jp  .AND.  il == ip  .AND.  no == nl )  CYCLE
4121                   pos_rel_x = l_agts(no)%x - x_a
4122                   pos_rel_y = l_agts(no)%y - y_a
4123                   dist_sq = pos_rel_x**2 + pos_rel_y**2
4124                   IF ( dist_sq > sra )  CYCLE
4125                   r_sq    = (2*radius_agent)**2
4126                   v_rel_x   = agents(nl)%speed_x - l_agts(no)%speed_x
4127                   v_rel_y   = agents(nl)%speed_y - l_agts(no)%speed_y
4128!
4129!--                Collision is already occuring, default to standard social forces.
4130                   IF ( dist_sq <= r_sq )  THEN
4131                      dist = SQRT( dist_sq ) + 1.0d-12
4132                      ax_semimaj = 0.5_wp * SQRT( dist )
4133
4134                      force_x = force_x - 0.125_wp * repuls_agent                                  &
4135                                     * d_sigma_rep_agent / ax_semimaj                              &
4136                                     * EXP( - ax_semimaj * d_sigma_rep_agent )                     &
4137                                     * ( pos_rel_x / dist )
4138
4139                      force_y = force_y - 0.125_wp * repuls_agent                                  &
4140                                     * d_sigma_rep_agent / ax_semimaj                              &
4141                                     * EXP( - ax_semimaj * d_sigma_rep_agent )                     &
4142                                     * ( pos_rel_y / dist )
4143!
4144!--                Currently no collision, calculate collision avoidance force according to
4145!--                Karamouzas et al (2014, PRL 113,238701)
4146                   ELSE
4147!
4148!--                   Factors
4149                      a_pl = v_rel_x**2 +  v_rel_y**2
4150                      b_pl = pos_rel_x*v_rel_x + pos_rel_y*v_rel_y
4151                      c_pl = dist_sq - r_sq
4152                      d_pl = b_pl**2 - a_pl*c_pl
4153!
4154!--                   If the two agents are moving non-parallel, calculate collision avoidance
4155!--                   social force
4156                      IF ( d_pl > 0.0_wp  .AND.  ( a_pl < -0.00001 .OR. a_pl > 0.00001 ) )  THEN
4157
4158                         d_pl   = SQRT( d_pl )
4159                         coll_t = ( b_pl - d_pl ) / a_pl
4160                         IF ( coll_t > 0.0_wp )  THEN
4161!
4162!--                         Dummy factor
4163                            ddum_f = 1. / ( a_pl * coll_t**2 ) * ( 2. / coll_t + 1.0 * d_coll_t_0 )
4164!
4165!--                         x-component of social force
4166                            force_x = force_x - k_pl * EXP( -coll_t * d_coll_t_0 ) *               &
4167                                      ( v_rel_x - ( b_pl * v_rel_x - a_pl * pos_rel_x ) / d_pl ) * &
4168                                      ddum_f
4169!
4170!--                         y-component of social force
4171                            force_y = force_y - k_pl * EXP( -coll_t * d_coll_t_0 ) *               &
4172                                      ( v_rel_y - ( b_pl * v_rel_y - a_pl * pos_rel_y ) / d_pl ) * &
4173                                      ddum_f
4174
4175                         ENDIF
4176                      ENDIF
4177                   ENDIF
4178                ENDDO
4179             ENDDO
4180          ENDDO
4181
4182       CASE ( 'walls' )
4183
4184          srw = scan_radius_wall
4185          corner_found = .FALSE.
4186!
4187!--       Find relevant grid boxes (those that could contain topography within radius)
4188          sc_x_start = (x_a - srw) * ddx
4189          sc_x_end   = (x_a + srw) * ddx
4190          sc_y_start = (y_a - srw) * ddx
4191          sc_y_end   = (y_a + srw) * ddx
4192          IF ( sc_x_start < nxlg ) sc_x_start = nxlg
4193          IF ( sc_x_end   > nxrg ) sc_x_end   = nxrg
4194          IF ( sc_y_start < nysg ) sc_y_start = nysg
4195          IF ( sc_y_end   > nyng ) sc_y_end   = nyng
4196!
4197!--       Find "walls" ( i.e. topography steps (up or down) higher than one grid box ) that are
4198!--       perpendicular to the agent within the defined search radius. Such obstacles cannot be
4199!--       passed and a social force to that effect is applied.
4200!--       Walls only apply a force perpendicular to the wall to the agent.
4201!--       There is therefore a search for walls directly right, left, south and north of the agent.
4202!--       All other walls are ignored.
4203!--
4204!--       Check for wall left of current agent
4205          ij_dum = 0
4206          IF ( sc_x_start < ip )  THEN
4207             DO  il = ip - 1, sc_x_start, -1
4208!
4209!--             Going left from the agent, check for a right wall
4210                IF ( BTEST( obstacle_flags(jp,il), 2 ) )  THEN
4211!
4212!--                Obstacle found in grid box il, wall at right side
4213                   x_wall = (il+1)*dx
4214!
4215!--                Calculate force of found wall on agent
4216                   CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_a )
4217!
4218!--                Calculate new x starting index for later scan for corners
4219                   ij_dum = il + 1
4220                   EXIT
4221                ENDIF
4222             ENDDO
4223          ENDIF
4224          IF ( ij_dum /= 0 ) sc_x_start = ij_dum
4225
4226!
4227!--       Check for wall right of current agent
4228          ij_dum = 0
4229          IF ( sc_x_end > ip )  THEN
4230             DO  il = ip + 1, sc_x_end
4231!
4232!--             Going right from the agent, check for a left wall
4233                IF ( BTEST( obstacle_flags(jp,il), 6 ) )  THEN
4234!
4235!--                Obstacle found in grid box il, wall at left side
4236                   x_wall = il*dx
4237!
4238!--                Calculate force of found wall on agent
4239                   CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_a )
4240!
4241!--                Calculate new x end index for later scan for corners
4242                   ij_dum = il - 1
4243                   EXIT
4244                ENDIF
4245             ENDDO
4246          ENDIF
4247          IF ( ij_dum /= 0 ) sc_x_end = ij_dum
4248
4249!
4250!--          Check for wall south of current agent
4251          ij_dum = 0
4252          IF ( sc_y_start < jp )  THEN
4253             DO  jl = jp - 1, sc_y_start, -1
4254!
4255!--             Going south from the agent, check for a north wall
4256                IF ( BTEST( obstacle_flags(jl,ip), 0 ) )  THEN
4257!
4258!--                Obstacle found in grid box jl, wall at left side
4259                   y_wall = ( jl + 1 ) * dy
4260
4261                   CALL mas_timestep_wall_corner_force( x_a, x_a, y_a, y_wall )
4262!
4263!--                Calculate new y starting index for later scan for corners
4264                   ij_dum = jl + 1
4265                   EXIT
4266                ENDIF
4267             ENDDO
4268          ENDIF
4269          IF ( ij_dum /= 0 ) sc_y_start = ij_dum
4270
4271!
4272!--       Check for wall north of current agent
4273          ij_dum = 0
4274          IF ( sc_y_end > jp )  THEN
4275             DO  jl = jp + 1, sc_y_end
4276!
4277!--             Going north from the agent, check for a south wall
4278                IF ( BTEST( obstacle_flags(jl,ip), 4 ) )  THEN
4279!
4280!--                   obstacle found in grid box jl, wall at left side
4281                   y_wall = jl * dy
4282
4283                   CALL mas_timestep_wall_corner_force( x_a, x_a, y_a, y_wall )
4284!
4285!--                Calculate new y end index for later scan for corners
4286                   ij_dum = jl - 1
4287                ENDIF
4288             ENDDO
4289          ENDIF
4290          IF ( ij_dum /= 0 ) sc_y_end = ij_dum
4291
4292!
4293!--       Scan for corners surrounding current agent.
4294!--       Only gridcells that are closer than the closest wall in each direction (n,s,r,l) are
4295!--       considered in the search since those further away would have a significantly smaller
4296!--       resulting force than the closer wall.
4297          DO  il = sc_x_start, sc_x_end
4298             DO  jl = sc_y_start, sc_y_end
4299                IF ( il == ip  .OR.  jl == jp )  CYCLE
4300!
4301!--             Corners left of agent
4302                IF ( il < ip )  THEN
4303!
4304!--                South left quadrant: look for north right corner
4305                   IF ( jl < jp )  THEN
4306                      IF ( BTEST( obstacle_flags(jl,il), 1 ) )  THEN
4307!
4308!--                      Calculate coordinates of the found corner
4309                         x_wall = ( il + 1 ) * dx
4310                         y_wall = ( jl + 1 ) * dy
4311
4312                         CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_wall )
4313
4314                      ENDIF
4315!
4316!--                North left quadrant: look for south right corner
4317                   ELSEIF ( jl > jp )  THEN
4318                      IF ( BTEST( obstacle_flags(jl,il), 3 ) )  THEN
4319!
4320!--                      Calculate coordinates of the corner of mentioned gridcell that is closest
4321!--                      to the current agent.
4322                         x_wall = ( il + 1 ) * dx
4323                         y_wall = jl * dy
4324
4325                         CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_wall )
4326
4327                      ENDIF
4328                   ENDIF
4329                ELSEIF ( il > ip )  THEN
4330!
4331!--                South right quadrant: look for north left corner
4332                   IF ( jl < jp )  THEN
4333                      IF ( BTEST( obstacle_flags(jl,il), 7 ) )  THEN
4334!
4335!--                      Calculate coordinates of the corner of mentioned gridcell that is closest
4336!--                      to the current agent.
4337                         x_wall = il * dx
4338                         y_wall = ( jl + 1 ) * dy
4339
4340                         CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_wall )
4341
4342                      ENDIF
4343!
4344!--                North right quadrant: look for south left corner
4345                   ELSEIF ( jl > jp )  THEN
4346                      IF ( BTEST( obstacle_flags(jl,il), 5 ) )  THEN
4347!
4348!--                      Calculate coordinates of the corner of mentioned gridcell that is closest
4349!--                      to the current agent.
4350                         x_wall = il * dx
4351                         y_wall = jl * dy
4352
4353                         CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_wall )
4354
4355                      ENDIF
4356                   ENDIF
4357                ENDIF
4358             ENDDO
4359          ENDDO
4360
4361       CASE DEFAULT
4362
4363    END SELECT
4364
4365 END SUBROUTINE mas_timestep_social_forces
4366
4367!--------------------------------------------------------------------------------------------------!
4368! Description:
4369! ------------
4370!> Given a distance to the current agent, calculates the force a found corner or wall exerts on that
4371!> agent
4372!--------------------------------------------------------------------------------------------------!
4373 SUBROUTINE mas_timestep_wall_corner_force( xa, xw, ya, yw )
4374
4375    IMPLICIT NONE
4376
4377    REAL(wp) ::  dist_l     !< distance to obstacle
4378    REAL(wp) ::  force_d_x  !< increment of social force, x-direction
4379    REAL(wp) ::  force_d_y  !< increment of social force, x-direction
4380    REAL(wp) ::  xa         !< x-position of agent
4381    REAL(wp) ::  xw         !< x-position of wall
4382    REAL(wp) ::  ya         !< x-position of agent
4383    REAL(wp) ::  yw         !< y-position of wall
4384
4385    force_d_x = 0.0_wp
4386    force_d_y = 0.0_wp
4387!
4388!-- Calculate coordinates of corner relative to agent postion and distance between corner and agent.
4389    xw = xa - xw
4390    yw = ya - yw
4391    dist_l = SQRT( ( xw )**2 + ( yw )**2 )
4392!
4393!-- Calculate x and y component of repulsive force induced by previously found corner
4394    IF ( dist_l > 0 )  THEN
4395       force_d_x = repuls_wall * d_sigma_rep_wall                                                  &
4396                   * EXP( -dist_l * d_sigma_rep_wall )                                             &
4397                   * xw / (dist_l)
4398       force_d_y = repuls_wall * d_sigma_rep_wall                                                  &
4399                   * EXP( -dist_l * d_sigma_rep_wall )                                             &
4400                   * yw / (dist_l)
4401    ENDIF
4402
4403! !--    forces that are located outside of a sight radius of
4404! !--    200 degrees (-> COS(100./180.*pi) = COS(.555*pi)) of
4405! !--    current agent are considered to have an effect of 50%
4406!        IF ( force_d_x * agents(nl)%speed_e_x +               &
4407!             force_d_y * agents(nl)%speed_e_y <               &
4408!             SQRT(force_d_x**2 + force_d_y**2) *              &
4409!             COS( .55555555 * 3.1415 ) )                      &
4410!        THEN
4411!           force_d_x = force_d_x * .5_wp
4412!           force_d_y = force_d_y * .5_wp
4413!        ENDIF
4414
4415!
4416!-- Add force increment to total force of current agent
4417    force_x = force_x + force_d_x
4418    force_y = force_y + force_d_y
4419
4420 END SUBROUTINE mas_timestep_wall_corner_force
4421
4422
4423!
4424!-- Calculates distance of point P to edge (A,B). If A = B, calculates point-to-point distance from
4425!-- A/B to P.
4426 FUNCTION dist_point_to_edge( a_x, a_y, b_x, b_y, p_x, p_y )
4427
4428    IMPLICIT NONE
4429
4430    REAL(wp)  :: a_x                 !< x-coordinate of point A of edge
4431    REAL(wp)  :: a_y                 !< y-coordinate of point A of edge
4432    REAL(wp)  :: ab_x                !< x-coordinate of vector from A to B
4433    REAL(wp)  :: ab_y                !< y-coordinate of vector from A to B
4434    REAL(wp)  :: ab_d                !< inverse length of vector from A to B
4435    REAL(wp)  :: ab_u_x              !< x-coordinate of vector with direction of ab and length 1
4436    REAL(wp)  :: ab_u_y              !< y-coordinate of vector with direction of ab and length 1
4437    REAL(wp)  :: ap_x                !< x-coordinate of vector from A to P
4438    REAL(wp)  :: ap_y                !< y-coordinate of vector from A to P
4439    REAL(wp)  :: b_x                 !< x-coordinate of point B of edge
4440    REAL(wp)  :: b_y                 !< y-coordinate of point B of edge
4441    REAL(wp)  :: ba_x                !< x-coordinate of vector from B to A
4442    REAL(wp)  :: ba_y                !< y-coordinate of vector from B to A
4443    REAL(wp)  :: bp_x                !< x-coordinate of vector from B to P
4444    REAL(wp)  :: bp_y                !< y-coordinate of vector from B to P
4445    REAL(wp)  :: dist_x              !< x-coordinate of point P
4446    REAL(wp)  :: dist_y              !< y-coordinate of point P
4447    REAL(wp)  :: dist_point_to_edge  !< y-coordinate of point P
4448    REAL(wp)  :: p_x                 !< x-coordinate of point P
4449    REAL(wp)  :: p_y                 !< y-coordinate of point P
4450
4451    ab_x = - a_x + b_x
4452    ab_y = - a_y + b_y
4453    ba_x = - b_x + a_x
4454    ba_y = - b_y + a_y
4455    ap_x = - a_x + p_x
4456    ap_y = - a_y + p_y
4457    bp_x = - b_x + p_x
4458    bp_y = - b_y + p_y
4459
4460    IF ( ab_x * ap_x + ab_y * ap_y <= 0. )  THEN
4461       dist_point_to_edge = SQRT( ( a_x - p_x )**2 + ( a_y - p_y )**2 )
4462    ELSEIF ( ba_x * bp_x + ba_y * bp_y <= 0. )  THEN
4463       dist_point_to_edge = SQRT( ( b_x - p_x )**2 + ( b_y - p_y )**2)
4464    ELSE
4465       ab_d = 1.0_wp / SQRT( ( ab_x )**2 + ( ab_y )**2 )
4466       ab_u_x = ab_x * ab_d
4467       ab_u_y = ab_y * ab_d
4468       dist_x = ap_x - ( ap_x * ab_u_x + ap_y * ab_u_y ) * ab_u_x
4469       dist_y = ap_y - ( ap_x * ab_u_x + ap_y * ab_u_y ) * ab_u_y
4470       dist_point_to_edge = SQRT( dist_x**2 + dist_y**2 )
4471    ENDIF
4472
4473 END FUNCTION dist_point_to_edge
4474
4475!
4476!-- Returns the heuristic between points A and B (currently the straight distance)
4477 FUNCTION heuristic( ax, ay, bx, by )
4478
4479    IMPLICIT NONE
4480
4481    REAL(wp)  :: ax           !< x-coordinate of point A
4482    REAL(wp)  :: ay           !< y-coordinate of point A
4483    REAL(wp)  :: bx           !< x-coordinate of point B
4484    REAL(wp)  :: by           !< y-coordinate of point B
4485    REAL(wp)  :: heuristic    !< return value
4486
4487    heuristic = SQRT( ( ax - bx )**2 + ( ay - by )**2 )
4488
4489 END FUNCTION heuristic
4490
4491!
4492!-- Calculates if point P is left of the infinite line that contains A and B (direction: A to B).
4493!-- Concept: 2D rotation of two vectors
4494 FUNCTION is_left( ax, ay, bx, by, px, py )
4495
4496    IMPLICIT NONE
4497
4498    LOGICAL  :: is_left !< return value; TRUE if P is left of AB
4499
4500    REAL(wp)  :: ax     !< x-coordinate of point A
4501    REAL(wp)  :: ay     !< y-coordinate of point A
4502    REAL(wp)  :: bx     !< x-coordinate of point B
4503    REAL(wp)  :: by     !< y-coordinate of point B
4504    REAL(wp)  :: px     !< x-coordinate of point P
4505    REAL(wp)  :: py     !< y-coordinate of point P
4506
4507    is_left = (bx-ax)*(py-ay)-(px-ax)*(by-ay) > 0
4508    IF ( ( ABS( ax - px ) < .001 .AND. ABS( ay - py ) < .001 )  .OR.                               &
4509         ( ABS( bx - px ) < .001 .AND. ABS( by - py ) < .001) )                                    &
4510    THEN
4511       is_left = .FALSE.
4512    ENDIF
4513
4514 END FUNCTION is_left
4515
4516!
4517!-- Calculates if point P is right of the infinite line that contains A and B (direction: A to B)
4518!-- Concept: 2D rotation of two vectors
4519 FUNCTION is_right( ax, ay, bx, by, px, py )
4520
4521    IMPLICIT NONE
4522
4523    LOGICAL  :: is_right !< return value; TRUE if P is right of AB
4524
4525    REAL(wp), INTENT(IN)  :: ax     !< x-coordinate of point A
4526    REAL(wp), INTENT(IN)  :: ay     !< y-coordinate of point A
4527    REAL(wp), INTENT(IN)  :: bx     !< x-coordinate of point B
4528    REAL(wp), INTENT(IN)  :: by     !< y-coordinate of point B
4529    REAL(wp), INTENT(IN)  :: px     !< x-coordinate of point P
4530    REAL(wp), INTENT(IN)  :: py     !< y-coordinate of point P
4531
4532    is_right = (bx-ax)*(py-ay)-(px-ax)*(by-ay) < 0
4533    IF ( ( ABS( ax - px ) < 0.001_wp .AND. ABS( ay - py ) < 0.001_wp )  .OR.                       &
4534         ( ABS( bx - px ) < 0.001_wp .AND. ABS( by - py ) < 0.001_wp ) )                           &
4535    THEN
4536       is_right = .FALSE.
4537    ENDIF
4538
4539 END FUNCTION is_right
4540
4541!
4542!-- Returns true if the line segments AB and PQ share an intersection
4543 FUNCTION intersect( ax, ay, bx, by, px, py, qx, qy )
4544
4545    IMPLICIT NONE
4546
4547    LOGICAL  :: intersect !< return value; TRUE if intersection was found
4548    LOGICAL  :: la        !< T if a is left of PQ
4549    LOGICAL  :: lb        !< T if b is left of PQ
4550    LOGICAL  :: lp        !< T if p is left of AB
4551    LOGICAL  :: lq        !< T if q is left of AB
4552    LOGICAL  :: poss      !< flag that indicates if an intersection is still possible
4553    LOGICAL  :: ra        !< T if a is right of PQ
4554    LOGICAL  :: rb        !< T if b is right of PQ
4555    LOGICAL  :: rp        !< T if p is right of AB
4556    LOGICAL  :: rq        !< T if q is right of AB
4557
4558    REAL(wp)  :: ax     !< x-coordinate of point A
4559    REAL(wp)  :: ay     !< y-coordinate of point A
4560    REAL(wp)  :: bx     !< x-coordinate of point B
4561    REAL(wp)  :: by     !< y-coordinate of point B
4562    REAL(wp)  :: px     !< x-coordinate of point P
4563    REAL(wp)  :: py     !< y-coordinate of point P
4564    REAL(wp)  :: qx     !< x-coordinate of point Q
4565    REAL(wp)  :: qy     !< y-coordinate of point Q
4566
4567    intersect = .FALSE.
4568    poss      = .FALSE.
4569!
4570!-- Intersection is possible only if P and Q are on opposing sides of AB
4571    lp = is_left(ax,ay,bx,by,px,py)
4572    rq = is_right(ax,ay,bx,by,qx,qy)
4573    IF ( lp  .AND.  rq )  poss = .TRUE.
4574    IF ( .NOT. poss )  THEN
4575       lq = is_left(ax,ay,bx,by,qx,qy)
4576       rp = is_right(ax,ay,bx,by,px,py)
4577       IF ( lq  .AND.  rp )  poss = .TRUE.
4578    ENDIF
4579!
4580!-- Intersection occurs only if above test (poss) was true AND A and B are on opposing sides of PQ.
4581    IF ( poss )  THEN
4582       la = is_left(px,py,qx,qy,ax,ay)
4583       rb = is_right(px,py,qx,qy,bx,by)
4584       IF ( la  .AND.  rb )  intersect = .TRUE.
4585       IF ( .NOT. intersect )  THEN
4586          lb = is_left(px,py,qx,qy,bx,by)
4587          ra = is_right(px,py,qx,qy,ax,ay)
4588          IF ( lb  .AND.  ra )  intersect = .TRUE.
4589       ENDIF
4590    ENDIF
4591
4592 END FUNCTION intersect
4593
4594!
4595!-- Gives a nuber randomly distributed around an average
4596 FUNCTION random_normal( avg, variation )
4597
4598    IMPLICIT NONE
4599
4600    REAL(wp)  :: avg            !< x-coordinate of vector from A to B
4601    REAL(wp)  :: random_normal  !< y-coordinate of vector from A to B
4602    REAL(wp)  :: variation      !< y-coordinate of vector from A to B
4603
4604    REAL(wp), DIMENSION(12)  :: random_arr  !< inverse length of vector from A to B
4605
4606    CALL RANDOM_NUMBER( random_arr )
4607    random_normal = avg + variation * ( SUM( random_arr ) - 6.0_wp )
4608
4609 END FUNCTION random_normal
4610
4611
4612 END MODULE multi_agent_system_mod
Note: See TracBrowser for help on using the repository browser.