public void ThrowsWhenCurrentSourceCutset() { // a cutset builder.AddCurrentSource(2, 3, 1, "I2"); builder.AddCurrentSource(0, 1, 1, "I1"); // some other devices builder.AddVoltageSource(1, 2, 1, "V4"); builder.AddCurrentSource(1, 4, 1, "I3"); // cannot be in cutset builder.AddResistor(4, 2, 1, "R2"); builder.AddResistor(0, 3, 1, "R"); var devices = Assert.Throws <CurrentBranchCutsetException>(() => builder.BuildCircuit()).Devices; Assert.Equal(new[] { "I1", "I2" }, devices.Select(e => e.Tag).OrderBy(s => s)); }
public static void ResistorSweep() { var builder = new CircuitBuilder(); builder .AddVoltageSource(1, 0, 12, "VS") .AddResistor(0, 2, 10) .AddResistor(1, 2, 10) .AddResistor(2, 3, 5) .AddResistor(1, 3, 5, "R1"); var circuit = builder.BuildCircuit(); var model = circuit.GetLargeSignalModel(); var vsouce = (ITwoTerminalLargeSignalDevice)model.FindDevice("VS"); var res = (Resistor)circuit.FindDevice("R1"); // sweep for values from 1 Ohm to 15 Ohm for (var i = 1; i <= 15; i++) { res.Resistance = i + 1; // set resistance // calculate model.EstablishDcBias(); var v1 = model.NodeVoltages[1]; var v2 = model.NodeVoltages[2]; var v3 = model.NodeVoltages[3]; var iV = vsouce.Current; // print values Console.WriteLine($"{i + 1}Ohm: {v1}V {v2}V {v3}V {iV}A"); } }
public static void SimpleSubcircuit() { var builder = new CircuitBuilder(); var batteryDefinition = builder .AddVoltageSource(1, 2, 9) .AddResistor(2, 3, 1.5) .BuildSubcircuit(new[] { 1, 3 }); builder.Clear(); builder .AddDevice(new[] { 0, 1 }, new Subcircuit(batteryDefinition)) .AddSubcircuit(new[] { 0, 1 }, batteryDefinition); }
public static void LiveSimExample() { var builder = new CircuitBuilder(); builder .AddVoltageSource(1, 0, new SinusoidalBehavior { Amplitude = 5, Frequency = 1e3 }) .AddCapacitor(1, 2, 0.125, null, "C1") .AddResistor(2, 0, 1); var m1 = builder.BuildCircuit().GetLargeSignalModel(); m1.EstablishDcBias(); Console.WriteLine("Time V_1 V_2"); var timestep = 10e-6; while (m1.CurrentTimePoint <= 1e-3) { var time = m1.CurrentTimePoint; Console.WriteLine($"{time} {m1.NodeVoltages[1]} {m1.NodeVoltages[2]}"); m1.AdvanceInTime(timestep); } builder.AddCapacitor(2, 0, 0.125, null, "C2"); var m2 = builder.BuildCircuit().GetLargeSignalModel(); var c1 = (LargeSignalCapacitor)m1.FindDevice("C1"); var intprop = typeof(LargeSignalCapacitor).GetProperty("IntegrationMethod", BindingFlags.Instance | BindingFlags.NonPublic); intprop.SetValue(m2.FindDevice("C1"), intprop.GetValue(c1)); var ctxprop = typeof(LargeSignalCircuitModel).GetField("context", BindingFlags.Instance | BindingFlags.NonPublic); var ctx = (ISimulationContext)ctxprop.GetValue(m1); // too complex not worth it var tpprop = ctx.GetType().GetProperty("TimePoint", BindingFlags.NonPublic | BindingFlags.Instance); tpprop.SetValue(ctxprop.GetValue(m2), ctx.TimePoint); }
private static void Simple2() { var builder = new CircuitBuilder(); builder .AddVoltageSource(1, 0, 12, "VS") .AddResistor(0, 2, 10) .AddResistor(1, 2, 10) .AddResistor(2, 3, 5) .AddResistor(1, 3, 5); var circuit = builder.BuildCircuit(); var model = circuit.GetLargeSignalModel(); model.EstablishDcBias(); Console.WriteLine(model.NodeVoltages[1]); // 12 V Console.WriteLine(model.NodeVoltages[2]); // 8 V Console.WriteLine(model.NodeVoltages[3]); // 10 V var vsouce = (ITwoTerminalLargeSignalDevice)model.FindDevice("VS"); Console.WriteLine(vsouce.Current); // -0.8 V }