DC analysis
A DC analysis makes it possible to sweep over one or more independent sources, assuming the circuit is static (not changing in time, anywhere in the circuit). You can for example construct the input-output relation of an amplifier using this type of analysis, or find out how a resistor influences other parts of the circuit. It is run using the DC class.
Let's consider the following circuit:
We wish to find the I-V curve for multiple Vgs voltages. This can be achieved by passing multiple ISweep instances to the simulation's constructor (you can also change them afterwards in the simulation's DC parameters).
The code will look like this:
// Make the bipolar junction transistor
var nmos = new Mosfet1("M1", "d", "g", "0", "0", "example");
var nmosmodel = new Mosfet1Model("example");
nmosmodel.SetParameter("kp", 150.0e-3);
// Build the circuit
var ckt = new Circuit(
new VoltageSource("Vgs", "g", "0", 0),
new VoltageSource("Vds", "d", "0", 0),
nmosmodel,
nmos
);
// Sweep the base current and vce voltage
var dc = new DC("DC 1", new[]
{
new ParameterSweep("Vgs", new LinearSweep(0, 3, 0.2)),
new ParameterSweep("Vds", new LinearSweep(0, 5, 0.1)),
});
// Export the collector current
var currentExport = new RealPropertyExport(dc, "M1", "id");
// Run the simulation
dc.ExportSimulationData += (sender, args) =>
{
var vgsVoltage = dc.GetCurrentSweepValue()[0];
var vdsVoltage = dc.GetCurrentSweepValue()[1];
var current = currentExport.Value;
};
dc.Run(ckt);
First we build our NMOS transistor M1. For this we need an IComponent implementation for a mosfet, which we can connect to nodes in the circuit. Most components also need a reference to a model (another IEntity in the same circuit) to function properly. Models typically describe general common properties (eg. threshold voltage, transconducance gain, etc.) while components will typically describe behavior on a device-by-device basis (eg. transistor width and length, device temperature, etc.).
In our case, Spice# provides us with a component that implements the model equations of a transistor, called Mosfet1, which also needs a model of the type Mosfet1Model. This model is identical to Spice's mosfet LEVEL=1 model.
Every entity can have one or more parameters, which our stored in parameter sets. The parameters can be accessed directly, or using their given name in conjunction with the SetParameter method. The mosfet's Parameters and ModelParameters contain all the available parameters, including their given names which are defined by a ParameterName attribute. Do keep in mind that parameter sets can extend other parameter sets that also contain parameters! Use the API and the GitHub repository to your advantage to find out which parameters you can change.
After running and plotting the data we get:
If we wanted to implement the same simulation in the original Spice simulator, we would provide the following netlist:
NMOS biasing example
.MODEL example NMOS(Kp=150m)
M1 d g 0 0
Vgs g 0 0
Vds d 0 0
.DC Vds 0 5 0.1 Vgs 0 3 0.2
* Export voltages/currents/...
.END
This netlist would be parsed, executed, and the results are then written to a file which can then be processed. Spice# is a library, and you have access to the data during execution. For example, you could change parts of the simulation during runtime, you could automate designs, and you can freely choose during the simulation which data you want to use and ignore.