Skip to content

Access to vector of variables #908

Closed
@ValentinKaisermayer

Description

@ValentinKaisermayer

Consider a simple example, where I use a vector of variables.

function advection(;name, c=1, L=10, N=10, x0=0, xL=1)
    @parameters t
    @variables x[1:N](t)

    x0 = fill(x0, N)

    Δz = L / N
    Dt = Differential(t)
    eqs = [
        Dt(x[1]) ~ -c * (-xL + x[1]) / Δz
        [Dt(x[i]) ~ -c * (-x[i-1] + x[i]) / Δz for i in 2:N]...
    ]
    return ODESystem(eqs, t, [x], []; name=name, default_u0=[(x .=> x0)...])
end

@named model = advection()
sys = structural_simplify(model)
prob = ODAEProblem(sys, Pair[], (0, 50))
sol = solve(prob, Tsit5())
plot(sol)

test

If I try to access the variables like I would do for other models with scalar variables, I get:

julia> model.x
ERROR: MethodError: no method matching nameof(::Array{Num,1})
Closest candidates are:
  nameof(::VSCodeServer.JuliaInterpreter.Frame) at .vscode\extensions\julialang.language-julia-1.1.35\scripts\packages\JuliaInterpreter\src\utils.jl:17
  nameof(::JuliaInterpreter.Frame) at .julia\packages\JuliaInterpreter\lObtQ\src\utils.jl:17
  nameof(::Core.IntrinsicFunction) at reflection.jl:1194
  ...
Stacktrace:
 [1] getname(::Array{Num,1}) at .julia\packages\ModelingToolkit\9sIpH\src\systems\abstractsystem.jl:126
 [2] (::ModelingToolkit.var"#25#29"{Symbol})(::Array{Num,1}) at .julia\packages\ModelingToolkit\9sIpH\src\systems\abstractsystem.jl:223
 [3] findnext(::ModelingToolkit.var"#25#29"{Symbol}, ::Array{Array{Num,1},1}, ::Int64) at .\array.jl:1810
 [4] findfirst(::Function, ::Array{Array{Num,1},1}) at .\array.jl:1861
 [5] getproperty(::ODESystem, ::Symbol; namespace::Bool) at .julia\packages\ModelingToolkit\9sIpH\src\systems\abstractsystem.jl:223
 [6] getproperty(::ODESystem, ::Symbol) at .julia\packages\ModelingToolkit\9sIpH\src\systems\abstractsystem.jl:210
 [7] top-level scope at REPL[88]:1

The displayed information for the model is also not quite correct.

julia> model
Model model with 10 equations
States (1):
  Num[x₁(t), x₂(t), x₃(t), x₄(t), x₅(t), x₆(t), x₇(t), x₈(t), x₉(t), x₁₀(t)]
Parameters (0):

It should be 10 states - I think.

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