1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! # 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](EnergyEnvelope::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: [`Band`]s on the deviation
//! from a baseline, against which an offer's [`Misfit`]s are listed.
//!
//! ## Baselines and settlement
//!
//! - [`baseline`]: `dprognosis@1`, `schedule@1`, `mbma@1` and `xofy@1`, each with required
//! parameters and a [card](baseline::cards); 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 recorded [`MeasurementPoint`]. A [`Settlement`] has a canonical text and a
//! `sha256:` [`Digest`] over RFC 8785 canonical JSON;
//! [`Settlement::from_canonical`] reads the other side's back and [`Settlement::diff`] names
//! where they part. [`Settlement::rebound`] reports 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`](Error::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"
//! );
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
// The README's example is compiled and run as a doctest.
;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Series;
pub use ;
pub use ;
pub use ;
/// The `jiff` crate, whose `Timestamp`, `civil::Date`, `civil::Time` and `tz::TimeZone` appear
/// in this crate's API.
pub use jiff;
/// A power literal in watts, exact and checked at compile time.
///
/// ```
/// use flexibility::{w, Power};
/// assert_eq!(w!(500), Power::from_watts(500));
/// assert_eq!(w!(-3_000), Power::from_watts(-3_000));
/// ```
///
/// A fraction of a watt does not compile:
///
/// ```compile_fail
/// let _ = flexibility::w!(0.5);
/// ```
/// 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.
///
/// ```
/// use flexibility::{kw, Power};
/// assert_eq!(kw!(4.2), Power::from_watts(4_200));
/// assert_eq!(kw!(-3), Power::from_watts(-3_000));
/// assert_eq!(kw!(11), Power::from_watts(11_000));
/// ```
///
/// A value that is not whole watts does not compile:
///
/// ```compile_fail
/// let _ = flexibility::kw!(4.0001);
/// ```
/// A power literal in megawatts, exact and checked at compile time.
///
/// ```
/// use flexibility::{mw, Power};
/// assert_eq!(mw!(1.5), Power::from_watts(1_500_000));
/// ```
/// An energy literal in watt-hours, exact and checked at compile time.
///
/// ```
/// use flexibility::{wh, Energy};
/// assert_eq!(wh!(500), Energy::from_watt_seconds(1_800_000));
/// ```
///
/// A fraction of a watt-hour does not compile:
///
/// ```compile_fail
/// let _ = flexibility::wh!(0.5);
/// ```
/// 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.
///
/// ```
/// use flexibility::{kwh, Energy};
/// assert_eq!(kwh!(1.5), Energy::from_watt_seconds(5_400_000));
/// assert_eq!(kwh!(30).to_string(), "30 kWh");
/// ```
///
/// ```compile_fail
/// let _ = flexibility::kwh!(1.0001);
/// ```
/// An energy literal in megawatt-hours, exact and checked at compile time.
///
/// ```
/// use flexibility::{mwh, kwh};
/// assert_eq!(mwh!(1.2), kwh!(1_200));
/// ```