%pylab notebook
%precision 1

"""
Explanation: Excercises Electric Machinery Fundamentals
Chapter 4
Problem 4-8
End of explanation
"""


S   = 200e6 # [VA]
Vl  =  12e3 # [V]
PF  = 0.85  # lagging
fse = 50 # [Hz]
p   = 20
ra = 0.1 # [pu]
xs = 0.9 # [pu]

"""
Explanation: Description
A 200-MVA, 12-kV, 0.85-PF-lagging, 50-Hz, 20-pole, Y-connected water turbine generator has a per-unit synchronous reactance of 0.9 and a per-unit armature resistance of 0.1. This generator is operating in parallel with a large power system (infinite bus).
End of explanation
"""


n_sync = 120*fse / p
print('''
n_sync = {:.0f} r/min
=================='''.format(n_sync))

"""
Explanation: (a)

What is the speed of rotation of this generator’s shaft?

(b)

What is the magnitude of the internal generated voltage $E_A$ at rated conditions?

(c)

What is the torque angle of the generator at rated conditions?

(d)

What are the values of the generator’s synchronous reactance and armature resistance in ohms?

(e)
If the field current is held constant, 

What is the maximum power possible out of this generator?
How much reserve power or torque does this generator have at full load?

(f)
At the absolute maximum power possible,

How much reactive power will this generator be supplying or consuming? 
Sketch the corresponding phasor diagram. (Assume $I_F$ is still unchanged.)

SOLUTION
(a)
The speed of rotation of this generator’s shaft is:
$$n_\text{sync} = \frac{120f_{se}}{P}$$
End of explanation
"""


vphase = 1.0

"""
Explanation: (b)
The per-unit phase voltage at rated conditions is $\vec{v}_\phi = 1.0 \angle 0^\circ$
End of explanation
"""


ia_angle = -arccos(PF)  # negative because of lagging PF
ia = 1.0 * (cos(ia_angle) + sin(ia_angle)*1j)
print('ia = {:.1f} pu ∠{:.1f}°'.format(abs(ia), ia_angle/pi*180))

"""
Explanation: and the per-unit phase current $\vec{i}_A$ at rated conditions is
End of explanation
"""


ea = vphase + ra*ia + xs*1j*ia
ea_angle = arctan(ea.imag/ea.real)
print('ea = {:.2f} pu ∠{:.1f}°'.format(abs(ea), ea_angle/pi*180))

"""
Explanation: , so the per-unit internal generated voltage is:
$$\vec{e_A} = \vec{v_\phi} + r_A\vec{i_A} + jx_S\vec{I_A}$$
End of explanation
"""


Vphase_base = Vl / sqrt(3)
print('Vphase_base = {:.0f}V'.format(Vphase_base))

"""
Explanation: The base phase voltage is:
End of explanation
"""


EA = ea * Vphase_base
print('''
EA = {:.0f} V ∠{:.1f}°
==================='''.format(abs(EA), ea_angle/pi*180))

"""
Explanation: so the internal generated voltage is:
End of explanation
"""


delta = ea_angle
print('''
δ = {:.1f}°
========='''.format(delta/pi *180))

"""
Explanation: (c)
The torque angle of the generator is:
End of explanation
"""


Zbase = (3*Vphase_base**2) / S
print('Zbase = {:.2f} Ω'.format(Zbase))

"""
Explanation: (d)
The base impedance of the generator is:
$$Z_\text{base} = \frac{3V^2_{\phi_\text{base}}}{S_\text{base}}$$
End of explanation
"""


Xs = xs * Zbase
print('''
Xs = {:.3f} Ω
============'''.format(Xs))

"""
Explanation: Therefore the synchronous reactance is:
End of explanation
"""


Ra = ra * Zbase
print('''
Ra = {:.3f} Ω
============'''.format(Ra))

"""
Explanation: and the armature resistance is:
End of explanation
"""


Pmax = (3*Vphase_base*abs(EA)) / Xs * sin(pi/2)
print('''
Pmax = {:.0f} MW
============='''.format(Pmax/1e6))

"""
Explanation: (e)
If the field current is held constant (and the armature resistance is ignored), the power out of this generator is given by:
$$P = \frac{3V_\phi E_A}{X_S}\sin{\delta}$$
The max power is given by:
$$P_\text{max} = \frac{3V_\phi E_A}{X_S}\sin{90°}$$
End of explanation
"""


P = S*PF
P/1e6

"""
Explanation: Since the full load power is:
End of explanation
"""


P/Pmax*100

"""
Explanation: MW, this generator is supplying
End of explanation
"""


EA_angle_f = pi / 2 # [rad]
EA_f = abs(EA) * (cos(EA_angle_f) + sin(EA_angle_f)*1j)
Ia_f = (EA_f - Vphase_base) / (Ra + Xs *1j)
Ia_angle_f = arctan(Ia_f.imag/Ia_f.real)
print('Ia_f = {:.0f} A ∠{:.1f}°'.format(abs(Ia_f), Ia_angle_f/pi*180))

"""
Explanation: percent of the maximum possible power at full load conditions.
(f)
At the maximum power possible, the torque angle $\delta =  90^\circ$, so the phasor $\vec{E}_A$ will be at an angle of 90°, and the current flowing will be:
$$\vec{E}A = \vec{V}\phi + R_A\vec{I}A + jX_S\vec{I}_A$$
$$\vec{I}_A = \frac{\vec{E}_A - \vec{V}\phi}{R_A + jX_S}$$
End of explanation
"""


Ia_angle_f/pi*180

"""
Explanation: The impedance angle $\theta$ is:
End of explanation
"""


Q = 3 * Vphase_base * abs(Ia_f) * sin(Ia_angle_f)
print('''
Q = {:.0f} var
==========='''.format(Q/1e6))

"""
Explanation: degrees, and the reactive power supplied by the generator is:
$$Q = 3V_\phi I_A\sin{\theta}$$
End of explanation
"""


