21
21
22
22
ParameterIndex (portion, idx) = ParameterIndex (portion, idx, false )
23
23
24
- const ParamIndexMap = Dict{Union{Symbol, BasicSymbolic} , Tuple{Int, Int}}
24
+ const ParamIndexMap = Dict{BasicSymbolic, Tuple{Int, Int}}
25
25
const UnknownIndexMap = Dict{
26
- Union{Symbol, BasicSymbolic} , Union{Int, UnitRange{Int}, AbstractArray{Int}}}
26
+ BasicSymbolic, Union{Int, UnitRange{Int}, AbstractArray{Int}}}
27
27
28
28
struct IndexCache
29
29
unknown_idx:: UnknownIndexMap
30
- discrete_idx:: Dict{Union{Symbol, BasicSymbolic} , Tuple{Int, Int, Int}}
30
+ discrete_idx:: Dict{BasicSymbolic, Tuple{Int, Int, Int}}
31
31
tunable_idx:: ParamIndexMap
32
32
constant_idx:: ParamIndexMap
33
33
dependent_idx:: ParamIndexMap
34
34
nonnumeric_idx:: ParamIndexMap
35
- observed_syms:: Set{Union{Symbol, BasicSymbolic} }
35
+ observed_syms:: Set{BasicSymbolic}
36
36
discrete_buffer_sizes:: Vector{Vector{BufferTemplate}}
37
37
tunable_buffer_sizes:: Vector{BufferTemplate}
38
38
constant_buffer_sizes:: Vector{BufferTemplate}
@@ -57,14 +57,6 @@ function IndexCache(sys::AbstractSystem)
57
57
end
58
58
unk_idxs[usym] = sym_idx
59
59
unk_idxs[rsym] = sym_idx
60
- if hasname (sym) && (! iscall (sym) || operation (sym) != = getindex)
61
- name = getname (usym)
62
- rname = getname (rsym)
63
- unk_idxs[name] = sym_idx
64
- unk_idxs[rname] = sym_idx
65
- symbol_to_variable[name] = sym
66
- symbol_to_variable[rname] = sym
67
- end
68
60
idx += length (sym)
69
61
end
70
62
for sym in unks
@@ -80,14 +72,6 @@ function IndexCache(sys::AbstractSystem)
80
72
rsym = renamespace (sys, arrsym)
81
73
unk_idxs[arrsym] = idxs
82
74
unk_idxs[rsym] = idxs
83
- if hasname (arrsym)
84
- name = getname (arrsym)
85
- rname = getname (rsym)
86
- unk_idxs[name] = idxs
87
- unk_idxs[rname] = idxs
88
- symbol_to_variable[name] = arrsym
89
- symbol_to_variable[rname] = arrsym
90
- end
91
75
end
92
76
end
93
77
@@ -102,16 +86,6 @@ function IndexCache(sys::AbstractSystem)
102
86
push! (observed_syms, ttsym)
103
87
push! (observed_syms, rsym)
104
88
push! (observed_syms, rttsym)
105
- if hasname (sym) && (! iscall (sym) || operation (sym) != = getindex)
106
- symbol_to_variable[getname (sym)] = eq. lhs
107
- symbol_to_variable[getname (ttsym)] = eq. lhs
108
- symbol_to_variable[getname (rsym)] = eq. lhs
109
- symbol_to_variable[getname (rttsym)] = eq. lhs
110
- push! (observed_syms, getname (sym))
111
- push! (observed_syms, getname (ttsym))
112
- push! (observed_syms, getname (rsym))
113
- push! (observed_syms, getname (rttsym))
114
- end
115
89
end
116
90
end
117
91
@@ -143,16 +117,12 @@ function IndexCache(sys::AbstractSystem)
143
117
rttinp = renamespace (sys, ttinp)
144
118
is_parameter (sys, inp) ||
145
119
error (" Discrete subsystem $i input $inp is not a parameter" )
120
+
146
121
disc_clocks[inp] = i
147
122
disc_clocks[ttinp] = i
148
123
disc_clocks[rinp] = i
149
124
disc_clocks[rttinp] = i
150
- if hasname (inp) && (! iscall (inp) || operation (inp) != = getindex)
151
- disc_clocks[getname (inp)] = i
152
- disc_clocks[getname (ttinp)] = i
153
- disc_clocks[getname (rinp)] = i
154
- disc_clocks[getname (rttinp)] = i
155
- end
125
+
156
126
insert_by_type! (disc_buffers[i], inp)
157
127
end
158
128
@@ -163,16 +133,12 @@ function IndexCache(sys::AbstractSystem)
163
133
rttsym = renamespace (sys, ttsym)
164
134
is_parameter (sys, sym) ||
165
135
error (" Discrete subsystem $i unknown $sym is not a parameter" )
136
+
166
137
disc_clocks[sym] = i
167
138
disc_clocks[ttsym] = i
168
139
disc_clocks[rsym] = i
169
140
disc_clocks[rttsym] = i
170
- if hasname (sym) && (! iscall (sym) || operation (sym) != = getindex)
171
- disc_clocks[getname (sym)] = i
172
- disc_clocks[getname (ttsym)] = i
173
- disc_clocks[getname (rsym)] = i
174
- disc_clocks[getname (rttsym)] = i
175
- end
141
+
176
142
insert_by_type! (disc_buffers[i], sym)
177
143
end
178
144
t = get_iv (sys)
@@ -191,12 +157,6 @@ function IndexCache(sys::AbstractSystem)
191
157
disc_clocks[ttsym] = i
192
158
disc_clocks[rsym] = i
193
159
disc_clocks[rttsym] = i
194
- if hasname (sym) && (! iscall (sym) || operation (sym) != = getindex)
195
- disc_clocks[getname (sym)] = i
196
- disc_clocks[getname (ttsym)] = i
197
- disc_clocks[getname (rsym)] = i
198
- disc_clocks[getname (rttsym)] = i
199
- end
200
160
end
201
161
end
202
162
@@ -237,13 +197,7 @@ function IndexCache(sys::AbstractSystem)
237
197
disc_clocks[ttsym] = user_affect_clock
238
198
disc_clocks[rsym] = user_affect_clock
239
199
disc_clocks[rttsym] = user_affect_clock
240
- if hasname (sym) &&
241
- (! iscall (sym) || operation (sym) != = getindex)
242
- disc_clocks[getname (sym)] = user_affect_clock
243
- disc_clocks[getname (ttsym)] = user_affect_clock
244
- disc_clocks[getname (rsym)] = user_affect_clock
245
- disc_clocks[getname (rttsym)] = user_affect_clock
246
- end
200
+
247
201
buffer = get! (disc_buffers, user_affect_clock, Dict {Any, Set{BasicSymbolic}} ())
248
202
insert_by_type! (buffer, affect. lhs)
249
203
else
@@ -259,12 +213,7 @@ function IndexCache(sys::AbstractSystem)
259
213
disc_clocks[ttdisc] = user_affect_clock
260
214
disc_clocks[rdisc] = user_affect_clock
261
215
disc_clocks[rttdisc] = user_affect_clock
262
- if hasname (disc) && (! iscall (disc) || operation (disc) != = getindex)
263
- disc_clocks[getname (disc)] = user_affect_clock
264
- disc_clocks[getname (ttdisc)] = user_affect_clock
265
- disc_clocks[getname (rdisc)] = user_affect_clock
266
- disc_clocks[getname (rttdisc)] = user_affect_clock
267
- end
216
+
268
217
buffer = get! (
269
218
disc_buffers, user_affect_clock, Dict {Any, Set{BasicSymbolic}} ())
270
219
insert_by_type! (buffer, disc)
@@ -316,21 +265,13 @@ function IndexCache(sys::AbstractSystem)
316
265
for (j, sym) in enumerate (buffer[btype])
317
266
disc_idxs[sym] = (clockidx, i, j)
318
267
disc_idxs[default_toterm (sym)] = (clockidx, i, j)
319
- if hasname (sym) && (! iscall (sym) || operation (sym) != = getindex)
320
- disc_idxs[getname (sym)] = (clockidx, i, j)
321
- disc_idxs[getname (default_toterm (sym))] = (clockidx, i, j)
322
- end
323
268
end
324
269
end
325
270
end
326
271
for (sym, clockid) in disc_clocks
327
272
haskey (disc_idxs, sym) && continue
328
273
disc_idxs[sym] = (clockid, 0 , 0 )
329
274
disc_idxs[default_toterm (sym)] = (clockid, 0 , 0 )
330
- if hasname (sym) && (! iscall (sym) || operation (sym) != = getindex)
331
- disc_idxs[getname (sym)] = (clockid, 0 , 0 )
332
- disc_idxs[getname (default_toterm (sym))] = (clockid, 0 , 0 )
333
- end
334
275
end
335
276
336
277
function get_buffer_sizes_and_idxs (buffers:: Dict{Any, Set{BasicSymbolic}} )
@@ -345,16 +286,6 @@ function IndexCache(sys::AbstractSystem)
345
286
idxs[ttp] = (i, j)
346
287
idxs[rp] = (i, j)
347
288
idxs[rttp] = (i, j)
348
- if hasname (p) && (! iscall (p) || operation (p) != = getindex)
349
- idxs[getname (p)] = (i, j)
350
- idxs[getname (ttp)] = (i, j)
351
- idxs[getname (rp)] = (i, j)
352
- idxs[getname (rttp)] = (i, j)
353
- symbol_to_variable[getname (p)] = p
354
- symbol_to_variable[getname (ttp)] = p
355
- symbol_to_variable[getname (rp)] = p
356
- symbol_to_variable[getname (rttp)] = p
357
- end
358
289
end
359
290
push! (buffer_sizes, BufferTemplate (T, length (buf)))
360
291
end
@@ -366,6 +297,14 @@ function IndexCache(sys::AbstractSystem)
366
297
dependent_idxs, dependent_buffer_sizes = get_buffer_sizes_and_idxs (dependent_buffers)
367
298
nonnumeric_idxs, nonnumeric_buffer_sizes = get_buffer_sizes_and_idxs (nonnumeric_buffers)
368
299
300
+ for sym in Iterators. flatten ((keys (unk_idxs), keys (disc_idxs), keys (tunable_idxs),
301
+ keys (const_idxs), keys (dependent_idxs), keys (nonnumeric_idxs),
302
+ observed_syms, independent_variable_symbols (sys)))
303
+ if hasname (sym) && (! iscall (sym) || operation (sym) != = getindex)
304
+ symbol_to_variable[getname (sym)] = sym
305
+ end
306
+ end
307
+
369
308
return IndexCache (
370
309
unk_idxs,
371
310
disc_idxs,
@@ -384,18 +323,26 @@ function IndexCache(sys::AbstractSystem)
384
323
end
385
324
386
325
function SymbolicIndexingInterface. is_variable (ic:: IndexCache , sym)
387
- return check_index_map (ic . unknown_idx, sym) != = nothing
388
- end
389
-
390
- function SymbolicIndexingInterface . is_variable (ic :: IndexCache , sym :: Symbol )
326
+ if sym isa Symbol
327
+ sym = get (ic . symbol_to_variable, sym, nothing )
328
+ sym === nothing && return false
329
+ end
391
330
return check_index_map (ic. unknown_idx, sym) != = nothing
392
331
end
393
332
394
333
function SymbolicIndexingInterface. variable_index (ic:: IndexCache , sym)
334
+ if sym isa Symbol
335
+ sym = get (ic. symbol_to_variable, sym, nothing )
336
+ sym === nothing && return nothing
337
+ end
395
338
return check_index_map (ic. unknown_idx, sym)
396
339
end
397
340
398
341
function SymbolicIndexingInterface. is_parameter (ic:: IndexCache , sym)
342
+ if sym isa Symbol
343
+ sym = get (ic. symbol_to_variable, sym, nothing )
344
+ sym === nothing && return false
345
+ end
399
346
return check_index_map (ic. tunable_idx, sym) != = nothing ||
400
347
check_index_map (ic. discrete_idx, sym) != = nothing ||
401
348
check_index_map (ic. constant_idx, sym) != = nothing ||
405
352
406
353
function SymbolicIndexingInterface. parameter_index (ic:: IndexCache , sym)
407
354
if sym isa Symbol
408
- sym = ic. symbol_to_variable[sym]
355
+ sym = get (ic. symbol_to_variable, sym, nothing )
356
+ sym === nothing && return nothing
409
357
end
410
358
validate_size = Symbolics. isarraysymbolic (sym) &&
411
359
Symbolics. shape (sym) != = Symbolics. Unknown ()
@@ -425,10 +373,18 @@ function SymbolicIndexingInterface.parameter_index(ic::IndexCache, sym)
425
373
end
426
374
427
375
function SymbolicIndexingInterface. is_timeseries_parameter (ic:: IndexCache , sym)
376
+ if sym isa Symbol
377
+ sym = get (ic. symbol_to_variable, sym, nothing )
378
+ sym === nothing && return false
379
+ end
428
380
return check_index_map (ic. discrete_idx, sym) != = nothing
429
381
end
430
382
431
383
function SymbolicIndexingInterface. timeseries_parameter_index (ic:: IndexCache , sym)
384
+ if sym isa Symbol
385
+ sym = get (ic. symbol_to_variable, sym, nothing )
386
+ sym === nothing && return nothing
387
+ end
432
388
idx = check_index_map (ic. discrete_idx, sym)
433
389
idx === nothing && return nothing
434
390
clockid, partitionid... = idx
0 commit comments