Skip to main content

Lcapy is a Python package for linear circuit analysis. It uses SymPy for symbolic mathematics.

Run tests and checks Binder Documentation

Lcapy can symbolically analyse circuits described with netlists or by series/parallel combinations of components. It can also manipulate continuous-time and discret-time expressions.

Comprehensive documentation can be found at https://lcapy.readthedocs.io/en/latest/

Circuit analysis

The circuit is described using netlists, similar to SPICE, with arbitrary node names (except for the ground node which is labelled 0). The netlists can be loaded from a file or created at run-time. For example:

>>> from lcapy import Circuit, s, t
>>> cct = Circuit("""
... Vs 2 0 {5 * u(t)}
... Ra 2 1
... Rb 1 0
... """)

The circuit can then be interrogated to determine branch currents, branch voltages, and node voltages (with respect to the ground node 0). For example:

>>> cct[1].V(t)
5⋅R_b⋅u(t)
──────────
 Rₐ + R_b
>>> cct.Ra.I(t)
 5⋅u(t)
────────
Rₐ + R_b
>>> cct.Ra.V(s)
   5⋅Rₐ
────────────
s⋅(Rₐ + R_b)

One-port networks

One-port networks can be created by series and parallel combinations of other one-port networks. The primitive one-port networks are the following ideal components:

  • V independent voltage source
  • I independent current source
  • R resistor
  • C capacitor
  • L inductor

These components are converted to s-domain models and so capacitor and inductor components can be specified with initial voltage and currents, respectively, to model transient responses.

The components have the following attributes:

  • Zoc open-circuit impedance
  • Ysc short-circuit admittance
  • Voc open-circuit voltage
  • Isc short-circuit current

The component values can be specified numerically or symbolically using strings, for example,

>>> from lcapy import Vdc, R, L, C, s, t
>>> R1 = R('R_1')
>>> L1 = L('L_1')
>>> a = Vdc(10) + R1 + L1

Here a is the name of the network formed with a 10 V DC voltage source in series with R1 and L1.

The s-domain open circuit voltage across the network can be printed with:

>>> a.V(s)
10/s

The time domain open circuit voltage is given by:

>>> a.V(t)
10

The s-domain short circuit current through the network can be printed with:

>>> a.Isc(s)
10/(L_1*s**2 + R_1*s)

The time domain short circuit current is given by:

>>> a.Isc(t)
10/R_1

If you want units displayed:

>>> state.show_units=True
>>> a.Isc(t)
10/R_1.A

Two-port networks

One-port networks can be combined to form two-port networks. Methods are provided to determine transfer responses between the ports.

Here's an example of creating a voltage divider (L section)

>>> from lcapy import *
>>> a = LSection(R('R_1'), R('R_2'))

Limitations

  1. Non-linear components cannot be modelled (apart from a linearisation around a bias point).

  2. High order systems can go crazy.

  3. Some two-ports generate singular matrices.

Schematics

LaTeX schematics can be generated using circuitikz from the netlist. Additional drawing hints, such as direction and size are required.

>>> from lcapy import Circuit
>>> cct = Circuit("""
... P1 1 0; down
... R1 1 3; right
... L1 3 2; right
... C1 3 0_1; down
... P2 2 0_2; down
... W 0 0_1; right
... W 0_1 0_2; right""")
>>> cct.draw(filename='pic.tex')

In this example, P denotes a port (open-circuit) and W denotes a wire (short-circuit). The drawing hints are separated from the netlist arguments by a semicolon. They are a comma separated list of key-value pairs except for directions where the dir keyword is optional. The symbol label can be changed using the l keyword; the voltage and current labels are specified with the v and i keywords. For example,

>>> from lcapy import Circuit
>>> cct = Circuit("""
... V1 1 0; down
... R1 1 2; left=2, i=I_1, v=V_{R_1}
... R2 1 3; right=2, i=I_2, v=V_{R_2}
... L1 2 0_1; down, i=I_1, v=V_{L_1}
... L2 3 0_3; down, i=I_1, v=V_{L_2}
... W 0 0_3; right
... W 0 0_1; left""")
>>> cct.draw(scale=3, filename='pic2.svg')

The drawing direction is with respect to the positive node; i.e., the drawing is performed from the positive to the negative node. Since lower voltages are usually lower in a schematic, then the direction of voltage sources and ports is usually down.

By default, component (and current) labels are drawn above horizontal components and to the right of vertical components. Voltage labels are drawn below horizontal components and to the left of vertical components.

Node names containing a dot or underscore are not displayed.

Jupyter notebooks

Lcapy can be used with Jupyter Notebooks. For a number of examples see https://github.com/mph-/lcapy/tree/master/doc/examples/notebooks . These include:

Installation in Google Colab

To use Lcapy in Google Colab, run the following commands in the Colab notebook:

!pip install pdflatex
!sudo apt-get install texlive-latex-recommended
!sudo apt install texlive-latex-extra
!sudo apt install dvipng
!pip install lcapy

This will install all the required packages to run Lcapy on Colab.

Open In Colab

Documentation

For comprehensive documentation, see http://lcapy.readthedocs.io/en/latest (alternatively, the documentation can be viewed in a web browser after running 'make doc' in the top-level directory).

For release notes see http://lcapy.readthedocs.io/en/latest/releases.html

For another view on Lcapy see https://blog.ouseful.info/2018/08/07/an-easier-approach-to-electrical-circuit-diagram-generation-lcapy/

Citation

To cite Lcapy in publications use

Hayes M. 2022. Lcapy: symbolic linear circuit analysis with Python. PeerJ Computer Science 8:e875 https://doi.org/10.7717/peerj-cs.875

A BibTeX entry for LaTeX users is

@article{10.7717/peerj-cs.875,
 title = {Lcapy: symbolic linear circuit analysis with {Python}},
 author = {Hayes, Michael},
 year = 2022,
 month = Feb,
 keywords = {Linear circuit analysis, symbolic computation, Python},
 pages = {e875},
 journal = {PeerJ Computer Science},
 issn = {2376-5992},
 url = {https://doi.org/10.7717/peerj-cs.875},
 doi = {10.7717/peerj-cs.875}
}

Copyright 2014--2022 Michael Hayes, UCECE

Metadata

Release files for lcapy 1.26

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lcapy 1.26
File Size Uploaded
lcapy-1.26.tar.gz 578.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lcapy 1.26
File Interpreter ABI Platform
lcapy-1.26-py3-none-any.whl Python 3 none any Details

Total release size: 1.0 MB

Release files / lcapy-1.26.tar.gz

Download URL lcapy-1.26.tar.gz
Size 578.4 kB
Tags Source
SHA-256 checksum
How to use checksums
edfc9635cc1ac62351038efc48b01e2f55620154115b19d22e36c4f8655e9220
BLAKE2b-256 checksum
How to use checksums
706ee7498864d5dfc7d6d7fbe6fda81b24c5caf74e6202cfa762fe9eeb34d0c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.10

Release files / lcapy-1.26-py3-none-any.whl

Download URL lcapy-1.26-py3-none-any.whl
Size 452.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b9f6091133bbd923e7fc89a71b185edb0fd141d0e46a410bafaa7b40b82c715f
BLAKE2b-256 checksum
How to use checksums
72a87baabf86871aecb7ce079a1f92b1a867097efe373bb0d2980e961012f738
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.26 This release

2 release files

1.24

2 release files

1.23

2 release files

1.22

2 release files

1.21

2 release files

1.20

2 release files

1.19

2 release files

1.17

2 release files

1.16

2 release files

1.15

2 release files

1.14

2 release files

1.12

2 release files

1.11

2 release files

1.10.1

2 release files

1.10

2 release files

1.9.1

2 release files

1.9

2 release files

1.8

2 release files

1.7

2 release files

1.6

2 release files

1.5.1

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

0.99

2 release files

0.98

2 release files

0.97

2 release files

0.96

2 release files

0.95

2 release files

0.94

2 release files

0.93

2 release files

0.92

2 release files

0.91

2 release files

0.90

2 release files

0.88

2 release files

0.87

2 release files

0.86

2 release files

0.85

2 release files

0.84

2 release files

0.83

2 release files

0.82

2 release files

0.81

2 release files

0.80

2 release files

0.77

2 release files

0.76

2 release files

0.75

2 release files

0.74

2 release files

0.73

2 release files

0.72

2 release files

0.71

2 release files

0.70

2 release files

0.69

2 release files

0.67.0

2 release files

0.66.0

2 release files

0.64.0

2 release files

0.63

2 release files

0.62

2 release files

0.61.0

2 release files

0.60.5

2 release files

0.60.4

2 release files

0.60.3

2 release files

0.60.2

2 release files

0.60

2 release files

0.52

2 release files

0.51

2 release files

0.50.3

2 release files

0.50.0

2 release files

0.49.1

2 release files

0.49.0

2 release files

0.46.1

2 release files

0.46.0

2 release files

0.42-0

2 release files

0.40.1

2 release files

0.39.0

2 release files

0.38.9

2 release files

0.38.7

2 release files

0.38.1

2 release files

0.38.0

1 release file

0.37.0

1 release file

0.36.2

1 release file

0.36.0

3 release files

0.35.0

2 release files

0.34.0

2 release files

0.33.4

2 release files

0.6.14

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page