summaryrefslogtreecommitdiff
path: root/Chemical_Engineering_Thermodynamics/ch10_1.ipynb
blob: ed29419814c6138428a34c90fb5cd79a387d41c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 10 : Residual Properties by Equations of State"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.2  Page Number : 334"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Preparation of fugacity and fugacity coefficient\n",
      "\n",
      "import math\n",
      "\n",
      "# Variables\n",
      "T = 40 + 273.15;\t\t\t#[C] - Temperature\n",
      "P_1 = 0;\t\t\t#[bar]\n",
      "P_2 = 10;\t\t\t#[bar]\n",
      "V_liq = 90.45;\t\t\t#[cm**(3)/mol]\n",
      "V_liq = V_liq*10**(-6);\t\t\t#[m**(3)/mol]\n",
      "P_sat = 4.287;\t\t\t#[bar] \n",
      "\n",
      "# For butadiene\n",
      "T_c = 425.0;\t\t\t#[K] - Critical temperature\n",
      "P_c = 43.3;\t\t\t#[bar] - Critical pressure\n",
      "P_c = P_c*10**(5);\t\t\t#[N/m**(2)]\n",
      "w = 0.195;\t\t\t# Acentric factor\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "# Calculations and Results\n",
      "# Let us calculate second virial coefficient at 40 C\n",
      "Tr = T/T_c;\t\t\t# Reduced temperature\n",
      "B_0 = 0.083-(0.422/(Tr)**(1.6));\n",
      "B_1 = 0.139-(0.172/(Tr)**(4.2));\n",
      "#We know,(B*Pc)/(R*Tc) = B_0+(w*B_1)\n",
      "B = ((B_0 + (w*B_1))*(R*T_c))/P_c;\t\t\t#[m**(3)/mol] - Second virial coefficient\n",
      "\n",
      "# math.log(f/P) = (B*P)/(R*T)\n",
      "# f = P*exp((B*P)/(R*T))\n",
      "\n",
      "print \" The table is as follows\"\n",
      "print \" Pbar \\t\\t fbar \\t\\t                 phi\";\n",
      "from numpy import zeros\n",
      "P = [1,2,3,4,4.287,5,6,8,10];\n",
      "f = zeros(9);\n",
      "phi = zeros(9);\n",
      "for i in range(5):\n",
      "    f[i]=P[i]*(math.exp((B*P[i]*10**(5))/(R*T)));\t\t\t#[bar]  # Pressure inside the exponential term has to be in N/m**(2)\n",
      "    phi[i]= (f[i]/P[i]);\n",
      "    print \" %f \\t %f \\t\\t\\t %f\"%(P[i],f[i],phi[i]) \n",
      "\n",
      "f_sat = f[4];\n",
      "\n",
      "# From pressure of 4.287 bar onwards the valid equation to compute fugacity of compressed liquid is given by\n",
      "# f = f_sat*exp[V_liq*(P-P_sat)/(R*T)]\n",
      "\n",
      "for j in range(5,9):\n",
      "    f[j] = f_sat*math.exp((V_liq*(P[j]-P_sat)*10**(5))/(R*T));\t#[bar]  # Pressure inside the exponential term has to be in N/m**(2)\n",
      "    phi[j] = f[j]/P[j]; \n",
      "    print \" %f \\t %f \\t\\t\\t %f\"%(P[j],f[j],phi[j]);\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The table is as follows\n",
        " Pbar \t\t fbar \t\t                 phi\n",
        " 1.000000 \t 0.978336 \t\t\t 0.978336\n",
        " 2.000000 \t 1.914284 \t\t\t 0.957142\n",
        " 3.000000 \t 2.809221 \t\t\t 0.936407\n",
        " 4.000000 \t 3.664484 \t\t\t 0.916121\n",
        " 4.287000 \t 3.902801 \t\t\t 0.910380\n",
        " 5.000000 \t 3.912481 \t\t\t 0.782496\n",
        " 6.000000 \t 3.926097 \t\t\t 0.654349\n",
        " 8.000000 \t 3.953471 \t\t\t 0.494184\n",
        " 10.000000 \t 3.981037 \t\t\t 0.398104\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.3  Page Number : 334"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of enthalpy entropy and internal energy change\n",
      "\n",
      "\n",
      "import math \n",
      "from scipy.integrate import quad \n",
      "\n",
      "# Variables\n",
      "n = 100.;\t\t\t#[mol] - No of moles\n",
      "T_1 = 600.;\t\t\t#[K] - Initial temperature\n",
      "T_2 = 300.;\t\t\t#[K] - Final temperature\n",
      "P_1 = 10.;\t\t\t#[atm] - Initial pressure\n",
      "P_1 = P_1*101325;\t\t\t#[Pa]\n",
      "P_2 = 5.;\t\t\t#[atm] - Final presssure\n",
      "P_2 = P_2*101325;\t\t\t#[Pa]\n",
      "Tc = 369.8;\t\t\t#[K] - Critical temperature\n",
      "Pc = 42.48;\t\t\t#[bar] - Critical pressure\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "w = 0.152;\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "# Calculations\n",
      "# At 600 K\n",
      "Tr = T_1/Tc;\t\t\t# Reduced temperature\n",
      "B_0 = 0.083-(0.422/(Tr)**(1.6));\n",
      "B_1 = 0.139-(0.172/(Tr)**(4.2));\n",
      "#We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "B = ((B_0 + (w*B_1))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol] - Second virial coefficient\n",
      "dB0_dT = 0.422*1.6*Tc**(1.6)*T_1**(-2.6);\t\t\t# (dB_0/dT)\n",
      "dB1_dT = 0.172*4.2*Tc**(4.2)*T_1**(-5.2);\t\t\t# (dB_1/dT)\n",
      "dB_dT = ((R*Tc)/(Pc))*(dB0_dT + w*dB1_dT);\t\t\t# dB/dT\n",
      "\n",
      "# Now let us calculate B and dB/dT at 300 K\n",
      "Tr_prime = T_2/Tc;\t\t\t# Reduced temperature\n",
      "B_0_prime = 0.083-(0.422/(Tr_prime)**(1.6));\n",
      "B_1_prime = 0.139-(0.172/(Tr_prime)**(4.2));\n",
      "#We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "B_prime = ((B_0_prime + (w*B_1_prime))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol] - Second virial coefficient\n",
      "dB0_dT_prime = 0.422*1.6*Tc**(1.6)*T_2**(-2.6);\t\t\t# (dB_0/dT)\n",
      "dB1_dT_prime = 0.172*4.2*Tc**(4.2)*T_2**(-5.2);\t\t\t# (dB_1/dT)\n",
      "dB_dT_prime = ((R*Tc)/(Pc))*(dB0_dT_prime + w*dB1_dT_prime);\t\t\t# dB/dT\n",
      "\n",
      "# The change in enthalpy for ideal gas is given by\n",
      "\n",
      "def f16(T): \n",
      "    return -0.966+7.279*10**(-2)*T-3.755*10**(-5)*T**(2)+7.58*10**(-9)*T**(3)\n",
      "\n",
      "delta_H_ig =  quad(f16,T_1,T_2)[0]\n",
      "\n",
      "delta_H_ig = delta_H_ig*4.184;\t\t\t#[J/mol]\n",
      "\n",
      "# We know that delta_H_ig = delta_U_ig + R*delta_T. Therefore change in internal energy is given by \n",
      "delta_U_ig = delta_H_ig - R*(T_2 - T_1);\t\t\t#[J/mol]\n",
      "\n",
      "# The change in entropy of ideal gas is given by \n",
      "\n",
      "def f17(T): \n",
      "    return Cp_0/T\n",
      "\n",
      "#delta_S_ig =  quad(f17,T_1,T_2) - R*math.log(P_2/P_1)[0]\n",
      "\n",
      "\n",
      "def f18(T): \n",
      "    return (-0.966+7.279*10**(-2)*T-3.755*10**(-5)*T**(2)+7.58*10**(-9)*T**(3))/T\n",
      "\n",
      "delta_S_ig =  quad(f18,T_1,T_2)[0]*4.184 - R*math.log(P_2/P_1)\n",
      "\n",
      "\n",
      "# Now let us calculate the change in enthalpy of gas. We know that\n",
      "# delta_H = delta_H_ig + delta_H_R\n",
      "# delta_H_R = H_2_R - H_1_R\n",
      "H_2_R = B_prime*P_2 - P_2*T_2*dB_dT_prime;\t\t\t# [J/mol]\n",
      "H_1_R = B*P_1 - P_1*T_1*dB_dT;\t\t\t# [J/mol]\n",
      "delta_H_R = H_2_R - H_1_R;\t\t\t# [J/mol]\n",
      "delta_H = delta_H_ig + delta_H_R;\t\t\t# [J/mol]\n",
      "\n",
      "# Let us calculate the residual entropy of gas\n",
      "S_2_R = -P_2*dB_dT_prime;\t\t\t#[J/mol-K]\n",
      "S_1_R = -P_1*dB_dT;\t\t\t#[J/mol-K]\n",
      "delta_S = delta_S_ig + (S_2_R - S_1_R);\t\t\t#[J/mol-K]\n",
      "\n",
      "# Let us calculate the residual internal energy of gas\n",
      "U_2_R = -P_2*T_2*dB_dT_prime;\t\t\t#[J/mol-K]\n",
      "U_1_R = -P_1*T_1*dB_dT;\t\t\t#[J/mol-K]\n",
      "delta_U = delta_U_ig + (U_2_R - U_1_R);\t\t\t#[J/mol-K]\n",
      "\n",
      "# For 100 mol sample,\n",
      "delta_H_ig = delta_H_ig*n*10**(-3);\t\t\t#[kJ/mol]\n",
      "delta_H = delta_H*n*10**(-3);\t\t\t#[kJ/mol]\n",
      "\n",
      "delta_U_ig = delta_U_ig*n*10**(-3);\t\t\t#[kJ/mol]\n",
      "delta_U = delta_U*n*10**(-3);\t\t\t#[kJ/mol]\n",
      "\n",
      "delta_S_ig = delta_S_ig*n*10**(-3);\t\t\t#[kJ/mol]\n",
      "delta_S = delta_S*n*10**(-3);\t\t\t#[kJ/mol]\n",
      "\n",
      "# Results\n",
      "print \" The value of delta_H = %f kJ/mol\"%(delta_H);\n",
      "print \" The value of delta_H_ig ideal gas)= %f J/mol\"%(delta_H_ig);\n",
      "print \" The value of delta_U = %f kJ/mol\"%(delta_U);\n",
      "print \" The value of delta_U_ig ideal gas) = %f kJ/mol\"%(delta_U_ig);\n",
      "print \" The value of delta_S = %f kJ/mol\"%(delta_S);\n",
      "print \" The value of delta_S_ig ideal gas) = %f kJ/mol\"%(delta_S_ig);\n",
      "\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The value of delta_H = -3130.406247 kJ/mol\n",
        " The value of delta_H_ig ideal gas)= -3096.763542 J/mol\n",
        " The value of delta_U = -2867.753839 kJ/mol\n",
        " The value of delta_U_ig ideal gas) = -2847.343542 kJ/mol\n",
        " The value of delta_S = -6.466831 kJ/mol\n",
        " The value of delta_S_ig ideal gas) = -6.358994 kJ/mol\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.4  Page Number : 337"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of molar heat capacity\n",
      "\n",
      "# Variables\n",
      "T = 35 + 273.15;\t\t\t#[K] - Temperature\n",
      "P = 10;\t\t\t#[atm] - Pressure\n",
      "P = P*101325;\t\t\t#[Pa]\n",
      "# Methane obeys the equation of state\n",
      "# Z = 1 + (P*B)/(R*T)\n",
      "\n",
      "# Calculations\n",
      "# At 35 C,\n",
      "B = -50;\t\t\t#[cm**(3)/mol]\n",
      "dB_dT = 1.0;\t\t\t#[cm**(3)/mol-K] - dB/dT\n",
      "dB_dT = dB_dT*10**(-6);\t\t\t#[m**(3)/mol-K]\n",
      "d2B_dT2 = -0.01;\t\t\t#[cm**(3)/mol-K**(2)] - d**2(B)/d(T**2)\n",
      "d2B_dT2 = d2B_dT2*10**(-6);\t\t\t#[m**(3)/mol-K**(2)]\n",
      "\n",
      "# Ideal gas molar heat capacity of methane is given by\n",
      "# Cp_0 = 4.75 + 1.2*10**(-2)*T + 0.303*10**(-5)*T**(2) - 2.63*10**(-9)*T**(3)\n",
      "\n",
      "# The molar heat capacity is given by\n",
      "# Cp = Cp_0 + Cp_R\n",
      "# For virial gas equation of state \n",
      "Cp_R = -P*T*d2B_dT2;\t\t\t#[J/mol-K]\n",
      "\n",
      "# thus heat capacity is given by \n",
      "# Cp = a + b*T + c*T**(2) + d*T**(3) - P*T*d2B_dT2\n",
      "# Putting the values, we get\n",
      "Cp = (4.75 + 1.2*10**(-2)*T + 0.303*10**(-5)*T**(2) - 2.63*10**(-9)*T**(3))*4.184 - P*T*d2B_dT2;\t\t\t#[J/mol-K]\n",
      "\n",
      "# Results\n",
      "print \" The molar heat capacity of methane is %f J/mol-K\"%(Cp);\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The molar heat capacity of methane is 39.349753 J/mol-K\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.5  Page Number : 338"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of final temperature after expansion\n",
      "\n",
      "from scipy.optimize import fsolve \n",
      "\n",
      "# Variables\n",
      "T_1 = 360;\t\t\t#[K] - Initial temperature\n",
      "P_1 = 10;\t\t\t#[bar] - Initial pressure\n",
      "P_1 = P_1*10**(5);\t\t\t#[Pa]\n",
      "Tc = 408.1;\t\t\t#[K] - Critical temperature\n",
      "Pc = 36.48;\t\t\t#[bar] - Critical pressure\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "w = 0.181;\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "Cv_0 = 106.0;\t\t\t#[J/mol-K]\n",
      "\n",
      "# Calculations\n",
      "# At 360 K\n",
      "Tr = T_1/Tc;\t\t\t# Reduced temperature\n",
      "B_0 = 0.083-(0.422/(Tr)**(1.6));\n",
      "B_1 = 0.139-(0.172/(Tr)**(4.2));\n",
      "#We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "B = ((B_0 + (w*B_1))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol] - Second virial coefficient\n",
      "dB0_dT = 0.422*1.6*Tc**(1.6)*T_1**(-2.6);\t\t\t# (dB_0/dT)\n",
      "dB1_dT = 0.172*4.2*Tc**(4.2)*T_1**(-5.2);\t\t\t# (dB_1/dT)\n",
      "dB_dT = ((R*Tc)/(Pc))*(dB0_dT + w*dB1_dT);\t\t\t# dB/dT\n",
      "\n",
      "# At state 1\n",
      "V_1 = B + (R*T_1)/P_1;\t\t\t#[m**(3)/mol] - Molar volume\n",
      "# At state 1\n",
      "V_2 = 10*V_1;\t\t\t#[m**(3)/mol] - Molar volume\n",
      "\n",
      "T_2 = -(P_1*T_1*(dB_dT))/Cv_0 + T_1;\t\t\t#[K]\n",
      "\n",
      "T = 350;\n",
      "\n",
      "fault = 10;\n",
      "def f(T): \n",
      "    return  106*(T-T_1)+972.72-((R*T**(2))/(V_2-B_prime))*dB_dT_prime\n",
      "while(fault>0.007):\n",
      "        Tr_prime = T/Tc;\t\t\t# Reduced temperature\n",
      "        B_0_prime = 0.083-(0.422/(Tr_prime)**(1.6));\n",
      "        B_1_prime = 0.139-(0.172/(Tr_prime)**(4.2));\n",
      "            #We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "        B_prime = ((B_0_prime + (w*B_1_prime))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol] - Second virial coefficient\n",
      "        dB0_dT_prime = 0.422*1.6*Tc**(1.6)*T_2**(-2.6);\t\t\t# (dB_0/dT)\n",
      "        dB1_dT_prime = 0.172*4.2*Tc**(4.2)*T_2**(-5.2);\t\t\t# (dB_1/dT)\n",
      "        dB_dT_prime = ((R*Tc)/(Pc))*(dB0_dT_prime + w*dB1_dT_prime);\t\t\t# dB/dT\n",
      "        T_prime = fsolve(f,0.15)\n",
      "        fault=abs(T-T_prime);\n",
      "        T = T + 0.001;\n",
      "\n",
      "# Results\n",
      "print \" The final temperature is %f K\"%(T);\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The final temperature is 351.910000 K\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.6  Page Number : 339"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of fugacity of liquid benzene\n",
      "\n",
      "# Variables\n",
      "T = 220 + 273.15;\t\t\t#[K] - Temperature\n",
      "Tc = 562.2;\t\t\t#[K] - Critical temperature\n",
      "Pc = 48.98;\t\t\t#[bar] - Critical pressure\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "w = 0.210;\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "P_sat = 1912.86;\t\t\t#[kPa] - Saturation pressure at 220 C\n",
      "P_sat = P_sat*10**(3);\t\t\t#[Pa]\n",
      "Mol_wt = 78.114;\t\t\t#[g/mol] - Molecular weight of benzene\n",
      "\n",
      "# Calculations and Results\n",
      "#(1)\n",
      "# Since liquid and vapour are in equilibrium the fugacity is saturated fugacity (f_sat) and can be calculated using virial gas equation of state\n",
      "# At 220 C\n",
      "Tr = T/Tc;\t\t\t# Reduced temperature\n",
      "B_0 = 0.083-(0.422/(Tr)**(1.6));\n",
      "B_1 = 0.139-(0.172/(Tr)**(4.2));\n",
      "#We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "B = ((B_0 + (w*B_1))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol] - Second virial coefficient\n",
      "\n",
      "# We know that math.log(f/P) = (B*P)/(R*T)\n",
      "# Thus at saturated conditions\n",
      "# math.log(f_sat/P_sat) = B*P_sat/(R*T)\n",
      "f_sat = P_sat*(math.exp((B*P_sat)/(R*T)));\t\t\t#[Pa]\n",
      "f_sat = f_sat*10**(-3);\t\t\t#[kPa]\n",
      "\n",
      "print \" 1).The fugacity of liquid benzene is %f kPa\"%(f_sat);\n",
      "\n",
      "#(2)\n",
      "P = 2014.7;\t\t\t# [psia] - Total gauge pressure\n",
      "P = 138.94;\t\t\t# [bar]\n",
      "P = P*10**(5);\t\t\t# [Pa]\n",
      "den = 0.63;\t\t\t# [g/cm**(3)] - density of benzene\n",
      "den = den*10**(3);\t\t\t# [kg/m**(3)]\n",
      "\n",
      "# Therefore specific volume is \n",
      "V = 1/den;\t\t\t#[m/**(3)/kg]\n",
      "# Molar volume is given by\n",
      "V = V*Mol_wt*10**(-3);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# Thus fugacity at 220 C and pressure P is given by \n",
      "f = f_sat*(math.exp((V*(P-P_sat))/(R*T)));\n",
      "\n",
      "print \" 2).The fugacity of liquid benzene is %f kPa\"%(f);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The fugacity of liquid benzene is 1551.083216 kPa\n",
        " 2).The fugacity of liquid benzene is 2228.386532 kPa\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.7  Page Number : 341"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of molar enthalpy\n",
      "\n",
      "# Variables\n",
      "# C = -0.067 + 30.7/T\n",
      "# D = 0.0012 - 0.416/T\n",
      "\n",
      "T = 80 + 273.15;\t\t\t#[K]\n",
      "P = 30;\t\t\t#[bar]\n",
      "#P = P;\t\t\t#[N/m**(2)]\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "\n",
      "# Calculations\n",
      "# We have the relation derived in the book\n",
      "# d(G/(R*T)) = (V/(R*T))dP - (H/(R*T**(2)))dT\n",
      "# Writing the same equation for ideal gas and subtracting it from the above equation we get\n",
      "# d(G_R/(R*T)) = (V_R/(R*T))dP - (H_R/(R*T**(2)))dT\n",
      "# Therefore, H_R/(R*T**(2)) = -[del((G_R)/(R*T))/del(T)]_P\n",
      "\n",
      "# Substituting the relation G_R/(R*T) = math.log(f/P), we get\n",
      "# H_R/(R*T**(2)) = -[del(math.log(f/P))/del(T)]_P = -[del(-C*P - D*P**(2))/del(T)]_P\n",
      "# or, H_R/(R*T**(2)) = P*(dC/dT) + P**(2)*dD/dT\n",
      "# Note that in the above equation the partial derivative is replaced by full derivative as C and D are functions of temperature. Therfore we get\n",
      "# H_R/(R*T**(2)) = (30.7*P)/T**(2) + (0.416*P**(2))/T**(2)\n",
      "# H_R/R = - 30.7*P + 0.416*P**(2)\n",
      "\n",
      "# Substituting the given conditions we get\n",
      "H_R = R*(-30.7*P + 0.416*P**(2));\t\t\t#[J/mol]\n",
      "\n",
      "# Results\n",
      "print \" The molar enthalpy of the gas relative to that of the ideal gas at 80 C and 30 bar pressure is H_R = %f J/mol\"%(H_R);\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The molar enthalpy of the gas relative to that of the ideal gas at 80 C and 30 bar pressure is H_R = -4544.432400 J/mol\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.8  Page Number : 341"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Determination of second and third virial coefficients and fugacity\n",
      "\n",
      "# Variables\n",
      "# (1)\n",
      "T = 311;\t\t\t#[K] - Temperature\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "# Pressure in 'bar' is given below\n",
      "P = [0.690,1.380,2.760,5.520,8.280,11.034,13.800,16.550];\n",
      "# Molar volume in 'm**(3)/mol' is given below\n",
      "V = [0.0373,0.0186,0.00923,0.00455,0.00298,0.00220,0.00175,0.00144];\n",
      "\n",
      "# Z = 1 + (B/V) + (C/V**(2))\n",
      "# (Z-1)*V = B + (C/V)\n",
      "\n",
      "from numpy import zeros\n",
      "Z=zeros(8);\n",
      "k=zeros(8);\n",
      "t=zeros(8);\n",
      "\n",
      "# Calculations and Results\n",
      "for i in range(8):\n",
      "    Z[i]=(P[i]*10**(5)*V[i])/(R*T);\n",
      "    k[i]=(Z[i]-1)*V[i];\n",
      "    t[i]=1./V[i];\n",
      "\n",
      "from scipy.stats import linregress\n",
      "from numpy import array\n",
      "t = array(t)\n",
      "k = array(k)\n",
      "C,B,c,d,e = linregress(t.T,k.T)\n",
      "#[C,B,sig]=reglin(t',k');\n",
      "\n",
      "#From the regression, we get intercept = B and slope = C,and thus,\n",
      "\n",
      "print \" 1).The second virial coefficient of CO2 is given by B = %e m**3)/mol\"%(B);\n",
      "print \"     The thied virial coefficient of CO2 is given by C = %e m**6)/mol**2)\"%(C);\n",
      "\n",
      "# (2)\n",
      "P_final = 13.8;\t\t\t#[bar]\n",
      "\n",
      "def f40(P): \n",
      "\t return V-(R*T)/P\n",
      "\n",
      "# We know that R*T*math.log(f/P) =  quad(f40,0,P)[0]\n",
      "\n",
      "# Therefore we have to plot V - (R*T)/P versus P and calculate the area beneath the curve from 0 to 13.8 bar\n",
      "# For this we need the value of the term V - (R*T)/P at P = 0. At low pressure the virial equation becomes\n",
      "# Z = 1 + (B/V)\n",
      "#and V - (R*T)/P = (Z*R*T)/P - (R*T)/P = (1 + (B/V))*((R*T)/P) - (R*T)/P = (B*R*T)/(P*V) = (B/Z)\n",
      "# Thus lim P tending to zero (V - (R*T)/P) = B    ( as P tend to zero, Z tend to 1 ) \n",
      "\n",
      "P_prime = [0.000,0.690,1.380,2.760,5.520,8.280,11.034,13.800];\n",
      "V_prime = [0.000,0.0373,0.0186,0.00923,0.00455,0.00298,0.00220,0.00175];\n",
      "summation = 0;\n",
      "x=zeros(8);\n",
      "y=zeros(8);\n",
      "z=zeros(8);\n",
      "for j in range(2,8):\n",
      "    x[j]=V_prime[j]-(R*T)/(P_prime[j]*10**(5));\t\t\t#[m**(3)/mol]\n",
      "    y[j]=(x[j] + x[j-1])/2;\n",
      "    z[j]=y[j]*((P_prime[j]-P_prime[j-1]))*10**(5);\n",
      "    summation = summation + z[j] ;\t\t\t#[J/mol]\n",
      "\n",
      "summation = summation + 2*z[1] - z[1];\t\t\t# Because in the above calculation,in order to calculate the average a summation of z(2) is not included,only half of it gets added\n",
      "\n",
      "\n",
      "def f41(P): \n",
      "\t return V - (R*T)/P\n",
      "\n",
      "\t\t\t# Now we have, area =  quad(f41,0,13.8*10**(5))[0]\n",
      "\n",
      "\t\t\t# R*T*math.log(f/P) = summation\n",
      "f = P_final*(math.exp(summation/(R*T)));\t\t\t#[bar]\n",
      "\n",
      "\n",
      "print \" 2).The fugacity of steam at 311 K and 13.8 bar pressure is %f bar\"%(f);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The second virial coefficient of CO2 is given by B = -1.515264e-04 m**3)/mol\n",
        "     The thied virial coefficient of CO2 is given by C = 5.618543e-08 m**6)/mol**2)\n",
        " 2).The fugacity of steam at 311 K and 13.8 bar pressure is 12.892780 bar\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.9  Page Number : 344"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Determination of second and third virial coefficients\n",
      "\n",
      "# Variables\n",
      "T = 0 + 273.15;\t\t\t#[K] - Temperature\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\t\t\t# Pressure in 'atm' is given below\n",
      "P = [100,200,300,400,500,600,700,800,900,1000];\n",
      "\t\t\t# The compressibility factor values are\n",
      "Z = [1.069,1.138,1.209,1.283,1.356,1.431,1.504,1.577,1.649,1.720];\n",
      "\n",
      "# Z = 1 + (B/V) + (C/V**(2))\n",
      "# (Z-1)*V = B + (C/V)\n",
      "from numpy import zeros\n",
      "\n",
      "V = zeros(10);\n",
      "k = zeros(10);\n",
      "t = zeros(10);\n",
      "\n",
      "# Calculations and Results\n",
      "for i in range(10):\n",
      "    V[i]=Z[i]*R*T/(P[i]*101325);\t\t\t#[m**(3)/mol]\n",
      "    k[i]=(Z[i]-1)*V[i];\n",
      "    t[i]=1/V[i];\n",
      "\n",
      "from scipy.stats import linregress\n",
      "C,B,c,d,e = linregress(t,k)\n",
      "\t\t\t#[C,B,sig]=reglin(t,k);\n",
      "\n",
      "\t\t\t#From the regression, we get intercept = B and slope = C,and thus,\n",
      "\n",
      "print \" 1).The second virial coefficient of H2 is given by B = %e m**3)/mol\"%(B);\n",
      "print \"     The thied virial coefficient of H2 is given by C = %e m**6)/mol**2)\"%(C);\n",
      "\n",
      "\t\t\t# (2)\n",
      "\t\t\t# We know that, limit P tending to zero (V-(R*T)/P) = B, therfore P = 0,  V-(R*T)/P = B\n",
      "\n",
      "def f42(P): \n",
      "\t return V-(R*T)/P \t\t\t#and determine the integral integrate((V-(R*T)/P)\n",
      "\n",
      "\t\t\t# Now let us tabulate V-(R*T)/P and determine the integral  quad(f42,0,1000)[0]\n",
      "\n",
      "\n",
      "P_prime = [0,100,200,300,400,500,600,700,800,900,1000];\n",
      "Z_prime = [0,1.069,1.138,1.209,1.283,1.356,1.431,1.504,1.577,1.649,1.720];\n",
      "\n",
      "summation = 0;\n",
      "V_prime = zeros(11);\n",
      "x = zeros(11);\n",
      "y = zeros(11);\n",
      "z = zeros(11);\n",
      "for j in range(1,11):\n",
      "    V_prime[j]=Z_prime[j]*R*T/(P_prime[j]*101325);\t\t\t#[m**(3)/mol]\n",
      "    x[j]=V_prime[j]-(R*T)/(P_prime[j]*101325);\n",
      "    y[j]=(x[j] + x[j-1])/2;\n",
      "    z[j]=y[j]*((P_prime[j]-P_prime[j-1]))*101325;\n",
      "    summation = summation + z[j] ;\t\t\t#[J/mol]\n",
      "\n",
      "summation = summation + 2*z[1] - z[1];\t\t\t# Because in the above calculation,in order to calculate the average a summation of z(2) is not included,only half of it gets added\n",
      "\n",
      "\t\t\t# Now we have\n",
      "\t\t\t# R*T*math.log(f/P) = summation\n",
      "P_dash = 1000;\t\t\t#[atm] - Pressure at which fugacity is to be calculated\n",
      "T_dash = 273.15;\t\t\t#[K] - Temperature at which fugacity is to be calculated\n",
      "f = P_dash*math.exp(summation/(R*T_dash));\t\t\t#[atm] \n",
      "\n",
      "print \" 2).The fugacity of H2 at 0 C and 1000 atm pressure is  f = %f atm\"%(f);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The second virial coefficient of H2 is given by B = 1.345242e-05 m**3)/mol\n",
        "     The thied virial coefficient of H2 is given by C = 5.286525e-10 m**6)/mol**2)\n",
        " 2).The fugacity of H2 at 0 C and 1000 atm pressure is  f = 2030.305169 atm\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.10  Page Number : 345"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Determination of work done and the exit temperature\n",
      "\n",
      "from scipy.optimize import fsolve \n",
      "import math\n",
      "from scipy.integrate import quad \n",
      "\n",
      "# Variables\n",
      "P_1 = 1*10.**(6.);\t\t\t#[Pa] - Initial pressure\n",
      "T_1 = 200. + 273.15;\t\t#[K] - Initial temperature\n",
      "P_2 = 8*10.**(6);\t\t\t#[Pa] - Final pressure\n",
      "Tc = 647.1;\t\t\t        #[K] - Critical temperature of water\n",
      "Pc = 220.55;    \t\t\t#[bar] - Critical pressure of water\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "w = 0.345;\n",
      "R = 8.314;\t\t    \t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "# For the virial gas the following are the relations for residual enthalpy and entropy\n",
      "# H_R = B*P - P*T*(dB/dT) \n",
      "# S_R = -P*(dB/dT)\n",
      "# Where, (dB/dT) = ((R*Tc)/Pc)*((dB_0/dT) + w*(dB_1/dT))\n",
      "# dB0_dT = 0.422*1.6*Tc**(1.6)*T**(-2.6);\t\t\t# (dB_0/dT)\n",
      "# dB1_dT = 0.172*4.2*Tc**(4.2)*T**(-5.2);\t\t\t# (dB_1/dT)\n",
      "\n",
      "# (1)\n",
      "Cp_0 = 29.114;\t\t\t#[J/mol-K] - Specific heat capacity at constant pressure\n",
      "# For the isentropic process entropy change is zero, thus\n",
      "# delta_S = Cp_0*math.log(T_2/T_1) - P_2*(dB/dT)_2 + P_1*(dB/dT)_1 = 0\n",
      "\n",
      "# Calculations\n",
      "# At state 1, \n",
      "Tr_1 = T_1/Tc;\n",
      "B0_1 = 0.083 - 0.422/(Tr_1**(1.6));\n",
      "B1_1 = 0.139 - 0.172/(Tr_1**(4.2));\n",
      "# (B*Pc)/(R*Tc) = B0 + w*B1\n",
      "B_1 = ((B0_1 + w*B1_1)*(R*Tc))/Pc;\t\t\t# [m**(3)/mol] - Second virial coefficient at state 1\n",
      "dB0_dT_1 = 0.422*1.6*Tc**(1.6)*T_1**(-2.6);\t\t\t# (dB_0/dT)\n",
      "dB1_dT_1 = 0.172*4.2*Tc**(4.2)*T_1**(-5.2);\t\t\t# (dB_1/dT)\n",
      "dB_dT_1 = ((R*Tc)/Pc)*((dB0_dT_1) + w*(dB1_dT_1));\t\t\t# (dB/dT)_1\n",
      "\n",
      "# Now let us assume the exit temperature to be 870 K, at this temperature\n",
      "# T_2 = 870;\t\t\t#[K] - \n",
      "# At this temperature\n",
      "# delta_S = Cp_0*math.log(T_2/T_1) - P_2*(dB/dT)_2 + P_1*(dB/dT)_1 = \n",
      "\n",
      "\n",
      "T_2 = 860.;\t\t\t#[K] - Exit temperature\n",
      "# Therefore at state 2, we have\n",
      "Tr_2 = T_2/Tc;\n",
      "B0_2 = 0.083 - 0.422/(Tr_2**(1.6));\n",
      "B1_2 = 0.139 - 0.172/(Tr_2**(4.2));\n",
      "# (B*Pc)/(R*Tc) = B0 + w*B1\n",
      "B_2 = ((B0_2 + w*B1_2)*(R*Tc))/Pc;\t\t\t# [m**(3)/mol] - Second virial coefficient at state 2\n",
      "dB0_dT_2 = 0.422*1.6*Tc**(1.6)*T_2**(-2.6);\t\t\t# (dB_0/dT)\n",
      "dB1_dT_2 = 0.172*4.2*Tc**(4.2)*T_2**(-5.2);\t\t\t# (dB_1/dT)\n",
      "dB_dT_2 = ((R*Tc)/Pc)*((dB0_dT_2) + w*(dB1_dT_2));\t\t\t# (dB/dT)_2\n",
      "\n",
      "delta_H_s = Cp_0*(T_2 - T_1) + B_2*P_2 -P_2*T_2*(dB_dT_2) - B_1*P_1 + P_1*T_1*(dB_dT_1);\t\t\t#[J/mol] - Enthalpy change\n",
      "\n",
      "# As no heat exchange is assumed to take place with the surroundings,work transfer is given by\n",
      "W_1 = - delta_H_s;\t\t\t# [J/mol]\n",
      "\n",
      "# Results\n",
      "print \" 1).The exit temperature is %f K\"%(T_2);\n",
      "print \"     The required amount of work is %f J/mol\"%(W_1);\n",
      "\n",
      "\n",
      "# (2)\n",
      "eff = 0.8;\t\t\t# Adiabatic efficiency\n",
      "delta_H_a = delta_H_s/0.8;\t\t\t# Actual enthalpy change\n",
      "\n",
      "# Now for calculating the value of T_exit\n",
      "# delta_H_a = Cp_0*(T_exit - T_1) + B*P_2 -P_2*T_exit*(dB_dT) - B_1*P_1 + P_1*T_1*(dB_dT_1)\n",
      "# On simplification we get\n",
      "# 29.114*(T_2 - T_1)*B_2*8*10**(6)-8*10**(6)*T_2*(dB/dT)_2 = 12643.77 \n",
      "\n",
      "# Let us assume a temperature of say\n",
      "T = 900.;\t\t\t#[K]\n",
      "fault=10.;\n",
      "\n",
      "def f(T_exit): \n",
      "\t return  delta_H_a - Cp_0*(T_exit - T_1) + B*P_2 -P_2*T_exit*(dB_dT) - B_1*P_1 + P_1*T_1*(dB_dT_1)\n",
      "\t \n",
      "while(fault>0.3):\n",
      "    Tr = T/Tc;\n",
      "    B0 = 0.083 - 0.422/(Tr**(1.6));\n",
      "    B1 = 0.139 - 0.172/(Tr**(4.2));\n",
      "    \t\t\t# (B*Pc)/(R*Tc) = B0 + w*B1\n",
      "    B = ((B0 + w*B1)*(R*Tc))/Pc;\t\t\t# [m**(3)/mol] - Second virial coefficient at state 2\n",
      "    dB0_dT = 0.422*1.6*Tc**(1.6)*T**(-2.6);\t\t\t# (dB_0/dT)\n",
      "    dB1_dT = 0.172*4.2*Tc**(4.2)*T**(-5.2);\t\t\t# (dB_1/dT)\n",
      "    dB_dT = ((R*Tc)/Pc)*((dB0_dT) + w*(dB1_dT));\t\t\t# (dB/dT)_1\n",
      "\n",
      "    T_exit = fsolve(f,900)\n",
      "    fault=abs(T-T_exit);\n",
      "    T = T + 0.2;\n",
      "\n",
      "Texit = T;\n",
      "\n",
      "\t\t\t# As no heat exchange is assumed to take place with the surroundings,work transfer is given by\n",
      "W_2 = - delta_H_a;\t\t\t# [J/mol]\n",
      "\n",
      "print \" 2).The exit temperature is %f K\"%(Texit);\n",
      "print \"     The required amount of work is %f J/mol\"%(W_2);\n",
      "\n",
      "\n",
      "def f20(T): \n",
      "\t return Cp_0/T\n",
      "\n",
      "T_prime = 700.;\t\t\t#[K]\n",
      "fault1=10.;\n",
      "\n",
      "def f1(T_out): \n",
      "\t return  32.2168*math.log(T_out) + 0.1922*10**(-2)*T_out + 0.5274*10**(-5) \\\n",
      "\t *T_2**(2) - 1.1976*10**(-9)*T_out**(3)-8*10**(6)*dB_dT_prime -216.64\n",
      "\n",
      "while(fault1>0.5):\n",
      "    Tr_prime = T_prime/Tc;\n",
      "    B0_prime = 0.083 - 0.422/(Tr_prime**(1.6));\n",
      "    B1_prime = 0.139 - 0.172/(Tr_prime**(4.2));\n",
      "    B_prime = ((B0_prime + w*B1_prime)*(R*Tc))/Pc;\t\t\t# [m**(3)/mol] - Second virial coefficient at state 2\n",
      "    dB0_dT_prime = 0.422*1.6*Tc**(1.6)*T_prime**(-2.6);\t\t\t# (dB_0/dT)\n",
      "    dB1_dT_prime = 0.172*4.2*Tc**(4.2)*T_prime**(-5.2);\t\t\t# (dB_1/dT)\n",
      "    dB_dT_prime = ((R*Tc)/Pc)*((dB0_dT_prime) + w*(dB1_dT_prime));\t\t\t# (dB/dT)_1\n",
      "    T_out = fsolve(f1,10)\n",
      "    fault1=abs(T_prime-T_out);\n",
      "    T_prime = T_prime + 0.5;\n",
      "\n",
      "T_out = T_prime;\n",
      "\n",
      "# Now we have to calculate enthalpy change as W = -delta_H\n",
      "\n",
      "def f21(T): \n",
      "\t return (7.7 + 0.04594*10**(-2.)*T + 0.2521*10**(-5.)*T**(2.) - 0.8587*10.**(-9.)*T**(3.))*4.184\n",
      "\n",
      "delta_H_3 =  quad(f21,T_1,T_out)[0] + B_prime*P_2 - P_2*T_out*dB_dT_prime - B_1*P_1 + P_1*T_1*dB_dT_1\n",
      "print T_1,T_out\n",
      "\n",
      "\n",
      "W_3 = - delta_H_3;\t\t\t# [J/mol]\n",
      "\n",
      "print \" 3).The exit temperature is %f K\"%(T_out);\n",
      "print \"     The required amount of work is %f J/mol\"%(W_3);\n",
      "\n",
      "#(4)\n",
      "n = 0.8;\t\t\t# Adiabatic efficiency\n",
      "delta_H_a_4 = delta_H_3/n;\t\t\t#[J/mol]\n",
      "W_4 = -delta_H_a_4;\t\t\t#[J/mol]\n",
      "\n",
      "\n",
      "T_prime1 = 700.;\t\t\t#[K]\n",
      "fault2=10.;\n",
      "\n",
      "def f2(T_2): \n",
      "\t return  7.7*4.184*(T_2-T_1) + ((0.04594*4.184*10**(-2))/2)* \\\n",
      "\t (T_2**(2)-T_1**(2)) + ((0.2521*4.184*10**(-5))/3)*(T_2**(3)-T_1**(3)) - \\\n",
      "\t ((0.8587*4.184*10**(-9))/4)*(T_2**(4)-T_1**(4)) + B_prime1*8*10**(6) \\\n",
      "\t - 8*10**(6)*T_2*dB_dT_prime1 + 191.7 + 496.81 - delta_H_a_4\n",
      "\n",
      "while(fault2>0.5):\n",
      "    Tr_prime1 = T_prime1/Tc;\n",
      "    B0_prime1 = 0.083 - 0.422/(Tr_prime1**(1.6));\n",
      "    B1_prime1 = 0.139 - 0.172/(Tr_prime1**(4.2));\n",
      "    \t\t\t# (B*Pc)/(R*Tc) = B0 + w*B1\n",
      "    B_prime1 = ((B0_prime1 + w*B1_prime1)*(R*Tc))/Pc;\t\t\t# [m**(3)/mol] - Second virial coefficient at state 2\n",
      "    dB0_dT_prime1 = 0.422*1.6*Tc**(1.6)*T_prime1**(-2.6);\t\t\t# (dB_0/dT)\n",
      "    dB1_dT_prime1 = 0.172*4.2*Tc**(4.2)*T_prime1**(-5.2);\t\t\t# (dB_1/dT)\n",
      "    dB_dT_prime1 = ((R*Tc)/Pc)*((dB0_dT_prime1) + w*(dB1_dT_prime1));\t\t\t# (dB/dT)_1\n",
      "    T_out1 = fsolve(f2,100)\n",
      "    fault2=abs(T_prime1-T_out1);\n",
      "    T_prime1 = T_prime1 + 0.5;\n",
      "\n",
      "T_out1 = T_prime1;\n",
      "\n",
      "print \" 4).The exit temperature is %f K\"%(T_out1);\n",
      "print \"     The required amount of work is %f J/mol\"%(W_4);\n",
      "\n",
      "\n",
      "# answers are varies because of rounding error. please check it manually."
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The exit temperature is 860.000000 K\n",
        "     The required amount of work is -10667.724150 J/mol\n",
        " 2).The exit temperature is 916.600000 K\n",
        "     The required amount of work is -13334.655188 J/mol\n",
        "473.15"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 755.5\n",
        " 3).The exit temperature is 755.500000 K\n",
        "     The required amount of work is -9282.701079 J/mol\n",
        " 4).The exit temperature is 809.500000 K"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "     The required amount of work is -11603.376349 J/mol\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.11  Page Number : 348"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of temperature and pressure\n",
      "\n",
      "# Variables\n",
      "Vol = 0.15;\t\t\t    #[m**(3)] - Volume of the cylinder\n",
      "P_1 = 100.;\t\t\t    #[bar] - Initial pressure\n",
      "P_1 = P_1*10**(5);\t\t#[Pa]\n",
      "T_1 = 170.;\t\t\t    #[K] - Initial temperature\n",
      "n_withdrawn = 500.;\t\t#[mol] - Withdrawn number of moles\n",
      "R = 8.314;\t\t    \t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "\n",
      "# Calculations and Results\n",
      "#(1)\n",
      "Y = 1.4;\t\t\t# Coefficient of adiabatic expansion\n",
      "n_total = (P_1*Vol)/(R*T_1);\t\t\t#[mol] - Total number of moles\n",
      "n_2 = n_total - n_withdrawn;\t\t\t#[mol] - Left number of moles\n",
      "V_1 = Vol/n_total;\t\t\t#[m**(3)/mol] - Molar volume at initial state.\n",
      "# At final state\n",
      "V_2 = Vol/n_2;\t\t\t#[m**(3)/mol] - Molar volume at final state\n",
      "\n",
      "# During duscharging  P_1*V_1**(Y) = P_2*V_2**(Y), therefore\n",
      "P_2_1 = P_1*((V_1/V_2)**(Y));\t\t\t#[Pa] - Final pressure\n",
      "P_2_1 = P_2_1*10**(-5);\t\t\t#[bar]\n",
      "T_2_1 = ((P_2_1*10**(5))*V_2)/R;\t\t\t#[K] - Final temperature\n",
      "\n",
      "print \" 1).The final temperature %f K\"%(T_2_1);\n",
      "print \"     The final pressure %f bar\"%(P_2_1);\n",
      "\n",
      "\n",
      "def f53(T): \n",
      "\t return Cp_0/T\n",
      "\n",
      "T_prime = 150;\t\t\t#[K]\n",
      "error = 10;\n",
      "while(error>1):\n",
      "    f_T = 18.886*math.log(T_prime) + 4.2*10**(-3)*T_prime - 92.4;\n",
      "    f_T_dash = 18.886/T_prime + 4.2*10**(-3);\n",
      "    T_new = T_prime - (f_T/f_T_dash);\n",
      "    error=abs(T_prime - T_new);\n",
      "    T_prime = T_new;\n",
      "\n",
      "T_2_2 = T_prime;\t\t\t#[K] - Final temperature\n",
      "P_2_2 = ((n_2*R*T_2_2)/Vol)*10**(-5);\t\t\t#[bar] - Final pressure\n",
      "\n",
      "print \" 2).The final temperature %f K\"%(T_2_2);\n",
      "print \"     The final pressure %f bar\"%(P_2_2);\n",
      "\n",
      "#(3)\n",
      "Tc = 126.2;\t\t\t#[K] - Critical temperature of nitrogen\n",
      "Pc = 34.0;\t\t\t#[bar] - Critical pressure of nitrogen\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "w = 0.038;\t\t\t# Acentric factor\n",
      "\n",
      "\n",
      "dB0_dT = 0.422*1.6*Tc**(1.6)*T_1**(-2.6);\t\t\t# (dB_0/dT) at state 1\n",
      "dB1_dT = 0.172*4.2*Tc**(4.2)*T_1**(-5.2);\t\t\t# (dB_1/dT) at state 1\n",
      "dB_dT = ((R*Tc)/Pc)*((dB0_dT) + w*(dB1_dT));\t\t\t# (dB/dT) at state 1\n",
      "# The residual entropy at the initial state is given by \n",
      "S_R_1 = -P_1*(dB_dT);\t\t\t#[J/mol-K]\n",
      "\n",
      "# Now let us calculate molar volume at initial state\n",
      "Tr = T_1/Tc;\t\t\t# Reduced temperature\n",
      "B_0 = 0.083-(0.422/(Tr)**(1.6));\n",
      "B_1 = 0.139-(0.172/(Tr)**(4.2));\n",
      "\n",
      "#We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "B = ((B_0+(w*B_1))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "V_1_3 = B + (R*T_1)/P_1;\t\t\t#[m**(3)/mol]\n",
      "# Therefore number of moles in the initial state is\n",
      "n_1_3 = Vol/V_1_3;\t\t\t#[mol]\n",
      "# Therefore final number of moles is\n",
      "n_2_3 = n_1_3 - n_withdrawn;\n",
      "\n",
      "# Therefore molar volume at final state is\n",
      "V_2_3 = Vol/n_2_3;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# Now let us determine the relation between pressure and temperature in the final state\n",
      "# P_2_3 = (R*T_2_3)/(V_2_3 - B_2)\n",
      "#delta_S = 0, thus delta_S_ig + delta_S_R = 0\n",
      "delta_S_R = - S_R_1;\n",
      "\n",
      "def f54(T): \n",
      "\t return Cp_0/T\n",
      "\n",
      "\n",
      "T_2_prime = 135;\t\t\t#[K]\n",
      "delta = 0.1;\n",
      "error = 10;\n",
      "while(error>0.01):\n",
      "    T_r = T_2_prime/Tc;\t\t\t# Reduced temperature\n",
      "    B_0_3 = 0.083-(0.422/(T_r)**(1.6));\n",
      "    B_1_3 = 0.139-(0.172/(T_r)**(4.2));\n",
      "    B_3 = ((B_0_3+(w*B_1_3))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol]\n",
      "    dB0_dT_3 = 0.422*1.6*Tc**(1.6)*T_2_prime**(-2.6);\t\t\t# (dB_0/dT)\n",
      "    dB1_dT_3 = 0.172*4.2*Tc**(4.2)*T_2_prime**(-5.2);\t\t\t# (dB_1/dT)\n",
      "    dB_dT_3 = ((R*Tc)/Pc)*((dB0_dT_3) + w*(dB1_dT_3));\t\t\t# (dB/dT)\n",
      "    P_2_3 = (R*T_2_prime)/(V_2_3 - B_3);\n",
      "    delta_S = 27.2*(math.log(T_2_prime/T_1)) + 4.2*10**(-3)*(T_2_prime - T_1) - R*(math.log(P_2_3/P_1)) - P_2_3*(dB_dT_3) + delta_S_R;\n",
      "    T_new = T_2_prime + delta;\n",
      "    error=abs(delta_S);\n",
      "    T_2_prime = T_new;\n",
      "\n",
      "T_2_3 = T_2_prime;\t\t\t#[K] - Final temperature\n",
      "\t\t\t# Therefore at T_2_3\n",
      "P_2_3 = P_2_3*10**(-5);\t\t\t#[bar] - Final pressure\n",
      "\n",
      "print \" 3).The final temperature %f K\"%(T_2_3);\n",
      "print \"     The final pressure %f bar\"%(P_2_3);\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The final temperature 131.761821 K\n",
        "     The final pressure 40.991361 bar\n",
        " 2).The final temperature 129.504151 K\n",
        "     The final pressure 40.288995 bar\n",
        " 3).The final temperature 141.300000 K\n",
        "     The final pressure 57.079997 bar\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.12  Page Number : 351"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of change of internal energy enthalpy entropy and exergy\n",
      "\n",
      "# Variables\n",
      "P_1 = 80.;\t\t\t        #[bar] - Initial pressure\n",
      "P_1 = P_1*10.**(5);\t\t\t#[Pa]\n",
      "T_1 = 300. + 273.15;\t\t#[T] - Initial temperature\n",
      "P_2 = 40.;\t\t\t        #[bar] - Final pressure\n",
      "P_2 = P_2*10**(5);\t\t\t#[Pa]\n",
      "T_2 = 300. + 273.15;\t\t#[K] - Final temperature\n",
      "T_0 = 25. + 273.15;\t\t\t#[K] - Surrounding temperature\n",
      "P_0 = 1.;\t\t\t        #[atm] - Surrounding pressure\n",
      "P_0 = P_0*101325;\t\t\t#[Pa]\n",
      "Tc = 647.1;\t\t\t        #[K]\n",
      "Pc = 220.55;\t\t\t    #[bar]\n",
      "Pc = Pc*10.**(5);\t\t\t#[Pa]\n",
      "R = 8.314;\t\t\t        #[J/mol*K] - Universal gas constant\n",
      "\n",
      "\n",
      "# Calculations\n",
      "# For van der Walls equation of state\n",
      "a = (27*R**(2)*Tc**(2))/(64*Pc);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = (R*Tc)/(8*Pc);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "\n",
      "def f(V): \n",
      "\t return V**(3)-(b+(R*T_1)/P_1)*V**(2)+(a/P_1)*V-(a*b)/P_1\n",
      "V_1_1=fsolve(f,0.1)\n",
      "V_1_2=fsolve(f,10)\n",
      "V_1_2=fsolve(f,100)\n",
      "# The largest root is considered because of vapour\n",
      "V_1 = V_1_1;\n",
      "\n",
      "U_R_1 = -a/V_1;\t\t\t#[J/mol] - Internal energy\n",
      "H_R_1 = P_1*V_1 - R*T_1 - a/V_1;\t\t\t#[J/mol] - Enthalpy\n",
      "S_R_1 = R*math.log((P_1*(V_1-b))/(R*T_1));\n",
      "\n",
      "def f1(V): \n",
      "\t return V**(3)-(b+(R*T_2)/P_2)*V**(2)+(a/P_2)*V-(a*b)/P_2\n",
      "V_2_1 = fsolve(f1,0.1)\n",
      "V_2_2 = fsolve(f1,10)\n",
      "V_2_3 = fsolve(f1,100)\n",
      "V_2 = V_2_1;\n",
      "\n",
      "U_R_2 = -a/V_2;\t\t\t#[J/mol] - Internal energy\n",
      "H_R_2 = P_2*V_2 - R*T_2 - a/V_2;\t\t\t#[J/mol] - Enthalpy\n",
      "S_R_2 = R*math.log((P_2*(V_2-b))/(R*T_2));\n",
      "\n",
      "delta_U_R = U_R_2 - U_R_1;\t\t\t#\n",
      "delta_H_R = H_R_2 - H_R_1;\t\t\t#\n",
      "delta_S_R = S_R_2 - S_R_1;\t\t\t#\n",
      "\n",
      "delta_U_ig = 0;\t\t\t#[J/mol] - As temperature is constant\n",
      "delta_H_ig = 0;\t\t\t#[J/mol] - As temperature is constant\n",
      "delta_S_ig = - R*math.log(P_2/P_1);\t\t\t# [J/mol-K]\n",
      "delta_U = delta_U_R + delta_U_ig;\t\t\t#[J/mol]\n",
      "delta_H = delta_H_R + delta_H_ig;\t\t\t#[J/mol]\n",
      "delta_S = delta_S_R + delta_S_ig;\t\t\t#[J/mol-K]\n",
      "\n",
      "\t\t\t# Change in exergy is given by\n",
      "\t\t\t# delta_phi = phi_1 - phi_2 = U_1 - U_2 + P_0*(V_1 - _V_2) - T_0*(S_1 - S_2)\n",
      "delta_phi = - delta_U + P_0*(V_1 - V_2) - T_0*(-delta_S);\t\t\t#[J/mol]\n",
      "\n",
      "# Results\n",
      "print \" The change in internal energy is %f J/mol\"%(delta_U);\n",
      "print \" The change in enthalpy is %f J/mol\"%(delta_H);\n",
      "print \" The change in entropy is %f J/mol-K\"%(delta_S);\n",
      "print \" The change in exergy is %f J/mol\"%(delta_phi);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The change in internal energy is 615.070900 J/mol\n",
        " The change in enthalpy is 1053.220316 J/mol\n",
        " The change in entropy is 6.930264 J/mol-K\n",
        " The change in exergy is 1389.940735 J/mol\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.13  Page Number : 353"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of change in enthalpy\n",
      "\n",
      "# Variables\n",
      "T_1 = 500.;\t\t\t#[K] - Initial temperature\n",
      "P_1 = 30.;\t\t\t#[atm] - Initial pressure\n",
      "P_1 = P_1*101325;\t\t\t#[Pa]\n",
      "P_2 = 1;\t\t\t#[atm] - Final pressure\n",
      "P_2 = P_2*101325;\t\t\t#[Pa]\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\t\t\t# For chlorine\n",
      "Tc = 417.2;\t\t\t#[K] - Critical temperature\n",
      "Pc = 77.10;\t\t\t#[bar] - Critical pressure\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "\n",
      "#Redlich Kwong equation of state,\n",
      "a = (0.42748*(R**(2))*(Tc**(2.5)))/Pc;\t\t\t# [Pa*m**(6)*K**(1/2)/mol]\n",
      "b = (0.08664*R*Tc)/Pc;\t\t\t# [m**(3)/mol]\n",
      "\n",
      "# Calculations\n",
      "def f1(V): \n",
      "\t return V**(3)-((R*T_1)/P_1)*V**(2)-((b**(2))+((b*R*T_1)/P_1)-(a/(T_1**(1./2)*P_1)))*V-(a*b)/(T_1**(1./2)*P_1)\n",
      "V_1=fsolve(f1,1)\n",
      "V_2=fsolve(f1,10)\n",
      "V_3=fsolve(f1,100)\n",
      "\n",
      "V = V_1;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "\n",
      "Z = (P_1*V_1)/(R*T_1);\t\t\t#compressibility factor\n",
      "\n",
      "# The residual enthalpy at state 1 is given by\n",
      "H_R_1 = (Z-1)*R*T_1 + ((3*a)/(2*b*T_1**(1./2)))*(math.log(V/(V+b)));\t\t\t#[J/mol]\n",
      " \n",
      "# Since chlorine is assumed to behave ideally under the final condition,therefore\n",
      "H_R_2 = 0;\t\t\t# Residual enthalpy at state 2\n",
      "delta_H_R = H_R_2 - H_R_1;\t\t\t#[J/mol] - Residual enthalpy change\n",
      "# and since isothermal conditions are maintained, therfore\n",
      "delta_H_ig = 0;\t\t\t# Enthalpy change under ideal condition\n",
      "delta_H = delta_H_R + delta_H_ig;\t\t\t#[J/mol]\n",
      "\n",
      "# Results\n",
      "print \" The change in enthalpy is given by delta_H = %f J/mol\"%(delta_H);\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The change in enthalpy is given by delta_H = 1053.558471 J/mol\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.14  Page Number : 353"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of final temperature\n",
      "\n",
      "#(1)\n",
      "#This part involves proving a relation in which no mathematical components are involved.\n",
      "#For proof refer to this example 10.14 on page number 354 of the book.\n",
      "\n",
      "\n",
      "#(2)\n",
      "\n",
      "# Variables\n",
      "Vol_1 = 0.1;\t\t\t#[m**(3)] - Initial volume of each compartment\n",
      "n_1 = 400;\t\t\t#[mol] - Initial number of moles in compartment 1\n",
      "V_1 = Vol_1/n_1;\t\t\t#[m**(3)/mol] - Molar volume at state 1\n",
      "T_1 = 294;\t\t\t#[K]\n",
      "Vol_2 = 0.2;\t\t\t#[m**(3)] - Final volume of the compartment after removing the partition.\n",
      "n_2 = n_1;\t\t\t#[mol] - Number of moles remains the same\n",
      "V_2 = Vol_2/n_2;\t\t\t#[m**(3)/mol] - Molar volume at state 2\n",
      "\n",
      "a = 0.1362;\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = 3.215*10**(-5);\t\t\t#[m**(3)/mol]\n",
      "Cv_0 = 12.56;\t\t\t#[J/mol-K] - Heat capacity in ideal gas state\n",
      "\n",
      "# Calculations\n",
      "# For overall system q = 0, and no work is done, therefore delta_U = 0\n",
      "# Therfore from the relation proved in part (1), we have\n",
      "T_2 = T_1 + (a/Cv_0)*(1/V_2 - 1/V_1);\t\t\t#[K]\n",
      "\n",
      "# Results\n",
      "print \" 2).The final temperatutre is %f K\"%(T_2)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 2).The final temperatutre is 272.312102 K\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.17  Page Number : 356"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Determination of work done and the exit temperature\n",
      "\n",
      "# Variables\n",
      "P_1 = 1*10**(6);\t\t\t#[Pa] - Initial pressure\n",
      "T_1 = 200 + 273.15;\t\t\t#[K] - Initial temperature\n",
      "P_2 = 8*10**(6);\t\t\t#[Pa]\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "Y = 1.4;\t\t\t# Index of expansion\n",
      "Cp_0 = 29.114;\t\t\t#[J/mol-K]\n",
      "\t\n",
      "a = 0.55366;\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = 3.049*10**(-5);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# Calculations\n",
      "V_1 = 3.816*10**(-3);\t\t\t#[m**(3)/mol]\n",
      "Z_1 = (P_1*V_1)/(R*T_1);\n",
      "\n",
      "T_2 = T_1*(P_2/P_1)**((Y-1)/Y);\t\t\t#[K]\n",
      "\n",
      "# At 8 MPa and T_2,\n",
      "# The molar volume of steam following van der Walls equation of state (as reported in the book) is\n",
      "V_2 = 8.41*10**(-4);\t\t\t#[m**(3)/mol]\n",
      "# And the compressibility factor is \n",
      "Z_2 = (P_2*V_2)/(R*T_2);\n",
      "\n",
      "# For van der Walls equation of state we know that\n",
      "# delta_S_R/R = math.log(Z_2/Z_1) + math.log((V_2 - b)/V_2) - math.log((V_1 - b)/V_1)\n",
      "delta_S_R = R*(math.log(Z_2/Z_1) + math.log((V_2 - b)/V_2) - math.log((V_1 - b)/V_1));\t\t\t#[J/mol]\n",
      "\n",
      "# delta_S_ig = Cp_0*math.log(T_2/T_1) - R*math.log(P_2/P_1)\n",
      "# The entropy change is therefore\n",
      "# delta_S = delta_S_ig + delta_S_R\n",
      "# But during an isentropic process the total entropy change is zero\n",
      "# Therefore we have to modify the exit temperature so that the entropy change is zero\n",
      "\n",
      "# Let us assume a temperature, say T = 870 K\n",
      "# At 870 K the molar volume of steam following van der Walls equation of state (as reported in the book) is\n",
      "#  V_3 = 8.57*10**(-4);\t\t\t# [m**(3)/mol]\n",
      "# Therefore\n",
      "# Z_3 = (P_2*V_3)/(R*T_2);\n",
      "# At this temperature,\n",
      "# delta_S = Cp_0*math.log(T/T_1) - R*math.log(P_2/P_1) + R*(math.log(Z/Z_1) + R*math.log((V - b)/V) - R*math.log((V_1 - b)/V_1))\n",
      "\n",
      "T = 800;\t\t\t#[K]\n",
      "fault=10;\n",
      "\n",
      "def f1(V): \n",
      "\t return V**(3)-(b+(R*T)/P_2)*V**(2)+(a/P_2)*V-(a*b)/P_2\n",
      "\t \n",
      "def f2(T): \n",
      "\t return  Cp_0*math.log(T/T_1) - R*math.log(P_2/P_1) + R*(math.log(Z/Z_1) + R*(math.log((V - b)/V)) - R*(math.log((V_1 - b)/V_1)))\n",
      "\n",
      "\n",
      "while(fault>0.3):\n",
      "    \t\t\t# At T and 8 MPa\n",
      "    V = fsolve(f1,1)\n",
      "    Z = (P_2*V)/(R*T);\n",
      "    \n",
      "    T_exit = fsolve(f2,0.1)\n",
      "    fault=abs(T-T_exit);\n",
      "    T = T + 0.5;\n",
      "\n",
      "Texit = T;\n",
      "\n",
      "# Now applying the first law to an adiabatic process we get\n",
      "# W = - delta_H\n",
      "\n",
      "# For van der Walls gas the enthalpy change is given by\n",
      "delta_H_s = Cp_0*(T_exit - T_1) + (Z - 1)*R*T_exit - a/V - (Z_1-1)*R*T_1 + a/V_1;\t\t\t#[J/mol]\n",
      "W = - delta_H_s;\t\t\t#[J/mol]\n",
      "\n",
      "# Results\n",
      "print \" 1).The exit temperature is %f K\"%(Texit);\n",
      "print \"     The work required is given by W = %f J/mol\"%(W);\n",
      "\n",
      "#(2)\n",
      "eff = 0.8;\t\t\t# Adiabatic efficiency\n",
      "delta_H_a = eff*delta_H_s;\t\t\t#[J/mol] - Actual enthalpy change\n",
      "W_2 = - delta_H_a;\n",
      "\n",
      "\t\t\t# Let us assume a temperature, say\n",
      "T_prime= 900;\t\t\t#[K]\n",
      "fault1=10;\n",
      "def f22(V): \n",
      "\t return V**(3)-(b+(R*T_prime)/P_2)*V**(2)+(a/P_2)*V-(a*b)/P_2\n",
      "\n",
      "def f3(T_prime): \n",
      "\t return  Cp_0*(T_prime - T_1) + (Z_prime - 1)*R*T_prime - a/V_prime - 13230.49\n",
      "\t \n",
      "while(fault1>0.3):\n",
      "    \t\t\t# At T_prime and 8 MPa\n",
      "    V_prime=fsolve(f22,1)\n",
      "    Z_prime = (P_2*V_prime)/(R*T_prime);\n",
      "    T_exit1 = fsolve(f3,100)\n",
      "    fault1=abs(T_prime-T_exit1);\n",
      "    T_prime = T_prime + 0.2;\n",
      "\n",
      "Texit1 = T_prime;\n",
      "\n",
      "print \" 2).The exit temperature is %f K\"%(Texit1);\n",
      "print \"     The work required is given by W = %f J/mol\"%(W_2);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The exit temperature is 916.500000 K\n",
        "     The work required is given by W = -12195.093996 J/mol\n",
        " 2).The exit temperature is 958.400000 K"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "     The work required is given by W = -9756.075197 J/mol\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.19  Page Number : 359"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of molar volume and fugacity\n",
      "\n",
      "# Variables\n",
      "T = 100 + 273.15;\t\t\t#[K] - Temperature\n",
      "Tc = 647.1;\t\t\t#[K] - Critical temperature of water\n",
      "Pc = 220.55;\t\t\t#[bar] - Critical pressure of water\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "# Calculations\n",
      "a = (27*R**(2)*Tc**(2))/(64*Pc);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = (R*Tc)/(8*Pc);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# The cubic form of van der Walls equation of state is given by,\n",
      "# V**(3) - (b + (R*T)/P)*V**(2) + (a/P)*V - (a*b)/P = 0\n",
      "\n",
      "# For water vapour at 100 C under saturated conditions pressure is 1 atm, therefore\n",
      "P = 1;\t\t\t#[atm]\n",
      "P = P*101325;\t\t\t#[Pa]\n",
      "\n",
      "# At 100 C and 1 atm \n",
      "def f(V): \n",
      "\t return V**(3)-(b+(R*T)/P)*V**(2)+(a/P)*V-(a*b)/P\n",
      "V_1 = fsolve(f,0.1)\n",
      "V_1 = fsolve(f,10)\n",
      "V_1 = fsolve(f,100)\n",
      "V = V_1;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "\n",
      "f = P*(math.exp(math.log((R*T)/(P*(V-b))) + b/(V-b) - (2*a)/(R*T*V)));\t\t\t#[Pa]\n",
      "f = f/101325;\t\t\t#[atm]\n",
      "\n",
      "# Results\n",
      "print \" The molar volume is %f m**3)/mol\"%(V);\n",
      "print \" The fugacity is %f atm\"%(f);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The molar volume is 0.030469 m**3)/mol\n",
        " The fugacity is 0.995168 atm\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.20  Page Number : 359"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of enthalpy and entropy change\n",
      "\n",
      "# Variables\n",
      "P_1 = 6;\t\t\t#[bar] - Initial pressure\n",
      "P_1 = P_1*10**(5);\t\t\t#[Pa]\n",
      "T_1 = 100 + 273.15;\t\t\t#[T] - Initial temperature\n",
      "P_2 = 12;\t\t\t#[bar] - Final pressure\n",
      "P_2 = P_2*10**(5);\t\t\t#[Pa]\n",
      "T_2 = 500 + 273.15;\t\t\t#[K] - Final temperature\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "Y = 1.126;\t\t\t# Index of expansion\n",
      "Cp_0 = (R*Y)/(Y-1);\t\t\t#[J/mol-K]\n",
      "\n",
      "# For propane\n",
      "Tc = 369.8;\t\t\t#[K]\n",
      "Pc = 42.48;\t\t\t#[bar]\n",
      "Pc = Pc*10**(5);\n",
      "w = 0.152;\n",
      "\n",
      "# Calculations and Results\n",
      "#(1)\n",
      "# For van der Walls equation of state\n",
      "a = (27*R**(2)*Tc**(2))/(64*Pc);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = (R*Tc)/(8*Pc);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# The cubic form of van der Walls equation of state is given by,\n",
      "# V**(3) - (b + (R*T)/P)*V**(2) + (a/P)*V - (a*b)/P = 0\n",
      "\n",
      "# At state 1 (100 C and 6 bar) \n",
      "def f(V): \n",
      "\t return V**(3)-(b+(R*T_1)/P_1)*V**(2)+(a/P_1)*V-(a*b)/P_1\n",
      "V_1_1 = fsolve(f,1)\n",
      "V_1_2 = fsolve(f,10)\n",
      "V_1_3 = fsolve(f,100)\n",
      "# The largest root is considered because of molar volume of vapour phase is to determined\n",
      "V_1 = V_1_1;\t\t\t#[m**(3)/mol]\n",
      "# Thus compressibility factor is\n",
      "Z_1 = (P_1*V_1)/(R*T_1);\t\t\t#compressibility factor\n",
      "\n",
      "H_R_1 = (Z_1 - 1)*R*T_1 - (a/V_1);\t\t\t# [J/mol]\n",
      "S_R_1 = R*math.log((P_1*(V_1-b))/(R*T_1));\t\t\t# [J/mol-K]\n",
      "\n",
      "# At state 2 (500 C and 12 bar) \n",
      "def f1(V): \n",
      "    return V**(3)-(b+(R*T_2)/P_2)*V**(2)+(a/P_2)*V-(a*b)/P_2\n",
      "V_2_1 = fsolve(f1,1)\n",
      "V_2_2 = fsolve(f1,10)\n",
      "V_2_3 = fsolve(f1,100)\n",
      "# The largest root is considered because of molar volume of vapour phase is to determined\n",
      "V_2 = V_2_1;\t\t\t#[m**(3)/mol]\n",
      "# Thus compressibility factor is\n",
      "Z_2 = (P_2*V_2)/(R*T_2);\t\t\t#compressibility factor\n",
      "\n",
      "H_R_2 = (Z_2 - 1)*R*T_2 - (a/V_2);\t\t\t# [J/mol]\n",
      "S_R_2 = R*math.log((P_2*(V_2-b))/(R*T_2));\t\t\t# [J/mol-K]\n",
      "\n",
      "# Ideal gas entropy change is given by\n",
      "delta_S_ig = Cp_0*math.log(T_2/T_1) - R*math.log(P_2/P_1);\t\t\t#[J/mol-K]\n",
      "# Entropy change is given by\n",
      "delta_S = delta_S_ig + (S_R_2 - S_R_1);\t\t\t#[J/mol-k]\n",
      "\n",
      "# Ideal gas enthalpy change is given by\n",
      "delta_H_ig = Cp_0*(T_2 - T_1);\t\t\t#[J/mol]\n",
      "# Enthalpy change is given by\n",
      "delta_H = delta_H_ig + (H_R_2 - H_R_1);\t\t\t#[J/mol]\n",
      "\n",
      "print \"1).The change in enthalpy is %f J/mol\"%(delta_H);\n",
      "print \"    The change in entropy is %f J/mol-K\"%(delta_S);\n",
      "\n",
      "#(2)\n",
      "# Virial equation of state\n",
      "\n",
      "# At state 1 (372.15 K, 6 bar) let us calculate B and dB/dT\n",
      "Tr = T_1/Tc;\t\t\t# Reduced temperature\n",
      "B_0 = 0.083-(0.422/(Tr)**(1.6));\n",
      "B_1 = 0.139-(0.172/(Tr)**(4.2));\n",
      "\n",
      "#We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "B = ((B_0+(w*B_1))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol]\n",
      "dB0_dT = 0.422*1.6*Tc**(1.6)*T_1**(-2.6);\t\t\t# (dB_0/dT) at state 1\n",
      "dB1_dT = 0.172*4.2*Tc**(4.2)*T_1**(-5.2);\t\t\t# (dB_1/dT) at state 1\n",
      "dB_dT = ((R*Tc)/Pc)*((dB0_dT) + w*(dB1_dT));\t\t\t# (dB/dT) at state 1\n",
      "\n",
      "H_R_1_2 = B*P_1 - P_1*T_1*dB_dT;\t\t\t#[J/mol] - Residual enthalpy at state 1\n",
      "S_R_1_2 = -P_1*(dB_dT);\t\t\t#[J/mol-K] - Residual entropy at state 1\n",
      "\n",
      "# At state 2 (773.15 K, 12 bar)\n",
      "Tr_2 = T_2/Tc;\t\t\t# Reduced temperature\n",
      "B_0_2 = 0.083-(0.422/(Tr_2)**(1.6));\n",
      "B_1_2 = 0.139-(0.172/(Tr_2)**(4.2));\n",
      "\n",
      "#We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "B_2 = ((B_0_2+(w*B_1_2))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol]\n",
      "dB0_dT_2 = 0.422*1.6*Tc**(1.6)*T_2**(-2.6);\t\t\t# (dB_0/dT) at state 1\n",
      "dB1_dT_2 = 0.172*4.2*Tc**(4.2)*T_2**(-5.2);\t\t\t# (dB_1/dT) at state 1\n",
      "dB_dT_2 = ((R*Tc)/Pc)*((dB0_dT_2) + w*(dB1_dT_2));\t\t\t# (dB/dT) at state 1\n",
      "\n",
      "H_R_2_2 = B_2*P_2 - P_2*T_2*dB_dT_2;\t\t\t#[J/mol] - Residual enthalpy at state 1\n",
      "S_R_2_2 = -P_2*(dB_dT_2);\t\t\t#[J/mol-K] - Residual entropy at state 1\n",
      "\n",
      "delta_H_2 = delta_H_ig + (H_R_2_2 - H_R_1_2);\t\t\t#[J/mol]\n",
      "delta_S_2 = delta_S_ig + (S_R_2_2 - S_R_1_2);\t\t\t#[J/mol]\n",
      "\n",
      "print \"2).The change in enthalpy is %f J/mol\"%(delta_H_2);\n",
      "print \"    The change in entropy is %f J/mol-K\"%(delta_S_2);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1).The change in enthalpy is 29798.211799 J/mol\n",
        "    The change in entropy is 48.649067 J/mol-K\n",
        "2).The change in enthalpy is 29992.807365 J/mol\n",
        "    The change in entropy is 49.021724 J/mol-K\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.21  Page Number : 362"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of fugacity\n",
      "\n",
      "# Variables\n",
      "P = 2.76*10**(6);\t\t\t#[N/m**(2)] - Pressure\n",
      "T = 310.93;\t\t\t#[K] - Temperature\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "# For n-butane\n",
      "Tc = 425.18;\t\t\t#[K] - Critical temperature\n",
      "Pc = 37.97;\t\t\t#[bar] - Critical pressure\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "w = 0.193;\n",
      "den = 0.61;\t\t\t#[g/cm**(3)]\n",
      "mol_wt = 58;\t\t\t#[g/mol] - Molecular weight of butane\n",
      "\n",
      "# Calculations and Results\n",
      "# math.log(P_sat) = 15.7374 - 2151.63/(T-36.24)\n",
      "P_sat = math.exp(15.7374 - 2151.63/(T-36.24));\t\t\t#[mm Hg]\n",
      "P_sat = (P_sat/760)*101325;\t\t\t#[N/m**(2)]\n",
      "\n",
      "#(1)\n",
      "# Let us determine the second virial coefficient at 310.93 K\n",
      "Tr = T/Tc;\t\t\t# Reduced temperature\n",
      "B_0 = 0.083-(0.422/(Tr)**(1.6));\n",
      "B_1 = 0.139-(0.172/(Tr)**(4.2));\n",
      "#We know,(B*Pc)/(R*Tc) = B_0 + (w*B_1)\n",
      "B = ((B_0+(w*B_1))*(R*Tc))/Pc;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# Fugacity under saturated conditions is given by\n",
      "# math.log(f_sat/P_sat) = (B*P_sat)/(R*T)\n",
      "f_sat = P_sat*(math.exp((B*P_sat)/(R*T)));\t\t\t#[N/m**(2)]\n",
      "\n",
      "# The molar volume is given by\n",
      "V_liq = (1/(den*1000))*(mol_wt/1000);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "f = f_sat*math.exp(V_liq*(P-P_sat)/(R*T));\n",
      "\n",
      "print \" 1).The fugacity of n-butane is %e N/m**2)\"%(f);\n",
      "\n",
      "#(2)\n",
      "# For van der Walls equation of state\n",
      "a = (27*R**(2)*Tc**(2))/(64*Pc);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = (R*Tc)/(8*Pc);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# The cubic form of van der Walls equation of state is given by,\n",
      "# V**(3) - (b + (R*T)/P)*V**(2) + (a/P)*V - (a*b)/P = 0\n",
      "\n",
      "# At 100 C and 1 atm \n",
      "def f(V): \n",
      "    return V**(3)-(b+(R*T)/P)*V**(2)+(a/P)*V-(a*b)/P\n",
      "V_1 = fsolve(f,0.1)\n",
      "V_1 = fsolve(f,10)\n",
      "V_1 = fsolve(f,100)\n",
      "# The above equation has only 1 real root, other two roots are imaginary\n",
      "V = V_1;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# math.log(f/P) = math.log((R*T)/(P*(V-b))) + b/(V-b) -(2*a)/(R*T*V)\n",
      "f_2 = P*(math.exp(math.log((R*T)/(P*(V-b))) + b/(V-b) -(2*a)/(R*T*V)));\n",
      "\n",
      "print \" 2).The fugacity of n-butane is %e N/m**2)\"%(f_2);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The fugacity of n-butane is 3.293476e+05 N/m**2)\n",
        " 2).The fugacity of n-butane is 8.997352e+05 N/m**2)\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.22  Page Number : 363"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of enthalpy change\n",
      "\n",
      "# Variables\n",
      "T = 50+273.15;\t\t\t#[K] - Temperature\n",
      "P = 25.*10**(3);\t\t\t#[Pa] - Pressure\n",
      "y1 = 0.5;\t\t\t#[mol] - mole fraction of equimolar mixture\n",
      "y2 = 0.5;\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "#For component 1 (methyl ethyl ketone)\n",
      "Tc_1  = 535.5;\t\t\t#[K] - Critical temperature\n",
      "Pc_1 = 41.5*10**(5);\t\t\t#[N/m**(2)] - Critical pressure\n",
      "Vc_1 = 267.;\t\t\t#[cm**(3)/mol] - Critical volume\n",
      "Zc_1 = 0.249;\t\t\t# Critical compressibility factor\n",
      "w_1 = 0.323;\t\t\t# acentric factor\n",
      "\n",
      "#For component 2 (toluene)\n",
      "Tc_2 = 591.8;\t\t\t#[K]\n",
      "Pc_2 = 41.06*10**(5);\t\t\t#[N/m**(2)]\n",
      "Vc_2 = 316.;\t\t\t#[cm**(3)/mol]\n",
      "Zc_2 = 0.264;\n",
      "w_2 = 0.262;\n",
      "\n",
      "# Calculations and Results\n",
      "# For equation of state Z = 1 + B/V\n",
      "#For component 1, let us calculate B and dB/dT\n",
      "Tr_1 = T/Tc_1;\t\t\t#Reduced temperature\n",
      "#At reduced temperature\n",
      "B1_0 = 0.083-(0.422/(Tr_1)**(1.6));\n",
      "B1_1 = 0.139-(0.172/(Tr_1)**(4.2));\n",
      "#We know,(B*Pc)/(R*Tc) = B_0+(w*B_1)\n",
      "B_11 = ((B1_0+(w_1*B1_1))*(R*Tc_1))/Pc_1;\t\t\t# [m**(3)/mol-K] \n",
      "dB0_dT_1 = 0.422*1.6*Tc_1**(1.6)*T**(-2.6);\t\t\t# [m**(3)/mol-K] -  (dB_0/dT)\n",
      "dB1_dT_1 = 0.172*4.2*Tc_1**(4.2)*T**(-5.2);\t\t\t# [m**(3)/mol-K] -  (dB_1/dT)\n",
      "dB_dT_1 = ((R*Tc_1)/Pc_1)*((dB0_dT_1) + w_1*(dB1_dT_1));\t\t\t#[m**(3)/mol-K] -  (dB/dT)_\n",
      "\n",
      "#Similarly for component 2\n",
      "Tr_2 = T/Tc_2;\t\t\t#Reduced temperature\n",
      "#At reduced temperature Tr_2,\n",
      "B2_0 = 0.083 - (0.422/(Tr_2)**(1.6));\n",
      "B2_1 = 0.139 - (0.172/(Tr_2)**(4.2));\n",
      "B_22 = ((B2_0+(w_2*B2_1))*(R*Tc_2))/Pc_2;\t\t\t#[m**(3)/mol]\n",
      "dB0_dT_2 = 0.422*1.6*Tc_2**(1.6)*T**(-2.6);\t\t\t# [m**(3)/mol-K] -  (dB_0/dT)\n",
      "dB1_dT_2 = 0.172*4.2*Tc_2**(4.2)*T**(-5.2);\t\t\t# [m**(3)/mol-K] -  (dB_1/dT)\n",
      "dB_dT_2 = ((R*Tc_2)/Pc_2)*((dB0_dT_2) + w_2*(dB1_dT_2));\t\t\t#[m**(3)/mol-K] -  (dB/dT)_\n",
      "\n",
      "#For cross coeffcient, let us calculate B and dB/dT\n",
      "Tc_12 = (Tc_1*Tc_2)**(1./2);\t\t\t#[K]\n",
      "w_12 = (w_1 + w_2)/2.;\n",
      "Zc_12 = (Zc_1 + Zc_2)/2;\n",
      "Vc_12 = (((Vc_1)**(1./3) + (Vc_2)**(1./3))/2)**(3);\t\t\t#[cm**(3)/mol]\n",
      "Vc_12 = Vc_12*10**(-6);\t\t\t#[m**(3)/mol]\n",
      "Pc_12 = (Zc_12*R*Tc_12)/Vc_12;\t\t\t#[N/m**(2)]\n",
      "\n",
      "#Now we have,(B_12*Pc_12)/(R*Tc_12) = B_0+(w_12*B_1)\n",
      "#where B_0 and B_1 are to be evaluated at Tr_12\n",
      "Tr_12 = T/Tc_12;\n",
      "#At reduced temperature Tr_12\n",
      "B_0 = 0.083 - (0.422/(Tr_12)**(1.6));\n",
      "B_1 = 0.139 - (0.172/(Tr_12)**(4.2));\n",
      "B_12 = ((B_0 + (w_12*B_1))*R*Tc_12)/Pc_12;\t\t\t#[m**(3)/mol]\n",
      "dB0_dT_12 = 0.422*1.6*Tc_12**(1.6)*T**(-2.6);\t\t\t# [m**(3)/mol-K] -  (dB_0/dT)\n",
      "dB1_dT_12 = 0.172*4.2*Tc_12**(4.2)*T**(-5.2);\t\t\t# [m**(3)/mol-K] -  (dB_1/dT)\n",
      "dB_dT_12 = ((R*Tc_12)/Pc_12)*((dB0_dT_12) + w_12*(dB1_dT_12));\t\t\t#[m**(3)/mol-K] -  (dB/dT)_12\n",
      "\n",
      "#For the mixture\n",
      "B = y1**(2)*B_11 + 2*y1*y2*B_12 + y2**(2)*B_22;\t\t\t#[m**(3)/moL]\n",
      "\n",
      "# The equation of state can be written as\n",
      "# V**(2) - ((R*T)/P) - (B*R*T)/P = 0\n",
      "# V**(2) - 0.1075*V + 1.737*10**(-4) = 0\n",
      "def f(V): \n",
      "    return V**(2) - 0.1075*V + 1.737*10**(-4)\n",
      "V1 = fsolve(f,0.1)\n",
      "V2 = fsolve(f,1)\n",
      "# We will consider the root which is near to R*T/P\n",
      "V = V1;\n",
      "# dB/dT = y_1**(2)*dB_11/dT + y_2**(2)*dB_22/dT + 2*y_1*y_2*dB_12/dT\n",
      "dB_dT = y1**(2)*dB_dT_1 + y2**(2)*dB_dT_2 + 2*y1*y2*dB_dT_12;\t\t\t#[m**(3)/mol-K]\n",
      "\n",
      "# For equation of state Z = 1 + B/V\n",
      "H_R = (B*R*T)/V - ((R*T**(2))/V)*dB_dT;\t\t\t#[J/mol]\n",
      "\n",
      "print \" 1).The value of H_R for the mixture using virial equation of state is %f J/mol\"%(H_R);\n",
      "\n",
      "#(2)\n",
      "#  For van der Walls equation of state \n",
      "a_11 = (27*R**(2)*Tc_1**(2))/(64*Pc_1);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "a_22 = (27*R**(2)*Tc_2**(2))/(64*Pc_2);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "a_12 = (a_11*a_22)**(1./2);\n",
      "b_1 = (R*Tc_1)/(8*Pc_1);\t\t\t#[m**(3)/mol]\n",
      "b_2 = (R*Tc_2)/(8*Pc_2);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# For the mixture\n",
      "a = y1**(2)*a_11 + y2**(2)*a_22 + 2*y1*y2*a_12;\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = y1*b_1 + y2*b_2;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# From the cubic form of van der Walls equation of state\n",
      "def f1(V): \n",
      "    return V**(3)-(b+(R*T)/P)*V**(2)+(a/P)*V-(a*b)/P\n",
      "V2_1 = fsolve(f1,0.1)\n",
      "V2_2 = fsolve(f1,10)\n",
      "V2_3 = fsolve(f1,100)\n",
      "# The largest root is considered\n",
      "V_2 = V2_1;\n",
      "\n",
      "# The residual enthalpy is given by\n",
      "H_R_2 = P*V_2 - R*T -a/V_2;\t\t\t#[J/mol]\n",
      "\n",
      "print \" 2).The value of H_R for the mixture using van der Walls equation of state is %f J/mol\"%(H_R_2);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The value of H_R for the mixture using virial equation of state is -151.289795 J/mol\n",
        " 2).The value of H_R for the mixture using van der Walls equation of state is -38.476127 J/mol\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.23  Page Number : 366"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of fugacity of water vapour\n",
      "\n",
      "# Variables\n",
      "T = 320 + 273.15;\t\t\t#[K]\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "\n",
      "# For water\n",
      "Tc = 647.1;\t\t\t#[K]\n",
      "Pc = 220.55;\t\t\t#[bar]\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "\n",
      "# The cubic form of Redlich Kwong equation of state is given by,\n",
      "# V**(3) - ((R*T)/P)*V**(2) - ((b_1**(2)) + ((b_1*R*T)/P) - (a/(T**(1/2)*P))*V - (a*b)/(T**(1/2)*P) = 0\n",
      "\n",
      "# At 320 C and 70 bar pressure\n",
      "P_1 = 70;\t\t\t#[bar]\n",
      "P_1 = P_1*10**(5);\t\t\t#[Pa]\n",
      "\n",
      "# Calculations and Results\n",
      "a = (0.42748*(R**(2))*(Tc**(2.5)))/Pc;\t\t\t#[Pa*m**(6)*K**(1/2)/mol]\n",
      "b = (0.08664*R*Tc)/Pc;\t\t\t#[m**(3)/mol]\n",
      "# Solving the cubic equation\n",
      "def f1(V): \n",
      "    return V**(3)-((R*T)/P_1)*V**(2)-((b**(2))+((b*R*T)/P_1)-(a/(T**(1./2)*P_1)))*V-(a*b)/(T**(1./2)*P_1)\n",
      "V1=fsolve(f1,1)\n",
      "V2=fsolve(f1,10)\n",
      "V3=fsolve(f1,100)\n",
      "# The largest root is considered because at 320 C and 70 bar vapour phase exists.\n",
      "V_1 = V1;\t\t\t#[m**(3)/mol]\n",
      "# Thus compressibility factor is\n",
      "Z_1 = (P_1*V_1)/(R*T);\n",
      "\n",
      "# For Redlich-Kwong equation of state\n",
      "# math.log(f/P) = Z - 1 - math.log(V_1/(V_1-b)) + (a/(b*R*(T**(3/2))))*math.log(V/(V+b))\n",
      "f_1 = P_1*(math.exp(Z_1-1-math.log(Z_1)+math.log(V_1/(V_1-b))+(a/(b*R*(T**(3./2))))*math.log(V_1/(V_1+b))));\t\t\t#[Pa]\n",
      "f_1 = f_1*10**(-5);\t\t\t#[bar]\n",
      "\n",
      "print \" The fugacity of water vapour at 320 C and 70 bar pressure is %f bar\"%(f_1);\n",
      "\n",
      "# At 320 C and 170 bar pressure, we have\n",
      "P_2 = 170;\t\t\t#[bar]\n",
      "P_2 = P_2*10**(5);\t\t\t#[Pa]\n",
      "\n",
      "# Solving the cubic equation\n",
      "def f2(V): \n",
      "    return V**(3)-((R*T)/P_2)*V**(2)-((b**(2))+((b*R*T)/P_2)-(a/(T**(1./2)*P_2)))*V-(a*b)/(T**(1./2)*P_2)\n",
      "V4 = fsolve(f2,1)\n",
      "V5 = fsolve(f2,10)\n",
      "V6 = fsolve(f2,100)\n",
      "# The above equation has only 1 real root,other two roots are imaginary. Therefore,\n",
      "V_2 = V6;\t\t\t#[m**(3)/mol]\n",
      "# Thus compressibility factor is\n",
      "Z_2 = (P_2*V_2)/(R*T);\n",
      "\n",
      "# For Redlich-Kwong equation of state\n",
      "# math.log(f/P) = Z - 1 - math.log(V_1/(V_1-b)) + (a/(b*R*(T**(3/2))))*math.log(V/(V+b))\n",
      "f_2 = P_2*(math.exp(Z_2-1-math.log(Z_2)+math.log(V_2/(V_2-b))+(a/(b*R*(T**(3./2))))*math.log(V_2/(V_2+b))));\t\t\t#[Pa]\n",
      "f_2 = f_2*10**(-5);\t\t\t#[bar]\n",
      "\n",
      "print \" The fugacity of water vapour at 320 C and 170 bar pressure is %f bar\"%(f_2);\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The fugacity of water vapour at 320 C and 70 bar pressure is 60.456239 bar\n",
        " The fugacity of water vapour at 320 C and 170 bar pressure is 101.590989 bar\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stderr",
       "text": [
        "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py:227: RuntimeWarning: The iteration is not making good progress, as measured by the \n",
        "  improvement from the last ten iterations.\n",
        "  warnings.warn(msg, RuntimeWarning)\n"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.24  Page Number : 367"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Determination of change in internal energy\n",
      "\n",
      "# Variables\n",
      "Vol = 0.057;\t\t\t#[m**(3)] - Volume of car tyre\n",
      "P_1 = 300.;\t\t\t#[kPa] - Initial pressure\n",
      "P_1 = P_1*10**(3);\t\t\t#[Pa]\n",
      "T_1 = 300.;\t\t\t#[K] - Initial temperature\n",
      "P_2 = 330.;\t\t\t#[kPa] - Finnal pressure\n",
      "P_2 = P_2*10**(3);\t\t\t#[Pa]\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "Cv_0 = 21.;\t\t\t#[J/mol-K] - Heat capacity for air \n",
      "\n",
      "# For oxygen\n",
      "Tc_O2 = 154.6;\t\t\t#[K] - Critical temperature\n",
      "Pc_O2 = 50.43;\t\t\t#[bar] - Critical pressure\n",
      "Pc_O2 = Pc_O2*10**(5);\t\t\t#[Pa]\n",
      "y1 = 0.21;\t\t\t# - Mole fraction of oxygen\n",
      "# For nitrogen\n",
      "Tc_N2 = 126.2;\t\t\t#[K] - Critical temperature\n",
      "Pc_N2 = 34.00;\t\t\t#[bar] - Critical pressure\n",
      "Pc_N2 = Pc_N2*10**(5);\t\t\t#[Pa]\n",
      "y2 = 0.79;\t\t\t# - Mole fraction of nitrogen\n",
      "\n",
      "# Calculations and Results\n",
      "# (1)\n",
      "# Assuming ideal gas behaviour. The volume remains the same,therefore,we get\n",
      "# P_1/T_1 = P_2/T_2\n",
      "T_2 = P_2*(T_1/P_1);\t\t\t#[K]\n",
      "\n",
      "n = (P_1*Vol)/(R*T_1);\t\t\t#[mol] - Number of moles\n",
      "delta_U = n*Cv_0*(T_2-T_1);\t\t\t#[J]\n",
      "\n",
      "print \" 1).The change in internal energy for ideal gas behaviour) is %f J\"%(delta_U);\n",
      "\n",
      "#(2)\n",
      "#  For van der Walls equation of state \n",
      "a_O2 = (27*R**(2)*Tc_O2**(2))/(64*Pc_O2);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "a_N2 = (27*R**(2)*Tc_N2**(2))/(64*Pc_N2);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "a_12 = (a_O2*a_N2)**(1./2);\n",
      "b_O2 = (R*Tc_O2)/(8*Pc_O2);\t\t\t#[m**(3)/mol]\n",
      "b_N2 = (R*Tc_N2)/(8*Pc_N2);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# For the mixture\n",
      "a = y1**(2)*a_O2 + y2**(2)*a_N2 + 2*y1*y2*a_12;\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = y1*b_O2 + y2*b_N2;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# From the cubic form of van der Walls equation of state\n",
      "# At 300 K and 300 kPa,\n",
      "def f1(V): \n",
      "    return V**(3)-(b+(R*T_1)/P_1)*V**(2)+(a/P_1)*V-(a*b)/P_1\n",
      "V_1 = fsolve(f1,0.1)\n",
      "V_2 = fsolve(f1,10)\n",
      "V_3 = fsolve(f1,100)\n",
      "# The above equation has only 1 real root, other two roots are imaginary\n",
      "V = V_1;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# Now at P = 330 kPa and at molar volume V\n",
      "# The van der Walls equation of state is\n",
      "# (P + a/V**(2))*(V - b) = R*T\n",
      "T_2_prime = ((P_2 + a/V**(2))*(V - b))/R;\t\t\t#[K]\n",
      "n_prime = Vol/V;\t\t\t#[mol]\n",
      "\n",
      "# Total change in internal energy is given by\n",
      "# delta_U_prime = n_prime*delta_U_ig + n_prime*(U_R_2 - U_R_1)\n",
      "# delta_U_prime = n_prime*Cv_0*(T_2_prime - T_1) + n_prime*a(1/V_2 - 1/V_1);\n",
      "# Since V_1 = V_2 = V, therefore\n",
      "delta_U_prime = n_prime*Cv_0*(T_2_prime - T_1);\n",
      "\n",
      "print \" 2).The change in internal energy for van der Walls equation of state) is %f J\"%(delta_U_prime);\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The change in internal energy for ideal gas behaviour) is 4319.220592 J\n",
        " 2).The change in internal energy for van der Walls equation of state) is 4299.872282 J\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.25  Page Number : 369"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of enthalpy and entropy change\n",
      "\n",
      "# Variables\n",
      "T_1 = 150 + 273.15;\t\t\t#[K] - Initial emperature\n",
      "T_2 = T_1;\t\t\t# Isothermal process\n",
      "P_1 = 100.*10**(3);\t\t\t#[Pa] - Initial pressure\n",
      "P_2 = 450.*10**(3);\t\t\t#[Pa] - Final pressure\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "# For water\n",
      "Tc = 647.1;\t\t\t#[K] - Critical temperature\n",
      "Pc = 220.55;\t\t\t#[bar] - Critical pressure\n",
      "Pc = Pc*10.**(5);\n",
      "w = 0.345;\n",
      "Mol_wt = 18.015;\t\t\t#[g/mol] - Molecular weight of water\n",
      "Cp_0 = 4.18;\t\t\t#[J/mol-K] - Smath.tan(math.radiansard heat capacity of water\n",
      "\n",
      "# Calculations\n",
      "# Both phases are superheated vapour phases because at 150 C the vapour pressure of steam is 4.67 bar and both operating pressures are below saturated pressure.\n",
      "# In Peng-Robinson equation of state\n",
      "m = 0.37464 + 1.54226*w - 0.26992*w**(2);\n",
      "# At T_1 and P_1, we have\n",
      "Tr = T_1/Tc;\n",
      "alpha = (1 + m*(1 - Tr**(1./2)))**(2);\n",
      "a = ((0.45724*(R*Tc)**(2))/Pc)*alpha;\t\t\t#[Pa*m**(6)/mol**(2)]\n",
      "b = (0.07780*R*Tc)/Pc;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# Cubuc form of Peng-Robinson equation of stste is given by\n",
      "# V**(3)+(b-(R*T)/P)*V**(2)-((3*b**(2))+((2*R*T*b)/P)-(a/P))*V+b**(3)+((R*T*(b**(2))/P)-((a*b)/P)=0;\n",
      "# Solving the cubic equation\n",
      "def f(V): \n",
      "    return V**(3)+(b-(R*T_1)/P_1)*V**(2)-((3*b**(2))+((2*R*T_1*b)/P_1)-(a/P_1))*V+b**(3)+((R*T_1*(b**(2)))/P_1)-((a*b)/P_1)\n",
      "V1 = fsolve(f,-1)\n",
      "V2 = fsolve(f,0)\n",
      "V3 = fsolve(f,1)\n",
      "#The largest root is for vapour phase,\n",
      "#The largest root is only considered as the systemis gas\n",
      "V_1 = V3;\t\t\t#[m**(3)/mol]\n",
      "# Thus compressibility factor is\n",
      "Z_1 = (P_1*V_1)/(R*T_1);\n",
      "\n",
      "# At T_2 and P_2, we have\n",
      "# Cubuc form of Peng-Robinson equation of stste is given by\n",
      "# V**(3)+(b-(R*T)/P)*V**(2)-((3*b**(2))+((2*R*T*b)/P)-(a/P))*V+b**(3)+((R*T*(b**(2))/P)-((a*b)/P)=0;\n",
      "# Solving the cubic equation\n",
      "def f1(V): \n",
      "    return V**(3)+(b-(R*T_2)/P_2)*V**(2)-((3*b**(2))+((2*R*T_2*b)/P_2)-(a/P_2))*V+b**(3)+((R*T_2*(b**(2)))/P_2)-((a*b)/P_2)\n",
      "V4 = fsolve(f1,-1)\n",
      "V5 = fsolve(f1,0)\n",
      "V6 = fsolve(f1,1)\n",
      "#The largest root is for vapour phase,\n",
      "#The largest root is only considered as the systemis gas\n",
      "V_2 = V6;\t\t\t#[m**(3)/mol]\n",
      "# Thus compressibility factor is\n",
      "Z_2 = (P_2*V_2)/(R*T_2);\n",
      "\n",
      "# In the Peng-Robinson equation of stste\n",
      "# da/dT = -(a*m)/((alpha*T*Tc)**(1/2))\n",
      "# The residual enthalpy is given by\n",
      "# H_R = R*T*(Z-1) + (((T*(da_dT))-a)/(2*2**(1/2)*b))*math.log((Z+(1+2**(1/2)*((P*b)/(R*T))))/(Z+(1-2**(1/2)*((P*b)/(R*T)))))\n",
      "\n",
      "# At state 1\n",
      "da_dT_1 = -(a*m)/((alpha*T_1*Tc)**(1./2));\t\t\t#[Pa*m**(6)/mol**(2)]\n",
      "H_R_1 = R*T_1*(Z_1-1) + (((T_1*(da_dT_1))-a)/(2*(2**(1./2))*b))* \\\n",
      "math.log((Z_1+(1+2**(1./2))*((P_1*b)/(R*T_1)))/(Z_1+(1-2**(1./2))*((P_1*b)/(R*T_1))));\n",
      "\n",
      "# At state 2\n",
      "da_dT_2 = -(a*m)/((alpha*T_2*Tc)**(1/2.));\t\t\t#[Pa*m**(6)/mol**(2)]\n",
      "H_R_2 = R*T_2*(Z_2-1) + (((T_2*(da_dT_2))-a)/(2*2**(1./2)* \\\n",
      "b))*math.log((Z_2+(1+2**(1./2))*((P_2*b)/(R*T_1)))/(Z_2+(1-2**(1./2))*((P_2*b)/(R*T_1))));\n",
      "\n",
      "# Since the temperature is the same,therefore ideal gas change in enthalpy is zero and thus\n",
      "delta_H = H_R_2 - H_R_1;\t\t\t#[J/mol]\n",
      "delta_H = delta_H/Mol_wt;\t\t\t#[kJ/kg]\n",
      "\n",
      "# The residual entropy relation for a substance following Peng - Robinson equation of state ia\n",
      "# S_R = R*math.log(Z - (P*b)/(R*T)) + (da_dT/(2*2**(1/2)*b))*math.log((Z+(1+2**(1/2))*((P*b)/(R*T)))/(Z+(1-2**(1/2))*((P*b)/(R*T))))\n",
      "\n",
      "# The residual entropy at state 1 is\n",
      "S_R_1 = R*math.log(Z_1 - (P_1*b)/(R*T_1)) + (da_dT_1/(2*2**(1./2)*b))* \\\n",
      "math.log((Z_1+(1+2**(1./2))*((P_1*b)/(R*T_1)))/(Z_1+(1-2**(1./2))*((P_1*b)/(R*T_1))));\n",
      "\n",
      "# The residual entropy at state 2 is\n",
      "S_R_2 = R*math.log(Z_2 - (P_2*b)/(R*T_2)) + (da_dT_2/(2*2**(1./2)*b)) \\\n",
      "*math.log((Z_2+(1+2**(1./2))*((P_2*b)/(R*T_2)))/(Z_2+(1-2**(1./2))*((P_2*b)/(R*T_2))));\n",
      "\n",
      "delta_S_R = S_R_2 - S_R_1;\t\t\t#[J/mol-K]\n",
      "\n",
      "# The ideal gas change in entropy is\n",
      "delta_S_ig = Cp_0*math.log(T_2/T_1) - R*math.log(P_2/P_1);\t\t\t#[J/mol-K]\n",
      "\n",
      "# Therefore\n",
      "delta_S = delta_S_R + delta_S_ig;\t\t\t#[J/mol-K]\n",
      "\n",
      "# Results\n",
      "print \" The enthalpy change is given by delta_H = %f kJ/mol\"%(delta_H);\n",
      "print \" The entropy change is given by  delta_S = %f J/mol-K\"%(delta_S);\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The enthalpy change is given by delta_H = -11.747722 kJ/mol\n",
        " The entropy change is given by  delta_S = -12.826050 J/mol-K\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.26  Page Number : 370"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of final temperature and pressure\n",
      "\n",
      "# Variables\n",
      "Vol = 0.15;\t\t\t#[m**(3)]\n",
      "T_1 = 170;\t\t\t#[K] - Initial emperature\n",
      "P_1 = 100;\t\t\t#[bar] - Initial pressure\n",
      "P_1 = P_1*10**(5);\t\t\t#[Pa]\n",
      "R = 8.314;\t\t\t#[J/mol*K] - Universal gas constant\n",
      "# For nitrogen\n",
      "Tc = 126.2;\t\t\t#[K] - Critical tempeature\n",
      "Pc = 34;\t\t\t#[bar] - Critical pressure\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "w = 0.038;\n",
      "# Cp_0 = 27.2+4.2*10**(-3)*T\n",
      "\n",
      "# Calculations and Results\n",
      "#(1)\n",
      "# For van der Walls equation of state\n",
      "a = (27*R**(2)*Tc**(2))/(64*Pc);\t\t\t#[Pa-m**(6)/mol**(2)]\n",
      "b = (R*Tc)/(8*Pc);\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# The cubic form of van der Walls equation of state is given by,\n",
      "# V**(3) - (b + (R*T)/P)*V**(2) + (a/P)*V - (a*b)/P = 0\n",
      "# On simplification the equation changes to\n",
      "# V**(3) - 1.799*10**(4)*V**(2) + 1.366*10**(-8)*V - 5.269*10**(-13) = 0\n",
      "\n",
      "# Solving the cubic equation \n",
      "def f(V): \n",
      "    return V**(3)-1.799*10**(-4)*V**(2) + 1.366*10**(-8)*V - 5.269*10**(-13)\n",
      "V1 = fsolve(f,1)\n",
      "V2 = fsolve(f,10)\n",
      "V3 = fsolve(f,100)\n",
      "# The above equation has only 1 real root, other two roots are imagimnary\n",
      "V_1 = V1;\t\t\t#[m**(3)/mol]\n",
      "# Thus total number of moles is given by\n",
      "n_1 = Vol/V_1;\t\t\t#[mol]\n",
      "\n",
      "# After 500 mol are withdrawn, the final number of moles is given by\n",
      "n_2 = n_1 - 500;\t\t\t#[mol]\n",
      "# Thus molar volume at final state is \n",
      "V_2 = Vol/n_2;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# The ideal entropy change is guven by\n",
      "\n",
      "def f24(T): \n",
      "    return 27.2+4.2*10**(-3)*T\n",
      "\n",
      "# delta_S_ig =  quad(f24,T_1,T_2) - R*math.log(P_2/P_1)[0]\n",
      "\n",
      "# The residual entropy change is given by\n",
      "# delta_S_R = R*math.log((P_2*(V_2-b))/(R*T_2)) - R*math.log((P_1*(V_1-b))/(R*T_1)) \n",
      "# delta_S = delta_S_ig = delta_S_R\n",
      "\n",
      "def f25(T): \n",
      "    return 27.2+4.2*10**(-3)*T\n",
      "\n",
      "# delta_S =  quad(f25,T_1,T_2) + R*math.log((V_2-b)/(V_1-b))[0]\n",
      "\n",
      "# During discharging delta_S = 0, thus on simplification we get\n",
      "# 18.886*math.log(T_2) + 4.2*10**(-3)*T_2 - 92.937 = 0\n",
      "# Solving the above equation we get\n",
      "def f1(T_2): \n",
      "    return 18.886*math.log(T_2) + 4.2*10**(-3)*T_2 - 92.937\n",
      "T_2 = fsolve(f1,1)\n",
      "\n",
      "# Thus at T_2, \n",
      "P_2 = (R*T_2)/(V_2-b) - a/V_2**(2);\t\t\t#[N/m**(2)]\n",
      "P_2 = P_2*10**(-5);\t\t\t#[bar]\n",
      "\n",
      "print \" 1).The final temperature is %f K\"%(T_2);\n",
      "print \"     The final pressure is %f bar\"%(P_2);\n",
      "\n",
      "#(2)\n",
      "# In Peng-Robinson equation of state\n",
      "m = 0.37464 + 1.54226*w - 0.26992*w**(2);\n",
      "# At T_1 and P_1, we have\n",
      "Tr = T_1/Tc;\n",
      "alpha = (1 + m*(1 - Tr**(1./2)))**(2);\n",
      "a_2 = ((0.45724*(R*Tc)**(2))/Pc)*alpha;\t\t\t#[Pa*m**(6)/mol**(2)]\n",
      "b_2 = (0.07780*R*Tc)/Pc;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# Cubuc form of Peng-Robinson equation of stste is given by\n",
      "# V**(3)+(b-(R*T)/P)*V**(2)-((3*b**(2))+((2*R*T*b)/P)-(a/P))*V+b**(3)+((R*T*(b**(2))/P)-((a*b)/P)=0;\n",
      "# Solving the cubic equation\n",
      "def f2(V): \n",
      "    return V**(3)+(b_2-(R*T_1)/P_1)*V**(2)-((3*b_2**(2))+((2*R*T_1*b_2)/P_1)-(a_2/P_1))*V+b_2**(3)+((R*T_1*(b_2**(2)))/P_1)-((a_2*b_2)/P_1)\n",
      "V4 = fsolve(f2,-1)\n",
      "V5 = fsolve(f2,0)\n",
      "V6 = fsolve(f2,0.006)\n",
      "#The above equation has only 1 real root,the other two roots are imaginary\n",
      "V_1_2 = V6;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# The number of moles in the initial state is given by\n",
      "n_1_2 = Vol/V_1_2;\t\t\t#[mol]\n",
      "# After 500 mol are withdrawn, the final number of moles is given by\n",
      "n_2_2 = n_1_2 - 500;\t\t\t#[mol]\n",
      "# Thus molar volume at final state is \n",
      "V_2_2 = Vol/n_2_2;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# At the final state the relation between pressure and temperature is\n",
      "# P_2_2 = (R*T_2_2)/(V_2_2-b_2) - a_2/V_2_2**(2)\n",
      "# P_2_2 = 7.23*10**(4)*T_2 - 3.93*10**(7)*a_2\n",
      "\n",
      "# Now let us calculate the residual entropy at initial state\n",
      "Z_1 = (P_1*V_1_2)/(R*T_1);\n",
      "da_dT_1 = -(a*m)/((alpha*T_1*Tc)**(1./2));\t\t\t#[Pa*m**(6)/mol**(2)] - da/dT\n",
      "\n",
      "# The residual entropy change for Peng-Robinson equatiob of state is given by\n",
      "# S_R = R*math.log(Z-(P*b)/(R*T)) + (da_dT/(2*2**(1/2)*b))*math.log((V+(1+2**(1/2))*b))/((V+(1-2**(1/2)*b))));\n",
      "S_R_1 = R*(math.log(Z_1-(P_1*b_2)/(R*T_1))) + (da_dT_1/(2*2**(1.2)*b_2))*(math.log((V_1_2+(1+2**(1./2))*b_2)/(V_1_2+(1-2**(1./2))*b_2)));\n",
      "\n",
      "# The total entropy change is given by\n",
      "# delta_S = delta_S_ig + delta_S_R\n",
      "\n",
      "def f26(T): \n",
      "    return 27.2+4.2*10**(-3)*T\n",
      "\n",
      "# where, delta_S_ig =  quad(f26,T_1,T_2_2) - R*math.log(P_2_2/P_1)[0]\n",
      "\n",
      "# and, P_2_2 = (R*T_2_2)/(V_2_2-b_2) - a_2/V_2_2**(2)\n",
      "# On simplification we get\n",
      "# delta_S = 27.2*math.log(T_2_2-T_1) + 4.2*10**(-3)*(T_2_2-T_1) - R*math.log(P_2_2/P_1) + R*math.log(Z_2-(P_2_2*b)/(R*T_2_2)) + 6226*(da_dT_2) + 9.22\n",
      "\n",
      "# Now we have the determine the value of T_2_2 such that delta_S = 0\n",
      "# Starting with a temperature of 150 K\n",
      "T_prime = 100.;\t\t\t#[K]\n",
      "error = 10.;\n",
      "while(error>0.1):\n",
      "    Tr_prime = T_prime/Tc;\n",
      "    alpha_prime = (1 + m*(1 - Tr_prime**(1./2)))**(2);\n",
      "    a_prime = ((0.45724*(R*Tc)**(2))/Pc)*alpha_prime;\n",
      "    P_prime = 7.23*10**(4)*T_prime - 3.93*10**(7)*a_prime;\n",
      "    Z_prime = (P_prime*V_2_2)/(R*T_prime);\n",
      "    da_dT_prime = -(a_prime*m)/((alpha_prime*T_prime*Tc)**(1./2));\n",
      "    delta_S = 27.2*math.log(T_prime/T_1) + 4.2*10**(-3)*(T_prime-T_1) - R*math.log(P_prime/P_1) + R*math.log(Z_prime-((P_prime*b_2)/(R*T_prime))) + 6226*(da_dT_prime) + 9.22;\n",
      "    error=abs(delta_S);\n",
      "    T_prime = T_prime + 0.3;\n",
      "\n",
      "T_2_2 = T_prime;\t\t\t#[K] - Final temperature\n",
      "P_2_2 = P_prime*10**(-5);\t\t\t#[bar] - Final pressure\n",
      "\n",
      "print \" 2).The final temperature is %f K\"%(T_2_2);\n",
      "print \"     The final pressure is %f bar\"%(P_2_2);\n",
      "\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1).The final temperature is 133.131844 K\n",
        "     The final pressure is 39.636659 bar\n",
        " 2).The final temperature is 134.200000 K\n",
        "     The final pressure is 40.130674 bar\n"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.27  Page Number : 374"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Calculation of vapour pressure\n",
      "\n",
      "# Variables\n",
      "T = 373.15;\t\t\t#[K]\n",
      "Tc = 562.16;\t\t\t#[K]\n",
      "Pc = 48.98;\t\t\t#[bar]\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "R = 8.314;\t\t\t#[J/mol-K] - Universal gas constant\n",
      "\n",
      "# The cubic form of Redlich Kwong equation of state is given by,\n",
      "# V**(3) - ((R*T)/P)*V**(2) - ((b_1**(2)) + ((b_1*R*T)/P) - (a/(T**(1/2)*P))*V - (a*b)/(T**(1/2)*P) = 0\n",
      "\n",
      "# Calculations\n",
      "a = (0.42748*(R**(2))*(Tc**(2.5)))/Pc;\t\t\t#[Pa*m**(6)*K**(1/2)/mol]\n",
      "b = (0.08664*R*Tc)/Pc;\t\t\t#[m**(3)/mol]\n",
      "\n",
      "# At 373.15 K, let us assume the pressure to be 2.5 bar and under these conditions \n",
      "P_1 = 2.5;\t\t\t#[bar]\n",
      "P_1 = P_1*10**(5);\t\t\t#[bar]\n",
      "\n",
      "# Putting the values in Redlich Kwong equation of state, the equation becomes\n",
      "# V**(3) - 0.0124*V**(2) + 8.326*10**(-6)*V - 7.74*10**(-10) = 0\n",
      "# Solving the cubic equation\n",
      "\n",
      "def f(V): \n",
      "    return V**(3) - 0.0124*V**(2) + 8.326*10**(-6)*V - 7.74*10**(-10)\n",
      "V1=fsolve(f,-9)\n",
      "V2=fsolve(f,10)\n",
      "V3=fsolve(f,0.1)\n",
      "# The largest root and the smallest root is considered for liquid phase and vapour phase respectively.\n",
      "V_liq = V1;\t\t\t#[m**(3)/mol] - Molar volume in liquid phase\n",
      "V_vap = V3;\t\t\t#[m**(3)/mol] - Molar volume in vapour phase\n",
      "\n",
      "# Let us calculate the fugacity of vapour phase\n",
      "# math.log(f_vap/P) = b/(V-b) + math.log((R*T)/(P*(V-b))) - (a/(R*T**(1.5)))*(1/(V+b) - (1/b)*math.log(V/(V+b)))\n",
      "f_vap = P_1*math.exp(b/(V_vap-b) + math.log((R*T)/(P_1*(V_vap-b))) - (a/(R*T**(1.5)))*(1/(V_vap+b) - (1/b)*math.log(V_vap/(V_vap+b))));\t\t\t#[Pa]\n",
      "\n",
      "# Let us calculate the fugacity of the liquid phase\n",
      "f_liq = P_1*math.exp(b/(V_liq-b) + math.log((R*T)/(P_1*(V_liq-b))) - (a/(R*T**(1.5)))*(1/(V_liq+b) - (1/b)*math.log(V_liq/(V_liq+b))));\n",
      "\n",
      "\n",
      "# The two fugacities are not same; therefore another pressure is to be assumed. The new pressure is\n",
      "P_new = P_1*(f_liq/f_vap);\t\t\t#[Pa]\n",
      "\n",
      "# At P_new\n",
      "def f1(V): \n",
      "    return V**(3) - ((R*T)/P_new)*V**(2) - (b**(2) + ((b*R*T)/P_new) - a/(T**(1/2)*P_new))*V - (a*b)/(T**(1/2)*P_new)\n",
      "V4=fsolve(f1,-9)\n",
      "V5=fsolve(f1,10)\n",
      "V6=fsolve(f1,0.1)\n",
      "# The largest root and the smallest root is considered for liquid phase and vapour phase respectively.\n",
      "V_liq_2 = V4;\t\t\t#[m**(3)/mol] - Molar volume in liquid phase\n",
      "V_vap_2 = V6;\t\t\t#[m**(3)/mol] - Molar volume in vapour phase\n",
      "\n",
      "f_vap_prime = P_new*math.exp(b/(V_vap_2-b) + math.log((R*T)/(P_new*(V_vap_2-b))) - (a/(R*T**(1.5)))*(1/(V_vap_2+b) - (1/b)*math.log(V_vap_2/(V_vap_2+b))));\t\t\t#[Pa]\n",
      "f_liq_prime = P_new*math.exp(b/(V_liq_2-b) + math.log((R*T)/(P_new*(V_liq_2-b))) - (a/(R*T**(1.5)))*(1/(V_liq_2+b) - (1/b)*math.log(V_liq_2/(V_liq_2+b))));\n",
      "\n",
      "# Since the fugacities of liquid and vapour phasesare almost same the assumed pressure may be taken as vapour pressure at 373.15 K\n",
      "P_new = P_new*10**(-5);\t\t\t#[bar]\n",
      "\n",
      "# Results\n",
      "print \" The vapour pressure of benzene using Redlich Kwong equation of state is %f bar\"%(P_new);\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The vapour pressure of benzene using Redlich Kwong equation of state is 2.666995 bar\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.28  Page Number : 374"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Determination of vapour pressure\n",
      "\n",
      "# Variables\n",
      "T = 150 + 273.15;\t\t\t#[K]\n",
      "Tc = 647.1;\t\t\t#[K]\n",
      "Pc = 220.55;\t\t\t#[bar]\n",
      "Pc = Pc*10**(5);\t\t\t#[Pa]\n",
      "w = 0.345;\n",
      "R = 8.314;\t\t\t#[J/mol-K] - Universal gas constant\n",
      "\n",
      "# Let us assume a pressure of 100 kPa.\n",
      "P_1 = 100.*10**(3);\t\t\t#[Pa]\n",
      "\n",
      "# Calculations\n",
      "# At 100 kPa and 423.15 K, from Peng-Robinson equation of stste \n",
      "m = 0.37464 + 1.54226*w - 0.26992*w**(2);\n",
      "Tr = T/Tc;\n",
      "alpha = (1 + m*(1 - Tr**(1./2)))**(2);\n",
      "a = ((0.45724*(R*Tc)**(2))/Pc)*alpha;\t\t\t#[Pa*m**(6)/mol**(2)]\n",
      "b = (0.07780*R*Tc)/Pc;\t\t\t#[m**(3)/mol]\n",
      "# Cubic form of Peng-Robinson equation of stste is given by\n",
      "# V**(3)+(b-(R*T)/P)*V**(2)-((3*b**(2))+((2*R*T*b)/P)-(a/P))*V+b**(3)+((R*T*(b**(2))/P)-((a*b)/P)=0;\n",
      "# Solving the cubic equation\n",
      "def f(V): \n",
      "    return V**(3)+(b-(R*T)/P_1)*V**(2)-((3*b**(2))+((2*R*T*b)/P_1)-(a/P_1))*V+b**(3)+((R*T*(b**(2)))/P_1)-((a*b)/P_1)\n",
      "V1 = fsolve(f,-1)\n",
      "V2 = fsolve(f,0)\n",
      "V3 = fsolve(f,1)\n",
      "# The largest root and the smallest root is considered for liquid phase and vapour phase respectively.\n",
      "V_liq = V1;\t\t\t#[m**(3)/mol] - Molar volume in liquid phase\n",
      "V_vap = V3;\t\t\t#[m**(3)/mol] - Molar volume in vapour phase\n",
      "\n",
      "# The compressibility factor is given by\n",
      "Z_vap = (P_1*V_vap)/(R*T);\t\t\t# For liquid phase\n",
      "Z_liq = (P_1*V_liq)/(R*T);\t\t\t# For vapour phase\n",
      "\n",
      "# The math.expression for fugacity of Peng Robinson equation is\n",
      "# math.log(f/P) = (Z-1) - math.log(Z-((P*b)/(R*T))) - (a/(2*2**(1/2)*b*R*T))*math.log((Z+(1+2**(1/2))*((P*b)/(R*T)))/((Z+(1-2**(1/2))*((P*b)/(R*T)))\n",
      "# For vapour phase\n",
      "f_P_vap = math.exp((Z_vap-1) - math.log(Z_vap-((P_1*b)/(R*T))) - (a/(2*2**(1./2)*b*R*T))*math.log((Z_vap+(1+2**(1./2))*((P_1*b)/(R*T)))/(Z_vap+(1-2**(1./2))*((P_1*b)/(R*T)))));\n",
      "# For liquid phase\n",
      "f_P_liq = math.exp((Z_liq-1) - math.log(Z_liq-((P_1*b)/(R*T))) - (a/(2*2**(1./2)*b*R*T))*math.log((Z_liq+(1+2**(1./2))*((P_1*b)/(R*T)))/(Z_liq+(1-2**(1./2))*((P_1*b)/(R*T)))));\n",
      "\n",
      "# Therefore f_liq/f_vap can be calculated as\n",
      "fL_fV = (f_P_liq/f_P_vap);\n",
      "\n",
      "# The two values (f/P)_vap and (f/P)_vap are not same [ (f_P_liq/f_P_vap) >1 ]; therefore another pressure is to be assumed. The new pressure be\n",
      "P_new = P_1*(f_P_liq/f_P_vap);\t\t\t#[Pa]\n",
      "\n",
      "# At P_new and 423.15 K, from Peng-Robinson equation of stste \n",
      "\n",
      "# V**(3)+(b-(R*T)/P)*V**(2)-((3*b**(2))+((2*R*T*b)/P)-(a/P))*V+b**(3)+((R*T*(b**(2))/P)-((a*b)/P)=0;\n",
      "# Solving the cubic equation\n",
      "def f(V): \n",
      "    return V**(3)+(b-(R*T)/P_new)*V**(2)-((3*b**(2))+((2*R*T*b)/P_new)-(a/P_new))*V+b**(3)+((R*T*(b**(2)))/P_new)-((a*b)/P_new)\n",
      "V4 = fsolve(f,-1)\n",
      "V5 = fsolve(f,0)\n",
      "V6 = fsolve(f,1)\n",
      "# The largest root and the smallest root is considered for liquid phase and vapour phase respectively.\n",
      "V_liq_2 = V4;\t\t\t#[m**(3)/mol] - Molar volume in liquid phase\n",
      "V_vap_2 = V6;\t\t\t#[m**(3)/mol] - Molar volume in vapour phase\n",
      "\n",
      "# The compressibility factor is given by\n",
      "Z_vap_2 = (P_new*V_vap_2)/(R*T);\t\t\t# For liquid phase\n",
      "Z_liq_2 = (P_new*V_liq_2)/(R*T);\t\t\t# For vapour phase\n",
      "\n",
      "# For vapour phase\n",
      "f_P_vap_2 = math.exp((Z_vap_2-1) - math.log(Z_vap_2-((P_new*b)/(R*T))) - (a/(2*2**(1./2)*b*R*T))*math.log((Z_vap_2+(1+2**(1./2))*((P_new*b)/(R*T)))/(Z_vap_2+(1-2**(1./2))*((P_new*b)/(R*T)))));\n",
      "# For liquid phase\n",
      "f_P_liq_2 = math.exp((Z_liq_2-1) - math.log(Z_liq_2-((P_new*b)/(R*T))) - (a/(2*2**(1./2)*b*R*T))*math.log((Z_liq_2+(1+2**(1./2))*((P_new*b)/(R*T)))/(Z_liq_2+(1-2**(1./2))*((P_new*b)/(R*T)))));\n",
      "\n",
      "# Therefore f_liq/f_vap can be calculated as\n",
      "fL_fV_2 = (f_P_liq_2/f_P_vap_2);\n",
      "\n",
      "# And new pressure is given by\n",
      "P_new_prime = P_new*(f_P_liq_2/f_P_vap_2);\t\t\t#[Pa]\n",
      "P_new_prime = P_new_prime*10**(-5);\n",
      "\n",
      "# Since the change in pressure is small, so we can take this to be the vapour pressure at 150 C\n",
      "\n",
      "# Results\n",
      "print \" The vapour pressure of water using Peng-Robinson equation of stste  is %f bar\"%(P_new_prime);\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The vapour pressure of water using Peng-Robinson equation of stste  is 4.675976 bar\n"
       ]
      }
     ],
     "prompt_number": 24
    }
   ],
   "metadata": {}
  }
 ]
}