Skip to content

Commit fa7a18c

Browse files
committed
fix: pass type to var generation
1 parent c5bff6a commit fa7a18c

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

docs/src/basics/MTKModel_Connector.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ end
220220
```
221221

222222
!!! note
223-
223+
224224
For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/)
225225

226226
## More on `Model.structure`
@@ -246,7 +246,7 @@ Dict{Symbol, Any} with 7 entries:
246246
:components => [[:model_a, :ModelA]]
247247
:variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var), :v_array=>Dict(:size=>(2, 3)))
248248
:icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png")
249-
:kwargs => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :v=>Dict{Symbol, Union{Nothing, Symbol}}(:value=>:v_var, :type=>nothing), :v_array=>Dict(:value=>nothing, :type=>nothing), :p1=>Dict(:value=>nothing))
249+
:kwargs => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :v=>Dict{Symbol, Union{Nothing, Symbol}}(:value=>:v_var, :type=>Real), :v_array=>Dict(:value=>nothing, :type=>Real), :p1=>Dict(:value=>nothing))
250250
:structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin))
251251
:independent_variable => t
252252
:extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB]

src/systems/model_parsing.jl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ end
110110

111111
function parse_variable_def!(dict, mod, arg, varclass, kwargs;
112112
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
113-
type::Union{Type, Nothing} = nothing)
113+
type::Type = Real)
114114
metatypes = [(:connection_type, VariableConnectType),
115115
(:description, VariableDescription),
116116
(:unit, VariableUnit),
@@ -133,7 +133,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
133133
else
134134
push!(kwargs, Expr(:kw, Expr(:(::), a, Union{Nothing, type}), nothing))
135135
end
136-
var = generate_var!(dict, a, varclass; indices)
136+
var = generate_var!(dict, a, varclass; indices, type)
137137
dict[:kwargs][getname(var)] = Dict(:value => def, :type => type)
138138
(var, def)
139139
end
@@ -153,15 +153,15 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
153153
else
154154
push!(kwargs, Expr(:kw, Expr(:(::), a, Union{Nothing, type}), nothing))
155155
end
156-
var = generate_var!(dict, a, b, varclass; indices)
156+
var = generate_var!(dict, a, b, varclass; indices, type)
157157
type !== nothing && (dict[varclass][getname(var)][:type] = type)
158158
dict[:kwargs][getname(var)] = Dict(:value => def, :type => type)
159159
(var, def)
160160
end
161161
Expr(:(=), a, b) => begin
162162
Base.remove_linenums!(b)
163163
def, meta = parse_default(mod, b)
164-
var, def = parse_variable_def!(dict, mod, a, varclass, kwargs; def)
164+
var, def = parse_variable_def!(dict, mod, a, varclass, kwargs; def, type)
165165
dict[varclass][getname(var)][:default] = def
166166
if meta !== nothing
167167
for (type, key) in metatypes
@@ -179,7 +179,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
179179
(var, def)
180180
end
181181
Expr(:tuple, a, b) => begin
182-
var, def = parse_variable_def!(dict, mod, a, varclass, kwargs)
182+
var, def = parse_variable_def!(dict, mod, a, varclass, kwargs; type)
183183
meta = parse_metadata(mod, b)
184184
if meta !== nothing
185185
for (type, key) in metatypes
@@ -200,34 +200,38 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
200200
Expr(:ref, a, b...) => begin
201201
indices = map(i -> UnitRange(i.args[2], i.args[end]), b)
202202
parse_variable_def!(dict, mod, a, varclass, kwargs;
203-
def, indices)
203+
def, indices, type)
204204
end
205205
_ => error("$arg cannot be parsed")
206206
end
207207
end
208208

209209
function generate_var(a, varclass;
210-
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing)
211-
var = indices === nothing ? Symbolics.variable(a) : first(@variables $a[indices...])
210+
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
211+
type = Real)
212+
var = indices === nothing ? Symbolics.variable(a; T=type) : first(@variables $a[indices...]::type)
212213
if varclass == :parameters
213214
var = toparam(var)
214215
end
215216
var
216217
end
217218

218219
function generate_var!(dict, a, varclass;
219-
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing)
220+
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
221+
type = Real)
220222
vd = get!(dict, varclass) do
221223
Dict{Symbol, Dict{Symbol, Any}}()
222224
end
223225
vd isa Vector && (vd = first(vd))
224226
vd[a] = Dict{Symbol, Any}()
225227
indices !== nothing && (vd[a][:size] = Tuple(lastindex.(indices)))
226-
generate_var(a, varclass; indices)
228+
generate_var(a, varclass; indices, type)
227229
end
228230

229231
function generate_var!(dict, a, b, varclass;
230-
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing)
232+
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
233+
type = Real)
234+
# (type isa Nothing && type = Real)
231235
iv = generate_var(b, :variables)
232236
prev_iv = get!(dict, :independent_variable) do
233237
iv
@@ -239,10 +243,10 @@ function generate_var!(dict, a, b, varclass;
239243
vd isa Vector && (vd = first(vd))
240244
vd[a] = Dict{Symbol, Any}()
241245
var = if indices === nothing
242-
Symbolics.variable(a, T = SymbolicUtils.FnType{Tuple{Any}, Real})(iv)
246+
Symbolics.variable(a, T = SymbolicUtils.FnType{Tuple{Any}, type})(iv)
243247
else
244248
vd[a][:size] = Tuple(lastindex.(indices))
245-
first(@variables $a(iv)[indices...])
249+
first(@variables $a(iv)[indices...]::type)
246250
end
247251
if varclass == :parameters
248252
var = toparam(var)

test/model_parsing.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ModelingToolkit, Test
22
using ModelingToolkit: get_gui_metadata, get_systems, get_connector_type,
3-
get_ps, getdefault, getname, scalarize, VariableDescription,
4-
RegularConnector
3+
get_ps, getdefault, getname, scalarize, symtype,
4+
VariableDescription, RegularConnector
55
using URIs: URI
66
using Distributions
77
using DynamicQuantities, OrdinaryDiffEq
@@ -251,14 +251,23 @@ end
251251
par1::Int = 1
252252
par2(t)::Int,
253253
[description = "Enforced `par4` to be an Int by setting the type to the keyword-arg."]
254-
par3(t)::Float64 = 1.0
254+
par3(t)::BigFloat = 1.0
255255
par4(t)::Float64 = 1 # converts 1 to 1.0 of Float64 type
256+
par5[1:3]::BigFloat
257+
par6(t)[1:3]::BigFloat
258+
par7(t)[1:3]::BigFloat = 1.0, [description = "with description"]
256259
end
257260
end
258261

259262
@named type_model = TypeModel()
260263

261-
@test getname.(parameters(type_model)) == [:par0, :par1, :par2, :par3, :par4]
264+
@test symtype(type_model.par1) == Int
265+
@test symtype(type_model.par2) == Int
266+
@test symtype(type_model.par3) == BigFloat
267+
@test symtype(type_model.par4) == Float64
268+
@test symtype(type_model.par5[1]) == BigFloat
269+
@test symtype(type_model.par6[1]) == BigFloat
270+
@test symtype(type_model.par7[1]) == BigFloat
262271

263272
@test_throws TypeError TypeModel(; name = :throws, flag = 1)
264273
@test_throws TypeError TypeModel(; name = :throws, par0 = 1)
@@ -350,8 +359,8 @@ end
350359
@test A.structure[:extend] == [[:e], :extended_e, :E]
351360
@test A.structure[:equations] == ["e ~ 0"]
352361
@test A.structure[:kwargs] ==
353-
Dict{Symbol, Dict}(:p => Dict(:value => nothing, :type => nothing),
354-
:v => Dict(:value => nothing, :type => nothing))
362+
Dict{Symbol, Dict}(:p => Dict(:value => nothing, :type => Real),
363+
:v => Dict(:value => nothing, :type => Real))
355364
@test A.structure[:components] == [[:cc, :C]]
356365
end
357366

0 commit comments

Comments
 (0)