;; DemGenTrout 1.3 (2012)
;;
;; Developed by
;; Beatrice M. Frank
;; Earth and Life Institute
;; Universite catholique de Louvain
;; Beatrice.Frank@gmail.com
;; 
;; Last updated: September 2012

breed[ trout ]
breed[ dead ]
breed[ gone ]
breed[ stocked ]
breed[ hybrid ]

;; trout state variables
trout-own[ sex genotype natal-stream week-of-birth week-of-birth2 ;; fixed
  current-stream age stage status body-length body-weight condition-factor num-offspring
  spawned? moved-to-spawn? returned? ]

;; gone trout state variables
gone-own[ sex genotype natal-stream week-of-birth week-of-birth2 ;; fixed
  current-stream age stage status body-length body-weight condition-factor num-offspring
  spawned? moved-to-spawn? returned? ]

;; dead trout state variables
dead-own[ sex genotype natal-stream week-of-birth week-of-birth2 ;; fixed
  current-stream age stage status body-length body-weight condition-factor num-offspring
  spawned? moved-to-spawn? returned? ]

;; stocked state variables
stocked-own[ sex genotype natal-stream week-of-birth week-of-birth2 ;; fixed
  current-stream age stage status body-length body-weight condition-factor num-offspring
  spawned? moved-to-spawn? returned? newly-stocked? ]

;; hybrid state variables
hybrid-own[ sex genotype natal-stream week-of-birth week-of-birth2 ;; fixed
  current-stream age stage status body-length body-weight condition-factor num-offspring
  spawned? moved-to-spawn? returned? ]
  
;; stream state variables
patches-own [ stream flow temperature ] 

globals[ week week-of-year month-of-year year
 ;; trout initialisation
 prop-C prop-L init-N
 prop-age0-C prop-age1-C prop-age2-C prop-age3-C
 prop-age0-L prop-age1-L prop-age2-L
 meanl-age0-C meanl-age0-L meanl-age1-C meanl-age1-L meanl-age2-C meanl-age2-L meanl-age3-C meanl-age3-L
 sdl-age0-C sdl-age0-L sdl-age1-C sdl-age1-L sdl-age2-C sdl-age2-L sdl-age3-C sdl-age3-L 
 trdown-age0-C trdown-age0-L trdown-age1-C trdown-age1-L trdown-age2-C trdown-age2-L trdown-age3-C trdown-age3-L
 trup-age0-C trup-age0-L trup-age1-C trup-age1-L trup-age2-C trup-age2-L trup-age3-C trup-age3-L
 meanl-birth-C sdl-birth-C meanl-birth-L sdl-birth-L trdown-C trup-C trdown-L trup-L 
 mean-bcf-C sd-bcf-C mean-bcf-L sd-bcf-L
 propC-inL 
 ;; stream input data
 streamC-discharge streamL-discharge streamC-temperature streamL-temperature 
 mean-dischargeC sd-dischargeC mean-dischargeL sd-dischargeL
 mean-temperatureC sd-temperatureC mean-temperatureL sd-temperatureL
 max-dischargeC max-dischargeL

 ;; survival parameters 
 streamC-age0-survival streamC-age1-survival streamC-age2-survival streamC-age3-survival
 streamL-age0-survival streamL-age1-survival streamL-age2-survival streamL-age3-survival 
 streamC-age0-survival-list streamC-age1-survival-list streamC-age2-survival-list streamC-age3-survival-list
 streamL-age0-survival-list streamL-age1-survival-list streamL-age2-survival-list streamL-age3-survival-list  
 C-age0-survival C-age1-survival C-age2-survival C-age3-survival
 L-age0-survival L-age1-survival L-age2-survival L-age3-survival 
 predation-factor
 ;; growth parameters
 streamC-parK streamL-parK
 streamC-max-length streamC-parA streamC-parB streamL-max-length streamL-parA streamL-parB   
 ;; spawning parameters
 spawn-start spawn-end moved-prop spawn-mean-length spawn-mean-cond
 spawn-sd-length spawn-sd-cond spawn-min-age
 spawn-mean-temperature spawn-sd-temperature spawn-mean-flow spawn-sd-flow
 streamC-capacity offprod offprod-min-length offprod-max-length offprod-min
 offspringC-varA offspringC-varB offspringL-varA offspringL-varB
 offprodC-max offprodL-max 
 length-heritability 
 ;; hatching parameters
 hatch-start hatch-end
 ;; downstream movement parameters
 move-start move-end move-min-age move-max-age
 move-mean-length move-sd-length 
 move-age1-prob move-age1-varB move-age1-varA
 move-age2-prob move-age2-varB move-age2-varA 
 move-mean-temperature move-sd-temperature move-mean-flow move-sd-flow
 ;; leaving stream L forever parameters
 leaving-propC leaving-propL
 ;; stocking
; stocking-start stocking-end stocking-coefficient survival-factor
 num-stocked hybrid-spawning-prob ; trout-spawning-prob stocked-spawning-prob 

 out-year out-month out-week
]  

to setup
  clear-all      
  no-display     
  ;; controlling the random numbers
  random-seed 1223251200  
  
  ;; first week of the year = 1st to 7th October included
  set week 1
  set week-of-year 1
  set month-of-year 1
  set year 1
  
  import-world "DemGenTrout-world.csv"
  ask trout [set genotype lput "000/000" genotype]
  
  reset-ticks
  restore-defaults
end
  
to restore-defaults
  ;; restore the default parameters linked to stocking
  set stocking-start 1
  set stocking-end 10
  set stocking-coefficient 0.50     
  set survival-factor 10  
  set trout-spawning-prob 1.00
  set stocked-spawning-prob 0.10  
  ;; compute spawning probability for hybrids
  set hybrid-spawning-prob (trout-spawning-prob * (1 - ((1 - stocked-spawning-prob) / 2)))    
end

to set-parameters  
  ;; compute spawning probability for hybrids
  set hybrid-spawning-prob (trout-spawning-prob * (1 - ((1 - stocked-spawning-prob) / 2)))   
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SCHEDULING ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go
  no-display 

  tick 
  ;; end at year 35
  if ticks = 1820 [ user-message "Input data are only available for 35 years."  stop ]
  ;; a year = 52 weeks  
  set week-of-year (week mod 52) + 1
  set month-of-year (int(week-of-year / 4.5) mod 52) + 1 
  set year (int(week / 52)) + 1 ; mod 52
  
  ;; each week

;-- 1. Update stream hydrological conditions ----------------------------------------------------------
  
  ask patches [ update-stream-conditions ] 
   
;-- New. Introduce stocked trout in the system --------------------------------------------------------

  ;; introduction of stocked trout
  if week-of-year = 27 and year >= stocking-start and year <= stocking-end [ intro-stocked-trout ]   
  
let selected-breed (list trout stocked hybrid)
foreach selected-breed 
[    
  
;-- 2. Kill trout in each stream ----------------------------------------------------------------------

  ask ? [ trout-die ]         
    
;-- 3. Update trout length, weight and condition factor -----------------------------------------------
  
  ask ? [ trout-grow ]      

;-- 4. Reproduce trout in each stream ----------------------------------------------------------------- 

  ;; spawning time window: duration = 11 weeks
  if week-of-year >= spawn-start and week-of-year <= spawn-end
  [   
    ;--- 4.1. Identify candidate spawners ---;
    ask ? [ trout-become-candidate-spawners ] 
    ;; select some of the candidate spawners born in stream L in order to make them reproduce in C
    let candidate-spawners-streamL ? with [ status = "candidate-spawner" and current-stream = "L" 
      and natal-stream = "L" and not moved-to-spawn? ]
    while [ (count ? with [status = "selected-candidate-spawner"]) < (moved-prop * count candidate-spawners-streamL) ]
    [ ask one-of candidate-spawners-streamL [ set status "selected-candidate-spawner" ] ]      
    
    ;--- 4.2. Move candidate spawners upstream ---;   
    ask ? [ trout-move-to-spawn ]         
    
    ;; adapt offspring production if abundance in C is strictly higher than streamC-capacity
    ifelse count ? with [ current-stream = "C" ] > streamC-capacity 
    [ set offprodC-max offprod / 10 ]
    [ set offprodC-max offprod ]        

    ;--- 4.3. Produce offspring in each stream ---;  
    trout-spawn-in-streamC
    trout-spawn-in-streamL
  ]
   
;-- 5. Increment trout age and update stage -----------------------------------------------------------

  if week-of-year = (hatch-start - 1) ;; one week before hatching
  [ 
    ask ? [ trout-update-age ]
    ask ? with [age > 6] [die!] ;; trout die at age 7    
  ]       

;-- 6. Reveal offspring -------------------------------------------------------------------------------   
  
  ;; hatching time window: duration = 11 weeks
  if week-of-year >= hatch-start and week-of-year <= hatch-end
  [ (foreach sort ? with [age = -1]
      [ ask ?
        [
          if (week-of-year - week-of-birth) = (hatch-start - spawn-start) ;; delay = 10 weeks
          [ set hidden? false set age 0 ]
        ]])
  ]

;-- 7. Move trout of stream C downstream ----------------------------------------------------------
    
  ;; migration time window: duration = 25 weeks
  if week-of-year >= move-start and week-of-year <= move-end
  [             
    ;--- 7.1. Identify candidate migrants among juveniles ---; 
    ask ? [ trout-become-candidate-migrants ]          
     
    ;--- 7.2. Update movement probabilities ---;     
    update-move-age1-prob   
    update-move-age2-prob      
            
    ;--- 7.3. Move candidate migrants ---;
    ask ? [ trout-move-downstream ]                  
  ]        

;-- 8. Move upswimming spawners back to stream L ----------------------------------------------------- 

  if week-of-year = (spawn-end + 1) ;; the week after the spawning process
  [ 
    ask ? [ trout-move-back ]
  ]               
  
;-- 9. Remove young trout of stream L from the system ------------------------------------------------    
  
  ;; at the end of the year
  if week-of-year = 52
  [ 
    ;; some juveniles born in C and living in stream L do not settle and go elsewhere 
    let bornC-in-streamL ? with [current-stream = "L" and natal-stream = "C" and status = "migrant"]    
    let bornC-leaving (leaving-propC * count bornC-in-streamL)
    ask n-of bornC-leaving bornC-in-streamL [ leave-streamL ]  
    ;; some juveniles born in L and living in stream L do not settle and go elsewhere  
    let bornL-in-streamL ? with [current-stream = "L" and natal-stream = "L" and (age = 1 or age = 2)]
    let bornL-leaving (leaving-propL * count bornL-in-streamL)
    ask n-of bornL-leaving bornL-in-streamL [ leave-streamL ]   
  ]                
  
; Trout reset --------------------------------------------------------------------------------------
  
  ;; at the end of the year
  if week-of-year = 52
  [      
    ;; reset trout status and boolean state variables    
    ask ? [ set status "non-spawner" set moved-to-spawn? false set spawned? false set returned? false ] 
    ;; kill all hidden turtles  
    ask gone [die]     
    ask dead [die]          
  ]
]

; Increment the week -------------------------------------------------------------------------------
   
  set week week + 1
  
end

;;;;;;;;;;;;;;;;;;;;;;;;;;; STREAM PROCEDURE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; update water temperature and flow of each stream
to update-stream-conditions
  ifelse ticks < (6 * 52)
  ;; first 6 years (2004 to 2009)
  [
    ifelse stream = "C"
    [
      set flow item week streamC-discharge
      set temperature item week streamC-temperature 
    ]
    [
      set flow item week streamL-discharge
      set temperature item week streamL-temperature
    ]  
  ]
  ;; next years (2010 to 2038)
  [
    ifelse stream = "C"
    [
      set flow min (list max-dischargeC (item week streamC-discharge + random-lognormal mean-dischargeC sd-dischargeC))
      set temperature (item week streamC-temperature + random-normal mean-temperatureC sd-temperatureC) 
    ]
    [
      set flow min (list max-dischargeL (item week streamL-discharge + random-lognormal mean-dischargeL sd-dischargeL))
      set temperature (item week streamL-temperature + random-normal mean-temperatureL sd-temperatureL)
    ]  
  ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;; TROUT PROCEDURES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; trout mortality
to trout-die
  ;; in stream C
  if (breed = trout or breed = stocked) and stage = "fry" and current-stream = "C" [if random-float 1 > ((item week-of-year streamC-age0-survival) ^ (1 / 52)) [die!]]  
  if breed = hybrid and stage = "fry" and current-stream = "C" [if random-float 1 > (((item week-of-year streamC-age0-survival) * (1 - ((1 - (item week-of-year streamC-age0-survival)) / 2))) ^ (1 / 52)) [die!]]
  if (breed = trout or breed = stocked) and stage = "juvenile" and age = 1 and current-stream = "C" [if random-float 1 > ((item week-of-year streamC-age1-survival) ^ (1 / 52)) [die!]]
  if breed = hybrid and stage = "juvenile" and age = 1 and current-stream = "C" [if random-float 1 > (((item week-of-year streamC-age1-survival) * (1 - ((1 - (item week-of-year streamC-age1-survival)) / 2))) ^ (1 / 52)) [die!]]  
  if (breed = trout or breed = stocked) and stage = "juvenile" and age = 2 and current-stream = "C" [if random-float 1 > ((item week-of-year streamC-age2-survival) ^ (1 / 52)) [die!]]
  if breed = hybrid and stage = "juvenile" and age = 2 and current-stream = "C" [if random-float 1 > (((item week-of-year streamC-age2-survival) * (1 - ((1 - (item week-of-year streamC-age2-survival)) / 2))) ^ (1 / 52)) [die!]]    
  if (breed = trout or breed = stocked) and stage = "adult" and age < 6 and current-stream = "C" [if random-float 1 > ((item week-of-year streamC-age3-survival) ^ (1 / 52)) [die!]] 
  if breed = hybrid and stage = "adult" and age < 6 and current-stream = "C" [if random-float 1 > (((item week-of-year streamC-age3-survival) * (1 - ((1 - (item week-of-year streamC-age3-survival)) / 2))) ^ (1 / 52)) [die!]]    
  if week-of-year >= spawn-start and week-of-year <= spawn-end and (breed = trout or breed = stocked) and moved-to-spawn? = true and current-stream = "C" 
   [if random-float 1 > (((item week-of-year streamC-age3-survival) / predation-factor) ^ (1 / (spawn-end - spawn-start))) [die!]] 
  if week-of-year >= spawn-start and week-of-year <= spawn-end and breed = hybrid and moved-to-spawn? = true and current-stream = "C" 
   [if random-float 1 > ((((item week-of-year streamC-age3-survival) * (1 - ((1 - (item week-of-year streamC-age3-survival)) / 2))) / predation-factor) ^ (1 / (spawn-end - spawn-start))) [die!]]       
  ;; in stream L
  if (breed = trout or breed = stocked) and stage = "fry" and current-stream = "L" [if random-float 1 > ((item week-of-year streamL-age0-survival) ^ (1 / 52)) [die!]]
  if breed = hybrid and stage = "fry" and current-stream = "L" [if random-float 1 > (((item week-of-year streamL-age0-survival) * (1 - ((1 - (item week-of-year streamL-age0-survival)) / 2))) ^ (1 / 52)) [die!]]  
  if (breed = trout or breed = stocked) and stage = "juvenile" and age = 1 and current-stream = "L" [if random-float 1 > ((item week-of-year streamL-age1-survival) ^ (1 / 52)) [die!]]
  if breed = hybrid and stage = "juvenile" and age = 1 and current-stream = "L" [if random-float 1 > (((item week-of-year streamL-age1-survival) * (1 - ((1 - (item week-of-year streamL-age1-survival)) / 2))) ^ (1 / 52)) [die!]]  

  if breed = stocked and newly-stocked? = true and stage = "juvenile" and age = 2 and current-stream = "L" [if random-float 1 > (((item week-of-year streamL-age2-survival) / survival-factor) ^ (1 / 52)) [die!]]  
  if (breed = trout or breed = stocked) and stage = "juvenile" and age = 2 and current-stream = "L" [if random-float 1 > ((item week-of-year streamL-age2-survival) ^ (1 / 52)) [die!]]
  if breed = hybrid and stage = "juvenile" and age = 2 and current-stream = "L" [if random-float 1 > (((item week-of-year streamL-age2-survival) * (1 - ((1 - (item week-of-year streamL-age2-survival)) / 2))) ^ (1 / 52)) [die!]]
  
  if (breed = trout or breed = stocked) and stage = "adult" and age < 6 and current-stream = "L" [if random-float 1 > ((item week-of-year streamL-age3-survival) ^ (1 / 52)) [die!]]  
  if breed = hybrid and stage = "adult" and age < 6 and current-stream = "L" [if random-float 1 > (((item week-of-year streamL-age3-survival) * (1 - ((1 - (item week-of-year streamL-age3-survival)) / 2))) ^ (1 / 52)) [die!]]    
end

;; individual growth
to trout-grow
  ifelse current-stream = "C"
  [
    ;; von Bertalanffy growth equation for trout in stream C
    set body-length (body-length + (streamC-parK * (streamC-max-length - body-length)))
    let healthy-weight (streamC-parA * ((body-length) ^ streamC-parB)) ;; weight-length regression 
    set body-weight (condition-factor * healthy-weight)      
  ]
  [
    ;; von Bertalanffy growth equation for trout in stream L
    set body-length (body-length + (streamL-parK * (streamL-max-length - body-length)))  
    let healthy-weight (streamL-parA * ((body-length) ^ streamL-parB)) ;; weight-length regression
    set body-weight (condition-factor * healthy-weight)      
  ]
  set condition-factor max (list 0.5 (condition-factor + (- 0.008 + random-float 0.016)))
  ;; test for negative length and negative weight
  if body-length < 0 [ user-message "Negative fish length" ]
  if body-weight < 0 [ user-message "Negative fish weight" ]    
end

to die!
  set breed dead
  hide-turtle
end

;; identification of candidate spawners
to trout-become-candidate-spawners
  if age >= spawn-min-age and status != "spawner" and not moved-to-spawn? 
  [ if random-normal spawn-mean-length spawn-sd-length < body-length and 
    random-normal spawn-mean-cond spawn-sd-cond < condition-factor
    [ set status "candidate-spawner" ] ]  
end

;; movement to stream C for reproduction
to trout-move-to-spawn
 let temperature-streamL [temperature] of one-of patches with [ stream = "L" ]      
 let flow-streamL [flow] of one-of patches with [ stream = "L" ] 
 ;; spawners born in C or in L move to C
 if (status = "candidate-spawner" and not moved-to-spawn? and current-stream = "L" and natal-stream = "C") 
   or (status = "selected-candidate-spawner" and not moved-to-spawn?)     
 [ if random-lognormal spawn-mean-flow spawn-sd-flow < flow-streamL   
   [ set current-stream "C" set xcor 1 set moved-to-spawn? true set status "candidate-spawner" ] ]  
end

;; update trout age and stage
to trout-update-age
  set age age + 1
  trout-update-stage 
end  

;; update trout stage: fry, juvenile, adult 
to trout-update-stage
  if age < 1 [ set stage "fry" stop ]
  if age >= 1 and age <= 2 [ set stage "juvenile"stop ]
  if age > 2 [ set stage "adult" stop ] 
end

;; reproduction
to trout-spawn-in-streamC
  let trout-spawners trout with [ status = "candidate-spawner" or status = "spawner" and current-stream = "C" ] 
  let stocked-spawners-temp stocked with [ status = "candidate-spawner" or status = "spawner" and current-stream = "C" ]  
  let hybrid-spawners-temp hybrid with [ status = "candidate-spawner" or status = "spawner" and current-stream = "C" ]
  let num-stocked-spawners (floor ((count stocked-spawners-temp) * stocked-spawning-prob))
  let num-hybrid-spawners (floor ((count hybrid-spawners-temp) * hybrid-spawning-prob))  
  let stocked-spawners n-of num-stocked-spawners stocked-spawners-temp
  let hybrid-spawners n-of num-hybrid-spawners hybrid-spawners-temp  
  let spawners (turtle-set trout-spawners stocked-spawners hybrid-spawners) 
  ifelse count spawners < 2
  [ stop ]
  [  
    ;; length of parents when born (VBLF backwards)
    let born-length-parents VBLF-reverse spawners    
    ;; mean and variance of length of parents when born
    if length born-length-parents < 2 [ stop ]
    let mean-parents mean born-length-parents
    let var-parents variance born-length-parents
    let num-crossover ((count trout-spawners + count stocked-spawners + count hybrid-spawners) / 2)  
    ;; polygamous mating with satellite males (not in good condition)    
    repeat num-crossover
    [
      let num-males 1 + random 4 ; number of males drawn randomly from an uniform distribution from 1 to 4
      let all-parent-M spawners with [ sex = "M" ]
      if count all-parent-M < num-males [ stop ]         
      let no-parent-M max-n-of num-males spawners with [ sex = "M" ] [condition-factor]
      let sel-parent-M all-parent-M with [ not member? self no-parent-M and not spawned? ]  
      if count spawners with [ sex = "F" and not spawned? ] <= 1 [ stop ] 
      if count sel-parent-M < num-males [ stop ]  
      ;; select the parents: first, the spawners that have moved to spawn, then the other spawners  
      let parent-M n-of num-males sel-parent-M
      let parent-F max-one-of spawners with [ sex = "F" and not spawned? ] [condition-factor]     
      if (count spawners with [ sex = "M" and not spawned? and moved-to-spawn? ] - num-males) >= num-males  
      [ 
        if any? spawners with [ sex = "M" and not spawned? and moved-to-spawn? ] 
        [
          set all-parent-M spawners with [ sex = "M" and moved-to-spawn? ]
          set no-parent-M max-n-of num-males spawners with [ sex = "M" and moved-to-spawn? ] [condition-factor]
          set sel-parent-M all-parent-M with [ not member? self no-parent-M and not spawned? ]           
          set parent-M n-of num-males sel-parent-M
        ]
      ]
      if any? spawners with [ sex = "F" and not spawned? and moved-to-spawn?]
      [ set parent-F max-one-of spawners with [ sex = "F" and not spawned? and moved-to-spawn?] [condition-factor] ]
      ;; length deviances of parents M and F from mean-parents
      let dummy VBLF-reverse parent-M
      let dev-M 0
      let temp (random-normal meanl-birth-C sdl-birth-C)
      if temp < trdown-C [set temp trdown-C]   
      if temp > trup-C [set temp trup-C]                
      if length dummy = 0 [ set dev-M (mean-parents - temp) ]      
      if length dummy = 1 [ set dev-M (mean-parents - (item 0 dummy)) ]        
      if length dummy > 1 [ set dev-M (mean-parents - mean dummy) ]
      let dev-F (mean-parents - (VBLF-reverse parent-F))       
      ask parent-F
      [
        set spawned? true
        set status "spawner"
        set xcor 1
        let num-fry update-num-offspringC [body-length] of parent-F   
        hatch-trout num-fry
        [
          set hidden? true ;; hide trout until hatching
          set age -1 set stage "fry"  
          ;; genotype of parent-M is a mixture of the several genotypes of males
          let genotype-mix mixture ([genotype] of parent-M)
          ;; define genotypes for eggs
          let egg-genotype cross-genotype (genotype-mix) ([genotype] of parent-F)  
          set genotype egg-genotype   
          set breed item 0 assign-breed egg-genotype                                           
          set xcor 1
          set current-stream "C"
          set natal-stream "C"
          set num-offspring 0
          set week-of-birth week-of-year      
          set week-of-birth2 (week + 1)          
          ifelse random-float 1 > 0.5 [ set sex "M" ] [ set sex "F" ]
          set status "non-spawner" set spawned? false
          set moved-to-spawn? false set returned? false          
          ;; set body-length as function of parents' length and environment
          let genetic-dev (sqrt length-heritability) * (dev-M + dev-F)    
          let envi-var ((1 - length-heritability) ^ 2) * var-parents
          let envi-dev random-normal 0 (sqrt envi-var) 
          set body-length mean-parents + genetic-dev + envi-dev
          let temp2 (random-normal meanl-birth-C sdl-birth-C)  
          if temp2 < trdown-C [set temp2 trdown-C]   
          if temp2 > trup-C [set temp2 trup-C]                
          if body-length < trdown-C or body-length > trup-C [ set body-length temp2 ]           
          ;; set body-weight and condition-factor
          set condition-factor (random-normal mean-bcf-C sd-bcf-C)         
          set body-weight (condition-factor * ((body-length) ^ 3) / 100000)       
        ] 
        set num-offspring num-fry
      ]
      ask parent-M
      [
        ;set spawned? true ;; shut because males can reproduce more than once
        set status "spawner" 
        set xcor 1        
        set num-offspring [num-offspring] of parent-F               
      ] 
    ]
  ]               
end 

to trout-spawn-in-streamL
  let trout-spawners trout with [ status = "candidate-spawner" or status = "spawner" and current-stream = "L" ]
  let stocked-spawners-temp stocked with [ status = "candidate-spawner" or status = "spawner" and current-stream = "L" ]
  let hybrid-spawners-temp hybrid with [ status = "candidate-spawner" or status = "spawner" and current-stream = "L" ]
  let num-stocked-spawners (floor ((count stocked-spawners-temp) * stocked-spawning-prob))
  let num-hybrid-spawners (floor ((count hybrid-spawners-temp) * hybrid-spawning-prob))  
  let stocked-spawners n-of num-stocked-spawners stocked-spawners-temp
  let hybrid-spawners n-of num-hybrid-spawners hybrid-spawners-temp    
  let spawners (turtle-set trout-spawners stocked-spawners hybrid-spawners) 
  ifelse count spawners < 2
  [ stop ]
  [  
    let born-length-parents VBLF-reverse spawners 
    ;; mean and variance of length of parents when born
    if length born-length-parents < 2 [ stop ]  
    let mean-parents mean born-length-parents
    let var-parents variance born-length-parents  
    let num-crossover ((count trout-spawners + count stocked-spawners + count hybrid-spawners) / 2)  
    ;; polygamous mating with satellite males (not in good condition)    
    repeat num-crossover  
    [
      let num-males 1 + random 4 ; number of males drawn randomly from an uniform distribution from 1 to 4
      let all-parent-M spawners with [ sex = "M" ]
      if count all-parent-M < num-males [ stop ]         
      let no-parent-M max-n-of num-males spawners with [ sex = "M" ] [condition-factor]
      let sel-parent-M all-parent-M with [ not member? self no-parent-M and not spawned? ]  
      if count spawners with [ sex = "F" and not spawned? ] <= 1 [ stop ] 
      if count sel-parent-M < num-males [ stop ]  
      ;; select the parents: first, the spawners that have moved to spawn, then the other spawners  
      let parent-M n-of num-males sel-parent-M
      let parent-F max-one-of spawners with [ sex = "F" and not spawned? ] [condition-factor]     
      if (count spawners with [ sex = "M" and not spawned? and moved-to-spawn? ] - num-males) >= num-males  
      [ 
        if any? spawners with [ sex = "M" and not spawned? and moved-to-spawn? ] 
        [
          set all-parent-M spawners with [ sex = "M" and moved-to-spawn? ]
          set no-parent-M max-n-of num-males spawners with [ sex = "M" and moved-to-spawn? ] [condition-factor]
          set sel-parent-M all-parent-M with [ not member? self no-parent-M and not spawned? ]           
          set parent-M n-of num-males sel-parent-M
        ]
      ]
      if any? spawners with [ sex = "F" and not spawned? and moved-to-spawn?]
      [ set parent-F max-one-of spawners with [ sex = "F" and not spawned? and moved-to-spawn?] [condition-factor] ]
      ;; length deviances of parents M and F from mean-parents
      let dummy VBLF-reverse parent-M
      let dev-M 0
      let temp (random-normal meanl-birth-L sdl-birth-L)
      if temp < trdown-L [set temp trdown-L]   
      if temp > trup-L [set temp trup-L]                
      if length dummy = 0 [ set dev-M (mean-parents - temp) ]            
      if length dummy = 1 [ set dev-M (mean-parents - (item 0 dummy)) ]        
      if length dummy > 1 [ set dev-M (mean-parents - mean dummy) ]    
      let dev-F (mean-parents - (VBLF-reverse parent-F))      
      ask parent-F
      [
        set spawned? true
        set status "spawner"
        set xcor 0
        let num-fry update-num-offspringL [body-length] of parent-F 
        hatch-trout num-fry
        [
          set hidden? true ;; hide trout until hatching
          set age -1 set stage "fry"
          ;; genotype of parent-M is a mixture of the several genotypes of males
          let genotype-mix mixture ([genotype] of parent-M)
          ;; define genotypes for eggs
          let egg-genotype cross-genotype (genotype-mix) ([genotype] of parent-F)  
          set genotype egg-genotype   
          set breed item 0 assign-breed egg-genotype                                 
          set xcor 0
          set current-stream "L"
          set natal-stream "L"  
          set num-offspring 0   
          set week-of-birth week-of-year      
          set week-of-birth2 (week + 1)              
          ifelse random-float 1 > 0.5 [ set sex "M" ] [ set sex "F" ]    
          set status "non-spawner" set spawned? false
          set moved-to-spawn? false set returned? false                   
          ;; set body-length as function of parents' length and environment
          let genetic-dev (sqrt length-heritability) * (dev-M + dev-F)     
          let envi-var ((1 - length-heritability) ^ 2) * var-parents
          let envi-dev random-normal 0 (sqrt envi-var) 
          set body-length mean-parents + genetic-dev + envi-dev
          let temp2 (random-normal meanl-birth-L sdl-birth-L)  
          if temp2 < trdown-L [set temp2 trdown-L]   
          if temp2 > trup-L [set temp2 trup-L]                         
          if body-length < trdown-L or body-length > trup-L [ set body-length temp2 ]       
          ;; set body-weight and condition-factor
          set condition-factor (random-normal mean-bcf-L sd-bcf-L)
          set body-weight (condition-factor * ((body-length) ^ 3) / 100000)              
        ]
        set num-offspring num-fry
      ]
      ask parent-M
      [
        ;set spawned? true ;; shut because males can reproduce more than once
        set status "spawner"
        set xcor 0      
        set num-offspring [num-offspring] of parent-F
      ]       
    ]  
  ]      
end  

;; stream C
to-report update-num-offspringC [ female-length ] ;; linear function
  ;; compute the intermediate variables for the linear offspring production function for trout of stream C
  set offspringC-varA (offprodC-max - offprod-min) / (offprod-max-length - offprod-min-length)
  set offspringC-varB offprod-min - (offspringC-varA * offprod-min-length) 
  report ((offspringC-varA * female-length) + offspringC-varB)
end

;; stream L
to-report update-num-offspringL [ female-length ] ;; linear function
  ;; compute the intermediate variables for the linear offspring production function  for trout of stream C
  set offspringL-varA (offprodL-max - offprod-min) / (offprod-max-length - offprod-min-length)
  set offspringL-varB offprod-min - (offspringL-varA * offprod-min-length)    
  report ((offspringL-varA * female-length) + offspringL-varB)
end

to-report cross-genotype [ genotype1 genotype2 ]
  let new-genotype []
  let bit 0
  repeat length genotype1
  [
    let choice random 4
    if choice = 0
    [ set new-genotype (lput (word(substring (item bit genotype1) 0 4)(substring (item bit genotype2) 0 3)) new-genotype)]
    if choice = 1
    [ set new-genotype (lput (word(substring (item bit genotype1) 0 3) (substring (item bit genotype2) 3 7)) new-genotype)]
    if choice = 2
    [ set new-genotype (lput (word(substring (item bit genotype2) 0 3) (substring (item bit genotype1) 3 7)) new-genotype)]
    if choice = 3
    [ set new-genotype (lput (word(substring (item bit genotype1) 4 7) (substring (item bit genotype2) 3 7)) new-genotype)]      
    set bit bit + 1
  ]
  report new-genotype
end

to-report mixture [ genotype-temp ]
  let genotype-mix []
  let bit 0
  repeat length one-of genotype-temp
  [
    let choice random (length genotype-temp) ; choose randomly one genotype among n males
    set genotype-mix lput item bit (item choice genotype-temp) genotype-mix
    set bit bit + 1
  ] 
  report genotype-mix
end

to-report VBLF-reverse [ trout-indiv ]  
  ifelse is-agentset? trout-indiv
  [
    let length-when-born []
    (foreach sort trout-indiv
      [
        let time 0
        let length-diff 0
        let length-when-born-temp 0
        ask ?
        [
          set time (week - week-of-birth2)
          ifelse natal-stream = "C" 
          [ 
            set length-diff (streamC-max-length - body-length)
            set length-when-born-temp (streamC-max-length - (length-diff / ((1 - streamC-parK) ^ (time))))            
          ]
          [ 
            set length-diff (streamL-max-length - body-length)
            set length-when-born-temp (streamL-max-length - (length-diff / ((1 - streamL-parK) ^ (time))))            
          ]
        ]     
        set length-when-born lput length-when-born-temp length-when-born                
      ])
    report length-when-born 
  ]  
  [
    let time (week - ([week-of-birth2] of trout-indiv))      
    ifelse [natal-stream] of trout-indiv = "C"
    [ 
      let length-when-born (streamC-max-length - ((streamC-max-length - [body-length] of trout-indiv) / ((1 - streamC-parK) ^ (time)))) 
      report length-when-born
    ]
    [ 
      let length-when-born (streamL-max-length - ((streamL-max-length - [body-length] of trout-indiv) / ((1 - streamL-parK) ^ (time)))) 
      report length-when-born  
    ]   
  ]   
end

to trout-become-candidate-migrants
  if (age = move-min-age or age = move-max-age) and current-stream = "C" and natal-stream = "C" 
  [ if random-normal move-mean-length move-sd-length < body-length
    [ set status "candidate-migrant" ] ]
end

;; movement from C to L
to trout-move-downstream
  let temperature-streamC [temperature] of one-of patches with [ stream = "C" ]  
  let flow-streamC [flow] of one-of patches with [ stream = "C" ]    
  if status = "candidate-migrant"
  [ if random-normal move-mean-temperature move-sd-temperature < temperature-streamC 
    and random-lognormal move-mean-flow move-sd-flow < flow-streamC
    [ ifelse age = 1
      ;; juveniles age 1
      [ if random-float 1 < move-age1-prob 
        [ set xcor 0 set current-stream "L" set status "migrant" ] ]
      ;; juveniles age 2
      [ if random-float 1 < move-age2-prob
        [ set xcor 0 set current-stream "L" set status "migrant" ] ]
    ] 
  ]   
end    

to update-move-age1-prob ;; logistic function
  let nb1 count trout with [current-stream = "C" and body-length > 70]
  let Z1 (exp((move-age1-varA * nb1) + move-age1-varB))
  set move-age1-prob (Z1 / (1 + Z1))  
end

to update-move-age2-prob ;; logistic function
  let nb2 count trout with [current-stream = "C" and body-length > 70]
  let Z1 (exp((move-age2-varA * nb2) + move-age2-varB))
  set move-age2-prob (Z1 / (1 + Z1))  
end

to trout-move-back
  ;; candidate spawners return to previous stream
  if (status = "candidate-spawner" and current-stream = "C" and moved-to-spawn?)
  ;; effective spawners return to previous stream
    or (status = "spawner" and current-stream = "C" and moved-to-spawn?)
  [ set current-stream "L" set xcor 0 set returned? true ]  
end

to leave-streamL
  set breed gone
  hide-turtle
end

to intro-stocked-trout
  if year = 1
  [
    let num-trout count trout with [current-stream = "L"]
    set num-stocked round (stocking-coefficient * num-trout)
    ;print num-stocked
  ]
  create-stocked num-stocked
  [ 
    set breed stocked
    set newly-stocked? true
    setxy random-xcor -15
    set shape "box" set size 0.8
    set current-stream "L"
    set natal-stream "X"
    ifelse random-float 1 > 0.5
      [ set sex "M" ]
      [ set sex "F" ]
    ;; attribute trout genotypes at 12 microsatellites markers (loci)     
    let markers []
    file-open "input-genotypes-stocking.txt"
    set markers (sentence file-read (list ["999/999"]))  
    file-close
    let freq []
    file-open "input-genofreq-stocking.txt"
    set freq (sentence file-read (list [1]))
    file-close    
    set genotype []          
    set genotype assign-genotype (markers) (freq)
    set num-offspring 0     
    set status "non-spawner"
    set spawned? false set moved-to-spawn? false set returned? false      
    set age 2 
    trout-update-stage ;; a trout procedure setting stage from age  
    ;; attribute body-length, body-weight and condition-factor
    set body-length random-normal 248.47 26.71 set condition-factor 1.1 
    set body-weight (condition-factor * ((body-length) ^ 3) / 100000)     
    ;; test for negative length and negative weight
    if body-length < 0 [ user-message "Negative fish length" ]
    if body-weight < 0 [ user-message "Negative fish weight" ]               
  ] ;; end of create-stocked in stream L  
end

to-report assign-genotype [markers freq]
  let locus []
  let bit 0
  repeat length markers
  [
    set locus assign-locus (bit) (markers) (freq)     
    set genotype lput (first locus) genotype  
    set bit bit + 1
  ] 
  report genotype
end

to-report assign-locus [bit markers freq]
    let locus []
    let q 0
    let ran random-float 1.0
    (foreach (item bit markers) (item bit freq)
    [
      set q q + ?2
      if ran <= q [set locus lput ?1 locus] 
      if empty? locus and ?1 = last (item bit markers)
      [ set locus assign-locus (bit) (markers) (freq) ]
    ])    
    report locus
end

;; breed attribution to offspring in function of 13th loci in genotype
to-report assign-breed [ genotype-temp ]
  ;; wild trout
  if (last genotype-temp) = "000/000" [ report (list trout)  ]
  ;; stocked trout 
  if (last genotype-temp) = "999/999" [ report (list stocked)]
  ;; hybrids
  if (last genotype-temp) = "000/999" [ report (list hybrid) ]
  if (last genotype-temp) = "999/000" [ report (list hybrid) ]
end

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

to-report random-lognormal [mu sigma]
  let S sqrt(ln(((sigma / mu) ^ 2) + 1))
  let M ln (mu) - ((S ^ 2) / 2)
  report exp(random-normal M S)
end
@#$#@#$#@
GRAPHICS-WINDOW
10
420
255
531
-1
0
80.0
1
10
1
1
1
0
1
1
1
0
1
0
0
1
1
1
ticks
30.0

TEXTBOX
10
10
280
36
1. Click the Setup button to initialise the model
12
0.0
1

BUTTON
10
30
73
63
setup
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1

BUTTON
10
90
140
123
Change parameters
set-parameters
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0

BUTTON
145
90
275
123
Restore parameters
restore-defaults
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0

SLIDER
125
245
280
278
stocked-spawning-prob
stocked-spawning-prob
0
1
0.1
0.1
1
NIL
HORIZONTAL

SLIDER
125
140
280
173
stocking-coefficient
stocking-coefficient
0
1
0.5
0.1
1
NIL
HORIZONTAL

SLIDER
125
210
280
243
trout-spawning-prob
trout-spawning-prob
0
1
1
0.1
1
NIL
HORIZONTAL

TEXTBOX
10
70
315
96
2. Change the parameter values (or keep defaults)
12
0.0
1

TEXTBOX
10
270
160
288
3. Run the model
12
0.0
1

BUTTON
10
290
73
323
go
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
0

MONITOR
10
330
67
375
week
week-of-year
17
1
11

MONITOR
70
330
127
375
month
month-of-year
17
1
11

MONITOR
130
330
187
375
year
year
17
1
11

BUTTON
10
380
65
413
step-W
go
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0

BUTTON
70
380
125
413
step-M
repeat 4.3 [go]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0

BUTTON
130
380
185
413
step-Y
repeat 52 [go]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0

PLOT
295
25
515
150
Wild trout
Age
Number
-1.0
6.0
0.0
4000.0
true
false
"set-histogram-num-bars 8" ""
PENS
"trout" 1.0 1 -13345367 true "" "histogram [age] of trout with [current-stream = \\"C\\"]"

PLOT
740
25
1240
280
Trout in the tributary
Years
Number
0.0
10.0
0.0
4500.0
true
true
"" ""
PENS
"wild" 1.0 0 -13345367 true "" "if week-of-year = 1 [plotxy year count trout with [current-stream = \\"C\\"]]"
"hybrid" 1.0 0 -5825686 true "" "if week-of-year = 1 [plotxy year count hybrid with [current-stream = \\"C\\"]]"
"stocked" 1.0 0 -2674135 true "" "if week-of-year = 1 [plotxy year  count stocked with [current-stream = \\"C\\"]]"

PLOT
740
280
1240
540
Trout in the main river
Years
Number
0.0
10.0
0.0
2500.0
true
true
"" ""
PENS
"wild" 1.0 0 -13345367 true "" "if week-of-year = 1 [plotxy year count trout with [current-stream = \\"L\\"]]"
"hybrid" 1.0 0 -5825686 true "" "if week-of-year = 1 [plotxy year count hybrid with [current-stream = \\"L\\"]]"
"stocked" 1.0 0 -2674135 true "" "if week-of-year = 1 [plotxy year count stocked with [current-stream = \\"L\\"]]"

PLOT
515
25
735
150
Stocked trout
Age
Number
-1.0
6.0
0.0
15.0
true
false
"set-histogram-num-bars 8" ""
PENS
"stocked" 1.0 1 -2674135 true "" "histogram [age] of stocked with [current-stream = \\"C\\"]"

PLOT
405
150
625
270
Hybrids
Age
Number
-1.0
6.0
0.0
250.0
true
false
"set-histogram-num-bars 8" ""
PENS
"hybrid" 1.0 1 -5825686 true "" "histogram [age] of hybrid with [current-stream = \\"C\\"]"

PLOT
295
290
515
415
Wild trout
Age
Number
-1.0
6.0
0.0
1000.0
true
false
"set-histogram-num-bars 8" ""
PENS
"trout" 1.0 1 -13345367 true "" "histogram [age] of trout with [current-stream = \\"L\\"]"

PLOT
515
290
735
415
Stocked trout
Age
Number
-1.0
6.0
0.0
700.0
true
false
"set-histogram-num-bars 8" ""
PENS
"stocked" 1.0 1 -2674135 true "" "histogram [age] of stocked with [current-stream = \\"L\\"]"

PLOT
405
415
625
540
Hybrids
Age
Number
-1.0
6.0
0.0
150.0
true
false
"set-histogram-num-bars 8" ""
PENS
"hybrid" 1.0 1 -5825686 true "" "histogram [age] of hybrid with [current-stream = \\"L\\"]"

TEXTBOX
295
10
445
28
Tributary (stream C)
12
0.0
1

TEXTBOX
295
275
445
293
Main river (stream L)
12
0.0
1

SLIDER
10
190
120
223
stocking-start
stocking-start
1
35
1
1
1
NIL
HORIZONTAL

SLIDER
10
225
120
258
stocking-end
stocking-end
1
35
10
1
1
NIL
HORIZONTAL

MONITOR
10
140
120
185
Stocking duration
(stocking-end - stocking-start) + 1
17
1
11

SLIDER
125
175
280
208
survival-factor
survival-factor
1
20
10
1
1
NIL
HORIZONTAL

@#$#@#$#@
## WHAT IS IT?

This is the version 1.3 of a brown trout demogenetic model developed by Beatrice Frank during her PhD thesis (Supervisor: Prof. Philippe Baret, Earth and Life Institute, Universite catholique de Louvain). This version includes features of DemGenTrout 1.1 (faster initialisation) and simulates stocking with hatchery trout.

## HOW IT WORKS

The model, named DemGenTrout, was designed to understand how anthropogenic disturbances can affect a brown trout (_Salmo trutta_ L.) population living in a river / nursery brook system of the Lesse River network (Belgium). Changes in the demogenetic (i.e., demography and genetics) structure of the population were monitored, and at a latter stage, the model was used to predict how trout populations might respond to migration barriers and stocking with hatchery trout ([Frank & Baret, 2013](http://dx.doi.org/10.1016/j.ecolmodel.2012.09.017)).

The model has been described following the ODD (Overview, Design concepts, Details) protocol ([Grimm et al., 2006](http://dx.doi.org/10.1016/j.ecolmodel.2006.04.023); [2010](http://dx.doi.org/10.1016/j.ecolmodel.2010.08.019)) in DemGenTrout 1.1. To study stocking impacts on wild brown trout, several modifications of the model structure were needed. More specifically, one submodel, two breeds, and eight parameters (stocking-start, stocking-end, stocking-coefficient, num-stocked, survival-factor, trout-spawning-prob, stocked-spawning-prob, hybrid-spawning-prob) were added. Both spawning and survival submodels were adapted to include phenotypic and genetic differences between hatchery-reared and wild trout. 

The new submodel, 'intro-stocked-trout', simulates the introduction of hatchery individuals in the Lesse River (stream L). Each year at week 27 (i.e., the end of March, corresponding to the beginning of the fishing season) and during 10 years, a fixed number of stocked trout (num-stocked parameter) is introduced in stream L. This number is calculated once, at week 27 of year 1, as the product of the stocking-coefficient by the number of wild trout in stream L. The state variables of each stocked trout are defined as follows. Their length is drawn from a random normal distribution with a mean of 248.47 mm and a standard deviation of 26.71 mm. Their condition factor is set to 1.1, as hatchery fish are often larger and heavier than wild fish. The weight of each individual is calculated as its condition factor times its length cubed according to Fulton's formula. Genotypes are assigned in accordance with alleles and corresponding allelic frequencies observed for hatchery-reared individuals (see text files ''input-genotypes-stocking-12markers'' and ''input-genofreq-stocking-12markers''). All stocked individuals are of age 2. 

The spawning submodel was modified to incorporate the new breeds, which correspond to stocked trout and hybrids. Both breeds have the same state variables as those defined for wild trout. Breed attribution to each offspring depends of the mating. A thirteenth locus was added in trout genotypes for breed tracking, with homozygous alleles arbitrarily fixed to '000' and '999' for wild and hatchery trout, respectively. Hybrids are identified as individuals having heterozygous alleles (i.e., either '000/999' or '999/000'). Phenotypes are thus inherited the same way as genotypes. We hypothesised that stocked trout had reduced reproductive success in comparison with wild trout, and their probability of spawning was set to 0.10 (stocked-spawning-prob parameter). For wild trout, the spawning probability was set to 1.00 (trout-spawning-prob). The spawning probability for hybrids (hybrid-spawning-prob) is given by: trout-spawning-prob * (1 - ((1 - stocked-spawning-prob) / 2)) and is thus equal to 0.55. 

The survival process of trout was also adapted. We hypothesised a high mortality of hatchery-reared fish soon after their release, in comparison with wild fish. The survival rate of age-2 hatchery trout released in stream L was thus divided by the survival-factor, set to 10 (the survival of stocked trout is 10 times lower than the rate normally used for wild trout). As phenotypic differences between hatchery and wild fish often disappear after 1 year in nature, stocked trout of age strictly higher than 2 and hatchery trout born in the system have the same age-specific survival rates than wild trout. Survival of hybrids is computed from survival of wild (S<sub>W</sub>) and hatchery trout (S<sub>S</sub>): S<sub>H</sub> = S<sub>W</sub> * (1 - ((1 - S<sub>S</sub>) / 2)). 

## HOW TO USE IT

Click the 'Setup' button to initialise the model. In this version, the values of all variables are imported from the file ''DemGenTrout-world.csv''. 

You can change the values of parameters with the sliders, then click the 'Change parameters' button, or you can keep the default values (to restore them, click the 'Restore parameters' button). Only parameters linked to stocking can be modified:
  
  * stocking-start and stocking-end, the time period of stoking in years.
  * stocking-coefficient, the intensity of stocking in relation to the number of wild trout present in stream L (typically, values from 0.50 to 0.70 reflect a moderate stoking).
  * survival-factor, the division factor for survival rate of stocked individuals of age 2 living in stream L in relation to the survival rate of wild trout of the same age class.
  * trout-spawning-prob, the probability of spawning for wild trout.
  * stocked-spawning-prob, the probability of spawning for hatchery trout.

Click the 'go' button to start the simulation (click 'go' again to stop it). The model can also be run weekly ('step-W' button), monthly ('step-M') or yearly ('step-Y'). The two plots on the left show abundance of wild trout, stocked trout and hybrids in both streams, and is updated each year at week 1. The six plots on the right vary with time, and represent the age distribution of wild trout, stocked trout and hybrids in both streams.

## THINGS TO NOTICE AND TO TRY

You can test the following parameterizations described in [Frank & Baret (2013)](http://dx.doi.org/10.1016/j.ecolmodel.2012.09.017): 

  * a spawning probability equal to 1 for all breeds, by setting the stocked-spawning-prob equal to 1.
  * a survival rate for freshly stocked trout similar to wild trout, by setting the survival-factor equal to 1.
  * the two previous modifications, by setting both stocked-spawning-prob and survival-factor equal to 1.

Stocking with hatchery fish thus shows a relatively weak impact on the demographic structure of the brook population (i.e., abundance around 3300 individuals after 35 years of simulation) provided that stocked trout had lower survival and spawning probabilities than wild trout. However, a drastic reduction in abundance of wild trout in stream C is observed (i.e., abundance around 1800 individuals after 35 years) in the latter case implementing a situation where hatchery and wild trout have similar survival and spawning probabilities.

## RELATED MODELS

DemGenTrout 1.0: This is the first version of the DemGenTrout model, as described in [Frank & Baret (2013)](http://dx.doi.org/10.1016/j.ecolmodel.2012.09.017).

DemGenTrout 1.1: In comparison with DemGenTrout 1.0, the initialisation of this version is faster.

DemGenTrout 1.2: This version includes features of DemGenTrout 1.1 (faster initialisation) and simulates a barrier to upstream spawning migration.

## CREDITS AND CITATION

The development of the model was funded by the "Fonds pour la formation a la Recherche dans l'Industrie et dans l'Agriculture" (F.R.I.A.). During the formulation phase, the report describing the inSTREAM model of [Railsback et al. (2009)](http://www.fs.fed.us/psw/publications/documents/psw_gtr218) has been an inspirational source. For the programming phase, many of the sample NetLogo models were of great use.

To refer to this model in academic publications, please use: Frank, B.M., Baret, P.V. (2013). Simulating brown trout demogenetics in a river/nursery brook system: The individual-based model DemGenTrout. Ecological Modelling 248: 184-202.  

## COPYRIGHT AND LICENSE

DemGenTrout 1.3 (2012)

Beatrice M. Frank

Earth and Life Institute

Universite catholique de Louvain

beatrice.frank@gmail.com

![CC BY-NC-SA 3.0](http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png)
 
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/.
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250

airplane
true
0
Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15

arrow
true
0
Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150

box
false
0
Polygon -7500403 true true 150 285 285 225 285 75 150 135
Polygon -7500403 true true 150 135 15 75 150 15 285 75
Polygon -7500403 true true 15 75 15 225 150 285 150 135
Line -16777216 false 150 285 150 135
Line -16777216 false 150 135 15 75
Line -16777216 false 150 135 285 75

bug
true
0
Circle -7500403 true true 96 182 108
Circle -7500403 true true 110 127 80
Circle -7500403 true true 110 75 80
Line -7500403 true 150 100 80 30
Line -7500403 true 150 100 220 30

butterfly
true
0
Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
Circle -16777216 true false 135 90 30
Line -16777216 false 150 105 195 60
Line -16777216 false 150 105 105 60

car
false
0
Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
Circle -16777216 true false 180 180 90
Circle -16777216 true false 30 180 90
Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
Circle -7500403 true true 47 195 58
Circle -7500403 true true 195 195 58

circle
false
0
Circle -7500403 true true 0 0 300

circle 2
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240

cow
false
0
Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
Polygon -7500403 true true 73 210 86 251 62 249 48 208
Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123

cylinder
false
0
Circle -7500403 true true 0 0 300

dot
false
0
Circle -7500403 true true 90 90 120

face happy
false
0
Circle -7500403 true true 8 8 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240

face neutral
false
0
Circle -7500403 true true 8 7 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Rectangle -16777216 true false 60 195 240 225

face sad
false
0
Circle -7500403 true true 8 8 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183

fish
false
0
Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
Circle -16777216 true false 215 106 30

flag
false
0
Rectangle -7500403 true true 60 15 75 300
Polygon -7500403 true true 90 150 270 90 90 30
Line -7500403 true 75 135 90 135
Line -7500403 true 75 45 90 45

flower
false
0
Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
Circle -7500403 true true 85 132 38
Circle -7500403 true true 130 147 38
Circle -7500403 true true 192 85 38
Circle -7500403 true true 85 40 38
Circle -7500403 true true 177 40 38
Circle -7500403 true true 177 132 38
Circle -7500403 true true 70 85 38
Circle -7500403 true true 130 25 38
Circle -7500403 true true 96 51 108
Circle -16777216 true false 113 68 74
Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240

house
false
0
Rectangle -7500403 true true 45 120 255 285
Rectangle -16777216 true false 120 210 180 285
Polygon -7500403 true true 15 120 150 15 285 120
Line -16777216 false 30 120 270 120

leaf
false
0
Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195

line
true
0
Line -7500403 true 150 0 150 300

line half
true
0
Line -7500403 true 150 0 150 150

pentagon
false
0
Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120

person
false
0
Circle -7500403 true true 110 5 80
Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
Rectangle -7500403 true true 127 79 172 94
Polygon -7500403 true true 195 90 240 150 225 180 165 105
Polygon -7500403 true true 105 90 60 150 75 180 135 105

plant
false
0
Rectangle -7500403 true true 135 90 165 300
Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90

sheep
false
0
Rectangle -7500403 true true 151 225 180 285
Rectangle -7500403 true true 47 225 75 285
Rectangle -7500403 true true 15 75 210 225
Circle -7500403 true true 135 75 150
Circle -16777216 true false 165 76 116

square
false
0
Rectangle -7500403 true true 30 30 270 270

square 2
false
0
Rectangle -7500403 true true 30 30 270 270
Rectangle -16777216 true false 60 60 240 240

star
false
0
Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108

target
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240
Circle -7500403 true true 60 60 180
Circle -16777216 true false 90 90 120
Circle -7500403 true true 120 120 60

tree
false
0
Circle -7500403 true true 118 3 94
Rectangle -6459832 true false 120 195 180 300
Circle -7500403 true true 65 21 108
Circle -7500403 true true 116 41 127
Circle -7500403 true true 45 90 120
Circle -7500403 true true 104 74 152

triangle
false
0
Polygon -7500403 true true 150 30 15 255 285 255

triangle 2
false
0
Polygon -7500403 true true 150 30 15 255 285 255
Polygon -16777216 true false 151 99 225 223 75 224

truck
false
0
Rectangle -7500403 true true 4 45 195 187
Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
Rectangle -1 true false 195 60 195 105
Polygon -16777216 true false 238 112 252 141 219 141 218 112
Circle -16777216 true false 234 174 42
Rectangle -7500403 true true 181 185 214 194
Circle -16777216 true false 144 174 42
Circle -16777216 true false 24 174 42
Circle -7500403 false true 24 174 42
Circle -7500403 false true 144 174 42
Circle -7500403 false true 234 174 42

turtle
true
0
Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99

wheel
false
0
Circle -7500403 true true 3 3 294
Circle -16777216 true false 30 30 240
Line -7500403 true 150 285 150 15
Line -7500403 true 15 150 285 150
Circle -7500403 true true 120 120 60
Line -7500403 true 216 40 79 269
Line -7500403 true 40 84 269 221
Line -7500403 true 40 216 269 79
Line -7500403 true 84 40 221 269

wolf
false
0
Polygon -7500403 true true 135 285 195 285 270 90 30 90 105 285
Polygon -7500403 true true 270 90 225 15 180 90
Polygon -7500403 true true 30 90 75 15 120 90
Circle -1 true false 183 138 24
Circle -1 true false 93 138 24

x
false
0
Polygon -7500403 true true 270 75 225 30 30 225 75 270
Polygon -7500403 true true 30 75 75 30 270 225 225 270

@#$#@#$#@
NetLogo 5.0.2
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
default
0.0
-0.2 0 1.0 0.0
0.0 1 1.0 0.0
0.2 0 1.0 0.0
link direction
true
0
Line -7500403 true 150 150 90 180
Line -7500403 true 150 150 210 180

@#$#@#$#@
1
@#$#@#$#@
