Circuit Simulation with a 'SPICE3f5'-Derived Engine

Runs 'SPICE3' circuit netlists (operating point, DC sweep, transient, small-signal AC, pole-zero, noise, distortion, transfer function and sensitivity analyses) on 'Dune', a circuit simulator written in 'Rust' and derived from Berkeley 'SPICE3f5', validated against 'ngspice'. Results come back as long tibbles ready for plotting, and plots can be read from and written to 'SPICE3' rawfiles for exchange with other simulators.


spice spice logo

CI Lifecycle: experimental License: BSD-3-Clause DOI PyPI spice3 npm dune-sim crates.io dune-core

spice runs SPICE3 circuit netlists from R and hands the results back as tidy data. The engine is Dune, a circuit simulator written in Rust and derived from Berkeley SPICE3f5, checked circuit by circuit against ngspice.

  • Every SPICE3f5 analysis: operating point, DC sweep, transient, AC, pole-zero, noise, distortion, transfer function and sensitivity, plus periodic steady state (spice_pss()).
  • Every SPICE3f5 device, and BSIM3v3.
  • Long tibbles out (spice_tidy()), ready for 'ggplot2', or one column per variable (spice_wide()); plots picked by short name ("tran", "ac") and variables by node; autoplot() for a quick look.
  • Sessions and sweeps: alter a part and rerun without rebuilding (spice_session()), or sweep parameters across threads (spice_sweep()).
  • Decks as chunks: a knitr engine runs ```{spice} chunks in R Markdown and Quarto.
  • Circuits from figures: spice_pic() extracts the netlist of a schematic drawn in the pic language.
  • Rawfiles in SPICE3's own format, to exchange with other simulators (spice_read_raw(), spice_write_raw()).

Installation

spice is not on CRAN yet. Install the development version from GitHub; it compiles Rust code, so it needs cargo and rustc (>= 1.88), from https://rust-lang.org/tools/install/.

# install.packages("remotes")
remotes::install_github("milkway/dune", subdir = "r")

A first circuit

library(spice)

deck <- "RLC low-pass
vin in 0 pulse(0 1 0 1u) ac 1
r1 in mid 50
l1 mid out 1m
c1 out 0 1u
.tran 2u 1m
.ac dec 50 100 100k
.end"

res <- spice_run(deck)
res |> spice_tidy("tran", "out")   # long: one row per point
res |> spice_tidy("ac", "out")     # complex plots carry mag, db, phase
res |> spice_wide("tran")          # wide: one column per variable

vignette("spice") walks through this deck end to end.

Faithful to SPICE, and better where it can be measured

Netlist syntax, device models and analyses are SPICE3f5's. Where Dune departs from it by default, it is because the result is measurably closer to the converged answer at the same step: a transient integrates with TR-BDF2 instead of the trapezoidal rule, a switch flips at the instant its control crosses the threshold, a lossy line keeps the corners of the waves it carries. One option restores the SPICE3 behaviour of all of them:

spice_run(deck, options = list(compat = "spice3"))

vignette("defaults") shows the difference on a small circuit.

How it works

How a simulation runs: the deck is parsed and built into a circuit; for every point of an analysis the devices are linearized, the linear system is solved and convergence is tested, iterating until it holds; an accepted point goes to the results.

spice_run() hands the deck to the engine. It is parsed and built into a circuit, and every point of every analysis is a Newton-Raphson loop: linearize the devices, solve the linear system, test convergence at SPICE's tolerances. The accepted points come back as the plots that spice_tidy() and spice_wide() turn into tibbles.

The same engine elsewhere

Architecture of Dune: a pic figure goes through dune-pic and a SPICE deck through dune-netlist into dune-core, the engine; dune-raw writes rawfiles; the command line and the Python, R, JavaScript and C interfaces all sit on that one engine.

spice is one of several front ends to Dune; a deck gives the same numbers in all of them.

package install
Python spice3 pip install spice3
JavaScript / WebAssembly @strategicprojects/dune-sim npm install @strategicprojects/dune-sim
Rust dune-core, with dune-netlist, dune-raw and dune-pic cargo add dune-core
Command line dune brew install milkway/tap/dune

The circuit gallery shows every circuit of the engine's test corpus with its results, and rpic is the pic-language implementation behind spice_pic().

Citing

Dune has a DOI: 10.5281/zenodo.24052213. citation("spice") gives the entry for the package.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("spice")

0.1.0 by André Leite, 18 hours ago


https://milkway.github.io/dune/r/, https://github.com/milkway/dune


Report a bug at https://github.com/milkway/dune/issues


Browse source code at https://github.com/cran/spice


Authors: André Leite [aut, cre] , The Regents of the University of California [cph] ('SPICE3f5' , from which the simulation engine is derived) , The authors of the dependency Rust crates [cph] (see inst/AUTHORS for details)


Documentation:   PDF Manual  


BSD_3_clause + file LICENSE license


Imports cli, rlang, tibble

Suggests ggplot2, knitr, rmarkdown, testthat

System requirements: Cargo (Rust's package manager), rustc (>= 1.88.0)


See at CRAN