Skip to content

Order of parameters #365

@lungd

Description

@lungd

I was expecting to see sys.ps in the same order as I defined p. I think this is true for the states.

using ModelingToolkit, OrdinaryDiffEq, DiffEqCallbacks

@parameters t b a
@variables x(t)
@derivatives D'~t

equ = [D(x) ~ a * b]
u0 = [x => 1.0]

p = [	
	b => 0.2,
	a => 0.1,
]
display(p)

sys = ODESystem(equ)
display(sys.ps)

2-element Array{Pair{Operation,Float64},1}:
 b => 0.2
 a => 0.1
2-element Array{Variable,1}:
 a
 b

How am I supposed to change a specific parameter inside a callback?
Let's say I want to change the last parameter (param a) at a known time.

using ModelingToolkit, OrdinaryDiffEq, DiffEqCallbacks

@parameters t b a
@variables x(t)
@derivatives D'~t

equ = [D(x) ~ a * b]

u0 = [x => 1.0]

p = [	
	b => 0.2,
	a => 0.1,
]

sys = ODESystem(equ)

tspan = (0.0, 2.0)

starttimes = [1]
change!(integrator) = integrator.p[end] = 0.3
displ(integrator) = display(integrator.p)
cb1 = PresetTimeCallback(starttimes, change!)
cb2 = PresetTimeCallback(starttimes, displ)

prob = ODEProblem(sys, u0, tspan, p)
sol = solve(prob,Tsit5(),callback=CallbackSet(cb1, cb2))

But this code changes b due to the 'wrong' order of the parameters.
I could change the callback like the following if I know the identifier of the last parameter:

function change!(integrator)
	for i=1:length(sys.ps)
		if string(sys.ps[i]) == string(a)
			integrator.p[i] = 0.3
		end
	end
end

But there is a more elegant solution for sure...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions