public void When_LowpassRLSmallSignal_Expect_Reference() { /* * Lowpass RL filter in the frequency domain should have a single pole at s=-2pi*R/L */ // Create circuit double resistance = 1; var inductance = 1e-3; var ckt = new Circuit( new VoltageSource("V1", "IN", "0", 0.0) .SetParameter("acmag", 1.0), new Inductor("L1", "IN", "OUT", inductance), new Resistor("R1", "OUT", "0", resistance)); // Create simulation var ac = new AC("ac", new DecadeSweep(0.1, 1e6, 10)); // Create exports var exports = new IExport <Complex> [1]; exports[0] = new ComplexVoltageExport(ac, "OUT"); // Create references Func <double, Complex>[] references = { f => 1.0 / new Complex(1.0, inductance / resistance * 2 * Math.PI * f) }; // Run test AnalyzeAC(ac, ckt, exports, references); DestroyExports(exports); }
public void When_RCFilterAC_Expect_NoException() { // <example_AC> // Build the circuit var ckt = new Circuit( new VoltageSource("V1", "in", "0", 0.0), new Resistor("R1", "in", "out", 10.0e3), new Capacitor("C1", "out", "0", 1e-6) ); ckt["V1"].SetParameter("acmag", 1.0); // Create the simulation var ac = new AC("AC 1", new DecadeSweep(1e-2, 1.0e3, 5)); // Make the export var exportVoltage = new ComplexVoltageExport(ac, "out"); // Simulate ac.ExportSimulationData += (sender, args) => { var output = exportVoltage.Value; var decibels = 10.0 * Math.Log10(output.Real * output.Real + output.Imaginary * output.Imaginary); }; ac.Run(ckt); // </example_AC> }
public void When_ACRerun_Expect_Same() { // Create the circuit var ckt = new Circuit( new VoltageSource("V1", "in", "0", 10.0).SetParameter("acmag", 1.0), new Resistor("R1", "in", "out", 10), new Capacitor("C1", "out", "0", 20) ); // Create the transient analysis var ac = new AC("ac 1", new DecadeSweep(1, 1e9, 10)); var export = new ComplexVoltageExport(ac, "out"); // Run the simulation a first time for building the reference values var r = new List <Complex>(); void BuildReference(object sender, ExportDataEventArgs args) => r.Add(export.Value); ac.ExportSimulationData += BuildReference; ac.Run(ckt); ac.ExportSimulationData -= BuildReference; // Rerun the simulation for building the reference values var index = 0; void CheckReference(object sender, ExportDataEventArgs args) { Assert.AreEqual(r[index].Real, export.Value.Real, 1e-20); Assert.AreEqual(r[index++].Imaginary, export.Value.Imaginary, 1e-20); } ac.ExportSimulationData += CheckReference; ac.Rerun(); ac.ExportSimulationData -= CheckReference; }
/// <summary> /// Initializes a new instance of the <see cref="VoltageMagnitudeExport"/> class. /// </summary> /// <param name="name">Name of export.</param> /// <param name="simulation">Simulation</param> /// <param name="node">Positive node</param> /// <param name="reference">Negative reference node</param> public VoltageMagnitudeExport(string name, Simulation simulation, string node, string reference = null) : base(simulation) { Name = name ?? throw new System.ArgumentNullException(nameof(name)); Node = node ?? throw new System.ArgumentNullException(nameof(node)); Reference = reference; ExportImpl = new ComplexVoltageExport((FrequencySimulation)simulation, node, reference); }
/// <summary> /// Initializes a new instance of the <see cref="VoltageExport"/> class. /// </summary> /// <param name="name">Name of export.</param> /// <param name="simulation">Simulation.</param> /// <param name="node">Positive node.</param> /// <param name="reference">Negative reference node.</param> public VoltageExport(string name, Simulation simulation, string node, string reference = null) : base(simulation) { Name = name ?? throw new System.ArgumentNullException(nameof(name)); Node = node ?? throw new System.ArgumentNullException(nameof(node)); Reference = reference; if (simulation is FrequencySimulation fs) { ExportImpl = new ComplexVoltageExport(fs, node, reference); } else { ExportRealImpl = new RealVoltageExport((IBiasingSimulation)simulation, node, reference); } }
public void When_MutualInductanceSmallSignal_Expect_Reference() { // Create circuit var r1 = 100.0; var r2 = 500.0; var l1 = 10e-3; var l2 = 2e-3; var k = 0.693; var ckt = new Circuit( new VoltageSource("V1", "IN", "0", 0.0) .SetParameter("acmag", 1.0), new Resistor("R1", "IN", "1", r1), new Inductor("L1", "1", "0", l1), new Inductor("L2", "OUT", "0", l2), new Resistor("R2", "OUT", "0", r2), new MutualInductance("M1", "L1", "L2", k) ); // Create simulation var ac = new AC("ac", new DecadeSweep(1, 1e8, 10)); // Create exports var exports = new IExport <Complex> [1]; exports[0] = new ComplexVoltageExport(ac, "OUT"); // Create references var mut = k * Math.Sqrt(l1 * l2); var a = l1 * l2 - mut * mut; var b = r1 * l2 + r2 * l1; var c = r1 * r2; var num = mut * r2; Func <double, Complex>[] references = { f => { var s = new Complex(0.0, 2.0 * Math.PI * f); var denom = (a * s + b) * s + c; return(num * s / denom); } }; // Run simulation AnalyzeAC(ac, ckt, exports, references); DestroyExports(exports); }
public void When_MutualInductanceSmallSignal_Expect_Reference() { // Create circuit double r1 = 100.0; double r2 = 500.0; double l1 = 10e-3; double l2 = 2e-3; double k = 0.693; Circuit ckt = new Circuit(); ckt.Objects.Add( new VoltageSource("V1", "IN", "0", 0.0), new Resistor("R1", "IN", "1", r1), new Inductor("L1", "1", "0", l1), new Inductor("L2", "OUT", "0", l2), new Resistor("R2", "OUT", "0", r2), new MutualInductance("M1", "L1", "L2", k) ); ckt.Objects["V1"].SetParameter("acmag", 1.0); // Create simulation AC ac = new AC("ac", new DecadeSweep(1, 1e8, 10)); // Create exports Export <Complex>[] exports = new Export <Complex> [1]; exports[0] = new ComplexVoltageExport(ac, "OUT"); // Create references double mut = k * Math.Sqrt(l1 * l2); double a = l1 * l2 - mut * mut; double b = r1 * l2 + r2 * l1; double c = r1 * r2; double num = mut * r2; Func <double, Complex>[] references = { f => { Complex s = new Complex(0.0, 2.0 * Math.PI * f); Complex denom = (a * s + b) * s + c; return(num * s / denom); } }; // Run simulation AnalyzeAC(ac, ckt, exports, references); }
public void When_ACRC_Expect_Reference() { var ckt = new Circuit( new VoltageSource("V1", "in", "0", 0.0).SetParameter("acmag", 1.0), new LaplaceVoltageControlledVoltageSource("E1", "act", "0", "in", "0", new[] { 1.0 }, new[] { 1.0, 1e-3 })); var ac = new AC("ac", new DecadeSweep(1.0, 1e6, 3)); var a = new ComplexVoltageExport(ac, "act"); ac.ExportSimulationData += (sender, args) => { var s = args.Laplace; Complex r = 1.0 / (1.0 + 1e-3 * s); Assert.AreEqual(r.Real, a.Value.Real, 1e-20); Assert.AreEqual(r.Imaginary, a.Value.Imaginary, 1e-20); }; ac.Run(ckt); }