-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathparameter_buffer.jl
More file actions
1187 lines (1095 loc) · 39.9 KB
/
Copy pathparameter_buffer.jl
File metadata and controls
1187 lines (1095 loc) · 39.9 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
symconvert(::Type{T}, ::Type{F}, x::V) where {T, F, V} = convert(promote_type(T, V), x)
symconvert(::Type{T}, ::Type{F}, x::V) where {T <: Real, F, V} = convert(T, x)
symconvert(::Type{Real}, ::Type{F}, x::Integer) where {F} = convert(F, x)
symconvert(::Type{V}, ::Type{F}, x) where {V <: AbstractArray, F} = symconvert.(eltype(V), F, x)
struct MTKParameters{T, I, D, C, N, H}
tunable::T
initials::I
discrete::D
constant::C
nonnumeric::N
caches::H
function MTKParameters{T, I, D, C, N, H}(
tunables::T, initials::I, discrete::D,
constant::C, nonnumeric::N,
caches::H
) where {T, I, D, C, N, H}
if tunables isa StaticVector{0}
tunables = SVector{0, eltype(tunables)}()
end
if initials isa StaticVector{0}
initials = SVector{0, eltype(initials)}()
end
return new{typeof(tunables), typeof(initials), D, C, N, H}(
tunables, initials,
discrete, constant,
nonnumeric, caches
)
end
function MTKParameters(
tunables::T, initials::I, discrete::D,
constant::C, nonnumeric::N,
caches::H
) where {T, I, D, C, N, H}
return MTKParameters{T, I, D, C, N, H}(
tunables, initials, discrete, constant,
nonnumeric, caches
)
end
end
"""
function MTKParameters(sys::AbstractSystem, p, u0 = Dict(); t0 = nothing)
Create an `MTKParameters` object for the system `sys`. `p` (`u0`) are symbolic maps from
parameters (unknowns) to their values. The values can also be symbolic expressions, which
are evaluated given the values of other parameters/unknowns. `u0` is only required if
the values of parameters depend on the unknowns. `t0` is the initial time, for time-
dependent systems. It is only required if the symbolic expressions also use the independent
variable of the system.
This requires that `complete` has been called on the system (usually via
`mtkcompile` or `@mtkcompile`) and the keyword `split = true` was passed (which is
the default behavior).
"""
function MTKParameters(
sys::AbstractSystem, op; tofloat = false,
t0 = nothing, substitution_limit = 1000, floatT = nothing,
p_constructor = identity, fast_path = false
)
ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing
get_index_cache(sys)
else
error("Cannot create MTKParameters if system does not have index_cache")
end
all_ps = Set(get_ps(sys))
diffcache_params = SU.getmetadata(sys, DiffCacheParams, Dict{SymbolicT, Int}())::Dict{SymbolicT, Int}
if !fast_path
op = operating_point_preprocess(sys, op)
if floatT === nothing
floatT = float(float_type_from_varmap(op))
end
op = build_operating_point(sys, op; fast_path = true)
bound_ps = bound_parameters(sys)
bound_ics = intersect(bound_ps, keys(op))
isempty(bound_ics) || throw(BoundInitialConditionsError(collect(bound_ics)))
binds = filter(!Base.Fix2(===, COMMON_MISSING) ∘ last, parent(bindings(sys)))
no_override_merge!(op, binds)
missing_ps = setdiff(all_ps, keys(op))
to_rm = Set{SymbolicT}()
for p in missing_ps
Moshi.Match.@match p begin
BSImpl.Term(; f, args) && if f isa Initial end => begin
op[p] = args[1]
continue
end
_ => nothing
end
arr, isarr = split_indexed_var(p)
isarr || continue
haskey(op, arr) || continue
push!(to_rm, p)
end
setdiff!(missing_ps, keys(op), to_rm, keys(diffcache_params))
isempty(missing_ps) || throw(MissingParametersError(collect(missing_ps)))
end
if floatT === nothing
floatT = float(float_type_from_varmap(op))
end
if t0 !== nothing
op[get_iv(sys)] = t0
end
# Substitute through `AADSubWrapper` so that `COMMON_NOTHING` holes in partially
# specified array values are never substituted into expressions (issue #4607).
wrapped_op = AADSubWrapper(op)
ir = get_irstructure(sys)
evaluate_varmap!(ir, wrapped_op, all_ps; limit = substitution_limit, allow_symbolic = true)
tunable_buffer = Vector{ic.tunable_buffer_size.type}(
undef, ic.tunable_buffer_size.length
)
initials_buffer = Vector{ic.initials_buffer_size.type}(
undef, ic.initials_buffer_size.length
)
disc_buffer = Tuple(
BlockedArray(
Vector{subbuffer_sizes[1].type}(
undef, sum(x -> x.length, subbuffer_sizes)
),
map(x -> x.length, subbuffer_sizes)
)
for subbuffer_sizes in ic.discrete_buffer_sizes
)
const_buffer = Tuple(
Vector{temp.type}(undef, temp.length)
for temp in ic.constant_buffer_sizes
)
nonnumeric_buffer = Tuple(
Vector{temp.type}(undef, temp.length)
for temp in ic.nonnumeric_buffer_sizes
)
function set_value(sym, val)
done = true
if haskey(ic.tunable_idx, sym)
idx = ic.tunable_idx[sym]
tunable_buffer[idx] = val
elseif haskey(ic.initials_idx, sym)
idx = ic.initials_idx[sym]
initials_buffer[idx] = val
elseif haskey(ic.discrete_idx, sym)
idx = ic.discrete_idx[sym]
disc_buffer[idx.buffer_idx][idx.idx_in_buffer] = val
elseif haskey(ic.constant_idx, sym)
i, j = ic.constant_idx[sym]
const_buffer[i][j] = val
elseif haskey(ic.nonnumeric_idx, sym)
i, j = ic.nonnumeric_idx[sym]
nonnumeric_buffer[i][j] = val
elseif !isequal(default_toterm(sym), sym)
done = set_value(default_toterm(sym), val)
else
done = false
end
return done
end
diffcaches_buffer_idx = 0
diffcache_sizes = zeros(Int, length(diffcache_params))
if !isempty(diffcache_params)
representative = first(keys(diffcache_params))
diffcaches_buffer_idx, _ = ic.nonnumeric_idx[representative]
for (param, len) in diffcache_params
_, j = ic.nonnumeric_idx[param]
diffcache_sizes[j] = len
end
end
for sym in all_ps
haskey(diffcache_params, sym) && continue
val = fixpoint_sub(sym, wrapped_op; maxiters = max(div(substitution_limit, 2), 2), fold = Val(true), warn_maxiters = false)
ctype = symtype(sym)
if !SU.isconst(val) && isinitial(sym)
# The value of an `Initial` parameter that cannot be fully evaluated
# refers to variables not fixed by the operating point. Treat such
# (elements of) the value as unfixed, mirroring the `COMMON_NOTHING`
# handling below.
if SU.is_array_shape(SU.shape(val))
val = BSImpl.Const{VartypeT}(
map(SU.stable_eachindex(val)) do i
el = val[i]
SU.isconst(el) ? unwrap_const(el) : false
end
)
else
val = BSImpl.Const{VartypeT}(false)
end
end
if !SU.isconst(val)
error(lazy"Could not evaluate value of parameter $sym. Missing values for variables in expression $val.")
end
if ctype <: FnType
ctype = fntype_to_function_type(ctype)
end
if ctype == Real && floatT !== nothing
ctype = floatT
end
if isinitial(sym)
if val === COMMON_NOTHING
val = COMMON_FALSE
elseif SU.is_array_shape(SU.shape(val)) && any(Base.Fix2(===, COMMON_NOTHING) ∘ Base.Fix1(getindex, val), SU.stable_eachindex(val))
val = map(x -> x === COMMON_NOTHING ? false : unwrap_const(x), collect(val))
end
end
val = symconvert(ctype, floatT, unwrap_const(val))
set_value(sym, val)
end
tunable_buffer = narrow_buffer_type(tunable_buffer; p_constructor)
if isempty(tunable_buffer)
tunable_buffer = SVector{0, Float64}()
end
initials_buffer = narrow_buffer_type(initials_buffer; p_constructor)
if isempty(initials_buffer)
initials_buffer = SVector{0, Float64}()
end
disc_buffer = narrow_buffer_type.(disc_buffer; p_constructor)
const_buffer = narrow_buffer_type.(const_buffer; p_constructor)
if !iszero(diffcaches_buffer_idx)
cache_elT = eltype(initials_buffer)
diffcaches_elT = DiffCacheAllocatorAPIWrapper{cache_elT}
@set! nonnumeric_buffer[diffcaches_buffer_idx] = Vector{diffcaches_elT}(nonnumeric_buffer[diffcaches_buffer_idx])
for (i, len) in enumerate(diffcache_sizes)
nonnumeric_buffer[diffcaches_buffer_idx][i] = DiffCacheAllocatorAPIWrapper(DiffCache(zeros(cache_elT, len)))
end
end
# Don't narrow nonnumeric types
if !isempty(nonnumeric_buffer)
nonnumeric_buffer = map(p_constructor, nonnumeric_buffer)
end
mtkps = MTKParameters{
typeof(tunable_buffer), typeof(initials_buffer), typeof(disc_buffer),
typeof(const_buffer), typeof(nonnumeric_buffer), typeof(()),
}(
tunable_buffer,
initials_buffer, disc_buffer, const_buffer, nonnumeric_buffer, ()
)
return mtkps
end
function rebuild_with_caches(p::MTKParameters, cache_templates::BufferTemplate...)
buffers = map(cache_templates) do template
Vector{template.type}(undef, template.length)
end
return @set p.caches = buffers
end
function narrow_buffer_type(buffer::AbstractArray; p_constructor = identity)
type = Union{}
for x in buffer
type = promote_type(type, typeof(x))
end
return p_constructor(type.(buffer))
end
function narrow_buffer_type(
buffer::AbstractArray{<:AbstractArray}; p_constructor = identity
)
type = Union{}
for arr in buffer
for x in arr
type = promote_type(type, typeof(x))
end
end
buffer = map(buffer) do buf
p_constructor(type.(buf))
end
return p_constructor(buffer)
end
function narrow_buffer_type(buffer::BlockedArray; p_constructor = identity)
if eltype(buffer) <: AbstractArray
buffer = narrow_buffer_type.(buffer; p_constructor)
end
type = Union{}
for x in buffer
type = promote_type(type, typeof(x))
end
tmp = p_constructor(type.(buffer))
blocks = ntuple(Val(ndims(buffer))) do i
bsizes = blocksizes(buffer, i)
p_constructor(Int.(bsizes))
end
return BlockedArray(tmp, blocks...)
end
function buffer_to_arraypartition(buf)
return ArrayPartition(ntuple(i -> _buffer_to_arrp_helper(buf[i]), Val(length(buf))))
end
_buffer_to_arrp_helper(v::T) where {T} = _buffer_to_arrp_helper(eltype(T), v)
_buffer_to_arrp_helper(::Type{<:AbstractArray}, v) = buffer_to_arraypartition(v)
_buffer_to_arrp_helper(::Any, v) = v
function _split_helper(buf_v::T, recurse, raw, idx) where {T}
return _split_helper(eltype(T), buf_v, recurse, raw, idx)
end
function _split_helper(::Type{<:AbstractArray}, buf_v, ::Val{N}, raw, idx) where {N}
return map(b -> _split_helper(eltype(b), b, Val(N - 1), raw, idx), buf_v)
end
function _split_helper(
::Type{<:AbstractArray}, buf_v::BlockedArray, ::Val{N}, raw, idx
) where {N}
return BlockedArray(
map(b -> _split_helper(eltype(b), b, Val(N - 1), raw, idx), buf_v),
blocksizes(buf_v, 1)
)
end
function _split_helper(::Type{<:AbstractArray}, buf_v::Tuple, ::Val{N}, raw, idx) where {N}
return ntuple(
i -> _split_helper(eltype(buf_v[i]), buf_v[i], Val(N - 1), raw, idx),
Val(length(buf_v))
)
end
function _split_helper(::Type{<:AbstractArray}, buf_v, ::Val{0}, raw, idx)
return _split_helper((), buf_v, (), raw, idx)
end
function _split_helper(_, buf_v, _, raw, idx)
res = reshape(raw[idx[]:(idx[] + length(buf_v) - 1)], size(buf_v))
idx[] += length(buf_v)
return res
end
function _split_helper(_, buf_v::BlockedArray, _, raw, idx)
res = BlockedArray(
reshape(raw[idx[]:(idx[] + length(buf_v) - 1)], size(buf_v)), blocksizes(buf_v, 1)
)
idx[] += length(buf_v)
return res
end
function split_into_buffers(raw::AbstractArray, buf, recurse = Val(1))
idx = Ref(1)
return ntuple(i -> _split_helper(buf[i], recurse, raw, idx), Val(length(buf)))
end
function _update_tuple_helper(buf_v::T, raw, idx) where {T}
return _update_tuple_helper(eltype(T), buf_v, raw, idx)
end
function _update_tuple_helper(::Type{<:AbstractArray}, buf_v, raw, idx)
return ntuple(i -> _update_tuple_helper(buf_v[i], raw, idx), length(buf_v))
end
function _update_tuple_helper(::Any, buf_v, raw, idx)
copyto!(buf_v, view(raw, idx[]:(idx[] + length(buf_v) - 1)))
idx[] += length(buf_v)
return nothing
end
function update_tuple_of_buffers(raw::AbstractArray, buf)
idx = Ref(1)
return ntuple(i -> _update_tuple_helper(buf[i], raw, idx), Val(length(buf)))
end
SciMLStructures.isscimlstructure(::MTKParameters) = true
SciMLStructures.ismutablescimlstructure(::MTKParameters) = true
function SciMLStructures.canonicalize(::SciMLStructures.Tunable, p::MTKParameters)
arr = p.tunable
repack = let p = p
function (new_val)
return SciMLStructures.replace(SciMLStructures.Tunable(), p, new_val)
end
end
return arr, repack, true
end
function SciMLStructures.replace(::SciMLStructures.Tunable, p::MTKParameters, newvals)
@set! p.tunable = newvals
return p
end
function SciMLStructures.replace!(::SciMLStructures.Tunable, p::MTKParameters, newvals)
copyto!(p.tunable, newvals)
return nothing
end
function SciMLStructures.canonicalize(::SciMLStructures.Initials, p::MTKParameters)
arr = p.initials
repack = let p = p
function (new_val)
return SciMLStructures.replace(SciMLStructures.Initials(), p, new_val)
end
end
return arr, repack, true
end
function SciMLStructures.replace(::SciMLStructures.Initials, p::MTKParameters, newvals)
@set! p.initials = newvals
return p
end
function SciMLStructures.replace!(::SciMLStructures.Initials, p::MTKParameters, newvals)
copyto!(p.initials, newvals)
return nothing
end
for (Portion, field, recurse) in [
(SciMLStructures.Discrete, :discrete, 1)
(SciMLStructures.Constants, :constant, 1)
(Nonnumeric, :nonnumeric, 1)
(SciMLStructures.Caches, :caches, 1)
]
@eval function SciMLStructures.canonicalize(::$Portion, p::MTKParameters)
as_vector = buffer_to_arraypartition(p.$field)
repack = let p = p
function (new_val)
return SciMLStructures.replace(($Portion)(), p, new_val)
end
end
return as_vector, repack, true
end
@eval function SciMLStructures.replace(::$Portion, p::MTKParameters, newvals)
@set! p.$field = split_into_buffers(newvals, p.$field, Val($recurse))
return p
end
@eval function SciMLStructures.replace!(::$Portion, p::MTKParameters, newvals)
update_tuple_of_buffers(newvals, p.$field)
return nothing
end
end
function Base.copy(p::MTKParameters)
tunable = copy(p.tunable)
initials = copy(p.initials)
discrete = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.discrete)
constant = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.constant)
nonnumeric = isempty(p.nonnumeric) ? p.nonnumeric : copy.(p.nonnumeric)
caches = isempty(p.caches) ? p.caches : copy.(p.caches)
return MTKParameters(
tunable,
initials,
discrete,
constant,
nonnumeric,
caches
)
end
function ArrayInterface.ismutable(
::Type{
MTKParameters{
T, I, D, C, N, H,
},
}
) where {T, I, D, C, N, H}
return ArrayInterface.ismutable(T) || ArrayInterface.ismutable(I) ||
any(ArrayInterface.ismutable, fieldtypes(D)) ||
any(ArrayInterface.ismutable, fieldtypes(C)) ||
any(ArrayInterface.ismutable, fieldtypes(N))
end
function SymbolicIndexingInterface.parameter_values(p::MTKParameters, pind::ParameterIndex)
return _ducktyped_parameter_values(p, pind)
end
function _ducktyped_parameter_values(p, pind::ParameterIndex)
@unpack portion, idx = pind
if portion isa SciMLStructures.Tunable
return idx isa Int ? p.tunable[idx] : view(p.tunable, idx)
end
if portion isa SciMLStructures.Initials
return idx isa Int ? p.initials[idx] : view(p.initials, idx)
end
i, j, k... = idx
if portion isa SciMLStructures.Discrete
return isempty(k) ? p.discrete[i][j] : p.discrete[i][j][k...]
elseif portion isa SciMLStructures.Constants
return isempty(k) ? p.constant[i][j] : p.constant[i][j][k...]
elseif portion === NONNUMERIC_PORTION
return isempty(k) ? p.nonnumeric[i][j] : p.nonnumeric[i][j][k...]
else
error("Unhandled portion $portion")
end
end
function SymbolicIndexingInterface.set_parameter!(
p::MTKParameters, val, pidx::ParameterIndex
)
@unpack portion, idx, validate_size = pidx
if portion isa SciMLStructures.Tunable
if validate_size && size(val) !== size(idx)
throw(InvalidParameterSizeException(size(idx), size(val)))
end
p.tunable[idx] = val
elseif portion isa SciMLStructures.Initials
if validate_size && size(val) !== size(idx)
throw(InvalidParameterSizeException(size(idx), size(val)))
end
p.initials[idx] = val
else
i, j, k... = idx
if portion isa SciMLStructures.Discrete
if isempty(k)
if validate_size && size(val) !== size(p.discrete[i][j])
throw(
InvalidParameterSizeException(
size(p.discrete[i][j]), size(val)
)
)
end
p.discrete[i][j] = val
else
p.discrete[i][j][k...] = val
end
elseif portion isa SciMLStructures.Constants
if isempty(k)
if validate_size && size(val) !== size(p.constant[i][j])
throw(InvalidParameterSizeException(size(p.constant[i][j]), size(val)))
end
p.constant[i][j] = val
else
p.constant[i][j][k...] = val
end
elseif portion === NONNUMERIC_PORTION
if isempty(k)
p.nonnumeric[i][j] = val
else
p.nonnumeric[i][j][k...] = val
end
else
error("Unhandled portion $portion")
end
end
return nothing
end
function narrow_buffer_type_and_fallback_undefs(
oldbuf::AbstractVector, newbuf::AbstractVector
)
type = Union{}
for i in eachindex(newbuf)
isassigned(newbuf, i) || continue
type = promote_type(type, typeof(newbuf[i]))
end
if type == Union{}
type = eltype(oldbuf)
end
newerbuf = similar(newbuf, type)
for i in eachindex(newbuf)
if isassigned(newbuf, i)
newerbuf[i] = newbuf[i]
else
newerbuf[i] = oldbuf[i]
end
end
return newerbuf
end
function validate_parameter_type(ic::IndexCache, p, idx::ParameterIndex, val)
p = unwrap(p)
if p isa Symbol
p = get(ic.symbol_to_variable, p, nothing)
p === nothing && return validate_parameter_type(ic, idx, val)
end
stype = symtype(p)
sz = if stype <: AbstractArray
size(p)
elseif stype <: Number
size(p)
else
SU.Unknown(-1)
end
return validate_parameter_type(ic, stype, sz, p, idx, val)
end
function validate_parameter_type(ic::IndexCache, idx::ParameterIndex, val)
stype = get_buffer_template(ic, idx).type
if (
idx.portion == SciMLStructures.Tunable() ||
idx.portion == SciMLStructures.Initials()
) && !(idx.idx isa Int)
stype = AbstractArray{<:stype}
end
return validate_parameter_type(
ic, stype, SU.Unknown(-1), nothing, idx, val
)
end
function validate_parameter_type(ic::IndexCache, stype, sz, sym, index, val)
(; portion) = index
if stype <: FnType
stype = fntype_to_function_type(stype)
end
# Nonnumeric parameters have to match the type
if portion === NONNUMERIC_PORTION
val isa stype && return nothing
throw(
ParameterTypeException(
:validate_parameter_type, sym === nothing ? index : sym, stype, val
)
)
end
# Array parameters need array values...
if stype <: AbstractArray && !isa(val, AbstractArray)
throw(
ParameterTypeException(
:validate_parameter_type, sym === nothing ? index : sym, stype, val
)
)
end
# ... and must match sizes
if stype <: AbstractArray && !(sz isa SU.Unknown) && size(val) != sz
throw(InvalidParameterSizeException(sym, val))
end
# Early exit
val isa stype && return nothing
return if stype <: AbstractArray
# Arrays need handling when eltype is `Real` (accept any real array)
etype = eltype(stype)
if etype <: Real
etype = Real
end
# This is for duals and other complicated number types
etype = SciMLBase.parameterless_type(etype)
eltype(val) <: etype || throw(
ParameterTypeException(
:validate_parameter_type, sym === nothing ? index : sym, AbstractArray{etype}, val
)
)
else
# Real check
if stype <: Real
stype = Real
end
stype = SciMLBase.parameterless_type(stype)
val isa stype ||
throw(
ParameterTypeException(
:validate_parameter_type, sym === nothing ? index : sym, stype, val
)
)
end
end
function indp_to_system(indp)
while hasmethod(symbolic_container, Tuple{typeof(indp)})
indp = symbolic_container(indp)
end
return indp
end
function SymbolicIndexingInterface.remake_buffer(indp, oldbuf::MTKParameters, idxs, vals)
return _remake_buffer(indp, oldbuf, idxs, vals)
end
function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true)
return __remake_buffer(indp, oldbuf, idxs, vals; validate)
end
function __remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true)
newbuf = @set oldbuf.tunable = similar(oldbuf.tunable, Any)
@set! newbuf.initials = similar(oldbuf.initials, Any)
@set! newbuf.discrete = Tuple(similar(buf, Any) for buf in newbuf.discrete)
@set! newbuf.constant = Tuple(similar(buf, Any) for buf in newbuf.constant)
@set! newbuf.nonnumeric = Tuple(similar(buf, Any) for buf in newbuf.nonnumeric)
function handle_parameter(ic, sym, idx, val)
if validate
if sym === nothing
validate_parameter_type(ic, idx, val)
else
validate_parameter_type(ic, sym, idx, val)
end
end
# `ParameterIndex(idx)` turns off size validation since it relies on there
# being an existing value
return set_parameter!(newbuf, val, ParameterIndex(idx))
end
handled_idxs = Set{ParameterIndex}()
# If the parameter buffer is an `MTKParameters` object, `indp` must eventually drill
# down to an `AbstractSystem` using `symbolic_container`. We leverage this to get
# the index cache.
ic = get_index_cache(indp_to_system(indp))
for (idx, val) in zip(idxs, vals)
sym = nothing
if val === missing
val = get_temporary_value(idx)
end
if symbolic_type(idx) == ScalarSymbolic()
sym = idx
idx = parameter_index(ic, sym)
if idx === nothing
@warn "Symbolic variable $sym is not a (non-dependent) parameter in the system"
continue
end
idx in handled_idxs && continue
handle_parameter(ic, sym, idx, val)
push!(handled_idxs, idx)
elseif symbolic_type(idx) == ArraySymbolic()
sym = idx
idx = parameter_index(ic, sym)
if idx === nothing
symbolic_has_known_size(sym) ||
throw(ParameterNotInSystem(sym))
size(sym) == size(val) || throw(InvalidParameterSizeException(sym, val))
for (i, vali) in zip(eachindex(sym), eachindex(val))
idx = parameter_index(ic, sym[i])
if idx === nothing
@warn "Symbolic variable $sym is not a (non-dependent) parameter in the system"
continue
end
# Intentionally don't check handled_idxs here because array variables always take priority
# See Issue#2804
handle_parameter(ic, sym[i], idx, val[vali])
push!(handled_idxs, idx)
end
else
idx in handled_idxs && continue
handle_parameter(ic, sym, idx, val)
push!(handled_idxs, idx)
end
else # NotSymbolic
if !(idx isa ParameterIndex)
throw(ArgumentError("Expected index for parameter to be a symbolic variable or `ParameterIndex`, got $idx"))
end
handle_parameter(ic, nothing, idx, val)
end
end
@set! newbuf.tunable = narrow_buffer_type_and_fallback_undefs(
oldbuf.tunable, newbuf.tunable
)
if eltype(newbuf.tunable) <: Integer
T = promote_type(eltype(newbuf.tunable), Float64)
@set! newbuf.tunable = T.(newbuf.tunable)
end
@set! newbuf.initials = narrow_buffer_type_and_fallback_undefs(
oldbuf.initials, newbuf.initials
)
if eltype(newbuf.initials) <: Integer
T = promote_type(eltype(newbuf.initials), Float64)
@set! newbuf.initials = T.(newbuf.initials)
end
@set! newbuf.discrete = narrow_buffer_type_and_fallback_undefs.(
oldbuf.discrete, newbuf.discrete
)
@set! newbuf.constant = narrow_buffer_type_and_fallback_undefs.(
oldbuf.constant, newbuf.constant
)
for (oldv, newv) in zip(oldbuf.nonnumeric, newbuf.nonnumeric)
for i in eachindex(oldv)
isassigned(newv, i) && continue
newv[i] = oldv[i]
end
end
@set! newbuf.nonnumeric = Tuple(
typeof(oldv)(newv) for (oldv, newv) in zip(oldbuf.nonnumeric, newbuf.nonnumeric)
)
if !ArrayInterface.ismutable(oldbuf)
@set! newbuf.tunable = similar_type(oldbuf.tunable, eltype(newbuf.tunable))(newbuf.tunable)
@set! newbuf.initials = similar_type(oldbuf.initials, eltype(newbuf.initials))(newbuf.initials)
@set! newbuf.discrete = ntuple(Val(length(newbuf.discrete))) do i
similar_type.(oldbuf.discrete[i], eltype(newbuf.discrete[i]))(newbuf.discrete[i])
end
@set! newbuf.constant = ntuple(Val(length(newbuf.constant))) do i
similar_type.(oldbuf.constant[i], eltype(newbuf.constant[i]))(newbuf.constant[i])
end
@set! newbuf.nonnumeric = ntuple(Val(length(newbuf.nonnumeric))) do i
similar_type.(oldbuf.nonnumeric[i], eltype(newbuf.nonnumeric[i]))(newbuf.nonnumeric[i])
end
end
return newbuf
end
# For type-inference when using `SII.setp_oop`
@generated function _remake_buffer(
indp, oldbuf::MTKParameters{T, I, D, C, N, H},
idxs::Union{Tuple{Vararg{ParameterIndex}}, AbstractArray{<:ParameterIndex{P}}},
vals::Union{AbstractArray, Tuple}; validate = true
) where {T, I, D, C, N, H, P}
# fallback to non-generated method if values aren't type-stable
if vals <: AbstractArray && !isconcretetype(eltype(vals))
return quote
$__remake_buffer(indp, oldbuf, collect(idxs), vals; validate)
end
end
# given an index in idxs/vals and the current `eltype` of the buffer,
# return the promoted eltype of the buffer
function promote_valtype(i, valT)
# tuples have distinct types, arrays have a common eltype
valT′ = vals <: AbstractArray ? eltype(vals) : fieldtype(vals, i)
# if the buffer is a scalarized buffer but the variable is an array
# e.g. an array tunable, take the eltype
if valT′ <: AbstractArray && !(valT <: AbstractArray)
valT′ = eltype(valT′)
end
return promote_type(valT, valT′)
end
# types of the idxs
idxtypes = if idxs <: AbstractArray
# if both are arrays, there is only one possible type to check
if vals <: AbstractArray
(eltype(idxs),)
else
# if `vals` is a tuple, we repeat `eltype(idxs)` to check against
# every possible type of the buffer
ntuple(Returns(eltype(idxs)), Val(fieldcount(vals)))
end
else
# `idxs` is a tuple, so we check against all buffers
fieldtypes(idxs)
end
# promote types
tunablesT = eltype(T)
for (i, idxT) in enumerate(idxtypes)
idxT <: ParameterIndex{SciMLStructures.Tunable} || continue
tunablesT = promote_valtype(i, tunablesT)
end
initialsT = eltype(I)
for (i, idxT) in enumerate(idxtypes)
idxT <: ParameterIndex{SciMLStructures.Initials} || continue
initialsT = promote_valtype(i, initialsT)
end
discretesT = ntuple(Val(fieldcount(D))) do i
bufT = eltype(fieldtype(D, i))
for (j, idxT) in enumerate(idxtypes)
idxT <: ParameterIndex{SciMLStructures.Discrete, i} || continue
bufT = promote_valtype(i, bufT)
end
bufT
end
constantsT = ntuple(Val(fieldcount(C))) do i
bufT = eltype(fieldtype(C, i))
for (j, idxT) in enumerate(idxtypes)
idxT <: ParameterIndex{SciMLStructures.Constants, i} || continue
bufT = promote_valtype(i, bufT)
end
bufT
end
nonnumericT = ntuple(Val(fieldcount(N))) do i
bufT = eltype(fieldtype(N, i))
for (j, idxT) in enumerate(idxtypes)
idxT <: ParameterIndex{Nonnumeric, i} || continue
bufT = promote_valtype(i, bufT)
end
bufT
end
expr = quote
tunables = $similar(oldbuf.tunable, $tunablesT)
copyto!(tunables, oldbuf.tunable)
initials = $similar(oldbuf.initials, $initialsT)
copyto!(initials, oldbuf.initials)
discretes = $(
Expr(
:tuple,
(
:($similar(oldbuf.discrete[$i], $(discretesT[i])))
for i in 1:length(discretesT)
)...
)
)
$(
(
:($copyto!(discretes[$i], oldbuf.discrete[$i]))
for i in 1:length(discretesT)
)...
)
constants = $(
Expr(
:tuple,
(
:($similar(oldbuf.constant[$i], $(constantsT[i])))
for i in 1:length(constantsT)
)...
)
)
$(
(
:($copyto!(constants[$i], oldbuf.constant[$i]))
for i in 1:length(constantsT)
)...
)
nonnumerics = $(
Expr(
:tuple,
(
:($similar(oldbuf.nonnumeric[$i], $(nonnumericT[i])))
for i in 1:length(nonnumericT)
)...
)
)
$(
(
:($copyto!(nonnumerics[$i], oldbuf.nonnumeric[$i]))
for i in 1:length(nonnumericT)
)...
)
caches = copy.(oldbuf.caches)
newbuf = MTKParameters(
tunables, initials, discretes, constants, nonnumerics, caches
)
end
if idxs <: AbstractArray
push!(
expr.args, :(
for (idx, val) in zip(idxs, vals)
$setindex!(newbuf, val, idx)
end
)
)
else
for i in 1:fieldcount(idxs)
push!(expr.args, :($setindex!(newbuf, vals[$i], idxs[$i])))
end
end
if !ArrayInterface.ismutable(oldbuf)
push!(expr.args, :(tunables = $similar_type($T, $tunablesT)(tunables)))
push!(expr.args, :(initials = $similar_type($I, $initialsT)(initials)))
push!(
expr.args,
:(
discretes = $(
Expr(
:tuple,
(
:($similar_type($(fieldtype(D, i)), $(discretesT[i]))(discretes[$i]))
for i in 1:length(discretesT)
)...
)
)
)
)
push!(
expr.args,
:(
constants = $(
Expr(
:tuple,
(
:($similar_type($(fieldtype(C, i)), $(constantsT[i]))(constants[$i]))
for i in 1:length(constantsT)
)...
)
)
)
)
push!(
expr.args,
:(
nonnumerics = $(
Expr(
:tuple,
(
:($similar_type($(fieldtype(C, i)), $(nonnumericT[i]))(nonnumerics[$i]))
for i in 1:length(nonnumericT)
)...
)
)
)
)
push!(
expr.args,
:(
newbuf = MTKParameters(
tunables, initials, discretes, constants, nonnumerics, caches
)
)
)
end
push!(expr.args, :(return newbuf))
return expr
end
function as_any_buffer(p::MTKParameters)
@set! p.tunable = similar(p.tunable, Any)
@set! p.initials = similar(p.initials, Any)
@set! p.discrete = Tuple(similar(buf, Any) for buf in p.discrete)
@set! p.constant = Tuple(similar(buf, Any) for buf in p.constant)
@set! p.nonnumeric = Tuple(similar(buf, Any) for buf in p.nonnumeric)
@set! p.caches = Tuple(similar(buf, Any) for buf in p.caches)
return p
end
struct NestedGetIndex{T}
x::T
end
function Base.getindex(ngi::NestedGetIndex, idx::Tuple)
i, j, k... = idx
return ngi.x[i][j][k...]