optimise / optimize
Optimise device parameters
General form
optimise [options] vary ... [vary ...] goal optimise
Adjust device and model parameters to minimise or maximise an expression evaluated on the results of an analysis, or to bring it as close as possible to a target. optimize is the same command.
The search uses the Nelder-Mead simplex method, running the analysis once for each set of parameter values it tries. When it finishes, the best values are set in the circuit and simulated once more, and that run's plot is kept as the current plot. The result is printed with the number of simulations used, the best value of the goal, and a table of the parameters.
A .optimise (or .optimize) card in the circuit takes the same arguments. A run does the cards first, in order, and then runs the circuit with the optimised values. optimise with no arguments does the cards on their own.
Parameters to vary
vary dev [param] min max [init] [lin|log] vary @dev[param] min max [init] [lin|log] varymod model param min max [init] [lin|log] varymod @model[param] min max [init] [lin|log]
vary gives a device parameter (a device's principal value, such as a resistance, if param is left out), and varymod a model parameter. Each is kept between min and max, starting from init if given. The search works on a logarithmic scale if the range is positive and covers a decade or more, or if log is given; lin forces a linear scale.
Goal
The goal must come last: the rest of the line is the expression.
minimise expr- Make expr as small as possible.
minimizeis the same. maximise expr- Make expr as large as possible.
maximizeis the same. target value expr- Make expr as close as possible to value, by minimising the mean square error. Both may be vectors, so a whole response can be fitted to a curve.
The expression is evaluated on each run's results, so it can use the analysis's vectors and scale, and the results of .meas cards.
The .meas cards are run again after every run, so their results follow the parameters. A meas command, even one in a .control block before optimise, only measures the plot that was current when it ran; the runs optimise does won't have its result, and the goal fails with no such vector. Use a .meas card instead.
Options
analysis analysis line- The analysis to run for each evaluation, for example
analysis tran 1n 1u. By default the circuit's own analyses are run, as by run. The analysis only needs to cover what the goal uses, and a shorter one makes each evaluation faster: for a goal at one frequency, an ac analysis of that frequency alone is enough, for exampleanalysis ac lin 1 1k 1k. maxeval n- The maximum number of simulations (default 500).
tol x- The convergence tolerance (default 1e-4).
step x- The initial size of the simplex, as a fraction of each parameter's range (default 0.1).
restarts n- How many times to restart the search after it converges (default 1).
quiet- Only print the result.
Steps outside a parameter's range are clamped to it. A simulation that fails is given a very large cost, so the search moves away from it.
Examples
Choose c1 in an RC low pass for a 5µs rise time, using a .meas result as the goal (from examples/optimise_card_demo.cir). The expected result is c1 = 5µ/(1k·ln 9) = 2.276nF.
vin in 0 pulse(0 1 1u 1n 1n 50u 100u) r1 in out 1k c1 out 0 1n .tran 0.05u 20u .meas tran risetime TRIG v(out) VAL=0.1 RISE=1 TARG v(out) VAL=0.9 RISE=1 .optimise analysis tran 0.05u 20u vary c1 0.1n 10n target 5u risetime
Set the gain of an amplifier at 1kHz to 40dB by choosing the emitter resistor r3. The circuit's own .ac card sweeps from 1Hz to 1MHz, but each evaluation only needs the one frequency:
.ac dec 10 1 1meg .meas ac gain1k FIND vdb(out) AT=1k .control optimise analysis ac lin 1 1k 1k vary r3 100 10k target 40 gain1k .endc
Fit a Sallen-Key low pass to the magnitude response of a 1kHz Butterworth filter over the whole ac sweep (from examples/optimise_demo.cir). The target is a vector, compared point by point; the expected result is c1 = 22.5nF and c2 = 11.25nF.
optimise analysis ac dec 10 100 10k vary c1 1n 100n vary c2 1n 100n target (1/sqrt(1+(frequency/1k)^4)) mag(v(out))