# SUBMODEL

Editor-only blocks (SUBMODEL, SUB_ENTRY, SUB_EXIT) that group blocks into a reusable sub-model with named ports, expanded away before the run.

Editor-only placeholder shared by three types, never run directly -- `core/submodels.flatten()` expands them away before the engine builds any process. SUB_ENTRY: an input port of a submodel definition (`port` names it; exactly one outgoing link inside the definition, none incoming). SUB_EXIT: an output port of a definition (`port` names it; incoming links only). SUBMODEL: an instance of a definition dropped on the canvas (`definition` names it, `arguments` binds its parameters).

Family: container. Python builder: `m.submodel(...)`. Editor-only: the engine flattens it away before a run.

## Typical uses

Use SUBMODEL to place a reusable group of blocks on the canvas, such as a workstation or a ward, and to keep a large model readable. The group is defined once, with named entry and exit ports, and used many times with different arguments. It is a tool of the editor and of the model file, not of the run.

## Behaviour and rules

A SUBMODEL names a `definition` and binds its parameters in `arguments`, each a number, an expression or a flag. Before a run, the engine replaces every instance by a copy of the definition's blocks, named `<instance>/<inner>`, and rewires the links through the ports. A submodel therefore costs nothing at run time.

Results and names inside an instance carry that prefix. A sequence step must use the prefixed name of a station inside an instance. A block that runs unflattened stops with an error, which only happens when a layout bypasses the loader.

The `preset` records which catalogue row the arguments came from. It is for provenance only. Editing an argument afterwards is fine, and nothing re-reads the catalogue. The block can have several outgoing links, and each names the exit port it leaves from.

## Ports

- Inputs: any number of incoming links.
- Outputs: several, one for each branch; the block picks the link by its index at run time.

## Parameters

| Parameter | Type | Default | Required | Quantity | Unit key | Description |
|---|---|---|---|---|---|---|
| `definition` | str | `''` |  |  |  | Name of the submodel definition |
| `arguments` | dict | `{}` |  |  |  | Parameter bindings: {name: number | expression | bool} |
| `preset` | str | `''` |  |  |  | Provenance only: the catalogue row whose values were written into `arguments`. The values are what the model uses, so editing one afterwards is fine and nothing re-reads the catalogue. |

## Statistics

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

| Frame | Columns |
|---|---|
| [`resource_summary`](../kpis.md#resource_summary) | `Run`, `Resource`, `Total Seizes`, `Usage % (snapshot)`, `Idle %`, `Scheduled Capacity Avg`, `Down %`, `Busy Cost`, `Idle Cost`, `Usage Cost`, `Total Cost`, `Queue Length Avg`, `Queue Length Max`, `Queue Length at End`, `Queue Length Mode`, `Queue Length Mode %`, `Queue Len P95`, `Queue Len P99`, `Queue Len Std`, `Time in Queue Avg`, `Time in Queue Avg (incl. waiting)`, `Time in Queue Max`, `Time in Queue Mode`, `Time in Queue Mode %`, `Wait P95`, `Wait P99`, `Wait Std`, `Bottleneck Score`, `Bottleneck %` |
| `material_flow_summary` | `Run`, `From`, `To`, `Entities` |

## Problem codes

A check of the model reports these about this block:

| Code | Meaning |
|---|---|
| `E_SUBMODEL_ARGUMENTS` | A SUBMODEL instance's arguments are not an object. |
| `E_UNKNOWN_SUBMODEL` | A SUBMODEL instance names a definition that does not exist. |

## Example

The snippet seeds itself; its output is from a run made when this page was generated. `SUB_ENTRY` and `SUB_EXIT` are the ports of a definition, `SUBMODEL` an instance of it.

```python
from nagasim import build as nb

m = nb.Model(time_limit=200)
m.add_resource(name="Lathe")
cell = m.definition("Cell")                     # a reusable group: SUB_ENTRY -> blocks -> SUB_EXIT
cell.sub_entry("in") >> cell.process(resource_name="Lathe", distribution=nb.Constant(value=2)) >> cell.sub_exit("out")
m.create(distribution=nb.Constant(value=5)) >> m.submodel(definition="Cell") >> m.dispose()

results = m.run(seed=1)
print(int(results.kpi("resource/Lathe/Total Seizes")), "parts went through the Cell submodel")
```

```text
41 parts went through the Cell submodel
```

## Used in

Bundled models: [Boids flock (continuous, heading)](../../gallery/agent-based.md), [Nagel-Schreckenberg traffic (ring)](../../gallery/agent-based.md), [Schelling segregation (grid)](../../gallery/agent-based.md), [SIR on a small-world network](../../gallery/agent-based.md), [Sugarscape with regrowth (grid, patches)](../../gallery/agent-based.md), [Wolf-Sheep predation with grass (grid)](../../gallery/agent-based.md) and 27 more.

Guides: [Coming from Arena](../../coming-from/arena.md), [Model file format](../../model-file-format.md), [Submodels](../../modelling-guide/submodels.md).

## See also

- [All blocks](index.md), [KPI reference](../kpis.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
