# PyOPUS installation guide

## Contents

- [Installation from wheel files](#installation-from-wheel-files)
  - [Linux](#linux)
  - [Windows (quickstart)](#windows-quickstart)
  - [Windows (from your own package sources)](#windows-from-your-own-package-sources)
- [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)
- [Authors and credits](#authors-and-credits)

---

## Installation from wheel files

### Linux

Tested under Debian 13 (Trixie) amd64.

#### 1. Install Spice Opus

Skip this step if you are using some other supported simulator.

Download it from <http://www.spiceopus.si> and install it to `/usr/local`.

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

Become root and run:

```bash
apt-get install \
    libstdc++6 libopenmpi-dev \
    liblapack3 liblapack-dev libblas3 libblas-dev \
    kicad gfortran \
    python3 python3-venv python3-pip python3-wheel \
    python3-setuptools python3-setuptools-scm \
    python3-numpy python3-scipy python3-matplotlib python3-greenlet \
    python3-mpi4py python3-lxml python3-pyqtgraph python3-cvxopt \
    python3-pandas python3-pyside6.qtwidgets python3-pyside6.qtcore
```

| Package | Purpose |
|---|---|
| `kicad` | schematic entry |
| `gfortran` | CUTEr support |
| `libopenmpi-dev`, `python3-mpi4py` | parallel processing (MPI) |
| `liblapack*`, `libblas*` | linear algebra libraries |
| `python3-*` | Python and the libraries PyOPUS depends on |

#### 3. Create a Python virtual environment

Starting with Debian 12, `.whl` files cannot be installed into the system-wide
Python environment, because they could change the behavior of the interpreter
and affect system packages that depend on Python. A virtual environment is
isolated from the system-wide installation.

Next, create a virtual environment. Give virtual environment access to system `site-packages` directory (i.e. the virtual environment will include all python modules that were installed with `apt`). 

```bash
python3 -m venv --system-site-packages <path to virtual environment>
```

#### 4. Activate the virtual environment

```bash
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 any `.whl` file 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`.

#### 5. Install PySide6

The PyOPUS GUI and the plotter use PySide6 (Qt for Python). PySide6 6.8 or
newer is required. If you installed it from .deb packages you are done.
Otherwise do

```bash
pip install pyside6
```

#### 6. Install datatable (optional)

Datatable is faster than Pandas. It is not a replacement for Pandas, but PyOPUS
uses it if it is available. There is no Debian package, so install it from PyPI:

```bash
pip3 install datatable
```

#### 7. Install PyOPUS

Download the PyOPUS wheel for Linux from
<http://fides.fe.uni-lj.si/pyopus/download/0.14/pyopus-0.14-cp313-cp313-linux_x86_64.whl>
and install it (with the virtual environment active):

```bash
pip3 install PyOPUS-0.14-cp313-cp313-linux_x86_64.whl
```

---

### Windows (quickstart)

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 Spice Opus

Skip this step if you are using some other supported simulator.

Download it from <http://www.spiceopus.si> and install it to `c:\SpiceOpus`.
Reboot.

#### 3. Download the PyOPUS quickstart

Download
<http://fides.fe.uni-lj.si/pyopus/download/0.14/PyOPUS-0.14-quickstart-win_amd64.zip>
and unpack it to `c:\`. This creates the folder
`c:\PyOPUS-0.14-quickstart-win_amd64`.

#### 4. Install Python 3

In `c:\PyOPUS-0.14-quickstart-win_amd64` run the installer
`python-3.13.11-amd64.exe` and choose:

- Check **Add Python to path**
- Click **Customize installation**
- Check **PIP**
- Check **TCL/TK and Idle**
- Click **Next**
- Check **Install for all users**
- Check **Associate files with Python**
- Check **Add Python to environment variables**
- Check **Precompile standard library**
- Install to `c:\Python313`

If asked, allow the installer to remove the 260 character path length
limitation.

#### 5. Install Microsoft MPI

In `c:\PyOPUS-0.14-quickstart-win_amd64` run `msmpisetup.exe`.

#### 6. Install prerequisite wheels and PyOPUS

The required wheel files are in `c:\PyOPUS-0.14-quickstart-win_amd64` along with
the PyOPUS wheel. Install them all by running:

```bat
install.bat
```

> **Note:** The GUI and the plotter need PySide6 (6.8 or newer). It is a
> dependency of the PyOPUS wheel, but if the quickstart folder does not contain
> a PySide6 wheel and `install.bat` cannot download it, install it manually:
>
> ```bat
> pip install pyside6
> ```

#### 7. Clean up

You can now delete `c:\PyOPUS-0.14-quickstart-win_amd64`.

---

### Windows (from your own package sources)

Tested under Windows 11.

#### 1. Prepare KiCad, Spice Opus and the download

Go through steps 1-3 of [Windows (quickstart)](#windows-quickstart).

#### 2. Install Python

Download Python 3.13.11 and install it to `c:\Python313`. Make sure the
interpreter is in the system path, i.e. add `c:\Python313` and
`c:\Python313\Scripts` to the `PATH` environment variable.

#### 3. Install Microsoft MPI

Download Microsoft MPI and install it.

#### 4. Install Python libraries

```bat
pip install wheel setuptools setuptools-scm numpy scipy cvxopt matplotlib greenlet mpi4py lxml pyside6 pyqtgraph pandas
```

Optionally, you can also install `python-datatable`.

> **Note:** Make sure the `mpi4py` you use is linked against the installed
> version of Microsoft MPI.

#### 5. Install PyOPUS

Download
<http://fides.fe.uni-lj.si/pyopus/download/0.14/PyOPUS-0.14-cp313-cp313-win_amd64.whl>,
open the command prompt and type:

```bat
pip install PyOPUS-0.14-cp313-cp313-win_amd64.whl
```

---

## 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):** Make sure `python3` is in your path. If PyOPUS is not
> installed in the Python libraries folder, also make sure its installation
> folder is listed in the `PYTHONPATH` environment variable.

### 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.14/PyOPUS-0.14-doc-demo.tar.gz>

Online:

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

---

## Building PyOPUS from sources

Download the source tarball from
<http://fides.fe.uni-lj.si/pyopus/download/0.14/PyOPUS-0.14.tar.gz>
and unpack it.

Since 0.11.2 you can also 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.14 or newer):

```bash
git clone --branch v0.14 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 the Linux PyOPUS installation

See [Linux](#linux) above.

#### 2. Install the prerequisites for building

```bash
apt-get install \
    gcc g++ libstdc++-14-dev make \
    texlive texlive-latex-extra dvipng graphviz \
    python3-dev python3-sphinx python3-sphinx-rtd-theme \
    python3-wheel python3-setuptools python3-setuptools-scm \
    python3-setuptools-whl python3-build
```

> **Note:** Install `libstdc++xx-dev` where `xx` matches the version of the
> g++ compiler.

#### 3. Build

Activate the Python virtual environment (if you have one). In the folder where
the PyOPUS sources are located run:

```bash
./pack
```

The wheel file, demos and documentation, and the source package are placed
under `dist/`.

### Building for Windows (wheel only)

Tested under Windows 11.

#### 1. Install the prerequisites for the Windows PyOPUS installation

See [Windows (quickstart)](#windows-quickstart) above.

#### 2. Install the prerequisites for building

- Visual Studio 2022
- Python libraries (in a command prompt):

  ```bat
  pip install setuptools setuptools-scm wheel build
  ```

#### 3. Uninstall a development install (if present)

If PyOPUS was installed in development mode (editable install), uninstall it
before building:

```bat
pip uninstall pyopus
```

This command is commented out at the beginning of `pack.bat`.

#### 4. Build

Start the *VS 2022 x64 Native Tools Command Prompt*, go to the folder where the
PyOPUS sources are located and run:

```bat
pack.bat
```

The wheel file is placed under `dist/`.

### 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.

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

1. Build PyOPUS:

   ```bash
   python3 -m build --no-isolation --wheel
   ```

2. Install it:

   | Platform | Command |
   |---|---|
   | Linux | `pip3 install -e . --no-deps` |
   | Windows | `python setup.py develop` |

   > Due to a quirk in pip that prevents editable installation of PyOPUS, the
   > Windows command differs from the Linux one.

   > **Note:** `--no-deps` skips dependencies, so PySide6 (6.8 or newer) must
   > already be installed (`pip install pyside6`), otherwise the GUI will not
   > start.

3. To remove the editable installation:

   | Platform | Command |
   |---|---|
   | Linux | `pip3 uninstall pyopus` |
   | Windows | `pip uninstall pyopus` |

   On Windows this command is commented out at the end of `pack.bat`.

> **Note:** Under Linux the `pack` script performs a development 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.
