Skip to main content

Crate flexibility

Crate flexibility 

Source
Expand description

§flexibility

Grid flexibility as exact, replayable arithmetic. A grid operator caps what a site may draw, an aggregator plans what it will draw, both agree on what it would have drawn anyway (the baseline), and settle what was delivered. This crate computes all four in whole watts, in one sign convention, with a record of who set every bound and a digest both sides can recompute.

It is the arithmetic, not a market, a protocol stack or a controller: limits, plans and meter values go in as arguments; watts, watt-seconds and receipts come out.

§The shapes

  • Power is whole watts, positive for consumption, negative for feed-in (UFTP 3.1 § 4.2.1). kw!, w! and mw! are exact literals checked at compile time; Deviation is power relative to a baseline, Energy is watt-seconds.
  • A Grid is a run of imbalance settlement periods (ISPs) in a time zone: a Central European Grid::day has 92, 96 or 100 quarter hours. Grid::window turns local times into a Window and refuses the ones a clock change skips or repeats. A Series holds one value per ISP.
  • A Limit bounds power over a window; an Envelope is the strictest bound per ISP, each naming its Provenance; a Schedule is checked against it. Envelope::explain says why an ISP allows what it allows, and a Guarantee (the § 14a EnWG minimum) refuses any cap below it.
  • An EnergyEnvelope adds bounds on a stored Level (a car’s departure energy, a battery’s capacity) and gives a device’s steady headroom over a window; pool sums devices, says whether the result is exact, and checks a split.
  • A DeviationEnvelope is what a flexibility request asks for: Bands on the deviation from a baseline, against which an offer’s Misfits are listed.

§Baselines and settlement

§Protocols

Each converter is a Cargo feature, off by default, lossless or refusing:

FeatureProtocolMaps
uftpUFTP 3.1 (Shapeshifter)requests, offers, orders and settlement items
eebusEEBUS LPC and LPPactive and failsafe limits, timed or open-ended, both ways
s2S2 (EN 50491-12-2)PEBC power envelopes, PPBC power sequences
openadrOpenADR 3import and export capacity limits, both ways; usage reports
csipCSIP-AUS (IEEE 2030.5)dynamic operating envelopes at the connection point
iec104IEC 60870-5-104active-power set points, types 48–50, both ways
clsCLS.EEDIcontrol-box limits and failsafes, both ways
meteringGerman metering rulesthe § 14a minimum, Lastgang energy as power

§Guarantees

The crate reads no clock, file, network, environment or random source, and does no floating-point arithmetic: the same inputs give the same bytes on every machine. A float on the wire (S2, IEC 104) is read once, as the decimal written. What cannot be represented exactly is refused with an Error that names the values and offers a hint, never rounded silently. Time is jiff, with the time-zone database bundled.

use flexibility::jiff::{civil::{date, time}, tz::TimeZone};
use flexibility::*;

let berlin = TimeZone::get("Europe/Berlin")?;
let grid = Grid::day(date(2026, 12, 1), &berlin, IspDuration::FIFTEEN_MINUTES)?;
let noon = grid.local(time(12, 0, 0, 0))?;

// The grid operator caps consumption at 4.2 kW from 17:00 to 19:00 local time.
let evening = grid.window(time(17, 0, 0, 0), time(19, 0, 0, 0))?;
let dso = Provenance::new("dso.example", "order-42", noon);
let envelope = Envelope::unbounded(grid.clone()).apply(&Limit::consumption_cap(evening, kw!(4.2), dso))?;

// A car planned at 7.4 kW all evening breaks it in ISPs 69 to 76.
let car = Provenance::new("agr.example", "plan-7", noon);
let plan = Schedule::new(Series::filled(grid, kw!(7.4)), car);
let violations = plan.check(&envelope)?;
assert_eq!(violations.len(), 8);
assert_eq!(
    violations[0].to_string(),
    "ISP 69 (17:00–17:15 +01:00): planned 7.4 kW above max 4.2 kW set by dso.example order-42"
);

Re-exports§

pub use baseline::Baseline;
pub use baseline::MethodId;
pub use jiff;

Modules§

accuracy
How well a baseline method predicts what a site actually did, scored exactly.
baseline
Baselines: what a site would have done without the activation, by a named method.
cls
CLS.EEDI (KEO, FNN control box) power limits as limits and back, behind the cls feature.
csip
CSIP-AUS dynamic operating envelopes (IEEE 2030.5), behind the csip feature.
eebus
EEBUS LPC and LPP limits as Limits and back, behind the eebus feature.
iec104
IEC 60870-5-104 active-power set points as limits and back, behind the iec104 feature.
metering
metering’s numbers in this crate’s shapes, behind the metering feature: the § 14a minimum as a Guarantee, and quarter-hour Lastgang energy as average power per ISP, for settle.
openadr
OpenADR 3 capacity limits and usage reports, behind the openadr feature.
pool
Pools of devices: what a pool can offer, and whether a given split of a pool plan works.
s2
S2 (EN 50491-12-2) power envelopes and power sequences, behind the s2 feature.
uftp
UFTP 3.1 in this crate’s shapes, behind the uftp feature: a FlexRequest as a DeviationEnvelope, a FlexOffer option or a FlexOrder as a deviation series, the FlexOrderSettlement item for an order, and the check a receiver can run on a settlement alone.

Macros§

kw
A power literal in kilowatts, exact and checked at compile time: the macro reads the literal’s text, never a float, so kw!(4.2) is exactly 4 200 W.
kwh
An energy literal in kilowatt-hours, exact and checked at compile time: kwh!(30) is 108 000 000 watt-seconds, kwh!(1.5) 1 500 Wh.
mw
A power literal in megawatts, exact and checked at compile time.
mwh
An energy literal in megawatt-hours, exact and checked at compile time.
w
A power literal in watts, exact and checked at compile time.
wh
An energy literal in watt-hours, exact and checked at compile time.

Structs§

Band
The allowed deviation in one ISP, both ends inclusive, and why it is bounded.
Bounds
The allowed power range: an optional minimum and maximum, the minimum never above the maximum. With consumption positive, a consumption cap is a maximum and a feed-in cap a (negative) minimum.
Breach
Where and how a limit breaks a guarantee.
Deviation
Power relative to a baseline, consumption positive: −3 kW is “3 kW less consumption, or 3 kW more feed-in, than the baseline”.
DeviationEnvelope
Bounds on the deviation from a baseline per ISP, set by one party: a flexibility request. An ISP without a band is one the request says nothing about.
DeviceId
The id of a dedicated measurement device: 1–64 characters, no control characters.
Digest
A SHA-256 digest of a canonical text, written sha256: and 64 lowercase hex digits.
Energy
An exact amount of energy in whole watt-seconds, consumption positive.
EnergyEnvelope
Power bounds per ISP and bounds on the stored level after each ISP, from an initial level.
Envelope
The allowed power per ISP of a grid, for each bound who set it, every limit applied, and the guarantees in force.
Explanation
Why one ISP allows what it allows.
Grid
A run of count ISPs of equal length from start, labelled in a time zone.
Guarantee
Power an envelope must keep allowing over a window: what a limit may not take away.
Headroom
How far a schedule may move, in one ISP (Envelope::headroom) or steadily over a window (EnergyEnvelope::steady_headroom): None where the envelope has no bound.
IspDuration
The length of one imbalance settlement period (ISP), in whole minutes that divide a day.
IspLabel
An ISP number with its interval and the local UTC offset at its start: what a person reads.
Level
Bounds on a stored level: an optional minimum and maximum energy, the minimum never above the maximum.
LevelViolation
One ISP boundary at which a plan’s level leaves its bounds.
Limit
Bounds over a window, with provenance.
Line
One settled ISP.
Misfit
One ISP in which an offer or order misfits a DeviationEnvelope.
Power
An exact amount of power in whole watts.
Provenance
Who set a limit, guarantee, envelope or schedule, under which reference, and when.
Rebound
The rebound after a settlement: per ISP of the window that follows it, and in total, with the digests of what it was computed from, the settlement’s among them.
ReboundLine
One ISP of a Rebound.
Report
Everything wrong with one plan against an EnergyEnvelope: power and level.
Schedule
Planned power per ISP, with provenance.
Series
Exactly one value per ISP of its grid.
Settlement
A settlement: every line, the totals, and what it was computed from.
Violation
One ISP in which a schedule leaves its envelope.
Window
A non-empty, half-open interval of time [from, until).

Enums§

Broken
Which bound a planned power breaks.
Difference
The first thing two settlements disagree on.
Direction
Which way power flows.
Disposition
Why a request bounds an ISP [UFTP 3.1 § 2.4.4].
Error
Why an operation refused. Every variant names the values involved; Display renders it as one line, and Error::hint suggests a remedy where there is one.
LevelBroken
Which level bound a plan breaks.
MeasurementPoint
Where the actual power was measured: the connection point’s meter (the NC DR default, Art 12) or a named dedicated measurement device (Art 13). Recorded in every settlement and its receipt, so a sub-meter series cannot pass as the fiscal meter’s.
MisfitKind
How an ISP of an offer or order misfits a DeviationEnvelope.
Rounding
How a division may round. Every division in the crate names one.

Constants§

MAX_EXACT
The largest integer magnitude in a canonical text, 2^53: RFC 8785 writes numbers as IEEE doubles, which are exact up to here. An energy total of 2^53 watt-seconds is about 2.5 TWh.
MAX_ISPS
The largest grid: one leap year of 1-minute ISPs. Larger grids are refused before anything is allocated.
MAX_RECEIPT_BYTES
The largest canonical text Settlement::from_canonical reads: 96 MiB, more than a settlement of MAX_ISPS lines needs.

Functions§

canonical_series
The canonical text of a power series, the document whose digest a settlement names as its actual input: for callers who want to publish or check one input.
settle
Settles actual against baseline for ordered, ISP by ISP. All three are on the event’s ISPs; Series::over cuts a day’s measurement to them.