modelon-community / PyFMI

PyFMI is a package for loading and interacting with Functional Mock-Up Units (FMUs) both for Model Exchange and Co-Simulation, which are compiled dynamic models compliant with the Functional Mock-Up Interface (FMI)
http://jmodelica.org/pyfmi
GNU Lesser General Public License v3.0
160 stars 38 forks source link

Error on loading Fmu #240

Closed YanceyYq closed 3 months ago

YanceyYq commented 4 months ago

environment ubuntu python=3.9 pyfmi=2.13 CoSimulation model fmiVersion=2 isssues when model_sub1 = FMUModelCS2("./Sin.fmu")The error has appeared. Traceback (most recent call last): File "/home/yancey/Desktop/FMU/Fmu/Test/Lin/6.6/4.0/Run/fmiLin.py", line 7, in model_sub1 = FMUModelCS2("./Sin.fmu") File "src/pyfmi/fmi.pyx", line 6997, in pyfmi.fmi.FMUModelCS2.init File "src/pyfmi/fmi.pyx", line 4135, in pyfmi.fmi.FMUModelBase2.init pyfmi.fmi.InvalidBinaryException: The FMU could not be loaded. Error loading the binary. Could not load the FMI function 'fmi2GetStringStatus'. /tmp/yancey/JModelica.org/jm_tmpyk86h234/binaries/linux64/Sin.so: undefined symbol: fmi2GetStringStatus

To satisfy the usage of FMUModelCS2, what is needed in Sin.so?

modelonrobinandersson commented 4 months ago

Hi @YanceyYq, the FMU needs to support the FMI Interface. In your case it looks like the FMU is missing an implementation for fmi2GetStringStatus, you can see a list of the exposed symbols in your so-file using nm -gD Sin.so

You can see a list of all the fmi functions for CS FMUs in the FMI specification, page 110 of 132.

YanceyYq commented 3 months ago

use nm -gD Sin.so fmi2Instantiate exists. (base) yancey@yancey-VirtualBox:~/Desktop/FMU/Fmu/Test/Lin/6.6/7.0/Sin/Sin/binaries/linux64$ nm -gD Sin.so w __cxa_finalize@GLIBC_2.2.5 U exit@GLIBC_2.2.5 00000000000013a5 T fmi2FreeInstance U fmi2GetStringStatus U fmi2Initialize U fmi2Instantiate when model_sub1 = FMUModelCS2("./Sin.fmu")The error has appeared. Traceback (most recent call last): File "/home/yancey/Desktop/FMU/Fmu/Test/Lin/6.6/7.0/Run/fmiLin.py", line 7, in model_sub1 = FMUModelCS2("./Sin.fmu") File "src/pyfmi/fmi.pyx", line 6997, in pyfmi.fmi.FMUModelCS2.init File "src/pyfmi/fmi.pyx", line 4135, in pyfmi.fmi.FMUModelBase2.init pyfmi.fmi.InvalidBinaryException: The FMU could not be loaded. Error loading the binary. Could not load the FMU binary: /tmp/yancey/JModelica.org/jm_tmpjidxwtn0/binaries/linux64/Sin.so: undefined symbol: fmi2Instantiate

modelonrobinandersson commented 3 months ago

Hi @YanceyYq, the U here in U fmi2Instantiate means that the symbol is used by the library but not defined in any of the object files used when creating that library, which is also why you see the error.

YanceyYq commented 3 months ago

I have referenced the fmi-library, and the function definitions are in fmi2Functions.h.

modelonrobinandersson commented 3 months ago

Hi @YanceyYq! Did you build fmi library? it is not just headers that are utilized from fmi library, also the libraries that have to be built from source

YanceyYq commented 3 months ago

library built from source code. When using the nm command to view it, there is no fmi2Instantiate. error

modelonrobinandersson commented 3 months ago

Hi @YanceyYq, the issue is not in FMI Library but likely in the tool you have used to generate the FMU. PyFMI is using FMI Library to access specific functions on the FMU, for example in PyFMI you have the function instantiate which in turn calls fmi2_import_instantiate which in turn via FMIL invokes fmi2Instantiate on the FMU itself. As far as I can tell from the information you have provided, the symbol is undefined in the FMU itself, since the function is referenced in the FMU but not defined, it is likely a linking issue in the FMU generation.

YanceyYq commented 3 months ago

Thank you for your help.