Getting started
In this section we will try to quickly go over everything you need to create a simple circuit and simulate it using Spice#.
Installation
The easiest way to install Spice# is by installing the NuGet package Spice#.
You can also clone the repository directly. However, while you get the latest features and bug fixes, the documentation might not be up to date!
Status | |
---|---|
Windows | |
MacOS | |
Linux/Ubuntu |
Building a circuit
Let's start with a very simple circuit known as a resistive voltage divider. The schematic looks as follows.
The output voltage of this circuit is 2/3 times the input voltage for those wondering.
The components are stored in a Circuit. This is a container for so-called entities (IEntity), which is the term for anything that can affect simulations. The Circuit is defined in the namespace SpiceSharp, while all default components are typically specified in the namespace SpiceSharp.Components.
// Build the circuit
var ckt = new Circuit(
new VoltageSource("V1", "in", "0", 1.0),
new Resistor("R1", "in", "out", 1.0e4),
new Resistor("R2", "out", "0", 2.0e4)
);
Running a DC analysis on the circuit
A DC simulation will sweep a voltage or current source value (or anything else in Spice#). The result is a transfer curve in function of the swept parameter.
We will sweep the input voltage source from -1V to 1V in steps of 200mV.
// Create a DC simulation that sweeps V1 from -1V to 1V in steps of 100mV
var dc = new DC("DC 1", "V1", -1.0, 1.0, 0.2);
// Catch exported data
dc.ExportSimulationData += (sender, args) =>
{
var input = args.GetVoltage("in");
var output = args.GetVoltage("out");
};
dc.Run(ckt);
Access to simulation output data is usually achieved by registering to the ExportSimulationData event. This event is automatically fired by the simulation when the data is ready to be exported. The event arguments contains some easy access to main parts of the circuits (like voltages and currents).
The output will show:
-1 V : -0.667 V
-0.8 V : -0.533 V
-0.6 V : -0.4 V
-0.4 V : -0.267 V
-0.2 V : -0.133 V
0 V : 0 V
0.2 V : 0.133 V
0.4 V : 0.267 V
0.6 V : 0.4 V
0.8 V : 0.533 V
1 V : 0.667 V
Using exports
Using exports allows for faster access to voltages, currents, circuit properties, etc. compared to using the event arguments of ExportSimulationData. For example, we could be interested in the current through voltage source V1. In which case we can define some exports like this:
// Create a DC simulation that sweeps V1 from -1V to 1V in steps of 100mV
var dc = new DC("DC 1", "V1", -1.0, 1.0, 0.2);
// Create exports
var inputExport = new RealVoltageExport(dc, "in");
var outputExport = new RealVoltageExport(dc, "out");
var currentExport = new RealPropertyExport(dc, "V1", "i");
// Catch exported data
dc.ExportSimulationData += (sender, args) =>
{
var input = inputExport.Value;
var output = outputExport.Value;
var current = currentExport.Value;
};
dc.Run(ckt);
This will lead to the result:
input = -1
(V),output = -0.667
(V),current = 3.33e-05
(A)input = -0.8
(V),output = -0.533
(V),current = 2.67e-05
(A)input = -0.6
(V),output = -0.4
(V),current = 2e-05
(A)input = -0.4
(V),output = -0.267
(V),current = 1.33e-05
(A)input = -0.2
(V),output = -0.133
(V),current = 6.67e-06
(A)input = 0
(V),output = 0
(V),current = 0
(A)input = 0.2
(V),output = 0.133
(V),current = -6.67e-06
(A)input = 0.4
(V),output = 0.267
(V),current = -1.33e-05
(A)input = 0.6
(V),output = 0.4
(V),current = -2e-05
(A)input = 0.8
(V),output = 0.533
(V),current = -2.67e-05
(A)input = 1
(V),output = 0.667
(V),current = -3.33e-05
(A)
These export classes are setup automatically. These classes dynamically build a method that allows extracting data more efficiently during the ExportSimulationData event compared to using the event's arguments.