You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to do a flash point calculation and I will have to take derivatives of this. The problem is that the flash point calculation requires solving a nonlinear equation iteratively. This results in ForwardDiff being 10x as slow as FiniteDiff. I know performance can vary, but I thought AD methods were supposed to outperform non-AD methods.
using Roots
using ForwardDiff
using FiniteDiff
function evaporated_frac(n, K)
RetType = promote_type( eltype(n), eltype(K) )
(kMin, kMax) = extrema(K)
if kMin > 1 #Everything is a gas
return RetType(1.0)
elseif kMax < 1 #Everything is a liquid
return RetType(0.0)
end
#Acquire mole fractions
z = n./sum(n)
dK = K .- 1.0
#Set up Rachford-Rice equation
function err(β)
return sum( (z.*dK) ./ (1 .+ β.*dK) )
end
#Set up initial guess and limits for Brent's method
ϵ = 1e-6
βLim = sort( 1 ./ (1 .- [kMax, kMin]) )
β = find_zero(err, βLim+[ϵ,-ϵ], Roots.Brent())
return clamp(β, 0.0, 1.0)
end
n = 50 .* rand(10)
K = 2 .* rand(10)
@time β = evaporated_frac(n, K)
@time g1 = FiniteDiff.finite_difference_gradient(x->evaporated_frac(n,x), K)
@time g2 = ForwardDiff.gradient(x->evaporated_frac(n,x), K)
Generally, it is. For timing code in Julia, it's best to use the BenchmarkTools.jl package and @btime rather than using the @time macro. The former will characterize the run-time of the function once it's JIT compiled whereas the later will time a number of other processes as well (in this case primarily the garbage collector).
I'm trying to do a flash point calculation and I will have to take derivatives of this. The problem is that the flash point calculation requires solving a nonlinear equation iteratively. This results in ForwardDiff being 10x as slow as FiniteDiff. I know performance can vary, but I thought AD methods were supposed to outperform non-AD methods.
0.000019 seconds (26 allocations: 2.953 KiB)
0.031502 seconds (62.05 k allocations: 3.497 MiB)
0.941194 seconds (5.06 M allocations: 248.243 MiB, 5.24% gc time)
The text was updated successfully, but these errors were encountered: