# TRANSFER

Adds to or takes from a continuous tank, stock or inventory level (fuel, material, a pool) as the entity passes, optionally waiting until it is available.

Family: move. Python builder: `m.transfer(...)`.

## Typical uses

Use TRANSFER where a single entity moves a lump of a continuous level. A truck unloads 20 tonnes into a tank, an order takes 5 units from stock, or a delivery refills a store. It is the bridge between entities and levels. Rates that run continuously belong in flows, not here.

## Behaviour and rules

The `amount` is an expression, and a negative result counts as 0. In `remove` mode, the block takes the amount from the level down to its minimum. In `add` mode, it puts the amount in up to the level's capacity, or without a limit if it has none.

With `wait` off, the block moves what the level can pay for right now and goes on. The shortfall is recorded on the level as `unmet` for a removal and as `spilled` for an addition. With `wait` on, the entity queues until the level can pay the full amount, then takes it all at once. Waiting demands are served first in first out.

An amount the level never reaches waits until the end of the run, as in HOLD's `Infinite` mode. A level that is a conveyor cannot be written by TRANSFER. A level name that is not declared is caught by a check. The `level_summary` frame reports the totals.

## Ports

- Inputs: any number of incoming links.
- Outputs: one outgoing link.

## Parameters

| Parameter | Type | Default | Required | Quantity | Unit key | Description |
|---|---|---|---|---|---|---|
| `level` | str | `''` | yes |  |  | Name of the level this block adds to or removes from. |
| `amount` | str | `'1'` |  |  |  | How much to move: an expression, e.g. 20 or entity.quantity. |
| `mode` | str | `'remove'` |  |  |  | "remove" takes the amount out of the level, "add" puts it in. |
| `wait` | bool | `False` |  |  |  | Wait until the level can pay the full amount (the backorder pattern); otherwise move what it can and record the rest as unmet. |

## Statistics

Every run fills `system` and `entity_summary` (see the [KPI reference](../kpis.md)). The example below also fills:

| Frame | Columns |
|---|---|
| `level_summary` | `Run`, `Level`, `Unit`, `Average`, `Min`, `Max`, `Final`, `Time Empty %`, `Time Full %`, `Total In`, `Total Out`, `Spilled`, `Unmet` |
| `material_flow_summary` | `Run`, `From`, `To`, `Entities` |

## Problem codes

A check of the model reports these about this block:

| Code | Meaning |
|---|---|
| `E_CONVEYOR_WRITE` | Something would write a conveyor stock directly (a TRANSFER block or agent transfer action, a BUFFER's mirror level, initial_state.levels, or a subscripted conveyor), bypassing its slat model. |
| `E_LEVEL_UNKNOWN` | A LEVEL("name") call, a TRANSFER block or an agent's transfer action names a level the model does not declare. |
| `E_TRANSFER_MODE` | A TRANSFER block's or an agent transfer action's mode is neither "remove" nor "add". |

## Example

The snippet seeds itself; its output is from a run made when this page was generated.

```python
from nagasim import build as nb

m = nb.Model(time_limit=100)
m.add_level(name="Tank", capacity=100, initial=60)
m.create(distribution=nb.Constant(value=10)) >> m.transfer(level="Tank", amount="5", mode="remove", wait=True) >> m.dispose()

results = m.run(seed=1)
print(round(results.kpi("level/Tank/Final"), 1), "units left in the tank after the run")
```

```text
5.0 units left in the tank after the run
```

## Used in

Bundled models: [Biogas plant (library assets)](../../gallery/energy.md), [Order-driven warehouse (library assets)](../../gallery/logistics.md), [(s, S) inventory with backorders](../../gallery/supply-chain.md), [Two-echelon supply chain (library assets)](../../gallery/supply-chain.md).

Guides: [Continuous quantities: levels, flows and time-series inputs](../../modelling-guide/continuous-quantities.md), [Digital twin](../../digital-twin.md), [Agent-based modelling](../../agent-based-modelling.md), [Coming from Vensim](../../coming-from/vensim.md), [System dynamics](../../system-dynamics.md).

## See also

- [All blocks](index.md), [KPI reference](../kpis.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
- Same family (move): [BUFFER](buffer.md), [CONVEYOR](conveyor.md), [CONVEYOR_TRANSFER](conveyor-transfer.md), [STATION](station.md), [TRANSPORT](transport.md)
