예제 #1
0
        public void TestThrowsWhenNodeIsNotConnectedToGround()
        {
            builder.AddResistor(1, 0, 1);
            builder.AddResistor(1, 2, 2);
            builder.AddResistor(0, 2, 3);
            builder.AddCurrentSource(1, 0, 5);

            // add device 'far away'
            builder.AddResistor(3, 4, 3);

            //            builder.BuildCircuit();
            Assert.Throws <NoDcPathToGroundException>(() => builder.BuildCircuit());
        }
예제 #2
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);
        }
예제 #3
0
        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 SimulationData Simulate(List <InitialSwitchState> initialSwitchStates, List <InitialGeneratorState> initialGeneratorStates)
        {
            var simulation = new Simulation(totalSteps);
            var builder    = new CircuitBuilder(circuitData, simulation);
            var circuit    = builder.BuildCircuit();

            circuit.SetSwiches(initialSwitchStates);
            circuit.SetGenerators(initialGeneratorStates);
            var results = RunSimulation(simulation);

            return(results);
        }
예제 #5
0
        public void LoadFile(string file)
        {
            if (!string.IsNullOrEmpty(file))
            {
                _circuitBuilder = new CircuitBuilder();

                FileStream        fileStream = new FileStream(file, FileMode.Open);
                List <ParserData> parserData = _parserFactory.ReturnParser(file).Parse(file, fileStream);
                _modelController.SetCircuit(_circuitBuilder?.BuildCircuit(parserData, ShowErrorPopup));

                _viewController.DrawFrame(_modelController.GetNodes());
                Program.log.Invoke("> Loaded circuit");
            }
        }
예제 #6
0
        public static void Simple()
        {
            // requires NextGenSpice.Core.Circuit and
            // NextGenSpice.Core.Devices namespace
            var builder = new CircuitBuilder();

            builder
            .AddDevice(new[] { 1, 0 }, new VoltageSource(12, "VS"))
            .AddDevice(new[] { 0, 2 }, new Resistor(10))
            .AddDevice(new[] { 1, 2 }, new Resistor(10))
            .AddDevice(new[] { 2, 3 }, new Resistor(5))
            .AddDevice(new[] { 1, 3 }, new Resistor(5));
            var circuit = builder.BuildCircuit();

            //                        var builder = new CircuitBuilder();
            //                        builder
            //                            .AddVoltageSource(1, 0, 12)
            //                            .AddResistor(0, 2, 10)
            //                            .AddResistor(1, 2, 10)
            //                            .AddResistor(2, 3, 5)
            //                            .AddResistor(1, 3, 5);
            //                        var circuit = builder.BuildCircuit();

            var model = circuit.GetLargeSignalModel();
            // equivalent to
            var m = AnalysisModelCreator
                    .Instance.Create <LargeSignalCircuitModel>(circuit);


            //            builder
            //                .AddVoltageSource(1, 0, 12, "VS")
            ////              .AddDevice(new[] { 1, 0 }, new VoltageSourceDevice(12, "VS"))
            ////                ...

            model.EstablishDcBias();
            Console.WriteLine(model.NodeVoltages[1]);             // 12 V
            Console.WriteLine(model.NodeVoltages[2]);             //  8 V
            Console.WriteLine(model.NodeVoltages[3]);             // 10 V

            // equivalent to
            // var vsource = (ITwoTerminalLargeSignalDevice) model.Devices.Single(
            //    dev => dev.DefinitionDevice.Tag.Equals("VS"));
            var vsouce = (ITwoTerminalLargeSignalDevice)model.FindDevice("VS");

            Console.WriteLine(vsouce.Current);             // -0.8 V
        }
        static void Main(string[] args)
        {
            var nodeFactory = NodeFactory.GetInstance;

            nodeFactory
            .RegisterStrategy <InputStrategy>("input_high", "input_low")
            .RegisterStrategy <ProbeStrategy>("probe")
            .RegisterStrategy <NotStrategy>("not")
            .RegisterStrategy <AndStrategy>("and")
            .RegisterStrategy <OrStrategy>("or")
            .RegisterStrategy <NorStrategy>("nor")
            .RegisterStrategy <NandStrategy>("nand")
            .RegisterStrategy <XorStrategy>("xor");

            var(nodes, edges) = FileParser.ParseFile(FileReader.ReadFile("Circuit3_Encoder"));

            var circuitBuilder = new CircuitBuilder();

            circuitBuilder.BuildNodes(nodes);
            circuitBuilder.BuildEdges(edges);

            var circuit = circuitBuilder.BuildCircuit();


            var watch = new Stopwatch();

            watch.Start();
            circuit.Simulate();
            watch.Stop();

            Console.Out.WriteLine($"Propagation delay: {circuit.ExecutionTime()} nanoseconds");

            foreach (var test in circuit.OutputNodes)
            {
                Console.Out.WriteLine($"{test.Name}.Output = {test.Output}");
            }
        }
예제 #8
0
        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
        }