Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task 12 — Energy Grid and Power Distribution Network

Objective

Create an energy-grid management system for a network of power producers, substations, transmission infrastructure, storage systems, regions, and consumers.

The system must manage:

  • power plants
  • generators
  • renewable generation
  • substations
  • transformers
  • transmission lines
  • distribution lines
  • energy storage
  • consumer regions
  • critical consumers
  • generation capacity
  • electricity demand
  • power allocation
  • network capacity
  • maintenance
  • failures
  • overloads
  • power rerouting
  • energy deficits
  • load shedding
  • reserve capacity
  • restoration
  • operational events
  • grid history

The implementation must continuously maintain the relationship between:

Generation
Demand
Network Capacity

while reacting to failures and changing conditions. This task models a simplified power network.

It is not intended to reproduce physical AC power-flow calculations.

The focus is software modeling, capacity allocation, network constraints, operational decisions, failure propagation, and recovery.

Grid Structure

Conceptually:

        Power Plant
             |
             v
        Generator
             |
             v
        Transmission Network
             |
             v
        Substation
             |
             v
        Transformer
             |
             v
        Distribution Network
             |
             v
        Consumer Region

Alternative paths may exist between substations.

This allows the system to reroute available power when a line fails.

Power Plant

A power plant contains one or more generators.

type PowerPlant struct {
    ID       string
    Name     string
    Type     PowerPlantType
    RegionID string
    Status   PlantStatus
}

Possible types:

  • nuclear
  • gas
  • coal
  • hydro
  • wind
  • solar

Possible states:

  • available
  • running
  • reduced
  • maintenance
  • failed
  • offline

Generator

A generator represents an individual generation unit.

type Generator struct {
    ID             string
    PowerPlantID   string
    MaximumOutputMW float64
    MinimumOutputMW float64
    CurrentOutputMW float64
    Status         GeneratorStatus
}

Possible states:

  • available
  • running
  • reduced
  • maintenance
  • failed
  • offline

Generation Rules

For an active generator:

MinimumOutputMW <= CurrentOutputMW <= MaximumOutputMW

A failed or offline generator must produce:

0 MW

unless a specific transitional state is explicitly modeled.

Renewable Generation

Wind and solar generators may have:

AvailableOutputMW

that changes according to environmental conditions. For example:

Solar Maximum = 120 MW
Available Now  = 65 MW

The system cannot schedule:

100 MW

from that generator at that moment.

Substation

Substations connect sections of the network.

type Substation struct {
    ID       string
    Name     string
    RegionID string
    Status   SubstationStatus
}

Possible states:

  • available
  • operational
  • maintenance
  • failed
  • isolated

Transformer

Transformers have limited transfer capacity.

type Transformer struct {
    ID            string
    SubstationID  string
    CapacityMW    float64
    CurrentLoadMW float64
    Status        TransformerStatus
}

The invariant is:

CurrentLoadMW <= CapacityMW

during normal operation.

Transmission Line

A transmission line connects two grid nodes.

type TransmissionLine struct {
    ID          string
    FromNodeID  string
    ToNodeID    string
    CapacityMW  float64
    CurrentFlowMW float64
    Status      LineStatus
}

Possible states:

  • available
  • active
  • maintenance
  • failed
  • isolated

For this exercise, lines may be treated as bidirectional unless explicitly configured otherwise.

Transmission Capacity

For every active line:

abs(CurrentFlowMW) <= CapacityMW

The system must not reroute power through a path whose capacity would be exceeded.

Consumer Region

A consumer region represents aggregated electricity demand.

type ConsumerRegion struct {
    ID            string
    Name          string
    DemandMW      float64
    Priority      ConsumerPriority
    SuppliedMW    float64
}

Possible priority levels:

  • critical
  • high
  • normal
  • low

Critical Consumers

A region may contain critical consumers. Examples:

  • hospital
  • emergency service
  • water treatment
  • telecommunications
  • data center
  • transport infrastructure

A possible model is:

type CriticalConsumer struct {
    ID              string
    RegionID        string
    Name            string
    MinimumSupplyMW float64
    Priority        int
}

Critical minimum supply should be protected before lower-priority discretionary demand when load shedding becomes necessary.

Demand

Demand changes over time. Create:

type DemandSnapshot struct {
    Timestamp time.Time
    RegionID  string
    DemandMW  float64
}

The grid should be able to process a complete demand snapshot for all regions.

Generation Snapshot

Similarly:

type GenerationSnapshot struct {
    Timestamp        time.Time
    GeneratorID      string
    AvailableOutputMW float64
    CurrentOutputMW  float64
}

Base Grid

Create the following simplified grid. Power plants:

PP01 — North Nuclear
PP02 — West Gas
PP03 — River Hydro
PP04 — East Wind
PP05 — South Solar

Installed generation:

PP01 = 400 MW
PP02 = 220 MW
PP03 = 180 MW
PP04 = 140 MW
PP05 = 120 MW

Total installed generation:

1060 MW

Actual available generation may be lower.

Substations

Create:

S01
S02
S03
S04
S05
S06
S07
S08

Transmission Network

Create links:

S01 <-> S02
S01 <-> S03
S02 <-> S04
S02 <-> S05
S03 <-> S05
S03 <-> S06
S04 <-> S07
S05 <-> S07
S05 <-> S08
S06 <-> S08
S07 <-> S08

This topology provides multiple alternative routes.

Example Transmission Capacities

Use:

S01-S02 = 250 MW
S01-S03 = 220 MW
S02-S04 = 160 MW
S02-S05 = 180 MW
S03-S05 = 150 MW
S03-S06 = 170 MW
S04-S07 = 140 MW
S05-S07 = 160 MW
S05-S08 = 180 MW
S06-S08 = 150 MW
S07-S08 = 120 MW

Consumer Regions

Create six regions:

R01 — North
R02 — West
R03 — Central
R04 — East
R05 — South
R06 — Metropolitan

Example demand:

R01 = 120 MW
R02 = 135 MW
R03 = 180 MW
R04 = 110 MW
R05 = 125 MW
R06 = 250 MW

Total demand:

920 MW

Available Generation Scenario

Assume current available generation is:

PP01 = 360 MW
PP02 = 170 MW
PP03 = 150 MW
PP04 = 95 MW
PP05 = 75 MW

Total:

850 MW

Demand:

920 MW

Therefore:

Deficit = 70 MW

The system must determine how to respond to the deficit.

Generation and Demand Balance

Conceptually:

Generation + Storage Discharge
=
Supplied Demand + Storage Charge + Unallocated Surplus

The implementation should produce a balance report.

type GridBalance struct {
    TotalGenerationMW float64
    TotalDemandMW     float64
    TotalSuppliedMW   float64
    DeficitMW         float64
    SurplusMW         float64
}

Reserve Capacity

Some generation may be available but not currently active.

type GenerationReserve struct {
    GeneratorID       string
    AvailableReserveMW float64
    ActivationTime    time.Duration
}

Before load shedding, the system should attempt to use eligible reserve capacity according to the configured policy.

Energy Storage

Create storage systems.

type EnergyStorage struct {
    ID                string
    NodeID            string
    CapacityMWh       float64
    StoredEnergyMWh   float64
    MaxChargeMW       float64
    MaxDischargeMW    float64
    Status            StorageStatus
}

Possible states:

  • available
  • charging
  • discharging
  • maintenance
  • failed

Storage Constraints

The system must preserve:

0 <= StoredEnergyMWh <= CapacityMWh

and:

ChargeRate <= MaxChargeMW
DischargeRate <= MaxDischargeMW

Storage cannot discharge more energy than it currently contains.

Surplus Scenario

If:

Generation = 980 MW
Demand     = 900 MW

then:

Surplus = 80 MW

The system may:

  • charge storage
  • reduce generation
  • leave remaining surplus unallocated

according to configured rules.

Deficit Resolution

When demand exceeds generation, use the following conceptual order:

1. Increase available generation
2. Activate reserve generation
3. Discharge available storage
4. Reroute power where network constraints prevent delivery
5. Apply load shedding if deficit remains

Every decision must be recorded.

Load Shedding

Load shedding intentionally reduces supplied demand. Create:

type LoadSheddingAction struct {
    RegionID       string
    RequestedMW    float64
    ShedMW         float64
    RemainingMW    float64
    Reason         string
}

Load-Shedding Priority

For the base task, protect demand in this order:

critical
high
normal
low

Shedding should begin with:

low

priority demand. Within equal priority:

higher available shed capacity first

and then:

lower RegionID

for deterministic behavior.

Critical minimum supply should not be shed while lower-priority reducible demand remains available.

Load-Shedding Scenario

Suppose the unresolved deficit is:

70 MW

and available reducible demand is:

R05 — low    — 40 MW reducible
R04 — normal — 30 MW reducible
R03 — normal — 50 MW reducible
R06 — high   — 25 MW reducible

The system should first shed:

R05 = 40 MW

Remaining deficit:

30 MW

Then use eligible normal-priority demand according to the deterministic rule.

The final report must identify exactly where the 70 MW reduction occurred.

Network Reachability

Generation existing somewhere in the grid does not automatically mean it can reach every consumer.

The implementation must evaluate:

  • network connectivity
  • line status
  • line capacity
  • transformer capacity

A region may experience a local deficit even when total grid generation is sufficient.

Power Route

A simplified power route may be represented as:

type PowerRoute struct {
    Nodes       []string
    AllocatedMW float64
}

A route is valid only if every line on the path can accept the additional allocation.

Rerouting

When a transmission line fails:

remove that line from available topology

and determine whether affected power can be routed through alternative paths. For example:

S02 -> S05

fails. Possible alternatives may include:

S02 -> S01 -> S03 -> S05

or:

S02 -> S04 -> S07 -> S05

The implementation must verify capacity along the complete alternative route.

Bottleneck Capacity

For a path:

S02 -> S01 -> S03 -> S05

with available capacities:

S02-S01 = 80 MW
S01-S03 = 120 MW
S03-S05 = 45 MW

the maximum additional transfer through the path is:

45 MW

The path capacity is limited by its bottleneck.

Generator Failure

Create a failure scenario:

PP01 / Generator G01 fails

Lost production:

180 MW

The system must determine:

  • new total generation
  • new deficit
  • available reserve
  • available storage
  • network ability to redistribute power
  • required load shedding

Failure Propagation

A failure may affect more than one entity. Example:

        Generator Failure
                |
                v
        Generation Deficit
                |
                v
        Higher Flow on Alternative Lines
                |
                v
        Transmission Capacity Reached
                |
                v
        Local Deficit
                |
                v
        Load Shedding

The implementation must not stop analysis after detecting the first failure.

Transmission Line Failure

Create:

type GridFailure struct {
    ID         string
    EntityType string
    EntityID   string
    Timestamp  time.Time
    Reason     string
}

When a line fails:

Status = failed
CurrentFlowMW = 0

Existing allocations depending on the line must be reconsidered.

Overload Detection

An overload exists when a proposed or current flow exceeds allowed capacity.

CurrentFlowMW > CapacityMW

The system should detect overload before accepting a new allocation whenever possible.

Overload Resolution

Possible actions:

  • reduce transfer
  • use alternative route
  • increase local generation
  • discharge local storage
  • shed load

The system must record which action was selected.

Cascading Failure Scenario

Create a controlled simulation:

TL-05 fails

Its previous flow is redistributed. This causes:

TL-08

to exceed capacity. The system must:

  • detect the overload
  • prevent or isolate invalid flow
  • recalculate available routes
  • determine remaining supply
  • apply load shedding if required

The simulation should be bounded.

Do not implement an uncontrolled infinite failure cascade.

Maintenance

Grid components may enter planned maintenance. Examples:

- generator
- transformer
- transmission line
- storage system

Create:

type MaintenanceWindow struct {
    ID         string
    EntityType string
    EntityID   string
    StartTime  time.Time
    EndTime    time.Time
    Status     MaintenanceStatus
}

Maintenance Planning

Before approving maintenance, calculate whether the grid can still satisfy required demand.

The report should identify:

- lost capacity
- alternative generation
- alternative routes
- reserve requirements
- expected deficit
- expected load shedding

Maintenance should not be silently approved if it would violate configured critical-supply requirements.

Transformer Failure

If a transformer fails, downstream demand may become unreachable. The system must determine:

  • affected regions
  • affected critical consumers
  • alternative transformer path if one exists
  • unsupplied demand

Critical Supply

Critical consumers define:

MinimumSupplyMW

Example:

Hospital Complex = 12 MW
Water Treatment   = 8 MW
Telecom Core      = 5 MW

During severe deficit:

25 MW

must be protected for these consumers before discretionary lower-priority load.

Blackout

A consumer region enters blackout when:

SuppliedMW = 0

A partial supply reduction is:

brownout / load shedding

for the purposes of this exercise.

Islanding

A failure may split the grid into disconnected components. Example:

        Grid before failure:
        
            A --- B --- C
                  |
                  D
        
        After failures:
        
            A --- B
            
            C --- D

The system must detect independent connected components.

Each component must independently evaluate:

  • local generation
  • local demand
  • local storage
  • local deficit

A surplus in one island cannot supply another disconnected island.

Restoration

When failed infrastructure becomes available again, restoration should occur explicitly. Conceptually:

        component repaired
              |
              v
        topology restored
              |
              v
        capacity recalculated
              |
              v
        load shedding reduced
              |
              v
        normal supply restored

Restoration should prioritize:

  • critical
  • high
  • normal
  • low

consumers.

Restoration Event

type RestorationAction struct {
    RegionID       string
    RestoredMW     float64
    PreviousSupplyMW float64
    NewSupplyMW    float64
}

Grid Snapshot

The system should produce a complete snapshot.

type GridSnapshot struct {
    Timestamp          time.Time
    GenerationMW       float64
    DemandMW           float64
    SuppliedMW         float64
    StorageEnergyMWh   float64
    ActiveFailures     int
    OverloadedLines    []string
    UnsuppliedRegions  []string
}

Historical Metrics

Store snapshots over time. This allows queries such as:

  • peak demand
  • minimum generation
  • largest deficit
  • maximum storage usage
  • number of failures
  • total shed energy

Operational Event

Create:

type GridEvent struct {
    ID         string
    Timestamp  time.Time
    EntityType string
    EntityID   string
    EventType  string
    Details    string
}

Examples:

  • generator started
  • generator failed
  • line overloaded
  • line failed
  • storage discharge started
  • reserve activated
  • load shedding started
  • region restored
  • maintenance started
  • maintenance completed

Idempotency

Important commands should contain:

RequestID

Examples:

  • activate reserve
  • change generator output
  • start maintenance
  • report failure
  • restore component
  • execute load shedding

Processing the same request twice must not:

  • activate reserve twice
  • shed the same load twice
  • double-count generation
  • duplicate failure events
  • restore capacity twice

Grid Invariants

The implementation should preserve:

        generator output <= available generator capacity
        
        line flow <= active line capacity
        
        transformer load <= transformer capacity
        
        storage energy <= storage capacity
        
        storage energy >= 0
        
        supplied regional demand <= requested regional demand
        
        failed line carries no power
        
        failed generator produces no power
        
        disconnected islands cannot exchange power
        
        critical minimum supply is protected according to configured policy
        
        duplicate commands do not duplicate operational effects

Query Operations

Support:

  • get current grid balance
  • get power plant status
  • get generator status
  • get regional demand
  • get regional supply
  • get transmission-line load
  • get transformer load
  • get storage state
  • get active failures
  • get active maintenance
  • get overloaded components
  • get load-shedding actions
  • get critical consumer status
  • find alternative route
  • get connected grid components
  • get grid snapshot
  • get historical metrics

Queries must not modify grid state.

Command Operations

State-changing operations include:

  • set generator output
  • activate reserve generation
  • start storage charging
  • start storage discharge
  • report generator failure
  • report line failure
  • report transformer failure
  • restore component
  • schedule maintenance
  • start maintenance
  • complete maintenance
  • reroute allocation
  • execute load shedding
  • restore shed load

Validation

Validate:

  • duplicate IDs
  • unknown node
  • unknown plant
  • unknown generator
  • unknown region
  • unknown transmission line
  • unknown transformer
  • unknown storage system
  • negative demand
  • negative generation
  • negative capacity
  • generation above available capacity
  • flow above line capacity
  • transformer overload
  • storage overcharge
  • storage over-discharge
  • invalid maintenance interval
  • allocation through failed component
  • duplicate RequestID

Required Test Scenarios

Create tests for at least:

  • balanced generation and demand
  • generation surplus
  • generation deficit
  • reserve activation
  • storage charging
  • storage discharge
  • storage capacity limit
  • load shedding
  • critical consumer protection
  • deterministic shedding order
  • successful alternative route
  • route bottleneck calculation
  • no alternative route
  • generator failure
  • transmission-line failure
  • transformer failure
  • local deficit despite global generation surplus
  • overload detection
  • maintenance capacity analysis
  • maintenance rejection
  • grid island detection
  • independent island balancing
  • cascading overload scenario
  • component restoration
  • load restoration
  • duplicate failure request
  • duplicate load-shedding request
  • grid snapshot
  • historical metrics

Large Grid Scenario

Create a simulation containing at least:

  • 5 power plants
  • 10 generators
  • 8 substations
  • 12 transformers
  • 15 transmission lines
  • 6 consumer regions
  • 10 critical consumers
  • 3 storage systems

Simulate the following sequence:

1. Normal grid operation
2. Demand increases by 12%
3. Renewable generation falls
4. Reserve generator activates
5. Major transmission line fails
6. Power is rerouted
7. Alternative line reaches capacity
8. Storage begins discharging
9. Remaining deficit requires load shedding
10. Critical consumers remain protected
11. Failed line is repaired
12. Normal topology is restored
13. Shed demand is progressively restored
14. Storage begins recovery charging

After every step, generate a new:

GridSnapshot

and verify all grid invariants.

Modeling Goal

The purpose of this task is to model a distributed resource network where total capacity alone does not determine whether demand can be satisfied.

A useful conceptual architecture is:

        Generation Service
              |
              +-- Power Plants
              +-- Generators
              +-- Renewable Availability
              +-- Reserve Capacity
              |
              v
        Grid Topology
              |
              +-- Substations
              +-- Transformers
              +-- Transmission Lines
              |
              v
        Demand Service
              |
              +-- Consumer Regions
              +-- Critical Consumers
              +-- Demand Snapshots
              |
              v
        Grid Balancer
              |
              +-- Generation Allocation
              +-- Storage
              +-- Deficit Detection
              +-- Surplus Handling
              |
              v
        Network Allocation
              |
              +-- Route Capacity
              +-- Bottlenecks
              +-- Rerouting
              +-- Island Detection
              |
              v
        Grid Protection
              |
              +-- Overload Detection
              +-- Failure Handling
              +-- Load Shedding
              +-- Critical Supply
              |
              v
        Recovery
              |
              +-- Component Restoration
              +-- Load Restoration
              +-- Storage Recovery
              |
              v
        Operational History

The main challenge is understanding that:

enough total generation

does not necessarily mean:

every consumer can receive enough power

because power delivery is constrained by topology, component state, transfer capacity, storage availability, and failures.

Scalionix Docs

Keyboard Shortcuts

Navigate the documentation without leaving the keyboard.
Navigation
Previous subject
←
Next subject
→
Previous subsection
Alt + ↑
Next subsection
Alt + ↓
Interface
Documentation Home
Ctrl + Enter
Search
Alt + Q
Open shortcuts
?
Close dialog
Esc
Scalionix Docs

Search Documentation