a SPICE-like electronic circuit simulator written in Python
a SPICE-like electronic circuit simulator written in Python
The code should be easy to read and modify, the main language is Python -- 2 or 3 -- and it is platform-independent.
If you are interested in the project or the technology, have an issue with an example, or you would like to contribute by requesting, refactoring or documenting a particular feature, please reach out to us on GitHub!
The results are saved to disk, plotted or printed to stdout and can be read/processed by the most common tools (eg. Octave, gnuplot, Matlab, gwave and others)
The program requires:
Matplotlib is strongly recommended and no plotting will work without.
If you need more information about the dependencies, check the Install notes.
ahkab
can be run as a Python libraryfrom ahkab import new_ac, run
from ahkab.circuit import Circuit
from ahkab.plotting import plot_results # calls matplotlib for you
import numpy as np
# Define the circuit
cir = Circuit('Butterworth 1kHz band-pass filter')
cir.add_vsource('V1', 'n1', cir.gnd, dc_value=0., ac_value=1.)
cir.add_resistor('R1', 'n1', 'n2', 50.)
cir.add_inductor('L1', 'n2', 'n3', 0.245894)
cir.add_capacitor('C1', 'n3', 'n4', 1.03013e-07)
cir.add_inductor('L2', 'n4', cir.gnd, 9.83652e-05)
cir.add_capacitor('C2', 'n4', cir.gnd, 0.000257513)
cir.add_inductor('L3', 'n4', 'n5', 0.795775)
cir.add_capacitor('C3', 'n5', 'n6', 3.1831e-08)
cir.add_inductor('L4', 'n6', cir.gnd, 9.83652e-05)
cir.add_capacitor('C4', 'n6', cir.gnd, 0.000257513)
cir.add_capacitor('C5', 'n7', 'n8', 1.03013e-07)
cir.add_inductor('L5', 'n6', 'n7', 0.245894)
cir.add_resistor('R2', 'n8', cir.gnd, 50.)
# Define the analysis
ac1 = new_ac(.97e3, 1.03e3, 1e2, x0=None)
# run it
res = run(cir, ac1)
# plot the results
plot_results('5th order 1kHz Butterworth filter', [('|Vn8|',"")], res['ac'],
outfilename='bpf_transfer_fn.png')
ahkab
can be run from the command line with a netlist fileThe syntax is:
`$ python ahkab -o graph.dat <netlist file>`
See ahkab --help
for command line switches, also online on the documentation pages.
The documentation is available on RTD.
There, you can find a documentation and examples regarding how to simulate from a Python script.
Refer to the netlist syntax page if you prefer to write netlist files that describe the circuit.
Experience with running SPICE or related commercial simulators can be very useful: this is not for the faint of heart.
Patches and pull requests are welcome!
Writes @ggventurini:
"This project was born when I was an enthusistic undergrad, apparently with plenty of free time, attending "Simulazione Circuitale" (Circuit Simulation) taught by Prof. A. Brambilla back in Italy at the Polytechnic University of Milan.
I am grateful to prof. Brambilla for teaching one of the most interesting courses of my university years. -GV"
Does it work? Bugs? Do you have patches? Did you run some noteworthy simulation? Let us know on GitHub! You can also reach the maintainer at @itdaniher @gmail.com, he's happy to talk about Python.
If you wish to support the development of ahkab
, please donate to cancer research:
or
Authors: Giuseppe Venturini, with contributions from Ian Daniher, Rob Crowther, and KOLANICH.
Maintainer: Ian Daniher
Code: the module py3compat.py
is (c) 2013 - the Jinja team.
Dependencies: many thanks to the authors of numpy
, scipy
, sympy
, matplotlib
and tabulate
!