Skip to content

Commit f5562dc

Browse files
committed
Toggle extra non-finite value error with keyword argument
1 parent dcfbe2d commit f5562dc

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/debugging.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ end
66
struct LoggedFun{F}
77
f::F
88
args::Any
9+
error_nonfinite::Bool
910
end
1011
LoggedFunctionException(lf::LoggedFun, args, msg) = LoggedFunctionException(
1112
"Function $(lf.f)($(join(lf.args, ", "))) " * msg * " with input" *
@@ -20,22 +21,22 @@ function (lf::LoggedFun)(args...)
2021
catch err
2122
throw(LoggedFunctionException(lf, args, "errors")) # Julia automatically attaches original error message
2223
end
23-
if !isfinite(val)
24+
if lf.error_nonfinite && !isfinite(val)
2425
throw(LoggedFunctionException(lf, args, "output non-finite value $val"))
2526
end
2627
return val
2728
end
2829

29-
function logged_fun(f, args...)
30+
function logged_fun(f, args...; error_nonfinite = true) # remember to update error_nonfinite in debug_system() docstring
3031
# Currently we don't really support complex numbers
31-
term(LoggedFun(f, args), args..., type = Real)
32+
term(LoggedFun(f, args, error_nonfinite), args..., type = Real)
3233
end
3334

34-
debug_sub(eq::Equation) = debug_sub(eq.lhs) ~ debug_sub(eq.rhs)
35-
function debug_sub(ex)
35+
debug_sub(eq::Equation; kw...) = debug_sub(eq.lhs; kw...) ~ debug_sub(eq.rhs; kw...)
36+
function debug_sub(ex; kw...)
3637
iscall(ex) || return ex
3738
f = operation(ex)
3839
args = map(debug_sub, arguments(ex))
39-
f in LOGGED_FUN ? logged_fun(f, args...) :
40+
f in LOGGED_FUN ? logged_fun(f, args...; kw...) :
4041
maketerm(typeof(ex), f, args, metadata(ex))
4142
end

src/systems/abstractsystem.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,10 +2260,12 @@ macro mtkbuild(exprs...)
22602260
end
22612261

22622262
"""
2263-
$(SIGNATURES)
2263+
debug_system(sys::AbstractSystem; error_nonfinite = true)
22642264
22652265
Replace functions with singularities with a function that errors with symbolic
2266-
information. E.g.
2266+
information. If `error_nonfinite`, debugged functions that output nonfinite values
2267+
(like `Inf` or `NaN`) also display errors, even though the raw function itself
2268+
does not throw an exception (like `1/0`). For example:
22672269
22682270
```julia-repl
22692271
julia> sys = debug_system(complete(sys))
@@ -2276,15 +2278,15 @@ ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input
22762278
sin(P(t)) => 0.0
22772279
```
22782280
"""
2279-
function debug_system(sys::AbstractSystem)
2281+
function debug_system(sys::AbstractSystem; kw...)
22802282
if has_systems(sys) && !isempty(get_systems(sys))
22812283
error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or structural_simplify(sys) first.")
22822284
end
22832285
if has_eqs(sys)
2284-
@set! sys.eqs = debug_sub.(equations(sys))
2286+
@set! sys.eqs = debug_sub.(equations(sys); kw...)
22852287
end
22862288
if has_observed(sys)
2287-
@set! sys.observed = debug_sub.(observed(sys))
2289+
@set! sys.observed = debug_sub.(observed(sys); kw...)
22882290
end
22892291
return sys
22902292
end

0 commit comments

Comments
 (0)