--- title: "Getting started with spice" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Getting started with spice} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>") has_ggplot2 <- requireNamespace("ggplot2", quietly = TRUE) ``` spice runs SPICE3 netlists on Dune, a circuit simulator derived from Berkeley SPICE3f5, and hands the results back as tidy data. This vignette runs one deck end to end: a second-order RLC low-pass driven by a voltage step, with a transient and an AC analysis. ```{r setup} library(spice) ``` ## A deck A deck is ordinary SPICE3 input. The first line is the title; the analysis cards (`.tran`, `.ac`, ...) say what to compute. Here it is given as text; `spice_run()` equally accepts the path of a `.cir` file. Schematic of the RLC low-pass: source Vin, series R1 and L1, capacitor C1 across the output. ```{r deck} 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" ``` ## Running it ```{r run} res <- spice_run(deck) res ``` Each analysis produces a plot, named as SPICE3 names it in a rawfile. Simulator options can be overridden from R, with the names and units of the `.options` card: ```{r options} tight <- spice_run(deck, options = list(reltol = 1e-5, method = "gear")) ``` ## Tidy results `spice_tidy()` (or `tibble::as_tibble()` of one plot) returns the long format: one row per point and variable, with the plot's scale repeated on every row. Its second argument picks plots, by label or by a short name of the analysis (`"op"`, `"dc"`, `"tran"`, `"ac"`, ...). ```{r tidy} tran <- spice_tidy(res, "tran") tran ``` The third argument picks variables; a bare node name stands for its voltage. The AC plot is complex, so instead of `value` it has `re` and `im`, and with them `mag`, `db` and `phase` (in degrees). ```{r ac} ac <- res |> spice_tidy("ac", "out") ac[, c("scale", "db", "phase")] ``` `spice_wide()` is the other layout, for arithmetic between signals: one row per point, the scale first and one column per variable. ```{r wide} w <- res |> spice_wide("tran") w # The largest voltage across the inductor max(abs(w$`v(mid)` - w$`v(out)`)) ``` ## Plotting the transient ```{r plot, eval = has_ggplot2, fig.width = 6.5, fig.height = 3.5} library(ggplot2) volts <- res |> spice_tidy("tran", c("in", "out")) ggplot(volts, aes(scale * 1e3, value, colour = variable)) + geom_line() + labs(x = "time (ms)", y = "voltage (V)", colour = NULL, title = "Step response of the RLC low-pass") ``` For a quick look, `autoplot()` draws any plot with a scale: ```{r autoplot, eval = has_ggplot2, fig.width = 6.5, fig.height = 3.5} autoplot(spice_plots(res)[["AC Analysis"]]) ``` ## Decks as chunks Loading spice also gives knitr a `spice` engine. In R Markdown or Quarto, a ```` ```{spice} ```` chunk holds a deck: the engine runs it, prints the head of its tidy table, and leaves the result in a variable named after the chunk label, ready for the R chunks below it (see `?spice_knitr` for the chunk options). ````markdown `r ''````{spice divider} divider v1 1 0 dc 0 r1 1 2 1k r2 2 0 3k .dc v1 0 10 1 .end ``` ```` ```{spice divider} divider v1 1 0 dc 0 r1 1 2 1k r2 2 0 3k .dc v1 0 10 1 .end ``` ```{r divider-plot, eval = has_ggplot2, fig.width = 6.5, fig.height = 3} autoplot(spice_plots(divider)[[1]]) ``` ## Circuits from figures A schematic drawn in [pic](https://github.com/milkway/rpic-lang) with the dune prelude (`resistor()`, `vsource()`, `npn()`, ...; the figure opts in with `spice = 1`) is a deck too: `spice_pic()` extracts its netlist -- the block labels are the refdes, the wires the nodes -- and `spice_run()` runs the extraction. `spice_pic_svg()` draws the figure. ```{r pic} fig <- ".PS spice = 1 In: (0,1); Out: (2,1) V1: vsource(In, (0,0), DC 5) R1: resistor(In, Out, 1k) R2: resistor(Out, (2,0), 3k) line from R2.N to V1.N; ground(V1.N) .op .PE" p <- spice_pic(fig) cat(p$netlist) spice_run(p) |> spice_op("out") p$map$pins ``` ## Exchanging rawfiles Results write to and read from SPICE3 rawfiles, the format ngspice reads and writes with `-r`: ```{r raw} f <- tempfile(fileext = ".raw") spice_write_raw(res, f) back <- spice_read_raw(f) identical(spice_tidy(back), spice_tidy(res)) ```