public void When_VCCSDC2_Expect_Reference() { // Found by Marcin Golebiowski var ckt = new Circuit( new VoltageSource("V1", "1", "0", 200), new Resistor("R1", "1", "0", 10), new VoltageControlledCurrentSource("G1", "2", "0", "1", "0", 1.5), new Resistor("R2", "2", "0", 100)); var op = new OP("op1"); var current = new RealPropertyExport(op, "G1", "i"); op.ExportSimulationData += (sender, args) => Assert.AreEqual(300.0, current.Value, 1e-12); op.Run(ckt); current.Destroy(); }
public void When_CapacitorTransient_Expect_Reference() { var ckt = new Circuit( new VoltageSource("V1", "in", "0", new Sine(0, 1, 100)), new Capacitor("C1", "in", "0", 1e-6), new BehavioralCurrentSource("C2", "in", "0", "1u*ddt(V(in))")); var tran = new Transient("tran", 1e-6, 0.1); var reference = new RealPropertyExport(tran, "C1", "i"); var actual = new RealPropertyExport(tran, "C2", "i"); tran.ExportSimulationData += (sender, args) => { Assert.AreEqual(reference.Value, actual.Value, 1e-12); }; tran.Run(ckt); }
public void When_SimpleCapacitorTransient_Expect_Reference() { var ckt = new Circuit( new VoltageSource("V1", "in", "0", new Sine(0, 1, 1e3)), new Capacitor("C1", "in", "0", 1e-3), new BehavioralCapacitor("C2", "in", "0", "1m*x")); var tran = new Transient("tran", 1e-6, 1e-3); var refExport = new RealPropertyExport(tran, "C1", "i"); var actExport = new RealPropertyExport(tran, "C2", "i"); tran.ExportSimulationData += (sender, args) => { Assert.AreEqual(refExport.Value, actExport.Value, 1e-9); }; tran.Run(ckt); }
public void When_LCTankTransient_Expect_Reference() { /* * Test for LC tank circuit, an inductor parallel with a capacitor will resonate at a frequency of 1/(2*pi*sqrt(LC)) */ // Build circuit var capacitance = 1e-3; var inductance = 1e-6; var initialCurrent = 1e-3; var ckt = new Circuit( new Inductor("L1", "OUT", "0", inductance) .SetParameter("ic", initialCurrent), new Capacitor("C1", "OUT", "0", capacitance) ); /* * WARNING: An LC tank is a circuit that oscillates and does not converge. This causes the global truncation error * to increase as time goes by! * For this reason, the absolute tolerance is made a little bit less strict. */ AbsTol = 1e-9; // Create simulation var tran = new Transient("tran", 1e-9, 1e-3, 1e-7); tran.TimeParameters.InitialConditions["OUT"] = 0.0; tran.TimeParameters.UseIc = true; // Create exports var exports = new IExport <double> [1]; exports[0] = new RealPropertyExport(tran, "C1", "v"); // Create reference function var amplitude = Math.Sqrt(inductance / capacitance) * initialCurrent; var omega = 1.0 / Math.Sqrt(inductance * capacitance); Func <double, double>[] references = { t => - amplitude * Math.Sin(omega * t) }; // Run test AnalyzeTransient(tran, ckt, exports, references); DestroyExports(exports); }
public void When_ResistorOperatingPoint_Expect_Reference() { /* * A circuit contains a DC voltage source 10V and resistor 1000 Ohms * The test verifies that after OP simulation: * 1) a current through resistor is 0.01 A (Ohms law) */ var ckt = CreateResistorDcCircuit(10, 1000); // Create simulation, exports and references var op = new OP("op"); var exports = new Export <double> [1]; exports[0] = new RealPropertyExport(op, "R1", "i"); double[] references = { 0.01 }; // Run AnalyzeOp(op, ckt, exports, references); }
public void When_NMOSIVCharacteristic_Expect_NoException() { // <example_DC> // Make the bipolar junction transistor var nmos = new Mosfet1("M1") { Model = "example" }; nmos.Connect("d", "g", "0", "0"); var nmosmodel = new Mosfet1Model("example"); nmosmodel.SetParameter("kp", 150.0e-3); // Build the circuit var ckt = new Circuit( new VoltageSource("Vgs", "g", "0", 0), new VoltageSource("Vds", "d", "0", 0), nmosmodel, nmos ); // Sweep the base current and vce voltage var dc = new DC("DC 1", new[] { new SweepConfiguration("Vgs", 0, 3, 0.2), new SweepConfiguration("Vds", 0, 5, 0.1), }); // Export the collector current var currentExport = new RealPropertyExport(dc, "M1", "id"); // Run the simulation dc.ExportSimulationData += (sender, args) => { var vgsVoltage = dc.Sweeps[0].CurrentValue; var vdsVoltage = dc.Sweeps[1].CurrentValue; var current = currentExport.Value; }; dc.Run(ckt); // </example_DC> }
public void When_CdSmallSignal_Expect_NoException() { // Bug reported by William C. Donaldson (dsonbill) // Issue 115 var ckt = new Circuit { CreateDiode("D1", "0", "OUT", "1N914"), CreateDiodeModel("1N914", "Is=2.52e-9 Rs=0.568 N=1.752 Cjo=4e-12 M=0.4 tt=20e-9"), new VoltageSource("V1", "OUT", "0", 1.0) .SetParameter("acmag", 1.0) }; // Create simulation var ac = new AC("ac", new DecadeSweep(1e3, 10e6, 5)); // Get the property Cd var export = new RealPropertyExport(ac, "D1", "cd"); ac.Run(ckt); }
public void When_RunNonlinearResistor_Expect_NoException() { // <example_customcomponent_nonlinearresistor_test> // Build the circuit var ckt = new Circuit( new VoltageSource("V1", "out", "0", 0.0), new NonlinearResistor("RNL1", "out", "0") ); ckt.Entities["RNL1"].SetParameter("a", 2.0e3); ckt.Entities["RNL1"].SetParameter("b", 0.5); // Setup the simulation and export our current var dc = new DC("DC", "V1", -2.0, 2.0, 1e-2); var current = new RealPropertyExport(dc, "V1", "i"); dc.ExportSimulationData += (sender, args) => Console.WriteLine(-current.Value); dc.Run(ckt); // </example_customcomponent_nonlinearresistor_test> }
public void When_MOS1OPExample_Expect_Convergence() { // Found by Marcin Golebiowski var ckt = new Circuit( new VoltageSource("V1", "g", "0", 10), new VoltageSource("V2", "d", "0", 10), CreateMOS1("Md", "d", "g", "0", "0", "my-nmos", "is=1e-32") ); // Create the simulation var op = new OP("op"); var export = new RealPropertyExport(op, "my-nmos", "is"); op.ExportSimulationData += (sender, args) => { var current = export.Value; }; op.Run(ckt); }
public void When_ResistorModified_Expect_NoException() { // <example_Stochastic> // Build the circuit var ckt = new Circuit( new VoltageSource("V1", "in", "0", 1.0), new Resistor("R1", "in", "0", 1.0e3)); // Create the simulation var op = new OP("Op 1"); // Attach events to apply stochastic variation var rndGenerator = new Random(); var counter = 0; op.BeforeExecute += (sender, args) => { // Apply a random value of 1kOhm with 5% tolerance var value = 950 + 100 * rndGenerator.NextDouble(); var sim = (Simulation)sender; sim.EntityParameters["R1"].SetParameter("resistance", value); }; op.AfterExecute += (sender, args) => { // Run 10 times counter++; args.Repeat = counter < 10; }; // Make the exports var current = new RealPropertyExport(op, "R1", "i"); // Simulate op.ExportSimulationData += (sender, args) => { // This will run 1o times var result = current.Value; }; op.Run(ckt); // </example_Stochastic> }
public void When_SimpleResistor_Expect_Reference() { var ckt = new Circuit( new VoltageSource("V1", "in", "0", 0.0), new Resistor("R1", "in", "out", 1e3), new BehavioralResistor("R2", "out", "0", "1k")); var dc = new DC("DC", "V1", -5, 5, 0.5); // Check power var refPower = new RealPropertyExport(dc, "R1", "p"); var actPower = new RealPropertyExport(dc, "R2", "p"); dc.ExportSimulationData += (sender, args) => { var input = args.GetSweepValues()[0]; var output = args.GetVoltage("out"); Assert.AreEqual(input, output * 2, 1e-9); Assert.AreEqual(refPower.Value, actPower.Value, 1e-9); }; dc.Run(ckt); }
public void When_DiodeDC_Expect_NoException() { /* * Bug found by Marcin Golebiowski * Running simulations twice will give rise to errors. We are using a diode model here * in order to make sure we're use states, extra equations, etc. */ var ckt = new Circuit(); ckt.Add( CreateDiode("D1", "OUT", "0", "1N914"), CreateDiodeModel("1N914", "Is=2.52e-9 Rs=0.568 N=1.752 Cjo=4e-12 M=0.4 tt=20e-9"), new VoltageSource("V1", "OUT", "0", 0.0) ); // Create simulations var dc = new DC("DC 1", "V1", -1, 1, 10e-3); var op = new OP("OP 1"); // Create exports var dcExportV1 = new RealPropertyExport(dc, "V1", "i"); var dcExportV12 = new RealPropertyExport(dc, "V1", "i"); dc.ExportSimulationData += (sender, args) => { var v1 = dcExportV1.Value; var v12 = dcExportV12.Value; }; var opExportV1 = new RealPropertyExport(op, "V1", "i"); op.ExportSimulationData += (sender, args) => { var v1 = opExportV1.Value; }; // Run DC and op dc.Run(ckt); dc.Run(ckt); }
public void When_ExportSwitch_Expect_Reference() { var ckt = new Circuit( new VoltageSource("V1", "in", "0", new Sine(1, 1, 10)), new Resistor("R1", "in", "out", 1e3), new Resistor("R2", "out", "0", 1e3)); var sim1 = new DC("dc", "V1", 0, 2, 0.2); var sim2 = new Transient("tran", 1e-3, 0.5); var vexport = new RealVoltageExport(sim1, "out"); var iexport = new RealCurrentExport(sim1, "V1"); var pexport = new RealPropertyExport(sim1, "R1", "p"); sim1.ExportSimulationData += (sender, e) => { var input = e.GetVoltage("in"); Assert.AreEqual(input * 0.5, vexport.Value, 1e-9); Assert.AreEqual(-input / 2.0e3, iexport.Value, 1e-9); Assert.AreEqual(input * input / 4.0 / 1.0e3, pexport.Value, 1e-9); }; sim2.ExportSimulationData += (sender, e) => { var input = e.GetVoltage("in"); Assert.AreEqual(Math.Sin(2 * Math.PI * 10 * e.Time) + 1.0, input, 1e-9); Assert.AreEqual(input * 0.5, vexport.Value, 1e-9); Assert.AreEqual(-input / 2.0e3, iexport.Value, 1e-9); Assert.AreEqual(input * input / 4.0 / 1.0e3, pexport.Value, 1e-9); }; sim1.Run(ckt); // Switch exports vexport.Simulation = sim2; iexport.Simulation = sim2; pexport.Simulation = sim2; sim2.Run(ckt); }
public void When_NMOSIVCharacteristic_Expect_NoException() { // <example_DC> // Create the mosfet and its model var nmos = new Mosfet1("M1", "d", "g", "0", "0", "example"); var nmosmodel = new Mosfet1Model("example"); nmosmodel.SetParameter("kp", 150.0e-3); // Build the circuit var ckt = new Circuit( new VoltageSource("Vgs", "g", "0", 0), new VoltageSource("Vds", "d", "0", 0), nmosmodel, nmos ); // Sweep the base current and vce voltage var dc = new DC("DC 1", new[] { new ParameterSweep("Vgs", new LinearSweep(0, 3, 0.2)), new ParameterSweep("Vds", new LinearSweep(0, 5, 0.1)), }); // Export the collector current var currentExport = new RealPropertyExport(dc, "M1", "id"); // Run the simulation dc.ExportSimulationData += (sender, args) => { var vgsVoltage = dc.GetCurrentSweepValue()[0]; var vdsVoltage = dc.GetCurrentSweepValue()[1]; var current = currentExport.Value; }; dc.Run(ckt); // </example_DC> }
public static void DC() { // Build the circuit var ckt = new Circuit( new VoltageSource("V1", "in", "0", 5.0), new Resistor("R1", "in", "out", 1.0e3), new Resistor("R2", "out", "0", 2.0e3) ); // Create an Operating Point simulation var dc = new OP("my op"); Export <double> currentExport = new RealPropertyExport(dc, "R1", "i"); dc.ExportSimulationData += (sender, exportDataEventArgs) => { double voltage = exportDataEventArgs.GetVoltage("out"); double current = currentExport.Value; Console.WriteLine($"Out: {voltage} V, Current: {current} A"); }; // Run the simulation dc.Run(ckt); // Per Sven: The simulation searches for the Operating Point of the circuit, // which is the DC solution to the circuit without sweeping any parameters. }