PYPOWER Documentation

repository·master·Indexed 19 days ago

https://github.com/rwl/pypower

A Python port of MATPOWER providing solvers for DC and AC power flow (Newton's method & Fast Decoupled) and DC and AC Optimal Power Flow (OPF). It utilizes SciPy for computations and provides both a Python API via pypower.api and command-line interfaces (pf and opf) for solving power flow problems using built-in test cases or custom case data files. Supports optional integration with IPOPT and LP Solve for specific OPF problems.

Tokens
5.5K
Snippets
15
Records
18
Agent score
60%

What's inside PYPOWER

  1. Overview of PYPOWER features

    master

    PYPOWER is a power flow and Optimal Power Flow (OPF) solver. It is a Python translation of MATPOWER that utilizes SciPy for its computations.

    Key capabilities include:

    • Power Flow: Supports both DC and AC (using Newton's method and Fast Decoupled methods).
    • Optimal Power Flow (OPF): Supports both DC and AC formulations.
  2. Install PYPOWER from a source tarball

    master

    To install from a downloaded tarball, unpack the archive and run the setup.py script. On UNIX systems, you may need to prefix the command with sudo if installing to a directory requiring root privileges.

    $ tar zxf PYPOWER-4.0.tar.gz
    $ python setup.py install
    
    # On UNIX systems requiring root privileges:
    $ sudo python setup.py install
  3. Test PYPOWER locally

    master

    You can run the PYPOWER test suite locally using tox. Ensure you have a virtual environment active with the necessary dependencies installed.

    venv/bin/python -m tox -e py27,py38

    (Note: Replace py27,py38 with the Python versions supported by your environment.)

    venv/bin/python -m tox -e py27,py38
  4. Install IPOPT for AC and DC OPF problems

    master

    IPOPT can be used to solve AC Optimal Power Flow (OPF) problems and DC OPF problems with quadratic cost functions.

    To use IPOPT with PYPOWER, you must install the IPOPT solver and its Python interface, PyIPOPT.

    1. Install IPOPT and MUMPS

    IPOPT requires a linear solver; MUMPS is the recommended free software option. IPOPT provides a script to download and patch MUMPS automatically during its compilation process.

    1. Download and extract the IPOPT source (version > 3.9.1).
    2. Use the provided script in ThirdParty/Mumps to fetch MUMPS.
    3. Configure, compile, and install IPOPT (e.g., using --prefix=/usr/local).
    4. Update your system linker (e.g., via /etc/ld.so.conf.d/ipopt.conf) to include the paths to the IPOPT and MUMPS libraries.

    2. Install PyIPOPT

    After installing the system libraries, you must compile the PyIPOPT Python extension. You will need to modify the makefile to match your specific environment paths for CC, PYTHON_INCLUDE, NUMPY_INCLUDE, and the IPOPT/MUMPS library/include directories before running make and make install.

    # Example IPOPT installation flow
    $ wget http://www.coin-or.org/download/source/Ipopt/Ipopt-3.9.3.tgz
    $ tar xvf Ipopt-3.9.3.tgz
    $ cd Ipopt-3.9.3/
    $ cd ThirdParty/Mumps
    $ ./get.Mumps
    $ cd ../..
    $ ./configure --prefix=/usr/local
    $ make
    $ sudo make install
    
    # Update linker paths
    $ sudo nano -w /etc/ld.so.conf.d/ipopt.conf
    # Add these lines:
    # /usr/local/lib/coin
    # /usr/local/lib/coin/ThirdParty
    
    # Install PyIPOPT
    $ svn checkout https://pyipopt.googlecode.com/svn/trunk/ pyipopt
    $ cd pyipopt/
    # [Modify Makefile with your local paths]
    $ make
    $ make install
  5. Get support for PYPOWER

    master

    For questions, comments, or general inquiries regarding PYPOWER, use the following channels:

  6. Run power flow and OPF via CLI

    master

    After installation, PYPOWER provides two command-line interfaces: pf for power flow and opf for Optimal Power Flow (OPF).

    Power Flow (pf)

    Use the pf command to solve power flow problems. You can specify a built-in test case using the -c flag or provide a direct path to a Python case data file.

    • List options: venv/bin/pf -h
    • Run a built-in test case (e.g., IEEE 14 bus):
      venv/bin/pf -c case14
    • Run from a specific file path:
      venv/bin/pf /path/to/case14.py

    Optimal Power Flow (opf)

    The opf command follows the same syntax as pf. You can solve an OPF and save the resulting solved case to a file using the --solvedcase flag.

    • List options: venv/bin/opf -h
    • Run OPF and save results (e.g., IEEE Reliability Test System):
      venv/bin/opf -c case24_ieee_rts --solvedcase=rtsout.py
    venv/bin/pf -c case14
    venv/bin/opf -c case24_ieee_rts --solvedcase=rtsout.py
  7. Use the PYPOWER Command Line Interface (CLI)

    master

    PYPOWER provides two primary CLI tools: pf for power flow and opf for Optimal Power Flow (OPF). After installation, these commands are added to your system path.

    Basic Usage

    To run a power flow on a specific case data file:

    $ pf [options] [casedata]

    To run an OPF:

    $ opf [options] [casedata]

    Running Built-in Test Cases

    You can run any of the built-in test cases using the -c or --testcase flag.

    Available test cases: case30_userfcns, case118, case9, case30Q, case30pwl, case6ww, case57, case39, case14, case9Q, case30, case300, case4gs, case24_ieee_rts.

    Example (running IEEE 14 bus test case):

    $ pf -c case14

    Example (running OPF for IEEE RTS and saving the result):

    $ opf -c case24_ieee_rts --solvedcase=rtsout.py

    Saving Results

    Use the --solvedcase=SOLVEDCASE option to write the solved case to a file.

    • If the filename ends in .mat, it is saved as a MAT-file.
    • Otherwise, it is saved as a Python file.
    # Run power flow on a specific file
    $ pf /path/to/case14.py
    
    # Run OPF on a test case and save output
    $ opf -c case24_ieee_rts --solvedcase=rtsout.py
  8. Install LP Solve for DC OPF problems

    master

    LP Solve can be used to solve DC OPF problems with piecewise linear cost functions via its Python interface.

    Installation Steps:

    1. Download and unpack the lp_solve source archive.
    2. Build the source using the provided script: sh ./ccc.
    3. Copy the liblpsolve55 files to your system's library path (e.g., /usr/local/lib/).
    4. Download and unpack the lp_solve Python source code into the same directory as the main lp_solve source.
    5. Compile and install the Python extension using setup.py.
    # Build LP Solve
    $ cd lp_solve_5.5/lpsolve55
    $ sh ./ccc
    
    # Copy libraries
    $ cp bin/ux32/* /usr/loca/lib/
    
    # Install Python extension
    $ cd ../extra/Python/
    $ sudo python setup.py install
  9. Install PYPOWER

    master

    The recommended way to install PYPOWER is via pip. It is highly recommended to use a virtual environment to manage dependencies.

    Prerequisites

    • Python >= 3.5

    Installation Steps

    1. Create a virtual environment (optional but recommended):
      python3.8 -m venv venv
    2. Install dependencies (NumPy, SciPy, and PyRLU):
      venv/bin/python -m pip install -r requirements.txt
    3. Install PYPOWER:
      venv/bin/python -m pip install PYPOWER

    Alternatively, you can install from a downloaded tarball:

    tar zxf PYPOWER-5.x.y.tar.gz
    venv/bin/python setup.py install
    venv/bin/python -m pip install PYPOWER