Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"id": "8218498b",
"metadata": {},
"source": [
"\n",
"Two basic approaches are implemented in teqp:\n",
"\n",
"* Iterative calculations given guess values\n",
"* Tracing along iso-curves (constant temperature, etc.) powered by the isochoric thermodynamics formalism"
]
},
{
"cell_type": "code",
"id": "c0b4e863",
"metadata": {},
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
"source": [
"import teqp\n",
"import numpy as np\n",
"import pandas\n",
"import matplotlib.pyplot as plt\n",
"teqp.__version__"
]
},
{
"cell_type": "markdown",
"id": "e57e532b",
"metadata": {},
"source": [
"## Iterative Phase Equilibria"
]
},
{
"cell_type": "markdown",
"id": "1baf38e1",
"metadata": {},
"source": [
"### Pure fluid\n",
"\n",
"For a pure fluid, phase equilibrium between two phases is defined by equating the pressures and Gibbs energies in the two phases. This represents a 2D non-linear rootfinding problem. Newton's method can be used for the rootfinding, and in teqp, automatic differentiation is used to obtain the necessary Jacobian matrix so the implementation is quite efficient.\n",
"\n",
"The method requires guess values, which are the densities of the liquid and vapor densities. In some cases, ancillary or superancillary equations have been developed which provide curves of guess densities as a function of temperature.\n",
"\n",
"For a pure fluid, you can use the ``pure_VLE_T`` method to carry out the iteration."
]
},
{
"cell_type": "raw",
"id": "f0ca0b22",
"metadata": {
"raw_mimetype": "text/restructuredtext"
},
"source": [
"The Python method is here: :py:meth:`pure_VLE_T <teqp.teqp.pure_VLE_T>`"
]
},
{
"cell_type": "code",
"id": "2674227c",
"metadata": {},
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
"source": [
"# Instantiate the model\n",
"model = teqp.canonical_PR([300], [4e6], [0.1])\n",
"\n",
"T = 250 # [K], Temperature to be used\n",
"\n",
"# Here we use the superancillary to get guess values (actually these are more \n",
"# accurate than the results we will obtain from iteration!)\n",
"rhoL0, rhoV0 = model.superanc_rhoLV(T)\n",
"display('guess:', [rhoL0, rhoV0])\n",
"\n",
"# Carry out the iteration, return the liquid and vapor densities\n",
"# The guess values are perturbed to make sure the iteration is actually\n",
"# changing the values\n",
"teqp.pure_VLE_T(model, T, rhoL0*0.98, rhoV0*1.02, 10)"
]
},
{
"cell_type": "markdown",
"id": "f8805ae1",
"metadata": {},
"source": [
"### Binary Mixture"
]
},
{
"cell_type": "markdown",
"id": "76bccf19",
"metadata": {},
"source": [
"For a binary mixture, the approach is roughly similar to that of a pure fluid. The pressure is equated between phases, and the chemical potentials of each component in each phase are forced to be the same. \n",
"\n",
"Again, the user is required to provide guess values, in this case molar concentrations in each phase, and a Newton method is implemented to solve for the phase equilibrium. The analytical Jacobian is obtained from automatic differentiation.\n",
"\n",
"The ``mix_VLE_Tx`` function is the binary mixture analog to ``pure_VLE_T`` for pure fluids."
]
},
{
"cell_type": "raw",
"id": "eef189fd",
"metadata": {
"raw_mimetype": "text/restructuredtext"
},
"source": [
"The Python method is here: :py:meth:`mix_VLE_Tx <teqp.teqp.mix_VLE_Tx>`"
]
},
{
"cell_type": "code",
"id": "b12bd318",
"metadata": {},
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
"source": [
"zA = np.array([0.01, 0.99])\n",
"model = teqp.canonical_PR([300,310], [4e6,4.5e6], [0.1, 0.2])\n",
"model1 = teqp.canonical_PR([300], [4e6], [0.1])\n",
"T = 273.0 # [K]\n",
"# start off at pure of the first component\n",
"rhoL0, rhoV0 = model1.superanc_rhoLV(T)\n",
"\n",
"# then we shift to the given composition in the first phase\n",
"# to get guess values\n",
"rhovecA0 = rhoL0*zA\n",
"rhovecB0 = rhoV0*zA\n",
"\n",
"# carry out the iteration\n",
"code, rhovecA, rhovecB = teqp.mix_VLE_Tx(model, T, rhovecA0, rhovecB0, zA, \n",
" 1e-10, 1e-10, 1e-10, 1e-10, # stopping conditions\n",
" 10 # maximum number of iterations\n",
" )\n",
"code, rhovecA, rhovecB"
]
},
{
"cell_type": "markdown",
"id": "d72ef08e",
"metadata": {},
"source": [
"You can (and should) check the value of the return code to make sure the iteration succeeded. Do not rely on the numerical value of the enumerated return codes!"
]
},
{
"cell_type": "markdown",
"id": "f4e3f914",
"metadata": {},
"source": [
"When it comes to mixture thermodynamics, as soon as you add another component to a pure component to form a binary mixture, the complexity of the thermodynamics entirely changes. For that reason, mixture iterative calculations for mixtures are orders of magnitude more difficult to carry out. Asymmetric mixtures can do all sorts of interesting things that are entirely unlike those of pure fluids, and the algorithms are therefore much, much more complicated. Formulating phase equilibrium problems is not much more complicated than for pure fluids, but the most challenging aspect is to obtain good guess values from which to start an iterative routine, and the difficulty of this problem increases with the complexity of the mixture thermodynamics.\n",
"Ulrich Deiters and Ian Bell have developed a number of algorithms for tracing phase equilibrium solutions as the solution of ordinary differential equations rather than carrying out iterative routines for a given state point. The advantage of the tracing calculations is that they can often be initiated at a state point that is entirely known, for instance the pure fluid endpoint for a subcritical isotherm or isobar."
]
},
{
"cell_type": "raw",
"id": "e0097771",
"metadata": {
"raw_mimetype": "text/restructuredtext"
},
"source": [
"The Python method is here: :py:meth:`trace_VLE_isotherm_binary <teqp.teqp.trace_VLE_isotherm_binary>`"
]
},
{
"cell_type": "markdown",
"id": "63902dba",
"metadata": {},
"source": [
"The C++ implementation returns a string in JSON format, which can be conveniently operated upon, for instance after converting the returned data structure to a ``pandas.DataFrame``. A simple example of plotting a subcritical isotherm for a \"boring\" mixture is presented here:"
]
},
{
"cell_type": "code",
"id": "49dcba2b",
"metadata": {},
"source": [
"model = teqp.canonical_PR([300,310], [4e6,4.5e6], [0.1, 0.2])\n",
"model1 = teqp.canonical_PR([300], [4e6], [0.1])\n",
"T = 273.0 # [K]\n",
"rhoL0, rhoV0 = model1.superanc_rhoLV(T) # start off at pure of the first component\n",
"j = teqp.trace_VLE_isotherm_binary(model, T, np.array([rhoL0, 0]), np.array([rhoV0, 0]))\n",
"display(str(j)[0:100]+'...') # The first few bits of the data\n",
"df = pandas.DataFrame(j) # Now as a data frame\n",
"df.head(3)"
]
},
{
"cell_type": "code",
"id": "9aecca78",
"metadata": {},
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
"source": [
"plt.plot(df['xL_0 / mole frac.'], df['pL / Pa']/1e6)\n",
"plt.plot(df['xV_0 / mole frac.'], df['pL / Pa']/1e6)\n",
"plt.gca().set(xlabel='$x_1,y_1$ / mole frac.', ylabel='p / MPa')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "a9c9fe55",
"metadata": {},
"source": [
"Isn't that exciting!\n",
"\n",
"You can also provide an optional set of flags to the function to control other behaviors of the function, and switch between simple Euler and adaptive RK45 integration (the default)"
]
},
{
"cell_type": "raw",
"id": "264c5123",
"metadata": {
"raw_mimetype": "text/restructuredtext"
},
"source": [
"The options class is here: :py:meth:`TVLEOptions <teqp.teqp.TVLEOptions>`"
]
},
{
"cell_type": "markdown",
"id": "d4193110",
"metadata": {},
"source": [
"Supercritical isotherms work approximately in the same manner"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5b925ad",
"metadata": {},
"outputs": [],
"source": [
"Tc_K = [190.564, 154.581]\n",
"pc_Pa = [4599200, 5042800]\n",
"acentric = [0.011, 0.022]\n",
"model = teqp.canonical_PR(Tc_K, pc_Pa, acentric)\n",
"model1 = teqp.canonical_PR([Tc_K[0]], [pc_Pa[0]], [acentric[0]])\n",
"T = 170.0 # [K] # Note: above Tc of the second component\n",
"rhoL0, rhoV0 = model1.superanc_rhoLV(T) # start off at pure of the first component\n",
"j = teqp.trace_VLE_isotherm_binary(model, T, np.array([rhoL0, 0]), np.array([rhoV0, 0]))\n",
"df = pandas.DataFrame(j) # Now as a data frame\n",
"plt.plot(df['xL_0 / mole frac.'], df['pL / Pa']/1e6)\n",
"plt.plot(df['xV_0 / mole frac.'], df['pL / Pa']/1e6)\n",
"plt.gca().set(xlabel='$x_1,y_1$ / mole frac.', ylabel='p / MPa')\n",
"plt.show()"
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
},
{
"cell_type": "markdown",
"id": "936c5f55",
"metadata": {},
"source": [
"As of version 0.10.0, isobar tracing has been added to ``teqp``. It operates in fundamentally the same fashion as the isotherm tracing and the same recommendations about starting at a pure fluid apply"
]
},
{
"cell_type": "raw",
"id": "81eacaf0",
"metadata": {
"raw_mimetype": "text/restructuredtext"
},
"source": [
"The tracer function class is here: :py:meth:`trace_VLE_isobar_binary <teqp.teqp.trace_VLE_isobar_binary>`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "109bc65b",
"metadata": {},
"outputs": [],
"source": [
"Tc_K = [190.564, 154.581]\n",
"pc_Pa = [4599200, 5042800]\n",
"acentric = [0.011, 0.022]\n",
"model = teqp.canonical_PR(Tc_K, pc_Pa, acentric)\n",
"model1 = teqp.canonical_PR([Tc_K[0]], [pc_Pa[0]], [acentric[0]])\n",
"T = 170.0 # [K] # Note: above Tc of the second component\n",
"rhoL0, rhoV0 = model1.superanc_rhoLV(T) # start off at pure of the first component\n",
"p0 = rhoL0*model1.get_R(np.array([1.0]))*T*(1+model1.get_Ar01(T, rhoL0, np.array([1.0])))\n",
"j = teqp.trace_VLE_isobar_binary(model, p0, T, np.array([rhoL0, 0]), np.array([rhoV0, 0]))\n",
"df = pandas.DataFrame(j) # Now as a data frame\n",
"plt.plot(df['xL_0 / mole frac.'], df['T / K'])\n",
"plt.plot(df['xV_0 / mole frac.'], df['T / K'])\n",
"plt.gca().set(xlabel='$x_1,y_1$ / mole frac.', ylabel='T / K')\n",
"plt.show()"
]
"celltoolbar": "Raw Cell Format",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}