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!
Platform | Status |
---|---|
AppVeyor CI (Windows) build status | |
Travis CI (Linux/Mono) build status |
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.
Creating this circuit is done using the Circuit-class. This is a container of multiple so-called entities (IEntity), such as voltage sources and resistors. The Circuit-class 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 (by default) sweep a voltage or current source value and it will assume a "quiet" circuit. The result is a DC 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);
By default, access to simulation output data can be achieved by registering to the ExportSimulationData event. This event is automatically fired by the simulation when the data is ready to be exported.
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. For example, we could be interested in the current through voltage source V1. In which case we can define some exports:
// 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:
-1 V : -0.667 V. 3.33E-05 A
-0.8 V : -0.533 V. 2.67E-05 A
-0.6 V : -0.4 V. 2E-05 A
-0.4 V : -0.267 V. 1.33E-05 A
-0.2 V : -0.133 V. 6.67E-06 A
0 V : 0 V. 0 A
0.2 V : 0.133 V. -6.67E-06 A
0.4 V : 0.267 V. -1.33E-05 A
0.6 V : 0.4 V. -2E-05 A
0.8 V : 0.533 V. -2.67E-05 A
1 V : 0.667 V. -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.