# CHARGE

Charges an entity (an electric vehicle, a battery pack) with the energy it needs at up to its own power, sharing a site power cap with every other entity at this block; it leaves when full or when its time is up.

The block's total grid kW (what the entities take over `efficiency`) is kept in `power_variable`, so a flow with `rate: VAR("name")` puts it on the electricity bus; `site_limit` caps that grid kW. On leaving the entity carries `charged_kwh`, `charge_full` (1 or 0) and `charge_time` (entry to full, or to leaving when not full, in `time_limit_unit`).

Family: work. Python builder: `m.charge(...)`.

## Typical uses

Use CHARGE for electric vehicles or battery packs that take energy at a limited power while they stay at a site. Several vehicles can share one site power cap. It fits a depot, a car park or a charging bay. For a vehicle that must go to a charger as part of a fleet trip, use the fleet's battery and charger sections instead.

## Behaviour and rules

Every entity at the block is a charging session with its own need in kWh, its own power limit in kW and an optional deadline. The block re-divides the site cap whenever a session starts, fills up or reaches its deadline. Power is constant in between, so the energy is exact.

With `sharing` set to `equal`, each session gets an equal share, and what a slow session cannot use goes to the others. With `first_come`, sessions are served in order of arrival, each taking as much as it can. A `site_limit` of 0 means no cap. The `efficiency` is the share of grid energy that reaches the battery. The cap and `power_variable` are measured on the grid side.

The entity leaves when it is full, or at its time limit if one is set, whichever comes first. With `leave_when_full` off, a full entity stays until the time limit and draws nothing. On leaving, the entity carries `charged_kwh`, `charge_full` (1 or 0) and `charge_time`.

An entity with no need leaves at once as full. An entity with no power and no time limit leaves at once with nothing charged, and a warning is logged. A deadline already past sends the entity on, charged 0. There is no taper, and the cap is constant.

## Ports

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

## Parameters

| Parameter | Type | Default | Required | Quantity | Unit key | Description |
|---|---|---|---|---|---|---|
| `energy` | str | `'0'` | yes | energy |  | Energy the entity needs, kWh: an expression, e.g. entity.need_kwh. |
| `max_power` | str | `'0'` | yes | power |  | Most power the entity takes, kW: an expression, e.g. min(11, entity.max_kw). |
| `time_limit` | str | `''` |  | time | `time_limit_unit` | Longest time the entity stays (an expression); it leaves then, full or not. Empty = until full. |
| `time_limit_unit` | str | `'base'` |  |  |  | Unit of `time_limit` and of the `charge_time` the entity carries: "base" (the model's) or one of its quantity's units. |
| `time_limit_from` | str | `''` |  |  |  | Instant, in model time, the time limit counts from: an expression such as entity.arrived_at. Empty = when the entity enters. A deadline already past sends it on at once, charged 0. |
| `leave_when_full` | bool | `True` |  |  |  | Leave as soon as the need is met. Off: stay (drawing nothing) until the time limit, as a car stays plugged in. |
| `site_limit` | float | `0.0` |  | power |  | Most power all entities at this block draw together from the grid, kW; 0 = no cap. |
| `efficiency` | float | `1.0` |  | ratio |  | Energy the entity stores per unit drawn from the grid (charger losses); 1 = none. The site limit and power_variable are grid side. |
| `sharing` | str | `'equal'` |  |  |  | How the cap is shared: "equal" (an equal share each; what a slow entity cannot use goes to the others) or "first_come" (in order of arrival, each as much as it can take). |
| `power_variable` | str | `''` |  |  |  | Model variable kept equal to the block's total grid kW, for a flow rate VAR("name") on the electricity bus. Empty = none. |

## Statistics

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

| Frame | Columns |
|---|---|
| [`user_summary`](../kpis.md#user_summary) | `Run`, `Statistic`, `Kind`, `Count`, `Value`, `Min`, `Max`, `Std` |
| `material_flow_summary` | `Run`, `From`, `To`, `Entities` |

## Problem codes

A check of the model reports these about this block:

| Code | Meaning |
|---|---|
| `E_CHARGE` | A CHARGE block's sharing rule is neither "equal" nor "first_come", its efficiency is not in (0, 1], or it keeps a full entity (leave_when_full off) with no time limit to leave by. |

## 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=600, time_unit="minutes")
cars = m.create(distribution=nb.Constant(value=15), max_arrivals=12)
bay = m.charge(energy="20", max_power="50", site_limit=60)      # 20 kWh a car, 50 kW a car, 60 kW for the site, so two cars share it
cars >> bay >> m.record(stat_name="charge_minutes", kind="tally", expr="entity.charge_time") >> m.dispose()

results = m.run(seed=1)
print(results["user_summary"].round(2).to_string(index=False))
```

```text
 Run      Statistic  Kind  Count  Value  Min    Max   Std
   1 charge_minutes tally     12  79.83 30.0 107.43 25.34
```

## Used in

Guides: [How do I share a site's power limit between chargers?](../../how-to/recipes/site-power.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 (work): [DELAY](delay.md), [MOTION](motion.md), [PROCESS](process.md)
