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

Reduce TTF@mtkmodel #3471

Merged
merged 10 commits into from
Mar 20, 2025
Merged
28 changes: 28 additions & 0 deletions src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,34 @@ PrecompileTools.@compile_workload begin
@variables x(ModelingToolkit.t_nounits)
@named sys = ODESystem([ModelingToolkit.D_nounits(x) ~ -x], ModelingToolkit.t_nounits)
prob = ODEProblem(structural_simplify(sys), [x => 30.0], (0, 100), [], jac = true)
@mtkmodel __testmod__ begin
@constants begin
c = 1.0
end
@structural_parameters begin
structp = false
end
if structp
@variables begin
x(t) = 0.0, [description = "foo", guess = 1.0]
end
else
@variables begin
x(t) = 0.0, [description = "foo w/o structp", guess = 1.0]
end
end
@parameters begin
a = 1.0, [description = "bar"]
if structp
b = 2 * a, [description = "if"]
else
c
end
end
@equations begin
x ~ a + b
end
end
end

export AbstractTimeDependentSystem,
Expand Down
8 changes: 6 additions & 2 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ end
# The comments indicate the syntax matched by a block; either when parsed directly
# when it is called recursively for parsing a part of an expression.
# These variable definitions are part of test suite in `test/model_parsing.jl`
function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
Base.@nospecializeinfer function parse_variable_def!(
dict, mod, arg, varclass, kwargs, where_types;
def = nothing, type::Type = Real, meta = Dict{DataType, Expr}())
@nospecialize
arg isa LineNumberNode && return
MLStyle.@match arg begin
# Parses: `a`
Expand Down Expand Up @@ -1355,7 +1357,9 @@ push_something!(v, x...) = push_something!.(Ref(v), x)

define_blocks(branch) = [Expr(branch), Expr(branch), Expr(branch), Expr(branch)]

function parse_top_level_branch(condition, x, y = nothing, branch = :if)
Base.@nospecializeinfer function parse_top_level_branch(
condition, x, y = nothing, branch::Symbol = :if)
@nospecialize
blocks::Vector{Union{Expr, Nothing}} = component_blk, equations_blk, parameter_blk, variable_blk = define_blocks(branch)

for arg in x
Expand Down
Loading