aboutsummaryrefslogtreecommitdiffstats
path: root/examples/rc_lp.py
blob: 0a79b60c234d67199263388bd52c05817496ef6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3
"""Example: run a plain ngspice netlist with kisim -- no KiCad involved.

    micromamba run -n kicad_work python examples/rc_lp.py
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

import numpy as np

from kisim import KiSim

sim = KiSim("rc_lp.cir", tran="1u 2m")   # .cir input: used as-is, no kicad-cli
r = sim.run()                            # -> results/run.cir + run.raw (cached)

tau = 1e3 * 100e-9                       # R1 * C1
vout = r.v("vout")
print(f"5 V step into 1 kohm + 100 nF: tau = {tau * 1e6:.0f} us")
print(f"V(vout) at 1 tau: {np.interp(tau, r.t, vout):.3f} V   (1 - 1/e of 5 V)")
print(f"V(vout) settled:  {vout[-1]:.3f} V")

r.plot(["vin", "vout"], title="RC low-pass: 5 V step", file=sim.workdir / "rc_lp.png")
print("plot:", sim.workdir / "rc_lp.png")