ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

conditionally chosen array variable

Open baggepinnen opened this issue 2 weeks ago • 0 comments

I've found it hard to use if statements to control how equations involving arrays behave. This pattern is very common in modelica multibody models, but in MTK I frequently have to degrade parameters to be structural parameters to make it work. The following are two failed attempts

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@mtkmodel ArrayIf begin
    @parameters begin
        stable = true
    end
    @variables begin
        x(t)[1:2]
    end
    @equations begin
        if stable
            D(x) ~ -x
        else
            D(x) ~ x
        end
    end
end

@mtkbuild arrayif = ArrayIf()
julia> @mtkbuild arrayif = ArrayIf()
ERROR: TypeError: non-boolean (Num) used in boolean context
@mtkmodel ArrayIf begin
    @parameters begin
        stable = true
    end
    @variables begin
        x(t)[1:2]
    end
    @equations begin
        D(x) ~ ifelse(stable, -x, x)
    end
end

@mtkbuild arrayif = ArrayIf()
ERROR: MethodError: no method matching ifelse(::SymbolicUtils.BasicSymbolic{…}, ::Symbolics.ArrayOp{…}, ::SymbolicUtils.BasicSymbolic{…})

baggepinnen avatar Jun 19 '24 13:06 baggepinnen