-
Notifications
You must be signed in to change notification settings - Fork 45
/
multifluid.jl
executable file
·283 lines (246 loc) · 9.78 KB
/
multifluid.jl
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
struct MultiFluidParam <: EoSParam
Mw::SingleParam{Float64}
Tc::SingleParam{Float64}
Pc::SingleParam{Float64}
Vc::SingleParam{Float64}
acentricfactor::SingleParam{Float64}
lb_volume::SingleParam{Float64}
reference_state::ReferenceState
end
function MultiFluidParam(components,pures,reference_state = nothing)
Mw = SingleParam("Mw",components,[pure.properties.Mw for pure in pures])
Tc = SingleParam("Tc",components,[pure.properties.Tc for pure in pures])
Pc = SingleParam("Pc",components,[pure.properties.Pc for pure in pures])
Vc = SingleParam("Vc",components,1 ./ [pure.properties.rhoc for pure in pures])
acentricfactor = SingleParam("acentric factor",components,[pure.properties.acentricfactor for pure in pures])
lb_volume = SingleParam("lower bound volume",components,[pure.properties.lb_volume for pure in pures])
ref = __init_reference_state_kw(reference_state)
return MultiFluidParam(Mw,Tc,Pc,Vc,acentricfactor,lb_volume,ref)
end
struct MultiFluid{𝔸,𝕄,ℙ} <: EmpiricHelmholtzModel
components::Vector{String}
params::MultiFluidParam
pures::Vector{SingleFluid{𝔸}}
mixing::𝕄
departure::ℙ
Rgas::Float64
references::Vector{String}
end
Rgas(model::MultiFluid) = model.Rgas
"""
MultiFluid(components;
idealmodel = nothing,
ideal_userlocations = String[],
pure_userlocations = String[],
mixing = AsymmetricMixing,
departure = EmpiricDeparture,
mixing_userlocations = String[],
departure_userlocations = String[],
estimate_pure = false,
estimate_mixing = :off,
coolprop_userlocations = true,
Rgas = nothing,
reference_state = nothing,
verbose = false)
## Input parameters
- JSON data (CoolProp and teqp format)
## Input models
- `idealmodel`: Ideal Model. if it is `nothing`, then it will parse the ideal model from the input JSON.
- `mixing`: mixing model for temperature and volume.
- `departure`: departure model
## Description
Instantiates a multi-component Empiric EoS model. `Rgas` can be used to set the value of the gas constant that is used during property calculations.
If `coolprop_userlocations` is true, then Clapeyron will try to look if the fluid is present in the CoolProp library.
If `estimate_pure` is true, then, if a JSON is not found, the pure model will be estimated, using the `XiangDeiters` model
`estimate_mixing` is used to fill missing mixing values in the case of using `AsymmetricMixing`. on other mixing models it has no effect.
- `estimate_mixing = :off` will perform no calculation of mixing parameter, throwing an error if missing values are found.
- `estimate_mixing = :lb` will perform Lorentz-Berthelot estimation of missing mixing parameters. (γT = βT = γv = βv = 1.0). additionally, you can pass `LorentzBerthelotMixing` to use `k` and `l` BIP instead.
- `estimate_mixing = :linear` will perform averaging of γT and γv so that `T(x) = ∑xᵢTᵢ` and `V(x) = ∑xᵢVᵢ` on missing mixing parameters. Additionally, you can use `LinearMixing` to perform this directly.
`Rgas` sets the value of the gas constant to be used by the multifluid. The default is the following:
- If `Rgas` is not specified and the input is a single component model, then the value of `Rgas` will be taken from the fluid json file.
- If `Rgas` is not specified and the input is a multi-component model, then the value of `Rgas` will be set to `Clapeyron.R̄ = Rgas() = 8.31446261815324` (2019 defined constant value)
"""
MultiFluid
function MultiFluid(components;
idealmodel = nothing,
pure_userlocations = String[],
ideal_userlocations = String[],
mixing = AsymmetricMixing,
departure = EmpiricDeparture,
mixing_userlocations = String[],
departure_userlocations = String[],
estimate_pure = false,
estimate_mixing = :off,
coolprop_userlocations = true,
Rgas = nothing,
reference_state = nothing,
verbose = false)
_components = format_components(components)
if idealmodel === nothing
idealmodels = FillArrays.Fill(nothing,length(_components))
else
init_idealmodel = init_model(idealmodel,components,ideal_userlocations,verbose,reference_state)
idealmodels = split_model(init_idealmodel,1:length(_components))
end
pures = [
SingleFluid(comp;
userlocations = pure_userlocations,
idealmodel = idealmodels[i],
verbose = verbose,
estimate_pure = estimate_pure,
coolprop_userlocations = coolprop_userlocations,
Rgas = Rgas
)
for (i,comp) in pairs(_components)]
mixing = init_model(mixing,components,mixing_userlocations,verbose)
departure = init_model(departure,components,departure_userlocations,verbose)
params = MultiFluidParam(_components,pures,reference_state)
references = unique!(reduce(vcat,pure.references for pure in pures))
if Rgas == nothing
if length(pures) != 1
Rgas = Clapeyron.Rgas()
else
Rgas = Clapeyron.Rgas(pures[1])
end
end
model = MultiFluid(_components,params,pures,mixing,departure,Rgas,references)
recombine_mixing!(model,model.mixing,estimate_mixing)
recombine_departure!(model,model.departure)
set_reference_state!(model,verbose = verbose)
return model
end
function reduced_delta(model,V,T,z,Σz = sum(z))
Vᵣ = v_scale(model,z,Σz)
Σz * Vᵣ/V
end
function reduced_tau(model,V,T,z,Σz = sum(z))
Tᵣ = T_scale(model,z,Σz)
Tᵣ / T
end
function a_ideal(model::MultiFluid,V,T,z,∑z = sum(z))
#log(δi) = log(ρ * vc[i]) = -log(V) + log(sum(z)) + log(vc[i])
res = zero(V+T+first(z))
m₀ = model.pures
Tinv = 1/T
Tc = model.params.Tc
vc = model.params.Vc
Rinv = 1/Rgas(model)
for i in 1:length(model)
m₀ᵢ = m₀[i]
a₀ᵢ = reduced_a_ideal(m₀ᵢ,Tc[i] * Tinv)
R₀ = m₀ᵢ.ideal.R0
if !iszero(R₀)
a₀ᵢ *=Rinv*R₀
end
zᵢ = z[i]
res += zᵢ*a₀ᵢ
res += xlogx(zᵢ,vc[i])
res
end
res /= ∑z
res -= log(V)
return res
end
function a_res(model::MultiFluid,V,T,z)
∑z = sum(z)
δ = reduced_delta(model,V,T,z,∑z)
τ = reduced_tau(model,V,T,z,∑z)
return multiparameter_a_res(model,V,T,z,model.departure,δ,τ,∑z)
end
function eos(model::MultiFluid,V,T,z = SA[1.0])
∑z = sum(z)
a₀ = a_ideal(model,V,T,z,∑z)
δ = reduced_delta(model,V,T,z,∑z)
τ = reduced_tau(model,V,T,z,∑z)
aᵣ = multiparameter_a_res(model,V,T,z,model.departure,δ,τ,∑z)
return ∑z*@R̄()*T*(a₀+aᵣ) + reference_state_eval(model,V,T,z)
end
function eos_res(model::MultiFluid,V,T,z = SA[1.0])
∑z = sum(z)
δ = reduced_delta(model,V,T,z,∑z)
τ = reduced_tau(model,V,T,z,∑z)
aᵣ = multiparameter_a_res(model,V,T,z,model.departure,δ,τ,∑z)
return ∑z*@R̄()*T*aᵣ
end
v_scale(model::MultiFluid,z = SA[1.0],∑z = sum(z)) = v_scale(model,z,model.mixing,∑z)
T_scale(model::MultiFluid,z = SA[1.0],∑z = sum(z)) = T_scale(model,z,model.mixing,∑z)
p_scale(model::MultiFluid,z=SA[1.]) = dot(z,model.params.Pc.values)/sum(z)
T_scales(model::MultiFluid,z=SA[1.]) = model.params.Tc.values
#single functions, dispatch to pure
function x0_sat_pure(model::MultiFluid,T)
single_component_check(x0_sat_pure,model)
x0_sat_pure(only(model.pures),T)
end
function x0_psat(model::MultiFluid,T,crit = nothing)
single_component_check(x0_psat,model)
x0_psat(only(model.pures),T,crit)
end
function x0_saturation_temperature(model::MultiFluid,p)
single_component_check(x0_saturation_temperature,model)
x0_saturation_temperature(only(model.pures),p)
end
function crit_pure(model::MultiFluid)
single_component_check(crit_pure,model)
crit_pure(only(model.pures))
end
function lb_volume(model::MultiFluid,z=SA[1.])
return dot(z,model.params.lb_volume.values)
end
#use ideal gas
function x0_volume_gas(model::MultiFluid,p,T,z=SA[1.])
V = sum(z)*R̄*T/p
return V
end
#use each available pure x0_volume_liquid
function x0_volume_liquid(model::MultiFluid,T,z)
v0 = zero(T+first(z))
for (i,pure) in pairs(model.pures)
v0 += z[i]*x0_volume_liquid(pure,T,SA[1.0])
end
return v0
end
function wilson_k_values!(K,model::MultiFluid,p,T,crit = nothing)
n = length(model)
pure = split_model.(model)
_Tc = model.params.Tc.values
_Pc = model.params.Pc.values
for i ∈ 1:n
pure_i = pure[i]
Tc,pc = _Tc[i],_Pc[i]
ps = first(saturation_pressure(pure_i,0.7*Tc))
ω = -log10(ps/pc) - 1.0
K[i] = exp(log(pc/p)+5.373*(1+ω)*(1-Tc/T))
end
return K
end
#set reference states:
reference_state(model::MultiFluid) = model.params.reference_state
set_reference_state!(model::MultiFluid;verbose = false) = set_reference_state_empiric!(model;verbose)
function set_reference_state_empiric!(model;verbose = false)
#handle cases where we don't need to do anything
ref = reference_state(model)
ref === nothing && return nothing
ref.std_type == :no_set && return nothing
if verbose
@info "Calculating reference states for $model..."
@info "Reference state type: $(info_color(ref.std_type))"
end
#allocate the appropiate caches.
initialize_reference_state!(model,ref)
pures = model.pures
if all(iszero,ref.z0) #pure case
pure_refs = split_model(ref,(SA[i] for i ∈ 1:length(model)))
_set_reference_state!.(pures,SA[1.0],pure_refs)
ref.a0 .= only.(getfield.(pure_refs,:a0))
ref.a1 .= only.(getfield.(pure_refs,:a1))
else
_set_reference_state!(model,ref.z0)
end
for (i,pure) in pairs(pures)
ref_a = pure.ideal.ref_a
ref_a[1] = pure_refs[i].a0[1]
ref_a[2] = pure_refs[i].a1[1]
end
return model
end
export MultiFluid