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 — Type
CachedFMU2{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::Stringfmu file pathstart::Realsimulation start timestop::Realsimulation stop timeins::AbstractVectorvector of fmu inputs (as string or symbol)outs::AbstractVectorvector of fmu outputs (as string or symbol)staterefs::AbstractVectorvector of fmu states (as string or symbol)parameters::Union{Dict, Nothing}=nothingdictionary of fmu parameters or nothing if the default parameters should be used
Fields
fmu::FMU2fmu object as defined byFMICore.jlc::FMU2Componentfmu component object as defined byFMICore.jlinput_value_references::Rvector of non-human-readable references to the inputs (for working with the fmu)output_value_references::Rvector of non-human-readable references to the outputs (for working with the fmu)state_value_references::Rvector of non-human-readable references to the states (for working with the fmu)
FMIExchange.affectFMU! — Method
affectFMU!(model::CachedModel{<:CachedFMU2}, integrator, idx)Handle events in the FMU
FMIExchange.condition! — Method
condition!(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 — Function
get_time_callbacks(model::CachedModel{<:CachedFMU2}, start, stop)Return a callback to handle all time events in the FMU
FMIExchange.get_state_callbacks — Function
get_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 — Function
get_step_callbacks(model::CachedModel{<:CachedFMU2})Return a callback to handle integrator step completion in the FMU
FMIExchange.get_input_callbacks — Function
get_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 — Type
AbstractSimModelSimulation 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 — Type
SimModel <: 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 theoutvariableilength: 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 — Type
CachedModel <: 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! — Method
output!(::AbstractSimModel, u, p, t)Write the model output to the relevant indices of p
FMIExchange.get_callbacks — Method
get_callbacks(model::AbstractSimModel, start, stop)Return a vector of callbacks associated with the simulation model.
Arguments
model::AbstractSimModelsimulation modelstartsimulation start timestopsimulation stop time
Model Specifications
Model specifications contain human-readable address maps, and can be converted to simulation models for simulation.
FMIExchange.AbstractModelSpecification — Type
AbstractModelSpecification{S1,S2}Custom type with human-readable model information which allows automatic generation of simulation models
FMIExchange.ModelSpecification — Type
ModelSpecification{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 theoutvariable
See also SimModel
FMIExchange.FMUSpecification — Type
FMUSpecification{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 — Method
usize(spec::AbstractModelSpecification)Return the length of the state memory buffer
FMIExchange.psize — Method
psize(spec::AbstractModelSpecification)Return the length of the io memory buffer
FMIExchange.create_model — Function
create_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 — Method
address_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 — Method
link_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
srchuman-readable strings/symbols or machine-readable indices of the model io that will be copieddsthuman-readable strings/symbols or machine-readable indices of the copy destinationsiomapdictionary mapping the human-readable strings/symbols to indices (if src and dst provided as string/symbol)
FMIExchange.output_callback — Method
output_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 — Method
dynamics(models)Return a single DifferentialEquations.jl-compatible function that calculates derivatives of all models in models