API

Specify the FMU dynamics and the metadata

FMUGeneration.JuliaFMUType
function JuliaFMU(
    name::String,
    fmi_version::FMIEnums.FMIVersion,
    fmi_types::AbstractVector{<:FMIEnums.ModelType};
    default_tspan = nothing,
    default_stepsize = nothing,
    default_tolerance = nothing,
    independent_variable = (name="time", ),
    parameters = NamedTuple[],
    inputs = NamedTuple[],
    states = NamedTuple[],
    outputs = NamedTuple[],
    dependencies = (Pkg.Types.PackageSpec[], Pkg.Types.PackageSpec[]),
    deserialization_utils = Expr[],
    objects = FMUObject[],
    dynamics_utils = Expr[],
    ode_function = nothing,
    state_initializer = :((u, in, p, t) -> u),
    observables_function = nothing,
    cosimulator = nothing,
    cosimulator_solver=:(OrdinaryDiffEq.AutoTsit5(OrdinaryDiffEq.FBDF())),
    cosimulator_integrator_options = (;),
    mass_matrix_diagonal = nothing,
    adjoint_derivatives = false,
)

Constructor for the metadata and dynamics of a Functional Mockup Unit (FMU) which can be used to generate a FMU Julia package and compile it into a FMU binary.

Arguments

  • name::String: Name of the FMU

  • fmi_version::FMIEnums.FMIVersion: Version of the FMI standard

  • fmi_types::AbstractVector{<:FMIEnums.ModelType}: Types of the FMU (e.g. Model Exchange, Co-simulation)

  • default_tspan::Union{Nothing, Tuple{Float64, Float64}}: Default time span for the simulation. Defaults to nothing.

  • default_stepsize::Union{Nothing, Float64}: Default step size for the simulation. Defaults to nothing.

  • default_tolerance::Union{Nothing, Float64}: Default tolerance for the simulation. Defaults to nothing.

  • independent_variable::Union{NamedTuple, Nothing}: The independent variable (e.g. time) of the FMU. Given as a NamedTuple with a required name field and an optional description field.

nothing declares none, which only FMI 2.0 permits: it then treats one as implicitly present (name "time", unit "s"), whereas FMI 3.0 requires exactly one. Ex. (name="time", description="Simulation time"). Defaults to (name="time",).

  • parameters::NamedTuple[]: Parameters of the FMU. Ex. (name="p1", description="Parameters description", start=0.0). Defaults to an empty array.

  • inputs::NamedTuple[]: Inputs of the FMU. Ex. (name="x1", description="Input 1", start=0.0). Defaults to an empty array.

  • states::NamedTuple[]: States of the FMU. Ex. (name="u1", description="State 1", start=0.0). Defaults to an empty array.

  • outputs::NamedTuple[]: Outputs of the FMU. Ex. (name="o1", description="Output 1"). Defaults to an empty array.

  • dependencies::Tuple{Pkg.Types.PackageSpec[], Pkg.Types.PackageSpec[]}: Dependencies of the FMU. Ex. @deps [OrdinaryDiffEq]. Defaults to an empty array.

  • deserialization_utils::Expr[]: Re-declarations of any types that were defined in Main when @objects captured them; they must match the originals exactly. Defaults to an empty array.

  • objects::Vector{FMUObject}: Objects to serialize into the generated package. Ex. @objects [test_obj]. Defaults to an empty capture.

  • dynamics_utils::Expr[]: Utilities which require deserialized objects and are needed for the dynamics. Defaults to an empty array.

  • ode_function::Union{Nothing, Expr}: Expression of an in-place ODE function for the FMU of the form ode_function!(derivatives, states, inputs, parameters, time). Defaults to nothing.

  • state_initializer::Union{Nothing, Expr}: State initializer for the FMU. Expression of out-of-place function of the form state_initializer(states, inputs, parameters, time) -> initialized_states. Defaults to nothing.

  • parameter_initializer::Union{Nothing, Expr}: Parameter initializer for the FMU. Expression of out-of-place function of the form parameter_initializer(parameters, inputs, time) -> initialized_parameters. Called immediately after state_initializer at fmi*ExitInitializationMode. Defaults to nothing.

  • observables_function::Union{Nothing, Expr}: Observables function for the FMU. Expression of in-place function of the form observables_function!(outputs, states, inputs, parameters, time) -> outputs. Defaults to nothing.

  • cosimulator::Union{Nothing, Expr}: Co-simulator for the FMU. Defaults to nothing. Expression of out-of-place function of the form cosimulator(states, inputs, parameters, time) -> states.

  • cosimulator_solver::Union{Nothing,Expr}: Expression of solver for the co-simulator. Defaults to nothing.

  • cosimilator_odefunction_options::Union{Nothing, Expr, NamedTuple}: ODEFunction options for the co-simulator solver. Defaults to an empty NamedTuple.

  • cosimulator_integrator_options::Union{Nothing,Expr,NamedTuple}: Integrator options for the co-simulator solver. Defaults to an empty NamedTuple.

  • mass_matrix_diagonal::Union{Nothing,Vector{Float64}}: Diagonal of the mass matrix M in state order. A zero entry marks an algebraic state whose derivative slot carries the equation residual instead of a derivative. Exposed to importers via dyad_fmi[2|3]GetMassMatrixDiagonal. When nothing (default), we store a vector of ones, with zeros for states flagged as algebraic in their metadata.

  • adjoint_derivatives::Union{Bool,ADTypes.AbstractADType}: Selects how fmi3GetAdjointDerivative (the reverse-mode vector-Jacobian product J'seed) is provided. FMI 3.0 only. Opt-in and false by default, since currently, this feature is not compatible with juliac trimming. Options: false leaves the feature out entirely; AutoReverseDiff(compile=false) re-records the tape on every call and is therefore safe even for value-dependent branches; AutoReverseDiff(compile=true) replays one cached, compiled tape (faster, but valid only for a branch-free RHS). true implies AutoReverseDiff(compile=false) for this method, but in general true means we will attempt to select the best AD backend (see the ModelingToolkitFMUGenerationExt). Other AbstractADType types are not currently supported.

  • build::Bool: Automatically generate code and compile the FMU. Defaults to true.

source

Miscellaneous

FMUGeneration.@depsMacro
macro deps(deps)

This macro is used to define the dependencies that is needed to be available in the FMU.

Arguments

  • deps::AbstractVector: A vector of package modules
source
FMUGeneration.@objectsMacro
macro objects(objs, backend=:jls)

Capture the objects that need to be available in the FMU as a Vector{FMUObject} (concatenable with vcat). Serialization is deferred to generate_code! — see FMUObject.

We default to the Base.Serialization backend, but the user can specify a different backend.

  • :jls - Base.Serialization
  • :bson - BSON.jl

Arguments

  • objs::AbstractVector: A vector of objects to be captured
  • backend::Symbol: The backend to use for serialization.
source