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

ODESystem not dealing with dynamic parameters #3365

Closed
Sush1090 opened this issue Feb 3, 2025 · 1 comment
Closed

ODESystem not dealing with dynamic parameters #3365

Sush1090 opened this issue Feb 3, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@Sush1090
Copy link

Sush1090 commented Feb 3, 2025

Describe the bug 🐞
Hello the question was first posted here

I have repeated it below.

ODE solver does not consider the parameter to be dynamic. Instead fixes the value to be at t=0
Expected behavior

The chosen parameter should change over t and should affect the solution to the ODE.

Minimal Reproducible Example 👇

using ModelingToolkit, DifferentialEquations
using ModelingToolkit: t_nounits as t, D_nounits as D

function test(;name)
    vars = @variables begin
        x(t)
    end
    para = @parameters begin
        α(t)
    end
    eqs = [
        D(x) ~ α
    ]
    ODESystem(eqs,t,vars,para,name=name)
end
@named model = test()
sys = structural_simplify(model)
para = [sys.α => cos(t)]
u0 = [sys.x => 1]
tspan = (0,2π)
prob = ODEProblem(sys,u0,tspan,para)
sol = solve(prob)

Error & Stacktrace ⚠️
No error

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
[961ee093] ModelingToolkit v9.62.0
[0c46a032] DifferentialEquations v7.15.0
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
  • Output of versioninfo()
Julia Version 1.11.3
Commit d63adeda50 (2025-01-21 19:42 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 20 × 12th Gen Intel(R) Core(TM) i7-12800H
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, alderlake)
Threads: 1 default, 0 interactive, 1 GC (on 20 virtual cores)

Additional context

@Sush1090 Sush1090 added the bug Something isn't working label Feb 3, 2025
@tpdsantos
Copy link

tpdsantos commented Feb 7, 2025

Dynamic parameters work differently from variables. In order to do that, you need to create a callable parameter of the form (param ::FuncType)(..) and then call param(t) inside the equations:

using ModelingToolkit, DifferentialEquations
using ModelingToolkit: t_nounits as t, D_nounits as D

function test(;name)
    vars = @variables begin
        x(t)
    end
    para = @parameters begin::Function)(..)
    end
    eqs = [
        D(x) ~ α(t)
    ]
    ODESystem(eqs,t,vars,para,name=name)
end
@named model = test()
sys = structural_simplify(model)
para = [sys.α => cos] # the input must be the function
u0 = [sys.x => 1]
tspan = (0,2π)
prob = ODEProblem(sys,u0,tspan,para)
sol = solve(prob)

For more information, you can go to the documentation page about callable parameters: https://docs.sciml.ai/ModelingToolkit/stable/tutorials/callable_params/#Callable-parameter-syntax

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

No branches or pull requests

2 participants