API
Deployment Workspace
FMUGeneration.clear_deployment_workspace
— Functionclear_deployment_workspace()
Clears/instantiates the deployment workspace. This is required to be called before defining a new model to be deployed.
Code Generation for FMU Package
FMUGeneration.@define
— Macro@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
FMUGeneration.set_default
— Functionset_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.
FMUGeneration.@continuous_model
— Macro@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
FMUGeneration.@initial_state_transform
— Macro@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]
FMUGeneration.@output_transform
— Macro@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]
FMUGeneration.@cosimulator
— Macro@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
FMUGeneration.@add_packages
— MacroAdd list of packages as dependencies to the FMU.
Version are matched with the host environment automatically. dev
ed packages' paths are automatically inferred.
...
Arguments
lib_path
: The path to the generated julia packagedeps
: A list of pacakges to add into the generated julia packages as dependencies.
Example
@add_packages ["OrdinaryDiffEq"]
FMUGeneration.generate_fmu_code
— Functiongenerate_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 FMUtemplate_path
: The path to the FMU template file. This is not required to be changed.testing
: Deprecated. Please useJULIA_FMU_TEST_MODE
environment variable instead.
Example
generate_fmu_code()
FMUGeneration.generate_fmu_package
— Functiongenerate_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 FMUpath=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.
...
XML Generation
FMUGeneration.generateXML
— FunctiongenerateXML(
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 storedinputs
: list of inputs each described using a named tupleparameters
: list of parameters each described using a named tupleconts
: list of continuous variables each described using a named tupleoutputs
: list of outputs each described using a named tupletend
: end time of the simulationfmu_type=:MECS
: describe the type of the FMU to be either:ME
,:CS
or:MECS
sysname="test"
: name of the FMU.
...
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 thefmi2ModelDescription
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")
FMU Compilation
FMUGeneration.compile_fmu
— Functioncompile_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.
...
Miscellaneous
FMUGeneration.@import_fmu_pkg
— MacroImport generated FMU Julia package using its path on disk.
...
Arguments
lib_path
: path of the generated Julia package on disk.
...
FMUGeneration.dirsize
— Functiondirsize(path::AbstractString) -> Any
Compute the size of the directory.
...
Arguments
path
: the path of the directory whose size is to be checked.
...
Missing docstring for valid_dl_path
. Check Documenter's build log for details.