Skip to content

Commit dcfbe2d

Browse files
committed
Error on non-finite values when debugging systems
1 parent f2c94b5 commit dcfbe2d

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/debugging.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ struct LoggedFun{F}
77
f::F
88
args::Any
99
end
10+
LoggedFunctionException(lf::LoggedFun, args, msg) = LoggedFunctionException(
11+
"Function $(lf.f)($(join(lf.args, ", "))) " * msg * " with input" *
12+
join("\n " .* string.(lf.args .=> args)) # one line for each "var => val" for readability
13+
)
1014
Base.showerror(io::IO, err::LoggedFunctionException) = print(io, err.msg)
1115
Base.nameof(lf::LoggedFun) = nameof(lf.f)
1216
SymbolicUtils.promote_symtype(::LoggedFun, Ts...) = Real
1317
function (lf::LoggedFun)(args...)
14-
try
15-
return lf.f(args...) # try to call with numerical input, as usual
18+
val = try
19+
lf.f(args...) # try to call with numerical input, as usual
1620
catch err
17-
throw(LoggedFunctionException(
18-
"Function $(lf.f)($(join(lf.args, ", "))) errors with input" *
19-
join("\n " .* string.(lf.args .=> args)) # one line for each "var => val" for readability
20-
)) # Julia automatically attaches original error message
21+
throw(LoggedFunctionException(lf, args, "errors")) # Julia automatically attaches original error message
2122
end
23+
if !isfinite(val)
24+
throw(LoggedFunctionException(lf, args, "output non-finite value $val"))
25+
end
26+
return val
2227
end
2328

2429
function logged_fun(f, args...)

src/systems/abstractsystem.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,18 +2266,14 @@ Replace functions with singularities with a function that errors with symbolic
22662266
information. E.g.
22672267
22682268
```julia-repl
2269-
julia> sys = debug_system(sys)
2269+
julia> sys = debug_system(complete(sys))
22702270
2271-
julia> sys = complete(sys)
2272-
2273-
julia> prob = ODEProblem(sys, [1.0, 0.0], (0.0, 1.0))
2271+
julia> prob = ODEProblem(sys, [0.0, 2.0], (0.0, 1.0))
22742272
22752273
julia> prob.f(prob.u0, prob.p, 0.0)
2276-
ERROR: Function log(-cos(Q(t))) errors with input
2277-
-cos(Q(t)) => -1.0
2278-
Stacktrace:
2279-
[1] (::ModelingToolkit.LoggedFun{typeof(log)})(num_args::Float64)
2280-
...
2274+
ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input
2275+
1 => 1
2276+
sin(P(t)) => 0.0
22812277
```
22822278
"""
22832279
function debug_system(sys::AbstractSystem)

test/odesystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ testdict = Dict([:name => "test"])
939939
sys = complete(debug_system(sys))
940940
prob = ODEProblem(sys, [], (0.0, 1.0))
941941
@test_throws "log(-cos(Q(t))) errors" prob.f([1, 0], prob.p, 0.0)
942-
@test prob.f([0, 2], prob.p, 0.0)[1] == 1 / 0
942+
@test_throws "/(1, sin(P(t))) output non-finite value" prob.f([0, 2], prob.p, 0.0)
943943

944944
let
945945
@variables x(t) = 1

0 commit comments

Comments
 (0)