Esempio n. 1
0
        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);
        }