NTNU-IHB / PythonFMU

A lightweight framework that enables the packaging of Python3.x code as co-simulation FMUs
MIT License
131 stars 40 forks source link

Error loading FMU file using MATLAB simulink due to library error #110

Closed davidel27 closed 3 years ago

davidel27 commented 3 years ago

Hello I have some issues with loading the fmu file that generated using PythonFMU to MATLAB simulink FMU import module. The model in matlab is the same model as the given example, just instead of using pyFMI I use matlab simulink. It recognises the model, the inputs and outputs of the FMU file, but when I try to simulate it, it results with the following error:

Error in supplied FMU: Unable to load dynamic library 'C:\Users\david\Documents\FMU\slrprj_fmu\c9a9319\PythonSlave\binaries\win64\PythonSlave.dll': The specified module could not be found. For more information, see the FMU troubleshooting documentation.

I checked for the following dll and it is do exist and it seems to be correct. Is there a way to fix that? Thank you and best regards

markaren commented 3 years ago

The specified module could not be found.

This error could mean that PythonSlave.dll itself is missing some dependency. The first thing I would check is whether or not Python is on PATH. PythonFMU requires Python3 on the system (and added to PATH).

You can also use Dependeny Walker to see what dlls the FMU requires (plain PythonFMU requires Python3.dll only)

davidel27 commented 3 years ago

Hi thank you for your fast replay. You were right, python was not added to my path. I use anaconda thefore I added to path the following directorys: ../Anaconda3 ../Anaconda3/Scripts In the first directory there is python.exe and python3.dll. I managed to launch python and build the fmu directly through the cmd. (i.e. python pythonfmu build -f) Now it is no longer shows the initial error but just kills the MATLAB and simulink with no reason given.

markaren commented 3 years ago

How about other importing tools? It is quick to check with FMPy using python -m fmpy.gui

davidel27 commented 3 years ago

It is still not working.

I opened a new folder in C:\test and created python file with the name FMUtest.py:

image

Later I opened the CMD as an admini: and wrote the following commands:

image

And the FMU file were created:

image

Using FMPy the following error occures:

image

Same error for the FMPy GUI when trying to load the FMU.

Using MATLAB simulink, it does recognise the FMU and the outputs:

image

but when trying to run simulation, it crashes with the following error:

image image

I also tried to use RecurDyn as master but it also crushed without giving an error.

markaren commented 3 years ago

Can you share the FMU you have?

markaren commented 3 years ago

You are using Python 64 bit right?

davidel27 commented 3 years ago

Yes I have python 64 and the FMU file:

FMUtest.zip

markaren commented 3 years ago

image

I'm running Python 3.7. So I guess the Python 3.9 API have some new functions. Nevertheless, the FMU loads and provides an error. Could it be your Python version? Perhaps Python 3.9 is just incompatible with PythonFMU. I think 3.8 is the highest we have tested.

davidel27 commented 3 years ago

I use python 3.8.3

davidel27 commented 3 years ago

I will try python 3.7 and let you know. thank you very much!

markaren commented 3 years ago

Seems Python 3.9 is also ok: https://github.com/NTNU-IHB/PythonFMU/pull/111

davidel27 commented 3 years ago

Can you give me the environment that you use? Is there additional stuff I need to install except python, PythonFMU? (with no external packges)

markaren commented 3 years ago

I'm using plain Python 3.7.4. pip install pythonfmu is enough if you only use core Python libraries. However, the CI is running with versions ranging from 3.6 to 3.8.

Btw, I looked at the time API and there is no function called pause. So that would lead to a crash. Also, perhaps MATLAB/Simulink do not like the needsExecutionTool=true attribute in the modelDescription.xml ? Try re-exporting with --no-external-tool

markaren commented 3 years ago

Also the error you get from fmpy does not seem to be related to PythonFMU (etree). Does it work with other FMUs?

davidel27 commented 3 years ago

I created a new environment in anaconda named py374 and have a python version 3.7.4. I installed the libraries: pythonfmu FMPy

I changed a little bit the code: image

and build using the command: pythonfmu build -f FMUtest.py --no-external-tool

Using FMPy GUI it succeed!!: image

But Using MATLAB simulink it resulted with the same error as before: image

I will try to contact MathWork support to obtain the solution.

The files in the folder: FMUtest.zip

Best regards and Thank you very much!!

markaren commented 3 years ago

Closing. Please re-open if you find that PythonFMU has an issue that keeps it from working with MATLAB/Simulink.

MinsGoing commented 5 months ago

I had the same problem, did you solve it?

davidel27 commented 5 months ago

I had the same problem, did you solve it?

Yes I have managed to solve it.

Can you explicitly say which software you used as master? And which python environment do you have? (is it anaconda?) And also the operating system. The solution may be a little bit diffrent in each scenario.

MinsGoing commented 5 months ago

I had the same problem, did you solve it?

Yes I have managed to solve it.

Can you explicitly say which software you used as master? And which python environment do you have? (is it anaconda?) And also the operating system. The solution may be a little bit diffrent in each scenario.

I'm using Anaconda environment(D:\Anaconda3\envs\fmi_env) on win11 to execute pythonfmu build -f .I added (D:\Anaconda3\envs\fmi_env) to the system path. Everything works fine in FMPy GUI, on the same machine, import to simulink (matlab 2020R) without scopeconnected, click to run without crashing, connect to scopeto run and it crashes.

davidel27 commented 5 months ago

I had the same problem, did you solve it?

Yes I have managed to solve it. Can you explicitly say which software you used as master? And which python environment do you have? (is it anaconda?) And also the operating system. The solution may be a little bit diffrent in each scenario.

I'm using Anaconda environment(D:\Anaconda3\envs\fmi_env) on win11 to execute pythonfmu build -f .I added (D:\Anaconda3\envs\fmi_env) to the system path. Everything works fine in FMPy GUI, on the same machine, import to simulink (matlab 2020R) without scopeconnected, click to run without crashing, connect to scopeto run and it crashes.

The main issue is the usage of anaconda. In anaconda environment under windows, the environment variables are not properly defined. Although, manually adding the environment variables in the system PATH.

The solution that worked for me is to uninstall the anaconda framework and reinstall anaconda with "Add to PATH envirnment variables..." option checked (although it is not recomended by the anaconda installer).

Make sure to reboot your machine after reinstalling anaconda and that should work.

MinsGoing commented 5 months ago

I had the same problem, did you solve it?

Yes I have managed to solve it. Can you explicitly say which software you used as master? And which python environment do you have? (is it anaconda?) And also the operating system. The solution may be a little bit diffrent in each scenario.

I'm using Anaconda environment(D:\Anaconda3\envs\fmi_env) on win11 to execute pythonfmu build -f .I added (D:\Anaconda3\envs\fmi_env) to the system path. Everything works fine in FMPy GUI, on the same machine, import to simulink (matlab 2020R) without scopeconnected, click to run without crashing, connect to scopeto run and it crashes.

The main issue is the usage of anaconda. In anaconda environment under windows, the environment variables are not properly defined. Although, manually adding the environment variables in the system PATH.

The solution that worked for me is to uninstall the anaconda framework and reinstall anaconda with "Add to PATH envirnment variables..." option checked (although it is not recomended by the anaconda installer).

Make sure to reboot your machine after reinstalling anaconda and that should work.

Thank you very much, now that it's working