Skip to content
Tutorials Center
My learning

Topics

Lesson 44 of 48 ยท Projects and Practice

Project: Build a Circuit Solver in Python

Write a small circuit simulator from scratch: parse a netlist, stamp resistors and sources into a matrix, solve with numpy and extend it to AC with complex numbers.

Programs like SPICE, which engineers use to simulate circuits with millions of components, rest on one idea you already know: nodal analysis. In this project you will write a small version yourself. It reads a circuit written as text, builds a matrix equation, solves it with numpy and prints every node voltage. Then you will extend it with three lines of code to handle AC circuits. By the end you will understand exactly what a circuit simulator does, and you will have a tool to check your own homework.

A pipeline: a netlist of five elements, a conductance matrix and the solved node voltages 10, 5.455 and 3.636 volts
Write the circuit as text, stamp each element into a matrix, solve once, read every node voltage.

What you will learn

  • How a netlist describes a circuit.
  • How each element "stamps" values into the nodal matrix.
  • How voltage sources are handled (modified nodal analysis).
  • How the same code solves AC circuits with complex numbers.

Key terms

Netlist: a text description of a circuit, one line per element, naming the nodes it connects.

Node: a point where elements join; node 0 is ground.

Stamp: the contribution an element adds to the matrix.

Modified nodal analysis (MNA): nodal analysis extended with extra unknowns, the currents of voltage sources.

The idea

Nodal analysis writes KCL at every node except ground. For resistors, the KCL equation at node k reads: sum over connected resistors of G (v_k โˆ’ v_other) equals the current injected by sources into that node. Collect all the equations and you get

G v = i

where G is the conductance matrix, v the vector of node voltages and i the injected currents. Two patterns make G easy to build:

  • A resistor of conductance g between nodes a and b adds +g to G[a][a] and G[b][b] and โˆ’g to G[a][b] and G[b][a]. (Ground rows and columns are simply skipped.)
  • A current source injecting I into node a adds +I to i[a].

A voltage source has no finite conductance, so it cannot be stamped in the same way. MNA solves the problem by adding one new unknown, the source current i_k, and one new equation, v_p โˆ’ v_q = V. The matrix grows by one row and column per voltage source, and numpy still solves it in one call.

The example circuit

The picture shows this netlist (ohms and volts):

TEXT
V1 1 0 10
R1 1 2 1000
R2 2 0 2000
R3 2 3 1000
R4 3 0 2000

By hand: R3 + R4 = 3 kฮฉ is in parallel with R2 = 2 kฮฉ, giving 1.2 kฮฉ. The divider with R1 gives V2 = 10 ร— 1.2/2.2 = 5.4545 V, and V3 = V2 ร— 2/3 = 3.6364 V. With V1 fixed, nodes 2 and 3 satisfy the 2 ร— 2 system in the picture: 2.5 V2 โˆ’ V3 = 10 mA and โˆ’V2 + 1.5 V3 = 0, whose solution is V2 = 60/11, V3 = 40/11.

The solver

The function below reads a netlist, finds the nodes, builds the matrix and returns the node voltages and the voltage-source currents. It supports R, V, I (and later C, L).

Check against power

A good simulator is self-checking. The power delivered by the source must equal the power absorbed by all resistors (conservation of energy). The program below verifies this, and it also tests the solver on a Wheatstone bridge.

Extending to AC

Replace every element by its admittance at the chosen ฯ‰: 1/R for a resistor, jฯ‰C for a capacitor and 1/(jฯ‰L) for an inductor. Declare the matrix as complex, and the same code now solves phasor circuits. Voltages and currents come out as complex numbers whose magnitude and angle you can read off directly. In DC mode the capacitor is an open circuit (zero admittance) and the inductor a short (very large admittance).

Common misconceptions

"A simulator needs differential equations." For DC and AC steady state it only solves linear algebra. Transient simulation adds time stepping on top, replacing each capacitor and inductor by a small resistor-source pair at each step.

"A bad matrix means a bad solver." Usually the netlist is wrong: a floating node (no path to ground) or two ideal voltage sources in parallel makes the matrix singular.

Try it

The first program is the complete solver. Run it on the example netlist and compare with the hand calculation.

Python The MNA solver

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second program reuses the solver (copied in compact form) on two new problems: a Wheatstone bridge, where a balanced bridge gives zero voltage across the middle, and an AC RL circuit that should reproduce the 2 โˆ  โˆ’53.13ยฐ A current from the impedance lesson.

Python Bridge and AC

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In nodal analysis the matrix equation is

    G is the conductance matrix, v the node voltages and i the injected currents.
  2. 2. How is a resistor of conductance g between nodes a and b stamped?

    The conductance contributes to each node's own equation and couples the two nodes with a negative sign.
  3. 3. Why does MNA add an extra unknown for each voltage source?

    A voltage source fixes a voltage difference but its current is free, so the current is added as an unknown with an extra equation.
  4. 4. To solve an AC circuit with the same solver you

    Replace each element by its admittance at ฯ‰ and solve with complex numbers.
  5. 5. A netlist with a node connected to nothing else but one resistor to another floating node gives

    Without a reference path the voltages are undetermined, so the matrix cannot be inverted.

Summary

  • A netlist plus stamps turns a circuit into G v = i; numpy solves it in one call.
  • Voltage sources add an extra row and column (MNA).
  • The same code does AC when admittances become complex.
  • Checking power balance and a hand calculation is how you trust a simulator.

Next: Practice problems