A753 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A7 · Lesson 53 · Project

Build the core of SPICE in forty lines

Every circuit simulator, from SPICE to the one running the sims in this course, starts the same way: read a list of parts, turn each part into a few entries of a matrix, and solve. In this project you'll write that core yourself. Your solver will read a netlist of resistors, current sources and voltage sources, assemble the equations with the "stamps" that make nodal analysis mechanical, and solve circuits too tangled to do by hand. Along the way, every idea from this course shows up: the incidence matrix, symmetry, positive definiteness, and the null space you remove by grounding a node.

Predict

Your solver builds the conductance matrix G for a circuit and forgets to ground any node. What will the solver do?

Name it

Stamps

Number the nodes 1 to N, with 0 for ground. Each part adds a small pattern, its stamp, to the system G𝐯=𝐢:

A resistor R between nodes a and b (conductance g=1/R):

Gaa+=gGbb+=gGab−=gGba−=g

skipping any entry for node 0. This is ATCA assembled one branch at a time.

A current source pushing current I from node a through itself to node b: ia−=I and ib+=I.

Voltage sources: modified nodal analysis

A voltage source fixes va−vb=E and carries an unknown current j. Add j as an extra unknown and the constraint as an extra equation:

[GBBT0][𝐯𝐣]=[𝐢𝐞]

where column k of B has +1 at row a and −1 at row b for the kth source. This is modified nodal analysis (MNA), what SPICE does. The matrix is still symmetric, but no longer positive definite: the zero block makes it indefinite, so the solver needs pivoting.

The netlist

One part per line: a kind, two nodes, a value in SI units.

V 1 0 10
R 1 2 1000
R 2 0 1000

Worked example

By hand: a 1 mA source into node 1, with 1 kΩ from node 1 to ground, 1 kΩ from node 1 to node 2, and 1 kΩ from node 2 to ground.

Show the solution
  1. Stamp the resistors. In siemens: G=[0.002−0.001−0.0010.002] 𝐢=[0.0010]
  2. Solve. 𝐯=G−1𝐢=(0.667,0.333)V Your solver should reproduce this exactly.

Answer v1 = 0.667 V and v2 = 0.333 V.

Worked example

By hand: a 10 V source from node 1 to ground feeding two 1 kΩ resistors in series to ground.

Show the solution
  1. Unknowns. v1, v2 and the source current j.
  2. The pieces. The resistors give G=[0.001−0.001−0.0010.002] and B=(1,0).
  3. The full system. A=[0.001−0.0011−0.0010.0020100] A[v1v2j]=[0010]
  4. Solve. It gives v1=10, v2=5 and j=−5 mA.
  5. The sign of j. The current j is defined as flowing into the source's positive end, so −5 mA means 5 mA flowing out to the circuit.

Answer v1 = 10 V and v2 = 5 V, and the source supplies 5 mA (j=−5 mA).

Check

Step 1. Write stamp_resistor(G, a, b, R), adding a resistor's stamp to the NumPy array G in place. Nodes are numbered from 1, node 0 is ground and gets no row or column, and node k uses index k−1.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 2. Write solve_ri(lines) for netlists of resistors (R a b ohms) and current sources (I a b amps, pushing current from a to b through the source). Return the node voltages as a NumPy array, node 1 first. Use stamp_resistor (included).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 3. Write solve_mna(lines) that also handles voltage sources (V a b volts, meaning va−vb = volts), using modified nodal analysis. Return only the node voltages.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 4. A bridge has R1 = 1 kΩ from the top to node 2, R2 = 2 kΩ from node 2 to ground, R3 = 10 kΩ from the top to node 3, and R4 from node 3 to ground. What R4 balances it (v2=v3)? Check your answer with your solver.

Write units the way you normally would: 1.5 kΩ, 1500 Ω and 0.0015 MΩ are the same answer.

Explain why

Why does the conductance matrix you assemble from resistor stamps always come out symmetric?

Practice

Keep practicing

4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.