# How do I work out an angle, a length or a count in an expression?

A layout figure often needs a little geometry: the angle of a ramp, its run on the floor, or how many pallets fit along it. The math and trigonometry functions do that inside an expression, so the figure follows the inputs. The script works out a 20 m ramp that rises 1.5 m and prints a few other shapes the same functions give.

```python
from nagasim import build as nb

# A ramp 20 m long rises 1.5 m. The math functions turn that into an angle, a run and the number of
# 1.2 m pallet lengths it takes; the hyperbolic ones shape a soft start for a conveyor speed.
m = nb.Model(time_limit=1, time_unit="hours", sd={"dt": 1, "method": "euler"})
m.add_auxiliary(name="rise", expr="1.5")
m.add_auxiliary(name="length", expr="20")
m.add_auxiliary(name="angle_deg", expr='DEG(ASIN(AUX("rise") / AUX("length")))')
m.add_auxiliary(name="run_m", expr='sqrt(AUX("length") ** 2 - AUX("rise") ** 2)')
m.add_auxiliary(name="slope_check_deg", expr='DEG(ATAN(AUX("rise") / AUX("run_m")))')
m.add_auxiliary(name="grade_pct", expr='100 * TAN(RAD(AUX("angle_deg")))')
m.add_auxiliary(name="pallets", expr='ceil(AUX("length") / 1.2)')
m.add_auxiliary(name="soft_start", expr="TANH(1.5)")
m.add_auxiliary(name="catenary_sag", expr="COSH(0.5) - 1")
m.add_auxiliary(name="growth", expr="SINH(0.5)")
m.add_auxiliary(name="decades", expr="log10(5000)")
m.add_auxiliary(name="doublings", expr="log(5000, 2)")
m.add_level(name="Clock", initial=0)
m.add_flow(name="tick", to="Clock", rate="1")

series = m.run(seed=1)["timeseries"]
last = series[series["Time"] == series["Time"].max()].set_index("Series")["Value"]
for name in ("angle_deg", "run_m", "slope_check_deg", "grade_pct", "pallets", "soft_start",
             "catenary_sag", "growth", "decades", "doublings"):
    print(f"{name:<16} {last['aux/' + name]:8.3f}")

```

Save it as `recipe-ramp-geometry.py` and run it with `python`. Nothing here is random, so the numbers below are the same on your machine.

```text
angle_deg           4.301
run_m              19.944
slope_check_deg     4.301
grade_pct           7.521
pallets            17.000
soft_start          0.905
catenary_sag        0.128
growth              0.521
decades             3.699
doublings          12.288

```

## How it works

1. `ASIN`, `ATAN` and `TAN` work in radians; `DEG` and `RAD` convert, so the ramp's 4.3 degrees and 7.5 % grade agree.
2. `sqrt` gives the run on the floor, `ceil` rounds the pallet count up, and `log10` and `log(x, base)` count decades and doublings.
3. `TANH`, `SINH` and `COSH` give smooth S-curves and sags, for example a conveyor speed that eases in.

## Elements used

- Functions: [`ASIN`](../../reference/functions/trigonometry.md#asin), [`ATAN`](../../reference/functions/trigonometry.md#atan), [`TAN`](../../reference/functions/trigonometry.md#tan), [`TANH`](../../reference/functions/trigonometry.md#tanh), [`SINH`](../../reference/functions/trigonometry.md#sinh), [`COSH`](../../reference/functions/trigonometry.md#cosh), [`DEG`](../../reference/functions/trigonometry.md#deg), [`RAD`](../../reference/functions/trigonometry.md#rad), [`sqrt`](../../reference/functions/math.md#sqrt), [`ceil`](../../reference/functions/math.md#ceil), [`log`](../../reference/functions/math.md#log), [`log10`](../../reference/functions/math.md#log10).
- 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).
