Side cross-section of the ball basin: rigid discs, tunable bounciness, gravity and shaking, with live energy and height-distribution graphs.
The science
Each ball is a rigid disc of mass m = rho pi r^2 (rho = 200 kg/m^2 of cross-section, like a hollow plastic ball). Between contacts it falls with a = -g. When two discs overlap along the contact normal n their approach speed v_n is replaced by -e v_n, which for equal masses means an impulse J = (1+e)|v_n|/2 on each ball; e = 1 conserves kinetic energy, e near 0 makes the balls stick together and lose it.
A ball dropped from height h0 rebounds to e^2 h0 after one bounce, so the n-th rebound reaches e^(2n) h0. Shaking the basin is modelled as a pseudo-force -m a sin(2 pi f t) in the basin frame. The energy graph shows how fast the pile settles; the height histogram shows the packed layers.
Simplifications: 2D discs instead of 3D spheres, no friction or spin, no air drag, equal masses, positional pile-up resolved by two relaxation passes per 1/480 s step, so very dense piles slightly compress and the total energy can drift upward a little.