@@ -242,25 +242,42 @@ function wrap_array_vars(
242
242
# tunables are scalarized and concatenated, so we need to have assignments
243
243
# for the non-scalarized versions
244
244
array_tunables = Dict {Any, AbstractArray{Int}} ()
245
- for p in ps
246
- idx = parameter_index (sys, p)
247
- idx isa ParameterIndex || continue
248
- idx. portion isa SciMLStructures. Tunable || continue
249
- idx. idx isa AbstractArray || continue
250
- array_tunables[p] = idx. idx
251
- end
252
245
# Other parameters may be scalarized arrays but used in the vector form
253
- other_array_parameters = Assignment[]
246
+ other_array_parameters = Dict {Any, Any} ()
247
+
254
248
for p in ps
249
+ p = unwrap (p)
250
+ if iscall (p) && operation (p) == getindex
251
+ p = arguments (p)[1 ]
252
+ end
253
+ symtype (p) <: AbstractArray && Symbolics. shape (p) != Symbolics. Unknown () || continue
254
+ scal = collect (p)
255
+ # all scalarized variables are in `ps`
256
+ all (x -> any (isequal (x), ps), scal) || continue
257
+ (haskey (array_tunables, p) || haskey (other_array_parameters, p)) && continue
258
+
255
259
idx = parameter_index (sys, p)
256
- if Symbolics. isarraysymbolic (p)
257
- idx === nothing || continue
258
- push! (other_array_parameters, p ← collect (p))
259
- elseif iscall (p) && operation (p) == getindex
260
- idx === nothing && continue
261
- # all of the scalarized variables are in `ps`
262
- all (x -> any (isequal (x), ps), collect (p))|| continue
263
- push! (other_array_parameters, p ← collect (p))
260
+ if idx === nothing
261
+ idxs = map (Base. Fix1 (parameter_index, sys), scal)
262
+ if all (x -> x isa ParameterIndex && x. portion isa SciMLStructures. Tunable, idxs)
263
+ idxs = map (x -> x. idx, idxs)
264
+ end
265
+ if all (x -> x isa Int, idxs)
266
+ if vec (idxs) == idxs[begin ]: idxs[end ]
267
+ idxs = reshape (idxs[begin ]: idxs[end ], size (idxs))
268
+ elseif vec (idxs) == idxs[begin ]: - 1 : idxs[end ]
269
+ idxs = reshape (idxs[begin ]: - 1 : idxs[end ], size (idxs))
270
+ end
271
+ array_tunables[p] = idxs
272
+ else
273
+ other_array_parameters[p] = scal
274
+ end
275
+ elseif idx isa Int
276
+ continue
277
+ elseif idx. portion != SciMLStructures. Tunable ()
278
+ other_array_parameters[p] = scal
279
+ else
280
+ array_tunables[p] = idx. idx
264
281
end
265
282
end
266
283
for (k, inds) in array_vars
@@ -278,7 +295,8 @@ function wrap_array_vars(
278
295
[k ← :(view ($ (expr. args[uind]. name), $ v)) for (k, v) in array_vars],
279
296
[k ← :(view ($ (expr. args[uind + 1 ]. name), $ v))
280
297
for (k, v) in array_tunables],
281
- other_array_parameters
298
+ [k ← Code. MakeArray (v, typeof (v))
299
+ for (k, v) in other_array_parameters]
282
300
),
283
301
expr. body,
284
302
false
@@ -294,7 +312,9 @@ function wrap_array_vars(
294
312
vcat (
295
313
[k ← :(view ($ (expr. args[uind]. name), $ v)) for (k, v) in array_vars],
296
314
[k ← :(view ($ (expr. args[uind + 1 ]. name), $ v))
297
- for (k, v) in array_tunables]
315
+ for (k, v) in array_tunables],
316
+ [k ← Code. MakeArray (v, typeof (v))
317
+ for (k, v) in other_array_parameters]
298
318
),
299
319
expr. body,
300
320
false
@@ -310,7 +330,9 @@ function wrap_array_vars(
310
330
[k ← :(view ($ (expr. args[uind + 1 ]. name), $ v))
311
331
for (k, v) in array_vars],
312
332
[k ← :(view ($ (expr. args[uind + 2 ]. name), $ v))
313
- for (k, v) in array_tunables]
333
+ for (k, v) in array_tunables],
334
+ [k ← Code. MakeArray (v, typeof (v))
335
+ for (k, v) in other_array_parameters]
314
336
),
315
337
expr. body,
316
338
false
@@ -499,15 +521,18 @@ function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym)
499
521
return unwrap (sym) in 1 : length (parameter_symbols (sys))
500
522
end
501
523
return any (isequal (sym), parameter_symbols (sys)) ||
502
- hasname (sym) && is_parameter (sys, getname (sym))
524
+ hasname (sym) && ! (iscall (sym) && operation (sym) == getindex) &&
525
+ is_parameter (sys, getname (sym))
503
526
end
504
527
505
528
function SymbolicIndexingInterface. is_parameter (sys:: AbstractSystem , sym:: Symbol )
506
529
if has_index_cache (sys) && (ic = get_index_cache (sys)) != = nothing
507
530
return is_parameter (ic, sym)
508
531
end
509
532
510
- named_parameters = [getname (sym) for sym in parameter_symbols (sys) if hasname (sym)]
533
+ named_parameters = [getname (x)
534
+ for x in parameter_symbols (sys)
535
+ if hasname (x) && ! (iscall (x) && operation (x) == getindex)]
511
536
return any (isequal (sym), named_parameters) ||
512
537
count (NAMESPACE_SEPARATOR, string (sym)) == 1 &&
513
538
count (isequal (sym),
@@ -543,7 +568,7 @@ function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym)
543
568
return sym
544
569
end
545
570
idx = findfirst (isequal (sym), parameter_symbols (sys))
546
- if idx === nothing && hasname (sym)
571
+ if idx === nothing && hasname (sym) && ! ( iscall (sym) && operation (sym) == getindex)
547
572
idx = parameter_index (sys, getname (sym))
548
573
end
549
574
return idx
@@ -559,13 +584,16 @@ function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym::Sym
559
584
return idx
560
585
end
561
586
end
562
- idx = findfirst (isequal (sym), getname .(parameter_symbols (sys)))
587
+ pnames = [getname (x)
588
+ for x in parameter_symbols (sys)
589
+ if hasname (x) && ! (iscall (x) && operation (x) == getindex)]
590
+ idx = findfirst (isequal (sym), pnames)
563
591
if idx != = nothing
564
592
return idx
565
593
elseif count (NAMESPACE_SEPARATOR, string (sym)) == 1
566
594
return findfirst (isequal (sym),
567
595
Symbol .(
568
- nameof (sys), NAMESPACE_SEPARATOR_SYMBOL, getname .( parameter_symbols (sys)) ))
596
+ nameof (sys), NAMESPACE_SEPARATOR_SYMBOL, pnames ))
569
597
end
570
598
return nothing
571
599
end
0 commit comments