cracks_2009


Information

Created with NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.



Procedures

NetLogo Version: NetLogo 4.0.4

;; SUMMARY
;;;; Randomly punching holes in a sheet of material.
;; COPYRIGHT & LICENSE
;;;; Copyright (C) 2005 James P. Steiner
;;;; Some Rights Reserved.
;;;; Creative Commons Attribution-NonCommercial-ShareAlike License v. 2.0.
;;;; Visit http://creativecommons.org/licenses/by-nc-sa/2.0/ for more information.
;;

globals [ cracked! cells borders edge-cells cell-count flaw-count t
          edge1-cells edge2-cells edge4-cells edge8-cells border-width
          holes 
        ]
          
          
patches-own [ cracked? connected? voided? edges edge1 edge2 edge4 edge8 border?]


to setup
    ca
    set cracked! false
    
    set border-width 1 + int(flaw-size / 2)
    ask patches
    [
      set cracked? false
      set connected? false
      set voided? false
      set border? false
      set edge1 false
      set edge2 false
      set edge4 false
      set edge8 false
      set edges [0]
    ]
    set cells patches with    [     (abs pxcor) < max-pxcor - border-width + 1
                                and (abs pycor) < max-pycor - border-width + 1]
    set borders patches with  [     (abs pxcor) >= max-pxcor - border-width + 1
                                or  (abs pycor) >= max-pycor - border-width + 1]
                                
    set edge-cells cells with [ is-edge1? or is-edge2? or is-edge4? or is-edge8? ]

    set edge1-cells edge-cells with [ is-edge1? ]
    set edge2-cells edge-cells with [ is-edge2? ]
    set edge4-cells edge-cells with [ is-edge4? ]
    set edge8-cells edge-cells with [ is-edge8? ]
                                
    ask borders [ set pcolor black  set connected? false set border? true ]
    ask cells [ set pcolor white ]
    set cell-count count cells
    set flaw-count 0
    setup-plotting
end                           


to crack
    
    add-flaw
    propigate-edges
    if remove-voids? and ticks mod frequency = 0 [ remove-voids ]
    color-cells
    plot flaw-count
    set cracked! any? cells with [ (edge1 and edge2) or (edge4 and edge8) ]
    tick
    if cracked! [ stop ]
end


to-report check-if-cracked?
 report true
end


to add-flaw
   let new-x 0
   let new-y 0
  
   ifelse lines?
   [ line-flaw ]
   [ dot-flaw ]
end

to dot-flaw
   ask one-of cells 
   [ ask cells in-radius flaw-size  with [ not cracked? ]
     [ crack-it
     ]
   ]
   set holes holes + 1
end

to line-flaw
   let cell one-of cells
   let angle random 360
   let fx [ pxcor - .5 + random-float 1 ] of cell
   let fy [ pycor - .5 + random-float 1 ] of cell
   let fdx sin angle
   let fdy cos angle
   repeat flaw-size
   [ ask patch fx fy
     [ if not (cracked? or border? )[ crack-it ]
     ]
     set fx fx + fdx
     set fy fy + fdy
   ]
   set holes holes + 1
end

to crack-it
      set cracked? true
      set pcolor black
      set flaw-count flaw-count + 1
      set edge1 is-edge1?
      set edge2 is-edge2?
      set edge4 is-edge4? 
      set edge8 is-edge8?
end


to-report is-edge1?
  report pxcor = max-pxcor - border-width
end


to-report is-edge2?
  report pxcor = border-width + min-pxcor
end


to-report is-edge4?
  report pycor = max-pycor - border-width
end


to-report is-edge8?
  report pycor = border-width + min-pycor
end

to propigate-edges
    ;; marks flaws with the edge the flaw is connected to, if any.
   
        propigate-edge1
        propigate-edge2
        propigate-edge4
        propigate-edge8    
end


to propigate-edge1
    let more? 0
  let cracked-cells 0
  let loose-cells 0
  

    set cracked-cells cells with [ cracked? ]
    
    set loose-cells cracked-cells with [ not edge1 and any? neighbors with [ edge1 ] ]
    set more? any? loose-cells
    while [ more? ]
    [ ask loose-cells [ set edge1 true ]
      set loose-cells cracked-cells with [ not edge1 and any? neighbors with [ edge1 ] ]
      set more? any? loose-cells
    ]
end


to propigate-edge2
    let more? 0
  let loose-cells 0
  let cracked-cells 0
  
    
    set cracked-cells cells with [ cracked? ]
    
    set loose-cells cracked-cells with [ not edge2 and any? neighbors with [ edge2 ] ]
    set more? any? loose-cells
    while [ more? ]
    [ ask loose-cells [ set edge2 true ]
      set loose-cells cracked-cells with [ not edge2 and any? neighbors with [ edge2 ] ]
      set more? any? loose-cells
    ]
end


to propigate-edge4
    let more? 0
  let loose-cells 0
  let cracked-cells 0
  
    
    set cracked-cells cells with [ cracked? ]
    
    set loose-cells cracked-cells with [ not edge4 and any? neighbors with [ edge4 ] ]
    set more? any? loose-cells
    while [ more? ]
    [ ask loose-cells [ set edge4 true ]
      set loose-cells cracked-cells with [ not edge4 and any? neighbors with [ edge4 ] ]
      set more? any? loose-cells
    ]
end


to propigate-edge8
    let more? 0
  let loose-cells 0
  let cracked-cells 0
  
    
    set cracked-cells cells with [ cracked? ]
    
    set loose-cells cracked-cells with [ not edge8 and any? neighbors with [ edge8 ] ]
    set more? any? loose-cells
    
    while [ more? ]
    [ ask loose-cells [ set edge8 true ]
      set loose-cells cracked-cells with [ not edge8 and any? neighbors with [ edge8 ] ]
      set more? any? loose-cells
    ]
end


to remove-voids
    check-voids ;; finds uncracked cells not connected to any edge
    crack-voids ;; makes uncracked cells not connected to any edge drop out
end


to check-voids
    let more? 0
  let whole-cells 0
  let unconnected-cells 0
  
    ask cells [ set connected? false ]
    
    ask edge-cells with [ not cracked? ]
    [ set connected? true ]
    
    set whole-cells cells with [ not cracked? ]
    
    set unconnected-cells whole-cells with [ not connected? and any? neighbors4 with [connected?] ]
    set more? any? unconnected-cells

    while [ more? ]
    [ ask unconnected-cells [ set connected? true ]
      set unconnected-cells whole-cells with [ not connected? and any? neighbors4 with [connected?] ]
      set more? any? unconnected-cells
    ]  
end

to crack-voids
    ask cells with [ not connected? and not cracked? ]
    [ set voided? true crack-it ]
end


to color-cells
    ask cells with [ cracked? and (edge1 or edge2 or edge4 or edge8)]
    [ set-cell-color ]
end 


to set-cell-color    
  let rr 0
  let bb 0
  let gg 0

    if edge1 [ set rr 194 ]
    if edge2 and not edge4 [ set gg 127 ]
    if edge4 and not edge2 [ set gg 194 ]
    if edge2 and edge4 [ set gg 255 ]
    if edge8 [ set bb 194 ]
    set pcolor rgb rr gg bb
end


to setup-plotting
  set-current-plot "Crack-Plot"
  set-current-plot-pen "Cracked"
end


to mark-time [ mssg ]
    print mssg + timer
end


                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download cracks_2009 --