From 0ff268ab3ac630277e33c56311532bc296a3f477 Mon Sep 17 00:00:00 2001 From: Yosef Sirak Date: Mon, 2 Oct 2023 14:00:15 -0700 Subject: [PATCH 1/8] added shunt power factor correction in electronics --- ...shunt_capacitor_power_factor_correction.py | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 electronics/shunt_capacitor_power_factor_correction.py diff --git a/electronics/shunt_capacitor_power_factor_correction.py b/electronics/shunt_capacitor_power_factor_correction.py new file mode 100644 index 000000000000..2865f37afce6 --- /dev/null +++ b/electronics/shunt_capacitor_power_factor_correction.py @@ -0,0 +1,69 @@ +import math + + + +def shunt_capacitor_power_factor_correction( + voltage: float, frequency: float, real_power: float, current_power_factor: float , expected_power_factor:float +)->float: + + """ + Calculate shunt capacitor that will be added in order to achieve the expected power factor from the current power factor + + Examples: + >>> shunt_capacitor_power_factor_correction(120,60,4000,0.8,0.95) + 0.00031043753362948597 + >>> shunt_capacitor_power_factor_correction(150,50,2000,0.6,0.87) + 0.00021690547192207782 + >>> shunt_capacitor_power_factor_correction(115,0,800,0.6,0.87) + Traceback (most recent call last): + ... + ValueError: frequency is zero dc circuit + >>> shunt_capacitor_power_factor_correction(0,60,800,0.6,0.87) + Traceback (most recent call last): + ... + ValueError: voltage is zero no excitation + """ + if ( + not isinstance(current_power_factor, (int, float)) + or not isinstance(expected_power_factor, (int, float)) + or current_power_factor < -1 + or current_power_factor > 1 + or expected_power_factor < -1 + or expected_power_factor > 1 + ): + raise ValueError("power_factor must be a valid float value between -1 and 1.") + + if ( + frequency == 0 + ): + raise ValueError("frequency is zero dc circuit") + + if ( + voltage == 0 + ): + raise ValueError("voltage is zero no excitation") + + current_theta = math.acos(current_power_factor) + expected_theta= math.acos(expected_power_factor) + + current_apparent_power = real_power / current_power_factor + current_reactive_power = current_apparent_power * math.sin(current_theta) + expected_apparent_power= real_power / expected_power_factor + expected_reactive_power= expected_apparent_power * math.sin(expected_theta) + + """ + The difference between the new and old reactive powers is due to the + parallel addition of the capacitor to the load + """ + change_reactive_power = current_reactive_power - expected_reactive_power + + return change_reactive_power/( 2* math.pi * frequency * (voltage**2)) + pass + + + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 43f259624405b843d606c3819556a051f7e008d5 Mon Sep 17 00:00:00 2001 From: Yosef Sirak <95920190+Yosef-6@users.noreply.github.com> Date: Mon, 2 Oct 2023 14:16:09 -0700 Subject: [PATCH 2/8] Update shunt_capacitor_power_factor_correction.py --- electronics/shunt_capacitor_power_factor_correction.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/electronics/shunt_capacitor_power_factor_correction.py b/electronics/shunt_capacitor_power_factor_correction.py index 2865f37afce6..35be951aceb4 100644 --- a/electronics/shunt_capacitor_power_factor_correction.py +++ b/electronics/shunt_capacitor_power_factor_correction.py @@ -1,5 +1,7 @@ -import math +# https://www.electronics-tutorials.ws/accircuits/power-factor-correction.html +# https://www.youtube.com/watch?v=YZcBkFdstEU +import math def shunt_capacitor_power_factor_correction( From 70076dc27fec35926a7e01f860fe1968117cb4eb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 21:18:53 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...shunt_capacitor_power_factor_correction.py | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/electronics/shunt_capacitor_power_factor_correction.py b/electronics/shunt_capacitor_power_factor_correction.py index 35be951aceb4..7e17c19db62d 100644 --- a/electronics/shunt_capacitor_power_factor_correction.py +++ b/electronics/shunt_capacitor_power_factor_correction.py @@ -5,16 +5,19 @@ def shunt_capacitor_power_factor_correction( - voltage: float, frequency: float, real_power: float, current_power_factor: float , expected_power_factor:float -)->float: - + voltage: float, + frequency: float, + real_power: float, + current_power_factor: float, + expected_power_factor: float, +) -> float: """ Calculate shunt capacitor that will be added in order to achieve the expected power factor from the current power factor - + Examples: >>> shunt_capacitor_power_factor_correction(120,60,4000,0.8,0.95) 0.00031043753362948597 - >>> shunt_capacitor_power_factor_correction(150,50,2000,0.6,0.87) + >>> shunt_capacitor_power_factor_correction(150,50,2000,0.6,0.87) 0.00021690547192207782 >>> shunt_capacitor_power_factor_correction(115,0,800,0.6,0.87) Traceback (most recent call last): @@ -26,32 +29,28 @@ def shunt_capacitor_power_factor_correction( ValueError: voltage is zero no excitation """ if ( - not isinstance(current_power_factor, (int, float)) + not isinstance(current_power_factor, (int, float)) or not isinstance(expected_power_factor, (int, float)) - or current_power_factor < -1 - or current_power_factor > 1 + or current_power_factor < -1 + or current_power_factor > 1 or expected_power_factor < -1 - or expected_power_factor > 1 + or expected_power_factor > 1 ): raise ValueError("power_factor must be a valid float value between -1 and 1.") - - if ( - frequency == 0 - ): + + if frequency == 0: raise ValueError("frequency is zero dc circuit") - if ( - voltage == 0 - ): + if voltage == 0: raise ValueError("voltage is zero no excitation") current_theta = math.acos(current_power_factor) - expected_theta= math.acos(expected_power_factor) - + expected_theta = math.acos(expected_power_factor) + current_apparent_power = real_power / current_power_factor current_reactive_power = current_apparent_power * math.sin(current_theta) - expected_apparent_power= real_power / expected_power_factor - expected_reactive_power= expected_apparent_power * math.sin(expected_theta) + expected_apparent_power = real_power / expected_power_factor + expected_reactive_power = expected_apparent_power * math.sin(expected_theta) """ The difference between the new and old reactive powers is due to the @@ -59,12 +58,10 @@ def shunt_capacitor_power_factor_correction( """ change_reactive_power = current_reactive_power - expected_reactive_power - return change_reactive_power/( 2* math.pi * frequency * (voltage**2)) + return change_reactive_power / (2 * math.pi * frequency * (voltage**2)) pass - - if __name__ == "__main__": import doctest From 978f29f00afa7fd7ed1156d5ce33c58b8d84511e Mon Sep 17 00:00:00 2001 From: Yosef Sirak <95920190+Yosef-6@users.noreply.github.com> Date: Mon, 2 Oct 2023 14:41:36 -0700 Subject: [PATCH 4/8] Update shunt_capacitor_power_factor_correction.py removed unnecessary pass and reformatted long line --- electronics/shunt_capacitor_power_factor_correction.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/electronics/shunt_capacitor_power_factor_correction.py b/electronics/shunt_capacitor_power_factor_correction.py index 7e17c19db62d..354fc7604004 100644 --- a/electronics/shunt_capacitor_power_factor_correction.py +++ b/electronics/shunt_capacitor_power_factor_correction.py @@ -12,7 +12,8 @@ def shunt_capacitor_power_factor_correction( expected_power_factor: float, ) -> float: """ - Calculate shunt capacitor that will be added in order to achieve the expected power factor from the current power factor + Calculate shunt capacitor that will be added in order to achieve the + expected power factor Examples: >>> shunt_capacitor_power_factor_correction(120,60,4000,0.8,0.95) @@ -59,7 +60,6 @@ def shunt_capacitor_power_factor_correction( change_reactive_power = current_reactive_power - expected_reactive_power return change_reactive_power / (2 * math.pi * frequency * (voltage**2)) - pass if __name__ == "__main__": From 748cdf5609af36fba21561556e8db96b386d0572 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 21:43:57 +0000 Subject: [PATCH 5/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- electronics/shunt_capacitor_power_factor_correction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electronics/shunt_capacitor_power_factor_correction.py b/electronics/shunt_capacitor_power_factor_correction.py index 354fc7604004..8a988b0eed5c 100644 --- a/electronics/shunt_capacitor_power_factor_correction.py +++ b/electronics/shunt_capacitor_power_factor_correction.py @@ -12,7 +12,7 @@ def shunt_capacitor_power_factor_correction( expected_power_factor: float, ) -> float: """ - Calculate shunt capacitor that will be added in order to achieve the + Calculate shunt capacitor that will be added in order to achieve the expected power factor Examples: From a2a80d76721610e67472389575af723ed885958e Mon Sep 17 00:00:00 2001 From: Yosef Sirak Date: Fri, 6 Oct 2023 00:29:22 -0700 Subject: [PATCH 6/8] renamed and added inductor correction --- electronics/power_factor_correction.py | 131 ++++++++++++++++++ ...shunt_capacitor_power_factor_correction.py | 68 --------- 2 files changed, 131 insertions(+), 68 deletions(-) create mode 100644 electronics/power_factor_correction.py delete mode 100644 electronics/shunt_capacitor_power_factor_correction.py diff --git a/electronics/power_factor_correction.py b/electronics/power_factor_correction.py new file mode 100644 index 000000000000..8b876a5b957b --- /dev/null +++ b/electronics/power_factor_correction.py @@ -0,0 +1,131 @@ +# https://www.electronics-tutorials.ws/accircuits/power-factor-correction.html +# https://www.youtube.com/watch?v=YZcBkFdstEU + +import math + + +def shunt_capacitor_power_factor_correction( + voltage: float, + frequency: float, + real_power: float, + current_power_factor: float, + expected_power_factor: float, +) -> float: + """ + Calculate shunt capacitor that will be added in order to achieve the + expected power factor + + Examples: + >>> shunt_capacitor_power_factor_correction(120,60,4000,0.8,0.95) + 0.00031043753362948597 + >>> shunt_capacitor_power_factor_correction(150,50,2000,0.6,0.87) + 0.00021690547192207782 + >>> shunt_capacitor_power_factor_correction(115,0,800,0.6,0.87) + Traceback (most recent call last): + ... + ValueError: frequency is zero dc circuit + >>> shunt_capacitor_power_factor_correction(0,60,800,0.6,0.87) + Traceback (most recent call last): + ... + ValueError: voltage is zero no excitation + """ + + + if ( + not isinstance(current_power_factor, (int, float)) + or not isinstance(expected_power_factor, (int, float)) + or current_power_factor < -1 + or current_power_factor > 1 + or expected_power_factor < -1 + or expected_power_factor > 1 + ): + raise ValueError("power_factor must be a valid float value between -1 and 1.") + + if frequency == 0: + raise ValueError("frequency is zero dc circuit") + + if voltage == 0: + raise ValueError("voltage is zero no excitation") + + current_theta = math.acos(current_power_factor) + expected_theta = math.acos(expected_power_factor) + + current_apparent_power = real_power / current_power_factor + current_reactive_power = current_apparent_power * math.sin(current_theta) + expected_apparent_power = real_power / expected_power_factor + expected_reactive_power = expected_apparent_power * math.sin(expected_theta) + + """ + The difference between the new and old reactive powers is due to the + parallel addition of the capacitor to the load + """ + change_reactive_power = current_reactive_power - expected_reactive_power + + return change_reactive_power / (2 * math.pi * frequency * (voltage**2)) + + + +def shunt_inductor_power_factor_correction( + voltage: float, + frequency: float, + real_power: float, + current_power_factor: float, + expected_power_factor: float, +) -> float: + """ + Calculate shunt capacitor that will be added in order to achieve the + expected power factor + + Examples: + >>> shunt_inductor_power_factor_correction(120,60,4000,0.8,0.95) + 0.02266540783980564 + >>> shunt_inductor_power_factor_correction(120,60,4000,-0.8,-0.4) + 0.006195660726930193 + >>> shunt_inductor_power_factor_correction(115,0,800,-0.6,0.87) + Traceback (most recent call last): + ... + ValueError: frequency is zero dc circuit + >>> shunt_inductor_power_factor_correction(0,60,800,-0.6,0.87) + Traceback (most recent call last): + ... + ValueError: voltage is zero no excitation + """ + + if ( + not isinstance(current_power_factor, (int, float)) + or not isinstance(expected_power_factor, (int, float)) + or current_power_factor < -1 + or current_power_factor > 1 + or expected_power_factor < -1 + or expected_power_factor > 1 + ): + raise ValueError("power_factor must be a valid float value between -1 and 1.") + + if frequency == 0: + raise ValueError("frequency is zero dc circuit") + + if voltage == 0: + raise ValueError("voltage is zero no excitation") + + current_theta = math.acos(current_power_factor) + expected_theta = math.acos(expected_power_factor) + + current_apparent_power = real_power / current_power_factor + current_reactive_power = current_apparent_power * math.sin(current_theta) + expected_apparent_power = real_power / expected_power_factor + expected_reactive_power = expected_apparent_power * math.sin(expected_theta) + + """ + The difference between the new and old reactive powers is due to the + parallel addition of the inductor to the load + """ + change_reactive_power = current_reactive_power - expected_reactive_power + + return (voltage**2) / ( 2 * math.pi * frequency * change_reactive_power ) + + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/electronics/shunt_capacitor_power_factor_correction.py b/electronics/shunt_capacitor_power_factor_correction.py deleted file mode 100644 index 8a988b0eed5c..000000000000 --- a/electronics/shunt_capacitor_power_factor_correction.py +++ /dev/null @@ -1,68 +0,0 @@ -# https://www.electronics-tutorials.ws/accircuits/power-factor-correction.html -# https://www.youtube.com/watch?v=YZcBkFdstEU - -import math - - -def shunt_capacitor_power_factor_correction( - voltage: float, - frequency: float, - real_power: float, - current_power_factor: float, - expected_power_factor: float, -) -> float: - """ - Calculate shunt capacitor that will be added in order to achieve the - expected power factor - - Examples: - >>> shunt_capacitor_power_factor_correction(120,60,4000,0.8,0.95) - 0.00031043753362948597 - >>> shunt_capacitor_power_factor_correction(150,50,2000,0.6,0.87) - 0.00021690547192207782 - >>> shunt_capacitor_power_factor_correction(115,0,800,0.6,0.87) - Traceback (most recent call last): - ... - ValueError: frequency is zero dc circuit - >>> shunt_capacitor_power_factor_correction(0,60,800,0.6,0.87) - Traceback (most recent call last): - ... - ValueError: voltage is zero no excitation - """ - if ( - not isinstance(current_power_factor, (int, float)) - or not isinstance(expected_power_factor, (int, float)) - or current_power_factor < -1 - or current_power_factor > 1 - or expected_power_factor < -1 - or expected_power_factor > 1 - ): - raise ValueError("power_factor must be a valid float value between -1 and 1.") - - if frequency == 0: - raise ValueError("frequency is zero dc circuit") - - if voltage == 0: - raise ValueError("voltage is zero no excitation") - - current_theta = math.acos(current_power_factor) - expected_theta = math.acos(expected_power_factor) - - current_apparent_power = real_power / current_power_factor - current_reactive_power = current_apparent_power * math.sin(current_theta) - expected_apparent_power = real_power / expected_power_factor - expected_reactive_power = expected_apparent_power * math.sin(expected_theta) - - """ - The difference between the new and old reactive powers is due to the - parallel addition of the capacitor to the load - """ - change_reactive_power = current_reactive_power - expected_reactive_power - - return change_reactive_power / (2 * math.pi * frequency * (voltage**2)) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 83b24cc92fc2fb0f21ca510489f0f9ee33fc6b95 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 07:30:28 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- electronics/power_factor_correction.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/electronics/power_factor_correction.py b/electronics/power_factor_correction.py index 8b876a5b957b..04affdb56d89 100644 --- a/electronics/power_factor_correction.py +++ b/electronics/power_factor_correction.py @@ -29,24 +29,23 @@ def shunt_capacitor_power_factor_correction( ... ValueError: voltage is zero no excitation """ - if ( not isinstance(current_power_factor, (int, float)) or not isinstance(expected_power_factor, (int, float)) - or current_power_factor < -1 - or current_power_factor > 1 + or current_power_factor < -1 + or current_power_factor > 1 or expected_power_factor < -1 - or expected_power_factor > 1 + or expected_power_factor > 1 ): raise ValueError("power_factor must be a valid float value between -1 and 1.") - + if frequency == 0: raise ValueError("frequency is zero dc circuit") if voltage == 0: raise ValueError("voltage is zero no excitation") - + current_theta = math.acos(current_power_factor) expected_theta = math.acos(expected_power_factor) @@ -64,7 +63,6 @@ def shunt_capacitor_power_factor_correction( return change_reactive_power / (2 * math.pi * frequency * (voltage**2)) - def shunt_inductor_power_factor_correction( voltage: float, frequency: float, @@ -90,14 +88,14 @@ def shunt_inductor_power_factor_correction( ... ValueError: voltage is zero no excitation """ - + if ( not isinstance(current_power_factor, (int, float)) or not isinstance(expected_power_factor, (int, float)) - or current_power_factor < -1 - or current_power_factor > 1 + or current_power_factor < -1 + or current_power_factor > 1 or expected_power_factor < -1 - or expected_power_factor > 1 + or expected_power_factor > 1 ): raise ValueError("power_factor must be a valid float value between -1 and 1.") @@ -121,11 +119,10 @@ def shunt_inductor_power_factor_correction( """ change_reactive_power = current_reactive_power - expected_reactive_power - return (voltage**2) / ( 2 * math.pi * frequency * change_reactive_power ) - + return (voltage**2) / (2 * math.pi * frequency * change_reactive_power) if __name__ == "__main__": import doctest - + doctest.testmod() From a378d6bfef68c12f9dfa3aede9f06143c350104a Mon Sep 17 00:00:00 2001 From: cclauss Date: Tue, 8 Sep 2026 13:57:53 +0000 Subject: [PATCH 8/8] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 69bdb2b6c9d0..fcbe0ded03f6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -479,6 +479,7 @@ * [Ic 555 Timer](electronics/ic_555_timer.py) * [Ind Reactance](electronics/ind_reactance.py) * [Ohms Law](electronics/ohms_law.py) + * [Power Factor Correction](electronics/power_factor_correction.py) * [Real And Reactive Power](electronics/real_and_reactive_power.py) * [Resistor Color Code](electronics/resistor_color_code.py) * [Resistor Equivalence](electronics/resistor_equivalence.py) @@ -876,6 +877,7 @@ * [Test Factorial](maths/test_factorial.py) * [Test Prime Check](maths/test_prime_check.py) * [Three Sum](maths/three_sum.py) + * [Tonelli Shanks](maths/tonelli_shanks.py) * [Trailing Zeroes](maths/trailing_zeroes.py) * [Trapezoidal Rule](maths/trapezoidal_rule.py) * [Triplet Sum](maths/triplet_sum.py)