---
title: "Sweeps, tolerances and design exploration"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Sweeps, tolerances and design exploration}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
*The results on this page were computed when the vignette was written
(see `vignettes/precompile.R`); the code is what produced them.*
``` r
library(spice)
```
A simulation answers "what does this circuit do". Design asks "what
should the values be" and "what happens when they are off by 5 %".
`spice_sweep()` runs one deck over many parameter sets, in parallel if
asked, and returns everything as one tidy table.
## Choosing the damping
A second-order RLC low-pass. The resistor sets the damping: too little
and the step rings, too much and it crawls.
``` r
rlc <- "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 1.2m
.end"
```
Critical damping is at
``` r
2 * sqrt(1e-3 / 1e-6)
#> [1] 63.24555
```
``` r
sw <- spice_sweep(rlc, r1 = c(10, 30, 63, 150))
tr <- sw |> spice_tidy(vars = "out")
ggplot(tr, aes(scale * 1e3, value, colour = factor(r1))) +
geom_line(linewidth = 0.7) +
labs(x = "time (ms)", y = "v(out) (V)", colour = "r1 (ohm)",
title = "Step response against damping")
```
plot of chunk damping
plot of chunk hist