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 — EnergySector is the single entry point other sectors call into via allocate_power().

  • PowerGenerator: Represents a single power generation unit (e.g., Solar, Nuclear). Each generator has a power_capacity_step (scaled by model.time_scale), an efficiency, and an availability factor (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 has max_operational_cap_kwh / min_operational_cap_kwh bounds, a current charge_level, and separate charge_efficiency / discharge_efficiency values, 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 the allocation_mode config key; defaults to PROPORTIONAL.

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_power or desync state.power_demanded from 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: PROPORTIONAL scales each sector’s allocation by its share of total demand; EQUAL calls _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 active pool sharing the remaining_power equally.

  • 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 from remaining_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 current power_demand plus the power needed to fully recharge every storage, using each storage’s own charge_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 each generate() 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_shortage are 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 as remaining_excess <= 0 (rather than continue-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

\[P_{\text{needed\_to\_charge}} = \sum_{i \in \text{storages}} \frac{C_{\text{max}, i} - C_{\text{current}, i}}{\eta_{\text{charge}, i}}\]
\[P_{\text{useful}} = P_{\text{demand}} + P_{\text{needed\_to\_charge}}\]

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

\[P_{\text{from\_gen}} = \min(P_{\text{demand}}, P_{\text{gen}})\]
\[\text{Demand}_{\text{remaining}} = P_{\text{demand}} - P_{\text{from\_gen}}\]
\[P_{\text{from\_storage}} = \text{Storages discharge to meet } \text{Demand}_{\text{remaining}}\]
\[P_{\text{supply}} = P_{\text{from\_gen}} + P_{\text{from\_storage}}\]
\[P_{\text{shortage}} = \max(0, P_{\text{demand}} - P_{\text{supply}})\]

4. Charge Storage With Excess

\[P_{\text{excess}} = \max(0, P_{\text{gen}} - P_{\text{demand}})\]
\[\text{Storages use } P_{\text{excess}} \text{ to charge, accounting for } \eta_{\text{charge}}, \text{ until } P_{\text{excess}} \text{ is exhausted.}\]

5. Max-Min Fair Share (EQUAL allocation mode)

For the active (unsatisfied) sector pool \(A\) sharing \(P_{\text{remaining}}\):

\[\text{share} = \frac{P_{\text{remaining}}}{|A|}\]

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: availability is 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.