from seuif97 import *

# State  1
p1 = 8.0  # in MPa
t1 = px2t(p1, 1)
h1 = px2h(p1, 1)          # h1 = 2758.0    From table A-3  kj/kg
s1 = px2s(p1, 1)          # s1 = 5.7432    From table A-3  kj/kg.k

# State  2 ,p2=0.008
p2 = 0.008
s2 = s1
t2 = ps2t(p2, s2)
h2 = ps2h(p2, s2)

# State 3 is saturated liquid at 0.008 MPa
p3 = 0.008
t3 = px2t(p3, 0)
h3 = px2h(p3, 0)  # kj/kg
s3 = px2s(p3, 0)

# State 4
p4 = p1
s4 = s3
h4 = ps2h(p4, s4)
t4 = ps2h(p4, s4)

"""
Explanation: The  Examples Rankine Cycle 8.1,8.2
Michael J. Moran, Howard N. Shapiro, Daisie D. Boettner, Margaret B. Bailey. Fundamentals of Engineering Thermodynamics(7th Edition). John Wiley & Sons, Inc. 2011
Chapter 8 : Vapor Power Systems:
1 EXAMPLE 8.1 Analyzing an Ideal Rankine Cycle P438
2 EXAMPLE 8.2 Analyzing a Rankine Cycle with Irreversibilities P444
1 Example 8.1: Analyzing an Ideal Rankine Cycle
Steam is the working fluid in an ideal Rankine cycle. 
Saturated vapor enters the turbine at 8.0 MPa and saturated liquid exits the condenser at a pressure of 0.008 MPa. 
The net power output of the cycle is 100 MW.


Process 1–2: Isentropic expansion of the working fluid through the turbine from saturated vapor at state 1 to the condenser pressure.
Process 2–3: Heat transfer from the working fluid as it flows at constant pressure
through the condenser with saturated liquid at state 3.
Process 3–4: Isentropic compression in the pump to state 4 in the compressed liquid region.
Process 4–1: Heat transfer to the working fluid as it flows at constant pressure through the boiler to complete the cycle.

Determine for the cycle
(a) the thermal efficiency,
(b) the back work ratio, 
(c) the mass flow rate of the steam,in kg/h,
(d) the rate of heat transfer, Qin, into the working fluid as it passes through the boiler, in MW,
(e) the rate of heat transfer, Qout, from the condensing steam as it passes through the condenser, in MW,
(f) the mass flow rate of the condenser cooling water, in kg/h, if cooling water enters the condenser at 15°C and exits at 35°C.
Engineering Model:


1 Each component of the cycle is analyzed as a control volume at steady state. The control volumes are shown on the accompanying sketch by dashed lines.


2 All processes of the working fluid are internally reversible.


3 The turbine and pump operate adiabatically.


4 Kinetic and potential energy effects are negligible.


5 Saturated vapor enters the turbine. Condensate exits the condenser as saturated liquid.


To begin the analysis, we fix each of the principal states（1，2，3，4) located on the accompanying schematic and T–s diagrams.
1.1 States
End of explanation
"""


# Part(a)
# Mass and energy rate balances for control volumes
# around the turbine and pump give, respectively

# turbine
wtdot = h1 - h2
# pump
wpdot = h4-h3

# The rate of heat transfer to the working fluid as it passes
# through the boiler is determined using mass and energy rate balances as
qindot = h1-h4

# thermal efficiency
eta = (wtdot-wpdot)/qindot

# Result for part a
print('(a) The thermal efficiency for the cycle is {:>.2f}%'.format(eta*100))

"""
Explanation: 1..2  Analysis the Cycle
(a) The thermal efficiency
The net power developed by the cycle is
$\dot{W}_{cycle}=\dot{W}_t-\dot{W}_p$
Mass and energy rate balances for control volumes around the turbine and pump give,respectively
$\frac{\dot{W}_t}{\dot{m}}=h_1-h_2$
$\frac{\dot{W}_p}{\dot{m}}=h_4-h_3$
where $\dot{m}$ is the mass flow rate of the steam. The rate of heat transfer to the working fluid as it passes through the boiler is determined using mass and energy rate balances as
$\frac{\dot{Q}_{in}}{\dot{m}}=h_1-h_4$
The thermal efficiency is then
$\eta=\frac{\dot{W}t-\dot{W}_p}{\dot{Q}{in}}=\frac{(h_1-h_2)-(h_4-h_3)}{h_1-h_4}$
End of explanation
"""


# Part(b)
# back work ratio：bwr, defined as the ratio of the pump work input to the work
# developed by the turbine.
bwr = wpdot/wtdot                                    #

# Result
print('(b) The back work ratio is {:>.2f}%'.format(bwr*100))

# Part(c)
Wcycledot = 100.00     # the net power output of the cycle in MW
mdot = (Wcycledot*10**3*3600)/((h1-h2)-(h4-h3))       # mass flow rate in kg/h

# Result
print('(c) The mass flow rate of the steam is {:>.2f}kg/h'.format(mdot))

# Part(d)
Qindot = mdot*qindot/(3600*10**3)                     # in MW

# Results
print('(d) The rate of heat transfer Qindot into the working fluid as' +
      ' it passes through the boiler is {:>.2f}MW'.format(Qindot))

# Part(e)
Qoutdot = mdot*(h2-h3)/(3600*10**3)                   # in MW

# Results
print('(e) The rate of heat transfer Qoutdot from the condensing steam ' +
      'as it passes through the condenser is {:>.2f}MW.'.format(Qoutdot))

# Part(f)

# Given:
tcwin = 15
tcwout = 35

hcwout = tx2h(tcwout, 0)   # From table A-2,hcwout= 146.68  kj/kg

hcwin = tx2h(tcwin, 0)    # hcwin 62.99
mcwdot = (Qoutdot*10**3*3600)/(hcwout-hcwin)          # in kg/h

# Results
print('(f) The mass flow rate of the condenser cooling water is {:>.2f}kg/h.'.format(mcwdot))

"""
Explanation: (b) The back work ratio is
$bwr=\frac{\dot{W}_p}{\dot{W}_t}=\frac{h_4-h_3}{h_1-h_2}$
(c) The mass flow rate of the steam can be obtained from the expression for the net power given in part (a)
$\dot{m}=\frac{\dot{W}_{cycle}}{(h_1-h_2)-(h_4-h_3)}$
(d) With the expression for $\dot{Q}_{in}$ in from part (a) and previously determined specific enthalpy values
$\dot{Q}_{in}=\dot{m}(h_1-h_4)$
(e) Mass and energy rate balances applied to a control volume enclosing the steam side of the condenser give
$\dot{Q}_{out}=\dot{m}(h_2-h_3)$
(f) Taking a control volume around the condenser, the mass and energy rate balances give at steady state
$\require{cancel} 0=\dot{\cancel{Q}}^{0}{cv}-\dot{\cancel{w}}^{0}{cv}+\dot{m}{cw}(h{cw,in}-h_{cw,out})+\dot{m}(h_2-h_3)$
where $\dot{m}{cw}$ is the mass flow rate of the cooling water. Solving for $\dot{m}{cw}$
$\dot{m}{cw}=\frac{\dot{m}(h_2-h_3)}{h{cw,in}-h_{cw,out}}$
End of explanation
"""


from seuif97 import *

# State  1
p1 = 8.0               #  in MPa
t1 =px2t(p1,1) 
h1=px2h(p1,1)          # h1 = 2758.0    From table A-3  kj/kg
s1=px2s(p1,1)          # s1 = 5.7432    From table A-3  kj/kg.k

# State  2 ,p2=0.008
p2=0.008
s2s = s1
h2s=ps2h(p2,s2s)
t2s=ps2t(p2,s2s)
etat_t=0.85
h2=h1-etat_t*(h1-h2s)
t2 =ph2t(p2,h2)     
s2 =ph2s(p2,h2)     

# State 3 is saturated liquid at 0.008 MPa
p3 = 0.008            
t3=px2t(p3,0) 
h3 =px2h(p3,0)                                     #  kj/kg
s3 =px2s(p3,0)     

#State 4 
p4 = p1
s4s=s3
h4s =ps2h(p4,s4s)
t4s =ps2t(p4,s4s) 
etat_p=0.85
h4=h3+(h4s-h3)/etat_p
t4 =ph2t(p4,h4)     
s4 =ph2s(p4,h4)

"""
Explanation: 2 Example8.2 :Analyzing a Rankine Cycle with Irreversibilities
Reconsider the vapor power cycle of Example 8.1, but include in the analysis that the turbine and the pump each have an isentropic efficiency of 85%. 
Determine for the modified cycle 


(a) the thermal efficiency, 


(b) the mass flow rate of steam, in kg/h, for a net power output of 100MW, 


(c) the rate of heat transfer $\dot{Q}_{in}$ in into the working fluid as it passes through the boiler, in MW, 


(d) the rate of heat transfer $\dot{Q}_{out}$ out from the condensing steam as it passes through the condenser, in MW, 


(e) the mass flow rate of the condenser cooling water, in kg/h, if cooling water enters the condenser at 15°C and exits as 35°C.


SOLUTION
Known: A vapor power cycle operates with steam as the working fluid. The turbine and pump both have efficiencies of 85%.
Find: Determine the thermal efficiency, the mass flow rate, in kg/h, the rate of heat transfer to the working fluid as it passes through the boiler, in MW, the heat transfer rate from the condensing steam as it passes through thecondenser, in MW, and the mass flow rate of the condenser cooling water, in kg/h.
Engineering Model:


Each component of the cycle is analyzed as a control volume at steady state.


The working fluid passes through the boiler and condenser at constant pressure. Saturated vapor enters the turbine. The condensate is saturated at the condenser exit.


The turbine and pump each operate adiabatically with an efficiency of 85%.


Kinetic and potential energy effects are negligible



Analysis:
Owing to the presence of irreversibilities during the expansion of the steam through the turbine, there is an increase in specific entropy from turbine inlet to exit, as shown on the accompanying T–s diagram. Similarly,there is an increase in specific entropy from pump inlet to exit.
Let us begin the analysis by fixing each of the principal states.
1.2 States
End of explanation
"""


# Part(a)
eta = ((h1-h2)-(h4-h3))/(h1-h4)             # thermal efficiency

# Result for part (a)
print('Thermal efficiency is: {:>.2f}%'.format(100*eta))

# Part(b)
Wcycledot = 100                             # given,a net power output of 100 MW
# Calculations
mdot = (Wcycledot*(10**3)*3600)/((h1-h2)-(h4-h3))
# Result for part (b)
print('The mass flow rate of steam for a net power output of 100 MW is {:>.2f}kg/h'.format(mdot))

# Part(c)
Qindot = mdot*(h1-h4)/(3600 * 10**3)
# Result
print('The rate of heat transfer Qindot into the working fluid as it passes through the boiler, is {:>.2f}MW.'.format(Qindot))

# Part(d)
Qoutdot = mdot*(h2-h3)/(3600*10**3)
# Result
print('The rate of heat transfer Qoutdot from the condensing steam as it passes through the condenser, is {:>.2f}MW.'.format(Qoutdot))

# Part(e)
tcwin = 15
tcwout = 35
hcwout = tx2h(tcwout, 0)   # From table A-2,hcwout= 146.68  kj/kg
hcwin = tx2h(tcwin, 0)    # hcwin 62.99
mcwdot = (Qoutdot*10**3*3600)/(hcwout-hcwin)
# Result
print('The mass flow rate of the condenser cooling water, is {:>.2f}kg/h'.format(mcwdot))

"""
Explanation: 2.2 Analysis the Cycle
End of explanation
"""


%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(10.0,5.0))

# saturated vapor and liquid entropy lines 
npt = np.linspace(10,647.096-273.15,200) # range of temperatures
svap = [s for s in [tx2s(t, 1) for t in npt]]
sliq = [s for s in [tx2s(t, 0) for t in npt]]
plt.plot(svap, npt, 'r-')
plt.plot(sliq, npt, 'b-')

t=[t1,t2s,t3,t4s+15]
s=[s1,s2s,s3,s4s]

# point 5
t.append(px2t(p1,0))
s.append(px2s(p1,0))

t.append(t1)
s.append(s1)

plt.plot(s, t, 'ko-')

tb=[t1,t2]
sb=[s1,s2]
plt.plot(sb, tb, 'k--')
tist=[t2,t2s]
sist=[s2,s2s]
plt.plot(sist, tist, 'ko-')

sp=[s3,s3+0.3]
tp=[t3,ps2t(p4,s3+0.3)+15]
plt.plot(sp, tp, 'ko--')

tist=[t2,t2s]
sist=[s2,px2s(p2,1)]
plt.plot(sist, tist, 'g-')

plt.xlabel('Entropy (kJ/(kg K)')
plt.ylabel('Temperature (°C)')
plt.grid()

"""
Explanation: 1.2.3 T-S   Diagram
End of explanation
"""


