A C++ SPICE-grade linear circuit simulator built from scratch, implementing Modified Nodal Analysis for DC, AC, and transient analysis. The engine has been validated against physical hardware via an Arduino Hardware-in-the-Loop system, and compiled to WebAssembly to run as an interactive browser-based virtual lab.
The IB Physics curriculum has been updated and greatly downscaled the coverage of circuits. Kirchhoff's laws are not even covered. One student seeking to increase their knowledge of the topic would perhaps do some reading on the side. A crazy one would build an AC/DC linear circuit simulator from scratch to understand exactly how electrical circuits are modelled and the rigorous mathematics behind them. Hence the motivation for this project.
This is a C++-based linear circuit simulator built from scratch. It is based on Modified Nodal Analysis (discussed below), validated against real hardware, and running live in the browser via WebAssembly. It was built because understanding how circuit simulation actually works requires implementing it.
This simulator employs Modified Nodal Analysis (MNA) to use Kirchhoff's current law to solve for circuit quantities via systems of equations. The "modified" being due to the need to account for voltage sources. While for other components the voltage across them is what's being found, it is the current through voltage sources what needs to be found in those scenarios.
The systems of equations to solve are of the form:
Here, the first row is for a component that is not a voltage source, such as a resistor. The second row is for a voltage source.
-
$G$ is the conductance/admittance matrix constructed from components -
$B$ and$C$ map the connections of the voltage sources -
$D$ is zero for ideal independent voltage sources -
$v$ is the unknown voltage across the component -
$j$ is the unknown current through the voltage source -
$v_s$ and$i_s$ are the known voltages and currents
The simulator algorithms construct the matrix iteratively via "stamping rules". For example, a resistor
For AC analysis, the real-valued conductances are replaced with frequency-dependent complex admittances,
Energy storing components like charging capacitors or RLC circuits are not instantaneous. To simulate time, the simulator implements numerical integration using the backward Euler method. A continuous differential equation such as
Based on the previous step, capacitors and inductors are replaced with a parallel combination of an equivalent conductance
Circuit components appear to map nicely to an inheritance-oriented OOP design. An abstract component class can be inherited by Resistor, Capacitor, Inductor, etc. classes. However, the program must iterate rapidly through every component to stamp values into a contiguous matrix. A Component struct using a ComponentType enum allows the matrix builder to use contiguous Component vectors, resulting in faster matrix assembly and better cache locality.
Because the MNA formulation introduces
The simulator's transient analysis has been validated against physical RC circuits using a Hardware-in-the-Loop system. An Arduino Uno acquires live voltage measurements from breadboard circuits and transmits them to the simulator in real time, enabling direct comparison between theoretical and measured behaviour.
Across three experiments (RC charging, leaky capacitor, RC discharge), systematic model refinement reduced RMSE by 90–93%. Residual error after refinement is bounded by ADC quantisation at 4.88mV resolution. This is a physical measurement floor, not a modelling limitation.
For full methodology, data, and analysis see ANALYSIS.md.
The C++ engine has been compiled to WebAssembly via Emscripten and deployed as a browser-based virtual lab. Users can draw circuits on an interactive schematic canvas, run DC, AC, or transient simulations client-side with no installation, and load pre-built HIL experiments to overlay Arduino measurements against the simulation output.
Stack: React · TypeScript · Tailwind · Vite · Emscripten · Zustand · Recharts
Source: web/
Available at: CircuitSimulator/
CircuitSimulator/ C++ source: parser, circuit, solver CircuitSimulatorApp/ CLI entry point CircuitSimulatorTests/ Google Test unit tests web/ React + WebAssembly frontend tools/ HIL data conversion scripts ANALYSIS.md Full HIL experiment methodology and results build_wasm.bat Emscripten build script