TIMELY model

No preview image

1 collaborator

Img_0643 Pia Backmann (Author)

Tags

competition 

Tagged by Pia Backmann over 6 years ago

herbivory 

Tagged by Pia Backmann over 6 years ago

ibm 

Tagged by Pia Backmann over 6 years ago

inducible 

Tagged by Pia Backmann over 6 years ago

plant 

Tagged by Pia Backmann over 6 years ago

plant defense 

Tagged by Pia Backmann over 6 years ago

Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.0.4 • Viewed 299 times • Downloaded 14 times • Run 0 times
Download the 'TIMELY model' modelDownload this modelEmbed this model

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


COPYRIGHT AND LICENSE AND WHATEVER...

Copyright 2012 Uri Wilensky.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, US

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

: ) : ( : ) : ( : ) : ( : ) : ( :

Copyright Two-layer model: 2009-2012 Yue Lin. yue.lin.tud@gmail.com Copyright Time-delay model (

To reference this model in academic publications, we ask you to include these citations for the model itself and for the NetLogo software:

  • Lin Y, Huth F, Berger U, Grimm V (2013). The role of belowground competition and plastic biomass allocation in altering plant mass-density relationships. Oikos, xx:xxx-xxx

  • Wilensky, U (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

To get a full version pi model with R-extension for statistical analysis, please contact Yue Lin.

Comments and Questions

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

Click to Run Model

extensions [array table] ; for using arrays

globals
[
  ;-----------variables for spatial settings ---------------;
  y-offset
  weltweite
  weltbreite
  size-factor ;; depending on the grid size, take care of the sizes of individuals

 ;----------- variables for simulation time ----------------;
  steps
  number-last-step ;; for calculating mortality


  Path

  LARVA_INSERT_TIMES

  ;--------------  Larva-overall-variables ------------------;
 ; conversion-factor    ; the percentage of food a larva converts into body mass
  tolerance_larva       ; the defense level of host plant at which the larva leaves the plant (if mobile)
  B-larva-max           ; maximal biomass of a larva (mass at which larvae pupate in grams)
  sum-unique-visited-pupation    ; number of plants visited by larvae (uniques) which reached pupation stage
  sum-visited-pupation
  sum-unique-visited   ; number of all plants visited by larvae (uniques per larvae)
  sum-visited          ; number of  all plants visited by larvae
  pupation-counter     ; number of larvae which reached pupation
  pupation-age-sum     ; sum of ages of larvae in ticks
  switch-counter       ; number of switching actions
  defense-switch-count ; number of switches due to the high defense-level of host plant
  defense-switch-count-now ; number of switches due to the high defense-level of host plant
  ;--------------- plant overall variables ------------------;

  B_before_growth          ;; array of the plant's above biomass (state of the beginning of the time-step, use this as buffer for further defense-level calculations
  Feeding-this-step        ;; array of the amount of plant material eaten by all caterpillars on the plant
  Defense_produced_above   ;; array which stores the above-ground costs for defense of this curent time-step
 ; intrinsic-growth-rate   ;; a constant of growth function for plants, intrinsic growth rate of mass
  B0a_max                  ;; constant: ideal maximum biomass for shoot
  B0b_max                  ;; constant: ideal maximum biomass for root

  PLANT-TAUS               ;; array of the taus of plants
  numTaus                  ;; how many taus do we have
  defense_max
  LARVENPFLANZEN           ;; all plants which have larvae from the beginning on




  ;------------------- for plots, sensitivity, GA etc. ----------------------------;
  overall-mean-damage-tau1
  overall-mean-damage-tau2
  current_number_tau1
  current_number_tau2
  sum-tau1
  sum-tau2
  sum-above-tau1
  sum-above-tau2
  sum-residience-tau1
  sum-residience-tau2
  sum-number-plants-tau1    ;; number of plants with genotype tau1 which are currently alive
  sum-number-plants-tau2    ;; number of plants with genotype tau2 which are currently alive
  sum_damage_tau1           ;; damage caused by feeding larvae on all plants of genotype tau1 for the current time-step
  sum_damage_tau2           ;; damage caused by feeding larvae on all plants of genotype tau2 for the current time-step
  all_sum_damage_tau1       ;; sum of damage caused by feeding larvae on all plants of genotype tau1 over all time-steps
  all_sum_damage_tau2       ;; sum of damage caused by feeding larvae on all plants of genotype tau2 over all time-steps
  all-sum-above-plants      ;; above-ground biomass of all plants over all time-steps (sums up)
  sum-number-plants         ;; count of all steps in all time steps (sums up with each step)
  sum-above-plants          ;; above-ground biomasse aller pflanzen (current tick)
  sum-below-plants          ;; below-ground biomasse aller pflanzen (current tick)
  sum-plants                ;; gesamt-biomasse aller pflanzen (current tick)
    ; -> daraus dann: productivity -> sum-above-plants auch intereessant std and mean für ergebnisse
  mean-above-plants         ;; Mean plant above-ground mass current time-step
  mean-below-plants         ;; Mean plant below-ground mass current time-step
  mean-plants               ;; Mean plant mass of all plants (whole plant) current time-step
  std-above-plants          ;; standard deviation of plant above-ground mass current time-step
  std-below-plants          ;; standard deviation of plant below-ground mass current time-step
  std-plants                ;; standard deviation of plant mass of all plants (whole plant) current time-step

  ;---------------- for calculating mortality ---------------;
  number-alive-plants
  number-dead-plants
  sum-number-dead-plants
  larval-death-number           ;; number of larvas that died

  sum-residience-time           ;; how long did the larvae stay on the plants
  sum-induced-plants       ;;
  sum-damage-plants             ;; damage caused by feeding larvae on all plants for the current time-step
  all-sum-damage-plants         ;; sum of damage caused by feeding larvae on all plants over all time-steps
  ;;;;   reporters: ;;;;
  ;overall-mean-damage-plants    ;; prozentualer anteil schaden von gesamtbiomasse die insgesamt produziert wurde
  ;mean-infestation-rate-plants  ;; mean über alle ticks  (count alle pflanzen mit raupe drauf / count alle pflanzen)



  ;; what to minimize, what to maximize
  ; productivity                    ;; absolute number, maximize this or: minimize:  sum( max possible biomass) - productivity
  ; all-sum-damage-plants           ;; absolute, to be minimized
  ; overall-mean-damage-plants      ;; to be minimized, in %
  ; mean-infestation-rate-plants    ;; in %, to be minimized
]

  breed [plants plant]
  breed [larvae larva]


plants-own
[
  A_ZOI_above        ;; area of above-ground ZOI
  A_ZOI_below        ;; area of below-ground ZOI
  rad-below          ;; below-ground radius
  rad-above          ;; above-ground radius

  C_a                ;; Index for Above-ground Competition
  C_b                ;; Index for Below-ground Competition


  B_max              ;; current, realistic maximum biomass for whole plant(asymptotic biomass)
  B                  ;; current body biomass in grams
  B-above            ;; current biomass of above-ground
  B-below            ;; current biomass of below-ground

  delta_gr_above     ;; above-ground growth rate of current time-step
  delta_gr_below     ;; below-ground growth rate of current time-step
  delta_gr           ;; current growth rate of whole plant

  r-s-ratio          ;; root/shoot ratio
  plant-dead         ;; plants with gr of 0 will have this value true
  gr-by-above        ;; growth determined by above-ground


  defense-fraction   ;; fraction of biomass which is allocated for defense, if plant is induced
  delta_defense_a    ;; defense compounds produced in the current time-step for above-ground
  delta_defense_b    ;; defense compounds produced in the current time-step for below-ground
  defense-level      ;; the current level of defense in plant (max: 0.24)
  MEMORY             ;; array of the masses of larvae for each time-step  ("Memory" of the plant)
  damage-sum         ;; sum of all damage received by feeding larvae over all time-steps (in mg)
  above-mass-produced;; sum of all above-biomass-produced over all time steps (in mg) before larval feeding

  tau                ;; defense-trait of plant
  current-tau        ;; can differ from  tau if priming modus is set "on"
  tau-type           ;; 1 or 2 (for GA = 3 modus)
  primed             ;; 0 = no current priming, 1 = priming
]

larvae-own
 [
   age               ;; age in days
   mobile?           ;; boolean, true, when larva can switch plants (depends on age and biomass)
   switch            ;; is the larva currently moving between plants?
   host              ;; number of the host plant (integer): from 0 to max(plants)
   biomass-larva     ;; biomass in mg (float)
   dispersal_radius  ;; distance which larva can walk to find a new plant
   last-movement-tick;; when did larvae last switch plants?
   distance-path     ;; distance of path to next host-plant
   VISITED           ;; list with numbers of plants visited
   plants-visited    ;; how many plants have been visited (same plant can be counted twice)
 ]

patches-own
[
  nb-compete-above    ;; above-ground sharing of competition
  nb-compete-below    ;; below-ground sharing of competition
]



;;;;;;;;;;;;;;;;;;;;;  plant-setting  ;;;;;;;;;;;;;;;;;;;;;
;;;;;;;         initialisation of plants            ;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to plant-setting

    set B random-normal 30000 3000 ; [mg]
    set B-above B / 2
    set B-below B / 2

    set rad-above ( B-above ^ ( 3 / 8 ) ) * ( PI ^ ( -1 / 2 ) )
    set A_ZOI_above B-above ^ ( 3 / 4 )
    set rad-below ( B-below ^ ( 3 / 8 ) ) * ( PI ^ ( -1 / 2 ) )
    set A_ZOI_below  B-below ^ ( 3 / 4 )

    ;---------------  larvae and defense -------------;
    if(level-GA = 1) ; if the GA works on individual basis -> the array of tau-values are optimised by GA
    [
       set tau array:item PLANT-TAUS who  ; funktioniert!
    ]
    if (level-GA = 2)
    [ ; else: if the GA works on population basis -> tau-range and tau-median are optimised
          set tau random-normal tau-median tau-range
          if tau < 0
          [
            let zufall random ( tau-range + 1)
            set tau zufall
          ]
    ]
    if (level-GA = 3)
    [
         set tau tau1
         set tau-type 1
         ; if random-float 1 > 0.5  ; mit dieser Einstellung sind die Taus nicht genau gleichmäßig verteilt
         ; (ein Genotyp kann zufällig häufiger bzw. weniger häufig sein. Wird durch viele Simulationen aber ausgegelichen)
         ; [
         ;   set tau tau2
         ;]
         let number-tau1 count plants with [tau-type = 1]
         if number-tau1 >  density * frequency / 100
         [
           set tau tau2
           set tau-type 2
         ]
    ]
    set current-tau tau
    set defense-fraction defense_fraction_all
    set defense-level initial_defense_level; eigentlich 0.0
    set damage-sum 0        ;;  (in mg)
    set above-mass-produced B-above ;; (in mg)
    set primed 0

   ; set damage 0

    set MEMORY array:from-list n-values ( simulength + current-tau ) [0] ; initialisiere array der länge "simulation length" (+ tau, da es in der Vergangeneit startet)
    ;; initial gefüllt mit Nullen, hier werdn dann die Larvenmassen pro step eingetragen, quasi als "Gedächnis" der Pflanze, ob ihr Schaden zugefügt wurde
    set label current-tau
    set label-color black
    set shape "circle"
    ;------------------------------------------------;
    set plant-dead false

    ifelse display-below-ground
      [
        set size rad-below * size-factor * 2
        set color scale-color gray  size (size-factor * 1.0) (size-factor * 7.0)
      ]
      [
        set size rad-above * size-factor * 2
        set color scale-color lime size (size-factor * 3) ( size-factor * 80)
       ; set color scale-color lime  size (size-factor * 0.1) (size-factor * 3.5)
      ]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  larva-setting  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                                          ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to larva-setting
   set switch 0               ;; 0: larvae is currently NOT moving between plants, 1: larva is moving between plants
   set plants-visited 1
   set age 0                  ;; age in days
   set distance-path 0
   set mobile? false          ;; boolean, true, when larva can switch plants (depends on age and biomass)
   set host -1                ;; number of the host plant (integer)
   set last-movement-tick 0   ;; time of last inter-plant movement
   set biomass-larva 1        ;; biomass in mg
   set size 15
   set label " ";biomass-larva
   set dispersal_radius dispersal_radius_larvae  * 100 ; in cm, 1 = 100 cm
   set shape "bug"
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   larva-wave     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to larva-wave
  let current_number_larvae 0
  while [ current_number_larvae < wave-larvae ]
  [
    set current_number_larvae current_number_larvae + 1
    let host-plant one-of plants
    if  any? plants with  [ array:item MEMORY (ticks + current-tau) = 0 ]  ;; zuerst immer auf die aktuell nicht-befallenen Pflanzen setzen
    [
      set host-plant one-of plants with [ array:item MEMORY (ticks + current-tau) = 0] ;; only one larvae per plant -> if larvae time >= 0: plant already infested
    ]
    if [plant-dead] of host-plant = true
    [
      print("i wanted to create a larva on a dead plant")
    ]
    if host-plant = nobody
    [
      print("i wanted to create a larva on a nonexisting plant")
    ]
    create-larvae 1
    [
      larva-setting
      setxy  [ xcor ] of host-plant [ ycor ] of host-plant;; noch überlegen ob ich genau 50-50 auf die beiden genotypen verteile, oder ob es sich statistisch schnell rausmittelt
      set host [ who ] of host-plant
      pen-down
      set pen-size 3
      set VISITED (list host) ; setze die aktuelle Pflanzennummer in visit-array ein
    ]
    ask host-plant
    [
      array:set MEMORY (ticks + current-tau) (sum [biomass-larva] of larvae-here) ; setze die Masse der Larve in Feld des Arrays mit aktuellen Zeitpunkt ein
    ]
  ]
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;              setup             ;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup

  file-close-all
  if(local = true)
  [
    clear-all
  ]
  random-seed randomseed
  reset-ticks

  if(local = true)
  [
    set numTaus 61
    set PLANT-TAUS array:from-list n-values (density) [1]
    let k 0 ; glaube, array fängt bei 0 an...
    while[k < density]
    [
      array:set PLANT-TAUS k random 61
      set k (k + 1)
    ]
  ]
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  set LARVA_INSERT_TIMES table:make
  let k 0
  while [k <= simulength]
  [
    table:put LARVA_INSERT_TIMES k 0
    set k (k + 1)
  ]

   print LARVA_INSERT_TIMES

  let i 0
  while [i < number_larvae]
  [
    let time random simulength
    let value table:get LARVA_INSERT_TIMES time
    table:put LARVA_INSERT_TIMES time (value + 1)
    set i (i + 1)
  ]
 print LARVA_INSERT_TIMES

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


  ;;;;;;;;;; Skales: temporal, spatial ;;;;;;;;;;;;;;;

  set weltweite 249
  set weltbreite 249
  resize-world 0 weltbreite 0 weltweite
  set-patch-size 2.3
  set size-factor (world-width / 800)

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  ask patches
  [ set pcolor white
    set nb-compete-above 0
    set nb-compete-below 0
  ]  create-plants density
  [
    plant-setting
    setxy random-xcor random-ycor
  ]
  ; ----------------- Globals (of larvae) setting ---------------------;
  set B-larva-max 10000
  set tolerance_larva defense-tolerance

  ;--------- Buffer for growth (at the beginning of time-step ---------;

  set B_before_growth array:from-list n-values density [0];

  ; ------------------- Current_defense_costs -------------------------;

  set Defense_produced_above array:from-list n-values density [0]; array with number-plants entrys, initialize with zeros biomass after growth of plant but before larva-feeding

  ;-----------  Feeding amount of larvae on this plant per time step -----------;

  set Feeding-this-step   array:from-list n-values density [0]

 ; -------------------------------------------------------------------;

 ;.............   plant globals ......................................;
  set-default-shape plants "circle"
  set B0a_max 500000 ;[mg]  ; eigentlich beide 500000 mg
  set B0b_max 500000 ;[mg]

  if larva-waves = 1
  [
    distribute-larvae number_larvae
  ]
  if larva-waves = 2
  [
    distribute-larvae ( number_larvae / 2 )
  ]
  ;--------------- Global variables for plots ------------------;
  set sum_damage_tau1 0
  set sum_damage_tau2 0
  set overall-mean-damage-tau1 0
  set overall-mean-damage-tau2 0
  set sum-tau1 0
  set sum-tau2 0
  set sum-residience-tau1 0
  set sum-residience-tau2 0
  set sum-number-plants-tau1 0   ;; number of plants with genotype tau1 which are currently alive
  set sum-number-plants-tau2 0   ;; number of plants with genotype tau2 which are currently alive
  set sum_damage_tau1 0          ;; damage caused by feeding larvae on all plants of genotype tau1 for the current time-step
  set sum_damage_tau2 0          ;; damage caused by feeding larvae on all plants of genotype tau2 for the current time-step
  set all_sum_damage_tau1 0       ;; sum of damage caused by feeding larvae on all plants of genotype tau1 over all time-steps
  set all_sum_damage_tau2 0      ;; sum of damage caused by feeding larvae on all plants of genotype tau2 over all time-steps
  set sum-unique-visited-pupation 0   ; number of plants visited by larvae (uniques) which reached pupation stage
  set sum-visited-pupation 0
  set sum-unique-visited 0  ; number of all plants visited by larvae (uniques per larvae)
  set sum-visited  0        ; number of  all plants visited by larvae
  set pupation-counter 0    ; number of larvae which reached pupation

  set all-sum-above-plants 0
  set sum-number-plants 0              ;; count of all steps in all time steps (sums up with each step)
  set sum-above-plants 0               ;; above-ground biomasse aller pflanzen (current tick)
  set sum-below-plants 0               ;; below-ground biomasse aller pflanzen (current tick)
  set sum-plants 0                     ;; gesamt-biomasse aller pflanzen (current tick)
    ; -> daraus dann: productivity -> sum-above-plants auch intereessant std and mean für ergebnisse
  set mean-above-plants 0              ;; Mean plant above-ground mass current time-step
  set mean-below-plants 0              ;; Mean plant below-ground mass current time-step
  set mean-plants 0                    ;; Mean plant mass of all plants (whole plant) current time-step
  set std-above-plants 0               ;; standard deviation of plant above-ground mass current time-step
  set std-below-plants 0               ;; standard deviation of plant below-ground mass current time-step
  set std-plants 0                     ;; standard deviation of plant mass of all plants (whole plant) current time-step
  set sum-induced-plants 0             ;; number of plants which are induced (sums up over time)
  set  switch-counter 0                ;; number of switching actions
  set defense-switch-count 0
  set defense-switch-count-now 0
  set defense_max 0.3

   ;---------------- for calculating mortality ---------------;
  set number-alive-plants 0
  set number-dead-plants 0
  set sum-number-dead-plants 0
  set larval-death-number 0            ;; number of larvas that died

  set sum-residience-time 0            ;; how long did the larvae stay on the plants
  set sum-damage-plants 0              ;; damage caused by feeding larvae on all plants for the current time-step
  set all-sum-damage-plants 0          ;; sum of damage caused by feeding larvae on all plants over all time-steps
 ; set mean-infestation-rate-plants 0   ;; mean über alle ticks  (count alle pflanzen mit raupe drauf / count alle pflanzen)
 ; set overall-mean-damage-plants 0     ;; prozentualer anteil schaden von gesamtbiomasse die insgesamt produziert wurde
  ;; what to minimize, what to maximize
  ; productivity                    ;; absolute number, maximize this or: minimize:  sum( max possible biomass) - productivity
  ; all-sum-damage-plants           ;; absolute, to be minimized
  ; overall-mean-damage-plants      ;; to be minimized, in %
  ; mean-infestation-rate-plants    ;; in %, to be minimized

     ask larvae
     [
       let xround precision xcor 3
       let yround precision ycor 3
   ;    file-print (word  who " "  xround  " "  yround )
     ]
  ;   let filestring (word "/home/pb55xara/Desktop/Time_delay/NEU/neu_data/larval_masses.txt")
   ; file-open  filestring;; Opening file for writing
  ;  file-print "plant.nr x.coord y.coord"
  ;file-print  "tick number mass defense mass.plant"
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   runtime   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go
  tick
  set defense-switch-count-now 0
  if not any? plants [ stop ]
  make-updates ; update variables, counters...
  make-plots
  ; setze counter auf null
  set sum-damage-plants 0 ; wird in consumed funktion gesetzt
  set number-dead-plants 0 ;wird in decompose gesetzt

  if ticks = simulength
  [
    ;file-close
    stop
  ]
  if priming = true  ;; priming routine: reduce delay time of all plants in radius of induced plants by half
  [
    ask plants with [plant-dead = false]
    [
      set current-tau  tau
      if any? plants in-radius priming-radius with [color = yellow]
      [
        set current-tau floor ( tau / 2 )
      ]
    ]
  ]
;;;;;;;;; larval wave?;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;2                                                                                                     ;;
  if larva-waves = 2 and ticks = wave-delay
  [
    larva-wave
  ]
  if larva-waves = "continuous"
  [
     let larvae-tick table:get LARVA_INSERT_TIMES ticks
     distribute-larvae larvae-tick
  ]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  set Feeding-this-step   array:from-list n-values density [0]
  ask larvae [ set age (age + 1) ]
  ask plants
  [
    array:set MEMORY (ticks + tau) ( sum [biomass-larva] of larvae-here )
    array:set B_before_growth who B-above
  ]
  ;; growth and death of plants
  ask plants with [plant-dead = false]
    [
      calculate-sizes-of-ZOI
    ]
    calculate-competition-indices
    ask plants with [plant-dead = false]
    [
      potential-plant-growth       ;; potential plant growth considering competition and resource scarceness
      plant-defense-production
      smallest-compartment-defines-all
      self-restriction-of-plant-growth
      allometric-growth-adjustment
    ]
  ;;; growth, movement and death of larvae
  ask larvae
  [
    larval-growth
    update-mobile
    pupation?
    larva-choose-new-host-plant
    larval_death
  ]


    ; Hier: Calculate defense level of plant, using the calculated amount of produced defense of this plant during this current time-step
  ask plants with [plant-dead = false]
  [
    ;let current_costs array:item Current_defense_costs who ; the above-ground biomass gained during this time step
    calculate-plant-defense-level array:item Defense_produced_above who
  ]
  ask plants with [plant-dead = true]
  [
    decompose
  ]

  ask plants
  [
     ifelse display-below-ground
     [set size rad-below * size-factor * 2]
     [set size rad-above * size-factor * 2]
  ]
  set steps steps + 1
end   ; of to go
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  distribute_larvae  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     ;;                                                                                                      ;;
     ;;  observer-procedure:                                                                                 ;;
     ;;                                                                                                      ;;
     ;;Calculates the current area of both, the plant's above- and belowground ZOI.
     ;;
     ;; Input Parameter: plant identity (plant p)
     ;; Returns: ZOI_p(above) and ZOI_p(below)
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to distribute-larvae [putnumlarvae]
  ;; Create and distribute larvae randomly on plants
    set current_number_tau1 0
    set current_number_tau2 0
    let current_number_larvae 0
    while [ current_number_larvae < putnumlarvae ]
    [
      set current_number_larvae current_number_larvae + 1
      let host-plant one-of plants
      if  any? plants with  [ array:item MEMORY (ticks + current-tau) = 0 ]  ;; zuerst immer auf die aktuell nicht-befallenen Pflanzen setzen
      [
        set host-plant one-of plants with [ array:item MEMORY (ticks + current-tau) = 0] ;; only one larvae per plant -> if larvae time >= 0: plant already infested
      ]
      if(level-GA = 3)
      [
        ifelse [tau-type] of host-plant = 1
        [
          set current_number_tau1 current_number_tau1 + 1
        ]
        [
          set current_number_tau2 current_number_tau2 + 1
        ]
      ]
      create-larvae 1
      [
        larva-setting
        setxy  [ xcor ] of host-plant [ ycor ] of host-plant;; noch überlegen ob ich genau 50-50 auf die beiden genotypen verteile, oder ob es sich statistisch schnell rausmittelt
        set host [ who ] of host-plant
        set VISITED (list host) ; setze die aktuelle Pflanzennummer in visit-array ein
        pen-down
        set pen-size 3
        ask patches in-radius dispersal_radius  [set pcolor blue]
      ]
      ask host-plant
      [
        array:set MEMORY (ticks + current-tau) (sum [biomass-larva] of larvae-here) ; setze die Masse der Larve in Feld des Arrays mit aktuellen Zeitpunkt ein
      ]
    ]
end 




     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  calculate-sizes-of-ZOI  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     ;;                                                                                                      ;;
     ;;  observer-procedure:                                                                                 ;;
     ;;                                                                                                      ;;
     ;;Calculates the current area of both, the plant's above- and belowground ZOI.
     ;;
     ;; Input Parameter: plant identity (plant p)
     ;; Returns: ZOI_p(above) and ZOI_p(below)
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-sizes-of-ZOI

      set A_ZOI_above B-above ^ ( 3 / 4 )
      set A_ZOI_below B-below ^ ( 3 / 4 )
      set rad-above ( B-above ^ ( 3 / 8 ) ) * ( PI ^ ( -1 / 2 ) )
      set rad-below ( B-below ^ ( 3 / 8 ) ) * ( PI ^ ( -1 / 2 ) )
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  calculate-competition-indices  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                                                      ;;
;;  observer-procedure:                                                                                 ;;
;;                                                                                                      ;;
;;  Calculates for both compartments of plant p (root and shoot) the current competition indices,       ;;
;;  C_a and C_b. C_a and C_a are defined as average of the plant's share of the available resources     ;;
;;  over all patches within the plant's ZOI (above- or belowground)                                     ;;
;;                                                                                                      ;;
;;  It distinguishes between different types of competition which can be chosen manually
;;  at the beginning of the simulation (in the interface):                                              ;;
;;  "off", "complete symmetry", "size symmetry", "allometric size asymmetry".                           ;;
;;
;;  Used local variables:
;;
;;  - nb-compete-above/nb-compete-below:
;;             index which determines, how many (and depending on chosen competition mode to            ;;
;;             which proportion) plants are sharing the same patch.                                     ;;
;;                                                                                                      ;;
;;  - number_patches_ZOI_above (or below):                                                              ;;
;;              sum of all patches within the current plant's ZOI (above or belowground)                ;;
;;
;;   Returns:
;;
;;  - C_a/C_b:                                                                                          ;;
;;           competition index of above- and belowground compartment                                    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-competition-indices

  ;;-------------------------- types of above-ground competition ----------------------------------;;
  ask plants
  [
      let num_patches count patches in-radius (rad-above * size-factor)
      if ( num_patches <= 0)
      [
        set plant-dead true
      ]
  ]
    if above-competition = "off"
    [
      ask plants with [plant-dead = false] [ set C_a 1]
    ]

    if above-competition = "complete symmetry"
    [
      ask patches [ set nb-compete-above 0 ]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-above * size-factor) [ set nb-compete-above nb-compete-above + 1 ]
      ]
      ask patches with [nb-compete-above > 0]
      [
        set nb-compete-above 1 / nb-compete-above  ; the plant's share of the resources of current patch
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_above (count patches in-radius (rad-above * size-factor) )
        let nbsh-compete-above  (sum ([nb-compete-above] of patches in-radius (rad-above * size-factor)))
        set C_a nbsh-compete-above / number_patches_ZOI_above
      ]
    ]

    if above-competition = "allometric symmetry"
    [
      ask patches [set nb-compete-above 0]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-above * size-factor) [ set nb-compete-above nb-compete-above + [B-above ^ (3 / 4)] of myself ]
      ]
      ask patches with [nb-compete-above > 0]
      [
        set nb-compete-above 1 / nb-compete-above  ; the plant's share of the resources of current patch
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_above (count patches in-radius (rad-above * size-factor) )
        let nbsh-compete-above  (sum ([nb-compete-above * [B-above ^ (3 / 4)] of myself] of patches in-radius (rad-above * size-factor)))
        set C_a nbsh-compete-above / number_patches_ZOI_above
      ]
    ]

    if above-competition = "size symmetry"
    [
      ask patches [set nb-compete-above 0]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-above * size-factor) [ set nb-compete-above nb-compete-above + [B-above] of myself ]
      ]
      ask patches with [nb-compete-above > 0]
      [
        set nb-compete-above 1 / nb-compete-above
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_above (count patches in-radius (rad-above * size-factor) )
        let nbsh-compete-above  (sum ([nb-compete-above * [B-above] of myself] of patches in-radius (rad-above * size-factor)))
        set C_a nbsh-compete-above / number_patches_ZOI_above

      ]
    ]

    if above-competition = "allometric asymmetry"
    [
      ask patches [set nb-compete-above 0]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-above * size-factor) [ set nb-compete-above nb-compete-above + [B-above ^ 10] of myself ]
      ]
      ask patches with [nb-compete-above > 0]
      [
        set nb-compete-above 1 / nb-compete-above
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_above (count patches in-radius (rad-above * size-factor) )
        let nbsh-compete-above  (sum ([nb-compete-above * [B-above ^ 10] of myself] of patches in-radius (rad-above * size-factor)))
        set C_a nbsh-compete-above / number_patches_ZOI_above
      ]
    ]

    if above-competition = "complete asymmetry"
    [
      ask patches [set nb-compete-above 0]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-above * size-factor)
        [
          ifelse nb-compete-above = 0 [ set nb-compete-above [who] of myself ]
          [
            ifelse ( [B-above] of plant ( nb-compete-above ) ) > [B-above] of myself [ ] ;do nothing
            [
              set nb-compete-above [who] of myself ]
          ]
        ]
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_above (count patches in-radius ( rad-above * size-factor ) )
        let nbsh-compete-above  (count patches in-radius ( rad-above * size-factor ) with [nb-compete-above = [who] of myself] )
        set C_a nbsh-compete-above / number_patches_ZOI_above
      ]
    ]

    ;; type of below-ground competition
    if below-competition = "off"
    [
      ask plants with [plant-dead = false] [ set C_b 1]
    ]

    if below-competition = "complete symmetry"
    [
      ask patches [set nb-compete-below 0]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-below * size-factor)
        [
          set nb-compete-below nb-compete-below + 1
        ]
      ]
      ask patches with [nb-compete-below > 0]
      [
        set nb-compete-below 1 / nb-compete-below
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_below (count patches in-radius (rad-below * size-factor) )
        let nbsh-compete-below  (sum ([nb-compete-below] of patches in-radius (rad-below * size-factor)))
        set C_b nbsh-compete-below / number_patches_ZOI_below
      ]
    ]

    if below-competition = "allometric symmetry"
    [
      ask patches [set nb-compete-below 0]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-below * size-factor) [ set nb-compete-below nb-compete-below + [B-below ^ (3 / 4)] of myself ]
      ]
      ask patches with [nb-compete-below > 0]
      [
        set nb-compete-below 1 / nb-compete-below
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_below (count patches in-radius (rad-below * size-factor) )
        let nbsh-compete-below  (sum ([nb-compete-below * [B-below ^ (3 / 4)] of myself] of patches in-radius (rad-below * size-factor)))
        set C_b nbsh-compete-below / number_patches_ZOI_below
      ]
    ]

    if below-competition = "size symmetry"
    [
      ask patches [set nb-compete-below 0]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-below * size-factor) [ set nb-compete-below nb-compete-below + [B-below] of myself ]
      ]
      ask patches with [nb-compete-below > 0]
      [
        set nb-compete-below 1 / nb-compete-below
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_below (count patches in-radius (rad-below * size-factor) )
        let nbsh-compete-below  (sum ([nb-compete-below * [B-below] of myself] of patches in-radius (rad-below * size-factor)))
        set C_b nbsh-compete-below / number_patches_ZOI_below
      ]
    ]

    if below-competition = "allometric asymmetry"
    [
      ask patches [set nb-compete-below 0]
      ask plants with [plant-dead = false]
      [
        ask patches in-radius (rad-below * size-factor) [ set nb-compete-below nb-compete-below + [B-below ^ 10] of myself ]
      ]
      ask patches with [nb-compete-below > 0]
      [
        set nb-compete-below 1 / nb-compete-below
      ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_below (count patches in-radius (rad-below * size-factor) )
        let nbsh-compete-below  (sum ([nb-compete-below * [B-below ^ 10] of myself] of patches in-radius (rad-below * size-factor)))
        set C_b nbsh-compete-below / number_patches_ZOI_below
      ]
    ]

    if below-competition = "complete asymmetry"
    [
      ask patches [set nb-compete-below 0]
      ask plants [ ask patches in-radius (rad-below * size-factor)
        [ ifelse nb-compete-below = 0
          [ set nb-compete-below [who] of myself ]
          [ ifelse ( [B-below] of plant ( nb-compete-below ) ) > [B-below] of myself
            [ ] ;do nothing
            [ set nb-compete-below [who] of myself ] ] ] ]
      ask plants with [plant-dead = false]
      [
        let number_patches_ZOI_below (count patches in-radius ( rad-below * size-factor ) )
        let nbsh-compete-below  (count patches in-radius ( rad-below * size-factor ) with [nb-compete-below = [who] of myself] )
        set C_b nbsh-compete-below / number_patches_ZOI_below
      ]
    ]
end  ; of calculate-competition-indices



    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;    potential-plant-growth   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;                                                                                                  ;;
    ;; a plant (turtle) procedure                                                                       ;;
    ;;                                                                                                  ;;
    ;; calculates the potential growth of plant p at the current time. Potential growth means, that     ;;
    ;; resources available and plant-plant competition are considered, larval damage,                   ;;
    ;; defense allocation and growth plasticity are not included here.                                  ;;
    ;;                                                                                                  ;;
    ;; Variables:                                                                                       ;;
    ;;    - delta_gr_above/below:
    ;;         the gain/loss of biomass (above or belowground) for current time-step                    ;;
    ;;    - R_a / R_b:                                                                                  ;;
    ;;          resource limitation above/below                                                         ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to potential-plant-growth

  let R_a 1 - resource-limitation-above
  let R_b 1 - resource-limitation-below

  ifelse R_a <= 0 or R_b <= 0
  [
      set plant-dead true ; plant dies if no resources available
  ]
  [
    set delta_gr_above A_ZOI_above * R_a * C_a * intrinsic-growth-rate
    set delta_gr_below A_ZOI_below * R_b * C_b * intrinsic-growth-rate
  ]
end  ; of potential-plant-growth





    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   plant-defense-production    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;                                                                                                      ;;
    ;; A turtle-procedure (plants):
    ;;                                                                                                      ;;
    ;; calculates the amount of defense produced for the shoot and the root compartment                     ;;
    ;; and incorporates the energy required to produce the defense compounds into the growth equations      ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to plant-defense-production

  set delta_defense_a defense-costs delta_gr_above
  set delta_defense_b defense-costs delta_gr_below

  ;; include defense into growth equations:
  set delta_gr_above delta_gr_above  - delta_defense_a
  set delta_gr_below delta_gr_below - delta_defense_b

 array:set Defense_produced_above  who delta_defense_a
end  ; of plant-defense-production



;;;;;;;;;;;;;;;;;;;;;; to-report defense-costs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                             ;;
;;A plant procedure (turtle):                                                  ;;
;;                                                                             ;;
;;  calculates the costs of a plant to allocate defenses                       ;;
;;  when defense production is currently induced, else: defense = 0            ;;
;;  defense is currently induced                                               ;;
;;  + sets plant colour "yellow" if currently induced                          ;;
;;                                                                             ;;
;;   Receives:                                                                 ;;
;,           - delta_biomass (gain in biomass)                                 ;;
;;   Returns:                                                                  ;;
;;           - defense-costs                                                   ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to-report defense-costs[ delta_biomass ]

  let costs 0.0
  set color green
  if(array:item MEMORY ticks > 0)                  ;; Is the induced defense currently activated (did a larva attack in time step (t-tau)?
                                                   ;; ticks - tau (array ist um tau Schritte nach hinten -> Richtung Vergangenheit verschoben)
  [

    set color yellow                               ;; induced plants can be recognized
    set costs defense-fraction * delta_biomass
    if delta_biomass < 0
    [
      set costs 0
    ]
    if defense-level >= defense_max  ; is the maximum of defense reached? -< then stop producing defense!
    [
      set color orange
      set costs 0
    ]
    set sum-induced-plants sum-induced-plants + 1
  ]
   report costs
end 


    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   smallest-compartment-defines-all   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;                                                                                                   ;;
    ;; A turtle-procedure (plants):                                                                      ;;
    ;;                                                                                                   ;;
    ;; The growth rate is restricted to the growth capacity of the smallest compartment                  ;;
    ;;
    ;; Variables:                                                                                        ;;
    ;;
    ;;    - delta_gr:                                                                                    ;;
    ;;             biomass produced this time-step for whole plant                                       ;;
    ;;    - delta_gr_above/below:                                                                        ;;
    ;;             biomass produced above/belowground (current time step)                                ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to smallest-compartment-defines-all

    ifelse (delta_gr_above < delta_gr_below)
    [
       set delta_gr  2 * delta_gr_above
    ]
    [
       set  delta_gr  2 * delta_gr_below
    ]
end   ; of smallest-compartment-defines-all



    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   self-restriction-of-plant-growth   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;                                                                                                   ;;
    ;; A turtle-procedure (plants):                                                                      ;;
    ;;                                                                                                   ;;
    ;; Calculates self-limiting growth term to ensure that the maximal biomass a plant can               ;;
    ;; maintain is not exceeded.                                                                         ;;
    ;;
    ;; Variables:                                                                                        ;;
    ;;
    ;;    - B0a_max/B0b_max:                                                                             ;;
    ;;             constant giving the maximal biomass (above- and below) a plant can attain under       ;;
    ;;             perfect conditions (no resource limitations, no competition).                         ;;
    ;;    - B_max:                                                                                       ;;
    ;;             the maximal size, the plant can attain under current conditions                       ;;
    ;;    - B:                                                                                           ;;
    ;;             the current biomass (above + belowground) of the plant                                ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to self-restriction-of-plant-growth

    let R_a 1 - resource-limitation-above
    let R_b 1 - resource-limitation-below
    set B_max B0a_max * (R_a * C_a) ^ 4 + B0b_max * (R_b * C_b) ^ 4
    ifelse delta_gr < 0
    [  ; for negative growth: set growth equal to 0
           ; no growth -> gr, gr_above, gr-below = 0
       set B B + 0
    ]
    [
       set B B + delta_gr
    ]
    ifelse ( B_max > 0) and (B_max > B)
    [
      set delta_gr_above delta_gr_above * ( 1 - (B / B_max) ^ 0.25)
      set delta_gr_below delta_gr_below * ( 1 - (B / B_max) ^ 0.25)
    ]
    [
      set delta_gr_above 0.0000000000001
      set delta_gr_below 0.0000000000001
    ]
end   ; of self-restriction-of-plant-growth



    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   allometric-growth-adjustment   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;                                                                                                   ;;
    ;; A turtle-procedure (plants):                                                                      ;;
    ;;                                                                                                   ;;
    ;;  growth plasticity:                                                                               ;;
    ;; "harmonizes" the relation of both compartments by above-and belowground growth                    ;;
    ;;                                                                                                   ;;
    ;; Variables:                                                                                        ;;
    ;;
    ;;    - B:                    total biomass of plant (root and shoot)                                ;;
    ;;                                                                                                   ;;
    ;;    - rad-below/above:      radius of ZOIs with updated biomasses                                  ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to allometric-growth-adjustment

      ;; growth, allocation and death
      ifelse delta_gr < 0
        [  ; for negative growth: set growth equal to 0
           ; no growth -> gr, gr_above, gr-below = 0
          set B B + 0
          set B-above B-above + 0
          set rad-above ( B-above ^ ( 3 / 8 ) ) * ( PI ^ ( -1 / 2 ) )
          set B-below B-below + 0
          set rad-below ( B-below ^ ( 3 / 8 ) ) * ( PI ^ ( -1 / 2 ) )

        ]
        [
           ; calculate growth of entire plant and "harmonize" the relation of both compartments by above- and belowground growth
          set B-above (B-above + (delta_gr * (delta_gr_below ^ (3 / 4)) / (delta_gr_above ^ (3 / 4) + delta_gr_below ^ (3 / 4))))
          set rad-above ( B-above ^ ( 3 / 8 ) ) * ( PI ^ ( -1 / 2 ) )

          set B-below (B-below + (delta_gr * (delta_gr_above ^ (3 / 4)) / (delta_gr_above ^ (3 / 4) + delta_gr_below ^ (3 / 4))))
          set rad-below ( B-below ^ ( 3 / 8 ) ) * ( PI ^ ( -1 / 2 ) )

          set B B-above + B-below
        ]

      set r-s-ratio (B-below / B-above)
      ; calculate gain in produced mass during this step
      let biomass_before array:item B_before_growth who
      let difference B-above - biomass_before
      set above-mass-produced above-mass-produced + difference
     ; if (who = 100)[ print (word "tick " ticks " plant 100 biomass produced " difference " biomass before " biomass_before " biomass now " B-above)]
end   ; of allometric-growth-adjustment


 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;    larval-growth    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;                                                                                             ;;
 ;; A turtle-procedure (larva):
 ;;      Calculates the change in biomass of the larva. Growth depends on the abundance and     ;;
 ;;      quality, thus defense-level of the current host plant and the larva's current size.    ;;
 ;;      Growth parameters are adapted to field-work data.                                      ;;
 ;;                                                                                             ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to larval-growth

  let possible-biomass-larva 0
  let consumed 0
  let age_nodef 0
  let age_def 0
  let defense 0
  let costs 0 ; momentan 0, könnte aber noch auf reelle Bewegungs und Stoffwechselkosten der Larven gesetzt werden.
  let biomass-before biomass-larva
;  print (word "tick = " ticks " biomass larva = " biomass-larva)
  if(biomass-before < 0)
  [
    set biomass-before 0
  ]
  ifelse switch = 0 ; does larva have currently a host plant
  [
	    ; let larva grow prop. to consumed biomass

	    let age_old_def (biomass-before / exp(-6.329)) ^ (1 / 4.661)
	    let age_old_nodef (biomass-before / exp(-8.355)) ^ (1 / 5.856)
	    set age_def age_old_def + 1 / 6
	    set age_nodef age_old_nodef + 1 / 6
    ;  print (word "tick " ticks " age_Def " (age_def * 6) " age_NoDef " (age_nodef * 6) " mass_old "biomass-before )
	    set defense [defense-level] of plant host
	    let defenseless_factor ((0.2 - [defense-level] of plant host) / 0.2 )
	    if( defenseless_factor < 0 )
	    [
	      set defenseless_factor  0.0
	    ]
	    let wt_factor (([defense-level] of plant host) / 0.2 )
	    if( wt_factor > 1 )
	    [
	      set wt_factor 1.0
	    ]
	    set possible-biomass-larva defenseless_factor * exp(-8.355) * age_nodef ^ 5.856 + wt_factor * exp(-6.329) * age_def ^ 4.661
	    ;; calculate Larval damage
	    set consumed ( possible-biomass-larva - biomass-before ) / conversion-factor
    ;  print(word "biomass larva = " biomass-before "  consumed = " consumed )
	    ifelse consumed > ([B-above] of plant host);check if plant will be entirely consumed
	    [
	      ifelse [B-above] of plant host < 0
	      [
  	    	  set consumed 0
	      ]
	      [
  	 	      set consumed [B-above] of plant host
	      ]
	      ask plant host ; in both cases the plant is dead (eaten by larva)
	      [
		        set plant-dead true
	      ]
	      ask larvae-here
	      [
		        set switch 1
	      ]
	    ]; end of consumed > biomass
	    [ ; if consumed < biomass-above of plant
	      ask plant host
		    [
  		    set B-above B-above - consumed  ;; complete growth equation of plant
  		    set B B - consumed
          if B-above < death-threshold
          [
            set plant-dead true
            ask larvae-here
            [

              set switch 1
            ]
          ]

		    ]
	    ] ; end of consumed > biomass-above of plant

	  ;; verschoben von außerhalb der switch=0 ifelse abfrage nach innen, da nur wenn switch=0 ein host vorhanden und deswegen host kram gemacht werden muss
	    set biomass-larva biomass-before + consumed * conversion-factor
	    set sum-damage-plants sum-damage-plants + consumed
      if (level-GA = 3)
      [
           ifelse ([ tau-type ] of plant host = 1)
           [
               set sum_damage_tau1 sum_damage_tau1 + consumed
           ]
           [ ; host = tau2
               set sum_damage_tau2  sum_damage_tau2 + consumed
           ] ; end of tau2
      ;print (word "plant number " host " larva " who " consumed " consumed " sum-damage " sum-damage-plants " tick " ticks)
      ]
	    ;; store the amount of consumed plant material of ALL Larvae currently feeding on the host plant in one array:
	    let in-array array:item Feeding-this-step host ; array of the plant: stores for each time step how much has been consumed by all larvae on this current plant (adds up)
	    array:set   Feeding-this-step  host ( consumed + in-array )
  ] ; end of switch = 0
  [
    	set consumed 0
  ] ; end of switch = -1
   let defense-word "NA"
   let masse "NA"
   if( switch = 0)
   [
       set defense-word ([defense-level] of plant host)
       set masse ([B-above] of plant host)
   ]
  ;file-print (word ticks " " who " " biomass-larva " " defense-word " " masse)
end   ; of larval-growth



     ;;;;;;;;;;;;;;;;;;;;;  update-mobile  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     ;;                                                                    ;;
     ;; a turtle procedure (larvae)                                        ;;
     ;; checks, whether larval mass and age are high enough to             ;;
     ;; set the larva as "mobile" -> so that it can switch plants          ;;
     ;;                                                                    ;;
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to update-mobile
  if biomass-larva > 400 and age > 8 * 6 and switch? = true ; mass: 3. instar-mass minimum: 35 mg, age: 5 days in ticks
  [
    set mobile? true
  ]
end   ; of update-mobile


     ;;;;;;;;;;;;;;;;;;;;;;;;;  pupation?  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     ;;                                                                    ;;
     ;; a turtle procedure (larvae)                                        ;;
     ;; checks, whether larval mass is high enough to let the larva
     ;; go to pupate (it then disappears off the simulation)               ;;
     ;;                                                                    ;;
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to pupation?

  if biomass-larva > 10000
  [
    set sum-visited-pupation sum-visited-pupation + length VISITED
    set VISITED remove-duplicates VISITED
    set sum-unique-visited-pupation sum-unique-visited-pupation + length VISITED
    set pupation-counter pupation-counter + 1
    set pupation-age-sum  pupation-age-sum + ticks
    set sum-unique-visited sum-unique-visited  + length VISITED; number of all plants visited by larvae (uniques per larvae)
    set sum-visited sum-visited + plants-visited

    die ; larve verpuppt sich -> richtet somit auf Pflanzen keinen Schaden mehr an...
        ; print (word "Larve " who " hat sich verpuppt")
  ]
end   ; of pupation?




 ;;;;;;;;;;;;;;;;;;;;;;;;;;;    larva-choose-new-host-plant   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;                                                                                               ;;
 ;; A turtle-procedure (larva):
 ;;    All mobile larvae check if they leave the host plant (if defense-level > tolerance_larva)  ;;
 ;;    Elsewise the larva stays on the plant.                                                     ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to larva-choose-new-host-plant
    ; see whether plant is "unpalatable" for larva (too much defense)
    let costs 0
    ifelse ( switch = 0 )
    [

      if ( [defense-level] of plant host > tolerance_larva ) and ( mobile?  = true) and (last-movement-tick + 6 < ticks)
      [
        set switch 1
        set defense-switch-count defense-switch-count + 1
        set defense-switch-count-now defense-switch-count-now + 1
      ]

    ] ; end of section for larvae with host plant at the beginning of the tick
    [
      set biomass-larva biomass-larva - costs; hier kommen eventuell noch Kosten für Aktivitäten der Larve hinzu
      switch-plants ; wenn die Raupe keinen host hatte
   ]
end   ;; of larva-choose-new-host-plant


     ;;;;;;;;;;;;;;;;;;;;;;   switch-plants   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     ;;                                                                               ;;
     ;; a turtle procedure (larvae)                                                   ;;
     ;; The larva chooses  the host plant to visit next (acc. to dispersal radius).   ;;
     ;; For the moment: no costs for switching, later higher death prob.              ;;
     ;; duration of switching: 1 time step (4 hours)                                  ;;
     ;;                                                                               ;;
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to switch-plants
  set switch 0  ; switchen erledigt
  ifelse mobile? = true  ; nur mobile Larven wechseln Pflanzen
  [
    let new-host host
    if movement-modus = 1
    [
      ifelse any? plants in-radius dispersal_radius with [ who != new-host]
      [
      ;  print "switch"
        ;print(word "plants are in disp radius")
        while [ host = new-host ]
        [
          ; 1) erfasse alle Pflanzen innerhalb Raupenradius
          ; 2) Berechne die Distanzen zur Raupe
          ; 3) Berechne I_max
          ; 4) Berechne Intervallabschnitt pro Pflanze und tue diese als "value" in die Table, als "key" dann die Pflanzen nummer (who)
          ; 5) ziehe Zufallszahl (float) rand float Int_max und prüfe, in welches Intervall sie fällt -> dies ist die neue host plant...!

          ; 1: erfasse alle Pflanzen in Raupen-dispersal-radius und 2) berechne ihre Distanzen zur Raupe:

          let distance_all 0       ; sum of all distances
          let distance_plant 0     ; distance of the current plant from larva in center
          let distance_quotient 0  ; the length of the interval for current plant
          let interval_max 0       ; length of the whole interval

                                   ; calculate the sum of all distances of all plants in dispersal_radius

            ask plants in-radius dispersal_radius with [plant-dead = false]
            [
              set distance_plant distancexy [xcor] of myself [ycor] of myself
              set distance_all  distance_all + exp distance_plant
            ]
          ; now calculate
          let interval table:make

            ask plants in-radius dispersal_radius with [plant-dead = false]
            [
              if ( who != [host] of myself )
              [
                set distance_plant distancexy [xcor] of myself [ycor] of myself
                ; print (word "plant nr.: " who  ", host number = " [host] of myself ", biomass of plant in dispersal-radius = " B-above  ", plant-dead = " plant-dead ", distance = " distance_plant)
                ; self = gerade aufgerufene Pflanze
                ; myself = die Raupe, die die ganzen "ask-Plants" aufgerufen hat
                ; calculate distance between larva and plant in dispersal-radius: works :)
                if(distance_plant > 0)
                [
                  ; set distance_quotient distance_quotient +  distance_all / (exp distance_plant)
                  set distance_quotient distance_quotient +  distance_all / (exp distance_plant)

                  ; make table with keys = plant numbers and values = distances to larva (center)
                  table:put interval who distance_quotient
                ]
              ]
          ]
          set interval_max distance_quotient

          ; interval is set, now draw a random float and look in which section it can be sorted (next host plant)...
          let zufall random-float interval_max
          let schluessel table:keys interval
          let i 0
          let interval_length 0
          while  [zufall > interval_length]  ;go through list (from the start to the end) unless the random-float is smaller than intervall-border
                                             ; -> the corresponding key is the "who" of the new host plant
          [
            let llave item i schluessel
            ;print (word "Key = " llave)
            set interval_length table:get interval llave
            set new-host llave
            set i  ( i + 1 )
          ]
          ;set new-host [who] of one-of plants
        ]
      ;  file-print(word who " " precision xcor 3 " "precision ycor 3 " " precision [ xcor ] of plant new-host 3 " " precision [ ycor ] of plant new-host 3)
         set host new-host
         set distance-path distancexy [xcor] of plant host [ycor] of plant host
        ; file-print (word who " " distance-path)
         set last-movement-tick ticks
         let host-plant plant host
         setxy [ xcor ] of host-plant [ ycor ] of host-plant
         ask host-plant [array:set MEMORY (ticks + current-tau) (sum [biomass-larva] of larvae-here)] ;; setze die Masse der Larve in Feld des Arrays mit aktuellen Zeitpunkt ein
      ]
      [
        ;else
        ; print(word "larva " who " just died")
        die
      ]
    ]; end of movement-modus "distance"
   if movement-modus = 2
   [
        let host-plant one-of plants
        set host [who] of host-plant
        set distance-path distancexy [xcor] of plant host [ycor] of plant host
     ;   file-print(word who " " precision xcor 3 " "precision ycor 3 " " precision [ xcor ] of plant host 3 " " precision [ ycor ] of plant host 3)
        set last-movement-tick ticks
        setxy [ xcor ] of host-plant [ ycor ] of host-plant
        ask host-plant [array:set MEMORY (ticks + current-tau) (sum [biomass-larva] of larvae-here)] ;; setze die Masse der Larve in Feld des Arrays mit aktuellen Zeitpunkt ein
    ] ; end of random distribution
   set plants-visited plants-visited + 1
   set VISITED lput host VISITED
  ]
  [  ; not mobile
  if plant host = nobody ;; if plant death starts the switching by decompose for a nonmobile larva this gets triggered
  [
    die
  ]
  ]
end   ; of switch-plants



     ;;;;;;;;;;;;;;;;;;;;;  larval_death  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     ;;                                                                    ;;
     ;; a turtle procedure (larvae)                                        ;;
     ;; calculates the current probability of the larva to die during      ;;
     ;; the current time step (this depends on the current plant's defense ;;
     ;; level, the larval biomass and is highest if the larva is currently ;;
     ;; commuting                                                          ;;
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to larval_death
  let death_prob 0
  let coeff 0
  let death_dice random-float 1.0 ;; neu eingeführt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; new
  ifelse switch = 0  ; larva on plant
  [
     ifelse mobile?   ; larva >= 3rd instar
    [
      set coeff ( death_coeff + 1.5 * [defense-level] of plant host - 0.1 ) / 6 ; zwischen 0.0 und 1.0
    ]
    [  ; larva < 3rd instar
      set coeff ( death_coeff + 1.5 * [defense-level] of plant host) / 6
    ]
  ]
  [ ; if not on plant
    set coeff 1 / 6 * ( distance-path * 1.5 / dispersal_radius  + death_coeff )
   ; set coeff 1 / 6   + death_coeff
  ]
  if coeff > 1.0
  [
      set coeff  1.0
  ]
  if coeff < 0.0
  [
      set coeff  0.0
  ]
  set death_prob  ( coeff / (1 + log biomass-larva exp 1 ) )
  if switch = 1
  [
    ;print (word "larva number " who " death_prob " death_prob " distance_penalty " coeff " distance " distance-path)
  ]
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  if death_dice <= death_prob
  [
   set larval-death-number larval-death-number + 1
    set VISITED remove-duplicates VISITED
    set  sum-unique-visited sum-unique-visited  + length VISITED; number of all plants visited by larvae (uniques per larvae)
    set sum-visited sum-visited + plants-visited
   die
  ]
end   ; of larval_death



;;;;;;;;;;;;;;;;;;;;;; calculate-plant-defense-level ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                             ;;
;; A plant procedure (turtle):
;; calculates the new defense level of the plant                               ;;
;; for the current time-step                                                   ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-plant-defense-level [ defense-gain-above ];; VORSICHT: HIER NOCH EVTL UMÄNDERN -> Momentan für gesamte Pflanze dasselbe Defense-Level

  let biomass_before array:item B_before_growth who ;only above-ground: array of the plant's above biomass (state of the beginning of the time-step)
  let defense-level-before defense-level
  let defense_before defense-level * biomass_before
  let eaten 0
  ifelse any? larvae-here
  [
    set eaten array:item Feeding-this-step who ; biomass removed by all caterpillars feeding on this plant during this time step
    set damage-sum damage-sum + eaten
    set defense-level ( defense-level *  biomass_before + defense-gain-above - eaten * defense-level ) / B-above ; aktualisiere defense level
  ]
  [
    set defense-level ((defense-level-before * biomass_before + defense-gain-above ) / B-above ); ansonsten kein fraßschaden der Raupe -> keine produzierte Defense wird "weggenommen"
  ]
end   ; of calculate-plant-defense-level



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;  decomposition  ;;;;;;;;;;
;;   A turtle (plant) procedure  ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to decompose
  let host-number who
  ask larvae with [host = host-number]
  [
    set switch 1
  ]
  set sum-number-dead-plants sum-number-dead-plants + 1
  die

  ifelse display-below-ground
    [ set size rad-below * size-factor * 2 ]
    [ set size rad-above * size-factor * 2 ]
end  ; of decomposition





;;;;;;;;;;;;;;;;;;;;;;;;;  make-updates  ;;;;;;;;;;;;;;;;;;;;;;;
;;
;; prints out desired variables in the given file
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to make-updates
 if (level-GA = 3)
 [
    set sum-tau1 sum [B] of plants with [tau-type = 1] + sum-tau1
    set sum-tau2 sum [B] of plants with [tau-type = 2] + sum-tau2
    set sum-above-tau1 sum [B-above] of plants with [tau-type = 1] + sum-above-tau1
    set sum-above-tau2 sum [B-above] of plants with [tau-type = 2] + sum-above-tau2
 ;   set  overall-mean-damage-tau1 (sum_damage_tau1  / sum-above-tau1)  ;; gesamtsumme schaden/ gesamtsumme biomasse (für tau 1)
 ;   set  overall-mean-damage-tau2 (sum_damage_tau2 / sum-above-tau2)   ;; gesamtsumme schaden/ gesamtsumme biomasse (für tau 2)
 ]
  ; beachte hier die gestorbenen Pflanzen -> die haben auch eine Masse = 0, die zum Mean beiträgt!
  set number-alive-plants count plants
  set sum-number-plants sum-number-plants + number-alive-plants ;; count of all steps in all time steps (sums up with each step)
  update-sum-residience-time
  ; number-dead-plants wird in decompose gesetzt
  set sum-above-plants sum [B-above] of plants          ;; above-ground biomasse aller pflanzen (current tick)
  set sum-below-plants sum [B-below] of plants          ;; below-ground biomasse aller pflanzen (current tick)
  set sum-plants sum [B] of plants

  set all-sum-above-plants all-sum-above-plants + sum-above-plants
  set all-sum-damage-plants all-sum-damage-plants + sum-damage-plants         ;; sum of damage caused by feeding larvae on all plants over all time-steps, given in larval_growth
 ; print (word " all-sum-above " all-sum-above-plants " summe schaden " all-sum-damage-plants " tick " ticks)
;  print "-----------------------------------------------------------------------------------"

    ; -> daraus dann: productivity -> sum-above-plants auch intereessant std and mean für ergebnisse
  set mean-above-plants mean [B-above] of plants        ;; Mean plant above-ground mass current time-step
  set mean-below-plants mean [B-below] of plants        ;; Mean plant below-ground mass current time-step
  set mean-plants mean [B] of plants              ;; Mean plant mass of all plants (whole plant) current time-step
  if(number-alive-plants >= 2)
  [
      set std-above-plants standard-deviation [B-above] of plants         ;; standard deviation of plant above-ground mass current time-step
      set std-below-plants standard-deviation [B-below] of plants          ;; standard deviation of plant below-ground mass current time-step
      set std-plants standard-deviation [B] of plants                ;; standard deviation of plant mass of all plants (whole plant) current time-step
  ]
  ; larval-death-number  wird in larval_death gesetzt


  let mean-biomass-larvae 0

  if any? larvae
  [
   set mean-biomass-larvae mean [biomass-larva] of larvae
  ]
end 

to set-larvae-on-plants
  [

  ]
end 






;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  make-plots ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; an observer procedure
;; updates all plots seen in the "interface"-Tab
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to make-plots

 ; auto-plot-on
; if(ticks mod 6 = 0)[ print (word "tick " ticks " plant " plant-number " ,  biomass-above-ground " [B-above] of plant plant-number " biomass-plant " [B] of plant plant-number) ]
  set-current-plot "Larva_dead"
  set-plot-pen-mode 0
  set-current-plot-pen "number-dead"
  plotxy (ticks + 1) larval-death-number  ;sums up: number of larvae which really died (and did not go to pupate)

  set-current-plot "Above-ground-sizes"
  set-current-plot-pen "above"
  histogram [B-above] of plants
  set-histogram-num-bars 20

  set-current-plot "mean infestation rate plants"
  set-current-plot-pen "all"
    set-plot-pen-mode 0
  plotxy (ticks + 1) mean-infestation-rate-plants


  ;; Mean residence time of larvae
    set-current-plot "Mean residence time" ; in %
    if count plants > 0
    [
      set-current-plot-pen "residience"
      let mean-residence-ticks sum-residience-time / sum-number-plants * 100
      plotxy (ticks + 1) mean-residence-ticks
    ]

  ;; biomass
  set-current-plot "Mean biomass through time"
  if count plants > 0
  [
    set-current-plot-pen "Mean"
    plotxy (ticks + 1) mean [B] of plants
    set-current-plot-pen "Mean-above"
    plotxy (ticks + 1) mean [B-above] of plants
    set-current-plot-pen "Mean-below"
    plotxy (ticks + 1) mean [B-below] of plants
  ]

  set-current-plot "Total biomass through time"
  if count plants > 0
  [
    set-current-plot-pen "Total"
    plotxy (ticks + 1) sum [B] of plants
    set-current-plot-pen "Total-above"
    plotxy (ticks + 1) sum [B-above] of plants
    set-current-plot-pen "Total-below"
    plotxy (ticks + 1) sum [B-below] of plants
    set-current-plot-pen "Total-prod"
    plotxy (ticks + 1)  sum [above-mass-produced] of plants

  ]
      if any? larvae
    [
      set-current-plot "mean-biomass-larvae"
      set-current-plot-pen "mass"
      set-plot-pen-mode 0
      plotxy (ticks + 1) mean [biomass-larva] of larvae


      set-current-plot "biomass-larvae"
      set-histogram-num-bars 20
      histogram [biomass-larva] of larvae
    ]

    set-current-plot "tau-distribution"
   ; set-histogram-num-bars ( tau-max - tau-min )
    histogram [tau] of plants

    set-current-plot "mean-residience-time (alive)"
    set-histogram-num-bars 20 ; in steps of 5%
    histogram [ mean-residence-time ] of plants

  ;; Mean-damage
  set-current-plot "Sum of damage"
  ; mean damage = summe aller Schäden, die an Pflanze angefallen sind/
  set-current-plot-pen "damage"
  set-plot-pen-mode 0
  plotxy (ticks + 1) all-sum-damage-plants

  ;; Mean-damage
  set-current-plot "Mean-damage"
  ; mean damage = summe aller Schäden, die an Pflanze angefallen sind/
  set-current-plot-pen "Tau1"
  set-plot-pen-mode 0
  plotxy (ticks + 1)  overall-mean-damage-plants

  ;; Mean-damage
  if any? plants with [ who = plant-number]
  [
    set-current-plot "defense-plot-single-plant"
    ; mean damage = summe aller Schäden, die an Pflanze angefallen sind/
    set-current-plot-pen "mass-above"
    set-plot-pen-mode 0
    plotxy (ticks + 1) [B-above] of plant plant-number
    set-current-plot-pen "biomass"
    set-plot-pen-mode 0
    plotxy (ticks + 1) [B] of plant plant-number
    set-current-plot-pen "defense"
    set-plot-pen-mode 0
    plotxy (ticks + 1) ([B-above] of plant plant-number * [defense-level] of plant plant-number)
    set-current-plot-pen "damage"
    set-plot-pen-mode 0
     let damage array:item Feeding-this-step plant-number
    plotxy (ticks + 1) (damage)
  ]
end   ; of make-plots

;;-----------------------------------------------------------------------------;;

to-report factor [n]  ;; return the smallest integer divider of n larger than its square root (translated directly from Weiner's code)
  let root floor(sqrt(n))
  while [(n / root) < root] [ set root root + 1 ]
  report root
end 

to-report dead-larvae
 report larval-death-number
end 

to-report number-dead
 report sum-number-dead-plants
end 

to-report productivity
 let prod sum-above-plants
 report prod
end 

to-report overall_damage  ;; absolute value, sum of all damage any plant has taken over all time steps
 report all-sum-damage-plants
end 

to-report BIOMASSVECTOR ;; vector of all biomasses per tau at the end of simulation

 let TAU-BIOMASS array:from-list n-values (numTaus) [0]
 ask plants
 [
   array:set TAU-BIOMASS tau (array:item TAU-BIOMASS tau + B-above)
 ]
 let pia-list array:to-list TAU-BIOMASS
 report pia-list
end 

to-report ALLBIOMASSES ;; vector of all biomasses at the end of simulation

 let ALLBIOMASSES-vector array:from-list n-values (density) [0]
 ask plants
 [
   array:set ALLBIOMASSES-vector who B-above

 ]
 let result array:to-list ALLBIOMASSES-vector
 report result
end 

to-report overall-mean-damage-plants ; in %
 let damage  ( sum [damage-sum] of plants  / sum [above-mass-produced] of plants ) * 100  ;; prozentualer anteil schaden über gesamtbiomasse produziert
 report damage
end 

to print-growth
  ask plants
  [
        let infested 0
    if (any? larvae-here)
    [
      set infested 1
    ]
    let induced 0
    if(color != green)
    [
        set induced 1
    ]
    let damage array:item Feeding-this-step who
    let defense defense-level
    file-print(word ticks " " who " " B-above " " B-below " " infested " " induced " " defense " " damage)
  ]
end  ; of decomposition

to-report mean-infestation-rate-plants  ;in %
   let mean-infestation sum-residience-time / sum-number-plants * 100
 report mean-infestation
end 

to-report mean-induced-rate-plants  ;in %
   let mean-induced sum-induced-plants / sum-number-plants * 100
 report mean-induced
end 

to-report mean-unique-pupation  ;how many plants did larvae visit
  let anzahl pupation-counter
  if(anzahl = 0)
  [
    set anzahl 1
  ]
 report sum-unique-visited-pupation / anzahl
end 

to-report mean-visit-pupation  ;how many plants did larvae visit
    let anzahl pupation-counter
  if(anzahl = 0)
  [
    set anzahl 1
  ]
   report sum-visited-pupation / anzahl
end 

to-report pupation-age
 let anzahl pupation-counter
  if(anzahl = 0)
  [
    set anzahl 1
  ]
 let test pupation-age-sum / anzahl
 report test
end 

to-report number-visit-unique  ;how many plants did larvae visit
 report sum-unique-visited / number_larvae
end 

to-report mean-visit-all  ;how many plants did larvae visit
 report sum-visited / number_larvae
end 

to-report mean-defense-switches  ;how many plants did larvae visit
 report defense-switch-count / number_larvae
end 




;;;;;;;;;;;;;;;;;;;;;;;update-sum-residence_time  ;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; an observer procedure
;; calculate (for each tick) the sum of the ticks
;; in which plants are occupied by larvae. Sums up over ticks
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to update-sum-residience-time
  ask plants
    [
      if any? larvae-here
      [
        set sum-residience-time sum-residience-time + 1
      ]
    ]
; needs a counter for all ticks*plants of whole simulation to have the TRUE mean of residience time
end 

     ;;;;;;;;;;;;;;;;;;;  update mean-residence-time  ;;;;;;;;;;;;;;;;;;;;;;;;
     ;;                                                                     ;;
     ;; Reporter for a single plant                                         ;;
     ;; Calculates and returns the number of ticks when larvae occupied     ;;
     ;; the plant. [in %]                                                   ;;
     ;;                                                                     ;;
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to-report  mean-residence-time ;[plant-number]  ; for one plant, in %
;  ask plant plant-number
 ; [
    let laenge ticks
    let liste array:to-list  MEMORY        ; Listen lassen sich für diesen Zweck leichter bearbeiten... -> wandle array in Liste um
                                           ; filter the nonzero values from the list
    let nonzero_MEMORY filter [ ?1 -> ?1 > 0 ] liste
    let number_ticks length nonzero_MEMORY ; number of ticks a plant has been visited by larvae
                                           ; not the *true* mean of residience time (forgets deead plants) however still useful
  let residience number_ticks / laenge * 100
  report residience
end 

There is only one version of this model, created over 6 years ago by Pia Backmann.

Attached files

File Type Description Last updated
Supplement_TIMELY_model_FINAL.pdf pdf Supplement of the model, including the ODD over 6 years ago, by Pia Backmann Download
TRACE_document.pdf pdf TRACE document of the TIMELY model over 6 years ago, by Pia Backmann Download

This model does not have any ancestors.

This model does not have any descendants.