← All examples
29

Canonical Grid model

Model-Time Warehouse Simulation

Simulated-time coverage: STOCK integration, PREV smoothing, DELAY lead times, clock reads, a seeded demand fan, and a trajectory handle.

Scale
Medium
Source
29-model-time-simulation.grid
Length
59 lines
Collection
Forecasting & decisions
Level
Advanced
Runtime
Simulation
Version
1.0.0

Watch it in Grid

See this model in motion.

Watch the model respond in the product, then inspect the exact source and checkpoints on this page.

Companion film

Monte Carlo in a cell

Model revenue and cost as distributions, widen uncertainty, and watch the launch decision change with the odds.

21 secGrid 0.61.0
Open film page

What this model gives you

A 52-week warehouse model with seasonal demand, memory, delayed arrivals, stocks, and a replayable trajectory.

25 min to study · Source reviewed 2026-08-24

Continue with guided practice

What to notice

  • Model-time clocks
  • PREV and DELAY
  • STOCK integration
  • Seeded scenarios

Requirements

  • Simulation runner
  • Model history
  • No external source

Expected checkpoint

A known state for this walkthrough.

At the opening simulation coordinate before advancing model time.

Opening stock
480E1
On order
0E2
Demand
60F3
Status
coveredF1
01 · Open

The workbook retains a useful point estimate

Clock defaults and STOCK initial values keep the model readable outside a simulation run.

02 · Advance

Memory, delays, and flows follow model time

PREV smooths demand, DELAY carries shipment history, and STOCK integrates on-hand and on-order state.

03 · Replay

Seeded scenarios and trajectories can be compared

A finite trajectory turns the stateful model into an inspectable experiment without copying it into another tool.

29-model-time-simulation.grid
Get Grid
MODEL "Model-Time Warehouse Simulation"
DESCRIPTION "Simulated-time coverage: STOCK integration, PREV smoothing, DELAY lead times, clock reads, a seeded demand fan, and a trajectory handle."
VERSION "1.0.0"
AUTHOR "Grid Team"
TAGS "canonical", "portable", "simulation", "model-time", "inventory"

# Static parameters. A simulation run advances a hypothetical clock (here
# t0=0, dt=1, steps=52, timeUnit=week) as one replayable ledger event whose
# internal steps never write ordinary bindings, so these inputs stay intact.
A1 = 480
A2 = 60
A3 = 4
A4 = 0.3
A5 IS currency = 2.40

# Clock reads. Outside a simulation resolve SIM_TIME and SIM_STEP return
# #N/A, so DEFAULT keeps the point-estimate resolve clean.
B1 = SIM_TIME() DEFAULT 0
B2 = SIM_STEP() DEFAULT 0
B3 = B2 > 0 THEN "in-run" ELSE "opening"

# Weekly demand follows a 13-week seasonal swing driven by model time, and
# PREV supplies the one-step memory for exponential smoothing.
C1 = A2 * (1 + 0.25 * SIN(2 * PI() * B1 / 13))
C2 = PREV(C3, A2)
C3 = A4 * C1 + (1 - A4) * C2

# Replenishment: order up to lead-time-plus-safety cover. DELAY keeps the
# fixed shipping history (lag_steps must be a positive integer literal), so
# today's arrival is the order placed four simulated weeks back.
D1 = C3 * (A3 + 2)
D2 = MAX(D1 - E1 - E2, 0)
D3 = DELAY(D2, 4, 0)

# Integrated state. STOCK adds dt * net_flow every simulated step; outside a
# run each stock reports its initial value, which keeps this file portable.
E1 = STOCK(A1, D3 - C1)
E2 = STOCK(0, D2 - D3)
E3 = STOCK(0, C1)

# Service and economics readouts.
F1 = E1 < C3 * A3 THEN "reorder-risk" ELSE "covered"
F2 IS currency = E1 * A5
F3 = ROUND(C3, 1)

# Reproducible demand-shock fan. Fixed seeds make the Monte Carlo sweep
# replayable, and each experiment row records its own runId in model history.
G1 = [NORM.INV(RAND_SEEDED(seed), 1.0, 0.08) FOR seed IN 301..316]
G2 = ROUND(AVERAGE(G1), 3)
G3 = COUNTIF(G1, ">1.1") / 16
G4 = ROUND(D1 * MAX(G1), 0)

# Trajectory seam. In workbook semantics SIM_TRAJECTORY returns the resident
# value; under RENDER it pages a finite read-only frame (column t plus the
# tracked output) on an independent presentation clock.
H1 = SIM_TRAJECTORY(E1, {t0: 0, dt: 1, steps: 52, timeUnit: "week"})
H2 = `on_hand={E1} on_order={E2} smoothed_demand={F3} status={F1}`

END MODEL