Simulation: evaluating a model to understand how a system might evolve under a particular set of conditions.
Think of simulation as data generation (or generative modeling).
The model represents a particular data-generating process.
Why Simulate Systems?
System involves complex, nonlinear dynamics that may not be analytically tractable.
Setting up and running a real-world experiment is not possible.
State depends on prior states or states of nearby locations, so need to iterate over multiple spatial or temporal steps.
Need to understand range of system performance across rarely-seen conditions.
Types of Simulation Models
Deterministic vs. Stochastic
Discrete vs. Continuous
Simulation Model Workflow
Simulation Workflow
Simulation Model Applications
Water balance/hydrologic flow models;
Climate models (ocean heat/CO\(_2\) uptake through “box” layers)
Airsheds
Epidemiology
Social science (agent-based models)
Example: Airsheds
What Is A Box Model?
Box models are a common building block of simulation models.
Box models are all about mass-balance (mass \(m\)), assume well-mixed within box.
Can be steady-state \((\dot{m} = 0)\) or not.
Airshed Model
Let’s look at a simple steady-state model of an airshed.
Variable
Meaning
Units
\(m\)
mass of some air pollutant
g
\(C\)
concentration in box
g/m\(^3\)
\(S, D\)
source, deposition rate within the box
g/s
\(u\)
wind speed
m/s
\(L, W, H\)
box dimensions
m
Selecting Box Dimensions
What is relevant for the box dimensions \(L\), \(W\), and \(H\)?
Primarily the assumption(s) about mixing:
Mixing height: is there an atmospheric inversion which limits mixing height?
Homogeneity of input/output flows and emissions.
Steady-State Airshed Box Model
Steady-state box ⇒ \(\dot{m} = 0\).
\[\begin{align}
0 &= m_\text{in} - m_\text{out} + S - D \\[0.5em]
&\class{\fragment}{{} = (u WH) C_\text{in} - (u WH) C + S - D } \\[0.5em]
\end{align}\]
Solving for \(C\): \[C = C_{in} + \frac{S-D}{uWH}\]
Decay Processes
Now let’s assume some process affecting \(m\) depends on time.
For example: let’s say we care about an air pollutant which has a first-order decay rate \(k\), so \(D(t) = D_0 - km(t)\).
\[
\Rightarrow \frac{dm}{dt} = m_\text{in} - m_\text{out} + S - D_0 - km
\]
Starting at \(t=1\), iterate equation until end-time \(T\).
Simple Simulation Code
# this function computes the increment by which the concentration# is updated at each stepfunctionbox_simulate_timestep(C, Ci, u, W, H, L, S, D, k)return ((u / L) * (Ci - C) + (S - D) / (W * H * L) - (k * C))end# this function loops over the timesteps to simulate# the concentration seriesfunctionairshed_simulate(C₀, Ci, u, W, H, L, S, D, k, T, Δt)# initialize C storage# for code simplicity we make the array length T+1 so# index 1 is C₀ steps =Int64(T / Δt) C =zeros(steps +1) C[1] = C₀for t =1:steps C[t+1] = C[t] + Δt *box_simulate_timestep(C[t], Ci, u, W, H, L, S, D, k)end# the first element of C is the initial conditionreturn CendΔt =1.0T =10C₀ =0.1k =0.3u =2L =4W =4H =4Ci =0.2S =10D =13C =airshed_simulate(C₀, Ci, u, W, H, L, S, D, k, T, Δt)
Simulation Results
Code
p =plot(; xlabel="Time", ylabel=L"Pollutant concentration (g/m$^3$)")# find exact solution and plot for comparison# need these substitutions for the exact solution; not critical otherwiseV =4^3P = (u / L) * Ci + (S - D) / Vl = (u / L) + k# use steps of 0.01 to smooth the plottingC₁ = C₀ .*exp.(-l * (0:0.01:T))C₂ = (P / l) * (1.-exp.(-l * (0:0.01:T)))C_exact = C₁ .+ C₂plot!(p, 0:0.01:T, C_exact, linewidth=3, color=:black, label="Exact Solution")# add simulated solutionplot!(p, 0:Δt:T, C, linewidth=3, color=:blue, label="Simulated Solution (Δt = 1)")
functionairshed_simulate_dynamic(C₀, Ci, u, W, H, L, S, D, k, T, Δt)# initialize C storage# for code simplicity we make the array length T+1 so# index 1 is C₀ steps =Int64(T / Δt) C =zeros(steps +1) C[1] = C₀for t =1:steps C[t+1] = C[t] + Δt *box_simulate_timestep(C[t], Ci[t], u[t], W, H, L, S, D, k)end# the first element of C is the initial conditionreturn Cend
Figure 4: Dynamic simulation results for airshed model.
Multi-Box Simulation
More Complex Domains
We can use the single box simulation as a building block for more complex domains, possibly with different dynamics.
Two box airshed model
Multi-Box Simulation Approach
Tip: Use smaller functions as a building block!
Multi-Box Simulation Approach
# here we use our timestep function on each box individually,# but update them "together"functionairshed_twobox_simulate(C1₀, C2₀, Ci, u, W1, W2, H1, H2, L1, L2, S1, D1, S2, D2, k, T, Δt)# initialize C storage# for code simplicity we make the array length T+1 so# index 1 is C₀ steps =Int64(T / Δt) C1 =zeros(steps +1) C2 =zeros(steps +1) C1[1] = C1₀ C2[1] = C2₀# for each time step, first we update box 1, then box 2for t =1:steps C1[t+1] = C1[t] + Δt *box_simulate_timestep(C1[t], Ci, u, W1, H1, L1, S1, D1, k) C2[t+1] = C2[t] + Δt *box_simulate_timestep(C2[t], C1[t+1], u, W2, H2, L2, S2, D2, k)endreturn (C1, C2)end