Wiki

Clone wiki

javaFMI / FmuWrapper

Fmu wrapper

Getting started

To include this library in your project you can either download the corresponding jar from here or use Maven. If you do it using our download section, don't forget to include JNA (4.1.0) and Simple-XML (2.7.1). In the case of using maven, please use this code:

#!maven

<dependency>
    <groupId>org.siani.javafmi</groupId>
    <artifactId>fmu-wrapper</artifactId>
    <version>LATEST</version>
</dependency>

Simulation class

Most cases you only need to open an FMU set some variables an iterate over time to read some other variables. For those cases in which you want to go fast, use the Simulation class, here are some usage examples

int startTime = 1;
int stopTime = 2000;
int stepSize = 1;
Simulation simulation = new Simulation("path/to/foo.fmu");
simulation.init(starTime,stopTime);
for(int i=0; i < 2000; i++)
     simulation.doStep(stepSize)
simulation.terminate();

You can also iterate trough the simulation via:

Simulation simulation = new Simulation("path/to/file.fmu");
simulation.init(startTime, stopTime);
while (!simulation.isTerminated())
   simulation.doStep(stepSize);
simulation.terminate();

Variables can be read and written in the FMU as follows:

simulation.write("foo", "bar").with(1.0, 2.0);
double[] values = simulation.read("foo", "bar").asDouble();

The with(...) method is overloaded to support the FMI types and when you read the variables you have to define the type you expect explicitly.

When you call the init method for FMUs of the FMI v2, this calls are performed:

    double startTime = 0.;
    double stopTime = 1.:
    simulation.init(startTime, stopTime);
    //  sets up experiment
    //  enters init mode
    //  exits init mode
    simulation.doStep(0.1);

This automated calls could not match your needs, or maybe this API is not what you want. In that case use the Access class.

Access class

Simulation class offers you a simplified way to work with the FMUs. However, you can have a full FMI access by creating a new Access object from a previously defined Simulation:

Simulation simulation = new Simulation("path/to/foo.fmu");
simulation.init(startTime, stopTime);
Access access = new Access(simulation);
String[] unknownVariables = new String[]{"unknown", "variables"};
String knownVariable = "known";
double[] directionalDerivatives = access.getDirectionalDerivative(knownVariable, unknownVariables);
simulation.terminate();

Take care that there are two Access classes, one for FMI v1.0 and other for FMI v2.0 so that you need to import the class from the package of the FMU version you are using. An exception will be thrown if you do not use the right version of Access in accordance to the used fmu:

import org.javafmi.v2.Access;

[...]

Simulation simulation = new Simulation("path/to/v1/foo.fmu");
Access access = new Access(simulation); // throws an exception

Updated