fmi-export 0.2.0

FMU export support for FMI 3.0
Documentation

fmi-export

A Rust interface to FMUs (Functional Mockup Units) that follow the FMI Standard. This crate provides necessary interfaces and utilities to construct FMUs.

This crate is part of rust-fmi.

See http://www.fmi-standard.org

Quick start: Build an FMU

  1. Install the cargo-fmi subcommand:
cargo install cargo-fmi
  1. Create a new crate for your model using cargo-fmi:
cargo fmi new my-model

This will generate a cdylib crate with a sample model struct deriving FmuModel:

use fmi_export::FmuModel;

#[derive(FmuModel, Default, Debug)]
struct MyModel {
    #[variable(causality = Output, start = 1.0)]
    y: f64,
}

The export_fmu! macro will generate the required FMI-API exports.

fmi_export::export_fmu!(MyModel);
  1. Build and bundle the FMU with cargo-fmi:
cargo fmi --package my-model bundle

Building FMUs

This repository builds FMI 3.0 FMUs from pure Rust code, and is driven by the FmuModel derive macro.

The FMI API interfacing boilerplate is generated, and automated packaging is handled by the cargo-fmi subcommand.

Minimal FMU setup

Your FMU crate must:

  • Be a cdylib:
[lib]
crate-type = ["cdylib"]
  • Derive FmuModel for your model struct
  • Export FMI symbols via export_fmu!

Example skeleton:

use fmi_export::FmuModel;

#[derive(FmuModel, Default, Debug)]
struct MyModel {
    #[variable(causality = Output, start = 1.0)]
    y: f64,
}

fmi_export::export_fmu!(MyModel);

Build an example FMU (this repo)

From the repository root:

cargo fmi --package can-triggered-output bundle

The FMU zip is written to:

target/fmu/<model_identifier>.fmu

<model_identifier> is the Rust cdylib target name (for can-triggered-output, this is can_triggered_output).

Common options

  • Build a release FMU:
cargo fmi --package can-triggered-output bundle --release
  • Build for a specific target:
cargo fmi --package can-triggered-output bundle --target x86_64-unknown-linux-gnu

Building FMUs from Modelica models

Using the rumoca crate, fmi-export can generate Rust code from Modelica models.

The template can be used directly via Rumoca, but the primary workflow is to enable the rumoca feature in fmi-export and call its helper API from build.rs.

Quick Start

  1. Add fmi-export with the rumoca feature to your Cargo.toml:
[build-dependencies]
fmi-export = { version = "0.1.1", features = ["rumoca"] }
  1. Invoke the Rumoca compiler in your crates' build.rs:
let model_path = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
    .join("src/model.mo");
println!("cargo:rerun-if-changed={}", model_path.display());
fmi_export::rumoca::write_modelica_to_out_dir("MyModel", &model_path)
    .expect("render model.mo");

Direct Rumoca usage is also supported (path shown is for this repo checkout):

rumoca model.mo -m MyModel --template-file fmi-export/templates/rust-fmi.jinja > my_fmu_crate/src/lib.rs

If you're outside this repo, point --template-file at a local copy of rust-fmi.jinja from the fmi-export/templates directory.

See templates/README.md and the examples for further details.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.