# How do I delay goods in transit by a fixed time or a spread of times?

Goods that leave on day 5 either all arrive exactly 4 days later, or arrive over a spread of days around the mean. DELAYP is the first, and DELAYN and DELAY3 the second. The script sends a dispatch step through all three.

```python
from nagasim import build as nb

# Dispatch jumps from 0 to 10 a day at day 5, and the goods take 4 days on average to arrive.
# DELAYP is a pipeline: every unit arrives exactly 4 days later. DELAYN (here 2 stages) and DELAY3
# spread the arrivals around day 9, DELAY3 more tightly than the 2-stage one.
m = nb.Model(time_limit=30, time_unit="days", sd={"dt": 0.25, "method": "euler"})
m.add_auxiliary(name="Dispatch", expr="STEP(10, 5)")
for name, expr in (("Pipeline", 'DELAYP(AUX("Dispatch"), 4)'),
                   ("Two stages", 'DELAYN(AUX("Dispatch"), 4, 2)'),
                   ("Three stages", 'DELAY3(AUX("Dispatch"), 4)')):
    m.add_auxiliary(name=name, expr=expr)
m.add_level(name="Received", initial=0)
m.add_flow(name="arrivals", to="Received", rate='AUX("Pipeline")')

series = m.run(seed=1)["timeseries"].pivot(index="Time", columns="Series", values="Value")
view = series[["aux/Dispatch", "aux/Pipeline", "aux/Two stages", "aux/Three stages"]]
view.columns = ["Dispatch", "Pipeline", "Two stages", "Three stages"]
print(view.reindex([6, 8, 10, 14, 20], method="nearest").round(1).rename_axis("Day").to_string())

```

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

```text
     Dispatch  Pipeline  Two stages  Three stages
Day                                              
6        10.0       0.0         0.8           0.2
8        10.0       0.0         4.4           3.9
10       10.0      10.0         7.4           7.6
14       10.0      10.0         9.5           9.7
20       10.0      10.0        10.0          10.0

```

## How it works

1. `DELAYP(x, tau)` is a pipeline: the output is `x` exactly `tau` later. `DELAYN(x, tau, n)` and `DELAY3(x, tau)` spread the output over `n` and 3 stages.
2. All three have a 4-day total delay, so the spread versions start earlier and finish later than the exact pipeline.
3. Use the pipeline for a fixed lead time and the staged forms when the transit time itself varies.

## Elements used

- Functions: [`DELAYP`](../../reference/functions/system-dynamics.md#delayp), [`DELAYN`](../../reference/functions/system-dynamics.md#delayn), [`DELAY3`](../../reference/functions/system-dynamics.md#delay3), [`STEP`](../../reference/functions/system-dynamics.md#step).
- Model-file sections: [`auxiliaries`](../../reference/model-file/auxiliaries.md), [`levels`](../../reference/model-file/levels.md), [`flows`](../../reference/model-file/flows.md).
- Python: [`Model`](../../reference/api/Model.md).

Back to all [recipes](index.md).
