Skip to content

Commit cdc3d69

Browse files
committed
feat: update the vartype of kwargs rel. to symbolic-array
While here, removes passing `indices` around to generate_var, update_kwargs_and_metadata! and parse_variable_def! as these no longer handles symbolic-arrays.
1 parent 78f0967 commit cdc3d69

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

src/systems/model_parsing.jl

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,17 @@ end
148148

149149
pop_structure_dict!(dict, key) = length(dict[key]) == 0 && pop!(dict, key)
150150

151-
function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
151+
function update_kwargs_and_metadata!(dict, kwargs, a, def, type,
152152
varclass, where_types, meta)
153-
if indices isa Nothing
154-
if !isnothing(meta) && haskey(meta, VariableUnit)
155-
uvar = gensym()
156-
push!(where_types, uvar)
157-
push!(kwargs, Expr(:kw, :($a::Union{Nothing, $uvar}), nothing))
158-
else
159-
push!(kwargs, Expr(:kw, :($a::Union{Nothing, $type}), nothing))
160-
end
161-
dict[:kwargs][a] = Dict(:value => def, :type => type)
153+
if !isnothing(meta) && haskey(meta, VariableUnit)
154+
uvar = gensym()
155+
push!(where_types, uvar)
156+
push!(kwargs, Expr(:kw, :($a::Union{Nothing, $uvar}), nothing))
162157
else
163-
vartype = gensym(:T)
164-
push!(kwargs,
165-
Expr(:kw,
166-
Expr(:(::), a,
167-
Expr(:curly, :Union, :Nothing, Expr(:curly, :AbstractArray, vartype))),
168-
nothing))
169-
if !isnothing(meta) && haskey(meta, VariableUnit)
170-
push!(where_types, vartype)
171-
else
172-
push!(where_types, :($vartype <: $type))
173-
end
174-
dict[:kwargs][a] = Dict(:value => def, :type => AbstractArray{type})
158+
push!(kwargs, Expr(:kw, :($a::Union{Nothing, $type}), nothing))
175159
end
160+
dict[:kwargs][a] = Dict(:value => def, :type => type)
161+
176162
if dict[varclass] isa Vector
177163
dict[varclass][1][a][:type] = AbstractArray{type}
178164
else
@@ -207,8 +193,8 @@ function update_readable_metadata!(varclass_dict, meta::Dict, varname)
207193
end
208194
end
209195

210-
function push_array_kwargs_and_metadata!(
211-
dict, indices, meta, type, varclass, varname, varval)
196+
function update_array_kwargs_and_metadata!(
197+
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
212198
dict[varclass] = get!(dict, varclass) do
213199
Dict{Symbol, Dict{Symbol, Any}}()
214200
end
@@ -221,6 +207,18 @@ function push_array_kwargs_and_metadata!(
221207
:type => type
222208
)))
223209

210+
vartype = gensym(:T)
211+
push!(kwargs,
212+
Expr(:kw,
213+
Expr(:(::), varname,
214+
Expr(:curly, :Union, :Nothing, Expr(:curly, :AbstractArray, vartype))),
215+
nothing))
216+
if !isnothing(meta) && haskey(meta, VariableUnit)
217+
push!(where_types, vartype)
218+
else
219+
push!(where_types, :($vartype <: $type))
220+
end
221+
224222
# Useful keys for kwargs entry are: value, type and size.
225223
dict[:kwargs][varname] = varclass_dict[][varname]
226224

@@ -237,13 +235,12 @@ function unit_handled_variable_value(meta, varname)
237235
end
238236

239237
function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
240-
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
241-
type::Type = Real, meta = Dict{DataType, Expr}())
238+
def = nothing, type::Type = Real, meta = Dict{DataType, Expr}())
242239
arg isa LineNumberNode && return
243240
MLStyle.@match arg begin
244241
a::Symbol => begin
245-
var = generate_var!(dict, a, varclass; indices, type)
246-
update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
242+
var = generate_var!(dict, a, varclass; type)
243+
update_kwargs_and_metadata!(dict, kwargs, a, def, type,
247244
varclass, where_types, meta)
248245
return var, def, Dict()
249246
end
@@ -259,8 +256,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
259256
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
260257
end
261258
Expr(:call, a, b) => begin
262-
var = generate_var!(dict, a, b, varclass, mod; indices, type)
263-
update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
259+
var = generate_var!(dict, a, b, varclass, mod; type)
260+
update_kwargs_and_metadata!(dict, kwargs, a, def, type,
264261
varclass, where_types, meta)
265262
return var, def, Dict()
266263
end
@@ -270,7 +267,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
270267
Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin
271268
(@isdefined type) || (type = Real)
272269
varname = Meta.isexpr(a, :call) ? a.args[1] : a
273-
push!(kwargs, Expr(:kw, varname, nothing))
270+
### push!(kwargs, Expr(:kw, varname, nothing))
274271
meta = parse_metadata(mod, meta_val)
275272
varval = (@isdefined default_val) ? default_val :
276273
unit_handled_variable_value(meta, varname)
@@ -285,8 +282,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
285282
var = :($varname = $first(@variables ($a[$(indices)]::$type = $varval),
286283
$meta_val))
287284
end
288-
push_array_kwargs_and_metadata!(
289-
dict, indices, meta, type, varclass, varname, varval)
285+
update_array_kwargs_and_metadata!(
286+
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
290287
(:($varname...), var), nothing, Dict()
291288
end
292289
Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) ||
@@ -297,7 +294,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
297294
meta = parse_metadata(mod, def_n_meta)
298295
varval = unit_handled_variable_value(meta, varname)
299296
val, def_n_meta = (def_n_meta.args[1], def_n_meta.args[2:end])
300-
push!(kwargs, Expr(:kw, varname, nothing))
297+
### push!(kwargs, Expr(:kw, varname, nothing))
301298
if varclass == :parameters
302299
Meta.isexpr(a, :call) &&
303300
assert_unique_independent_var(dict, a.args[end])
@@ -329,15 +326,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
329326
end
330327
varval, meta = def_n_meta, nothing
331328
end
332-
push_array_kwargs_and_metadata!(
333-
dict, indices, meta, type, varclass, varname, varval)
329+
update_array_kwargs_and_metadata!(
330+
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
334331
(:($varname...), var), nothing, Dict()
335332
end
336333
Expr(:(::), Expr(:ref, a, indices...), type) ||
337334
Expr(:ref, a, indices...) => begin
338335
(@isdefined type) || (type = Real)
339336
varname = a isa Expr && a.head == :call ? a.args[1] : a
340-
push!(kwargs, Expr(:kw, varname, nothing))
341337
if varclass == :parameters
342338
Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end])
343339
var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname))
@@ -350,8 +346,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
350346
throw("Symbolic array with arbitrary length is not handled for $varclass.
351347
Please open an issue with an example.")
352348
end
353-
push_array_kwargs_and_metadata!(
354-
dict, indices, nothing, type, varclass, varname, nothing)
349+
update_array_kwargs_and_metadata!(
350+
dict, indices, kwargs, nothing, type, varclass, varname, nothing, where_types)
355351
(:($varname...), var), nothing, Dict()
356352
end
357353
Expr(:(=), a, b) => begin
@@ -391,11 +387,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
391387
end
392388
end
393389

394-
function generate_var(a, varclass;
395-
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
396-
type = Real)
397-
var = indices === nothing ? Symbolics.variable(a; T = type) :
398-
first(@variables $a[indices...]::type)
390+
function generate_var(a, varclass; type = Real)
391+
var = Symbolics.variable(a; T = type)
399392
if varclass == :parameters
400393
var = toparam(var)
401394
elseif varclass == :independent_variables
@@ -425,7 +418,7 @@ function generate_var!(dict, a, varclass;
425418
vd isa Vector && (vd = first(vd))
426419
vd[a] = Dict{Symbol, Any}()
427420
indices !== nothing && (vd[a][:size] = Tuple(lastindex.(indices)))
428-
generate_var(a, varclass; indices, type)
421+
generate_var(a, varclass; type)
429422
end
430423

431424
function assert_unique_independent_var(dict, iv::Num)

0 commit comments

Comments
 (0)