Skip to content

Add support for setting parameters with a nested Dict #1159

Open
@awadell1

Description

@awadell1

Currently setting u0 and p requires access to scope that contains @variables / @parameters.

u0 = [
    x => 2.0
    subsys.x => 2.0
]

Could support be added for setting parameters with nested Dicts (Or something similar)?

u0 = Dict(
  :x => 2.0,
  :subsys => Dict(:x => 2.0)
)

A minimal (non-working) example of my use case is below. Basically, I want to define my system in a function (Or compose it programmatically) and then set the parameters and initial state later / in a different scope.

using ModelingToolkit

function decay(;name)
    @parameters t a
    @variables x(t) f(t)
    D = Differential(t)
    ODESystem([
        D(x) ~ -a*x + f
    ];
    name=name)
end

function system()
    # Subsystems
    @named decay1 = decay()
    @named decay2 = decay()

    # Connect Subsystems
    @variables t
    D = Differential(t)
    connections = [
        decay2.f ~ decay1.x,
        D(decay1.f) ~ 0,
    ]
    ODESystem(connections; systems = [decay1, decay2])
end

# x, f, decay1 and decay2 are out of scope here
x0 = Dict(
    :decay1 => Dict(:x => 1.0, f => 0.01),
    :decay2 => Dict(:x => 1.0),
)
p = Dict(
    :decay1 => Dict( :a => 0.1 ),
    :decay2 => Dict( :a => 0.2 ),
)

using OrdinaryDiffEq
prob = ODEProblem(system(), x0, (0.0, 100.0), p)
sol = solve(prob, Tsit5())

I'm happy to work on this, but would need some pointers as to where to start. I'm guessing with varmap_to_vars.

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