Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FMU without state variables #142

Closed
AnHeuermann opened this issue Aug 11, 2022 · 3 comments · Fixed by #185
Closed

FMU without state variables #142

AnHeuermann opened this issue Aug 11, 2022 · 3 comments · Fixed by #185
Labels
enhancement New feature or request

Comments

@AnHeuermann
Copy link

Description

I have a (2.0, ME) FMU that doesn't have any state variables. It seems that FMI.jl is not catching these and calls the ODE solver on an empty array of states which results in a crash.

How to reproduce

mwe.mo

model Mwe
  Real x;
  Real y;
equation
  1 = x^2 + y*time;
  0 = x + y^2;
end Mwe;

Create 2.0 ModelExchange FMU with e.g. OpenModelica.
Windows FMU (remove the .zip part): Mwe.fmu.zip
Ubuntu Focal FMU: Mwe.fmu.zip

julia> using FMI
julia> myFMU = fmiLoad("Mwe.fmu");
julia> simData = fmiSimulate(myFMU, 0.0, 1.0; recordValues=["x", "y"])
ERROR: BoundsError: attempt to access 0-element Vector{Float64} at index [1]
Stacktrace:
  [1] getindex
    @ ./array.jl:861 [inlined]
  [2] ODE_DEFAULT_NORM
    @ ~/.julia/packages/DiffEqBase/Da1WO/src/common_defaults.jl:25 [inlined]
  [3] __init(prob::SciMLBase.ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Any}, SciMLBase.ODEFunction{true, FMI.var"#84#93"{FMU2Component}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, alg::OrdinaryDiffEq.CompositeAlgorithm{Tuple{OrdinaryDiffEq.Vern7, OrdinaryDiffEq.Rodas4{0, false, LinearSolve.GenericLUFactorization{LinearAlgebra.RowMaximum}, typeof(OrdinaryDiffEq.DEFAULT_PRECS), Val{:forward}, true, nothing}}, OrdinaryDiffEq.AutoSwitch{OrdinaryDiffEq.Vern7, OrdinaryDiffEq.Rodas4{0, false, Nothing, typeof(OrdinaryDiffEq.DEFAULT_PRECS), Val{:forward}, true, nothing}, Rational{Int64}, Int64}}, timeseries_init::Tuple{}, ts_init::Tuple{}, ks_init::Tuple{}, recompile::Type{Val{true}}; saveat::Tuple{}, tstops::Tuple{}, d_discontinuities::Tuple{}, save_idxs::Nothing, save_everystep::Bool, save_on::Bool, save_start::Bool, save_end::Nothing, callback::SciMLBase.CallbackSet{Tuple{}, Tuple{SciMLBase.DiscreteCallback{DiffEqCallbacks.var"#27#28", DiffEqCallbacks.FunctionCallingAffect{FMI.var"#89#98"{Vector{UInt32}, Nothing, FMU2Component, FMICore.FMU2Solution}, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.functioncalling_initialize), typeof(SciMLBase.FINALIZE_DEFAULT)}, SciMLBase.DiscreteCallback{DiffEqCallbacks.var"#30#31", DiffEqCallbacks.SavingAffect{FMI.var"#91#100"{Vector{UInt32}, Vector{UInt32}, Nothing, FMU2Component}, Float64, Tuple{Float64, Float64}, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.saving_initialize), typeof(SciMLBase.FINALIZE_DEFAULT)}}}, dense::Bool, calck::Bool, dt::Float64, dtmin::Nothing, dtmax::Float64, force_dtmin::Bool, adaptive::Bool, gamma::Rational{Int64}, abstol::Nothing, reltol::Float64, qmin::Rational{Int64}, qmax::Int64, qsteady_min::Int64, qsteady_max::Int64, beta1::Nothing, beta2::Nothing, qoldinit::Rational{Int64}, controller::Nothing, fullnormalize::Bool, failfactor::Int64, maxiters::Int64, internalnorm::typeof(DiffEqBase.ODE_DEFAULT_NORM), internalopnorm::typeof(LinearAlgebra.opnorm), isoutofdomain::typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), unstable_check::typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), verbose::Bool, timeseries_errors::Bool, dense_errors::Bool, advance_to_tstop::Bool, stop_at_next_tstop::Bool, initialize_save::Bool, progress::Bool, progress_steps::Int64, progress_name::String, progress_message::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), userdata::Nothing, allow_extrapolation::Bool, initialize_integrator::Bool, alias_u0::Bool, alias_du0::Bool, initializealg::OrdinaryDiffEq.DefaultInit, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol, Symbol}, NamedTuple{(:default_set, :second_time), Tuple{Bool, Bool}}})
    @ OrdinaryDiffEq ~/.julia/packages/OrdinaryDiffEq/5B7d7/src/solve.jl:274
  [4] #__solve#502
    @ ~/.julia/packages/OrdinaryDiffEq/5B7d7/src/solve.jl:4 [inlined]
  [5] __solve(::SciMLBase.ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Any}, SciMLBase.ODEFunction{true, FMI.var"#84#93"{FMU2Component}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, ::Nothing; default_set::Bool, kwargs::Base.Pairs{Symbol, Any, NTuple{4, Symbol}, NamedTuple{(:second_time, :callback, :dtmax, :reltol), Tuple{Bool, SciMLBase.CallbackSet{Tuple{}, Tuple{SciMLBase.DiscreteCallback{DiffEqCallbacks.var"#27#28", DiffEqCallbacks.FunctionCallingAffect{FMI.var"#89#98"{Vector{UInt32}, Nothing, FMU2Component, FMICore.FMU2Solution}, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.functioncalling_initialize), typeof(SciMLBase.FINALIZE_DEFAULT)}, SciMLBase.DiscreteCallback{DiffEqCallbacks.var"#30#31", DiffEqCallbacks.SavingAffect{FMI.var"#91#100"{Vector{UInt32}, Vector{UInt32}, Nothing, FMU2Component}, Float64, Tuple{Float64, Float64}, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.saving_initialize), typeof(SciMLBase.FINALIZE_DEFAULT)}}}, Float64, Float64}}})
    @ DifferentialEquations ~/.julia/packages/DifferentialEquations/TZ7Qg/src/default_solve.jl:9
  [6] #__solve#52
    @ ~/.julia/packages/DiffEqBase/Da1WO/src/solve.jl:1036 [inlined]
  [7] #solve_call#28
    @ ~/.julia/packages/DiffEqBase/Da1WO/src/solve.jl:448 [inlined]
  [8] solve_up(::SciMLBase.ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Any}, SciMLBase.ODEFunction{true, FMI.var"#84#93"{FMU2Component}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, ::Nothing, ::Vector{Float64}, ::Vector{Any}; kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:callback, :dtmax, :reltol), Tuple{SciMLBase.CallbackSet{Tuple{}, Tuple{SciMLBase.DiscreteCallback{DiffEqCallbacks.var"#27#28", DiffEqCallbacks.FunctionCallingAffect{FMI.var"#89#98"{Vector{UInt32}, Nothing, FMU2Component, FMICore.FMU2Solution}, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.functioncalling_initialize), typeof(SciMLBase.FINALIZE_DEFAULT)}, SciMLBase.DiscreteCallback{DiffEqCallbacks.var"#30#31", DiffEqCallbacks.SavingAffect{FMI.var"#91#100"{Vector{UInt32}, Vector{UInt32}, Nothing, FMU2Component}, Float64, Tuple{Float64, Float64}, DataStructures.BinaryMinHeap{Float64}, Vector{Float64}}, typeof(DiffEqCallbacks.saving_initialize), typeof(SciMLBase.FINALIZE_DEFAULT)}}}, Float64, Float64}}})
    @ DiffEqBase ~/.julia/packages/DiffEqBase/Da1WO/src/solve.jl:805
  [9] #solve#33
    @ ~/.julia/packages/DiffEqBase/Da1WO/src/solve.jl:771 [inlined]
 [10] fmi2SimulateME(fmu::FMU2, c::Nothing, t_start::Float64, t_stop::Float64; tolerance::Nothing, dt::Nothing, solver::Nothing, customFx::Nothing, recordValues::Vector{String}, saveat::Nothing, x0::Nothing, setup::Nothing, reset::Nothing, instantiate::Nothing, freeInstance::Nothing, terminate::Nothing, inputValueReferences::Nothing, inputFunction::Nothing, parameters::Nothing, dtmax::Nothing, callbacks::Vector{Any}, showProgress::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ FMI ~/.julia/packages/FMI/1vhoW/src/FMI2_sim.jl:814
 [11] #fmi2Simulate#107
    @ ~/.julia/packages/FMI/1vhoW/src/FMI2_sim.jl:1034 [inlined]
 [12] #fmi2Simulate#15
    @ ~/.julia/packages/FMI/1vhoW/src/FMI2_comp_wraps.jl:17 [inlined]
 [13] fmiSimulate(::FMU2, ::Float64, ::Vararg{Float64}; kwargs::Base.Pairs{Symbol, Vector{String}, Tuple{Symbol}, NamedTuple{(:recordValues,), Tuple{Vector{String}}}})
    @ FMI ~/.julia/packages/FMI/1vhoW/src/FMI.jl:527
 [14] top-level scope
    @ REPL[17]:1

Versions

  • Julia Version 1.7.2
    Commit bf53498635 (2022-02-06 15:21 UTC)
    Platform Info:
    OS: Linux (x86_64-pc-linux-gnu)
    CPU: Intel(R) Xeon(R) Gold 6248R CPU @ 3.00GHz
    WORD_SIZE: 64
    LIBM: libopenlibm
    LLVM: libLLVM-12.0.1 (ORCJIT, cascadelake)
  • FMI.jl: 0.9.0
@ThummeTo
Copy link
Owner

ThummeTo commented Aug 29, 2022

Didn't know that this is even possible :-D
In this case we should use a custom "simulation" loop without ODE-solver I guess?
So basically a loop over time that only sets FMU inputs and retrieves values of interest?
Best regards!

@ThummeTo
Copy link
Owner

ThummeTo commented Sep 9, 2022

This is not as trivial as I thought, because events must be handled correctly. Because we have no ODE, we can't use a ODE-solver and therefore we can't use the callbacks from DiffEqCallbacks.jl for event handling...

@ThummeTo ThummeTo added the enhancement New feature or request label Oct 13, 2022
@JoKircher
Copy link
Contributor

Will have a look

@ThummeTo ThummeTo mentioned this issue May 8, 2023
@ThummeTo ThummeTo linked a pull request Jul 3, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants