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 for a circuit and forgets to ground any node. What will the solver do?
It fails. Without a ground, the vector of all ones is in the null space of (lesson 19), so is singular and has no inverse. Numbering nodes from 1 and leaving out node 0, the ground, removes that null space.
Name it
Stamps
Number the nodes 1 to , with 0 for ground. Each part adds a small pattern, its stamp, to the system :
A resistor between nodes and (conductance ):
skipping any entry for node 0. This is assembled one branch at a time.
A current source pushing current from node through itself to node : and .
Voltage sources: modified nodal analysis
A voltage source fixes and carries an unknown current . Add as an extra unknown and the constraint as an extra equation:
where column of has at row and at row for the th 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 1000Worked 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
- Stamp the resistors. In siemens:
- Solve. Your solver should reproduce this exactly.
Answer = 0.667 V and = 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
- Unknowns. , and the source current .
- The pieces. The resistors give and .
- The full system.
- Solve. It gives , and mA.
- The sign of . The current is defined as flowing into the source's positive end, so mA means 5 mA flowing out to the circuit.
Answer = 10 V and = 5 V, and the source supplies 5 mA ( 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 uses index .
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 = 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 = 1 kΩ from the top to node 2, = 2 kΩ from node 2 to ground, = 10 kΩ from the top to node 3, and from node 3 to ground. What balances it ()? 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?
Each resistor joins two nodes and adds the same to both entry and entry , plus on the two diagonal entries. Every stamp is symmetric on its own, and adding symmetric matrices gives a symmetric matrix. Physically, a resistor conducts equally in both directions, which is the reciprocity of lesson 11; in matrix terms, is symmetric for any incidence matrix and diagonal .
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.