|
| 1 | +import dataclasses |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | + |
| 5 | +@dataclasses.dataclass() |
| 6 | +class ReservoirParams: |
| 7 | + """ |
| 8 | + Data class to hold static parameters and initial timeseries data for a reservoir. |
| 9 | +
|
| 10 | + Attributes: |
| 11 | + name (str): The unique name of the reservoir. |
| 12 | + min_day (int): The day of the year when the target level is typically at its minimum. |
| 13 | + max_day (int): The day of the year when the target level is typically at its maximum. |
| 14 | + min_level (float): The minimum operational water level (meters). |
| 15 | + max_level (float): The maximum operational water level (meters). |
| 16 | + max_head (float): The maximum hydraulic head difference available for generation (meters). |
| 17 | + max_storage (float): The maximum storage capacity of the reservoir (m³). |
| 18 | + max_release (float): The maximum allowable daily release rate (m³/day). |
| 19 | + max_generation (float): The maximum power generation capacity (MW). |
| 20 | + turbine_factor (float): The efficiency factor of the turbine(s). |
| 21 | + inflow_ts (pd.Series): Timeseries of daily natural inflow into the reservoir (m³/day), |
| 22 | + indexed from 1 to sim_horizon. |
| 23 | + minflow_ts (pd.Series): Minimum environmental flow (m³/day), indexed from 1 to sim_horizon. |
| 24 | + upstream_units (list[str]): List of upstream reservoir names that feed into this reservoir. |
| 25 | + downstream_flow_fracs (dict[str, float]): Dictionary mapping downstream reservoir names to their |
| 26 | + respective flow fractions (0-1). |
| 27 | + """ |
| 28 | + |
| 29 | + name: str |
| 30 | + min_day: int |
| 31 | + max_day: int |
| 32 | + min_level: float |
| 33 | + max_level: float |
| 34 | + max_head: float |
| 35 | + max_storage: float |
| 36 | + max_release: float |
| 37 | + max_generation: float |
| 38 | + turbine_factor: float |
| 39 | + inflow_ts: pd.Series |
| 40 | + minflow_ts: pd.Series |
| 41 | + upstream_units: list[str] |
| 42 | + downstream_flow_fracs: dict[str, float] |
| 43 | + |
| 44 | + def __post_init__(self): |
| 45 | + """Perform basic validation after initialization.""" |
| 46 | + # Flow fractions of downstream units should sum to 1 |
| 47 | + if self.downstream_flow_fracs: |
| 48 | + if not 0.999 <= sum(self.downstream_flow_fracs.values()) <= 1.001: |
| 49 | + raise ValueError( |
| 50 | + f"Downstream units for {self.name} do not sum to 1: " |
| 51 | + f"{self.downstream_flow_fracs}" |
| 52 | + ) |
| 53 | + |
| 54 | + # Check that inflow and minflow timeseries are indexed correctly |
| 55 | + if not self.inflow_ts.index.equals(self.minflow_ts.index): |
| 56 | + raise ValueError( |
| 57 | + f"Inflows and minflows for {self.name} are not indexed the same: " |
| 58 | + f"{self.inflow_ts.index} vs {self.minflow_ts.index}" |
| 59 | + ) |
| 60 | + |
| 61 | + # Indexing starts at 1 |
| 62 | + if self.inflow_ts.index[0] != 1: |
| 63 | + raise ValueError( |
| 64 | + f"Inflows for {self.name} do not start at 1: {self.inflow_ts.index[0]}" |
| 65 | + ) |
| 66 | + if self.minflow_ts.index[0] != 1: |
| 67 | + raise ValueError( |
| 68 | + f"Minflows for {self.name} do not start at 1: {self.minflow_ts.index[0]}" |
| 69 | + ) |
| 70 | + |
| 71 | + # Inflow must be greater than minflow for all days |
| 72 | + if not all(self.inflow_ts >= self.minflow_ts): |
| 73 | + raise ValueError( |
| 74 | + f"Inflows for {self.name} are less than minflows on some days: " |
| 75 | + f"{(self.inflow_ts < self.minflow_ts).sum()} days" |
| 76 | + ) |
0 commit comments