SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.43k stars 209 forks source link

plotting errors with time dependent parameters round 2 #3010

Open isaacsas opened 2 months ago

isaacsas commented 2 months ago

I'm still seeing a couple bugs on 9.36:

using ModelingToolkit, OrdinaryDiffEq, Plots
t = ModelingToolkit.t_nounits
D = ModelingToolkit.D_nounits
@variables x(t)
@parameters c(t)
@mtkbuild sys = ODESystem(
    D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [1.0 => [c ~ c + 1]])
prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0])
sol = solve(prob, Tsit5())
plot(sol; idxs = [sys.c, sys.x])

The last time point is missing when plotting c, i.e. below notice that the jump in c at time 6 is not shown and the graph terminates early:

c

Also, symbol based plotting or indexing doesn't seem to work for c, i.e.

julia> sol(1.0; idxs = :c)
ERROR: BoundsError: attempt to access Tuple{Int64, Int64} at index [3]
Stacktrace:
  [1] getindex(t::Tuple, i::Int64)
    @ Base ./tuple.jl:31
  [2] parameter_index(sys::ODESystem, sym::Symbol)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/yfT8s/src/systems/abstractsystem.jl:632
  [3] parameter_index (repeats 2 times)
    @ ~/.julia/packages/SymbolicIndexingInterface/CErRX/src/index_provider_interface.jl:59 [inlined]
  [4] _getp(sys::ODESolution{…}, ::SymbolicIndexingInterface.ScalarSymbolic, ::SymbolicIndexingInterface.NotSymbolic, p::Symbol)
    @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/CErRX/src/parameter_indexing.jl:348
  [5] getp
    @ ~/.julia/packages/SymbolicIndexingInterface/CErRX/src/parameter_indexing.jl:41 [inlined]
  [6] _getu(sys::ODESolution{…}, ::SymbolicIndexingInterface.ScalarSymbolic, ::SymbolicIndexingInterface.NotSymbolic, sym::Symbol)
    @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/CErRX/src/state_indexing.jl:124
  [7] getu
    @ ~/.julia/packages/SymbolicIndexingInterface/CErRX/src/state_indexing.jl:31 [inlined]
  [8] (::ODESolution{…})(t::Float64, ::Type{…}, idxs::Symbol, continuity::Symbol)
    @ SciMLBase ~/.julia/packages/SciMLBase/FfEKU/src/solutions/ode_solutions.jl:284
  [9] #_#478
    @ ~/.julia/packages/SciMLBase/FfEKU/src/solutions/ode_solutions.jl:215 [inlined]
 [10] AbstractODESolution
    @ ~/.julia/packages/SciMLBase/FfEKU/src/solutions/ode_solutions.jl:210 [inlined]
 [11] top-level scope
    @ REPL[25]:1
Some type information was truncated. Use `show(err)` to see complete types.

and

julia> plt = plot(sol; idxs = :c)
ERROR: BoundsError: attempt to access Tuple{Int64, Int64} at index [3]
Stacktrace:
  [1] getindex(t::Tuple, i::Int64)
    @ Base ./tuple.jl:31
  [2] parameter_index(sys::ODESystem, sym::Symbol)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/yfT8s/src/systems/abstractsystem.jl:632
  [3] parameter_index (repeats 2 times)
    @ ~/.julia/packages/SymbolicIndexingInterface/CErRX/src/index_provider_interface.jl:59 [inlined]
  [4] _getp(sys::ODESolution{…}, ::SymbolicIndexingInterface.ScalarSymbolic, ::SymbolicIndexingInterface.NotSymbolic, p::Symbol)
    @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/CErRX/src/parameter_indexing.jl:348
  [5] getp(sys::ODESolution{…}, p::Symbol)
    @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/CErRX/src/parameter_indexing.jl:41
  [6] macro expansion
    @ ~/.julia/packages/SciMLBase/FfEKU/src/solutions/solution_interface.jl:305 [inlined]
  [7] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, sol::SciMLBase.AbstractTimeseriesSolution)
    @ SciMLBase ~/.julia/packages/RecipesBase/BRe07/src/RecipesBase.jl:300
  [8] _process_userrecipes!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/user_recipe.jl:38
  [9] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/RecipesPipeline.jl:72
 [10] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any)
    @ Plots ~/.julia/packages/Plots/kLeqV/src/plot.jl:223
 [11] plot(args::Any; kw...)
    @ Plots ~/.julia/packages/Plots/kLeqV/src/plot.jl:102
 [12] top-level scope
    @ REPL[26]:1
Some type information was truncated. Use `show(err)` to see complete types.
isaacsas commented 2 months ago

For reference, I really like this new feature! So I'm hoping to switch this current Catalyst example to use it once these issues are fixed: https://docs.sciml.ai/Catalyst/stable/model_simulation/examples/periodic_events_simulation/#periodic_event_simulation_plotting_light