ModiaSim / TinyModia.jl

Deprecated package (use instead Modia.jl)
MIT License
20 stars 4 forks source link

Cartesian pendulum #8

Open crlaugh opened 3 years ago

crlaugh commented 3 years ago

I tried to implement a single pendulum model in Cartesian coordinates, but got an error when I tried to instantiate it. Should this model work with the current release (0.7.2)?

using TinyModia
using ModiaBase
using ModiaPlot

# Cartesian pendulum model. 
CPendulum = Model(
    L = Var(1.0, parameter=true),
    m = Var(1.0, parameter=true),
    g_n = Var(9.80665, parameter=true),

    x = Var(init=1/sqrt(2)),
    y = Var(init=-1/sqrt(2)),
    vx = Var(),
    vy = Var(),
    F = Var(),

    equation = :[  
        der(x) = vx
        der(y) = vy

        m*der(vx) = (F/L)*x
        m*der(vy) = (F/L)*y - m*g_n

        x^2 + y^2 = L^2]
)

cPendulum = @instantiateModel(CPendulum)

When I try to instantiate this, I get

julia> cPendulum = @instantiateModel(CPendulum)

Instantiating model Main.CPendulum
ERROR: BoundsError: attempt to access 0-element Vector{Any} at index [1]
Stacktrace:
  [1] getindex
    @ .\array.jl:801 [inlined]
  [2] derivative(ex::Expr, timeInvariants::Base.KeySet{Any, OrderedCollections.OrderedDict{Any, Any}})
    @ ModiaBase.Differentiate C:\Users\laughman\.julia\packages\ModiaBase\igtEB\src\Differentiate.jl:84
  [3] (::ModiaBase.Differentiate.var"#4#8"{Base.KeySet{Any, OrderedCollections.OrderedDict{Any, Any}}})(e::Expr)
    @ ModiaBase.Differentiate .\none:0
  [4] iterate
    @ .\generator.jl:47 [inlined]
  [5] collect_to!(dest::Vector{Expr}, itr::Base.Generator{Vector{Any}, ModiaBase.Differentiate.var"#4#8"{Base.KeySet{Any, OrderedCollections.OrderedDict{Any, Any}}}}, offs::Int64, st::Int64)
    @ Base .\array.jl:724
  [6] collect_to_with_first!(dest::Vector{Expr}, v1::Expr, itr::Base.Generator{Vector{Any}, ModiaBase.Differentiate.var"#4#8"{Base.KeySet{Any, OrderedCollections.OrderedDict{Any, Any}}}}, st::Int64)
    @ Base .\array.jl:702
  [7] collect(itr::Base.Generator{Vector{Any}, ModiaBase.Differentiate.var"#4#8"{Base.KeySet{Any, OrderedCollections.OrderedDict{Any, Any}}}})
    @ Base .\array.jl:683
  [8] derivative(ex::Expr, timeInvariants::Base.KeySet{Any, OrderedCollections.OrderedDict{Any, Any}})
    @ ModiaBase.Differentiate C:\Users\laughman\.julia\packages\ModiaBase\igtEB\src\Differentiate.jl:98
  [9] assignAndBLT(equations::Vector{Any}, unknowns::Vector{Any}, parameters::OrderedCollections.OrderedDict{Any, Any}, Avar::Vector{Int64}, G::Vector{Vector{Int64}}, states::Vector{Any}, logDetails::Bool, log::Bool, logTiming::Bool)
    @ TinyModia C:\Users\laughman\.julia\packages\TinyModia\yrVLj\src\TinyModia.jl:197
 [10] instantiateModel(model::NamedTuple{(:class, :L, :m, :g_n, :x, :y, :vx, :vy, :F, :equation), Tuple{Symbol, NamedTuple{(:class, :value, :parameter), Tuple{Symbol, Float64, Bool}}, NamedTuple{(:class, :value, :parameter), Tuple{Symbol, Float64, Bool}}, NamedTuple{(:class, :value, :parameter), Tuple{Symbol, Float64, Bool}}, NamedTuple{(:class, :init), Tuple{Symbol, Float64}}, NamedTuple{(:class, :init), Tuple{Symbol, Float64}}, NamedTuple{(:class,), Tuple{Symbol}}, NamedTuple{(:class,), Tuple{Symbol}}, NamedTuple{(:class,), Tuple{Symbol}}, Expr}}; modelName::String, modelModule::Module, source::String, FloatType::Type, aliasReduction::Bool, unitless::Bool, log::Bool, logModel::Bool, logDetails::Bool, logStateSelection::Bool, logCode::Bool, logExecution::Bool, logTiming::Bool)
    @ TinyModia C:\Users\laughman\.julia\packages\TinyModia\yrVLj\src\TinyModia.jl:847
 [11] top-level scope
    @ REPL[3]:1

Is TinyModia able to compile the Cartesian pendulum at this point, or is this functionality still in process?

HildingElmqvist commented 3 years ago

There was a bug in derivative. I have fixed it now in the development branch. However, I get in the log:

`Sorted highest derivative equations: [assigned variable]: [differentiation] equation Strongly connected components are enclosed in [] [ 2: der(vy): m der(vy) = (F / L) y - m g_n 4: der(der(y)): der(der(y)) = der(vy) 5: der(der(x)): ((2 der(x)) der(x) + (2x) der(der(x))) + ((2 der(y)) der(y) + (2y) der(der(y))) = 0 3: der(vx): der(der(x)) = der(vx) 1: F: m der(vx) = (F / L) * x ]

Error message from getSortedAndSolvedAST for model CPendulum: Cannot transform to ODE because constraint equations cannot be explicitly solved by tearing Involved variables: y x Involved equations: x ^ 2 + y ^ 2 = L ^ 2

Model error: Aborting Aborting instantiateModel for CPendulum in Main.TestLaughman`

This is a limitation in Modia at the moment. See:

https://modiasim.github.io/TinyModia.jl/stable/Tutorial.html#.4-State-selection-(DAEs)

image