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
//! baselines-and-settlement/SR-002: the canonical output of every method id is pinned.
//!
//! A snapshot that no longer matches means the method's meaning changed. The remedy is a new
//! method id (`mbma@2`), never an edited snapshot. Missing snapshots are
//! written once with `FLEXIBILITY_WRITE_SNAPSHOTS=1 cargo test --test snapshots`; existing ones
//! are never overwritten.

mod common;

use common::{agr, day, day_of, win};
use flexibility::MeasurementPoint;
use flexibility::baseline::{self, Adjustment, Mbma, XofY};
use flexibility::jiff::civil::date;
use flexibility::{Baseline, Deviation, MethodId, Power, Schedule, Series, settle};

fn varied() -> Series<Power> {
    // A recognisable day: 3 kW base, a ramp in the evening, a dip at night.
    let values = (1..=96i64)
        .map(|isp| Power::from_watts(3_000 + (isp % 7) * 137 + if isp > 60 { isp * 11 } else { 0 }))
        .collect();
    Series::new(day(), values).unwrap()
}

fn history() -> Vec<Series<Power>> {
    (21..=30)
        .map(|d| {
            let g = day_of(date(2026, 11, d));
            let values = (1..=96i64)
                .map(|isp| Power::from_watts(2_000 + i64::from(d) * 101 + (isp % 5) * 53))
                .collect();
            Series::new(g, values).unwrap()
        })
        .collect()
}

fn baseline_for(method: MethodId) -> Baseline {
    let evening = win(17, 0, 19, 0);
    match method {
        MethodId::DPrognosis1 => baseline::dprognosis(&Schedule::new(varied(), agr()), evening),
        MethodId::Schedule1 => baseline::schedule(&Schedule::new(varied(), agr()), evening),
        MethodId::Mbma1 => baseline::mbma(&varied(), evening, Mbma { n: 4, gap: 1 }),
        MethodId::XofY1 => baseline::xofy(
            &day(),
            evening,
            &history(),
            Some(&varied()),
            XofY {
                x: 5,
                y: 10,
                adjustment: Adjustment::Ratio {
                    window: 4,
                    gap: 1,
                    cap_percent: 20,
                },
            },
        ),
        _ => unreachable!("a new method id needs a snapshot"),
    }
    .unwrap()
}

fn check(name: &str, actual: &str) {
    let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/snapshots")
        .join(format!("{name}.json"));
    match std::fs::read_to_string(&path) {
        Ok(expected) => assert_eq!(
            actual,
            expected.trim_end(),
            "{name} changed: a changed output is a new method id, not a new snapshot"
        ),
        Err(_) if std::env::var_os("FLEXIBILITY_WRITE_SNAPSHOTS").is_some() => {
            std::fs::write(&path, format!("{actual}\n")).unwrap();
        }
        Err(e) => panic!("missing snapshot {}: {e}", path.display()),
    }
}

#[test]
fn every_method_id_is_pinned() {
    for method in MethodId::ALL {
        let b = baseline_for(method);
        check(method.as_str(), &b.canonical());
    }
}

#[test]
fn a_settlement_is_pinned() {
    let b = baseline_for(MethodId::Mbma1);
    let evening = win(17, 0, 19, 0);
    let actual = varied().over(evening).unwrap();
    let ordered = Series::filled(actual.grid().clone(), Deviation::from_watts(-1_500));
    check(
        "settlement-mbma@1",
        &settle(&b, &actual, &ordered, MeasurementPoint::ConnectionPoint)
            .unwrap()
            .canonical(),
    );
}