AGENT SKILL

PyPSA 电力系统建模分析

energysystems-pypsa

在 execute_energysystems_code 中使用 PyPSA 进行电力系统建模与分析——网络搭建、组件配置、潮流计算、最优调度、容量扩展、成本与拓扑分析。涵盖 PyPSA 约定以及会导致结果悄然出错的陷阱。

License 许可
MIT
最近更新
2026-11-14
Stars
★ 10
Forks
7

01 前言 Front Matter

name
energysystems-pypsa
description
Power system modeling and analysis with PyPSA inside execute_energysystems_code — network setup, components, power flow, optimal dispatch, capacity expansion, cost and topology analysis. Covers PyPSA conventions and the gotchas that cause silently wrong results.

03 SKILL.md 正文

Energy Systems / PyPSA

Use this skill when the user asks about power systems, electrical networks, generators, transmission lines, power flow, optimal dispatch, capacity planning, or renewable integration. Code runs in the execute_energysystems_code tool with PyPSA auto-imported.

The domain ships eight tools whose parameters and return values are described in their own schemas (visible through the tool interface). This skill covers what the schemas don't: PyPSA conventions, the non-obvious setup patterns, and the gotchas that produce silently wrong results.

Domain Tools

define_network
  └── add_components
        ├── run_power_flow
        ├── run_optimal_power_flow ──► analyze_costs
        ├── analyze_topology
        └── add_time_series ──► run_capacity_expansion

The tools are plain Python functions inside execute_energysystems_code. Write PyPSA directly when a task falls outside them. define_network returns a live pypsa.Network object; pass that object to the other tools in the same session.

Local Data Catalog

This server ships ready-made power-grid datasets. Search for one with the search_data MCP tool instead of building a network from scratch. Pass the returned opaque load_path unchanged into the execution environment:

# Paste the exact load_path string returned by search_data into this literal.
network = pypsa.Network("<blob>catalog-v1:...</blob>")

Agora Workbench resolves and fetches the underlying NetCDF object before the code runs. Do not attempt to inspect or reconstruct the catalog-v1: reference.

Auto-Imported Modules

Available without explicit imports:

import pypsa
import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

Critical: PyPSA Conventions

Bus-based topology — every component (generator, load, storage) attaches to a bus; lines connect bus pairs. Define buses before anything that references them, or the add silently fails.

Snapshots — time-series data must match the network's snapshot index length. Check len(n.snapshots) before attaching a profile; a length mismatch is the most common error.

Per-unit system — PyPSA uses per-unit for voltages (v_mag_pu), capacity factors (p_max_pu), and state of charge (state_of_charge). Physical units (MW, MVAr) are used for p_nom, p_set, s_nom.

Component Parameters

add_components takes lists of dicts. The keys that matter per component:

Component Key Parameters
Bus v_nom (kV)
Generator bus, p_nom (MW), marginal_cost (€/MWh), carrier, p_nom_extendable, capital_cost
Load bus, p_set (MW)
Line bus0, bus1, s_nom (MVA), x (pu), r (pu)
StorageUnit bus, p_nom (MW), max_hours, marginal_cost, capital_cost, p_nom_extendable

Tag generators with carrier ("coal", "gas", "wind", "solar") so analyze_costs can break costs down by technology.

Capacity Expansion: Extendable Components

To let the optimizer size a component, start it at zero capacity and mark it extendable with an annualized capital cost:

network = define_network(name="grid", snapshots=24)
add_components(
    network=network,
    generators=[{
        "name": "Wind", "bus": "Bus0", "carrier": "wind",
        "p_nom": 0,                # start with no capacity
        "p_nom_extendable": True,  # let the optimizer size it
        "capital_cost": 1000,      # €/MW/year annualized
        "marginal_cost": 0,
    }],
)

Then attach time-varying profiles with add_time_series (each profile dict: component_type, component_name, attribute, values) before run_capacity_expansion. Common time-varying attributes:

Component Attribute Meaning
Generator p_max_pu Capacity factor 0–1 (wind/solar profiles)
Load p_set Absolute demand (MW) per snapshot
StorageUnit inflow Natural inflow (MW), e.g. hydro

Power Flow: AC vs DC

Method Use Case Speed Accuracy
"ac" Voltage / reactive-power analysis Slower Full non-linear
"dc" Quick feasibility, large networks Fast Linear approximation

Solver

HiGHS is pre-installed and the default for optimization (OPF and capacity expansion) — no license required:

status, _ = n.optimize(solver_name="highs")

Gotchas

  • Generators need marginal_cost for OPF to be meaningful — one without it is treated as free and dispatched first.
  • Lines need s_nom > 0 for line-loading calculations.
  • Profile values length must exactly equal len(network.snapshots).
  • Use a full year of snapshots (8760) for realistic capacity-expansion results; a 24-hour horizon is fine for quick checks.

End-to-End Example

# Build a 2-bus network, solve OPF, break down costs by carrier
network = define_network(name="grid", snapshots=24)
add_components(
    network=network,
    buses=[{"name": "North"}, {"name": "South"}],
    generators=[
        {"name": "Gas", "bus": "North", "p_nom": 500, "marginal_cost": 50, "carrier": "gas"},
        {"name": "Wind", "bus": "South", "p_nom": 300, "marginal_cost": 0, "carrier": "wind"},
    ],
    loads=[{"name": "City", "bus": "South", "p_set": 400}],
    lines=[{"name": "N-S", "bus0": "North", "bus1": "South", "s_nom": 500, "x": 0.01}],
)
opf = run_optimal_power_flow(network=network)
costs = analyze_costs(network=network)
print(opf["status"], costs["total_cost"], costs["cost_by_carrier"])

04 同仓库更多 Skills

microsoft
microsoft/agora-workbench

多步骤工作流规划导航

workflow-planning

使用状态图规划和导航多步骤领域工作流。当任务涉及有序的工具依赖、多个状态转换,或不确定在领域序列中接下来应调用哪个工具时激活。

AI 工程师任务规划与分解
★ 10
microsoft
microsoft/agora-workbench

代码异步并行执行

async-execution

异步运行长时间或并行的代码。当代码运行时间超过 30 秒、处理大量独立输入,或用户要求在后台运行时激活。

软件开发工程师定时任务与自动化编排
★ 10
microsoft
microsoft/agora-workbench

数据资产传输与发布

artifacts

将数据资产获取到内核,在服务器之间传输 Python 对象,并将输出文件发布给用户。在处理数据文件、跨服务器数据移动,或用户要求下载或导出时激活。

数据工程师文件打包与导出
★ 10
microsoft
microsoft/agora-workbench

Agora Workbench MCP 代码执行

agora-workbench

通过编写在服务器持久内核中运行的 Python 来驱动 Agora Workbench MCP 服务器——即任何暴露 execute_*_code 工具的服务器,其工具与库都已内置于该内核中。当已连接 execute_*_code 工具且用户请求的工作可在其中运行时,应优先激活本技能,而不是求助于本地 shell、独立脚本或包安装。服务器已具备相应环境;应调用其工具,而非在本地重建该能力。

软件开发工程师开发环境与工具链管理
★ 10
microsoft
microsoft/agora-workbench

地球科学 STAC 数据获取

earthscience-stac

如何在 execute_earthscience_code 中查找并加载来自 Microsoft Planetary Computer(STAC)的数据。

数据工程师地球科学与地理空间数据分析
★ 10
microsoft
microsoft/agora-workbench

RDKit 分子分析与化学信息学

chemistry-rdkit

使用 RDKit 进行分子分析与化学信息学——通过领域工具和 execute_chemistry_code 工具实现 SMILES 处理、描述符计算、分子指纹、子结构搜索、相似性分析、聚类和类药性筛选。

化学研究员化学分析与化学信息学
★ 10