;==================== begin diffuse-individual.nls =============================
; diffuse-individual - random walk for individuals.  Respects non-wrapping
; world boundaries.
;
; By Rik Blok, 2013 <http://www.zoology.ubc.ca/~rikblok/wiki/doku.php>
;
; This is free and unencumbered software released into the public domain.
;
; Anyone is free to copy, modify, publish, use, compile, sell, or
; distribute this software, either in source code form or as a compiled
; binary, for any purpose, commercial or non-commercial, and by any
; means.
;
; Usage:
;
;   diffuse-individual _agents_ _distance_ _time-step_
;
; Example:
;
;   globals [ dt ]
;   to setup
;     set dt 0.1
;   end
;   to go
;     ifelse (well-mixed-switch?) [
;       ; turtles are well-mixed, can move anywhere
;       diffuse-individual turtles 0 dt
;     ][
;       ; move random direction, variance proportional to diffusion-const-slider * dt
;       diffuse-individual turtles diffusion-const-slider dt
;   end
;
; Known limitations:
;
;   2013-08-02 - doesn't consider spatial boundaries/restrictions
; 
; Revisions:
;
;   2013-08-02 - initial release by Rik Blok
;-------------------------------------------------------------------------------


to diffuse-individual [ 
    diffuse-individual-agents 
    diffuse-individual-diff-const 
    diffuse-individual-dt 
]
  if-else (diffuse-individual-diff-const = 0)
  [
    ; well-mixed, anybody could be anywhere
    ask diffuse-individual-agents [ setxy random-xcor random-ycor ]
  ][
    ; not well-mixed, do random walk
    ; see http://en.wikipedia.org/wiki/Random_walk#Relation_to_Wiener_process
    ; TODO: check, is this exactly the diffusion constant?
    let jump-size sqrt ( 4 * diffuse-individual-diff-const * diffuse-individual-dt )
    ask diffuse-individual-agents 
    [
      set heading random 360 
      jump jump-size 
    ]
  ]
end
;==================== end diffuse-individual.nls ===============================