User:Devon McCormick/DLA/cdla6Revised0.ijs

From J Wiki
Jump to: navigation, search

Referenced in this explanation and demonstration of performance timings.

NB.* cdla6Revised0.ijs: Diffusion-limited aggregation from
NB. as initially revised.

load 'coutil'
cocurrent 'dla'

NB.* grow: grow cluster by random DLA, one point at a time.
grow=: 3 : 0
   c=. 0
   point=: SZ release CTR
   while. y > c do.
       point=: walk point
       if. (point outOfBounds CTR)+.check_oob point do.
           point=: SZ release CTR
       else. if. check_collision point do.
               add_to_cluster point
               SZ=: CTR dist point
       c=. >: c

NB. State settings: set globals, initialize grid.
SZ=: 0                        NB.* SZ: Track maximum extent of the cluster
NB.* NBS: Neighboring points as complex numbers
NBS=: 1 1j1 0j1 _1j1 _1 _1j_1 0j_1 1j_1
SPC=: (3 : '1 (<<.-:$y) }y') 1000 1000$0     NB.* SPC: seed lattice in center.
CTR=: (3 : 'j./<.-:$y') SPC                  NB.* CTR: center point of lattice.

NB.* walk: Simple form of random walk: one-step.
walk=: 3 : 'y+(?#NBS){NBS'

NB.* dist: Calculate the max radius of the cluster
dist=: 4 : '>.SZ>.0{*.x-y'

NB.* release: Release a new particle distance x + 5 from center y
release=: 4 : '<.y+(x+5) r. 2p1*?0'

NB.* add_to_cluster: add point to cluster.
add_to_cluster=: 3 : 'SPC=: 1 (<+.y) } SPC'

NB. This probably could be improved: finds if point has landed on cluster by
NB. looking at the its 8 neighbors to see if there are any occupied cells.
NB.* check_collision: check if point within 1 cell of another point.
check_collision=: 3 : '1 e. ,(<([:<"1[: ]_1 0 1 +/~+.) y) { SPC'

NB.* check_oob: Check if point close to out-of-bounds (within 1 of any edge).
check_oob=: 13 : '(2+./ . >:y)+. y+./ . >:$SPC [ y=. >:+.y'
NB.* outOfBounds: Check if point has wandered far away from cluster.
outOfBounds=: 4 : '(SZ+10)<0{*.x-y'