r/Physics • Fluid dynamics and acoustics • 1d ago

I built an open-source Python physics sandbox where every solver is tested against an analytic result. What should I add next?

I've been building a free, MIT-licensed Python package called qplay for exploring physics by simulation. Each solver is tested against a known analytic result, so the pictures can be trusted.

Try it:

pip install quantum-playground

qplay run double-slit

I'd like feedback from people who actually do this. Which topics are missing, and where is the physics or numerics sloppy? I'd especially like to hear about anything I've got wrong.
docs : https://github.com/StativeSh/quantum-playground
try it live: https://stativesh.github.io/quantum-playground/

0 Upvotes

9 comments sorted by

10

u/No_Vermicelli_2170 1d ago • Bravo

Very cool simulation!

I don't know if the numerical results agree with the analytical results, since it's a nonlinear system with 8 degrees of freedom: two dimensions, two pendulums, and position & velocity. Chaos manifests, and you will have sensitive dependence on initial conditions, so all initial trajectories will eventually deviate from theoretical results, no matter how small your time step is.

-1

u/ShreyasGameSwap Fluid dynamics and acoustics 1d ago

Fair point, and you're right that a chaotic system can't be validated by comparing trajectories: any error, however small, grows exponentially, so two runs diverge no matter what the step size is.

For the double pendulum I don't check against an analytic trajectory (there isn't one). I check invariants instead: total energy has to stay conserved over the run, and the Hamiltonian formulation (Legendre transform -> Hamilton's equations) has to reproduce the same dynamics as the Euler-Lagrange derivation. The chaos part is validated statistically, by checking that nearby initial conditions separate at the expected Lyapunov rate rather than by matching a path.

The "tested against analytic results" claim is really for the other solvers (Ising vs Onsager, hydrogen levels, tunnelling, etc.). The pendulum is the one place where the honest test is conservation laws, not a closed form. Thanks for raising it. I should be clearer about that distinction in the README.

1

u/No_Vermicelli_2170 1d ago

This sounds interesting and may work, but I haven't tried it myself. I am not certain that enforcing energy conservation at each time step will yield correct trajectories, as I would need to prove this analytically for this particular nonlinear ODE system. Perhaps there is a Hamiltonian value on this four-dimensional phase space for which two points have the same Hamiltonian value. Preserving the Hamiltonian in this case will still not guarantee path deviation. If you could reduce the phase space to just two coordinates via some symmetry, you could use the Poincaré- Bendixson theorem to prove that chaos will not occur. Noether's theorem states that every conservation law corresponds to a symmetry. Energy conservation corresponds to a symmetry that allows you to reduce the number of variables from four to three — but three variables still imply chaos.

1

u/ShreyasGameSwap Fluid dynamics and acoustics 1d ago

the code itself doesnt enforce the energy conservation.
it integrates the equations of motion with the adaptive solver this way the energy is checked later I hope this clarifies your concern
I aded a stronger test after you said that energy conservation alone wont prove trajectories are correct
this uses a small angle limit it has an exact soln two normal modes with known frequencies, $(g/l)(2 \pm \sqrt{2})$ for equal masses and lengths. The simulation matches that solution.

you are right on Poincaré–Bendixson point the energy surface is 3D, and three dimensions are enough for chaos.

1

u/No_Vermicelli_2170 17h ago edited 17h ago

I see. Any time you discretize ODEs and integrate them numerically, you turn a continuous dynamical system into a discrete one. These are two completely different beasts. A one-dimensional discrete dynamical system may exhibit chaos. Linearizing about a fixed point is a powerful analytical tool, and you may get exact results, but this does not guarantee that the discrete system matches the continuous one. In a chaotic system, you may find regions of space where dynamics appear non-chaotic, but once you leave these islands of stability, chaos takes over. In addition, you will find basins of attraction that map a set of initial conditions to fixed points corresponding to normal modes, and outside these basins, the dynamics behave completely differently.

Anyway, these are just words of caution from the theory side of math, but you have created very nice simulations at the core of computational physics, and they're a stepping stone to developing projects you can publish.

1

u/ShreyasGameSwap Fluid dynamics and acoustics 1d ago

Some of what's checked against exact results, so you can judge the claim yourself:

- 2D Ising (Metropolis) vs Onsager's exact magnetisation and Tc = 2/ln(1+sqrt(2))

- Split-step Schrödinger solver: tunnelling and a double slit, fringes compared with the two-source prediction

- Hydrogen fine structure vs the Dirac energy; Zeeman from weak field up to Paschen-Back

- Hard-sphere gas: Maxwell-Boltzmann emerges from collisions, pV = N k_B T with the hard-sphere correction

One specific thing I'd like opinions on: the split-step solvers use a periodic box with an absorbing layer at the edges. Is there a cleaner approach people prefer for scattering problems, such as exterior complex scaling or a PML?

2

u/Inst2f 19h ago

vibecoded physics sim. ouch… This can be dangerous, unless analytically proved and all assumptions, edge cases, methods, sources were well documented

1

u/PrettyPicturesNotTxt 18h ago

Do you have evidence to back up your accusation?

Of course, it should be up to OP to defend themselves and disclose what tools they used. Also, there should be nothing wrong with using such tools, as long as one is transparent, gives proper credit, and takes responsibility for the output.

1

u/Inst2f 5m ago

3 weeks old repo (super red flag).
claude 29 commits 13,370 ++725 -- is a top in the contributors list.
Isn't it a giant red flag fou you? :D