# flows

Rate-based transfers between levels, re-evaluated at events.

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

A scenario, an experiment or an optimizer can set a field by the path `flows.<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 flow's 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 flow stands for; the run does not read it. |
| `from` | string or null |  |  |  | Level drawn from; omit for a source outside the model. A level at its min passes on no more than flows into it, so a rate needs no guard such as IF(LEVEL("x") &gt; 0, r, 0); on a step grid that guard reads the level a step late. |
| `to` | string or null |  |  |  | Level fed; omit for a sink outside the model. |
| `rate` | string or number | yes | `"0"` | rate (`rate_time_unit`) | Quantity per time unit: an expression over variables, TS(), TABLE(), LEVEL() and FLOW(). A negative value is clamped to 0 -- declare the reverse flow instead, or set "biflow". |
| `biflow` | boolean |  | `false` |  | Bi-directional: a negative rate runs the flow backwards instead of being clamped to 0. Both ends must be outside the model or levels with "min": null and no capacity or power limit. |
| `rate_time_unit` | choice |  | `"base"` |  | Unit the rate is per. FLOW(), FLOWSUM(), Avg Rate, Peak Rate, rate thresholds and peak_price read the flow in it too. One of `base`, `milliseconds`, `seconds`, `minutes`, `hours`, `days`, `weeks`, `months`, `years`. |
| `efficiency` | number |  | `1.0` | ratio | out = in * efficiency; the rest is a loss. |
| `price` | string or number |  | `"0"` | money | Money per unit moved; integrated exactly into the flow's Cost and the model's Total Cost. |
| `emission_factor` | string or number |  | `"0"` |  | Impact per unit moved (emissions, water, waste), integrated into Total Impact. |
| `tag` | string |  |  |  | Bus tag: every flow carrying it is summed by FLOWSUM("tag"), so a site's generation or demand is one number however many assets contribute to it. |
| `priority` | number |  | `0` |  | Merit order within a tag: FLOWSUM("tag", p) sums only the flows with a priority below p -- what the devices dispatched before this one actually took. |
| `peak_price` | number |  | `0` | money | Demand charge: money per unit of the flow's Peak Rate (per its rate_time_unit), billed ONCE at the end of the run. A time-varying tariff is "price". |
| `grid` | object |  |  |  | This flow's role in the `dispatch` plan: the site's grid import or export. The plan prices it with this flow's own `price`. Inert without a `dispatch` section. |
| `min_rate` | number |  |  | rate (`rate_time_unit`) | Minimum load: the flow runs at 0 or at least this rate, never in between. The gate is applied to the rate the expression ASKED for, before saturation, so it adds no event of its own and cannot chatter. |
| `components` | object |  |  |  | Share of what this flow moves that is each component, a number or an expression. Omit a component to carry the SOURCE level's own blend. |
| `leak` | object |  |  |  | A conveyor's leakage outflow; set "rate": "" and "from" the conveyor. |
| `thresholds` | list of object |  |  | rate (`rate_time_unit`) | Realised-rate values that fire discrete logic when crossed : a signal, a variable, and a lump cost per crossing -- a generator's start cost. Rising crossings are counted as the flow's Starts. |
| `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

The fragment refers to entries that are defined elsewhere in the same file:

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

The fragment:

```json
{"flows": [{"name": "Fill", "to": "Tank", "rate": "5"}]}
```

In the Python builder:

```python
from nagasim import build as nb
m = nb.Model()
m.add_flow(name='Fill', to='Tank', rate='5')
```

## Used in

Bundled models: [Berth: shore power](../../gallery/energy.md), [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) 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 SimPy](../../coming-from/simpy.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) and 11 more.

## See also

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