# Python scripting: the same sweep in the app and from Python

The `powercraft` Python package drives the same engine as the window. Every command you can run from a menu you can run from Python, and waveforms come back as numpy arrays with every sample. You sweep the buck's switching frequency in the app first, then write the same sweep in a dozen lines of Python and plot it with matplotlib against the textbook formula. About 15 minutes.

Pictures: a numbered red box is what you click or read, `[Run]` is a button, `Analysis › Parameter Sweep` a menu path.

## 1. Open the Parameter Sweep

Choose `File › Open Example › DC-DC › Buck converter, textbook`, then open the `Analysis` menu.

![The Analysis menu with Parameter Sweep, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/python/01-sweep-menu.png)

1. `Analysis`.
2. `Parameter Sweep`.

## 2. Sweep the switching frequency

The buck's circuit variables are `Vin`, `D` and `fsw`. Sweep `fsw` and measure the inductor's ripple.

![The Parameter Sweep form filled in, marked 1 to 6](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/python/02-sweep-form.png)

1. `Variable`: `fsw`.
2. `Values`: `25e3, 50e3, 100e3, 200e3`. They replace `Start`, `End` and `Points`.
3. `Signal`: `i(L1)`.
4. `Statistic`: `pp`, peak to peak.
5. `Window start`: `3m`, after the start-up.
6. `[Run]`.

## 3. Read the sweep

![The sweep: i(L1) ripple falls from 3.6 A at 25 kHz to 0.46 A at 200 kHz, marked 1 to 3](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/python/03-sweep-result.png)

1. `4 runs, 0 failed`. The runs go in parallel. The plot names its axes: `fsw` with ticks such as `50kHz`, and `i(L1) pp` in A.
2. `Copy CSV` puts the rows on the clipboard.
3. `Show data` lists them:

| fsw | i(L1) peak to peak | Vout (1 − D) / (L fsw) |
|---|---|---|
| 25 kHz | 3.62 A | 3.60 A |
| 50 kHz | 1.81 A | 1.80 A |
| 100 kHz | 0.91 A | 0.90 A |
| 200 kHz | 0.46 A | 0.45 A |

The ripple halves each time the frequency doubles, as the textbook says.

## 4. Install the Python package

The package is a wheel for your platform, for Python 3.9 and later:

```sh
pip install powercraft-*-cp39-abi3-macosx_11_0_arm64.whl   # the file you were given
pip install matplotlib                                          # for the plot
```

From the source tree, `cargo xtask python --wheel --release` builds the wheel (it needs [maturin](https://www.maturin.rs)).

## 5. The same sweep in Python

`docs/tutorials/python/ripple_sweep.py` opens the example, sets `fsw`, runs, and reads `i(L1)` as a numpy array after 3 ms:

```python
import powercraft

s = powercraft.Session()
s.execute("file.openExample", {"name": "buckIdeal"})
for f in [25e3, 50e3, 100e3, 200e3]:
    s.execute("circuit.variables", {"set": {"fsw": f}})
    s.run()
    i_l = s.waveforms(["i(L1)"], t0=3e-3)["i(L1)"]
    print(f"{f / 1e3:6.0f} kHz  {i_l.max() - i_l.min():.3f} A")
```

- `Session.execute(command, params)` runs any engine command by its id (the manual names them), and returns its reply as dicts and lists.
- `Session.run()` is `simulation.run`.
- `Session.waveforms(signals, t0, t1)` returns `"time"` and one array per signal, every sample.

Run the whole script; it prints the table and writes the plot:

```sh
python docs/tutorials/python/ripple_sweep.py ripple-sweep.png
```

```text
    25 kHz  3.617 A  (textbook 3.600 A)
    50 kHz  1.811 A  (textbook 1.800 A)
   100 kHz  0.911 A  (textbook 0.900 A)
   200 kHz  0.462 A  (textbook 0.450 A)
```

## 6. The plot

The script writes two matplotlib plots to `ripple-sweep.png`, outside the app, so this page shows no picture of them. Left, the inductor current at 25 kHz over the last 0.2 ms: a triangle 3.6 A high around the 5 A load. Right, on log axes, the four simulated points on the textbook line Vout (1 − D) / (L fsw). The same numbers as the app's sweep in step 3, because it is the same engine.

`Session.sweep(part, key, values, measure)` does the loop for a part's parameter and undoes its changes afterwards, `Session.dataframe()` gives a pandas DataFrame, and `apps/powercraft-python/examples/design_simulate_plot.ipynb` is a Jupyter notebook. See the [manual, chapter 17](https://voltecx-web.nguyenvanqui291.workers.dev/docs/manual/17-python/).

Back to the [learning path](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/).
