################################################################################"""Functions and Formulas Related to Passive Electrical Components (R/L/C).Filled with methods related capacitors."""################################################################################importnumpyas_np
[docs]defvcapdischarge(t,Vs,R,C):r""" Discharging Capacitor Function. Function to calculate the voltage of a capacitor that is discharging given the time. .. math:: V_c=V_s*e^{\frac{-t}{R*C}} Parameters ---------- t: float The time at which to calculate the voltage. Vs: float The starting voltage for the capacitor. R: float The ohmic value of the resistor being used to discharge. C: float Capacitive value (in Farads). Returns ------- Vc: float The calculated voltage of the capacitor. """ift<0:raiseValueError("Time must be greater than or equal to zero.")ifR*C==0:raiseValueError("Resistance and Capacitance must be non-zero.")Vc=Vs*(_np.exp(-t/(R*C)))returnVc
[docs]defvcapcharge(t,Vs,R,C):r""" Charging Capacitor Voltage. Function to calculate the voltage of a capacitor that is charging given the time. .. math:: V_c=V_s*(1-e^{\frac{-t}{R*C}}) Parameters ---------- t: float The time at which to calculate the voltage. Vs: float The charging voltage for the capacitor. R: float The ohmic value of the resistor being used to discharge. C: float Capacitive value (in Farads). Returns ------- Vc: float The calculated voltage of the capacitor. """ift<0:raiseValueError("Time must be greater than or equal to zero.")ifR*C==0:raiseValueError("Resistance and Capacitance must be non-zero.")Vc=Vs*(1-_np.exp(-t/(R*C)))returnVc
[docs]defcaptransfer(t,Vs,R,Cs,Cd):""" Capacitor Energy Transfer Function. Calculate the voltage across a joining resistor (R) that connects Cs and Cd, the energy-source and -destination capacitors, respectively. Calculate the final voltage across both capacitors. Parameters ---------- t: float Time at which to calculate resistor voltage. Vs: float Initial voltage across source-capacitor (Cs). R: float Value of resistor that connects capacitors. Cs: float Source capacitance value in Farads. Cd: float Destination capacitance value in Farads. Returns ------- rvolt: float Voltage across the resistor at time t. vfinal: float Final voltage that both capacitors settle to. """ift<0:raiseValueError("Time must be greater than zero.")try:tau=(R*Cs*Cd)/(Cs+Cd)rvolt=Vs*_np.exp(-t/tau)exceptZeroDivisionError:raiseZeroDivisionError("Sum of Source and Destination Capacitance must be non-zero.")vfinal=Vs*Cs/(Cs+Cd)returnrvolt,vfinal
# Define Capacitive Back-to-Back Switching Formula
[docs]defcapbacktoback(C1,C2,Lm,VLN=None,VLL=None):""" Back to Back Capacitor Transient Current Calculator. Function to calculate the maximum current and the frequency of the inrush current of two capacitors connected in parallel when one (energized) capacitor is switched into another (non-engergized) capacitor. .. note:: This formula is only valid for three-phase systems. Parameters ---------- C1: float The capacitance of the VLN: float, exclusive The line-to-neutral voltage experienced by any one of the (three) capacitors in the three-phase capacitor bank. VLL: float, exclusive The line-to-line voltage experienced by the three-phase capacitor bank. Returns ------- imax: float Maximum Current Magnitude during Transient ifreq: float Transient current frequency """# Evaluate Max Currentimax=_np.sqrt(2/3)*VLL*_np.sqrt((C1*C2)/((C1+C2)*Lm))# Evaluate Inrush Current Frequencyifreq=1/(2*_np.pi*_np.sqrt(Lm*(C1*C2)/(C1+C2)))returnimax,ifreq
# Define Apparent Power to Farad Conversion
[docs]deffarads(VAR,V,freq=60):r""" Capacitance from Apparent Power Formula. Function to calculate the required capacitance in Farads to provide the desired power rating (VARs). .. math:: C = \frac{VAR}{2*\pi*freq*V^2} Parameters ---------- VAR: float The rated power to meet. V: float The voltage across the capacitor; not described as VLL or VLN, merely the capacitor voltage. freq: float, optional The System frequency Returns ------- C: float The evaluated capacitance (in Farads). """returnVAR/(2*_np.pi*freq*V**2)
# Define Capacitor Energy Calculation
[docs]defcapenergy(C,V):r""" Capacitor Energy Formula. A simple function to calculate the stored voltage (in Joules) in a capacitor with a charged voltage. .. math:: E=\frac{1}{2}*C*V^2 Parameters ---------- C: float Capacitance in Farads. V: float Voltage across capacitor. Returns ------- energy: float Energy stored in capacitor (Joules). """energy=1/2*C*V**2returnenergy
# Define Capacitor Voltage Discharge Function
[docs]defloadedvcapdischarge(t,vo,C,P):# noqa: D401 "Loaded" is a valid word for this docstringr""" Loaded Capacitor Discharge Formula. Returns the voltage of a discharging capacitor after time (t - seconds) given initial voltage (vo - volts), capacitor size (cap - Farads), and load (P - Watts). .. math:: V_t=\sqrt{v_0^2-2*P*\frac{t}{C}} Parameters ---------- t: float Time at which to calculate voltage. vo: float Initial capacitor voltage. C: float Capacitance (in Farads) P: float Load power consumption (in Watts). Returns ------- Vt: float Voltage of capacitor at time t. """Vt=_np.sqrt(vo**2-2*P*t/C)returnVt
# Define Capacitor Discharge Function
[docs]deftimedischarge(Vinit,Vmin,C,P,dt=1e-3,RMS=True,Eremain=False):r""" Capacitor Discharge Time Formula. Returns the time to discharge a capacitor to a specified voltage given set of inputs. Parameters ---------- Vinit: float Initial Voltage (in volts) Vmin: float Final Voltage (the minimum allowable voltage) (in volts) C: float Capacitance (in Farads) P: float Load Power being consumed (in Watts) dt: float, optional Time step-size (in seconds) (defaults to 1e-3 | 1ms) RMS: bool, optional if true converts RMS Vin to peak Eremain: bool, optional if true: also returns the energy remaining in cap Returns ------- Returns time to discharge from Vinit to Vmin in seconds. May also return remaining energy in capacitor if Eremain=True """t=0# start at time t=0ifRMS:vo=Vinit*_np.sqrt(2)# convert RMS to peakelse:vo=Vinitvc=loadedvcapdischarge(t,vo,C,P)# set initial cap voltagewhilevc>=Vmin:t=t+dt# increment the timevcp=vc# save previous voltagevc=loadedvcapdischarge(t,vo,C,P)# calc. new voltageifEremain:E=capenergy(C,vcp)# calc. energyreturnt-dt,Eelse:returnt-dt
# Define Rectifier Capacitor Calculator
[docs]defrectifiercap(Iload,fswitch,dVout):r""" Rectifier Capacitor Formula. Returns the capacitance (in Farads) for a needed capacitor in a rectifier configuration given the system frequency (in Hz), the load (in amps) and the desired voltage ripple. .. math:: C=\frac{I_{load}}{f_{switch}*\Delta V_{out}} Parameters ---------- Iload: float The load current that must be met. fswitch: float The switching frequency of the system. dVout: float Desired delta-V on the output. Returns ------- C: float Required capacitance (in Farads) to meet arguments. """C=Iload/(fswitch*dVout)returnC
# Define Inductor Energy Formula
[docs]definductorenergy(L,I):r""" Energy Stored in Inductor Formula. Function to calculate the energy stored in an inductor given the inductance (in Henries) and the current. .. math:: E=\frac{1}{2}*L*I^2 Parameters ---------- L: float Inductance Value (in Henries) I: float Current traveling through inductor. Returns ------- E: float The energy stored in the inductor (in Joules). """return1/2*L*I**2
[docs]definductorcharge(t,Vs,R,L):r""" Charging Inductor Formula. Calculates the Voltage and Current of an inductor that is charging/storing energy. .. math:: V_L = V_s*e^{\frac{-R*t}{L}}// I_L = \frac{V_s}{R}*(1-e^{\frac{-R*t}{L}}) Parameters ---------- t: float Time at which to calculate voltage and current. Vs: float Charging voltage across inductor and resistor. R: float Resistance related to inductor. L: float Inductance value in Henries. Returns ------- Vl: float Voltage across inductor at time t. Il: float Current through inductor at time t. """Vl=Vs*_np.exp(-R*t/L)Il=Vs/R*(1-_np.exp(-R*t/L))returnVl,Il
# Define Inductor Discharge Function
[docs]definductordischarge(t,Io,R,L):r""" Discharging Inductor Formula. Calculates the Voltage and Current of an inductor that is discharging its stored energy. .. math:: I_L=I_0*e^{\frac{-R*t}{L}}// V_L=I_0*R*(1-e^{\frac{-R*t}{L}}) Parameters ---------- t: float Time at which to calculate voltage and current. Io: float Initial current traveling through inductor. R: float Resistance being discharged to. L: float Inductance value in Henries. Returns ------- Vl: float Voltage across inductor at time t. Il: float Current through inductor at time t. """Il=Io*_np.exp(-R*t/L)Vl=Io*R*(1-_np.exp(-R*t/L))return(Vl,Il)
[docs]defair_core_inductance(d:float,coil_l:float,n:int):r""" Compute Inductance of Air Core Inductor. Air core inductors that consist of a coil of conducting wire with no core. They are used in all sorts of electronic devices like radios and computers. .. math:: \frac{d^2 \cdot n^2}{457418\cdot d + 1016127\cdot l} Parameters ---------- d: float Coil diameter, measured in meters coil_l: float Coil length, measured in meters n: int Number of inductor turns Returns ------- float Inductance of air core inductor in Henry's (H) """k1=(1000*d**2)*n**2k2=(457418*d)+(1016127*coil_l)returnk1/k2
[docs]definductive_voltdiv(Vin=None,Vout=None,L1=None,L2=None,find=''):r""" Inductive voltage divider. Inductive voltage divider Inductive voltage dividers are made out of two inductors. One of the inductors is connected from the input to the output and the other one is connected from the output to ground. You can also use other components like resistors and inductors. .. math:: V_{out} = \frac{V_{in}*L1}{L1+L2} .. image:: /static/inductive-voltage-divider-circuit.png Parameters ---------- Vin: float, optional The input voltage for the system, default=None Vout: float, optional The output voltage for the system, default=None L1: float,optional Value of the inductor above the output voltage, default=None L2: float,optional Value of the inductor below the output voltage, default=None find: str, optional Control argument to specify which value should be returned. Returns ------- Vin: float, optional The input voltage for the system, default=None Vout: float, optional The output voltage for the system, default=None L1: float,optional Value of the inductor above the output voltage, default=None L2: float,optional Value of the inductor below the output voltage """ifVinisnotNoneandL1isnotNoneandL2isnotNone:Vout=(Vin*L1)/(L1+L2)elifVoutisnotNoneandL1isnotNoneandL2isnotNone:Vin=(Vout)*(L1+L2)/(L1)elifVinisnotNoneandVoutisnotNoneandL2isnotNone:L1=L2*(Vin-Vout)/(Vout)elifVinisnotNoneandVoutisnotNoneandL1isnotNone:L2=L1*Vout/(Vin-Vout)else:raiseValueError("ERROR: Invalid Parameters or too few"+" parameters given to calculate.")find=find.lower()iffind=='vin':returnVineliffind=='vout':returnVouteliffind=='l1':returnL1eliffind=='l2':returnL2else:returnVin,Vout,L1,L2