Thermo Simulation

Apr 2019
HTMLCSSJavaScript

In first semester of uni I did one Physics course as a science elective that had a module on thermodynamics, which inspired me to make some project related to it.

In the end I made what is essentially a particle simulation to simulate the transfer of heat between individual particles (molecules) at a micro-scale. At a macro-scale, the simulation has some emergent properties that are analogous to how real life matter works which is quite nice.

On the State of Matter

I went into this not expecting anything in particular (just driven by curiosity) and was pleasantly surprised when I was able to generate quite feasible simulations of both gaseous and solid forms of matter. Liquid matter is also sort of possible, although much less stable due to the particular phase diagram of the simulation.

Gas

Simulating a gas is quite easy parameter-tuning-wise. You just need it so the particles are hot (moving fast) enough to overcome the attractive forces between them.

Simulating a gas.
Simulating a gas.

Every now an then a particle will bounce off another particle, but most of the time particles travel in a straight line independently of each other.

In the real world, we can expect a similar situation with particles in a gas. Unlike in the simulation, the rare cases when particles bounce off or divert the path of another occur are actually very common due to the incomprehensible number of atoms. This is why the diffusion (mixture) of gases takes some time despite the average hydrogen particle moving 1.84km/s at room temperature.

Perhaps if I made a simulation with many more particles we could see this effect.

Solid

It's relatively easy to get a solid if you cool the particles down enough by reducing their average velocity/vibrations.

Simulating a solid.
Simulating a solid.

When the attractive forces dominate, the particles will settle into a stable low energy state, which for 2D point particles happens to be a hexagonal lattice structure.

In the real world, molecules live in 3D space and are not uniform point attractors. This is why the crystal structures can vary between different materials, but the same general principles apply.

Liquid

Using the simulation, it's possible to generate a kind of mushy blob that might be analogous to a liquid in the real world.

Simulating a liquid (almost).
Simulating a liquid (almost).

The blob is not a stable lattice structure (and so not a solid), but also isn't gas as most particles are not able to overcome the attractive forces between one another (thus the mushyness).

You may also notice that occasionally a particle gets a lucky boost of kinetic energy that is enough to escape the blob. This is actually to be expected and happens in the real world all the time: evaporation!

Achieving this liquid state however requires careful tuning of particle velocities. Part of the reason is that the particles are experiencing very low pressures due to existing in what is essentially a vacuum.

Phase diagram of water.
Notice how in this phase diagram of water, achieving a liquid is impossible if the pressure is low enough.

If I added some kind of external force pushing the particles inwards, perhaps we would see more liquids form. Project for another day 😊.

Technology

Stack

At the low level, the simulation was built with raw JavaScript using my own UI library canvasLib.

Particles

At a high level, the simulation is a collection of point objects with mass & velocity that attract and repel one another. Particles that are too close will repel one another at an increasing rate. Particles that are far enough apart will attract one another at a decreasing rate. Importantly, this creates a stable distance where the attractive and repulsive forces cancel each other out.

Sources

1. Phases of Matter diagram. By author of the original work: Cmglee - Own work, CC BY-SA 3.0.