FMI.jl Library Functions

Many of the functions in this library are based on already defined functions of the FMIImport.jl library.

Simulate FMUs

FMIImport.loadFMUFunction
loadFMU(pathToFMU; unpackPath, cleanup, type)

Loads an FMU, independent of the used FMI-version (the version is checked during unpacking the archive).

Arguments

  • path::String the path pointing on the FMU file.

Keywords

  • unpackPath::Union{String, Nothing}=nothing the optional unpack path, if nothing a temporary directory depending on the OS is picked.
  • cleanup::Bool=true a boolean indicating whether the temporary directory should be cleaned automatically.
  • type::Union{Symbol, Nothing}=nothing the type of FMU (:CS, :ME, :SE), if multiple types are available. If nothing one of the available types is chosen automatically with the priority CS > ME > SE.
source
FMI.simulateFunction
simulate(fmu, instance=nothing, tspan=nothing; kwargs...)
simulate(fmu, tspan; kwargs...)
simulate(instance, tspan; kwargs...)

Starts a simulation of the FMU2 for the instantiated type: CS, ME or SE (this is selected automatically or during loading of the FMU). You can force a specific simulation mode by calling simulateCS, simulateME or simulateSE directly.

Arguments

  • fmu::FMU: The FMU to be simulated.
  • c::Union{FMUInstance, Nothing}=nothing: The instance (FMI3) or component (FMI2) of the FMU, nothing if not available.
  • tspan::Union{Tuple{Float64, Float64}, Nothing}=nothing: Simulation-time-span as tuple (default = nothing: use default value from FMU's model description or (0.0, 1.0) if not specified)

Keyword arguments

  • recordValues::fmi2ValueReferenceFormat = nothing: Array of variables (Strings or variableIdentifiers) to record. Results are returned as DiffEqCallbacks.SavedValues
  • saveat = nothing: Time points to save (interpolated) values at (default = nothing: save at each solver timestep)
  • setup::Bool: call fmi2SetupExperiment, fmi2EnterInitializationMode and fmi2ExitInitializationMode before the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • reset::Bool: call fmi2Reset before each the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • instantiate::Bool: call fmi2Instantiate! simulate on a new created instance (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • freeInstance::Bool: call fmi2FreeInstance at the end of the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • terminate::Bool: call fmi2Terminate at the end of the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • inputValueReferences::fmi2ValueReferenceFormat = nothing: Input variables (Strings or variableIdentifiers) to set at each simulation step
  • inputFunction = nothing: Function to get values for the input variables at each simulation step.
  • parameters::Union{Dict{<:Any, <:Any}, Nothing} = nothing: Dict of parameter variables (strings or variableIdentifiers) and values (Real, Integer, Boolean, String) to set parameters during initialization
  • showProgress::Bool = true: print simulation progress meter in REPL

Input function pattern

[c: current component, u: current state ,t: current time, returning array of values to be passed to fmi2SetReal(..., inputValueReferences, inputFunction(...)) or fmi3SetFloat64]:

  • inputFunction(t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, u::AbstractVector{<:Real})
  • inputFunction(x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})

Returns:

See also simulate, simulateME, simulateCS, simulateSE.

source
FMI.simulateCSFunction
simulateCS(fmu, instance=nothing, tspan=nothing; kwargs...)
simulateCS(fmu, tspan; kwargs...)
simulateCS(instance, tspan; kwargs...)

Simulate CS-FMU for the given simulation time interval. State- and Time-Events are handled internally by the FMU.

Arguments

  • fmu::FMU: The FMU to be simulated.
  • c::Union{FMUInstance, Nothing}=nothing: The instance (FMI3) or component (FMI2) of the FMU, nothing if not available.
  • tspan::Union{Tuple{Float64, Float64}, Nothing}=nothing: Simulation-time-span as tuple (default = nothing: use default value from FMU's model description or (0.0, 1.0) if not specified)

Keyword arguments

  • tolerance::Union{Real, Nothing} = nothing: The tolerance for the internal FMU solver.
  • recordValues::fmi2ValueReferenceFormat = nothing: Array of variables (Strings or variableIdentifiers) to record. Results are returned as DiffEqCallbacks.SavedValues
  • saveat = nothing: Time points to save (interpolated) values at (default = nothing: save at each solver timestep)
  • setup::Bool: call fmi2SetupExperiment, fmi2EnterInitializationMode and fmi2ExitInitializationMode before the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • reset::Bool: call fmi2Reset before each the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • instantiate::Bool: call fmi2Instantiate! simulate on a new created instance (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • freeInstance::Bool: call fmi2FreeInstance at the end of the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • terminate::Bool: call fmi2Terminate at the end of the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • inputValueReferences::fmi2ValueReferenceFormat = nothing: Input variables (Strings or variableIdentifiers) to set at each simulation step
  • inputFunction = nothing: Function to get values for the input variables at each simulation step.
  • parameters::Union{Dict{<:Any, <:Any}, Nothing} = nothing: Dict of parameter variables (strings or variableIdentifiers) and values (Real, Integer, Boolean, String) to set parameters during initialization
  • showProgress::Bool = true: print simulation progress meter in REPL

Input function pattern

[c: current component, u: current state ,t: current time, returning array of values to be passed to fmi2SetReal(..., inputValueReferences, inputFunction(...)) or fmi3SetFloat64]:

  • inputFunction(t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, u::AbstractVector{<:Real})
  • inputFunction(x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})

Returns:

See also simulate, simulateME, simulateSE.

source
FMI.simulateSEFunction
simulateSE(fmu, instance=nothing, tspan=nothing; kwargs...)
simulateSE(fmu, tspan; kwargs...)
simulateSE(instance, tspan; kwargs...)

To be implemented ...

Arguments

  • fmu::FMU3: The FMU to be simulated. Note: SE is only available in FMI3.
  • c::Union{FMU3Instance, Nothing}=nothing: The instance (FMI3) of the FMU, nothing if not available.
  • tspan::Union{Tuple{Float64, Float64}, Nothing}=nothing: Simulation-time-span as tuple (default = nothing: use default value from FMU's model description or (0.0, 1.0) if not specified)

Keyword arguments

  • To be implemented ...

Returns:

See also simulate, simulateME, simulateCS.

source
FMI.simulateMEFunction
simulateME(fmu, instance=nothing, tspan=nothing; kwargs...)
simulateME(fmu, tspan; kwargs...)
simulateME(instance, tspan; kwargs...)

Simulate ME-FMU for the given simulation time interval. State- and Time-Events are handled correctly.

Arguments

  • fmu::FMU: The FMU to be simulated.
  • c::Union{FMUInstance, Nothing}=nothing: The instance (FMI3) or component (FMI2) of the FMU, nothing if not available.
  • tspan::Union{Tuple{Float64, Float64}, Nothing}=nothing: Simulation-time-span as tuple (default = nothing: use default value from FMU's model description or (0.0, 1.0) if not specified)

Keyword arguments

  • solver = nothing: Any Julia-supported ODE-solver (default = nothing: use DifferentialEquations.jl default solver)
  • recordValues::fmi2ValueReferenceFormat = nothing: Array of variables (Strings or variableIdentifiers) to record. Results are returned as DiffEqCallbacks.SavedValues
  • recordEventIndicators::Union{AbstractArray{<:Integer, 1}, UnitRange{<:Integer}, Nothing} = nothing: Array or Range of event indicators to record
  • recordEigenvalues::Bool=false: compute and record eigenvalues
  • saveat = nothing: Time points to save (interpolated) values at (default = nothing: save at each solver timestep)
  • x0::Union{AbstractArray{<:Real}, Nothing} = nothing: initial fmu State (default = nothing: use current or default-initial fmu state)
  • setup::Bool: call fmi2SetupExperiment, fmi2EnterInitializationMode and fmi2ExitInitializationMode before the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • reset::Bool: call fmi2Reset before each the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • instantiate::Bool: call fmi2Instantiate! simulate on a new created instance (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • freeInstance::Bool: call fmi2FreeInstance at the end of the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • terminate::Bool: call fmi2Terminate at the end of the simulation (default = nothing: use value from fmu's FMUExecutionConfiguration)
  • inputValueReferences::fmi2ValueReferenceFormat = nothing: Input variables (Strings or variableIdentifiers) to set at each simulation step
  • inputFunction = nothing: Function to get values for the input variables at each simulation step.
  • parameters::Union{Dict{<:Any, <:Any}, Nothing} = nothing: Dict of parameter variables (strings or variableIdentifiers) and values (Real, Integer, Boolean, String) to set parameters during initialization
  • callbacksBefore = []: callbacks to call before the internal callbacks for state- and time-events are called
  • callbacksAfter = []: callbacks to call after the internal callbacks for state- and time-events are called
  • showProgress::Bool = true: print simulation progress meter in REPL
  • solveKwargs...: keyword arguments that get passed onto the solvers solve call

Input function pattern

[c: current component, u: current state ,t: current time, returning array of values to be passed to fmi2SetReal(..., inputValueReferences, inputFunction(...)) or fmi3SetFloat64]:

  • inputFunction(t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, u::AbstractVector{<:Real})
  • inputFunction(x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})
  • inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})

Returns:

See also simulate, simulateCS, simulateSE.

source
FMIImport.unloadFMUFunction
unloadFMU(fmu::FMU2, cleanUp::Bool=true; secure_pointers::Bool=true)

Unload a FMU. Free the allocated memory, close the binaries and remove temporary zip and unziped FMU model description.

Arguments

  • fmu::FMU2: Mutable struct representing a FMU and all it instantiated instances in the FMI 2.0.2 Standard.
  • cleanUp::Bool= true: Defines if the file and directory should be deleted.

Keywords

  • secure_pointers=true whether pointers to C-functions should be overwritten with dummies with Julia assertions, instead of pointing to dead memory (slower, but more user safe)
source
FMIImport.reloadFunction
reload(fmu::FMU2)

Reloads the FMU-binary. This is useful, if the FMU does not support a clean reset implementation.

Arguments

  • fmu::FMU2: Mutable struct representing a FMU and all it instantiated instances in the FMI 2.0.2 Standard.
source

Handling Value References

FMIBase.stringToValueReferenceFunction
stringToValueReference(obj, names)

Finds the value reference for a given name.

Arguments

  • obj ∈ (fmi2ModelDescription, fmi3ModelDescription, FMU2, FMU3) the FMI object
  • names ∈ (String, AbstractVector{String}) the value refernce name or multiple names

Return

Returns a single or an array of fmi2ValueReferences (FMI2) or fmi3ValueReferences (FMI3) corresponding to the variable name(s).

source

External/additional functions

FMIBase.getModelNameFunction
getModelName(md::fmi2ModelDescription)

Returns the tag 'modelName' from the model description.

Arguments

  • md::fmi2ModelDescription: Struct which provides the static information of ModelVariables.

Returns

  • modelName::String: Returns the tag 'modelName' from the model description.
source
FMIBase.getNumberOfStatesFunction
getNumberOfStates(md::fmi2ModelDescription)

Returns the number of states of the FMU.

Arguments

  • md::fmi2ModelDescription: Struct which provides the static information of ModelVariables.

Returns

  • Returns the number of states of the FMU.
source
FMIBase.isModelExchangeFunction
isModelExchange(md::fmi2ModelDescription)

Returns true, if the FMU supports model exchange

Arguments

  • md::fmi2ModelDescription: Struct which provides the static information of ModelVariables.

Returns

  • ::Bool: Returns true, if the FMU supports model exchange
source
FMIBase.isCoSimulationFunction
isCoSimulation(md::fmi2ModelDescription)

Returns true, if the FMU supports co simulation

Arguments

  • md::fmi2ModelDescription: Struct which provides the static information of ModelVariables.

Returns

  • ::Bool: Returns true, if the FMU supports co simulation
source
FMIBase.getStateFunction
getState(solution::FMUSolution, vr::fmi2ValueReferenceFormat; isIndex::Bool=false)

Returns the solution state.

Arguments

  • solution::FMUSolution: Struct contains information about the solution value, success, state and events of a specific FMU.
  • vr::fmi2ValueReferenceFormat: wildcards for how a user can pass a fmi[X]ValueReference (default = md.valueReferences)

More detailed: fmi2ValueReferenceFormat = Union{Nothing, String, Array{String,1}, fmi2ValueReference, Array{fmi2ValueReference,1}, Int64, Array{Int64,1}, Symbol}

  • isIndex::Bool=false: Argument isIndex exists to check if vr ist the specific solution element ("index") that equals the given fmi2ValueReferenceFormat

Return

  • If he length of the given references equals 1, each element u in the collection solution.states.u, it is selecting the element at the index represented by indices[1] and returns it.

Thus, the collect() function is taking the generator expression and returning an array of the selected elements.

  • If more than one reference is given, the same process takes place as before. The difference is that now more than one index is accessed.

Source

source
FMIBase.getTimeFunction
getTime(solution::FMUSolution)

Returns the Solution time.

Arguments

  • solution::FMUSolution: Struct contains information about the solution value, success, state and events of a specific FMU.

Return

  • solution.states.t::tType: solution.state is a struct ODESolution with attribute t. t is the time points corresponding to the saved values of the ODE solution.
  • solution.values.t::tType: solution.value is a struct ODESolution with attribute t.t the time points corresponding to the saved values of the ODE solution.
  • If no solution time is found nothing is returned.

#Source

source
FMIBase.getStateDerivativeFunction
getStateDerivative(solution::FMUSolution, vr::fmi2ValueReferenceFormat; isIndex::Bool=false)

Returns the solution state derivative.

Arguments

  • solution::FMUSolution: Struct contains information about the solution value, success, state and events of a specific FMU.
  • vr::fmi2ValueReferenceFormat: wildcards for how a user can pass a fmi[X]ValueReference (default = md.valueReferences)

More detailed: fmi2ValueReferenceFormat = Union{Nothing, String, Array{String,1}, fmi2ValueReference, Array{fmi2ValueReference,1}, Int64, Array{Int64,1}, Symbol}

  • isIndex::Bool=false: Argument isIndex exists to check if vr ist the specific solution element ("index") that equals the given fmi2ValueReferenceFormat

Return

  • If the length of the given references equals 1, each element myt in the collection solution.states.t is selecting the derivative of the solution states represented by indices[1] in respect to time, at time myt and returns its it.

Thus, the collect() function is taking the generator expression and returning an array of the selected derivatives.

  • If more than one reference is given, the same process takes place as before. The difference is that now more than one index is accessed.

Source

source

fmiSet fmiGet fmiGet! fmiCanGetSetState fmiSetState fmiFreeState! fmiGetDependencies fmiProvidesDirectionalDerivative

Visualize simulation results

fmiPlot fmiPlot! Plots.plot

Save/load simulation results

fmiSaveSolution fmiSaveSolutionJLD2 fmiSaveSolutionMAT fmiSaveSolutionCSV fmiLoadSolution fmiLoadSolutionJLD2

FMI2 specific

fmi2Info fmi2Simulate fmi2VariableDependsOnVariable fmi2GetDependencies fmi2PrintDependencies

FMI3 specific

fmi3Info fmi3Simulate fmi3VariableDependsOnVariable fmi3GetDependencies fmi3PrintDependencies