From 9fb47fa962675c22cc56e0edaa04b8994214bc15 Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Thu, 7 Nov 2024 11:18:35 +0100 Subject: [PATCH 1/6] new file: star_delta_transform.py --- electronics/star_delta_transform.py | 125 ++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 electronics/star_delta_transform.py diff --git a/electronics/star_delta_transform.py b/electronics/star_delta_transform.py new file mode 100644 index 000000000..62dc925fc --- /dev/null +++ b/electronics/star_delta_transform.py @@ -0,0 +1,125 @@ +""" +In electrical engineering, the Y-Δ transform, also written wye-delta and also known by +many other names, is a mathematical technique to simplify the analysis of an electrical +network. +The name derives from the shapes of the circuit diagrams, which look respectively like +the letter Y and the Greek capital letter Δ. This circuit transformation theory was +published by Arthur Edwin Kennelly in 1899. It is widely used in analysis of three-phase +electric power circuits. + +The Y-Δ transform can be considered a special case of the star-mesh transform for three +resistors. In mathematics, the Y-Δ transform plays an important role in theory of +circular planar graphs. + +Source: https://en.wikipedia.org/wiki/Y-%CE%94_transform +""" + +from sys import exit +from unittest import mock + + +def delta_to_wye(r: list) -> dict: + """ + >>> from_delta_to_wye([2.0, 3.0, 4.0]) + {'r1': 1.3333333333333333, 'r2': 0.8888888888888888, 'r3': 0.6666666666666666} + """ + r_wye: dict = {} + ra, rb, rc = r[0], r[1], r[2] + r_wye.update({"r1": rb * rc / (ra + rb + rc)}) + r_wye.update({"r2": ra * rc / (ra + rb + rc)}) + r_wye.update({"r3": ra * rb / (ra + rb + rc)}) + return r_wye + + +def wye_to_delta(r: list) -> dict: + """ + >>> from_wye_to_delta([2.0, 3.0, 4.0]) + {'ra': 13.0, 'rb': 8.666666666666666, 'rc': 6.5} + """ + r1, r2, r3 = r[0], r[1], r[2] + r_delta: dict = {} + r_delta.update({"ra": (r1 * r2 + r2 * r3 + r3 * r1) / r1}) + r_delta.update({"rb": (r1 * r2 + r2 * r3 + r3 * r1) / r2}) + r_delta.update({"rc": (r1 * r2 + r2 * r3 + r3 * r1) / r3}) + return r_delta + + +def transform(mode: int, r: list) -> dict: + """ + >>> transform(1, [4.0, 5.0, 6.0]) + {'r1': 2.0, 'r2': 1.6, 'r3': 1.3333333333333333} + + >>> transform(2, [4.0, 5.0, 6.0]) + {'ra': 18.5, 'rb': 14.8, 'rc': 12.333333333333334} + """ + r_transformed = {} + if mode == 1: + r_transformed = delta_to_wye(r) + elif mode == 2: + r_transformed = wye_to_delta(r) + return r_transformed + + +def get_type_transform(): + mode: int = 0 + try: + print(""" + 1. From delta to wye + 2. From wye to delta + """) + mode = int(input("? --> ")) + except ValueError: + print("Invalid Value. Only int inputs are accepted") + exit() + return mode + + +def get_resistors_values(mode: int) -> list: + r: list = [] + print("Select conversion (type 1 or 2)") + try: + if mode == 1: + r = list( + map( + float, input("Resistant values (format ra rb rc): ").strip().split() + ) + )[:3] + elif mode == 2: + r = list( + map( + float, input("Resistant values (format r1 r2 r3): ").strip().split() + ) + )[:3] + else: + print("Incorrect selected option. Valid option 1 or 2") + except ValueError: + print("Invalid Value. Only int inputs are accepted") + exit() + return r + + +def test_get_type_transformation() -> None: + with mock.patch("builtins.input", return_value="1"): + m = get_type_transform() + assert m == 1 + + +def test_get_resistors_values() -> None: + with mock.patch("builtins.input", return_value="2 4 8"): + r = get_resistors_values(2) + assert r == [2.0, 4.0, 8.0] + + +def main() -> None: + print("star - delta transform") + mode = get_type_transform() + r = get_resistors_values(mode) + r_transformed = transform(mode, r) + print(f"Result: '{r_transformed}'") + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + main() From c5ba7fbee687118a5944dfd2c1fec9adbe2f5b6a Mon Sep 17 00:00:00 2001 From: MRJPEREZR Date: Thu, 7 Nov 2024 10:18:58 +0000 Subject: [PATCH 2/6] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553..2a5ac5584 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -422,6 +422,7 @@ * [Resistor Color Code](electronics/resistor_color_code.py) * [Resistor Equivalence](electronics/resistor_equivalence.py) * [Resonant Frequency](electronics/resonant_frequency.py) + * [Star Delta Transform](electronics/star_delta_transform.py) * [Wheatstone Bridge](electronics/wheatstone_bridge.py) ## File Transfer From 9bd3c3f998f6ae1c5d2399116ec48ec7a4642b08 Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Thu, 7 Nov 2024 11:25:18 +0100 Subject: [PATCH 3/6] fixing typos --- electronics/star_delta_transform.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/electronics/star_delta_transform.py b/electronics/star_delta_transform.py index 62dc925fc..b4c940e62 100644 --- a/electronics/star_delta_transform.py +++ b/electronics/star_delta_transform.py @@ -20,7 +20,7 @@ from unittest import mock def delta_to_wye(r: list) -> dict: """ - >>> from_delta_to_wye([2.0, 3.0, 4.0]) + >>> delta_to_wye([2.0, 3.0, 4.0]) {'r1': 1.3333333333333333, 'r2': 0.8888888888888888, 'r3': 0.6666666666666666} """ r_wye: dict = {} @@ -33,7 +33,7 @@ def delta_to_wye(r: list) -> dict: def wye_to_delta(r: list) -> dict: """ - >>> from_wye_to_delta([2.0, 3.0, 4.0]) + >>> wye_to_delta([2.0, 3.0, 4.0]) {'ra': 13.0, 'rb': 8.666666666666666, 'rc': 6.5} """ r1, r2, r3 = r[0], r[1], r[2] From 873dcf0c268d346047bfbded9d03beeec9697cda Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Thu, 7 Nov 2024 11:30:19 +0100 Subject: [PATCH 4/6] adding description to r argument --- electronics/star_delta_transform.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/electronics/star_delta_transform.py b/electronics/star_delta_transform.py index b4c940e62..5f7e2da09 100644 --- a/electronics/star_delta_transform.py +++ b/electronics/star_delta_transform.py @@ -20,6 +20,8 @@ from unittest import mock def delta_to_wye(r: list) -> dict: """ + r is a list with resistor values to be transform from delta to star + >>> delta_to_wye([2.0, 3.0, 4.0]) {'r1': 1.3333333333333333, 'r2': 0.8888888888888888, 'r3': 0.6666666666666666} """ @@ -33,6 +35,8 @@ def delta_to_wye(r: list) -> dict: def wye_to_delta(r: list) -> dict: """ + r is a list with resistor values to be transform from star to delta + >>> wye_to_delta([2.0, 3.0, 4.0]) {'ra': 13.0, 'rb': 8.666666666666666, 'rc': 6.5} """ @@ -60,7 +64,7 @@ def transform(mode: int, r: list) -> dict: return r_transformed -def get_type_transform(): +def get_type_transform() -> int: mode: int = 0 try: print(""" From 31b48187c634342a3fd10227a698420ba08b0e67 Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Thu, 7 Nov 2024 11:33:40 +0100 Subject: [PATCH 5/6] adding description to r argument --- electronics/star_delta_transform.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/electronics/star_delta_transform.py b/electronics/star_delta_transform.py index 5f7e2da09..42e15a4b9 100644 --- a/electronics/star_delta_transform.py +++ b/electronics/star_delta_transform.py @@ -50,6 +50,8 @@ def wye_to_delta(r: list) -> dict: def transform(mode: int, r: list) -> dict: """ + r is a list with resistor float values to be transform + >>> transform(1, [4.0, 5.0, 6.0]) {'r1': 2.0, 'r2': 1.6, 'r3': 1.3333333333333333} From 78929f65f55fdf5b4c68dd4627635824c1e1157e Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Thu, 7 Nov 2024 11:35:55 +0100 Subject: [PATCH 6/6] adding description to r argument --- electronics/star_delta_transform.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/electronics/star_delta_transform.py b/electronics/star_delta_transform.py index 42e15a4b9..491035e88 100644 --- a/electronics/star_delta_transform.py +++ b/electronics/star_delta_transform.py @@ -18,29 +18,25 @@ from sys import exit from unittest import mock -def delta_to_wye(r: list) -> dict: +def delta_to_wye(resistors: list) -> dict: """ - r is a list with resistor values to be transform from delta to star - >>> delta_to_wye([2.0, 3.0, 4.0]) {'r1': 1.3333333333333333, 'r2': 0.8888888888888888, 'r3': 0.6666666666666666} """ r_wye: dict = {} - ra, rb, rc = r[0], r[1], r[2] + ra, rb, rc = resistors[0], resistors[1], resistors[2] r_wye.update({"r1": rb * rc / (ra + rb + rc)}) r_wye.update({"r2": ra * rc / (ra + rb + rc)}) r_wye.update({"r3": ra * rb / (ra + rb + rc)}) return r_wye -def wye_to_delta(r: list) -> dict: +def wye_to_delta(resistors: list) -> dict: """ - r is a list with resistor values to be transform from star to delta - >>> wye_to_delta([2.0, 3.0, 4.0]) {'ra': 13.0, 'rb': 8.666666666666666, 'rc': 6.5} """ - r1, r2, r3 = r[0], r[1], r[2] + r1, r2, r3 = resistors[0], resistors[1], resistors[2] r_delta: dict = {} r_delta.update({"ra": (r1 * r2 + r2 * r3 + r3 * r1) / r1}) r_delta.update({"rb": (r1 * r2 + r2 * r3 + r3 * r1) / r2}) @@ -48,10 +44,8 @@ def wye_to_delta(r: list) -> dict: return r_delta -def transform(mode: int, r: list) -> dict: +def transform(mode: int, resistors: list) -> dict: """ - r is a list with resistor float values to be transform - >>> transform(1, [4.0, 5.0, 6.0]) {'r1': 2.0, 'r2': 1.6, 'r3': 1.3333333333333333} @@ -60,9 +54,9 @@ def transform(mode: int, r: list) -> dict: """ r_transformed = {} if mode == 1: - r_transformed = delta_to_wye(r) + r_transformed = delta_to_wye(resistors) elif mode == 2: - r_transformed = wye_to_delta(r) + r_transformed = wye_to_delta(resistors) return r_transformed