Skip to content

Commit 5a78065

Browse files
authored
Merge pull request #95 from ReactionMechanismGenerator/vjpfix
Fix issue with adjoint sensitivities for domains where rate coefficients vary
2 parents 94b834a + 58f1b41 commit 5a78065

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/Simulation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ By default uses the InterpolatingAdjoint algorithm with vector Jacobian products
138138
this assumes no changes in code branching during simulation, if that were to become no longer true, the Tracker
139139
based alternative algorithm is slower, but avoids this concern.
140140
"""
141-
function getadjointsensitivities(bsol::Q,target::String,solver::W;sensalg::W2=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(true)),abstol::Float64=1e-6,reltol::Float64=1e-3,kwargs...) where {Q,W,W2}
141+
function getadjointsensitivities(bsol::Q,target::String,solver::W;sensalg::W2=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)),abstol::Float64=1e-6,reltol::Float64=1e-3,kwargs...) where {Q,W,W2}
142142
@assert target in bsol.names || target in ["T","V","P"]
143143
if target in ["T","V","P"]
144144
ind = getthermovariableindex(bsol.domain,target)

src/TestReactors.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ y = sol(t);
116116
ja=jacobiany(y,p,t,domain,[],nothing);
117117
j = jacobianyforwarddiff(y,p,t,domain,[],nothing);
118118
@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false)
119+
120+
#sensitivities
121+
react = Reactor(domain,y0,(0.0,0.02),p=p) #Create the reactor object
122+
sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor
123+
sim = Simulation(sol,domain)
124+
dps = getadjointsensitivities(sim,"H2",CVODE_BDF();sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)),abstol=1e-16,reltol=1e-6)
125+
react2 = Reactor(domain,y0,(0.0,0.02);p=p,forwardsensitivities=true)
126+
sol2 = solve(react2.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); #solve the ode associated with the reactor
127+
sim2 = Simulation(sol2,domain)
128+
129+
x,dp = extract_local_sensitivities(sol2,0.02);
130+
ind = findfirst(isequal("H2"),sim2.names)
131+
dpvs = [v[ind] for v in dp]
132+
dpvs[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end]
133+
dpvs ./= sol2(0.02)[ind]
134+
rerr = (dpvs .- dps')./dpvs
135+
rerr = [isinf(x) ? 0.0 : x for x in rerr]
136+
@test all((abs.(rerr) .> 2e-1).==false)
119137
end;
120138

121139
#Constant P adiabatic Ideal Gas

0 commit comments

Comments
 (0)