Skip to content

Commit 8bde4c8

Browse files
committed
Dispatch getkfkrevs for autodiff p
getkfkrevs fix
1 parent b784b9a commit 8bde4c8

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

src/PhaseState.jl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Parameters
22
using SpecialFunctions
33
using LinearAlgebra
4+
using Tracker
45

56
@inline function calcgibbs(ph::U,T::W) where {U<:IdealPhase,W<:Real}
67
return getGibbs.(getfield.(ph.species,:thermo),T)
@@ -168,7 +169,7 @@ Maintains diffusion limitations if the phase has diffusionlimited=true
168169
end
169170
export getkfkrev
170171

171-
@inline function getkfkrevs(phase::U,T::W1,P::W2,C::W3,N::W4,ns::Q1,Gs::Q2,diffs::Q3,V::W5;kfs::W6=nothing) where {U<:AbstractPhase,W6,W5<:Real,W1<:Real,W2<:Real,W3<:Real,W4<:Real, Q1<:AbstractArray,Q2<:AbstractArray,Q3<:AbstractArray}
172+
@inline function getkfkrevs(phase::U,T::W1,P::W2,C::W3,N::W4,ns::Q1,Gs::Q2,diffs::Q3,V::W5;kfs::W6=nothing) where {U<:AbstractPhase,W6,W5<:Real,W1<:Real,W2<:Real,W3<:Real,W4<:Real, Q1<:AbstractArray,Q2<:Array{Float64,1},Q3<:AbstractArray}
172173
if !phase.diffusionlimited && kfs === nothing
173174
kfs = getkfs(phase,T,P,C,ns,V)
174175
krev = @fastmath kfs./getKcs(phase,T,Gs)
@@ -190,4 +191,53 @@ export getkfkrev
190191
end
191192
return kfs,krev
192193
end
194+
195+
@inline function getkfkrevs(phase::U,T::W1,P::W2,C::W3,N::W4,ns::Q1,Gs::Q2,diffs::Q3,V::W5;kfs::W6=nothing) where {U<:AbstractPhase,W6,W5<:Real,W1<:Real,W2<:Real,W3<:Real,W4<:Real, Q1<:AbstractArray,Q2<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},Q3<:AbstractArray} #autodiff p
196+
if !phase.diffusionlimited && kfs === nothing
197+
kfs = getkfs(phase,T,P,C,ns,V)
198+
krev = @fastmath kfs./getKcs(phase,T,Gs)
199+
elseif !phase.diffusionlimited && !(kfs === nothing)
200+
krev = @fastmath kfs./getKcs(phase,T,Gs)
201+
elseif phase.diffusionlimited && !(kfs === nothing)
202+
len = length(phase.reactions)
203+
krev = similar(kfs)
204+
kfsdiff = similar(kfs)
205+
@simd for i = 1:len
206+
@fastmath @inbounds kfsdiff[i],krev[i] = getkfkrev(phase.reactions[i],phase,T,P,C,N,ns,Gs,diffs,V;kf=kfs[i])
207+
end
208+
return kfsdiff, krev
209+
else
210+
len = length(phase.reactions)
211+
kfs = zeros(typeof(Gs[1]),len)
212+
krev = zeros(typeof(Gs[1]),len)
213+
@simd for i = 1:len
214+
@fastmath @inbounds kfs[i],krev[i] = getkfkrev(phase.reactions[i],phase,T,P,C,N,ns,Gs,diffs,V)
215+
end
216+
end
217+
return kfs,krev
218+
end
219+
220+
@inline function getkfkrevs(phase::U,T::W1,P::W2,C::W3,N::W4,ns::Q1,Gs::Array{Q2,1},diffs::Q3,V::W5;kfs::W6=nothing) where {U<:AbstractPhase,W6,W5<:Real,W1<:Real,W2<:Real,W3<:Real,W4<:Real, Q1<:AbstractArray,Q2<:ForwardDiff.Dual,Q3<:AbstractArray} #autodiff p
221+
if !phase.diffusionlimited && kfs === nothing
222+
kfs = getkfs(phase,T,P,C,ns,V)
223+
krev = @fastmath kfs./getKcs(phase,T,Gs)
224+
elseif !phase.diffusionlimited && !(kfs === nothing)
225+
krev = @fastmath kfs./getKcs(phase,T,Gs)
226+
elseif phase.diffusionlimited && !(kfs === nothing)
227+
len = length(phase.reactions)
228+
krev = similar(kfs)
229+
@simd for i = 1:len
230+
@fastmath @inbounds kfs[i],krev[i] = getkfkrev(phase.reactions[i],phase,T,P,C,N,ns,Gs,diffs,V;kf=kfs[i])
231+
end
232+
else
233+
len = length(phase.reactions)
234+
kfs = zeros(typeof(Gs[1]),len)
235+
krev = zeros(typeof(Gs[1]),len)
236+
@simd for i = 1:len
237+
@fastmath @inbounds kfs[i],krev[i] = getkfkrev(phase.reactions[i],phase,T,P,C,N,ns,Gs,diffs,V)
238+
end
239+
end
240+
return kfs,krev
241+
end
242+
193243
export getkfkrevs

0 commit comments

Comments
 (0)