|
| 1 | +""" |
| 2 | +In electrical engineering, the Y-Δ transform, also written wye-delta and also known by |
| 3 | +many other names, is a mathematical technique to simplify the analysis of an electrical |
| 4 | +network. |
| 5 | +The name derives from the shapes of the circuit diagrams, which look respectively like |
| 6 | +the letter Y and the Greek capital letter Δ. This circuit transformation theory was |
| 7 | +published by Arthur Edwin Kennelly in 1899. It is widely used in analysis of three-phase |
| 8 | +electric power circuits. |
| 9 | +
|
| 10 | +The Y-Δ transform can be considered a special case of the star-mesh transform for three |
| 11 | +resistors. In mathematics, the Y-Δ transform plays an important role in theory of |
| 12 | +circular planar graphs. |
| 13 | +
|
| 14 | +Source: https://en.wikipedia.org/wiki/Y-%CE%94_transform |
| 15 | +""" |
| 16 | + |
| 17 | +from sys import exit |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | + |
| 21 | +def delta_to_wye(r: list) -> dict: |
| 22 | + """ |
| 23 | + >>> from_delta_to_wye([2.0, 3.0, 4.0]) |
| 24 | + {'r1': 1.3333333333333333, 'r2': 0.8888888888888888, 'r3': 0.6666666666666666} |
| 25 | + """ |
| 26 | + r_wye: dict = {} |
| 27 | + ra, rb, rc = r[0], r[1], r[2] |
| 28 | + r_wye.update({"r1": rb * rc / (ra + rb + rc)}) |
| 29 | + r_wye.update({"r2": ra * rc / (ra + rb + rc)}) |
| 30 | + r_wye.update({"r3": ra * rb / (ra + rb + rc)}) |
| 31 | + return r_wye |
| 32 | + |
| 33 | + |
| 34 | +def wye_to_delta(r: list) -> dict: |
| 35 | + """ |
| 36 | + >>> from_wye_to_delta([2.0, 3.0, 4.0]) |
| 37 | + {'ra': 13.0, 'rb': 8.666666666666666, 'rc': 6.5} |
| 38 | + """ |
| 39 | + r1, r2, r3 = r[0], r[1], r[2] |
| 40 | + r_delta: dict = {} |
| 41 | + r_delta.update({"ra": (r1 * r2 + r2 * r3 + r3 * r1) / r1}) |
| 42 | + r_delta.update({"rb": (r1 * r2 + r2 * r3 + r3 * r1) / r2}) |
| 43 | + r_delta.update({"rc": (r1 * r2 + r2 * r3 + r3 * r1) / r3}) |
| 44 | + return r_delta |
| 45 | + |
| 46 | + |
| 47 | +def transform(mode: int, r: list) -> dict: |
| 48 | + """ |
| 49 | + >>> transform(1, [4.0, 5.0, 6.0]) |
| 50 | + {'r1': 2.0, 'r2': 1.6, 'r3': 1.3333333333333333} |
| 51 | +
|
| 52 | + >>> transform(2, [4.0, 5.0, 6.0]) |
| 53 | + {'ra': 18.5, 'rb': 14.8, 'rc': 12.333333333333334} |
| 54 | + """ |
| 55 | + r_transformed = {} |
| 56 | + if mode == 1: |
| 57 | + r_transformed = delta_to_wye(r) |
| 58 | + elif mode == 2: |
| 59 | + r_transformed = wye_to_delta(r) |
| 60 | + return r_transformed |
| 61 | + |
| 62 | + |
| 63 | +def get_type_transform(): |
| 64 | + mode: int = 0 |
| 65 | + try: |
| 66 | + print(""" |
| 67 | + 1. From delta to wye |
| 68 | + 2. From wye to delta |
| 69 | + """) |
| 70 | + mode = int(input("? --> ")) |
| 71 | + except ValueError: |
| 72 | + print("Invalid Value. Only int inputs are accepted") |
| 73 | + exit() |
| 74 | + return mode |
| 75 | + |
| 76 | + |
| 77 | +def get_resistors_values(mode: int) -> list: |
| 78 | + r: list = [] |
| 79 | + print("Select conversion (type 1 or 2)") |
| 80 | + try: |
| 81 | + if mode == 1: |
| 82 | + r = list( |
| 83 | + map( |
| 84 | + float, input("Resistant values (format ra rb rc): ").strip().split() |
| 85 | + ) |
| 86 | + )[:3] |
| 87 | + elif mode == 2: |
| 88 | + r = list( |
| 89 | + map( |
| 90 | + float, input("Resistant values (format r1 r2 r3): ").strip().split() |
| 91 | + ) |
| 92 | + )[:3] |
| 93 | + else: |
| 94 | + print("Incorrect selected option. Valid option 1 or 2") |
| 95 | + except ValueError: |
| 96 | + print("Invalid Value. Only int inputs are accepted") |
| 97 | + exit() |
| 98 | + return r |
| 99 | + |
| 100 | + |
| 101 | +def test_get_type_transformation() -> None: |
| 102 | + with mock.patch("builtins.input", return_value="1"): |
| 103 | + m = get_type_transform() |
| 104 | + assert m == 1 |
| 105 | + |
| 106 | + |
| 107 | +def test_get_resistors_values() -> None: |
| 108 | + with mock.patch("builtins.input", return_value="2 4 8"): |
| 109 | + r = get_resistors_values(2) |
| 110 | + assert r == [2.0, 4.0, 8.0] |
| 111 | + |
| 112 | + |
| 113 | +def main() -> None: |
| 114 | + print("star - delta transform") |
| 115 | + mode = get_type_transform() |
| 116 | + r = get_resistors_values(mode) |
| 117 | + r_transformed = transform(mode, r) |
| 118 | + print(f"Result: '{r_transformed}'") |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + from doctest import testmod |
| 123 | + |
| 124 | + testmod() |
| 125 | + main() |
0 commit comments