`@assert` handling for symbolically registered functions in MTK

Hello,

I would like to know if it is possible to have assert statements on parameters of an MTK component.
For general @assert a >1 cases it hit the error where symbolic expression appears in Boolean context.

Toy Example:

using ModelingToolkit, DifferentialEquations,Plots
using ModelingToolkit: t_nounits as t, D_nounits as D

function test(η ::Number)
    @assert abs(η) < 1
    return η
end
@register_symbolic test(η ::Number)

@component function myfun(;name)
    para = @parameters begin
        η
    end
    vars = @variables begin
        x(t)
     end
     
    eqs = [
        D(x) ~ -test(η)
    ]
    ODESystem(eqs, t, vars, para;name)
end

@named model = myfun()
sys = structural_simplify(model)
u0 = [sys.x => 1]
tspan = (0,10)
para = [sys.η => 0.5]
prob = ODEProblem(sys,u0,tspan,para)
sol = solve(prob)

Error:

ERROR: LoadError: TypeError: non-boolean (Num) used in boolean context
A symbolic expression appeared in a Boolean context. This error arises in situations where Julia expects a Bool, like
if boolean_condition             use ifelse(boolean_condition, then branch, else branch)
x && y                           use x & y
boolean_condition ? a : b        use ifelse(boolean_condition, a, b)

Is there a way around this?

Remove the type annotations on test and its registration. So just test(η) in both places. The type annotation on the function definition adds nothing when there is only one method and the restriction on the registration prevents all the necessary methods from being defined for you.

1 Like

Indeed, @register_symbolic test(η) would fix it. It only registers un-specified arugments.