11using  ModelingToolkit, OrdinaryDiffEq, NonlinearSolve, Test
2+ using  SymbolicIndexingInterface
23using  ModelingToolkit:  t_nounits as t, D_nounits as D
34
45@parameters  g
@@ -560,27 +561,69 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
560561        @test  init (prob, Tsit5 ()). ps[sym] ≈  val
561562        @test  solve (prob, Tsit5 ()). ps[sym] ≈  val
562563    end 
564+     function  test_initializesystem (sys, u0map, pmap, p, equation)
565+         isys =  ModelingToolkit. generate_initializesystem (sys; u0map, pmap, guesses =  ModelingToolkit. guesses (sys))
566+         @test  is_variable (isys, p)
567+         @test  equation in  equations (isys) ||  (0  ~  - equation. rhs) in  equations (isys)
568+     end 
563569    @variables  x (t) y (t)
564-     @parameters  p
565-     _p =  ModelingToolkit. setdefault (p, missing )
566-     _p =  ModelingToolkit. setguess (_p, 0.0 )
567-     @mtkbuild  sys =  ODESystem ([D (x) ~  x, _p ~  x +  y], t)
568-     prob =  ODEProblem (sys, [x =>  1.0 , y =>  1.0 ], (0.0 , 1.0 ))
569-     test_parameter (prob, _p, 2.0 )
570+     @parameters  p q
571+     u0map =  Dict (x =>  1.0 , y =>  1.0 )
572+     pmap =  Dict ()
573+     pmap[q] =  1.0 
574+     #  `missing` default, equation from ODEProblem
575+     @mtkbuild  sys =  ODESystem ([D (x) ~  x *  q, D (y) ~  y *  p], t; defaults =  [p =>  missing ])
576+     pmap[p] =  2 q
577+     prob =  ODEProblem (sys, u0map, (0.0 , 1.0 ), pmap)
578+     test_parameter (prob, p, 2.0 )
579+     #  `missing` default, provided guess
570580    @mtkbuild  sys =  ODESystem (
571581        [D (x) ~  x, p ~  x +  y], t; defaults =  [p =>  missing ], guesses =  [p =>  0.0 ])
572-     prob =  ODEProblem (sys, [x  =>   1.0 , y  =>   1.0 ] , (0.0 , 1.0 ))
582+     prob =  ODEProblem (sys, u0map , (0.0 , 1.0 ))
573583    test_parameter (prob, p, 2.0 )
574-     @mtkbuild  sys =  ODESystem ([D (x) ~  x, p ~  x +  y], t; guesses =  [p =>  0.0 ])
575-     prob =  ODEProblem (sys, [x =>  1.0 , y =>  1.0 ], (0.0 , 1.0 ), [p =>  missing ])
584+     test_initializesystem (sys, u0map, pmap, p, 0  ~  p -  x -  y)
585+ 
586+     #  `missing` to ODEProblem, equation from default
587+     @mtkbuild  sys =  ODESystem ([D (x) ~  x *  q, D (y) ~  y *  p], t; defaults =  [p =>  2 q])
588+     pmap[p] =  missing 
589+     prob =  ODEProblem (sys, u0map, (0.0 , 1.0 ), pmap)
590+     test_parameter (prob, p, 2.0 )
591+     test_initializesystem (sys, u0map, pmap, p, 0  ~  2 q -  p)
592+     test_parameter (prob2, p, 2.0 )
593+     #  `missing` to ODEProblem, provided guess
594+     @mtkbuild  sys =  ODESystem (
595+         [D (x) ~  x, p ~  x +  y], t; guesses =  [p =>  0.0 ])
596+     prob =  ODEProblem (sys, u0map, (0.0 , 1.0 ), pmap)
576597    test_parameter (prob, p, 2.0 )
598+     test_initializesystem (sys, u0map, pmap, p, 0  ~  x +  y -  p)
577599
578-     @mtkbuild  sys =  ODESystem ([D (x) ~  x, p ~  x +  y], t; guesses =  [p =>  1.0 ])
600+     #  No `missing`, default and guess
601+     @mtkbuild  sys =  ODESystem ([D (x) ~  x *  q, D (y) ~  y *  p], t; defaults =  [p =>  2 q], guesses =  [p =>  0.0 ])
602+     delete! (pmap, p)
603+     prob =  ODEProblem (sys, u0map, (0.0 , 1.0 ), pmap)
604+     test_parameter (prob, p, 2.0 )
605+     test_initializesystem (sys, u0map, pmap, p, 0  ~  2 q -  p)
606+ 
607+     #  Should not be solved for:
608+ 
609+     #  ODEProblem value with guess, no `missing`
610+     @mtkbuild  sys =  ODESystem ([D (x) ~  x *  q, D (y) ~  y *  p], t; guesses =  [p =>  0.0 ])
611+     _pmap =  merge (pmap, Dict (p =>  3 q))
612+     prob =  ODEProblem (sys, u0map, (0.0 , 1.0 ), _pmap)
613+     @test  prob. ps[p] ≈  3.0 
614+     @test  prob. f. initializeprob ===  nothing 
615+     #  Default overridden by ODEProblem, guess provided
616+     @mtkbuild  sys =  ODESystem ([D (x) ~  q *  x, D (y) ~  y *  p], t; defaults =  [p =>  2 q], guesses =  [p =>  1.0 ])
617+     prob =  ODEProblem (sys, u0map, (0.0 , 1.0 ), _pmap)
618+     @test  prob. ps[p] ≈  3.0 
619+     @test  prob. f. initializeprob ===  nothing 
620+ 
621+     @mtkbuild  sys =  ODESystem ([D (x) ~  x, p ~  x +  y], t; guesses =  [p =>  0.0 ])
579622    @test_throws  ModelingToolkit. MissingParametersError ODEProblem (
580623        sys, [x =>  1.0 , y =>  1.0 ], (0.0 , 1.0 ))
581624    @mtkbuild  sys =  ODESystem ([D (x) ~  x, p ~  x +  y], t)
582-     @test_throws  [" Invalid setup" " parameter p" " guess" ODEProblem (
583-         sys, [ x =>  1.0 , y =>  1.0 ], ( 0.0 ,  1.0 ), [ p =>  missing ] )
625+     @test_throws  [" Invalid setup" " parameter p" " guess" ModelingToolkit . generate_initializesystem (
626+         sys; u0map  =   Dict ( x =>  1.0 , y =>  1.0 ), pmap  =   Dict ( p =>  missing ), check_defguess  =   true )
584627
585628    @testset  " Null system" begin 
586629        @variables  x (t) y (t) s (t)
@@ -593,14 +636,15 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
593636    using  ModelingToolkitStandardLibrary. Mechanical. TranslationalModelica:  Fixed, Mass,
594637                                                                           Spring, Force,
595638                                                                           Damper
639+     using  ModelingToolkitStandardLibrary. Mechanical:  TranslationalModelica as TM
596640    using  ModelingToolkitStandardLibrary. Blocks:  Constant
597641
598-     @named  mass =  Mass (; m =  1.0 , s =  1.0 , v =  0.0 , a =  0.0 )
642+     @named  mass =  TM . Mass (; m =  1.0 , s =  1.0 , v =  0.0 , a =  0.0 )
599643    @named  fixed =  Fixed (; s0 =  0.0 )
600-     @named  spring =  Spring (; c =  2.0 )
644+     @named  spring =  Spring (; c =  2.0 , s_rel0  =   nothing )
601645    @named  gravity =  Force ()
602646    @named  constant =  Constant (; k =  9.81 )
603-     @named  damper =  Damper (; d =  0.1 )
647+     @named  damper =  TM . Damper (; d =  0.1 )
604648    @mtkbuild  sys =  ODESystem (
605649        [connect (fixed. flange, spring. flange_a), connect (spring. flange_b, mass. flange_a),
606650            connect (mass. flange_a, gravity. flange), connect (constant. output, gravity. f),
0 commit comments