Skip to content

Commit

Permalink
Merge pull request #3001 from hersle/fix_mtkmodel_iv
Browse files Browse the repository at this point in the history
Fix @mtkmodel independent variable generation
  • Loading branch information
ChrisRackauckas authored Aug 30, 2024
2 parents 6195049 + dc954e8 commit 4cb17cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/independent_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Define one or more independent variables. For example:
macro independent_variables(ts...)
:(@parameters $(ts...)) |> esc # TODO: treat independent variables separately from variables and parameters
end

toiv(s::Symbolic) = setmetadata(s, MTKVariableTypeCtx, PARAMETER)
toiv(s::Num) = Num(toiv(value(s)))
4 changes: 3 additions & 1 deletion src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ function generate_var(a, varclass;
first(@variables $a[indices...]::type)
if varclass == :parameters
var = toparam(var)
elseif varclass == :independent_variables
var = toiv(var)
end
var
end
Expand Down Expand Up @@ -315,7 +317,7 @@ end
function generate_var!(dict, a, b, varclass, mod;
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
type = Real)
iv = b == :t ? get_t(mod, b) : generate_var(b, :variables)
iv = b == :t ? get_t(mod, b) : generate_var(b, :independent_variables)
prev_iv = get!(dict, :independent_variable) do
iv
end
Expand Down
12 changes: 12 additions & 0 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,18 @@ end
@variables y(x)
@test_nowarn @named sys = ODESystem([y ~ 0], x)

# the same, but with @mtkmodel
@independent_variables x
@mtkmodel MyModel begin
@variables begin
y(x)
end
@equations begin
y ~ 0
end
end
@test_nowarn @mtkbuild sys = MyModel()

@variables x y(x)
@test_logs (:warn,) @named sys = ODESystem([y ~ 0], x)

Expand Down

0 comments on commit 4cb17cd

Please sign in to comment.