A heterogeneous reversible reaction with two oxydation states
Channel: Nils Berglund
Date: 24/03/2023

This simulation is a variant of the one shown in the video https://youtu.be/gB6ilM-wKUo but with two states of oxydation. A first attempt of simulating this appeared in the video https://youtu.be/KbU3PB2I5o4 , but there there was a bug in the code which has been fixed in the present version. 
The reaction shown here can be summarized by the equations "A + B ⇌ C" and "A + C ⇌ D", meaning that there are four types of molecules: type A, shown here in red, type B, shown in blue, type C (green), and type D (yellow). Whenever a molecule of type A encounters a molecule of type B or C, they have a certain probability of forming a molecule of type C or D. However, a C molecule can also dissociate into an A and a B molecule, and a D molecule can dissociate into an A and a C molecule, making the reaction reversible. Finally, the reaction is also exothermic, in the sense that creating a C or D molecule increases the kinetic energy of the system, while splitting a C ot D molecule decreases the energy.
In this simulation, there are initially 3035 molecules of type A, 1666 molecules of type B, and no molecules of type C. The molecules interact via a Lennard-Jones potential. Reactions occur with a given probability per time step when they are closer than a fixed distance. B molecules are 50% heavier than A molecules, C molecules weigh as much as an A molecule and a B molecule together, and D molecules weigh as much as two A and one B molecule. The total momentum is conserved during a reaction.
The separation between solid and gaseous phase is achieved by coupling all molecules above a certain height to a thermostat, while the molecules below are subject to a large viscous friction, which acts like a thermostat at zero temperature. For that reason, the A molecules approaching the lower region tend to slow down and pile up, a little bit like a gas condensing on a cold surface. This also slows down the chemical reaction, as the C molecules tend to act like an insulating layer, similar to corrosion on a metal. 
Reactions are highlighted by a small disc that quickly fades. The disc is white when two molecules merge, and colored when a molecule splits. To guide the eye, whenever three molecules of the same type are close enough, the triangular region between them is filled with the same color. In addition, a line is drawn between molecules when they are closer than a given distance (the reaction distance is smaller, however). The graph at the top right shows the number of molecules of each type as a function of time.
This simulation has two parts, showing the same evolution at two different speeds:
Time lapse: 0:00
Slow motion: 1:04
In the first part, time has been accelerated by a factor 3. 
The temperature is controlled by a thermostat, implemented here with the "Nosé-Hoover-Langevin" algorithm introduced by Ben Leimkuhler,  Emad Noorizadeh and Florian Theil, see reference below. The idea of the algorithm is to couple the momenta of the system to a single random process, which fluctuates around a temperature-dependent mean value. Lower temperatures lead to lower mean values. To save on computation time, particles are placed into a "hash grid", each cell of which contains between 3 and 10 particles. Then only the influence of other particles in the same or neighboring cells is taken into account for each particle. 
The Lennard-Jones potential is strongly repulsive at short distance, and mildly attracting at long distance. It is widely used as a simple yet realistic model for the motion of electrically neutral molecules. The force results from the repulsion between electrons due to Pauli's exclusion principle, while the attractive part is a more subtle effect appearing in a multipole expansion. For more details, see https://en.wikipedia.org/wiki/Lennard-Jones_potential

Render time: 38 minutes 15 seconds
Color scheme: Turbo, by Anton Mikhailov
https://gist.github.com/mikhailov-work/6a308c20e494d9e0ccc29036b28faa7a

Music: "No Good Right" by Freedom Trail Studio@FreedomTrailStudio

Reference: Leimkuhler, B., Noorizadeh, E. & Theil, F. A Gentle Stochastic Thermostat for Molecular Dynamics. J Stat Phys 135, 261–277 (2009). https://doi.org/10.1007/s10955-009-9734-0
http://www.maths.warwick.ac.uk/~theil/HL12-3-2009.pdf

Current version of the C code used to make these animations: 
https://github.com/nilsberglund-orleans/YouTube-simulations
https://www.idpoisson.fr/berglund/software.html
Some outreach articles on mathematics:
https://images.math.cnrs.fr/_Berglund-Nils-1343_.html
(in French, some with a Spanish translation)