# Export/interface Julia code to FMI/FMU standard .fmu

**URL:** <https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211>\
**Category:** Modelling & Simulations\
**Created:** [February 26, 2019, 12:09pm UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211 "2019-02-26T12:09:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)\
**Post date:** [February 26, 2019, 12:09pm UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/1 "2019-02-26T12:09:16Z")

</div>

Hi everyone,

I will start to build a modeling framework.  
I have simulation models which are built with Modelica, some others with Simulink/Simscape Toolbox and finally I have a control system designs with Julia and package JuMP. The control system aims to regulates the behavior of systems models (the master piece of my case study).

I have seen the FMI standard [1] and .fmu which is suitable to co-simulate from different tools and I would like to use it. I haved uploaded a figure to illustrate it.

My question is :

Does a julia package exist to export julia code to .fmu standard ? Or does a FMU interface to my julia code exist?

Thanks for all your answers.  
[julialangpost|554x392](https://discourse.julialang.org/404)

[1] [https://fmi-standard.org/](https://fmi-standard.org/)

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [February 27, 2019, 11:59am UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/2 "2019-02-27T11:59:21Z")

</div>

I haven’t run across anything. [This post](https://discourse.julialang.org/t/system-dynamics-packages/8222/10) suggests interest in doing that, but I don’t see an followup work at Modelon. Julia is good at wrapping C code. You might have a look at wrapping [FMILibrary](https://jmodelica.org/FMILibrary/). You could also try using [PyFMI](https://jmodelica.org/pyfmi/index.html) through Julia’s Python interface.

Maybe you could convert your Modelica models to [Modia](https://github.com/ModiaSim/Modia.jl). You might also have a look at [OMJulia](https://github.com/OpenModelica/OMJulia.jl), a Julia interface to OpenModelica.

---

<div class="post-metadata">

**Author:** ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)\
**Post date:** [February 28, 2019, 9:31am UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/3 "2019-02-28T09:31:18Z")

</div>

Thank for your answer and your links.

First of all, I have also a look [here](https://github.com/traversaro/awesome-fmi), where FMI import and export tools are listed. However according to the webpage, [SimulatorToFMU](https://github.com/LBNL-ETA/SimulatorToFMU) is able to export pyhton script.

Second, I can convert the Modelica models to Modia, however I have also some models which are based on Simscape toolbox from Simulink. My constraint is to export julia script to .fmu, where the .fmu can be reutilised.

Third, I tried a different way. According to Dymola User Manuel page 407, external functions in orther languages is possible (C, C++, fortran and Java), and also link to DLL in this web [page](https://stackoverflow.com/questions/41166086/external-functions-reference-headers-in-c-script-to-compiled-dll) and of course Dymola user manuel page 412.

I tried to build a share library from my julia function with [PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler.jl) which it can be a external library in modelica/Dymola and, finaly export this modelica model with the external function in .fmu. It can be summarise as:

1. Julia function : MyFunction.jl
2. Build a share library from the julia function with build\_shared\_lib(“.jl”): Myfunction.dll
3. External functions linking with DLL inside one Modelica model: ModelicaModelWithExternalFunction.mo
4. Export the modelica model with the linking DLL to .FMU: MyFMU.fmu

I have implemented the 4 steps. My trial julia function return the cab number:

> Base.@ccallable function julia\_main()::Cint  
> return 1729  
> end

Then, with REPL : build\_shared\_lib(“TaxiCabNumber.jl”). REPL wrote “All done”. The builddir is created, with the TaxiCabNumber.dll, however a large amount of other .dll are also there.

 ![builddir](https://global.discourse-cdn.com/julialang/original/3X/7/f/7f610cb72a18a1a0a5d49c8fd77029ca01e90d6f.png)

Then, I did my modelica.mo with external function. The modelica function is called add2bis (please note the julia\_main) (I did like [here](https://stackoverflow.com/questions/41166086/external-functions-reference-headers-in-c-script-to-compiled-dll) case B):

> function add2bis  
> output Real sum;  
> external “C” sum=julia\_main() annotation (  
> LibraryDirectory=“modelica://TestFunctionExt2/Ressources/Library”,  
> Library=“TaxiCabNumber”);  
> end add2bis;

Then, I exported my modelica model (with add2bis call) to .fmu. My fmu is called TestfunctionsExt2.fmu. Dymola built the .fmu and I imported with simulink:  
 ![simulinkFMU](https://global.discourse-cdn.com/julialang/original/3X/2/f/2f7dbd87cf4ef9695112dcefc438c09617f0aec9.png)

Then, I have run the simulation, and there was an error and simulation failed. I logged the FMU debug and I’ve got the following log:

> Log From FMU: [category:, status:fmierror] Problem with loading TaxiCabNumber.dll

The .fmu is equiavalent to .zip, I have modified .fmu to .zip in order to have a look inside, and the two dll files are presents: TaxeCabNumber.dll and TestFunctionsExt2.dll.

I have the following questions:

- Did I do some mistakes with the julia function according to PackageCompiler usage?
- According to [build\_shared\_lib](https://github.com/JuliaLang/PackageCompiler.jl), the generated library is called : buildir/libhello.{so,dylib,dll}. I do not understand the others files.dll from the buildir, what are their purpose? Also, what is the TaxiCabNumber.a?
- Are there some limitations with the TaxiCabNumber.dll generated from build\_shared\_lib(“TaxiCabNumber.jl”)?

Thanks for all your answers. Warmest regards.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [February 28, 2019, 12:52pm UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/4 "2019-02-28T12:52:10Z")

</div>

Hi,

Faced with a similar problem (coupling a Python or Julia optimization-based control algorithm with a Modelica model), I’ve started exploring an alternative way: call the Python/Julia code from Modelica.  
Since embedding the Python interpreter is complicated, I’ve used a client-server approach with a messaging between the two. The code is available at:

> **[GitHub - pierre-haessig/numrpc: NumRPC is a lightweight Remote procedure call...](https://github.com/pierre-haessig/numrpc)**
>
> NumRPC is a lightweight Remote procedure call (RPC) mechanism for exchanging numbers between different processes - GitHub - pierre-haessig/numrpc: NumRPC is a lightweight Remote procedure call (RPC...

This code is certainly not finished, by I’ve a running demo of a simple Python thermal controller with a simple Modelica thermal model. In an ideal world, I would have finished this along with a nice descriptive article for Modelica 2019 conf, but this didn’t happen ☹…

The Julia part is not started, but at least [ZMQ.jl](https://github.com/JuliaInterop/ZMQ.jl) does exist (which is why I selected this messaging lib), while I had to do the low-level C code for the Modelica side (it would probably have been easy for somebody proficient in C, but this is not my case).

---

<div class="post-metadata">

**Author:** ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)\
**Post date:** [March 6, 2019, 1:17pm UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/5 "2019-03-06T13:17:18Z")

</div>

Hello Pierre,

Thanks a lot for NumRPC.

I tried a different way from my last post. Instead of building a .dll file from my julia code, I used the build\_executable(“–.jl”, “–”) from PackageCompiler.jl to build an exe file. The .exe file is called inside the Modelica external C function with: system(“–.exe arg”);

I did the same steps as discribed in last post with the exe file. It works, dymola allows external C function wich run the exe, and I can export my Modelica package to fmu. I simulated the fmu with Simulink and it works too.

Regards

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [March 28, 2019, 7:33am UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/6 "2019-03-28T07:33:55Z")

</div>

I wish a package for dealing with FMI/FMU for both import and import gets implemented someday also.

I think it would be a good selling point for Julia (in particular in industry -iaw [fmi-standard.org](https://fmi-standard.org/tools/) it is supported by over 100 tools including Dymola, Modelica, Matlab and many others engineering tools-).

I have taken a look myself, but I feel I am not smart enough. ☹

At the end of the day, it looks like a zip file with a XML description file and the DLLs embedded. There seems to be C libraries for both [import](https://svn.jmodelica.org/FMILibrary/tags/2.0.3/) and [export](https://resources.qtronic.de/fmusdk/FmuSdk_reference.html). So it looks like the blocks are nearly there.

Then just a stupid question just in case somebody knows (despite this is probably the wrong forum). Would it be a good idea to perform what fmi-standard tries to do in WebAssembly? I understand it is more “cross-platform” that FMU’s DLLs. Besides those “TaxicabNumber.a” and “TaxiCabNumber.dll” seem to be huge (\>130Mb each) while exports to webassembly from julia seem to be tiny. Now even more when it looks like you don’t need to run it in a browser with things like [life](https://github.com/perlin-network/life).

---

<div class="post-metadata">

**Author:** ![togo59](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/togo59/32/4138_2.png) [@togo59](https://discourse.julialang.org/u/togo59)\
**Post date:** [June 24, 2020, 3:31pm UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/7 "2020-06-24T15:31:04Z")

</div>

I also have a need for FMI/FMU support as I have to interface my Julia control systems algorithms with a fast physics model written in C++. The FMI/FMU standard is the only way I wish to go as that will also provide access to other code that supports the same standards. I will start using [JuliaFMI](https://github.com/fmi-tools/JuliaFMI) soon but it would be good to know who else is using this.  
Roger.

---

<div class="post-metadata">

**Author:** ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)\
**Post date:** [June 15, 2021, 9:52am UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/8 "2021-06-15T09:52:20Z")

</div>

Hi all,  
I just came across this topic and wanted to let you know that we recently published a FMI-Library for Julia called [fmi.jl](https://github.com/ThummeTo/FMI.jl). We support full FMI 2.0 including CS and ME (with event handling). Windows and Linux, but Linux is currently not completely tested. FMI Cross Checks coming soon 😉

---

<div class="post-metadata">

**Author:** ![RogerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogerp/32/17496_2.png) [@RogerP](https://discourse.julialang.org/u/RogerP)\
**Post date:** [June 18, 2021, 12:46pm UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/10 "2021-06-18T12:46:37Z")

</div>

Hi, Have you seen [GitHub - AnHeuermann/JuliaFMI: Simulator for FMUs in Julia (WIP)](https://github.com/AnHeuermann/JuliaFMI) ?

---

<div class="post-metadata">

**Author:** ![AnHeuermann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anheuermann/32/27059_2.png) [@AnHeuermann](https://discourse.julialang.org/u/AnHeuermann)\
**Post date:** [July 12, 2021, 3:47pm UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/11 "2021-07-12T15:47:35Z")

</div>

My [AnHeuermann/JuliaFMI](https://github.com/AnHeuermann/JuliaFMI) was a first try on getting a FMU simulator to run with just Julia. I started the project to teach myself Julia and did most of it during my free time. As I don’t have much time to actually finish the simulator and stumbled across some fundamental difficulties, the current state is not satisfying.

@ThummeTo and I are in contact and I will probably archive the AnHeuermann/JuliaFMI repo soon. The fmi.jl project is actively developed, so it has the potential to become the default FMI tool for Julia based FMU import.

I guess FMU export on the other side would be best implemented in the available packages that deal with ODE/DAE simulation. According to [this issue about FMI support](https://github.com/SciML/ModelingToolkit.jl/issues/135#issuecomment-796633382) it looks like ModelingToolkit.jl will add something in that direction.

---

<div class="post-metadata">

**Author:** ![RogerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogerp/32/17496_2.png) [@RogerP](https://discourse.julialang.org/u/RogerP)\
**Post date:** [July 12, 2021, 4:12pm UTC](https://discourse.julialang.org/t/export-interface-julia-code-to-fmi-fmu-standard-fmu/21211/12 "2021-07-12T16:12:02Z")

</div>

@AnHeuermann I am using the fruits of your labours and I know you’ve been in contact with @jcook with whom I work. It’s great to know this really important work is convergent. I wish I were clever enough to contribute. However, as my colleague will tell you, I am an expert at breaking things and/or failing to get things to work. So I can lend a hindrance if required. 😋
