flexibility 0.3.0

Grid flexibility as exact, replayable arithmetic: limits, baselines, UFTP-exact settlement, and UFTP, EEBUS, S2, OpenADR, CSIP-AUS, IEC 104, CLS.EEDI converters.
Documentation
//! A grid operator caps a site's consumption for the evening peak; the site's connection
//! limits it all day; § 14a keeps 4.2 kW guaranteed. An aggregator's charging plan is checked
//! against all of it, and the envelope explains itself.
//!
//! Run with `cargo run --example evening_cap`. It asserts its own results.

use flexibility::jiff::civil::{date, time};
use flexibility::jiff::tz::TimeZone;
use flexibility::{
    Broken, Envelope, Grid, Guarantee, IspDuration, Limit, Provenance, Schedule, Series, Window, kw,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let berlin = TimeZone::get("Europe/Berlin")?;
    let grid = Grid::day(date(2026, 12, 1), &berlin, IspDuration::FIFTEEN_MINUTES)?;
    let day = Window::new(grid.start(), grid.end())?;
    let evening = grid.window(time(17, 0, 0, 0), time(19, 0, 0, 0))?;
    let noon = grid.local(time(12, 0, 0, 0))?;

    let rule = Provenance::new("bnetza", "§ 14a minimum", grid.start());
    let connection = Provenance::new("site.example", "connection-11kW", grid.start());
    let dso = Provenance::new("dso.example", "flex-order-42", noon);

    let envelope = Envelope::unbounded(grid.clone())
        .guarantee(&Guarantee::consumption(day, kw!(4.2), rule)?)?
        .apply(&Limit::consumption_cap(day, kw!(11), connection.clone()))?
        .apply(&Limit::consumption_cap(evening, kw!(4.2), dso.clone()))?;

    // A cap below the guarantee is refused, naming both parties.
    let too_deep = envelope.apply(&Limit::consumption_cap(evening, kw!(3), dso.clone()));
    println!("refused: {}", too_deep.unwrap_err());

    // Why is ISP 69 at 4.2 kW? The DSO; the connection limit is shadowed.
    let why = envelope.explain(69).ok_or("ISP 69 is on the grid")?;
    assert_eq!(why.max_source.as_ref(), Some(&dso));
    assert_eq!(why.shadowed.len(), 1);
    println!("{why}");

    // A car charging at 7.4 kW from 16:00 to 20:00, nothing otherwise.
    let charging = grid.window(time(16, 0, 0, 0), time(20, 0, 0, 0))?;
    let hours = grid.isps(charging).ok_or("the window is on the grid")?;
    let plan = Series::new(
        grid.clone(),
        (1..=grid.count())
            .map(|isp| {
                if hours.contains(&isp) {
                    kw!(7.4)
                } else {
                    kw!(0)
                }
            })
            .collect(),
    )?;
    let agr = Provenance::new("agr.example", "charging-plan-7", noon);
    let schedule = Schedule::new(plan, agr);
    let violations = schedule.check(&envelope)?;

    assert_eq!(violations.len(), 8);
    for v in &violations {
        assert!((69..=76).contains(&v.label.isp()));
        assert_eq!(v.broken, Broken::AboveMax(kw!(4.2)));
        assert_eq!(v.source, dso);
    }
    println!("{}", violations[0]);

    // How much room is left? 3.2 kW too much in the evening, 3.6 kW spare before it.
    let room = envelope.headroom(&schedule)?;
    let up = |isp| room.get(isp).and_then(|h| h.up).map(|d| d.to_string());
    assert_eq!(up(69).as_deref(), Some("-3.2 kW"));
    assert_eq!(up(65).as_deref(), Some("+3.6 kW"));
    println!("headroom up: ISP 65 {:?}, ISP 69 {:?}", up(65), up(69));
    println!("energy planned: {}", schedule.series().energy()?);
    Ok(())
}