SciML / SymbolicIndexingInterface.jl

A general interface for symbolic indexing of SciML objects used in conjunction with Domain-Specific Languages
https://docs.sciml.ai/SymbolicIndexingInterface/stable/
MIT License
14 stars 6 forks source link

refactor: improve getu performance for vectors involving observed quantities #41

Closed AayushSabharwal closed 7 months ago

AayushSabharwal commented 7 months ago

Checklist

Additional context

Add any other context about the problem here.

AayushSabharwal commented 7 months ago
julia> using ModelingToolkit, SymbolicIndexingInterface, OrdinaryDiffEq
julia> using BenchmarkTools
julia> iv = only(@variables(t))
julia> sts = @variables s1(t) = 2.0 s1s2(t) = 2.0 s2(t) = 2.0
julia> ps = @parameters k1 = 1.0 c1 = 2.0
julia> eqs = [(Differential(t))(s1) ~ -0.25 * c1 * k1 * s1 * s2
           (Differential(t))(s1s2) ~ 0.25 * c1 * k1 * s1 * s2
           (Differential(t))(s2) ~ -0.25 * c1 * k1 * s1 * s2]
julia> @named sys = ODESystem(eqs)
julia> prob = ODEProblem(sys, [], (0.0, 1.0))
julia> sol = solve(prob, Tsit5())
julia> syms1 = [s1 + s2, 2s1 + s2, s1 + 2s2, 3s1+s2, 3s1+2s2, 4s2, 5s1, s1s2+3, s1s2];
julia> obs = SymbolicIndexingInterface.observed(sol, syms1)
julia> f = getu(sol, syms1)
julia> @btime obs(sol.u[1], sol.prob.p, sol.t[1]);
  577.575 ns (25 allocations: 3.75 KiB)

julia> @btime f(sol, 1);
  325.369 ns (21 allocations: 800 bytes)

julia> @btime obs.(sol.u, sol.prob.p |> tuple, sol.t);
  2.306 μs (118 allocations: 9.45 KiB)

julia> @btime f(sol);
  2.009 μs (113 allocations: 5.77 KiB)

Somehow getu is faster now. I verified results:

julia> obs(sol.u[1], sol.prob.p, sol.t[1]) == f(sol, 1)
true
julia> obs.(sol.u, sol.prob.p |> tuple, sol.t) == f(sol)
true
codecov[bot] commented 7 months ago

Codecov Report

Attention: 37 lines in your changes are missing coverage. Please review.

Comparison is base (dab25be) 28.35% compared to head (b8c9101) 25.33%.

Files Patch % Lines
src/state_indexing.jl 0.00% 37 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #41 +/- ## ========================================== - Coverage 28.35% 25.33% -3.03% ========================================== Files 7 7 Lines 201 225 +24 ========================================== Hits 57 57 - Misses 144 168 +24 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.