flexibility 0.1.1

Grid flexibility as exact, replayable arithmetic: limits, baselines, UFTP-exact settlement, and UFTP, EEBUS, S2, OpenADR, IEC 104 converters.
Documentation
//! 005: bounds on a deviation from a baseline — what a FlexRequest asks for.

mod common;

use common::*;
use flexibility::jiff::civil::date;
use flexibility::{
    Band, Bounds, Deviation, DeviationEnvelope, Disposition, Error, Power, Series, kw,
};

fn d(kw: i64) -> Deviation {
    Deviation::from_watts(kw * 1_000)
}

fn band(min: i64, max: i64, disposition: Disposition) -> Band {
    Band::new(d(min), d(max), disposition).unwrap()
}

/// UFTP 3.1 Figure 2-8, values read off the figure (kW): congestion at ISP 2, so ISP 2 is
/// Requested (consume at least 10 kW less); ISPs 1, 3–5 are Available, with the most room for
/// more consumption in ISP 5. On ISPs 69–73 of a Berlin day, so labels read as local time.
fn figure_2_8() -> DeviationEnvelope {
    let g = day().slice(69..=73).unwrap();
    let a = Disposition::Available;
    let bands = vec![
        Some(band(-10, 10, a)),
        Some(band(-50, -10, Disposition::Requested)),
        Some(band(-10, 10, a)),
        Some(band(-10, 10, a)),
        Some(band(-10, 30, a)),
    ];
    DeviationEnvelope::new(Series::new(g, bands).unwrap(), dso())
}

fn offer(kw: [i64; 5]) -> Series<Deviation> {
    Series::new(figure_2_8().grid().clone(), kw.map(d).to_vec()).unwrap()
}

#[test]
fn an_offer_shifting_consumption_to_isp_5_fits() {
    assert_eq!(figure_2_8().check(&offer([0, -10, 0, 0, 10])).unwrap(), []);
}

#[test]
fn an_offer_short_in_the_requested_isp_is_a_misfit() {
    let misfits = figure_2_8().check(&offer([0, -5, 0, 0, 5])).unwrap();
    assert_eq!(misfits.len(), 1);
    assert_eq!(
        misfits[0].to_string(),
        "ISP 70 (17:15–17:30 +01:00): offered -5 kW outside -50 kW to -10 kW (requested) set by dso.example order-42"
    );
}

#[test]
fn a_deviation_where_nothing_was_requested_is_reported_and_zero_is_not() {
    let g = day().slice(69..=70).unwrap();
    let env = DeviationEnvelope::new(
        Series::new(
            g.clone(),
            vec![Some(band(-10, 0, Disposition::Requested)), None],
        )
        .unwrap(),
        dso(),
    );
    let quiet = Series::new(g.clone(), vec![d(-5), d(0)]).unwrap();
    assert_eq!(env.check(&quiet).unwrap(), []);
    let loud = Series::new(g, vec![d(-5), d(1)]).unwrap();
    let m = env.check(&loud).unwrap();
    assert_eq!((m.len(), m[0].band), (1, None));
    assert!(
        m[0].to_string()
            .ends_with("offered +1 kW where dso.example order-42 requested nothing")
    );
}

#[test]
fn an_offer_covering_part_of_the_request_is_read_as_zero_elsewhere() {
    let part = Series::new(day().slice(70..=70).unwrap(), vec![d(-20)]).unwrap();
    assert_eq!(figure_2_8().check(&part).unwrap(), []);
    let none = Series::new(day().slice(71..=71).unwrap(), vec![d(0)]).unwrap();
    let m = figure_2_8().check(&none).unwrap();
    assert_eq!(m[0].label.isp(), 70, "the requested ISP left at zero");
}

#[test]
fn an_offer_on_another_day_is_refused() {
    let other = Series::filled(day_of(date(2026, 12, 2)).slice(69..=73).unwrap(), d(0));
    assert!(matches!(
        figure_2_8().check(&other),
        Err(Error::GridMismatch { .. })
    ));
}

#[test]
fn a_band_refuses_a_minimum_above_its_maximum() {
    assert!(matches!(
        Band::new(d(1), d(-1), Disposition::Requested),
        Err(Error::InvertedBand { .. })
    ));
}

#[test]
fn figure_2_8_resolves_against_the_forecast() {
    // The figure's forecast (kW): 10, 25, 15, 15, 5.
    let forecast = Series::new(
        figure_2_8().grid().clone(),
        vec![kw!(10), kw!(25), kw!(15), kw!(15), kw!(5)],
    )
    .unwrap();
    let envelope = figure_2_8().resolve(&forecast).unwrap();
    let b = |isp| envelope.bounds(isp).unwrap();
    assert_eq!(b(70), Bounds::new(Some(kw!(-25)), Some(kw!(15))).unwrap());
    assert_eq!(b(73), Bounds::new(Some(kw!(-5)), Some(kw!(35))).unwrap());
    assert_eq!(envelope.max_source(70), Some(&dso()));
    // Equal neighbouring bounds are one layer, so explain lists nothing shadowed.
    assert_eq!(envelope.explain(71).unwrap().shadowed, []);
    // A part of the day resolves on that part.
    let part = forecast.slice(70..=70).unwrap();
    assert_eq!(
        figure_2_8().resolve(&part).unwrap().grid().numbers(),
        70..=70
    );
}

#[test]
fn resolving_at_the_edge_of_the_range_is_refused() {
    let edge = Series::filled(figure_2_8().grid().clone(), Power::from_watts(i64::MAX));
    assert!(matches!(
        figure_2_8().resolve(&edge),
        Err(Error::Overflow { .. })
    ));
}