@@ -6,6 +6,9 @@ using ModelingToolkitStandardLibrary.Blocks: smooth_sin, smooth_cos, smooth_damp
66 smooth_triangular, triangular, square
77using OrdinaryDiffEq: ReturnCode. Success
88using DataInterpolations
9+ using SymbolicIndexingInterface
10+ using SciMLStructures: SciMLStructures, Tunable
11+ using Optimization
912
1013@testset " Constant" begin
1114 @named src = Constant (k = 2 )
480483
481484@testset " ParametrizedInterpolation" begin
482485 @variables y (t) = 0
483- @parameters u[ 1 : 15 ] = rand (15 )
484- @parameters x[ 1 : 15 ] = 0 : 14.0
486+ u = rand (15 )
487+ x = 0 : 14.0
485488
486489 @testset " LinearInterpolation" begin
487490 @named i = ParametrizedInterpolation (LinearInterpolation, u, x)
494497 sol = solve (prob)
495498
496499 @test SciMLBase. successful_retcode (sol)
500+
501+ prob2 = remake (prob, p= [i. data => ones (15 )])
502+ sol2 = solve (prob2)
503+
504+ @test SciMLBase. successful_retcode (sol2)
505+ @test all (only .(sol2. u) .≈ sol2. t) # the solution for y' = 1 is y(t) = t
506+
507+ set_data! = setp (prob2, i. data)
508+ set_data! (prob2, zeros (15 ))
509+ sol3 = solve (prob2)
510+ @test SciMLBase. successful_retcode (sol3)
511+ @test iszero (sol3)
512+
513+ function loss (x, p)
514+ prob0, set_data! = p
515+ ps = parameter_values (prob0)
516+ arr, repack, alias = SciMLStructures. canonicalize (Tunable (), ps)
517+ T = promote_type (eltype (x), eltype (arr))
518+ promoted_ps = SciMLStructures. replace (Tunable (), ps, T .(arr))
519+ prob = remake (prob0; p = promoted_ps)
520+
521+ set_data! (prob, x)
522+ sol = solve (prob)
523+ sum (abs2 .(only .(sol. u) .- sol. t))
524+ end
525+
526+ set_data! = setp (prob, i. data)
527+ of = OptimizationFunction (loss, AutoForwardDiff ())
528+ op = OptimizationProblem (of, u, (prob, set_data!), lb = zeros (15 ), ub = fill (2.0 , 15 ))
529+
530+ # check that type changing works
531+ @test length (ForwardDiff. gradient (x -> of (x, (prob, set_data!)), u)) == 15
532+
533+ r = solve (op, Optimization. LBFGS (), maxiters = 1000 )
534+ @test of (r. u, (prob, set_data!)) < of (u, (prob, set_data!))
497535 end
498536
499537 @testset " BSplineInterpolation" begin
0 commit comments