An experimental MoonBit interface to FMI-compliant FMUs.
moon add MuVirus/fmigit clone https://github.com/MuVirus/MoonFMI.git
cd MoonFMI
moon build --target nativemoon run tests/data/fetch-fmus.mbtx --target nativemoon run cmd/main --target native -- simulate tests/data/.cache/fmi2/BouncingBall.fmu \
--stop-time 3 --step-size 0.01 --output ball.csvmoon run cmd/main --target native -- validate tests/data/.cache/fmi2/BouncingBall.fmu
moon run cmd/main --target native -- info tests/data/.cache/fmi2/BouncingBall.fmumoon run cmd/main --target native -- simulate tests/data/.cache/fmi2/VanDerPol.fmu \
--interface me --solver rk4 --stop-time 5 --output vdp.csvmoon run cmd/main --target native -- package build/MyModel --output MyModel.fmuimport {
"MuVirus/fmi/src/backend/native" @native,
"MuVirus/fmi/src/fmi" @fmi,
"MuVirus/fmi/src/sim" @sim,
"moonbitlang/core/env",
"moonbitlang/x/fs",
}
supported_targets = "native"///|
fn run_ball(fmu_path : String) -> Unit {
let data = @fs.read_file_to_bytes(fmu_path) catch {
_ => return println("cannot read " + fmu_path)
}
let host = @fmi.parse_host_platform(@native.host_platform_name()).unwrap()
@fs.create_dir(".moonfmi-tmp") catch {
_ => ()
}
let work_dir = ".moonfmi-tmp/run-\{@env.now()}"
let loaded = @native.load_fmi2_fmu(data, host~, temp_directory=work_dir) catch {
_ => return println("load failed")
}
let config = @sim.CsConfig::new(
start_time=0.0,
stop_time=3.0,
step_size=0.01,
record=[1U, 2U],
)
let result = @sim.run_cs_simulation(loaded.instance(), config~) catch {
_ => {
loaded.dispose()
return println("simulation failed")
}
}
loaded.dispose()
println("\{result.steps()} steps, \{result.series().length()} series")
}