In [1]:

```
from __future__ import division
from math import log
#Variable Declaration:
p1 = 5 #Initial pressure(in bar):
T1 = 300 #Initial temperature(in K):
p2 = 2 #Final pressure(in bar):
Cp = 1.004 #Cp for air(in kJ/kg.K):
R = 0.287 #Gas constant for air(in kJ/kg.K):
#Calculation:
T2 = T1 #As it is a throttling process:
dS = Cp*log(T2/T1)-R*log(p2/p1) #Change in entropy(in kJ/kg.K):
#Results:
print "Change in entropy: ",round(dS,3),"KJ/Kg.K"
```

In [2]:

```
from __future__ import division
from sympy import *
#Variable Declaration:
m = 5 #Mass of water(in kg):
T1 = 27+273.16 #Atmospheric temperature(in K):
T2 = 100+273.16 #Temperature of evaporation(in K):
T3 = 400+273.16 #Temperature at which steam is generated(in K):
Cp = 4.2 #Specific heat of water(in kJ/kg.K):
q2 = 2260 #Heat of vaporisation(in kJ/kg):
T = symbols('T') #Symbolic variable for Temperature:
R = 8.314 #Universal gas constant:
ms = 18 #Molar mass of steam (in g):
#Calculation:
Q1 = m*Cp*(T2-T1) #Heat added for increasing water temperature from 27C to 100C(in kJ):
dS1 = Q1/T1 #Entropy change during water temperature rise(in kJ/K):
Q2 = m*q2 #Heat of vaporization(in kJ):
dS2 = Q2/T2 #Entropy change during water to steam change(in kJ/K):
Rs = round(R/ms,3) #Value of R for steam (KJ/Kg.K)
CP = Rs*(3.5+1.2*T+0.14*T**2) #Molar heat capacity at constant pressure for steam(J/Kg.K)
dQ = m*10**-3*CP
dS3 = round(integrate(apart(dQ/T),(T,T2,T3)),2)
dS = dS1+dS2+dS3 #Total entropy change(in kJ/K):
#Results:
print "!--- Small differences in error is due to integration approximation in coding ---!"
print "Total change in entropy of universe: ",round(dS,2),"KJ/K"
```

In [3]:

```
from math import log
#Variable Declaration:
p1 = 125 #Initial pressure(in kPa):
p2 = 375 #Final pressure(in kPa):
T1 = 27+273 #Intial temperature(in K):
R = 8.314/32 #Gas constant for oxygen(in kJ/kg.K):
#Calculation:
dS = -R*log(p2/p1) #Change in entropy(in kJ/kg.K):
#Results:
print "Change in entropy: ",round(dS,3),"KJ/Kg.K"
```

In [4]:

```
from __future__ import division
from math import log
#Variable Declaration:
m = 1 #Mass of the block(in kg):
T1 = 150+273.15 #Temperature of the block(in K):
T2 = 25+273.15 #Temperature of the sea(in K):
C = 0.393 #Heat capacity of copper(in kJ/kg.K):
#Calculation:
dSb = m*C*log(T2/T1) #Change in entropy of block(in kJ/K):
Q = m*C*(T1-T2) #Heat lost by water(in kJ): #Heat lost by the block will be equal to heat gained by the water
dSw = round(Q/T2,3) #Change in entropy of water(in kJ/K):
dSu = dSb+dSw #Entropy change of universe(in kJ/K):
#Results:
print "Change in entropy of universe: ",round(dSu*10**3,1),"J/K"
```

In [5]:

```
from __future__ import division
#Variable Declaration:
m = 1 #Mass of the block(in kg):
T = 27+273 #Temperature of the block(in K):
h = 200 #Height(in m):
s = 0.393 #Heat capacity for copper(in kJ/kg.K):
g = 9.81 #Acceleration due to gravity(in m/s**2):
#Calculation:
PE = m*g*h #Change in potential energy(in J):
Q = PE #In this case:
dSu = Q/T #Change in entropy of universe(in J/kg.K):
#Results:
print "Change in entropy of universe: " ,round(dSu,2),"J/kg.K"
```

In [6]:

```
from __future__ import division
from math import log
#Variable Declaration:
m1 = 1 #Mass(in kg) of Block 1:
T1 = 150+273 #Temperature(in K):
C1 = 0.393 #Specific heat(in kJ/kg.K):
m2 = 0.5 #Mass(in kg) of Block 2:
T2 = 0+273 #Temperature(in K):
C2 = 0.381 #Specific heat(in kJ/kg.K):
#Calculation:
Tf = (m1*C1*T1+m2*C2*T2)/(m1*C1+m2*C2) #Final temperature(in K):
dS1 = m1*C1*log(Tf/T1) #Entropy change in block 1(in kJ/K):
dS2 = m2*C2*log(Tf/T2) #Entropy change in block 2(in kJ/K):
dS = dS1+dS2 #Total entropy change(in kJ/K):
#Results:
print "Change in entropy of universe: ",round(dS,4),"J/K"
```

In [8]:

```
from __future__ import division
#Variable Declaration:
T1 = 1800 #Maximum temperature(in K):
T2 = 300 #Minimum temperature(in K):
Q1 = 5 #Rate at which heat is added(in MW):
W = 2 #Work output(in MW):
#Calculation:
Q2 = Q1-W #Heat rejected(in MW):
dSg = (-Q1/T1+Q2/T2) #Entropy generated(in MW/K):
w = T2*dSg #Work lost(in MW):
#Results:
print "Work lost: ",round(w,2),"MW"
```

In [9]:

```
from __future__ import division
from sympy import *
#Variable Declaration:
T1 = 500 #Temperature of the system(in K):
T2 = 300 #Temperature of the reservoir(in K):
T = symbols('T') #Symbolic representation of tempreture:
#Calculation:
C = 0.05*T**2 + 0.10*T + 0.085 #Heat Capacity:
Q1 = integrate(C,(T,T2,T1)) #Maximum heat(in J):
dSs =integrate(apart(C/T),(T,T1,T2)) #Entropy change of the system(in J/K):
W = (Q1/T2+dSs)*T2 #Maximum work available(in kJ):
#Results:
print "Maximum work: ",round(W/(10**3),2),"KJ"
```

In [10]:

```
from __future__ import division
from sympy import *
#Variable Declaration:
p1 = 3000 #Initial pressure(in kPa):
v1 = 0.05 #Initial volume(in m**3):
v2 = 0.3 #Final volume(in m**3):
n = 1.4 #Value of n:
dS = 0 #Entropy change:
T,P = symbols('T,P') #Symbolic expressions for T and P respectively
#Calculation:
p2 = round(p1*((v1/v2)**n)) #Final pressure(in MPa):
V = (p1*v1**n/P)**(1/n)
dH = integrate(V,(P,p2,p1)) #Change in enthalpy(in kJ):
#Results:
print "Enthalpy change: ",round(dH,1),"KJ"
```

In [11]:

```
from __future__ import division
from math import log
#Variable Declaration:
m = 2 #Mass of air(in kg):
v1 = 1 #Initial volume(in m**3):
v2 = 10 #Final volume(in m**3):
R = 287 #Gas const(in J/kg.K):
#Calculation:
dSa = m*R*log(v2/v1) #Change in entropy of air(in J/K):
dSs = 0 #During free expansion, entropy change of surroundings(in J/K):
dSu = dSa+dSs #Entropy change of universe(in J/K):
#Results:
print "Entropy change of air: ",round(dSa,2),"J/K"
print "Entropy change of surroundings: ",round(dSs),"J/K"
print "Entropy change of universe: ",round(dSu,2),"J/K"
```

In [12]:

```
from __future__ import division
from math import log
#Variable Declaration:
m = 0.5 #Mass of air(in kg):
p1 = 1.013*10**5 #Initial pressure(in Pa):
p2 = 0.8*10**6 #Final pressure(in Pa):
T1 = 800 #Initial temperature(in K):
n = 1.2 #Index of compression:
r = 1.4 #Adiabatic index of compression:
Cv = 0.71*10**3 #Value of Cv(in J/kg.K):
#Calculation:
T2 = T1*((p2/p1)**((n-1)/n)) #Final temperature(in K):
dS = m*Cv*((n-r)/(n-1))*log(T2/T1) #Total entropy change(in J/K):
#Results:
print "Entropy change: ",abs(round(dS,2)),"J/K"
```

In [16]:

```
from __future__ import division
#Variable Declaration:
Q1 = 500 #Heat supplied by source (Kcal/s)
T1 = 600 #Temperature of source(K):
T2 = 300 #Temperature of sink(K):
def feasibility(Q2):
Y = Q1/T1 - Q2/T2
if(Y>0):
return "Under this condition engine is not possible"
elif (Y<0):
return "Engine is feasible and cycle is irreversible"
elif (Y==0):
return "Engine is feasible and cycle is reversible"
#Results:
print "(i) If heat rejected at 200 Kcal/s then ",feasibility(200)
print "(ii) If heat rejected at 400 Kcal/s then ",feasibility(400)
print "(iii) If heat rejected at 250 Kcal/s then ",feasibility(250)
```

In [17]:

```
from __future__ import division
from math import log
#Variable Declaration:
p1 = 0.5 #Pressure at point 1(in MPa):
T1 = 400 #Temperature at point 1(in K):
p2 = 0.3 #Pressure at point 2(in MPa):
T2 = 350 #Temperature at point 2(in K):
R = 0.287 #Gas constant(in kJ/kg.K):
Cp = 1.004 #Value of Cp(in kJ/kg.K):
#Calculation:
ds = Cp*log(T1/T2)-R*log(p1/p2) #Entropy change(in kJ/kg.K):
#Results:
print "Change in entropy: ",round(ds,5),"KJ/Kg.K"
print "Hence flow occurs from 1 to 2 i.e. from 0.5 MPa, 400 K to 0.3 MPa & 350 K"
```

In [19]:

```
from __future__ import division
#Variable Declaration:
Q12 = 1000 #Heat added in process 1-2(in kJ):
Q34 = 800 #Heat added in process 3-4(in kJ):
T1 = 500 #Temperature at point 1(in K):
T3 = 400 #Temperature at point 3(in K):
T5 = 300 #Temperature at point 5(in K):
#Calculation:
Qt = Q12+Q34 #Total heat added(in kJ):
S12 = Q12/T1 #Entropy change from state 1-2(in kJ/K):
S34 = Q34/T3 #Entropy change from state 3-4(in kJ/K):
S56 = S12+S34 #Entropy change from state 5-6(in kJ/K):
Q56 = T5*S56 #Heat rejected in process 5-6(in kJ):
W = Q12+Q34-Q56 #Net work done(in kJ):
n = W/Qt #Thermal efficiency of the cycle:
#Results:
print "Work done: ",round(W),"KJ"
print "Thermal efficiency: ",round(n*100,2),"%"
```

In [21]:

```
from __future__ import division
from sympy import *
#Variable Declaration:
T1 = 800 #Temperature of the reservoirs(in K):
T2 = 700
T3 = 600
T4 = 320 #Temperature of the sink(in K):
Q2 = 10 #Total heat rejected to the heat sink(in kJ/s):
W = 20 #Work done(in kW):
Q11,Q12,Q13 = symbols('Q11,Q12,Q13')
#Calculation:
Q1 = Q2+W #Total heat added(in kJ/s):
EQ1 = 0.7*Q12 - Q11 #Heat from reservoir 1(in kJ/s) formed as equation:
EQ2 = Q1-1.7*Q12 - Q13 #Heat from reservoir 3(in kJ/s) formed as equation:
EQ3 = Q11/T1 + Q12/T2 +Q13/T3 - Q2/T4 #For reversible engine
result = solve([EQ1,EQ2,EQ3],[Q11,Q12,Q13])
#Results:
print "!!!--There are some error in calculations in the book --!"
print "Heat supplied by reservoir at 800 K: ",round(result[Q11],2),"KJ/s"
print "Heat supplied by reservoir at 700 K: ",round(result[Q12],2),"KJ/s"
print "Heat supplied by reservoir at 600 K: ",round(result[Q13],2),"KJ/s"
```

In [22]:

```
from __future__ import division
from math import log
#Variable Declaration:
v1 = 0.04 #Volume of the chamber(in m**3):
p1 = 10 #Initial pressure(in bar):
T1 = 25+273 #Initial temperature(in K):
R = 0.287 #Gas constant(in kJ/kg.K):
Cv = 0.71 #Value of Cv(in kJ/kg.K):
#Calculation:
T2 = T1 #Final temperature(in K):
v2 = 2*v1 #Final volume(in m**3):
p2 = p1*v1/v2 #Final pressure(in bar):
m = p1*10**2*v1/(R*T1) #Initial mass(in kg):
dS = m*R*log(v2/v1)+m*Cv*log(T2/T1) #Change in entropy(in kJ/K):
#Results:
print "Entropy change: ",round(dS,5), "KJ/K"
print "The process is irreversible"
```

In [23]:

```
from __future__ import division
from math import log
#Variable Declaration:
ma = 0.6 #Mass in tank A(in kg):
mb = 1 #Mass in tank B(in kg):
Ta = 90+273 #Temperature in tank A(in K):
Tb = 45+273 #Temperature in tank B(in K):
pa = 1 #Pressure in tank A(in bar):
pb = 2 #Pressure in tank B(in bar):
R = 0.287 #Gas constant(in kJ/kg.K):
Cp = 1.005 #Value of Cp(in kJ/kg.K):
#Calculation:
Tf = (ma*Ta+mb*Tb)/(ma+mb) #Final temperature(in K):
va = ma*R*Ta/pa #Volume of tank A(in m**3):
vb = mb*R*Tb/pb #Volume of tank B(in m**3):
pf = (ma+mb)*R*Tf/(va+vb) #Final pressure(in kPa):
dS = ma*(Cp*log(Tf/Ta)-R*log(pf/pa))+mb*(Cp*log(Tf/Tb)-R*log(pf/pb)) #Entropy change(in kJ/K):
#Results:
print "Final Pressure: ",round(pf,2),"KPa"
print "Entropy produced: ",round(dS,5),"KJ/K"
```

In [24]:

```
from __future__ import division
from math import log
#Variable Declaration:
va = 4 #Volume of the tanks(in m**3):
vb = 4
vc = 4
pa = 6 #Pressure in tank A(in bar):
Ta = 90+273 #Temperature in tank A(in K):
pb = 3 #Pressure in tank B(in bar):
Tb = 300+273 #Temperature in tank B(in K):
pc = 12 #Pressure in tank C(in bar):
Tc = 50+273 #Temperature in tank C(in K):
Ra = 0.287 #Gas constant for air(in kJ/kg.K):
Rn = 0.297 #Gas constant for nitrogen(in kJ/kg.K):
ra = 1.4 #Adiabatic index of compression for air:
rn = 1.4 #Adiabatic index of compression for nitrogen:
Cp = 1.005 #Value of Cp(in kJ/kg.K):
Cv = 0.718 #Value of Cv(in kJ/kg.K):
#Calculations:
ma = pa*10**2*va/(Ra*Ta) #Mass in tank A(in kg): #Part (i)
mb = pb*10**2*vb/(Ra*Tb) #Mass in tank B(in kg):
Td = (ma*Ta+mb*Tb)/(ma+mb) #Final temperature(in K):
pd = Ra*Td*(ma+mb)/((va+vb)*10**2) #Final pressure(in bar):
dS1 = ma*Cp*log(Td/Ta)-ma*Ra*log(pd/pa)+mb*Cp*log(Td/Tb)-mb*Ra*log(pd/pb) #Entropy change(in kJ/K):
mc = pc*10**2*vc/(Rn*Tc) #Mass in tank C(in kg): #Part (ii)
md = ma+mb #Mass in tank D(in kg):
Cvn = Rn/(rn-1) #Value of Cv for nitrogen(in kJ/kg.K):
Cpn = rn*Cvn #Value of Cp for nitrogen(in kJ/kg.K):
mf = md+mc #Total mass(in kg):
CvF = (md*Cv+mc*Cvn)/mf #Final Cv(in kJ/kg.K):
RF = (md*Ra+mc*Rn)/mf #Final gas constant(in kJ/kg.K):
TF = (md*Cv*Td+mc*Cvn*Tc)/(mf*CvF) #Final temperature(in K):
VF = va+vb+vc #Final volume(in m**3):
pF = mf*RF*TF/VF #Final pressure(in kPa):
dS2 = md*(Cp*log(TF/Td)-Ra*log(pF/(pd*10**2)))+mc*(Cpn*log(TF/Tc)-Rn*log(pF/(pc*10**2))) #Change in entropy(in kJ/K):
#Results:
print "Entropy change in case 1: ",round(dS1,3),"KJ/K"
print "Entropy change in case 2: ",round(dS2,3),"KJ/K"
```