flexibility 0.1.1

Grid flexibility as exact, replayable arithmetic: limits, baselines, UFTP-exact settlement, and UFTP, EEBUS, S2, OpenADR, IEC 104 converters.
Documentation
//! 007: pools — the aggregate, exact or outer, and the check of a given split.

mod common;

use common::{at, day};
use flexibility::pool::{self, Aggregate};
use flexibility::{
    Bounds, EnergyEnvelope, Envelope, Error, Grid, Level, Limit, Power, Provenance, Schedule,
    Series, Window, kw, kwh, w,
};

fn who(reference: &str) -> Provenance {
    Provenance::new("agr.example", reference, at(0, 0))
}

fn boxed(grid: &Grid, min: Power, max: Power) -> EnergyEnvelope {
    let all = Window::new(grid.start(), grid.end()).unwrap();
    let env = Envelope::unbounded(grid.clone())
        .apply(&Limit::new(
            all,
            Bounds::new(Some(min), Some(max)).unwrap(),
            who("device"),
        ))
        .unwrap();
    EnergyEnvelope::new(env, kwh!(0))
}

fn plan(grid: &Grid, kw: &[Power]) -> Schedule {
    Schedule::new(Series::new(grid.clone(), kw.to_vec()).unwrap(), who("plan"))
}

fn grid() -> Grid {
    day().slice(69..=70).unwrap()
}

#[test]
fn power_boxes_sum_exactly() {
    let g = grid();
    let agg = pool::aggregate(
        &[boxed(&g, kw!(0), kw!(11)), boxed(&g, kw!(-3), kw!(3))],
        who("pool"),
    )
    .unwrap();
    assert!(agg.is_exact());
    assert_eq!(
        agg.envelope().power().bounds(69),
        Some(Bounds::new(Some(kw!(-3)), Some(kw!(14))).unwrap())
    );
    assert_eq!(agg.envelope().power().max_source(69), Some(&who("pool")));
    assert_eq!(agg.envelope().power().guarantees(), []);
}

#[test]
fn an_unbounded_side_stays_unbounded() {
    let g = grid();
    let all = Window::new(g.start(), g.end()).unwrap();
    let capped = EnergyEnvelope::new(
        Envelope::unbounded(g.clone())
            .apply(&Limit::consumption_cap(all, kw!(5), who("cap")))
            .unwrap(),
        kwh!(0),
    );
    let agg = pool::aggregate(&[capped, boxed(&g, kw!(0), kw!(5))], who("pool")).unwrap();
    assert_eq!(
        agg.envelope().power().bounds(69),
        Some(Bounds::at_most(kw!(10)))
    );
}

/// Car A must charge 10 kW in both ISPs to have 5 kWh; device B may take 0–10 kW, no storage.
/// The outer aggregate (0–20 kW, no level bound) accepts a pool plan of 0 then 20 kW, which no
/// split can follow: A would have to draw 10 kW in ISP 69 while B draws −10 kW.
#[test]
fn an_outer_aggregate_can_accept_a_plan_no_split_follows() {
    let g = grid();
    let a = boxed(&g, kw!(0), kw!(10))
        .level_at(g.end(), Level::at_least(kwh!(5)), who("departure"))
        .unwrap();
    let b = boxed(&g, kw!(0), kw!(10));
    let agg = pool::aggregate(&[a.clone(), b.clone()], who("pool")).unwrap();
    let Aggregate::Outer(env) = &agg else {
        panic!("A has a level bound")
    };
    let pool_plan = plan(&g, &[kw!(0), kw!(20)]);
    assert!(
        env.check(&pool_plan).unwrap().fits(),
        "inside the outer aggregate"
    );

    let split = pool::check_split(
        &pool_plan,
        &[
            (&a, &plan(&g, &[kw!(0), kw!(10)])),
            (&b, &plan(&g, &[kw!(0), kw!(10)])),
        ],
    )
    .unwrap();
    assert_eq!(split.sums, []);
    assert_eq!(split.devices.len(), 1);
    assert_eq!(split.devices[0].0, 0, "device 0, the car");
    assert_eq!(split.devices[0].1.level[0].level, kwh!(2.5));
}

#[test]
fn a_split_that_misses_the_pool_plan_is_named() {
    let g = grid();
    let (a, b) = (boxed(&g, kw!(0), kw!(11)), boxed(&g, kw!(0), kw!(11)));
    let pool_plan = plan(&g, &[kw!(10), kw!(10)]);
    let ok = pool::check_split(
        &pool_plan,
        &[
            (&a, &plan(&g, &[kw!(5), kw!(4)])),
            (&b, &plan(&g, &[kw!(5), kw!(6)])),
        ],
    )
    .unwrap();
    assert!(ok.fits());
    let short = pool::check_split(
        &pool_plan,
        &[
            (&a, &plan(&g, &[kw!(5), kw!(4)])),
            (&b, &plan(&g, &[w!(4999), kw!(6)])),
        ],
    )
    .unwrap();
    assert_eq!(short.sums.len(), 1);
    assert_eq!(
        (short.sums[0].label.isp(), short.sums[0].devices),
        (69, w!(9999))
    );
    assert_eq!(
        short.sums[0].to_string(),
        "ISP 69 (17:00–17:15 +01:00): pool plans 10 kW, devices sum to 9.999 kW"
    );
}

#[test]
fn a_pool_needs_devices_on_one_grid() {
    assert!(matches!(
        pool::aggregate(&[], who("pool")),
        Err(Error::NoData { .. })
    ));
    let other = day().slice(71..=72).unwrap();
    assert!(matches!(
        pool::aggregate(
            &[
                boxed(&grid(), kw!(0), kw!(1)),
                boxed(&other, kw!(0), kw!(1))
            ],
            who("pool")
        ),
        Err(Error::GridMismatch { .. })
    ));
}