SFI-Mechatronics/FMI4j


FMI4j is a software package for dealing with Functional Mockup Units (FMUs) on the JVM

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.sfi-mechatronics:fmi4j:0.26.0'
	}
	dependencies {
		implementation("com.github.sfi-mechatronics:fmi4j:0.26.0")
	}
	<dependency>
	    <groupId>com.github.sfi-mechatronics</groupId>
	    <artifactId>fmi4j</artifactId>
	    <version>0.26.0</version>
	</dependency>

                            
    libraryDependencies += "com.github.sfi-mechatronics" % "fmi4j" % "0.26.0"
        
        

                            
    :dependencies [[com.github.sfi-mechatronics/fmi4j "0.26.0"]]
        
        

Readme


FMI4j

License: MIT contributions welcome

CI Bintray

Gitter

FMI4j is a software package for dealing with Functional Mock-up Units (FMUs) on the Java Virtual Machine (JVM), written in Kotlin.

FMI4j supports import of both FMI 1.0 and 2.0 for Co-simulation. For Model Exchange version 2.0 is supported. <br/>

Export of FMI 2.0 for Co-simulation is also supported.

Compared to other FMI libraries targeting the JVM, FMI4j is considerably faster due to the fact that we use JNI instead of JNA. Considering FMI-import, a significant speedup (2-5x) compared to other open-source FMI implementations for the JVM should be expected. For FMI-export FMI4j is multiple orders of magnitude faster than any existing open source alternative.


Artifacts are available through Bintray.

<a name="api"></a> FMI import


class Demo {
    
    void main(String[] args) {
        
        Fmu fmu = Fmu.from(new File("path/to/fmu.fmu")); //URLs are also supported
        FmuSlave slave = fmu.asCoSimulationFmu().newInstance();

        slave.simpleSetup();
        
        double stop = 10;
        double stepSize = 1.0/100;
        while(slave.getSimulationTime() <= stop) {
            if (!slave.doStep(stepSize)) {
                break;
            }
        }
        slave.terminate(); //or close, try with resources is also supported
        fmu.close();
        
    }
    
}

<a name="api"></a> FMI export

Write the code
@SlaveInfo(
        modelName = "MyJavaSlave",
        author = "John Doe"
)
public class JavaSlave extends Fmi2Slave {
    
    @ScalarVariable
    private int intOut = 99;
    @ScalarVariable
    private double realOut = 2.0;
    @ScalarVariable
    private double[] realsOut = {50.0, 200.0};
    @ScalarVariable
    private String[] string = {"Hello", "world!"};
    
    private ComplexObject obj = ComplexObject();
    
    public JavaSlave(Map<String, Object> args) {
        super(args);
    }

    @Override
    protected void registerVariables() {
        register(integer("complexInt", () -> obj.integer)
                .causality(Fmi2Causality.output));
        register(real("complexReal", () -> obj.real)
                .causality(Fmi2Causality.output));
    }

    @Override
    public void doStep(double currentTime, double dt) {
        realOut += dt;
    }

}
Build the FMU
Usage: fmu-builder [-h] [-d=<destFile>] -f=<jarFile> -m=<mainClass>
  -d, --dest=<destFile>    Where to save the FMU.
  -f, --file=<jarFile>     Path to the Jar.
  -h, --help               Print this message and quits.
  -m, --main=<mainClass>   Fully qualified name of the main class.

In order to build the fmu-builder tool, clone this repository and invoke ./gradlew installDist. The distribution will be located in the folder fmu-builder-app/build/install.


Would you rather build FMUs using Python? Check out PythonFMU! <br> Or would you rather simulate FMUs using C++? Check out FMI4cpp! <br> Need to distribute your FMUs? FMU-proxy to the rescue! <br> Need a complete co-simulation framework with SSP support? Check out Vico <br>