Skip to main content

Lightweight circuit simulator (single-file)

Project description

pycircuit_new

Lightweight single-file circuit simulator using Modified Nodal Analysis (MNA).

pycircuit_new is a compact, single-file Python library for building and simulating simple electrical networks made of resistors, LEDs, ideal voltage sources, and capacitors. It focuses on clarity and a small API surface so you can quickly add circuits, run DC or transient simulations, and inspect voltages and currents.


Highlights

  • Single-file library, easy to embed or publish as a package.
  • DC solver using Modified Nodal Analysis. Works with one ideal voltage source per circuit.
  • Time-domain transient simulation with companion models for capacitors, supporting backward Euler and trapezoidal integration.
  • Simple component classes and a user-friendly Circuit wrapper for common tasks.
  • Optional schematic drawing using schemdraw for quick visualisation.

Requirements

  • Python 3.8 or newer.

  • Runtime dependencies:

    • numpy
    • schemdraw (only required if you intend to draw diagrams)

Install dependencies with pip:

pip install numpy schemdraw

If you publish the package to PyPI, runtime dependencies should be declared in the package metadata.


Quick start

Example: build and simulate a simple series circuit with a 5 V source and a 100 ohm resistor.

from pycircuit_new import Circuit, Resistor, PowerSource

c = Circuit()
vs = PowerSource(5.0, [1, 0])    # voltage from node 1 to ground (0)
r = Resistor(100.0, [1, 0])      # 100 ohm between node 1 and ground

c.add(vs)
c.add(r)

c.simulate()                      # run DC simulation
print('Node voltages:', c._node_voltage_cache)  # or use public helpers
print('Source current:', c.calculate_current())
print('Voltage across resistor:', c.measure_voltage([1, 0]))

The simulate() method populates component states with currents and voltage drops. calculate_current() returns the current through the power source.


Transient simulation

Run a time-domain simulation with capacitors using a companion model. The Circuit.transient_simulate method returns time points, node histories, and component current histories.

from pycircuit_new import Circuit, PowerSource, Resistor, Capacitor

c = Circuit()
c.add(PowerSource(10.0, [1, 0]))
c.add(Resistor(1000.0, [1, 0]))
c.add(Capacitor(1e-6, [1, 0], initial_voltage=0.0))

times, node_history, comp_history = c.transient_simulate(t_stop=0.01, dt=1e-4, integration_method='backward_euler')
# node_history maps node -> numpy array of voltages at each time step

Use integration_method='trapezoidal' for higher accuracy when appropriate.


API reference (compact)

Key classes

  • ComponentSpec(kind, value, nodes, id=None) Immutable specification for a component. kind is one of 'R', 'LED', 'V', 'C'.

  • ComponentState Mutable per-component simulation state. Holds current and voltage_drop.

  • Component Abstract base class for all components. Use concrete classes below.

  • Resistor(resistance, connections, id=None) Resistor component. Methods: set_current, calculate_voltage_drop, calculate_power.

  • LED(resistance, connections, forward_voltage=2.0, id=None) Simplified LED model with a forward voltage offset. Methods similar to Resistor.

  • PowerSource(voltage, connections, id=None) Ideal DC voltage source.

  • Capacitor(capacitance, connections, initial_voltage=0.0, id=None) Capacitor that stores previous voltage for transient simulation.

  • Circuit() Top-level simulator and container for components. Important methods:

    • add(component) add a component to the circuit.
    • simulate() run DC analysis and update component states.
    • calculate_current() return total source current.
    • calculate_total_resistance() approximate or compute equivalent resistance.
    • measure_voltage([n1, n2]) measure voltage difference between nodes.
    • measure_current([n1, n2]) measure current through a component between nodes.
    • draw_circuit_diagram() draw a simple left-to-right schematic using schemdraw.
    • transient_simulate(t_stop, dt, record_nodes=None, integration_method='backward_euler') perform transient run.
    • compare_integration_methods(t_stop, dt, node_to_compare=2) helper to compare BE vs trapezoidal.

Also top-level functions solve_mna and solve_mna_time_step are available for advanced use.


Notes and limitations

  • The LED model is linearized and uses a forward-voltage offset. It is not a full diode I-V model.
  • Only a single ideal voltage source is supported in the DC solver. Additional sources will raise an error.
  • The DC solver treats capacitors as open circuits. Use transient simulation to include capacitors.
  • The library does not currently include advanced netlist parsing or automated layout for drawing.

Development and testing

Contributions are welcomed in these areas:

  • Run the existing examples interactively to validate behavior.
  • Consider adding unit tests for: stamping operations, small circuits with known solutions, transient responses for RC circuits.

License

MIT Licence


Contributing

Contributions and bug reports are welcome. Open a ticket or a pull request with a small, focused change. Add tests for new features.


Contact

Author: Ege Güvener
Email: egeguvener0808@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycircuit_new-0.1.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycircuit_new-0.1.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pycircuit_new-0.1.1.tar.gz.

File metadata

  • Download URL: pycircuit_new-0.1.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for pycircuit_new-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5c3bd4bdecf754f1a6fb71895612705c2acd8b56f62e3d6dc920e57adfaff3c4
MD5 33ca625b3d2b6cc3fbf3d2386ab1545c
BLAKE2b-256 d3458e1e4f58b4c5f8474b5b2af6aaedc1c0254160822ad97ab497ca26390681

See more details on using hashes here.

File details

Details for the file pycircuit_new-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pycircuit_new-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for pycircuit_new-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 05ecd23fbc7a870dd1e613be63273a3a180bc6beae7d5735aca9d20ee9d1d203
MD5 816e7df5f6f2bfce25396cbedad54d35
BLAKE2b-256 4db70f9804a5cceafb2c3aea7037c1500a3805316fc2f7c02d483fcfbf9abce0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page