Invalid Shapes error on creating ODEProblem from MTK system

I have been running into this problem in many situations and am at a loss to figure it out. It always seems to occur with ifelse() statements, abs(), and the like. Unfortunately I have to have a lot of these types of statements to handle bi-directional fluid flow. I should say I am using stream variables with instream(). You often have to calculate fluid properties for each potential flow direction.

The common thread to all of them is the

SymbolicUtils.Unknown(2))

argument to whatever function that errors out.

I am working on finding a MWE but it is very difficult as it almost seems to be a “Heisenbug”-- it seems to occur inconsistently, depending on what exactly I have run, whether I have restarted the REPL.

I have had some success using the @register_symbolic macro to hide these conditional like statements.

Upon creating and ODEProblem

prob = ODEProblem(sys, u0, (0.0, sim_time); guesses=new_guess, use_scc=false)

I sometimes get an error of this form:

ERROR: ArgumentError: Invalid shapes for <=: (UnitRange{Int64}[], SymbolicUtils.Unknown(2)).
Stacktrace:
  [1] _throw_array(::Function, ::SymbolicUtils.SmallVec{UnitRange{Int64}, Vector{UnitRange{Int64}}}, ::Vararg{Any})
    @ SymbolicUtils C:\Users\mark.garnett\.julia\packages\SymbolicUtils\kFlic\src\methods.jl:276
  [2] promote_shape(::typeof(<=), sha::Union{…}, shb::Union{…})
    @ SymbolicUtils C:\Users\mark.garnett\.julia\packages\SymbolicUtils\kFlic\src\methods.jl:589
  [3] maketerm(::Type{…}, f::Any, args::SymbolicUtils.SmallVec{…}, metadata::Nothing; type::Any)
    @ SymbolicUtils C:\Users\mark.garnett\.julia\packages\SymbolicUtils\kFlic\src\terminterface.jl:332
  [4] maketerm(::Type{…}, f::Any, args::SymbolicUtils.SmallVec{…}, metadata::Nothing)
    @ SymbolicUtils C:\Users\mark.garnett\.julia\packages\SymbolicUtils\kFlic\src\terminterface.jl:259
  [5] combine_fold(::Type{…}, op::Any, args::SymbolicUtils.SmallVec{…}, meta::Union{…}, can_fold::Bool)
    @ SymbolicUtils C:\Users\mark.garnett\.julia\packages\SymbolicUtils\kFlic\src\substitute.jl:222
  [6] (::SymbolicUtils.DefaultSubstituter{…})(ex::SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{…})
    @ SymbolicUtils C:\Users\mark.garnett\.julia\packages\SymbolicUtils\kFlic\src\substitute.jl:196
  [7] (::SymbolicUtils.DefaultSubstituter{…})(ex::SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{…}) (repeats 2 times)
    @ SymbolicUtils C:\Users\mark.garnett\.julia\packages\SymbolicUtils\kFlic\src\substitute.jl:180
  [8] (::Symbolics.FixpointSubstituter{…})(ex::SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{…})
    @ Symbolics C:\Users\mark.garnett\.julia\packages\Symbolics\pC2pn\src\variable.jl:437
  [9] maybe_build_initialization_problem(sys::System, iip::Bool, op::ModelingToolkitBase.AtomicArrayDict{…}, t::Float64, guesses::Dict{…}; time_dependent_init::Bool, u0_constructor::Function, p_constructor::Function, floatT::Type, initialization_eqs::Vector{…}, use_scc::Bool, eval_expression::Bool, eval_module::Module, missing_guess_value::ModelingToolkitBase.MissingGuessValue.var"typeof(MissingGuessValue)", implicit_dae::Bool, is_steadystateprob::Bool, expression::Type, kwargs::@Kwargs{…})
    @ ModelingToolkitBase C:\Users\mark.garnett\.julia\packages\ModelingToolkitBase\Rcink\src\systems\problem_utils.jl:1382
 [10] process_SciMLProblem(constructor::Type, sys::System, op::Vector{…}; build_initializeprob::Bool, implicit_dae::Bool, t::Float64, guesses::Dict{…}, warn_initialize_determined::Bool, initialization_eqs::Vector{…}, eval_expression::Bool, eval_module::Module, fully_determined::Nothing, check_initialization_units::Bool, u0_eltype::Nothing, tofloat::Bool, u0_constructor::typeof(identity), p_constructor::typeof(identity), check_length::Bool, symbolic_u0::Bool, warn_cyclic_dependency::Bool, circular_dependency_max_cycle_length::Int64, circular_dependency_max_cycles::Int64, initsys_mtkcompile_kwargs::@NamedTuple{}, substitution_limit::Int64, use_scc::Bool, time_dependent_init::Bool, algebraic_only::Bool, missing_guess_value::ModelingToolkitBase.MissingGuessValue.var"typeof(MissingGuessValue)", allow_incomplete::Bool, is_initializeprob::Bool, is_steadystateprob::Bool, return_operating_point::Bool, kwargs::@Kwargs{…})
    @ ModelingToolkitBase C:\Users\mark.garnett\.julia\packages\ModelingToolkitBase\Rcink\src\systems\problem_utils.jl:1603
 [11] (ODEProblem{…})(sys::System, op::Any, tspan::Tuple{…}; callback::Any, check_length::Bool, eval_expression::Bool, expression::Type, eval_module::Module, check_compatibility::Bool, _skip_events::Bool, kwargs::@Kwargs{…})
    @ ModelingToolkitBase C:\Users\mark.garnett\.julia\packages\ModelingToolkitBase\Rcink\src\problems\odeproblem.jl:111
 [12] ODEProblem
    @ C:\Users\mark.garnett\.julia\packages\ModelingToolkitBase\Rcink\src\problems\odeproblem.jl:101 [inlined]
 [13] ODEProblem
    @ .\none:-1 [inlined]
 [14] #ODEProblem#792
    @ .\none:-1 [inlined]

Has anyone run into this? Using MTK v11.26.0