flexpool-sim 0.5.0

flexpool replay: bitemporal series with decision-time views, synthetic and imported market days, scenarios and a forward-only test clock. No I/O, no real clock.
Documentation
//! A deterministic, plausible German day-ahead day.
//!
//! Morning and evening peaks, a midday solar dip whose depth depends on a seeded "sunniness"
//! (deep enough for negative prices on sunny days), and noise. Computed in `f64`, quantised to the
//! day's price granularity through `flexpool_core::quantize`, clamped to the price limits in force.
//! The same seed gives the same day on every platform and version (own SplitMix64, no `rand`).

use flexpool_core::{BusinessDay, EurPerMwh, Granularity, quantize};
use time::{Duration, OffsetDateTime};

/// SplitMix64 — tiny, deterministic, good enough for synthetic data.
#[derive(Debug, Clone)]
pub struct SplitMix64(u64);

impl SplitMix64 {
    /// A generator for `seed`.
    #[must_use]
    pub const fn new(seed: u64) -> Self {
        Self(seed)
    }

    /// The next 64 random bits.
    pub fn next_u64(&mut self) -> u64 {
        self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.0;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    /// A uniform value in `[0, 1)`.
    pub fn unit(&mut self) -> f64 {
        // 53 random mantissa bits.
        (self.next_u64() >> 11) as f64 / (1_u64 << 53) as f64
    }
}

fn bump(hour: f64, centre: f64, width: f64) -> f64 {
    let x = (hour - centre) / width;
    (-x * x).exp()
}

/// The price of each interval of `day` at `minutes` resolution, deterministic for `seed`.
///
/// Returns `(start, price)` pairs in time order.
#[must_use]
pub fn day_ahead(
    day: &BusinessDay,
    minutes: u16,
    seed: u64,
    price_step: Granularity,
    limits: (EurPerMwh, EurPerMwh),
) -> Vec<(OffsetDateTime, EurPerMwh)> {
    let mut rng = SplitMix64::new(seed ^ u64::from(day.date().to_julian_day().unsigned_abs()));
    let sunniness = rng.unit();
    let base = 70.0 + 40.0 * rng.unit();
    let step = Duration::minutes(i64::from(minutes));
    let mut out = Vec::new();
    let mut start = day.start();
    while start < day.end() {
        let local = flexpool_core::calendar::to_berlin(start);
        let hour = f64::from(local.hour()) + f64::from(local.minute()) / 60.0;
        let shape = 38.0 * bump(hour, 8.0, 1.6) + 55.0 * bump(hour, 19.0, 1.8)
            - (40.0 + 130.0 * sunniness * sunniness) * bump(hour, 13.0, 2.4)
            - 18.0 * bump(hour, 3.5, 2.0);
        let noise = (rng.unit() - 0.5) * 12.0;
        let price = quantize(base + shape + noise, price_step)
            .map(|q| EurPerMwh::new(q.value))
            .unwrap_or(EurPerMwh::ZERO)
            .clamp(limits.0, limits.1);
        out.push((start, price));
        start += step;
    }
    out
}