Flexible and easy to use non-linear transient electric circuit simulator.


Keywords
electronics, circuit, simulation, non-linear, transient, steady-state, time-domain
License
MIT
Install
pip install respice==0.3.8

Documentation

respice

Not super optimized, but extremely flexible and easy to use non-linear transient electric circuit simulator

Prerequisites

Install necessary packages via

pip3 install -r requirements.txt

Usage

Create your circuit and simulate it!

import networkx

from respice.analysis import Circuit
from respice.components import CurrentSourceDC, R


# Define components for our circuit.
R1 = R(100)
R2 = R(200)
R3 = R(100)
R4 = R(200)
R5 = R(100)
Isrc = CurrentSourceDC(0.1)

# Construct the circuit. All circuits are just
# Digraphs allowing multiple edges. On each edge
# one component.
wheatstone_bridge = Circuit()
wheatstone_bridge.add_element(0, 1, R1)
wheatstone_bridge.add_element(0, 2, R2)
wheatstone_bridge.add_element(1, 2, R3)
wheatstone_bridge.add_element(1, 3, R4)
wheatstone_bridge.add_element(2, 3, R5)
wheatstone_bridge.add_element(3, 0, Isrc)

# Simulate! 1000 steps with a time-step of 100ms
wheatstone_bridge.simulate(0.1, 1000)

The results are attached to each component instance in their component.v and component.i members. Those contain the voltages and currents respectively for each time step as a list.

Circuits are graphs (like mentioned in the snippet). More precisely, a Digraph allowing multiple edges from and to the same nodes. Each edge represents a single two-terminal component (like a resistor). Those are connected to nodes, which are simple joints that can be arbitrarly named or identified (for example numbers like in the example above, but strings, or even other objects are possible if necessary).

Example Plotting

from respice.examples import RC
from respice.analysis import simulate
from matplotlib import pyplot as plt


# Define an example RC circuit. The package respice.examples
# contains a few!
rc = RC(100, 100e-6, 10)  # 100Ohm, 100uF, 10V

dt = 0.01
cycles = 1000
rc.simulate(dt, cycles)

# Necessary for matplotlib.
x_vals = [dt * x for x in range(cycles)]

# Setup voltage and current plots.
plt.subplot(211)
plt.ylabel('V')
plt.plot(x_vals, rc.R.v)
plt.plot(x_vals, rc.C.v)
plt.legend([rc.R.name, rc.C.name])

plt.subplot(212)
plt.ylabel('I')
plt.plot(x_vals, rc.R.i)
plt.plot(x_vals, rc.C.i)
plt.legend([rc.R.name, rc.C.name])

# Display.
plt.show()

Limitations

  • Voltage sources

    Since the analysis algorithm utilizes the nodal analysis, voltage sources make a problem. Although it's possible to integrate them into the equations, they represent an exception. This project tries to abstract components as much as possible to avoid such exceptions. That's why we are still thinking about a more general concept (e.g. mixing nodal analysis with loop currents).

  • Mutually coupled components

    Certain components like magnetically coupled coils can't be represented currently. We are thinking about how to allow components to push new equations onto the simulation equation stack to take care of those constraints, effectively allowing to couple different graph edges together.

  • Multi-terminal components

    All edges represent one single two-terminal component with a start end end terminal (the direction matters, for example for diodes or current sources!). So how to represent multi-terminal components in our graph system? Our current vision doesn't change the underlying graph representation, but add a few more requirements how to embed those elements into them. In fact, multi-terminal components can be represented as multiple edges, connecting each of the terminal together, and each edge fulfills a subfunction to represent the voltage between two arbitrary terminals. This is tightly coupled with the concept of mutually coupled components, to represent the coupling that usually exists in multi-terminal components.

The Future

  • Incorporating interfaces for heat-dynamics

    Components are often depending on operation temperature. This can highly change behaviour of the whole circuit. Implementing new simulation variables like current component temperature could allow to simulate temperature influence. This is especially useful for safety analysis and estimating the maximum critical operation point.

    This might even serve as a general concept to introduce even more parameters besides heat that influence component performance and behaviour.

  • Enhancing components (maybe heat-dynamics coupled) to simulate breakage

    Components can break. Either due to age, or because currents where to high. Consecutively extending components to contain "breakage-states" (so state variables that tell you if the element is destroyed or not) could improve analysis for circuits operating near critical operation points.