Unit 3 / 11

Simulation of Physical Systems

Gains:

  • Ability to simulate a dynamic physical system with solvers such as solve_ivp and verify the validity of the simulation by monitoring total energy or momentum conservation
  • Ability to distinguish numerical artifacts from real physics by selecting the step size with a convergence test
  • Ability to test the simulation result by comparing it with known analytical situations and physical limit states

Much of physics deals with time-varying systems: an oscillating pendulum, an orbiting planet, a cooling body, a propagating wave. Most of these systems are described by differential equations—that is, equations that relate the rate of change of a quantity (position, temperature) to that quantity itself. These equations often do not have an analytical (formula-exact) solution; instead we simulate: moving the system forward in small time steps and numerically monitoring its behavior. In this unit, you will learn how to use artificial intelligence (AI) to build simulation code and — most importantly — how to verify with conservation laws whether the simulation accurately reflects physics.

The basic idea of ​​simulation and the role of AI

A simulation consists of this cycle: take the current state, apply the equation of change, move forward by a small Δt, repeat. The AI ​​is very fast at building this loop, choosing the right solver — the algorithm that solves the differential equation step by step, and editing the code. The physicist's standard tool is scipy.integrate.solve_ivp (the SciPy function that solves initial value problems). But before you trust the simulation created by AI, the question you must ask is this: Does this simulation preserve the physical quantities (energy, momentum, angular momentum) that need to be preserved?

System

Equation type

Validation criteria

pendulum, spring

Second degree ODE

Constancy of total energy

planet orbit

Newtonian gravitational ODE

Angular momentum + conservation of energy

Cool/heat

First degree ODE

Convergence to equilibrium temperature

radioactive decay

Exponential ODE

Half-life control

Monte Carlo (random)

statistical

Convergence to known mean/distribution

Note: ODE stands for "ordinary differential equation" — an equation that involves a derivative with respect to a single variable (usually time).

Step by step: a reliable simulation

1. Clarify the physics and initial conditions. What is the equation of the system? What is the starting position, speed, temperature? What are the units? Give these explicitly to the AI.

2. Select appropriate solver and step size. Too large a time step makes the simulation unstable (the result explodes or the physics breaks); Too small a step slows down unnecessarily. AI may suggest, but control the decision.

3. Follow the conservation law in code. Calculate and print the total energy (or momentum) throughout the simulation. If it drifts a magnitude that should remain constant, the simulation is unreliable.

4. Compare with a known solution. For example, the period of a small amplitude pendulum is known by the formula T = 2π√(L/g); Compare the period given by the simulation with this.

5. Change parameter, test behavior. Is the oscillation dampened when friction is added? Does the orbit change as expected when the mass increases? Test with physical intuition.

Tip: Add an "energy tracker" to each simulation: calculate the total energy at each step and plot it as a ratio to the initial energy. In a frictionless system, this ratio should remain constant close to 1. Drift of more than 1% is a sign that you need to reduce the step size or change the solver.

three mini cases

Case 1 — Burst of energy. A student simulated a planetary orbit with a simple Euler method (the most primitive step method) written by AI. The orbit gradually widened over time and the planet "flew away". As the student monitored the total energy, he saw that it was constantly increasing—a known flaw of Euler's method. He asked AI for a solver that conserves energy better (a more suitable method with solve_ivp) and the orbit settled into a stable ellipse.

Case 2 — Wrong unit, wrong period. A teacher found the period to be 0.2 s in a pendulum simulation, but the expected value was 2 s. When he examined it, he realized that the AI ​​entered the length in centimeters and used it as a meter in the formula. With unit correction, the simulation agreed with the analytical value of T = 2π√(L/g).

Case 3 — Monte Carlo verification. A researcher asked the AI ​​for a code that randomly samples (Monte Carlo — statistical simulation with random numbers) particles hitting a detector. To verify the result, he ran a simple case known analytically (e.g. estimating π by the ratio of the area of ​​a circle) by the same method; When the code correctly predicted π, its confidence in the actual simulation increased. He also observed that as the number of samples increased, the result narrowed and converged to the real value.

Four copyable templates

1) Conservation-monitored simulation:

Write Python code that simulates the following physical system with solve_ivp: [system, equation, initial conditions, units]. Throughout the simulation, calculate the total energy (and momentum, if any) at each step and plot its ratio to its initial value over time. Note that in the frictionless case, this ratio should remain constant ~1.

2) Analytical comparison:

Add code that compares the result of the following simulation to the known analytical solution of this system ([formula]). Plot them both on the same graph and print the maximum difference between numerical and analytical. Simulation code: [here]

3) Step size / stability test:

Write code that runs the following simulation with different time steps (e.g. dt = 0.1, 0.01, 0.001) and shows how the results change. Specify after which step size the result stabilizes (converges). Code: [here]

4) Physical limit state testing:

Suggest 3 physical limit state tests for the following simulation: (e.g. energy should be conserved when friction is zero, stalling quickly when friction is large, how the period should change when mass doubles). Write the expected result of each test in one sentence. Code: [here]

Weak prompt / Strong prompt

Weak: "Write a pendulum simulation."
Result: code with no units, no validation, no conservation checks; It is unclear whether it reflects physics or not.
Strong: "Write a solve_ivp code that simulates a simple pendulum of length L = 1 m, starting angle 10°, with g = 9.81 m/s² for 10 seconds. Monitor the total energy at each step and plot the ratio to the start. Compare the measured period with the small amplitude formula T = 2π√(L/g) and print the difference."
Result: A reliable simulation with units, conservation monitoring, and comparison with the analytical value.

Common mistakes

  • Not following the law of conservation. If energy or momentum is drifting, the simulation is wrong; Trusting the result without seeing this is the most common mistake.
  • Choosing the step size arbitrarily. Too big a step creates indecision, too small a step creates unnecessary costs; Convergence testing is essential.
  • Not making analytical comparisons. Without comparing it to a known special case (small amplitude, frictionless state), the simulation remains unsupervised.
  • Mistaking a numerical flaw for physics. Behavior such as orbital expansion is often a flaw of the method, not real physics.
  • Fixing the number of samples in random simulation. It is misleading to increase the number of samples in Monte Carlo and rely on a single result without seeing convergence.
Caution: Just because a simulation produces a "nice graph" does not mean it is correct. The visual is convincing but deceptive. Before trusting the simulation, be sure to follow a conservation law and compare it with a known situation. An unverified simulation is not physical evidence.

In summary

Simulation is a powerful way to understand dynamic physical systems that lack analytical solutions and quickly builds AI simulation code. But the value of the simulation depends on whether it accurately reflects physics. The way to ensure this is to follow conservation laws, select the appropriate step size by convergence testing, compare the result with known analytical cases, and test physical limit cases. In the next unit we will shift the focus from generated data to analysis of actual experimental data.

Application task

Choose a simple dynamic system (pendulum, free fall + air resistance or cooling). With template 1, print and run a simulation code that includes a conservation or balance check in the AI. Check whether the monitored quantity (energy or temperature) behaves as expected. Then change a parameter (friction, mass, step size) and see if the change in behavior agrees with your physical intuition. Write it down in 5-6 sentences.

checklist

  • [ ] I clarified the equation, initial conditions and units of the system.
  • [ ] I followed a conservation law (energy/momentum) in the simulation.
  • [ ] I verified the step size with a convergence test.
  • [ ] I compared the result with a known analytical situation.
  • [ ] I have performed at least one physical limit state test.
  • [ ] I distinguished numerical defects from real physics.