Skip to content

Commit 7a32bf2

Browse files
authored
Merge pull request #168 from ReactionMechanismGenerator/electrocat-rmg-prep
Multi-domain Species Selection and Electrocat RMG prep
2 parents cc9f1e4 + 6c28816 commit 7a32bf2

4 files changed

Lines changed: 62 additions & 52 deletions

File tree

.github/workflows/CI.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v2
1414
- uses: julia-actions/setup-julia@v1
1515
with:
16-
version: 1.4
16+
version: 1.5
1717
- uses: actions/cache@v1
1818
env:
1919
cache-name: cache-artifacts
@@ -30,8 +30,8 @@ jobs:
3030
julia -e 'ENV["PYTHON"]="/home/runner/.julia/conda/3/bin/python"; using Pkg; Pkg.add("PyCall"); Pkg.build("PyCall")'
3131
- name: Build
3232
run: |
33-
julia -e 'using Pkg; Pkg.develop(PackageSpec(path="../ReactionMechanismSimulator.jl")); Pkg.add(PackageSpec(;name="ReverseDiff",version="1.4"));'
34-
- name: Run tests
33+
julia -e 'using Pkg; Pkg.develop(PackageSpec(path="../ReactionMechanismSimulator.jl"));'
34+
- name: Run tests
3535
run: |
3636
julia -e 'using Pkg; Pkg.test("ReactionMechanismSimulator";coverage=true)'
3737
- uses: julia-actions/julia-processcoverage@v1

.github/workflows/documentation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- uses: actions/checkout@v2
1515
- uses: julia-actions/setup-julia@latest
1616
with:
17-
version: 1.4
17+
version: 1.5
1818
- name: Install dependencies
1919
run: |
2020
wget https://repo.anaconda.com/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh;

src/EdgeAnalysis.jl

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using Logging
66
using Sundials
77
using SparseArrays
88
using DiffEqBase: build_solution
9+
using Base.Iterators: flatten
910

1011
abstract type AbstractTerminationCriterion end
1112

@@ -129,7 +130,7 @@ Calculate key flux and concentration related quantities for edge analysis
129130
vcpdivR[1] = cpdivR
130131
vphi = Array{Any,1}(undef,length(domains))
131132
vphi[1] = phi
132-
rtsall,frtsall,rrtsall = addreactionratecontributionsforwardreverse!(dydt,domain.rxnarray,cstot,kfs,krevs,V)
133+
rts,frts,rrts = addreactionratecontributionsforwardreverse!(dydt,domain.rxnarray,cstot,kfs,krevs,V)
133134
rtsall = [rts]
134135
frtsall = [frts]
135136
rrtsall = [rrts]
@@ -162,20 +163,21 @@ Calculate key flux and concentration related quantities for edge analysis
162163
for (i,domain) in enumerate(domains)
163164
calcdomainderivatives!(domain,dydt,interfaces;t=t,T=vT[i],P=vP[i],Us=vUs[i],Hs=vHs[i],V=vV[i],C=vC[i],ns=vns[i],N=vN[i],Cvave=vCvave[i])
164165
end
165-
return dydt,rtsall,frtsall,rrtsall,cstot
166+
return dydt,collect(flatten(rtsall)),collect(flatten(frtsall)),collect(flatten(rrtsall)),cstot
166167
end
167168
export calcfluxes
168169

169170
"""
170171
Precalculate important indices and maps for use in edge analysis
171172
"""
172-
function getkeyselectioninds(coreeedgedomains,coreedgeinters,domains,inters)
173-
corespcsinds = flatten([coreedgedomains[i].indexes[1]:coreedgedomains[i].indexes[1]+domains[i].indexes[2]-domains[i].indexes[1] for i = 1:length(domains)])
174-
edgespcsinds = flatten([coreedgedomains[i].indexes[1]+domains[i].indexes[2]-domains[i].indexes[1]:coreedgedomains[i].indexes[2] for i = 1:length(domains)])
173+
function getkeyselectioninds(coreedgedomains,coreedgeinters,domains,inters)
174+
corespcsinds = collect(flatten([coreedgedomains[i].indexes[1]:coreedgedomains[i].indexes[1]+domains[i].indexes[2]-domains[i].indexes[1] for i = 1:length(domains)]))
175+
edgespcsinds = collect(flatten([coreedgedomains[i].indexes[1]+domains[i].indexes[2]-domains[i].indexes[1]+1:coreedgedomains[i].indexes[2] for i = 1:length(domains)]))
175176
corerxninds = Array{Int64,1}()
176177
edgerxninds = Array{Int64,1}()
177-
reactantindices = zeros(Int64,(3,length(corerxninds)))
178-
productindices = zeros(Int64,(3,length(corerxninds)))
178+
Nrxns = sum(length(d.phase.reactions) for d in coreedgedomains)
179+
reactantindices = zeros(Int64,(3,Nrxns))
180+
productindices = zeros(Int64,(3,Nrxns))
179181
coretoedgespcmap = Dict{Int64,Int64}()
180182
coretoedgerxnmap = Dict{Int64,Int64}()
181183
spcindexcore = 0
@@ -194,18 +196,18 @@ function getkeyselectioninds(coreeedgedomains,coreedgeinters,domains,inters)
194196
for (j,rxn) in enumerate(coreedgedomains[i].phase.reactions)
195197
coreind = findfirst(isequal(rxn),domains[i].phase.reactions)
196198
if coreind === nothing
197-
push!(edgerxninds,j+indexedge)
198-
else
199-
coretoedgerxnmap[coreind+indexcore] = j+indexedge
200-
push!(corerxninds,j+indexedge)
199+
push!(edgerxninds,j+rxnindexedge)
200+
else
201+
coretoedgerxnmap[coreind+rxnindexcore] = j+rxnindexedge
202+
push!(corerxninds,j+rxnindexedge)
201203
end
202204
end
203205
spcindexcore += length(domains[i].phase.species)
204206
spcindexedge += length(coreedgedomains[i].phase.species)
205207
rxnindexcore += length(domains[i].phase.reactions)
206208
rxnindexedge += length(coreedgedomains[i].phase.reactions)
207-
208-
indend = length(domains[i].reactions)
209+
210+
indend = length(domains[i].phase.reactions)
209211
reactantindices[:,ind:ind+indend-1] = domains[i].rxnarray[1:3,:]
210212
productindices[:,ind:ind+indend-1] = domains[i].rxnarray[4:6,:]
211213
ind += indend
@@ -223,10 +225,7 @@ function getkeyselectioninds(coreeedgedomains,coreedgeinters,domains,inters)
223225
ind += indend
224226
end
225227
end
226-
corerxninds = flatten(corerxnrangearray)
227-
edgerxninds = flatten(edgerxnrangearray)
228-
229-
return corespcsinds,corerxninds,edgespcsinds,edgerxninds,reactantindices,productindices,coretoedgespcmap,coretoedgerxnmap
228+
return corespcsinds,collect(flatten(corerxninds)),edgespcsinds,collect(flatten(edgerxninds)),reactantindices,productindices,coretoedgespcmap,coretoedgerxnmap
230229
end
231230

232231
"""
@@ -269,55 +268,57 @@ function processfluxes(sim::SystemSimulation,
269268
#process core species consumption and production rates
270269
index = 1
271270
for d in getfield.(sim.sims,:domain)
272-
for i = index:index+size(d.rxnarray)[2]
271+
for i = 1:size(d.rxnarray)[2]
273272
if any(d.rxnarray[:,i].>length(corespeciesconcentrations))
274273
continue
275274
end
276275
for j = 1:3
277276
if d.rxnarray[j,i] != 0
278-
corespeciesconsumptionrates[d.rxnarray[j,i]] += frts[i]
279-
corespeciesproductionrates[d.rxnarray[j,i]] += rrts[i]
277+
corespeciesconsumptionrates[d.rxnarray[j,i]] += frts[i+index]
278+
corespeciesproductionrates[d.rxnarray[j,i]] += rrts[i+index]
280279
else
281280
break
282281
end
283282
end
284283
for j = 4:6
285284
if d.rxnarray[j,i] != 0
286-
corespeciesproductionrates[d.rxnarray[j,i]] += frts[i]
287-
corespeciesconsumptionrates[d.rxnarray[j,i]] += rrts[i]
285+
corespeciesproductionrates[d.rxnarray[j,i]] += frts[i+index]
286+
corespeciesconsumptionrates[d.rxnarray[j,i]] += rrts[i+index]
288287
else
289288
break
290289
end
291290
end
292291
end
293292
index += size(d.rxnarray)[2]
294293
end
295-
for d in inters
296-
for i = index:index+size(d.rxnarray)[2]
297-
if any(d.rxnarray[:,i].>length(corespeciesconcentrations))
298-
continue
299-
end
300-
for j = 1:3
301-
if d.rxnarray[j,i] != 0
302-
corespeciesconsumptionrates[d.rxnarray[j,i]] += frts[i]
303-
corespeciesproductionrates[d.rxnarray[j,i]] += rrts[i]
304-
else
305-
break
294+
for d in sim.interfaces
295+
if hasproperty(d,:rxnarray)
296+
for i = 1:size(d.rxnarray)[2]
297+
if any(d.rxnarray[:,i].>length(corespeciesconcentrations))
298+
continue
306299
end
307-
end
308-
for j = 4:6
309-
if d.rxnarray[j,i] != 0
310-
corespeciesproductionrates[d.rxnarray[j,i]] += frts[i]
311-
corespeciesconsumptionrates[d.rxnarray[j,i]] += rrts[i]
312-
else
313-
break
300+
for j = 1:3
301+
if d.rxnarray[j,i] != 0
302+
corespeciesconsumptionrates[d.rxnarray[j,i]] += frts[i+index]
303+
corespeciesproductionrates[d.rxnarray[j,i]] += rrts[i+index]
304+
else
305+
break
306+
end
307+
end
308+
for j = 4:6
309+
if d.rxnarray[j,i] != 0
310+
corespeciesproductionrates[d.rxnarray[j,i]] += frts[i+index]
311+
corespeciesconsumptionrates[d.rxnarray[j,i]] += rrts[i+index]
312+
else
313+
break
314+
end
314315
end
315316
end
317+
index += size(d.rxnarray)[2]
316318
end
317-
index += size(d.rxnarray)[2]
318319
end
319-
320-
return dydt,rts,frts,rrts,cs,corespeciesratse,charrate,edgespeciesrates,edgereactionrates,corespeciesrateratios,edgespeciesrateratios,corereactionrates,corespeciesconcentrations,corespeciesproductionrates,corespeciesconsumptionrates
320+
321+
return dydt,rts,frts,rrts,cs,corespeciesrates,charrate,edgespeciesrates,edgereactionrates,corespeciesrateratios,edgespeciesrateratios,corereactionrates,corespeciesconcentrations,corespeciesproductionrates,corespeciesconsumptionrates
321322
end
322323

323324
"""
@@ -699,8 +700,8 @@ function selectobjects(react,coreedgedomains,coreedgeinters,domains,inters,
699700
inte = init(react.ode,solver,abstol=atol,reltol=rtol);
700701

701702
t = inte.t
702-
sim = getsim(inte,react,coreedgedomains,inters,corep,coretoedgespcmap)
703-
703+
sim = getsim(inte,react,coreedgedomains,coreedgeinters,corep,coretoedgespcmap)
704+
704705
y0 = sim.sol[end]
705706
spcsaddindices = Array{Int64,1}()
706707
firsttime = true
@@ -712,7 +713,7 @@ function selectobjects(react,coreedgedomains,coreedgeinters,domains,inters,
712713
end
713714
code = check_error(inte)
714715
t = inte.t
715-
sim = getsim(inte,react,coreedgedomains,inters,coreedgep,coretoedgespcmap)
716+
sim = getsim(inte,react,coreedgedomains,coreedgeinters,coreedgep,coretoedgespcmap)
716717
terminated,interrupt,conversion = identifyobjects!(sim,corespcsinds,corerxninds,edgespcsinds,
717718
edgerxninds,reactantindices,productindices,unimolecularthreshold,bimolecularthreshold,
718719
trimolecularthreshold,maxedgespeciesrateratios,tolmovetocore,tolinterruptsimulation,ignoreoverallfluxcriterion,filterreactions,

src/Interface.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ function ReactiveInternalInterfaceConstantTPhi(domain1,domain2,reactions,T,A,phi
9292
krevs = kfs./Kc
9393
M,Nrp1,Nrp2 = getstoichmatrix(domain1,domain2,reactions)
9494
reversibility = Array{Bool,1}(getfield.(reactions,:reversible))
95+
if isa(reactions,Vector{Any})
96+
reactions = convert(Vector{ElementaryReaction},reactions)
97+
end
98+
if isa(kfs,Vector{Any})
99+
kfs = convert(Vector{Float64},kfs)
100+
end
95101
return ReactiveInternalInterfaceConstantTPhi(domain1,domain2,reactions,
96102
rxnarray,M,Nrp1,Nrp2,kfs,krevs,T,A,[1,length(reactions)],
97103
[0,1],kfs[1:end],reversibility),kfs[1:end]
@@ -192,16 +198,19 @@ function upgradekinetics(rxns,domain1,domain2)
192198
elseif domain2surf
193199
surfdomain = domain2
194200
end
201+
newrxns = Array{ElementaryReaction,1}(undef,length(rxns))
195202
for (i,rxn) in enumerate(rxns)
196203
if isa(rxn.kinetics,StickingCoefficient)
197204
spc = [spc for spc in rxn.reactants if !(spc in surfdomain.phase.species)]
198205
@assert length(spc) == 1
199206
kin = stickingcoefficient2arrhenius(rxn.kinetics,surfdomain.phase.sitedensity,length(rxn.reactants)-1,spc[1].molecularweight)
200-
rxns[i] = ElementaryReaction(index=rxn.index,reactants=rxn.reactants,reactantinds=rxn.reactantinds,products=rxn.products,
207+
newrxns[i] = ElementaryReaction(index=rxn.index,reactants=rxn.reactants,reactantinds=rxn.reactantinds,products=rxn.products,
201208
productinds=rxn.productinds,kinetics=kin,radicalchange=rxn.radicalchange,reversible=rxn.reversible,pairs=rxn.pairs)
209+
else
210+
newrxns[i] = rxn
202211
end
203212
end
204-
return rxns
213+
return [rxn for rxn in newrxns]
205214
end
206215

207216
function stickingcoefficient2arrhenius(sc,sitedensity,N,mw;Tmin=300.0,Tmax=2000.0)

0 commit comments

Comments
 (0)