MCPcopy Index your code
hub / github.com/Harnesser/tiny-spice-rs

github.com/Harnesser/tiny-spice-rs @0.9.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.9.0 ↗ · + Follow
194 symbols 687 edges 46 files 76 documented · 39%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

A Tiny SPICE Simulator

Ionsamhlóir ciorcaid an-bheag.

A teeny-weeny SPICE circuit simulator implemented in Rust. It can read a (simple) SPICE deck, perform the (limited) analyses listed in the .control section and write some waveform data to a file.

Supported SPICE Deck Stuff

Currently supported components (alphabetically): * C - capacitor * D - diode (basic) * E - voltage-controlled voltage source (VCVS) * G - voltage-controlled current source (VCCS) * I - current source, DC and SIN() * R - resistor * V - voltage source, DC, SIN() and PWL() * X - subcircuits

Analyses supported: * op - DC operating point * trans - Transient analysis

In SPICE decks: * The 'first-line is a title' behaviour is supported * Engineering notation is supported, e.g. 1k is 1000 * A control block with a small list of commands between .control and .endc is supported * Limited bracket expressions are supported for subcircuits, R & C.

Unsupported Stuff

A huge list of things are not supported. Everything not listed above, which includes: * DC Sweeps are not supported * MOSFETs and other transistors are not supported * Noise Analysis is not supported * Circuit topology checks are not supported * Even simple commands such as print and plot are not supported

Running a Simulation

The binary name of the teeny-weeny SPICE simulator in this repo is tiny-spice-rs. Since it is written in Rust, so you'll need Cargo and all that to run simulations.

As an example, let's run a transient analysis on a full-wave rectifier. A sinewave voltage source drives one pair of terminals of the diode bridge. The other pair of diode bridge terminals has a capacitor and resistor in parallel as a load.

The circuit in ngspice/fullwave_rectifier.spi is this:

Full-Wave Rectifier

V1 1 2 SIN(0 5 1e3) ; input voltage
V2 2 0 0  ; ground, and current measure

* full-wave rectifier
D1 1 3
D2 4 1
D3 2 3
D4 4 2

* Small caps across the diodes to prevent time-step-too-small
CD1 1 3 12pF
CD2 4 1 12pF
CD3 2 3 12pF
CD4 4 2 12pF

* Load
Rl 3 4 1k
Cl 3 4 1uF

.control
*  option reltol = 0.001
*  option abstol = 1e-12

  tran 100ns 2ms 
  option ; ngspice only shows new values after analysis

  plot v(1,2) v(3,4) ; (ngspice)
.endc

Build the simulator, and run the simulation using the command below:

cargo run ngspice/fullwave_rectifier.spi

Waveforms will be stored in a file called waves/fullwave_rectifier/tran.dat. Waveforms for all nodes in the design will be in this file. The file is in TSV format (tab-separated values). The first row has the column names. The second row has the units of eac column. All other rows contain waveform data in floating-point format.

Time    v(0)    v(1)    v(2)    v(3)    v(4)    i(0)    i(1)
s   V   V   V   V   V   A   A
0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
0.000000050 0.000000000 0.000785398 0.000000000 0.000392699 0.000392699 -0.000000377    0.000000000
0.000000150 0.000000000 0.003141592 0.000000000 0.001570796 0.001570796 -0.000000566    -0.000000000
0.000000350 0.000000000 0.007853978 0.000000000 0.003926989 0.003926989 -0.000000566    -0.000000000
0.000000750 0.000000000 0.017278725 0.000000000 0.008639363 0.008639363 -0.000000566    -0.000000000
0.000001250 0.000000000 0.036128001 0.000000000 0.018064001 0.018064001 -0.000000567    -0.000000000
0.000001750 0.000000000 0.054976764 0.000000000 0.027488382 0.027488382 -0.000000454    0.000000000
0.000002250 0.000000000 0.070683480 0.000000000 0.035341741 0.035341740 -0.000000379    -0.000000000

To view the waveforms, load in a spreadsheet and chart some columns. For example, chart: * the input voltage v(1)-v(2) * the output voltage v(3)-v(4)

(If you have python3 and matplotlib installed, try:

python3 bin/r8n -expr "2-3,4-5" waves/fullwave_rectifier

)

Fullwave rectifier waveforms from tiny-spice-rs

Tools Used

Needed: * Rust 1.71.1

Development stuff: * KST2 * Python 3.8.10 * matplotlib * python3-tk

Other Notes

  • Some documentation on how the solver works in docs/
  • Example SPICE files in ngspice
  • There are some tests: use make test

Core symbols most depended-on inside this repo

Shape

Function 87
Method 83
Class 19
Enum 5

Languages

Rust97%
Python3%

Modules by API surface

src/circuit.rs26 symbols
src/spice.rs23 symbols
src/engine.rs22 symbols
src/analysis.rs9 symbols
src/bracket_expression.rs7 symbols
tests/test_trans_irrc.rs6 symbols
src/expander.rs6 symbols
src/element/diode.rs6 symbols
tests/test_trans_irrc_hpf.rs5 symbols
bin/r8n/waveforms.py5 symbols
tests/test_trans_irrd_sine.rs4 symbols
tests/test_trans_ird_sine.rs4 symbols

For agents

$ claude mcp add tiny-spice-rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page