# levels

Continuous quantities with bounds (battery energy, tank volume, an inventory position), read by LEVEL("name").

In the model file this key holds a list of `level` entries, each named by its `name`.

A scenario, an experiment or an optimizer can set a field by the path `levels.<name>.<field>`.

## Typical uses

These sections model quantities that change continuously: a tank, a battery, a stock, an energy bus. A `level` is the quantity with its bounds. A `flow` moves it at a rate. A `conversion` turns a feed into a product that is released later, such as biogas from feedstock. Use them when counting entities is the wrong view.

## Behaviour and rules

A flow rate is an expression that is re-evaluated only at events, so it is constant between them. Every level is then a straight line between events, and the engine solves for the instant a level reaches a bound or a threshold. There is no fixed time step unless `simulation.sd` asks for one.

The engine enforces the bounds. A level at its `min` passes on no more than flows into it, and a full level scales its inflows down. What is lost is recorded as `unmet` for demand not served and as `spilled` for overflow. A rate needs no guard for an empty level. A negative rate is clamped to 0 unless the flow is a `biflow`.

A `price` is money per unit moved and an `emission_factor` is an impact per unit. Both are integrated exactly. A `peak_price` is a demand charge, billed once at the end of the run. A flow with a `tag` is summed by `FLOWSUM("tag")`, which is how many assets share one site bus.

A TRANSFER block moves a lump between an entity and a level. A conversion releases its product after a delay that follows a yield curve, in cohorts closed every `cohort_interval`.

## Fields

| Field | Type | Required | Default | Unit | Description |
|---|---|---|---|---|---|
| `diagram` | object |  |  |  | Position in the stock-and-flow view; ignored by the engine. |
| `name` | string | yes |  |  | The level's name, the argument of LEVEL("name"). |
| `dims` | list of string |  |  |  | Subscripts this record runs over: it is flattened into one record per element, named "Name[north]" ("Name[north,young]" for two). Inside, "x[dim]" in a LEVEL/FLOW/AUX/DELAY name reads the same element, and SUM(x)/MEAN(x) aggregate over "x[*]". |
| `elements` | object |  |  |  | Per-element equations: {"north": "2", "a,x": "3"} replaces the record's first expression (a level's initial, a flow's rate, an auxiliary's expr, a delay's input) for those elements; the shared one covers the rest. Keys join one element per dim with a comma. |
| `description` | string |  |  |  | What the level stands for; the run does not read it. |
| `unit` | string |  |  |  | Label only, e.g. kWh or tonnes. |
| `capacity` | number or null |  |  |  | Upper bound; omit for an unbounded accumulator. |
| `min` | number or null |  | `0.0` |  | Lower bound; null for a stock unbounded below . |
| `initial` | number or string |  | `0.0` |  | Value at t=0, inside [min, capacity]; or an expression, evaluated once in dependency order . |
| `max_inflow` | number or null |  |  | rate (`rate_time_unit`) | Largest total rate that may be ADDED to the level (a charge-power limit). |
| `max_outflow` | number or null |  |  | rate (`rate_time_unit`) | Largest total rate that may be REMOVED from it (a discharge-power limit). |
| `rate_time_unit` | choice |  | `"base"` |  | Unit the two power limits are per. One of `base`, `milliseconds`, `seconds`, `minutes`, `hours`, `days`, `weeks`, `months`, `years`. |
| `thresholds` | list of object |  |  |  | Level values that fire discrete logic when crossed. |
| `components` | list of object |  |  |  | Named components the level's content is tracked in: a gas holder's ch4. Each inflow carries a share, each outflow draws at the level's own blend, and the component is conserved exactly. Read by FRACTION(name, component). |
| `blend_tolerance` | number |  | `0.005` | ratio | How far the fraction may drift from the value the outflows are drawing at before the engine re-evaluates. The crossing instant is solved in closed form; the component is conserved exactly whatever this is set to. |
| `storage` | object |  |  |  | A storage the `dispatch` plan may drive: its power limits, efficiencies and the variable the plan writes its bus-side setpoint to (positive charges). Rates are per this level's rate_time_unit. Inert without a `dispatch` section. |
| `conveyor` | object |  |  |  | Makes the stock a Stella conveyor: material rides round(transit_time/dt) slats of the SD grid (euler only), leaves as the one primary outflow (rate "") and may leak on the way through "leak" flows. The stock is the sum of the slats and changes only at grid points. |
| `locals` | object |  |  |  | Submodel parameter bindings, written by flattening; not authored by hand. |

A field of a nested object is described in the JSON Schema (`naga schema`).

## Example

```json
{"levels": [{"name": "Tank", "capacity": 100, "initial": 40}]}
```

In the Python builder:

```python
from nagasim import build as nb
m = nb.Model()
m.add_level(name='Tank', capacity=100, initial=40)
```

## Used in

Bundled models: [Biogas digester, holder and CHP](../../gallery/energy.md), [Biogas plant (library assets)](../../gallery/energy.md), [Depot: PV, EV charging and heat pump](../../gallery/energy.md), [PV + battery sizing and dispatch](../../gallery/energy.md), [PV + battery site (library assets)](../../gallery/energy.md), [Supply line: order-rate management game](../../gallery/games.md) and 11 more.

Guides: [Continuous quantities: levels, flows and time-series inputs](../../modelling-guide/continuous-quantities.md), [Coming from AnyLogic](../../coming-from/anylogic.md), [Coming from Vensim](../../coming-from/vensim.md), [How do I turn a heading and a distance into x and y?](../../how-to/recipes/bearing.md), [How do I add a daily or weekly cycle to a rate?](../../how-to/recipes/daily-cycle.md), [How do I discount a stream of cash?](../../how-to/recipes/discounted-cash.md) and 10 more.

## See also

- [All sections](index.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
