Programmatically check if a variable is in an equation in ModelingToolkit.jl

Given an Equation generated by ModelingToolkit.jl, I want to check, programmatically, whether some variable exists in the equation. MWE:

using ModelingToolkit

@variables t            # independent variable (time)
dDt = Differential(t)   # define an operator for the differentiation w.r.t. time
@variables T(t) = 300.0       # temperature, in Kelvin

eq = (dDt(T) ~ T^4)

hasvariable(eq, T) # true, but how to code this function?

# I am also happy with having
hasvariable(eq.rhs, T)

context: i am developing some code that should in principle allow me to swap dynamical processes while generating a model, and if some variables exist or not, these processes need to be included or not.

The best thing is to check the incidence instead of analyzing this symbolically. @YingboMa might have a preference on how to expose this better.

One way would be to use get_variables or the in-place version from Symbolics to find the variables within eq.rhs and eq.lhs separately. Then you can compare against this list.

1 Like