NetLogo banner

NetLogo Publications
Contact Us

Modeling Commons

Beginners Interactive NetLogo Dictionary (BIND)
NetLogo Dictionary

User Manuals:
Farsi / Persian


NetLogo Models Library:
Sample Models/Chemistry & Physics/GasLab

(back to the library)

GasLab Maxwells Demon

[screen shot]

If you download the NetLogo application, this model is included. You can also Try running it in NetLogo Web


This model illustrates a famous thought experiment, which raised important issues about the nature of entropy.

In 1871, James Clerk Maxwell imagined a situation where a very small and nimble being could, by opening and closing a valve between two gas-filled chambers, allow only faster particles to go through one way and only slower particles to go through the other. By doing this, the being (later called a demon) could raise the temperature of one chamber (faster particles) and lower the temperature of the other (slower particles), without the expenditure of work. This was a violation of the Second Law of Thermodynamics, and he asserted that this could not actually occur. The implications of this puzzle have continued to be central to thermodynamics, entropy, and information theory up to the present time.

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.


The particles are modeled as hard balls with no internal energy except that which is due to their motion. Collisions between particles are elastic. Particles are colored according to speed --- blue for slow, green for medium, and red for high speeds.

Coloring of the particles is with respect to one speed (10). Particles with a speed less than 5 are blue, ones that are more than 15 are red, while all in those in-between are green.

Particles behave according to the following rules: 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). 3. A random axis is chosen, as if they are two balls that hit each other and this axis is the line connecting their centers. 4. 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. 5. Each turtle is assigned its new velocity, energy, and heading. 6. 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 setup is the same as that for the "Two-Gas" model. What is added is a "valve". It transports fast particles from the left to the right chamber when they arrive at the turquoise strip, and slow particles from the right to the left chamber when they arrive at the violet strip. When a particle passes through, it becomes larger for a short time and displays its speed.


Settings: NUMBER-OF-PARTICLES: total number of particles in the two chambers INIT-PARTICLE-SPEED: initial speed of all the particles PARTICLE-MASS: particles' mass

Other settings: COLLIDE? If this is On, the particles collide. If it is Off, they do not collide. THRESHOLD: The threshold is how much faster or slower than the average speed a particle must be going to be transferred to the other chamber by the "valve". DEMON?: If this is On, the valve is operating. If it is Off, the valve does nothing. TIME-DISPLAY-PARTICLES: determines for how long the particles are visually enlarged and their speeds are labeled, before the go back to their original size.

Initialize the model by pressing SETUP, and press GO to run it.

Monitors: AVERAGE SPEED: average speed of all the particles. AVERAGE SPEED LEFT: average particle speed in the left chamber. AVERAGE SPEED RIGHT: average particle speed in the right chamber.

Plots: PARTICLES COUNT: the number of particles in the left (turquoise) and right (violet) chambers. AVERAGE ENERGIES: the average energy of the particles in the left and right chambers. This is calculated as the average of 1/2 mv^2 for the particles. AVERAGE SPEEDS: the average speeds of the particles in both the left and the right chambers.


Watch the "valve" carefully. Can you see fast (red) particles jump right and slow (blue) particles jump left when they run into the valve? They are enlarged and their speeds are labeled for a short time.

What happens to the average energies and speeds in the left and right chambers? Do the values settle down after a long time?

Notice the particles count in the two chambers. While the model starts out with more particles going into the right chamber than the left, this changes after a while. Why does that happen? Given the Maxwell-Boltzmann distribution, which chamber should have more particles?


Change the valve threshold. Does it change the rate of evolution of the model? Does it change its eventual result?


What happens if there are particles of different masses? (See Two Gas model.)

How would you calculate the pressure in each box? (See Piston models.) Do they remain equal?

If such a valve were possible, could "free energy" be gotten from the two chambers at different temperatures?

Another way of increasing order and later extracting energy might be to allow the valve to let through particles in only one direction, so that eventually one chamber is filled with gas, while the other one ends up empty. Can you create such a system?


Look at the other GasLab models.


This model was developed as part of the GasLab curriculum ( and has also been incorporated into the Connected Chemistry curriculum (


If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.

For the model itself:

Please cite the NetLogo software as:


Copyright 1997 Uri Wilensky.


This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit 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

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

(back to the NetLogo Models Library)