Running a Transient simulation

A transient simulation only becomes interesting if we have a time-varying signal driving the system. So we will apply a Waveform-object (a Sine object) to the voltage source to get some nice-looking simulation results of a half-wave rectifier.

It should be noted that in the original Spice software, a waveform was an integral part of the instance code. Spice.NET does not follow this paradigm, and provides an abstract Waveform class that can generate values in function of time.

using System;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using SpiceNet.Circuits;
using SpiceNet.Models;
using SpiceNet.Simulations;
using SpiceNet.Models.Waveforms;

namespace SpiceNetTest
{
    public partial class Form1 : Form
    {
        private Series serieOutput;

        /// <summary>
        /// Constructor
        /// </summary>
        public Form1()
        {
            InitializeComponent();

            // Setup the chart
            chartResults.Series.Clear();
            serieOutput = chartResults.Series.Add("Output");
            serieOutput.ChartType = SeriesChartType.FastLine;

            // Create a circuit
            Circuit ckt = new Circuit();

            // Add devices to the circuit
            ckt.Add(new VoltageSource("V1", "IN", "GND", 1.0));
            ckt.Add(new Resistor("R1", "IN", "OUT", 1.0e3));

            // Create the diode model
            Model model = new DiodeModel("1N4148");
            model["is"] = 4.352e-9; model["n"] = 1.906;
            model["bv"] = 110; model["ibv"] = 0.0001;
            model["rs"] = 0.6458; model["cjo"] = 7.048e-13;
            model["vj"] = 0.869; model["m"] = 0.03;
            model["fc"] = 0.5; model["tt"] = 3.48e-9;
            model.Add(new Diode("D1", "OUT", "GND"));
            ckt.Add(model);

            // Perform a DC analysis from -5V to 5V in steps of 1mV
            ckt.FindInstance("V1")["waveform"] = new Sine(0.0, 5.0, 1.0e6); // 1MHz
            DoTran(ckt, 1e-9, 5e-6);
        }

        /// <summary>
        /// Do an AC simulation
        /// </summary>
        private void DoTran(Circuit ckt, double maxStep, double stop)
        {
            SpiceTask task = new SpiceTask("Task 1");

            // Create a DC analysis with a job
            DcTran tran = new DcTran();
            tran.AddJob(new TranJob("Tran1", maxStep, stop));
            tran.ExportData += Export;

            // Add the DC analysis to the task and run it
            task.Analyses.Add(tran);
            ckt.DoJob(true, task);
        }

        /// <summary>
        /// Export the data to the chart
        /// </summary>
        private void Export(Analysis a, Circuit ckt)
        {
            // Get the time and voltage
            double time = ckt.State.Time;
            int index_out = ckt.Nodes["OUT"].Number;
            double voltage = ckt.State.Solution[index_out];
            serieOutput.Points.AddXY(time, voltage);
        }
    }
}

Giving us the result:


And there we have it. We quickly went over the three most used simulation types: DC, AC and Transient simulations. We built a circuit with a nonlinear component and we showed that the numerical methods give sensible results.

All that is left, is to say: Have fun!

Last edited Feb 4 at 9:43 PM by SBoulang, version 5