DEB-IBM for southern elephant seals

DEB-IBM for southern elephant seals  preview image

1 collaborator

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.0.1 • Viewed 139 times • Downloaded 5 times • Run 0 times
Download the 'DEB-IBM for southern elephant seals ' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Info tab cannot be displayed because of an encoding error

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;; ======================================== INTRODUCITON =============================================
;; ========================== DEFINITION OF PARAMETERS AND STATE VARIABLES ===========================
   ;; Original model from Martin et al 2012/13
   ;; Adjustments have been made to fit the model to southern elephant seal data  for female seals only.
   ;; Values selected as per DEBtool unless otherwise specified, the definition of parameters for the
   ;; individuals the notation follows the DEBtool-notation as far as possible. Deviation: rates are
   ;; indicated with "_rate"

   ;; The model description and sensitivity analysis of the baseline model is published in PLoS ONE,
   ;; Goedegebuure et al. (2018, DOI 10.1371/journal.pone.0194950) Modelling southern elephant seals
   ;; Mirounga leonina using an individual-based model coupled with a dynamic energy budget.
   ;; For which the actual model is available from http://modelingcommons.org/browse/one_model/5348

   ;; The model description and modifications from the baseline model that allow for the running
   ;; of the scenarios are to be published, but are as yet only available in Chapter 4 of my thesis:
   ;; Using the DEB-IBM to assess the drivers of the decreasing population of elephant seals at
   ;; Macquarie Island (submitted to external examiners May 3, 2018).

   ;; General model information and handbook are found at https://ccl.northwestern.edu/netlogo/docs/
;; ========================================== MODEL START ==============================================
;; Declarations
;extensions [            ;; extensions do not work in the online version of NetLogo - thus blocked out
;  profiler              ;; for profiling of the model, adjusted on the interface
;]
globals[                 ;; set global parameters for the model
  my-seed
  model_just_started
  max-ticks
  counting
  ;; - - - - - - - - - - - - - - - time management
  ;; ---------------- to keep track of the day, month, and year in the model
  day-of-year
  day-of-month
  month-of-year
  year
  pups-born     ;; for overall output

  ;; ------------------- for competition calculations
  population          ; P
  competition         ; Delta P
  carrying_capacity   ; K

  ;; - - - - - - - - - - - - - - - stages for IBM
  foetus        ;;0           ;; U_H < U_H^b                 ; not yet born
  pup           ;;1           ;; U_H > U_H^b & U_H < U_H^x   ; born and weaning
  juvenile      ;;2           ;; U_H > U_H^x & U_H < U_H^p   ; past weaning
  mature        ;;3           ;; U_H > U_H^p                 ; mature

  ;; - - - - - - - - - - - - - - - status for IBM
  mother-dependent  ;; 0 - foetus or pup
  fasting           ;; 1 - resting/moulting/lactating
  foraging          ;; 2

  ;; -------------- pup mortality
  pup-mortality

  ;============================================================================================= HYPOTHESIS TESTING
  ;---------- K for Density dependence
  k1-k2                           ; the difference between K1 and K2
  rate-of-decline                 ; the number of individuals that the population declines by per year
  t-for-k2                        ; new time to set next level of K
  change-time                     ; for periodic change in K
  capacity-change                 ; for changing K by some fraction
  change-factor                   ; 0/1 for positive or negative change in capacity

  ; -------------- yearling mortality
  percent-pup-affected            ; percentage of pups affected with energy change
  SA_change

  ; --------------- fecundity
  percent-mum-affected
  fec_change
  fecundity_changed_now?

  ;---------------- climate variation
  climate-change
  clim-var
  ;=============================================================================================
]
;; --------------------------------------patches-own-------------------------------------------------------------
;patches-own[
;    ;; for now this isn't actually used... competition has been implemented through K1 - K2
;  X        ; # / cm^2, prey density
;  d_X      ; change of prey density in time
;]
;; ---------------------------------------------------------------------------------------------------
turtles-own[
;  is_male?    ; 1 / 0 -- implement when males are included (for now running model with only females)
  age         ; age of seals (in days)
  stage       ; foetus, pup, juvenile or mature
  status      ; fasting, foraging or mother-dependent

  ;; - - - - - - - - - - - - - - - STATE VARIABLES - - - - - - - - - - - - - - - - - - - - - - - - - -
  L           ; cm, structural length
  Lmax        ; cm, maximum structural length
  dL          ; change of structural length in time

  U_H         ; t L^2, scaled maturity
  dU_H        ; change of scaled maturity in time

  U_E         ; t L^2, scaled reserves
  dU_E        ; change of scaled reserves in time
  e_scaled    ; - , scaled reserves per unit of structure
  l_scaled    ; - , scaled structural length

  U_R         ; t L^2, scaled energy in reproduction buffer (not standard DEB)
  dU_R        ; change of energy in reproduction buffer (reproduction rate)

  ;;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  iv          ; to include individual variability for DEB-IBM parameters on a normal distribution
                 ; with a mean on the input paramater and a coefficent of variation equal to the cv

  g           ; - , energy investment ratio

  ;; - - - - - - - - - - - - - - - FLUXES (used by several submodels) - - - - - - - - - - - - - - - -
  S_A         ; assimilation flux
  S_C         ; mobilisation flux

  ;; - - - - - - - - - - - - - - - STANDARD DEB PARAMETERS - - - - - - - - - - - - - - - - - - - - -
  U_H^b       ; t L^2, scaled maturity at birth
  U_H^x       ; t L^2, scaled maturity at weaning
  U_H^p       ; t L^2, scaled maturity at puberty

  ;; - - - - - - - - - - - - - - - PREY DYNAMICS (only relevant if prey-dynamics not constant)
  f           ; - , scaled functional response (food availability) (value between 0 and 1; where 1 = max feeding)
  ;; for now the following isn't actually used... competition has been implemented below
;  K           ; # / cm^2, (half) saturation coefficient
;  J_XAm_rate  ; # / (cm^2 t), surface-area-specific maximum ingestion rate

  ;; - - - - - - - - - - - - - - - AGEING -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  r_rate          ; growth rate

  q_accel         ; - , ageing acceleration
  dq_accel        ; change of ageing acceleration in time
  h_rate          ; - , hazard rate
  dh_rate         ; change of hazard rate in time

  ;; - - - - - - - - REPRODUCTION / MOULTING
  my_ID                 ; collect ID of self
  offspring_ID          ; collect ID of offspring (-1 if none)
  mother_ID             ; collect ID of mother (-1 if none)
;  partner_ID           ; collect ID of potential partner for mating (-1 if none) ---- as no males yet
  cumulative_UE          ; initial cumulative energy required for allocation to pup
  cum_UE                 ; final cumulative energy required for allocation to pup ---- proportional to mother's size

  is_pregnant?          ; 1 / 0
  resting?              ; 1 / 0
  moulting?             ; 1 / 0
  haul-out?             ; 1 / 0
  breeding?             ; 1 / 0
  impregnated?          ; 1 / 0

  had-pup?              ; 1 / 0
  lactating?            ; 1 / 0

  tried-mating          ; count for trying to get pregnant
  time-since-mating     ; days for calculation to start conception
  time-since-suckling   ; days for calculation to weaning
  days-moulting         ; days for calculation of getting back to foraging
  days-foraging         ; days for calculation for resting time (females and young ones)
  days-resting          ; for resting calculation (females and young ones)

  puppy                 ; for plotting
  total-puppies         ; for death output

  die-now               ; 0 / 1 - for death in this timestep
  died-of-?    ; 0 / 1
  final-age             ; recording before death
  final-pups            ; recording before death
  breeding-age          ; recording before death
  reprod-period         ; recording before death
  fecundity             ; recording before death

  weaning_age           ; final age at weaning
  days-dead             ; for collecting age and other final data

  xx                    ; for testing for pup-affected//mum-affected
  pup-affected          ; 0 / 1 -- for affected with less energy intake
  mum-affected

  track-life            ; for tracking life span of turtles (life table)

]

;; ========================== SETUP PROCEDURE: SETTING INITIAL CONDITIONS ============================

to setup
  ca
  set my-seed new-seed                           ;; to make the model reproducible but keep the seed used random
;  set my-seed 1548502745                         ;; manually set seed
  output-write word "Generated seed: " my-seed   ;; print it in the output box on the interface
  random-seed my-seed
  set model_just_started true                    ;; for profiling

  reset-ticks
  set max-ticks max-years * 360
  set carrying_capacity max-pop-1

  ;------------------------------------ scenarios
  set k1-k2 max-pop-1 - max-pop-2                             ; the difference between k1 and k2
  set rate-of-decline k1-k2 / time-for-decline                ; the number of individuals that the population declines by per year
  set percent-pup-affected 0
  set fecundity_changed_now? 0

  set pup-mortality 0                                           ;; to set for start (change in do-time-management)
  if (pup-mort = "minimum") [ set pup-mortality min-pup-mort ]
  if (pup-mort = "mean")    [ set pup-mortality mean-pup-mort ] ;; mean based on McMahon et al 2000, 2003
  if (pup-mort = "maximum") [ set pup-mortality max-pup-mort ]

  ;; - - - - - - - - - - - - - - - time management
  set day-of-year 1                ;; start on Jan 1
  set day-of-month 1
  set month-of-year 1
  set year 0

  ;; - - - - - - - - stages for IBM
  set foetus 0       ;; U_H < U_H^b
  set pup 1          ;; U_H > U_H^b & U_H < U_H^x
  set juvenile 2     ;; U_H > U_H^x & U_H < U_H^p
  set mature 3       ;; U_H > U_H^p

  ;; - - - - - - - - status for IBM
  set mother-dependent 0  ;; foetus/pup
  set fasting 1
  set foraging 2

  ;; - - - - - - - - - - - - - - - create seal population as on interface
  crt start_population
  ask turtles [ individual-variability ] ;; setting the initial settings of seals

  set climate-change 0
  set clim-var 0

;  profiler:reset     ;; clear the profile data
end 
;; ========================== SUBMODELS ==============================================================
;; -------------------------- INITIAL SETTINGS--------------------------------------------------------

to individual-variability
  ;; following Martin et al's model -- to apply individual variability - cv set to 0.02
  ;; iv applied to DEB and IBM parameters.
  set iv e ^ (random-normal 0 cv)

  ;; calculate competition based on the current number of individuals
  set competition (1 - f_scaled) * ( 1 - (start_population / (2 * carrying_capacity - start_population)))
  ;; to set effective food availability (f) from initial food availability (f_scaled)
  set f (f_scaled + competition) * iv
  if f > 1 [set f 1]

  set g g_init * iv

  ;; set scaled maturities
  set U_H^b (E_H^b_init / p_am) ;* iv        ;; scaled maturity at birth (cm^2 d)
  set U_H^x (E_H^x_init / p_am) ;* iv        ;; scaled maturity at weaning (cm^2 d)
  set U_H^p (E_H^p_init / p_am) ;* iv        ;; scaled maturity at puberty (cm^2 d)

  ;; set scaled length
  set Lmax L_w^m * shape_factor  ;; max struct length
  ;; so that L > L_w^x and L < L_w^m (size between weaner and full adult)
  set L ((L_w^x + random (L_w^m - L_w^x)) * shape_factor)

  ;; set initial reserve
  ;; these initial settings are balanced out by the second generation seals
  set l_scaled L / Lmax                     ;; scaled length -- resets in calc-dL
  set U_E ((L ^ 3) * (l_scaled / v_rate))   ;; as e < l means starvation model is implemented and e = v_rate * U_E / L ^ 3
  set U_H U_E / 2.87                        ;; as U_H / U_E = 2.145 @ x, 3.585 @ p. -- not starting with pups - thus excluded from average.


  set e_scaled (v_rate * (U_E / (L ^ 3)))   ;; resets every time step in calc-dU_E
  ;; sanity check
  if U_H < U_H^x  [ set U_H U_H^x ]         ;; make sure every individual to start with has enenough stored energy

  ;; set stages -- the model will only start with juveniles or adults (foetus/pup initiated from mother only)
  ifelse U_H < U_H^p
  [ set stage juvenile    ;; 2
    set age round (23 + random (1059 - 23))
    set h_rate random-float 1.0e-19
    set U_R U_H
  ]
  [ set stage mature      ;; 3
    set age round (1059 + random (5400 - 1059))
    set h_rate random-float 1.0e-09
    set U_R U_H
  ]

  ;; set reproduction and moutling settings -- for now no pregnancy in start of model
  set status foraging
  set my_ID who              ; collect own ID
  set offspring_ID -1        ; collect ID of offspring (-1 when no offspring)
  set mother_ID -1           ; collect ID of mother (-1 when no longer relying on mother)

  ;; calcualtion for the cumulative energy required to produce a healthy offspring that reaches the required size at birth (L_w^b)
  ;; -- based on the modified calculation in comments on the DEB book (p. 38).
  ;; This calculation is adjusted here to suit our species by requiring only 70%
  ;; of the mother's UR levels for the lower limit of energy needs. (Used as the mean in simulations.)

  set cumulative_UE (((L_w^b * shape_factor) ^ 3) * ((f_scaled + g) / v_rate) * (1 + (3 / 4) * ((L_w^b / L_w^m) / f_scaled)))
  set cum_UE cumulative_UE * l_scaled * 0.2

  set impregnated? 0         ; 1 / 0 conception
  set is_pregnant? 0         ; 1 / 0 implantation
  set breeding? 0            ; 1 / 0 start in september
  set had-pup? 0             ; 1 / 0
  set moulting? 0            ; 1 / 0 once a year
  set lactating? 0           ; 1 / 0 after birth
  set resting? 0             ; 1 / 0 after foraging for extensive period
  set haul-out? 0            ; 1 / 0 mid winter haul out for juvenile seals

  set tried-mating 0         ; count for trying to mate
  set time-since-mating 0    ; days for calculation to start conception
  set time-since-suckling 0  ; days for calculation to weaning
  set days-moulting 0        ; days for calculation of getting back to foraging
  set days-foraging 0        ; days for calculation for resting time (females and young ones)
  set days-resting 0         ; for resting calculation (females and young ones)

  set puppy 0                ; for plotting
  set total-puppies 0        ; for death output for individuals

  set die-now 0              ; recording before death
  set died-of-? 0
  set final-age 0            ; recording before death
  set final-pups -1          ; recording before death
  set breeding-age 0         ; recording before death
  set reprod-period 0        ; recording before death
  set fecundity -1           ; recording before death

  set weaning_age 0          ; track age at weaning
  set days-dead 0            ; to track dead individual parameters

  set xx 100                 ; so that no-one starts off with being affected

  ;----------------------------------------- yearling mortality and fecundity scenarios
  set pup-affected 0         ; 0 / 1
  set xx random-float 100    ; set as random value between 0 and 100 (%)
  if xx < percent-pup-affected [ set pup-affected 1 ]

  set mum-affected 0         ; 0 / 1
  set xx random-float 100
  if xx < percent-mum-affected [ set mum-affected 1 ]

  set track-life 0          ; set in time-management
end 

;; ========================== GO PROCEDURE: RUNNING THE MODEL ========================================

to go
  tick
  ;; start ticks at beginning of procedure so that the whole thing happens on the correct day
  ;; see Railsback and Grimm (2011) for more detaied information on why this should be first.

;  if ((model_just_started = true) and (use_profiler? = true)) [
;    profiler:start                        ;; start profiling
;    set model_just_started false          ;; reset
;  ]

  ;; for days of month, year etc. also adds day to the age of the turtles
  ;; also includes change for scenario implementations
  do-time-management

  ask turtles with [ die-now = 0 ] [ ;reset change each day
    set dL 0
    set dU_E 0
    set dU_H 0
    set dU_R 0
    set cum_UE cumulative_UE * l_scaled * 0.2
  ]

  if fecundity_changed_now? = 1 [
    ask turtles [
      if mum-affected > 0 [
        if increase? = "yes" [ set cum_UE cum_UE * (1 + fec_change) ]
        if increase? = "no"  [ set cum_UE cum_UE * (1 - fec_change) ]
      ]
    ]
  ]

  set population (count turtles) ;with [stage > 1]) ; not mother dependent...
  ask turtles [
    ifelse population < 1.9 * carrying_capacity
    [ set competition (1 - f_scaled) * ( 1 - (population / (2 * carrying_capacity - population)))
      set f (f_scaled + competition) * iv
      if climate-change = 1 [ set f f * clim-var ]
    ]
    [ set competition (1 - f_scaled) * ( 1 - (population / (carrying_capacity / 10) ) )
      set f (f_scaled + competition) * iv
      if climate-change = 1 [ set f f * clim-var ]
    ]
    if f > 1 [ set f 1 ]
  ]

;  if climate-change = 1 [
;    ask turtles [
;      set f (f_scaled + competition) * iv * clim-var
;      if f > 1 [set f 1]
;    ]
;  ]

  ;; go through daily life of seals - only for independent seals: status = 0 for mother-dependent seals (foetus/pup)
  ;; U_E, U_H, and L calculations only applied to independent seals - for pups this is implemented
  ;; in update-offspring-energy. Individuals calculate the change in their state var based on the current conditions
  ask turtles with [ die-now = 0 and status > 0 ][ check-status ]
  ask turtles with [ die-now = 0 and status > 0 ][ check-breeding ]

  ask turtles with [ die-now = 0 and status > 0 ][ calc-dU_E ]   ; calculate change in scaled reserve
  ask turtles with [die-now = 0 and status > 0]  [ calc-dU_H-R ] ; calc change in scaled maturity and reproductive buffer

  ask turtles with [ die-now = 0 and status > 0 ][ calc-dL ]     ; calculate change in structural length

  ;; aging is applied to all seals from the day that they were born - ageing here isn't a daily increase in age,
  ;; but deterioration of structure (acceleration and hazard rates)
  ask turtles with [ die-now = 0 and stage > 0 ][ calc-ageing ] ; apply aging to turtles

  ;; apply updated variables - must be implemented last as dL includes a stavation sub model which could change
  ;; the values in U_H, U_R and dL depending on activities.
  ask turtles with [ die-now = 0 and status > 0 ][ update ]

  ;; once adults and juveniles have updated their settings apply applicable changes to offspring
  ask turtles with [ die-now = 0 and is_pregnant? = 1 ][ update-offspring-energy ]
  ask turtles with [ die-now = 0 and stage = 1 ][ pup-feeding ]

  ;; apply mortality (ageing accelerations; h_rate from calc-ageing)
  ask turtles with [ die-now = 0 and stage > 0 ][ apply-ageing ]

  ;; end model if no seals or no time left.
  if count turtles <= 20 or ticks = max-ticks [
;    if (use_profiler? = true) [
;      profiler:stop                                               ;; stop profiling
;      print profiler:report                                       ;; view the results
;    ]
    if count turtles <= 20 [ print " stopped as no seals left" ]
    if ticks = max-ticks [ print " stopped as max time reached" ]
    stop                                                          ;; stop running the model
  ]
  ;; sanity check
  ;; to aviod completely crazy populations - stop the model when too many seals
  if count turtles > 50 * start_population [
;    if (use_profiler? = true) [
;      profiler:stop                                               ;; stop profiling
;      print profiler:report                                       ;; view the results
;    ]
    print "stopped as too many seals"
    stop
  ]
  ;; then the plots are updated - as when no turtles left, the plots show errors.
                          ;;as per below
  ask turtles with [die-now = 1 and days-dead = 0][
    set final-age age / 360
    set final-pups total-puppies
    set reprod-period round (final-age - breeding-age)
    if reprod-period = 0 [ set reprod-period 1]
    set fecundity final-pups / round reprod-period
    set days-dead 1
  ]

;  ask turtles [if stage > 1 and f > 1 [set f 1]]

  do-plots
  ask turtles with [ die-now = 1 and days-dead > 1 ][ die ] ;; plots have been updated - so seals can now die (and be removed from the model)
end 

;;===================================================================================================
;; ----------------- RESERVE DYNAMICS ----------------------------------------------------------------
;; change in reserves: determined by the difference between assimilation (feeding; S_A) and mobilization
;; (energy use; S_C) fluxes. when food-dynamics are constant f = the value of f_scaled set in the user interface

to calc-dU_E
;  set f (f_scaled + competition) * iv;; competition calculated at start of timestep
;  if f > 1 [set f 1]                                 ;; cannot have access to more than 100% food

  set e_scaled (v_rate * (U_E / (L ^ 3)))  ;; calculate the reserve energy --> usable energy saved over time,
                                           ;;this changes as U_E and L change over time (de/dt = (f-e) v_rate / L)

  ;; calculate S_C depending on foraging or fasting is implemented
  if status = foraging [ set S_C (L ^ 2 * ((g * e_scaled) / (g + e_scaled)) * (1 + ((L * k_M_rate) / v_rate ))) ]
  if status = fasting [
    set f 0 ; fasting, thus no intake
    set S_C (((k_m_rate * g * kap) / v_rate) * (L ^ 3))
  ]

  if age < 360 and status = foraging [
    set xx random-float 100
    if xx < 80 [ set f 0.2 * f ]
  ]

  ifelse is_pregnant? = 1 and [stage] of turtle offspring_ID = 0  ;; != nobody has been removed from here.
  [ set S_A (f * ((L ^ 2) + ([L ^ 2] of turtle offspring_ID))) ]  ;; upregulation of intake during pregnancy
  [ set S_A (f * (L ^ 2)) ]

  if age < (age_for_pup_energy_change * 360) [
    if pup-affected = 1 [ set S_A S_A * (1 - SA_change) ]
  ]

  set dU_E (S_A - S_C)
end 

to calc-dU_H-R ;;-----------------------------------------------------------
  ifelse U_H > U_H^p
;  ifelse stage = mature
  [ set dU_R ((1 - kap) * S_C - k_J_rate * U_H^p)  ; adult
    set dU_H 0
  ]; -----------------------------------------------------
  [ set dU_H ((1 - kap) * S_C - k_J_rate * U_H)    ; juvenile
    set dU_R 0
  ]
end 


;; ----------------- DYNAMICS OF STRUCTURAL LENGTH----------------------------------------------------
;; the following procedure calculates change in structural length, if growth is negative the individual
;; does not have enough energy to pay somatic maintenance and the starvation submodel is run where growth
;; is set to 0 and individuals divert enough energy from development (for juveniles) or reproduction
;; (for adults) to pay maintenance costs

to calc-dL

  ;; non starvation conditions:
  if L < Lmax [
    set dL ((1 / 3) * (((v_rate /(g * L ^ 2)) * S_C) - k_M_rate * L))
    if dL < 0 [ set dL 0 ]
  ]

  ;; if scaled energy per unit of structure is less than the scaled length --> use starvation strategy
  set l_scaled L / Lmax    ;; scaled length
  if e_scaled < l_scaled [ ;; modified following section 4.2.3 of DEB-IBM user manual (Martin et al)
    set dL 0               ;; cannot have negative growth so set growth to zero
    set S_C ((k_m_rate * g * kap)/ v_rate) * (L ^ 3)

    ;; check to see if the turtle is mature to see where remainig energy is diverted to
;    ifelse U_H > U_H^p
    ifelse stage = mature ;---------------------------------------------------------------------------------
    [ set dU_R (1 - kap) * S_C - k_J_rate * U_H^p - kap * L ^ 2 * (l_scaled - e_scaled) ;; mature (dU_R)
      set dU_H 0
    ]
    [ set dU_H (1 - kap) * S_C - k_J_rate * U_H^p - kap * L ^ 2 * (l_scaled - e_scaled)  ;; immature (dU_H)
      set dU_R 0
    ]
    ;; adjusted reserves as S_C changed
    set dU_E (S_A - S_C)
    if e_scaled <= 0 [ type turtle who type " with stage = " type stage print " died as e < 0 " ;; sanity check
      if offspring_ID > -1 [
        ask turtle offspring_ID [
          if stage = 0 [
            set die-now 1
            set died-of-? 2
          ]
          if stage = 1 [
            set mother_ID -1
            set stage 2 ;immediate juvenile as it needs to fend for itself
            set status 1 ; fasting as left on land
            set resting? 0
            set moulting? 1 ; so that pup stays on land for ~50 days
            set f (f_scaled + competition) * iv
            if f > 1 [set f 1]
            ;; as offspring update happens after adults - go through this now -
            ;; but in this case only the check-status as energy already updated in update-offspring-energy
            check-status
            type turtle who type " juv as mum died. Age: " print [age] of turtle who
          ]
        ]
      ]
      set die-now 1
      set died-of-? 1
    ]
  ]
end 
;; ----------------- AGEING --------------------------------------------------------------------------
;; the following procedure calculates the change in damage enducing compounds of an individual

to calc-ageing
  ;; h_a not dependent on food density - thus affects the max life span.
  ;; s_G dependent on food density
  ;; h_a = max life span of ~25 years.
  ;; s_G from Matlab code at T, h_a manually selected based on max age.

  ;; calculates the rate of growth, scaled acceleration change & scaled hazard rate
  set r_rate (3 / L) * dL
  set dq_accel (((q_accel * (L ^ 3 / Lmax ^ 3) * s_G) + h_a ) * e_scaled * ((v_rate / L) - r_rate) - (r_rate * q_accel))
  set dh_rate (q_accel - (r_rate * h_rate))

  ; apply effects
  set q_accel q_accel + dq_accel
  set h_rate h_rate + dh_rate
end 

to apply-ageing
  ;; calculate chance of death
  let mortality-chance random-float (mortality-float * iv)
  if iv < 0.95 [ set mortality-chance mortality-chance / 10]

  if mortality-chance < h_rate [
    if offspring_ID > -1 and turtle offspring_ID != nobody [
      ; print " mother died of old age" ;; sanity check
      ask turtle offspring_ID [
        if stage = 0 [
          set die-now 1
          set died-of-? 2
        ]
        if stage = 1 [
          set mother_ID -1
          set stage 2             ;; immediate juvenile as it needs to fend for itself
          set status 1            ;; fasting as left on land
          set resting? 0
          set moulting? 1         ;; so that pup stays on land for ~50 days
          set f (f_scaled + competition) * iv
          if f > 1 [set f 1]
          ;; as offspring update happens after adults - go through this now -
          ;; but here only the check-status as energy already updated in update-offspring-energy
          check-status
        ]
      ]
    ]
    set die-now 1
    set died-of-? 3
  ]

  ;; apply pup mortality to pups within their first year of life
  ;; calculation for mortality: (1/360) * (1-0.716) = 7.89e-4
  ;; based on McMahon et al 2000, 2003 for a
  ;; survival rate of 71.6% as pups weigh (over) the required amount
  if pup_mort? [
  if age < 360 [                              ;; yearling
    let chance random-float 1
    if chance < pup-mortality [
      if offspring_ID > -1 [
        print "error - yearling has a pup?"
        ask turtle offspring_ID [
            set die-now 1
            set died-of-? 4
          ]
        ]
        if mother_ID > -1 [                   ;; advice mother of death of pup if applicable
          ask turtle mother_ID [
            set offspring_ID -1
            set lactating? 0
            set is_pregnant? 0
            set impregnated? 0
            set time-since-suckling 0
            ; not changing breeding or mating status as it could be that the mother has been impregnated again
          ]
        ]
        set die-now 1
        set died-of-? 5
      ]
    ]
  ]
end 

; ============================ UPDATE ===============================================================

to update

  if (dU_R <= 0 and dU_H > 0) [ ;; somatic growth till age 6, start breeding at age 3 (Desprez et al 2014)
    if age > 360 [              ;; but should really save energy for self in first year...
      set dU_R (dU_H * 0.6)     ; 60% to dU_R
      set dU_H (dU_H - dU_R)
    ]
  ]

  set U_E U_E + dU_E
  set U_H U_H + dU_H
  if stage = juvenile and U_H > U_H^p [ ;; if they reach puberty levels - move extras into U_R
    set dU_R dU_R + (U_H - U_H^p)
    set U_H U_H^p
  ]

  set U_R U_R + dU_R
  set L L + dL
  if (breeding-age < 1 and U_R > cum_UE) [ set breeding-age age / 360 ]


  if stage = 2 and age < 360 [
    if U_H < 0.92 * U_H^x [
      set die-now 1
      set died-of-? 1
    ]
  ]

  ;; kill off seals with energy levels too low
  if U_R < 0 or U_H < 0 or e_scaled < 0 [
    if mother_ID > -1 [ print "energy levels too low to continue pregancy"
      ask turtle mother_ID [
        set offspring_ID -1
        set breeding? 0
        set lactating? 0
        set is_pregnant? 0
        set impregnated? 0

        set tried-mating 0
        set time-since-mating 0
        set time-since-suckling 0
      ]
    ]
    if offspring_ID > -1 [
      ifelse [stage] of turtle offspring_ID = 0
      [ ask turtle offspring_ID [        ;; foetus
          set die-now 1
          set died-of-? 2
        ]
      ]

      [ ask turtle offspring_ID [        ;; pup
          set mother_ID -1
          set stage 2           ;;immediate juvenile as it needs to fend for itself
          set status 1          ;; fasting as left on land
          set resting? 0
          set moulting? 1       ;; so that pup stays on land for ~50 days
          set f (f_scaled + competition) * iv
          if f > 1 [set f 1]
          ;; as offspring update happens after adults - go through this now
          check-status
          calc-dU_E
          calc-dU_H-R
          calc-dL
          calc-ageing
          update
        ]
      ]
    ]
    set die-now 1
    set died-of-? 1
  ]
  ;; update stages (birth and weaning to juvenile to be done in different section later)
  if U_H >= U_H^p and stage = juvenile [ set stage mature ]
end 
;=======================================================================================

to check-status
  let max-moulting-time 0 ;; max moulting time set following Carrick et al 1962
  let max-resting-time 0  ;; max resting  time set following Carrick et al 1962
  let max-foraging-time 0 ;; max foraging time set following Carrick et al 1962

  ;; setting the correct limits for each stage - applies only to juveniles and adults
  ;; iv (individual variability) applied to durations
  if stage = 2 [
    ;; Carrick et al (1962) states that moulting for juveniles could be up to 4 weeks.
    set max-moulting-time moult-duration-juv * iv
    set max-resting-time resting-duration-juv * iv
    set max-foraging-time forage-duration-adult * iv
    ;; However, juveniles cannot stay out for too long in their first year,
    ;; and newly weaned pups need to stay on land for ~5-6 weeks (here 50 days).
    ;; pup resting after weaning period is included on the interface.
    if age < 360 [
      set max-moulting-time moult-duration-pup * iv
      set max-foraging-time forage-duration-juv * iv
    ]
  ]
  ;; Female southern elephant seals come on land twice per year. Once for breeding where they come
  ;; on land, give birth approx. 8 days later, then have a suckling pup for 23 days after which
  ;; they return to the water. During this time the seals do not enter the water and loose approx. 35%
  ;; of their body weight over three weeks (Desprez et al 2014). The second time that the seals come on
  ;; land is approx. 70 days after they have returned to the water following breeding (Hindell et al 1994)
  ;; (98 days acording to Carrick et al 1962) for moulting. The adult female seals stay on land for
  ;; approx. 30 days, however, they might enter the water for a quick feed off shore (Hindell et al 1994).
  ;; In this model this is ignored, for simplicity.
  if stage = 3 [
    ;; Adult moulting 30 - 50 days for females and males, respectively (Hindell et al 1994)
    ;; foraging 70 days after breeding season for moulting by breeding adults - (Hindell et al 1994)
    set max-moulting-time moult-duration-adult * iv
    set max-resting-time resting-duration-adult * iv
    set max-foraging-time forage-duration-adult * iv
  ]

  ;;========================================= FORAGING ================================================
  ;;-------------------------------------------------------
  ;; Do this before the setting of fasting periods as otherwise they miss a day.The options for
  ;; counting the days that they are fasting are either time-since-breeding-start,
  ;; time-after-breeding-end or days-moulting.
  ;; Applied to either foraging or fasting status.
  if status = foraging [
    set days-foraging days-foraging + 1
    if days-foraging > max-foraging-time [
      set days-foraging 0
      set status fasting
      set resting? 1
    ]
  ]

  ;;========================================== FASTING =================================================
  ;; Keeping track of days out at sea, particularly for young seals.
  ;; If they are out for too long, they'll come back onto land for a short while to rest.
  ;; fasting for both moulting and resting - resting includes the period where mum gives birth to pup.
  ;; lactating duration dealt with in update-offspring-energy

  if status = fasting [
    if moulting? = 1 [ ;;----------------------- moulting
      set days-moulting days-moulting + 1
      if days-moulting > max-moulting-time [
        set days-moulting 0
        set moulting? 0
        set resting? 0
        set status foraging
        set days-foraging 0
      ]
    ]

    if resting? = 1 [ ;;------------------------ resting
      set days-resting days-resting + 1
      if days-resting > max-resting-time [
        set days-resting 0
        set resting? 0
        set status foraging
        set days-foraging 0
      ]
    ]

    if haul-out? = 1 [ ;;----------------------- mid winter haul out -- Hindell & Burton 1988
      set days-resting days-resting + 1
      if days-resting > (mwho-juv * iv) [ ;; as set on interface
        set days-resting 0
        set haul-out? 0
        set status foraging
        set days-foraging 0
      ]
    ]
  ]
  ;; --------- implement annual moulting for females
  if month-of-year = 12 [
    if day-of-month > 1 and day-of-month < 7 [
      if age > 360 [                                  ;;as newly weaned pups have already moulted by now
        set status fasting
        set days-foraging 0
        set moulting? 1
        set days-moulting 0
      ]
    ]
  ]

  ;; Juveniles need to have a mid-winter haul out - which is implemented here. Duration is set on the interface
  ;; following Carrick et al 1962. This haul out could be anywhere from April to July, but for now set for
  ;; only July.
  ;;-------------implement midwinter haulout
  if month-of-year = 7 [                  ;; july
    if day-of-month > 5 and day-of-month < 15 [
      if stage = 2 [                        ;; juvenile
        if status  = foraging [             ;; foraging
          set status fasting                ;; now fasting during haul out
          set haul-out? 1
          set resting? 0
          set moulting? 0
          set days-resting 0
        ]
      ]
    ]
  ]
end 

;;====================================== BREEDING ================================================

to check-breeding
  ;; start in reverse chronological order so not everything happens in the same time step!
  ;; breeding is set to 1 in september - which is when the females come onto land  to get impregnated.
  ;; This is only applied if the seal wasn't already impregnated while suckling it's previous pup.
  ;; As ses technically only breed every two years the breeding like this should be applied if
  ;; breeding is unsuccesful during weaning of previous pup.
  if breeding? = 1 [
    if offspring_ID > -1 and turtle offspring_ID != nobody [ ;;-------------------------------
      if ([stage] of turtle offspring_ID = 0)
      and [age] of turtle offspring_ID >= breeding-duration
      [
        give-birth
        set time-since-mating 0
      ]
      ;; preparation for birth on land - 8 days prior following Carrick et al // Deprez et al?
      if time-since-mating = (breeding-duration + diapause - 8)[
        set status fasting
        set resting? 1
        set days-foraging 0
        set days-resting 0
      ]
    ]
    ;; time for pregnancy to start - foetus created following Laws 1984 in Knox 2007
    if time-since-mating = diapause and is_pregnant? = 0 [
      ifelse offspring_ID > -1 [
        ifelse turtle offspring_ID = nobody
        [ set offspring_ID 0
          set lactating? 0
          set is_pregnant? 0
          set impregnated? 0
          set time-since-suckling 0
          set status foraging
          set days-foraging 0

          set puppy 0
        ]
        [ if [age] of turtle offspring_ID > 90 [
          ask turtle offspring_ID [
            set f (f_scaled + competition) * iv
            if f > 1 [set f 1]
            set stage juvenile
            set status fasting
            set moulting? 1          ;; so that pup stays on land for 50 days
            set days-moulting 0
            set mother_ID -1
          ]
          set offspring_ID -1
          set lactating? 0
          set is_pregnant? 0
          set impregnated? 0
          set time-since-suckling 0
          set status foraging
          set days-foraging 0

          set puppy 0
          ]
        ]
      ]
      [ do-pregnancy ]
    ]

    if impregnated? = 1 or is_pregnant? = 1[                         ; add a day to the pregnancy
      set time-since-mating time-since-mating + 1
    ]
  ]

  ;;seals not breeding yet - but it's the right time so could be:
  if lactating? = 0 [                            ;; not lactating
    if impregnated? = 0  and is_pregnant? = 0 [  ;; not pregnant from this season
      if tried-mating = 0[                       ;; hasn't tried to get pregnant yet
        if month-of-year = 10 [                  ;; it is the right month
          if day-of-month < 5 [                  ;; first week of the month
            if status = foraging [               ;; wasn't on land already
              set status fasting                 ;; adjusting settings
              set resting? 1
              set days-foraging 0
              set days-resting 0
            ]
          ]
        ]
      ]
    ]
  ]

  if impregnated? = 0  and is_pregnant? = 0 and status = fasting [ ;; get pregnant
    if month-of-year = 10 [
      if tried-mating < 7 [
        get-impregnated
      ]
    ]
  ]

  ;; Included a cycle where lactating mothers breed with invisible males while they're on land with a pup.
  ;; Mothers come on land about 8 days before giving birth. They then give birth and stay on land for
  ;; 23 days with the pups while they suckle. During this time the females are getting ready for mating
  ;; again - and although they generally (at Macca) only breed every other year, it'd be good to get
  ;; this going - during that time. So after about 19 days the female can get impregnated again.
  ;; This leaves 4 days for trying to get pregnant - if it doesn't work, they won't breed this year.
  ;; Following Laws 1984, Hindell et al 1994, Hindell & Slip 1997.
  if lactating? = 1 [
    if impregnated? = 0 and is_pregnant? = 0 [
      if month-of-year = 10 [
        if (time-since-suckling >= 19) [ get-impregnated ]
      ]
    ]
  ]

  if month-of-year = 9 [                                             ;; check month for start breeding
    if breeding? = 0 [                                               ;; check if not yet breeding
      if is_pregnant? = 0 and impregnated? = 0 [ set breeding? 1 ]   ;; check that not already pregnant or impregnated
    ]
  ]

  if month-of-year = 11 and day-of-month = 1 [                       ;; failed breeders - going back to foraging
    if breeding? = 1 [
      if is_pregnant? = 0 and impregnated? = 0 [
        set breeding? 0
        set status foraging
        set resting? 0
        set time-since-mating 0
        set days-foraging 0
        set tried-mating 0
      ]
    ]
  ]
end 
;;================================================

to get-impregnated
  ;; mating success following Fedal et al 1996, Deprez et al 2014
  ;; 3 yo @ 0.98, 4 yo @ 0.21, 5 yo @ 0.15, 6+ @ 0.75
  ;; no breeding senecense for seals - following Hindell and Little 1988
  let prob_mating_fail 0
  set tried-mating tried-mating + 1

  ifelse age < (6 * 360)
  [ ifelse age < (5 * 360)
    [ ifelse age < (4 * 360)
      [ set prob_mating_fail prob_mate_fail_3yo ]  ;; < 4 yo
      [ set prob_mating_fail prob_mate_fail_4yo ]  ;; 4-5 yo
    ]
    [ set prob_mating_fail prob_mate_fail_5yo ]    ;; 5-6 yo
  ]
  [ set prob_mating_fail prob_mate_fail_6plus ]    ;; > 6 yo

  let cum-chance random-float 1                      ;; value 0 - 1
  if cum-chance > prob_mating_fail [
    set impregnated? 1
    set time-since-mating 1
    set tried-mating 0
    set breeding? 1
  ]
end 
;;================================================

to do-pregnancy
  set is_pregnant? 1
  ;; NOTE:
        ;; original model for daphina spp. sets UR to zero after reproduction/hatching of offspring,
        ;; however, this CANNOT be done for seals as during reproductive time the seals need to fast
        ;; and take energy from UR to support themselves. Hence the check of UR levels before reproduction,
        ;; as personal survival is more important to the mothers than giving birth/producing offspring.

        ;; from section 7.7 in the DEB book and the associated comments we know the calculation for
        ;; cummulative energy invested in the pups. calculations are available for both foetal and
        ;; lactation periods. Thus we can make sure that a seal only goes ahead with the pregnancy if
        ;; they have enough energy to support the foetus through to birth.

  if U_R < cum_UE [
    set is_pregnant? 0
    set impregnated? 0
    set time-since-mating 0
    set status foraging
    set days-foraging 1
    set breeding? 0
    set puppy 0
  ]

  ;; creating a new turtle/individual......
  if is_pregnant? = 1 [
    hatch 1 [
      let hatch_ID who
      set mother_ID my_ID
      ask turtle mother_ID [ set offspring_ID hatch_ID ]        ;; connect mother and young
      set offspring_ID -1
      set my_ID who

      set f (f_scaled + competition) * iv
      if f > 1 [set f 1]

      set age 1
      set stage foetus
      set status mother-dependent

      set-foetus-variables
    ]
  ]
end 
;;====================================

to give-birth
  set breeding? 0
  set is_pregnant? 0
  set impregnated? 0
  set time-since-mating 0
  set resting? 1
  set lactating? 1
  set time-since-suckling 0
  set status fasting

  if turtle offspring_ID = nobody [
    set puppy 0
    set total-puppies total-puppies - 1
    set resting? 0
    set lactating? 0
    set status foraging
    set breeding? 0
    set pups-born pups-born - 1
  ]
  if turtle offspring_ID != nobody [
    ask turtle offspring_ID [
      set h_rate random-float 1.0e-19
      set stage pup
      set age 1
      set weaning_age 0

      set xx random-float 100
      if xx < percent-pup-affected [ set pup-affected 1 ]
    ]
    set puppy 1
    set total-puppies total-puppies + 1
    set pups-born pups-born + 1
  ]
end 
;;===============================================================================================================

to update-offspring-energy
  if offspring_ID > 0 [
    ;; separating stages of offspring between foetus and pups
    if [stage] of turtle offspring_ID = foetus [
      ask turtle offspring_ID [

        ;; following the additonal DEB information - "Comments on DEB book" - page 152, 501

        ;; adjusted the values here that aren't directly deived from Matlab - this makes a difference and allows
        ;; the seals grow at an accurate rate. Calculations for length following the fast foetal development should
        ;; be L(t) = t * v_rate / 3, but then the seals take 900-odd days till birth. Adjusting to match 217 days the
        ;; calculation becomes 25.85 = 217 * v_rate / 0.545 where 25.85 = L_w^b * shape-factor
        ;; thus:
        let r_B v_rate / (0.545 * Lmax)     ;; adjusted growth calculation for fast foetal development - birth @ U_H^b

        set dL r_B * Lmax
        if pup-affected = 1 [ set dL dL * (1 - (SA_change / 2)) ]
        set L L + dL

        ;; The following calcualtions approximately follow the DEB book (p. 283). Reserve flux from mother
        ;; to foetus bypasses assimilatory system of the foetus - thus dU_E = S_A
        set dU_E [e_scaled] of turtle mother_ID * L ^ 2 * kap_F                     ;; adjusted kap_F and modified formula
        if pup-affected = 1 [ set dU_E dU_E * (1 - SA_change) ]

        set dU_H dU_E

        set U_E U_E + dU_E
        set U_H U_H + dU_H

      ]
      ;; update mother's reproduction buffer
      set U_R U_R - ([dU_E * kap_F] of turtle offspring_ID)
    ]
  ]
end 

to pup-feeding

  ifelse mother_ID > -1 [ ;;--------------- sanity check
    set f f_scaled * 2 * iv                            ;; higher fat content in milk (16 - 55% Hindell et al 1994)
    set e_scaled (v_rate * (U_E / (L ^ 3)))

    ;; test for proportional growth of pup and energy supply by mum
    set S_A (f * L ^ 2) / kap_L
    if pup-affected = 1 [ set S_A S_A * (1 - SA_change) ]

    set S_C 3 * (L ^ 2 * ((g * e_scaled) / (g + e_scaled)) * (1 + ((L * k_M_rate) / v_rate ))) ;; increased the energy mobilisation of pups

    set dU_E (S_A - S_C)
    set dU_H ((1 - kap_L) * S_C - (k_J_rate * U_H))
    set dL (((v_rate /(g * L ^ 2)) * S_C) - (k_M_rate * L))  ;; adjused for increase rate of growth (*3)

    set L L + dL
    set U_E U_E + dU_E
    set U_H U_H + dU_H
    set weaning_age weaning_age + 1
    set l_scaled L / Lmax

    ask turtle mother_ID [
      set U_R U_R - [ S_A * kap_L ] of turtle offspring_ID;--------see comments on DEB book (Kooijman 2010) - page 38
      set time-since-suckling time-since-suckling + 1
    ]
  ]
  [ ;; there's no mum -- they should go into fasting for some time before heading to the water for feeding
    set f (f_scaled + competition) * iv
    if f > 1 [set f 1]
    set stage juvenile
    set status fasting
    set moulting? 1          ;; so that pup stays on land for 50 days
    set days-moulting 0
  ]

  if age > weaning-duration
  [ ask turtle mother_ID [
      ;; update mother's details
      set offspring_ID -1
      set lactating? 0
      set is_pregnant? 0
      set time-since-suckling 0
      set status foraging
      set days-foraging 0

      set puppy 0
    ]
    ;; update pup details
    set f (f_scaled + competition) * iv
    if f > 1 [set f 1]
;    set f 0.5
    set stage juvenile
    set status fasting
    set moulting? 1          ;; so that pup stays on land for 50 days
    set days-moulting 0
    set mother_ID -1
  ]
end 
;;===============================================================================================================
; -------------------------- TIME MANAGEMENT --------------------------------------------------------------------

to do-time-management
  ;; model works on a 360 day year, where each month has 30 days.
  ask turtles [ set age age + 1 ]
  ask turtles with [ die-now > 0 ][ set days-dead days-dead + 1 ]

  set day-of-year day-of-year + 1
  set day-of-month day-of-month + 1

  if day-of-month > 30 [
    set day-of-month 1
    set month-of-year month-of-year + 1
  ]

  if month-of-year > 12 or day-of-year > 360 [
    set day-of-year 1
    set day-of-month 1
    set month-of-year 1

    set year year + 1
    ask turtles with [U_H > U_H^b] [ set had-pup? 0 ]
  ]

  if ticks = (clear-plots * 360) [ clear-all-plots ]

;-------------------------------------- IMPLEMENT SCENARIOS
  ;--------------------------------------------------------DENSITY DEPENDENCE
  if food_change? [
  ifelse max-pop-2 < max-pop-1
  ;K2 < K1 - - population decrease
  [ if (ticks >= implement-change-at * 360)                      ;; K2 is smaller so at this time the population starts declining
    and (carrying_capacity > max-pop-2)[                    ;; assuming (check here) that the population hasn't already reached K2
      let t (ticks / 360)                                   ;; setting t at this time

      if t = implement-change-at [                               ;; if t (now) is when the population should start declining
        set carrying_capacity carrying_capacity - (rate-of-decline * period-in-K)   ;; set the new K at the decline calculated for a x year period (set on interface)
        set t-for-k2 t + period-in-K                                                ;; and reset the time for the next change 10 years further
      ]
      if t = t-for-k2 [                                     ;; if t (now) equals time for the next change
        set carrying_capacity carrying_capacity - (rate-of-decline * period-in-K)   ;; set the new K at the decline calculated for a ten year period
        set t-for-k2 t + period-in-K                                                ;; and reset the time for the next change 10 years further
      ]
    ]
  ]

  ;K2 > K1 - - population increase
  [ if ticks = implement-change-at * 360 [
      set carrying_capacity max-pop-2   ;; as the poplation increases slowly - this can be a one off change.
      ]
    ]
  ]

;------------------------------------------------------------- CLIMATE VARIATION
  if decadal_change? [
    let t (ticks / 360)

    if t = implement-change-at or t = t-for-K2 [
      if change_freq = "ENSO"    [ set change-time 7 ]
      if change_freq = "SAM"     [ set change-time 4 ]
      if change_freq = "decadal" [ set change-time 10 ]
      if change_freq = "random"  [ set change-time 15 ]

      set climate-change 1
      set t-for-K2 t + change-time
    ]

    if climate-change = 1 [
      set clim-var ((rv * (sin ((360 / change-time) * year))) + 1)
    ]
  ]

  ;------------------------------------------------------------- IMPLEMENT CHANGE IN PUP ENERGY INTAKE
  if pup_energy_change? [
    let t (ticks / 360)
    if t = implement-change-at [
;      if percent_of_pup_change = "five"   [ set percent-pup-affected 5 ]  ; for 5% of pups affected
;      if percent_of_pup_change = "ten"    [ set percent-pup-affected 10 ] ; for 10% of pups affected
      if percent_of_pup_change = "twenty" [ set percent-pup-affected 20 ] ; for 20% of pups affected
;      if percent_of_pup_change = "thirty" [ set percent-pup-affected 30 ] ; for 30% of pups affected
;      if percent_of_pup_change = "fourty" [ set percent-pup-affected 40 ] ; for 40% of pups affected
      if percent_of_pup_change = "fifty"  [ set percent-pup-affected 50 ] ; for 50% of pups affected
;      if percent_of_pup_change = "sixty"  [ set percent-pup-affected 60 ] ; for 60% of pups affected
      if percent_of_pup_change = "eighty" [ set percent-pup-affected 80 ] ; for 40% of pups affected

;      if percent_of_energy_change = "five"   [ set SA_change 0.05 ] ; for 5% less energy intake
;      if percent_of_energy_change = "ten"    [ set SA_change 0.10 ] ; for 10% less energy intake
      if percent_of_energy_change = "twenty" [ set SA_change 0.20 ] ; for 20% less energy intake
;      if percent_of_energy_change = "thirty" [ set SA_change 0.30 ] ; for 30% less energy intake
;      if percent_of_energy_change = "fourty" [ set SA_change 0.40 ] ; for 40% less energy intake
      if percent_of_energy_change = "fifty"  [ set SA_change 0.50 ] ; for 50% less energy intake
;      if percent_of_energy_change = "sixty"  [ set SA_change 0.60 ] ; for 60% less energy intake
      if percent_of_energy_change = "eighty" [ set SA_change 0.80 ] ; for 80% less energy intake
    ]
  ]

  ;----------------------------------------------------------- IMPLEMENT CHANGE IN MUMs FECUNDITY // CUM REP THRESHOLD
  if mother_fecundity_change? [
    let t (ticks / 360)
    if t = implement-change-at [
      ;if percent_of_mum_change = "five"    [ set percent-mum-affected 5]
      ;if percent_of_mum_change = "ten"     [ set percent-mum-affected 10]
      if percent_of_mum_change = "twenty"  [ set percent-mum-affected 20]
      if percent_of_mum_change = "fifty"   [ set percent-mum-affected 50]
      if percent_of_mum_change = "eighty"  [ set percent-mum-affected 80]
      ;if percent_of_mum_change = "hundred" [ set percent-mum-affected 100]

      ;if percent_of_fec_change = "five"    [ set fec_change 0.05 ]
      ;if percent_of_fec_change = "ten"     [ set fec_change 0.10 ]
      ;if percent_of_fec_change = "fifteen" [ set fec_change 0.15 ]
      if percent_of_fec_change = "twenty"  [ set fec_change 0.20 ]
      if percent_of_fec_change = "fifty"   [ set fec_change 0.50 ]
      if percent_of_fec_change = "eighty"  [ set fec_change 0.80 ]

      set fecundity_changed_now? 1
    ]
  ]

  ;; ==============================================================TRACK FOR LIFE TABLES
  if ticks > 56 * 360 and ticks < 57 * 360 [
    ask turtles with [stage = 1 and age < 10] [set track-life 1]
  ]

  if ticks > 90 * 360 and ticks < 91 * 360 [
    ask turtles with [stage = 1 and age < 10] [set track-life 1]
  ]
end 
;;===============================================================================================================

to set-foetus-variables
  ;; - - - - - - - - - - - - - - - STATE VARIABLES - - - - - - - - - - - - - - - - - - - - - - - - - -
  set L 0.0001
  set dL 0
  set U_H 0.0001
  set dU_H 0

  set U_E 0.0001
  set dU_E 0
  set e_scaled [e_scaled] of turtle mother_ID
  set l_scaled L / Lmax

  set U_R 0
  set dU_R 0

  ;;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  set iv e ^ (random-normal 0 cv)

;  set J_XAm_rate   J_XAm_rate_init * iv
  set g g_init * iv

  set U_H^b (E_H^b_init / p_am) ;* iv        ;; scaled maturity at birth (cm^2 d)
  set U_H^x (E_H^x_init / p_am) ;* iv        ;; scaled maturity at weaning (cm^2 d)
  set U_H^p (E_H^p_init / p_am) ;* iv        ;; scaled maturity at puberty (cm^2 d)
  ;; - - - - - - - - - - - - - - - AGEING -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  set r_rate 0
  set q_accel 0
  set dq_accel 0
  set h_rate 0
  set dh_rate 0

  ;; - - - - - - - - REPRODUCTION / MOULTING
  set cumulative_UE (((L_w^b * shape_factor) ^ 3) * ((f_scaled + g) / v_rate) * (1 + (3 / 4) * ((L_w^b / L_w^m) / f_scaled)))
  set cum_UE cumulative_UE * l_scaled * 0.2

  set is_pregnant? 0
  set resting? 0
  set moulting? 0
  set breeding? 0
  set impregnated? 0

  set had-pup? 0
  set lactating? 0

  set tried-mating 0
  set time-since-mating 0
  set time-since-suckling 0
  set days-moulting 0
  set days-foraging 0
  set days-resting 0

  set puppy 0
  set total-puppies 0

  set die-now 0
  set died-of-? 0
  set final-age 0
  set final-pups -1
  set breeding-age 0
  set reprod-period 0
  set fecundity -1

  set weaning_age 0
  set days-dead 0

  set pup-affected 0
  set mum-affected 0
  set xx random-float 100
  if xx < percent-pup-affected [ set pup-affected 1 ]
  set xx random-float 100
  if xx < percent-mum-affected [ set mum-affected 1 ]

  set track-life 0
end 


;;===============================================================================================================
;; ----------------- PLOTS ----------------------------------------------------------------------------

to do-plots
;;   -------------------------------------- population plotting
  set-current-plot "total population numbers" ;;--------------------- total numbers
  set-current-plot-pen "adult"
  plot (count turtles with [stage = 3])
  set-current-plot-pen "juvenile"
  plot (count turtles with [stage = 2 and age > 360])
  set-current-plot-pen "pup"
  plot (count turtles with [stage = 1])
  set-current-plot-pen "embryo"
  plot (count turtles with [stage = 0])
  set-current-plot-pen "yearling"
  plot (count turtles with [age < 360 and stage > 0])
  set-current-plot-pen "population"
  plot (count turtles with [stage > 1])

  set-current-plot "population dynamics" ;; ------------------------- proportion
  set-current-plot-pen "0.5"
  plot 0.5
  set-current-plot-pen "adult"
  plot ((count turtles with [stage = 3]) / (count turtles + 0.001))
  set-current-plot-pen "juvenile"
  plot ((count turtles with [stage = 2]) / (count turtles + 0.001))
  set-current-plot-pen "pup"
  plot ((count turtles with [stage = 1]) / (count turtles + 0.001))
  set-current-plot-pen "embryo"
  plot ((count turtles with [stage = 0]) / (count turtles + 0.001))
  set-current-plot-pen "yearling"
  plot ((count turtles with [age < 360 and stage > 0]) / (count turtles + 0.001))

  set-current-plot "count" ;; ---------------------- total pups and breeders
  set-current-plot-pen "pups"
  plot (sum [puppy] of turtles )
  set-current-plot-pen "breeders"
  plot (count turtles with [U_R > cum_UE])  ; calculation for breeding energy levels
  set-current-plot-pen "total"
  plot count turtles
  set-current-plot-pen "K-pop"
  plot count turtles with [stage > 1]
  set-current-plot-pen "K"
  plot carrying_capacity

  set-current-plot "test pups survival" ;------------------------------------------- pup survival
  set-current-plot-pen "embryo"
  plot count turtles with [stage = 0]
  set-current-plot-pen "pup"
  plot count turtles with [stage = 1]
  set-current-plot-pen "yearling"
  plot count turtles with [stage = 2 and age < 350];-------------------------------------------------------------------- analysis

  set-current-plot "competition combined"
  set-current-plot-pen "comb"
  if any? turtles with [stage > 1 and age > 360 and status = 2 and f > 0][;-------------------------------------------------------------------- analysis
    plot mean [f] of turtles with [stage > 1 and f > 0]
  ]

  ;;;  ;; ----------------------------------------------- growth actual length
;;;  ;; to plot scaled length - remove "/ shape_factor" from plot and
;;;  ;; add "* shape_factor" to the set levels
  set-current-plot "L"
  if any? turtles with [stage > 1][ ;; set levels
    set-current-plot-pen "Lmax"
    plot mean [Lmax / shape_factor * f_scaled] of turtles
    set-current-plot-pen "Lp"
    plot mean [L_w^p] of turtles
    set-current-plot-pen "Lx"
    plot mean [L_w^x] of turtles
    set-current-plot-pen "Lb"
    plot mean [L_w^b] of turtles
  ]
  set-current-plot-pen "mean ad"        ;; adults
  ifelse any? turtles with [stage = 3]
  [ plot mean [L / shape_factor] of turtles with [stage = 3] ][ plot 0 ]
  set-current-plot-pen "mean juv"       ;; juveniles
  ifelse any? turtles with [stage = 2]
  [ plot mean [L / shape_factor] of turtles with [stage = 2] ][ plot 0 ]
   set-current-plot-pen "mean pup"       ;; pups
  ifelse any? turtles with [stage = 1]
  [ plot mean [L / shape_factor] of turtles with [stage = 1] ][ plot 0 ]
  set-current-plot-pen "mean embryo"  ;; embryos
  ifelse any? turtles with [stage = 0]
  [ plot mean [L / shape_factor] of turtles with [stage = 0] ][ plot 0 ]
  set-current-plot-pen "min ad"        ;; adults
  ifelse any? turtles with [stage = 3]
  [ plot min [L / shape_factor] of turtles with [stage = 3] ][ plot 0 ]
  set-current-plot-pen "min juv"       ;; juveniles
  ifelse any? turtles with [stage = 2]
  [ plot min [L / shape_factor] of turtles with [stage = 2] ][ plot 0 ]
  set-current-plot-pen "max ad"        ;; adults
  ifelse any? turtles with [stage = 3]
  [ plot max [L / shape_factor] of turtles with [stage = 3] ][ plot 0 ]
  set-current-plot-pen "max juv"       ;; juveniles
  ifelse any? turtles with [stage = 2]
  [ plot max [L / shape_factor] of turtles with [stage = 2] ][ plot 0 ]

  ; ------------------------------------------- energy level plotting
  set-current-plot "total mean Us - adult" ;; ----------------------adult energy
  if any? turtles with [stage = 3][
    set-current-plot-pen "0"
    plot 0
    set-current-plot-pen "U_H^p"
    plot (mean [U_H^p] of turtles)

    set-current-plot-pen "U_H"
    plot (mean [U_H] of turtles with [stage = 3])
    set-current-plot-pen "U_R-high"
    ifelse any? turtles with [stage = 3 and iv > 0.95]
    [ plot (mean [U_R] of turtles with [stage = 3 and iv > 0.95])][plot 0 ]
    set-current-plot-pen "U_R-low"
    ifelse any? turtles with [stage = 3 and iv < 0.95]
    [ plot (mean [U_R] of turtles with [stage = 3 and iv < 0.95])][plot 0 ]
    set-current-plot-pen "cum_UE"
    plot (mean [cum_UE] of turtles with [stage = 3])
  ]

  set-current-plot "total mean Us - juv" ;; -------------------------juvenile energy
  if any? turtles with [stage = 2][
    set-current-plot-pen "0"
    plot 0
    set-current-plot-pen "U_H^x"
    plot (mean [U_H^x] of turtles)
    set-current-plot-pen "U_H^p"
    plot (mean [U_H^p] of turtles)

    set-current-plot-pen "U_H"
    plot (mean [U_H] of turtles with [stage = 2])
    set-current-plot-pen "U_R-high"
    ifelse any? turtles with [stage = 2 and iv > 0.95]
    [ plot (mean [U_R] of turtles with [stage = 2 and iv > 0.95])][plot 0 ]
    set-current-plot-pen "U_R-low"
    ifelse any? turtles with [stage = 2 and iv < 0.95]
    [ plot (mean [U_R] of turtles with [stage = 2 and iv < 0.95])][plot 0 ]
    set-current-plot-pen "cum_UE"
    plot (mean [cum_UE] of turtles with [stage = 2])
  ]

  set-current-plot "pregnancy ages";;-------------------------------- age at pregancy
  set-current-plot-pen "10y"
  set-plot-pen-color 127
  plot 10
  set-current-plot-pen "4y"
  plot 4
  set-current-plot-pen "5y"
  plot 5
  set-current-plot-pen "max preg"
  ifelse any? turtles with [stage = 3 and offspring_ID > -1]
  [ plot (max [age] of turtles with [stage = 3 and offspring_ID > -1] / 360) ][ plot 0 ]
  set-current-plot-pen "mean preg" ;;;----------------------------------------------------------------- re-written below 15/02
  ifelse any? turtles with [stage = 3 and offspring_ID > -1]
  [ plot (mean [age] of turtles with [stage = 3 and offspring_ID > -1] / 360) ][ plot 0 ]
  set-current-plot-pen "min preg"
  ifelse any? turtles with [stage = 3 and offspring_ID > -1]
  [ plot (min [age] of turtles with [stage = 3 and offspring_ID > -1] / 360) ][ plot 0 ]
  set-current-plot-pen "mean preg" ;;;----------------------------------------------------------------- 15/02
  ifelse any? turtles with [offspring_ID > -1]
  [ plot (mean [age] of turtles with [offspring_ID > -1] / 360) ][ plot 0 ]
  set-current-plot-pen "min preg"
  ifelse any? turtles with [offspring_ID > -1]
  [ plot (min [age] of turtles with [offspring_ID > -1] / 360) ][ plot 0 ] ;-------------------------------------------------------------------- analysis
  set-current-plot-pen "first-preg";------------------------------------------ added 15/02
  ifelse any? turtles with [offspring_ID > -1 and total-puppies < 2]
  [ plot (mean [age] of turtles with [offspring_ID > -1 and total-puppies < 2] / 360) ][ plot 0 ]

  set-current-plot "fecundity"
  ifelse any? turtles with [fecundity > -1]
  [ set-current-plot-pen "max"
    plot max [fecundity] of turtles with [fecundity > -1];-------------------------------------------------------------------- analysis
    set-current-plot-pen "mean"
    plot mean [fecundity] of turtles with [fecundity > -1]
    set-current-plot-pen "min"
    plot min [fecundity] of turtles with [fecundity > -1]
  ]
  [ set-current-plot-pen "max"
    plot 0
    set-current-plot-pen "mean"
    plot 0
    set-current-plot-pen "min"
    plot 0
  ]

  set-current-plot "min fecundity"
  if any? turtles with [fecundity > -1]
  [ plot min [fecundity] of turtles with [fecundity > -1]]

  set-current-plot "mean fecundity"
  if any? turtles with [fecundity > -1]
  [ plot mean [fecundity] of turtles with [fecundity > -1]]

  set-current-plot "max fecundity"
  if any? turtles with [fecundity > -1]
  [ plot max [fecundity] of turtles with [fecundity > -1]]

  set-current-plot "max pups over max reproductive period"
  ifelse any? turtles with [reprod-period > 0]
  [ set-current-plot-pen "pups"
    plot max [final-pups] of turtles with [reprod-period > 0]
    set-current-plot-pen "period"
    plot max [reprod-period] of turtles with [final-pups = max [final-pups] of turtles with [reprod-period > 0]]
  ]
  [ set-current-plot-pen "pups"
    plot 0
    set-current-plot-pen "period"
    plot 0
  ]

  set-current-plot "final pups for seals"
  set-current-plot-pen "default"
  ifelse any? turtles with [final-pups > 0]
  [ plot max [final-pups] of turtles with [final-pups > 0] ][plot 0]
  set-current-plot-pen "only breeders - max"
  if any? turtles with [final-pups > 0][ plot max [final-pups] of turtles with [final-pups > 0]];-------------------------------------------------------------------- analysis
  set-current-plot-pen "only breeders - mean"
  if any? turtles with [final-pups > 0][ plot mean[final-pups] of turtles with [final-pups > 0]]
  set-current-plot-pen "mum-aff = 1"
  if any? turtles with [final-pups > 0 and mum-affected = 1][ plot max [final-pups] of turtles with [final-pups > 0 and mum-affected = 1]];-------------------------------------------------------------------- analysis
  set-current-plot-pen "mum-aff = 0"
  if any? turtles with [final-pups > 0 and mum-affected = 0][ plot max [final-pups] of turtles with [final-pups > 0 and mum-affected = 0]];-------------------------------------------------------------------- analysis

  set-current-plot "final age"
  if any? turtles with [final-age > 0]
  [ set-current-plot-pen "default"
    plot mean [final-age] of turtles with [final-age > 0]
    set-current-plot-pen "24"
    plot 24
  ]

  set-current-plot "seals older than 22 yo";;----------------------- count old seals
  set-current-plot-pen "count"
  plot count turtles with [age > (22 * 360)]

  set-current-plot "pup affected plot"
  set-current-plot-pen "default"
  plot ((count turtles with [pup-affected = 1 and age < (360 * 4) ]) / (count turtles with [age < (360 * 4)] + 0.001))
  set-current-plot-pen "0.5"
  plot 0.5

  set-current-plot "lifetable"
  set-current-plot-pen "default"
  ifelse any? turtles with [track-life = 1]
  [plot count turtles with [track-life = 1]]
  [plot 0]
  set-current-plot-pen "pen-1"
  ifelse any? turtles with [track-life = 1]
  [plot sum [puppy] of turtles with [track-life = 1]]
  [plot 0]

;;;;============================================================================================================================
;;;;============================================================================================================================
;;  ;plots for tracking of individual seal - intake, storage------------------------ individual plots
  if (turtle track-turtle != nobody) [

    set-current-plot "f of seal"
    set-current-plot-pen "default"
    plot [f] of turtle track-turtle
    set-current-plot-pen "1"
    plot 1

    set-current-plot "Seal energy use"
    set-current-plot-pen "U_H"
    plot ([U_H] of turtle track-turtle)
    set-current-plot-pen "U_R"
    plot ([U_R] of turtle track-turtle)
    set-current-plot-pen "U_H^b"
    plot ([U_H^b] of turtle track-turtle)
    set-current-plot-pen "U_H^x"
    plot ([U_H^x] of turtle track-turtle)
    set-current-plot-pen "U_H^p"
    plot ([U_H^p] of turtle track-turtle)
    set-current-plot-pen "breeding level"
    plot ([cum_UE] of turtle track-turtle)
    set-current-plot-pen "0"
    plot 0

    set-current-plot "seal energy intake"
    set-current-plot-pen "default"
    plot ([U_E] of turtle track-turtle)

    set-current-plot "seal size"
    plot [L / shape_factor] of turtle track-turtle


    set-current-plot "g and e" ;------------------------------------------- g e
    set-current-plot-pen "ge"
    plot [(g * e_scaled) / (g + e_scaled)] of turtle track-turtle
    set-current-plot-pen "l"
    plot [l_scaled] of turtle track-turtle
    set-current-plot-pen "e"
    plot [e_scaled] of turtle track-turtle


    set-current-plot "pregnant?"
    set-current-plot-pen "preg"
    ifelse [impregnated?] of turtle track-turtle = 1
    [plot 1][plot 0]
    set-current-plot-pen "age"
    ifelse [age] of turtle track-turtle = 1 * 360
    [ plot 1.5 ]
    [ ifelse [age] of turtle track-turtle = 2 * 360
      [ plot 1.5]
      [ ifelse [age] of turtle track-turtle = 3 * 360
        [ plot 1.5 ]
        [ ifelse [age] of turtle track-turtle = 4 * 360
          [ plot 1.5 ]
          [ ifelse [age] of turtle track-turtle = 5 * 360
            [ plot 1.5 ]
            [ ifelse [age] of turtle track-turtle = 6 * 360
              [ plot 1.5]
              [ ifelse [age] of turtle track-turtle = 7 * 360
                [ plot 1.5 ]
                [ ifelse [age] of turtle track-turtle = 8 * 360
                  [ plot 1.5 ]
                    [ ifelse [age] of turtle track-turtle = 9 * 360
                    [ plot 1.5 ]
                    [ ifelse [age] of turtle track-turtle = 10 * 360
                      [ plot 1.5]
                      [ ifelse [age] of turtle track-turtle = 15 * 360
                        [ set-plot-pen-color 105
                          plot 1.5]
                        [ plot 0]
                      ]
                    ]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
  ]
;
;;
;;  ;;==============================================================================================================
;;  ;;================================================================================= EXTRA PLOTS=================
;;;
;;;    set-current-plot "U of preg"
;;;  if any? turtles with [offspring_ID > -1]
;;;  [ set-current-plot-pen "0"
;;;    plot 0
;;;    set-current-plot-pen "U_H^p"
;;;    plot (mean [U_H^p] of turtles)
;;;    set-current-plot-pen "U_H^x"
;;;    plot (mean [U_H^x] of turtles)
;;;    set-current-plot-pen "U_H"
;;;    plot (mean [U_H] of turtles with [offspring_ID > -1])
;;;    set-current-plot-pen "U_R"
;;;    plot (mean [U_R] of turtles with [offspring_ID > -1])
;;;    set-current-plot-pen "cum_UE"
;;;    plot (mean [cum_UE] of turtles with [offspring_ID > -1])
;;;  ]
;;
;;;  set-current-plot "dead seals"
;;;  plot count turtles with [die-now = 1]
;;;

;
;;
;;;
;;;  set-current-plot "breeding age"
;;;  ifelse any? turtles with [breeding-age > 0]
;;;  [ set-current-plot-pen "max"
;;;    plot max [breeding-age] of turtles with [breeding-age > 0]
;;;    set-current-plot-pen "mean"
;;;    plot mean [breeding-age] of turtles with [breeding-age > 0]
;;;    set-current-plot-pen "min"
;;;    plot min [breeding-age] of turtles with [breeding-age > 0]
;;;  ]
;;;  [ set-current-plot-pen "max" plot 0
;;;    set-current-plot-pen "mean" plot 0
;;;    set-current-plot-pen "min" plot 0]
;;
;;;  set-current-plot "reproductive period"
;;;  ifelse any? turtles with [reprod-period > 0]
;;;  [ plot max [reprod-period] of turtles with [reprod-period > 0]][plot 0]
;;
;;
;;;  set-current-plot "weaning"
;;;  ifelse any? turtles with [stage = 1]
;;;  [ plot max [weaning_age] of turtles with [stage = 1]]
;;;  [ plot 0]
;;;
;;  set-current-plot "total mean Us - pup" ;; -------------------------pup energy
;;  if any? turtles with [stage = 1][
;;    set-current-plot-pen "U_H^b"
;;    plot (mean [U_H^b] of turtles)
;;    set-current-plot-pen "U_H^x"
;;    plot (mean [U_H^x] of turtles)
;;    set-current-plot-pen "U_H 1"
;;    plot (min [U_H] of turtles with [stage = 1])
;;    set-current-plot-pen "U_H 2"
;;    plot (mean [U_H] of turtles with [stage = 1])
;;    set-current-plot-pen "U_H 3"
;;    plot (max [U_H] of turtles with [stage = 1])
;;  ]
;;;
;;;  set-current-plot "total mean Us - embryo" ;;--------------------- embryo enery
;;;  if any? turtles with [stage = 0][
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;    set-current-plot-pen "U_H^b"
;;;    plot (mean [U_H^b] of turtles)
;;;    set-current-plot-pen "U_H^x"
;;;    plot (mean [U_H^x] of turtles)
;;;    set-current-plot-pen "U_H 1"
;;;    plot (min [U_H] of turtles with [stage = 0])
;;;    set-current-plot-pen "U_H 2"
;;;    plot (mean [U_H] of turtles with [stage = 0])
;;;    set-current-plot-pen "U_H 3"
;;;    plot (max [U_H] of turtles with [stage = 0])
;;;
;;;  ]
;;;
;;;  set-current-plot "Seals fasting" ;;---------------------------- total juveniles fasting
;;;  set-current-plot-pen "juvenile"
;;;  plot count turtles with [age > 360 and stage < 3 and status = fasting and haul-out? = 0]
;;;  set-current-plot-pen "juv-mwho"
;;;  plot count turtles with [age > 360 and stage < 3 and status = fasting and haul-out? = 1]
;;;  set-current-plot-pen "yearling"
;;;  plot count turtles with [age < 361 and status = fasting and haul-out? = 0]
;;;  set-current-plot-pen "year-mwho"
;;;  plot count turtles with [age < 361 and stage < 3 and status = fasting and haul-out? = 1]
;;;
;;;  set-current-plot "U_R test" ;;------------------------------- reproduction buffer levels seals
;;;  set-current-plot-pen "preg min"
;;;  ifelse any? turtles with [is_pregnant? = 1]
;;;  [ plot min [U_R] of turtles with [is_pregnant? = 1]][ plot 0 ]
;;;  set-current-plot-pen "preg mean"
;;;  ifelse any? turtles with [is_pregnant? = 1]
;;;  [ plot mean [U_R] of turtles with [is_pregnant? = 1]][ plot 0 ]
;;;;  set-current-plot-pen "preg max"
;;;;  ifelse any? turtles with [is_pregnant? = 1]
;;;;  [ plot max [U_R] of turtles with [is_pregnant? = 1]][ plot 0 ]
;;;;  set-current-plot-pen "non-preg min"
;;;;  ifelse any? turtles with [is_pregnant? = 0 and age > 4 * 360 and age < 15 * 360]
;;;;  [plot min [U_R] of turtles with [is_pregnant? = 0 and age > 4 * 360 and age < 15 * 360]][plot 0]
;;;;  set-current-plot-pen "non-preg mean"
;;;;  ifelse any? turtles with [is_pregnant? = 0 and age > 4 * 360 and age < 15 * 360]
;;;;  [plot mean [U_R] of turtles with [is_pregnant? = 0 and age > 4 * 360 and age < 15 * 360]] [plot 0]
;;;;  set-current-plot-pen "non-preg max"
;;;;  ifelse any? turtles with [is_pregnant? = 0 and age > 4 * 360 and age < 15 * 360]
;;;;  [plot max [U_R] of turtles with [is_pregnant? = 0 and age > 4 * 360 and age < 15 * 360]][plot 0]
;;;;  set-current-plot-pen "cum_UE"
;;;;  plot mean [cum_UE] of turtles
;;;  set-current-plot-pen "0.5 of cum_UE"
;;;  plot (mean [0.5 * cum_UE] of turtles)
;;
;;
;;
;;
;;
;;
;;;  set-current-plot "pup e-scaled"
;;;  set-current-plot-pen "pup"
;;;  ifelse any? turtles with [stage = 1]
;;;  [plot mean [e_scaled] of turtles with [stage = 1]][plot 0]
;;;  set-current-plot-pen "adult"
;;;  ifelse any? turtles with [stage = 3]
;;;  [plot mean [e_scaled] of turtles with [stage = 3]][plot 0]
;;;  set-current-plot-pen "1"
;;;  plot 1
;;;
;;
;;;
;;;  set-current-plot "embryo - pup L"
;;;  set-current-plot-pen "embryo"  ;; embryos
;;;  ifelse any? turtles with [stage = 0]
;;;  [ plot max [L / shape_factor] of turtles with [stage = 0] ][ plot 0 ]
;;;  set-current-plot-pen "pup"       ;; pups
;;;  ifelse any? turtles with [stage = 1]
;;;  [ plot max [L / shape_factor] of turtles with [stage = 1] ][ plot 0 ]
;;;  set-current-plot-pen "1"
;;;  plot mean [L_w^x] of turtles
;;;  set-current-plot-pen "0"
;;;  plot mean [L_w^b] of turtles
;;;
;;
;;;  set-current-plot "SC_pup"
;;;  set-current-plot-pen "default"
;;;  ifelse any? turtles with [stage = 1]
;;;  [plot mean [S_C] of turtles with [stage = 1]][plot 0]
;;
;;;  ; ---------------------------------- energy level plotting
;;;  ; --------------------------------------------- delta energy plotting
;;;  set-current-plot "total deltas - adult" ;; adults
;;;  if any? turtles with [stage = 3][
;;;    set-current-plot-pen "dU_E"
;;;    plot (mean [dU_E] of turtles  with [stage = 3])
;;;    set-current-plot-pen "dU_H"
;;;    plot (mean [dU_H] of turtles  with [stage = 3])
;;;    set-current-plot-pen "dU_R"
;;;    plot (mean [dU_R] of turtles  with [stage = 3])
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;  ]
;;;
;;;  set-current-plot "total deltas - juv" ;; juveniles
;;;  if any? turtles with [stage = 2][
;;;    set-current-plot-pen "dU_E"
;;;    plot (mean [dU_E] of turtles  with [stage = 2])
;;;    set-current-plot-pen "dU_H"
;;;    plot (mean [dU_H] of turtles  with [stage = 2])
;;;    set-current-plot-pen "dU_R"
;;;    plot (mean [dU_R] of turtles  with [stage = 2])
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;  ]
;;;  set-current-plot "total deltas - pup" ;; pups
;;;  if any? turtles with [stage = 1][
;;;;    set-current-plot-pen "dU_E"
;;;;    plot (mean [dU_E] of turtles  with [stage = 1])
;;;    set-current-plot-pen "dU_H"
;;;    plot (mean [dU_H] of turtles  with [stage = 1])
;;;    set-current-plot-pen "dU_R"
;;;    plot (mean [dU_R] of turtles  with [stage = 1])
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;  ]
;;;
;;;  set-current-plot "dU_E pup"
;;;  if any? turtles with [stage = 1][
;;;    set-current-plot-pen "default"
;;;    plot (mean [dU_E] of turtles  with [stage = 1])
;;;  ]
;;;;
;;;  set-current-plot "total deltas - embryo" ;; embryos
;;;  if any? turtles with [stage = 0][
;;;    set-current-plot-pen "dU_E"
;;;    plot (mean [dU_E] of turtles  with [stage = 0])
;;;    set-current-plot-pen "dU_H"
;;;    plot (mean [dU_H] of turtles  with [stage = 0])
;;;    set-current-plot-pen "dU_E w kap_F"
;;;    plot (mean [dU_E * kap_F] of turtles  with [stage = 0])
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;  ]
;;
;;;  ;;----------------------------------------------- delta growth plotting
;;;  set-current-plot "dL" ;-------------------------------------------- delta growth
;;;  ifelse any? turtles with [stage = 3] ;; adults
;;;  [ set-current-plot-pen "adult"
;;;    plot (mean [dL] of turtles with [stage = 3])
;;;  ]
;;;  [ set-current-plot-pen "adult"
;;;    plot 0
;;;  ]
;;;  ifelse any? turtles with [stage = 2] ;; juveniles
;;;  [ set-current-plot-pen "juv"
;;;    plot (mean [dL] of turtles with [stage = 2])
;;;  ]
;;;  [ set-current-plot-pen "juv"
;;;    plot 0
;;;  ]
;;;;  ifelse any? turtles with [stage = 1] ;; pups
;;;;  [ set-current-plot-pen "pups"
;;;;    plot (mean [dL] of turtles with [stage = 1])
;;;;  ]
;;;;  [ set-current-plot-pen "pups"
;;;;    plot 0
;;;;  ]
;;;
;;;;  set-current-plot "dL foetus + pups" ;----------------------------- delta growth foetus/pups
;;;;  ifelse any? turtles with [stage = 1] ;; pups
;;;;  [ set-current-plot-pen "pups"
;;;;    plot (mean [dL] of turtles with [stage = 1])
;;;;  ]
;;;;  [ set-current-plot-pen "pups"
;;;;    plot 0
;;;;  ]
;;;;  ifelse any? turtles with [stage = 0] ;; foetus
;;;;  [ set-current-plot-pen "embryo"
;;;;    plot (mean [dL] of turtles with [stage = 0])
;;;;  ]
;;;;  [ set-current-plot-pen "embryo"
;;;;    plot 0
;;;;  ]
;;;
;;;  if any? turtles with [e_scaled > l_scaled][;;------------------non-starvation mode
;;;;    1)
;;;    set-current-plot "normal deltas" ;--------------------------------- normal delta
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;    set-current-plot-pen "dU_E"
;;;    plot (mean [dU_E] of turtles with [e_scaled > l_scaled])
;;;    set-current-plot-pen "dU_H"
;;;    plot (mean [dU_H] of turtles with [e_scaled > l_scaled])
;;;    set-current-plot-pen "dU_R"
;;;    plot (mean [dU_R] of turtles with [e_scaled > l_scaled])
;;;
;;;;    2)
;;;    set-current-plot "normal Us" ;-------------------------------------- normal U
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;    set-current-plot-pen "U_E"
;;;    plot (mean [U_E] of turtles with [e_scaled > l_scaled])
;;;    set-current-plot-pen "U_H"
;;;    plot (mean [U_H] of turtles with [e_scaled > l_scaled])
;;;    set-current-plot-pen "U_R"
;;;    plot (mean [U_R] of turtles with [e_scaled > l_scaled])
;;;  ]
;;;  ;--------------------------------- starvation condition plots
;;;  if any? turtles with [e_scaled < l_scaled][
;;;;    type " count turtles with e_scaled < l_scaled " print count turtles with [e_scaled < l_scaled]
;;;;    1)
;;;    set-current-plot "starvation deltas" ;------------------------------ starvation delta
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;    set-current-plot-pen "dU_E"
;;;    plot (mean [dU_E] of turtles with [e_scaled < l_scaled])
;;;    set-current-plot-pen "dU_H"
;;;    plot (mean [dU_H] of turtles with [e_scaled < l_scaled])
;;;    set-current-plot-pen "dU_R"
;;;    plot (mean [dU_R] of turtles with [e_scaled < l_scaled])
;;;;    2)
;;;    set-current-plot "starvation Us" ;---------------------------------- starvation Us
;;;    set-current-plot-pen "0"
;;;    plot 0
;;;    set-current-plot-pen "U_E"
;;;    plot (mean [U_E] of turtles with [e_scaled < l_scaled])
;;;    set-current-plot-pen "U_H"
;;;    plot (mean [U_H] of turtles with [e_scaled < l_scaled])
;;;    set-current-plot-pen "U_R"
;;;    plot (mean [U_R] of turtles with [e_scaled < l_scaled])
;;;  ]
;;
;;  ;------------------------------ general plots
;;
;;;  set-current-plot "S_C" ;---------------------------------------------- SC
;;;  set-current-plot-pen "0"
;;;  plot 0
;;;  set-current-plot-pen "S_A"
;;;  plot (mean [S_A] of turtles with [stage > 1])
;;;  set-current-plot-pen "S_C"
;;;  plot (mean [S_C] of turtles with [stage > 1])
;;
;;
;;;  ;;-------------------------pregnancy/non-pregnancy U_R checks
;;;  set-current-plot "U_R of pregnant seals"
;;;  set-current-plot-pen "mean U_R"
;;;  ifelse any? turtles with [is_pregnant? = 1]
;;;  [ plot mean [U_R] of turtles with [is_pregnant? = 1]][ plot 0 ]
;;;  set-current-plot-pen "min U_R"
;;;  ifelse any? turtles with [is_pregnant? = 1]
;;;  [ plot min [U_R] of turtles with [is_pregnant? = 1]][ plot 0 ]
;;;  set-current-plot-pen "cum_UE"
;;;  plot mean [cum_UE] of turtles
;;;
;;;  set-current-plot "U_R of non pregnant seals"
;;;  set-current-plot-pen "mean U_R"
;;;  plot mean [U_R] of turtles with [is_pregnant? = 0 and age > 4 * 360 and age < 15 * 360]
;;;  set-current-plot-pen "max U_R"
;;;  plot max [U_R] of turtles with [is_pregnant? = 0 and age > 4 * 360 and age < 15 * 360]
;;;  set-current-plot-pen "cum_UE"
;;;  plot mean [cum_UE] of turtles
;;
;;  ;;------------------------------------------------------- births
;;;  set-current-plot "prop succesful births to breeders" ;--------------------- births of breeders
;;;  set-current-plot-pen "pen-1"
;;;  plot 0.25
;;;  set-current-plot-pen "pen-2"
;;;  plot 0.5
;;;  set-current-plot-pen "pen-3"
;;;  plot 0.75
;;;  set-current-plot-pen "pen-4"
;;;  plot 1
;;;  if count turtles with [stage = 1] / (0.00000000000001 + count turtles with [U_R > cum_UE]) < 2 [
;;;    set-current-plot-pen "default"
;;;    plot (count turtles with [stage = 1] / (0.00000000000001 + count turtles with [U_R > cum_UE]))
;;;  ]
;;;  if count turtles with [stage = 1] / (0.00000000000001 + count turtles with [U_R > cum_UE and age < (15 * 360)]) < 2 [
;;;    set-current-plot-pen "pen-5"
;;;    plot (count turtles with [stage = 1] / (0.00000000000001 + count turtles with [U_R > cum_UE and age < (15 * 360)]))
;;;  ]
;;;
  ;;  set-current-plot "pop dyn" ;; ------------------------- proportion based on age
;;;  set-current-plot-pen "0.5"
;;;  plot 0.5
;;;  set-current-plot-pen "6+"
;;;  plot ((count turtles with [stage > 1 and age > 360 * 6]) / (count turtles + 0.001))
;;;  set-current-plot-pen "pup-6"
;;;  plot ((count turtles with [stage  > 1 and age < 360 * 6]) / (count turtles + 0.001))
;;;  set-current-plot-pen "pup"
;;;  plot ((count turtles with [stage = 1]) / (count turtles + 0.001))
;;;  set-current-plot-pen "embryo"
;;;  plot ((count turtles with [stage = 0]) / (count turtles + 0.001))
;;;
;
;;  set-current-plot "fec_by_age"
;;  if ticks > 50 * 360 [
;;  if any? turtles with [total-puppies > 0][
;;
;;    ifelse any? turtles with [total-puppies > 0 and age > 3 * 360 and age < 4  * 360] [
;;      set-current-plot-pen "pen-2"
;;      plot mean [total-puppies / ((3 / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 3 * 360 and age < 4 * 360]
;;    ][set-current-plot-pen "pen-2" plot 0]
;;    ifelse any? turtles with [total-puppies > 0 and age > 4 * 360 and age < 5 * 360] [
;;      set-current-plot-pen "pen-3"
;;      plot mean [total-puppies / ((4 / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 4 * 360 and age < 5 * 360]
;;    ][set-current-plot-pen "pen-3" plot 0]
;;    ifelse any? turtles with [total-puppies > 0 and age > 5 * 360 and age < 6 * 360] [
;;      set-current-plot-pen "pen-4"
;;      plot mean [total-puppies / ((5 / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 5 * 360 and age < 6 * 360]
;;    ][set-current-plot-pen "pen-4" plot 0]
;;    ifelse any? turtles with [total-puppies > 0 and age < 6 * 360 and age < 7 * 360] [
;;      set-current-plot-pen "pen-5"
;;      plot mean [total-puppies / ((6 / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 6 * 360 and age < 7 * 360]
;;    ][set-current-plot-pen "pen-5" plot 0]
;;    ifelse any? turtles with [total-puppies > 0 and age > 7 * 360 and age < 8  * 360] [
;;      set-current-plot-pen "pen-6"
;;      plot mean [total-puppies / ((7 / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 7 * 360 and age < 8 * 360]
;;    ][set-current-plot-pen "pen-6" plot 0]
;;    ifelse any? turtles with [total-puppies > 0 and age > 8 * 360 and age < 9 * 360] [
;;      set-current-plot-pen "pen-7"
;;      plot mean [total-puppies / ((8 / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 8 * 360 and age < 9 * 360]
;;    ][set-current-plot-pen "pen-7" plot 0]
;;    ifelse any? turtles with [total-puppies > 0 and age > 9 * 360 and age < 10 * 360] [
;;      set-current-plot-pen "pen-8"
;;      plot mean [total-puppies / ((9 / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 9 * 360 and age < 10 * 360]
;;    ][set-current-plot-pen "pen-8" plot 0]
;;    ifelse any? turtles with [total-puppies > 0 and age > 10 * 360 and age < 11 * 360] [
;;      set-current-plot-pen "pen-9"
;;      plot mean [total-puppies / ((10 / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 10 * 360 and age < 11 * 360]
;;    ][set-current-plot-pen "pen-9" plot 0]
;;    ifelse any? turtles with [total-puppies > 0 and age > 11 * 360] [
;;      set-current-plot-pen "pen-10"
;;      plot mean [total-puppies / ((age / 360) - breeding-age)] of turtles with [total-puppies > 0 and age > 11 * 360]
;;    ][set-current-plot-pen "pen-10" plot 0]
;;
;;  ]
;;  ]
;;
;;  set-current-plot "rate_of_survival"
;;  set-current-plot-pen "pen-1"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age < 1 * 360 and stage > 0] / count turtles with [die-now = 0 and age < 1 * 360 and stage > 0]
;;  set-current-plot-pen "pen-2"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 1 * 360 and age < 2 * 360] / count turtles with [die-now = 0 and age > 1 * 360 and age < 2 * 360]
;;  set-current-plot-pen "pen-3"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 2 * 360 and age < 3 * 360] / count turtles with [die-now = 0 and age > 2 * 360 and age < 3 * 360]
;;  set-current-plot-pen "pen-4"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 3 * 360 and age < 4 * 360] / count turtles with [die-now = 0 and age > 3 * 360 and age < 4 * 360]
;;  set-current-plot-pen "pen-5"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 4 * 360 and age < 5 * 360] / count turtles with [die-now = 0 and age > 4 * 360 and age < 5 * 360]
;;  set-current-plot-pen "pen-6"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 5 * 360 and age < 6 * 360] / count turtles with [die-now = 0 and age > 5 * 360 and age < 6 * 360]
;;  set-current-plot-pen "pen-7"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 6 * 360 and age < 7 * 360] / count turtles with [die-now = 0 and age > 6 * 360 and age < 7 * 360]
;;  set-current-plot-pen "pen-8"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 7 * 360 and age < 8 * 360] / count turtles with [die-now = 0 and age > 7 * 360 and age < 8 * 360]
;;  set-current-plot-pen "pen-9"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 8 * 360 and age < 9 * 360] / count turtles with [die-now = 0 and age > 8 * 360 and age < 9 * 360]
;;  set-current-plot-pen "pen-10"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 19 * 360 and age < 10 * 360] / count turtles with [die-now = 0 and age > 9 * 360 and age < 10 * 360]
;;  set-current-plot-pen "pen-11"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 10 * 360 and age < 11 * 360] / count turtles with [die-now = 0 and age > 10 * 360 and age < 11 * 360]
;;  set-current-plot-pen "pen-12"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 11 * 360 and age < 12 * 360] / count turtles with [die-now = 0 and age > 11 * 360 and age < 12 * 360]
;;  set-current-plot-pen "pen-13"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 12 * 360 and age < 13 * 360] / count turtles with [die-now = 0 and age > 12 * 360 and age < 13 * 360]
;;  set-current-plot-pen "pen-14"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 13 * 360 and age < 14 * 360] / count turtles with [die-now = 0 and age > 13 * 360 and age < 14 * 360]
;;  set-current-plot-pen "pen-15"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 14 * 360 and age < 15 * 360] / count turtles with [die-now = 0 and age > 14 * 360 and age < 15 * 360]
;;  set-current-plot-pen "pen-16"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 15 * 360 and age < 16 * 360] / count turtles with [die-now = 0 and age > 15 * 360 and age < 16 * 360]
;;  set-current-plot-pen "pen-17"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 16 * 360 and age < 17 * 360] / count turtles with [die-now = 0 and age > 16 * 360 and age < 17 * 360]
;;  set-current-plot-pen "pen-18"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 17 * 360 and age < 18 * 360] / count turtles with [die-now = 0 and age > 17 * 360 and age < 18 * 360]
;;  set-current-plot-pen "pen-19"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 18 * 360 and age < 19 * 360] / count turtles with [die-now = 0 and age > 18 * 360 and age < 19 * 360]
;;
;;  ifelse any? turtles with [age > 19 * 360 and age < 20 * 360][
;;  set-current-plot-pen "pen-20"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 19 * 360 and age < 20 * 360] / count turtles with [die-now = 0 and age > 19 * 360 and age < 20 * 360]
;;    ][set-current-plot-pen "pen-20" plot 0]
;;  ifelse any? turtles with [age > 20 * 360 and age < 21 * 360][
;;  set-current-plot-pen "pen-21"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 20 * 360 and age < 21 * 360] / count turtles with [die-now = 0 and age > 20 * 360 and age < 21 * 360]
;;    ][set-current-plot-pen "pen-21" plot 0]
;;  ifelse any? turtles with [age > 21 * 360 and age < 22 * 360][
;;  set-current-plot-pen "pen-22"
;;  plot count turtles with [die-now = 1 and days-dead = 1 and age > 21 * 360 and age < 22 * 360] / count turtles with [die-now = 0 and age > 21 * 360 and age < 22 * 360]
;;  ][set-current-plot-pen "pen-22" plot 0]
;;    ifelse any? turtles with [age > 22 * 360 and age < 23 * 360][
;;    set-current-plot-pen "pen-23"
;;    plot count turtles with [die-now = 1 and days-dead = 1 and age > 22 * 360 and age < 23 * 360] / count turtles with [die-now = 0 and age > 22 * 360 and age < 23 * 360]
;;  ][set-current-plot-pen "pen-23" plot 0]
;;  ifelse any? turtles with [age > 23 * 360 and age < 24 * 360][
;;    set-current-plot-pen "pen-24"
;;    plot count turtles with [die-now = 1 and days-dead = 1 and age > 23 * 360 and age < 24 * 360] / count turtles with [die-now = 0 and age > 23 * 360 and age < 24 * 360]
;;  ][set-current-plot-pen "pen-24" plot 0]
;;  ifelse any? turtles with [age > 24 * 360][
;;    set-current-plot-pen "pen-25"
;;    plot count turtles with [die-now = 1 and days-dead = 1 and age > 24 * 360 and age < 25 * 360] / count turtles with [die-now = 0 and age > 24 * 360 and age < 25 * 360]
;;  ][set-current-plot-pen "pen-25" plot 0]
;;  ifelse any? turtles with [age > 25 * 360][
;;    set-current-plot-pen "pen-26"
;;    plot count turtles with [die-now = 1 and days-dead = 1 and age > 25 * 360] / count turtles with [die-now = 0 and age > 25 * 360]
;;  ][set-current-plot-pen "pen-26" plot 0]
;
;
;
;
;;;  set-current-plot "death reason"
;;;  if any? turtles with [die-now = 1 and days-dead = 1] [
;;;    set-current-plot-pen "1 - low energy"
;;;    ifelse any? turtles with [die-now = 1 and days-dead = 1 and died-of-? = 1]
;;;    [ plot ((count turtles with [die-now = 1 and days-dead = 1 and died-of-? = 1]) / (count turtles with [die-now = 1 and days-dead = 1 ] + 0.001))]
;;;    [ plot 0 ]
;;;    set-current-plot-pen "2 - foetus w mum"
;;;    ifelse any? turtles with [die-now = 1 and days-dead = 1 and died-of-? = 2]
;;;    [ plot ((count turtles with [die-now = 1 and days-dead = 1 and died-of-? = 2])/ (count turtles with [die-now = 1 and days-dead = 1 ] + 0.001))]
;;;    [ plot 0 ]
;;;    set-current-plot-pen "3 - old age"
;;;    ifelse any? turtles with [die-now = 1 and days-dead = 1 and died-of-? = 3]
;;;    [ plot ((count turtles with [die-now = 1 and days-dead = 1 and died-of-? = 3])/ (count turtles with [die-now = 1 and days-dead = 1 ] + 0.001))]
;;;    [ plot 0 ]
;;;    set-current-plot-pen "4 - error"
;;;    ifelse any? turtles with [die-now = 1 and days-dead = 1 and died-of-? = 4]
;;;    [ plot ((count turtles with [die-now = 1 and days-dead = 1 and died-of-? = 4])/ (count turtles with [die-now = 1 and days-dead = 1 ] + 0.001))]
;;;    [ plot 0 ]
;;;    set-current-plot-pen "5 - pup mort"
;;;    ifelse any? turtles with [die-now = 1 and days-dead = 1 and died-of-? = 5]
;;;    [ plot ((count turtles with [die-now = 1 and days-dead = 1 and died-of-? = 5])/ (count turtles with [die-now = 1 and days-dead = 1 ] + 0.001))]
;;;    [ plot 0 ]
;;;  ]
;;
;
;;
;;;  set-current-plot "total mean Us - yearling" ;; -------------------------yearling energy
;;;  if any? turtles with [stage = 2 and age < 350][
;;;    set-current-plot-pen "U_H^p"
;;;    plot (mean [U_H^p] of turtles)
;;;    set-current-plot-pen "U_H^x"
;;;    plot (mean [U_H^x] of turtles)
;;;    set-current-plot-pen "U_H^b"
;;;    plot (mean [U_H^b] of turtles)
;;;    set-current-plot-pen "max UH"
;;;    plot (max [U_H] of turtles with [stage = 2 and age < 350])
;;;    set-current-plot-pen "mean UH"
;;;    plot (mean [U_H] of turtles with [stage = 2 and age < 350])
;;;    set-current-plot-pen "min UH"
;;;    plot (min [U_H] of turtles with [stage = 2 and age < 350])
;;;  ]
;;
;;set-current-plot "competition"
;;  set-current-plot-pen "comb"
;;  if any? turtles with [stage > 1 and age > 360 and status = 2 and f > 0][;-------------------------------------------------------------------- analysis
;;    plot mean [f] of turtles with [stage > 1 and f > 0]
;;  ]
;;  set-current-plot-pen "max juv"
;;  if any? turtles with [stage = 2 and status = 2 and f > 0 and age > 360][
;;    plot max [f] of turtles with [stage = 2 and f > 0] ; status = 2, only while foraging
;;  ]
;;  set-current-plot-pen "mean juv"
;;  if any? turtles with [stage = 2 and status = 2 and f > 0 and age > 360][
;;    plot mean [f] of turtles with [stage = 2 and f > 0] ; status = 2, only while foraging
;;  ]
;;  set-current-plot-pen "min juv"
;;  if any? turtles with [stage = 2 and status = 2 and f > 0 and age > 360][
;;    plot min [f] of turtles with [stage = 2 and f > 0] ; status = 2, only while foraging
;;  ]
;;  set-current-plot-pen "max adult"
;;  if any? turtles with [stage = 3 and status = 2 and f > 0][
;;    plot max [f] of turtles with [stage = 3 and f > 0] ; status = 2, only while foraging
;;  ]
;;  set-current-plot-pen "mean adult"
;;  if any? turtles with [stage = 3 and status = 2 and f > 0][
;;    plot mean [f] of turtles with [stage = 3 and f > 0] ; status = 2, only while foraging
;;  ]
;;  set-current-plot-pen "min adult"
;;  if any? turtles with [stage = 3 and status = 2 and f > 0][
;;    plot min [f] of turtles with [stage = 3 and f > 0] ; status = 2, only while foraging
;;  ]
;
;
;;
;;;  set-current-plot "total count"
;;;  set-current-plot-pen "old"
;;;  plot count turtles with [stage > 1]
;;;  set-current-plot-pen "young"
;;;  plot count turtles with [stage = 1]
;;;
;
;;
;
;
;
;;;;


;
;;
;  set-current-plot "L for export"
;  if any? turtles with [stage > 1][ ;; set levels
;    set-current-plot-pen "Lmax"
;    plot mean [Lmax / shape_factor * f_scaled] of turtles
;    set-current-plot-pen "Lp"
;    plot mean [L_w^p] of turtles
;    set-current-plot-pen "Lx"
;    plot mean [L_w^x] of turtles
;    set-current-plot-pen "Lb"
;    plot mean [L_w^b] of turtles
;  ]
;  set-current-plot-pen "max"
;  ifelse any? turtles
;  [ plot (max [L / shape_factor] of turtles)][ plot 0 ]
;  set-current-plot-pen "mean adult"
;  ifelse any? turtles  with [stage = 3]
;  [ plot (mean [L / shape_factor] of turtles with [stage = 3]) ][ plot 0 ]
;  set-current-plot-pen "min adult"
;  ifelse any? turtles  with [stage = 3]
;  [ plot (min [L / shape_factor] of turtles with [stage = 3]) ][ plot 0 ]
;  set-current-plot-pen "mean juv"
;  ifelse any? turtles  with [stage = 2]
;  [ plot (mean [L / shape_factor] of turtles with [stage = 2]) ][ plot 0]
;  set-current-plot-pen "min juv"
;  ifelse any? turtles  with [stage = 2]
;  [ plot (min [L / shape_factor] of turtles with [stage = 2]) ][ plot 0 ]
;  set-current-plot-pen "max pup"
;  ifelse any? turtles  with [stage = 2]
;  [ plot (mean [L / shape_factor] of turtles with [stage = 2]) ][ plot 0]
;  set-current-plot-pen "min pup"
;  ifelse any? turtles  with [stage = 2]
;  [ plot (min [L / shape_factor] of turtles with [stage = 2]) ][ plot 0 ]
;;
;;;  set-current-plot "L of embryo"
;;;  if any? turtles with [stage = 0][
;;;    set-current-plot-pen "Lx"
;;;    plot mean [L_w^x] of turtles
;;;    set-current-plot-pen "Lb"
;;;    plot mean [L_w^b] of turtles
;;;    set-current-plot-pen "L 1"
;;;    plot min [L / shape_factor] of turtles with [stage = 0]
;;;    set-current-plot-pen "L 2"
;;;    plot mean [L / shape_factor] of turtles with [stage = 0]
;;;    set-current-plot-pen "L 3"
;;;    plot max [L / shape_factor] of turtles with [stage = 0]
;;;  ]
;;
;;
;;;
;;;;;
;;;;;  ;--------------------------------------------------------------------------------
;;;;;  ;--------------------------------------------------------------------------------
;;;;;
;
;;;
;;;
;;;
;;;;  ;;----------------------------------------------------------------------------------------------------
;;;;  ;;---------------------------------------------------------------------------------------------------
;;;;;
;;;  ;---------------------------------- age plotting
;;  set-current-plot "age" ;----------------------------------------------- age
;;  set-current-plot-pen "max"
;;;  plot (max [age] of turtles / 360)
;;;  set-current-plot-pen "mean adult"
;;;  ifelse any? turtles  with [stage = 3]
;;;  [ plot (mean [age] of turtles with [stage = 3] / 360) ][ plot 0 ];-------------------------------------------------------------------- analysis
;;;  set-current-plot-pen "min adult"
;;;  ifelse any? turtles  with [stage = 3]
;;;  [ plot (min [age] of turtles with [stage = 3] / 360) ][ plot 0 ];-------------------------------------------------------------------- analysis
;;;  set-current-plot-pen "mean juv"
;;;  ifelse any? turtles  with [stage = 2]
;;;  [ plot (mean [age] of turtles with [stage = 2] / 360) ][ plot 0]
;;;  set-current-plot-pen "min juv"
;;;  ifelse any? turtles  with [stage = 2]
;;;  [ plot (min [age] of turtles with [stage = 2] / 360) ][ plot 0 ]
;;  set-current-plot-pen "max pup"
;;  ifelse any? turtles  with [stage = 1]
;;  [ plot (mean [age] of turtles with [stage = 1] / 360) ][ plot 0]
;;  set-current-plot-pen "min pup"
;;  ifelse any? turtles  with [stage = 1]
;;  [ plot (min [age] of turtles with [stage = 1] / 360) ][ plot 0 ]
;;;  set-current-plot-pen "23"
;;;  plot 23
;;;  set-current-plot-pen "15"
;;;  plot 15
;;;  set-current-plot-pen "7"
;;;  plot 7
;;;  set-current-plot-pen "4"
;;;  plot 4
;;;  set-current-plot-pen "1"
;;;  plot 1
;;;
;;;;
;
;;;;
;;;;;  set-current-plot "lactating and pregnant?"
;;;;;  ifelse any? turtles with [lactating? = 1 and impregnated? = 1]
;;;;;  [ set-current-plot-pen "yes"
;;;;;    plot 1
;;;;;    plot-pen-down
;;;;;    set-plot-pen-color 127
;;;;;    set-plot-pen-mode 1
;;;;;  ]
;;;;;  [ set-current-plot-pen "yes"
;;;;;    plot 0.5
;;;;;    plot-pen-up
;;;;;    set-plot-pen-color 7
;;;;;  ]
;;;
;;;;
end 

There are 2 versions of this model.

Uploaded by When Description Download
Merel Goedegebuure about 1 year ago Updated DEB-IBM to accommodate for implementation of scenarios (climate variability, yearling mortality, fecundity change, density dependence) affecting southern elephant seal population. Download this version
Merel Goedegebuure about 1 year ago Initial upload Download this version

Attached files

File Type Description Last updated
DEB-IBM for southern elephant seals .png preview Preview for 'DEB-IBM for southern elephant seals ' about 1 year ago, by Merel Goedegebuure Download

This model does not have any ancestors.

This model does not have any descendants.