Skip to content

Commit 7580968

Browse files
committed
reduce boxing variables (using JuliaLang/julia#60478)
1 parent 6a3fcd7 commit 7580968

File tree

22 files changed

+78
-116
lines changed

22 files changed

+78
-116
lines changed

src/estimation/estimation.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,16 @@ function objective_function(estimation::Estimation,guesses)
357357
inputs = data.inputs
358358
outputs = data.outputs
359359
weights = data.weights
360-
if isempty(inputs)
361-
prediction = property(model_r)
360+
prediction = if isempty(inputs)
361+
property(model_r)
362362
elseif length(inputs)==1
363-
prediction = property.(Ref(model_r),inputs[1])
363+
property.(Ref(model_r),inputs[1])
364364
elseif length(inputs)==2
365-
prediction = property.(Ref(model_r),inputs[1],inputs[2])
365+
property.(Ref(model_r),inputs[1],inputs[2])
366366
elseif length(inputs)==3
367-
prediction = property.(Ref(model_r),inputs[1],inputs[2],inputs[3])
367+
property.(Ref(model_r),inputs[1],inputs[2],inputs[3])
368368
else
369-
prediction = property.(Ref(model_r),inputs...)
369+
property.(Ref(model_r),inputs...)
370370
end
371371

372372
if length(outputs)==1

src/methods/initial_guess.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,9 +1015,9 @@ function solve_2ph_taylor(v10,v20,a1,da1,d2a1,a2,da2,d2a2,p_scale = 1.0,μ_scale
10151015
end
10161016
x0 = SVector((log(v10),log(v20)))
10171017
x = Solvers.nlsolve2(F0,x0,Solvers.Newton2Var())
1018-
v1,v2 = exp(x[1]), exp(x[2])
1019-
p1 = log(v1/v10)*(-v1*d2a1) - da1
1020-
return v1, v2, p1
1018+
v1_sol,v2_sol = exp(x[1]), exp(x[2])
1019+
p1_sol = log(v1_sol/v10)*(-v1_sol*d2a1) - da1
1020+
return v1_sol, v2_sol, p1_sol
10211021
end
10221022

10231023
function solve_2ph_taylor(model1::EoSModel,model2::EoSModel,T,v1,v2,p_scale = 1.0,μ_scale = 1.0)

src/methods/property_solvers/fugacity_coefficient.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,11 @@ function ∂lnϕ∂n∂P(model::EoSModel, p, T, z=SA[1.], cache = ∂lnϕ_cache(
147147
n = sum(z)
148148
Z = p*V/RT/n
149149

150+
ncomponents = length(z)
150151
F_res(model, V, T, z) = eos_res(model, V, T, z) / RT
151152
fun(aux) = F_res(model, aux[1], T, @view(aux[2:(ncomponents+1)]))
152153

153-
ncomponents = length(z)
154+
154155
aux[1] = V
155156
aux[2:end] = z
156157
result = ForwardDiff.hessian!(result, fun, aux, hconfig, Val{false}())

src/methods/property_solvers/multicomponent/flash/QP.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ function qp_flash_x0(model,β,p,z,method::FlashMethod)
4646
#Tmin,Tmax = extrema(T0)
4747
#we approximate sat(T) ≈ exp(-dpdT*T*T(1/T - 1/T0)/p)*p
4848
K = similar(dpdT,typeof(Tmax))
49-
x = z ./ sum(z)
50-
ft(_T) = qp_f0_T!(K,x,dpdT,p,_T,β)
49+
xx = z ./ sum(z)
50+
ft(_T) = qp_f0_T!(K,xx,dpdT,p,_T,β)
5151
#we do a search over Tmin-Tmax domain, finding the minimum value of the objective function
5252
Tm = β*Tmax + (1 - β)*Tmin
5353
Tr1 = range(Tmin,Tm,5*length(model))

src/methods/property_solvers/multicomponent/flash/QT.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ function qt_flash_x0(model,β,T,z,method::FlashMethod)
4242
p_bubble = @sum(ps[i]*z[i])/∑z
4343
p_dew = ∑z/@sum(z[i]/ps[i])
4444
pmin,pmax = p_dew,p_bubble
45-
x = z ./ sum(z)
46-
fp(p) = qt_f0_p!(K,x,p,ps,β)
45+
xx = z ./ sum(z)
46+
fp(p) = qt_f0_p!(K,xx,p,ps,β)
4747
pm = β*pmin + (1-β)*pmax
4848
pr1 = range(pmin,pm,5*length(model))
4949
pr2 = range(pm,pmax,5*length(model))

src/methods/property_solvers/multicomponent/flash/general_flash.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,17 +573,15 @@ function xy_flash(model::EoSModel,spec::FlashSpecifications,z,comps0,β0,volumes
573573
s = copy(input)
574574
#config,μconfig = xy_flash_config(model,input)
575575
f!(output,input) = xy_flash_neq(output,model,zz,np,input,new_spec,nothing)
576-
Θ(_f) = 0.5*dot(_f,_f)
577576
srtol = abs2(cbrt(rtol))
578577
function Θ(_f,_z)
579578
f!(_f,_z)
580579
_f[slacks] .= 0
581-
Θ(_f)
580+
0.5*dot(_f,_f)
582581
end
583582
config = ForwardDiff.JacobianConfig(f!,F,x)
584583
#ForwardDiff.jacobian!(J,f!,F,x,config,Val{false}())
585-
f!(F,x)
586-
Θx = Θ(F)
584+
Θx = Θ(F,x)
587585
Fnorm = sqrt(2*Θx)
588586
spec_norm = norm(viewlast(F,2),Inf)
589587
converged = Fnorm < rtol

src/methods/property_solvers/multicomponent/solids/sle_solubility.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ function sle_solubility(model::CompositeModel,p,T,z;solute=nothing,x0=nothing)
3131
Tm = model.solid.params.Tm.values[idx_sol_s][1]
3232

3333
idx_sol_l = zeros(Bool,nf)
34-
solute_l = mapping[idx_sol_s][1]
35-
ν_l = [solute_l[1][i][2] for i in 1:length(solute_l[1])]
36-
solute_l = [solute_l[1][i][1] for i in 1:length(solute_l[1])]
34+
solute_l1 = mapping[idx_sol_s][1]
35+
ν_l = [solute_l1[1][i][2] for i in 1:length(solute_l1[1])]
36+
solute_l = [solute_l1[1][i][1] for i in 1:length(solute_l1[1])]
3737

3838

3939
for i in solute_l
@@ -152,9 +152,9 @@ function sle_solubility_T(model::CompositeModel,z,p=1e5;solute=nothing,x0=nothin
152152
Tm = model.solid.params.Tm.values[idx_sol_s][1]
153153

154154
idx_sol_l = zeros(Bool,nf)
155-
solute_l = mapping[idx_sol_s][1]
156-
ν_l = [solute_l[1][i][2] for i in 1:length(solute_l[1])]
157-
solute_l = [solute_l[1][i][1] for i in 1:length(solute_l[1])]
155+
solute_l1 = mapping[idx_sol_s][1]
156+
ν_l = [solute_l1[1][i][2] for i in 1:length(solute_l1[1])]
157+
solute_l = [solute_l1[1][i][1] for i in 1:length(solute_l1[1])]
158158

159159
for i in solute_l
160160
idx_sol_l[fluid_components .== i] .= true

src/methods/property_solvers/multicomponent/tp_flash/electrolyte_flash.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ end
283283

284284
function rachfordrice(K, z, Z; β0=nothing, ψ0=nothing, non_inx=FillArrays.Fill(false,length(z)), non_iny=FillArrays.Fill(false,length(z)),verbose = false)
285285
# Function to solve Rachdord-Rice mass balance
286-
β,status,limits = rachfordrice_β0(K.*exp.(Z.*ψ0),z,β0,non_inx,non_iny)
286+
status = rachfordrice_status(K.*exp.(Z.*ψ0),z,β0,non_inx,non_iny)
287287
if status == RREq
288288
function rachford_rice_donnan(x,K,z,Z)
289289
β = x[1]
@@ -304,9 +304,9 @@ function rachfordrice(K, z, Z; β0=nothing, ψ0=nothing, non_inx=FillArrays.Fill
304304
ff(F,x) = rachford_rice_donnan(x,K,z,Z)
305305
results = Solvers.nlsolve(ff,x0)
306306
sol = Clapeyron.Solvers.x_sol(results)
307-
β = sol[1]
308-
ψ = sol[2]
309-
return SVector(Base.promote(β,ψ))
307+
β_sol = sol[1]
308+
ψ_sol = sol[2]
309+
return SVector(Base.promote(β_sol,ψ_sol))
310310
else
311311
return SVector(Base.promote(β0,ψ0))
312312
end

src/methods/property_solvers/multicomponent/tp_flash/multiphase.jl

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,63 +1057,27 @@ function split_phase_tpd(model,p,T,z,w,phase_z = :unknown,phase_w = :unknown,vz
10571057
x2 = similar(w)
10581058
g1 = eos(model,vw,T,w) + p*vw
10591059
gz = eos(model,vz,T,z) + p*vz
1060+
10601061
if isone(β2)
10611062
x2 .= z
10621063
else
10631064
@. x2 = (z - β2 * w) / (1 - β2)
10641065
end
1066+
10651067
x3 = x2
1066-
phase = phase_w == phase_z ? phase_z : :unknown
1067-
if is_vapour(phase_w) && is_unknown(phase)
1068-
phase = :liquid
1069-
end
1070-
v2 = volume(model,p,T,x2,threaded = false,phase = phase)
1071-
v3 = volume(model,p,T,x3,threaded = false,phase = phase)
1072-
g2 = eos(model,v2,T,x2) + p*v2
1073-
g3 = eos(model,v3,T,x3) + p*v3
1074-
dg1 = g1 - gz
1075-
dg2 = β2*g1 + (1-β2)*g2 - gz
1076-
function f(βx)
1077-
x3 .= (z .- βx .* w) ./ (1 .- βx)
1078-
v3 = volume(model,p,T,x3,threaded = false,phase = phase)
1079-
g3 = eos(model,v3,T,x3) + p*v3
1080-
return βx*g1 + (1-βx)*g3 - gz
1081-
end
1082-
ϕ = 0.6180339887498949
1083-
βi = ϕ*β1 + (1-ϕ)*β2
1084-
βi0 = one(βi)*10
1085-
_1 = one(βi)
1086-
dgi = βi*g1 + (1-βi)*g3 - gz
1087-
βi*g1 + (1-βi)*g3 - gz
1088-
for i in 1:20
1089-
x3 .= (z .- βi .* w) ./ (1 .- βi)
1090-
v3 = volume(model,p,T,x3,threaded = false,phase = phase)
1091-
g3 = eos(model,v3,T,x3) + p*v3
1092-
isnan(g3) && break
1093-
dgi = βi*g1 + (1-βi)*g3 - gz
1094-
#quadratic interpolation
1095-
A = @SMatrix [β1*β1 β1 _1; β2*β2 β2 _1; βi*βi βi _1]
1096-
b = SVector(dg1,dg2,dgi)
1097-
c = A\b
1098-
βi_intrp = -0.5*c[2]/c[1]
1099-
if dgi < dg2 < dg1
1100-
dg1,β1 = dgi,βi
1101-
elseif dgi < dg1 < dg2
1102-
dg2,β2,v2 = dgi,βi,v3
1103-
elseif dgi < dg1
1104-
dg1,β1,v1 = dgi,βi,v3
1105-
elseif dgi < dg2
1106-
dg2,β2,v2 = dgi,βi,v3
1107-
else
1108-
break
1109-
end
1110-
βi0 = βi
1111-
βi_bisec = ϕ*β1 + (1-ϕ)*β2
1112-
βi = β1 <= βi_intrp <= β2 ? βi_intrp : βi_bisec
1113-
abs(βi0 - βi) < 1e-5 && break
1068+
phase = phase_w == phase_z ? phase_z : (is_vapour(phase_w) ? :liquid : :unknown)
1069+
vcache = Ref(g1)
1070+
1071+
function ff(_β)
1072+
x3 .= (z .-.* w) ./ (1 .- _β)
1073+
_v3 = volume(model,p,T,x3,threaded = false,phase = phase)
1074+
g3 = eos(model,_v3,T,x3) + p*_v3
1075+
vcache[] = _v3
1076+
return*g1 + (1-_β)*g3 - gz
11141077
end
1115-
#@assert βi*w + (1-βi)*x3 ≈ z
1116-
return (1-βi),x3,v3,βi,w,vw,dgi
1078+
1079+
βi = Solvers.optimize(ff,(β2,oneunit(β2)),Solvers.BoundOptim1Var(),NLSolvers.OptimizationOptions(x_abstol = 1e-5)) #@assert βi*w + (1-βi)*x3 ≈ z
1080+
return (1-βi),x3,vcache[],βi,w,vw,dgi
11171081
end
11181082

11191083
function split_phase_k(model,p,T,z,K = nothing,vz = volume(model,p,T,z),pures = split_pure_model(model))

src/methods/property_solvers/singlecomponent/widom.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function widom_temperature(model,p;T0 = nothing,v0 = nothing)
104104
_T0 = T0*_1
105105
end
106106

107-
function f_wp(x::AbstractVector)
107+
function f_wp_vec(x::AbstractVector)
108108
v,T = exp(x[1]),x[2]
109109
Cₚ(∂v,∂T) = VT_isobaric_heat_capacity(model,∂v,∂T,SA[1.0])
110110
fp(∂v,∂T) = pressure(model,∂v,∂T,SA[1.0])
@@ -119,12 +119,12 @@ function widom_temperature(model,p;T0 = nothing,v0 = nothing)
119119
#we do a refinement of the initial value first. it improves convergence near the critical point
120120
function f_wp(T)
121121
v = volume(model,p,T,phase = :l,vol0 = _v0)
122-
return f_wp(SVector(log(v),T))[1]
122+
return f_wp_vec(SVector(log(v),T))[1]
123123
end
124124
prob = Roots.ZeroProblem(f_wp,_T0)
125125
_T00 = Roots.solve(prob,Roots.Order2(),atol = 1e-1)
126126
_v00 = volume(model,p,_T00,phase = :l, vol0 = _v0)
127-
res = Solvers.nlsolve2(f_wp,SVector(log(_v00),_T00),Solvers.Newton2Var())
127+
res = Solvers.nlsolve2(f_wp_vec,SVector(log(_v00),_T00),Solvers.Newton2Var())
128128
log_v_widom,T_widom = res
129129
return T_widom,exp(log_v_widom)
130130
end
@@ -233,7 +233,7 @@ function ciic_temperature(model,p;T0 = nothing,v0 = nothing)
233233
lb_v = lb_volume(model,_T0,SA[1.0])
234234
scale = p*p/_T0/lb_volume(model,_T0,SA[1.0])
235235
#P*P/(T*v)
236-
function f_ciic(x::AbstractVector)
236+
function f_ciic_vec(x::AbstractVector)
237237
v,T = exp(x[1]),x[2]
238238
Cₚ(∂v) = VT_isobaric_heat_capacity(model,∂v,T,SA[1.0])
239239
fp(∂v) = pressure(model,∂v,T,SA[1.0])
@@ -247,13 +247,13 @@ function ciic_temperature(model,p;T0 = nothing,v0 = nothing)
247247
#we do a refinement of the initial value first. it improves convergence near the critical point
248248
function f_ciic(T)
249249
v = volume(model,p,T,phase = :l,vol0 = _v0)
250-
return f_ciic(SVector(log(v),T))[1]
250+
return f_ciic_vec(SVector(log(v),T))[1]
251251
end
252252
prob = Roots.ZeroProblem(f_ciic,_T0)
253253
_T00 = Roots.solve(prob,Roots.Order2(),atol = 1e-1)
254254
_v00 = volume(model,p,_T00,phase = :l, vol0 = _v0)
255255

256-
res = Solvers.nlsolve2(f_ciic,SVector(log(_v00),_T00),Solvers.Newton2Var())
256+
res = Solvers.nlsolve2(f_ciic_vec,SVector(log(_v00),_T00),Solvers.Newton2Var())
257257
v_ciic,T_ciic = exp(res[1]),res[2]
258258
return T_ciic,v_ciic
259259
end

0 commit comments

Comments
 (0)