Maxwell's Demon

This week we were asked to create a simulation of Maxwell’s Demon.

Click here to run the simulation online

I could not attend the class and I never heard of Maxwell’s Demon, so by reading about it and watching some youtube I understood that’s it an interesting thought experiment about the 2nd law of thermodynamics, entropy and information.

Basically, you have two chambers with gas molecules of different energy. The chambers are connected by a door, controlled by a demon. The demon opens and shuts the door such that fast particles can pass to one side, and slow molecules to the other side. This causes the two chambers to become more organized, thus the entropy decreases. According to the 2nd law, entropy can only increase in a closed system, so where do the extra work come from? is it the demon opening and shutting the door? apparently not, since we can think of a demon randomly opening and shutting the door, exerting the same amount of work. Then, the work must be done in the demon’s brain. In the information which it uses to decide on the action of the door.

To simulate this, I chose to use three.js. While I am personally more comfortable with Python, three.js allows for fast and easy rendering of 3d objects, and I wanted to create a 3d chamber.

I followed the code in this post: Bounce Some Particles with three.js. Which gives you some nice particles bouncing around in two dimensions. I added another dimenson, and an invisible wall (x=0) that particles couldn’t pass. Then I added a demon door - a space in the wall that if the particle touched it might pass. If it’s a malicous demon, the particle passes dependant on its speed (which is randomly chosen). If it’s a random demon, the particle might pass at a 50% chance.

You can run the simulation and play with its parameters here

You can increase the number of particles, speed and size of the box and hit refresh. Also, you can change the demon mode from malicous to random (no need to refresh!) and visually examine how the entropy changes.

At each animation frame, the particles are moved according to their randomly chosed direction vectors:

particles[i].position.x += particles[i].direction.x;
particles[i].position.y += particles[i].direction.y;
particles[i].position.z += particles[i].direction.z;

// if edge is reached, bounce back
if (Math.abs(particles[i].position.x) > (2 * config.boxRadius)) {
    particles[i].direction.x = -particles[i].direction.x;
}
if (Math.abs(particles[i].position.y) > config.boxRadius) {
    particles[i].direction.y = -particles[i].direction.y;
}
if (Math.abs(particles[i].position.z) > config.boxRadius) {
    particles[i].direction.z = -particles[i].direction.z;
}

But, another code block comes which handles the case of the middle “wall” and the demon door inside of it:

// if the middle "invisible" wall is touched, bounce back
if (prevX > 0 && particles[i].position.x <= 0) {
  // unless, we hit the devil's door!
  if (Math.abs(particles[i].position.y) < config.demonDoorRadius &&
  Math.abs(particles[i].position.z) < config.demonDoorRadius) {
    if (config.malicousDemon > 0) {
      if (!particles[i].hot) {
        // thou shall not pass
        particles[i].position.x = prevX;
        particles[i].direction.x = -particles[i].direction.x;
      }
    } else if (Math.random() > 0.5) {
      // random demon
      particles[i].position.x = prevX;
      particles[i].direction.x = -particles[i].direction.x;
    }
  } else {
    // this means we hit the middle wall, but not the door, bounce
    particles[i].position.x = prevX;
    particles[i].direction.x = -particles[i].direction.x;
  }

}

For brevity I only included the part for the hot particles, but the same logic exists for cold ones. For the full source code, open the simulation and View Source Code.