Metadata-Version: 2.2
Name: pulsim
Version: 2.1.0
Summary: High-performance circuit simulator for power electronics
Keywords: circuit,simulation,spice,power electronics,eda
Author-Email: Pulsim Authors <pulsim@example.com>
License: MIT
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: C++
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)
Project-URL: Homepage, https://github.com/pulsim/pulsim-core
Project-URL: Documentation, https://pulsim.readthedocs.io
Project-URL: Repository, https://github.com/pulsim/pulsim-core
Project-URL: Issues, https://github.com/pulsim/pulsim-core/issues
Requires-Python: >=3.10
Requires-Dist: numpy>=1.20
Provides-Extra: jupyter
Requires-Dist: ipywidgets>=8.0; extra == "jupyter"
Requires-Dist: matplotlib>=3.5; extra == "jupyter"
Requires-Dist: plotly>=5.0; extra == "jupyter"
Provides-Extra: data
Requires-Dist: pandas>=1.3; extra == "data"
Requires-Dist: xarray>=2022.0; extra == "data"
Provides-Extra: fast
Requires-Dist: numba>=0.58; extra == "fast"
Provides-Extra: lib
Requires-Dist: PyYAML>=6.0; extra == "lib"
Provides-Extra: opt
Requires-Dist: pymoo>=0.6; extra == "opt"
Provides-Extra: codegen
Requires-Dist: sympy>=1.13; extra == "codegen"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: pytest-timeout>=2.2; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
Requires-Dist: PyYAML>=6.0; extra == "dev"
Requires-Dist: rich>=13.0; extra == "dev"
Requires-Dist: pybind11-stubgen==3.0.0; extra == "dev"
Requires-Dist: mypy==2.4.1; extra == "dev"
Requires-Dist: mpmath>=1.3; extra == "dev"
Requires-Dist: sympy>=1.13; extra == "dev"
Provides-Extra: docs
Requires-Dist: mkdocs<2.0,>=1.6; extra == "docs"
Requires-Dist: mkdocs-material<9.6,>=9.5; extra == "docs"
Requires-Dist: mike<3.0,>=2.1; extra == "docs"
Requires-Dist: pymdown-extensions<11.0,>=10.9; extra == "docs"
Requires-Dist: mkdocstrings[python]>=1.0; extra == "docs"
Requires-Dist: griffe>=2.0; extra == "docs"
Provides-Extra: schematic
Requires-Dist: schemdraw<1.0,>=0.18; extra == "schematic"
Requires-Dist: networkx>=3.0; extra == "schematic"
Requires-Dist: cairosvg>=2.7; extra == "schematic"
Requires-Dist: anthropic>=0.34; extra == "schematic"
Provides-Extra: all
Requires-Dist: pulsim[data,dev,jupyter,schematic]; extra == "all"
Description-Content-Type: text/markdown

<div align="center">

```
██████╗ ██╗   ██╗██╗     ███████╗██╗███╗   ███╗
██╔══██╗██║   ██║██║     ██╔════╝██║████╗ ████║
██████╔╝██║   ██║██║     ███████╗██║██╔████╔██║
██╔═══╝ ██║   ██║██║     ╚════██║██║██║╚██╔╝██║
██║     ╚██████╔╝███████╗███████║██║██║ ╚═╝ ██║
╚═╝      ╚═════╝ ╚══════╝╚══════╝╚═╝╚═╝     ╚═╝
```

### Header-only C++23 + Python power-electronics simulator with an in-house sparse-LU kernel.

[![CI](https://github.com/lgili/Pulsim/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/lgili/Pulsim/actions/workflows/ci.yml)
[![Docs](https://github.com/lgili/Pulsim/actions/workflows/docs.yml/badge.svg?branch=main)](https://lgili.github.io/Pulsim/)
[![PyPI](https://img.shields.io/pypi/v/pulsim.svg?logo=python&logoColor=white&color=3776ab)](https://pypi.org/project/pulsim/)
[![Python](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-3776ab?logo=python&logoColor=white)](https://pypi.org/project/pulsim/)
[![C++23](https://img.shields.io/badge/C%2B%2B-23-00599c?logo=cplusplus&logoColor=white)](https://en.cppreference.com/w/cpp/23)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Citation](https://img.shields.io/badge/cite-CITATION.cff-orange.svg)](CITATION.cff)
[![v2.1.0 release](https://img.shields.io/badge/release-v2.1.0-success.svg)](CHANGELOG.md)

[**Docs**](https://lgili.github.io/Pulsim/) ·
[**Tutorials**](docs/tutorials/) ·
[**API reference**](docs/api-reference.md) ·
[**How Pulsim works**](docs/how-pulsim-works/) ·
[**Examples**](examples/scripts/) ·
[**Changelog**](CHANGELOG.md)

</div>

---

## TL;DR

Pulsim is a SPICE alternative tuned for the **switched-mode power
electronics** workload: PWL devices, finite-state topologies,
$10^8$-step transients. Where SPICE pays full assembly + LU
factorization on every step, Pulsim caches one state-space
per switch combination and updates only the columns of $L+U$
that actually changed.

The v1.3.0 sparse LU rewrite + the v1.4.0 generalised path-based
update framework give:

| Workload                            | n_state | Speedup vs baseline       |
|-------------------------------------|--------:|---------------------------|
| Buck single-bit Gray-code           | 14      | **2.81×**                 |
| Multi-bit δ=2 switch transition     | 14      | **1.58×** vs Eigen LU     |
| Parametric sweep (R/L/C)            | 14–26   | **3.0–3.7×** vs rebuild   |
| AC sweep complex LU                 | 8–32    | rough parity with Eigen   |

Both single-bit and (v1.4.0+) multi-bit / parametric paths run
through an **in-house C++23 sparse LU**. No KLU, no SuiteSparse,
no `Eigen::SparseLU<complex>` in production — only Eigen as a
matrix container.

---

## Why Pulsim

| Feature | What it buys you |
|---|---|
| **PLECS-style PWL cache** (Layer 4) | switched-converter transient in milliseconds, not minutes |
| **Header-only C++23 kernel** | drop `pulsim/` into your CMake via `pulsim::core`; no static-library link |
| **In-house sparse LU** (Pulsim's own, v1.3.0+) | path-based partial refactor for switch flips + parameter sweeps; zero third-party LU |
| **Python-first ergonomics** | `CircuitBuilder` takes string node names + SI units; one `SimulationResult` for transients, AC, sweeps, MC |
| **Mixed-domain control chain** | PID / comparator / rate-limiter / FOC / thermal at kernel speed (no Python interpreter per step) |
| **Frequency-domain analysis** | small-signal MNA Bode + swept-sine FRA + closed-loop GM/PM in the same surface |
| **Drop-in parameter sweep + MC** (v1.4.0+) | `sweep_path_aware` / `monte_carlo_path_aware` reuse the cached LU across all sweep points |

---

## Performance

Numbers captured 2026-05-24 on macOS / Apple Silicon (-O3 -DNDEBUG).
Reproducible end-to-end via `cmake --build build --target pulsim_benchmarks`
followed by `./build/core/pulsim_benchmarks "[rank1][microbench]"` (and
the analogous tags `[multi_bit]`, `[parametric]`, `[ac_sweep]`).

### 1) Single-bit rank-1 (v1.3.0, N-switch chain)

3-backend microbench. (A) baseline `solve` per-mask cache,
(B) Eigen sliding solver, (C) Pulsim path-based partial refactor.

| N | n_state | µs/solve (A) | µs/eigen (B) | µs/pulsim (C) | **C/A** |
|---:|---:|---:|---:|---:|---:|
| 4  | 6  | 6.74  | 6.15 | 2.30 | **2.93×** |
| 12 | 14 | 10.01 | 5.41 | 3.56 | **2.81×** |
| 16 | 18 | 12.15 | 7.08 | 4.31 | **2.82×** |
| 20 | 22 | 13.85 | 8.24 | 5.08 | **2.73×** |
| 24 | 26 | 16.41 | 9.83 | 6.13 | **2.68×** |

Zero fallbacks across all 1999 single-bit Gray-code flips × 8 N values.

### 2) Multi-bit path-union (v1.4.0, Pulsim ÷ Eigen sliding solver)

| N (n_state) | δ=1 | δ=2 | δ=3 | δ=4 |
|---|---:|---:|---:|---:|
| 8 (10)  | **3.12×** | 1.62× | 1.61× | 1.42× |
| 12 (14) | 1.72×     | 1.58× | 1.58× | 1.42× |
| 16 (18) | 1.56×     | 1.28× | 1.51× | 1.25× |
| 20 (22) | 1.36×     | 1.42× | 1.54× | 1.51× |
| 24 (26) | 1.55×     | 1.46× | 1.33× | 1.42× |

Hit rate decays gracefully: ~45 % path-union at δ=2 → ~10 % at δ=4
(rest fall back cleanly to full factorize).

### 3) Parametric sweep / Monte Carlo (v1.4.0, Pulsim ÷ legacy rebuild)

| n_state | 50 pts | 100 pts | 500 pts | 1000 pts |
|---:|---:|---:|---:|---:|
| 8  | **5.18×** | 3.29× | 3.55× | 3.68× |
| 14 | 3.57×     | 3.02× | 3.51× | 3.35× |
| 26 | 3.53×     | 3.31× | 3.38× | 3.40× |

**Zero fallbacks** across all 12 cells.

### 4) AC sweep complex LU (v1.4.0, Pulsim ÷ Eigen)

| n | µs/freq Eigen | µs/freq Pulsim | Pulsim ÷ Eigen | Parity Δ |
|---:|---:|---:|---:|---:|
| 8   | 8.98  | 5.48  | **0.61× (Pulsim faster)** | 1×10⁻²² |
| 16  | 8.16  | 7.64  | 0.94× | 5×10⁻²² |
| 32  | 13.98 | 14.56 | 1.04× | 3×10⁻²¹ |
| 64  | 28.49 | 33.36 | 1.17× | 1×10⁻²¹ |
| 128 | 46.13 | 91.52 | 1.98× (Eigen faster) | 4×10⁻²¹ |

Both solvers numerically interchangeable. v1.4.0 contribution is
**no third-party LU on the production path**; `Backend::Eigen` is
kept explicitly available as a paper-comparison baseline.

---

## Quick start

### Install from PyPI

```bash
pip install pulsim
```

Binary wheels for CPython 3.10–3.13 on macOS (Apple silicon), Linux
(manylinux x86_64) and Windows (x64). The only runtime dependency is
NumPy. Plotting and DataFrame export are optional extras:

```bash
pip install "pulsim[jupyter]"   # matplotlib (+ plotly, ipywidgets): p.scope, p.plot.*
pip install "pulsim[data]"      # pandas (+ xarray): SweepResult.to_dataframe()
```

### Build from source

Only needed to hack on the C++ kernel. You need a C++23 compiler and
CMake ≥ 3.20; everything else is header-only or fetched at configure
time:

| Dependency | Why |
|---|---|
| **C++23 compiler** | AppleClang 15+ / Clang 17+ / GCC 13+ |
| **CMake ≥ 3.20** | Build system (driven by scikit-build-core under `pip install`). |
| **Eigen 3.4+** | Header-only sparse linear algebra (matrix + vector containers). Pulsim ships its own sparse LU on top. A system copy is used if found, otherwise CMake downloads Eigen 3.4.0. |
| yaml-cpp, pybind11 | Fetched by CMake (pybind11 is taken from the build environment when present). |

```bash
# macOS (Homebrew)
brew install cmake ninja eigen

# Debian / Ubuntu
sudo apt-get install -y cmake ninja-build libeigen3-dev

# Fedora
sudo dnf install cmake ninja-build eigen3-devel
```

```bash
git clone https://github.com/lgili/Pulsim.git
cd Pulsim
pip install -e .
```

### First simulation — 8 lines

```python
import pulsim as p

b = p.CircuitBuilder()
b.add_voltage_source("Vin", "n0", "gnd", 5.0)
b.add_resistor      ("R1", "n0", "vc",   1000.0)
b.add_capacitor     ("C1", "vc", "gnd",  1e-6)

res = p.simulate(b, t_end=5e-3, dt=1e-5)
print(f"v_C(5 ms) = {res.states[-1][b.node_id_of('vc')]:.3f} V")   # 4.966 V
```

To see the waveform, install the `[jupyter]` extra (matplotlib) and add
one line:

<!-- doctest: requires=matplotlib -->
```python
p.scope(b, res, signals=["vc"])           # one-line waveform plot
```

### Parametric sweep — exploit the v1.4.0 path-based refactor

```python
import numpy as np
import pulsim as p

def make_rc(R=5.0, C=10e-6):
    b = p.CircuitBuilder()
    b.add_voltage_source("v1", "vin", "gnd", 10.0)
    b.add_resistor      ("R_load", "vin", "vout", R)
    b.add_capacitor     ("C_out", "vout", "gnd", C)
    return b

base = make_rc()
VOUT = base.node_id_of("vout")              # ask for the node by name

def vout_at_t_end(res, _params):
    return {"vout": float(res.states[-1][VOUT])}

# Sweep R_load through 100 values. v1.4.0 path-based refactor:
# build the cache ONCE, then reuse the L+U factors at each point.
out = p.sweep_path_aware(
    base,                                    # initial builder
    params={"R_load": np.linspace(1, 20, 100).tolist()},
    kpi_fn=vout_at_t_end,
    t_end=50e-6, dt=1e-6,                    # tau = R·C spans 10–200 µs
    verbose=False,
)
assert not out.failed, out.failed            # [(index, error), ...]
for R, v in zip(out.params["R_load"][:3], out.kpis["vout"][:3]):
    print(f"R_load = {R:6.3f} Ω  ->  vout = {v:.3f} V")
# With the [data] extra (pandas): out.to_dataframe() gives one row per run.
```

For Monte Carlo, swap `sweep_path_aware` →
`monte_carlo_path_aware(builder, distributions={"R_load": lambda r:
r.uniform(1, 20)}, n_samples=1000, ...)` and get the same ~3.4×
speedup over the legacy rebuild-per-sample pattern.

### Using Pulsim with AI assistants

The package ships a usage guide written for AI coding assistants (and
useful to people): the verified 2.x idioms, exact signatures, the traps
that return a plausible wrong number, and how to check a result. It
always matches the installed version:

```bash
python -m pulsim.guide          # print it (also: pulsim.ai_guide())
python -m pulsim.guide --path   # where the file is
```

Point your assistant at it, e.g. in `CLAUDE.md`, `AGENTS.md` or
`.github/copilot-instructions.md`: *"Before writing or changing code that
uses pulsim, run `python -m pulsim.guide` and follow it."* The docs also
publish [`llms.txt`](docs/llms.txt), an index for tools that read one.

---

## What's new in v2.1

Pulsim 2.1.0 keeps the defaults of the existing `simulate()` options. What
it adds sits behind new options that default off:

* the **exact engine**'s losses, recorders, sensitivities and output rule
  (`engine='exact'`, `output_rtol=`);
* a fourth-order L-stable integrator on the variable-step engine
  (`integrator='esdirk43'`);
* **ideal switches and diodes** in branch form (`form='branch'`) and
  complementarity mode selection (`mode_selection='lcp'`);
* **external devices** through a stable C ABI (`pulsim.add_external_device`,
  `pulsim.xdev`), new devices (thyristor family, MNA-native machines,
  permeance magnetics, Bergeron line, datasheet power MOSFET), periodic
  steady state, sensitivities and in-loop recorders.

It also corrects a long list of silent wrong answers, so **some 2.0
numbers move with default options**, and a few 2.0 calls now raise a named
error. Before upgrading a study whose numbers you have recorded, read the
[2.0 → 2.1 migration notes](docs/migration-guide.md#from-20-to-21) and the
2.1.0 entry of [`CHANGELOG.md`](CHANGELOG.md).

## What's new in v2.0

Pulsim 2.0.0 (2026-09-08) is a **breaking** release. Its rule
throughout: a named refusal beats a plausible wrong number. The full list
of breaking changes is the "headline breaks" part of the 2.0.0 entry in
[`CHANGELOG.md`](CHANGELOG.md); there is no separate 1.x → 2.0 porting
guide yet. ([`docs/migration-from-v1.md`](docs/migration-from-v1.md) is
older and different: it covers the C++ v1-runtime → v2 module split.)

* **`engine='auto'` is the default, and it routes.** Pass `dt` and you
  get the fixed-step `pwl` engine exactly as before; omit it and the
  engine picks, taking the variable-step TR-BDF2 path whenever the
  circuit qualifies. `result.engine_used` / `result.engine_route_reason`
  say what ran and why.
* **Device semantics changed.** `add_mosfet` / `add_mosfet_level1` add
  the intrinsic body diode `<name>_body` by default (`body_diode=False`
  / `with_body_diode=False` to opt out), so each MOSFET takes two
  switch bits — address them with `builder.switch_index_of(name)`. An
  IGBT can no longer conduct in reverse, and `add_three_phase_vsi`
  ships its freewheel diodes by default.
* **Refusals instead of guesses.** A part file without provenance, a
  PLECS file whose losses are a formula, a SPICE `.MODEL` the mapping
  cannot honour, a snapshot whose device histories do not match the
  circuit, and `steady_state` on a circuit whose one-period map is not
  affine are all refused by name. The parts shipped in `pulsim.lib` are
  synthetic illustrations and do not resolve by part number unless
  `allow_synthetic=True`.
* **Known engine defects are documented, not hidden.** Four are pinned
  by strict xfails that name the mechanism (see the changelog).
  [`docs/known-limitations.md`](docs/known-limitations.md) lists every
  known limitation of 2.0 with its measured effect and a workaround.

---

## What's NOT in Pulsim (intentionally)

Many simulators inherit a sprawling dependency tree from their
sparse-LU backend. Pulsim deliberately doesn't:

| ❌ Not used | Why |
|---|---|
| **SuiteSparse / KLU** | The v1.3.0 in-house LU replaces KLU completely. No SuiteSparse linkage, no licensing complexity. |
| **Eigen::SparseLU<complex>** | Replaced in v1.4.0 by the in-house complex specialisation. `Backend::Eigen` is retained explicitly as a paper-comparison baseline. |
| **dpsim / dwf / PSCAD vendored code** | Pulsim's algorithms are entirely first-party; the methods paper claims ours, not someone else's. |
| **Runtime Python in kernel** | The C++ kernel has zero Python dependency. `pybind11` only crosses the boundary at the API surface. |
| **Heavy installer / multi-step build** | `pip install pulsim` or `cmake --build`; no make-then-make-install dance. |

What IS used: **Eigen 3.4+** (header-only, as a sparse matrix
container) and **pybind11** (for the Python binding). That's it.

---

## Architecture

10 layers, bottom-up:

```
Layer 0:  Sparse LU + matrix containers      (in-house, real + complex)
Layer 1:  Topology graph + switch state mask
Layer 2:  Device models (R, L, C, MOSFET, IGBT, source, ...)
Layer 3:  Symbolic stamping helpers (Newton refresh)
Layer 4:  PWL state-space cache  ← refactor_parametric + solve_rank1
Layer 5:  Solver loop + event detection + run_transient
Layer 6:  CircuitBuilder (ergonomic API surface)
Layer 7:  YAML loader + reference circuits
Layer 8:  AC analysis (small-signal MNA Bode + FRA)
Layer 9:  Python facade (CircuitBuilder, simulate, sweep, plot)
```

See [`docs/how-pulsim-works/`](docs/how-pulsim-works/) for the
chapter-by-chapter walkthrough (18 figures, 11 chapters, MNA basics
through path-based partial refactor with full provenance).

---

## Validation

```bash
# C++ kernel tests (Catch2 binaries per layer)
ctest --test-dir build --output-on-failure
# 498 / 498 tests pass on a fresh checkout

# Python runtime tests
PYTHONPATH=build/python pytest python/tests -v
# 6 / 6 path-aware sweep tests pass

# Benchmark suite (opt-in, not in default ctest)
./build/core/pulsim_benchmarks "[rank1][microbench]"
./build/core/pulsim_benchmarks "[multi_bit][microbench]"
./build/core/pulsim_benchmarks "[parametric][microbench]"
./build/core/pulsim_benchmarks "[ac_sweep][microbench]"
```

Reference CSV traces for the 10 converter showcases live under
`benchmarks/baselines/`. A 1.0-native regression runner that consumes
them is on the roadmap.

---

## Roadmap

| Version | Highlight | Status |
|---|---|---|
| **v1.3.0** | In-house real sparse LU (KLU removed) | ✅ shipped |
| **v1.4.0** | Complex sparse LU + multi-bit + parametric refactor + Python helpers | ✅ shipped |
| v1.5.0 | Reachability-based sparse triangular solve (close the n ≥ 64 Eigen gap on AC sweep) | not shipped — still open (v1.5.0 was never tagged) |
| v1.6.0 | AC-sweep symbolic-reuse: amortise `analyze` across all frequencies | not shipped — still open (v1.6–v1.8 shipped other work, see CHANGELOG) |
| v1.7.0 | BTF block-triangular ordering — composes with path-union for an extra ~2× on wide multi-bit transitions | not shipped — still open (v1.6–v1.8 shipped other work, see CHANGELOG) |
| **v2.0.0** | Breaking: `engine='auto'` routing, devices that refuse instead of guessing — see [What's new in v2.0](#whats-new-in-v20) | ✅ released 2026-09-08 |
| **v2.1.0** | Opt-in exact-engine features, `esdirk43`, ideal switches, external devices, new devices; silent wrong answers fixed — see [What's new in v2.1](#whats-new-in-v21) | release candidate |

---

## Citation

If you use Pulsim in academic work, please cite:

```bibtex
@software{pulsim,
  author  = {Gili, Luiz Carlos},
  title   = {Pulsim: A piecewise-linear state-space simulator for switched-mode power electronics},
  version = {2.1.0},
  url     = {https://github.com/lgili/Pulsim},
  year    = {2026},
}
```

Machine-readable [`CITATION.cff`](CITATION.cff) is in the repo root.

---

## Contributing

PRs welcome. Workflow:

1. Open an issue describing the change (or pick one from the
   [issue tracker](https://github.com/lgili/Pulsim/issues)).
2. For non-trivial work, draft an OpenSpec proposal under
   `openspec/changes/<name>/` (see existing proposals under
   `openspec/changes/archive/` for the format).
3. Implement + add tests under `core/tests/` (C++) or
   `python/tests/` (Python).
4. Ensure all 498 C++ tests + 6 Python tests pass and
   `openspec validate --strict` is green.
5. Open the PR; CI runs on every push.

See [`CONTRIBUTING.md`](CONTRIBUTING.md) for the full guide.

---

## License

[MIT](LICENSE) © 2026 Luiz Carlos Gili. Free for academic +
commercial use; please cite when used in academic work.

---

## Documentation deployment

Docs are published by `.github/workflows/docs.yml` using
MkDocs Material + mike:

- **PR**: strict docs build (no deploy)
- **`main`**: deploys to the `dev` channel
- **`vX.Y.Z` tag**: deploys release docs and updates `latest`

In repository settings, set **Pages Source** to **GitHub Actions**.

Live docs: [https://lgili.github.io/Pulsim/](https://lgili.github.io/Pulsim/)
