Building a Fluid Simulator


Building a Fluid Simulator

Hope
Welcome to the series. Our goal is to build a fluid simulator from the ground up, then use it to study familiar phenomena like lift and drag, as well as less familiar ones such as vortex shedding and the interplay between pressure and velocity.

Corey
And instead of beginning with ready-made software, we are building the tool ourselves for two reasons. First, we will be learning the fundamentals anyway, so applying those ideas directly is useful. More importantly, seeing where the concepts and equations come from makes the simulator something we can genuinely understand and trust.

Hope
Exactly. We will start at the subatomic level, then reduce the complexity in several stages. We will average over molecules, describe the fluid as a continuous field, discretize that field in space and time, and finally handle the boundaries properly.

Corey
That modular structure matters. There is not one universally correct way to simplify a fluid problem. Different layers or approaches can be exchanged when needed, and organizing the simulator into modules gives us a framework we can reuse in future problems.

Hope
The central theme is information reduction. A fluid contains an overwhelming number of molecules, and much of the detailed information is irrelevant if we want a global quantity such as net lift. The exact rotation or location of one molecule is not what determines the final answer; what matters is the average behavior of large groups of molecules.

Corey
So before we simulate anything, we need to ask which information is useful and which information can be averaged away. Modeling every molecule with all of its subatomic details would be far beyond what we need—and computationally infeasible.

Hope
That brings us to the microscopic foundation: quantum mechanics. It helps explain why modeling matter at the smallest scale is so difficult, beginning with the meaning of measurement itself.

Corey
For a macroscopic object, such as a ball, tracking a trajectory is straightforward. But tracking an elementary particle, like an electron near a hydrogen nucleus, is different. To learn where the electron is, we might send photons toward the atom, but the photon can disturb the electron and change what happens next.

Hope
At that scale, looking is really interacting. The measuring device and the electron are much closer in energy than the components of an ordinary macroscopic measurement, so the measurement exerts a significant influence. Even apart from that disturbance, the Heisenberg uncertainty principle places a fundamental limit on how precisely position and momentum can both be known.

Corey
That means an undisturbed, perfectly defined particle trajectory loses its meaning at the quantum level. What does remain meaningful is probability: for example, the likelihood of detecting an electron in a particular region of an atom.

Hope
Those probability distributions can have different shapes depending on properties such as the electron's energy or the strength of an electric field. With multiple electrons, their motions are correlated, so the true multi-electron probability distribution is not simply a collection of independent single-electron distributions, although those can be used as an approximation.

Corey
And elementary particles have another complication: they behave both like particles and like waves. They are detected as distinct impacts at locations, but when they are not observed, they can diffract, interfere, and go around corners.

Hope
Quantum mechanics provides a mathematical framework for that behavior. Its two central ingredients here are a wave function, which contains information about the states of the particles, and the Schrödinger equation, which evolves that wave function in time. From the resulting wave function, we derive probabilities for measurement outcomes.

Corey
The computational problem appears when we try to represent that wave function. Imagine one particle in a two-dimensional region, with a complex number assigned to every point. If we restrict the region and divide each side into one thousand cells, we already need a million cells—and therefore a million complex values—for just one particle.

Hope
And those cells are coupled. One cell cannot be understood in isolation because neighboring cells influence the evolution, and the complete wave function describes the whole system. Adding particles makes the problem grow dramatically, because the wave function must represent joint probabilities.

Corey
For two particles in one-dimensional space, the joint wave function needs two spatial dimensions. It tells us the probability of finding particle one in one region while particle two is in another. The individual particle probabilities are obtained as marginal probabilities by considering every possible position of the other particle.

Hope
If each physical dimension has one thousand cells, two particles in one-dimensional space already require a million cells in the joint representation. Two particles in two-dimensional physical space produce a wave function living in four dimensions, which can require a million times a million cells. More particles, three-dimensional space, and properties such as spin make the growth even worse.

Corey
The wave function also needs physical rules. Inter-particle potentials can represent attraction or repulsion, and identical particles must be treated as indistinguishable. Swapping two electrons should not create physically labeled outcomes such as 'electron one' and 'electron two'; the model has to include the appropriate symmetry.

Hope
So a direct quantum simulation of even a small fluid quickly becomes impossible on conventional computers. We need surrogate models that preserve essential physical behavior while being much faster to solve.

Corey
One possible reduction is model order reduction. Many dynamical systems evolve in recognizable patterns, so instead of evolving every cell, we can represent the behavior using a smaller collection of mode shapes, or standing waves, and evolve their scaling factors.

Hope
For a particle in a potential that pushes it back toward the center, the wave function can show repetitive behavior. A carefully selected combination of a few standing-wave shapes may approximate the full evolution very accurately. In a linear evolution equation, the scaling factors can be especially simple to compute.

Corey
A standing wave has a probability distribution that stays fixed even though its complex wave-function values oscillate. Each complex value can rotate around a circle in the complex plane, while its absolute square—and therefore the probability—remains constant. The rotation frequency is related to the energy and becomes important later.

Hope
The number and choice of modes let us trade accuracy for computational cost. But this does not solve our fluid problem by itself. It is still a quantum model involving wave functions and probabilities, and finding suitable modes for high-dimensional systems can itself be difficult.

Corey
What we need is a change in the underlying paradigm: molecular dynamics. Instead of tracking every possible quantum state, we approximate atoms as localized particles with positions, velocities, and unique classical trajectories.

Hope
The positions and velocities together form a point in phase space. Phase space may have many dimensions, but the simulation only needs to evolve one point for one selected trajectory, rather than all possible states simultaneously. This is a major computational advantage.

Corey
Molecular dynamics treats each atom as a particle in classical mechanics. Attractions, repulsions, and bonding are represented by forces that depend on distance, usually through inter-atomic potentials. A potential describes the work needed to move a particle, and differentiating the potential gives the force.

Hope
The Lennard-Jones potential is one common example. It represents weak Van der Waals attraction together with strong short-range repulsion. Instead of the Schrödinger equation evolving a wave function, Newton's second law evolves positions and velocities: the net force determines the change in momentum, and numerical integration updates the particles.

Corey
But why is this classical approximation reasonable? Consider the interaction between a nucleus and an electron. The same force produces much less acceleration in the heavy nucleus than in the light electron, so from the electron's perspective the nucleus moves very slowly, while from the nucleus's perspective the electron reacts almost immediately.

Hope
That difference in timescales lets us separate the motion mathematically. We can hold the nuclei at fixed positions while solving for the electrons. The electron calculation then supplies potential energy values, and the resulting energy changes provide forces acting on the nuclei.

Corey
For the electron calculation, we seek the lowest-energy wave function reached after energy loss through radiation. The nuclei effectively see that state because the electrons respond so quickly. The lowest-energy state is associated with the first standing wave, and its energy is related to its oscillation frequency.

Hope
Repeating that lowest-energy calculation for different arrangements of the nuclei produces an inter-atomic potential energy surface. Its derivative gives the net force between nuclei. In practice, the full calculation is often replaced by empirical approximations, such as the Lennard-Jones potential or the Morse potential for interatomic bonding.

Corey
The nuclei can then be propagated either quantum mechanically or as classical particles. The particle approximation is more reasonable for heavy nuclei because localization increases uncertainty in momentum, but the larger mass keeps the resulting velocity spread relatively low and slows the spread in future position.

Hope
Under the assumptions we need here, atoms can therefore be simulated as particles moving along trajectories within potentials. Atoms can form molecules, and molecules can translate, rotate, collide, and vibrate. Molecular vibrations occur on much smaller length scales than the motion shown in simplified illustrations.

Corey
The accuracy depends on the system's conditions and the desired level of detail. Usually, masses and potential parameters are chosen empirically so that statistical quantities from many molecular interactions match measurements, at least over the range of conditions for which the parameters were developed.

Hope
We can simplify even further by replacing potential-based interactions with instantaneous collisions. Collisions are cheaper to compute—velocities can essentially be reflected—and parameters can be adjusted so that the collision model and the potential model produce similar results from a sufficiently distant, statistical viewpoint.

Corey
That shift in viewpoint is crucial. Molecular dynamics does not only reduce complexity by combining subatomic particles into atoms or simplifying potentials. It also moves us from a quantum calculation over all possible joint states to classical trajectories that can be sampled and averaged afterward.

Hope
In classical mechanics, we can compute a set of trajectories and then average them. We can even trade statistical significance against computational cost by simulating fewer trajectories. Compared with a quantum model that might handle only a few subatomic particles, molecular dynamics can reach roughly one hundred thousand atoms in an educational setup.

Corey
The next abstraction is the kinetic theory of gases. It carries the molecular-dynamics assumptions one step further by combining atom particles into molecule particles. Since it is a theory of gases, it works best when particles are spread out and their free-flight periods between interactions are much longer than the interactions themselves.

Hope
In this model, a molecule is represented by a single point mass with an effective collision radius. The mass is approximately the sum of its components, but choosing the radius is not obvious because real molecules can scatter in more complicated ways.

Corey
For example, in a molecular-dynamics experiment, molecules with the same total energy can approach one another with different offsets. Their outcomes vary because the total energy includes both atomic kinetic energy and inter-atomic bonding potential energy. A point-mass collision model with one fixed radius always gives the same type of outcome, so it cannot reproduce every local detail.

Hope
That loss of detail is intentional. We choose an effective radius that gives the best overall behavior, rather than demanding that every individual interaction match. A molecule passing through the attractive part of a potential may perform a slingshot-like maneuver, which a pure collision cannot represent locally.

Corey
Instead of weighting many isolated experiments by hand, we simulate the fluid as a whole. The fluid naturally supplies a distribution of random pre-collision conditions, and we compare global statistical behavior—such as how particles mix between layers—rather than comparing every individual collision.

Hope
There are many possible global statistics, and the key is to think fluid-wise rather than interaction-wise. The particles remain in the simulation, but local differences are acceptable if the collection behaves correctly on average. Those assumptions complete the microscopic, kinetic view of a fluid.

Corey
The next part of the series will use this particle model to derive the macroscopic perspective, where we can simulate flow on much larger scales. Concepts such as pressure, viscosity, temperature, and flow velocity will emerge from the collective motion and collisions of molecules.

Hope
Let's recap the progression. We began with the practical and theoretical limits on what we can know about elementary particles. Quantum mechanics naturally introduced probabilities, and then we progressively reduced the information we had to track.

Corey
The biggest leap was moving from quantum mechanics to classical mechanics. That allowed us to represent atoms with individual trajectories and to model probabilistic behavior by averaging many trajectories afterward. From there, kinetic theory combined those atom-level particles into a statistical model of gas molecules.

Hope
The broader lesson is that a complex problem can be broken into simpler parts. Once we identify the central challenge—in this episode, reducing information—we can look for patterns, choose guiding principles, and build the solution layer by layer.

Corey
That is what makes intimidating subjects such as quantum mechanics and fluid simulation feel believable. We are not skipping the foundations; we are understanding why each abstraction is needed and what information it preserves.

Hope
With that microscopic foundation in place, we are ready to move upward toward the macroscopic description of fluid flow—and eventually toward a simulator we can inspect, modify, and use to explore fluid dynamics for ourselves.

Ask anything
We've read this whole study set, so questions about it get the best answers.