-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pcsaft_mix.py
More file actions
251 lines (237 loc) · 8.1 KB
/
test_pcsaft_mix.py
File metadata and controls
251 lines (237 loc) · 8.1 KB
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
import torch
import numpy as np
from feos_torch.pcsaft_mix import PcSaftMix
from feos.eos import State, Contributions, PhaseEquilibrium, EquationOfState
from feos.pcsaft import (
PcSaftParameters,
PcSaftRecord,
Identifier,
PureRecord,
PcSaftBinaryRecord,
BinaryRecord,
)
from feos.si import ANGSTROM, NAV, KELVIN, KB, PASCAL, RGAS, BAR
def test_pcsaft():
params = [
[[1.5, 3.2, 150, 0, 0, 0, 0, 0], [2.5, 3.5, 250, 0, 0, 0, 0, 0]],
[[1.5, 3.2, 150, 2.5, 0, 0, 0, 0], [2.5, 3.5, 250, 0, 0, 0, 0, 0]],
[[1.5, 3.2, 150, 0, 0, 0, 0, 0], [2.5, 3.5, 250, 2, 0, 0, 0, 0]],
[[1.5, 3.2, 150, 2.5, 0, 0, 0, 0], [2.5, 3.5, 250, 2, 0, 0, 0, 0]],
[[1.5, 3.2, 150, 0, 0.03, 2500, 2, 1], [2.5, 3.5, 250, 0, 0, 0, 0, 0]],
[[1.5, 3.2, 150, 0, 0, 0, 0, 0], [2.5, 3.5, 250, 0, 0.025, 1500, 1, 2]],
[[1.5, 3.2, 150, 0, 0.03, 2500, 1, 1], [2.5, 3.5, 250, 0, 0.025, 1500, 1, 1]],
[[1.5, 3.2, 150, 2.5, 0.03, 2500, 1, 1], [2.5, 3.5, 250, 2, 0.025, 1500, 1, 1]],
[[1.5, 3.2, 150, 0, 0.03, 2500, 1, 1], [2.5, 3.5, 250, 0, 0.025, 1500, 0, 1]],
[[1.5, 3.2, 150, 0, 0.03, -500, 0, 2], [2.5, 3.5, 250, 0, 0.025, 1500, 1, 1]],
[[1.5, 3.2, 150, 0, 0, 0, 0, 0], [2.5, 3.5, 250, 0, 0.025, 1500, 0, 1]],
[[1.5, 3.2, 150, 0, 0.03, 2500, 2, 2], [2.5, 3.5, 250, 0, 0.025, 1500, 1, 1]],
[[1.5, 3.2, 150, 0, 0.03, 2500, 2, 2], [2.5, 3.5, 250, 0, 0.025, 1500, 1, 1]],
[[1.5, 3.2, 150, 0, 0.03, 2500, 1, 2], [2.5, 3.5, 250, 0, 0.025, 1500, 2, 1]],
]
kij = torch.tensor([[-0.05, 0]] * len(params), dtype=torch.float64)
kij[12, 1] = 3000
x = torch.tensor(params, dtype=torch.float64)
T = 300
temperature = torch.tensor([T] * len(params), dtype=torch.float64)
rho = [0.001, 0.002]
density = torch.tensor([rho] * len(params), dtype=torch.float64)
records = [
[
PcSaftRecord(
p[0],
p[1],
p[2],
p[3],
kappa_ab=p[4],
epsilon_k_ab=p[5],
na=p[6],
nb=p[7],
)
for p in param
]
for param in params
]
records = [[PureRecord(Identifier(), 1, r) for r in record] for record in records]
pcsaft = [
EquationOfState.pcsaft(
PcSaftParameters.new_binary(
record,
kij
if epsilon_k_ab == 0
else PcSaftBinaryRecord(kij, None, epsilon_k_ab),
)
)
for record, (kij, epsilon_k_ab) in zip(records, kij)
]
states = [
State(eos, T * KELVIN, partial_density=np.array(rho) / (NAV * ANGSTROM**3))
for eos in pcsaft
]
a_feos = [
s.molar_helmholtz_energy(Contributions.Residual)
* (s.density / KB / s.temperature * ANGSTROM**3)
for s in states
]
p_feos = [s.pressure() * (ANGSTROM**3 / KB / s.temperature) for s in states]
v1_feos = [s.partial_molar_volume()[0] / (NAV * ANGSTROM**3) for s in states]
v2_feos = [s.partial_molar_volume()[1] / (NAV * ANGSTROM**3) for s in states]
mu1_feos = [
s.chemical_potential(Contributions.Residual)[0] / RGAS / s.temperature
for s in states
]
mu2_feos = [
s.chemical_potential(Contributions.Residual)[1] / RGAS / s.temperature
for s in states
]
eos = PcSaftMix(x, kij)
a = eos.helmholtz_energy_density(temperature, density)
_, p, mu, v = eos.derivatives(temperature, density)
for i, s in enumerate(
[
"np/np",
"p/np",
"np/p",
"p/p",
"a/np",
"np/a",
"a/a",
"ap/ap",
"a/x",
"x/a",
"np/x",
"aa/a",
"a/a k",
"aa/aa",
]
):
print(
f"{s:5} feos: {a_feos[i]:.16f} {mu1_feos[i]:.16f} {mu2_feos[i]:.16f} {p_feos[i]:.16f} {v1_feos[i]:.16f} {v2_feos[i]:.16f}"
)
print(
f" torch: {a[i].item():.16f} {mu[i,0].item():.16f} {mu[i,1].item():.16f} {p[i].item():.16f} {v[i,0].item():.16f} {v[i,1].item():.16f}\n"
)
assert np.abs(a_feos[i] - a[i].item()) < 1e-14
assert np.abs(mu1_feos[i] - mu[i, 0].item()) < 1e-14
assert np.abs(mu2_feos[i] - mu[i, 1].item()) < 1e-14
assert np.abs(p_feos[i] - p[i].item()) < 1e-14
assert np.abs(v1_feos[i] - v[i, 0].item()) < 1e-11
assert np.abs(v2_feos[i] - v[i, 1].item()) < 1e-11
def test_bubble_point():
h = 1e-8
kij = -0.15
epsilon_k_aibj = 1000
kij = torch.tensor(
[[kij, epsilon_k_aibj], [kij + h, epsilon_k_aibj]],
dtype=torch.float64,
requires_grad=True,
)
params = torch.tensor(
[[[1, 3.5, 150, 0, 0.02, 1500, 1, 1], [1, 3.5, 200, 0, 0.03, 2500, 1, 1]]]
* len(kij),
dtype=torch.float64,
)
temperature = torch.tensor(
[150] * len(params), dtype=torch.float64, requires_grad=True
)
pressure = torch.tensor(
[1e5] * len(params), dtype=torch.float64, requires_grad=True
)
liquid_molefracs = torch.tensor(
[0.5] * len(params), dtype=torch.float64, requires_grad=True
)
eos = PcSaftMix(params, kij)
p, _ = eos.bubble_point(temperature, liquid_molefracs, pressure)
p[0].backward()
print(kij.grad[(0, 0)].item())
records = [
[
PcSaftRecord(
p[0],
p[1],
p[2],
p[3],
kappa_ab=p[4],
epsilon_k_ab=p[5],
na=p[6],
nb=p[7],
)
for p in param
]
for param in params
]
records = [[PureRecord(Identifier(), 1, r) for r in record] for record in records]
pcsaft = [
EquationOfState.pcsaft(
PcSaftParameters.new_binary(
record,
PcSaftBinaryRecord(kij, None, epsilon_k_aibj),
)
)
for record, (kij, epsilon_k_aibj) in zip(records, kij)
]
p_feos = [
PhaseEquilibrium.bubble_point(
eos, 150 * KELVIN, np.array([0.5, 0.5]), BAR
).vapor.pressure()
/ PASCAL
for eos in pcsaft
]
print((p_feos[1] - p_feos[0]) / h)
assert np.abs(p[0].item() - p_feos[0]) < 1e-8
assert np.abs(p[1].item() - p_feos[1]) < 1e-8
assert np.abs(kij.grad[(0, 0)].item() - (p_feos[1] - p_feos[0]) / h) < 1
def test_dew_point():
h = 1e-8
kij = -0.15
kij = torch.tensor(
[[kij, 0], [kij + h, 0]], dtype=torch.float64, requires_grad=True
)
params = torch.tensor(
[[[1, 3.5, 150, 0, 0, 0, 0, 0], [1, 3.5, 200, 0, 0, 0, 0, 0]]] * len(kij),
dtype=torch.float64,
)
temperature = torch.tensor(
[150] * len(params), dtype=torch.float64, requires_grad=True
)
pressure = torch.tensor(
[1e5] * len(params), dtype=torch.float64, requires_grad=True
)
vapor_molefracs = torch.tensor(
[0.5] * len(params), dtype=torch.float64, requires_grad=True
)
eos = PcSaftMix(params, kij)
p, _ = eos.dew_point(temperature, vapor_molefracs, pressure)
p[0].backward()
print(kij.grad[(0, 0)].item())
records = [
[
PcSaftRecord(
p[0],
p[1],
p[2],
p[3],
kappa_ab=p[4],
epsilon_k_ab=p[5],
)
for p in param
]
for param in params
]
records = [[PureRecord(Identifier(), 1, r) for r in record] for record in records]
pcsaft = [
EquationOfState.pcsaft(PcSaftParameters.new_binary(record, kij))
for record, (kij, _) in zip(records, kij)
]
p_feos = [
PhaseEquilibrium.dew_point(
eos, 150 * KELVIN, np.array([0.5, 0.5]), BAR
).vapor.pressure()
/ PASCAL
for eos in pcsaft
]
print((p_feos[1] - p_feos[0]) / h)
print(p)
print(p_feos)
assert np.abs(p[0].item() - p_feos[0]) < 1e-8
assert np.abs(p[1].item() - p_feos[1]) < 1e-8
assert np.abs(kij.grad[(0, 0)].item() - (p_feos[1] - p_feos[0]) / h) < 1