Example #1
0
def equivalent_resistance(netlist, a, b, sparse=False):
    """Calculate equivalent resistance as seen through nodes a and b.

    Works by adding a 1 ampere current generator between a and b, then
    solving the circuit. The resistance returned then is
        R = [e(a) - e(b)] / 1

    Raises
        * ValueError: the original netlist has a non-resistor component
        * KeyError: either node a or b not found in netlist
    """

    if not check_resistive(netlist):
        raise ValueError("Network is not resistive")

    for node in (a, b):
        if node not in netlist.nodenum and node != netlist.ground:
            raise KeyError(f"Node `{node}` not found in netlist")

    resistive_network = deepcopy(netlist)
    resistive_network.process_component(["a1", "A", "1", a, b])
    circuit = n.Circuit(resistive_network, sparse=sparse)
    solution = circuit.solve()

    e = [0, 0]
    for i, node in enumerate((a, b)):
        if node != "g":
            j = solution.nodenum[node]
            e[i] = solution.result[j]

    return e[0] - e[1]
Example #2
0
 def assert_print(self, path, expected, mock_stdout):
     netlist = n.Netlist(path)
     circuit = n.Circuit(netlist)
     solution = circuit.solve()
     print(solution)
     output = mock_stdout.getvalue()
     self.assertEqual(output, expected)
Example #3
0
def main():
    args = parser.parse_args()

    try:
        netlist = n.Netlist(args.netlist_path)
    except FileNotFoundError:
        exit(1)

    circuit = n.Circuit(netlist, sparse=args.sparse)

    try:
        solution = circuit.solve()
    except n.UnconnectedCircuitError:
        exit(1)

    print(solution)
Example #4
0
def main():
    if len(sys.argv) >= 2:
        netlist_path = sys.argv[1]
    else:
        print("Missing argument: netlist file")
        exit(1)
    if len(sys.argv) == 3:
        sparse = sys.argv[2] == "sparse"
    else:
        sparse = False

    try:
        netlist = n.Netlist(netlist_path)
    except FileNotFoundError:
        print("File not found: {}".format(netlist_path))
        exit(1)
    circuit = n.Circuit(netlist, sparse=sparse)
    solution = circuit.solve()
    print(solution)