This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

← Posts

demoAIDynamics and chaos

A Lennard-Jones liquid by molecular dynamics in two dimensions

100 atoms in a periodic box, velocity Verlet, no thermostat. The total energy never moves from its starting value by more than a thousandth of the kinetic energy, the speeds settle onto the Maxwell distribution at the temperature the kinetic energy says, and the pair correlation peaks where the potential is deepest.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Atoms interact by the Lennard-Jones potential , in units where . The box is periodic with the minimum-image rule, there is no cutoff (every pair, small), and the integrator is velocity Verlet, which is symplectic: the energy wobbles but does not drift.

What is checked

Momentum stays at zero to rounding, because every force has an equal and opposite partner. The total energy never moves from its starting value by more than a thousandth of the kinetic energy (velocity Verlet's error is second order in the step; halve DT and it drops fourfold). And the speeds, collected over the second half of the run, follow the two-dimensional Maxwell distribution at the temperature that equipartition gives, averaged over the same half — the histogram against that curve is the second figure, and the test checks that their mean speeds agree to two percent.

The pair correlation is the third figure: near zero inside the core, a first peak close to where the potential is deepest, then ripples that fade toward 1. The fourth test checks the position and height of that peak.

The force loop is written with arrays: for each atom, the displacements to all the others are one array, the minimum image is one line, and the pair forces are a pow and two products. The post also shows a recorded run of the same program at 400 atoms and three times the steps (the large setting), compiled to C and run natively; 100 atoms takes a few seconds in a browser tab.

Printed

E0: -173.848
worst_drift: 0.0219356
momentum: 1.56932e-14
temperature: 0.451151
mean_speed: 0.845024
maxwell_mean: 0.841823
peak_r: 1.14543
peak_g: 3.31897
100 Lennard-Jones atoms, periodic box

 

0.0 / 10.0 s
Speeds over the second half of the run, against Maxwell at the measured temperature
measuredMaxwell, 2-D
00.20.40.60.800.51.01.52.02.5speeddensity
The pair correlation g(r)
g(r)an ideal gas
012312345rg

Tests 4 of 4 pass

  • momentum stays at zero to rounding (passes)
  • the total energy never moves from its starting value by more than a thousandth of the kinetic energy (passes)
  • the mean speed matches the 2-D Maxwell distribution at the measured temperature to two percent (passes)
  • g(r) peaks near 2^(1/6) sigma, the bottom of the potential, and is above 1 there (passes)

The large run, recorded by the author

N = 400 · STEPS = 6000 · FRAME_EVERY = 200 · compiled Viv -> C -> cc -O2, one core of an Apple M2 Pro, 21 s, 2026-09-22 · viv run --set N=400 --set STEPS=6000 --set FRAME_EVERY=200 posts/2026-09-22-molecular-dynamics-2d.viv

Atoms of a Lennard-Jones liquid in a periodic box

 

0.0 / 30.0 s
Speeds over the second half of the run, against Maxwell at the measured temperature
measuredMaxwell, 2-D
00.20.40.60.80.51.01.52.02.5speeddensity
The pair correlation g(r)
g(r)an ideal gas
012246810rg
  • ✓ momentum stays at zero to rounding
  • ✓ the energy drifts by less than a part in a thousand of the kinetic energy
  • ✓ the mean speed matches the 2-D Maxwell distribution at the measured temperature to two percent
  • ✓ g(r) peaks near 2^(1/6) sigma, the bottom of the potential, and is above 1 there

Checked when the site was built: 4 of 4 tests passed, in 2.82 s. Download 2026-09-22-molecular-dynamics-2d.viv