In [1]:

```
# -*- coding: utf8 -*-
from __future__ import division
#Example: 8.1
''' Consider a Carnot-cycle heat pump with R-134a as the working ﬂuid. Heat is absorbed
into the R-134a at 0◦C, during which process it changes from a two-phase state to saturated
vapor. The heat is rejected from the R-134a at 60◦C and ends up as saturated liquid. Find
the pressure after compression, before the heat rejection process, and determine the COP
for the cycle.'''
#Variable Declaration:
Th = 60 #temperature at which heat is rejected from R-134a
Tl = 0 #temperature at which heat is absorbed into the R-134a
s1 = 1.7262 #specific entropy at 0 Celsius
s3 = 1.2857 #specific entropy at 60 celsius
s4 = s3 #process of state change from 3-4 is isentropic
#Calculations:
s2 = s1 #process of state change from 1-2 is isentropic
P2 = 1400+(1600-1400)*(1.7262-1.736)/(1.7135-1.736) #pressure after compression in kPa
B = (Th+273)/(Th-Tl)#coefficient of performance of refrigerator
#Results:
print "Pressure after compression is P2: ",round(P2,1) ,'KPa'
print "Coefficient of performance of refrigerator: ",round(B,2)
```

In [2]:

```
# -*- coding: utf8 -*-
from __future__ import division
#Example: 8.2
''' A cylinder/piston setup contains 1 L of saturated liquid refrigerant R-410a at 20◦C.
The piston now slowly expands, maintaining constant temperature to a ﬁnal pressure of
400 kPa in a reversible process. Calculate the work and heat transfer required to accom-
plish this process'''
#Variable Declaration:
u1 = 87.94 #specific internal energy of R-12 at state 1 in kJ/kg
u2 = 276.44 #specific internal energy of R-12 at state 2 in kJ/kg
s1 = 0.3357 #specific entropy at state 1 in kJ/kg-K
s2 = 1.2108 #specific entropy at state 2 in kJ/kg-K
V = 0.001 #volume of saturated liquid in m**3
v1 = 0.000923 #specific volume in m**3/kg
T = 20 #temperature of liquid in celsius
#Calculations:
m = V/v1 #mass of saturated liquid in kg
Q12 = m*(T+273.15)*(s2-s1) #heat transfer in kJ to accomplish the process
W12 = m*(u1-u2)+Q12 #work required to accomplish the process
#Results
print "Work required to accomplish the process: ",round(W12,1),'KJ'
print "Heat transfer: ",round(Q12,1),'KJ'
```

In [3]:

```
# -*- coding: utf8 -*-
from __future__ import division
from math import log
#Example: 8.3
''' One kilogram of liquid water is heated from 20◦C to 90◦C. Calculate the entropy change,
assuming constant speciﬁc heat, and compare the result with that found when using the
steam tables.
Control mass: Water.
Initial and ﬁnal states: Known.
Model: Constant speciﬁc heat, value at room temperature.'''
#Variable Declaration:
C = 4.184 # specific heat of water in kJ/kg-K
T1 = 20 #initial temperature of water in celsius
T2 = 90 #final temperature of water in celsius
#Calculations:
dS1 = C*log((T2+273.2)/(T1+273.2)) #change in entropy in kJ/kg-K
dS2 = 1.1925-0.2966 #in kJ/kg-K using steam tables
#Results
print "Change in entropy assuming constant specific heat: ",round(dS1,4),'KJ/Kg-K'
print "Using steam table: ",round(dS2,4) ,'KJ/Kg-K'
```

In [4]:

```
# -*- coding: utf8 -*-
from __future__ import division
from math import log
from sympy import symbols, integrate
#Example: 8.4
''' Consider Example 5.7, in which oxygen is heated from 300 to 1500 K. Assume that during
this process the pressure dropped from 200 to 150 kPa. Calculate the change in entropy
per kilogram.'''
#Variable Declaration:
T1 = 300 #initial temperature in kelvins
T2 = 1500 #final temperature in kelvins
P1 = 200 #initial pressure in kPa
P2 = 150 #final pressure in kPa
R = 0.2598 # in kJ/kg-K
Cp = 0.922 #specific heat in kJ/kg-K at constant pressure
dsT2 = 8.0649 #in kJ/kg-K
dsT1 = 6.4168 #in kJ/kg-K
x = symbols('x')#Symbolic representation of theta
#Calculations:
dS1 = dsT2-dsT1-R*log(P2/P1) #entropy change calculated using ideal gas tables
dS2 = integrate(0.88/x-0.0001+0.54*x-0.33*x**2,(x,0.3,1.5))-R*log(P2/P1) #entropy change calculated using empirical equation
dS3 = Cp*log(T2/T1)-R*log(P2/P1) #entropy change assuming constant specific heat in kJ/kg-K
dS4 = 1.0767*log(T2/T1)+0.0747 #entropy change assuming specific heat is constant at its value at 990K
#Results
print "Change in entropy using ideal gas tables: ",round(dS1,4),'KJ/Kg-K'
print "Change in entropy using empirical equation: ",round(dS2,4),'KJ/Kg-K'
print "Change in entropy using the value of specific heat at 300K: ",round(dS3,4),'KJ/Kg-K'
print "Change in entropy assuming specific heat is constant at its value at 900K: ",round(dS4,4),'KJ/Kg-K'
```

In [5]:

```
# -*- coding: utf8 -*-
from __future__ import division
from math import log
#Example: 8.5
''' Calculate the change in entropy per kilogram as air is heated from 300 to 600 K while
pressure drops from 400 to 300 kPa. Assume:
1. Constant speciﬁc heat.
2. Variable speciﬁc heat.'''
#Variable Declaration:
Cp = 1.004 #specific heat at constant pressure in kJ/kg-K
R = 0.287 #gas constant in kJ/kg-K
P1 = 400 #initial pressure in kPa
P2 = 300 #final pressure in kPa
T1 = 300 #initial temperature in K
T2 = 600 #final temperature in K
s1 = 6.8693 #specific entropy at T1
s2 = 7.5764 #specific entropy at T2
#Calculations:
dS1 = Cp*log(T2/T1)-R*log(P2/P1)#entropy change assuming constant specific heat
dS2 = s2-s1-R*log(P2/P1) #entropy change assuming variable specific heat
#Results
print "Entropy change assuming constant specific heat: ",round(dS1,4),'KJ/Kg-K'
print "Assuming variable specific heat: ",round(dS2,4),'KJ/Kg-K'
```

In [6]:

```
# -*- coding: utf8 -*-
from __future__ import division
from math import log
#Example: 8.6
''' One kilogram of air is contained in a cylinder ﬁtted with a piston at a pressure of 400
kPa and a temperature of 600 K. The air is expanded to 150 kPa in a reversible adiabatic
process. Calculate the work done by the air.
Control mass: Air.
Initial state: P 1 , T 1 ; state 1 ﬁxed.
Final state: P 2 .
Process: Reversible and adiabatic.
Model: Ideal gas and air tables, Table A.7.'''
#Variable Declaration:
T1 = 600 #initial temperature of air in K
P1 = 400 #intial pressure of air in kPa
P2 = 150 #final pressure in kPa
u1 = 435.10 #specific internal energy at temperature T1 in kJ/kg
sT1 = 7.5764 #specific entropy at temperature T1 in kJ/kg-K
R = 0.287 #gas constant in kJ/kg-K
ds = 0
T2 = 457 #final temperature in K
u2 = 328.14 #specific internal energy at temperature T2 in kJ/kg
#Calculations:
sT2 = ds+sT1+R*log(P2/P1)#specific entropy at temperature T2 in kJ/kg-K
w = u1-u2 #work done by air in kJ/kg
#Results
print "Work done by air: ",round(w,2),'KJ/Kg'
```

In [7]:

```
# -*- coding: utf8 -*-
from __future__ import division
#Example: 8.7
''' In a reversible process, nitrogen is compressed in a cylinder from 100 kPa and 20◦
C to 500 kPa. During this compression process, the relation between pressure and volume is
PV 1.3 = constant. Calculate the work and heat transfer per kilogram, and show this process
on P–v and T–s diagrams.
Control mass: Nitrogen.
Initial state: P 1 , T 1 ; state 1 known.
Final state: P 2 .
Process: Reversible, polytropic with exponent n < k.
Diagram: Fig. 8.14.
Model: Ideal gas, constant speciﬁc heat—value at 300 K.'''
#Variable Declaration:
P2 = 500 #final pressure in cylinder in kPa
P1 = 100 #initial pressure in cylinder in kPa
T1 = 20+273.2 #initial temperature inside cylinder in Kelvins
n = 1.3
R = 0.2968 #gas constant in kJ/kg-K
Cvo = 0.745 #specific heat at constant volume in kJ/kg-K
#Calculations:
T2 = (T1)*(P2/P1)**((n-1)/n)#final temperature inside cylinder in K
w12 = R*(T2-T1)/(1-n) #work in kJ/kg
q12 = Cvo*(T2-T1)+w12 #heat transfer in kJ/kg
#Results
print "Work done is w12: ",round(w12,1),"KJ/Kg"
print "Heat transfer are: ",round(q12,1),"KJ/Kg"
```

In [8]:

```
# -*- coding: utf8 -*-
from __future__ import division
#Example: 8.8
'''Consider an electric space heater that converts 1 kW of electric power into a heat ﬂux of
1 kW delivered at 600 K from the hot wire surface. Let us look at the process of the energy
conversion from electricity to heat transfer and ﬁnd the rate of total entropy generation.
Control mass: The electric heater wire.
State: Constant wire temperature 600 K.'''
#Variable Declaration:
m = 1 #mass of saturated water vapour
sfg = 6.0480 #in kJ/K
T = 25 #temperature of surrounding air in celsius
hfg = 2257.0 #in kJ/kg
#Calculations:
dScm = -m*sfg #change in entropy of control mass in kJ/K
Qsurr = m*hfg #heat transferred to surr in kJ
dSsurr = Qsurr/(T+273.15)#in kJ/K
dSnet = dScm+dSsurr #net increase in entropy in kJ/K
#Results:
print "Increase in entropy of water plus surr:",round(dSnet,4) ,'KJ/K'
```

In [9]:

```
# -*- coding: utf8 -*-
from __future__ import division
#Example: 8.9
''' Consider an electric space heater that converts 1 kW of electric power into a heat ﬂux of
1 kW delivered at 600 K from the hot wire surface. Let us look at the process of the energy
conversion from electricity to heat transfer and ﬁnd the rate of total entropy generation.
Control mass: The electric heater wire.
State: Constant wire temperature 600 K.'''
#Variable Declaration:
Qout = 1 #value of heat flux generated by 1kW of electric power
T = 600 #temperature of hot wire surface in K
#Calculations:
Sgen = Qout/T #entropy generation in kW/K
#Results
print "Entropy generation is: ",round(Sgen,5),'KW/K'
```

In [10]:

```
# -*- coding: utf8 -*-
from __future__ import division
#Example: 8.10
''' Consider a modern air conditioner using R-410a working in heat pump mode, as shown in
Fig. 8.21. It has a COP of 4 with 10 kW of power input. The cold side is buried underground,
where it is 8◦C, and the hot side is a house kept at 21◦C. For simplicity, assume that the
cycle has a high temperature of 50◦C and a low temperature of −10◦C (recall Section
7.10). We would like to know where entropy is generated associated with the heat pump,
assuming steady-state operation.'''
#Variable Declaration:
B = 4 #COP of air conditioner
W = 10 #power input of air conditioner in kW
Thigh = 323 #in Kelvin
Tlow = 263 #in Kelvin
Tl = 281 #in K
Th = 294 #in K
#Calculations:
Qh = B*W #in kW
Ql = Qh-W #in kW
SgenHP = (Qh*1000/Thigh)-(Ql*1000/Tlow) #in W/K
SgenCV1 = Ql*1000/Tlow-Ql*1000/Tl #in W/K
SgenCV2 = Qh*1000/Th-Qh*1000/Thigh #in W/K
SgenTOT = SgenCV1+SgenCV2+SgenHP #in W/K
#Results
print "Total entropy generated: ",round(SgenTOT,1),'W/K'
```