# GasLab Moving Piston

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)

## WHAT IS IT?

This model simulates the behavior of gas particles as the volume changes. In this model, the volume is slowly changing over time by a piston that is rising and falling. As the piston lowers, the volume of the box decreases and as the piston rises, the volume of the box increases. This systematic motion of the piston does no work on the particles inside the box. The piston only serves a mechanism to change the volume of the box.

The particles start with the same mass and speed upon the start of the simulation. The mass of the particles stays constant throughout the simulation, whereas, the speeds will change once particles start to collide. Particles are in constant motion colliding with other particles and the walls. All collisions are modeled as elastic collisions, in that the total kinetic energy before and after the collision is conserved. For example, when a fast moving particle collides with a slow moving particle, the fast moving particle will give some of its speed to the slow moving particle. Therefore, the fast moving particle will leave the collision moving slower then when it entered the collision. And the slow moving particle will speed up a bit. The speed in a particle to particle collision is still conserved. The collisions between a particle and a wall is modeled the same way. When the particles hit the wall they transfer momentum to the wall. After this transfer occurs, the particles then bounce off the wall with a different direction and speed. The system's pressure is calculated by averaging the number of collisions the particles have with the walls at each time step.

The Moving Piston model is one of a collection of GasLab models that use the same basic rules for expressing what happens when gas particles collide. Each model in this collection has different features to show the different aspects of the Gas Laws.

Multiple adaptations of this model can be found in the Chemistry folder of the Curricular Models section under the names Chem Volume 1 and 2. It is part of a suite of models used to teach students about the chemistry of the Gas Laws.

## HOW IT WORKS

The particles are modeled as single particles, all with the same mass and initial velocity. Molecules are modeled as perfectly elastic particles with no internal energy except that which is due to their motion. Collisions with the box and 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 particles collide is as follows:

- Two turtles "collide" if they find themselves on the same patch.
- A random axis is chosen, as if they were two billiard balls that hit and this axis was the line connecting their centers.
- They exchange momentum and energy along that axis, according to the conservation of momentum and energy. This calculation is done in the center mass system.
- Each turtle is assigned its new speed, energy and heading.
- 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

### Buttons

SETUP - puts in the initial conditions you have set with the sliders. Be sure to wait till the SETUP button stops before pushing GO.

GO - runs the code again and again. This is a "forever" button.

### Sliders

BOX-HEIGHT - height of the container

BOX-WIDTH - width of the container

NUMBER - number of particles

PISTON-SPEED - rate of the piston

SCALE - number of clock cycles over which to average the pressure

### Switch

HISTOGRAM? - turns histograms on or off

### Plots

VOLUME - plots the volume over time

PRESSURE - plots the pressure over time

PRESSURE VS. VOLUME - plots pressure over volume

PRESSURE * VOLUME - plots the value of pressure * volume over time

TEMPERATURE - plots the average temperature

SPEED HISTOGRAM - illustrates the number of particles at their various speeds

ENERGY HISTOGRAM - illustrates the number of particles at their various energy levels

### How to use it

Adjust the BOX-HEIGHT, BOX-WIDTH, NUMBER, and PISTON-SPEED variable before pressing SETUP. The SETUP button will set the initial conditions. The GO button will run the simulation.

In this model, though, the collisions of the piston with the particles are ignored. Note that there's a physical impossibility in the model here: in real life if you moved the piston down you would do work on the gas by compressing it, and its temperature would increase. In this model, the energy and temperature are constant no matter how you manipulate the piston. Nonetheless, the basic relationship between volume and pressure is correctly demonstrated here.

## THINGS TO NOTICE

How does the pressure change as the volume of the box changes? Compare the two plots of volume and pressure.

How does the pressure change as the shape of the box changes?

Measure changes in pressure and volume. Is there a clear quantitative relationship?

How can the relationship between pressure and volume be explained in terms of the collisions of molecules?

How does more particles change the relationship between pressure and volume?

What shapes do the energy and speed histograms reach after a while? Why aren't they the same? Do the pressure and volume affect these shapes?

## THINGS TO TRY

How would you calculate pressure? How does this code do it?

Change the number, mass, and initial velocity of the particles. Does this affect the pressure? Why? Do the results make intuitive sense? Look at the extremes: very few or very many molecules, high or low volumes.

Figure out how many molecules there *really* are in a box this size --- say a 10-cm cube. Look up or calculate the *real* mass and speed of a typical molecule. When you compare those numbers to the ones in the model, are you surprised this model works as well as it does?

## EXTENDING THE MODEL

Are there other ways one might calculate pressure?

Create an isothermal piston example where the user can manually move the piston to any level in the box.

Add in a temperature variable that allows for the particles to move the piston to the appropriate volume.

## NETLOGO FEATURES

Notice how collisions are detected by the turtles and how the code guarantees that the same two particles do not collide twice. What happens if we let the patches detect them?

## CREDITS AND REFERENCES

This model was developed as part of the GasLab curriculum (http://ccl.northwestern.edu/curriculum/gaslab/) and has also been incorporated into the Connected Chemistry curriculum (http://ccl.northwestern.edu/curriculum/ConnectedChemistry/)

## HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

- Wilensky, U. (2002). NetLogo GasLab Moving Piston model. http://ccl.northwestern.edu/netlogo/models/GasLabMovingPiston. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.

## COPYRIGHT AND LICENSE

Copyright 2002 Uri Wilensky.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

This model and associated activities and materials were created as part of the project: MODELING ACROSS THE CURRICULUM. The project gratefully acknowledges the support of the National Science Foundation, the National Institute of Health, and the Department of Education (IERI program) -- grant number REC #0115699. Additional support was provided through the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT -- NSF (REPP & ROLE programs) grant numbers REC #9814682 and REC-0126227.

## Comments and Questions

globals [ fast average slow ;; current counts avg-speed avg-energy ;; current averages vsplit vclock ;; clock variables raw-width raw-height ;; box size variables piston-position ;; position of the piston at any given time volume area ;; because this is 2D, area is the perimeter and volume is the area total-pressure ;; pressure per unit area pressure-history ;; list of 'scale' previous total-pressures avg-pressure ;; mean of the pressure-history initspeed initmass ;; initial speed and initial mass of the all the particles-particle mass always stays the same, but the speed changes. going-down? ;; flag for whether or not the piston is moving up or down ] turtles-own [ speed mass energy new-speed ;; Turtle Info v1t v1l tmp-turtle ;; Turtle 1 -- Collide heading2 mass2 speed2 v2t v2l turtle2 ;; Turtle 2 -- Collide theta ;; Turtles 1 + 2 -- Collide pressure ;; pressure variable ] ;; procedure that setup up initial variables to setup clear-all set going-down? true set pressure-history [] set initspeed 10.0 set initmass 1.0 set raw-width round (0.01 * box-width * max-pxcor) set raw-height round (0.01 * box-height * max-pycor) set piston-position 0.75 * raw-height set area ((4 * raw-width) + (2 * (piston-position + raw-height))) set volume ((2 * raw-width) * (piston-position + raw-height)) make-box draw-piston ;;set vclock 0 ;; create the gas molecules crt number [ set new-speed initspeed set mass initmass random-position set shape "circle" recolor set pressure 0 ] update-variables reset-ticks setup-histograms end ;; updates variables after every time tick to update-variables ;; Gas Variables ask turtles [ set speed new-speed set energy (0.5 * mass * speed * speed) ] 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 [ speed ] of turtles set avg-energy mean [ energy ] of turtles ;; System Variables calculate-pressure set vsplit (round (max [speed] of turtles * 1.2)) end ;; procedure that runs the model to go ask turtles [bounce] ask turtles [move] ask turtles [check-for-collision] ;; control the piston's motion if piston-position < (-0.75 * raw-height) [ set going-down? false ] if piston-position > (0.75 * raw-height) [ set going-down? true ] ifelse going-down? [ piston-down piston-speed / vsplit ] [ piston-up piston-speed / vsplit ] set vclock vclock + 1 ifelse (vclock = vsplit) [ tick set vclock 0 update-variables do-plotting do-histograms ] [ display ] end ;; turtle procedure for bouncing off of the walls to bounce ; if we're not about to hit a wall (yellow patch) ; or the piston (gray+2 patch), ; we don't need to do any further checks if ([pcolor] of patch-ahead 1 != yellow) and ([pcolor] of patch-ahead 1 != gray + 2) [ stop ] ; get the coordinates of the patch we'll be on if we go forward 1 let new-px [pxcor] of patch-ahead 1 let new-py [pycor] of patch-ahead 1 ; check: hitting left or right wall? if (abs new-px = raw-width) ; if so, reflect heading around x axis [ set heading (- heading) set pressure pressure + abs (dx * mass * speed) ] ; check: hitting piston or bottom wall? if (abs new-py = raw-height) or (new-py = round piston-position) ; if so, reflect heading around y axis [ set heading (180 - heading) set pressure pressure + abs (dy * mass * speed) ] end ;; turtle procedure that moves all the particles to move jump (speed / vsplit) end ;; turtle procedure to check to see if two particles collide to check-for-collision if count other turtles-here = 1 [ set tmp-turtle one-of other turtles-here if ((who > [who] of tmp-turtle) and (turtle2 != tmp-turtle)) [ collide ] ] end ;; turtle procedure for when two particles collide to collide get-turtle2-info calculate-velocity-components set-new-speed-and-headings end ;; turtle gets mass and speed info from turtle it is colliding with to get-turtle2-info set turtle2 tmp-turtle set mass2 [mass] of turtle2 set speed2 [new-speed] of turtle2 set heading2 [heading] of turtle2 end ;; calculates new turtle velocity after the collision to calculate-velocity-components 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)) let vcm (((mass * v1t) + (mass2 * v2t)) / (mass + mass2)) set v1t (vcm + vcm - v1t) set v2t (vcm + vcm - v2t) end ;; set new speed and headings of each turtles that has had a collision to set-new-speed-and-headings set new-speed sqrt ((v1t * v1t) + (v1l * v1l)) set heading (theta - (atan v1l v1t)) let new-speed2 sqrt ((v2t * v2t) + (v2l * v2l)) let new-heading (theta - (atan v2l v2t)) ask turtle2 [ set new-speed new-speed2 set heading new-heading ] recolor ask turtle2 [ recolor ] end to recolor ;; turtle procedure ifelse new-speed < (0.5 * initspeed) [ set color blue ] [ ifelse new-speed > (1.5 * initspeed) [ set color red ] [ set color green ] ] end ;; patch procedure to make a box to make-box ask patches with [ ((abs pxcor = raw-width) and (abs pycor <= raw-height)) or ((abs pycor = raw-height) and (abs pxcor <= raw-width)) ] [ set pcolor yellow ] end ;; turtle procedure to give turtles a random position within the confined area to random-position setxy ((1 - raw-width) + random-float (2 * raw-width - 2)) ((1 - raw-height) + random-float (raw-height + piston-position - 2)) end ;; ------ Piston ---------- to piston-up [dist] if (dist > 0) [ ifelse ((piston-position + dist) < raw-height - 1) [ undraw-piston set piston-position (piston-position + dist) draw-piston ] [ undraw-piston set piston-position (raw-height - 1) draw-piston ] set volume ((2 * raw-width) * (piston-position + raw-height)) set area ((4 * raw-width) + (2 * (piston-position + raw-height))) ] end to piston-down [dist] if (dist > 0) [ ifelse (piston-position - dist) > (2 - raw-height) [ undraw-piston set piston-position (piston-position - dist) ask turtles [ if (ycor >= (piston-position - 1)) [ bounce-off-piston ] ] draw-piston ] [ undraw-piston set piston-position (3 - raw-height) ask turtles [ if (pycor >= 3 - raw-height) [ bounce-off-piston ] ] draw-piston ] set area ((4 * raw-width) + (2 * (piston-position + raw-height))) set volume ((2 * raw-width) * (piston-position + raw-height)) ] end to draw-piston ask patches with [ ((pycor = (round piston-position)) and ((abs pxcor) < raw-width)) ] [ set pcolor gray + 2 ] end to undraw-piston ask patches with [ (pycor = round piston-position) and ((abs pxcor) < raw-width) ] [ set pcolor black ] end to bounce-off-piston ;; Turtles procedure particle bounces off piston ifelse ((((2 * piston-position) - (ycor + 2)) < (1 - raw-height)) or (((2 * piston-position) - (ycor + 2)) > (piston-position - 2))) [ set ycor ((random (raw-height + piston-position - 2)) - (raw-height - 1)) ] [ set ycor ((2 * piston-position) - (ycor + 2)) ] end to calculate-pressure ;; Observer procedure set total-pressure 100 * (sum [pressure] of turtles) / area ifelse (length pressure-history < scale) [ set pressure-history fput total-pressure pressure-history ] [ set pressure-history fput total-pressure but-last pressure-history ] set avg-pressure mean pressure-history ;; rezero pressures in preparation for the next cycle ask turtles [ set pressure 0 ] end ;;; plotting procedures to setup-histograms ;; Speed Histogram set-current-plot "Speed histogram" set-plot-x-range 0 (initspeed * 2) set-plot-y-range 0 ceiling (number / 6) set-current-plot-pen "average" set-histogram-num-bars 45 set-current-plot-pen "fast" set-histogram-num-bars 45 set-current-plot-pen "slow" set-histogram-num-bars 45 ;; Energy histogram 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 "average" set-histogram-num-bars 45 set-current-plot-pen "fast" set-histogram-num-bars 45 set-current-plot-pen "slow" set-histogram-num-bars 45 end ;; does actual plotting (called in Go) to do-plotting set-current-plot "Volume" plot volume set-current-plot "Pressure" plot avg-pressure set-current-plot "Temperature" plot avg-energy set-current-plot "Pressure vs. Volume" plotxy volume avg-pressure set-current-plot "Pressure * Volume" plot avg-pressure * volume / 1000 end ;; does actual histograms plotting (called in Go) to do-histograms if (histogram?) [ histo-energy histo-speed ] end ;; draw energy histogram to histo-energy set-current-plot "Energy histogram" set-current-plot-pen "average" histogram [ energy ] of turtles with [ color = green ] set-current-plot-pen "slow" histogram [ energy ] of turtles with [ color = blue ] set-current-plot-pen "fast" histogram [ energy ] of turtles with [ color = red ] set-current-plot-pen "avg-energy" plot-pen-reset draw-vert-line avg-energy end ;; draw speed histogram to histo-speed set-current-plot "Speed histogram" set-current-plot-pen "average" histogram [ speed ] of turtles with [ color = green ] set-current-plot-pen "slow" histogram [ speed ] of turtles with [ color = blue ] set-current-plot-pen "fast" histogram [ speed ] of turtles with [ color = red ] set-current-plot-pen "avg-speed" plot-pen-reset draw-vert-line avg-speed 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 ; Copyright 2002 Uri Wilensky. ; See Info tab for full copyright and license.

There are 15 versions of this model.

## Attached files

File | Type | Description | Last updated | |
---|---|---|---|---|

GasLab Moving Piston.png | preview | Preview for 'GasLab Moving Piston' | about 11 years ago, by Uri Wilensky | Download |

This model does not have any ancestors.

This model does not have any descendants.