SciML / Catalyst.jl

Chemical reaction network and systems biology interface for scientific machine learning (SciML). High performance, GPU-parallelized, and O(1) solvers in open source software.
https://docs.sciml.ai/Catalyst/stable/
Other
437 stars 71 forks source link

Special case of creating `ODEProblem` from `ODESystem` with conservation laws and default values takes *very* long time #886

Open TorkelE opened 1 month ago

TorkelE commented 1 month ago

I am going through the docs bit byt bit, trying to see which parts contribute to long build times. This bit in the FAQ one is a significant culprit (haven't bothered waiting to see how long it takes for it to finish/if it actually does):

using Catalyst, OrdinaryDiffEq, Plots
rn = @reaction_network ABtoC begin
  (k₊,k₋), A + B <--> C
end

# initial condition and parameter values
setdefaults!(rn, [:A => 1.0, :B => 2.0, :C => 0.0, :k₊ => 1.0, :k₋ => 1.0])

osys = convert(ODESystem, rn; remove_conserved = true)
osys = complete(osys)

observed(osys)

oprob = ODEProblem(osys, [], (0.0, 10.0), []) # We get stuck at this step.
sol = solve(oprob, Tsit5())

Ensuring completeness only after defaults are set, and using symbolic representations, does not help:

using Catalyst, OrdinaryDiffEq, Plots
rn = @network_component ABtoC begin
  (k₊,k₋), A + B <--> C
end
@unpack A, B, C, k₊, k₋ = rn

# initial condition and parameter values
setdefaults!(rn, [A => 1.0, B => 2.0, C => 0.0, k₊ => 1.0, k₋ => 1.0])
rn = complete(rn)

osys = convert(ODESystem, rn; remove_conserved = true)
osys = complete(osys)

observed(osys)

oprob = ODEProblem(osys, [], (0.0, 10.0), []) # We still get stuck at this step.
sol = solve(oprob, Tsit5())

A normal workflow, however, is fine:

using Catalyst, OrdinaryDiffEq, Plots
rn = @reaction_network ABtoC begin
  (k₊,k₋), A + B <--> C
end

oprob = ODEProblem(rn, [:A => 1.0, :B => 2.0, :C => 0.0], (0.0, 10.0), [:k₊ => 1.0, :k₋ => 1.0]; remove_conserved = true)
sol = solve(oprob, Tsit5())
isaacsas commented 1 month ago

Is this a Catalyst issue or a general MTK issue with the new symbolic indexing interface changes? If the latter this should be an issue over there.

TorkelE commented 1 month ago

I already spent over an hour trying to figure out what was going on with the FAQ docs, so I raised the issue here for a starter. When I get the opportunity I will try to look into this in more detail. Likely it is a MTK issue, in which case I agree it should be moved over there.

ChrisRackauckas commented 1 month ago

What function is taking the time?

TorkelE commented 1 month ago

ODEProblem(osys, [], (0.0, 10.0), [])

ChrisRackauckas commented 1 month ago

No, inside of that function.

TorkelE commented 1 month ago

not sure, haven't gotten that far yet