Skip to content

Commit 295df7e

Browse files
authored
Merge pull request #52 from ReactionMechanismGenerator/catphase
Catalyst Surfaces
2 parents 24c738c + e1a445d commit 295df7e

3 files changed

Lines changed: 160 additions & 4 deletions

File tree

src/Domain.jl

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,76 @@ function ParametrizedTConstantVDomain(;phase::IdealDiluteSolution,initialconds::
597597
end
598598
export ParametrizedTConstantVDomain
599599

600+
@with_kw struct ConstantTADomain{N<:AbstractPhase,S<:Integer,W<:Real, W2<:Real, I<:Integer, Q<:AbstractArray} <: AbstractConstantKDomain
601+
phase::N
602+
indexes::Q #assumed to be in ascending order
603+
constantspeciesinds::Array{S,1}
604+
T::W
605+
A::W
606+
kfs::Array{W,1}
607+
krevs::Array{W,1}
608+
efficiencyinds::Array{I,1}
609+
Gs::Array{W,1}
610+
rxnarray::Array{UInt16,2}
611+
mu::W = 0.0
612+
diffusivity::Array{W,1} = Array{Float64,1}()
613+
jacobian::Array{W,2} = Array{Float64,2}(undef,(0,0))
614+
sensitivity::Bool = false
615+
jacuptodate::MArray{Tuple{1},Bool,1,1}=MVector(false)
616+
t::MArray{Tuple{1},W2,1,1}=MVector(0.0)
617+
p::Array{W,1}
618+
end
619+
function ConstantTADomain(;phase::E2,initialconds::Dict{X,X2},constantspecies::Array{X3,1}=Array{String,1}(),
620+
sparse::Bool=false,sensitivity::Bool=false,stationary::Bool=false) where {E<:Real,E2<:AbstractPhase,W<:Real,X,X2,X3}
621+
#set conditions and initialconditions
622+
T = 0.0
623+
A = 0.0
624+
y0 = zeros(length(phase.species))
625+
spnames = [x.name for x in phase.species]
626+
for (key,val) in initialconds
627+
if key == "T"
628+
T = val
629+
elseif key == "A"
630+
A = val
631+
else
632+
ind = findfirst(isequal(key),spnames)
633+
@assert typeof(ind)<: Integer "$key not found in species list: $spnames"
634+
y0[ind] = val
635+
end
636+
end
637+
638+
@assert A != 0.0
639+
@assert T != 0.0
640+
ns = y0
641+
N = sum(ns)
642+
643+
if length(constantspecies) > 0
644+
spcnames = getfield.(phase.species,:name)
645+
constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies]
646+
else
647+
constspcinds = Array{Int64,1}()
648+
end
649+
efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0]
650+
Gs = calcgibbs(phase,T)
651+
if :solvent in fieldnames(typeof(phase)) && typeof(phase.solvent) != EmptySolvent
652+
mu = phase.solvent.mu(T)
653+
else
654+
mu = 0.0
655+
end
656+
C = phase.sitedensity
657+
kfs,krevs = getkfkrevs(phase=phase,T=T,P=0.0,C=C,N=N,ns=ns,Gs=Gs,diffs=[],V=A)
658+
p = vcat(Gs,kfs)
659+
if sparse
660+
jacobian=spzeros(typeof(T),length(phase.species),length(phase.species))
661+
else
662+
jacobian=zeros(typeof(T),length(phase.species),length(phase.species))
663+
end
664+
rxnarray = getreactionindices(phase)
665+
return ConstantTADomain(phase,MVector(phase.species[1].index,phase.species[end].index),constspcinds,
666+
T,A,kfs,krevs,efficiencyinds,Gs,rxnarray,mu,jacobian,sensitivity,MVector(false),MVector(0.0),stationary), y0, p
667+
end
668+
export ConstantTADomain
669+
600670
@inline function calcthermo(d::ConstantTPDomain{W,Y},y::J,t::Q,p::W3=DiffEqBase.NullParameters()) where {W3<:DiffEqBase.NullParameters,W<:IdealGas,Y<:Integer,J<:Array{Float64,1},Q} #no parameter input
601671
if t != d.t[1]
602672
d.t[1] = t
@@ -1294,6 +1364,73 @@ end
12941364
end
12951365
export calcthermo
12961366

1367+
@inline function calcthermo(d::ConstantTADomain{W,Y},y::J,t::Q,p::Q2=DiffEqBase.NullParameters()) where {Q2<:DiffEqBase.NullParameters,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q<:Real}
1368+
if t != d.t[1]
1369+
d.t[1] = t
1370+
d.jacuptodate[1] = false
1371+
end
1372+
ns = y[d.indexes[1]:d.indexes[2]]
1373+
N = sum(ns)
1374+
cs = ns./d.A
1375+
C = N/d.A
1376+
P = 0.0
1377+
return ns,cs,d.T,P,d.A,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0
1378+
end
1379+
1380+
@inline function calcthermo(d::ConstantTADomain{W,Y},y::J,t::Q,p::Q2=DiffEqBase.NullParameters()) where {Q2<:Array{Float64,1},W<:IdealSurface,Y<:Integer,J<:Array{Float64,1},Q<:Real}
1381+
if t != d.t[1]
1382+
d.t[1] = t
1383+
d.jacuptodate[1] = false
1384+
end
1385+
ns = y[d.indexes[1]:d.indexes[2]]
1386+
N = sum(ns)
1387+
cs = ns./d.A
1388+
C = N/d.A
1389+
P = 0.0
1390+
@views nothermochg = d.Gs == p[1:length(d.phase.species)]
1391+
if nothermochg
1392+
return ns,cs,d.T,P,d.A,C,N,d.mu,p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)],d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0
1393+
else
1394+
d.kfs = p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)]
1395+
d.Gs = p[1:length(d.phase.species)]
1396+
d.krevs = getkfkrevs(;phase=d.phase,T=d.T,P=P,C=C,N=N,ns=ns,Gs=d.Gs,diffs=d.diffusivity,V=d.V,kfs=d.kfs)[2]
1397+
return ns,cs,d.T,P,d.A,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0
1398+
end
1399+
end
1400+
1401+
@inline function calcthermo(d::ConstantTADomain{W,Y},y::Array{W2,1},t::Q,p::Q2=DiffEqBase.NullParameters()) where {W2<:ForwardDiff.Dual,Q2,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q<:Real} #autodiff y
1402+
if t != d.t[1]
1403+
d.t[1] = t
1404+
d.jacuptodate[1] = false
1405+
end
1406+
ns = y[d.indexes[1]:d.indexes[2]]
1407+
N = sum(ns)
1408+
cs = ns./d.A
1409+
C = N/d.A
1410+
P = 0.0
1411+
Gs = p[1:length(d.phase.species)]
1412+
kfs = convert(typeof(y),p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)])
1413+
krevs = convert(typeof(y),getkfkrevs(;phase=d.phase,T=d.T,P=P,C=C,N=N,ns=ns,Gs=Gs,diffs=d.diffusivity,V=d.V,kfs=kfs)[2])
1414+
return ns,cs,d.T,P,d.V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0
1415+
end
1416+
1417+
@inline function calcthermo(d::ConstantTADomain{W,Y},y::J,t::Q,p::Q2=DiffEqBase.NullParameters()) where {Q2,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q<:Real} #autodiff p
1418+
if t != d.t[1]
1419+
d.t[1] = t
1420+
d.jacuptodate[1] = false
1421+
end
1422+
ns = y[d.indexes[1]:d.indexes[2]]
1423+
N = sum(ns)
1424+
cs = ns./d.A
1425+
C = N/d.A
1426+
P = 0.0
1427+
Gs = p[1:length(d.phase.species)]
1428+
kfs = p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)]
1429+
krevs = getkfkrevs(;phase=d.phase,T=d.T,P=P,C=C,N=N,ns=ns,Gs=Gs,diffs=d.diffusivity,V=d.V,kfs=kfs)[2]
1430+
return ns,cs,d.T,P,d.A,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0
1431+
end
1432+
export calcthermo
1433+
12971434
@inline function calcdomainderivatives!(d::Q,dydt::Z7,interfaces::Z12;t::Z10,T::Z4,P::Z9,Us::Array{Z,1},Hs::Array{Z11,1},V::Z2,C::Z3,ns::Z5,N::Z6,Cvave::Z8) where {Q<:AbstractDomain,Z12,Z11,Z10,Z9,Z8<:Real,Z7,W<:IdealGas,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5}
12981435
for ind in d.constantspeciesinds #make dydt zero for constant species
12991436
@inbounds dydt[ind] = 0.0

src/Phase.jl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,32 @@ function IdealDiluteSolution(species,reactions,solvent; name="",diffusionlimited
7272
end
7373
export IdealDiluteSolution
7474

75-
@with_kw struct HomogeneousCatalyst{Q<:AbstractReaction} <: AbstractPhase
75+
@with_kw struct IdealSurface{W<:Tuple,W2} <: IdealPhase
7676
name::String = ""
7777
species::Array{Species,1}
78-
reactions::Array{Q,1}
78+
reactions::Array{ElementaryReaction,1}
79+
sitedensity::Float64
80+
stoichmatrix::W2
81+
Nrp::Array{Float64,1}
82+
veckinetics::W
83+
veckineticsinds::Array{Int64,1}
84+
vecthermo::NASAvec
85+
otherreactions::Array{ElementaryReaction,1}
7986
spcdict::Dict{String,Int64}
87+
diffusionlimited::Bool = false
88+
end
89+
function IdealSurface(species,reactions,sitedensity; name="",diffusionlimited=false)
90+
@assert diffusionlimited==false "diffusionlimited=true not supported for IdealSurface"
91+
vectuple,vecinds,otherrxns,otherrxninds,posinds = getveckinetics(reactions)
92+
rxns = vcat(reactions[vecinds],reactions[otherrxninds])
93+
rxns = [ElementaryReaction(index=i,reactants=rxn.reactants,reactantinds=rxn.reactantinds,products=rxn.products,
94+
productinds=rxn.productinds,kinetics=rxn.kinetics,radicalchange=rxn.radicalchange,pairs=rxn.pairs) for (i,rxn) in enumerate(rxns)]
95+
therm = getvecthermo(species)
96+
M,Nrp = getstoichmatrix(species,rxns)
97+
return IdealSurface(species=species,reactions=rxns,sitedensity=sitedensity,name=name,
98+
spcdict=Dict([sp.name=>sp.index for sp in species]),stoichmatrix=M,Nrp=Nrp,veckinetics=vectuple,veckineticsinds=posinds,vecthermo=therm,otherreactions=otherrxns,diffusionlimited=diffusionlimited)
8099
end
81-
export HomogeneousCatalyst
100+
export IdealSurface
82101

83102
"""
84103
construct the stochiometric matrix for the reactions and the reaction molecule # change

src/PhaseState.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export calcgibbs
1515
end
1616

1717

18-
@inline function calcenthalpyinternalgibbs(ph::IdealGas,T::W,P::Z,V::Q) where {W,Z,Q<:Real}
18+
@inline function calcenthalpyinternalgibbs(ph::Union{IdealGas,IdealSurface},T::W,P::Z,V::Q) where {W,Z,Q<:Real}
1919
Hs = getEnthalpy.(getfield.(ph.species,:thermo),T)
2020
Us = Hs .- R*T
2121
Gs = Hs .- T.*getEntropy.(getfield.(ph.species,:thermo),T)

0 commit comments

Comments
 (0)