Datons Stories #15 - PNIEC Simulation | Energy Mix After Nuclear Shutdown

Simulating Spain's energy mix after nuclear plant closures under the PNIEC plan. ML models trained on real ESIOS data, three replacement scenarios, and an interactive Streamlit app.

February 10, 2026 2:32:36 ES
energynuclearpniecsimulationrenewablesenergy-mixmachine-learningstreamlit

Get weekly updates

Live coding sessions, Python libraries for energy data, and hands-on tutorials. One email per week, zero fluff.

Result

Streamlit app showing simulation results — generation projections per technology with editable inputs

An interactive Streamlit app that simulates Spain’s energy mix year by year as nuclear plants close following the PNIEC schedule. ML models trained on real ESIOS data predict demand and generation, while users adjust installed capacity per technology to explore replacement scenarios.

The problem

Official PNIEC document — installed capacity targets table showing nuclear phase-out to 2030

  • Spain’s PNIEC plans staggered closure of 7 nuclear reactors (7,399 MW) between 2027 and 2035
  • Nuclear provides ~20% of electricity generation with a capacity factor of 0.89 — the highest of any technology
  • Replacing 1 MW of nuclear requires ~5 MW of solar PV (factor 0.17) or ~3.7 MW of wind (factor 0.24)
  • Nuclear shutdown debates lack real data-driven simulations showing the concrete impact

Capacity factors

Capacity factor comparison slide — nuclear at 89%, solar at 20%, wind at 25%

The capacity factor measures what percentage of the time a plant generates at maximum output. It’s the key concept for understanding why replacing nuclear is not simply installing the same capacity in renewables.

TechnologyFactorHours equiv./year
Nuclear0.897,796 h
Wind0.242,102 h
Combined cycle0.201,752 h
Solar PV0.171,489 h
Hydro0.151,314 h

Nuclear generates almost 90% of the time. Solar only 17%. You need roughly 5x the installed capacity to fill the gap.

The solution

We started with historical generation and demand data downloaded from the ESIOS API (Red Electrica) to build predictive models and an interactive simulation tool.

ESIOS and PNIEC data

ESIOS dashboard showing Spain's real-time energy mix composition by technology

Downloaded historical time series of generation by technology and electricity demand from the ESIOS API. The data includes hourly production from nuclear, wind, solar, combined cycle gas, hydro, and all other technologies in Spain’s energy mix.

Data preparation

Data preparation notebook — PNIEC capacity data interpolated month by month with Pandas

The PNIEC data comes as installed capacity targets per technology at the annual level. With Pandas we interpolated these values month by month to obtain a continuous series to feed the models. We also created temporal variables (hour, day, month, weekend) as features for the model.

Machine learning models

We trained XGBoosting models to predict electricity demand and generation by technology. The train/test split was explained using an exam analogy: the model studies with one set of data (train) and takes an exam with data it has never seen (test). If it only memorized the study answers, it would fail the exam — that’s overfitting.

Model evaluation

XGBoost model evaluation — MAE of 1880 MWh and demand histogram

Models were evaluated with two main metrics:

  • MAE (Mean Absolute Error): average deviation in absolute terms — how many MW the prediction is off on average
  • RMSE (Root Mean Squared Error): penalizes large errors more heavily — useful for catching wildly off predictions

The models showed acceptable performance for scenario simulation, capturing seasonal and hourly generation patterns.

Generation forecast

Generation forecast to 2030 — nuclear drops from 7000 MW to 2300 MW following PNIEC schedule

With the trained models, we projected future generation following the PNIEC schedule. Nuclear drops progressively from 7,000 MW to 2,300 MW while other technologies must compensate for the deficit.

Streamlit application

Using ChatGPT Codex to refactor the project into a Streamlit app

We built an interactive app with Streamlit and ChatGPT assistance. We refactored the notebooks into a clean project structure with separate modules for data, models, and visualization. The app lets users:

  • Select a future year (2027-2035)
  • See which nuclear plants have closed by that year
  • Adjust installed capacity per technology
  • Simulate hourly generation and view the resulting mix
  • Compare against historical generation

Project structure

app
streamlit_app.py# Main Streamlit application
components# UI components
src/nuclear_energy
cli.py# Command-line interface
config.py# Configuration (YAML)
technology.py# Technology definitions
io
loaders.py# Data loading (ESIOS, PNIEC)
validators.py# Input validation
preprocessing
pniec.py# Monthly PNIEC interpolation
simulation
engine.py# Simulation engine
interpolation.py# Temporal interpolation
demand_adjustment.py# Demand adjustment
optimization
engine.py# Optimization engine (LP)
components.py# Model components
schema.py# Configuration schema
reporting
plots.py# Plotly charts
tables.py# Result tables
configs
default.yaml# Base configuration
technologies.yaml# Per-technology parameters
optimization# Optimization scenarios
tests
unit# Unit tests

Three replacement scenarios

We simulated three scenarios to fill the gap left by nuclear:

Scenario A — Renewables only: Install enough wind and solar to compensate for lost nuclear energy. The problem: intermittency. There are hours with excess generation and hours with deficits that cannot be covered.

Scenario B — Gas as backup (Germany approach): Combine renewables with combined cycle gas turbines as backup technology. Covers the deficit but increases CO2 emissions — exactly what happened in Germany after their nuclear shutdown.

Scenario C — Optimized mix: Find the optimal combination of renewables, storage, and gas that minimizes both the energy deficit and emissions. The most realistic scenario but also the most complex to model.

Reference

Nuclear plants in Spain

PlantCapacity (MW)Planned closure
Almaraz I1,0492027
Almaraz II1,0062028
Asco I1,0332030
Cofrentes1,0922030
Asco II1,0272032
Vandellos II1,0872035
Trillo1,0662035

Total: 7,399 MW — approximately 20% of Spain’s electricity generation.

Machine learning concepts used

ConceptDescription
XGBoostingGradient boosting algorithm based on decision trees
Train/test splitData division for training and evaluation
OverfittingModel that memorizes training data but fails to generalize
MAEMean Absolute Error — average prediction deviation
RMSERoot Mean Squared Error — penalizes large errors
Feature engineeringCreating variables (hour, day, month, temperature) from raw data

Stack

  • Python · Pandas · Plotly · XGBoost · Streamlit · ESIOS API · ChatGPT

Resources

Table of Contents
Search sections

Subscribe to our newsletter

Get weekly insights on data, automation, and AI.

© 2026 Datons. All rights reserved.