Getting started#
Installation#
We recommend create an isolated environment with conda-forge compatible software first:
conda create -c conda-forge -n pwrsenv python=3.13
conda activate pwrsenv
pip install pwrs[all]
# or install the dev version
# pip install git+https://github.com/determ1ne/pwrs@dev
For OPF setup with external optimizers (for example, Ipopt), see Setup with external optimizers and the PowerModels-compatible OPF guide.
Load a built-in case#
Built-in MATPOWER cases are available from the top-level package for convenience.
Additionaly, Power Grid Lib - Optimal Power Flow
cases are available from pwrs.data.pglibopf.
import pwrs as mp
# MATPOWER case
mpc = mp.case9()
mpc = mp.data.matpower.case14()
# pglib-opf case
mpc = mp.data.pglibopf.pglib_opf_case14_ieee()
Run a power flow#
import pwrs as mp
mpc = mp.case9()
result = mp.runpf(mpc)
print(result.success)
print(result.bus)
See the Power flow guide for the workflows.
Run an optimal power flow#
import pwrs as mp
result = mp.runopf(mp.case30())
print(result.success)
See the Optimal power flow guide for the workflows.
Setup with external optimizers#
IPOPT#
You can install prebuilt binaries from conda-forge:
conda install -c conda-forge ipopt cyipopt
Coin-HSL support#
You may obtain or purchase the source code from the official store of Coin-HSL.
First, install the dependencies needed to build the HSL library:
# activate conda environment
conda activate pwrsenv
# install compiler and dependencies
conda install -c conda-forge gfortran pkg-config meson metis openblas pkg-config
Then compile the HSL library with prefix of the conda environment root:
cd coinhsl
# on Linux
meson setup builddir --buildtype=release --prefix=/YOUR_CONDA_ROOT/envs/pwrsenv
# on Windows
# meson setup builddir --buildtype=release --prefix=C:\YOUR_CONDA_ROOT\envs\pwrsenv --Dlibblas=openblas -Dliblapack=openblas
meson compile -C builddir
meson install -C builddir
Verify the installation by running an OPF with an HSL linear solver. The
POWER_MODELS backend is implemented with Pyomo/CVXPY in Python; it does not
start Julia or call PowerModels.jl.
import pwrs as mp
from pwrs.data.pglibopf import pglib_opf_case1354_pegase
mpc = pglib_opf_case1354_pegase()
mpopt = mp.MatpowerConfig()
mpopt.opf.backend = "POWER_MODELS"
mpopt.opf.power_models.formulation = "ACP"
mpopt.opf.power_models.solver = "IPOPT"
mpopt.ipopt.opts.update(
{
"linear_solver": "ma27",
"hsllib": "libcoinhsl.so", # or the path to your platform-specific library
"print_level": 5,
}
)
result = mp.runopf(mpc, mpopt)
The output should be similar to the following:
This is Ipopt version 3.14.19, running with linear solver ma27.