41
\$\begingroup\$

I recently had the opportunity to play around with LiveWire, which is a circuit simulator and began wondering how exactly they computed the voltages across each component and the current going through each track of wire.

I have only been taught basic circuit analysis skills (such as Mesh analysis and Nodal analysis) so far, and I'm not entirely sure that they would be generic enough to implement in a 'fits-all' way within a circuit simulator.

As a programmer this has intrigued me and I'd love to see what techniques are commonly employed when building circuit simulators such as this one.

My apologies if this does not belong here, it was a choice between here and StackOverflow and I felt that although it is a software-development oriented question, the question is more applicable to this site and it's userbase.

\$\endgroup\$
1
  • \$\begingroup\$ The main trick is lots of little time slices. Complex functions of time are therefore handled exeucting lots of difference equations with small time slices. Nasty complexity is usually reduced significantly if it can be expressed as differential equations, which become difference equations in a digital simulation. \$\endgroup\$ Nov 21, 2013 at 14:12

3 Answers 3

18
\$\begingroup\$

I've examined the code of the Falstad simulator in some detail. For circuits which consist only of linear components like resistors, switches, and voltage sources (things like logic-gate outputs are considered ground-connected voltage sources for purposes of the simulation) the simulator regards each circuit node, voltage source (connecting two nodes), or wire (likewise) as defining a linear equation and a variable, such that the number of equations and number of variables are always equal. For a circuit node, the variable is the voltage of the node, and the equation computes the total current flowing through it equal to the total current injected by any current sources. For a voltage source or wire (a wire being handled as a voltage source where the potential difference is zero), the equation sets the voltage difference between the two circuit node voltages equal to the required voltage difference, and the variable is the amount of current flowing through the voltage source from one node to the other.

Things like current sources and resistors are not associated with resistors or variables. Instead, current sources increase the total current required for one circuit node (remember each circuit node has an equation which evaluates the total current flowing in and out) and decrease it for the other. Resistors are a little trickier: for each endpoint's equation, the resistor adds terms for the node voltage of each endpoint.

A 100-ohm resistor connecting nodes 1 and 2, for example, would say that each volt increase on node 1 will decrease the current flowing into node 1 by 0.01 amps and increase the current flowing into node 2 by a like amount. Likewise, each volt increase on node 2 would increase the current flowing into node 1 by 0.01 amps and decrease the current flowing into node 2 by a like amount.

Consider a circuit with a 10 volt supply connecting nodes 1 and 5, and 100 ohm resistors connecting node 1 and 2, 2 and 3, 2 and 4, and 3 and 4. Assume further that there's a ground icon on node 1. Thus:

neg ---+-1---R100---2---R100---3---100---4---pos
      gnd           |                    |
                    +---------100--------+

There would be two "voltage sources": the ground lead and the 10 volt supply (which are regarded as equation/variable 5 and 6, respectively). The equations would thus be:

-X1*0.01                             +X5 -X6 = 0  Node 1
+X1*0.01 -X2*0.01          +X4*0.01          = 0  Node 2
         +X2*0.01 -X3*0.01 +X4*0.01          = 0  Node 3
         +X2*0.01          -X4*0.01      +X6 = 0  Node 4
-X1*1                                        = 0  Volts 5 (voltage between 1 and gnd)
-X1*1                      +X4*1             = 10 Volts 6 (voltage between 1 and 4)

This system of equation may be represented as an NxN matrix plus an N item array. Each equation is represented by a row in the matrix, with values on each row representing the coefficients of each variable. The right-hand side of each equation is stored in the separate array. Before solving the equations, one will know the net current flowing into each node (zero in this case), and the voltage difference between pairs of nodes connected by voltage sources. Solving the equations will yield the voltage at each node and the current flowing through each voltage source.

If the circuit contains capacitors, each of those will be regarded as a voltage source in series with a low-value resistor; after each simulation step, the voltage source will be adjusted according to the amount of current that flowed through it. Inductors will be regarded as high-value resistors which feed current into one and and take it out the other (the amount of current being adjusted according to the voltage across the resistance). For both capacitors and inductors, the value of the resistance will be controlled by the amount of time represented by a simulation step.

More complex circuit elements like transistors are regarded as combinations of voltage sources, current sources, and resistors. Unlike the simpler circuit elements which let everything get processed once per simulation time step, elements like transistors compute their effective resistances etc. based upon the voltages and currents they're seeing, evaluate all the resulting equations, and re-evaluate their resistance based upon the new voltages and currents, re-evaluate the equations, etc. in an effort to reach an equilibrium where their effective resistance is as it should be for the voltage and current the transitor is seeing.

The Falstad simulator can be decently fast for moderate-sized circuits which consist entirely of "linear" elements. The time to repeatedly solve a system of equations is pretty reasonable if the only thing that changes are the right-side coefficients. The time gets much slower if the left side changes (e.g. because a transistor's effective resistance goes up or down) because the system has to "refactor" the equations. Having to do refactor the equations multiple times per simulation step (may be necessary with transistors) makes things slower yet.

Using one big matrix for everything is not a good approach for large simulations; even though the matrix will be fairly sparse, it will take up space proportional to the square of the number of nodes plus voltage sources. The time required to solve the matrix on each simulation step will be proportional to the square of matrix size if refactoring is not required, or to the cube of matrix size if refactoring is required. Nonetheless, the approach does have a certain elegance when it comes to showing the relationship between a circuit and a system of linear equations.

\$\endgroup\$
5
  • 4
    \$\begingroup\$ Most SPICE-type simulators use this type of algorithm too. Here's an overview for spice ecircuitcenter.com/SpiceTopics/Overview/Overview.htm \$\endgroup\$
    – user6972
    Nov 21, 2013 at 17:46
  • 1
    \$\begingroup\$ FYI -- KLU is the common algorithm used in circuit simulators. KLU stands for Clark Kent LU, since it is based on Gilbert-Peierls’ algorithm, a non-supernodal algorithm, which is the predecessor to SuperLU, a supernodal algorithm. KLU is a sparse high performance linear solver that employs hybrid ordering mechanisms and elegant factorization and solve algorithms. \$\endgroup\$
    – user6972
    Nov 21, 2013 at 17:58
  • \$\begingroup\$ It's important to notice that there are several transistors models, BSIM or EKV, for example. Basically these models take in consideration all the physics, from a point of view, involved in the operation of a single transistor and the effects on its electrical characteristics. There is much more into it than just representing as combinations of voltage sources, current sources and resistors. If you want to select a different model you can use the LEVEL parameter. This tutorial from Jacob Baker's it's a good start. hope it helps. \$\endgroup\$
    – user22230
    May 30, 2015 at 16:02
  • \$\begingroup\$ I get [0,10,20,10,0,0] as the solution from that system here, which doesn't seem to be correct. Can you elaborate on this? \$\endgroup\$
    – 2080
    May 20 at 17:45
  • \$\begingroup\$ @2080: It's been so long since I worked that example or even looked at any such simulations, I don't remember how I derived the values. If you can find a particular mistake and offer a specific correction, I can edit the answer appropriately, but it would be inconvenient for me to try to re-derive it. \$\endgroup\$
    – supercat
    May 21 at 19:26
12
\$\begingroup\$

LiveWire is one of many circuit simulators, of varying levels of capability.

For instance, Falstad Circuit Simulator seems to be of a similar capability level as LiveWire - and the source code is offered at that link. That should be a good beginning.

For more sophisticated circuit simulation, many tools trace their roots back to SPICE by UC Berkley. The SPICE source code is available on request from UCB under BSD license.

Manufacturer-specific SPICE editions typically integrate very detailed semiconductor simulation models of their own products into their simulators. For instance, LTSpice IV from Linear Technologies or TINA-TI from Texas Instruments. Underneath, it's all usually SPICE.

Quoting from the WikiPedia page about SPICE:

Circuit simulation programs, of which SPICE and derivatives are the most prominent, take a text netlist describing the circuit elements (transistors, resistors, capacitors, etc.) and their connections, and translate this description into equations to be solved. The general equations produced are nonlinear differential algebraic equations which are solved using implicit integration methods, Newton's method and sparse matrix techniques.

At an even higher level of sophistication, several commercial products such as Proteus Virtual System Modeling, part of the Proteus Design Suite, use proprietary enhancements for mixed-mode SPICE circuit simulation - these tools can simulate both analog circuit behavior, and digital microcontroller code, with the interactions between them fully modeled.

At a much more limited and limiting level, some schematic editors, such as the Circuit Lab tool integrated into this site, provide a small range of simulation capabilities. While this may not be all that useful in practical non-trivial electronic design, studying their capabilities and implementation would provide a software developer some insight into what works well for users, and what doesn't.

\$\endgroup\$
10
\$\begingroup\$

There are three main analyses that are done by SPICE-like circuit simulators:

  • DC operating point
  • AC analysis
  • Transient analysis

The DC operating point analysis for a linear circuit (formed from DC sources, linear resistors, and linear controlled sources) is done using the modified nodal analysis (MNA). Mesh analysis could also be used, but it is very easy to set up the equations for nodal analysis.

For nonlinear circuits (which include devices like transistors, which can be modelled essentiallhy as nonlinear controlled sources), some additional tricks have to be used. A conceptually simple way is to use an extension of Newton's Method for multiple equations.

Newton's method involves guessing the solution, then making a linear model of the circuit that's only accurate "near" the guessed solution. The solution to the linearaized circuit is used as a new guess about the solution, and the process is iterated until the successive iterations "converge" on the (hopefully) correct solution for the nonlinear circuit. In the real world, more complicated nonlinear solvers are used to be able to do the solution more quickly and with fewer errors due to convergence failure.

The AC analysis is done by first doing a DC analysis to find an operating point. Then you study the effect of small perturbations around the operating point. "Small" means, by definition, small enough that nonlinear effects aren't important. That means the circuit elements are transformed into linear equivalent elements depending on the operating point. Then the MNA can be used (with complex numbers representing the impedance of energy-storing elements) to solve the effect of the perturbations caused by the AC sources in the circuit.

The transient analysis is done, like Olin says in comments, by considering how the circuit variables evolve over very small steps in time. Again at each time step the circuit is linearized around it's operating point, so that MNA can be used to set up the equations. A simple method for solving the behavior over time is Euler's Method. However again in practice more complicated methods are used to allow using larger timesteps with smaller errors.

You can see that a common thread in these methods is making a linear approximation to the circuit behavior and solving that with MNA until you find a solution to the nonlinear circuit behavior.

These three analyses have been the main ones done by SPICE-like simulators since the 1970's. Newer simulators add additional capabilities like harmonic balance (an extension of AC anlaysis to accomodate mixing effects from nonlinear elements), or electromagnetics simulations to simulate transmission line effects. But the DC, AC, and transient simulations are the first three you should understand when using a SPICE-like simulator.

\$\endgroup\$
2
  • \$\begingroup\$ I talked about how to set up the MNA equations in an earlier answer: electronics.stackexchange.com/questions/19198/… \$\endgroup\$
    – The Photon
    Nov 21, 2013 at 17:32
  • 3
    \$\begingroup\$ We should also add that digital circuit simulations use entirely different methods; and that makes analog-digital co-simulation into a whole problem of its own. \$\endgroup\$
    – The Photon
    Nov 21, 2013 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.