Skip to content

Commit 2b8decd

Browse files
committed
add vectorized kf and Kc calculation functions to PhaseState.jl
1 parent be4d4b1 commit 2b8decd

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

src/PhaseState.jl

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ export makespcsvector
5353
end
5454
export getkf
5555

56+
@inline function getkfs(ph::U,T::W1,P::W2,C::W3,ns::Q,V::W4) where {U<:AbstractPhase,W1,W2,W3,W4<:Real,Q<:AbstractArray}
57+
kfs = zeros(length(ph.reactions))
58+
i = 1
59+
oldind = 1
60+
ind = 0
61+
while i <= length(ph.veckineticsinds) #vectorized kinetics
62+
@inbounds ind = ph.veckineticsinds[i]
63+
@inbounds kfs[oldind:ind] = ph.veckinetics[i](;T=T,P=P,C=C)
64+
oldind = ind+1
65+
i += 1
66+
end
67+
@simd for i in ind+1:length(ph.reactions)
68+
@inbounds kfs[i] = getkf(ph.reactions[i],ph,T,P,C,ns,V)
69+
end
70+
return kfs
71+
end
72+
export getkfs
73+
5674
"""
5775
Calculates the diffusion limited rate coefficient
5876
for 1 spc returns Inf
@@ -101,6 +119,11 @@ export getDiffusiveRate
101119
end
102120
export getKc
103121

122+
@inline function getKcs(ph::U,T::Z,Gs::Array{Q,1}) where {U<:AbstractPhase,Q,Z<:Real}
123+
return @fastmath @inbounds exp.(ph.stoichmatrix*(Gs./(R*T)) .+ ph.Nrp.*log(1.0e5/(R*T)));
124+
end
125+
export getKcs
126+
104127
"""
105128
Calculates the forward and reverse rate coefficients for a given reaction, phase and state
106129
Maintains diffusion limitations if the phase has diffusionlimited=true
@@ -140,11 +163,16 @@ end
140163
export getkfkrev
141164

142165
@inline function getkfkrevs(;phase::U,T::W1,P::W2,C::W3,N::W4,ns::Q1,Gs::Q2,diffs::Q3,V::W5) where {U<:AbstractPhase,W5<:Real,W1<:Real,W2<:Real,W3<:Real,W4<:Real, Q1<:AbstractArray,Q2<:AbstractArray,Q3<:AbstractArray}
143-
len = length(phase.reactions)
144-
kf = zeros(typeof(N),len)
145-
krev = zeros(typeof(N),len)
146-
@simd for i = 1:len
147-
@fastmath @inbounds kf[i],krev[i] = getkfkrev(phase.reactions[i],phase,T,P,C,N,ns,Gs,diffs,V)
166+
if !phase.diffusionlimited
167+
kf = getkfs(phase,T,P,C,ns,V)
168+
krev = @fastmath kf./getKcs(phase,T,Gs)
169+
else
170+
len = length(phase.reactions)
171+
kf = zeros(typeof(N),len)
172+
krev = zeros(typeof(N),len)
173+
@simd for i = 1:len
174+
@fastmath @inbounds kf[i],krev[i] = getkfkrev(phase.reactions[i],phase,T,P,C,N,ns,Gs,diffs,V)
175+
end
148176
end
149177
return kf,krev
150178
end

0 commit comments

Comments
 (0)