Skip to content

plotting errors with time-dependent parameters #3004

New issue

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

By clicking “No 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? No Sign in to your account

Closed
isaacsas opened this issue Sep 1, 2024 · 2 comments · Fixed by #3007
Closed

plotting errors with time-dependent parameters #3004

isaacsas opened this issue Sep 1, 2024 · 2 comments · Fixed by #3007
Assignees
Labels
bug Something isn't working

Comments

@isaacsas
Copy link
Member

isaacsas commented Sep 1, 2024

For this example from the docs:

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())

Plotting the parameter's time-series kind of works

plot(sol; idxs = sys.c) 

parameter

but note the x-axis starts at t = 1 instead of t = 0.

Trying to plot the variable errors

plot(sol; idxs = sys.x) 

gives

ERROR: Cannot access discrete variable at time 0.0 before initial save 1.0
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] _hold_discrete
    @ ~/.julia/packages/SciMLBase/xlQdY/src/solutions/ode_solutions.jl:186 [inlined]
  [3] _broadcast_getindex_evalf
    @ ./broadcast.jl:709 [inlined]
  [4] _broadcast_getindex
    @ ./broadcast.jl:682 [inlined]
  [5] getindex
    @ ./broadcast.jl:636 [inlined]
  [6] macro expansion
    @ ./broadcast.jl:1004 [inlined]
  [7] macro expansion
    @ ./simdloop.jl:77 [inlined]
  [8] copyto!
    @ ./broadcast.jl:1003 [inlined]
  [9] copyto!
    @ ./broadcast.jl:956 [inlined]
 [10] copy
    @ ./broadcast.jl:928 [inlined]
 [11] materialize
    @ ./broadcast.jl:903 [inlined]
 [12] hold_discrete(disc_u::Vector{ModelingToolkit.NestedGetIndex{Tuple{…}}}, disc_t::Vector{Float64}, t::Vector{Float64})
    @ SciMLBase ~/.julia/packages/SciMLBase/xlQdY/src/solutions/ode_solutions.jl:197
 [13] #480
    @ ~/.julia/packages/SciMLBase/xlQdY/src/solutions/ode_solutions.jl:205 [inlined]
 [14] iterate
    @ ./generator.jl:47 [inlined]
 [15] collect(itr::Base.Generator{ParameterTimeseriesCollection{…}, SciMLBase.var"#480#481"{…}})
    @ Base ./array.jl:834
 [16] map
    @ ./abstractarray.jl:3313 [inlined]
 [17] get_interpolated_discretes(sol::ODESolution{…}, t::Vector{…}, deriv::Type, continuity::Symbol)
    @ SciMLBase ~/.julia/packages/SciMLBase/xlQdY/src/solutions/ode_solutions.jl:204
 [18] (::ODESolution{…})(t::Vector{…}, ::Type{…}, idxs::Vector{…}, continuity::Symbol)
    @ SciMLBase ~/.julia/packages/SciMLBase/xlQdY/src/solutions/ode_solutions.jl:340
 [19] #_#482
    @ ~/.julia/packages/SciMLBase/xlQdY/src/solutions/ode_solutions.jl:215 [inlined]
 [20] AbstractODESolution
    @ ~/.julia/packages/SciMLBase/xlQdY/src/solutions/ode_solutions.jl:210 [inlined]
 [21] solplot_vecs_and_labels(dims::Int64, vars::Vector{…}, plott::Vector{…}, sol::ODESolution{…}, plot_analytic::Bool, plot_analytic_timeseries::Nothing)
    @ SciMLBase ~/.julia/packages/SciMLBase/xlQdY/src/solutions/solution_interface.jl:506
 [22] diffeq_to_arrays(sol::ODESolution{…}, plot_analytic::Bool, denseplot::Bool, plotdensity::Int64, tspan::Nothing, vars::Vector{…}, tscale::Symbol, plotat::Nothing)
    @ SciMLBase ~/.julia/packages/SciMLBase/xlQdY/src/solutions/solution_interface.jl:400
 [23] macro expansion
    @ ~/.julia/packages/SciMLBase/xlQdY/src/solutions/solution_interface.jl:210 [inlined]
 [24] macro expansion
    @ ~/.julia/packages/RecipesBase/BRe07/src/RecipesBase.jl:340 [inlined]
 [25] macro expansion
    @ ~/.julia/packages/SciMLBase/xlQdY/src/solutions/solution_interface.jl:204 [inlined]
 [26] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, sol::SciMLBase.AbstractTimeseriesSolution)
    @ SciMLBase ~/.julia/packages/RecipesBase/BRe07/src/RecipesBase.jl:300
 [27] _process_userrecipes!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/user_recipe.jl:38
 [28] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/RecipesPipeline.jl:72
 [29] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any)
    @ Plots ~/.julia/packages/Plots/kLeqV/src/plot.jl:223
 [30] plot(args::Any; kw...)
    @ Plots ~/.julia/packages/Plots/kLeqV/src/plot.jl:102
 [31] top-level scope
    @ REPL[63]:1
Some type information was truncated. Use `show(err)` to see complete types.

Pkg.status is:

  [479239e8] Catalyst v14.4.0 `~/.julia/dev/Catalyst`
  [ccbc3e58] JumpProcesses v9.13.5
  [961ee093] ModelingToolkit v9.35.1
  [1dea7af3] OrdinaryDiffEq v6.89.0
  [91a5bcdd] Plots v1.40.8
  [2efcf032] SymbolicIndexingInterface v0.3.28
@isaacsas isaacsas added the bug Something isn't working label Sep 1, 2024
@SciML SciML deleted a comment Sep 1, 2024
@AayushSabharwal
Copy link
Member

There are two parts to this issue:

  • MTK's events don't save at t=0, so the initial value of the discrete is not known
  • Interpolation (rightly) refuses to interpolate the value of a discrete variable before the initial time it was saved. However, it should not do this if the value being interpolated isn't discrete.

@isaacsas
Copy link
Member Author

isaacsas commented Sep 2, 2024

Thanks!

No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants