# drafting_AlonOsnat_1

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

### WHAT IS IT?

This model is one in a series of GasLab models. They use the same basic rules for simulating the behavior of gases. Each model integrates different features in order to highlight different aspects of gas behavior.

The basic principle of the models is that gas particles are assumed to have two elementary actions: they move and they collide - either with other particles or with any other objects such as walls.

This model is the simplest gas model in the suite of GasLab models. The particles are moving and colliding with each other with no external constraints, such as gravity or containers. In this model, particles are modeled as perfectly elastic ones with no energy except their kinetic energy -- which is due to their motion. Collisions between particles are elastic. Particles are colored according to their speed -- blue for slow, green for medium, and red for high.

### HOW IT WORKS

The basic principle of all GasLab models is the following algorithm (for more details, see the model "GasLab Gas in a Box"):

1) A particle moves in a straight line without changing its speed, unless it collides with another particle or bounces off the wall.

2) Two particles "collide" if they find themselves on the same patch (NetLogo's View is composed of a grid of small squares called patches). In this model, two particles are aimed so that they will collide at the origin.

3) An angle of collision for the particles is chosen, as if they were two solid balls that hit, and this angle describes the direction of the line connecting their centers.

4) The particles exchange momentum and energy only along this line, conforming to the conservation of momentum and energy for elastic collisions.

5) Each particle is assigned its new speed, heading and energy.

### HOW TO USE IT

Initial settings:

- NUMBER-OF-PARTICLES: the number of gas particles.

- TRACE?: Draws the path of one individual particle.

- COLLIDE?: Turns collisions between particles on and off.

- INIT-PARTICLE-SPEED: the initial speed of each particle -- they all start with the same speed.

- PARTICLE-MASS: the mass of each particle -- they all have the same mass.

As in most NetLogo models, the first step is to press SETUP. It puts in the initial conditions you have set with the sliders. Be sure to wait till the SETUP button stops before pushing GO.

The GO button runs the models again and again. This is a "forever" button.

Monitors:

- PERCENT FAST, PERCENT MEDIUM, PERCENT SLOW monitors: percent of particles with different speeds: fast (red), medium (green), and slow (blue).

- AVERAGE SPEED: average speed of the particles.

- AVERAGE ENERGY: average kinetic energy of the particles.

Plots:

- SPEED COUNTS: plots the number of particles in each range of speed (fast, medium or slow).

- SPEED HISTOGRAM: speed distribution of all the particles. The gray line is the average value, and the black line is the initial average. The displayed values for speed are ten times the actual values.

- ENERGY HISTOGRAM: the distribution of energies of all the particles, calculated as (m*v^2)/2. The gray line is the average value, and the black line is the initial average.

Initially, all the particles have the same speed but random directions. Therefore the first histogram plots of speed and energy should show only one column each. As the particles repeatedly collide, they exchange energy and head off in new directions, and the speeds are dispersed -- some particles get faster, some get slower, and the plot will show that change.

### THINGS TO NOTICE

What is happening to the numbers of particles of different colors? Why are there more blue particles than red ones?

Can you observe collisions and color changes as they happen? For instance, when a red particle hits a green particle, what color do they each become?

Why does the average speed (avg-speed) drop? Does this violate conservation of energy?

This gas is in "endless space" -- no boundaries, no obstructions, but still a finite size! Is there a physical situation like this?

Watch the particle whose path is traced in the drawing. Notice how the path "wraps" around the world. 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? Could you get it to stay "local" or travel all over the world?

In what ways is this model an "idealization" of the real world?

### THINGS TO TRY

Set all the particles in part of the world, 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 particles 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 particles in the space around us?

We often say outer space is a vacuum. Is that really true? How many particles 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 particles of different masses? (See "GasLab Two Gas" model.)

How would you define and calculate pressure in this "boundless" space?

What happens if the gas is inside a container instead of a boundless space? (See "Gas in a Box" model.)

What happens if the collisions are non-elastic?

How does this 2-D model differ from the 3-D model?

Set up only two particles 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 particles had a "drift" tendency -- a force pulling them in one direction? Could you develop a model of a centrifuge, or charged particles in an electric field?

Find a way to monitor how often particles collide, and how far they go, on average, between collisions. The latter is called the "mean free path". What factors affect its value?

In what ways is this idealization different from the one used to derive the Maxwell-Boltzmann distribution? Specifically, what other code could be used to represent the two-body collisions of particles?

If MORE than two particles 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 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 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.

### HOW TO CITE

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

- Wilensky, U. (1997). NetLogo GasLab Free Gas model. http://ccl.northwestern.edu/netlogo/models/GasLabFreeGas. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

In other publications, please use:

- Copyright 1997 Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/GasLabFreeGas for terms of use.

### COPYRIGHT NOTICE

Copyright 1997 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 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.

This model was developed at the MIT Media Lab using CM StarLogo. See Wilensky, U. (1993). Thesis - Connected Mathematics: Building Concrete Relationships with Mathematical Knowledge. Adapted to StarLogoT, 1997, as part of the Connected Mathematics Project. Adapted to NetLogo, 2002, as part of the Participatory Simulations Project.

This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2002.

## Comments and Questions

globals [ tick-delta ;; how much we advance the tick counter this time through max-tick-delta ;; the largest tick-delta is allowed to be init-avg-speed init-avg-energy ;; initial averages avg-speed avg-energy ;; current averages fast medium slow ;; current counts percent-fast percent-medium ;; percentage of the counts percent-slow ;; percentage of the counts colliding-pair ] breed [ particles particle ] particles-own [ speed mass energy ;; particle info last-collision ] breed [riders rider] ;; riders-own [energy speed flockmates ;; agentset of nearby turtles nearest-neighbor ;; closest one of our flockmates ] to setup ca ask patches [ setup-road ] setup-riders setup-particles end to setup-particles set-default-shape particles "circle" set max-tick-delta 0.1073 make-particles update-variables set init-avg-speed avg-speed set init-avg-energy avg-energy ask particles [check-for-riders-collision ] end to setup-riders create-riders number-cyclists [ set size 4 ;; easier to see setxy random-xcor random-ycor ] end to setup-road ;; patch procedure ifelse ( pycor < 20) and ( pycor > -20 ) [ set pcolor 2 ] [set pcolor 45] end to go go-riders go-particles end to go-riders ask riders [ flock ] ;; the following line is used to make the turtles ;; animate more smoothly. repeat 5 [ ask turtles [ fd 0.3 ] display ] ;; for greater efficiency, at the expense of smooth ;; animation, substitute the following line instead: ;; ask turtles [ fd 1 ] tick end to flock ;; turtle procedure find-flockmates if any? flockmates [ find-nearest-neighbor ifelse distance nearest-neighbor < 2 [ separate ] [ align cohere ] ] end to find-flockmates ;; turtle procedure set flockmates other turtles in-radius 5 end to find-nearest-neighbor ;; turtle procedure set nearest-neighbor min-one-of flockmates [distance myself] end ;;; SEPARATE to separate ;; turtle procedure turn-away ([heading] of nearest-neighbor) 2 end to align ;; turtle procedure turn-towards average-flockmate-heading 2 set heading 90 end to cohere ;; urtle procedure turn-towards average-heading-towards-flockmates 7 end to-report average-flockmate-heading ;; turtle procedure ;; We can't just average the heading variables here. ;; For example, the average of 1 and 359 should be 0, ;; not 180. So we have to use trigonometry. let x-component sum [sin heading] of flockmates let y-component sum [cos heading] of flockmates ifelse x-component = 0 and y-component = 0 [ report heading ] [ report atan x-component y-component ] end to-report average-heading-towards-flockmates ;; turtle procedure ;; "towards myself" gives us the heading from the other turtle ;; to me, but we want the heading from me to the other turtle, ;; so we add 180 let x-component mean [sin (towards myself + 180)] of flockmates let y-component mean [cos (towards myself + 180)] of flockmates ifelse x-component = 0 and y-component = 0 [ report heading ] [ report atan x-component y-component ] end to turn-towards [new-heading max-turn] ;; turtle procedure turn-at-most (subtract-headings new-heading heading) max-turn end to turn-away [new-heading max-turn] ;; turtle procedure turn-at-most (subtract-headings heading new-heading) max-turn end ;; turn right by "turn" degrees (or left if "turn" is negative), ;; but never turn more than "max-turn" degrees to turn-at-most [turn max-turn] ;; turtle procedure ifelse abs turn > max-turn [ ifelse turn > 0 [ rt max-turn ] [ lt max-turn ] ] [ rt turn ] end to go-particles ask particles [ move ] ask particles [check-for-collision] tick-advance tick-delta if floor ticks > floor (ticks - tick-delta) [ update-variables ] calculate-tick-delta ask particles [check-for-riders-collision ] display end to update-variables ; set medium count particles with [color = green] ; set slow count particles with [color = blue] ; set fast count particles with [color = red] set percent-medium (medium / count particles) * 100 set percent-slow (slow / count particles) * 100 set percent-fast (fast / count particles) * 100 set avg-speed mean [speed] of particles set avg-energy mean [energy] of particles end to calculate-tick-delta ;; tick-delta is calculated in such way that even the fastest ;; particle will jump at most 1 patch length in a tick. As ;; particles jump (speed * tick-delta) at every tick, making ;; tick length the inverse of the speed of the fastest particle ;; (1/max speed) assures that. Having each particle advance at most ;; one patch-length is necessary for them not to jump over each other ;; without colliding. ifelse any? particles with [speed > 0] [ set tick-delta min list (1 / (ceiling max [speed] of particles)) max-tick-delta ] [ set tick-delta max-tick-delta ] end to move ;; particle procedure if patch-ahead (speed * tick-delta) != patch-here [ set last-collision nobody ] jump (speed * tick-delta) end to check-for-collision ;; particle procedure ;; Here we impose a rule that collisions only take place when there ;; are exactly two particles per patch. if count other particles-here = 1 [ ;; the following conditions are imposed on collision candidates: ;; 1. they must have a lower who number than my own, because collision ;; code is asymmetrical: it must always happen from the point of view ;; of just one particle. ;; 2. they must not be the same particle that we last collided with on ;; this patch, so that we have a chance to leave the patch after we've ;; collided with someone. let candidate one-of other particles-here with [who < [who] of myself and myself != last-collision] ;; we also only collide if one of us has non-zero speed. It's useless ;; (and incorrect, actually) for two particles with zero speed to collide. if (candidate != nobody) and (speed > 0 or [speed] of candidate > 0) [ collide-with candidate set last-collision candidate ask candidate [ set last-collision myself ] ] ] end ;; implements a collision with another particle. ;; ;; THIS IS THE HEART OF THE PARTICLE SIMULATION, AND YOU ARE STRONGLY ADVISED ;; NOT TO CHANGE IT UNLESS YOU REALLY UNDERSTAND WHAT YOU'RE DOING! ;; ;; The two particles colliding are self and other-particle, and while the ;; collision is performed from the point of view of self, both particles are ;; modified to reflect its effects. This is somewhat complicated, so I'll ;; give a general outline here: ;; 1. Do initial setup, and determine the heading between particle centers ;; (call it theta). ;; 2. Convert the representation of the velocity of each particle from ;; speed/heading to a theta-based vector whose first component is the ;; particle's speed along theta, and whose second component is the speed ;; perpendicular to theta. ;; 3. Modify the velocity vectors to reflect the effects of the collision. ;; This involves: ;; a. computing the velocity of the center of mass of the whole system ;; along direction theta ;; b. updating the along-theta components of the two velocity vectors. ;; 4. Convert from the theta-based vector representation of velocity back to ;; the usual speed/heading representation for each particle. ;; 5. Perform final cleanup and update derived quantities. to collide-with [ other-particle ] ;; particle procedure ;;; PHASE 1: initial setup ;; for convenience, grab some quantities from other-particle let mass2 [mass] of other-particle let speed2 [speed] of other-particle let heading2 [heading] of other-particle ;; since particles are modeled as zero-size points, theta isn't meaningfully ;; defined. we can assign it randomly without affecting the model's outcome. let theta (random-float 360) ;;; PHASE 2: convert velocities to theta-based vector representation ;; now convert my velocity from speed/heading representation to components ;; along theta and perpendicular to theta let v1t (speed * cos (theta - heading)) let v1l (speed * sin (theta - heading)) ;; do the same for other-particle let v2t (speed2 * cos (theta - heading2)) let v2l (speed2 * sin (theta - heading2)) ;;; PHASE 3: manipulate vectors to implement collision ;; compute the velocity of the system's center of mass along theta let vcm (((mass * v1t) + (mass2 * v2t)) / (mass + mass2) ) ;; now compute the new velocity for each particle along direction theta. ;; velocity perpendicular to theta is unaffected by a collision along theta, ;; so the next two lines actually implement the collision itself, in the ;; sense that the effects of the collision are exactly the following changes ;; in particle velocity. set v1t (2 * vcm - v1t) set v2t (2 * vcm - v2t) ;;; PHASE 4: convert back to normal speed/heading ;; now convert my velocity vector into my new speed and heading set speed sqrt ((v1t ^ 2) + (v1l ^ 2)) set energy (0.5 * mass * (speed ^ 2)) ;; if the magnitude of the velocity vector is 0, atan is undefined. but ;; speed will be 0, so heading is irrelevant anyway. therefore, in that ;; case we'll just leave it unmodified. if v1l != 0 or v1t != 0 [ set heading (theta - (atan v1l v1t)) ] ;; and do the same for other-particle ask other-particle [ set speed sqrt ((v2t ^ 2) + (v2l ^ 2)) set energy (0.5 * mass * (speed ^ 2)) if v2l != 0 or v2t != 0 [ set heading (theta - (atan v2l v2t)) ] ] ;; PHASE 5: final updates end ;; creates initial particles to make-particles create-particles number-of-particles [ setup-particle random-position ] ask particles [ set size 0.5 ] calculate-tick-delta end to setup-particle ;; particle procedure set speed init-particle-speed set mass particle-mass set color 106 set energy (0.5 * mass * (speed ^ 2)) set last-collision nobody end ;; place particle at random location. to random-position ;; particle procedure setxy ((1 + min-pxcor) + random-float ((2 * max-pxcor) - 2)) ((1 + min-pycor) + random-float ((2 * max-pycor) - 2)) end to-report last-n [n the-list] ifelse n >= length the-list [ report the-list ] [ report last-n n butfirst the-list ] end to check-for-riders-collision ;; check-for-particle-collision is a particle procedure that determines the time it takes to the collision between ;; two particles (if one exists). It solves for the time by representing the equations of motion for ;; distance, velocity, and time in a quadratic equation of the vector components of the relative velocities ;; and changes in position between the two particles and solves for the time until the next collision let my-x xcor let my-y ycor let my-particle-size size let my-x-speed (speed * convert-heading-x heading) let my-y-speed (speed * convert-heading-y heading) ask riders; with [self != myself] [ let dpx (xcor - my-x) ;; relative distance between particles in the x direction let dpy (ycor - my-y) ;; relative distance between particles in the y direction let x-speed (speed * convert-heading-x heading) ;; speed of other particle in the x direction let y-speed (speed * convert-heading-y heading) ;; speed of other particle in the x direction let dvx (x-speed - my-x-speed) ;; relative speed difference between particles in the x direction let dvy (y-speed - my-y-speed) ;; relative speed difference between particles in the y direction let sum-r (((my-particle-size) / 2 ) + (([size]of self) / 2 )) ;; sum of both particle radii ;; To figure out what the difference in position (P1) between two particles at a future time (t) would be, ;; one would need to know the current difference in position (P0) between the two particles ;; and the current difference in the velocity (V0) between of the two particles. ;; The equation that represents the relationship would be: P1 = P0 + t * V0 ;; we want find when in time (t), P1 would be equal to the sum of both the particle's radii (sum-r). ;; When P1 is equal to is equal to sum-r, the particles will just be touching each other at ;; their edges (a single point of contact). ;; Therefore we are looking for when: sum-r = P0 + t * V0 ;; This equation is not a simple linear equation, since P0 and V0 should both have x and y components ;; in their two dimensional vector representation (calculated as dpx, dpy, and dvx, dvy). ;; By squaring both sides of the equation, we get: (sum-r) * (sum-r) = (P0 + t * V0) * (P0 + t * V0) ;; When expanded gives: (sum-r ^ 2) = (P0 ^ 2) + (t * PO * V0) + (t * PO * V0) + (t ^ 2 * VO ^ 2) ;; Which can be simplified to: 0 = (P0 ^ 2) - (sum-r ^ 2) + (2 * PO * V0) * t + (VO ^ 2) * t ^ 2 ;; Below, we will let p-squared represent: (P0 ^ 2) - (sum-r ^ 2) ;; and pv represent: (2 * PO * V0) ;; and v-squared represent: (VO ^ 2) ;; then the equation will simplify to: 0 = p-squared + pv * t + v-squared * t^2 let p-squared ((dpx * dpx) + (dpy * dpy)) - (sum-r ^ 2) ;; p-squared represents difference of the ;; square of the radii and the square ;; of the initial positions let pv (2 * ((dpx * dvx) + (dpy * dvy))) ;;the vector product of the position times the velocity let v-squared ((dvx * dvx) + (dvy * dvy)) ;; the square of the difference in speeds ;; represented as the sum of the squares of the x-component ;; and y-component of relative speeds between the two particles ;; p-squared, pv, and v-squared are coefficients in the quadratic equation shown above that ;; represents how distance between the particles and relative velocity are related to the time, ;; t, at which they will next collide (or when their edges will just be touching) ;; Any quadratic equation that is the function of time (t), can represented in a general form as: ;; a*t*t + b*t + c = 0, ;; where a, b, and c are the coefficients of the three different terms, and has solutions for t ;; that can be found by using the quadratic formula. The quadratic formula states that if a is not 0, ;; then there are two solutions for t, either real or complex. ;; t is equal to (b +/- sqrt (b^2 - 4*a*c)) / 2*a ;; the portion of this equation that is under a square root is referred to here ;; as the determinant, D1. D1 is equal to (b^2 - 4*a*c) ;; and: a = v-squared, b = pv, and c = p-squared. let D1 pv ^ 2 - (4 * v-squared * p-squared) ;; the next line next line tells us that a collision will happen in the future if ;; the determinant, D1 is >= 0, since a positive determinant tells us that there is a ;; real solution for the quadratic equation. Quadratic equations can have solutions ;; that are not real (they are square roots of negative numbers). These are referred ;; to as imaginary numbers and for many real world systems that the equations represent ;; are not real world states the system can actually end up in. ;; Once we determine that a real solution exists, we want to take only one of the two ;; possible solutions to the quadratic equation, namely the smaller of the two the solutions: ;; (b - sqrt (b^2 - 4*a*c)) / 2*a ;; which is a solution that represents when the particles first touching on their edges. ;; instead of (b + sqrt (b^2 - 4*a*c)) / 2*a ;; which is a solution that represents a time after the particles have penetrated ;; and are coming back out of each other and when they are just touching on their edges. let time-to-collision -1 if D1 >= 0 and v-squared > 0 [set time-to-collision (- pv - sqrt D1) / (2 * v-squared) ] ;;solution for time step ;; if time-to-collision is still -1 there is no collision in the future - no valid solution ;; note: negative values for time-to-collision represent where particles would collide ;; if allowed to move backward in time. ;; if time-to-collision is greater than 1, then we continue to advance the motion ;; of the particles along their current trajectories. They do not collide yet. if future-time? time-to-collision [ ;; time-to-collision is relative (ie, a collision will occur one second from now) ;; We need to store the absolute time (ie, a collision will occur at time 48.5 seconds. ;; So, we add ticks to time-to-collision when we store it. set colliding-pair (list (time-to-collision + ticks) self myself) ;; sets a three element list of ;; time to collision and the colliding pair ; set colliding-particles lput colliding-pair colliding-particles ;; adds above list to collection ;; of colliding pairs and time ;; steps ] ] end to-report convert-heading-x [heading-angle] report sin heading-angle end to-report convert-heading-y [heading-angle] report cos heading-angle end to-report future-time? [time] ;;This reporter is necessary because of slight discrepancies in the floating point math. ;;Sometimes particles that are colliding at that instant are reported as colliding again ;;imperceptibly in the future; this causes the model to hang. ;;This function ensures that all expected collisions are legitimate collisions, and not just ;;ghosts of floating point errors. report time > .0000000001 end ; Copyright 1997 Uri Wilensky. All rights reserved. ; The full copyright notice is in the Information tab.

There is only one version of this model, created over 13 years ago by osnat gal.

## Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.