mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-29 05:53:07 +08:00
e1a42f05f5
* Added various code for calculating values of elements in full wave rectifier circuit. I have written a code for calculating various values in a full wave rectifier circuit. This code includes code for calculating the following elements : 1. MAX load current 2. DC current 3. DC voltage 4. MAX current 5. RMS current 6. RMS voltage * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename fwr..py to fwr.py * Update variable names * Refactor docstrings in full_wave_rectifier.py Updated docstrings for clarity and consistency. * Fix typo in ValueError message for resistances * Fix error message for invalid resistance value --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
132 lines
3.9 KiB
Python
132 lines
3.9 KiB
Python
from typing import NamedTuple
|
|
|
|
|
|
class Result(NamedTuple):
|
|
name: str
|
|
value: float
|
|
|
|
|
|
def max_load_current(rf: float, rs: float, rl: float, vm: float) -> tuple:
|
|
"""
|
|
This function can calculate the maximum load current(Im)
|
|
in a full-wave rectifier circuit.
|
|
Im = maximum load current
|
|
rf = Forward Resistance of the diode
|
|
rs = Transformer secondary winding resistance
|
|
rl = load resistance
|
|
vm = maximum voltage or peak voltage
|
|
Cases:-
|
|
>>> max_load_current(rf=2 , rs=4 , rl=6 , vm=15 )
|
|
Result(name='max_load_current', value=1.25)
|
|
>>> max_load_current(rf=2 , rs=4 , rl=6 , vm=0 )
|
|
Result(name='max_load_current', value=0.0)
|
|
>>> max_load_current(rf=2 , rs=-4 , rl=6 , vm=15 )
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: Resistance cannot be negative
|
|
>>> max_load_current(rf=0 , rs=0 , rl=0 , vm=15 )
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: At least one Resistance must be non-zero
|
|
"""
|
|
if (rf, rs, rl).count(0) == 3:
|
|
raise ValueError("At least one Resistance must be non-zero")
|
|
elif rf < 0 or rs < 0 or rl < 0:
|
|
raise ValueError("Resistance cannot be negative")
|
|
elif vm == 0:
|
|
return Result("max_load_current", vm / (rf + rs + rl))
|
|
else:
|
|
return Result("max_load_current", vm / (rf + rs + rl))
|
|
|
|
|
|
def dc_current(im: float) -> tuple:
|
|
"""
|
|
This function can calculate the Average DC Current(Idc) in a circuit.
|
|
In all cases, a negative sign shows
|
|
the opposite direction of current or voltage.
|
|
Idc = average direct current (DC)
|
|
im = maximum current or peak current
|
|
cases:
|
|
>>> dc_current(im=2)
|
|
Result(name='Idc', value=1.272)
|
|
>>> dc_current(im=0)
|
|
Result(name='Idc', value=0.0)
|
|
"""
|
|
return Result("Idc", 2 * im * 0.318)
|
|
|
|
|
|
def dc_voltage(vm: float) -> tuple:
|
|
"""
|
|
This function can calculate the Average DC Voltage(Vdc) in a circuit.
|
|
In all cases, a negative sign shows
|
|
the opposite direction of current or voltage.
|
|
Vdc = average direct current (DC)
|
|
vm = maximum voltage or peak voltage
|
|
cases:
|
|
>>> dc_voltage(vm=2)
|
|
Result(name='Vdc', value=1.272)
|
|
>>> dc_voltage(vm=0)
|
|
Result(name='Vdc', value=0.0)
|
|
"""
|
|
return Result("Vdc", 2 * 0.318 * vm)
|
|
|
|
|
|
def max_current(vm: float, rl: float) -> tuple:
|
|
"""
|
|
This function can calculate the maximum current(Im) in a circuit.
|
|
In all cases, a negative sign shows the
|
|
opposite direction of current or voltage.
|
|
vm = maximum voltage or peak voltage
|
|
rl = load resistance
|
|
cases:
|
|
>>> max_current(vm=2, rl=5)
|
|
Result(name='Max_current_Im', value=0.4)
|
|
>>> max_current(vm=2, rl=-5)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: Resistance cannot be negative or equal to zero
|
|
|
|
"""
|
|
if rl <= 0:
|
|
raise ValueError("Resistance cannot be negative or equal to zero")
|
|
else:
|
|
return Result("Max_current_Im", vm / rl)
|
|
|
|
|
|
def rms_current(im: float) -> tuple:
|
|
"""
|
|
This function calculates the RMS(Root Mean Square) value of current(Irms).
|
|
In all cases, a negative sign shows
|
|
the opposite direction of current or voltage.
|
|
Irms = Root Mean Square Value of current
|
|
im = maximum current or peak current
|
|
cases:
|
|
>>> rms_current(im=10)
|
|
Result(name='Irms', value=7.069999999999999)
|
|
>>> rms_current(im=0)
|
|
Result(name='Irms', value=0.0)
|
|
"""
|
|
return Result("Irms", im * 0.707)
|
|
|
|
|
|
def rms_voltage(vm: float) -> tuple:
|
|
"""
|
|
This function calculates the RMS(Root Mean Square) value of voltage(Vrms).
|
|
In all cases, a negative sign shows
|
|
the opposite direction of current or voltage.
|
|
Vrms = Root Mean Square Value of voltage
|
|
vm = maximum voltage or peak voltage
|
|
cases:
|
|
>>> rms_voltage(vm=20)
|
|
Result(name='Vrms', value=14.139999999999999)
|
|
>>> rms_voltage(vm=0)
|
|
Result(name='Vrms', value=0.0)
|
|
"""
|
|
return Result("Vrms", vm * 0.707)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|