Skip to content

How do I test a model with a step, a ramp and a pulse?

Before you trust a model, push a known input through it and see how it answers. STEP, RAMP and PULSE are the three standard inputs. The script feeds each into its own tank that drains 10% an hour.

from nagasim import build as nb

# Three standard test inputs into the same tank, which drains 10% of its content an hour:
# STEP(h, t0) jumps to h at t0, RAMP(s, t0) climbs by s an hour from t0, and PULSE(h, t0, w)
# is h for w hours from t0. Time is in hours.
m = nb.Model(time_limit=20, time_unit="hours", sd={"dt": 0.1, "method": "euler"})
for name, inflow in (("Step", "STEP(5, 2)"), ("Ramp", "RAMP(1, 2)"), ("Pulse", "PULSE(20, 2, 3)")):
    m.add_level(name=name, initial=0)
    m.add_flow(name=f"{name} in", to=name, rate=inflow)
    m.add_flow(name=f"{name} out", **{"from": name}, rate=f'0.1 * LEVEL("{name}")')

series = m.run(seed=1)["timeseries"].pivot(index="Time", columns="Series", values="Value")
view = series[["level/Step", "level/Ramp", "level/Pulse"]]
view.columns = ["Step", "Ramp", "Pulse"]
print(view.reindex([2, 4, 5, 8, 12, 20], method="nearest").round(1).rename_axis("Hour").to_string())

Save it as recipe-test-inputs.py and run it with python. The script seeds itself, so the numbers below are the same on your machine.

      Step  Ramp  Pulse
Hour                   
2      0.0   0.0    0.0
4      9.1   1.8   36.4
5     13.0   4.0   52.1
8     22.6  14.7   38.5
12    31.7  36.6   25.8
20    41.8  96.4   11.5

How it works

  1. STEP(h, t0) jumps to h at t0, RAMP(s, t0) climbs by s a unit of time from t0, and PULSE(h, t0, w) holds h for w.
  2. The step settles towards 50, the ramp keeps climbing, and the pulse rises, then drains back towards zero.
  3. If a response does not look like that, the structure is wrong, and not the data.

Elements used

Back to all recipes.