Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Adjoint Sensitivities #237

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using SciMLSensitivity
using ForwardDiff
using PreallocationTools
using LinearAlgebra

abstract type AbstractSimulation end
export AbstractSimulation
Expand Down Expand Up @@ -319,7 +320,7 @@
function rops(ssys::SystemSimulation, name, t)
domains = getfield.(ssys.sims, :domain)
ind = findfirst(isequal(name), ssys.names)
Nrxns = sum([length(sim.domain.phase.reactions) for sim in ssys.sims]) + sum(Vector{Int}([length(inter.reactions) for inter in ssys.interfaces if hasproperty(inter, :reactions)]))

Check warning on line 323 in src/Simulation.jl

View check run for this annotation

Codecov / codecov/patch

src/Simulation.jl#L323

Added line #L323 was not covered by tests
Nspcs = sum([length(getphasespecies(sim.domain.phase)) for sim in ssys.sims])
cstot = zeros(Nspcs)
vns = Array{Any,1}(undef, length(domains))
Expand Down Expand Up @@ -546,10 +547,10 @@
end
else
if target in ["T", "V", "P", "mass"] || !isempty(bsol.interfaces)
du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=g, dgdu_continuous=gdurevdiff,

Check warning on line 550 in src/Simulation.jl

View check run for this annotation

Codecov / codecov/patch

src/Simulation.jl#L550

Added line #L550 was not covered by tests
dgdp_continuous=dgdprevdiff, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...)
else
du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=sensg, dgdu_continuous=dsensgdurevdiff,

Check warning on line 553 in src/Simulation.jl

View check run for this annotation

Codecov / codecov/patch

src/Simulation.jl#L553

Added line #L553 was not covered by tests
dgdp_continuous=dsensgdprevdiff, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...)
end
end
Expand All @@ -561,7 +562,7 @@
dpadj ./= bsol.sol(bsol.sol.t[end])[ind]
end
end
return dpadj
return dpadj::LinearAlgebra.Adjoint{Float64, Vector{Float64}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that this is needed

end

function getadjointsensitivities(syssim::Q, bsol::W3, target::String, solver::W; sensalg::W2=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)),
Expand Down Expand Up @@ -605,7 +606,7 @@
dpadj ./= bsol.sol(bsol.sol.t[end])[ind]
end
end
return dpadj
return dpadj::LinearAlgebra.Adjoint{Float64, Vector{Float64}}
end
export getadjointsensitivities

Expand Down Expand Up @@ -782,15 +783,15 @@
CSV.write(save_name, df)
end

function save(syss::T, save_name::String) where {T<:SystemSimulation}
df = DataFrame(syss.sol)
for sim in syss.sims
rename!(df, names(df)[sim.domain.indexes[1]:sim.domain.indexes[2]] .=> sim.names .* "($(sim.domain.phase.name))")
for (thermovariable, index) in sim.domain.thermovariabledict
rename!(df, names(df)[index] => thermovariable)
end
end
CSV.write(save_name, df)

Check warning on line 794 in src/Simulation.jl

View check run for this annotation

Codecov / codecov/patch

src/Simulation.jl#L786-L794

Added lines #L786 - L794 were not covered by tests
end
export save

Loading