Index: /palm/trunk/SOURCE/Makefile
===================================================================
--- /palm/trunk/SOURCE/Makefile	(revision 3234)
+++ /palm/trunk/SOURCE/Makefile	(revision 3235)
@@ -25,4 +25,8 @@
 # -----------------
 # $Id$
+# Added palm dependency of multi_agent_system_mod, because of mas_last_actions
+# call at the end of palm run
+# 
+# 3167 2018-07-24 18:17:30Z suehring
 # Bugfix, add missing dependencies for multi-agent system
 # 
@@ -1227,4 +1231,5 @@
 	mod_kinds.o \
 	modules.o \
+	multi_agent_system_mod.o \
 	netcdf_data_input_mod.o \
 	pmc_interface_mod.o \
Index: /palm/trunk/SOURCE/modules.f90
===================================================================
--- /palm/trunk/SOURCE/modules.f90	(revision 3234)
+++ /palm/trunk/SOURCE/modules.f90	(revision 3235)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Added global variable dim_size_agtnum to own module. Necessary to avoid
+! circular dependency in agent output.
+! 
+! 3232 2018-09-07 12:21:44Z raasch
 ! references to mrun replaced by palmrun, and updated
 ! 
@@ -661,4 +665,23 @@
 
  END MODULE advection
+
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> The variable in this module is used by multi_agent_system_mod AND
+!> netcdf_interface_mod. It must be here to avoid circular dependency.
+!> This is a workaround.
+!------------------------------------------------------------------------------!
+ MODULE mas_global_attributes
+ 
+    USE kinds
+
+    INTEGER(iwp) ::  dim_size_agtnum  !< size of agent number dimension for netCDF output
+
+    SAVE
+
+ END MODULE mas_global_attributes
 
 
Index: /palm/trunk/SOURCE/multi_agent_system_mod.f90
===================================================================
--- /palm/trunk/SOURCE/multi_agent_system_mod.f90	(revision 3234)
+++ /palm/trunk/SOURCE/multi_agent_system_mod.f90	(revision 3235)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Bugfix in output, added agent-number dimension and related messages and
+! input parameters, updated cpu logging, added mas_last_actions
+! 
+! 3201 2018-08-20 11:45:01Z sward
 ! Bugfix, missing pre-processor directive. Set default 
 ! read_agents_from_restartfile = .FALSE. restarts not yet implemented.
@@ -87,24 +91,26 @@
     CHARACTER(LEN=15) ::  bc_mas_ns = 'absorb'  !< north/south boundary condition
 
-    INTEGER(iwp) ::  deleted_agents = 0          !< number of deleted agents per time step 
-    INTEGER(iwp) ::  heap_count                  !< number of items in binary heap (for pathfinding)
-    INTEGER(iwp) ::  ibc_mas_lr                  !< agent left/right boundary condition dummy
-    INTEGER(iwp) ::  ibc_mas_ns                  !< agent north/south boundary condition dummy
-    INTEGER(iwp) ::  ind_pm10 = -9               !< chemical species index of PM10
-    INTEGER(iwp) ::  ind_pm25 = -9               !< chemical species index of PM2.5
-    INTEGER(iwp) ::  iran_agent = -1234567       !< number for random generator
-    INTEGER(iwp) ::  min_nr_agent = 2            !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  ghla_count_recv             !< number of agents in left ghost layer
-    INTEGER(iwp) ::  ghna_count_recv             !< number of agents in north ghost layer
-    INTEGER(iwp) ::  ghra_count_recv             !< number of agents in right ghost layer
-    INTEGER(iwp) ::  ghsa_count_recv             !< number of agents in south ghost layer
-    INTEGER(iwp) ::  nr_move_north               !< number of agts to move north during exchange_horiz
-    INTEGER(iwp) ::  nr_move_south               !< number of agts to move south during exchange_horiz
-    INTEGER(iwp) ::  number_of_agents = 0        !< number of agents for each grid box (3d array is saved on agt_count)
-    INTEGER(iwp) ::  number_of_agent_groups = 1  !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  sort_count_mas = 0          !< counter for sorting agents
-    INTEGER(iwp) ::  agt_path_size = 15          !< size of agent path array
-    INTEGER(iwp) ::  step_dealloc_mas = 100      !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  total_number_of_agents      !< total number of agents in the whole model domain
+    INTEGER(iwp) ::  deleted_agents = 0                !< number of deleted agents per time step
+    INTEGER(iwp) ::  dim_size_agtnum_manual = 9999999  !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  heap_count                        !< number of items in binary heap (for pathfinding)
+    INTEGER(iwp) ::  ibc_mas_lr                        !< agent left/right boundary condition dummy
+    INTEGER(iwp) ::  ibc_mas_ns                        !< agent north/south boundary condition dummy
+    INTEGER(iwp) ::  ind_pm10 = -9                     !< chemical species index of PM10
+    INTEGER(iwp) ::  ind_pm25 = -9                     !< chemical species index of PM2.5
+    INTEGER(iwp) ::  iran_agent = -1234567             !< number for random generator
+    INTEGER(iwp) ::  min_nr_agent = 2                  !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  ghla_count_recv                   !< number of agents in left ghost layer
+    INTEGER(iwp) ::  ghna_count_recv                   !< number of agents in north ghost layer
+    INTEGER(iwp) ::  ghra_count_recv                   !< number of agents in right ghost layer
+    INTEGER(iwp) ::  ghsa_count_recv                   !< number of agents in south ghost layer
+    INTEGER(iwp) ::  maximum_number_of_agents = 0      !< maximum number of agents during run
+    INTEGER(iwp) ::  nr_move_north                     !< number of agts to move north during exchange_horiz
+    INTEGER(iwp) ::  nr_move_south                     !< number of agts to move south during exchange_horiz
+    INTEGER(iwp) ::  number_of_agents = 0              !< number of agents for each grid box (3d array is saved on agt_count)
+    INTEGER(iwp) ::  number_of_agent_groups = 1        !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  sort_count_mas = 0                !< counter for sorting agents
+    INTEGER(iwp) ::  agt_path_size = 15                !< size of agent path array
+    INTEGER(iwp) ::  step_dealloc_mas = 100            !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  total_number_of_agents            !< total number of agents in the whole model domain
 
     INTEGER(iwp), PARAMETER ::  NR_2_direction_move = 10000 !< parameter for agent exchange
@@ -114,17 +120,17 @@
     INTEGER(iwp), PARAMETER ::  max_number_of_agent_groups = 100 !< maximum allowed number of agent groups
 
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  agt_count  !< 3d array of number of agents of every grid box
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  agt_count         !< 3d array of number of agents of every grid box
     INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  s_measure_height  !< k-index(s-grid) for measurement
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  top_top_s  !< k-index of first s-gridpoint above topography
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  top_top_w  !< k-index of first v-gridpoint above topography
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  obstacle_flags  !< flags to identify corners and edges of topography that cannot be crossed by agents
-
-    LOGICAL ::  deallocate_memory_mas = .TRUE.         !< namelist parameter (see documentation)
-    LOGICAL ::  dt_3d_reached_mas                      !< flag: agent timestep has reached model timestep
-    LOGICAL ::  dt_3d_reached_l_mas                    !< flag: agent timestep has reached model timestep
-    LOGICAL ::  agents_active = .FALSE.                !< flag for agent system
-    LOGICAL ::  random_start_position_agents = .TRUE.  !< namelist parameter (see documentation)
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  top_top_s         !< k-index of first s-gridpoint above topography
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  top_top_w         !< k-index of first v-gridpoint above topography
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  obstacle_flags    !< flags to identify corners and edges of topography that cannot be crossed by agents
+
+    LOGICAL ::  deallocate_memory_mas = .TRUE.          !< namelist parameter (see documentation)
+    LOGICAL ::  dt_3d_reached_mas                       !< flag: agent timestep has reached model timestep
+    LOGICAL ::  dt_3d_reached_l_mas                     !< flag: agent timestep has reached model timestep
+    LOGICAL ::  agents_active = .FALSE.                 !< flag for agent system
+    LOGICAL ::  random_start_position_agents = .TRUE.   !< namelist parameter (see documentation)
     LOGICAL ::  read_agents_from_restartfile = .FALSE.  !< namelist parameter (see documentation)
-    LOGICAL ::  agent_own_timestep = .FALSE.           !< namelist parameter (see documentation)
+    LOGICAL ::  agent_own_timestep = .FALSE.            !< namelist parameter (see documentation)
 
     LOGICAL, DIMENSION(max_number_of_agent_groups) ::  a_rand_target = .FALSE. !< namelist parameter (see documentation)
@@ -134,5 +140,6 @@
     REAL(wp) ::  coll_t_0 = 3.                             !< namelist parameter (see documentation)
     REAL(wp) ::  corner_gate_start = 0.5_wp                !< namelist parameter (see documentation)
-    REAL(wp) ::  corner_gate_width = 1.0_wp                !< namelist parameter (see documentation)    
+    REAL(wp) ::  corner_gate_width = 1.0_wp                !< namelist parameter (see documentation)
+    REAL(wp) ::  dim_size_factor_agtnum = 1.0_wp           !< namelist parameter (see documentation)
     REAL(wp) ::  d_sigma_rep_agent                         !< inverse of sigma_rep_agent
     REAL(wp) ::  d_sigma_rep_wall                          !< inverse of sigma_rep_wall
@@ -174,45 +181,45 @@
 !-- Type for the definition of an agent
     TYPE agent_type
-        INTEGER(iwp) ::  block_nr        !< number for sorting
-        INTEGER(iwp) ::  group           !< number of agent group
-        INTEGER(idp) ::  id              !< particle ID (64 bit integer)
-        INTEGER(iwp) ::  path_counter    !< current target along path (path_x/y)
-        LOGICAL      ::  agent_mask      !< if this parameter is set to false the agent will be deleted
-        REAL(wp)     ::  age             !< age of agent
-        REAL(wp)     ::  age_m           !< age of agent
-        REAL(wp)     ::  dt_sum          !< sum of agents subtimesteps
-        REAL(wp)     ::  clo             !< clothing index
-        REAL(wp)     ::  energy_storage  !< energy stored by agent
-        REAL(wp)     ::  force_x         !< force term x-direction
-        REAL(wp)     ::  force_y         !< force term y-direction
-        REAL(wp)     ::  origin_x        !< origin x-position of agent
-        REAL(wp)     ::  origin_y        !< origin y-position of agent
-        REAL(wp)     ::  pm10            !< PM10 concentration at agent position
-        REAL(wp)     ::  pm25            !< PM25 concentration at agent position
-        REAL(wp)     ::  speed_abs       !< absolute value of agent speed
-        REAL(wp)     ::  speed_e_x       !< normalized speed of agent in x
-        REAL(wp)     ::  speed_e_y       !< normalized speed of agent in y
-        REAL(wp)     ::  speed_des       !< agent's desired speed
-        REAL(wp)     ::  speed_x         !< speed of agent in x
-        REAL(wp)     ::  speed_y         !< speed of agent in y
-        REAL(wp)     ::  thermal_index   !< the dynamic thermal index
-        REAL(wp)     ::  windspeed       !< absolute value of windspeed at agent position
-        REAL(wp)     ::  x               !< x-position
-        REAL(wp)     ::  y               !< y-position
-        REAL(wp)     ::  t               !< temperature
-        REAL(wp)     ::  t_x             !< x-position
-        REAL(wp)     ::  t_y             !< y-position
-        REAL(wp), DIMENSION(0:15) ::  path_x !< agent path to target (x)
-        REAL(wp), DIMENSION(0:15) ::  path_y !< agent path to target (y)
+        INTEGER(iwp) ::  block_nr             !< number for sorting
+        INTEGER(iwp) ::  group                !< number of agent group
+        INTEGER(idp) ::  id                   !< particle ID (64 bit integer)
+        INTEGER(iwp) ::  path_counter         !< current target along path (path_x/y)
+        LOGICAL      ::  agent_mask           !< if this parameter is set to false the agent will be deleted
+        REAL(wp)     ::  age                  !< age of agent
+        REAL(wp)     ::  age_m                !< age of agent
+        REAL(wp)     ::  dt_sum               !< sum of agents subtimesteps
+        REAL(wp)     ::  clo                  !< clothing index
+        REAL(wp)     ::  energy_storage       !< energy stored by agent
+        REAL(wp)     ::  force_x              !< force term x-direction
+        REAL(wp)     ::  force_y              !< force term y-direction
+        REAL(wp)     ::  origin_x             !< origin x-position of agent
+        REAL(wp)     ::  origin_y             !< origin y-position of agent
+        REAL(wp)     ::  pm10                 !< PM10 concentration at agent position
+        REAL(wp)     ::  pm25                 !< PM25 concentration at agent position
+        REAL(wp)     ::  speed_abs            !< absolute value of agent speed
+        REAL(wp)     ::  speed_e_x            !< normalized speed of agent in x
+        REAL(wp)     ::  speed_e_y            !< normalized speed of agent in y
+        REAL(wp)     ::  speed_des            !< agent's desired speed
+        REAL(wp)     ::  speed_x              !< speed of agent in x
+        REAL(wp)     ::  speed_y              !< speed of agent in y
+        REAL(wp)     ::  thermal_index        !< the dynamic thermal index
+        REAL(wp)     ::  windspeed            !< absolute value of windspeed at agent position
+        REAL(wp)     ::  x                    !< x-position
+        REAL(wp)     ::  y                    !< y-position
+        REAL(wp)     ::  t                    !< temperature
+        REAL(wp)     ::  t_x                  !< x-position
+        REAL(wp)     ::  t_y                  !< y-position
+        REAL(wp), DIMENSION(0:15) ::  path_x  !< agent path to target (x)
+        REAL(wp), DIMENSION(0:15) ::  path_y  !< agent path to target (y)
     END TYPE agent_type
 
-    TYPE(agent_type), DIMENSION(:), POINTER ::  agents       !< Agent array for this grid cell
-    TYPE(agent_type)                        ::  zero_agent   !< zero agent to avoid weird thing
-    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_north !< for agent exchange between PEs
-    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_south !< for agent exchange between PEs
-    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_l !< ghost layer left of pe domain
-    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_n !< ghost layer north of pe domain
-    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_r !< ghost layer right of pe domain
-    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_s !< ghost layer south of pe domain
+    TYPE(agent_type), DIMENSION(:), POINTER ::  agents               !< Agent array for this grid cell
+    TYPE(agent_type)                        ::  zero_agent           !< zero agent to avoid weird thing
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_north  !< for agent exchange between PEs
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_south  !< for agent exchange between PEs
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_l         !< ghost layer left of pe domain
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_n         !< ghost layer north of pe domain
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_r         !< ghost layer right of pe domain
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_s         !< ghost layer south of pe domain
 !
 !-- Type for 2D grid on which agents are stored
@@ -222,5 +229,5 @@
         INTEGER(iwp)                            ::  id_counter         !< agent id counter (removeable?)
         LOGICAL                                 ::  time_loop_done     !< timestep loop for agent advection
-        TYPE(agent_type), POINTER, DIMENSION(:) ::  agents          !< Particle array for this grid cell
+        TYPE(agent_type), POINTER, DIMENSION(:) ::  agents             !< Particle array for this grid cell
     END TYPE grid_agent_def
 
@@ -273,5 +280,5 @@
 !
 !-- Public functions
-    PUBLIC mas_init, mas_parin, multi_agent_system
+    PUBLIC mas_init, mas_last_actions, mas_parin, multi_agent_system
 
 !
@@ -286,4 +293,8 @@
        MODULE PROCEDURE mas_init
     END INTERFACE mas_init
+
+    INTERFACE mas_last_actions
+       MODULE PROCEDURE mas_last_actions
+    END INTERFACE mas_last_actions
 
     INTERFACE multi_agent_system
@@ -316,6 +327,6 @@
     INTEGER(iwp), SAVE ::  mas_count = 0      !< counts the mas-calls
 
-    LOGICAL ::  first_loop_stride   !< flag for first loop stride of agent sub-timesteps
-    LOGICAL ::  first_call = .TRUE. !< first call of mas flag for output
+    LOGICAL       ::  first_loop_stride   !< flag for first loop stride of agent sub-timesteps
+    LOGICAL, SAVE ::  first_call = .TRUE. !< first call of mas flag for output
 
     CALL cpu_log( log_point(9), 'mas', 'start' )
@@ -363,10 +374,4 @@
        ENDIF
 !
-!--    Start logging
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'start' )
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'pause' )
-       CALL cpu_log( log_point_s(16), 'mas_other_agents', 'start' )
-       CALL cpu_log( log_point_s(16), 'mas_other_agents', 'pause' )
-!
 !--    Flag is true by default, will be set to false if an agent has not yet
 !--    reached the model timestep
@@ -376,4 +381,5 @@
 !--    First part of agent transport:
 !--    Evaluate social forces for all agents at current positions
+       CALL cpu_log( log_point_s(9), 'mas_social_forces', 'start' )
        DO  i = nxl, nxr
           DO  j = nys, nyn
@@ -391,7 +397,9 @@
           ENDDO
        ENDDO
+       CALL cpu_log( log_point_s(9), 'mas_social_forces', 'stop' )
 !
 !--    Second part of agent transport:
 !--    timestep
+       CALL cpu_log( log_point_s(16), 'mas_timestep', 'start' )
        DO  i = nxl, nxr
           DO  j = nys, nyn
@@ -444,4 +452,5 @@
           ENDDO
        ENDDO
+       CALL cpu_log( log_point_s(16), 'mas_timestep', 'stop' )
 
 !
@@ -457,6 +466,4 @@
 #endif
 
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'stop' )
-       CALL cpu_log( log_point_s(16), 'mas_other_agents', 'stop' )
 !
 !--    Increment time since last release
@@ -465,4 +472,5 @@
 !
 !--    Move Agents local to PE to a different grid cell
+       CALL cpu_log( log_point_s(18), 'mas_move_exch_sort', 'start' )
        CALL mas_eh_move_agent
 !
@@ -473,4 +481,5 @@
 !--    determine new number of agents
        CALL mas_ps_sort_in_subboxes
+       CALL cpu_log( log_point_s(18), 'mas_move_exch_sort', 'stop' )
 !
 !--    Initialize variables for the next (sub-) timestep, i.e., for marking
@@ -540,5 +549,4 @@
        REAL(wp) ::  speed_round_y   !< rounding influence expressed as x speed component
 
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'continue' )
 !
 !--    loop over all agents in the current grid box
@@ -608,5 +616,4 @@
        ENDDO
 
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'pause' )
 !
 !-- corner rounding; to be added
@@ -1119,5 +1126,6 @@
 
        USE control_parameters,                                                 &
-           ONLY:  agt_time_count
+           ONLY:  agt_time_count, end_time, message_string,                    &
+                  multi_agent_system_end, multi_agent_system_start
 
        USE netcdf_interface,                                                   &
@@ -1130,4 +1138,6 @@
        USE NETCDF
 #endif
+       USE mas_global_attributes,                                              &
+           ONLY:  dim_size_agtnum
 
        IMPLICIT NONE
@@ -1135,4 +1145,5 @@
        INTEGER(iwp) ::  agt_size !< Agent size in bytes
        INTEGER(iwp) ::  dummy    !< dummy
+       INTEGER(iwp) ::  ii       !< counter (x)
        INTEGER(iwp) ::  ip       !< counter (x)
        INTEGER(iwp) ::  jp       !< counter (y)
@@ -1141,10 +1152,13 @@
        INTEGER(iwp) ::  noa_rcv  !< received number of agents
        INTEGER(iwp) ::  out_noa  !< number of agents for output
-       
+
        INTEGER(iwp), DIMENSION(0:numprocs-1) ::  noa_arr !< number of agents on each PE
 
        TYPE(agent_type), DIMENSION(:), ALLOCATABLE, TARGET ::  trf_agents !< all agents on current PE
        TYPE(agent_type), DIMENSION(:), ALLOCATABLE, TARGET ::  out_agents !< all agents in entire domain
+
        LOGICAL, INTENT (INOUT) :: ftest
+
+       LOGICAL, SAVE :: agt_dimension_exceeded = .FALSE.
 
        CALL cpu_log( log_point_s(17), 'mas_data_output', 'start' )
@@ -1172,5 +1186,4 @@
 !
 !--    Gather all agents on PE0 for output
-       CALL MPI_BARRIER( comm2d, ierr )
        IF ( myid == 0 )  THEN
           noa_arr(0) = noa
@@ -1228,48 +1241,101 @@
 
 #if defined( __netcdf )
-
-   !--    Output in netCDF format
-          IF ( ftest ) CALL check_open( 118 )
-
-   !
-   !--    Update the NetCDF time axis
+!
+!--       Update maximum number of agents
+          maximum_number_of_agents = MAX(maximum_number_of_agents, out_noa)
+!
+!--       Output in netCDF format
+          IF ( ftest ) THEN
+!
+!--          First, define size of agent number dimension from amount of agents
+!--          released, release interval, time of agent simulation and max
+!--          age of agents
+             dim_size_agtnum = MIN( MIN( multi_agent_system_end, end_time )    &
+                                       - multi_agent_system_start,             &
+                                    agent_maximum_age)
+
+             DO ii = 1, number_of_agent_groups
+                dim_size_agtnum = dim_size_agtnum                              &
+                                + (FLOOR( ( asr(ii)-asl(ii) ) / adx(ii) ) + 1) &
+                                * (FLOOR( ( asn(ii)-ass(ii) ) / ady(ii) ) + 1) &
+                                * (FLOOR( dim_size_agtnum / dt_arel )     + 1) &
+                                * dim_size_factor_agtnum
+                dim_size_agtnum = MIN( dim_size_agtnum, dim_size_agtnum_manual )
+             ENDDO
+             CALL check_open( 118 )
+          ENDIF
+
+!
+!--       Update the NetCDF time axis
           agt_time_count = agt_time_count + 1
 
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_time_agt, &
-                                  (/ time_since_reference_point /),        &
-                                  start = (/ agt_time_count /), count = (/ 1 /) )
+          IF ( .NOT. agt_dimension_exceeded ) THEN
+!
+!--          if number of agents to be output exceeds dimension, set flag and
+!--          print warning
+             IF ( out_noa > dim_size_agtnum ) THEN
+
+                agt_dimension_exceeded = .TRUE.
+                WRITE(message_string,'(A,F11.1,2(A,I8))')                      &
+                                'Number of agents exceeds agent dimension.' // &
+                                '&Starting at time_since_reference_point = ',  &
+                                time_since_reference_point,                    &
+                                ' s, &data may be missing.'//                  &
+                                '&Number of agents:     ', out_noa,            &
+                                '&Agent dimension size: ', dim_size_agtnum
+
+                CALL message( 'mas_data_output_agents',                        &
+                              'PA0420', 0, 1, 0, 6, 0 )
+
+             ENDIF
+          ENDIF
+
+!
+!--       reduce number of output agents to dimension size, if necessary
+          IF ( agt_dimension_exceeded ) THEN
+
+             out_noa = MIN( out_noa, dim_size_agtnum )
+
+          ENDIF
+
+          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_time_agt,                 &
+                                  (/ time_since_reference_point /),            &
+                                  start = (/ agt_time_count /),                &
+                                  count = (/ 1 /) )
           CALL netcdf_handle_error( 'mas_data_output_agents', 1 )
 
-   !
-   !--    Output all agent attributes
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(1), agents%x,         &
+!
+!--       Output agent attributes
+
+          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(1), agents%id,        &
+                                  start = (/ 1, agt_time_count /),             &
+                                  count = (/ out_noa /) )
+          CALL netcdf_handle_error( 'mas_data_output_agents', 2 )
+ 
+          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(2), agents%x,         &
                                   start = (/ 1, agt_time_count /),             &
                                   count = (/ out_noa /) )
           CALL netcdf_handle_error( 'mas_data_output_agents', 3 )
 
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(2), agents%y,         &
+          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(3), agents%y,         &
                                   start = (/ 1, agt_time_count /),             &
                                   count = (/ out_noa /) )
           CALL netcdf_handle_error( 'mas_data_output_agents', 4 )
 
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(3), agents%windspeed, &
+          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(4), agents%windspeed, &
                                   start = (/ 1, agt_time_count /),             &
                                   count = (/ out_noa /) )
           CALL netcdf_handle_error( 'mas_data_output_agents', 5 )
 
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(4), agents%t,         &
+          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(5), agents%t,         &
                                   start = (/ 1, agt_time_count /),             &
                                   count = (/ out_noa /) )
           CALL netcdf_handle_error( 'mas_data_output_agents', 6 )
 
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(5), agents%group,     &
+          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(6), agents%group,     &
                                   start = (/ 1, agt_time_count /),             &
                                   count = (/ out_noa /) )
           CALL netcdf_handle_error( 'mas_data_output_agents', 7 )
-
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(6), agents%id,        &
-                                  start = (/ 1, agt_time_count /),             &
-                                  count = (/ out_noa /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 8 )
+          CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
 
 
@@ -1291,12 +1357,13 @@
 !                                   start = (/ 1, agt_time_count /),             &
 !                                   count = (/ out_noa /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 10 )
+!           CALL netcdf_handle_error( 'mas_data_output_agents', 10 )
 
 #endif
 
           DEALLOCATE(out_agents)
+       ELSE
+          CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
        ENDIF
 
-       CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
 
     END SUBROUTINE mas_data_output_agents
@@ -1616,6 +1683,4 @@
        TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trsa  !< agents send to south PE
 
-       CALL cpu_log( log_point_s(18), 'mas_eh_exchange_horiz', 'start' )
-
 #if defined( __parallel )
 
@@ -2171,6 +2236,4 @@
 !--    Accumulate the number of agents transferred between the subdomains)
        CALL mas_eh_ghost_exchange
-
-       CALL cpu_log( log_point_s(18), 'mas_eh_exchange_horiz', 'stop' )
 
     END SUBROUTINE mas_eh_exchange_horiz
@@ -2499,6 +2562,4 @@
        TYPE(agent_type), DIMENSION(:), POINTER ::  agents_before_move !< agents before moving
 
-       CALL cpu_log( log_point_s(19), 'mas_eh_move_agent', 'start' )
-
        DO  ip = nxl, nxr
           DO  jp = nys, nyn
@@ -2545,6 +2606,4 @@
           ENDDO
        ENDDO
-
-       CALL cpu_log( log_point_s(19), 'mas_eh_move_agent', 'stop' )
 
        RETURN
@@ -3153,4 +3212,26 @@
 ! Description:
 ! ------------
+!> Output of informative message about maximum agent number
+!------------------------------------------------------------------------------!
+    SUBROUTINE mas_last_actions
+
+       USE control_parameters,                                                 &
+           ONLY:  message_string
+
+       IMPLICIT NONE
+
+       WRITE(message_string,'(A,I8,A)')                                        &
+                         'The maximumn number of agents durin this run was',   &
+                         maximum_number_of_agents,                             &
+                         '&Consider adjusting the INPUT parameter'//           &
+                         '&dim_size_agtnum_manual accordingly for the next run.'
+
+       CALL message( 'mas_data_output_agents', 'PA0457', 0, 0, 0, 6, 0 )
+
+    END SUBROUTINE mas_last_actions
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
 !> Finds the shortest path from a start position to a target position using the
 !> A*-algorithm
@@ -3194,7 +3275,5 @@
        TYPE(coord), DIMENSION(:), ALLOCATABLE, TARGET ::  tmp_path !< temporary path for resizing
 
-       CALL cpu_log( log_point_s(20), 'mas_nav_find_path', 'start' )
        node_counter = 0
-
 !
 !--    Create temporary navigation mesh including agent and target positions
@@ -3304,5 +3383,4 @@
 !--    Set current intermittent target of this agent
        DEALLOCATE(tmp_mesh, path)
-       CALL cpu_log( log_point_s(20), 'mas_nav_find_path', 'stop' )
 
     END SUBROUTINE mas_nav_a_star
@@ -3678,4 +3756,6 @@
                                     corner_gate_start,                         &
                                     corner_gate_width,                         &
+                                    dim_size_agtnum_manual,                    &
+                                    dim_size_factor_agtnum,                    &
                                     deallocate_memory_mas,                     &
                                     dist_to_int_target,                        &
@@ -3745,5 +3825,4 @@
        TYPE(agent_type), DIMENSION(:,:), ALLOCATABLE ::  sort_agents  !< sorted agent array
 
-       CALL cpu_log( log_point_s(21), 'mas_ps_sort_in_subboxes', 'start' )
        DO  ip = nxl, nxr
           DO  jp = nys, nyn
@@ -3796,5 +3875,4 @@
           ENDDO
        ENDDO
-       CALL cpu_log( log_point_s(21), 'mas_ps_sort_in_subboxes', 'stop' )
 
     END SUBROUTINE mas_ps_sort_in_subboxes
@@ -3945,5 +4023,4 @@
        CALL mas_agent_direction
 
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'continue' )
        DO n = 1, number_of_agents
 
@@ -3962,7 +4039,4 @@
        ENDDO
 
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'pause' )
-
-
     END SUBROUTINE mas_timestep_forces_call
 
@@ -3980,6 +4054,4 @@
        REAL(wp) ::  abs_v !< absolute value of velocity
        REAL(wp) ::  abs_f !< absolute value of force
-
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'continue' )
 
        DO n = 1, number_of_agents
@@ -4023,7 +4095,4 @@
 
        ENDDO
-
-       CALL cpu_log( log_point_s(9), 'mas_transport', 'pause' )
-
 
     END SUBROUTINE mas_timestep
@@ -4108,5 +4177,4 @@
 !--       current one
           CASE ( 'other_agents' )
-             CALL cpu_log( log_point_s(16), 'mas_other_agents', 'continue' )
 
              sra = scan_radius_agent
@@ -4206,5 +4274,4 @@
                 ENDDO
              ENDDO
-             CALL cpu_log( log_point_s(16), 'mas_other_agents', 'pause' )
 
           CASE ( 'walls' )
Index: /palm/trunk/SOURCE/netcdf_interface_mod.f90
===================================================================
--- /palm/trunk/SOURCE/netcdf_interface_mod.f90	(revision 3234)
+++ /palm/trunk/SOURCE/netcdf_interface_mod.f90	(revision 3235)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Changed MAS output dimension id_dim_agtnum to be of defined size and no longer
+! unlimited. Also changed some MAS output variables to be of type float
+! 
+! 3198 2018-08-15 09:23:10Z sward
 ! Redefined MAS limited time dimension to fit usage of multi_agent_system_end
 ! 
@@ -292,10 +296,12 @@
     USE NETCDF
 #endif
+    USE mas_global_attributes,                                                 &
+        ONLY:  dim_size_agtnum
 
     PRIVATE
 
     CHARACTER (LEN=16), DIMENSION(13) ::  agt_var_names =                      &
-          (/ 'ag_x            ', 'ag_y            ', 'ag_wind         ',       &
-             'ag_temp         ', 'ag_group        ', 'ag_id           ',       &
+          (/ 'ag_id           ', 'ag_x            ', 'ag_y            ',       &
+             'ag_wind         ', 'ag_temp         ', 'ag_group        ',       &
              'PM10            ', 'PM25            ', 'ag_therm_comf   ',       &
              'ag_uv           ', 'not_used        ', 'not_used        ',       &
@@ -303,6 +309,6 @@
 
     CHARACTER (LEN=16), DIMENSION(13) ::  agt_var_units = &
-          (/ 'meters          ', 'meters          ', 'm/s             ',       &
-             'K               ', 'dim_less        ', 'dim_less        ',       &
+          (/ 'dim_less        ', 'meters          ', 'meters          ',       &
+             'm/s             ', 'K               ', 'dim_less        ',       &
              'tbd             ', 'tbd             ', 'tbd             ',       &
              'tbd             ', 'not_used        ', 'not_used        ',       &
@@ -1980,27 +1986,32 @@
 
           CALL netcdf_create_var( id_set_agt, (/ id_dim_time_agt /), 'time',   &
-                                  NF90_DOUBLE, id_var_time_agt, 'seconds', '', &
+                                  NF90_REAL4, id_var_time_agt, 'seconds', '',  &
                                   332, 333, 000 )
-!
-!--       netCDF4 allows more than one unlimited dimension
-          CALL netcdf_create_dim( id_set_agt, 'agent_number',            &
-                                  NF90_UNLIMITED, id_dim_agtnum, 334 )
+
+          CALL netcdf_create_dim( id_set_agt, 'agent_number',                  &
+                                  dim_size_agtnum, id_dim_agtnum, 334 )
 
           CALL netcdf_create_var( id_set_agt, (/ id_dim_agtnum /),             &
-                                  'agent_number', NF90_DOUBLE,              &
-                                  id_var_agtnum, 'agent number', '', 335,   &
+                                  'agent_number', NF90_REAL4,                  &
+                                  id_var_agtnum, 'agent number', '', 335,      &
                                   336, 000 )
 !
 !--       Define variable which contains the real number of agents in use
           CALL netcdf_create_var( id_set_agt, (/ id_dim_time_agt /),           &
-                                  'real_num_of_agt', NF90_DOUBLE,              &
-                                  id_var_rnoa_agt, 'agent number', '', 337, &
+                                  'real_num_of_agt', NF90_REAL4,               &
+                                  id_var_rnoa_agt, 'agent number', '', 337,    &
                                   338, 000 )
+          i = 1
+          CALL netcdf_create_var( id_set_agt, (/ id_dim_agtnum,                &
+                                  id_dim_time_agt /), agt_var_names(i),        &
+                                  NF90_DOUBLE, id_var_agt(i),                  &
+                                  TRIM( agt_var_units(i) ),                    &
+                                  TRIM( agt_var_names(i) ), 339, 340, 341 )
 !
 !--       Define the variables
-          DO  i = 1, 6
+          DO  i = 2, 6
              CALL netcdf_create_var( id_set_agt, (/ id_dim_agtnum,             &
                                      id_dim_time_agt /), agt_var_names(i),     &
-                                     NF90_DOUBLE, id_var_agt(i),           &
+                                     NF90_REAL4, id_var_agt(i),                &
                                      TRIM( agt_var_units(i) ),                 &
                                      TRIM( agt_var_names(i) ), 339, 340, 341 )
Index: /palm/trunk/SOURCE/palm.f90
===================================================================
--- /palm/trunk/SOURCE/palm.f90	(revision 3234)
+++ /palm/trunk/SOURCE/palm.f90	(revision 3235)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Added mas_last_actions call and multi_agent_system_mod dependency
+! 
+! 3232 2018-09-07 12:21:44Z raasch
 ! references to mrun replaced by palmrun, and updated
 ! 
@@ -254,4 +257,7 @@
     USE indices,                                                               &
         ONLY:  nbgp
+
+    USE multi_agent_system_mod,                                                &
+        ONLY:  agents_active, mas_last_actions
 
     USE netcdf_data_input_mod,                                                 &
@@ -536,5 +542,6 @@
 !-- unit in routine user_last_actions.
     CALL cpu_log( log_point(4), 'last actions', 'start' )
-          
+
+    IF ( myid == 0 .AND. agents_active ) CALL mas_last_actions
     CALL user_last_actions
     CALL close_file( 0 )
