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
Poweris whole watts, positive for consumption, negative for feed-in (UFTP 3.1 § 4.2.1).kw!,w!andmw!are exact literals checked at compile time;Deviationis power relative to a baseline,Energyis watt-seconds.- A
Gridis a run of imbalance settlement periods (ISPs) in a time zone: a Central EuropeanGrid::dayhas 92, 96 or 100 quarter hours.Grid::windowturns local times into aWindowand refuses the ones a clock change skips or repeats. ASeriesholds one value per ISP. - A
Limitbounds power over a window; anEnvelopeis the strictest bound per ISP, each naming itsProvenance; aScheduleis checked against it.Envelope::explainsays why an ISP allows what it allows, and aGuarantee(the § 14a EnWG minimum) refuses any cap below it. - An
EnergyEnvelopeadds bounds on a storedLevel(a car’s departure energy, a battery’s capacity) and gives a device’s steady headroom over a window;poolsums devices, says whether the result is exact, and checks a split. - A
DeviationEnvelopeis what a flexibility request asks for:Bands on the deviation from a baseline, against which an offer’sMisfits are listed.
§Baselines and settlement
baseline:dprognosis@1,schedule@1,mbma@1andxofy@1, each with required parameters and a card; a method id never changes its meaning.settle: delivered flexibility and power deficiency per ISP, exactly as UFTP 3.1 § 2.6 computes them, at a recordedMeasurementPoint. ASettlementhas a canonical text and asha256:Digestover RFC 8785 canonical JSON;Settlement::from_canonicalreads the other side’s back andSettlement::diffnames where they part.Settlement::reboundreports what came back after the order.accuracy: how well a method predicts a site, scored exactly.
§Protocols
Each converter is a Cargo feature, off by default, lossless or refusing:
| Feature | Protocol | Maps |
|---|---|---|
uftp | UFTP 3.1 (Shapeshifter) | requests, offers, orders and settlement items |
eebus | EEBUS LPC and LPP | active and failsafe limits, timed or open-ended, both ways |
s2 | S2 (EN 50491-12-2) | PEBC power envelopes, PPBC power sequences |
openadr | OpenADR 3 | import and export capacity limits, both ways; usage reports |
csip | CSIP-AUS (IEEE 2030.5) | dynamic operating envelopes at the connection point |
iec104 | IEC 60870-5-104 | active-power set points, types 48–50, both ways |
cls | CLS.EEDI | control-box limits and failsafes, both ways |
metering | German metering rules | the § 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§
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
clsfeature. - csip
- CSIP-AUS dynamic operating envelopes (IEEE 2030.5), behind the
csipfeature. - eebus
- EEBUS LPC and LPP limits as
Limits and back, behind theeebusfeature. - iec104
- IEC 60870-5-104 active-power set points as limits and back, behind the
iec104feature. - metering
metering’s numbers in this crate’s shapes, behind themeteringfeature: the § 14a minimum as aGuarantee, and quarter-hour Lastgang energy as average power per ISP, forsettle.- openadr
- OpenADR 3 capacity limits and usage reports, behind the
openadrfeature. - 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
s2feature. - uftp
- UFTP 3.1 in this crate’s shapes, behind the
uftpfeature: a FlexRequest as aDeviationEnvelope, 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”.
- Deviation
Envelope - 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.
- Device
Id - 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.
- Energy
Envelope - 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
countISPs of equal length fromstart, 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):Nonewhere 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.
- Level
Violation - 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.
- Rebound
Line - 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;
Displayrenders it as one line, andError::hintsuggests a remedy where there is one. - Level
Broken - Which level bound a plan breaks.
- Measurement
Point - 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.
- Misfit
Kind - 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_canonicalreads: 96 MiB, more than a settlement ofMAX_ISPSlines needs.
Functions§
- canonical_
series - The canonical text of a power series, the document whose digest a settlement names as its
actualinput: for callers who want to publish or check one input. - settle
- Settles
actualagainstbaselineforordered, ISP by ISP. All three are on the event’s ISPs;Series::overcuts a day’s measurement to them.