8383# ## ODESystem with constraint equations, DAEs with constraints ###
8484# #################################################################
8585
86- # Test generation of boundary condition function.
86+ # Test generation of boundary condition function using `process_constraints`. Compare solutions to manually written boundary conditions
8787let
8888 @parameters α= 1.5 β= 1.0 γ= 3.0 δ= 1.0
89- @variables x (.. ) y (t )
90- eqs = [D (x (t)) ~ α * x (t) - β * x (t) * y,
91- D (y) ~ - γ * y + δ * x (t) * y]
89+ @variables x (.. ) y (.. )
90+ eqs = [D (x (t)) ~ α * x (t) - β * x (t) * y (t) ,
91+ D (y (t)) ~ - γ * y (t) + δ * x (t) * y (t) ]
9292
93- tspan = (0. , 10 . )
93+ tspan = (0. , 1 . )
9494 @mtkbuild lksys = ODESystem (eqs, t)
9595
9696 function lotkavolterra! (du, u, p, t)
111111 [u[1 ][1 ] - 1. , u[1 ][2 ] - 2. ]
112112 end
113113
114- u0 = [1. , 2. ]; p = [1.5 , 1. , 3 . , 1 . ]
114+ u0 = [1. , 2. ]; p = [1.5 , 1. , 1 . , 3 . ]
115115 genbc_iip = ModelingToolkit. process_constraints (lksys, nothing , u0, [1 , 2 ], tspan, true )
116116 genbc_oop = ModelingToolkit. process_constraints (lksys, nothing , u0, [1 , 2 ], tspan, false )
117117
@@ -130,48 +130,54 @@ let
130130 @test sol1 ≈ sol2
131131
132132 # Test with a constraint.
133- constraints = [x (0.5 ) ~ 1 . ]
133+ constraints = [y (0.5 ) ~ 2 . ]
134134
135135 function bc! (resid, u, p, t)
136- resid[1 ] = u[1 ][ 2 ] - 2 .
137- resid[2 ] = u (0.5 )[1 ] - 1 .
136+ resid[1 ] = u ( 0.0 ) [1 ] - 1 .
137+ resid[2 ] = u (0.5 )[2 ] - 2 .
138138 end
139139 function bc (u, p, t)
140- [u[1 ][ 2 ] - 2 . , u (0.5 )[1 ] - 1 . ]
140+ [u ( 0.0 ) [1 ] - 1 . , u (0.5 )[2 ] - 2 . ]
141141 end
142142
143- u0 = [1. , 2 . ]
144- genbc_iip = ModelingToolkit. process_constraints (lksys, constraints, u0, [2 ], tspan, true )
145- genbc_oop = ModelingToolkit. process_constraints (lksys, constraints, u0, [2 ], tspan, false )
143+ u0 = [1 , 1 . ]
144+ genbc_iip = ModelingToolkit. process_constraints (lksys, constraints, u0, [1 ], tspan, true )
145+ genbc_oop = ModelingToolkit. process_constraints (lksys, constraints, u0, [1 ], tspan, false )
146146
147- bvpi1 = SciMLBase. BVProblem (lotkavolterra!, bc!, [1. ,2. ], tspan, p)
148- bvpi2 = SciMLBase. BVProblem (lotkavolterra!, genbc_iip, [1. ,2. ], tspan, p)
149- bvpi3 = SciMLBase. BVProblem {true, SciMLBase.AutoSpecialize} (lksys, u0map, tspan, parammap; guesses, constraints)
147+ bvpi1 = SciMLBase. BVProblem (lotkavolterra!, bc!, u0, tspan, p)
148+ bvpi2 = SciMLBase. BVProblem (lotkavolterra!, genbc_iip, u0, tspan, p)
149+ bvpi3 = SciMLBase. BVProblem {true, SciMLBase.AutoSpecialize} (lksys, [x (t) => 1. ], tspan; guesses = [y (t) => 1. ], constraints)
150+ bvpi4 = SciMLBase. BVProblem {true, SciMLBase.FullSpecialize} (lksys, [x (t) => 1. ], tspan; guesses = [y (t) => 1. ], constraints)
150151
151- sol1 = solve (bvpi1, MIRK4 (), dt = 0.05 )
152- sol2 = solve (bvpi2, MIRK4 (), dt = 0.05 )
153- @test sol1 ≈ sol2 # don't get true equality here, not sure why
152+ @btime sol1 = solve (bvpi1, MIRK4 (), dt = 0.01 )
153+ @btime sol2 = solve (bvpi2, MIRK4 (), dt = 0.01 )
154+ @btime sol3 = solve (bvpi3, MIRK4 (), dt = 0.01 )
155+ @btime sol4 = solve (bvpi4, MIRK4 (), dt = 0.01 )
156+ @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why
154157
155158 bvpo1 = BVProblem (lotkavolterra, bc, [1 ,2 ], tspan, p)
156159 bvpo2 = BVProblem (lotkavolterra, genbc_oop, [1 ,2 ], tspan, p)
160+ bvpo3 = SciMLBase. BVProblem {false, SciMLBase.FullSpecialize} (lksys, [x (t) => 1. ], tspan, parammap; guesses = [y (t) => 1. ], constraints)
157161
158- sol1 = solve (bvpo1, MIRK4 (), dt = 0.05 )
159- sol2 = solve (bvpo2, MIRK4 (), dt = 0.05 )
160- @test sol1 ≈ sol2
162+ @btime sol1 = solve (bvpo1, MIRK4 (), dt = 0.05 )
163+ @btime sol2 = solve (bvpo2, MIRK4 (), dt = 0.05 )
164+ @btime sol3 = solve (bvpo3, MIRK4 (), dt = 0.05 )
165+ @test sol1 ≈ sol2 ≈ sol3
161166end
162167
163- function test_solvers (solvers, prob, u0map, constraints, equations = []; dt = 0.05 )
168+ function test_solvers (solvers, prob, u0map, constraints, equations = []; dt = 0.05 , atol = 1e-4 )
164169 for solver in solvers
165- sol = solve (prob, solver (); dt)
170+ println (" Solver: $solver " )
171+ @btime sol = solve (prob, solver (), dt = dt, abstol = atol)
166172 @test SciMLBase. successful_retcode (sol. retcode)
167173 p = prob. p; t = sol. t; bc = prob. f. bc
168174 ns = length (prob. u0)
169175 if isinplace (bvp. f)
170176 resid = zeros (ns)
171177 bc! (resid, sol, p, t)
172- @test isapprox (zeros (ns), resid)
178+ @test isapprox (zeros (ns), resid; atol )
173179 else
174- @test isapprox (zeros (ns), bc (sol, p, t))
180+ @test isapprox (zeros (ns), bc (sol, p, t); atol )
175181 end
176182
177183 for (k, v) in u0map
@@ -190,23 +196,21 @@ end
190196
191197# Simple ODESystem with BVP constraints.
192198let
193- t = ModelingToolkit. t_nounits; D = ModelingToolkit. D_nounits
194199 @parameters α= 1.5 β= 1.0 γ= 3.0 δ= 1.0
195- @variables x (.. ) y (t )
200+ @variables x (.. ) y (.. )
196201
197- eqs = [D (x (t)) ~ α * x (t) - β * x (t) * y,
198- D (y) ~ - γ * y + δ * x (t) * y]
202+ eqs = [D (x (t)) ~ α * x (t) - β * x (t) * y (t) ,
203+ D (y (t)) ~ - γ * y (t) + δ * x (t) * y (t) ]
199204
200205 u0map = []
201- parammap = [α => 7.5 , β => 4 , γ => 8.0 , δ => 5.0 ]
202206 tspan = (0.0 , 10.0 )
203- guesses = [x (t) => 1 .0 , y => 2. ]
207+ guesses = [x (t) => 4 .0 , y (t) => 2. ]
204208
205- @mtkbuild lotkavolterra = ODESystem (eqs, t)
209+ @mtkbuild lksys = ODESystem (eqs, t)
206210
207- constraints = [x (6. ) ~ 1.5 ]
208- bvp = SciMLBase. BVProblem {true, SciMLBase.AutoSpecialize} (lotkavolterra , u0map, tspan, parammap ; guesses, constraints)
209- test_solvers (solvers, bvp, u0map, constraints)
211+ constraints = [x (6. ) ~ 3.5 , x ( 3. ) ~ 7. ]
212+ bvp = SciMLBase. BVProblem {true, SciMLBase.AutoSpecialize} (lksys , u0map, tspan; guesses, constraints)
213+ test_solvers (solvers, bvp, u0map, constraints; dt = 0.1 )
210214
211215 # Testing that more complicated constraints give correct solutions.
212216 constraints = [y (2. ) + x (8. ) ~ 2. ]
0 commit comments