Creating a circuit

An electronic circuit in Spice has the following structure:
  • The Circuit object bundles all circuit data and simulation data.
    • One or more Model objects, such as ResistorModel, DiodeModel, ...
      • One or more Instance objects such as Resistor, Diode, ...
    • State/StateComplex properties - contains simulation data
    • Config property - Some simulation properties
    • Nodes property - A mapping of node names to indices in the solved equations

We will now make the following circuit:


By default, the Circuit class is in the SpiceNet.Circuits namespace, and all models and devices are in the SpiceNet.Models namespace.
We can add models to the circuit, or we can add instances/devices without specifying a model. In case of the latter, the circuit will automatically create a model with default parameters for us.
The code then looks as follows:

using System;
using System.Windows.Forms;
using SpiceNet.Circuits;
using SpiceNet.Models;

namespace SpiceNetTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 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));
            ckt.Add(new Diode("D1", "OUT", "GND"));
        }
    }
}

The default model parameters for a diode are rather awkward sometimes, so we will use an actual diode model. Let us use the NXP 1N4148 diode model for our tutorial:
 .MODEL 1N4148 D 
+ IS = 4.352E-9 
+ N = 1.906 
+ BV = 110 
+ IBV = 0.0001 
+ RS = 0.6458 
+ CJO = 7.048E-13 
+ VJ = 0.869 
+ M = 0.03 
+ FC = 0.5 
+ TT = 3.48E-9 


We would then create the model and add an instance to the model instead of the circuit. Then we can add the created model to the circuit.

// 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);

As you can see, model parameters are accessed by their lowercase name by convention. Unlike Spice, the names of the parameters here are case-sensitive. If a model parameter does not exist, a warning will be generated (ref. SpiceNet.Diagnostics.CircuitWarning). It is also possible to change parameters of instances in exactly the same way as models.
You can ask a list of accessible parameters by checking the instance's pTable property. Note that some parameters are read-only or write-only.

It is also possible to reconnect each instance to other nodes by using the function Connect(). For example, you could change the direction of the diode by writing:
Instance diode = new Diode("D1", "OUT", "GND");
...
diode.Connect("GND", "OUT");

For the rest of this tutorial, I will use the 1N4148 diode model. Our code now looks like this:
using System;
using System.Windows.Forms;
using SpiceNet.Circuits;
using SpiceNet.Models;

namespace SpiceNetTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 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);
        }
    }
}

In the next section we will run this circuit through a DC simulation!
 

Last edited Feb 5 at 9:20 AM by SBoulang, version 12