FMI4cpp

FMI4cpp is a brand new FMI 2.0 implementation for C++.  It has been written from the ground up in modern C++. The API is very similar to FMI4j.

All dependencies are available through vcpkg.


#include <iostream>
#include <fmi4cpp/fmi2/fmi4cpp.hpp>

using namespace fmi4cpp::fmi2;

const double stop = 10.0; 
const double stepSize = 1.0/100;

int main() {

    import::Fmu fmu("path/to/fmu.fmu");
    auto slave = fmu.asCoSimulationFmu().newInstance();

    auto md = slave->getModelDescription();    
    auto var = md.getVariableByName("myVar").asReal();

    slave->init();
    
    double t;
    double value;
    while ( (t = slave->getSimulationTime()) <= stop) {
    
        if (slave->doStep(stepSize)!= fmi2OK) {
            break;
        }

        if (var->read(*slave, value) != fmi2OK) {
            break;
        }
    
        std::cout << var.name() << "=" << value << std::endl;
    }
    
    slave->terminate();
    
}

https://github.com/SFI-Mechatronics/FMI4cpp

FMU-proxy

FMU-proxy is a framework for working with Functional Mock-up Units (FMUs) across languages and platforms. This is done by wrapping a set of FMUs inside a server program. Using common and language independent Remote Procedure Call (RPC) frameworks, like Apache Avro, Apache Thrift, gRPC and JSON-RPC over a range of network protocols, the FMU functions are effectively made available from pretty much any language on any platform.

Both Co-simulation and Model Exchange FMUs are supported, with the latter being wrapped as a Co-simulation FMU.

Server implementations exists in C++ and for the JVM. The C++ implementation is cross platform and builds just as easy on both platforms thanks to vcpkg and CMake.
For handling the FMUs, the C++ version relies on FMI Library, while the JVM version uses FMI4j.

Clients exists for C++, Python, JavaScript and for the JVM. Clients in other languages can easily be implemented as most of the code will be auto generated by your chosen RPC framework using the available schemas.
On the JVM, FMU-proxy shares the same interface as FMI4j – making local and remote execution of FMUs almost indistinguishable from one another.

Only FMI 2.0 and onward are planned to be supported.

https://github.com/SFI-Mechatronics/FMU-proxy

Gradle plugin for FMI

I have published an Grade plugin for simplifying programmatic interaction with FMUs. The plugin is powered by FMI4j, which is compatible with FMI 2.0 for Co-simulation and Model Exchange.

Among other things, the plugin generates type safe getter and setters for all FMU variables – grouped by causality.
It also generates javadoc based on the information found in the modelDescription.xml.


FmiSimulation instance = ControlledTemperature.newInstance();

instance.init(); //throws on error

//Variables are grouped by causality and have types!
RealVariable tempRef = instance.getOutputs().temperature_Reference();

double stop = 10.0;
double stepSize = 1E-2;
while (instance.getSimulationTime() <= stop) {

  if (!instance.doStep(stepSize)) {
    break;
  }

  FmuRead<Double> read = tempRef.read();
  System.out.println(tempRef.name + "=" + read.getValue());

}

instance.terminate();

To add it to your project, simply add the following code to your build.gradle

plugins {
  id "no.mechatronics.sfi.fmi4j.FmuPlugin" version "0.4.1"
}

The plugin will automatically add the required FMI4j dependency to your project.

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.