Skip to content

Commit 94576db

Browse files
committed
Let user pass list of functions that should be debugged
1 parent f5562dc commit 94576db

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/debugging.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const LOGGED_FUN = Set([log, sqrt, (^), /, inv])
2-
31
struct LoggedFunctionException <: Exception
42
msg::String
53
end
@@ -32,11 +30,11 @@ function logged_fun(f, args...; error_nonfinite = true) # remember to update err
3230
term(LoggedFun(f, args, error_nonfinite), args..., type = Real)
3331
end
3432

35-
debug_sub(eq::Equation; kw...) = debug_sub(eq.lhs; kw...) ~ debug_sub(eq.rhs; kw...)
36-
function debug_sub(ex; kw...)
33+
debug_sub(eq::Equation, funcs; kw...) = debug_sub(eq.lhs, funcs; kw...) ~ debug_sub(eq.rhs, funcs; kw...)
34+
function debug_sub(ex, funcs; kw...)
3735
iscall(ex) || return ex
3836
f = operation(ex)
39-
args = map(debug_sub, arguments(ex))
40-
f in LOGGED_FUN ? logged_fun(f, args...; kw...) :
37+
args = map(ex -> debug_sub(ex, funcs; kw...), arguments(ex))
38+
f in funcs ? logged_fun(f, args...; kw...) :
4139
maketerm(typeof(ex), f, args, metadata(ex))
4240
end

src/systems/abstractsystem.jl

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

22622262
"""
2263-
debug_system(sys::AbstractSystem; error_nonfinite = true)
2263+
debug_system(sys::AbstractSystem; functions = [log, sqrt, (^), /, inv, asin, acos], error_nonfinite = true)
22642264
2265-
Replace functions with singularities with a function that errors with symbolic
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
2265+
Wrap `functions` in `sys` so any error thrown in them shows helpful symbolic-numeric
2266+
information about its input. If `error_nonfinite`, functions that output nonfinite
2267+
values (like `Inf` or `NaN`) also display errors, even though the raw function itself
22682268
does not throw an exception (like `1/0`). For example:
22692269
22702270
```julia-repl
@@ -2278,15 +2278,18 @@ ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input
22782278
sin(P(t)) => 0.0
22792279
```
22802280
"""
2281-
function debug_system(sys::AbstractSystem; kw...)
2281+
function debug_system(sys::AbstractSystem; functions = [log, sqrt, (^), /, inv, asin, acos], kw...)
2282+
if !(functions isa Set)
2283+
functions = Set(functions) # more efficient "in" lookup
2284+
end
22822285
if has_systems(sys) && !isempty(get_systems(sys))
22832286
error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or structural_simplify(sys) first.")
22842287
end
22852288
if has_eqs(sys)
2286-
@set! sys.eqs = debug_sub.(equations(sys); kw...)
2289+
@set! sys.eqs = debug_sub.(equations(sys), Ref(functions); kw...)
22872290
end
22882291
if has_observed(sys)
2289-
@set! sys.observed = debug_sub.(observed(sys); kw...)
2292+
@set! sys.observed = debug_sub.(observed(sys), Ref(functions); kw...)
22902293
end
22912294
return sys
22922295
end

0 commit comments

Comments
 (0)