|
| 1 | +using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq, SymbolicIndexingInterface |
| 2 | +using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE |
| 3 | + |
| 4 | +@variables x(t) |
| 5 | +@brownian a |
| 6 | +@named inner_ode = ODESystem(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"]) |
| 7 | +@named inner_sde = System([D(x) ~ -sqrt(x) + a], t; assertions = [(x > 0) => "ohno"]) |
| 8 | +sys_ode = structural_simplify(inner_ode) |
| 9 | +sys_sde = structural_simplify(inner_sde) |
| 10 | + |
| 11 | +@testset "`debug_system` adds assertions" begin |
| 12 | + @testset "$(typeof(sys))" for (Problem, sys, alg) in [ |
| 13 | + (ODEProblem, sys_ode, Tsit5()), (SDEProblem, sys_sde, ImplicitEM())] |
| 14 | + dsys = debug_system(sys; functions = []) |
| 15 | + @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) |
| 16 | + prob = Problem(dsys, [x => 1.0], (0.0, 5.0)) |
| 17 | + sol = solve(prob, alg) |
| 18 | + @test !SciMLBase.successful_retcode(sol) |
| 19 | + prob.ps[ASSERTION_LOG_VARIABLE] = true |
| 20 | + sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg) |
| 21 | + @test !SciMLBase.successful_retcode(sol) |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +@testset "Hierarchical system" begin |
| 26 | + @testset "$(typeof(inner))" for (ctor, Problem, inner, alg) in [ |
| 27 | + (ODESystem, ODEProblem, inner_ode, Tsit5()), |
| 28 | + (System, SDEProblem, inner_sde, ImplicitEM())] |
| 29 | + @mtkbuild outer = ctor(Equation[], t; systems = [inner]) |
| 30 | + dsys = debug_system(outer; functions = []) |
| 31 | + @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) |
| 32 | + prob = Problem(dsys, [inner.x => 1.0], (0.0, 5.0)) |
| 33 | + sol = solve(prob, alg) |
| 34 | + @test !SciMLBase.successful_retcode(sol) |
| 35 | + prob.ps[ASSERTION_LOG_VARIABLE] = true |
| 36 | + sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg) |
| 37 | + @test !SciMLBase.successful_retcode(sol) |
| 38 | + end |
| 39 | +end |
0 commit comments