#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RenewableModel {
pub wind_capacity_mw: f64,
pub solar_capacity_mw: f64,
pub wind_midpoint_kmh: f64,
pub wind_steepness: f64,
}
const STC_IRRADIANCE: f64 = 1000.0;
impl RenewableModel {
pub const fn v1_uncalibrated() -> Self {
Self {
wind_capacity_mw: 0.0,
solar_capacity_mw: 0.0,
wind_midpoint_kmh: 30.0,
wind_steepness: 0.12,
}
}
pub fn wind_capacity_factor(&self, wind_kmh: f64) -> f64 {
1.0 / (1.0 + (-self.wind_steepness * (wind_kmh - self.wind_midpoint_kmh)).exp())
}
pub fn estimate_wind_mw(&self, wind_kmh: f64) -> f64 {
self.wind_capacity_mw * self.wind_capacity_factor(wind_kmh)
}
pub fn estimate_solar_mw(&self, irradiance_wm2: f64) -> f64 {
self.solar_capacity_mw * (irradiance_wm2 / STC_IRRADIANCE).max(0.0)
}
}
#[derive(Debug, Clone, Copy)]
pub struct RenewableSample {
pub wind_kmh: f64,
pub irradiance_wm2: f64,
pub eolien_mw: f64,
pub solaire_mw: f64,
}
pub fn calibrate(samples: &[RenewableSample], curve: RenewableModel) -> Option<RenewableModel> {
let (mut sw_xy, mut sw_xx, mut ss_xy, mut ss_xx) = (0.0, 0.0, 0.0, 0.0);
for s in samples {
let cf = curve.wind_capacity_factor(s.wind_kmh);
sw_xy += cf * s.eolien_mw;
sw_xx += cf * cf;
let x = (s.irradiance_wm2 / STC_IRRADIANCE).max(0.0);
ss_xy += x * s.solaire_mw;
ss_xx += x * x;
}
if sw_xx <= f64::EPSILON || ss_xx <= f64::EPSILON {
return None;
}
Some(RenewableModel {
wind_capacity_mw: sw_xy / sw_xx,
solar_capacity_mw: ss_xy / ss_xx,
..curve
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capacity_factor_is_monotonic_and_bounded() {
let m = RenewableModel::v1_uncalibrated();
let (low, mid, high) = (
m.wind_capacity_factor(5.0),
m.wind_capacity_factor(30.0),
m.wind_capacity_factor(80.0),
);
assert!((0.0..=1.0).contains(&low));
assert!(low < mid && mid < high);
assert!((mid - 0.5).abs() < 1e-9, "médiane = 0,5 au point milieu");
}
#[test]
fn solar_is_linear_in_irradiance_and_zero_at_night() {
let m = RenewableModel {
solar_capacity_mw: 10_000.0,
..RenewableModel::v1_uncalibrated()
};
assert_eq!(m.estimate_solar_mw(0.0), 0.0);
assert_eq!(m.estimate_solar_mw(1000.0), 10_000.0);
assert!((m.estimate_solar_mw(500.0) - 5_000.0).abs() < 1e-9);
}
#[test]
fn calibration_recovers_known_capacities() {
let truth = RenewableModel {
wind_capacity_mw: 24_000.0,
solar_capacity_mw: 18_000.0,
..RenewableModel::v1_uncalibrated()
};
let samples: Vec<RenewableSample> = [(10.0, 200.0), (35.0, 600.0), (60.0, 950.0)]
.iter()
.map(|&(wind, irr)| RenewableSample {
wind_kmh: wind,
irradiance_wm2: irr,
eolien_mw: truth.estimate_wind_mw(wind),
solaire_mw: truth.estimate_solar_mw(irr),
})
.collect();
let fit = calibrate(&samples, RenewableModel::v1_uncalibrated()).unwrap();
assert!((fit.wind_capacity_mw - 24_000.0).abs() < 1e-6);
assert!((fit.solar_capacity_mw - 18_000.0).abs() < 1e-6);
}
}