Skip to content

Commit c1206b9

Browse files
committed
add Reactor constructor for multidomain simulations
reorders variables to put the thermodynamic variables at the end and the species variables at the beginning cases where parameters are shared between domains are identified and combined in the parameter vector
1 parent d25a0e9 commit c1206b9

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

src/Reactor.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,83 @@ function Reactor(domain::T,y0::Array{W,1},tspan::Tuple,interfaces::Z=[];p::X=Dif
3131
end
3232
return Reactor(domain,ode,recsolver,forwardsensitivities)
3333
end
34+
function Reactor(domains::T,y0s::W,tspan::W2,interfaces::Z=[],ps::X=DiffEqBase.NullParameters();forwardsensitivities=false) where {T<:Tuple,W,Z<:AbstractArray,X,W2}
35+
#adjust indexing
36+
y0 = zeros(sum(length(y) for y in y0s))
37+
Nvars = 0
38+
for domain in domains
39+
Nvars += domain.indexes[end]
40+
end
41+
n = Nvars
42+
k = 1
43+
for (j,domain) in enumerate(domains)
44+
Nspcs = length(domain.phase.species)
45+
Ntherm = length(domain.indexes) - 2
46+
for i = 1:6, j = 1:size(domain.rxnarray)[2]
47+
if domain.rxnarray[i,j] != 0
48+
domain.rxnarray[i,j] += k-1
49+
end
50+
end
51+
domain.indexes[1] = k
52+
k += Nspcs
53+
domain.indexes[2] = k-1
54+
y0[domain.indexes[1]:domain.indexes[2]] = y0s[j][1:Nspcs]
55+
for m = 3:2+Ntherm
56+
domain.indexes[m] = n
57+
y0[n] = y0s[j][Nspcs+m-2]
58+
n -= 1
59+
end
60+
end
61+
62+
p = Array{Float64,1}()
63+
phases = []
64+
phaseinds = Array{Tuple,1}()
65+
for (i,domain) in enumerate(domains)
66+
if domain.phase in phases
67+
ind = findfirst(phases.==domain.phase)
68+
domain.parameterindexes[1] = phaseinds[ind][1]
69+
domain.parameterindexes[2] = phaseinds[ind][2]
70+
ds = [domains[ind] for ind in findall(x->x.phase==domain.phase,domains)]
71+
if any(.!isa.(ds,AbstractConstantKDomain)) && any(isa.(ds,AbstractConstantKDomain)) #handle different p formats for the same phase
72+
for d in ds
73+
if !isa(d,AbstractConstantKDomain)
74+
p[domain.parameterindexes[1]:domain.parameterindexes[2]] .= d.p
75+
break
76+
end
77+
end
78+
for d in ds
79+
if isa(d,AbstractConstantKDomain)
80+
if forwardsensitivities == true #error if running into sensitivity bug
81+
error("forward sensitivities is not supported for domain combinations that share rate coefficients, but treat them differently ex: ConstantTPDomain and ConstantVDomain")
82+
end
83+
d.alternativepformat = true
84+
end
85+
end
86+
end
87+
else
88+
domain.parameterindexes[1] = length(p)+1
89+
domain.parameterindexes[2] = length(p)+length(ps[i])
90+
push!(phaseinds,(length(p)+1,length(p)+length(ps[i])))
91+
push!(phases,domain.phase)
92+
p = vcat(p,ps[i])
93+
end
94+
end
95+
96+
dydt(dy::X,y::T,p::V,t::Q) where {X,T,Q<:Real,V} = dydtreactor!(dy,y,t,domains,interfaces,p=p)
97+
jacy!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q<:Real,V} = jacobiany!(J,y,p,t,domains,interfaces,nothing)
98+
jacp!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q<:Real,V} = jacobianp!(J,y,p,t,domains,interfaces,nothing)
99+
100+
odefcn = ODEFunction(dydt;paramjac=jacp!)
101+
102+
if forwardsensitivities
103+
ode = ODEForwardSensitivityProblem(odefcn,y0,tspan,p)
104+
recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES)
105+
else
106+
ode = ODEProblem(odefcn,y0,tspan,p)
107+
recsolver = Sundials.CVODE_BDF()
108+
end
109+
return Reactor(domains,ode,recsolver,forwardsensitivities),y0,p
110+
end
34111
export Reactor
35112

36113
@inline function getrate(rxn::T,cs::Array{W,1},kfs::Array{Q,1},krevs::Array{Q,1}) where {T<:AbstractReaction,Q,W<:Real}

0 commit comments

Comments
 (0)