liveBook · Manning

Simulation

As explained in Julia as a Second Language

A simulation is a computational representation that models the behavior of a system by advancing its state according to specified rules.

A simulation advances a model through small time steps rather than trying to solve a complex system with one equation. For a rocket, each step changes the propellant remaining in its tank and recalculates how the rocket moves. section 8.7 section 8.8

Listing 8.16 Updating the propellant mass after Δt time has elapsed
function update!(r::StagedRocket, t::Number, Δt::Number)
    mflow = mass_flow(thrust(r), Isp(r.engine))
    consume!(r.tank, mflow * Δt)
end
 
# Payload has no tanks with propellant to consume
update!(r::Payload, t::Number, Δt::Number) = nothing

The rocket’s update! method computes propellant use from thrust and specific impulse, then consumes the amount used during Δt. A payload needs no such update because it has no propellant tank. section 8.7

Listing 8.19 Simulating a rocket launch and determining the maximum altitude of the rocket
function launch!(rocket::Rocket, Δt::Real)
    g = 9.80665  # acceleration caused by gravity
    v = 0.0      # velocity
    h = 0.0      # altitude (height)
 
    for t in 0:Δt:1000                      #1
        m = mass(rocket)                    #2
        F = thrust(rocket) - m*g
 
        remaining = update!(rocket, t, Δt)
 
        # Any propellant and thrust left?
        if remaining == 0 || F <= 0
            return (t, h)                   #3
        end
 
        h += v*Δt                           #4
        a = F/m
        v += a*Δt
    end
end

The launch loop calculates mass and net force, updates propellant, and advances altitude and velocity using Δt. It returns the elapsed time and altitude when propellant is exhausted or thrust cannot overcome gravity. section 8.8

Try a launch

With a 300-unit payload, a small tank, and a Rutherford engine, launch!(rocket, 0.5) returns approximately (45.0, 31117.8): 45 seconds and 31 kilometers of altitude. The tank is empty afterward. Try changing the payload or tank size; a larger tank need not produce a higher result, because its mass also increases gravity’s pull. section 8.8

The model has boundaries: it omits air resistance and cannot simulate stage transitions, even though rockets with multiple stages can be constructed. Its result therefore describes this simplified launch, not a full real-world flight. section 8.7 section 8.8

How Julia as a Second Language covers it

How deep it goes
Works through propellant and motion updates in a launch loop, but stops short of air resistance and stage transitions.
Examples
Runs a small-tank rocket at half-second steps, reaching about 31 kilometers after 45 seconds, then suggests varying payload and tank size.
What it assumes
Assumes familiarity with Julia functions, types, and arithmetic while showing how their rocket components combine in a time-stepped loop.
What you can do after
Build it from scratch by combining a propellant update with a loop that advances rocket velocity and altitude.
How it connects
Connects simulation to time steps, state updates, thrust, gravity, mass, and propellant consumption.

Also explained in

Compare how 6 books explain simulation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.