Advanced Examples
Introduction
The netlists and scripts discussed on this page are intended to give users of MacSpice 3 and other Spice-compatible simulators examples illustrating how the command line interpreter can be used to perform sosphisticated tasks. There are also some simpler Introductory Examples and Optimizer Examples (MacSpice-specific) elsewhere on this site.
The Ljubljana Examples
The examples in this section illustrate how the MacSpice interactive interpreter can be used to automate complicated analyses.
Puhan et al. (1998) used Spice to investigate how the output impedance of a transistor amplifier increases at low output voltages as a result of crossover distortion reducing the loop-gain. The example file PowerAmpMac.cir recreates graphs corresponding to figures in their paper:
- Figure 2 - The crossover compensation.
- Figure 3 - The output impedance at different biases.
- Figure 5 - The output impedance vs the input offset.
The same paper discusses using the Nelder and Mead (1965) simplex method to optimise the frequency response of a one-transistor amplifier by adjusting the values of two resistors. SimplexMac.cir performs this process.
I am grateful to Janez Puhan for providing copies of the original (poweramp.cir) and (simplex.cir) examples referred to in the above paper.
References
J. A. Nelder and R. A. Mead, A simplex method for function minimization, Computer Journal 7 (1965) 308-313.
J. Puhan, T. Tuma, I. Fajfar, SPICE for Windows 95/98/NT, Electrotechnical Review, Ljubljana Slovenia 65(5) (1998) 267-271.
Model Validation and Netlist Documentation
The examples in this section illustrate some of the steps in the design of a precision bandgap voltage reference, and have been constructed from netlists generously contributed by Don Sauer. His website, Ideas to Integrated Circuits, contains many more interesting examples.
If you use Spice to simulate circuts you must
validate the models by comparing simulated tests and measurements with results
from the physical devices. For example,
CompareNPNwithSi.cir is used to
check that the temperature dependence of a NPN device model
VNPNsauer.mod agrees with measured values. Note the comment
observing that The XTI term is strange. The number really should be 3.
But a different number seems to be required on every different Spice
simulator.
. This is a compelling illustration of how important
this type of test is.
Spice netlists are a form of software and must be documented appropriately. The Bandgap.cir example (which requires LPNPsauer.mod and VNPNsauer.mod to run) includes an 'ascii-art' rendering of the circuit diagram. Also, it uses the interactive command language to make virtual measurements at a list of spot temperatures.
Gaussian Noise Source
The rndsrc script generates Gaussian random numbers using the Box-Mueller algorithm. These are output into a file 'PWL_FILE.inc' in the syntax required for a PWL voltage source, which is ready to be imported into a netlist by means of the .include mechanism.
Plotting Graphs
Colour Schemes
MacSpice uses a palette of 20 colours for its graphical display. These can be redefined by setting the appropriate variables (color1...color20) as illustrated:
This example also illustrates how the hardcopy command can be used to make MacSpice generate colour PostScript files.
Multiple Analyses on the Same Graph
This example runs AC analyses at several different temperatures and then displays the results on a single graph.
Graphs with Dotted / Dashed / Multi-Coloured Curves
Dotted and dashed lines and multi-coloured curves can be drawn on MacSpice plots by exploiting the fact that IEEE-754 NaN values are not drawn:
Initial Conditions
The recommended way to set non-equilibrium initial conditions is to use the '.IC' statement. This example illustrates the transient behaviour of a second-order filter circuit fed by a 1.0 V DC source V1.
In the absence of a '.IC' statement:
all nodes remain at a constant 1.0 V during the transient analysis. By adding a '.IC' statement, node 2 can be given an initial value, say 0.5 V, differing from its steady-state default:
Notice that specifying the voltage at node 2 in this manner has affected the initial voltage of node 3. The file:
illustrates that it is not possible to achieve the desired results simply by adding the 'UIC' keyward to the tran statement and specifying an initial voltage as part of the statement defining C2. This is because 'UIC' inhibits calculation of the quiescent state so (in the absence of specific '.IC' statements) all nodes default to zero as their initial value.
The Alter Command
The alter and altermod commands can be used to modify the properties device instances and models in the current circuit. It is most often used in scripts. For example,
Alter-Demo.cir takes a simple RC-filter and plots graphs showing how it responds to square wave for a range of resistance values, and then to a sinewave input created by altering the time-dependent function of the voltage source.
Translation of PSpice Netlists
Many netlists that are freely available on the internet are written for PSpice in an extended version of the syntax used by MacSpice. This Perl script:
- ps2macspice.zip (Version 1.3.1)
is able to convert many such netlists into a standard (Berkeley Spice 3f4) source file.
Sudoku Solver
Every new version of MacSpice must solve a hard Sudoku puzzle as one of its pre-release tests. The scripts in:
illustrate how MacSpice is programmed to accomplish this using its command line interpreter language. To run the scripts, drag 'Sudoku Example' onto the MacSpice application icon.