@@ -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 )
479482
480483@testset " ParametrizedInterpolation" begin
481484 @variables y (t) = 0
482- @parameters u[ 1 : 15 ] = rand (15 )
483- @parameters x[ 1 : 15 ] = 0 : 14.0
485+ u = rand (15 )
486+ x = 0 : 14.0
484487
485488 @testset " LinearInterpolation" begin
486489 @named i = ParametrizedInterpolation (LinearInterpolation, u, x)
493496 sol = solve (prob)
494497
495498 @test SciMLBase. successful_retcode (sol)
499+
500+ prob2 = remake (prob, p= [i. data => ones (15 )])
501+ sol2 = solve (prob2)
502+
503+ @test SciMLBase. successful_retcode (sol2)
504+ @test all (only .(sol2. u) .≈ sol2. t) # the solution for y' = 1 is y(t) = t
505+
506+ set_data! = setp (prob2, i. data)
507+ set_data! (prob2, zeros (15 ))
508+ sol3 = solve (prob2)
509+ @test SciMLBase. successful_retcode (sol3)
510+ @test iszero (sol3)
511+
512+ function loss (x, p)
513+ prob0, set_data! = p
514+ ps = parameter_values (prob0)
515+ arr, repack, alias = SciMLStructures. canonicalize (Tunable (), ps)
516+ T = promote_type (eltype (x), eltype (arr))
517+ promoted_ps = SciMLStructures. replace (Tunable (), ps, T .(arr))
518+ prob = remake (prob0; p = promoted_ps)
519+
520+ set_data! (prob, x)
521+ sol = solve (prob)
522+ sum (abs2 .(only .(sol. u) .- sol. t))
523+ end
524+
525+ set_data! = setp (prob, i. data)
526+ of = OptimizationFunction (loss, AutoForwardDiff ())
527+ op = OptimizationProblem (of, u, (prob, set_data!), lb = zeros (15 ), ub = fill (2.0 , 15 ))
528+
529+ # check that type changing works
530+ @test length (ForwardDiff. gradient (x -> of (x, (prob, set_data!)), u)) == 15
531+
532+ r = solve (op, Optimization. LBFGS (), maxiters = 1000 )
533+ @test of (r. u, (prob, set_data!)) < of (u, (prob, set_data!))
496534 end
497535
498536 @testset " BSplineInterpolation" begin
0 commit comments