# PyOPUS installation guide

## Contents

- [Installation from wheel files](#installation-from-wheel-files)
  - [Supported Python versions](#supported-python-versions)
  - [Linux](#linux)
  - [Windows](#windows)
- [Configuring KiCad](#configuring-kicad)
- [Preparing PyOPUS for parallel processing with MPI](#preparing-pyopus-for-parallel-processing-with-mpi)
- [Demos and documentation](#demos-and-documentation)
- [Building PyOPUS from sources](#building-pyopus-from-sources)
  - [Building for Linux](#building-for-linux-wheel-demos-and-documentation-and-source)
  - [Building for Windows](#building-for-windows-wheel-only)
  - [Development install (editable)](#install-pyopus-for-development-in-source-folders)
- [Credits](#credits)

---

## Installation from wheel files

### Supported Python versions

The PyOPUS wheel works with Python 3.10 or newer. A single wheel file
(tagged `cp310-abi3`) can be installed on any of these versions, e.g. on
Python 3.10, 3.12 or 3.14, without rebuilding. This is possible because
the PyOPUS binary modules use only the stable part of the Python C
interface (the *Stable ABI*, also called *abi3*).

> **Note:** The wheel does **not** work with free-threaded Python builds
> (`python3.13t`, `python3.14t`, ..., where the GIL is disabled), nor with
> PyPy. On a free-threaded build PyOPUS must be built from source, which
> requires a C/C++ compiler and gfortran. The limited API must be disabled when building,
> e.g. by running
>
> ```bash
> pip install . -Csetup-args=-Dpython.allow_limited_api=false
> ```
>
> in the PyOPUS source directory (see
> [Building PyOPUS from sources](#building-pyopus-from-sources)). The build
> produces a wheel for that particular Python version. Importing the binary
> modules re-enables the GIL (Python prints a warning). To check which build
> you have, run
>
> ```bash
> python3 -c "import sysconfig; print('free-threaded' if sysconfig.get_config_var('Py_GIL_DISABLED') else 'regular')"
> ```

### Linux

Tested under Debian 13 (Trixie) amd64.

#### 1. Install prerequisites (Debian packages)

Become root and run:

```bash
apt-get install kicad libopenmpi-dev python3 python3-venv python3-pip
```

| Package | Purpose |
|---|---|
| `kicad` | schematic entry |
| `libopenmpi-dev` | parallel processing (MPI) |
| `python3*` | Python, virtual environments, and pip |

#### 2. Create and activate a Python virtual environment

Starting with Debian 12, packages cannot be installed with pip into the
system-wide Python environment. Create a virtual environment and activate it:

```bash
python3 -m venv <path to virtual environment>
source <path to virtual environment>/bin/activate
```

The prompt changes to reflect the active environment. To leave it, type
`deactivate`.

> **Note:** Activate the environment before you install PyOPUS and
> before you run the PyOPUS GUI (the `pyog` command).
>
> Without activating it, you can still call the environment's tools directly:
> `<path to virtual environment>/bin/python3` and
> `<path to virtual environment>/bin/pip`.

#### 3. Install PyOPUS

With the virtual environment active, type:

```bash
pip install pyopus
```

This also installs all Python libraries PyOPUS depends on (NumPy, SciPy,
PySide6, mpi4py, ...). Optionally, you can also install `datatable`.

> **Note:** The Linux wheel requires glibc 2.39 or newer (e.g. Debian 13,
> Ubuntu 24.04 or newer). To check the glibc version run `ldd --version`. On
> older distributions pip cannot use the wheel and tries to build PyOPUS from
> the source package, which fails unless the build prerequisites are
> installed. In that case see
> [Building PyOPUS from sources](#building-pyopus-from-sources).

> **Note:** CUTEr test problems are compiled when they are first used. This
> requires additional packages:
>
> ```bash
> apt-get install gcc gfortran python3-dev liblapack-dev libblas-dev
> ```
>
> and setuptools in the virtual environment (`pip install setuptools`).

---

### Windows

Tested under Windows 11.

#### 1. Install KiCad (for schematic entry)

KiCad is used for schematic entry. Tested with 6.0.4 x86_64.
Download it from <http://www.kicad.org/> and install it.

#### 2. Install Python

Download Python 3.10 or newer from <https://www.python.org/downloads/windows/>
and install it. Check **Add Python to PATH** (or **Add Python to environment
variables**). If asked, allow the installer to remove the 260 character path
length limitation.

#### 3. Install Microsoft MPI

Download Microsoft MPI from
<https://learn.microsoft.com/en-us/message-passing-interface/microsoft-mpi>
and install it (`msmpisetup.exe`).

#### 4. Install PyOPUS

Open the command prompt and type:

```bat
pip install pyopus
```

This also installs all Python libraries PyOPUS depends on (NumPy, SciPy,
PySide6, mpi4py, ...). Optionally, you can also install `datatable`.

---

## Configuring KiCad

PyOPUS uses KiCad 5+ as its schematic editor. The steps below need to be done
only once; KiCad stores the settings in its configuration.

> **Note:** If you use a Python virtual environment, activate it first and then
> start `eeschema`.

### 1. Add the netlister plugin

1. Start the schematic editor (`eeschema`).
2. Open the netlist dialog:
   - **KiCad 5:** Tools → Generate Netlist File
   - **KiCad 6+:** File → Export → Netlist
3. Click **Add Plugin**.
4. Under **Name** enter:

   ```
   Spice Opus
   ```

5. Under **Netlist command** enter:

   ```
   python3 -m pyopus.netlister.kicad -i "%I" -o "%O"
   ```

   On Windows use:

   ```
   <full path to pythonw.exe> -m pyopus.netlister.kicad -i "%I" -o "%O"
   ```

6. Click **OK**. A tab for generating Spice Opus netlists appears.

> **Note (Linux):** If PyOPUS is installed in a virtual environment and you
> do not want to activate it before starting `eeschema`, use the
> environment's Python in the netlist command:
>
> ```
> <path to virtual environment>/bin/python3 -m pyopus.netlister.kicad -i "%I" -o "%O"
> ```

### 2. KiCad 5: symbol placement (optional)

To make KiCad 5 place symbols in the same way as KiCad 4, open
Preferences → General Options → **Editing** tab and uncheck:

- Automatically place symbol fields
- Allow field autoplace to change justification
- Always align autoplaced fields to the 50 mil grid

### 3. Add the pyopus symbol library

The library is distributed with the PyOPUS demos in `demo/kicad/library`:

| KiCad version | File(s) | Plugin Type |
|---|---|---|
| 5 | `pyopus.lib` (symbols) and `pyopus.dcm` (auxiliary data) | Legacy |
| 6+ | `pyopus.kicad_sym` | KiCad |

Select Preferences → Manage Symbol Libraries. In the **Global Libraries** tab
click **Append Library** and fill in the new entry:

| Field | Value |
|---|---|
| Active | mark checkbox |
| Nickname | `pyopus` |
| Library Path | absolute path to `pyopus.lib` (V5) or `pyopus.kicad_sym` (V6+) |
| Plugin Type | Legacy (V5), KiCad (V6+) |
| Description | PyOPUS symbol library |

Leave the **Options** field empty.

### 4. Generate a Spice Opus netlist

1. Open the netlist dialog:
   - **KiCad 5:** Tools → Generate Netlist File
   - **KiCad 6+:** File → Export → Netlist
2. Choose the **Spice Opus** tab and click **Generate**.

A window with the generated netlist pops up. In case of an error it shows the
error message. To show the window only in case of an error, add the `-n`
option to the netlist command.

### Alternative for KiCad 6+: netlister as a BOM generator

Generating the netlist via File → Export → Netlist is a bit awkward. An
alternative is to set up the netlister as a bill of materials (BOM) generator
script.

1. Select Tools → Generate BOM (in KiCad 9: *Generate Legacy Bill of
   Materials...*).
2. Click the **+** icon to add a new BOM script.
3. Select any `.py` file on your computer and click **Open**.
4. Give the script a name, e.g. `PyOPUS Netlister for KiCad`.
5. Change the command line of the generator to:

   ```
   "python3" -m pyopus.netlister.kicad -i "%I" -o "%O"
   ```

   On Windows:

   ```
   "<full path to pythonw.exe>" -m pyopus.netlister.kicad -i "%I" -o "%O"
   ```

Now you can generate the netlist by clicking the BOM icon, choosing the
"PyOPUS Netlister for KiCad" generator script and clicking **Generate**.

> **Note:** Netlisting in KiCad 9 under Windows works only if invoked from
> Tools → Generate Legacy Bill of Materials... with **Show console window**
> enabled. This seems to be a KiCad bug.

---

## Preparing PyOPUS for parallel processing with MPI

**Parallel processes on your local machine:** nothing further to do.

**Parallel processes on a computer farm:** if your home folder
`/home/<username>` is NFS-mounted on all worker computers as
`/home/<username>`, there is nothing further to do. Otherwise see the
documentation of `pyopus.parallel.base` and set the
`PARALLEL_MIRRORED_STORAGE` environment variable on all participating
computers.

**Local copies of files (mirroring):** if workers store local copies in `/tmp`,
there is nothing further to do. To store them elsewhere, see the documentation
of `pyopus.parallel.base` and set the `PARALLEL_LOCAL_STORAGE` environment
variable on all participating computers.

### Windows

Before starting any parallel run, start the following in a command prompt on
all hosts where you want to start MPI processes:

```bat
smpd -d 3
```

The `-d 3` option produces debug output, which helps you solve problems with
your MPI setup. The `smpd` executable is part of Microsoft MPI.

> **Note:** Parallel processing on a computer farm was not tested under
> Windows. This is uncharted territory. Reports are welcome.

---

## Demos and documentation

Demos and documentation in HTML format are available as a `.tar.gz` archive:
<http://fides.fe.uni-lj.si/pyopus/download/0.17/pyopus-0.17-doc-demo.tar.gz>

Online:

- Demos: <http://fides.fe.uni-lj.si/pyopus/download/0.17/demo>
- Documentation: <http://fides.fe.uni-lj.si/pyopus/download/0.17/docsrc/_build/html>

---

## Building PyOPUS from sources

Clone the main branch to get the latest merged bugfixes:

```bash
git clone https://codeberg.org/arpadbuermen/PyOPUS.git
```

or clone a tagged version (0.15 or newer):

```bash
git clone --branch v0.17 https://codeberg.org/arpadbuermen/PyOPUS.git
```

### Building for Linux (wheel, demos and documentation, and source)

Tested under Debian 13 (Trixie) amd64.

#### 1. Install the prerequisites for building

`pack` builds without build isolation (`--no-isolation`) and the
documentation build imports all PyOPUS modules. Therefore the build
environment must contain the build tools, the numpy headers, Sphinx, and all
the libraries PyOPUS depends on. Become root and run:

```bash
apt-get install \
    build-essential gfortran git patchelf libopenmpi-dev \
    python3 python3-venv python3-pip python3-dev \
    python3-numpy python3-numpy-dev python3-scipy python3-matplotlib \
    python3-greenlet python3-mpi4py python3-cvxopt python3-pyqtgraph \
    python3-lxml python3-pandas \
    python3-pyside6.qtcore python3-pyside6.qtgui python3-pyside6.qtwidgets \
    python3-build python3-wheel \
    python3-sphinx python3-sphinx-rtd-theme graphviz
```

| Package | Purpose |
|---|---|
| `build-essential`, `gfortran` | C/C++ and FORTRAN compilers |
| `git` | version from git tags, source package (`meson dist`) |
| `patchelf` | used by `auditwheel` |
| `libopenmpi-dev` | MPI library for `mpi4py` |
| `python3`, `python3-venv`, `python3-pip`, `python3-dev` | Python, virtual environments, pip, and Python headers |
| `python3-numpy`, `python3-numpy-dev` | numpy and its C headers |
| `python3-scipy` ... `python3-pyside6.*` | libraries PyOPUS depends on (imported by the documentation build) |
| `python3-build`, `python3-wheel` | building the source package and the wheel, retagging the wheel |
| `python3-sphinx`, `python3-sphinx-rtd-theme` | documentation |
| `graphviz` | inheritance diagrams in the documentation |

`gfortran` is required for building the FORTRAN test problems (More-Wild,
Luksan-Vlcek, Karmitsa). Under Linux the binary modules link the FORTRAN
runtime library (`libgfortran`) dynamically because the static version
supplied with the distributions cannot be linked into shared libraries.
`auditwheel` (see below) copies it into the wheel so that the wheel works
on systems without gfortran.

#### 2. Create and activate a Python virtual environment

The build environment uses the Python libraries installed with `apt-get`, so
create the virtual environment with access to the system-wide packages:

```bash
python3 -m venv --system-site-packages <path to virtual environment>
source <path to virtual environment>/bin/activate
```

#### 3. Install Meson, meson-python, Ninja, and auditwheel

PyOPUS is built with [Meson](https://mesonbuild.com) via
[meson-python](https://meson-python.readthedocs.io) (Meson 1.5 and
meson-python 0.18 or newer). The meson-python package in Debian 13 (0.17) is
too old: it does not put the license file into the wheel. Therefore install
meson-python, Meson, and Ninja with pip in the virtual environment (with the
virtual environment active), together with `auditwheel` which gives the
Linux wheel a `manylinux` platform tag (required for uploading to PyPI):

```bash
pip install "meson-python>=0.18" "meson>=1.5" ninja auditwheel
```

> **Note:** The Debian packages `python3-mesonpy`, `meson`, and
> `ninja-build` are not needed. If they are installed, the versions in the
> virtual environment take precedence while the environment is active.
> Meson and Ninja must stay installed in the virtual environment if you use
> an editable install (the binary modules are rebuilt on import).

#### 4. Build

Activate the Python virtual environment. In the folder where
the PyOPUS sources are located run:

```bash
./pack
```

`pack` builds the source package, builds the wheel from the source package,
performs an editable install (see
[Install PyOPUS for development](#install-pyopus-for-development-in-source-folders)),
and builds the documentation. The wheel file, demos and documentation, and the
source package are placed under `dist/`. Documentation and demo files are
collected in `staging/` which is removed at the end.

> **Note:** The source package is created from the last git commit
> (`meson dist`). Uncommitted changes are not included in the source package
> and in the wheel. `pack` prints a warning if there are uncommitted changes.

> **Note:** meson-python tags abi3 wheels with the version of the Python
> that builds them (e.g. `cp313-abi3`). Because the binary modules use the
> Python 3.10 limited API, `pack` retags the wheel as `cp310-abi3`. If
> `auditwheel` is available, `pack` then replaces the `linux_x86_64` platform
> tag with a `manylinux` tag. The `manylinux` tag reflects the glibc version
> of the build system (e.g. `manylinux_2_39` on Debian 13), so the wheel
> installs only on Linux distributions with the same or newer glibc.

#### Version

The version is determined by `buildtools/version.py` from the git tags
(`vX.Y` or `vX.Y.Z`):

| State of the source tree | Version |
|---|---|
| on tag `v0.14`, no uncommitted changes | `0.14` |
| 20 commits after tag `v0.14` | `0.14.post20+g75e27a7` |
| uncommitted changes | `0.14.post20+g75e27a7.dirty` |

Source packages contain a `VERSION` file with the version, since they are
built without git. Only wheels with versions without a `+` part (i.e. built
on a tag from a clean source tree) can be uploaded to PyPI. To print the
version run

```bash
python3 buildtools/version.py
```

#### Building for free-threaded Python

Free-threaded Python (e.g. `python3.14t`) does not support the limited API
before Python 3.15. Disable it when building:

```bash
python3 -m build --no-isolation -Csetup-args=-Dpython.allow_limited_api=false
```

or, for installing directly from the source folder,

```bash
pip install --no-build-isolation -Csetup-args=-Dpython.allow_limited_api=false .
```

`pack` does this automatically when run with a free-threaded Python. Debian
does not package free-threaded Python. You can get it, e.g., with
[uv](https://docs.astral.sh/uv/) (`pipx install uv`):

```bash
uv python install 3.14t
uv venv --python 3.14t <path to virtual environment>
uv pip install --python <path to virtual environment> "meson-python>=0.18" "meson>=1.5" ninja "numpy>=2" build wheel auditwheel
```

This virtual environment does not see the Python libraries installed with
`apt-get`. `pack` also builds the documentation which imports all PyOPUS
modules, so install Sphinx and the libraries PyOPUS depends on as well:

```bash
uv pip install --python <path to virtual environment> sphinx sphinx-rtd-theme \
    scipy matplotlib greenlet mpi4py cvxopt pyqtgraph lxml pandas pyside6
```

If some of them are not available for free-threaded Python, build only the
wheel with `python3 -m build --no-isolation` (see above) instead of running
`pack`.

#### Building for macOS

Install the Xcode command line tools (`xcode-select --install`) and
gfortran (e.g. with [Homebrew](https://brew.sh): `brew install gcc`), then
install `"meson-python>=0.18"`, `"meson>=1.5"`, `ninja`, `"numpy>=2"`,
`build`, and `wheel` with pip in a virtual environment and run `python3 -m build --no-isolation` in the
source folder. The FORTRAN runtime library is linked statically into the
binary modules. The macOS build has not been tested yet.

#### Files included in the wheel

All files in the `pyopus/` folder are installed except those matching the
exclude patterns in `meson.build` (`exclude_file_patterns`,
`exclude_dir_patterns`). The folders `pyopus/gui/resources/` (icon sources)
and `pyopus/gui/obsolete/` are not installed.

The list of excluded files is computed when Meson configures the build. If
you add a file that matches an exclude pattern (e.g. a backup file `foo.py~`)
to an existing build folder (e.g. the editable install's `build/cp313`),
reconfigure the build so that the file is excluded:

```bash
meson setup --reconfigure build/cp313
```

Builds made with `pack` and `python3 -m build` use a fresh build folder and
need no reconfiguration. Meson also reconfigures automatically after every
git commit or checkout.

#### Regenerating the GUI icons

The GUI loads its icons from `pyopus/gui/resources.py` which is generated
from `pyopus/gui/resources/resources.qrc` and the PNG files in
`pyopus/gui/resources/`. It is committed to git and shipped with the
sources. After changing the icons regenerate it with (requires
`pyside6-rcc` which comes with PySide6)

```bash
make -C pyopus/gui/resources
```

### Building for Windows (wheel only)

Tested under Windows 11.

#### 1. Install the prerequisites for building

MinGW-w64 with gcc, g++, and gfortran. From <https://winlibs.com> download
the Win64 release with the **UCRT** runtime (not MSVCRT), **POSIX** threads,
and without LLVM/Clang/LLD/LLDB (.zip). Tested with GCC 16.1.0 (with POSIX
threads) + MinGW-w64 14.0.0 (UCRT) - release 4. Unpack it (e.g.
to `C:\mingw64`), and add its `bin` folder (e.g. `C:\mingw64\bin`) to the
`PATH`. Check that `gfortran --version` works in a new command prompt.

Visual Studio is not used. PyOPUS uses gfortran for the FORTRAN test
problems, and C/C++ objects built with Visual Studio cannot be easily
combined with gfortran objects.

#### 2. Install Python, Git, and the Python libraries needed for building

- Python 3.10 or newer, see step 2 under [Windows](#windows) above.
- [Git for Windows](https://git-scm.com/download/win). Needed when building
  from a git clone (the version is obtained from the git tags). Make sure
  `git` is in the `PATH`. Not needed when building from an unpacked source
  package (it contains a `VERSION` file).
- Python libraries (in a command prompt):

  ```bat
  pip install "meson-python>=0.18" "meson>=1.5" ninja "numpy>=2" wheel build
  ```

KiCad, Microsoft MPI, and the libraries PyOPUS depends on are not needed for
building the wheel. For a
[development install](#install-pyopus-for-development-in-source-folders)
also install Microsoft MPI (see [Windows](#windows)) and the libraries
PyOPUS depends on:

```bat
pip install numpy scipy matplotlib greenlet mpi4py cvxopt pyqtgraph lxml pandas pyside6
```

#### 3. Build

Open a command prompt, go to the folder where the PyOPUS sources are located
and run:

```bat
pack.bat
```

`pack.bat` passes the Meson native file `buildtools\mingw.ini` to the build
so that MinGW is used even if Visual Studio is installed. The runtime
libraries of MinGW (libgcc, libstdc++, libgfortran, libquadmath,
winpthread) are linked statically, so the binary modules depend only on
DLLs that come with Windows and on `python3.dll`. To check this run (MinGW's
`objdump`)

```bat
objdump -p pyopus\problems\_lvns.cp310-win_amd64.pyd | findstr "DLL Name"
```

on a binary module extracted from the wheel (the exact file name may differ).

The wheel file is placed under `dist/` and retagged as `cp310-abi3` (see the notes in
[Building for Linux](#building-for-linux-wheel-demos-and-documentation-and-source)).
Unlike under Linux the wheel is built directly from the source tree, so
uncommitted changes are included.

### Install PyOPUS for development (in source folders)

PyOPUS is installed in the folder where the sources used for building it are
located (an *editable install*), so all changes to `.py` files affect the
behavior of the library immediately.

Changes to C/C++ sources are also picked up: the binary modules are
rebuilt automatically when PyOPUS is imported. Therefore Meson and Ninja must
remain installed in the environment. The binary modules are built in
`build/cp<python version>` (e.g. `build/cp313`), not in the `pyopus/`
folder.

1. Install the build prerequisites (see the build sections above).

2. Enter the source folder (where the `pyopus/` subfolder is located) and run

   ```bash
   pip install --no-build-isolation --no-deps -e .
   ```

   With free-threaded Python add
   `-Csetup-args=-Dpython.allow_limited_api=false`. Under Windows add
   `-Csetup-args=--native-file=buildtools\mingw.ini` (MinGW, see
   [Building for Windows](#building-for-windows-wheel-only)).

   > **Note:** `--no-build-isolation` is required: the automatic rebuild
   > uses the Meson, Ninja, and numpy installed in the environment.
   > `--no-deps` skips dependencies, so all libraries PyOPUS depends on must
   > already be installed. Under Linux they are installed with `apt-get` in
   > step 1 of [Building for Linux](#building-for-linux-wheel-demos-and-documentation-and-source).
   > Under Windows see step 2 of
   > [Building for Windows](#building-for-windows-wheel-only).
   > PySide6 must be 6.8 or newer, otherwise the GUI will not start.

   To see the output of the automatic rebuild set the environment variable
   `MESONPY_EDITABLE_VERBOSE=1`.

   > **Note:** Meson reconfigures the build folder automatically when
   > `meson.build` changes and after every git commit or checkout. Reconfigure
   > it manually after changes Meson does not detect, e.g. new files matching
   > the exclude patterns (see [Files included in the wheel](#files-included-in-the-wheel)),
   > a different compiler, or an upgrade of Meson:
   >
   > ```bash
   > meson setup --reconfigure build/cp313
   > ```
   >
   > Activate the virtual environment first so that its Meson is used. The
   > automatic rebuild uses the Meson recorded in the build folder, so do not
   > reconfigure with a different Meson (e.g. Debian's `/usr/bin/meson` when
   > a newer one is installed in the virtual environment). Otherwise
   > `meson install` and the automatic rebuild may fail with *Build data file
   > ... references functions or classes that don't exist*. If this happens,
   > delete the `build/` folder and repeat the editable install.

3. Run the tests (from the source folder):

   ```bash
   for t in tests/test_*.py; do python3 $t; done
   ```

   Under Windows (in a command prompt):

   ```bat
   for %t in (tests\test_*.py) do python %t
   ```

4. To remove the editable installation:

   ```bash
   pip uninstall pyopus
   ```

   Then delete the `build/` folder.

> **Note:** Do not delete the `build/` folder while the editable install is
> present, the binary modules are loaded from there. If you did, reinstall.

> **Note:** Under Linux the `pack` script performs an editable install before
> building the documentation, so that Sphinx can import the PyOPUS modules.

---

## Credits

PyOPUS uses code from the following packages:

- FORTRAN test problems implemented by
  - Napsu Karmitsa
  - Ladislav Luksan and Jan Vlcek
  - Jorge More and Stefan Wild
- MADS test problems MDO and STYRENE implemented by Sebastien Le Digabel,
  Charles Audet, et al.

See `src/*.zip` and `src/*.tar.gz` for details on the respective licenses.
