The AHA Model  Revision: 12809
Reference implementation 04 (HEDG02_04)
p_debug.f90
Go to the documentation of this file.
1 !> @file p_debug.f90
2 !! This file contaions external procedure(s) for testing and debugging. By the
3 !! separate nature, all the procedures from this file are `external`.
4 !! @warning They should not be used or called from the normal model code,
5 !! nor referred in the main Makefile.
6 !! @note By the temporary and ephemeral nature of the debugging code, the
7 !! @ref intro_style_rules "coding style" requirements are greatly
8 !! relaxed here.
9 !! @author Sergey Budaev <sergey.budaev@uib.no>
10 !! @date 2016-2017
11 
12 !-------------------------------------------------------------------------------
13 ! $Id: p_debug.f90 6159 2017-11-07 09:19:06Z sbu062 $
14 !-------------------------------------------------------------------------------
15 
16 !-----------------------------------------------------------------------------
17 !> Debugging and testing lifecycle, random wakls etc.
18 !! Has been initially implemented as `DEBUG_03: block`
19 !! @warning Code formatting style is not necessarily adhered to here.
21 
22  use commondata
23  use file_io
24  use the_evolution
25 
26  real(SRP) :: visrange, irradiance, step_rwalk, cost_step
27  integer :: food_item_selected, conspecif_selected
28  integer :: i, iii, ind
29  type(food_resource) :: joined_food_res_tmp
30 
31  type(timer_cpu) :: stopwatch_walk_wise
32 
33  real(SRP), dimension(100) :: way_passed ! way = distance between walks.
34 
35  real(SRP) :: surlig_log_dt, surlig_log_st
36 
37  !> PROCNAME is the procedure name for logging and debugging
38  character(len=*), parameter :: PROCNAME = "(life_cycles_DEBUG_TEST)"
39 
40  integer :: agent_in
41 
42  call log_configure("writeonstdout" , .true.)
43  call log_msg( "*** START DEBUG BLOCK ***" )
44  call log_configure("writeonstdout" , is_screen_output)
45 
46  !> ### Testing and debugging: description ###
47  !> Testing how the procedure for joining several food resources works.
48  !! Here it just joins the food resources in the safe and dangerous
49  !! habitats. Because the agents are limited to stay in the safe, this just
50  !! creates an extra processing overhead.
51  call joined_food_res_tmp%join( habitat_safe%food, habitat_dangerous%food, &
52  reindex=.true., label="TEMPORARY" )
53 
54  call joined_food_res_tmp%save_csv(csv_file_name="foods_joined.csv")
55  call habitat_safe%food%save_csv(csv_file_name="foods_safe.csv")
56  call habitat_dangerous%food%save_csv(csv_file_name="foods_dangerous.csv")
57 
59 
60  do_inds: do ind = 1, 20
61 
62  if (proto_parents%individual(ind)%is_dead()) exit do_inds
63 
64  call log_configure("writeonstdout" , .true.)
65  call log_msg( "Individual " // tostr(ind) )
66  call log_configure("writeonstdout" , is_screen_output)
67 
68  call log_dbg(ltag_info // "Body mass: " // tostr(proto_parents%individual(ind)%get_mass()))
69  call log_dbg(ltag_info // "Birth mass: " // tostr(proto_parents%individual(ind)%get_mass_birth()))
70  call log_dbg(ltag_info // "Body leng: " // tostr(proto_parents%individual(ind)%get_length()))
71  call log_dbg(ltag_info // "Energy: " // tostr(proto_parents%individual(ind)%get_energy() ))
72 
73  call log_dbg( ltag_info // "Standard cost of swimming 1 SL : " // &
74  tostr(proto_parents%individual(ind)%cost_swim_std(1)) )
75  call log_dbg( ltag_info // "Standard cost of swimming 100 SL: " // &
76  tostr(proto_parents%individual(ind)%cost_swim_std(100)) )
77  call log_dbg( ltag_info // "Standard cost of swimming LS SL: " // &
78  tostr(proto_parents%individual(ind)%cost_swim_std()) )
79  call log_delimiter(log_level_chapter)
80 
81  walks_beh: do i=1, 500 ! LIFESPAN
82 
83  if (proto_parents%individual(ind)%is_dead()) then
84  call log_msg("*****************")
85  call log_msg("AGENT " // tostr(proto_parents%individual(ind)%get_id()) // " DEAD" )
86  exit walks_beh
87  end if
88 
89  !Global_Time_Step_Model_Current = i
90 
91  !> Initialise stopwatch for timing each walk.
92  call stopwatch_walk_wise%start("Walk # " // tostr(i))
93 
94  if (is_debug) call log_delimiter(log_level_chapter)
95  call log_dbg("Agent walk no=" // tostr(i) // " , agent ID " // &
96  tostr(proto_parents%individual(ind)%get_id()) // &
97  " (# " // tostr(ind) // "), name:" &
98  // trim(proto_parents%individual(ind)%individ_label())&
99  // ", is male: " // tostr(proto_parents%individual(ind)%is_male()) // ".")
100 
101  call log_dbg ("Agent body length: " // &
102  tostr(proto_parents%individual(ind)%body_length) // &
103  ", body mass: " // &
104  tostr(proto_parents%individual(ind)%body_mass) // &
105  ", energy: " // &
106  tostr(proto_parents%individual(ind)%get_energy()) )
107 
108  surlig_log_dt = light_surface(global_time_step_model_current)
109  surlig_log_st = light_surface(global_time_step_model_current, daylight_stochastic)
110 
111  call log_dbg (" Light at surface deterministic: " // &
112  tostr(surlig_log_dt) // &
113  ", stochastic: " // &
114  tostr(surlig_log_st) // &
115  ", light at depth: " // &
116  tostr(proto_parents%individual(ind)%dpos()) // &
117  " is :" // &
118  tostr(light_depth(proto_parents%individual(ind)%dpos(), &
119  light_surface(global_time_step_model_current, &
121 
122  agent_in = proto_parents%individual(ind)%find_environment()
123 
124  call log_dbg( ltag_info // "Agent is in the environment " // &
125  tostr(agent_in) // ", with name " // &
126  global_habitats_available(agent_in)%get_label() )
127 
128  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
129  ! PRODUCE PERCEPTIONS
130  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
131  !===================================================
132  ! Inner perceptions: stomach, bodymass, energy, age, reproductive factor
133  call proto_parents%individual(ind)%perceptions_inner()
134 
135  !===================================================
136  ! Environmental perceptions: light, depth
137  call proto_parents%individual(ind)%perceptions_environ()
138  call log_dbg("Environmental perceptions: light " // &
139  tostr(proto_parents%individual(ind)%perceive_light%get_current()) // &
140  ", depth " // &
141  tostr(proto_parents%individual(ind)%perceive_depth%get_current()) )
142 
143  call log_dbg("Agent's data: depth: " // tostr(proto_parents%individual(ind)%dpos()) )
144 
145  !===================================================
146  ! Spatial perceptions food, conspecifics, predators
147  call proto_parents%individual(ind)%see_food( &
148  global_habitats_available( agent_in )%food )
149  !call proto_parents%individual(ind)%see_food( habitat_safe%food )
150  !call proto_parents%individual(ind)%see_food(joined_food_res_tmp)
151  call proto_parents%individual(ind)%see_consp( proto_parents%individual )
152  call proto_parents%individual(ind)%see_pred( habitat_safe%predators )
153  !> add perceptions to the memory
154  call proto_parents%individual(ind)%perception_to_memory()
155 
156  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
157  ! PRODUCE MOTIVATIONS AND GOS
158  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
159  !===================================================
160  call proto_parents%individual(ind)%motivations_percept_components()
161  call proto_parents%individual(ind)%motivations_primary_calc()
162  !> Primary motivations are logged in the @ref intro_debug_mode
163  !! "debug mode".
164  call log_dbg( ltag_info // "Primary motivations: " // &
165  "hunger: " // &
166  tostr(proto_parents%individual(ind)%motivations%hunger%motivation_prim) // &
167  ", avoid_passive: "// &
168  tostr(proto_parents%individual(ind)%motivations%avoid_passive%motivation_prim) // &
169  ", avoid_active: " // &
170  tostr(proto_parents%individual(ind)%motivations%avoid_active%motivation_prim) // &
171  ", reproduce: " // &
172  tostr(proto_parents%individual(ind)%motivations%reproduction%motivation_prim), &
173  procname, modname )
174  call proto_parents%individual(ind)%modulation()
175  call log_dbg( ltag_info // "Motivations post modulation: " // &
176  "hunger: " // &
177  tostr(proto_parents%individual(ind)%motivations%hunger%motivation_finl) // &
178  ", avoid_passive: "// &
179  tostr(proto_parents%individual(ind)%motivations%avoid_passive%motivation_finl) // &
180  ", avoid_active: " // &
181  tostr(proto_parents%individual(ind)%motivations%avoid_active%motivation_finl) // &
182  ", reproduce: " // &
183  tostr(proto_parents%individual(ind)%motivations%reproduction%motivation_finl), &
184  procname, modname )
185  call proto_parents%individual(ind)%motivations_to_memory()
186  call proto_parents%individual(ind)%gos_find()
187 
188  ! Determine population-wise maximum motivation for threshold.
190  maxval( proto_parents%individual%motivations%max_perception() )
191 
192  call log_dbg( ltag_info // "*** Maximum motivation for rescale:" // &
194  ", whole population." )
195 
196  call log_dbg(ltag_info // "*** Agent can see foods: " // &
197  tostr(proto_parents%individual(ind)%perceive_food%get_count()))
198  if ( proto_parents%individual(ind)%has_food() ) then
199  call log_dbg(" distance >" // &
200  tostr(proto_parents%individual(ind)%perceive_food%foods_distances))
201  call log_dbg(" size >" // &
202  tostr(proto_parents%individual(ind)%perceive_food%foods_seen%get_size() ))
203  call log_dbg(" dist. (d/sl) >" // &
204  tostr(proto_parents%individual(ind)%perceive_food%foods_distances &
205  / proto_parents%individual(ind)%get_length()))
206  block
207  integer :: i
208  real(SRP), allocatable, dimension(:) :: visibility, p_capture
209  allocate(visibility(proto_parents%individual(ind)%perceive_food%get_count()))
210  allocate(p_capture(proto_parents%individual(ind)%perceive_food%get_count()))
211  do i=1,proto_parents%individual(ind)%perceive_food%get_count()
212  visibility(i)=proto_parents%individual(ind)%perceive_food%foods_seen(i)%visibility()
213  p_capture(i)= &
214  proto_parents%individual(ind)%perceive_food%foods_seen(i)%capture_probability( &
215  distance=proto_parents%individual(ind)%perceive_food%foods_distances(i) )
216  end do
217  call log_dbg(" vis range >" // tostr(visibility))
218  call log_dbg(" dist. (d/vr) >" // &
219  tostr(proto_parents%individual(ind)%perceive_food%foods_distances &
220  / visibility ))
221  call log_dbg(" P capture >" // tostr(p_capture))
222  end block
223  end if
224  call log_dbg(" average distance: " // &
225  tostr(proto_parents%individual(ind)%perceive_food%get_meandist()))
226  call log_dbg(" memory food N: " // &
227  tostr(proto_parents%individual(ind)%memory_stack%memory_food))
228  call log_dbg(" memory food dist: " // &
229  tostr(proto_parents%individual(ind)%memory_stack%memory_foodist))
230  call log_dbg(" memory food size: " // &
231  tostr(proto_parents%individual(ind)%memory_stack%memory_foodsiz))
232  call log_dbg(" average distance in memory: " // &
233  tostr(proto_parents%individual(ind)%memory_stack%get_food_mean_dist()))
234  call log_dbg(" probability of capture subjective: " // &
235  tostr(proto_parents%individual(ind)%food_probability_capture_subjective()))
236 
237  call log_dbg(ltag_info // "*** Agent can see conspecifics: " // &
238  tostr(proto_parents%individual(ind)%perceive_consp%get_count()))
239 
240  if ( proto_parents%individual(ind)%has_consp() ) then
241  call log_dbg(" iid >" // &
242  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_cid()))
243  !call LOG_DBG(" sex (is male), subjective >" // &
244  ! TOSTR(proto_parents%individual(ind)%perceive_consp%conspecifics_seen%is_male()))
245  call log_dbg(" sex (is male), objective >" // &
246  tostr(proto_parents%individual( &
247  proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_cid() )%is_male()))
248  !call LOG_DBG(" mass subjective >" // &
249  ! TOSTR(proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_mass()))
250  call log_dbg(" mass objective >" // &
251  tostr(proto_parents%individual( &
252  proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_cid() )%get_mass()))
253  call log_dbg( "Probability of reproduction: " // &
254  tostr(proto_parents%individual(ind)%probability_reproduction()) )
255  end if
256 
257  call log_dbg(ltag_info // "*** Agent can see predators: " // &
258  tostr(proto_parents%individual(ind)%perceive_predator%get_count()))
259 
260  if (proto_parents%individual(ind)%starved_death()) then
261  call proto_parents%individual(ind)%dies()
262  if (is_debug) call log_delimiter(log_level_section)
263  call log_dbg ( "INFO: Agent dies due to starvation, ID: " // &
264  tostr(proto_parents%individual(ind)%get_id()) // &
265  "at time step " // tostr(i) )
266  call log_dbg (" Body length: " // &
267  tostr(proto_parents%individual(ind)%body_length) // &
268  ", body mass: " // &
269  tostr(proto_parents%individual(ind)%body_mass) // &
270  ", maximum mass: " // &
271  tostr(proto_parents%individual(ind)%body_mass_maximum) // &
272  ", birth mass : " // &
273  tostr(proto_parents%individual(ind)%body_mass_birth) )
274  call log_dbg(" Energy :" // &
275  tostr(proto_parents%individual(ind)%energy_current) // &
276  ", energy maximum: " // &
277  tostr(proto_parents%individual(ind)%energy_maximum) )
278  if (is_debug) call log_delimiter(log_level_section)
279  call log_dbg(stopwatch_walk_wise%log())
280  exit walks_beh
281  end if
282 
283  call log_dbg( "GOS is :" // proto_parents%individual(ind)%gos_label() )
284  call log_dbg( "GOS arousal :" // tostr(proto_parents%individual(ind)%arousal()) )
285 
286 
287  call log_dbg("Before walk:")
288  call log_dbg(" +++ Current mass: " // tostr(proto_parents%individual(ind)%mass()) // &
289  ", length: " // tostr(proto_parents%individual(ind)%length()) // &
290  ", energy: " // tostr(proto_parents%individual(ind)%get_energy()) )
291 
292  call log_dbg( ltag_info // "Mean distance to food items: " // &
293  tostr(proto_parents%individual(ind)%memory_stack%get_food_mean_dist()) , &
294  procname, modname )
295  block
296  real(SRP) :: dist_food_sl
297  dist_food_sl = proto_parents%individual(ind)%memory_stack%get_food_mean_dist() / &
298  proto_parents%individual(ind)%get_length()
299  call log_dbg( ltag_info // "Mean distance to food items: " // &
300  tostr(dist_food_sl) // " SL units of agent ", &
301  procname, modname )
302  end block
303 
304  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
305  ! PRODUCE BEHAVIOURS
306  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
307  if (is_debug) call log_delimiter(log_level_chapter)
308  call log_dbg(ltag_info // "START BEHAVIOUR (do_behave)")
309 
310  call proto_parents%individual(ind)%do_behave( &
311  rescale_max_motivation= &
312  global_rescale_maximum_motivation)
313 
314  if (is_debug) call log_delimiter(log_level_chapter)
315  call log_dbg(ltag_info // "EXECUTED BEHAVIOUR: " // &
316  proto_parents%individual(ind)%behaviour_is() )
317  if (is_debug) call log_delimiter(log_level_chapter)
318  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
319 
320  call log_dbg("After walk:")
321  call log_dbg(" +++ Current mass: " // tostr(proto_parents%individual(ind)%mass()) // &
322  ", length: " // tostr(proto_parents%individual(ind)%length()) // &
323  ", energy: " // tostr(proto_parents%individual(ind)%get_energy()) )
324 
325  call log_dbg( ltag_info // "Current testosterone: " // &
326  tostr(proto_parents%individual(ind)%testosterone_level) // &
327  ", current estrogen: " // &
328  tostr(proto_parents%individual(ind)%estrogen_level ) )
329 
330  if ( proto_parents%individual(ind)%is_male() ) &
331  call log_dbg( ltag_info // "Testosterone: " // tostr(proto_parents%individual(ind)%testosterone_history ) )
332 
333  if ( proto_parents%individual(ind)%is_female() ) &
334  call log_dbg( ltag_info // "Estrogen: " // tostr(proto_parents%individual(ind)%estrogen_history ) )
335 
336  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
337  ! Update sex steroids, subtract living cost
338  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
339  call proto_parents%individual(ind)%sex_steroids_update()
340  call proto_parents%individual(ind)%subtract_living_cost()
341  call proto_parents%individual(ind)%energy_update()
342 
343 
344  call proto_parents%individual(ind)%age_increment()
345 
346  end do walks_beh
347 
348  end do do_inds
349 
350  call generation_one%fitness_calc()
351 
352  !> @important Save the individual data for the whole parent population,
353  !! generation one, to a csv file.
354  save_data_inds_debug_end: block
355  character(len=:), allocatable :: output_data_file
356  !> Save individual data
357  output_data_file = "agents_" // model_name // "_" // mmdd // &
358  "_gen_" // tostr(global_generation_number_current, &
359  generations) // "_end" // csv
360  call proto_parents%save_csv(output_data_file, is_logging=.true.)
361  end block save_data_inds_debug_end
362 
363  ! Note that disassemble is necessary to update the original habitats
364  ! from the global habitat array
365  call disassemble ( habitat_safe, habitat_dangerous )
366 
367  call joined_food_res_tmp%save_csv(csv_file_name="foods_joined_after.csv")
368  call habitat_safe%food%save_csv(csv_file_name="foods_safe_after.csv")
369  call habitat_dangerous%food%save_csv(csv_file_name="foods_dangerous_after.csv")
370 
371 
372 
373  call system_halt(message="NORMAL TERMINATION DEBUG TEST PROCEDURE")
374 
375  !###########################################################################
376 
377  inds: do ind=1, 20 !! proto_parents%population_size
378 
379  if (proto_parents%individual(ind)%is_dead()) exit inds
380 
381  way_passed = missing
382 
383  !call LOG_DBG("**** ind=" // TOSTR(ind))
384  ! place agent from zero point
385  !call proto_parents%individual(ind)%position( SPATIAL(0., 0., 0.) )
386  ! place agent randomly not needed as they are positioned uniformly at
387  ! GENERATIONS_LOOP_GA
388  !call proto_parents%individual(ind)%place_uniform(habitat_safe)
389 
390  if (is_debug) call log_delimiter(log_level_volume)
391  call log_dbg("********** Agent " // tostr(ind) // "*****************")
392  call log_dbg("Agent location one:" // &
393  tostr(proto_parents%individual(ind)%location(.true.)))
394  call log_dbg ("Agent body length: " // &
395  tostr(proto_parents%individual(ind)%body_length) // &
396  ", body mass: " // &
397  tostr(proto_parents%individual(ind)%body_mass) // &
398  ", energy: " // &
399  tostr(proto_parents%individual(ind)%get_energy()) // "=" // &
400  tostr(energy_reserve(proto_parents%individual(ind)%body_mass, &
401  proto_parents%individual(ind)%body_length)) )
402 
403  walks: do i=1, 100
404 
405  !> Initialise stopwatch for timing each walk.
406  call stopwatch_walk_wise%start("Walk # " // tostr(i))
407 
408  if (is_debug) call log_delimiter(log_level_chapter)
409  call log_dbg("Agent walk no=" // tostr(i) // " , agent ID " // &
410  tostr(proto_parents%individual(ind)%get_id()) // &
411  " (# " // tostr(ind) // "), name:" &
412  // trim(proto_parents%individual(ind)%individ_label())&
413  // ", is male: " // tostr(proto_parents%individual(ind)%is_male()) // ".")
414 
415  call log_dbg ("Agent body length: " // &
416  tostr(proto_parents%individual(ind)%body_length) // &
417  ", body mass: " // &
418  tostr(proto_parents%individual(ind)%body_mass) // &
419  ", energy: " // &
420  tostr(proto_parents%individual(ind)%get_energy()) )
421 
422  ! do random walk
423  call stopwatch_op_current%start()
424 
425  step_rwalk = dist2step(400.0)
426  call log_dbg(" Step size for random walk: " // tostr(step_rwalk) // &
427  ", " // tostr(step_rwalk / proto_parents%individual(ind)%get_length()) // &
428  " agent's body sizes." )
429 
430  call proto_parents%individual(ind)%rwalk( step_rwalk, 0.50, &
431  habitat_safe)
432 
433  call log_dbg(" cycle ind:walk "// tostr(ind) // ":"// tostr(i) // &
434  ", Loc: " // tostr(proto_parents%individual(ind)%location(.true.)))
435  call log_dbg(" way "// &
436  tostr(proto_parents%individual(ind)%way()))
437 
438  !> Update the i-th walk distance/way for distribution plot.
439  way_passed(i) = proto_parents%individual(ind)%way()
440 
441  cost_step = proto_parents%individual(ind)%cost_swim(distance=way_passed(i))
442  call log_dbg(" Cost of random walk step: " // tostr(cost_step) // &
443  " is " // tostr(100.0_srp * cost_step / proto_parents%individual(ind)%body_mass ) // &
444  "% of agent's body mass." )
445 
446  !> Introduce the cost of swimming here:
447  proto_parents%individual(ind)%body_mass=proto_parents%individual(ind)%body_mass - &
448  cost_step
449 
450  !===================================================
451  ! Inner perceptions: stomach, bodymass, energy, age, reproductive factor
452  call proto_parents%individual(ind)%perceptions_inner()
453 
454  !===================================================
455  ! Environmental perceptions: light, depth
456  call proto_parents%individual(ind)%perceptions_environ()
457  call log_dbg("Environmental perceptions: light " // &
458  tostr(proto_parents%individual(ind)%perceive_light%get_current()) // &
459  ", depth " // &
460  tostr(proto_parents%individual(ind)%perceive_depth%get_current()) )
461 
462  call log_dbg("Agent's data: depth: " // tostr(proto_parents%individual(ind)%dpos()) )
463 
464  !===================================================
465  ! Spatial perceptions food, conspecifics, predators
466 
467  !call proto_parents%individual(ind)%see_food(habitat_safe%food)
468  call proto_parents%individual(ind)%see_food(joined_food_res_tmp)
469 
470  call proto_parents%individual(ind)%see_consp( proto_parents%individual )
471 
472  call proto_parents%individual(ind)%see_pred( habitat_safe%predators )
473 
474  !===================================================
475  call proto_parents%individual(ind)%motivations_percept_components()
476  call proto_parents%individual(ind)%motivations_primary_calc()
477  call proto_parents%individual(ind)%modulation()
478  call proto_parents%individual(ind)%motivations_to_memory()
479  call proto_parents%individual(ind)%gos_find()
480 
481  if (proto_parents%individual(ind)%starved_death()) then
482  call proto_parents%individual(ind)%dies()
483  if (is_debug) call log_delimiter(log_level_section)
484  call log_dbg ("INFO: Agent dies due to starvation, ID: " // &
485  tostr(proto_parents%individual(ind)%get_id()))
486  call log_dbg (" Body length: " // &
487  tostr(proto_parents%individual(ind)%body_length) // &
488  ", body mass: " // &
489  tostr(proto_parents%individual(ind)%body_mass) // &
490  ", maximum mass: " // &
491  tostr(proto_parents%individual(ind)%body_mass_maximum) // &
492  ", birth mass : " // &
493  tostr(proto_parents%individual(ind)%body_mass_birth) )
494  call log_dbg(" Energy :" // &
495  tostr(proto_parents%individual(ind)%energy_current) // &
496  ", energy maximum: " // &
497  tostr(proto_parents%individual(ind)%energy_maximum) )
498  if (is_debug) call log_delimiter(log_level_section)
499  call log_dbg(stopwatch_walk_wise%log())
500  exit walks
501  end if
502 
503  call log_dbg( "GOS is :" // proto_parents%individual(ind)%gos_label() )
504  call log_dbg( "GOS arousal :" // tostr(proto_parents%individual(ind)%arousal()) )
505 
506  call log_dbg("**** can see food: " // tostr(proto_parents%individual(ind)%perceive_food%get_count()))
507  has_food_check: if ( proto_parents%individual(ind)%has_food() ) then
508  call log_dbg(" coord(1) >" // &
509  tostr(proto_parents%individual(ind)%perceive_food%foods_seen(1)%location(.true.)))
510  call log_dbg(" distance >" // &
511  tostr(proto_parents%individual(ind)%perceive_food%foods_distances))
512  call log_dbg(" dist. (d/l) >" // &
513  tostr(proto_parents%individual(ind)%perceive_food%foods_distances &
514  / proto_parents%individual(ind)%get_length()))
515  call log_dbg(" available >" // &
516  tostr(proto_parents%individual(ind)%perceive_food%foods_seen%is_available()))
517  call log_dbg(" iid >" // &
518  tostr(proto_parents%individual(ind)%perceive_food%foods_seen%get_iid()))
519 
520  do iii=1, proto_parents%individual(ind)%perceive_food%get_count()
521  call log_dbg(" Prob:" // tostr(iii) // "=" // &
522  tostr(proto_parents%individual(ind)%perceive_food%foods_seen(iii)%capture_probability( &
523  distance=proto_parents%individual(ind)%perceive_food%foods_distances(iii) ) ) // &
524  " dist:" // tostr(proto_parents%individual(ind)%perceive_food%foods_distances(iii)) // &
525  " size:" // tostr(proto_parents%individual(ind)%perceive_food%foods_seen(iii)%get_size()) // &
526  " mass:" // tostr(proto_parents%individual(ind)%perceive_food%foods_seen(iii)%get_mass()) &
527  )
528  end do
529 
530  ! Test above and below functions for spatial objects.
531  call log_dbg(" Food items are above: " // &
532  tostr( proto_parents%individual(ind)%is_above(proto_parents%individual(ind)%perceive_food%foods_seen) ) )
533 
534  call log_dbg(" Food items are below: " // &
535  tostr( proto_parents%individual(ind)%is_below(proto_parents%individual(ind)%perceive_food%foods_seen) ) )
536 
537  call log_dbg(" **Food items are above (user operator test): " // &
538  tostr( proto_parents%individual(ind) .above. proto_parents%individual(ind)%perceive_food%foods_seen ) )
539  if (proto_parents%individual(ind) .above. proto_parents%individual(ind)%perceive_food%foods_seen(1)) &
540  call log_dbg(" **Test operator .above. :: agent is above the first item TRUE; " // &
541  "agent dpos: " // tostr(proto_parents%individual(ind)%dpos()) // &
542  ", food item dpos: " // tostr( proto_parents%individual(ind)%perceive_food%foods_seen(1)%dpos() ) )
543 
544  call log_dbg(" ++Food items are below (user operator test): " // &
545  tostr( proto_parents%individual(ind) .below. proto_parents%individual(ind)%perceive_food%foods_seen ) )
546  if (proto_parents%individual(ind) .below. proto_parents%individual(ind)%perceive_food%foods_seen(1)) &
547  call log_dbg(" ++Test operator .below. :: agent is below the first item TRUE; " // &
548  "agent dpos: " // tostr(proto_parents%individual(ind)%dpos()) // &
549  ", food item dpos: " // tostr( proto_parents%individual(ind)%perceive_food%foods_seen(1)%dpos() ) )
550 
551  call log_dbg("Relative functions test:")
552  call log_dbg(" Food items above: " // tostr(proto_parents%individual(ind)%food_items_above()))
553  call log_dbg(" Food items mass above: " // tostr(proto_parents%individual(ind)%food_mass_above()))
554  call log_dbg(" Food items below: " // tostr(proto_parents%individual(ind)%food_items_below()))
555  call log_dbg(" Food items mass below: " // tostr(proto_parents%individual(ind)%food_mass_below()))
556 
557  !exit INDS
558  call log_dbg( "Finding food took " // tostr(stopwatch_op_current%elapsed()) )
559 
560  !===================================================
561  call log_dbg(" +++ Current mass: " // tostr(proto_parents%individual(ind)%mass()) // &
562  ", length: " // tostr(proto_parents%individual(ind)%length()) // &
563  ", energy: " // tostr(proto_parents%individual(ind)%get_energy()) )
564 
565  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
566  !++++++++++++ select food item +++++++++++++++++++++++
567  food_item_selected = proto_parents%individual(ind)%food_item_select(rescale_max_motivation=6.0_srp)
568  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
569  !call proto_parents%individual(ind)%do_eat_food_item(food_item_selected, habitat_safe%food)
570 
571  ! ++++ EAT FOOD ITEM
572  call proto_parents%individual(ind)%do_eat_food_item(food_item_selected, joined_food_res_tmp)
573 
574  call log_dbg("**** Tried to eat food item: " // tostr(food_item_selected))
575  call log_dbg(" +++ Updated mass: " // tostr(proto_parents%individual(ind)%mass()) // &
576  ", length: " // tostr(proto_parents%individual(ind)%length()) // &
577  ", energy: " // tostr(proto_parents%individual(ind)%get_energy()) )
578 
579  call log_dbg("Selected food iid " // &
580  tostr(proto_parents%individual(ind)%perceive_food%foods_seen(food_item_selected)%food_iid) )
581 
582  call log_dbg("Selected item status " // &
583  tostr(proto_parents%individual(ind)%perceive_food%foods_seen(food_item_selected)%eaten) )
584 
585  iii = proto_parents%individual(ind)%perceive_food%foods_seen(food_item_selected)%food_iid
586  call log_dbg("Selected food iid " // tostr(iii) )
587  call log_dbg("Selected item status " // &
588  tostr(proto_parents%individual(ind)%perceive_food%foods_seen(food_item_selected)%eaten) )
589 
590  !call LOG_DBG("Selected food item iid in resource " // TOSTR(habitat_safe%food%food(iii)%food_iid) )
591  call log_dbg("Selected food item iid in resource " // tostr(joined_food_res_tmp%food(iii)%food_iid) )
592  !call LOG_DBG("Selected food item status in resource " // TOSTR(habitat_safe%food%food(iii)%eaten) )
593  call log_dbg("Selected food item status in resource " // tostr(joined_food_res_tmp%food(iii)%eaten) )
594 
595  else has_food_check
596  !> If no food objects were encountered we still grow with zero food gain.
597  call proto_parents%individual(ind)%mass_grow(0.0_srp)
598  call proto_parents%individual(ind)%len_grow(0.0_srp)
599  end if has_food_check
600 
601  call log_dbg("Food items status " // tostr(proto_parents%individual(ind)%perceive_food%foods_seen%eaten) )
602  !print *, "********** Food items status ", proto_parents%individual(ind)%perceive_food%foods_seen%eaten
603 
604  call log_dbg("**** can see consp: " // tostr(proto_parents%individual(ind)%perceive_consp%get_count() ) )
605  has_consp_chk: if ( proto_parents%individual(ind)%has_consp() ) then
606  call log_dbg(" coord(1) >" // &
607  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen(1)%location(.true.)))
608  call log_dbg(" iid >" // &
609  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_cid()))
610  call log_dbg(" sex (male) >" // &
611  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen%is_male()))
612  call log_dbg(" sex (male) ?>" // &
613  tostr(proto_parents%individual( &
614  proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_cid() )%is_male()))
615 
616  call log_dbg(" mass >" // &
617  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_mass()))
618  call log_dbg(" mass ?>" // &
619  tostr(proto_parents%individual( &
620  proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_cid() )%get_mass()))
621  call log_dbg( "Probability of reproduction: " // &
622  tostr(proto_parents%individual(ind)%probability_reproduction()) )
623 
624  call log_dbg( "Absolute difference in mass (all sex agents): " // &
625  tostr( proto_parents%individual(ind)%get_mass() - &
626  average( proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_mass() ) ) &
627  )
628  call log_dbg( "Delta mass relative (all sex agents): " // &
629  tostr( &
630  within( proto_parents%individual(ind)%get_mass() / &
631  average( proto_parents%individual(ind)%perceive_consp%conspecifics_seen%get_mass() ), &
632  minval(probability_reproduction_delta_mass_abscissa), &
633  maxval(probability_reproduction_delta_mass_abscissa) &
634  ) ) )
635 
636  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
637  ! ++++++++++++++++++++++ slect conspecific ++++++++++++++++++++++++++++
638  conspecif_selected = proto_parents%individual(ind)%consp_select(rescale_max_motivation=6.0_srp)
639  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
640  call log_dbg("Selected conspecific No: " // tostr(conspecif_selected) )
641  call log_dbg("Selected consp iid: " // &
642  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen(conspecif_selected)%cid) )
643 
644  call log_dbg("Selected consp is male: " // &
645  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen(conspecif_selected)%sex_is_male ) )
646 
647  call log_dbg("Selected consp mass: " // &
648  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen(conspecif_selected)%get_mass() ) )
649 
650  call log_dbg("Selected conspecific coordinates: " // &
651  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen(conspecif_selected)%x) // "," // &
652  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen(conspecif_selected)%y) // "," // &
653  tostr(proto_parents%individual(ind)%perceive_consp%conspecifics_seen(conspecif_selected)%depth) )
654 
655  ! stop
656  !exit INDS
657  end if has_consp_chk
658 
659  call log_dbg("**** can see pred: " // tostr(proto_parents%individual(ind)%perceive_predator%get_count() ) )
660  if ( proto_parents%individual(ind)%has_pred() ) then
661  call log_dbg(" coord(1) =" // &
662  tostr(proto_parents%individual(ind)%perceive_predator%predators_seen(1)%location(.true.)))
663  call log_dbg(" iid =" // &
664  tostr(proto_parents%individual(ind)%perceive_predator%predators_seen%get_cid()))
665  call log_dbg(" dist =" // &
666  tostr(proto_parents%individual(ind)%perceive_predator%predators_seen%get_dist()))
667  call log_dbg(" attack rates=" // &
668  tostr(proto_parents%individual(ind)%perceive_predator%predators_attack_rates))
669 
670  call log_dbg(" Probability of capture by predator 1: " // &
671  tostr(proto_parents%individual(ind)%risk_pred( &
672  proto_parents%individual(ind)%perceive_predator%predators_seen(1), &
673  proto_parents%individual(ind)%perceive_predator%predators_attack_rates(1) )))
674  ! stop
675  !exit INDS
676  ! Calculate visual range for the predator
677  irradiance = &
678  light_depth( depth=proto_parents%individual(ind)%dpos(), &
679  surface_light = &
680  light_surface(tstep=global_time_step_model_current, &
681  is_stochastic=daylight_stochastic) )
682  visrange = m2cm( visual_range( &
683  irradiance = irradiance, &
684  prey_area = &
685  carea( cm2m( proto_parents%individual(ind)% &
686  perceive_predator%predators_seen(1)%get_size() ) ), &
687  prey_contrast = preycontrast_default ) )
688  call log_dbg(" visual range =" // tostr(visrange) )
689  !stop
690 
691  end if
692 
693  !call LOG_DBG( "**** Probability of reproduction: " // &
694  ! TOSTR(proto_parents%individual(ind)%probability_reproduction()) )
695 
696  !> add perceptions to the memory
697  call proto_parents%individual(ind)%perception_to_memory()
698 
699 
700  call log_dbg("INFO: Subtracting cost of living for agent # " // &
701  tostr(ind) // " and add weight and length to the history.")
702 
703  !> Subtract the cost of living
704  call proto_parents%individual(ind)%subtract_living_cost()
705 
706  !> Update increments to sex steroids.
707  call proto_parents%individual(ind)%sex_steroids_update()
708 
709  call add_to_history(proto_parents%individual(ind)%body_length_history, &
710  proto_parents%individual(ind)%body_length)
711 
712  call add_to_history(proto_parents%individual(ind)%body_mass_history, &
713  proto_parents%individual(ind)%body_mass)
714 
715  call log_dbg(stopwatch_walk_wise%log())
716 
717  call proto_parents%individual(ind)%destroy_perception()
718  call log_dbg("Perceptions destroyed for ind " // tostr(ind) // "("// tostr(i) // ")")
719 
720  end do walks
721 
722  !> Saving histograms of `way_passed`, the distance of each walk.
723  call log_dbg("Average walk step distance: " // tostr(average(way_passed)) )
724  call debug_histogram_save(x_data=way_passed, &
725  csv_out_file="debug_hist_way_" // mmdd // "_g" // &
726  tostr(global_generation_number_current) // "_ind_" // &
727  tostr(ind) // csv, &
728  delete_csv=.false., enable_non_debug=.true. )
729 
730  call log_dbg("INFO: Subtracting cost of living for agent # " // &
731  tostr(ind) // " and add weight and length to the history.")
732 
733  !> Subtract the cost of living
734  call proto_parents%individual(ind)%subtract_living_cost()
735 
736  call proto_parents%individual(ind)%sex_steroids_update()
737 
738  call add_to_history(proto_parents%individual(ind)%body_length_history, &
739  proto_parents%individual(ind)%body_length)
740 
741  call add_to_history(proto_parents%individual(ind)%body_mass_history, &
742  proto_parents%individual(ind)%body_mass)
743 
744  if (proto_parents%individual(ind)%starved_death()) then
745  call proto_parents%individual(ind)%dies_debug()
746  if (is_debug) call log_delimiter(log_level_section)
747  call log_dbg ("INFO: Agent dies due to starvation, ID: " // &
748  tostr(proto_parents%individual(ind)%get_id()))
749  call log_dbg (" Body length: " // &
750  tostr(proto_parents%individual(ind)%body_length) // &
751  ", body mass: " // &
752  tostr(proto_parents%individual(ind)%body_mass) // &
753  ", maximum mass: " // &
754  tostr(proto_parents%individual(ind)%body_mass_maximum) // &
755  ", birth mass : " // &
756  tostr(proto_parents%individual(ind)%body_mass_birth) )
757  call log_dbg(" Energy :" // &
758  tostr(proto_parents%individual(ind)%energy_current) // &
759  ", energy maximum: " // &
760  tostr(proto_parents%individual(ind)%energy_maximum) )
761  if (is_debug) call log_delimiter(log_level_section)
762  end if
763 
764 
765 
766  ! DEBUG: save movement history of each agent.
767  call csv_matrix_write ( reshape( &
768  [proto_parents%individual(ind)%history%x, &
769  proto_parents%individual(ind)%history%y, &
770  proto_parents%individual(ind)%history%depth], &
771  [history_size_spatial, 3]), &
772  "zz_move_hist_" // mmdd // &
773  "_gen_" // tostr(global_generation_number_current, generations) // &
774  "_ind_" // tostr(ind) // csv, &
775  ["X","Y", "Z"] &
776  )
777 
778  ! DEBUG: save body mass history of each agent.
779  call csv_matrix_write ( reshape( &
780  [proto_parents%individual(ind)%body_length_history, &
781  proto_parents%individual(ind)%body_mass_history], &
782  [history_size_agent_prop, 2]), &
783  "zz_length_mass_hist_" // mmdd // &
784  "_gen_" // tostr(global_generation_number_current, generations) // &
785  "_ind_" // tostr(ind) // csv, &
786  ["LENGTH","MASS "] &
787  )
788 
789  ! DEBUG: save perception history of each agent.
790  call csv_matrix_write ( reshape( &
791  [real(proto_parents%individual(ind)%memory_stack%memory_food), &
792  proto_parents%individual(ind)%memory_stack%memory_foodsiz, &
793  proto_parents%individual(ind)%memory_stack%memory_reprfac ], &
794  [history_size_perception, 3]), &
795  "zz_percept_" // mmdd // &
796  "_gen_" // tostr(global_generation_number_current, generations) // &
797  "_ind_" // tostr(ind) // "_name_" // &
798  trim(proto_parents%individual(ind)%individ_label()) // csv, &
799  ["PRC_FOOD ","PRC_FOOD_SIZ","PRC_REPRFAC "] &
800  )
801 
802 
803  end do inds
804 
805  print *, "************** INDS ended "
806  !stop
807 
808  !> Save the individual data for the whole test parent population,
809  !! generation one, to a csv file. This is after the walk states of
810  !! the agents.
811  save_data_inds_init: block
812  character(len=:), allocatable :: output_data_file
813  !> Save individual data
814  output_data_file = "agents_" // model_name // "_" // mmdd // &
815  "_gen_" // tostr(global_generation_number_current, &
816  generations) // "_z_end" // csv
817  call proto_parents%save_csv(output_data_file, is_logging=.true.)
818  end block save_data_inds_init
819 
820  call csv_matrix_write ( reshape( &
821  [ joined_food_res_tmp%food%x, &
822  joined_food_res_tmp%food%y, &
823  joined_food_res_tmp%food%depth, &
824  joined_food_res_tmp%food%size, &
825  conv_l2r(joined_food_res_tmp%food%eaten), &
826  real(joined_food_res_tmp%food%food_iid,SRP)], &
827  [joined_food_res_tmp%number_food_items, 6]), &
828  "zzz_zzz_food_ALL_after_" // MODEL_NAME // "_" // MMDD // &
829  "_gen_" // TOSTR(Global_Generation_Number_Current, GENERATIONS) // csv, &
830  ["X ","Y ", "D ", "SIZE", "EATN", "IID "] &
831  )
832 
833  call joined_food_res_tmp%unjoin( habitat_safe%food, habitat_dangerous%food, reindex=.true. )
834 
835  call joined_food_res_tmp%destroy()
836 
837  call csv_matrix_write ( reshape( &
838  [ habitat_safe%food%food%x, &
839  habitat_safe%food%food%y, &
840  habitat_safe%food%food%depth, &
841  habitat_safe%food%food%size, &
842  conv_l2r(habitat_safe%food%food%eaten), &
843  real(habitat_safe%food%food%food_iid,SRP)], &
844  [habitat_safe%food%number_food_items, 6]), &
845  "zzz_food_safe_" // MODEL_NAME // "_" // MMDD // &
846  "_gen_" // TOSTR(Global_Generation_Number_Current, GENERATIONS) // csv, &
847  ["X ","Y ", "D ", "SIZE", "EATN", "IID "] &
848  )
849 
850  call csv_matrix_write ( reshape( &
851  [ habitat_dangerous%food%food%x, &
852  habitat_dangerous%food%food%y, &
853  habitat_dangerous%food%food%depth, &
854  habitat_dangerous%food%food%size, &
855  conv_l2r(habitat_dangerous%food%food%eaten), &
856  real(habitat_dangerous%food%food%food_iid,SRP)],&
857  [habitat_dangerous%food%number_food_items, 6]), &
858  "zzz_food_dang_" // MODEL_NAME // "_" // MMDD // &
859  "_gen_" // TOSTR(Global_Generation_Number_Current, GENERATIONS) // csv, &
860  ["X ","Y ", "D ", "SIZE", "EATN", "IID "] &
861  )
862 
863  call system_halt(message="NORMAL TERMINATION DEBUG TEST PROCEDURE")
864 
865 end subroutine life_cycles_debug_test
866 
867 !=============================================================================
COMMONDATA – definitions of global constants and procedures.
Definition: m_common.f90:1497
logical, public, protected is_debug
Sets the model in the debug mode if TRUE. The Debug mode generates huge additional outputs and logs....
Definition: m_common.f90:1981
character(len= *), parameter, private modname
MODNAME always refers to the name of the current module for use by the LOGGER function LOG_DBG....
Definition: m_common.f90:1591
logical, parameter, public daylight_stochastic
Flag for stochastic daylight pattern (if TRUE) or deterministic sinusoidal (when FALSE)....
Definition: m_common.f90:2513
subroutine log_dbg(message_string, procname, modname)
LOG_DBG: debug message to the log. The message goes to the logger only when running in the DEBUG mode...
Definition: m_common.f90:9171
logical, parameter, public true
Safety parameter avoid errors in logical values, so we can now refer to standard Fortran ....
Definition: m_common.f90:1632
real(srp), public global_rescale_maximum_motivation
Global maximum sensory information that is updated for the whole population of agents.
Definition: m_common.f90:4888
logical, public, protected is_screen_output
Sets the model in screen output mode. If TRUE, the logger output goes to the screen (standard output ...
Definition: m_common.f90:1999
integer, public global_time_step_model_current
The current global time step of the model. This is a global non fixed-parameter variable that is upda...
Definition: m_common.f90:2095
character(len= *), parameter, public ltag_info
Definition: m_common.f90:1821
Definition of high level file objects.
Definition: m_fileio.f90:110
Implementation of the genetic algorithm.
Definition: m_evolut.f90:14
type(population), pointer, public proto_parents
Definition: m_evolut.f90:52
type(habitat), public habitat_dangerous
Definition: m_evolut.f90:45
type(habitat), public habitat_safe
We have an environment composed of two habitats, safe and a dangerous.
Definition: m_evolut.f90:45
subroutine life_cycles_debug_test
Debugging and testing lifecycle, random wakls etc. Has been initially implemented as DEBUG_03: block
Definition: p_debug.f90:21
CPU timer container object for debugging and speed/performance control. Arbitrary timers can be insta...
Definition: m_common.f90:1883