Monthly Archives: May 2018

FMI4J – A FMI library for the JVM

FMI4j is a new open-source Kotlin library that allows import and simulation of FMUs compliant with the FMI 2.0 standard for Model Exchange and Co-simulation.
As Kotlin is 100% interoperable with Java, its up to the end user which language to use.

As of late 2017, Fmi4j is the only Java library that support Model Exchange 2.0.
FMI4j can not only import such FMUs, but can also solve them using any of the solvers available in Apache Commons Math package.

FMI4j uses the gradle build system, allowing you to easily set it up on your own machine.
A Gradle plugin is available as well.

Artifacts are published to maven central.

API:

Fmu fmu = Fmu.from(new File("path/to/fmu.fmu")); //URLs are also supported

FmiSimulation instance = fmu.asCoSimulationFmu().newInstance();

// Model Exchange is also supported:
//
// Solver solver = ApacheSolvers.euler(1E-3);
// FmiSimulation instance = fmu.asModelExchangeFmu(solver).newInstance(); 

instance.init(); //throws on error

double stop = 10;
double stepSize = 1.0/100;
while(instance.getCurrentTime() <= stop) {
    instance.doStep(stepSize);
}
instance.terminate(); //or close, try with resources is also supported

fmu.close() // <- also done automatically by the library if you forget to do it yourself

FMU2Jar

FMU2Jar is a command line utility for converting Functional Mock-up Units (FMUs) into regular Java libraries.

This allows the FMU to be loaded into an application using maven artifacts rather that referring to the location of the FMU in your app.

Furthermore, typesafe getters and setters for each variable is generated and accessiable though the API.

E.g. an FMU named “Car” containing a ScalarVariable named “speed” of type Real and causality == OUTPUT can be accessed like so:


Motor motorInstance = Motor.newInstance();
RealVariable speed = motorInstance.getOutputs().getSpeed();

FmuRead<Double> read = speed.read();
if (read.getStatus() == Status.OK) {
  System.out.println(speed.name + "=" + read.getValue());
}

FMU2Jar is available here.