@@ -193,7 +193,8 @@ function SymbolicIndexingInterface.is_variable(sys::AbstractSystem, sym)
193
193
h = getsymbolhash (sym)
194
194
return haskey (ic. unknown_idx, h) ||
195
195
haskey (ic. unknown_idx, getsymbolhash (default_toterm (sym))) ||
196
- hasname (sym) && is_variable (sys, getname (sym))
196
+ (istree (sym) && operation (sym) === getindex &&
197
+ is_variable (sys, first (arguments (sym))))
197
198
else
198
199
return any (isequal (sym), variable_symbols (sys)) ||
199
200
hasname (sym) && is_variable (sys, getname (sym))
@@ -214,18 +215,15 @@ function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym)
214
215
if has_index_cache (sys) && get_index_cache (sys) != = nothing
215
216
ic = get_index_cache (sys)
216
217
h = getsymbolhash (sym)
217
- return if haskey (ic. unknown_idx, h)
218
- ic. unknown_idx[h]
219
- else
220
- h = getsymbolhash (default_toterm (sym))
221
- if haskey (ic. unknown_idx, h)
222
- ic. unknown_idx[h]
223
- elseif hasname (sym)
224
- variable_index (sys, getname (sym))
225
- else
226
- nothing
227
- end
228
- end
218
+ haskey (ic. unknown_idx, h) && return ic. unknown_idx[h]
219
+
220
+ h = getsymbolhash (default_toterm (sym))
221
+ haskey (ic. unknown_idx, h) && return ic. unknown_idx[h]
222
+ sym = unwrap (sym)
223
+ istree (sym) && operation (sym) === getindex || return nothing
224
+ idx = variable_index (sys, first (arguments (sym)))
225
+ idx === nothing && return nothing
226
+ return idx[arguments (sym)[(begin + 1 ): end ]. .. ]
229
227
end
230
228
idx = findfirst (isequal (sym), variable_symbols (sys))
231
229
if idx === nothing && hasname (sym)
@@ -264,8 +262,7 @@ function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym)
264
262
else
265
263
h = getsymbolhash (default_toterm (sym))
266
264
haskey (ic. param_idx, h) || haskey (ic. discrete_idx, h) ||
267
- haskey (ic. constant_idx, h) || haskey (ic. dependent_idx, h) ||
268
- hasname (sym) && is_parameter (sys, getname (sym))
265
+ haskey (ic. constant_idx, h) || haskey (ic. dependent_idx, h)
269
266
end
270
267
end
271
268
return any (isequal (sym), parameter_symbols (sys)) ||
@@ -286,27 +283,12 @@ function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym)
286
283
if has_index_cache (sys) && get_index_cache (sys) != = nothing
287
284
ic = get_index_cache (sys)
288
285
h = getsymbolhash (sym)
289
- return if haskey (ic. param_idx, h)
290
- ParameterIndex (SciMLStructures. Tunable (), ic. param_idx[h])
291
- elseif haskey (ic. discrete_idx, h)
292
- ParameterIndex (SciMLStructures. Discrete (), ic. discrete_idx[h])
293
- elseif haskey (ic. constant_idx, h)
294
- ParameterIndex (SciMLStructures. Constants (), ic. constant_idx[h])
295
- elseif haskey (ic. dependent_idx, h)
296
- ParameterIndex (nothing , ic. dependent_idx[h])
286
+ return if (idx = ParameterIndex (ic, sym)) != = nothing
287
+ idx
288
+ elseif (idx = ParameterIndex (ic, default_toterm (sym))) != = nothing
289
+ idx
297
290
else
298
- h = getsymbolhash (default_toterm (sym))
299
- if haskey (ic. param_idx, h)
300
- ParameterIndex (SciMLStructures. Tunable (), ic. param_idx[h])
301
- elseif haskey (ic. discrete_idx, h)
302
- ParameterIndex (SciMLStructures. Discrete (), ic. discrete_idx[h])
303
- elseif haskey (ic. constant_idx, h)
304
- ParameterIndex (SciMLStructures. Constants (), ic. constant_idx[h])
305
- elseif haskey (ic. dependent_idx, h)
306
- ParameterIndex (nothing , ic. dependent_idx[h])
307
- else
308
- nothing
309
- end
291
+ nothing
310
292
end
311
293
end
312
294
@@ -329,7 +311,7 @@ function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym::Sym
329
311
end
330
312
331
313
function SymbolicIndexingInterface. parameter_symbols (sys:: AbstractSystem )
332
- return parameters (sys)
314
+ return full_parameters (sys)
333
315
end
334
316
335
317
function SymbolicIndexingInterface. is_independent_variable (sys:: AbstractSystem , sym)
@@ -419,6 +401,7 @@ for prop in [:eqs
419
401
:metadata
420
402
:gui_metadata
421
403
:discrete_subsystems
404
+ :parameter_dependencies
422
405
:solved_unknowns
423
406
:split_idxs
424
407
:parent
@@ -703,9 +686,12 @@ function namespace_expr(O, sys, n = nameof(sys); ivs = independent_variables(sys
703
686
# metadata from the rescoped variable
704
687
rescoped = renamespace (n, O)
705
688
similarterm (O, operation (rescoped), renamed,
706
- metadata = metadata (rescoped)):: T
689
+ metadata = metadata (rescoped))
690
+ elseif Symbolics. isarraysymbolic (O)
691
+ # promote_symtype doesn't work for array symbolics
692
+ similarterm (O, operation (O), renamed, symtype (O), metadata = metadata (O))
707
693
else
708
- similarterm (O, operation (O), renamed, metadata = metadata (O)):: T
694
+ similarterm (O, operation (O), renamed, metadata = metadata (O))
709
695
end
710
696
elseif isvariable (O)
711
697
renamespace (n, O)
@@ -747,7 +733,29 @@ function parameters(sys::AbstractSystem)
747
733
ps = first .(ps)
748
734
end
749
735
systems = get_systems (sys)
750
- unique (isempty (systems) ? ps : [ps; reduce (vcat, namespace_parameters .(systems))])
736
+ result = unique (isempty (systems) ? ps :
737
+ [ps; reduce (vcat, namespace_parameters .(systems))])
738
+ if has_parameter_dependencies (sys) &&
739
+ (pdeps = get_parameter_dependencies (sys)) != = nothing
740
+ filter (result) do sym
741
+ ! haskey (pdeps, sym)
742
+ end
743
+ else
744
+ result
745
+ end
746
+ end
747
+
748
+ function dependent_parameters (sys:: AbstractSystem )
749
+ if has_parameter_dependencies (sys) &&
750
+ (pdeps = get_parameter_dependencies (sys)) != = nothing
751
+ collect (keys (pdeps))
752
+ else
753
+ []
754
+ end
755
+ end
756
+
757
+ function full_parameters (sys:: AbstractSystem )
758
+ vcat (parameters (sys), dependent_parameters (sys))
751
759
end
752
760
753
761
# required in `src/connectors.jl:437`
@@ -1518,13 +1526,12 @@ function linearization_function(sys::AbstractSystem, inputs,
1518
1526
sys = ssys
1519
1527
x0 = merge (defaults (sys), Dict (missing_variable_defaults (sys)), op)
1520
1528
u0, _p, _ = get_u0_p (sys, x0, p; use_union = false , tofloat = true )
1529
+ ps = parameters (sys)
1521
1530
if has_index_cache (sys) && get_index_cache (sys) != = nothing
1522
1531
p = MTKParameters (sys, p)
1523
- ps = reorder_parameters (sys, parameters (sys))
1524
1532
else
1525
1533
p = _p
1526
1534
p, split_idxs = split_parameters_by_type (p)
1527
- ps = parameters (sys)
1528
1535
if p isa Tuple
1529
1536
ps = Base. Fix1 (getindex, ps).(split_idxs)
1530
1537
ps = (ps... ,) # if p is Tuple, ps should be Tuple
@@ -1610,7 +1617,7 @@ function linearize_symbolic(sys::AbstractSystem, inputs,
1610
1617
kwargs... )
1611
1618
sts = unknowns (sys)
1612
1619
t = get_iv (sys)
1613
- ps = parameters (sys)
1620
+ ps = full_parameters (sys)
1614
1621
p = reorder_parameters (sys, ps)
1615
1622
1616
1623
fun = generate_function (sys, sts, ps; expression = Val{false })[1 ]
@@ -2121,3 +2128,17 @@ function Symbolics.substitute(sys::AbstractSystem, rules::Union{Vector{<:Pair},
2121
2128
error (" substituting symbols is not supported for $(typeof (sys)) " )
2122
2129
end
2123
2130
end
2131
+
2132
+ function process_parameter_dependencies (pdeps, ps)
2133
+ pdeps === nothing && return pdeps, ps
2134
+ if pdeps isa Vector && eltype (pdeps) <: Pair
2135
+ pdeps = Dict (pdeps)
2136
+ elseif ! (pdeps isa Dict)
2137
+ error (" parameter_dependencies must be a `Dict` or `Vector{<:Pair}`" )
2138
+ end
2139
+
2140
+ ps = filter (ps) do p
2141
+ ! haskey (pdeps, p)
2142
+ end
2143
+ return pdeps, ps
2144
+ end
0 commit comments