oemof 上手教程
关联项目:oemof
前置要求:Python 3.9+、能源系统建模基础;优化需求解器(cbc 开源)。
预计耗时:跑通示例约 1 小时。
官方仓库:github.com/oemof/oemof-solph
本教程带你完成从环境准备到跑通核心工作流的完整过程。命令与示例针对主流环境给出,实际版本参数请以官方文档为准。
一、环境准备
环境准备
python3 -m venv venv && source venv/bin/activate
# 开源求解器 cbc
# Ubuntu: sudo apt install coinor-cbc
二、安装部署
安装
pip install oemof.solph
求解器可选 cbc(开源)、Gurobi、CPLEX 等。
三、快速上手:跑通第一个示例
第一个能源系统模型
from oemof import solph
from oemof.tools import economics
# 创建能源系统
energysystem = solph.EnergySystem(
timeindex=solph.create_time_index(2024, number=24)
)
# 创建母线
electricity = solph.Bus(label="electricity")
energysystem.add(electricity)
# 添加源与汇
energysystem.add(solph.components.Source(
label="grid", outputs={electricity: solph.Flow(variable_costs=0.1)}))
energysystem.add(solph.components.Sink(
label="demand", inputs={electricity: solph.Flow(fix=[0.5]*24, nominal_value=1)}))
energysystem.add(solph.components.Source(
label="wind", outputs={electricity: solph.Flow(
nominal_value=1, variable_costs=0.01, max=[0.8]*24)}))
# 建模并求解
model = solph.Model(energysystem)
model.solve(solver='cbc')
# 结果
results = solph.processing.results(model)
print(results[(electricity, None)]['sequences'])
四、核心工作流
典型工作流:能源系统优化
- 定义系统:创建 EnergySystem 与时间索引。
- 添加组件:Bus、Source、Sink、Transformer、Storage。
- 设置能流:Flow 参数(容量、成本、效率、时间序列)。
- 建模求解:solph.Model + solve。
- 分析结果:processing.results 提取各能流结果。
- 情景对比:改变参数或结构,比较优化结果。
五、进阶实践
- 多能耦合:用 Bus 连接电、热、气网络,用 Transformer 建模转换。
- 存储建模:Storage 组件建模电池或蓄热。
- 非线性扩展:配合 oemof-thermal 做详细热力建模。
- 大规模:用 timeindex 聚合与并行求解。
六、常见问题
Q:oemof 与 PyPSA 区别? A:oemof 更通用(多能流),PyPSA 更聚焦电力系统优化。
Q:需要求解器吗? A:需要,cbc 开源免费。
Q:适合什么场景? A:综合能源系统、多能耦合、微网优化。
Q:学习难吗? A:概念较多,建议从示例入手。