Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 40 additions & 126 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,137 +5,51 @@

The code should be easy to read and modify, the main language is Python -- 2 or 3 -- and it is platform-independent.

News!
-----
## Welcome to the PYPY branch of `ahkab`!

- Ahkab v0.16 was released on May 3 2015, including new features, many bugfixes and improved documentation. It is recommended to upgrade. Check out [the release notes](https://github.com/ahkab/ahkab/releases/tag/v0.16) for more!
- The whole codebase has been going through a (yet incomplete) refactoring and documenting effort. The [new documentation is available on RTD](http://ahkab.readthedocs.org/en/latest/).
* We try to run always against the latest [pypy](http://pypy.org/).
* Right now, most efforts are geared towards the Python 2 implementation, but that's just temporary.

My resources are limited these days, so the much-needed work is proceeding slowly, albeit hopefully steadily. If you are interested and you would like to contribute to refactoring or documenting a particular feature, it would be very welcome.
All which is not strictly necessary is disabled in this experimental branch.

[![Build Status](https://travis-ci.org/ahkab/ahkab.png?branch=master)](https://travis-ci.org/ahkab/ahkab) [![Coverage Status](https://coveralls.io/repos/ahkab/ahkab/badge.png?branch=master)](https://coveralls.io/r/ahkab/ahkab?branch=master) [![PyPi version](http://img.shields.io/badge/version-0.16-brightgreen.png)](https://pypi.python.org/pypi/ahkab/) [![GPLv2 license](http://img.shields.io/badge/license-GPL%20v2-brightgreen.png)](https://raw.githubusercontent.com/ahkab/ahkab/master/LICENSE)
[![DOI](https://zenodo.org/badge/doi/10.5281/zenodo.17404.svg)](http://dx.doi.org/10.5281/zenodo.17404)
## Performance

Supported simulations:
----------------------
Although numeric code does not really work for now with Pypy (we need `scipy`!), symbolic calculations are performed
very quickly on this alternative Python implementation.

- Numeric:
- **Operating point**, with guess computation to speed up the solution. See example: [Downscaling current mirror](https://ahkab.readthedocs.org/en/latest/examples/OP_simulation.html)
- **DC sweep**
- **Transient analysis**, available differentiation formulas: implicit Euler, trapezoidal, gear orders from 2 to 5. See for example the [simulation of a Colpitts Oscillator](https://ahkab.readthedocs.org/en/latest/examples/Transient-Example.html).
- **AC analysis**
- **PZ** analysis
- **Periodic steady state analysis** of non-autonomous circuits, *time* *domain* shooting and brute-force algorithms.
- Symbolic:
- **Small signal analysis**, AC or DC, with extraction of transfer functions, DC gain, poles and zeros. Various [symbolic analysis examples on this page](https://ahkab.readthedocs.org/en/latest/examples/Symbolic-simulation.html).
Take a look at PYPY running 5+ times faster than CPython with no speed optimization on our side:

The results are saved to disk, plotted or printed to stdout and can be read/processed by the most common tools (eg. [Octave](http://www.gnu.org/software/octave/), [gnuplot](http://www.gnuplot.info/), [Matlab](http://www.mathworks.com/products/matlab/), [gwave](http://www.telltronics.org/software/gwave/) and others)

Install
-------

The program requires:

- the Python interpreter version 2 or 3 (at least v.2.6 for Python2, v.3.3 for Python3),
- numpy>=1.7.0, scipy>=0.14.0, sympy>=0.7.6 and tabulate>=0.7.3.

Matplotlib is strongly recommended and no plotting will work without.

If you need more information about the dependencies, check the [Install notes](https://ahkab.readthedocs.org/en/latest/help/Install-Notes.html).

Usage
-----

### 1. `ahkab` can be run as a Python library

<img src="https://rawgithub.com/ahkab/ahkab/master/doc/images/readme_example/pbf.svg" alt="Example schematic: a 5th order 1kHz band-pass Butterworth filter"/>

``` {.sourceCode .python}
from 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(2.*np.pi*.97e3, 2.*np.pi*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')
```

<img src="https://rawgithub.com/ahkab/ahkab/master/doc/images/readme_example/bpf_results.svg" alt="Example: AC simulation results"/>

#### 2. `ahkab` can be run from the command line with a netlist file

The syntax is:

`$ python ahkab -o graph.dat <netlist file>`

See `ahkab --help` for command line switches, [also online on the documentation pages.](http://ahkab.readthedocs.org/en/latest/help/Command-Line-Help.html)

### Documentation

The [documentation is available on RTD](http://ahkab.readthedocs.org/en/latest/).

There, you can find a [documentation](http://ahkab.readthedocs.org/en/latest/ahkab.html) and [examples](http://ahkab.readthedocs.org/en/latest/examples/Python_API.html) regarding how to simulate from a Python script.

Refer to the [netlist syntax page](http://ahkab.readthedocs.org/en/latest/help/Netlist-Syntax.html) 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.

### Development model

- The development happens on the [github repository](https://github.com/ahkab/ahkab),
- Mostly on the master branch, with feature branch being created only for special purposes or non-trivial features.
- Snapshots are released on a (hopefully) regular basis and are available on the [Releases pages, complete with changelog](https://github.com/ahkab/ahkab/releases) and on [PYPI](https://pypi.python.org/pypi/ahkab/)

Patches and pull requests are welcome!

### How this project was born

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](http://brambilla.dei.polimi.it/) 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

### Bugs and patches

Does it work? Bugs? Do you have patches? Did you run some noteworthy simulation? Let me know! Feedback is very welcome, my [email address](http://tinymailto.com/5310) is available after a captcha.

### Support the development with a donation

If you wish to support the development of `ahkab`, ***please donate to cancer research:***

- [Association for International Cancer Research (eng)](http://www.aicr.org.uk/donate.aspx),

or

- [Fond. IRCCS Istituto Nazionale dei Tumori (it)](http://www.istitutotumori.mi.it/modules.php?name=Content&pa=showpage&pid=24).

### Credits

**Authors:** [Giuseppe Venturini](https://github.com/ggventurini), with contributions from [Ian Daniher](https://github.com/itdaniher) and [Rob Crowther](https://github.com/weilawei).

**Code:** the module `py3compat.py` is (c) 2013 - the Jinja team.

**Dependencies:** many thanks to the authors of `numpy`, `scipy`, `sympy`, ``matplotlib`` and ``tabulate``!

Runtime [s]
0.000 1.295 2.590 3.885 5.180 6.475 7.770 9.065 10.360
| | | | | | | | |
Nodes +--------------------------------------------------------------------------------->
|
2.000 +o
|=*
|
4.000 +o
|==*
|
6.000 +o
|===*
|
8.000 +----o
|=====*
|
10.000 +--------------------o
|=======*
|
12.000 +-------------------------------------------------------------------------------o
|============*
|
+--------------------------------------------------------------------------------->
| | | | | | | | |
0.000 1.295 2.590 3.885 5.180 6.475 7.770 9.065 10.360
Runtime [s]

LEGEND:
--* Ahkab 0.16-pypy, running on CPython, R2R ladder DAC
==o Ahkab 0.16-pypy, running on Pypy2, R2R ladder DAC
```
48 changes: 35 additions & 13 deletions ahkab/ahkab.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,17 @@
from optparse import OptionParser

import numpy as np
import scipy as sp

# There's no scipy on pypy
try:
import scipy as sp
scipy_available = True
except ImportError:
scipy_available = False

import sympy

# no matplotlib -> no plotting
import tabulate
try:
import matplotlib
Expand All @@ -146,12 +155,14 @@
plotting_available = False

# analyses
from . import dc_analysis
from . import transient
from . import ac
from . import pss
if scipy_available:
from . import dc_analysis
from . import transient
from . import ac
from . import pss
from . import pz

from . import symbolic
from . import pz

# parser
from . import netlist_parser
Expand Down Expand Up @@ -289,7 +300,7 @@ def new_dc(start, stop, points, source, sweep_type='LINEAR', guess=True, x0=None
'verbose': verbose}


def new_tran(tstart, tstop, tstep, x0='op', method=transient.TRAP,
def new_tran(tstart, tstop, tstep, x0='op', method='TRAP',
use_step_control=True, outfile=None, verbose=0):
"""Assembles a TRAN analysis and returns the analysis object.

Expand Down Expand Up @@ -782,10 +793,17 @@ def process_postproc(postproc_list, title, results, outfilename):
plotting.show_plots()
return None

analysis = {'op': dc_analysis.op_analysis, 'dc': dc_analysis.dc_analysis,
'tran': transient.transient_analysis, 'ac': ac.ac_analysis,
'pss': pss.pss_analysis, 'symbolic': symbolic.symbolic_analysis,
'temp': set_temperature, 'pz':pz.calculate_singularities}
if scipy_available:
analysis = {'op': dc_analysis.op_analysis,
'dc': dc_analysis.dc_analysis,
'tran': transient.transient_analysis,
'ac': ac.ac_analysis,
'pss': pss.pss_analysis,
'symbolic': symbolic.symbolic_analysis,
'temp':set_temperature,
'pz':pz.calculate_singularities}
else:
analysis = {'symbolic': symbolic.symbolic_analysis}


def main(filename, outfile="stdout", verbose=3):
Expand Down Expand Up @@ -825,7 +843,11 @@ def main(filename, outfile="stdout", verbose=3):
printing.print_info_line(
(" Python %s" % (sys.version.split('\n')[0],), 6), verbose)
printing.print_info_line((" Numpy %s" % (np.__version__), 6), verbose)
printing.print_info_line((" Scipy %s" % (sp.__version__), 6), verbose)
if scipy_available:
printing.print_info_line((" Scipy %s" % (sp.__version__), 6), verbose)
else:
printing.print_info_line(
(" Scipy not found: functionality will be reduced.", 6), verbose)
printing.print_info_line((" Sympy %s" % (sympy.__version__), 6), verbose)
printing.print_info_line((" Tabulate %s" % (tabulate.__version__), 6), verbose)
if plotting_available:
Expand All @@ -837,7 +859,7 @@ def main(filename, outfile="stdout", verbose=3):
(matplotlib.matplotlib_fname()), 6), verbose)
else:
printing.print_info_line(
(" Matplotlib not found.", 6), verbose)
(" Matplotlib not found, plotting disabled.", 6), verbose)


read_netlist_from_stdin = (filename is None or filename == "-")
Expand Down
9 changes: 7 additions & 2 deletions ahkab/dc_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@

import numpy as np
import numpy.linalg
import scipy.sparse
import scipy.sparse.linalg

try:
import scipy.sparse
import scipy.sparse.linalg
except ImportError:
pass
# pypy run! no numeric code will run!

from . import devices
from . import diode
Expand Down
9 changes: 7 additions & 2 deletions ahkab/diode.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@
division, print_function)

import numpy as np
import scipy, scipy.optimize

from scipy.optimize import newton
try:
import scipy, scipy.optimize
from scipy.optimize import newton
except ImportError:
pass
# pypy run. If the user will try numeric stuff we'll fail
# ungracefully. After all, this is an experiment!

from . import constants
from . import printing
Expand Down
11 changes: 8 additions & 3 deletions ahkab/ekv.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,15 @@
from __future__ import (unicode_literals, absolute_import,
division, print_function)

import scipy, scipy.optimize
import math
try:
import scipy, scipy.optimize
from scipy.optimize import newton
except ImportError:
pass
# pypy run
# no numerical stuff

from scipy.optimize import newton
import math

from . import constants
from . import options
Expand Down
23 changes: 17 additions & 6 deletions ahkab/netlist_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@
import os

from . import circuit
from . import dc_analysis

try:
from . import dc_analysis
except ImportError:
#will fail on pypy
# no numeric simulations there
pass

from . import devices
from . import diode
from . import mosq
Expand All @@ -122,12 +129,16 @@
from . import options

# analyses syntax
from .dc_analysis import specs as dc_spec
from .ac import specs as ac_spec
from .transient import specs as tran_spec
from .pss import specs as pss_spec
from .pz import specs as pz_specs
try:
from .dc_analysis import specs as dc_spec
from .ac import specs as ac_spec
from .transient import specs as tran_spec
from .pss import specs as pss_spec
from .pz import specs as pz_specs
except ImportError:
dc_spec = ac_spec = tran_spec = pss_spec = pz_specs = {}
from .symbolic import specs as symbolic_spec

from .devices import time_fun_specs
from .devices import sin, pulse, exp

Expand Down
11 changes: 6 additions & 5 deletions ahkab/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@
plotting_display_figsize = (12.94, 8)
#: Default size for plots saved to disk.
plotting_save_figsize = (20, 10)
# plotting_rcParams['font.family'] = 'Baskerville'
plotting_rcParams['axes.labelsize'] = 11
plotting_rcParams['xtick.labelsize'] = 11
plotting_rcParams['ytick.labelsize'] = 11
plotting_rcParams['legend.fontsize'] = 11
if plotting_rcParams is not None:
# plotting_rcParams['font.family'] = 'Baskerville'
plotting_rcParams['axes.labelsize'] = 11
plotting_rcParams['xtick.labelsize'] = 11
plotting_rcParams['ytick.labelsize'] = 11
plotting_rcParams['legend.fontsize'] = 11
6 changes: 5 additions & 1 deletion ahkab/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,11 @@ def test():
import numpy as np
import sympy

from scipy.interpolate import InterpolatedUnivariateSpline
try:
from scipy.interpolate import InterpolatedUnivariateSpline
except ImportError:
pass
# pypy run!

from nose.tools import ok_, nottest
from nose.plugins.skip import SkipTest
Expand Down
Loading