globals [ mouse-clicked ;; keeps track of click-and-hold mouse-double-click ;; set to true if two mouse clicks are registerd in a quarter second clicked-arrow ;; who was clicked on... grid ;; this is a lines of the grid arrow ;; is a agents mylist ;; is a colluns and lines in order of paint p ;; is a variable that defines the probability ] breed [ line ] breed [ circle ] breed [ square ] breed [ arrows ] arrows-own [ collided? bounced? ] ;; SET UP THE WORLD to startup setup end to setup ca ;; Color the patches so they're easier to see ask patches [ set pcolor white ] ask patches with [ abs pxcor = max-pxcor or abs pycor = max-pycor ] [ set pcolor black ] setup-turtles-grid setup-turtles-arrow set mouse-clicked false set mouse-double-click false set clicked-arrow nobody end to click-arrows ;; detects a single mouse click if not mouse-clicked and mouse-down? [ ;; detects if this single mouse click is soon after another ifelse timer <= .15 [ set mouse-double-click true ] [ set mouse-double-click false] ;; everytime the mouse is clicked, the timer starts reset-timer set mouse-clicked true ;; if there are turtles at the current mouse location, then pick one ;; this if statement keeps the program from having problems if ;; you click on an empty patch if any? arrows-at round mouse-xcor round mouse-ycor [ set clicked-arrow one-of arrows-at round mouse-xcor round mouse-ycor ] ] ;; if a turtle is only clicked, then it moves to match the mouse if is-agent? clicked-arrow and not mouse-double-click [ ask clicked-arrow [ setxy mouse-xcor mouse-ycor ] ] ;; detects raising the mouse button if mouse-clicked and not mouse-down? [ set mouse-clicked false if is-agent? clicked-arrow [ ;; center up arrow ask clicked-arrow [ setxy round xcor round ycor ] set clicked-arrow nobody ] ] end to setup-turtles-grid set-default-shape line "line" ;; draw vertical lines ask patches with [ pycor = 0 ] [ sprout 1 [ set breed line set color black set heading 0 set size world-height ] ] ;; draw horizontal lines ask patches with [ pxcor = 0 ] [ sprout 1 [ set breed line set color black set heading 90 set size world-width ] ] end to setup-turtles-arrow set-default-shape arrows "arrows" let spaces count patches with [ pcolor = white ] if number > spaces * .5 [ set number precision (spaces * .5) ( -2) ] create-custom-arrows number [ let kind random 4 set color item kind [ blue green red brown ] set heading item kind [ 0 90 180 270 ] setxy (plus-minus (max-pxcor - 1)) (plus-minus (max-pycor - 1)) while [ any? other-arrows-here or abs pxcor = max-pxcor or abs pycor = max-pycor ] [ fd 1 ] ] end to-report plus-minus [ range ] report (range - random (range * 2)) end to collision ;; particle procedure ;; two cases of collisions ;; case 1: arrows positioned like this: ;; |__|__|__| ;; |=>|__|<=| ;; |__|__|__| ;; in this case, next move will cause both arrows to occupy same patch ;; so, to detect collision, arrows check for another arrow on the same patch ;; with the opposite heading ;; after this reflection, arrows will collide again ;; case 2: ;; |__|__|__| ;; |=>|<=|__| ;; |__|__|__| ;; in this case, the next move will cause the arrows to swap places ;; so, at no time are both arrows on the same patch ;; if this case should be counted as a collision, if is required for ;; arrows to check for arrow on next patch with opposite heading ;; to avoid the need for case 2, one could make arrows move and detect "without-interruption" ;; so that an arrow will move and detect the collision before the other arrow has a chance to move. ;; in this method, it is up to the detecting arrow to tell the other arrow that a collision has occured. ;; this could result in multiple collisions and reflections in one turn set collided? any? other-arrows-here with [180 = subtract-headings heading (heading-of myself)] if random-float 1.0 < p ;; swap colors with "me" [ let me self let temp-color color set color color-of me set color-of me temp-color ] if not collided? [ let me self set collided? any? ( value-from (patch-ahead 0) [arrows-here with [180 = subtract-headings heading (heading-of me)] ] ) ] end to limits ;; turtle procedure ; check: hitting left or right wall? set bounced? ( abs (pxcor + dx) > max-pxcor - 1 or abs (pycor + dy) > max-pycor - 1 ) end to react-and-move if bounced? [ rt 180 ] if collided? [ rt 90 ] jump 1 ;; detect and prevent overlap if any? other-arrows-here with [ heading = heading-of myself and who < who-of myself ] [ jump -1 ] end to go ;; detect ask arrows [ limits collision ] ;; everyone acts on the detected collisions and bounces ask arrows [ react-and-move ] end ; (C) 2004 Uri Wilensky. From where if it originated the present part of code here. ; ; (C) 2005 Luis C. da Costa, James Steiner and Mario Campanema ; This code may be freely copied, distributed, altered, ; or otherwise used by anyone for any legal purpose. ; ; This model was created as part of the project: ; AGENTS FOR COMPUTACIONAL MODELING: BASED IN CELLULAR AUTOMATA AND PROCESS ALGEBRAS. ; The project gratefully acknowledges the support of the ; National Laboratory of Scientific Computing - NLSC ; Computer Science Coordination - CSC ; Petr—polis - Rio de Janeiro - Brazil. ; ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ; OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. @#$#@#$#@ GRAPHICS-WINDOW 321 10 656 366 12 12 13.0 1 10 1 1 1 0 1 1 1 -12 12 -12 12 CC-WINDOW 5 380 665 475 Command Center 0 MONITOR 2 192 316 241 NIL max values-from patches\n[count turtles-here] 3 1 BUTTON 47 152 262 185 Execute go T 1 T OBSERVER T NIL BUTTON 115 17 194 50 Setup setup NIL 1 T OBSERVER T NIL BUTTON 93 56 213 89 Move Turtles click-arrows T 1 T OBSERVER T NIL SLIDER 68 103 240 136 number number 1 300 5 1 1 NIL @#$#@#$#@ WHAT IS IT? ----------- This model demonstrates circular wave propagation using a cellular automaton on a square grid. The behavior of the waves approximates the Navier-Stokes equation, a well established fluid dynamics equation discovered in 1823. In the model, space is divided in to cells which are occupied by the gas particles. Each particle has the same mass and absolute velocity (each particle will move only a single cell at each time step). Space is broken up into neighborhoods of 2x2 cell squares. Collisions occur when multiple particles are in the same square and occur instantaneously, conserving both mass and momentum. The model is implemented using a Margolus neighborhood of 2x2 cell squares in which the particles belong to two separate spacetime sublattices, propagation and collision, (sometimes refered to as "even" and "odd" lattices), which evolve independently of each other. HOW IT WORKS ------------ The CA can be summarized with the following core rules: with two cases of collisions case 1: arrows positioned like this: |__|__|__| |=>|__|<=| |__|__|__| in this case, next move will cause both arrows to occupy same patch so, to detect collision, arrows check for another arrow on the same patch with the opposite heading after this reflection, arrows will collide again case 2: |__|__|__| |=>|<=|__| |__|__|__| in this case, the next move will cause the arrows to swap places so, at no time are both arrows on the same patch if this case should be counted as a collision, if is required for arrows to check for arrow on next patch with opposite heading It applies these rules to the even lattice of 2x2 squares followed by the odd lattice of 2x2 squares. Specifically, this means that the top left patch of four applies one of the above rules, then the bottom right patch of four does the same. To run the model in reverse, we simply switch the order of those two operations. No patch gets changed more then twice per tick. That means that each patch sees only one possible even rule and one possible odd rule each iteration. HOW TO USE IT ------------- The basic controls for the model are: SETUP - Sets up screen with a given percentage of particles GO - Run the model MOVE - For move the particles with the mouse EXTENDING THE MODEL ------------------- Can you create obstacles that deflect the movement of particles? This particular model is known as the HPP model. The HPP model is very limited. The FHP (Frisch, Hasslacher and Pomeau) model was invented in the mid-eighties in order to improve the accuracy of the HPP model. The underlying rules of the FHP model are similar to that of the HPP model except that the FHP model has a symmetry-group order of six. The hexagonal lattice allows for more advanced modeling, such as hydrodynamical simulations. Can you write a model that emulates a hexagonal lattice in NetLogo? NETLOGO FEATURES ---------------- In order for the algorithm to operate correctly, the size of the lattice must be even in both dimensions. However, the current version of NetLogo only supports odd sized grids. Therefore, it was necessary to add special code to this model to ignore the top row and rightmost column of the grid, effectively converting an odd-sized grid into an even-sized one. See also the "Even Grid Example" and "Lattice Gas Automaton" in the Code Examples and Chemistry & Physics sections of the Models Library. CREDITS AND REFERENCES ----------------------- Thanks to Ethan Bakshy for his help with this model. For more information about lattice gas automata, see: J. Hardy, Y. Pomeau & O. de Pazzis, Time evolution of two-dimensional model system. I. Invariant states and time correlation functions, J. Math. Phys. 14 (1973), pp. 1746-1759. U. Frisch, B. Hasslacher & Y. Pomeau, Lattice-gas automata for the Navier-Stokes equation, Phys. Rev. Lett. 56 (1986), pp. 1505-1508. T. Toffoli and N. Margolus. 1987. "Cellular Automata Machines: A New Environment for Modeling". Wolfram, S. 2002. A New Kind of Science. Wolfram Media Inc. Champaign, IL. To refer to this model in academic publications, please use: Wilensky, U. (2002). NetLogo Lattice Gas Automaton model. http://ccl.northwestern.edu/netlogo/models/LatticeGasAutomaton. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL. In other publications, please use: Copyright 2002 by Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/LatticeGasAutomaton for terms of use. @#$#@#$#@ default true 0 Polygon -7500403 true true 150 5 40 250 150 205 260 250 link true 0 Line -7500403 true 150 0 150 300 link direction true 0 Line -7500403 true 150 150 30 225 Line -7500403 true 150 150 270 225 arrows true 0 Polygon -7500403 true true 150 0 15 150 90 150 90 300 210 300 210 150 285 150 circle false 0 Circle -7500403 true true 30 30 240 line true 0 Line -7500403 true 150 0 150 300 square false 0 Rectangle -7500403 true true 30 30 270 270 @#$#@#$#@ NetLogo 3.1.1 @#$#@#$#@ setup @#$#@#$#@ @#$#@#$#@ @#$#@#$#@