Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 40 ยท Projects

Project: Numerical Simulation

Build two numerical simulations in Julia, a heat equation with broadcasting and an n-body orbit with structs, then check them against a runnable NumPy port.

Numerical simulation is the job Julia was designed for: you write the math almost as it appears in the textbook, and the compiler turns it into fast machine code. In this project you will build two classic simulations. The first, heat flowing along a rod, uses broadcasting on arrays. The second, two bodies orbiting each other, uses structs, functions and a time-stepping method that conserves energy well.

Julia is not installed on this site, so the Julia programs are read-only, but they are complete: you can paste them into a local REPL. Next to each one is a NumPy port that runs here, so you can see the numbers a correct program prints and check your own results against them. The ideas used come from Broadcasting, Structs and Performance Tips.

Project plan

What you will build

Heat equation Temperature along a rod, step by step.
Orbit simulation Two bodies pulling on each other.
Checks Conservation laws tell you if it is right.
1 / 2

Part 1: the heat equation

Temperature u(x, t) along a thin rod obeys โˆ‚u/โˆ‚t = ฮฑ โˆ‚ยฒu/โˆ‚xยฒ. We slice the rod into n points with spacing dx and march forward in time steps of dt. The simplest scheme (explicit finite differences) updates each interior point from itself and its two neighbors:

Text
u_new[i] = u[i] + ฮป * (u[i-1] - 2*u[i] + u[i+1]),   ฮป = ฮฑ * dt / dxยฒ

The scheme is only stable when ฮป โ‰ค 0.5; we choose ฮป = 0.4. The two end points stay at zero degrees (a cold clamp), and we start with a hot spot of 1.0 in the middle.

Here is the whole program in Julia. A small struct holds the settings; the step function is written with broadcasting on views:

JULIA
struct Rod
    n::Int          # number of points
    ฮฑ::Float64      # diffusivity
    dx::Float64
    dt::Float64
end

function Rod(n; ฮฑ = 1.0, ฮป = 0.4)
    dx = 1.0 / (n - 1)
    dt = ฮป * dx^2 / ฮฑ
    return Rod(n, ฮฑ, dx, dt)
end

# write the next temperatures into `unew`, reading from `u`
function step!(unew, u, rod::Rod)
    ฮป = rod.ฮฑ * rod.dt / rod.dx^2
    @views unew[2:end-1] .= u[2:end-1] .+
        ฮป .* (u[1:end-2] .- 2 .* u[2:end-1] .+ u[3:end])
    unew[1] = 0.0
    unew[end] = 0.0
    return unew
end

function simulate(rod::Rod, steps)
    u = zeros(rod.n)
    u[rod.n รท 2 + 1] = 1.0           # hot spot in the middle (1-based!)
    unew = similar(u)
    for _ in 1:steps
        step!(unew, u, rod)
        u, unew = unew, u            # swap the two buffers, no allocation
    end
    return u
end

rod = Rod(21)
u = simulate(rod, 200)
println("peak = ", round(maximum(u), digits = 4))
println("total heat = ", round(sum(u), digits = 4))

Look at what the dot syntax gives you. The right-hand side of step! has six operations on slices, but all the dots fuse into a single loop with no temporary arrays, and @views makes the slices zero-copy. The pair of buffers (u and unew) is swapped each step, so the time loop allocates nothing after setup. Compare with Performance Tips: preallocation, @views and an in-place ! function are all here.

Notice the index rod.n รท 2 + 1. Python's n // 2 is the 0-based middle index; in Julia, whose arrays start at 1, we add one to point at the same element.

Now the NumPy port. The slice u[1:-1] is the interior, exactly like u[2:end-1] in Julia (Python counts from 0 and its slice stops before the end):

Python Heat equation in NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You should see the peak fall from 0.4 to about 0.014 while the heat spreads out and the total decreases. The total shrinks because the clamped ends are held at zero, so heat flows out through them. If you changed ฮป to 0.6, the numbers would explode (about 10^27 after 200 steps), which is how you recognize a violated stability limit.

Part 2: two bodies in orbit

For the second simulation we model bodies in the plane that attract each other with gravity. The acceleration of body i is the sum over the other bodies of G * m_j * d / |d|ยณ, where d is the vector from i to j.

We store the state in a mutable struct. Positions and velocities are 2 ร— n matrices (one column per body), which keeps each body's coordinates next to each other in memory, because Julia arrays are column-major:

JULIA
mutable struct System
    pos::Matrix{Float64}      # 2 ร— n
    vel::Matrix{Float64}
    mass::Vector{Float64}
    G::Float64
end

function accelerations(sys::System)
    n = length(sys.mass)
    acc = zeros(2, n)
    for i in 1:n, j in 1:n
        i == j && continue
        d = @view(sys.pos[:, j]) .- @view(sys.pos[:, i])
        r = sqrt(d[1]^2 + d[2]^2)
        @views acc[:, i] .+= sys.G * sys.mass[j] .* d ./ r^3
    end
    return acc
end

function energy(sys::System)
    n = length(sys.mass)
    kinetic = 0.5 * sum(sys.mass[i] * (sys.vel[1, i]^2 + sys.vel[2, i]^2) for i in 1:n)
    potential = 0.0
    for i in 1:n, j in i+1:n
        d = sys.pos[:, j] .- sys.pos[:, i]
        potential -= sys.G * sys.mass[i] * sys.mass[j] / sqrt(d[1]^2 + d[2]^2)
    end
    return kinetic + potential
end

# kick-drift-kick "leapfrog": symplectic, so energy stays nearly constant
function advance!(sys::System, dt)
    acc = accelerations(sys)
    sys.vel .+= 0.5dt .* acc
    sys.pos .+= dt .* sys.vel
    acc = accelerations(sys)
    sys.vel .+= 0.5dt .* acc
    return sys
end

function orbit(steps; dt = 0.001)
    v = sqrt(0.5)                 # circular speed for two unit masses 1 apart
    sys = System([-0.5 0.5; 0.0 0.0],
                 [0.0 0.0; -v v],
                 [1.0, 1.0], 1.0)
    e0 = energy(sys)
    for _ in 1:steps
        advance!(sys, dt)
    end
    drift = abs(energy(sys) - e0) / abs(e0)
    return sys, drift
end

sys, drift = orbit(4443)          # about one full period
println(round.(sys.pos, digits = 4))
println("relative energy drift = ", drift)

Three details are worth studying. The matrix literal [-0.5 0.5; 0.0 0.0] has rows x and y and one column per body, so pos[:, 1] is body one. The method advance! ends in ! because it mutates sys. And energy gives you a built-in test: for an isolated system the total energy must stay constant, so a growing drift means a bug or a too-large dt.

The two unit masses start one unit apart and move perpendicular to their separation with speed โˆš0.5, which is the circular orbit speed. One period is 2ฯ€ ยท 0.5 / โˆš0.5 โ‰ˆ 4.443 time units, which is 4443 steps of dt = 0.001. After that many steps the bodies should be back very close to where they began.

Python Two-body orbit in NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The final x values are the starting ones to four decimals, and the y values are almost zero, which shows the orbit closed after one period. Circular orbits are easy for leapfrog, so for a harder test lower the starting speed (try 0.4 instead of โˆš0.5) to get an elliptical orbit, then change dt and watch how the energy drift responds.

Extending the project

  • Add a third body by adding a column to pos and vel and an entry to mass. Nothing else in the code changes, which is a payoff of writing the functions for any n.
  • Make a Body struct and store a Vector{Body}. It reads more naturally, but a matrix keeps the memory contiguous, so measure both with @btime (Debugging and Profiling).
  • Replace the hand-written loop with the DifferentialEquations.jl solvers from Differential Equations and Optimization and compare their energy drift with leapfrog.
  • Use a 2D grid for the heat equation with broadcasting over a matrix and a five-point stencil.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the heat step use @views and dot syntax together?

    @views makes slices zero-copy and the dots fuse the operations into one loop, so the step allocates nothing.
  2. 2. What happens to the explicit heat scheme if lambda is larger than 0.5?

    The explicit finite-difference scheme has a stability limit. Beyond it, errors are amplified at every step.
  3. 3. Why is the total energy a good test of the orbit simulation?

    A conserved quantity gives you a check that does not require the exact answer.
  4. 4. Why does unew and u get swapped with u, unew = unew, u?

    Swapping the references costs nothing, while creating a new array on every step would allocate in the hot loop.

Summary

  • A finite-difference step is one fused broadcast over views; preallocate two buffers and swap them.
  • Check the stability limit (ฮป โ‰ค 0.5) and the physical invariants (total energy) to validate a simulation.
  • Store the state in a struct, mutate it with a ! function, and write the functions for any number of bodies.
  • Leapfrog (kick-drift-kick) keeps orbital energy nearly constant; a bigger dt usually makes the drift grow, most visibly on elliptical orbits.
  • Julia counts from 1 and NumPy from 0, so translate indices carefully when you port code.

Next: process real tables in Project: A Data Analysis Pipeline.