API
Specify the FMU dynamics and the metadata
FMUGeneration.JuliaFMU — Typefunction 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 FMUfmi_version::FMIEnums.FMIVersion: Version of the FMI standardfmi_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 aNamedTuplewith a requirednamefield and an optionaldescriptionfield.
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 inMainwhen@objectscaptured 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 formode_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 formstate_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 formparameter_initializer(parameters, inputs, time) -> initialized_parameters. Called immediately afterstate_initializeratfmi*ExitInitializationMode. Defaults to nothing.observables_function::Union{Nothing, Expr}: Observables function for the FMU. Expression of in-place function of the formobservables_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 formcosimulator(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}:ODEFunctionoptions for the co-simulator solver. Defaults to an emptyNamedTuple.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 matrixMin state order. A zero entry marks an algebraic state whose derivative slot carries the equation residual instead of a derivative. Exposed to importers viadyad_fmi[2|3]GetMassMatrixDiagonal. Whennothing(default), we store a vector of ones, with zeros for states flagged as algebraic in their metadata.adjoint_derivatives::Union{Bool,ADTypes.AbstractADType}: Selects howfmi3GetAdjointDerivative(the reverse-mode vector-Jacobian productJ'seed) is provided. FMI 3.0 only. Opt-in andfalseby default, since currently, this feature is not compatible with juliac trimming. Options:falseleaves 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).trueimpliesAutoReverseDiff(compile=false)for this method, but in generaltruemeans we will attempt to select the best AD backend (see theModelingToolkitFMUGenerationExt). OtherAbstractADTypetypes are not currently supported.build::Bool: Automatically generate code and compile the FMU. Defaults totrue.
Miscellaneous
FMUGeneration.@deps — Macromacro 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
FMUGeneration.@objects — Macromacro 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 capturedbackend::Symbol: The backend to use for serialization.