Energy Sector¶
Purpose: The EnergySector serves as the central power authority for the entire simulation. Its sole responsibility is to manage the generation, storage, and distribution of electrical power to meet the aggregate demand from all other sectors.
Core Components:
EnergySector: Owns the fleet of generators and storages directly and performs the per-step power allocation. There is no separate microgrid/manager class —EnergySectoris the single entry point other sectors call into viaallocate_power().PowerGenerator: Represents a single power generation unit (e.g., Solar, Nuclear). Each generator has apower_capacity_step(scaled bymodel.time_scale), anefficiency, and anavailabilityfactor (e.g., a solar panel’s availability might be affected by the lunar day/night cycle).PowerStorage: Represents a battery or other storage unit. It hasmax_operational_cap_kwh/min_operational_cap_kwhbounds, a currentcharge_level, and separatecharge_efficiency/discharge_efficiencyvalues, simulating real-world energy losses.AllocationMode: Enum (PROPORTIONAL,EQUAL) selecting how scarce power is divided among requesting sectors when total demand exceeds what’s available. Set via theallocation_modeconfig key; defaults toPROPORTIONAL.
Operational Cycle & Key Algorithms¶
A. Demand Clamping & Dispatch (allocate_power)
allocate_power() is the entry point called with each sector’s raw power demand for the step.
Demands are first clamped to non-negative values, so a caller passing a negative number can’t inflate
available_poweror desyncstate.power_demandedfrom what’s actually handed out.The clamped total demand is passed into
step(), which generates/discharges power and returns how much is actually available.If available power covers total demand, every sector gets exactly what it asked for.
Otherwise (scarcity), the shortfall is divided according to
allocation_mode:PROPORTIONALscales each sector’s allocation by its share of total demand;EQUALcalls_equal_split().
B. Max-Min Fair Share Split (_equal_split)
Rather than naively dividing available_power by the sector count (which wastes power on sectors with little or no demand), _equal_split() performs iterative max-min fair-share redistribution:
All sectors with positive demand start in an
activepool sharing theremaining_powerequally.Any sector whose demand is fully covered by its equal share is marked
satisfied: its full demand is locked into its allocation, that amount is subtracted fromremaining_power, and it’s removed from the pool.The loop repeats, recomputing the equal share among whoever’s left, until either the pool is empty or no one is satisfied at the current share (in which case the remaining power is split evenly across the still-active pool and the loop ends).
This means a sector’s leftover unmet share is never stranded — it’s redistributed to sectors that can still use more.
C. Power Need & Generation (step)
The sector first computes
max_useful_power: the currentpower_demandplus the power needed to fully recharge every storage, using each storage’s owncharge_efficiency(not a fixed constant).Generators are asked to produce up to
max_useful_power, stopping as soon as the remaining useful power need drops to zero or below (checked before eachgenerate()call, so no generator is invoked when nothing more is needed).Freshly generated power is applied to demand first; any shortfall is drawn from storage discharge.
state.power_supplied/state.power_shortageare set from the result.
D. Charging Excess Generation (step)
Any generation beyond what demand consumed is excess power used to recharge storages in sequence.
The loop
breaks as soon asremaining_excess <= 0(rather thancontinue-ing through the remaining storages), and skips (continue) any storage already at full capacity.
Equations¶
Variables:
\(P_{\text{demand}}\): The total (clamped, non-negative) power demand for the current step.
\(C_{\text{max}, i}\), \(C_{\text{current}, i}\): Max and current charge of storage (i).
\(\eta_{\text{charge}, i}\), \(\eta_{\text{discharge}, i}\): The charge/discharge efficiency of storage (i).
\(P_{\text{gen}}\): Total power generated by all generators in the step.
\(P_{\text{supply}}\): The final power supplied to the grid.
1. Calculate Total Power Need
2. Generate Power
Generators produce power up to (P_{\text{useful}}), stopping once the remaining useful need is (\le 0).
3. Meet Demand & Calculate Shortfall
4. Charge Storage With Excess
5. Max-Min Fair Share (EQUAL allocation mode)
For the active (unsatisfied) sector pool \(A\) sharing \(P_{\text{remaining}}\):
Sectors with \(\text{Demand}_{\text{sector}} \le \text{share}\) are satisfied — their full demand is allocated and they leave \(A\) — and the process repeats on the remainder of \(A\) with the recomputed \(P_{\text{remaining}}\), until \(A\) is empty or no sector qualifies at the current share.
Configuration Options¶
The EnergySector is built from an energy component list in the world_system JSON, which the EnergySectorBuilder splits into generators and storages:
{
"sector_name": "energy",
"allocation_mode": "proportional",
"generators": [
{
"subtype": "solar",
"config": {
"power_capacity_kwh": 100,
"efficiency": 0.8,
"availability": 0.5
},
"quantity": 10
}
],
"storages": [
{
"subtype": "Li_Battery",
"config": {
"min_operational_cap_kwh": 3,
"max_operational_cap_kwh": 100,
"initial_charge_kwh": 20,
"charge_efficiency": 0.95,
"discharge_efficiency": 0.98
},
"quantity": 5
}
]
}
TODO: Potential Improvements¶
[ ] Generator Availability Modeling:
availabilityis a static config value rather than something that varies with simulation time (e.g., actual lunar day/night cycle), so solar generators don’t currently lose output at “night” on their own.[ ] Equal-Split Tie-Breaking: When
_equal_split()hits the “nobody fits in an equal share” branch, the remaining power is split evenly regardless of relative demand size; a partial-proportional fallback could be considered if this mode needs fairness in that edge case too.