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

Translate Particle problem to EnsembleProblem #942

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
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
61 changes: 61 additions & 0 deletions ext/DiffEqBaseMonteCarloMeasurementsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,65 @@
abs(value(u))
end

# To make DAE problems work, we translate the MCM problem into an ensemble problem,
# solve it, and then repackage the solution into an ODESolution with Particles

const ParticleODEProblem = ODEProblem{<:Any, <:Any, <:Any, <:MonteCarloMeasurements.SomeKindOfParticles}

using MonteCarloMeasurements: nparticles, vecindex

function SciMLBase.EnsembleProblem(prob::ParticleODEProblem, args...; kwargs...)
p = copy(prob.p)
u0 = copy(prob.u0)
N = max(nparticles(eltype(p)), nparticles(eltype(u0)))
p0 = vecindex.(p, 1)
u00 = vecindex.(u0, 1)
prob0 = remake(prob, p = p0, u0 = u00)

Check warning on line 67 in ext/DiffEqBaseMonteCarloMeasurementsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DiffEqBaseMonteCarloMeasurementsExt.jl#L61-L67

Added lines #L61 - L67 were not covered by tests

prob_func = function(probi,i,repeat)
for j in eachindex(probi.u0)
probi.u0[j] = vecindex(u0[j], i)
end
for j in eachindex(probi.p)
probi.p[j] = vecindex(p[j], i)
end
probi

Check warning on line 76 in ext/DiffEqBaseMonteCarloMeasurementsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DiffEqBaseMonteCarloMeasurementsExt.jl#L69-L76

Added lines #L69 - L76 were not covered by tests
end

eprob = EnsembleProblem(prob0; prob_func)

Check warning on line 79 in ext/DiffEqBaseMonteCarloMeasurementsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DiffEqBaseMonteCarloMeasurementsExt.jl#L79

Added line #L79 was not covered by tests
end

function DiffEqBase.solve(prob::ParticleODEProblem, alg, args...; kwargs...)
N = max(nparticles(eltype(prob.p)), nparticles(eltype(prob.u0)))
eprob = EnsembleProblem(prob)
esol = DiffEqBase.solve(eprob, alg, EnsembleThreads(); trajectories = N, kwargs...)
postprocess(esol)

Check warning on line 86 in ext/DiffEqBaseMonteCarloMeasurementsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DiffEqBaseMonteCarloMeasurementsExt.jl#L82-L86

Added lines #L82 - L86 were not covered by tests
end

function postprocess(esol)
soli = esol[1]
t = soli.t
nx = length(soli[1])

Check warning on line 92 in ext/DiffEqBaseMonteCarloMeasurementsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DiffEqBaseMonteCarloMeasurementsExt.jl#L89-L92

Added lines #L89 - L92 were not covered by tests

# [state_index][mc_index]
utraj = map(t) do t
data = reduce(hcat, OrdinaryDiffEq.EnsembleAnalysis.componentwise_vectors_timepoint(esol,t)) # nmc × nx
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
data = reduce(hcat, OrdinaryDiffEq.EnsembleAnalysis.componentwise_vectors_timepoint(esol,t)) # nmc × nx
data = reduce(hcat, SciMLBase.EnsembleAnalysis.componentwise_vectors_timepoint(esol,t)) # nmc × nx

xparts = Particles(data)

Check warning on line 97 in ext/DiffEqBaseMonteCarloMeasurementsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DiffEqBaseMonteCarloMeasurementsExt.jl#L95-L97

Added lines #L95 - L97 were not covered by tests
end

u_analytic = nothing
errors = nothing
k = nothing
prob = soli.prob
alg = soli.alg
interp = nothing
dense = false
tslocation = soli.tslocation
destats = nothing
alg_choice = soli.alg_choice
retcode = soli.retcode
ODESolution{eltype(soli.u[1]), 2}(utraj, u_analytic, errors, t, k, prob, alg, interp, dense,

Check warning on line 111 in ext/DiffEqBaseMonteCarloMeasurementsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DiffEqBaseMonteCarloMeasurementsExt.jl#L100-L111

Added lines #L100 - L111 were not covered by tests
tslocation, destats, alg_choice, retcode)
end

end
Loading