globals [ fast medium slow ;; current counts avg-speed avg-energy ;; current averages avg-speed-init avg-energy-init ;; initial averages clock vsplit vclock ;; clock variables box-edge ;; patch coords of box's edge fade-needed? ] turtles-own [ speed mass energy ;; turtle info v1t v1l tmp-turtle ;; collision info (turtle 1) heading2 mass2 speed2 v2t v2l turtle2 ;; collision info (turtle 2) theta ;; collision info (both turtles) ] to setup ca set box-edge (round (screen-edge-x * box-size / 100)) make-box set fade-needed? false set clock 0 set vclock 0 cct number [ set speed initspeed set mass initmass if who != 0 [ random-position ] rt random-float 360 set shape "circle" recolor ] update-variables set avg-speed-init avg-speed set avg-energy-init avg-energy setup-plots setup-histograms do-plotting do-histograms end to update-variables ask turtles [ set energy (0.5 * speed * speed * mass) ] set medium count turtles with [color = green] set slow count turtles with [color = blue] set fast count turtles with [color = red] set avg-speed mean values-from turtles [speed] set avg-energy mean values-from turtles [energy] set vsplit ceiling max values-from turtles [speed] end to recalculate-vsplit ;; this needs to be done without-interruption to be sure that nothing tries to ;; use vsplit or vclock before they've been recalculated without-interruption [ set vsplit 2 * vsplit set vclock 2 * vclock ] end to go ask turtles [ bounce ] ask turtles [ move ] if trace? [ ask turtle 0 [ stamp gray set fade-needed? true ] ] set vclock (vclock + 1) if (vclock = vsplit) [ set clock (clock + 1) set vclock 0 update-variables do-plotting do-histograms if fade-needed? [ fade-patches ] ] end to bounce ;; turtle procedure locals [new-px new-py] ; if we're not about to hit a wall (yellow patch), ; we don't need to do any further checks if pcolor-of patch-ahead 1 != yellow [ stop ] ; get the coordinates of the patch we'll be on if we go forward 1 set new-px pxcor-of patch-ahead 1 set new-py pycor-of patch-ahead 1 ; check: hitting left or right wall? if (abs new-px = box-edge) ; if so, reflect heading around x axis [ set heading (- heading) ] ; check: hitting top or bottom wall? if (abs new-py = box-edge) ; if so, reflect heading around y axis [ set heading (180 - heading) ] end to move ;; turtle procedure while [(speed / vsplit) >= 1.0] [ recalculate-vsplit ] jump (speed / vsplit) check-for-collision end to check-for-collision ;; turtle procedure if count other-turtles-here = 1 [ set tmp-turtle random-one-of other-turtles-here if ((who > who-of tmp-turtle) and (turtle2 != tmp-turtle)) [ collide ] ] end to collide ;; turtle procedure get-turtle2-info calculate-velocity-components set-new-speed-and-headings end to get-turtle2-info ;; turtle procedure set turtle2 tmp-turtle set mass2 mass-of turtle2 set speed2 speed-of turtle2 set heading2 heading-of turtle2 end to calculate-velocity-components locals [vcm] ;; CM vel. along dir. theta set theta (random-float 360) set v1l (speed * sin (theta - heading)) set v1t (speed * cos (theta - heading)) set v2l (speed2 * sin (theta - heading2)) set v2t (speed2 * cos (theta - heading2)) set vcm (((mass * v1t) + (mass2 * v2t)) / (mass + mass2)) set v1t (vcm + vcm - v1t) set v2t (vcm + vcm - v2t) end to set-new-speed-and-headings ;; turtle procedure set speed sqrt ((v1t * v1t) + (v1l * v1l)) set heading (theta - (atan v1l v1t)) set speed-of turtle2 sqrt ((v2t * v2t) + (v2l * v2l)) set heading-of turtle2 (theta - (atan v2l v2t)) recolor ask turtle2 [ recolor ] end to recolor ;; turtle procedure ifelse speed < (0.5 * initspeed) [ set color blue ] [ ifelse speed > (1.5 * initspeed) [ set color red ] [ set color green ] ] end to fade-patches locals [trace-patches] set trace-patches patches with [(pcolor != yellow) and (pcolor != black)] ifelse any? trace-patches [ ask trace-patches [ set pcolor ( pcolor - 0.4 ) if (not trace?) or (round pcolor = black) [ set pcolor black ] ] ] [ set fade-needed? false ] end to make-box ask patches with [((abs pxcor = box-edge) and (abs pycor <= box-edge)) or ((abs pycor = box-edge) and (abs pxcor <= box-edge))] [ set pcolor yellow ] end to random-position ;; turtle procedure setxy ((1 - box-edge) + random-float (2 * box-edge - 2)) ((1 - box-edge) + random-float (2 * box-edge - 2)) end ;;; plotting procedures to setup-plots set-current-plot "Speed Counts" set-plot-y-range 0 number end to do-plotting set-current-plot "Speed Counts" set-current-plot-pen "fast" plot fast set-current-plot-pen "medium" plot medium set-current-plot-pen "slow" plot slow end to setup-histograms set-current-plot "Speed Histogram" set-plot-x-range 0 (initspeed * 2) set-plot-y-range 0 ceiling (number / 6) set-current-plot-pen "medium" set-histogram-num-bars 40 set-current-plot-pen "slow" set-histogram-num-bars 40 set-current-plot-pen "fast" set-histogram-num-bars 40 set-current-plot-pen "init-avg-speed" draw-vert-line avg-speed-init set-current-plot "Energy Histogram" set-plot-x-range 0 (0.5 * (initspeed * 2) * (initspeed * 2) * initmass) set-plot-y-range 0 ceiling (number / 6) set-current-plot-pen "medium" set-histogram-num-bars 40 set-current-plot-pen "slow" set-histogram-num-bars 40 set-current-plot-pen "fast" set-histogram-num-bars 40 set-current-plot-pen "init-avg-energy" draw-vert-line avg-energy-init end to do-histograms set-current-plot "Speed Histogram" set-current-plot-pen "medium" histogram-from turtles with [color = green] [speed] set-current-plot-pen "slow" histogram-from turtles with [color = blue] [speed] set-current-plot-pen "fast" histogram-from turtles with [color = red] [speed] set-current-plot-pen "average-speed" plot-pen-reset draw-vert-line avg-speed set-current-plot "Energy Histogram" set-current-plot-pen "medium" histogram-from turtles with [color = green] [energy] set-current-plot-pen "slow" histogram-from turtles with [color = blue] [energy] set-current-plot-pen "fast" histogram-from turtles with [color = red] [energy] set-current-plot-pen "average-energy" plot-pen-reset draw-vert-line avg-energy end ; draws a vertical line at xval on the current-plot with the current plot-pen to draw-vert-line [xval] plotxy xval plot-y-min plot-pen-down plotxy xval plot-y-max plot-pen-up end ; *** NetLogo Model Copyright Notice *** ; ; This model was created as part of the project: CONNECTED MATHEMATICS: ; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL ; MODELS (OBPML). The project gratefully acknowledges the support of the ; National Science Foundation (Applications of Advanced Technologies ; Program) -- grant numbers RED #9552950 and REC #9632612. ; ; Copyright 1998 by Uri Wilensky. All rights reserved. ; ; Permission to use, modify or redistribute this model is hereby granted, ; provided that both of the following requirements are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit without permission ; from Uri Wilensky. ; Contact Uri Wilensky for appropriate licenses for redistribution for ; profit. ; ; This model was converted to NetLogo as part of the project: ; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN ; CLASSROOMS. The project gratefully acknowledges the support of the ; National Science Foundation (REPP program) -- grant number REC #9814682. ; Converted from StarLogoT to NetLogo, 2001. Updated 2002. ; ; To refer to this model in academic publications, please use: ; Wilensky, U. (1998). NetLogo GasLab Gas in a Box model. ; http://ccl.northwestern.edu/netlogo/models/GasLabGasinaBox. ; Center for Connected Learning and Computer-Based Modeling, ; Northwestern University, Evanston, IL. ; ; In other publications, please use: ; Copyright 1998 by Uri Wilensky. All rights reserved. See ; http://ccl.northwestern.edu/netlogo/models/GasLabGasinaBox ; for terms of use. ; ; *** End of NetLogo Model Copyright Notice *** @#$#@#$#@ GRAPHICS-WINDOW 269 10 688 450 49 49 4.1313131313131315 1 10 1 1 1 0 CC-WINDOW 5 402 798 497 Command Center MONITOR 188 53 269 102 clock clock 0 1 MONITOR 188 171 269 220 avg-speed avg-speed 2 1 MONITOR 98 230 179 279 medium medium 0 1 MONITOR 9 230 90 279 fast fast 0 1 MONITOR 188 230 269 279 slow slow 0 1 SWITCH 177 10 269 43 trace? trace? 0 1 -1000 SLIDER 9 181 179 214 initmass initmass 1.0 20.0 2.0 1.0 1 NIL SLIDER 9 95 179 128 number number 1 2000 500 1 1 NIL SLIDER 9 138 179 171 initspeed initspeed 1.0 20.0 10.0 1.0 1 NIL BUTTON 93 10 168 43 go go T 1 T OBSERVER NIL NIL BUTTON 9 10 84 43 NIL setup NIL 1 T OBSERVER T NIL PLOT 10 285 270 484 Speed Counts time count 0.0 35.0 0.0 500.0 true true PENS "fast" 1.0 0 -65536 true "medium" 1.0 0 -11352576 true "slow" 1.0 0 -16776961 true SLIDER 9 52 179 85 box-size box-size 5.0 100.0 100.0 1.0 1 % PLOT 691 12 986 238 Speed Histogram speed count 0.0 20.0 0.0 84.0 false true PENS "fast" 1.0 1 -65536 true "medium" 1.0 1 -11352576 true "slow" 1.0 1 -16776961 true "average-speed" 1.0 0 -7566196 true "init-avg-speed" 1.0 0 -16777216 true PLOT 691 241 984 462 Energy Histogram energy count 0.0 200.0 0.0 84.0 false true PENS "fast" 1.0 1 -65536 true "medium" 1.0 1 -11352576 true "slow" 1.0 1 -16776961 true "average-energy" 1.0 0 -7566196 true "init-avg-energy" 1.0 0 -16777216 true MONITOR 188 112 269 161 avg-energy avg-energy 3 1 @#$#@#$#@ WHAT IS IT? ----------- This program simulates the behavior of gas molecules. The Gas-in-a-Box model is one in a collection of GasLab models that use the same basic rules for expressing what happens when gas molecules collide. Each one has different features in order to show different aspects of the behavior of gases. HOW IT WORKS ------------ Molecules have kinetic energy (that which is due to their motion). Collisions between molecules are perfectly elastic. Molecules are colored according to speed -- blue for slow, green for medium, and red for high speeds. Coloring of the molecules is based on INITSPEED, molecules with a speed less than 50% of INITSPEED are blue, ones that are more than 50% faster are red, while all in between are green. The exact way two molecules collide is as follows: 1. Two molecules "collide" if they find themselves on the same patch. 2. A random axis is chosen, as if they are two balls that hit each other and this axis is the line connecting their centers. 3. They exchange momentum and energy along that axis, according to the conservation of momentum and energy. This calculation is done in the center of mass system. 4. Each turtle is assigned its new velocity, energy, and heading. 5. If a turtle finds itself on or very close to a wall of the container, it "bounces" -- that is, reflects its direction and keeps its same speed. HOW TO USE IT ------------- Initial settings: - BOX-SIZE-PERCENT: size of the box. (percentage of the screen size) - NUMBER: number of gas molecules - INITSPEED: initial speed of the molecules - INITMASS: mass of the molecules The SETUP button will set the initial conditions. The GO button will run the simulation. Other settings: - TRACE?: Traces the path of one of the molecules. This path fades over time to make the screen less cluttered. Monitors: - FAST, MEDIUM, SLOW: numbers of molecules with different speeds: fast (red), medium (green), and slow (blue). - AVG-SPEED: average speed of the molecules. - AVG-ENERGY: average kinetic energy of the molecules. - CLOCK: number of ticks that have run. Plots: - SPEED COUNTS: plots the number of molecules in each range of speed. - SPEED HISTOGRAM: speed distribution of all the molecules. The gray line is the average value, and the black line is the initial average. - ENERGY HISTOGRAM: distribution of energies of all the molecules, calculated as m*v*v/2. The gray line is the average value, and the black line is the initial average. Initially, all the molecules have the same speed but random directions. Therefore the first histogram plots of speed and energy should show only one column each. As the molecules repeatedly collide, they exchange energy and head off in new directions, and the speeds are dispersed -- some molecules get faster, some get slower. THINGS TO NOTICE ---------------- What is happening to the numbers of molecules of different colors? Does this match what's happening in the histograms? Why are there more blue molecules than red ones? Can you observe collisions and color changes as they happen? For instance, when a red molecule hits a green molecule, what color do they each become? Why does the average speed (avg-speed) drop? Does this violate conservation of energy? The molecule histograms quickly converge on the classic Maxwell-Boltzmann distribution. What's special about these curves? Why is the shape of the energy curve not the same as the speed curve? Watch the molecule whose path is traced in gray. Does the trace resemble Brownian motion? Can you recognize when a collision happens? What factors affect the frequency of collisions? What about the "angularity" of the path? Can you get it to stay "local" or travel all over the screen? In what ways is this model an incorrect idealization of the real world? THINGS TO TRY ------------- Set all the molecules in part of the screen, or with the same heading -- what happens? Does this correspond to a physical possibility? Try different settings, especially the extremes. Are the histograms different? Does the trace pattern change? Are there other interesting quantities to keep track of? Look up or calculate the REAL number, size, mass and speed of molecules in a typical gas. When you compare those numbers to the ones in the model, are you surprised this model works as well as it does? What physical phenomena might be observed if there really were a small number of big molecules in the space around us? We often say outer space is a vacuum. Is that really true? How many molecules would there be in a space the size of this computer? EXTENDING THE MODEL ------------------- Could you find a way to measure or express the "temperature" of this imaginary gas? Try to construct a thermometer. What happens if there are molecules of different masses? (See GasLab Two Gas model.) What happens if the collisions are non-elastic? How does this 2-D model differ from a 3-D model? Set up only two molecules to collide head-on. This may help to show how the collision rule works. Remember that the axis of collision is being randomly chosen each time. What if some of the molecules had a "drift" tendency -- a force pulling them in one direction? Could you develop a model of a centrifuge, or charged molecules in an electric field? Find a way to monitor how often molecules collide, and how far they go between collisions, on the average. The latter is called the "mean free path". What factors affect its value? In what ways is this idealization different from the idealization that is used to derive the Maxwell-Boltzmann distribution? Specifically, what other code could be used to represent the two-body collisions of molecules? If MORE than two molecules arrive on the same patch, the current code says they don't collide. Is this a mistake? How does it affect the results? Is this model valid for fluids in any aspect? How could it be made to be fluid-like? NETLOGO FEATURES ----------------- Notice the use of the histogram primitive. Notice how collisions are detected by the molecules and how the code guarantees that the same two molecules do not collide twice. What happens if we let the patches detect them? CREDITS AND REFERENCES ---------------------- This was one of the original Connection Machine StarLogo applications (under the name GPCEE) and is now ported to NetLogo as part of the Participatory Simulations project. To refer to this model in academic publications, please use: Wilensky, U. (1998). NetLogo GasLab Gas in a Box model. http://ccl.northwestern.edu/netlogo/models/GasLabGasinaBox. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL. In other publications, please use: Copyright 1998 by Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/GasLabGasinaBox for terms of use. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 circle false 0 Circle -7566196 true true 35 35 230 @#$#@#$#@ NetLogo 2.1.0 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@