# variables

Model-level variables, read and written by expressions; reset to `initial` each replication.

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

A scenario, an experiment or an optimizer can set a field by the path `variables.<name>.<field>`. A data binding can write a field.

## Typical uses

These sections hold the values that expressions read. `variables` are numbers that a model changes while it runs. `tables` are fixed lookups by one or two keys. `timeseries_inputs` are profiles over time, such as a tariff or a demand curve. `entities` describe the entity types: how they look and what holding them costs.

## Behaviour and rules

A variable is read as a bare name in an expression and written by an ASSIGN block. Its `initial` value is restored at the start of every replication. A table is read with `TABLE("name", key)` or with two keys for a 2-D table. The `key_mode` decides what happens between keys: `exact`, `step` or `linear`. A missing exact key is a run-time error unless the table has a `default`.

A time series is read with `TS("name")` for now, or with a second argument for another time. Its `interpolation` is `step` by default, which is exact for a rate. Past the last sample, `beyond` decides whether the last value holds, the series repeats, or the run stops with an error. Sample times must increase.

An entity type is named by the `e_type` of a CREATE block. A `holding_cost` charges each entity for the time it spends in the system, from its creation to its disposal, and only time after the warm-up counts.

## Fields

| Field | Type | Required | Default | Unit | Description |
|---|---|---|---|---|---|
| `name` | string | yes |  |  | The variable's name; expressions read it as a bare name. |
| `initial` | number or string or boolean |  | `0` |  | Its value at the start of each replication: a number, a boolean or an expression. |
| `trace` | boolean |  |  |  | Written by flattening for a variable an asset's picture follows; not authored by hand. |

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

## Example

```json
{"variables": [{"name": "batch_size", "initial": 5}]}
```

In the Python builder:

```python
from nagasim import build as nb
m = nb.Model()
m.add_variable(name='batch_size', initial=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), [PV + battery sizing and dispatch](../../gallery/energy.md), [PV + battery site (library assets)](../../gallery/energy.md), [Emergency department: staffing game](../../gallery/games.md) and 8 more.

Guides: [Digital twin](../../digital-twin.md), [Model file format](../../model-file-format.md), [Coming from Arena](../../coming-from/arena.md), [How do I stop a ratio dividing by zero?](../../how-to/recipes/safe-ratio.md), [Expressions and variables](../../modelling-guide/expressions-variables.md), [Semantic diff](../../semantic-diff.md).

## See also

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