henze-research-group / MODRLC

The Advanced Controls Test Bed (ACTB) is a virtual buildings test bed that interfaces external controllers to high-fidelity Spawn of EnergyPlus models.
26 stars 9 forks source link

Problems with FMU binary with Spawn #42

Closed GiuseppeR96 closed 1 year ago

GiuseppeR96 commented 1 year ago

Hi, I'd like to run a Spawn example from my own master Python code using pyFMI. I ask OpenModelica to extract an FMU from a Buildings Library model (e.g. Buildings.ThermalZones.EnergyPlus.Examples.SingleFamilyHouse.AirHeating), then I try to load the model on Python: model = load_fmu("AirHeating.fmu") such FMU will not work on Python, yielding this error message:

pyfmi.fmi.InvalidBinaryException:
 The FMU could not be loaded. Error loading the binary. Could not load 
the FMU binary:The specified module could not be found

I also tried with FMpy:

from fmpy import *
from fmpy.util import plot_result
fmu = 'AirHeating.fmu'
dump(fmu)
fmi_info(fmu)
result = simulate_fmu(fmu)
plot_result(result)

This code yields this error message: Exception: Failed to load shared library C:\ ...\AppData\Local\Temp\tmpmysxzg5z\binaries\win64\FMU_model.dll. Could not find module 'C:\Users\...\AppData\Local\Temp\tmpmysxzg5z\binaries\win64\FMU_model.dll' (or one of its dependencies). Try using the full path with constructor syntax. I tried inspecting an FMU from the MODRLC-actb project that uses spawn: https://github.com/henze-research-group/MODRLC/tree/actb/testcases/spawnrefsmalloffice/models and I noticed that the modelDescription file includes explicit references to the spawn exe, namely:

<ScalarVariable name="mod.building.spawnExe" valueReference="805306368" description="Name of the spawn executable, without extension, such as spawn-0.2.0-d7f1e095f3" causality="local" variability="constant" initial="exact">
            <String start="spawn-0.2.0-d7f1e095f3" />

which are absent in the FMU that I extracted with OpenModelica. Besides, there are differences in the binaries and resouces folders between the forementioned example and my own, OpenModelica generated FMU. May this be relevant?

It is worth noticing that:

Details on software and setup: • OS: Windows 10 • OpenModelica v1.19.0-dev.beta1 (64-bit) and OpenModelica v1.20.0-dev-327-g42e1d1bbb6 (64-bit) (attempted with both version) • FMI version: 2.0 • Python 3.8

ThibaultMarzullo commented 1 year ago

Ciao @GiuseppeR96 ,

While I cannot talk for the Spawn development team, I can tell you how we compile our models. If I cannot help you solve this issue, then I suggest you reach our to the Spawn team.

We do not compile the FMU directly from the Modelica IDE, instead we use Michael Wetter's Docker image with pyModelica installed to compile and export the FMU. If you are familiar with Docker, I suggest you create a container based on michaelwetter/ubuntu-1804_jmodelica_trunk. If you want to see an example of a Dockerfile used for creating such a container, see this file, found in the testing folder. Within the docker container, you can run a Python script with the following: from pymodelica import compile_fmu fmu_path = compile_fmu(model_path, file_name) This should save a compiled FMU in the Docker container. There are simple functions for copying from/to docker containers, you can find this information online pretty easily. So in summary, you would need to:

Having Docker in the way seems cumbersome, but it is quite easy to be honest. The reason we use this Docker container is because it is one of the last available sources for JModelica, and Spawn usually compiles well with JModelica.

In our case, since we need some extra steps during compilation, we are doing things a little differently. I write it here in case you find something useful for your application:

Last note: we were using the Modelica Buildings Library v8.0.0 to build our Spawn models, but now v9.0.0 is out. You should consider updating if you have a chance. Spawn is being actively developed, and I believe it now runs on Windows systems too.

Let me know if you have other questions on compiling Spawn FMUs, or on setting the Docker image up. Cordiali saluti Thibault

ThibaultMarzullo commented 1 year ago

I will reopen this issue if there is activity on this thread