Running an AC simulation

Doing an AC simulation is very similar to a DC simulation. Except that now the simulated results will be complex instead of real. This means that now the StateComplex-property of the circuit needs to be used to access our simulation data.

We should also specify an "acmag" parameter of 1.0 for the voltage source to provide the system with an excitation.

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

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;
            chartResults.ChartAreas[0].AxisX.Minimum = 1.0;
            chartResults.ChartAreas[0].AxisX.Maximum = 1.0e9;
            chartResults.ChartAreas[0].AxisX.IsLogarithmic = true;

            // 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")["acmag"] = 1.0; // Make sure the voltage excites the system
            DoAC(ckt, 100, 1.0, 1.0e9);
        }

        /// <summary>
        /// Do an AC simulation
        /// </summary>
        /// <param name="ckt">The circuit</param>
        /// <param name="n">The number of steps per decade</param>
        /// <param name="start">The initial frequency</param>
        /// <param name="stop">The final frequency</param>
        private void DoAC(Circuit ckt, int n, double start, double stop)
        {
            SpiceTask task = new SpiceTask("Task 1");

            // Create a DC analysis with a job
            AC ac = new AC();
            ac.AddJob(new AcJob("AC1", AC.StepTypes.Decade, n, start, stop));
            ac.ExportData += Export;

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

        /// <summary>
        /// Export the data to the chart
        /// </summary>
        /// <param name="a"></param>
        /// <param name="ckt"></param>
        private void Export(Analysis a, Circuit ckt)
        {
            // Get the frequency - Omega is actually w*1i here, so we need the 
            // imaginary part and convert it from rad/s to Hz
            double frequency = ckt.StateComplex.Omega.Imaginary / 2.0 / Math.PI;

            // Get the output amplitude and convert it to decibels
            int index_out = ckt.Nodes["OUT"].Number;
            double dB_out = 20.0 * Math.Log10(ckt.StateComplex.Solution[index_out].Magnitude);

            // Add the point to the graph
            serieOutput.Points.AddXY(frequency, dB_out);
        }
    }
}

This leads to the next graph:


Finally we will do a transient simulation!
 

Last edited Feb 4 at 8:32 PM by SBoulang, version 3