API

Deployment Workspace

Code Generation for FMU Package

FMUGeneration.@defineMacro
@define func

Define any function to be deployed in the FMU.

Example

@define function continuous_model(u, x, p, t)
    x₁, x₂ = x
    u₁, u₂ = u
    α, β, γ, δ = p
    du₁ = α * u₁ - β * u₁ * u₂ + x₁
    du₂ = δ * u₁ * u₂ - γ * u₂ - x₂
    du = [du₁, du₂]
end
source
FMUGeneration.set_defaultFunction
set_default(; initial_states = Float64[], default_parameters = Float64[],
             initial_inputs = Float64[],
             tspan = (0.0, 0.0))

Set the default initial states, parameters, inputs, and tspan for the model. This is required to be called before defining a new model to be deployed.

source
FMUGeneration.@continuous_modelMacro
@continuous_model forward_pass

A macro to define the continuous model for the model in the form (u, x, p, t) -> du where u is input vector, x is state vector, p is the parameter vector, and t is the time. This is a necessary macro to be called for any model exchange model to deployed.

Example

@continuous_model (u, x, p, t) -> begin
    x₁, x₂ = x
    u₁, u₂ = u
    α, β, γ, δ = p
    du₁ = α * u₁ - β * u₁ * u₂ + x₁
    du₂ = δ * u₁ * u₂ - γ * u₂ - x₂
    du = [du₁, du₂]
end
source
FMUGeneration.@initial_state_transformMacro
@initial_state_transform forward_pass

A macro to define the initial state transform for the model in the form (u, p) -> transformed_u where u is state vector and p is the parameter vector. This is an optional macro. The initial state transform defaults to identity (u, p) -> u if not explicitly defined.

Example

@initial_state_transform (u, p) -> u .+ p[1]
source
FMUGeneration.@output_transformMacro
@output_transform forward_pass

A macro to define the output transform for the model in the form (u, x, p, t) -> transformed_output where u is input vector, x is state vector, p is the parameter vector, and t is the time. This is an optional macro. The output transform defaults to identity (u, x, p, t) -> u if not explicitly defined.

Example

@output_transform (u, x, p, t) -> x ./ p[3]
source
FMUGeneration.@cosimulatorMacro
@cosimulator forward_pass

A macro to define the cosimulator for the model in the form (x, dt) -> x_next where x is observables vector at time t, dt is the time step and x_next is the computed obsevable vector at time t+dt. This is a necessary macro to be called for any co-simulation model to be deployed.

Example

@cosimulator (x, dt) -> begin
    x₁, x₂ = x
    x₁_next = x₁ + dt * x₂
    x₂_next = x₂ + dt * x₁
    x_next = [x₁_next, x₂_next]
end
source
FMUGeneration.@add_packagesMacro

Add list of packages as dependencies to the FMU.

Version are matched with the host environment automatically. deved packages' paths are automatically inferred.

...

Arguments

  • lib_path: The path to the generated julia package
  • deps: A list of pacakges to add into the generated julia packages as dependencies.

Example

@add_packages ["OrdinaryDiffEq"]
source
FMUGeneration.generate_fmu_codeFunction
generate_fmu_code(
    fmu_name;
    testing,
    template_path,
    kwargs...
)

Generates the FMU package code. The JULIA_FMU_TEST_MODE environment variable can be set to true to disable automatic deserialization within the FMU during compilation. This is useful for testing purposes.

Arguments

  • fmu_name: The name of the FMU
  • template_path: The path to the FMU template file. This is not required to be changed.
  • testing: Deprecated. Please use JULIA_FMU_TEST_MODE environment variable instead.

Example

generate_fmu_code()
source
FMUGeneration.generate_fmu_packageFunction
generate_fmu_package(
    fmu_name,
    template_path;
    path,
    pkg_name,
    num_threads
) -> Any

Generate FMI2Binary julia package with generated code included.

...

Arguments

  • fmu_name: The name of the FMU

  • path=mktempdir(@get_scratch!("JSDeploymentjl"); prefix="", cleanup=true): path to the directory where the Julia package will be generated.

  • num_threads=4: number of threads to use during inference.

...

source

XML Generation

FMUGeneration.generateXMLFunction
generateXML(
    lib_path_dir,
    inputs,
    parameters,
    conts,
    outputs,
    tend;
    fmu_type,
    sysname,
    kwargs...
) -> String

Generate FMU metadata file for the FMU.

...

Arguments

  • lib_path_dir: directory where the generated julia package is stored
  • inputs: list of inputs each described using a named tuple
  • parameters: list of parameters each described using a named tuple
  • conts: list of continuous variables each described using a named tuple
  • outputs: list of outputs each described using a named tuple
  • tend: end time of the simulation
  • fmu_type=:MECS: describe the type of the FMU to be either :ME, :CS or :MECS
  • sysname="test": name of the FMU.

...

source
generateXML(
    filename::String,
    inputs::Vector{XMLVariable},
    params::Vector{XMLVariable},
    conts::Union{Integer, Vector{XMLVariable}},
    outputs::Vector{XMLVariable},
    sysname::String,
    fmu_type::Symbol,
    tstop::Real;
    tstart,
    model_description_kwargs,
    tol,
    step_size,
    cont_start,
    fmu_compliance_log
) -> String

Generate the metadata for the FMU.

Arguments

  • filename::String: The name of the FMU file.
  • inputs::Vector{XMLVariable}: The inputs of the FMU.
  • params::Vector{XMLVariable}: The parameters of the FMU.
  • conts::Union{Vector{XMLVariable}, <:Integer}: The continuous states of the FMU.
  • outputs::Vector{XMLVariable}: The outputs of the FMU.
  • sysname::String: The name of the FMU.
  • fmu_type::Symbol: The type of the FMU. Can be :ME or :CS or :MECS.
  • tstop::Real: The stop time of the FMU.

Keyword Arguments

  • tstart::Real = 0: The start time of the FMU.
  • model_description_kwargs = (): The keyword arguments for the fmi2ModelDescription function.
  • tol = 1e-6: The tolerance of the FMU.
  • step_size = 1e-3: The step size of the FMU.
  • cont_start::Union{Vector{<:Real}, Nothing} = nothing: The start values of the continuous states.
  • fmu_compliance_log = true: Whether to log the FMU compliance.

Example

tend = 10.0
in0 = [0.01, 0.01]
p0 = [2.0, 1.875, 2.0, 1.875]
u0 = [0.9, 0.9]
inputs = [(name = "i$i", unit = "unit i$i", description = "desc i$i", start = in0[i])
            for i in 1:length(in0)]
params = [(name = "p$i", description = "desc p$i", start = p0[i]) for i in 1:length(p0)]
conts = [(name = "c$i", unit = "unit c$i", description = "desc c$i", start = u0[i])
            for i in 1:length(u0)]
outputs = [(name = "o$i", unit = "unit c$i", description = "desc c$i")
            for i in 1:length(u0)]
generateXML(inputs, params, conts, outputs, tend; sysname = "lv")
source

FMU Compilation

FMUGeneration.compile_fmuFunction
compile_fmu(
    lib_path,
    xml_path,
    fmu_out_path;
    include_lazy_artifacts,
    incremental,
    lib_path_dir
) -> Tuple{String, String, String}

This function compiles a julia package into an FMU.

...

Arguments

  • lib_path: the path to the Julia FMU package to be compiled.
  • xml_path: the path to the XML file containing the the metadata.
  • fmu_out_path: the path to which the compiled and compressed FMU with .fmu extension will be saved.
  • include_lazy_artifacts=false: whether to package artifacts of the dependencies marked as lazy in the FMU.
  • incremental=false: whether to compile the FMU incrementally.

...

source

Miscellaneous

FMUGeneration.dirsizeFunction
dirsize(path::AbstractString) -> Any

Compute the size of the directory.

...

Arguments

  • path: the path of the directory whose size is to be checked.

...

source
Missing docstring.

Missing docstring for valid_dl_path. Check Documenter's build log for details.