Reference
FMU-specific functions
FMIExchange.jl currently supports only FMI version 2.0. FMIExchange.jl automatically generates the callbacks required for handling all FMU events. To simulate the FMU, a CachedFMU2
must be wrapped in a CachedModel
(See Simulation Models)
FMIExchange.CachedFMU2
— TypeCachedFMU2{R}
CachedFMU2(fmuloc::String, start::Real, stop::Real, ins::AbstractVector, outs::AbstractVector, staterefs::AbstractVector, parameters::Union{Dict,Nothing}=nothing)
Wrapper struct for FMU2s. This is mainly used for dispatch purposes and ensures that the inputs, states and outputs are returned in the expected order
Arguments
fmuloc::String
fmu file pathstart::Real
simulation start timestop::Real
simulation stop timeins::AbstractVector
vector of fmu inputs (as string or symbol)outs::AbstractVector
vector of fmu outputs (as string or symbol)staterefs::AbstractVector
vector of fmu states (as string or symbol)parameters::Union{Dict, Nothing}=nothing
dictionary of fmu parameters or nothing if the default parameters should be used
Fields
fmu::FMU2
fmu object as defined byFMICore.jl
c::FMU2Component
fmu component object as defined byFMICore.jl
input_value_references::R
vector of non-human-readable references to the inputs (for working with the fmu)output_value_references::R
vector of non-human-readable references to the outputs (for working with the fmu)state_value_references::R
vector of non-human-readable references to the states (for working with the fmu)
FMIExchange.affectFMU!
— MethodaffectFMU!(model::CachedModel{<:CachedFMU2}, integrator, idx)
Handle events in the FMU
FMIExchange.condition!
— Methodcondition!(model::CachedModel{<:CachedFMU2}, x, t, integrator)
condition!(out, model::CachedModel{<:CachedFMU2}, x, t, integrator)
Return event indicators out
for the FMU (non-mutating and mutating version)
FMIExchange.get_time_callbacks
— Functionget_time_callbacks(model::CachedModel{<:CachedFMU2}, start, stop)
Return a callback to handle all time events in the FMU
FMIExchange.get_state_callbacks
— Functionget_state_callbacks(model::CachedModel{<:CachedFMU2})
Return a callback to handle state events in an FMU.
The returned callback is a continuous callback is able to detect events due to changes in the FMU state, but it will not be able to detect events due to changes in the FMU input.
See also get_input_callbacks
FMIExchange.get_step_callbacks
— Functionget_step_callbacks(model::CachedModel{<:CachedFMU2})
Return a callback to handle integrator step completion in the FMU
FMIExchange.get_input_callbacks
— Functionget_input_callbacks(model::CachedModel{<:CachedFMU2})
Return a callback to handle state events in an FMU.
The returned callback is a discrete callback will be able to detect events due to changes in the FMU input, but it will not be able to detect events due to changes in the FMU state.
See also get_state_callbacks
Simulation Models
Simulation models can be directly simulated with DifferentialEquations.jl like one normally would. They contain machine-readable address maps which allow to easily combine multiple simulation models in one simulation.
FMIExchange.AbstractSimModel
— TypeAbstractSimModel
Simulation model that can be directly simulated using DifferentialEquations.jl
. An AbstractSimModel
wraps an ODE with events such that it can be easily used in a simulation with multiple components.
Usage
A simulation model contains a number of inputs, states and outputs. States are subject to an ODE with events. Outputs can be calculated from the inputs and states of the model, but this calculation may not modify the state of the model.
FMIExchange.SimModel
— TypeSimModel <: AbstractSimModel
SimModel(f, g, ilength::Integer, olength::Integer, xlength::Integer, cbs; ioffset = 0, xoffset = 0)
Return a basic simulation model
Arguments
f
: function for calculating the derivative (refer toDifferentialEquations.jl
)g
: function for calculating the output. This function is called as followsg(out, state, in, time)
and should mutate theout
variableilength
: length of the model inputolength
: length of the model outputxlength
: length of the model statecbs
: callbacks associated with the modelioffset=0
: io memory buffer address offsetxoffset=0
: state memory buffer address offset
FMIExchange.CachedModel
— TypeCachedModel <: AbstractSimModel
CachedModel(fmu::CachedFMU2, uoffset=0, poffset=0)
Simulation model that contains caches for storing the state and input/output.
This is primarily used to accelerate FMUs: C-calls to the FMU cannot operate on array views. If a standard SimModel
were used, every C-call to the FMU would thus convert that view to a new array, allocating memory and massively slowing down the simulation. This struct preallocates fixed-size caches of types which can be directly passed to C-calls and do not need to be converted.
FMIExchange.AbstractSimModel
— Method(::AbstractSimModel)(u, p, t)
(::AbstractSimModel)(du, u, p, t)
Calculate the derivative of the model state. Refer to the documentation of DifferentialEquations.jl
FMIExchange.output!
— Methodoutput!(::AbstractSimModel, u, p, t)
Write the model output to the relevant indices of p
FMIExchange.get_callbacks
— Methodget_callbacks(model::AbstractSimModel, start, stop)
Return a vector of callbacks associated with the simulation model.
Arguments
model::AbstractSimModel
simulation modelstart
simulation start timestop
simulation stop time
Model Specifications
Model specifications contain human-readable address maps, and can be converted to simulation models for simulation.
FMIExchange.AbstractModelSpecification
— TypeAbstractModelSpecification{S1,S2}
Custom type with human-readable model information which allows automatic generation of simulation models
FMIExchange.ModelSpecification
— TypeModelSpecification{S1,S2,F,G} <: AbstractModelSpecification{S1,S2}
Fields
inputs::AbstractVector{S1}
: Vector of human-readable input namesoutputs::AbstractVector{S1}
: Vector of human-readable output namesstates::AbstractVector{S2}
: Vector of human-readable state namesdynamics::F
: Function for calculating the derivative (refer toDifferentialEquations.jl
)output::G
: Function for calculating the output. This function is called as followsoutput(out, state, in, time)
and should mutate theout
variable
See also SimModel
FMIExchange.FMUSpecification
— TypeFMUSpecification{S1, S2, S3<:Union{Symbol,AbstractString}, P<:Union{Dict{String,Float64},Nothing}} <: AbstractModelSpecification{S1,S2}
A model specification that can be converted to a CachedModel{<:CachedFMU2}
Fields
inputs::AbstractVector{S1}
: Vector of human-readable input namesoutputs::AbstractVector{S1}
: Vector of human-readable output namesstates::AbstractVector{S2}
: Vector of human-readable state namesfmu_location::S3
: Path to the FMUparameters::P
: Dictionary with FMU parameters (or nothing to use defaults)
FMIExchange.usize
— Methodusize(spec::AbstractModelSpecification)
Return the length of the state memory buffer
FMIExchange.psize
— Methodpsize(spec::AbstractModelSpecification)
Return the length of the io memory buffer
FMIExchange.create_model
— Functioncreate_model(::AbstractModelSpecification, uoffset=0, poffset=0; start=0.0, stop=1.0)
create_model(specs::AbstractVector{<:AbstractModelSpecification}, uoffset=0, poffset=0; start=0.0, stop=1.0)
Return a simulation model (<:AbstractSimModel
) with machine-readable address maps
If a vector of model specifications is provided, return a vector of simulation models with non-overlapping address maps
See also AbstractSimModel
Simulation composition functionality
Below functions are meant to simplify composing complex simulations with multiple models.
FMIExchange.address_map
— Methodaddress_map(ins, outs, states, m::AbstractSimModel)
address_map(spec::AbstractModelSpecification, uoffset=0, poffset=0)
address_map(specs::AbstractVector{<:AbstractModelSpecification}, uoffset=0, poffset=0)
Return two dictionaries linking human-readable strings/symbols for model inputs/outputs and states to machine-readable indices
FMIExchange.link_models
— Methodlink_models(src, dst)
link_models(src, dst, iomap)
Return a FunctionCallingCallback that connects inputs and outputs of models. This does not automatically resolve algebraic loops!
Arguments
src
human-readable strings/symbols or machine-readable indices of the model io that will be copieddst
human-readable strings/symbols or machine-readable indices of the copy destinationsiomap
dictionary mapping the human-readable strings/symbols to indices (if src and dst provided as string/symbol)
FMIExchange.output_callback
— Methodoutput_callback(m::AbstractSimModel)
output_callback(ms::AbstractVector{<:AbstractSimModel})
Return a callback that calls the output function of the simulation model m
(or every simulation model in ms
) after every integration step
FMIExchange.dynamics
— Methoddynamics(models)
Return a single DifferentialEquations.jl
-compatible function that calculates derivatives of all models in models