globals [ fast average slow ;; current counts avg-speed avg-energy ;; current averages avg-speed-init avg-energy-init ;; initial averages clock vsplit vclock ;; clock variables left-count right-count ;; # of molecules on each side propeller-velocity ;; current velocity of the propeller left-pressure right-pressure ;; pressure in each chamber left-walls right-walls ;; agentsets of the walls on each side (used when calculating pressure) propeller-angle ;; current angular position of the propeller ] turtles-own [ speed mass energy new-speed ;; turtle info v1t v1l tmp-turtle ;; collision info (turtle 1) heading2 mass2 speed2 v2t v2l turtle2 ;; collision info (turtle 2) theta ;; collision info (both molecules) ] patches-own [ wall? ;; is this patch part of the wall? pressure ;; sum of momentums of particles that have bounced here during this time slice ] to setup [mode] ca make-box set clock 0 set vclock 0 cct number [ set new-speed 10.0 set shape "circle" set mass 1.0 setup-position mode rt random-float 360 recolor ] update-variables set avg-speed-init avg-speed set avg-energy-init avg-energy rotate-propeller setup-plots do-plotting end to setup-position [mode] ;; turtle procedure if mode = "corner" [ setxy (- (screen-edge-x - 1)) (- (screen-edge-y - 1)) set heading random-float 90 fd random-float 8 ] if mode = "one side" [ setxy (- (1 + random-float (screen-edge-x - 2))) (random-float (screen-size-y - 3) - screen-edge-y + 1) ] if mode = "both sides" [ setxy (random-float (screen-size-x - 3) - screen-edge-x + 1) (random-float (screen-size-y - 3) - screen-edge-y + 1) ] end to update-variables ask turtles [ set speed new-speed set energy (0.5 * speed * speed * mass) ] set average 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 (round ((max values-from turtles [speed]) * 1.2)) set left-count count turtles with [xcor < 0] set right-count count turtles with [xcor >= 0] set left-pressure sum values-from left-walls [pressure] ask left-walls [ set pressure 0 ] set right-pressure sum values-from right-walls [pressure] ask right-walls [ set pressure 0 ] end to go ask turtles [ bounce ] ask turtles [ move ] set vclock (vclock + 1) rotate-propeller if (vclock = vsplit) [ set clock (clock + 1) set vclock 0 update-variables do-plotting ] end to rotate-propeller ;; use without-interruption to ensure that propeller is ;; never visible in partially drawn state without-interruption [ set-current-plot "Propeller" plot-pen-reset set propeller-angle propeller-angle - propeller-velocity / 2 ppu plotxy cos propeller-angle sin propeller-angle ppd plotxy 0 - cos propeller-angle 0 - sin propeller-angle ppu plotxy cos (propeller-angle + 90) sin (propeller-angle + 90) ppd plotxy 0 - cos (propeller-angle + 90) 0 - sin (propeller-angle + 90) ;; slow down the propeller due to friction set propeller-velocity propeller-velocity * 0.999 ] 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 set (pressure-of patch new-px new-py) (pressure-of patch new-px new-py) + mass * speed ; check: hitting left, right, or middle wall? if (abs new-px = screen-edge-x) or (pxcor != 0 and new-px = 0) ; if so, reflect heading around x axis [ set heading (- heading) ] ; check: hitting top or bottom wall? if (abs new-py = screen-edge-y) or (pxcor = 0) ; if so, reflect heading around y axis [ set heading (180 - heading) ] end to move ;; turtle procedure locals [old-xcor] set old-xcor xcor jump (speed / vsplit) if (old-xcor < 0) and (xcor >= 0) [ set propeller-velocity propeller-velocity + 0.03 * speed ] if (old-xcor > 0) and (xcor <= 0) [ set propeller-velocity propeller-velocity - 0.03 * speed ] 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 new-speed-of turtle2 set heading2 heading-of turtle2 end to calculate-velocity-components ;; turtle procedure locals [ vcm ] ;; CM vel. along dir. theta set theta (random-float 360) set v1l (new-speed * sin (theta - heading)) set v1t (new-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 new-speed sqrt ((v1t * v1t) + (v1l * v1l)) set heading (theta - (atan v1l v1t)) set new-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 new-speed < 5.0 [ set color blue ] [ ifelse new-speed > 15.0 [ set color red ] [ set color green ] ] end to make-box ask patches [ set pressure 0 set wall? false if ((abs pxcor = screen-edge-x) and (abs pycor <= screen-edge-y)) or ((abs pycor = screen-edge-y) and (abs pxcor <= screen-edge-x)) or ((pxcor = 0) and (abs pycor > propeller-radius)) [ set pcolor yellow set wall? true ] if (pxcor = 0) and (abs pycor <= propeller-radius) [ set pcolor gray ] ] set left-walls patches with [wall? and (pxcor < 0)] set right-walls patches with [wall? and (pxcor > 0)] end ;;; plotting procedures to setup-plots ;; work set-current-plot "Propeller Velocity" set-plot-y-range ceiling (number / -25) ceiling (number / 25) auto-plot-off set-current-plot-pen "x-axis" plotxy 0 0 plotxy 1000 0 auto-plot-on set-current-plot-pen "velocity" ;; pressure set-current-plot "Pressures" set-plot-y-range 0 ceiling (number / 25) end to do-plotting ;; counts set-current-plot "Molecule Counts" set-current-plot-pen "left" plot left-count set-current-plot-pen "right" plot right-count ;; work set-current-plot "Propeller Velocity" plot propeller-velocity ;; pressure set-current-plot "Pressures" set-current-plot-pen "left" plot left-pressure set-current-plot-pen "right" plot right-pressure ;; entropy set-current-plot "entropy" plot 100 * (1 / calculate-order) end to-report calculate-order locals [gridx gridy x-patches-per-grid-cell y-patches-per-grid-cell gridcount counts-list] set x-patches-per-grid-cell (screen-size-x / 5) set y-patches-per-grid-cell (screen-size-y / 5) set counts-list [] set gridx -2 repeat 5 [ set gridy -2 repeat 5 [ set gridcount count turtles with [int (pxcor / x-patches-per-grid-cell) = gridx and int (pycor / y-patches-per-grid-cell) = gridy] set counts-list lput gridcount counts-list set gridy gridy + 1 ] set gridx gridx + 1 ] ;; show counts-list report variance counts-list end ; *** NetLogo Model Copyright Notice *** ; ; This model was created 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. ; ; Copyright 2002 by Uri Wilensky. Updated 2002. 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. ; ; To refer to this model in academic publications, please use: ; Wilensky, U. (2002). NetLogo GasLab Second Law model. ; http://ccl.northwestern.edu/netlogo/models/GasLabSecondLaw. ; 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/GasLabSecondLaw ; for terms of use. ; ; *** End of NetLogo Model Copyright Notice *** @#$#@#$#@ GRAPHICS-WINDOW 254 10 548 325 35 35 4.0 0 10 1 1 1 CC-WINDOW 552 244 780 334 Command Center SLIDER 7 119 249 152 number number 10 500 200 10 1 molecules BUTTON 145 76 249 109 go go T 1 T OBSERVER NIL BUTTON 8 41 125 74 setup (corner) setup "corner" NIL 1 T OBSERVER T SLIDER 7 152 249 185 propeller-radius propeller-radius 0 20 17 1 1 NIL BUTTON 127 41 249 74 setup (one side) setup "one side" NIL 1 T OBSERVER T BUTTON 8 76 143 109 setup (both sides) setup "both sides" NIL 1 T OBSERVER T PLOT 7 195 249 409 Molecule Counts time # molecules 0.0 20.0 0.0 100.0 true true PENS "left" 1.0 0 -44544 true "right" 1.0 0 -16745473 true PLOT 7 409 249 623 Propeller Velocity time velocity 0.0 20.0 -8.0 8.0 true false PENS "velocity" 1.0 0 -11365376 true "x-axis" 1.0 0 -256 true TEXTBOX 319 335 750 409 RULES:\n- Molecule crossing left to right pushes propeller clockwise.\n- Molecule crossing right to left pushes propeller counterclockwise.\n- Propeller slows down over time as work is extracted from the system. PLOT 491 409 733 623 Pressures time pressure 0.0 20.0 0.0 8.0 true true PENS "left" 1.0 0 -44544 true "right" 1.0 0 -16745473 true PLOT 249 409 491 623 Entropy time entropy 0.0 20.0 0.0 1.0 true false PENS "entropy" 1.0 0 -65536 true MONITOR 249 360 310 409 NIL clock 0 1 PLOT 552 41 780 244 Propeller NIL NIL -1.0 1.0 -1.0 1.0 true false @#$#@#$#@ WHAT IS IT? ----------- This program simulates the Second Law of Thermodynamics via the behavior of gas molecules in a box. The Second Law of Thermodynamics states that systems tend towards increased entropy. Essentially what this means is that over time ordered systems become less ordered unless work is done on the system to keep it ordered. The GasLab Second Law model is based on the collection of GasLab models, and uses the same basic rules for expressing what happens when gas molecules collide. Each one of the GasLab models has different features in order to show different aspects of the behavior of gases. This model uses that framework in order to show the Second Law of Thermodynamics. HOW IT WORKS ------------ Molecules are modeled as perfectly elastic particles with no energy except their kinetic energy -- that which is due to their motion. Collisions between molecules are elastic. Particles are colored according to speed -- blue for slow, green for medium, and red for high speeds. The exact way two molecules collide is as follows: 1. Two turtles "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. The propeller is modeled such that it shows the effect of the flux of the molecules between the two sides of the box, but does not effect or interact with the molecules as they pass through. When molecules move from the left side to the right side they accelerate the propeller clockwise, and likewise, when molecules move from the right side to the left side they accelerate the propeller counter-clockwise. HOW TO USE IT ------------- SETUP: sets up the initial conditions and distributes the molecules in one of three different modes. Be sure to wait till the Setup button stops before pushing go. CORNER: all the molecules are created in the lower left corner of the box and diffuse outwards from there. ONE SIDE: all the molecules are created in the left side of the box evenly distributed. BOTH SIDES: all the molecules are created evenly distributed throughout the entire box. GO: runs the code again and again. This is a "forever" button. NUMBER: the number of gas molecules PROPELLER-RADIUS: the radius of the propeller in the opening between the sides of the box. The size of the opening is based on the size of the propeller. CLOCK: number of ticks that have run. PLOTS: MOLECULE COUNTS: plots the number of molecules on each side of the box. PROPELLER VELOCITY: plots the velocity of the propeller: positive is clockwise, negative is counter-clockwise. PRESSURES: plots the pressure of the gas on each side of the box. ENTROPY: plots a measure of the entropy of the system. As the molecules become more evenly and randomly distributed the entropy will increase. THINGS TO NOTICE ---------------- When the molecules are evenly distributed throughout the box, what do you notice about the behavior of the propeller? In what ways is this model a correct or incorrect idealization of the real world? In what ways can you quantify entropy? What is the best way to quantify entropy in this model? Does this model use this method? If not, what is wrong with the method being used? 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? Are there other interesting quantities to keep track of? 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.) How does this 2-D model differ from the 3-D model? 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? RELATED MODELS -------------- The GasLab suite of models, especially GasLab Maxwell's Demon, which models a theoretical system that seems to violate the Second Law of Thermodynamics. CREDITS AND REFERENCES ---------------------- Based on GasLab -- thanks to Brent Collins and Seth Tisue for their help on this model. To refer to this model in academic publications, please use: Wilensky, U. (1998). NetLogo GasLab Second Law model. http://ccl.northwestern.edu/netlogo/models/GasLabSecondLaw. 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/GasLabSecondLaw 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 7 7 285 square false 0 Rectangle -7566196 true true -7 -11 298 298 @#$#@#$#@ NetLogo 2.0beta4 @#$#@#$#@ setup "corner" repeat 75 [ go ] @#$#@#$#@ @#$#@#$#@