Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

# -*- coding: utf-8 -*- 

'''Chemical Engineering Design Library (ChEDL). Utilities for process modeling. 

Copyright (C) 2016, Caleb Bell <Caleb.Andrew.Bell@gmail.com> 

 

Permission is hereby granted, free of charge, to any person obtaining a copy 

of this software and associated documentation files (the "Software"), to deal 

in the Software without restriction, including without limitation the rights 

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

copies of the Software, and to permit persons to whom the Software is 

furnished to do so, subject to the following conditions: 

 

The above copyright notice and this permission notice shall be included in all 

copies or substantial portions of the Software. 

 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

SOFTWARE.''' 

 

from __future__ import division 

 

__all__ = ['has_matplotlib', 'ANYMATH', 'mathlib', 'to_num', 'CAS2int', 

'int2CAS', 'Parachor', 'property_molar_to_mass', 'property_mass_to_molar', 

'isobaric_expansion', '_isobaric_expansion', 'isothermal_compressibility', 

'Cp_minus_Cv', 'JT', 'speed_of_sound', 'Joule_Thomson', 

'phase_identification_parameter', 'phase_identification_parameter_phase', 

'isentropic_exponent', 'Vm_to_rho', 'rho_to_Vm', 

'Z', 'B_To_Z', 'B_from_Z', 'Z_from_virial_density_form', 

'Z_from_virial_pressure_form', 'zs_to_ws', 'ws_to_zs', 'zs_to_Vfs', 

'Vfs_to_zs', 'none_and_length_check', 'normalize', 'mixing_simple', 

'mixing_logarithmic', 'phase_set_property', 'TDependentProperty', 

'TPDependentProperty'] 

 

from cmath import sqrt as csqrt 

import numpy as np 

from scipy.constants import R 

from scipy.optimize import brenth 

from scipy.misc import derivative 

from scipy.integrate import quad 

from scipy.interpolate import interp1d, interp2d 

 

# __all__ may not be able to be imported due to the math library 

 

try: 

import matplotlib.pyplot as plt 

has_matplotlib = True 

except: 

has_matplotlib = False 

 

'''Experimental variable to switch math libraries to allow for computation 

with rational numbers, arbitrary precision, uncertainty propagation, or  

arrays. Off by default; See `mathlib` to change which math library is used.  

Must be changed in the code itself; cannot be toggled after importing. 

 

Note that due to the use of logic, support for these libraries is greatly 

varying. At this time, the use of these other library prevents the use of any 

object oriented code. 

 

Warning: If True, all the following math libraries must be available and will 

be imported: 

 

* numpy 

* scipy 

* mpmath 

* sympy 

* uncertainties 

''' 

ANYMATH = False 

 

'''Integer switch to change which library is used. Can be changed after import.  

Only has an effect if `ANYMATH` was True before import. The following options  

are available: 

* 1 : math 

* 2 : numpy 

* 3 : scipy 

* 4 : mpmath 

* 5 : sympy 

* 6 : uncertainties''' 

mathlib = 1 

 

if ANYMATH: 

import math 

import numpy as np 

import scipy 

import sympy 

import mpmath 

import uncertainties.umath as umath 

 

expfuncs = {1: math.exp, 2: np.exp, 3: scipy.exp, 4: mpmath.exp, 5:sympy.exp, 6: umath.exp} 

logfuncs = {1: math.log, 2: np.log, 3: scipy.log, 4: mpmath.log, 5:sympy.log, 6: umath.log} 

log10funcs = {1: math.log10, 2: np.log10, 3: scipy.log10, 4: mpmath.log, 5:lambda x : sympy.log(x, 10), 6: umath.log10} # Sympy doesn't support log10 

 

sqrtfuncs = {1: math.sqrt, 2: np.sqrt, 3: scipy.sqrt, 4: mpmath.sqrt, 5:sympy.sqrt, 6: umath.sqrt} 

sinfuncs = {1: math.sin, 2: np.sin, 3: scipy.sin, 4: mpmath.sin, 5:sympy.sin, 6: umath.sin} 

coshfuncs = {1: math.cosh, 2: np.cosh, 3: scipy.cosh, 4: mpmath.cosh, 5:sympy.cosh, 6: umath.cosh} 

sinhfuncs = {1: math.sinh, 2: np.sinh, 3: scipy.sinh, 4: mpmath.sinh, 5:sympy.sinh, 6: umath.sinh} 

 

pifuncs = {1: math.pi, 2: np.pi, 3: scipy.pi, 4: mpmath.pi, 5: sympy.pi, 6: math.pi} # uncertainties doesn't support pi 

 

pi = pifuncs[mathlib] 

 

def exp(x): 

return expfuncs[mathlib](x) 

def log(x): 

return logfuncs[mathlib](x) 

def log10(x): 

return log10funcs[mathlib](x) 

 

def sin(x): 

return sinfuncs[mathlib](x) 

def cosh(x): 

return coshfuncs[mathlib](x) 

def sinh(x): 

return sinhfuncs[mathlib](x) 

def sqrt(x): 

return sqrtfuncs[mathlib](x) 

 

else: 

from math import * 

__all__.extend(['acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 

'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 

'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 

'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 

'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']) 

 

 

def to_num(values): 

r'''Legacy function to turn a list of strings into either floats 

(if numeric), stripped strings (if not) or None if the string is empty. 

Accepts any numeric formatting the float function does. 

 

Parameters 

---------- 

values : list 

list of strings 

 

Returns 

------- 

values : list 

list of floats, strings, and None values [-] 

 

Examples 

-------- 

>>> to_num(['1', '1.1', '1E5', '0xB4', '']) 

[1.0, 1.1, 100000.0, '0xB4', None] 

''' 

for i in range(len(values)): 

try: 

values[i] = float(values[i]) 

except: 

if values[i] == '': 

values[i] = None 

else: 

values[i] = values[i].strip() 

pass 

return values 

 

 

def CAS2int(i): 

r'''Converts CAS number of a compounds from a string to an int. This is 

helpful when storing large amounts of CAS numbers, as their strings take up 

more memory than their numerical representational. All CAS numbers fit into 

64 bit ints. 

 

Parameters 

---------- 

CASRN : string 

CASRN [-] 

 

Returns 

------- 

CASRN : int 

CASRN [-] 

 

Notes 

----- 

Accomplishes conversion by removing dashes only, and then converting to an 

int. An incorrect CAS number will change without exception. 

 

Examples 

-------- 

>>> CAS2int('7704-34-9') 

7704349 

''' 

return int(i.replace('-', '')) 

 

 

def int2CAS(i): 

r'''Converts CAS number of a compounds from an int to an string. This is 

helpful when dealing with int CAS numbers. 

 

Parameters 

---------- 

CASRN : int 

CASRN [-] 

 

Returns 

------- 

CASRN : string 

CASRN [-] 

 

Notes 

----- 

Handles CAS numbers with an unspecified number of digits. Does not work on 

floats. 

 

Examples 

-------- 

>>> int2CAS(7704349) 

'7704-34-9' 

''' 

i = str(i) 

return i[:-3]+'-'+i[-3:-1]+'-'+i[-1] 

 

 

 

 

def Parachor(sigma, MW, rhol, rhog): 

'''Calculates a Chemical's Parachor according to DIPPR Method. 

 

>>> Parachor(0.02117, 114.22852, 700.03, 5.2609) # Octane; DIPPR: 350.6 

352.66655018657565 

''' 

rhol, rhog = rhol/1000., rhog/1000. 

sigma = sigma*1000 

P = sigma**0.25*MW/(rhol-rhog) 

return P 

 

 

#def Parachor2(sigma, Vml, Vmg): 

# r'''Calculate Parachor for a pure species, using its molar volumes in 

# liquid and vapor form, and surface tension. 

# 

# .. math:: 

# P = \frac{ \sigma^{0.25} MW}{\rho_L - \rho_V}=\sigma^{0.25} (V_L - V_V) 

# 

# Parameters 

# ---------- 

# sigma : float 

# Surface tension, [N/m] 

# Vml : float 

# Liquid molar volume [m^3/mol] 

# Vmg : float 

# Gas molar volume [m^3/mol] 

# 

# Returns 

# ------- 

# P : float 

# Parachor, [-] 

# 

# Notes 

# ----- 

# Parachor is normally specified in units of [], in which molar volumes must 

# be converted to mL/mol, and surface tension in mN/m. Pure SI units are: 

# 

# .. math:: 

# \frac{\text{kg}^{0.25}\cdot\text{meter}^3} 

# {\text{mole}\cdot \text{second}^{0.5}} 

# 

# The conversion to pure SI is to multiply by 1.77828e-7. 

# 

# Examples 

# -------- 

# Example 12.1 in [1]_. 

# 

# >>> Parachor(sigma=0.02119, Vml=9.6525097e-05, Vmg=0) 

# 207.09660808998868 

# 

# References 

# ---------- 

# .. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

# New York: McGraw-Hill Professional, 2000. 

# ''' 

# Vml, Vmg = Vml*1E6, Vmg*1E6 

# sigma = sigma*1000 

# P = sigma**0.25/(Vmg-Vml) 

# return P 

 

#print([Parachor2(sigma=0.02119, Vml=9.6525097e-05, Vmg=0)]) 

 

def property_molar_to_mass(A_molar, MW): # pragma: no cover 

if A_molar is None: 

return None 

A = A_molar*1000/MW 

return A 

 

 

def property_mass_to_molar(A_mass, MW): # pragma: no cover 

if A_mass is None: 

return None 

A_molar = A_mass*MW/1000 

return A_molar 

 

 

def isobaric_expansion(V1=None, V2=None, dT=0.01): # pragma: no cover 

if not (V1 and V2 and dT): 

return None 

V = (V1+V2)/2. 

beta = 1/V*(V2-V1)/dT 

return beta 

 

 

def _isobaric_expansion(V, dV_dT): 

r'''Calculate the isobaric coefficient of a thermal expansion, given its  

molar volume at a certain `T` and `P`, and its derivative of molar volume 

with respect to `T`. 

 

.. math:: 

\beta = \frac{1}{V}\left(\frac{\partial V}{\partial T} \right)_P 

 

Parameters 

---------- 

V : float 

Molar volume at `T` and `P`, [m^3/mol] 

dV_dT : float 

Derivative of molar volume with respect to `T`, [m^3/mol/K] 

 

Returns 

------- 

beta : float 

Isobaric coefficient of a thermal expansion, [1/K] 

 

Notes 

----- 

For an ideal gas, this expression simplified to: 

 

.. math:: 

\beta = \frac{1}{T} 

 

Examples 

-------- 

Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid): 

 

>>> _isobaric_expansion(0.000130229900873546, 1.58875261849113e-7) 

0.0012199599384121608 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

''' 

return dV_dT/V 

 

 

def isothermal_compressibility(V, dV_dP): 

r'''Calculate the isothermal coefficient of a compressibility, given its  

molar volume at a certain `T` and `P`, and its derivative of molar volume 

with respect to `P`. 

 

.. math:: 

\kappa = -\frac{1}{V}\left(\frac{\partial V}{\partial P} \right)_T 

 

Parameters 

---------- 

V : float 

Molar volume at `T` and `P`, [m^3/mol] 

dV_dP : float 

Derivative of molar volume with respect to `P`, [m^3/mol/Pa] 

 

Returns 

------- 

kappa : float 

Isothermal coefficient of a compressibility, [1/Pa] 

 

Notes 

----- 

For an ideal gas, this expression simplified to: 

 

.. math:: 

\kappa = \frac{1}{P} 

 

Examples 

-------- 

Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid): 

 

>>> isothermal_compressibility(0.000130229900873546, -2.72902118209903e-13) 

2.095541165119158e-09 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

''' 

return -dV_dP/V 

 

 

def phase_identification_parameter(V, dP_dT, dP_dV, d2P_dV2, d2P_dVdT): 

r'''Calculate the Phase Identification Parameter developed in [1]_ for 

the accurate and efficient determination of whether a fluid is a liquid or 

a gas based on the results of an equation of state. For supercritical  

conditions, this provides a good method for choosing which property  

correlations to use. 

 

.. math:: 

\Pi = V \left[\frac{\frac{\partial^2 P}{\partial V \partial T}} 

{\frac{\partial P }{\partial T}}- \frac{\frac{\partial^2 P}{\partial  

V^2}}{\frac{\partial P}{\partial V}} \right] 

 

Parameters 

---------- 

V : float 

Molar volume at `T` and `P`, [m^3/mol] 

dP_dT : float 

Derivative of `P` with respect to `T`, [Pa/K] 

dP_dV : float 

Derivative of `P` with respect to `V`, [Pa*mol/m^3] 

d2P_dV2 : float 

Second derivative of `P` with respect to `V`, [Pa*mol^2/m^6] 

d2P_dVdT : float 

Second derivative of `P` with respect to both `V` and `T`, [Pa*mol/m^3/K] 

 

Returns 

------- 

PIP : float 

Phase Identification Parameter, [-] 

 

Notes 

----- 

Heuristics were used by process simulators before the invent of this  

parameter.  

 

The criteria for liquid is Pi > 1; for vapor, Pi <= 1. 

 

Examples 

-------- 

Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid): 

 

>>> phase_identification_parameter(0.000130229900874, 582169.397484,  

... -3.66431747236e+12, 4.48067893805e+17, -20518995218.2) 

11.33428990564796 

 

References 

---------- 

.. [1] Venkatarathnam, G., and L. R. Oellrich. "Identification of the Phase 

of a Fluid Using Partial Derivatives of Pressure, Volume, and  

Temperature without Reference to Saturation Properties: Applications in  

Phase Equilibria Calculations." Fluid Phase Equilibria 301, no. 2  

(February 25, 2011): 225-33. doi:10.1016/j.fluid.2010.12.001. 

''' 

return V*(d2P_dVdT/dP_dT - d2P_dV2/dP_dV) 

 

#phase_identification_parameter(0.000130229900874, 582169.397484, -3.66431747236e+12, 4.48067893805e+17, -20518995218.2) 

 

def phase_identification_parameter_phase(PIP): 

r'''Uses the Phase Identification Parameter developed in [1]_ to determine 

if a fluid is a liquid or a vapor. 

 

The criteria for liquid is PIP > 1; for vapor, PIP <= 1. 

 

Parameters 

---------- 

PIP : float 

Phase Identification Parameter, [-] 

 

Returns 

------- 

phase : bool 

Either 'l' or 'g' 

 

Examples 

-------- 

Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid): 

 

>>> phase_identification_parameter_phase(11.33428990564796) 

'l' 

 

References 

---------- 

.. [1] Venkatarathnam, G., and L. R. Oellrich. "Identification of the Phase 

of a Fluid Using Partial Derivatives of Pressure, Volume, and  

Temperature without Reference to Saturation Properties: Applications in  

Phase Equilibria Calculations." Fluid Phase Equilibria 301, no. 2  

(February 25, 2011): 225-33. doi:10.1016/j.fluid.2010.12.001. 

''' 

return 'l' if PIP > 1 else 'g' 

 

 

def Cp_minus_Cv(T, dP_dT, dP_dV): 

r'''Calculate the difference between a real gas's constant-pressure heat 

capacity and constant-volume heat capacity, as given in [1]_, [2]_, and 

[3]_. The required derivatives should be calculated with an equation of 

state. 

 

.. math:: 

C_p - C_v = -T\left(\frac{\partial P}{\partial T}\right)_V^2/ 

\left(\frac{\partial P}{\partial V}\right)_T 

 

Parameters 

---------- 

T : float 

Temperature of fluid [K] 

dP_dT : float 

Derivative of `P` with respect to `T`, [Pa/K] 

dP_dV : float 

Derivative of `P` with respect to `V`, [Pa*mol/m^3] 

 

Returns 

------- 

Cp_minus_Cv : float 

Cp - Cv for a real gas, [J/mol/K] 

 

Notes 

----- 

Equivalent expressions are: 

 

.. math:: 

C_p - C_v= -T\left(\frac{\partial V}{\partial T}\right)_P^2/\left( 

\frac{\partial V}{\partial P}\right)_T 

 

C_p - C_v = T\left(\frac{\partial P}{\partial T}\right) 

\left(\frac{\partial V}{\partial T}\right) 

 

Note that these are not second derivatives, only first derivatives, some 

of which are squared. 

 

Examples 

-------- 

Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid): 

 

>>> Cp_minus_Cv(299, 582232.475794113, -3665180614672.253) 

27.654681381642394 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

.. [2] Walas, Stanley M. Phase Equilibria in Chemical Engineering.  

Butterworth-Heinemann, 1985. 

.. [3] Gmehling, Jurgen, Barbel Kolbe, Michael Kleiber, and Jurgen Rarey. 

Chemical Thermodynamics for Process Simulation. 1st edition. Weinheim:  

Wiley-VCH, 2012. 

''' 

return -T*dP_dT**2/dP_dV 

 

 

def speed_of_sound(V, dP_dV, Cp, Cv, MW=None): 

r'''Calculate a real fluid's speed of sound. The required derivatives should  

be calculated with an equation of state, and `Cp` and `Cv` are both the 

real fluid versions. Expression is given in [1]_ and [2]_; a unit conversion 

is further performed to obtain a result in m/s. If MW is not provided the  

result is returned in units of m*kg^0.5/s/mol^0.5. 

 

.. math:: 

w = \left[-V^2 \left(\frac{\partial P}{\partial V}\right)_T \frac{C_p} 

{C_v}\right]^{1/2} 

 

Parameters 

---------- 

V : float 

Molar volume of fluid, [m^3/mol] 

dP_dV : float 

Derivative of `P` with respect to `V`, [Pa*mol/m^3] 

Cp : float 

Real fluid heat capacity at constant pressure, [J/mol/K] 

Cv : float 

Real fluid heat capacity at constant volume, [J/mol/K] 

MW : float, optional 

Molecular weight, [g/mol] 

 

Returns 

------- 

w : float 

Speed of sound for a real gas, [m/s or m*kg^0.5/s/mol^0.5 or MW missing] 

 

Notes 

----- 

An alternate expression based on molar density is as follows: 

 

.. math:: 

w = \left[\left(\frac{\partial P}{\partial \rho}\right)_T \frac{C_p} 

{C_v}\right]^{1/2} 

 

The form with the unit conversion performed inside it is as follows: 

 

.. math:: 

w = \left[-V^2 \frac{1000}{MW}\left(\frac{\partial P}{\partial V} 

\right)_T \frac{C_p}{C_v}\right]^{1/2} 

 

Examples 

-------- 

Example from [2]_: 

 

>>> speed_of_sound(V=0.00229754, dP_dV=-3.5459e+08, Cp=153.235, Cv=132.435, MW=67.152) 

179.5868138460819 

 

References 

---------- 

.. [1] Gmehling, Jurgen, Barbel Kolbe, Michael Kleiber, and Jurgen Rarey. 

Chemical Thermodynamics for Process Simulation. 1st edition. Weinheim:  

Wiley-VCH, 2012. 

.. [2] Pratt, R. M. "Thermodynamic Properties Involving Derivatives: Using  

the Peng-Robinson Equation of State." Chemical Engineering Education 35, 

no. 2 (March 1, 2001): 112-115.  

''' 

if not MW: 

return (-V**2*dP_dV*Cp/Cv)**0.5 

else: 

return (-V**2*1000./MW*dP_dV*Cp/Cv)**0.5 

 

 

def Joule_Thomson(T, V, Cp, dV_dT=None, beta=None): 

r'''Calculate a real fluid's Joule Thomson coefficient. The required  

derivative should be calculated with an equation of state, and `Cp` is the 

real fluid versions. This can either be calculated with `dV_dT` directly,  

or with `beta` if it is already known. 

 

.. math:: 

\mu_{JT} = \left(\frac{\partial T}{\partial P}\right)_H = \frac{1}{C_p} 

\left[T \left(\frac{\partial V}{\partial T}\right)_P - V\right] 

= \frac{V}{C_p}\left(\beta T-1\right) 

 

Parameters 

---------- 

T : float 

Temperature of fluid, [K] 

V : float 

Molar volume of fluid, [m^3/mol] 

Cp : float 

Real fluid heat capacity at constant pressure, [J/mol/K] 

dV_dT : float, optional 

Derivative of `V` with respect to `T`, [m^3/mol/K] 

beta : float, optional 

Isobaric coefficient of a thermal expansion, [1/K] 

 

Returns 

------- 

mu_JT : float 

Joule-Thomson coefficient [K/Pa] 

 

Examples 

-------- 

Example from [2]_: 

 

>>> Joule_Thomson(T=390, V=0.00229754, Cp=153.235, dV_dT=1.226396e-05) 

1.621956080529905e-05 

 

References 

---------- 

.. [1] Walas, Stanley M. Phase Equilibria in Chemical Engineering.  

Butterworth-Heinemann, 1985. 

.. [2] Pratt, R. M. "Thermodynamic Properties Involving Derivatives: Using  

the Peng-Robinson Equation of State." Chemical Engineering Education 35, 

no. 2 (March 1, 2001): 112-115.  

''' 

if dV_dT: 

return (T*dV_dT - V)/Cp 

elif beta: 

return V/Cp*(beta*T - 1) 

else: 

raise Exception('Either dV_dT or beta is needed') 

 

 

def JT(T=None, V=None, Cp=None, isobaric_expansion=None): # pragma: no cover 

if not (T and V and Cp and isobaric_expansion): 

return None 

_JT = V/Cp*(T*isobaric_expansion - 1.) 

return _JT 

 

 

def isentropic_exponent(Cp, Cv): 

r'''Calculate the isentropic coefficient of a gas, given its constant- 

pressure and constant-volume heat capacity. 

 

.. math:: 

k = \frac{C_p}{C_v} 

 

Parameters 

---------- 

Cp : float 

Gas heat capacity at constant pressure, [J/mol/K] 

Cv : float 

Gas heat capacity at constant volume, [J/mol/K] 

 

Returns 

------- 

k : float 

Isentropic exponent, [-] 

 

Examples 

-------- 

>>> isentropic_exponent(33.6, 25.27) 

1.329639889196676 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

''' 

return Cp/Cv 

 

 

def Vm_to_rho(Vm, MW): 

r'''Calculate the density of a chemical, given its molar volume and 

molecular weight. 

 

.. math:: 

\rho = \frac{MW}{1000\cdot VM} 

 

Parameters 

---------- 

Vm : float 

Molar volume, [m^3/mol] 

MW : float 

Molecular weight, [g/mol] 

 

Returns 

------- 

rho : float 

Density, [kg/m^3] 

 

Examples 

-------- 

>>> Vm_to_rho(0.000132, 86.18) 

652.8787878787879 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

''' 

return (Vm)**-1*MW/1000. 

 

 

def rho_to_Vm(rho, MW): 

r'''Calculate the molar volume of a chemical, given its density and 

molecular weight. 

 

.. math:: 

V_m = \left(\frac{1000 \rho}{MW}\right)^{-1} 

 

Parameters 

---------- 

rho : float 

Density, [kg/m^3] 

MW : float 

Molecular weight, [g/mol] 

 

Returns 

------- 

Vm : float 

Molar volume, [m^3/mol] 

 

Examples 

-------- 

>>> rho_to_Vm(652.9, 86.18) 

0.00013199571144126206 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

''' 

return (rho*1000./MW)**-1 

 

 

def Z(T, P, V): 

r'''Calculates the compressibility factor of a gas, given its 

temperature, pressure, and molar volume. 

 

.. math:: 

Z = \frac{PV}{RT} 

 

Parameters 

---------- 

T : float 

Temperature, [K] 

P : float 

Pressure [Pa] 

V : float 

Molar volume, [m^3/mol] 

 

Returns 

------- 

Z : float 

Compressibility factor, [-] 

 

Examples 

-------- 

>>> Z(600, P=1E6, V=0.00463) 

0.9281019876560912 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

''' 

_Z = V*P/T/R 

return _Z 

 

 

def B_To_Z(B, T, P): 

r'''Calculates the compressibility factor of a gas, given its 

second virial coefficient. 

 

.. math:: 

Z = 1 + \frac{BP}{RT} 

 

Parameters 

---------- 

B : float 

Second virial coefficient, [m^3/mol] 

T : float 

Temperature, [K] 

P : float 

Pressure [Pa] 

 

Returns 

------- 

Z : float 

Compressibility factor, [-] 

 

Notes 

----- 

Other forms of the virial coefficient exist. 

 

Examples 

-------- 

>>> B_To_Z(-0.0015, 300, 1E5) 

0.9398638020957176 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

''' 

return 1. + B*P/R/T 

 

 

def B_from_Z(Z, T, P): 

r'''Calculates the second virial coefficient of a pure species, given the 

compressibility factor of the gas. 

 

.. math:: 

B = \frac{RT(Z-1)}{P} 

 

Parameters 

---------- 

Z : float 

Compressibility factor, [-] 

T : float 

Temperature, [K] 

P : float 

Pressure [Pa] 

 

Returns 

------- 

B : float 

Second virial coefficient, [m^3/mol] 

 

Notes 

----- 

Other forms of the virial coefficient exist. 

 

Examples 

-------- 

>>> B_from_Z(0.94, 300, 1E5) 

-0.0014966027640000014 

 

References 

---------- 

.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition. 

New York: McGraw-Hill Professional, 2000. 

''' 

return (Z - 1)*R*T/P 

 

 

def Z_from_virial_density_form(T, P, *args): 

r'''Calculates the compressibility factor of a gas given its temperature, 

pressure, and molar density-form virial coefficients. Any number of 

coefficients is supported. 

 

.. math:: 

Z = \frac{PV}{RT} = 1 + \frac{B}{V} + \frac{C}{V^2} + \frac{D}{V^3} 

+ \frac{E}{V^4} \dots 

 

Parameters 

---------- 

T : float 

Temperature, [K] 

P : float 

Pressure, [Pa] 

B to Z : float, optional 

Virial coefficients, [various] 

 

Returns 

------- 

Z : float 

Compressibility factor at T, P, and with given virial coefficients, [-] 

 

Notes 

----- 

For use with B or with B and C or with B and C and D, optimized equations  

are used to obtain the compressibility factor directly. 

If more coefficients are provided, uses numpy's roots function to solve  

this equation. This takes substantially longer as the solution is  

numerical. 

 

If no virial coefficients are given, returns 1, as per the ideal gas law. 

 

The units of each virial coefficient are as follows, where for B, n=1, and 

C, n=2, and so on. 

 

.. math:: 

\left(\frac{\text{m}^3}{\text{mol}}\right)^n 

 

Examples 

-------- 

>>> Z_from_virial_density_form(300, 122057.233762653, 1E-4, 1E-5, 1E-6, 1E-7) 

1.2843496002100001 

 

References 

---------- 

.. [1] Prausnitz, John M., Rudiger N. Lichtenthaler, and Edmundo Gomes de  

Azevedo. Molecular Thermodynamics of Fluid-Phase Equilibria. 3rd  

edition. Upper Saddle River, N.J: Prentice Hall, 1998. 

.. [2] Walas, Stanley M. Phase Equilibria in Chemical Engineering.  

Butterworth-Heinemann, 1985. 

''' 

l = len(args) 

if l == 1: 

return 1/2. + (4*args[0]*P + R*T)**0.5/(2*(R*T)**0.5) 

# return ((R*T*(4*args[0]*P + R*T))**0.5 + R*T)/(2*P) 

if l == 2: 

B, C = args 

# A small imaginary part is ignored 

return (P*(-(3*B*R*T/P + R**2*T**2/P**2)/(3*(-1/2 + csqrt(3)*1j/2)*(-9*B*R**2*T**2/(2*P**2) - 27*C*R*T/(2*P) + csqrt(-4*(3*B*R*T/P + R**2*T**2/P**2)**(3+0j) + (-9*B*R**2*T**2/P**2 - 27*C*R*T/P - 2*R**3*T**3/P**3)**(2+0j))/2 - R**3*T**3/P**3)**(1/3.+0j)) - (-1/2 + csqrt(3)*1j/2)*(-9*B*R**2*T**2/(2*P**2) - 27*C*R*T/(2*P) + csqrt(-4*(3*B*R*T/P + R**2*T**2/P**2)**(3+0j) + (-9*B*R**2*T**2/P**2 - 27*C*R*T/P - 2*R**3*T**3/P**3)**(2+0j))/2 - R**3*T**3/P**3)**(1/3.+0j)/3 + R*T/(3*P))/(R*T)).real 

if l == 3: 

# Huge mess. Ideally sympy could optimize a function for quick python  

# execution. Derived with kate's text highlighting 

B, C, D = args 

P2 = P**2 

RT = R*T 

BRT = B*RT 

T2 = T**2 

R2 = R**2 

RT23 = 3*R2*T2 

mCRT = -C*RT 

P2256 = 256*P2 

 

RT23P2256 = RT23/(P2256) 

big1 = (D*RT/P - (-BRT/P - RT23/(8*P2))**2/12 - RT*(mCRT/(4*P) - RT*(BRT/(16*P) + RT23P2256)/P)/P) 

big3 = (-BRT/P - RT23/(8*P2)) 

big4 = (mCRT/P - RT*(BRT/(2*P) + R2*T2/(8*P2))/P) 

big5 = big3*(-D*RT/P + RT*(mCRT/(4*P) - RT*(BRT/(16*P) + RT23P2256)/P)/P) 

big2 = 2*big1/(3*(big3**3/216 - big5/6 + big4**2/16 + csqrt(big1**3/27 + (-big3**3/108 + big5/3 - big4**2/8)**2/4))**(1/3)) 

big7 = 2*BRT/(3*P) - big2 + 2*(big3**3/216 - big5/6 + big4**2/16 + csqrt(big1**3/27 + (-big3**3/108 + big5/3 - big4**2/8)**2/4))**(1/3) + R2*T2/(4*P2) 

return (P*(((csqrt(big7)/2 + csqrt(4*BRT/(3*P) - (-2*C*RT/P - 2*RT*(BRT/(2*P) + R2*T2/(8*P2))/P)/csqrt(big7) + big2 - 2*(big3**3/216 - big5/6 + big4**2/16 + csqrt(big1**3/27 + (-big3**3/108 + big5/3 - big4**2/8)**2/4))**(1/3) + R2*T2/(2*P2))/2 + RT/(4*P))))/R/T).real 

 

args = list(args) 

args.reverse() 

args.extend([1, -P/R/T]) 

solns = np.roots(args) 

rho = [i for i in solns if not i.imag and i.real > 0][0].real # Quicker than indexing where imag ==0 

return P/rho/R/T 

 

 

def Z_from_virial_pressure_form(P, *args): 

r'''Calculates the compressibility factor of a gas given its pressure, and  

pressure-form virial coefficients. Any number of coefficients is supported. 

 

.. math:: 

Z = \frac{Pv}{RT} = 1 + B'P + C'P^2 + D'P^3 + E'P^4 \dots 

 

Parameters 

---------- 

P : float 

Pressure, [Pa] 

B to Z : float, optional 

Pressure form Virial coefficients, [various] 

 

Returns 

------- 

Z : float 

Compressibility factor at P, and with given virial coefficients, [-] 

 

Notes 

----- 

Note that although this function does not require a temperature input, it  

is still dependent on it because the coefficients themselves normally are 

regressed in terms of temperature. 

 

The use of this form is less common than the density form. Its coefficients 

are normally indicated with the "'" suffix. 

 

If no virial coefficients are given, returns 1, as per the ideal gas law. 

 

The units of each virial coefficient are as follows, where for B, n=1, and 

C, n=2, and so on. 

 

.. math:: 

\left(\frac{1}{\text{Pa}}\right)^n 

 

Examples 

-------- 

>>> Z_from_virial_pressure_form(102919.99946855308, 4.032286555169439e-09, 1.6197059494442215e-13, 6.483855042486911e-19) 

1.00283753944 

 

References 

---------- 

.. [1] Prausnitz, John M., Rudiger N. Lichtenthaler, and Edmundo Gomes de  

Azevedo. Molecular Thermodynamics of Fluid-Phase Equilibria. 3rd  

edition. Upper Saddle River, N.J: Prentice Hall, 1998. 

.. [2] Walas, Stanley M. Phase Equilibria in Chemical Engineering.  

Butterworth-Heinemann, 1985. 

''' 

return 1 + P*sum([coeff*P**i for i, coeff in enumerate(args)]) 

 

 

def zs_to_ws(zs, MWs): 

r'''Converts a list of mole fractions to mass fractions. Requires molecular 

weights for all species. 

 

.. math:: 

w_i = \frac{z_i MW_i}{MW_{avg}} 

 

MW_{avg} = \sum_i z_i MW_i 

 

Parameters 

---------- 

zs : iterable 

Mole fractions [-] 

MWs : iterable 

Molecular weights [g/mol] 

 

Returns 

------- 

ws : iterable 

Mass fractions [-] 

 

Notes 

----- 

Does not check that the sums add to one. Does not check that inputs are of 

the same length. 

 

Examples 

-------- 

>>> zs_to_ws([0.5, 0.5], [10, 20]) 

[0.3333333333333333, 0.6666666666666666] 

''' 

Mavg = sum(zi*MWi for zi, MWi in zip(zs, MWs)) 

ws = [zi*MWi/Mavg for zi, MWi in zip(zs, MWs)] 

return ws 

 

 

def ws_to_zs(ws, MWs): 

r'''Converts a list of mass fractions to mole fractions. Requires molecular 

weights for all species. 

 

.. math:: 

z_i = \frac{\frac{w_i}{MW_i}}{\sum_i \frac{w_i}{MW_i}} 

 

Parameters 

---------- 

ws : iterable 

Mass fractions [-] 

MWs : iterable 

Molecular weights [g/mol] 

 

Returns 

------- 

zs : iterable 

Mole fractions [-] 

 

Notes 

----- 

Does not check that the sums add to one. Does not check that inputs are of 

the same length. 

 

Examples 

-------- 

>>> ws_to_zs([0.3333333333333333, 0.6666666666666666], [10, 20]) 

[0.5, 0.5] 

''' 

tot = sum(w/MW for w, MW in zip(ws, MWs)) 

zs = [w/MW/tot for w, MW in zip(ws, MWs)] 

return zs 

 

 

def zs_to_Vfs(zs, Vms): 

r'''Converts a list of mole fractions to volume fractions. Requires molar 

volumes for all species. 

 

.. math:: 

\text{Vf}_i = \frac{z_i V_{m,i}}{\sum_i z_i V_{m,i}} 

 

Parameters 

---------- 

zs : iterable 

Mole fractions [-] 

VMs : iterable 

Molar volumes of species [m^3/mol] 

 

Returns 

------- 

Vfs : list 

Molar volume fractions [-] 

 

Notes 

----- 

Does not check that the sums add to one. Does not check that inputs are of 

the same length. 

 

Molar volumes are specified in terms of pure components only. Function 

works with any phase. 

 

Examples 

-------- 

Acetone and benzene example 

 

>>> zs_to_Vfs([0.637, 0.363], [8.0234e-05, 9.543e-05]) 

[0.5960229712956298, 0.4039770287043703] 

''' 

vol_is = [zi*Vmi for zi, Vmi in zip(zs, Vms)] 

tot = sum(vol_is) 

return [vol_i/tot for vol_i in vol_is] 

 

 

def Vfs_to_zs(Vfs, Vms): 

r'''Converts a list of mass fractions to mole fractions. Requires molecular 

weights for all species. 

 

.. math:: 

z_i = \frac{\frac{\text{Vf}_i}{V_{m,i}}}{\sum_i 

\frac{\text{Vf}_i}{V_{m,i}}} 

 

Parameters 

---------- 

Vfs : iterable 

Molar volume fractions [-] 

VMs : iterable 

Molar volumes of species [m^3/mol] 

 

Returns 

------- 

zs : list 

Mole fractions [-] 

 

Notes 

----- 

Does not check that the sums add to one. Does not check that inputs are of 

the same length. 

 

Molar volumes are specified in terms of pure components only. Function 

works with any phase. 

 

Examples 

-------- 

Acetone and benzene example 

 

>>> Vfs_to_zs([0.596, 0.404], [8.0234e-05, 9.543e-05]) 

[0.6369779395901142, 0.3630220604098858] 

''' 

mols_i = [Vfi/Vmi for Vfi, Vmi in zip(Vfs, Vms)] 

mols = sum(mols_i) 

return [mol_i/mols for mol_i in mols_i] 

 

 

def none_and_length_check(all_inputs, length=None): 

r'''Checks inputs for suitability of use by a mixing rule which requires 

all inputs to be of the same length and non-None. A number of variations 

were attempted for this function; this was found to be the quickest. 

 

Parameters 

---------- 

all_inputs : array-like of array-like 

list of all the lists of inputs, [-] 

length : int, optional 

Length of the desired inputs, [-] 

 

Returns 

------- 

False/True : bool 

Returns True only if all inputs are the same length (or length `length`) 

and none of the inputs contain None [-] 

 

Notes 

----- 

Does not check for nan values. 

 

Examples 

-------- 

>>> none_and_length_check(([1, 1], [1, 1], [1, 30], [10,0]), length=2) 

True 

''' 

if not length: 

length = len(all_inputs[0]) 

for things in all_inputs: 

if None in things or len(things) != length: 

return False 

return True 

 

 

def normalize(values): 

r'''Simple function which normalizes a series of values to be from 0 to 1, 

and for their sum to add to 1. 

 

.. math:: 

x = \frac{x}{sum_i x_i} 

 

Parameters 

---------- 

values : array-like 

array of values 

 

Returns 

------- 

fractions : array-like 

Array of values from 0 to 1 

 

Notes 

----- 

Does not work on negative values. 

 

Examples 

-------- 

>>> normalize([3, 2, 1]) 

[0.5, 0.3333333333333333, 0.16666666666666666] 

''' 

tot = sum(values) 

return [i/tot for i in values] 

 

 

def mixing_simple(fracs, props): 

r'''Simple function calculates a property based on weighted averages of 

properties. Weights could be mole fractions, volume fractions, mass 

fractions, or anything else. 

 

.. math:: 

y = \sum_i \text{frac}_i \cdot \text{prop}_i 

 

Parameters 

---------- 

fracs : array-like 

Fractions of a mixture 

props: array-like 

Properties 

 

Returns 

------- 

prop : value 

Calculated property 

 

Notes 

----- 

Returns None if any fractions or properties are missing or are not of the 

same length. 

 

Examples 

-------- 

>>> mixing_simple([0.1, 0.9], [0.01, 0.02]) 

0.019 

''' 

if not none_and_length_check([fracs, props]): 

return None 

result = sum(frac*prop for frac, prop in zip(fracs, props)) 

return result 

 

 

def mixing_logarithmic(fracs, props): 

r'''Simple function calculates a property based on weighted averages of 

logarithmic properties. 

 

.. math:: 

y = \sum_i \text{frac}_i \cdot \log(\text{prop}_i) 

 

Parameters 

---------- 

fracs : array-like 

Fractions of a mixture 

props: array-like 

Properties 

 

Returns 

------- 

prop : value 

Calculated property 

 

Notes 

----- 

Does not work on negative values. 

Returns None if any fractions or properties are missing or are not of the 

same length. 

 

Examples 

-------- 

>>> mixing_logarithmic([0.1, 0.9], [0.01, 0.02]) 

0.01866065983073615 

''' 

if not none_and_length_check([fracs, props]): 

return None 

return exp(sum(frac*log(prop) for frac, prop in zip(fracs, props))) 

 

 

def phase_set_property(phase=None, s=None, l=None, g=None, V_over_F=None): 

r'''Determines which phase's property should be set as a default, given 

the phase a chemical is, and the property values of various phases. For the 

case of liquid-gas phase, returns None. If the property is not available 

for the current phase, or if the current phase is not known, returns None. 

 

Parameters 

---------- 

phase : str 

One of {'s', 'l', 'g', 'two-phase'} 

s : float 

Solid-phase property 

l : float 

Liquid-phase property 

g : float 

Gas-phase property 

V_over_F : float 

Vapor phase fraction 

 

Returns 

------- 

prop : float 

The selected/calculated property for the relevant phase 

 

Notes 

----- 

Could calculate mole-fraction weighted properties for the two phase regime. 

Could also implement equilibria with solid phases. 

 

Examples 

-------- 

>>> phase_set_property(phase='g', l=1560.14, g=3312.) 

3312.0 

''' 

if phase == 's': 

return s 

elif phase == 'l': 

return l 

elif phase == 'g': 

return g 

elif phase == 'two-phase': 

return None #TODO: all two-phase properties? 

elif phase is None: 

return None 

else: 

raise Exception('Property not recognized') 

 

#print phase_set_property(phase='l', l=1560.14, g=3312.) 

 

 

TEST_METHOD_1 = 'Test method 1' 

TEST_METHOD_2 = 'Test method 2' 

 

 

class TDependentProperty(object): 

'''Class for calculating temperature-dependent chemical properties. Should load 

all data about a given chemical on creation. As data is often stored in pandas 

DataFrames, this means that creation is slow. However, the calculation of 

a property at a given temperature is very fast. As coefficients are stored 

in every instance, a user could alter them from those loaded by default. 

 

Designed to intelligently select which method to use at a given temperature, 

according to (1) selections made by the user specifying a list of ordered 

method preferences and (2) by using a default list of prefered methods. 

 

All methods should have defined criteria for determining if they are valid before 

calculation, i.e. a minimum and maximum temperature for coefficients to be 

valid. For constant property values used due to lack of 

temperature-dependent data, a short range is normally specified as valid. 

It is not assumed that any given method will succeed; for example many expressions are 

not mathematically valid past the critical point. If the method raises an 

exception, the next method is tried until either one method works or all 

the supposedly valid have been 

exhausted. Furthermore, all properties returned by the method are checked 

by a sanity function :obj:`test_property_validity`, which has sanity checks for 

all properties. 

 

Works nicely with tabular data, which is interpolated from if specified. 

Interpolation is cubic-spline based if 5 or more points are given, and 

linearly interpolated with if few points are given. Extrapolation is 

permitted if :obj:`tabular_extrapolation_permitted` is set to True. 

For both interpolation and 

extrapolation, a transform may be applied so that a property such as 

vapor pressure can be interpolated non-linearly. These are functions or 

lambda expressions which must be set for the variables :obj:`interpolation_T`, 

:obj:`interpolation_property`, and :obj:`interpolation_property_inv`. 

 

Attributes 

---------- 

name : str 

The name of the property being calculated 

units : str 

The units of the property 

method : str 

The method was which was last used successfully to calculate a property; 

set only after the first property calculation. 

forced : bool 

If True, only user specified methods will be considered; otherwise all 

methods will be considered if none of the user specified methods succeed 

interpolation_T : function 

A function or lambda expression to transform the temperatures of 

tabular data for interpolation; e.g. 'lambda self, T: 1./T' 

interpolation_property : function 

A function or lambda expression to transform tabular property values 

prior to interpolation; e.g. 'lambda self, P: log(P)' 

interpolation_property_inv : function 

A function or property expression to transform interpolated property 

values from the transform performed by `interpolation_property` back 

to their actual form, e.g. 'lambda self, P: exp(P)' 

tabular_extrapolation_permitted : bool 

Whether or not to allow extrapolation from tabulated data for a 

property 

Tmin : float 

Maximum temperature at which no method can calculate the property above; 

set based on rough rules for some methods. Used to solve for a 

particular property value, and as a default minimum for plotting. Often 

higher than where the property is theoretically higher, i.e. liquid 

density above the triple point, but this information may still be 

needed for liquid mixtures with elevated critical points. 

Tmax : float 

Minimum temperature at which no method can calculate the property under; 

set based on rough rules for some methods. Used to solve for a 

particular property value, and as a default minimum for plotting. Often 

lower than where the property is theoretically higher, i.e. liquid 

density beneath the triple point, but this information may still be 

needed for subcooled liquids or mixtures with depressed freezing points. 

property_min : float 

Lowest value expected for a property while still being valid; 

this is a criteria used by `test_method_validity`. 

property_max : float 

Highest value expected for a property while still being valid; 

this is a criteria used by `test_method_validity`. 

ranked_methods : list 

Constant list of ranked methods by default 

tabular_data : dict 

Stores all user-supplied property data for interpolation in format 

{name: (Ts, properties)} 

tabular_data_interpolators : dict 

Stores all interpolation objects, idexed by name and property 

transform methods with the format {(name, interpolation_T, 

interpolation_property, interpolation_property_inv): 

(extrapolator, spline)} 

sorted_valid_methods : list 

Sorted and valid methods stored from the last T_dependent_property 

call 

user_methods : list 

Sorted methods as specified by the user 

''' 

# Dummy properties 

name = 'Property name' 

units = 'Property units' 

tabular_extrapolation_permitted = True 

 

interpolation_T = None 

interpolation_property = None 

interpolation_property_inv = None 

 

method = None 

forced = False 

 

property_min = 0 

property_max = 1E4 # Arbitrary max 

 

# Tmin = None 

# Tmax = None 

ranked_methods = [] 

 

def set_user_methods(self, user_methods, forced=False): 

r'''Method used to select certain property methods as having a higher 

priority than were set by default. If `forced` is true, then methods 

which were not specified are excluded from consideration. 

 

As a side effect, `method` is removed to ensure than the new methods 

will be used in calculations afterwards. 

 

An exception is raised if any of the methods specified aren't available 

for the chemical. An exception is raised if no methods are provided. 

 

Parameters 

---------- 

user_methods : str or list 

Methods by name to be considered or prefered 

forced : bool, optional 

If True, only the user specified methods will ever be considered; 

if False other methods will be considered if no user methods 

suceed 

''' 

# Accept either a string or a list of methods, and whether 

# or not to only consider the false methods 

if isinstance(user_methods, str): 

user_methods = [user_methods] 

 

# The user's order matters and is retained for use by select_valid_methods 

self.user_methods = user_methods 

self.forced = forced 

 

# Validate that the user's specified methods are actual methods 

if set(self.user_methods).difference(self.all_methods): 

raise Exception("One of the given methods is not available for this chemical") 

if not self.user_methods and self.forced: 

raise Exception('Only user specified methods are considered when forced is True, but no methods were provided') 

 

# Remove previously selected methods 

self.method = None 

self.sorted_valid_methods = [] 

 

def select_valid_methods(self, T): 

r'''Method to obtain a sorted list methods which are valid at `T` 

according to `test_method_validity`. Considers either only user methods 

if forced is True, or all methods. User methods are first tested 

according to their listed order, and unless forced is True, then all 

methods are tested and sorted by their order in `ranked_methods`. 

 

Parameters 

---------- 

T : float 

Temperature at which to test methods, [K] 

 

Returns 

------- 

sorted_valid_methods : list 

Sorted lists of methods valid at T according to 

`test_method_validity` 

''' 

# Consider either only the user's methods or all methods 

# Tabular data will be in both when inserted 

if self.forced: 

considered_methods = list(self.user_methods) 

else: 

considered_methods = list(self.all_methods) 

 

# User methods (incl. tabular data); add back later, after ranking the rest 

if self.user_methods: 

[considered_methods.remove(i) for i in self.user_methods] 

 

# Index the rest of the methods by ranked_methods, and add them to a list, sorted_methods 

preferences = sorted([self.ranked_methods.index(i) for i in considered_methods]) 

sorted_methods = [self.ranked_methods[i] for i in preferences] 

 

# Add back the user's methods to the top, in order. 

if self.user_methods: 

[sorted_methods.insert(0, i) for i in reversed(self.user_methods)] 

 

sorted_valid_methods = [] 

for method in sorted_methods: 

if self.test_method_validity(T, method): 

sorted_valid_methods.append(method) 

 

return sorted_valid_methods 

 

@classmethod 

def test_property_validity(self, prop): 

r'''Method to test the validity of a calculated property. Normally, 

this method is used by a given property class, and has maximum and 

minimum limits controlled by the variables `property_min` and 

`property_max`. 

 

Parameters 

---------- 

prop : float 

property to be tested, [`units`] 

 

Returns 

------- 

validity : bool 

Whether or not a specifid method is valid 

''' 

if isinstance(prop, complex): 

return False 

elif prop < self.property_min: 

return False 

elif prop > self.property_max: 

return False 

return True 

 

def T_dependent_property(self, T): 

r'''Method to calculate the property with sanity checking and without 

specifying a specific method. `select_valid_methods` is used to obtain 

a sorted list of methods to try. Methods are then tried in order until 

one succeeds. The methods are allowed to fail, and their results are 

checked with `test_property_validity`. On success, the used method 

is stored in the variable `method`. 

 

If `method` is set, this method is first checked for validity with 

`test_method_validity` for the specified temperature, and if it is 

valid, it is then used to calculate the property. The result is checked 

for validity, and returned if it is valid. If either of th checks fail, 

the function retrieves a full list of valid methods with 

`select_valid_methods` and attempts them as described above. 

 

If no methods are found which succeed, returns None. 

 

Parameters 

---------- 

T : float 

Temperature at which to calculate the property, [K] 

 

Returns 

------- 

prop : float 

Calculated property, [`units`] 

''' 

# Optimistic track, with the already set method 

if self.method: 

# retest within range 

if self.test_method_validity(T, self.method): 

try: 

prop = self.calculate(T, self.method) 

if self.test_property_validity(prop): 

return prop 

except: # pragma: no cover 

pass 

 

# get valid methods at T, and try them until one yields a valid 

# property; store the method and return the answer 

self.sorted_valid_methods = self.select_valid_methods(T) 

for method in self.sorted_valid_methods: 

try: 

prop = self.calculate(T, method) 

if self.test_property_validity(prop): 

self.method = method 

return prop 

except: # pragma: no cover 

pass 

 

# Function returns None if it does not work. 

return None 

 

# def plot(self, Tmin=None, Tmax=None, methods=[], pts=50, only_valid=True, order=0): # pragma: no cover 

# return self.plot_T_dependent_property(Tmin=Tmin, Tmax=Tmax, methods=methods, pts=pts, only_valid=only_valid, order=order) 

 

def plot_T_dependent_property(self, Tmin=None, Tmax=None, methods=[], 

pts=50, only_valid=True, order=0): # pragma: no cover 

r'''Method to create a plot of the property vs temperature according to 

either a specified list of methods, or user methods (if set), or all 

methods. User-selectable number of points, and temperature range. If 

only_valid is set,`test_method_validity` will be used to check if each 

temperature in the specified range is valid, and 

`test_property_validity` will be used to test the answer, and the 

method is allowed to fail; only the valid points will be plotted. 

Otherwise, the result will be calculated and displayed as-is. This will 

not suceed if the method fails. 

 

Parameters 

---------- 

Tmin : float 

Minimum temperature, to begin calculating the property, [K] 

Tmax : float 

Maximum temperature, to stop calculating the property, [K] 

methods : list, optional 

List of methods to consider 

pts : int, optional 

A list of points to calculate the property at; if Tmin to Tmax 

covers a wide range of method validities, only a few points may end 

up calculated for a given method so this may need to be large 

only_valid : bool 

If True, only plot successful methods and calculated properties, 

and handle errors; if False, attempt calculation without any 

checking and use methods outside their bounds 

''' 

# This function cannot be tested 

if not has_matplotlib: 

raise Exception('Optional dependency matplotlib is required for plotting') 

if Tmin is None: 

if self.Tmin is not None: 

Tmin = self.Tmin 

else: 

raise Exception('Minimum temperature could not be auto-detected; please provide it') 

if Tmax is None: 

if self.Tmax is not None: 

Tmax = self.Tmax 

else: 

raise Exception('Maximum temperature could not be auto-detected; please provide it') 

 

if not methods: 

if self.user_methods: 

methods = self.user_methods 

else: 

methods = self.all_methods 

Ts = np.linspace(Tmin, Tmax, pts) 

if order == 0: 

for method in methods: 

if only_valid: 

properties, Ts2 = [], [] 

for T in Ts: 

if self.test_method_validity(T, method): 

try: 

p = self.calculate(T=T, method=method) 

if self.test_property_validity(p): 

properties.append(p) 

Ts2.append(T) 

except: 

pass 

plt.plot(Ts2, properties, label=method) 

else: 

properties = [self.calculate(T=T, method=method) for T in Ts] 

plt.plot(Ts, properties, label=method) 

plt.ylabel(self.name + ', ' + self.units) 

plt.title(self.name + ' of ' + self.CASRN) 

elif order > 0: 

for method in methods: 

if only_valid: 

properties, Ts2 = [], [] 

for T in Ts: 

if self.test_method_validity(T, method): 

try: 

p = self.calculate_derivative(T=T, method=method, order=order) 

properties.append(p) 

Ts2.append(T) 

except: 

pass 

plt.plot(Ts2, properties, label=method) 

else: 

properties = [self.calculate_derivative(T=T, method=method, order=order) for T in Ts] 

plt.plot(Ts, properties, label=method) 

plt.ylabel(self.name + ', ' + self.units + '/K^%d derivative of order %d' % (order, order)) 

plt.title(self.name + ' derivative of order %d' % order + ' of ' + self.CASRN) 

plt.legend() 

plt.xlabel('Temperature, K') 

plt.show() 

 

def interpolate(self, T, name): 

r'''Method to perform interpolation on a given tabular data set 

previously added via :obj:`set_tabular_data`. This method will create the 

interpolators the first time it is used on a property set, and store 

them for quick future use. 

 

Interpolation is cubic-spline based if 5 or more points are available, 

and linearly interpolated if not. Extrapolation is always performed 

linearly. This function uses the transforms `interpolation_T`, 

`interpolation_property`, and `interpolation_property_inv` if set. If 

any of these are changed after the interpolators were first created, 

new interpolators are created with the new transforms. 

All interpolation is performed via the `interp1d` function. 

 

Parameters 

---------- 

T : float 

Temperature at which to interpolate the property, [K] 

name : str 

The name assigned to the tabular data set 

 

Returns 

------- 

prop : float 

Calculated property, [`units`] 

''' 

key = (name, self.interpolation_T, self.interpolation_property, self.interpolation_property_inv) 

 

# If the interpolator and extrapolator has already been created, load it 

# if isinstance(self.tabular_data_interpolators, dict) and key in self.tabular_data_interpolators: 

# extrapolator, spline = self.tabular_data_interpolators[key] 

 

if key in self.tabular_data_interpolators: 

extrapolator, spline = self.tabular_data_interpolators[key] 

else: 

Ts, properties = self.tabular_data[name] 

 

if self.interpolation_T: # Transform ths Ts with interpolation_T if set 

Ts2 = [self.interpolation_T(T2) for T2 in Ts] 

else: 

Ts2 = Ts 

if self.interpolation_property: # Transform ths props with interpolation_property if set 

properties2 = [self.interpolation_property(p) for p in properties] 

else: 

properties2 = properties 

# Only allow linear extrapolation, but with whatever transforms are specified 

extrapolator = interp1d(Ts2, properties2, fill_value='extrapolate') 

# If more than 5 property points, create a spline interpolation 

if len(properties) >= 5: 

spline = interp1d(Ts2, properties2, kind='cubic') 

else: 

spline = None 

# if isinstance(self.tabular_data_interpolators, dict): 

# self.tabular_data_interpolators[key] = (extrapolator, spline) 

# else: 

# self.tabular_data_interpolators = {key: (extrapolator, spline)} 

self.tabular_data_interpolators[key] = (extrapolator, spline) 

 

# Load the stores values, tor checking which interpolation strategy to 

# use. 

Ts, properties = self.tabular_data[name] 

 

if T < Ts[0] or T > Ts[-1] or not spline: 

tool = extrapolator 

else: 

tool = spline 

 

if self.interpolation_T: 

T = self.interpolation_T(T) 

prop = tool(T) # either spline, or linear interpolation 

 

if self.interpolation_property: 

prop = self.interpolation_property_inv(prop) 

 

return float(prop) 

 

def set_tabular_data(self, Ts, properties, name=None, check_properties=True): 

r'''Method to set tabular data to be used for interpolation. 

Ts must be in increasing order. If no name is given, data will be 

assigned the name 'Tabular data series #x', where x is the number of 

previously added tabular data series. The name is added to all 

methods and iserted at the start of user methods, 

 

Parameters 

---------- 

Ts : array-like 

Increasing array of temperatures at which properties are specified, [K] 

properties : array-like 

List of properties at Ts, [`units`] 

name : str, optional 

Name assigned to the data 

check_properties : bool 

If True, the properties will be checked for validity with 

`test_property_validity` and raise an exception if any are not 

valid 

''' 

# Ts must be in increasing order. 

if check_properties: 

for p in properties: 

if not self.test_property_validity(p): 

raise Exception('One of the properties specified are not feasible') 

if not all(b > a for a, b in zip(Ts, Ts[1:])): 

raise Exception('Temperatures are not sorted in increasing order') 

 

if name is None: 

name = 'Tabular data series #' + str(len(self.tabular_data)) # Will overwrite a poorly named series 

self.tabular_data[name] = (Ts, properties) 

# TODO set Tmin and Tmax if not set 

 

self.method = None 

self.user_methods.insert(0, name) 

self.all_methods.add(name) 

 

self.set_user_methods(user_methods=self.user_methods, forced=self.forced) 

 

def solve_prop(self, goal, reset_method=True): 

r'''Method to solve for the temperature at which a property is at a 

specified value. `T_dependent_property` is used to calculate the value 

of the property as a function of temperature; if `reset_method` is True, 

the best method is used at each temperature as the solver seeks a 

solution. This slows the solution moderately. 

 

Checks the given property value with `test_property_validity` first 

and raises an exception if it is not valid. Requires that Tmin and 

Tmax have been set to know what range to search within. 

 

Search is performed with the brenth solver from SciPy. 

 

Parameters 

---------- 

goal : float 

Propoerty value desired, [`units`] 

reset_method : bool 

Whether or not to reset the method as the solver searches 

 

Returns 

------- 

T : float 

Temperature at which the property is the specified value [K] 

''' 

if self.Tmin is None or self.Tmax is None: 

raise Exception('Both a minimum and a maximum value are not present indicating there is not enough data for temperature dependency.') 

if not self.test_property_validity(goal): 

raise Exception('Input property is not considered plausible; no method would calculate it.') 

 

def error(T): 

if reset_method: 

self.method = None 

return self.T_dependent_property(T) - goal 

try: 

return brenth(error, self.Tmin, self.Tmax) 

except ValueError: 

raise Exception('To within the implemented temperature range, it is not possible to calculate the desired value.') 

 

def calculate_derivative(self, T, method, order=1): 

r'''Method to calculate a derivative of a property with respect to  

temperature, of a given order using a specified method. Uses SciPy's  

derivative function, with a delta of 1E-6 K and a number of points  

equal to 2*order + 1. 

 

This method can be overwritten by subclasses who may perfer to add 

analytical methods for some or all methods as this is much faster. 

 

If the calculation does not succeed, returns the actual error 

encountered. 

 

Parameters 

---------- 

T : float 

Temperature at which to calculate the derivative, [K] 

order : int 

Order of the derivative, >= 1 

method : str 

Method for which to find the derivative 

 

Returns 

------- 

derivative : float 

Calculated derivative property, [`units/K^order`] 

''' 

return derivative(self.calculate, T, dx=1e-6, args=[method], n=order, order=1+order*2) 

 

def T_dependent_property_derivative(self, T, order=1): 

r'''Method to obtain a derivative of a property with respect to  

temperature, of a given order. Methods found valid by  

`select_valid_methods` are attempted until a method succeeds. If no  

methods are valid and succeed, None is returned. 

 

Calls `calculate_derivative` internally to perform the actual 

calculation. 

 

.. math:: 

\text{derivative} = \frac{d (\text{property})}{d T} 

 

Parameters 

---------- 

T : float 

Temperature at which to calculate the derivative, [K] 

order : int 

Order of the derivative, >= 1 

 

Returns 

------- 

derivative : float 

Calculated derivative property, [`units/K^order`] 

''' 

sorted_valid_methods = self.select_valid_methods(T) 

for method in sorted_valid_methods: 

try: 

return self.calculate_derivative(T, method, order) 

except: 

pass 

return None 

 

def calculate_integral(self, T1, T2, method): 

r'''Method to calculate the integral of a property with respect to 

temperature, using a specified method. Uses SciPy's `quad` function 

to perform the integral, with no options. 

 

This method can be overwritten by subclasses who may perfer to add 

analytical methods for some or all methods as this is much faster. 

 

If the calculation does not succeed, returns the actual error 

encountered. 

 

Parameters 

---------- 

T1 : float 

Lower limit of integration, [K] 

T2 : float 

Upper limit of integration, [K] 

method : str 

Method for which to find the integral 

 

Returns 

------- 

integral : float 

Calculated integral of the property over the given range,  

[`units*K`] 

''' 

return float(quad(self.calculate, T1, T2, args=(method))[0]) 

 

def T_dependent_property_integral(self, T1, T2): 

r'''Method to calculate the integral of a property with respect to 

temperature, using a specified method. Methods found valid by  

`select_valid_methods` are attempted until a method succeeds. If no  

methods are valid and succeed, None is returned. 

 

Calls `calculate_integral` internally to perform the actual 

calculation. 

 

.. math:: 

\text{integral} = \int_{T_1}^{T_2} \text{property} \; dT 

 

Parameters 

---------- 

T1 : float 

Lower limit of integration, [K] 

T2 : float 

Upper limit of integration, [K] 

method : str 

Method for which to find the integral 

 

Returns 

------- 

integral : float 

Calculated integral of the property over the given range,  

[`units*K`] 

''' 

sorted_valid_methods = self.select_valid_methods(T1) 

for method in sorted_valid_methods: 

try: 

return self.calculate_integral(T1, T2, method) 

except: 

pass 

return None 

 

def calculate_integral_over_T(self, T1, T2, method): 

r'''Method to calculate the integral of a property over temperature 

with respect to temperature, using a specified method. Uses SciPy's  

`quad` function to perform the integral, with no options. 

 

This method can be overwritten by subclasses who may perfer to add 

analytical methods for some or all methods as this is much faster. 

 

If the calculation does not succeed, returns the actual error 

encountered. 

 

Parameters 

---------- 

T1 : float 

Lower limit of integration, [K] 

T2 : float 

Upper limit of integration, [K] 

method : str 

Method for which to find the integral 

 

Returns 

------- 

integral : float 

Calculated integral of the property over the given range,  

[`units`] 

''' 

return float(quad(lambda T: self.calculate(T, method)/T, T1, T2)[0]) 

 

def T_dependent_property_integral_over_T(self, T1, T2): 

r'''Method to calculate the integral of a property over temperature  

with respect to temperature, using a specified method. Methods found 

valid by `select_valid_methods` are attempted until a method succeeds.  

If no methods are valid and succeed, None is returned. 

 

Calls `calculate_integral_over_T` internally to perform the actual 

calculation. 

 

.. math:: 

\text{integral} = \int_{T_1}^{T_2} \frac{\text{property}}{T} \; dT 

 

Parameters 

---------- 

T1 : float 

Lower limit of integration, [K] 

T2 : float 

Upper limit of integration, [K] 

method : str 

Method for which to find the integral 

 

Returns 

------- 

integral : float 

Calculated integral of the property over the given range,  

[`units`] 

''' 

sorted_valid_methods = self.select_valid_methods(T1) 

for method in sorted_valid_methods: 

try: 

return self.calculate_integral_over_T(T1, T2, method) 

except: 

pass 

return None 

 

 

# Dummy functions, always to be overwritten, only for testing 

 

def __init__(self, CASRN=''): 

'''Create an instance of TDependentProperty. Should be overwritten by 

a method created specific to a property. Should take all constant 

properties on creation. 

 

Attributes 

---------- 

''' 

self.CASRN = CASRN 

self.load_all_methods() 

 

self.ranked_methods = [TEST_METHOD_2, TEST_METHOD_1] # Never changes 

self.tabular_data = {} 

self.tabular_data_interpolators = {} 

 

self.sorted_valid_methods = [] 

self.user_methods = [] 

 

def load_all_methods(self): 

r'''Method to load all data, and set all_methods based on the available 

data and properties. Demo function for testing only; must be 

implemented according to the methods available for each individual 

method. 

''' 

methods = [] 

Tmins, Tmaxs = [], [] 

if self.CASRN in ['7732-18-5', '67-56-1', '64-17-5']: 

methods.append(TEST_METHOD_1) 

self.TEST_METHOD_1_Tmin = 200. 

self.TEST_METHOD_1_Tmax = 350 

self.TEST_METHOD_1_coeffs = [1, .002] 

Tmins.append(self.TEST_METHOD_1_Tmin); Tmaxs.append(self.TEST_METHOD_1_Tmax) 

if self.CASRN in ['67-56-1']: 

methods.append(TEST_METHOD_2) 

self.TEST_METHOD_2_Tmin = 300. 

self.TEST_METHOD_2_Tmax = 400 

self.TEST_METHOD_2_coeffs = [1, .003] 

Tmins.append(self.TEST_METHOD_2_Tmin); Tmaxs.append(self.TEST_METHOD_2_Tmax) 

self.all_methods = set(methods) 

if Tmins and Tmaxs: 

self.Tmin = min(Tmins) 

self.Tmax = max(Tmaxs) 

 

def calculate(self, T, method): 

r'''Method to calculate a property with a specified method, with no 

validity checking or error handling. Demo function for testing only; 

must be implemented according to the methods available for each 

individual method. Include the interpolation call here. 

 

Parameters 

---------- 

T : float 

Temperature at which to calculate the property, [K] 

method : str 

Method name to use 

 

Returns 

------- 

prop : float 

Calculated property, [`units`] 

''' 

if method == TEST_METHOD_1: 

prop = self.TEST_METHOD_1_coeffs[0] + self.TEST_METHOD_1_coeffs[1]*T 

elif method == TEST_METHOD_2: 

prop = self.TEST_METHOD_2_coeffs[0] + self.TEST_METHOD_2_coeffs[1]*T 

elif method in self.tabular_data: 

prop = self.interpolate(T, method) 

return prop 

 

def test_method_validity(self, T, method): 

r'''Method to test the validity of a specified method for a given 

temperature. Demo function for testing only; 

must be implemented according to the methods available for each 

individual method. Include the interpolation check here. 

 

Parameters 

---------- 

T : float 

Temperature at which to determine the validity of the method, [K] 

method : str 

Method name to use 

 

Returns 

------- 

validity : bool 

Whether or not a specifid method is valid 

''' 

validity = True 

if method == TEST_METHOD_1: 

if T < self.TEST_METHOD_1_Tmin or T > self.TEST_METHOD_1_Tmax: 

validity = False 

elif method == TEST_METHOD_2: 

if T < self.TEST_METHOD_2_Tmin or T > self.TEST_METHOD_2_Tmax: 

validity = False 

elif method in self.tabular_data: 

# if tabular_extrapolation_permitted, good to go without checking 

if not self.tabular_extrapolation_permitted: 

Ts, properties = self.tabular_data[method] 

if T < Ts[0] or T > Ts[-1]: 

validity = False 

else: 

raise Exception('Method not valid') 

return validity 

 

 

class TPDependentProperty(TDependentProperty): 

'''Class for calculating temperature and pressure dependent chemical 

properties.''' 

interpolation_P = None 

method_P = None 

forced_P = False 

 

def set_user_methods_P(self, user_methods_P, forced_P=False): 

r'''Method to set the pressure-dependent property methods desired for 

consideration by the user. Can be used to exclude certain methods which 

might have unacceptable accuracy. 

 

As a side effect, the previously selected method is removed when 

this method is called to ensure user methods are tried in the desired 

order. 

 

Parameters 

---------- 

user_methods_P : str or list 

Methods by name to be considered or prefered for pressure effect 

forced : bool, optional 

If True, only the user specified methods will ever be considered; 

if False other methods will be considered if no user methods 

suceed 

''' 

# Accept either a string or a list of methods, and whether 

# or not to only consider the false methods 

if isinstance(user_methods_P, str): 

user_methods_P = [user_methods_P] 

 

# The user's order matters and is retained for use by select_valid_methods 

self.user_methods_P = user_methods_P 

self.forced_P = forced_P 

 

# Validate that the user's specified methods are actual methods 

if set(self.user_methods_P).difference(self.all_methods_P): 

raise Exception("One of the given methods is not available for this chemical") 

if not self.user_methods_P and self.forced: 

raise Exception('Only user specified methods are considered when forced is True, but no methods were provided') 

 

# Remove previously selected methods 

self.method_P = None 

self.sorted_valid_methods_P = [] 

 

def select_valid_methods_P(self, T, P): 

r'''Method to obtain a sorted list methods which are valid at `T` 

according to `test_method_validity`. Considers either only user methods 

if forced is True, or all methods. User methods are first tested 

according to their listed order, and unless forced is True, then all 

methods are tested and sorted by their order in `ranked_methods`. 

 

Parameters 

---------- 

T : float 

Temperature at which to test methods, [K] 

P : float 

Pressure at which to test methods, [Pa] 

 

Returns 

------- 

sorted_valid_methods_P : list 

Sorted lists of methods valid at T and P according to 

`test_method_validity` 

''' 

# Same as select_valid_methods but with _P added to variables 

if self.forced_P: 

considered_methods = list(self.user_methods_P) 

else: 

considered_methods = list(self.all_methods_P) 

 

if self.user_methods_P: 

[considered_methods.remove(i) for i in self.user_methods_P] 

 

preferences = sorted([self.ranked_methods_P.index(i) for i in considered_methods]) 

sorted_methods = [self.ranked_methods_P[i] for i in preferences] 

 

if self.user_methods_P: 

[sorted_methods.insert(0, i) for i in reversed(self.user_methods_P)] 

 

sorted_valid_methods_P = [] 

for method in sorted_methods: 

if self.test_method_validity_P(T, P, method): 

sorted_valid_methods_P.append(method) 

 

return sorted_valid_methods_P 

 

def TP_dependent_property(self, T, P): 

r'''Method to calculate the property with sanity checking and without 

specifying a specific method. `select_valid_methods_P` is used to obtain 

a sorted list of methods to try. Methods are then tried in order until 

one succeeds. The methods are allowed to fail, and their results are 

checked with `test_property_validity`. On success, the used method 

is stored in the variable `method_P`. 

 

If `method_P` is set, this method is first checked for validity with 

`test_method_validity_P` for the specified temperature, and if it is 

valid, it is then used to calculate the property. The result is checked 

for validity, and returned if it is valid. If either of the checks fail, 

the function retrieves a full list of valid methods with 

`select_valid_methods_P` and attempts them as described above. 

 

If no methods are found which succeed, returns None. 

 

Parameters 

---------- 

T : float 

Temperature at which to calculate the property, [K] 

P : float 

Pressure at which to calculate the property, [Oa] 

 

Returns 

------- 

prop : float 

Calculated property, [`units`] 

''' 

# Optimistic track, with the already set method 

if self.method_P: 

# retest within range 

if self.test_method_validity_P(T, P, self.method_P): 

try: 

prop = self.calculate_P(T, P, self.method_P) 

if self.test_property_validity(prop): 

return prop 

except: # pragma: no cover 

pass 

 

# get valid methods at T, and try them until one yields a valid 

# property; store the method_P and return the answer 

self.sorted_valid_methods_P = self.select_valid_methods_P(T, P) 

for method_P in self.sorted_valid_methods_P: 

try: 

prop = self.calculate_P(T, P, method_P) 

if self.test_property_validity(prop): 

self.method_P = method_P 

return prop 

except: # pragma: no cover 

pass 

# Function returns None if it does not work. 

return None 

 

def set_tabular_data_P(self, Ts, Ps, properties, name=None, check_properties=True): 

r'''Method to set tabular data to be used for interpolation. 

Ts and Psmust be in increasing order. If no name is given, data will be 

assigned the name 'Tabular data series #x', where x is the number of 

previously added tabular data series. The name is added to all 

methods and is inserted at the start of user methods, 

 

Parameters 

---------- 

Ts : array-like 

Increasing array of temperatures at which properties are specified, [K] 

Ps : array-like 

Increasing array of pressures at which properties are specified, [Pa] 

properties : array-like 

List of properties at Ts, [`units`] 

name : str, optional 

Name assigned to the data 

check_properties : bool 

If True, the properties will be checked for validity with 

`test_property_validity` and raise an exception if any are not 

valid 

''' 

# Ts must be in increasing order. 

if check_properties: 

for p in np.array(properties).ravel(): 

if not self.test_property_validity(p): 

raise Exception('One of the properties specified are not feasible') 

if not all(b > a for a, b in zip(Ts, Ts[1:])): 

raise Exception('Temperatures are not sorted in increasing order') 

if not all(b > a for a, b in zip(Ps, Ps[1:])): 

raise Exception('Pressures are not sorted in increasing order') 

 

if name is None: 

name = 'Tabular data series #' + str(len(self.tabular_data)) # Will overwrite a poorly named series 

self.tabular_data[name] = (Ts, Ps, properties) 

 

self.method_P = None 

self.user_methods_P.insert(0, name) 

self.all_methods_P.add(name) 

 

self.set_user_methods_P(user_methods_P=self.user_methods_P, forced_P=self.forced_P) 

 

def interpolate_P(self, T, P, name): 

r'''Method to perform interpolation on a given tabular data set 

previously added via `set_tabular_data_P`. This method will create the 

interpolators the first time it is used on a property set, and store 

them for quick future use. 

 

Interpolation is cubic-spline based if 5 or more points are available, 

and linearly interpolated if not. Extrapolation is always performed 

linearly. This function uses the transforms `interpolation_T`, 

`interpolation_P`, 

`interpolation_property`, and `interpolation_property_inv` if set. If 

any of these are changed after the interpolators were first created, 

new interpolators are created with the new transforms. 

All interpolation is performed via the `interp2d` function. 

 

Parameters 

---------- 

T : float 

Temperature at which to interpolate the property, [K] 

T : float 

Pressure at which to interpolate the property, [Pa] 

name : str 

The name assigned to the tabular data set 

 

Returns 

------- 

prop : float 

Calculated property, [`units`] 

''' 

key = (name, self.interpolation_T, self.interpolation_P, self.interpolation_property, self.interpolation_property_inv) 

 

# If the interpolator and extrapolator has already been created, load it 

if key in self.tabular_data_interpolators: 

extrapolator, spline = self.tabular_data_interpolators[key] 

else: 

Ts, Ps, properties = self.tabular_data[name] 

 

if self.interpolation_T: # Transform ths Ts with interpolation_T if set 

Ts2 = [self.interpolation_T(T2) for T2 in Ts] 

else: 

Ts2 = Ts 

if self.interpolation_P: # Transform ths Ts with interpolation_T if set 

Ps2 = [self.interpolation_P(P2) for P2 in Ps] 

else: 

Ps2 = Ps 

if self.interpolation_property: # Transform ths props with interpolation_property if set 

properties2 = [self.interpolation_property(p) for p in properties] 

else: 

properties2 = properties 

# Only allow linear extrapolation, but with whatever transforms are specified 

extrapolator = interp2d(Ts2, Ps2, properties2) # interpolation if fill value is missing 

# If more than 5 property points, create a spline interpolation 

if len(properties) >= 5: 

spline = interp2d(Ts2, Ps2, properties2, kind='cubic') 

else: 

spline = None 

self.tabular_data_interpolators[key] = (extrapolator, spline) 

 

# Load the stores values, tor checking which interpolation strategy to 

# use. 

Ts, Ps, properties = self.tabular_data[name] 

 

if T < Ts[0] or T > Ts[-1] or not spline or P < Ps[0] or P > Ps[-1]: 

tool = extrapolator 

else: 

tool = spline 

 

if self.interpolation_T: 

T = self.interpolation_T(T) 

if self.interpolation_P: 

P = self.interpolation_T(P) 

prop = tool(T, P) # either spline, or linear interpolation 

 

if self.interpolation_property: 

prop = self.interpolation_property_inv(prop) 

 

return float(prop) 

 

def plot_isotherm(self, T, Pmin=None, Pmax=None, methods_P=[], pts=50, 

only_valid=True): # pragma: no cover 

r'''Method to create a plot of the property vs pressure at a specified 

temperature according to either a specified list of methods, or the  

user methods (if set), or all methods. User-selectable number of  

points, and pressure range. If only_valid is set, 

`test_method_validity_P` will be used to check if each temperature in  

the specified range is valid, and `test_property_validity` will be used 

to test the answer, and the method is allowed to fail; only the valid  

points will be plotted. Otherwise, the result will be calculated and  

displayed as-is. This will not suceed if the method fails. 

 

Parameters 

---------- 

Pmin : float 

Minimum pressure, to begin calculating the property, [Pa] 

Pmax : float 

Maximum pressure, to stop calculating the property, [Pa] 

methods : list, optional 

List of methods to consider 

pts : int, optional 

A list of points to calculate the property at; if Pmin to Pmax 

covers a wide range of method validities, only a few points may end 

up calculated for a given method so this may need to be large 

only_valid : bool 

If True, only plot successful methods and calculated properties, 

and handle errors; if False, attempt calculation without any 

checking and use methods outside their bounds 

''' 

# This function cannot be tested 

if not has_matplotlib: 

raise Exception('Optional dependency matplotlib is required for plotting') 

if Pmin is None: 

if self.Pmin is not None: 

Pmin = self.Pmin 

else: 

raise Exception('Minimum pressure could not be auto-detected; please provide it') 

if Pmax is None: 

if self.Pmax is not None: 

Pmax = self.Pmax 

else: 

raise Exception('Maximum pressure could not be auto-detected; please provide it') 

 

if not methods_P: 

if self.user_methods_P: 

methods_P = self.user_methods_P 

else: 

methods_P = self.all_methods_P 

Ps = np.linspace(Pmin, Pmax, pts) 

for method_P in methods_P: 

if only_valid: 

properties, Ps2 = [], [] 

for P in Ps: 

if self.test_method_validity_P(T, P, method_P): 

try: 

p = self.calculate_P(T, P, method_P) 

if self.test_property_validity(p): 

properties.append(p) 

Ps2.append(P) 

except: 

pass 

plt.plot(Ps2, properties, label=method_P) 

else: 

properties = [self.calculate_P(T, P, method_P) for P in Ps] 

plt.plot(Ps, properties, label=method_P) 

plt.legend() 

plt.ylabel(self.name + ', ' + self.units) 

plt.xlabel('Pressure, Pa') 

plt.title(self.name + ' of ' + self.CASRN) 

plt.show() 

 

def plot_isobar(self, P, Tmin=None, Tmax=None, methods_P=[], pts=50, 

only_valid=True): # pragma: no cover 

r'''Method to create a plot of the property vs temperature at a  

specific pressure according to 

either a specified list of methods, or user methods (if set), or all 

methods. User-selectable number of points, and temperature range. If 

only_valid is set,`test_method_validity_P` will be used to check if  

each temperature in the specified range is valid, and 

`test_property_validity` will be used to test the answer, and the 

method is allowed to fail; only the valid points will be plotted. 

Otherwise, the result will be calculated and displayed as-is. This will 

not suceed if the method fails. 

 

Parameters 

---------- 

P : float 

Pressure for the isobar, [Pa] 

Tmin : float 

Minimum temperature, to begin calculating the property, [K] 

Tmax : float 

Maximum temperature, to stop calculating the property, [K] 

methods : list, optional 

List of methods to consider 

pts : int, optional 

A list of points to calculate the property at; if Tmin to Tmax 

covers a wide range of method validities, only a few points may end 

up calculated for a given method so this may need to be large 

only_valid : bool 

If True, only plot successful methods and calculated properties, 

and handle errors; if False, attempt calculation without any 

checking and use methods outside their bounds 

''' 

if not has_matplotlib: 

raise Exception('Optional dependency matplotlib is required for plotting') 

if Tmin is None: 

if self.Tmin is not None: 

Tmin = self.Tmin 

else: 

raise Exception('Minimum pressure could not be auto-detected; please provide it') 

if Tmax is None: 

if self.Tmax is not None: 

Tmax = self.Tmax 

else: 

raise Exception('Maximum pressure could not be auto-detected; please provide it') 

 

if not methods_P: 

if self.user_methods_P: 

methods_P = self.user_methods_P 

else: 

methods_P = self.all_methods_P 

Ts = np.linspace(Tmin, Tmax, pts) 

for method_P in methods_P: 

if only_valid: 

properties, Ts2 = [], [] 

for T in Ts: 

if self.test_method_validity_P(T, P, method_P): 

try: 

p = self.calculate_P(T, P, method_P) 

if self.test_property_validity(p): 

properties.append(p) 

Ts2.append(T) 

except: 

pass 

plt.plot(Ts2, properties, label=method_P) 

else: 

properties = [self.calculate_P(T, P, method_P) for T in Ts] 

plt.plot(Ts, properties, label=method_P) 

plt.legend() 

plt.ylabel(self.name + ', ' + self.units) 

plt.xlabel('Temperature, K') 

plt.title(self.name + ' of ' + self.CASRN) 

plt.show() 

 

def calculate_derivative_T(self, T, P, method, order=1): 

r'''Method to calculate a derivative of a temperature and pressure 

dependent property with respect to temperature at constant pressure, 

of a given order using a specified method. Uses SciPy's derivative  

function, with a delta of 1E-6 K and a number of points equal to  

2*order + 1. 

 

This method can be overwritten by subclasses who may perfer to add 

analytical methods for some or all methods as this is much faster. 

 

If the calculation does not succeed, returns the actual error 

encountered. 

 

Parameters 

---------- 

T : float 

Temperature at which to calculate the derivative, [K] 

P : float 

Pressure at which to calculate the derivative, [Pa] 

order : int 

Order of the derivative, >= 1 

method : str 

Method for which to find the derivative 

 

Returns 

------- 

d_prop_d_T_at_P : float 

Calculated derivative property at constant pressure,  

[`units/K^order`] 

''' 

return derivative(self.calculate_P, T, dx=1e-6, args=[P, method], n=order, order=1+order*2) 

 

def calculate_derivative_P(self, P, T, method, order=1): 

r'''Method to calculate a derivative of a temperature and pressure 

dependent property with respect to pressure at constant temperature, 

of a given order using a specified method. Uses SciPy's derivative  

function, with a delta of 0.01 Pa and a number of points equal to  

2*order + 1. 

 

This method can be overwritten by subclasses who may perfer to add 

analytical methods for some or all methods as this is much faster. 

 

If the calculation does not succeed, returns the actual error 

encountered. 

 

Parameters 

---------- 

P : float 

Pressure at which to calculate the derivative, [Pa] 

T : float 

Temperature at which to calculate the derivative, [K] 

order : int 

Order of the derivative, >= 1 

method : str 

Method for which to find the derivative 

 

Returns 

------- 

d_prop_d_P_at_T : float 

Calculated derivative property at constant temperature,  

[`units/Pa^order`] 

''' 

f = lambda P: self.calculate_P(T, P, method) 

return derivative(f, P, dx=1e-2, n=order, order=1+order*2) 

 

def TP_dependent_property_derivative_T(self, T, P, order=1): 

r'''Method to calculate a derivative of a temperature and pressure 

dependent property with respect to temperature at constant pressure, 

of a given order. Methods found valid by `select_valid_methods_P` are  

attempted until a method succeeds. If no methods are valid and succeed, 

None is returned. 

 

Calls `calculate_derivative_T` internally to perform the actual 

calculation. 

 

.. math:: 

\text{derivative} = \frac{d (\text{property})}{d T}|_{P} 

 

Parameters 

---------- 

T : float 

Temperature at which to calculate the derivative, [K] 

P : float 

Pressure at which to calculate the derivative, [Pa] 

order : int 

Order of the derivative, >= 1 

 

Returns 

------- 

d_prop_d_T_at_P : float 

Calculated derivative property, [`units/K^order`] 

''' 

sorted_valid_methods_P = self.select_valid_methods_P(T, P) 

for method in sorted_valid_methods_P: 

try: 

return self.calculate_derivative_T(T, P, method, order) 

except: 

pass 

return None 

 

def TP_dependent_property_derivative_P(self, T, P, order=1): 

r'''Method to calculate a derivative of a temperature and pressure 

dependent property with respect to pressure at constant temperature, 

of a given order. Methods found valid by `select_valid_methods_P` are  

attempted until a method succeeds. If no methods are valid and succeed, 

None is returned. 

 

Calls `calculate_derivative_P` internally to perform the actual 

calculation. 

 

.. math:: 

\text{derivative} = \frac{d (\text{property})}{d P}|_{T} 

 

Parameters 

---------- 

T : float 

Temperature at which to calculate the derivative, [K] 

P : float 

Pressure at which to calculate the derivative, [Pa] 

order : int 

Order of the derivative, >= 1 

 

Returns 

------- 

d_prop_d_P_at_T : float 

Calculated derivative property, [`units/Pa^order`] 

''' 

sorted_valid_methods_P = self.select_valid_methods_P(T, P) 

for method in sorted_valid_methods_P: 

try: 

return self.calculate_derivative_P(P, T, method, order) 

except: 

pass 

return None