Spice.NET is a port from the popular circuit simulation software Berkeley Spice. The original C code can be found on their website https://bwrcs.eecs.berkeley.edu/Classes/IcBook/SPICE/.
Please note that this is a hobby project. It is in no way meant to compete with other Spice-based simulators out there. The name comes from the combination of "Spice" and "Math.NET".

Dependencies

Spice.NET uses Math.NET to solve the matrix equations associated to the circuit. This library can be found here: https://www.mathdotnet.com/.

Differences with Spice

The original Spice software was written in C. As the original code is not object-oriented, this port required some creativity. Some parts have been abstracted to ensure that the software can be expanded more easily.
Another part I changed is the truncation method when integrating in Transient analysis using the Trapezoidal rule. This method is used to control time-steps based on the Local Truncation Error (LTE). I did the math myself (after having some problems with it), and found different formula's than Spice.

Features

Spice.NET implements the following features:
  • Circuit management, with a (named) node Framework
  • Models and Instances (Devices) Framework
    • VSRC/ISRC - Voltage Sources and Current Sources
      • Waveforms, DC, AC sources
    • VCVS/VCCS - Voltage-Controlled Voltage Sources and Current Sources
    • CCVS/CCCS - Current-Controlled Voltage Sources and Current Sources
    • Res - Resistors
    • Cap - Capacitors
    • Ind - Inductors and mutual inductances
    • External (optional) components can be added. Currently provided:
      • BJT - Bipolar Junction Transistor
      • BSIM 1, 2, 3.2.4 and 3.3.0 - Berkeley SIM MOSFET transistor
      • Dio - Diode model
      • JFet - JFet model
      • ... Possibly more to come
  • Simulations
    • DC curve
    • AC analysis
    • Transient analysis
    • Noise analysis

Not implemented

Currently, Spice.NET does not implement the following features that the original Spice project has:
  • Netlist parser
  • Distortion analysis
  • Sensitivity analysis
  • Pole-Zero analysis
  • The GEAR integration algorithm for transient analysis

Usage

Following the original Spice structure, everything starts with the Circuit class. Each Circuit contains one or more Model objects. Each Model contains one or more Instances (devices). Each instance can only be added to one type of model, and is automatically added to a default model if applicable.

To build a simple RC low-pass filter, one might write:

// Initialize
Circuit ckt = new Circuit();
ckt.Add(new VoltageSource("V1", "IN", "GND", 1.0));
ckt.Add(new Resistor("R1", "IN", "OUT", 1.0e3));
ckt.Add(new Capacitor("C1", "OUT", "GND", 1.0e-6));

To perform a simulation, a SpiceTask is needed. A SpiceTask contains one or more Analysis and the analysis will finally run a job. When data is ready to be exported, the event ExportData is called.
Put it all together, and you get something like:

ckt.FindInstance("V1")["acmag"] = 1.0; // AC magnitude for V1
SpiceTask task = new SpiceTask("Task 1");
AC analysis = new AC();
analysis.ExportData += analysis_ExportData;
analysis.AddJob(new AcJob("AC Job 1", AC.StepTypes.Decade, 100, 1.0, 1.0e5));
task.Analyses.Add(analysis);

// Perform the analysis
ckt.DoJob(true, task);

/// <summary>
/// Export analysis data
/// </summary>
/// <param name="a"></param>
/// <param name="ckt"></param>
void analysis_ExportData(Analysis a, Circuit ckt)
{
    // Get the row number in the solution
    int rowout = ckt.Nodes["OUT"].Number;

    // Get the frequency
    double freq = ckt.StateComplex.Omega.Imaginary / 2.0 / Math.PI;
    if (freq == 0.0)
        return;

    // Get the amplitude in dB
    double amp = 20.0 * Math.Log10(ckt.StateComplex.Solution[rowout].Magnitude);
    output.Points.AddXY(freq, amp);
}

Last edited Feb 25 at 9:18 AM by SBoulang, version 14