; Title: Geomorphology ABM in CHANS
; Version 6.4
; Author: Li An
; Time of revision: March 16, 2016
; Purpose: For education and demonstration purpose. With initial input from the paper "Feedbacks in human-landscape systems: Lessons from the Waldo 
; Canyon Fire of Colorado, USA" by Chin, An, Florsheim et al.
;
; Major changes from the last version (Version 6.3)
; 1) This one shows the 3D landscape by adding the features in Jie's Geomorphology-3D-V3-Jie.nlogo model 
;    (saved under D:\1.Grants\5-CHES-ABMs\CHES-Models\River-Geomorphology-ABMs\Jie-2016-02-29\)

breed [households household]    ; Add on 02-19-2015 (v5.7)
breed [stayers stayer]          ; the acutral points of the landscape, added by Jie
breed [walkers walker]          ; turtles that move around the landscape, added by Jie
                                        
globals [  
  flow           ; This variable default to a number less than 65 cubic feet per second (cfs)
  storm?
  storm-duration ; How long the storm has lasted, which is a number between 1 and 96 units (one 
                 ; unit is 15 minutes
  change-rate    ; The change rate of sediment thickness, which is a function of flow.
  available-thick   
  rate-reduction ; The reduction % of change-rate (the change rate of sediment thickness), default to 0 for no reduction (add on 02-17-2015). 
  Total-sedi-chg ; Total change (often reduciton) of sediment for the entire river segments (add on 02-19-2015).
  PES            ; A variable for average PES-level of all the households --Add on 02-19-2015 (v5.6)
  PES-scores     ; An intermediate variable (v5.8)
  Tolerance      ; When the accumulated change/total possible change (27.685) ratio goes above this level, HHs increase PES by "PES-inflation"
  PES-inflation  ; New variable Add 02-25-2015: increment of PES at a qualified time (V6.1)
  degrade-level  ; A number between 0 (no degradation) and 1 (complete degradation). Add 02-25-2015 (V6.1)
  color-decider  ; Add on March 3, 2015.
  runtime        ; added by Jie, to record which run is it in
]

patches-own [    ; The elevation at the top of sediment, which is the sum of bedrock-elev and sediment-thick
 stream?         ; added by Jie
 elevation
 bedrock-elev    ; The elevation at the top of hardrock
 elevation-vis   ; elevation for landscape visulization purposes, added by Jie
 sediment-thick  ; The thickness of sediment that lies above hard rock
 patch-ID        ; ID of each cell or patch
 river-color     ; added by Jie
]

walkers-own [    ; added by Jie
  peak?          ; indicates whether a turtle has reached a "peak", that is, it can no longer go "uphill" from where it stands                 
]

households-own [ ; This procedure is new. Add on 02-19-2015 (v5.7)
  PES-level      ; The payment level for the PES program that may take 0 (no), 1 (low), 2 (moderate), and 3 (high) payment levels
]

to setup                 ; Set up both the environment (landscape) and agents. Only executed once each simulation
  clear-all  
  resize-world 0 39 0 39 0 9 ; added by Jie
  
  setup-patches          ; This procedure is defined below. It sets up the "world" or the landscape
  setup-landscape        ; added by Jie
  setup-household        ; Add on 02-19-2015 (v5.7)   It sets up households on the landscape
  set runtime 0          ; added by Jie
  reset-ticks
                         
  if (file-exists? "StreamDataOutput.csv") [
    file-delete "StreamDataOutput.csv" ; Version 3: Each run (instead of each tick), delete the file and whatever inside it.
  ]
end           

to setup-household       ; This procedure is new. Add on 02-19-2015 (v5.7, with changes in v5.8)
    
  ask patches [ if (stream? = false and pzcor = 3 and (random-float 16) < 1.0) [sprout-households 1]] ; Households are placed on
  ask households [                                              ; non-water places randomly at a probability of 1/16.
    set shape "house" set size 1.2 
    set color-decider random-float 1    ; Add this line to fix a little bug on March 03, 2015.
    if (color-decider <= 0.25) [set PES-level 0 set color red]      ; Add on 02-20-2015 (v5.8). Add set color red 02-26-2015
    if (color-decider > 0.25 and color-decider <= 0.5) [set PES-level 1 set color brown]   ; Add on 02-20-2015 (v5.8)
    if (color-decider > 0.5 and color-decider <= 0.75) [set PES-level 2 set color yellow]  ; Add on 02-20-2015 (v5.8)
    if (color-decider > 0.75) [set PES-level 3 set color green]                            ; Add on 02-20-2015 (v5.8)
    ;show PES-level    
    ]
 ;show count households
 ;show count households with [PES-level = 0]
 ;show count households with [PES-level = 1]
 ;show count households with [PES-level = 2]
 ;show count households with [PES-level = 3]
  calculate-avg-PES  
  ;adopt-NBs-PES
end   

; Start of Jie's new 

to setup-patches
  
  ask patches [

    set stream? false   ; By default, the cells are not stream but dryland
    set elevation 2040  ; Default elevation, added by Jie
    ;set bedrock-elev 5000     ; Changed by Jie
    ;set sediment-thick 500    ; Changed by Jie
    set patch-ID 1200
    set river-color sky ; added by Jie. color code: blue 105; sky 95; cyan 85
    if (pxcor = 4 and pycor = 7) [
      set stream? true
      set bedrock-elev 2039.50
      set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 1]
    ]
    if (pxcor = 5 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.50       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 2]
    ]
    if (pxcor = 6 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.50       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 3]
    ]
    if (pxcor = 7 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.50       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 4]
    ]
    if (pxcor = 8 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.53       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 5]
     ]
    if (pxcor = 9 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.44       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 6]
     ]
    if (pxcor = 10 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.33       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 7]
     ]
    if (pxcor = 11 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.30       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 8]
     ]
    if (pxcor = 12 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.28       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 9]
     ]
    if (pxcor = 13 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.33       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 10]
     ]
    if (pxcor = 14 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.24       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 11]
     ]
    if (pxcor = 15 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.14       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 12]
     ]
    if (pxcor = 16 and pycor = 7) [
      set stream? true       set bedrock-elev 2039.09       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 13]
     ]
    if (pxcor = 17 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.94       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 14]
     ]
    if (pxcor = 18 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.90       set sediment-thick 0.30
      if pzcor = 0 [set patch-ID 15]
     ]
    if (pxcor = 19 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.82       set sediment-thick 0.40
      if pzcor = 0 [set patch-ID 16]
     ]
    if (pxcor = 20 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.71       set sediment-thick 0.50
      if pzcor = 0 [set patch-ID 17]
     ]
    if (pxcor = 21 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.60       set sediment-thick 0.60
      if pzcor = 0 [set patch-ID 18]
     ]
    if (pxcor = 22 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.49       set sediment-thick 0.70
      if pzcor = 0 [set patch-ID 19]
     ]
    if (pxcor = 23 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.37       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 20]
     ]
    if (pxcor = 24 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.38       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 21]
     ]
    if (pxcor = 25 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.37       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 22]
     ]
    if (pxcor = 26 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.34       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 23]
     ]
    if (pxcor = 27 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.30       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 24]
     ]
    if (pxcor = 28 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.26       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 25]
     ]
    if (pxcor = 29 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.23       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 26]
     ]
    if (pxcor = 30 and pycor = 7) [
      set stream? true       set bedrock-elev 2038.27       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 27]
     ]
    if (pxcor = 31 and pycor = 8) [
      set stream? true       set bedrock-elev 2038.06       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 28]
     ]
    if (pxcor = 32 and pycor = 9) [
      set stream? true       set bedrock-elev 2037.96       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 29]
     ]
    if (pxcor = 33 and pycor = 10) [
      set stream? true       set bedrock-elev 2037.93       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 30]
     ]
    if (pxcor = 34 and pycor = 11) [
      set stream? true       set bedrock-elev 2037.92       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 31]
     ]
    if (pxcor = 35 and pycor = 12) [
      set stream? true       set bedrock-elev 2037.93       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 32]
     ]
    if (pxcor = 35 and pycor = 13) [
      set stream? true       set bedrock-elev 2038.04       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 33]
     ]
    if (pxcor = 35 and pycor = 14) [
      set stream? true       set bedrock-elev 2037.87       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 34]
     ]
    if (pxcor = 35 and pycor = 15) [
      set stream? true       set bedrock-elev 2037.81       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 35]
     ]
    if (pxcor = 35 and pycor = 16) [
      set stream? true       set bedrock-elev 2037.81       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 36]
     ]
    if (pxcor = 35 and pycor = 17) [
      set stream? true       set bedrock-elev 2037.77       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 37]
     ]
    if (pxcor = 35 and pycor = 18) [
      set stream? true       set bedrock-elev 2037.59       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 38]
     ]
    if (pxcor = 35 and pycor = 19) [
      set stream? true       set bedrock-elev 2037.45       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 39]
     ]
    if (pxcor = 35 and pycor = 20) [
      set stream? true       set bedrock-elev 2037.45       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 40]
     ]
    if (pxcor = 35 and pycor = 21) [
      set stream? true       set bedrock-elev 2037.45       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 41]
     ]
    if (pxcor = 35 and pycor = 22) [
      set stream? true       set bedrock-elev 2037.35       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 42]
     ]
    if (pxcor = 35 and pycor = 23) [
      set stream? true       set bedrock-elev 2037.29       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 43]
     ]
    if (pxcor = 35 and pycor = 24) [
      set stream? true       set bedrock-elev 2037.25       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 44]
     ]
    if (pxcor = 35 and pycor = 25) [
      set stream? true       set bedrock-elev 2037.11       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 45]
     ]
    if (pxcor = 35 and pycor = 26) [
      set stream? true       set bedrock-elev 2037.00       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 46]
     ]
    if (pxcor = 35 and pycor = 27) [
      set stream? true       set bedrock-elev 2036.97       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 47]
     ]
    if (pxcor = 35 and pycor = 28) [
      set stream? true       set bedrock-elev 2036.92       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 48]
     ]
    if (pxcor = 35 and pycor = 29) [
      set stream? true       set bedrock-elev 2036.88       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 49]
     ]
    if (pxcor = 35 and pycor = 30) [
      set stream? true       set bedrock-elev 2036.87       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 50]
     ]
    if (pxcor = 35 and pycor = 31) [
      set stream? true       set bedrock-elev 2036.95       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 51]
     ]
    if (pxcor = 35 and pycor = 32) [
      set stream? true       set bedrock-elev 2036.88       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 52]
     ]
    if (pxcor = 35 and pycor = 33) [
      set stream? true       set bedrock-elev 2036.83       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 53]
     ]
    if (pxcor = 35 and pycor = 34) [
      set stream? true       set bedrock-elev 2036.65       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 54]
     ]
    if (pxcor = 35 and pycor = 35) [
      set stream? true       set bedrock-elev 2036.52       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 55]
     ]
    if (pxcor = 21 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.45       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 70]
     ]
    if (pxcor = 22 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.35       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 69]
     ]
    if (pxcor = 23 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.30       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 68]
     ]
    if (pxcor = 24 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.40       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 67]
     ]
    if (pxcor = 25 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.55       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 66]
     ]
    if (pxcor = 26 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.60       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 65]
     ]
    if (pxcor = 27 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.53       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 64]
     ]
    if (pxcor = 28 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.46       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 63]
     ]
    if (pxcor = 29 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.44       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 62]
     ]
    if (pxcor = 30 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.45       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 61]
     ]
    if (pxcor = 31 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.48       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 60]
     ]
    if (pxcor = 32 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.54       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 59]
     ]
    if (pxcor = 33 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.60       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 58]
     ]
    if (pxcor = 34 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.63       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 57]
     ]
    if (pxcor = 35 and pycor = 36) [
      set stream? true       set bedrock-elev 2036.57       set sediment-thick 0.80
      if pzcor = 0 [set patch-ID 56]
    ]

    ; added by Jie
    if (pzcor = 0 and patch-ID < 71) [
      set elevation bedrock-elev + sediment-thick
      set pcolor river-color
    ]
  ]
end
   

to setup-landscape   ; added by Jie

  ; make a landscape with hills and valleys
  ask n-of 10 patches with [pzcor = 0 and stream? = false] [
    set elevation 5000
  ]

  ask patches [ set elevation-vis elevation]
  ; slightly smooth out the landscape
  repeat 10 [
    ; the terrain does not require use of the 3D patch grid just the 3D
    ; space to display the terrain so when we diffuse add anything
    ; diffusing up and down back to the first layer of patches.
    diffuse elevation-vis 1
    ask patches with [pzcor = 0 and stream? = true] [
      set elevation-vis 2040
    ]
  ]

  let max-elevation max [elevation-vis] of patches with [pzcor = 0]
  let min-elevation min [elevation-vis] of patches with [pzcor = 0]

  ; use turtles and links to display the landscape
  ; each patch has one stayer linking to neighboring stayers
  ask patches with [pzcor = 0] [
    sprout-stayers 1 [
      create-links-with turtles-on neighbors
      ht
    ]
  ]
  ; each stayer is set at a height proportional to the patch's elevation
  ask stayers [
    set zcor ((elevation-vis - min-elevation) / (max-elevation - min-elevation)) * max-pzcor
  ]

  ; put some turtles on patch centers in the landscape
  ;ask n-of 80 stayers [
      ;hatch-walkers 1 [
        ;st
        ;set peak? false
        ;set color red
        ;pd
        ;set pen-size 3
      ;]
   ;]
end


to flow-change
  
  ifelse (ticks = 27 or (ticks >= 60 and ticks <= 63)) [  ; Flow change reflects whether it rains & the river flow rises on some days
    set flow 70      ; Days 27 and 59, 60, 61, 62, and 63 are days with storms. The number 70 is just one between 65 and 310
    set storm? true
  ]
  [ set flow 40
    set storm? false
  ]
  
  ifelse (storm? = false)   ; The above ifelse clause sets what days would be storm days
  [set storm-duration 0]    ; On days without storms, the storm duration is zero
  [                         ; On days with a storm, the storm duration is set to different numbers according to Anne's data
    if (ticks = 27) [  ; 
      set storm-duration 3  ; Changed back from 1 to 3--did on 10/15/2014 evening based on Version 3.
    ]    
    if (ticks = 60) [       ; 
      set storm-duration 2  ;
    ]  

    if (ticks >= 61 and ticks <= 63 ) [
      set storm-duration 80 ; Used to be 96--change made on 10/15/2014.
    ]
  ]
end

to pave-channel
  ifelse (ticks = pave-time and build-pavement? = true)
    [
      let List_of_segments []  
        
      ask patches with [pcolor = blue] [       ; pavement only happen on stream patches (blue)   
        set List_of_segments  lput patch-ID List_of_segments   ; Put all stream patches into the above list    
      ]  ; The end 
     
      foreach sort List_of_segments [   
    
        ask patches with [patch-ID = ?] [
          if (patch-ID = 15) [
            set sediment-thick 0    set bedrock-elev bedrock-elev + 0.3
          ]        
          if (patch-ID = 16) [
            set sediment-thick 0    set bedrock-elev bedrock-elev + 0.4
          ]            
          if (patch-ID = 17) [
            set sediment-thick 0    set bedrock-elev bedrock-elev + 0.5
          ]                
          if (patch-ID = 18) [
            set sediment-thick 0    set bedrock-elev bedrock-elev + 0.6
          ]        
          if (patch-ID = 19) [
            set sediment-thick 0    set bedrock-elev bedrock-elev + 0.7
          ]            
          if (patch-ID <= 50 and patch-ID >= 20) [
            set sediment-thick 0    set bedrock-elev bedrock-elev + 0.8
          ]            
          set elevation bedrock-elev + sediment-thick   
        ]   ; End of ask patches
      ] ; End of foreach loop  
    ]  
    [] ;Otherwise do nothing
end

to calculate-avg-PES       ; Add on 02-20-2015 (v5.8)
  
  set PES-scores  0    ; This procedure will be executed each step so we need to set it to zero at the beginning
  ask households [    
    set PES-scores PES-scores + PES-level    
  ]
  
  let PES-avg PES-scores / (count households) 
  set PES PES-avg ; Take out round on 02-23-2015 (v5.9)   
end

to increase-pay   ; Add on 02-25-2015 (v6.1)
  
  set Tolerance Allowed-degradation   ; To be determined and experimented by the user: 0 (do not allow any degradation)
                                      ; and 1 (all levels of degradation).
  ;print (word "****************Time****************" ticks word "Total degrade-level " degrade-level)
  set degrade-level Total-sedi-chg / 27.685    ; A measure of landscape degradation between 0 (best) and 1 (worst)
  
  ifelse (degrade-level > Tolerance and storm? = true) [set PES-inflation PES-increase] ; Only at times of storm,
  [set PES-inflation 0]               ; people would increase their PES at the magnitude of PES-inflation.
  
  ask households [ 
    ;if (ticks >= 60 and ticks <= 63) [
      ;show (word "Old " PES-level)
    ;]

    ifelse PES-level + PES-inflation < 3 [set PES-level PES-level + PES-inflation]
      [set PES-level 3]               ; Set a cap for PES at 3.
    
    ;if (ticks >= 60 and ticks <= 63) [
      ;show (word "New " PES-level)
    ;]   
  ]
  
end 

to adopt-NBs-PES       ; Each household looks at his neighbors and adopt their PES levels. Add on 02-20-2015 (v5.8)
  
  ask households [                        ; This loops through all household agents
    
    ;print (word "My household ID is "self word " with my earlier PES level = " PES-level " units")         ; Add 02-21-2015
        
    set PES-scores  0  ; These two variables need to be "emptied" as they are global variables
       
    ask other households in-radius Neighborhood-size  ; The neighborhood size (a radius) is set by the user (0 to 40)
      [set color white ; All households that are considered as neighbors are set to be white
        ;show (word "As a neighbor my ID is "self word " and My PES = " PES-level)      ; Add 02-21-2015
        set   PES-scores PES-scores + PES-level       ; Each neighbor's PES level is accumulated and assigned to PES-scores
      ]  
      
    ifelse ((count other households in-radius Neighborhood-size) != 0) ; If one or more households exist in my neighborhood
    [set PES-scores PES-scores / (count other households in-radius Neighborhood-size)] ; Calculate the average scores
    []   ; If my neighborhood is empty, do nothing
    
    ifelse ((count other households in-radius Neighborhood-size) != 0) ; If one or more households exist in my neighborhood
    [set PES-level PES-scores]                            ; Assign the average PES scores of my neighbors to me
    []                                                    ; Keep my earlier PES level if my neighborhood is empty
    
    ;show (word "For "self word ", my new PES level = " PES-level " units") ; Print the new PES level after considering my neighbors's
  ]
end

; Alternative 1: When storm comes, the sediments in all segments are removed to the same extent (not  very realistic)

to sediment-change-no-order                                        ; Changes made on 02-26-2015 (v6.2) 
  
  if (PES <= 1) [set rate-reduction 0.5 * PES]                        ; Add on 02-26-2015 (v6.2)   
  if (PES <= 2 and PES > 1) [set rate-reduction 0.2 * PES + 0.2]      ; Add on 02-26-2015 (v6.2)   
  if (PES <= 3 and PES > 2) [set rate-reduction 0.1 * PES + 0.6]      ; Add on 02-26-2015 (v6.2)   

  ask patches with [pcolor = blue] [
    
    ifelse (storm? = true)[
      set change-rate -0.113 * (1 - rate-reduction) * storm-duration ; add on 02-17-2015
    ]
    [set change-rate 0]
    
    ifelse (sediment-thick > 0 and storm? = true) [
    
      ifelse (sediment-thick > abs change-rate) [
        set sediment-thick sediment-thick + change-rate
        set Total-sedi-chg Total-sedi-chg + abs change-rate  ; Add 02-26-2015
      ]
      [
        set sediment-thick 0     
        set Total-sedi-chg Total-sedi-chg + sediment-thick
      ]
    ]
    []
    
    set elevation bedrock-elev + sediment-thick   
  ] 
end

; Alternative 2: When storm comes, the sediments in upstream segments are first removed:

to sediment-change-in-order 
  ;set PES 0                                  ; PES can take 0, 1, 2, or 3. To be linked to other variables (v5.6)
  if (PES <= 1) [set rate-reduction 0.5 * PES]               
  if (PES <= 2 and PES > 1) [set rate-reduction 0.2 * PES + 0.2]      ; Hypothetical relationship 02-23-2015 (v5.9)
  if (PES <= 3 and PES > 2) [set rate-reduction 0.1 * PES + 0.6]      ; Hypothetical relationship 02-23-2015 (v5.9)  
       
  let List_of_segments []                  ; Create a list as a container of stream patches
  
  ask patches with [pcolor = blue] [       ; Sediment change only happen on stream patches (blue)   
    
    set List_of_segments  lput patch-ID List_of_segments   ; Put all stream patches into the above list
    
    ifelse (storm? = true)[                ; Only for days with storm
      set change-rate -0.113 * (1.0 - rate-reduction) * storm-duration ; A negative number for sediment change per day. change on 02-17-2015
    ]
    [set change-rate 0]                    ; For days without storm, the change rate is zero  
      
  ]  ; The end of ask patches clause
  
  ifelse (storm? = false) [set available-thick 0]  ; Add on 02-25-2015
  [set available-thick 0.113 * (1.0 - rate-reduction) * storm-duration + available-thick] ; Note for days w/o storm, storm-duration =0. 
                                                      ; Change on 02-17-2015. Also + available-thick is added to make it carried over
                                                      ; to next step
  set Total-sedi-chg Total-sedi-chg + available-thick   ; Total amount of sediment change
   
  foreach sort List_of_segments [   
  
    ask patches with [patch-ID = ?] [ 
   
      if(storm? = true and available-thick > 0) [                   ; This "if" clause is added 02-19-2015
        ifelse (available-thick > sediment-thick) [                 ; This "if" condition limits to days with storm
          set available-thick available-thick - sediment-thick      ; The first available segment has its amount of sediment-thick removed
          set sediment-thick 0                                      ; As the result, its sediment is set to zero
        ]
        [
          set sediment-thick sediment-thick - available-thick
          set available-thick 0
        ]
      ]  ; End of the if(storm? = true and available-thick > 0) statement 
      set elevation bedrock-elev + sediment-thick        ; Finally, profile elevation is the sum of bedrock elevation     
    ]   ; End of ask patches
  ] ; End of foreach loop
  
end

to show-river-flow
  ; added by Jie
  set runtime runtime + 1
  ask patches with [patch-ID < 71] [
    set river-color (85 + 10 * ((runtime + patch-ID + 2) mod 3))
    set pcolor river-color
  ]
end

to do-plots

  set-current-plot "Profile elevations over segments"

  clear-plot
  
  let List_of_segments []
  let List_of_elev []
 
  ask patches with [patch-ID < 71] [ ; Changed by Jie

     set List_of_segments  lput patch-ID List_of_segments  
     set List_of_elev lput elevation List_of_elev
  ] 
  ;show sort List_of_segments   ; No need to print them except for code debugging 
  ;show sort List_of_segments
  ;show List_of_elev

  foreach sort List_of_segments [
    ask patches with [patch-ID = ?] [
      plotxy patch-ID elevation
    ]
  ]
  
  let mymean mean List_of_elev   
 
End 

; Note: remove the write-to-file part if it aims at creating a web-based Netlogo model.
to write-to-file   ; Added on Version 3.0, Oct 14, 2014  

  let List_of_segments []                  ; Create a list as a container of stream patches
  
  ask patches with [pcolor = blue] [       ; Only select stream patches   
    set List_of_segments  lput patch-ID List_of_segments   ; Put all stream patches into the list
  ]   

  file-open "StreamDataOutput.csv"
  
  foreach sort List_of_segments [
    ask patches with [patch-ID = ?] [
      file-type (word self ",")            ; This line print the patch coordinates
      file-type (word patch-ID ",")
      file-type (word ticks ",")
      file-type (word storm? ",")          ; Add on 02-19-2015
      file-type (word bedrock-elev ",")
      file-type (word sediment-thick ",")
      file-type (word elevation ",")
      file-type (word Total-sedi-chg ",")  ; Add on 02-19-2015
      file-type (word PES ",")             ; Add on 02-28-2015
      file-print "" ; This line is necessary; otherwise all data are written into one line
    ]
  ]
  file-close  
end

to show-data                   ; Add on 02-23-2015 (v5.9)
  
  if ticks = Simu-span - 1 [
    show word "The accumulated sediment = "Total-sedi-chg
    show word "The final Average PES level = " PES
    ]
end

to go
  if ticks >= Simu-span [ stop ]   

  flow-change
  ifelse (Sediment-chg-order? = true) [
    sediment-change-in-order
  ]
  [sediment-change-no-order]
  adopt-NBs-PES                ; Add on 02-20-2015 (v5.8)
  pave-channel
  show-river-flow              ; added by Jie
  increase-pay                 ; Add 02-25-2015 (V6.1)
  calculate-avg-PES            ; Add on 02-21-2015 (v5.8)
  do-plots
  write-to-file 
  show-data                    ; Add on 02-23-2015 (v5.9)
  tick
end
@#$#@#$#@
GRAPHICS-WINDOW
0
0
530
551
-1
-1
13.0
1
10
1
1
1
0
1
1
1
0
39
0
39
0
9
1
0
1
ticks
30.0

SLIDER
64
452
236
485
PES-increase
PES-increase
0
0.5
0.4
0.05
1
NIL
HORIZONTAL

SLIDER
65
522
237
555
Allowed-degradation
Allowed-degradation
0
1
1
0.05
1
NIL
HORIZONTAL

SLIDER
66
591
238
624
Neighborhood-size
Neighborhood-size
0
40
12
1
1
NIL
HORIZONTAL

SLIDER
64
126
236
159
Simu-span
Simu-span
0
400
200
1
1
NIL
HORIZONTAL

SLIDER
64
202
236
235
pave-time
pave-time
0
450
392
1
1
NIL
HORIZONTAL

SWITCH
66
284
213
317
build-pavement?
build-pavement?
1
1
-1000

SWITCH
66
364
237
397
Sediment-chg-order?
Sediment-chg-order?
0
1
-1000

PLOT
504
128
704
278
Average PES over time
NIL
NIL
0.0
10.0
0.0
10.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" "plot PES"

PLOT
283
296
483
446
Total sediment removal
Time
Sediment total
0.0
210.0
0.0
17.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" "plot Total-sedi-chg"

BUTTON
288
131
352
164
NIL
Setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1

BUTTON
407
131
470
164
Run
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1

PLOT
510
469
710
619
Profile elevations over segments
Segements
Elevation
0.0
72.0
2036.0
2040.0
true
false
"" "clear-plot"
PENS
"default" 1.0 0 -16777216 true "" "do-plots"

PLOT
502
298
702
448
Average profile elevations over time
Time
Elevation
0.0
210.0
2037.0
2040.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" "plot mean [elevation] of patches with [patch-ID < 71]"

PLOT
282
468
482
618
Elevation of segments #1 ~ #10
Segment IDs
Profile elevation
0.0
12.0
2037.0
2040.0
true
false
"" "clear-plot"
PENS
"default" 1.0 1 -5298144 true "" "plotxy 1 mean [elevation] of patches with [patch-ID = 1]"
"pen-1" 1.0 1 -7500403 true "" "plotxy 2 mean [elevation] of patches with [patch-ID = 2]"
"pen-2" 1.0 1 -2674135 true "" "plotxy 3 mean [elevation] of patches with [patch-ID = 3]"
"pen-3" 1.0 1 -955883 true "" "plotxy 4 mean [elevation] of patches with [patch-ID = 4]"
"pen-4" 1.0 1 -6459832 true "" "plotxy 5 mean [elevation] of patches with [patch-ID = 5]"
"pen-5" 1.0 1 -1184463 true "" "plotxy 6 mean [elevation] of patches with [patch-ID = 6]"
"pen-6" 1.0 1 -10899396 true "" "plotxy 7 mean [elevation] of patches with [patch-ID = 7]"
"pen-7" 1.0 1 -13840069 true "" "plotxy 8 mean [elevation] of patches with [patch-ID = 8]"
"pen-8" 1.0 1 -14835848 true "" "plotxy 9 mean [elevation] of patches with [patch-ID = 9]"
"pen-9" 1.0 1 -11221820 true "" "plotxy 10 mean [elevation] of patches with [patch-ID = 10]"

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

(a general understanding of what the model is trying to show or explain)

## HOW IT WORKS

(what rules the agents use to create the overall behavior of the model)

## HOW TO USE IT

(how to use the model, including a description of each of the items in the Interface tab)

## THINGS TO NOTICE

(suggested things for the user to notice while running the model)

## THINGS TO TRY

(suggested things for the user to try to do (move sliders, switches, etc.) with the model)

## EXTENDING THE MODEL

(suggested things to add or change in the Code tab to make the model more complicated, detailed, accurate, etc.)

## NETLOGO FEATURES

(interesting or unusual features of NetLogo that the model uses, particularly in the Code tab; or where workarounds were needed for missing features)

## RELATED MODELS

(models in the NetLogo Models Library and elsewhere which are of related interest)

## CREDITS AND REFERENCES

(a reference to the model's URL on the web if it has one, as well as any other necessary credits, citations, and links)
@#$#@#$#@
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
15
Circle -1 true true 203 65 88
Circle -1 true true 70 65 162
Circle -1 true true 150 105 120
Polygon -7500403 true false 218 120 240 165 255 165 278 120
Circle -7500403 true false 214 72 67
Rectangle -1 true true 164 223 179 298
Polygon -1 true true 45 285 30 285 30 240 15 195 45 210
Circle -1 true true 3 83 150
Rectangle -1 true true 65 221 80 296
Polygon -1 true true 195 285 210 285 210 240 240 210 195 210
Polygon -7500403 true false 276 85 285 105 302 99 294 83
Polygon -7500403 true false 219 85 210 105 193 99 201 83

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 -16777216 true false 253 133 245 131 245 133
Polygon -7500403 true true 2 194 13 197 30 191 38 193 38 205 20 226 20 257 27 265 38 266 40 260 31 253 31 230 60 206 68 198 75 209 66 228 65 243 82 261 84 268 100 267 103 261 77 239 79 231 100 207 98 196 119 201 143 202 160 195 166 210 172 213 173 238 167 251 160 248 154 265 169 264 178 247 186 240 198 260 200 271 217 271 219 262 207 258 195 230 192 198 210 184 227 164 242 144 259 145 284 151 277 141 293 140 299 134 297 127 273 119 270 105
Polygon -7500403 true true -1 195 14 180 36 166 40 153 53 140 82 131 134 133 159 126 188 115 227 108 236 102 238 98 268 86 269 92 281 87 269 103 269 113

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 3D 5.1.0
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
default
0.0
-0.2 0 0.0 1.0
0.0 1 1.0 0.0
0.2 0 0.0 1.0
link direction
true
0
Line -7500403 true 150 150 90 180
Line -7500403 true 150 150 210 180

@#$#@#$#@
0
@#$#@#$#@
