## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(spice)

## ----eval = FALSE-------------------------------------------------------------
# spice_run(deck, options = list(compat = "spice3"))

## ----deck---------------------------------------------------------------------
deck <- "relaxation oscillator
vdd 1 0 10
r1 1 2 10k
c1 2 0 100n ic=0
s1 2 0 2 0 sdis
.model sdis sw vt=5 vh=2 ron=100 roff=1e9
.tran 50u 20m uic
.end"

rp <- 1e4 * 100 / 1.01e4
vinf <- 10 * 100 / 1.01e4
exact <- 1e4 * 100e-9 * log(7 / 3) +
  rp * 100e-9 * log((7 - vinf) / (3 - vinf))
exact

## ----periods------------------------------------------------------------------
periods <- function(res) {
  v <- res |> spice_tidy("tran", "v(2)")
  i <- seq(2, nrow(v) - 1)
  peak <- i[v$value[i] >= v$value[i - 1] & v$value[i] > v$value[i + 1] &
    v$value[i] > 6]
  diff(v$scale[peak])
}

dune <- periods(spice_run(deck))
spice3 <- periods(spice_run(deck, options = list(compat = "spice3")))

## ----dune---------------------------------------------------------------------
range(dune / exact - 1)

## ----spice3-------------------------------------------------------------------
range(spice3 / exact - 1)

