-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathmodel_parsing.jl
More file actions
1491 lines (1367 loc) · 55.8 KB
/
Copy pathmodel_parsing.jl
File metadata and controls
1491 lines (1367 loc) · 55.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
$(TYPEDEF)
ModelingToolkit component or connector with metadata
# Fields
$(FIELDS)
"""
struct Model{F, S}
"""The constructor that returns System."""
f::F
"""
The dictionary with metadata like keyword arguments (:kwargs), base
system this Model extends (:extend), sub-components of the Model (:components),
variables (:variables), parameters (:parameters), structural parameters
(:structural_parameters) and equations (:equations).
"""
structure::S
"""
This flag is `true` when the Model is a connector and is `false` when it is
a component
"""
isconnector::Bool
end
(m::Model)(args...; kw...) = m.f(args...; kw...)
Base.parentmodule(m::Model) = parentmodule(m.f)
for f in (:connector, :mtkmodel)
isconnector = f == :connector ? true : false
@eval begin
macro $f(fullname::Union{Expr, Symbol}, body)
esc($(:_model_macro)(__module__, fullname, body, $isconnector))
end
end
end
flatten_equations(eqs::Vector{Equation}, eq::Equation) = vcat(eqs, [eq])
flatten_equations(eq::Vector{Equation}, eqs::Vector{Equation}) = vcat(eq, eqs)
function flatten_equations(eqs::Vector{Union{Equation, Vector{Equation}}})
foldl(flatten_equations, eqs; init = Equation[])
end
function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
if fullname isa Symbol
name, type = fullname, :System
else
if fullname.head == :(::)
name, type = fullname.args
else
error("`$fullname` is not a valid name.")
end
end
exprs = Expr(:block)
dict = Dict{Symbol, Any}(
:defaults => Dict{Symbol, Any}(),
:kwargs => Dict{Symbol, Dict}(),
:structural_parameters => Dict{Symbol, Dict}()
)
comps = Union{Symbol, Expr}[]
ext = []
eqs = Expr[]
icon = Ref{Union{String, URI}}()
ps, sps, vs, = [], [], []
c_evts = []
d_evts = []
cons = []
costs = []
kwargs = OrderedCollections.OrderedSet()
where_types = Union{Symbol, Expr}[]
push!(exprs.args, :(variables = []))
push!(exprs.args, :(parameters = []))
# We build `System` by default
push!(exprs.args, :(systems = ModelingToolkit.AbstractSystem[]))
push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[]))
push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}()))
Base.remove_linenums!(expr)
for arg in expr.args
if arg.head == :macrocall
parse_model!(exprs.args, comps, ext, eqs, icon, vs, ps,
sps, c_evts, d_evts, cons, costs, dict, mod, arg, kwargs, where_types)
elseif arg.head == :block
push!(exprs.args, arg)
elseif arg.head == :if
MLStyle.@match arg begin
Expr(:if, condition, x) => begin
parse_conditional_model_statements(comps, dict, eqs, exprs, kwargs,
mod, ps, vs, where_types,
parse_top_level_branch(condition, x.args)...)
end
Expr(:if, condition, x, y) => begin
parse_conditional_model_statements(comps, dict, eqs, exprs, kwargs,
mod, ps, vs, where_types,
parse_top_level_branch(condition, x.args, y)...)
end
_ => error("Got an invalid argument: $arg")
end
elseif isconnector
# Connectors can have variables listed without `@variables` prefix or
# begin block.
parse_variable_arg!(
exprs.args, vs, dict, mod, arg, :variables, kwargs, where_types)
else
error("$arg is not valid syntax. Expected a macro call.")
end
end
iv = get(dict, :independent_variable, nothing)
if iv === nothing
iv = dict[:independent_variable] = get_t(mod, :t)
end
push!(exprs.args, :(push!(equations, $(eqs...))))
push!(exprs.args, :(push!(parameters, $(ps...))))
push!(exprs.args, :(push!(systems, $(comps...))))
push!(exprs.args, :(push!(variables, $(vs...))))
gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) :
GUIMetadata(GlobalRef(mod, name))
consolidate = get(dict, :consolidate, default_consolidate)
description = get(dict, :description, "")
@inline pop_structure_dict!.(
Ref(dict), [:defaults, :kwargs, :structural_parameters])
sys = :($type($(flatten_equations)(equations), $iv, variables, parameters;
name, description = $description, systems, gui_metadata = $gui_metadata,
continuous_events = [$(c_evts...)], discrete_events = [$(d_evts...)],
defaults, costs = [$(costs...)], constraints = [$(cons...)], consolidate = $consolidate))
if length(ext) == 0
push!(exprs.args, :(var"#___sys___" = $sys))
else
push!(exprs.args, :(var"#___sys___" = $extend($sys, [$(ext...)])))
end
isconnector && push!(exprs.args,
:($Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))))
f = if length(where_types) == 0
:($(Symbol(:__, name, :__))(; name, $(kwargs...)) = $exprs)
else
f_with_where = Expr(:where)
push!(f_with_where.args,
:($(Symbol(:__, name, :__))(; name, $(kwargs...))), where_types...)
:($f_with_where = $exprs)
end
:($name = $Model($f, $dict, $isconnector))
end
pop_structure_dict!(dict, key) = length(dict[key]) == 0 && pop!(dict, key)
struct NoValue end
const NO_VALUE = NoValue()
function update_kwargs_and_metadata!(dict, kwargs, a, def, type,
varclass, where_types, meta)
if !isnothing(meta) && haskey(meta, VariableUnit)
uvar = gensym()
push!(where_types, uvar)
push!(kwargs,
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE))
else
push!(kwargs,
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE))
end
dict[:kwargs][a] = Dict(:value => def, :type => type)
if dict[varclass] isa Vector
dict[varclass][1][a][:type] = AbstractArray{type}
else
dict[varclass][a][:type] = type
end
end
function update_readable_metadata!(varclass_dict, meta::Dict, varname)
metatypes = [(:connection_type, VariableConnectType),
(:description, VariableDescription),
(:unit, VariableUnit),
(:bounds, VariableBounds),
(:noise, VariableNoiseType),
(:input, VariableInput),
(:output, VariableOutput),
(:irreducible, VariableIrreducible),
(:state_priority, VariableStatePriority),
(:misc, VariableMisc),
(:disturbance, VariableDisturbance),
(:tunable, VariableTunable),
(:dist, VariableDistribution)]
var_dict = get!(varclass_dict, varname) do
Dict{Symbol, Any}()
end
for (type, key) in metatypes
if (mt = get(meta, key, nothing)) !== nothing
key == VariableConnectType && (mt = nameof(mt))
var_dict[type] = mt
end
end
end
function update_array_kwargs_and_metadata!(
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
dict[varclass] = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : Ref(dict[varclass])
merge!(varclass_dict[],
Dict(varname => Dict(
:size => tuple([index_arg.args[end] for index_arg in indices]...),
:value => varval,
:type => type
)))
vartype = gensym(:T)
push!(kwargs,
Expr(:kw,
Expr(:(::), varname,
Expr(:curly, :Union, :Nothing, :Missing, NoValue,
Expr(:curly, :AbstractArray, vartype))),
NO_VALUE))
if !isnothing(meta) && haskey(meta, VariableUnit)
push!(where_types, vartype)
else
push!(where_types, :($vartype <: $type))
end
# Useful keys for kwargs entry are: value, type and size.
dict[:kwargs][varname] = varclass_dict[][varname]
meta !== nothing && update_readable_metadata!(varclass_dict[], meta, varname)
end
function unit_handled_variable_value(meta, varname)
varval = if meta isa Nothing || get(meta, VariableUnit, nothing) isa Nothing
varname
else
:($convert_units($(meta[VariableUnit]), $varname))
end
return varval
end
# This function parses various variable/parameter definitions.
#
# 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`
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`
# Recursively called by: `c(t) = cval + jval`
# Recursively called by: `d = 2`
# Recursively called by: `e, [description = "e"]`
# Recursively called by: `f = 3, [description = "f"]`
# Recursively called by: `k = kval, [description = "k"]`
# Recursively called by: `par0::Bool = true`
a::Symbol => begin
var = generate_var!(dict, a, varclass; type)
update_kwargs_and_metadata!(dict, kwargs, a, def, type,
varclass, where_types, meta)
return var, def, Dict()
end
# Parses: `par5[1:3]::BigFloat`
# Parses: `par6(t)[1:3]::BigFloat`
# Recursively called by: `par2(t)::Int`
# Recursively called by: `par3(t)::BigFloat = 1.0`
Expr(:(::), a, type) => begin
type = getfield(mod, type)
parse_variable_def!(
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
end
# Recursively called by: `i(t) = 4, [description = "i(t)"]`
# Recursively called by: `h(t), [description = "h(t)"]`
# Recursively called by: `j(t) = jval, [description = "j(t)"]`
# Recursively called by: `par2(t)::Int`
# Recursively called by: `par3(t)::BigFloat = 1.0`
Expr(:call, a, b) => begin
var = generate_var!(dict, a, b, varclass, mod; type)
update_kwargs_and_metadata!(dict, kwargs, a, def, type,
varclass, where_types, meta)
return var, def, Dict()
end
# Condition 1 parses:
# `(l(t)[1:2, 1:3] = 1), [description = "l is more than 1D"]`
# `(l2(t)[1:N, 1:M] = 2), [description = "l is more than 1D, with arbitrary length"]`
# `(l3(t)[1:3] = 3), [description = "l2 is 1D"]`
# `(l4(t)[1:N] = 4), [description = "l2 is 1D, with arbitrary length"]`
#
# Condition 2 parses:
# `(l5(t)[1:3]::Int = 5), [description = "l3 is 1D and has a type"]`
# `(l6(t)[1:N]::Int = 6), [description = "l3 is 1D and has a type, with arbitrary length"]`
#
# Condition 3 parses:
# `e2[1:2]::Int, [description = "e2"]`
# `h2(t)[1:2]::Int, [description = "h2(t)"]`
#
# Condition 4 parses:
# `e2[1:2], [description = "e2"]`
# `h2(t)[1:2], [description = "h2(t)"]`
Expr(:tuple, Expr(:(=), Expr(:ref, a, indices...), default_val), meta_val) ||
Expr(:tuple, Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), default_val), meta_val) ||
Expr(:tuple, Expr(:(::), Expr(:ref, a, indices...), type), meta_val) ||
Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin
(@isdefined type) || (type = Real)
varname = Meta.isexpr(a, :call) ? a.args[1] : a
meta = parse_metadata(mod, meta_val)
varval = (@isdefined default_val) ? default_val :
unit_handled_variable_value(meta, varname)
if varclass == :parameters
Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end])
var = :($varname = $first(@parameters ($a[$(indices...)]::$type = $varval),
$meta_val))
elseif varclass == :constants
Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end])
var = :($varname = $first(@constants ($a[$(indices...)]::$type = $varval),
$meta_val))
else
Meta.isexpr(a, :call) ||
throw("$a is not a variable of the independent variable")
assert_unique_independent_var(dict, a.args[end])
var = :($varname = $first(@variables ($a[$(indices)]::$type = $varval),
$meta_val))
end
update_array_kwargs_and_metadata!(
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
(:($varname...), var), nothing, Dict()
end
# Condition 1 parses:
# `par7(t)[1:3, 1:3]::BigFloat = 1.0, [description = "with description"]`
#
# Condition 2 parses:
# `d2[1:2] = 2`
# `l(t)[1:2, 1:3] = 2, [description = "l is more than 1D"]`
Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) ||
Expr(:(=), Expr(:ref, a, indices...), def_n_meta) => begin
(@isdefined type) || (type = Real)
varname = Meta.isexpr(a, :call) ? a.args[1] : a
if Meta.isexpr(def_n_meta, :tuple)
meta = parse_metadata(mod, def_n_meta)
varval = unit_handled_variable_value(meta, varname)
val, def_n_meta = (def_n_meta.args[1], def_n_meta.args[2:end])
if varclass == :parameters
Meta.isexpr(a, :call) &&
assert_unique_independent_var(dict, a.args[end])
var = :($varname = $varname === $NO_VALUE ? $val : $varname;
$varname = $first(@parameters ($a[$(indices...)]::$type = $varval),
$(def_n_meta...)))
elseif varclass == :constants
Meta.isexpr(a, :call) &&
assert_unique_independent_var(dict, a.args[end])
var = :($varname = $varname === $NO_VALUE ? $val : $varname;
$varname = $first(@constants ($a[$(indices...)]::$type = $varval),
$(def_n_meta...)))
else
Meta.isexpr(a, :call) ||
throw("$a is not a variable of the independent variable")
assert_unique_independent_var(dict, a.args[end])
var = :($varname = $varname === $NO_VALUE ? $val : $varname;
$varname = $first(@variables $a[$(indices...)]::$type = (
$varval),
$(def_n_meta...)))
end
else
if varclass == :parameters
Meta.isexpr(a, :call) &&
assert_unique_independent_var(dict, a.args[end])
var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname;
$varname = $first(@parameters $a[$(indices...)]::$type = $varname))
elseif varclass == :constants
Meta.isexpr(a, :call) &&
assert_unique_independent_var(dict, a.args[end])
var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname;
$varname = $first(@constants $a[$(indices...)]::$type = $varname))
else
Meta.isexpr(a, :call) ||
throw("$a is not a variable of the independent variable")
assert_unique_independent_var(dict, a.args[end])
var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname;
$varname = $first(@variables $a[$(indices...)]::$type = $varname))
end
varval, meta = def_n_meta, nothing
end
update_array_kwargs_and_metadata!(
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
(:($varname...), var), nothing, Dict()
end
# Condition 1 is recursively called by:
# `par5[1:3]::BigFloat`
# `par6(t)[1:3]::BigFloat`
#
# Condition 2 parses:
# `b2(t)[1:2]`
# `a2[1:2]`
Expr(:(::), Expr(:ref, a, indices...), type) ||
Expr(:ref, a, indices...) => begin
(@isdefined type) || (type = Real)
varname = a isa Expr && a.head == :call ? a.args[1] : a
if varclass == :parameters
Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end])
var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname))
elseif varclass == :constants
Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end])
var = :($varname = $first(@constants $a[$(indices...)]::$type = $varname))
elseif varclass == :variables
Meta.isexpr(a, :call) ||
throw("$a is not a variable of the independent variable")
assert_unique_independent_var(dict, a.args[end])
var = :($varname = $first(@variables $a[$(indices...)]::$type = $varname))
else
throw("Symbolic array with arbitrary length is not handled for $varclass.
Please open an issue with an example.")
end
update_array_kwargs_and_metadata!(
dict, indices, kwargs, nothing, type, varclass, varname, nothing, where_types)
(:($varname...), var), nothing, Dict()
end
# Parses: `c(t) = cval + jval`
# Parses: `d = 2`
# Parses: `f = 3, [description = "f"]`
# Parses: `i(t) = 4, [description = "i(t)"]`
# Parses: `j(t) = jval, [description = "j(t)"]`
# Parses: `k = kval, [description = "k"]`
# Parses: `par0::Bool = true`
# Parses: `par3(t)::BigFloat = 1.0`
Expr(:(=), a, b) => begin
Base.remove_linenums!(b)
def, meta = parse_default(mod, b)
var, def, _ = parse_variable_def!(
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) :
Ref(dict[varclass])
varclass_dict[][getname(var)][:default] = def
if meta !== nothing
update_readable_metadata!(varclass_dict[], meta, getname(var))
var, metadata_with_exprs = set_var_metadata(var, meta)
return var, def, metadata_with_exprs
end
return var, def, Dict()
end
# Parses: `e, [description = "e"]`
# Parses: `h(t), [description = "h(t)"]`
# Parses: `par2(t)::Int`
Expr(:tuple, a, b) => begin
meta = parse_metadata(mod, b)
var, def, _ = parse_variable_def!(
dict, mod, a, varclass, kwargs, where_types; type, meta)
varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) :
Ref(dict[varclass])
if meta !== nothing
update_readable_metadata!(varclass_dict[], meta, getname(var))
var, metadata_with_exprs = set_var_metadata(var, meta)
return var, def, metadata_with_exprs
end
return var, def, Dict()
end
_ => error("$arg cannot be parsed")
end
end
function generate_var(a, varclass; type = Real)
var = Symbolics.variable(a; T = type)
if varclass == :parameters
var = toparam(var)
elseif varclass == :constants
var = toconstant(var)
elseif varclass == :independent_variables
var = toiv(var)
end
var
end
singular(sym) = last(string(sym)) == 's' ? Symbol(string(sym)[1:(end - 1)]) : sym
function check_name_uniqueness(dict, a, newvarclass)
for varclass in [:variables, :parameters, :structural_parameters, :constants]
dvarclass = get(dict, varclass, nothing)
if dvarclass !== nothing && a in keys(dvarclass)
error("Cannot create a $(singular(newvarclass)) `$(a)` because there is already a $(singular(varclass)) with that name")
end
end
end
function generate_var!(dict, a, varclass;
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
type = Real)
check_name_uniqueness(dict, a, varclass)
vd = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
vd isa Vector && (vd = first(vd))
vd[a] = Dict{Symbol, Any}()
indices !== nothing && (vd[a][:size] = Tuple(lastindex.(indices)))
generate_var(a, varclass; type)
end
function assert_unique_independent_var(dict, iv::Num)
assert_unique_independent_var(dict, nameof(iv))
end
function assert_unique_independent_var(dict, iv)
prev_iv = get!(dict, :independent_variable) do
iv
end
prev_iv isa Num && (prev_iv = nameof(prev_iv))
@assert isequal(iv, prev_iv) "Multiple independent variables are used in the model $(typeof(iv)) $(typeof(prev_iv))"
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, :independent_variables)
assert_unique_independent_var(dict, iv)
check_name_uniqueness(dict, a, varclass)
vd = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
vd isa Vector && (vd = first(vd))
vd[a] = Dict{Symbol, Any}()
var = if indices === nothing
first(@variables $a(iv)::type)
else
vd[a][:size] = Tuple(lastindex.(indices))
first(@variables $a(iv)[indices...]::type)
end
if varclass == :parameters
var = toparam(var)
elseif varclass == :constants
var = toconstant(var)
end
var
end
# Use the `t` defined in the `mod`. When it is unavailable, generate a new `t` with a warning.
function get_t(mod, t)
try
get_var(mod, t)
catch e
if e isa UndefVarError
@warn("Could not find a predefined `t` in `$mod`; generating a new one within this model.\nConsider defining it or importing `t` (or `t_nounits`, `t_unitful` as `t`) from ModelingToolkit.")
variable(:t)
else
throw(e)
end
end
end
function parse_default(mod, a)
a = Base.remove_linenums!(deepcopy(a))
MLStyle.@match a begin
Expr(:block, x) => parse_default(mod, x)
Expr(:tuple, x, y) => begin
def, _ = parse_default(mod, x)
meta = parse_metadata(mod, y)
(def, meta)
end
::Symbol || ::Number => (a, nothing)
Expr(:call, a...) => begin
def = parse_default.(Ref(mod), a)
expr = Expr(:call)
for (d, _) in def
push!(expr.args, d)
end
(expr, nothing)
end
Expr(:if, condition, x, y) => (a, nothing)
Expr(:vect, x...) => begin
(a, nothing)
end
_ => error("Cannot parse default $a $(typeof(a))")
end
end
function parse_metadata(mod, a::Expr)
MLStyle.@match a begin
Expr(:vect, b...) => Dict(parse_metadata(mod, m) for m in b)
Expr(:tuple, a, b...) => parse_metadata(mod, b)
Expr(:(=), a, b) => Symbolics.option_to_metadata_type(Val(a)) => get_var(mod, b)
_ => error("Cannot parse metadata $a")
end
end
function parse_metadata(mod, metadata::AbstractArray)
ret = Dict()
for m in metadata
merge!(ret, parse_metadata(mod, m))
end
ret
end
function _set_var_metadata!(metadata_with_exprs, a, m, v::Expr)
push!(metadata_with_exprs, m => v)
a
end
function _set_var_metadata!(metadata_with_exprs, a, m, v)
wrap(set_scalar_metadata(unwrap(a), m, v))
end
function set_var_metadata(a, ms)
metadata_with_exprs = Dict{DataType, Expr}()
for (m, v) in ms
if m == VariableGuess && v isa Symbol
v = quote
$v
end
end
a = _set_var_metadata!(metadata_with_exprs, a, m, v)
end
a, metadata_with_exprs
end
function get_var(mod::Module, b)
if b isa Symbol
isdefined(mod, b) && return getproperty(mod, b)
isdefined(@__MODULE__, b) && return getproperty(@__MODULE__, b)
end
b
end
function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
cons, costs, dict, mod, arg, kwargs, where_types)
mname = arg.args[1]
body = arg.args[end]
if mname == Symbol("@description")
parse_description!(body, dict)
elseif mname == Symbol("@components")
parse_components!(exprs, comps, dict, body, kwargs)
elseif mname == Symbol("@extend")
parse_extend!(exprs, ext, dict, mod, body, kwargs)
elseif mname == Symbol("@variables")
parse_variables!(exprs, vs, dict, mod, body, :variables, kwargs, where_types)
elseif mname == Symbol("@parameters")
parse_variables!(exprs, ps, dict, mod, body, :parameters, kwargs, where_types)
elseif mname == Symbol("@structural_parameters")
parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs)
elseif mname == Symbol("@equations")
parse_equations!(exprs, eqs, dict, body)
elseif mname == Symbol("@constants")
parse_variables!(exprs, ps, dict, mod, body, :constants, kwargs, where_types)
elseif mname == Symbol("@continuous_events")
parse_continuous_events!(c_evts, dict, body)
elseif mname == Symbol("@discrete_events")
parse_discrete_events!(d_evts, dict, body)
elseif mname == Symbol("@icon")
isassigned(icon) && error("This model has more than one icon.")
parse_icon!(body, dict, icon, mod)
elseif mname == Symbol("@defaults")
parse_system_defaults!(exprs, arg, dict)
elseif mname == Symbol("@constraints")
parse_costs!(cons, dict, body)
elseif mname == Symbol("@costs")
parse_constraints!(costs, dict, body)
elseif mname == Symbol("@consolidate")
parse_consolidate!(body, dict)
else
error("$mname is not handled.")
end
end
push_additional_defaults!(dict, a, b::Number) = dict[:defaults][a] = b
push_additional_defaults!(dict, a, b::QuoteNode) = dict[:defaults][a] = b.value
function push_additional_defaults!(dict, a, b::Expr)
dict[:defaults][a] = readable_code(b)
end
function parse_system_defaults!(exprs, defaults_body, dict)
for default_arg in defaults_body.args[end].args
# for arg in default_arg.args
MLStyle.@match default_arg begin
# For cases like `p => 1` and `p => f()`. In both cases the definitions of
# `a`, here `p` and when `b` is a function, here `f` are available while
# defining the model
Expr(:call, :(=>), a, b) => begin
push!(exprs, :(defaults[$a] = $b))
push_additional_defaults!(dict, a, b)
end
_ => error("Invalid `@defaults` entry: `$default_arg`")
end
end
end
function parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs)
Base.remove_linenums!(body)
for arg in body.args
MLStyle.@match arg begin
Expr(:(=), Expr(:(::), a, type), b) => begin
type = getfield(mod, type)
b = _type_check!(get_var(mod, b), a, type, :structural_parameters)
push!(sps, a)
push!(kwargs, Expr(:kw, Expr(:(::), a, type), b))
dict[:structural_parameters][a] = dict[:kwargs][a] = Dict(
:value => b, :type => type)
end
Expr(:(=), a, b) => begin
push!(sps, a)
push!(kwargs, Expr(:kw, a, b))
dict[:structural_parameters][a] = dict[:kwargs][a] = Dict(:value => b)
end
a => begin
push!(sps, a)
push!(kwargs, a)
dict[:structural_parameters][a] = dict[:kwargs][a] = Dict(:value => nothing)
end
end
end
end
function extend_args!(a, b, dict, expr, kwargs, has_param = false)
# Whenever `b` is a function call, skip the first arg aka the function name.
# Whenever it is a kwargs list, include it.
start = b.head == :call ? 2 : 1
for i in start:lastindex(b.args)
arg = b.args[i]
arg isa LineNumberNode && continue
MLStyle.@match arg begin
x::Symbol => begin
if b.head != :parameters
if has_param
popat!(b.args, i)
push!(b.args[2].args, x)
else
b.args[i] = Expr(:parameters, x)
end
end
push!(kwargs, Expr(:kw, x, nothing))
dict[:kwargs][x] = Dict(:value => nothing)
end
Expr(:kw, x) => begin
b.args[i] = Expr(:kw, x, x)
push!(kwargs, Expr(:kw, x, nothing))
dict[:kwargs][x] = Dict(:value => nothing)
end
Expr(:kw, x, y) => begin
b.args[i] = Expr(:kw, x, x)
push!(kwargs, Expr(:kw, x, y))
dict[:kwargs][x] = Dict(:value => y)
end
Expr(:parameters, x...) => begin
has_param = true
extend_args!(a, arg, dict, expr, kwargs, has_param)
end
_ => error("Could not parse $arg of component $a")
end
end
end
const EMPTY_DICT = Dict()
const EMPTY_VoVoSYMBOL = Vector{Symbol}[]
const EMPTY_VoVoVoSYMBOL = Vector{Symbol}[[]]
function _arguments(model::Model)
vars = keys(get(model.structure, :variables, EMPTY_DICT))
vars = union(vars, keys(get(model.structure, :parameters, EMPTY_DICT)))
vars = union(vars, first(get(model.structure, :extend, EMPTY_VoVoVoSYMBOL)))
collect(vars)
end
function Base.names(model::Model)
collect(union(_arguments(model),
map(first, get(model.structure, :components, EMPTY_VoVoSYMBOL))))
end
function _parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist)
extend_args!(a, b, dict, expr, kwargs)
# `implicit_arglist` doubles as a flag to check the mode of `@extend`. It is
# `nothing` for explicit destructuring.
# The following block modifies the arguments of both base and higher systems
# for the implicit extend statements.
if implicit_arglist !== nothing
b.args = [b.args[1]]
push!(b.args, Expr(:parameters))
for var in implicit_arglist.args
push!(b.args[end].args, var)
if !haskey(dict[:kwargs], var)
push!(dict[:kwargs], var => Dict(:value => NO_VALUE))
push!(kwargs, Expr(:kw, var, NO_VALUE))
end
end
end
push!(ext, a)
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
push!(expr.args, :($a = $b))
if !haskey(dict, :extend)
dict[:extend] = [Symbol.(vars.args), a, b.args[1]]
else
push!(dict[:extend][1], Symbol.(vars.args)...)
dict[:extend][2] = vcat(dict[:extend][2], a)
dict[:extend][3] = vcat(dict[:extend][3], b.args[1])
end
push!(expr.args, :(@unpack $vars = $a))
end
function parse_extend!(exprs, ext, dict, mod, body, kwargs)
expr = Expr(:block)
push!(exprs, expr)
body = deepcopy(body)
MLStyle.@match body begin
Expr(:(=), a, b) => begin
if Meta.isexpr(b, :(=))
vars = a
if !Meta.isexpr(vars, :tuple)
error("`@extend` destructuring only takes an tuple as LHS. Got $body")
end
a, b = b.args
# This doubles as a flag to identify the mode of `@extend`
implicit_arglist = nothing
_parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist)
else
error("When explicitly destructing in `@extend` please use the syntax: `@extend a, b = oneport = OnePort()`.")
end
end
Expr(:call, a′, _...) => begin
a = Symbol(Symbol("#mtkmodel"), :__anonymous__, a′)
b = body
if (model = getproperty(mod, b.args[1])) isa Model
vars = Expr(:tuple)
append!(vars.args, names(model))
implicit_arglist = Expr(:tuple)
append!(implicit_arglist.args, _arguments(model))
append!(implicit_arglist.args,
keys(get(model.structure, :structural_parameters, EMPTY_DICT)))
_parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist)
else
error("Cannot infer the exact `Model` that `@extend $(body)` refers." *
" Please specify the names that it brings into scope by:" *
" `@extend a, b = oneport = OnePort()`.")
end
end
_ => error("`@extend` only takes an assignment expression. Got $body")
end
return nothing
end
function parse_variable_arg!(exprs, vs, dict, mod, arg, varclass, kwargs, where_types)
name, ex = parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types)
push!(vs, name)
push!(exprs, ex)
end
function convert_units(varunits::DynamicQuantities.Quantity, value)
DynamicQuantities.ustrip(DynamicQuantities.uconvert(
DynamicQuantities.SymbolicUnits.as_quantity(varunits), value))
end
convert_units(::DynamicQuantities.Quantity, value::NoValue) = NO_VALUE
function convert_units(
varunits::DynamicQuantities.Quantity, value::AbstractArray{T}) where {T}
DynamicQuantities.ustrip.(DynamicQuantities.uconvert.(
DynamicQuantities.SymbolicUnits.as_quantity(varunits), value))
end
function convert_units(varunits::Unitful.FreeUnits, value)
Unitful.ustrip(varunits, value)
end
convert_units(::Unitful.FreeUnits, value::NoValue) = NO_VALUE
function convert_units(varunits::Unitful.FreeUnits, value::AbstractArray{T}) where {T}
Unitful.ustrip.(varunits, value)
end
convert_units(::Unitful.FreeUnits, value::Num) = value
convert_units(::DynamicQuantities.Quantity, value::Num) = value
function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types)
vv, def, metadata_with_exprs = parse_variable_def!(
dict, mod, arg, varclass, kwargs, where_types)
if !(vv isa Tuple)
name = getname(vv)
varexpr = if haskey(metadata_with_exprs, VariableUnit)
unit = metadata_with_exprs[VariableUnit]
quote
$name = if $name === $NO_VALUE
$setdefault($vv, $def)
else
try
$setdefault($vv, $convert_units($unit, $name))
catch e
if isa(e, $(DynamicQuantities.DimensionError)) ||
isa(e, $(Unitful.DimensionError))
error("Unable to convert units for \'" * string(:($$vv)) * "\'")
elseif isa(e, MethodError)
error("No or invalid units provided for \'" * string(:($$vv)) *
"\'")
else
rethrow(e)
end
end
end
end
else
quote
$name = if $name === $NO_VALUE
$setdefault($vv, $def)
else
$setdefault($vv, $name)
end
end
end
metadata_expr = Expr(:block)
for (k, v) in metadata_with_exprs
push!(metadata_expr.args,
:($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v))))
end
push!(varexpr.args, metadata_expr)
return symbolic_type(vv) == ScalarSymbolic() ? name : :($name...), varexpr
else
return vv
end
end
function handle_conditional_vars!(
arg, conditional_branch, mod, varclass, kwargs, where_types)
conditional_dict = Dict(:kwargs => Dict(),
:parameters => Any[Dict{Symbol, Dict{Symbol, Any}}()],
:constants => Any[Dict{Symbol, Dict{Symbol, Any}}()],
:variables => Any[Dict{Symbol, Dict{Symbol, Any}}()])
for _arg in arg.args
name, ex = parse_variable_arg(
conditional_dict, mod, _arg, varclass, kwargs, where_types)
push!(conditional_branch.args, ex)
push!(conditional_branch.args, :(push!($varclass, $name)))
end
conditional_dict
end
function prune_conditional_dict!(conditional_tuple::Tuple)
prune_conditional_dict!.(collect(conditional_tuple))
end
function prune_conditional_dict!(conditional_dict::Dict)
for k in [:parameters, :variables, :constants]
length(conditional_dict[k]) == 1 && isempty(first(conditional_dict[k])) &&
delete!(conditional_dict, k)
end
isempty(conditional_dict[:kwargs]) && delete!(conditional_dict, :kwargs)
end
prune_conditional_dict!(_) = return nothing
function get_conditional_dict!(conditional_dict, conditional_y_tuple::Tuple)
k = get_conditional_dict!.(Ref(conditional_dict), collect(conditional_y_tuple))
push_something!(conditional_dict,
k...)
conditional_dict
end
function get_conditional_dict!(conditional_dict::Dict, conditional_y_tuple::Dict)
merge!(conditional_dict[:kwargs], conditional_y_tuple[:kwargs])
for key in [:parameters, :variables, :constants]
merge!(conditional_dict[key][1], conditional_y_tuple[key][1])
end
conditional_dict
end
get_conditional_dict!(a, b) = (return nothing)
function push_conditional_dict!(dict, condition, conditional_dict,
conditional_y_tuple, varclass)
vd = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
for k in keys(conditional_dict[varclass][1])
vd[k] = copy(conditional_dict[varclass][1][k])
vd[k][:condition] = (:if, condition, conditional_dict, conditional_y_tuple)
end
conditional_y_dict = Dict(:kwargs => Dict(),
:parameters => Any[Dict{Symbol, Dict{Symbol, Any}}()],
:constants => Any[Dict{Symbol, Dict{Symbol, Any}}()],
:variables => Any[Dict{Symbol, Dict{Symbol, Any}}()])
get_conditional_dict!(conditional_y_dict, conditional_y_tuple)
prune_conditional_dict!(conditional_y_dict)
prune_conditional_dict!(conditional_dict)
!isempty(conditional_y_dict) && for k in keys(conditional_y_dict[varclass][1])
vd[k] = copy(conditional_y_dict[varclass][1][k])
vd[k][:condition] = (:if, condition, conditional_dict, conditional_y_tuple)
end
end
function parse_variables!(exprs, vs, dict, mod, body, varclass, kwargs, where_types)
expr = Expr(:block)
push!(exprs, expr)
for arg in body.args
arg isa LineNumberNode && continue
MLStyle.@match arg begin
Expr(:if, condition, x) => begin