Optimal Power Flow (OPF)

pandapower

Optimal Power Flow finds a least-cost generation dispatch that still respects voltage limits, thermal limits, and generator capability. Unlike load flow, the solver is allowed to change dispatch (within your costs and limits).

Where to find it

Simulate → Optimal Power Flow (pandapower). Set costs and limits on generators, and voltage bands on buses, before you run.

When to use

  • Least-cost dispatch while serving load
  • Checking whether a target dispatch is feasible with voltage and thermal limits
  • Comparing AC OPF (more accurate) with DC OPF (faster, active power only)

How to run

  1. Fill generator costs (or accept dialog defaults) and bus/line limits used by OPF.
  2. Click Simulate → Optimal Power Flow.
  3. Choose AC or DC OPF and run.
  4. Read the new dispatch on generators and the flows that result.
AC vs DC OPF

AC OPF is a full nonlinear optimisation (P, Q, voltage magnitude and angle). DC OPF is a linearised active-power approximation, faster and typical for transmission screening. The solver uses mathematical programming (interior point / SQP-style methods) to minimise cost subject to the network constraints.

Dialog parameters

Parameter Description Type Options/Value Range Default
OPF Type Type of optimal power flow calculation radio AC Optimal Power Flow (runopp), DC Optimal Power Flow (rundcopp) AC OPF
Frequency System frequency radio 50 Hz, 60 Hz 50 Hz
AC Algorithm Solver for AC OPF radio PYPOWER PYPOWER
DC Algorithm Solver for DC OPF radio PYPOWER PYPOWER
Calculate Voltage Angles Whether to calculate voltage angles radio Auto, True, False Auto
Initialization Initial guess strategy radio Power Flow, Flat, Previous Results Power Flow
Delta (Convergence Tolerance) Optimization convergence tolerance number > 0 1e-8
Transformer Model Transformer representation in optimization radio Exact Model (t), Pi Model (pi) Exact Model
Cost function type Generator cost model for the objective radio Polynomial, Piecewise linear, No cost function No cost function
Marginal cost currency Display units for marginal cost fields (numeric values unchanged) select EUR, USD, GBP, CHF, PLN, SEK, NOK, DKK, JPY, CNY, INR, AUD, CAD, Other EUR

Objective Function

The default objective function minimizes total generation cost, which is typically defined as:

Minimize: Σ (Costi × Pgen,i) for all generators i

Where Costi is the cost coefficient for generator i and Pgen,i is the active power output. Cost functions can be linear, quadratic, or piecewise linear.

Polynomial versus piecewise linear (Electrisim)

In the OPF dialog, Cost function type selects how generator operating costs are modeled before calling pandapower:

  • Polynomial — Each synchronous generator gets a smooth cost in active power: by default a small positive quadratic term (convexity / curvature) plus a marginal linear rate that you set per machine (OPF run dialog and/or the generator’s OPF tab as marginal cost per MWh). This matches pandapower poly_cost (cp2_eur_per_mw2, cp1_eur_per_mw, cp0_eur). It is usually the most robust choice for AC OPF.
  • Piecewise linear — Cost is linear on each power segment; Electrisim builds a single segment between each generator’s minimum and maximum OPF active power, with slope equal to your marginal rate (same interpretation as the linear term in the polynomial case). This matches pandapower pwl_cost. Use it when you think of marginal price as constant between min and max P; multi-segment tariffs would require additional segments (not exposed individually in the UI).
  • No cost function — No polynomial/PWL costs are created for economic minimization; the OPF can still converge but economic dispatch is not driven by generator marginal costs.
Cost curve shape (schematic)

Same axes in both panels: horizontal axis is generator active power P over the dispatch range; vertical axis is operating cost contribution C (arbitrary units). Electrisim uses one linear segment for piecewise-linear cost between each machine’s OPF min/max P.

Polynomial versus piecewise-linear generation cost curves Two plots: left shows a smooth polynomial cost versus power with increasing slope; right shows a straight segment with constant slope. Polynomial (smooth) P (MW) Cost C ∂C/∂P increases Piecewise linear (one segment) P (MW) Cost C ∂C/∂P constant

Left: A positive quadratic term makes marginal cost rise with output—useful for differentiable models or rising incremental heat rates. Right: A single segment fixes marginal price over the whole OPF band (simple block tariff).

Currency — The OPF dialog lets you choose a marginal cost currency for labels only. Numerical values are passed to the solver as-is; pandapower still names internal cost columns with eur for historical reasons. Interpret objective values (e.g. total_cost) in the same unit system you used when typing marginal costs.

Constraints

The OPF solution must satisfy:

  • Power Balance: Total generation equals total load plus losses
  • Generator Limits: Pmin ≤ Pgen ≤ Pmax, Qmin ≤ Qgen ≤ Qmax
  • Voltage Limits: Vmin ≤ V ≤ Vmax at each bus using net.bus.min_vm_pu / max_vm_pu from the diagram (see Bus)
  • Thermal Limits: Branch loading respects element ratings; optional OPF caps — set max_loading_percent on lines, two-winding, or three-winding transformers. A value > 0 adds a pandapower OPF thermal limit for that element; 0 leaves the default unconstrained behaviour.
  • Reactive Power Limits: Generator reactive power capability constraints

Output Results

The OPF results include optimized dispatch and system state:

Output Category Parameters Description
Optimization Status Status, Success flag Whether optimization converged successfully
Total Cost Objective function value Minimum total generation cost achieved (monetary units)
Generator Dispatch P_opt, Q_opt Optimal active and reactive power output for each generator
Bus Voltages vm_pu, va_degree Optimal voltage magnitudes and angles at all buses
Power Flows P_from, Q_from, P_to, Q_to, Loading Optimal power flows and loading of lines and transformers
Marginal Costs Lambda_P, Lambda_Q Shadow prices (marginal costs) for active and reactive power at each bus
Constraint Violations Violation details Information about any constraint violations if optimization failed

Prerequisites

  • OPF-capable network: slack/external grid, generator and storage models compatible with pandapower OPF.
  • Bus voltage bounds (min_vm_pu, max_vm_pu) should bracket a feasible region.
  • For meaningful objective values, define polynomial or piecewise costs—Electrisim can auto-create defaults from the dialog Cost function type when no costs exist.

Diagram-level inputs

  • Generators: active/reactive limits and any OPF-specific fields exposed in element dialogs.
  • Storage: optimisation-tab limits map into pandapower OPF constraints.
  • Buses: voltage set-points and min/max magnitudes bound the OPF solution.

API response (successful OPF)

The route returns JSON with:

Key Contents
opf_converged Whether pandapower reported successful optimisation (net.OPF_converged).
total_cost net.res_cost objective scalar when converged.
busbars Voltages, injections, and OPF duals lam_p, lam_q when available.
lines Flows, currents, loading, optional thermal duals mu_sf/mu_st.
generators Dispatch plus gen_cost/marginal_cost estimated from polynomial costs.
externalgrids, loads Final injections consistent with the OPF solution.

Failures respond with error: true, a descriptive message, exception text, and optional pandapower diagnostic payloads.

Note: PandaModels.jl / PowerModels.jl OPF paths referenced in pandapower docs are not wired through Electrisim; only PYPOWER-style runopp/rundcopp are available.

Algorithm Details

AC OPF: Solves the full nonlinear optimization problem using Interior Point Method (IPM) or Sequential Quadratic Programming (SQP). Provides accurate results but requires significant computation time for large networks.

DC OPF: Uses a linearized power flow model (ignoring losses and reactive power). Much faster than AC OPF but less accurate. Suitable for transmission planning where reactive power is less critical.

Solver: PYPOWER, which is based on MATPOWER and uses MATLAB optimization solvers (converted to Python).

Limitations and Considerations

Important Notes:

  • OPF assumes perfect information and deterministic conditions - does not account for uncertainty
  • Cost functions must be defined for generators (linear or quadratic coefficients)
  • AC OPF may fail to converge for heavily loaded or ill-conditioned systems
  • DC OPF ignores reactive power, losses, and voltage constraints
  • Results represent steady-state optimal dispatch - dynamic constraints are not considered
  • Large networks may require significant computation time, especially for AC OPF
  • Market rules and regulatory constraints may not be fully represented

Technical References

Based on: pandapower Optimal Power Flow and PYPOWER.

Algorithm Type: Nonlinear/Linear optimization with Interior Point Method or Sequential Quadratic Programming.

Documentation References: 📘 pandapower Optimal Power Flow 📘 PYPOWER OPF

Last updated: October 2026

Electrisim on YouTube · Frontend on GitHub