/// <summary>Adds device to the circuit and connects it to the specified nodes.</summary> /// <param name="nodeConnections">Ids of the nodes to which the device terminals should connect.</param> /// <param name="device">The device to be added.</param> /// <returns></returns> public CircuitBuilder AddDevice(int[] nodeConnections, ICircuitDefinitionDevice device) { if (device.Tag != null && namedDevices.ContainsKey(device.Tag)) { throw new InvalidOperationException($"Circuit already contains device with name '{device.Tag}'"); } if (device.ConnectedNodes.Count != nodeConnections.Length) { throw new ArgumentException("Wrong number of connections."); } if (devices.Contains(device)) { throw new InvalidOperationException("Cannot insert same device twice more than once."); } // connect to nodes for (var i = 0; i < nodeConnections.Length; i++) { var id = nodeConnections[i]; EnsureHasNode(id); device.ConnectedNodes[i] = id; } devices.Add(device); if (device.Tag != null) { namedDevices[device.Tag] = device; } // invalidate cached validation result validatedCircuit = false; circuitException = null; return(this); }
/// <summary> /// Verifies that current subcircuit with given nodes as terminals represents valid SPICE circuit. That is: there /// are no floating nodes and there is a DC path between any two nodes not going through ground. /// </summary> public bool ValidateCircuit() { if (!validatedCircuit) { circuitException = ValidateCircuit_Internal(); } return(circuitException == null); }
/// <summary> /// Verifies correctness of the circuit topology, creates new instance of subcircuit representation and returns /// it. /// </summary> /// <returns></returns> public SubcircuitDefinition BuildSubcircuit(int[] terminals, object tag = null) { circuitException = ValidateSubcircuit_Internal(terminals); if (circuitException != null) { throw circuitException; } // subtract ground node from total node count return(new SubcircuitDefinition(NodeCount - 1, terminals, devices.ToArray(), tag)); }