使用 Rust 为 'Rust' 编码:建模 100 小时电池网格
发布: (2025年12月10日 GMT+8 04:33)
2 min read
原文: Dev.to
Source: Dev.to
Rust 实现铁空气电池
#[derive(Debug, Clone)]
struct IronAirBattery {
capacity_kwh: f64,
current_charge_kwh: f64,
efficiency_factor: f64, // Iron‑Air is usually around 50‑60%
max_charge_rate_kw: f64,
max_discharge_rate_kw: f64,
is_rusting: bool, // True = Discharging, False = Charging
}
impl IronAirBattery {
fn new(capacity: f64) -> Self {
IronAirBattery {
capacity_kwh: capacity,
current_charge_kwh: 0.0,
efficiency_factor: 0.55, // The physics constraint
max_charge_rate_kw: 50.0, // Slow charge
max_discharge_rate_kw: 50.0, // Slow discharge
is_rusting: true,
}
}
// The "Un‑Rusting" Process (Charging)
fn charge(&mut self, energy_input_kwh: f64) -> Result {
let actual_stored = energy_input_kwh * self.efficiency_factor;
if self.current_charge_kwh + actual_stored > self.capacity_kwh {
self.current_charge_kwh = self.capacity_kwh;
self.is_rusting = false;
return Ok(self.capacity_kwh);
}
self.current_charge_kwh += actual_stored;
self.is_rusting = false;
Ok(self.current_charge_kwh)
}
// The "Rusting" Process (Discharging)
fn discharge(&mut self, demand_kwh: f64) -> f64 {
self.is_rusting = true;
if self.current_charge_kwh >= demand_kwh {
self.current_charge_kwh -= demand_kwh;
demand_kwh
} else {
let remaining = self.current_charge_kwh;
self.current_charge_kwh = 0.0;
remaining
}
}
}
铁空气电池示意图

多日算法(Python)
import numpy as np
class HybridGridController:
def __init__(self, li_ion_capacity, iron_air_capacity):
self.li_ion_storage = li_ion_capacity
self.iron_air_storage = iron_air_capacity
# Threshold: Only use Iron‑Air if outage is predicted > 4 hours
self.long_duration_threshold = 4
def decision_logic(self, grid_status, forecast_hours_without_sun):
"""
grid_status: 'NORMAL', 'STRESS', 'BLACKOUT'
forecast_hours_without_sun: int
"""
print(f"Analyzing Grid: {grid_status} | Darkness Forecast: {forecast_hours_without_sun}h")
if grid_status == 'NORMAL':
# Priority: Recharge Iron‑Air first because it takes literally days
return "ACTION: CHARGE_IRON_AIR_SLOWLY"
elif grid_status == 'STRESS':
if forecast_hours_without_sun (
);
JavaScript 导出
); export default GridDashboard;
首先了解铁空气化学原理——因为你无法优化不了解的东西。
祝编码愉快(以及生锈)!