flexibility 0.2.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 DSO ordered 3 kW less consumption from 17:00 to 19:00. After the fact, it settles the
//! order against an MBMA baseline; the aggregator recomputes from its own copy of the meter
//! data and compares one digest; then the DSO reports what came back in the hour after.
//!
//! Run with `cargo run --example settle_an_order`. It asserts its own results.

use flexibility::MeasurementPoint;
use flexibility::baseline::{self, Mbma};
use flexibility::jiff::civil::{date, time};
use flexibility::jiff::tz::TimeZone;
use flexibility::{Deviation, Grid, IspDuration, Series, kw, settle};

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 evening = grid.window(time(17, 0, 0, 0), time(19, 0, 0, 0))?;
    let event = grid.isps(evening).ok_or("the evening is on the grid")?;

    // The site drew 7 kW; during the order it drew 4.5 kW, and 3 kW in the last half hour;
    // in the hour after, deferred load came back at 9 kW.
    let measured = Series::new(
        grid.clone(),
        (1..=grid.count())
            .map(|isp| match isp {
                75.. if event.contains(&isp) => kw!(3),
                _ if event.contains(&isp) => kw!(4.5),
                77..=80 => kw!(9),
                _ => kw!(7),
            })
            .collect(),
    )?;

    // The method the contract names, with its parameters: no defaults.
    let base = baseline::mbma(&measured, evening, Mbma { n: 4, gap: 1 })?;
    let actual = measured.over(evening)?;
    let ordered = Series::filled(actual.grid().clone(), Deviation::from_watts(-3_000));
    let dso = settle(&base, &actual, &ordered, MeasurementPoint::ConnectionPoint)?;
    println!("{dso}");

    // Delivered is capped at the order; the shortfall is the deficiency.
    assert_eq!(dso.lines()[0].delivered.to_string(), "-2.5 kW");
    assert_eq!(dso.lines()[0].deficiency.to_string(), "500 W");
    assert_eq!(dso.lines()[7].delivered.to_string(), "-3 kW");

    // The aggregator settles from its own copy and compares one hash.
    let copy = Series::new(grid.clone(), measured.values().to_vec())?;
    let agr = settle(
        &baseline::mbma(&copy, evening, Mbma { n: 4, gap: 1 })?,
        &copy.over(evening)?,
        &ordered,
        MeasurementPoint::ConnectionPoint,
    )?;
    assert_eq!(agr.digest(), dso.digest());
    assert_eq!(agr.diff(&dso), None);
    println!("both sides: {}", agr.digest());
    println!(
        "what {} does: {}",
        base.method(),
        base.method().card().formula
    );

    // Rebound: the same method over the order and the hour after it, so its meter-before ISPs
    // are the settlement's.
    let after = grid.window(time(19, 0, 0, 0), time(20, 0, 0, 0))?;
    let through = flexibility::Window::new(evening.from(), after.until())?;
    let rebound = dso.rebound(
        &baseline::mbma(&measured, through, Mbma { n: 4, gap: 1 })?,
        &measured.over(after)?,
        MeasurementPoint::ConnectionPoint,
    )?;
    println!("{rebound}");
    assert_eq!(rebound.total(), flexibility::kwh!(2));
    Ok(())
}