public void TestDifferentID() { Circuit circuit2 = new Circuit(); Node con20 = new Node(); // GND Node con21 = new Node(); Node dummyNode = new Node(); Node dummyConnection2 = new Node(); Node con22 = new Node(); Component battery2 = new Component(con20, con21, 0, 12); Component resistor21 = new Component(con20, con22, 2, 0); Component dummyComponent = new Component(dummyNode, dummyConnection2, 0, 0); Component dummyComponent2 = new Component(dummyNode, dummyConnection2, 0, 0); Component resistor22 = new Component(con21, con22, 2, 0); Component resistor23 = new Component(con21, con22, 2, 0); circuit2.AddComponent(battery2); circuit2.AddComponent(resistor21); circuit2.AddComponent(resistor22); circuit2.AddComponent(resistor23); CircuitCalculator circuitCalculator2 = new CircuitCalculator(); circuitCalculator2.Calculate(circuit2); Assert.IsTrue(Math.Abs(con20.Volts - 0) < 0.0001); Assert.IsTrue(Math.Abs(con21.Volts - 12) < 0.0001); Assert.IsTrue(Math.Abs(con22.Volts - 8) < 0.0001); Assert.IsTrue(Math.Abs(battery2.Amps - 4) < 0.0001); Assert.IsTrue(Math.Abs(resistor21.Amps - 4) < 0.0001); Assert.IsTrue(Math.Abs(resistor22.Amps - 2) < 0.0001); Assert.IsTrue(Math.Abs(resistor23.Amps - 2) < 0.0001); }
public void CircuitModifyShouldHaveCorrectNumberOfComponentsAndConnections() { Circuit circuit = new Circuit(); Node con0 = new Node(); // GND Node con1 = new Node(); Node con2 = new Node(); Component battery = new Component(con0, con1, 0, 24); Component resistor1 = new Component(con1, con2, 2, 0); Component resistor2 = new Component(con2, con0, 4, 0); circuit.AddComponent(battery); circuit.AddComponent(resistor1); circuit.AddComponent(resistor2); Assert.IsTrue(circuit.ListComponents.Count == 3); Assert.IsTrue(circuit.ListConnections.Count == 3); circuit.ModifyComponent(resistor1, con2, con0); circuit.ModifyComponent(resistor1, con0, con1); Assert.IsTrue(circuit.ListComponents.Count == 3); Assert.IsTrue(circuit.ListConnections.Count == 3); }
// Are these functions necessary???????? public void ModifyComponent(Component modifyComponent, Node oldNode, Node newNode) { if(oldNode.Id == modifyComponent.Nodes[0].Id) { // [0] is being replaced by the new one modifyComponent.Nodes[0].Components.Remove(modifyComponent); newNode.Components.Add(modifyComponent); modifyComponent.Nodes[0] = newNode; } else if(oldNode.Id == modifyComponent.Nodes[1].Id) { // [1] is being replaced by the new one modifyComponent.Nodes[1].Components.Remove(modifyComponent); newNode.Components.Add(modifyComponent); modifyComponent.Nodes[1] = newNode; } }
public Component(Node left, Node right, double resistance, double voltageSource) { if(left.Id == right.Id) { throw new Exception("New Component cannot have the same two connections"); } Nodes = new Node[2]; Nodes[0] = left; // If VoltageSource != 0, then this is the negative terminal Nodes[1] = right; Resistance = resistance; VoltageSource = voltageSource; Id = IdCounter; IdCounter++; }
public void TestForNoVoltageSourceConnections() { Circuit circuit = new Circuit(); Node con0 = new Node(); // GND Node con1 = new Node(); Component resistor1 = new Component(con0, con1, 2, 0); Component resistor2 = new Component(con1, con0, 2, 0); Component resistor3 = new Component(con1, con0, 2, 0); circuit.AddComponent(resistor1); circuit.AddComponent(resistor2); circuit.AddComponent(resistor3); CircuitCalculator circuitCalculator = new CircuitCalculator(); circuitCalculator.Calculate(circuit); }
public ConnectorViewModel(int id, IDiagramViewModel parent, FullyCreatedConnectorInfo sourceConnectorInfo, FullyCreatedConnectorInfo sinkConnectorInfo, Node left, Node right) : base(id,parent) { Init(sourceConnectorInfo, sinkConnectorInfo); Component = new CircuitSimulator.Component(left, right, DiagramViewModel.ZERO_RESISTANCE, 0); }
public void TestCorrectCircuirs() { Circuit circuit1 = new Circuit(); Node con10 = new Node(); // GND Node con11 = new Node(); Node con12 = new Node(); Node con13 = new Node(); Node con14 = new Node(); Component battery1 = new Component(con10, con11, 0, 24); Component resistor11 = new Component(con10, con12, 2, 0); Component resistor12 = new Component(con10, con12, 4, 0); Component resistor13 = new Component(con11, con13, 3, 0); Component resistor14 = new Component(con11, con14, 5, 0); Component resistor15 = new Component(con13, con14, 1, 0); Component resistor16 = new Component(con12, con13, 2, 0); circuit1.AddComponent(battery1); circuit1.AddComponent(resistor11); circuit1.AddComponent(resistor12); circuit1.AddComponent(resistor13); circuit1.AddComponent(resistor14); circuit1.AddComponent(resistor15); circuit1.AddComponent(resistor16); CircuitCalculator circuitCalculator1 = new CircuitCalculator(); circuitCalculator1.Calculate(circuit1); Assert.IsTrue(Math.Abs(con10.Volts - 0) < 0.0001); Assert.IsTrue(Math.Abs(con11.Volts - 24) < 0.0001); Assert.IsTrue(Math.Abs(con12.Volts - 6) < 0.0001); Assert.IsTrue(Math.Abs(con13.Volts - 15) < 0.0001); Assert.IsTrue(Math.Abs(con14.Volts - 16.5) < 0.0001); Assert.IsTrue(Math.Abs(battery1.Amps - 4.5) < 0.0001); Assert.IsTrue(Math.Abs(resistor11.Amps - 3) < 0.0001); Assert.IsTrue(Math.Abs(resistor12.Amps - 1.5) < 0.0001); Assert.IsTrue(Math.Abs(resistor13.Amps - 3) < 0.0001); Assert.IsTrue(Math.Abs(resistor14.Amps - 1.5) < 0.0001); Assert.IsTrue(Math.Abs(resistor15.Amps - 1.5) < 0.0001); Assert.IsTrue(Math.Abs(resistor16.Amps - 4.5) < 0.0001); Circuit circuit2 = new Circuit(); Node con20 = new Node(); // GND Node con21 = new Node(); Node con22 = new Node(); Component battery2 = new Component(con20, con21, 0, 12); Component resistor21 = new Component(con20, con22, 2, 0); Component resistor22 = new Component(con21, con22, 2, 0); Component resistor23 = new Component(con21, con22, 2, 0); circuit2.AddComponent(battery2); circuit2.AddComponent(resistor21); circuit2.AddComponent(resistor22); circuit2.AddComponent(resistor23); CircuitCalculator circuitCalculator2 = new CircuitCalculator(); circuitCalculator2.Calculate(circuit2); Assert.IsTrue(Math.Abs(con20.Volts - 0) < 0.0001); Assert.IsTrue(Math.Abs(con21.Volts - 12) < 0.0001); Assert.IsTrue(Math.Abs(con22.Volts - 8) < 0.0001); Assert.IsTrue(Math.Abs(battery2.Amps - 4) < 0.0001); Assert.IsTrue(Math.Abs(resistor21.Amps - 4) < 0.0001); Assert.IsTrue(Math.Abs(resistor22.Amps - 2) < 0.0001); Assert.IsTrue(Math.Abs(resistor23.Amps - 2) < 0.0001); }
public void MakeConnection(Node A, Node B) { // merges node B into A foreach (var component in B.Components) { ModifyComponent(component, B, A); } }