OpenModelica / OMSimulator

The OpenModelica FMI & SSP-based co-simulation environment.
Other
67 stars 49 forks source link

Failed to load functions for FMI 2.info: fmi2Instantiate() failed #1282

Closed Soren72 closed 3 months ago

Soren72 commented 5 months ago

Description

1) FMU of ThermoFluidStream.SimpleAirCycle example exported from Open Modelica as CS with CVODE. 2) Upon instantiation in OMSimulator, the following error is reported:

Failed to load functions for FMI 2.info: fmi2Instantiate() failed Function "fmi2FreeInstance" is not provided by this FMU.

It is unclear weather this is a OMSimulator problem or a Open Modelica export Issue. I can provide the FMU if needed. Any ideas?

@adrpo @arun3688

Version and OS

arun3688 commented 5 months ago

@Soren72 can you upload the fmu, i can test it

Soren72 commented 5 months ago

ThermofluidStream.Examples.SimpleAirCycle.zip

I had to rename the file to be able to attach it.

Soren72 commented 5 months ago

This is my very first "issue" and do not know how the notification in Github works. I hereby explicity notify you :-) @arun3688

arun3688 commented 5 months ago

@Soren72 Unfortunately i cannot test your fmu, as it has linux binaries and i am in windows , but i will try to generate the windows binaries for your model and see if i can run it

arun3688 commented 5 months ago

@Soren72 I successfully exported the fmu and tested with OMSimulato, but it reports me the following error with euler as solver

# OMSimulator C:/OPENMODELICAGIT/sitl_backend/ThermofluidStream.Examples.SimpleAirCycle.fmu --solver=euler
info:    Result file: model_res.mat (bufferSize=10)
LOG_ASSERT        | debug   | The arguments u_min and u_max provided in the function call
|                 | |       |     solveOneNonlinearEquation(f,u_min,u_max)
|                 | |       | do not bracket the root of the single non-linear equation 0=f(u):
|                 | |       |   u_min  = 190
|                 | |       |   u_max  = 647
|                 | |       |   fa = f(u_min) = -3.98813e+06
|                 | |       |   fb = f(u_max) = -3.52119e+06
|                 | |       | fa and fb must have opposite sign which is not the case
error:   [fmi2logger] C:/OPENMODELICAGIT/OpenModelica/model-4oq3o9vy/temp/0001_fmu (logFmi2Call): fmi2GetBoolean: terminated by an assertion.
error:   [updateSignals] failed to fetch variable turbine1.omega_from_input
LOG_ASSERT        | debug   | The arguments u_min and u_max provided in the function call
|                 | |       |     solveOneNonlinearEquation(f,u_min,u_max)
|                 | |       | do not bracket the root of the single non-linear equation 0=f(u):
|                 | |       |   u_min  = 190
|                 | |       |   u_max  = 647
|                 | |       |   fa = f(u_min) = -3.98813e+06
|                 | |       |   fb = f(u_max) = -3.52119e+06
|                 | |       | fa and fb must have opposite sign which is not the case
error:   [fmi2logger] C:/OPENMODELICAGIT/OpenModelica/model-4oq3o9vy/temp/0001_fmu (logFmi2Call): fmi2GetDerivatives: terminated by an assertion.
error:   [getDerivatives] fmi2_getDerivatives failed for FMU "model.root.fmu"
LOG_ASSERT        | debug   | The arguments u_min and u_max provided in the function call
|                 | |       |     solveOneNonlinearEquation(f,u_min,u_max)
|                 | |       | do not bracket the root of the single non-linear equation 0=f(u):
|                 | |       |   u_min  = 190
|                 | |       |   u_max  = 647
|                 | |       |   fa = f(u_min) = -3.98813e+06
|                 | |       |   fb = f(u_max) = -3.52119e+06
|                 | |       | fa and fb must have opposite sign which is not the case
error:   [fmi2logger] C:/OPENMODELICAGIT/OpenModelica/model-4oq3o9vy/temp/0001_fmu (logFmi2Call): fmi2GetBoolean: terminated by an assertion.
error:   [updateSignals] failed to fetch variable turbine1.omega_from_input
info:    0 warnings
info:    6 errors

with cvode solver the fmu is going into infinite loop something like below

[CVODE ERROR]  CVode
  At t = 0 and h = 1e-12, the corrector convergence test failed repeatedly or with |h| = hmin.

error:   [doStep] SUNDIALS_ERROR: CVode() failed with flag = -4

I suggest you to download the latest OpenModelica version and reexport the fmu and test it

Soren72 commented 5 months ago

@arun3688 I have Official Release 1.22.1. Du du mean Official, Stable or Nightly?

arun3688 commented 5 months ago

@Soren72 download the nightly version as it has the latest fixes

Soren72 commented 5 months ago

@arun3688 have now tested with nightly 1.23.0 with the same results as stable.

Soren72 commented 5 months ago

@arun3688 , any other suggestions on how I can progress in the testing?

arun3688 commented 5 months ago

@Soren72 I did some more debugging and in the end i found that it is related to convergence problem, and then i reduced the tolerance to 1e-5 and then the fmu works see the results below

# OMSimulator C:/OPENMODELICAGIT/sitl_backend/ThermofluidStream.Examples.SimpleAirCycle.fmu --tolerance=1e-5
info:    maximum step size for 'model.root': 0.200000
info:    Result file: model_res.mat (bufferSize=10)
info:    Final Statistics for 'model.root':
         NumSteps = 1015 NumRhsEvals  = 1269 NumLinSolvSetups = 134
         NumNonlinSolvIters = 1268 NumNonlinSolvConvFails = 9 NumErrTestFails = 36

I hope this solves the issue

Soren72 commented 5 months ago

@arun3688 Good! Did you use Euler or CVODE? We use and prefer CVODE.

arun3688 commented 5 months ago

@Soren72 I used CVODE

Soren72 commented 4 months ago

@arun3688 I still have problem and we think that the way forward is that you test it in the same OS, Ubuntu, that we have instead of windows. Is that possible? Ubuntu can be tested via an external drive or via a memory stick.

I also simplified the problem by only creating a FMU of a MSL step function component. Creating a FMU with Explicit Euler and run it in OMSimulator works. But creating a FMU with CVODE and run it in OMSimulator results in:

/opt/venv/3.10/bin/python3 /home/a97073/Neumann/Slask/SimulateSSP.py OMSimulator v2.1.1.post247-g614c663-linux-notlm-debug error: [terminate] Model "model" is in wrong model state Loading shared object failed: /home/a97073/Neumann/Slask/model-o0hkdxrm/temp/0001_SimpleAirCycle_Test/binaries/linux64/Slask_simple_test_modelica_CVODE.so (libsundials_cvode.so.5: cannot open shared object file: No such file or directory) Failed to load functions for FMI 2.info: fmi2Instantiate() failed Function "fmi2FreeInstance" is not provided by this FMU.

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

arun3688 commented 4 months ago

@Soren72 can you share your python script, it says the model is in wrong state.

Soren72 commented 4 months ago

@arun3688 See below for the script and I tried to attach the FMU by adding the extention .txt :-) Remove .txt before using it in Ubuntu.

Slask.simple_test_modelica_CVODE.fmu.txt

import OMSimulator oms = OMSimulator.OMSimulator() a = oms.getVersion() print(a) oms.setCommandLineOption("--ignoreInitialUnknowns=true") oms.setCommandLineOption("--suppressPath=true") oms.setCommandLineOption("--wallTime=true") oms.newModel("model") oms.addSystem("model.root", oms.system_wc) oms.addSubModel("model.root.SimpleAirCycle_Test", "Slask.simple_test_modelica_CVODE/Slask.simple_test_modelica_CVODE.fmu") oms.setResultFile("model", "sim_results.csv") oms.setStopTime("model", 1200) oms.setFixedStepSize("model", 1e-3) oms.setLoggingInterval("model", 1) oms.instantiate("model") oms.initialize("model") oms.simulate("model") oms.terminate("model") oms.delete("model")

arun3688 commented 4 months ago

@Soren72 I see that you are using OpenModelica version 1.22.0 and also OMSimulator version is also older, Modelica.Blocks.Source.Step is a very simple model and it should work by default regardless of the platform, please download the latest nightly version of Openmodelica and export the fmu and test it, this is my suggestion

Soren72 commented 4 months ago

@arun3688 now with OpenModelica version 1.23.0~dev-333-g4f26c77 and OMSimulator v2.1.1.post249-gc66d712-linux-notlm-debug:

/opt/venv/3.10/bin/python3 /home/sorst/Documents/Modelica/ThermofluidStream.Examples.SimpleAirCycle/temporary/OMS_script/SimulateSSP.py OMSimulator v2.1.1.post249-gc66d712-linux-notlm-debug error: [terminate] Model "model" is in wrong model state Loading shared object failed: /home/sorst/Documents/Modelica/ThermofluidStream.Examples.SimpleAirCycle/temporary/OMS_script/model-gm53rv2q/temp/0001_SimpleAirCycle_Test/binaries/linux64/SAC_CVODE_1_23_0.so (libsundials_cvode.so.5: cannot open shared object file: No such file or directory) Failed to load functions for FMI 2.info: fmi2Instantiate() failed Function "fmi2FreeInstance" is not provided by this FMU.

Process finished with exit code 139 (interrupted by signal 11:SIGSEGV) SAC_CVODE_1_23_0.fmu.txt

Soren72 commented 4 months ago

@arun3688 I tested the FMU in Dymola and got following similar error. I guess that means that it is probably OMEdit FMU export with CVODE in Ubuntu that is the problem. Agree? Should we close this issue and instead open a new one at OpenModelica. What do you prefer?

Log-file of program ./dymosim (generated: Mon Feb 26 09:53:13 2024)

dymosim started ... "Unnamed" simulating ... "dsin.txt" loading (dymosim input file) Error: The following error was detected at time: 0 libsundials_cvode.so.5: cannot open shared object file: No such file or directory The stack of functions is: SAC_CVODE_1_23_0_fmu.fmi_Functions.fmiModel SAC_CVODE_1_23_0_fmu.fmi_Functions.fmiModel("SAC_CVODE_1_23_0_fmu", sAC_CVODE_1_23_0_fmu.fmi_loggingOn, "file:////home/a97073/Neumann/Slask/Resources/Library/FMU/SAC_CVODE_1_23_0/resources", 0, 1)

Error: Integrator failed to start model.

... Error message from dymosim

ERROR: The simulation of Unnamed FAILED

arun3688 commented 4 months ago

@Soren72 can you generate the fmu in dymola with CVode solver and test it

arun3688 commented 4 months ago

@Soren72 I generated the Modelica.Blocks.Sources.Step.fmu in ubuntu with the following version

Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy

And the generated fmu works perfectly fine in OMSimulator with cvode

arun@dev:~/OpenModelica/test$ ../build/bin/OMSimulator Step.fmu --mode=cs --solver=cvode
info:    Result file: model_res.mat (bufferSize=10)

Can you test the attached fmu and see if it works

Step.zip

Soren72 commented 4 months ago

@arun3688 Your FMU works! Interesting. What else can differ between our environments? I will create a new and fresh UBUNTU environment and describe all steps that I do for you to see if you see something that differ from your installation.

Soren72 commented 4 months ago

@arun3688 It works if I install to a clean Ubuntu installation: cmake and "apt-get install libsundials-dev". Do you also do that and is it correct? I thought the needed libsundials was included in the OpenModelica installation since you can select CVODE at FMU generation and you do not get any information that the FMU is incorrect if you do not have the libsundials-dev installed.

arun3688 commented 4 months ago

@Soren72 No i did not do apt-get install libsundials-dev, the needed dlls should be included in OpenModelica installation atleast the sundials library , I am not a linux user but i hope the server already had libsundials installed so i am not exactly sure about this

Soren72 commented 4 months ago

@arun3688 My conclusion is that sundials library may not be included in the OpenModelica installation. If you are not a Linux user, do you involve someone that is it?

arun3688 commented 4 months ago

@arun3688 My conclusion is that sundials library may not be included in the OpenModelica installation. If you are not a Linux user, do you involve someone that is it? @AnHeuermann can you please comment on this, is this true sundials library not included in the OpenModelica installation of linux

AnHeuermann commented 4 months ago

I think the issue is already fixed in https://github.com/OpenModelica/OpenModelica/pull/12045.

From the FMU uploaded by @Soren72 I can see, that the compiler flags are set correctly, but the cvode libs are missing from the binaries directory:

binaries
└── linux64
    └── SAC_CVODE_1_23_0.so

Try again with the latest nightly build of OpenModelica. It changed

elseif(RUNTIME_DEPENDENCIES_LEVEL STREQUAL "modelica")
  install(TARGETS ${FMU_NAME}
    RUNTIME_DEPENDENCIES
-      DIRECTORIES ${EXTERNAL_LIBDIRECTORIES}
+      DIRECTORIES ${EXTERNAL_LIBDIRECTORIES} ${CVODE_DIRECTORY}
      PRE_EXCLUDE_REGEXES "api-ms-" "ext-ms-"
      POST_EXCLUDE_REGEXES "^\\/lib.*" "^\\/usr\\/lib.*" "^\\/usr\\/local\\/lib.*" ".*system32/.*\\.dll"
    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR})
Soren72 commented 4 months ago

Edit 2024-03-11: added "@username" @arun3688 & @AnHeuermann

No change with Connected to OpenModelica 1.23.0~dev-368-g5596fcb Connected to OMSimulator 2.1.1~248-gf7c50c9-linux-notlm-debug

step_SAAB.zip OMSimulator step_SAAB.fmu --mode=cs --solver=cvode Loading shared object failed: /home/sorst/Downloads/model-ufwzb3gr/temp/0001_step_SAAB/binaries/linux64/STEP_SAAB_nightly.so (libsundials_nvecserial.so.5: cannot open shared object file: No such file or directory) Failed to load functions for FMI 2.info: fmi2Instantiate() failed error: [terminate] Model "model" is in wrong model state Function "fmi2FreeInstance" is not provided by this FMU. Segmentation fault (core dumped)

By the way, the possibility to use "FMU Name" in FMI Option menu does not work. The FMU gets the model name+fmu. Should I start a separate Issue for that?

AnHeuermann commented 3 months ago

I can't reproduce the issue with omc v1.23.0-dev-376-g4df9b45477-cmake and OMSimulator v2.1.1.post248-gf7c50c9-linux-notlm-debug on Ubuntu 22.04.3:

buildFMU.mos

loadModel(ThermofluidStream);

cd("temp"); getErrorString();
setCommandLineOptions("--fmiFlags=s:cvode --fmuRuntimeDepends=modelica"); getErrorString();
buildModelFMU(ThermofluidStream.Examples.SimpleAirCycle, fmuType="cs"); getErrorString();

Build and simulate

mkdir temp
omc test.mos
OMSimulator --mode=cs --solver=cvode temp/SimpleAirCycle.fmu &> sim.log
LOG_SOLVER        | info    | CVODE linear multistep method CV_BDF
LOG_SOLVER        | info    | CVODE maximum integration order CV_ITER_NEWTON
LOG_SOLVER        | info    | CVODE use equidistant time grid YES
LOG_SOLVER        | info    | CVODE Using relative error tolerance 1.000000e-06
LOG_SOLVER        | info    | CVODE Using dense internal linear solver SUNLinSol_Dense.
LOG_SOLVER        | info    | CVODE Use internal dense numeric jacobian method.
LOG_SOLVER        | info    | CVODE uses internal root finding method NO
LOG_SOLVER        | info    | CVODE maximum absolut step size 0
LOG_SOLVER        | info    | CVODE initial step size is set automatically
LOG_SOLVER        | info    | CVODE maximum integration order 5
LOG_SOLVER        | info    | CVODE maximum number of nonlinear convergence failures permitted during one step 10
LOG_SOLVER        | info    | CVODE BDF stability limit detection algorithm OFF
[...]
AnHeuermann commented 3 months ago

Never mind, I found the issue why it's working for me but isn't for users. I have only the static version of CVODE available, so it's linked statically for me.

AnHeuermann commented 3 months ago

Okay, a quick repuild with -DSUNDIALS_BUILD_SHARED_LIBS=ON later and indeed I have all the dependencies installed in binaries/linux64:

binaries
└── linux64
    ├── SimpleAirCycle.so
    ├── libsundials_cvode.so.5
    ├── libsundials_cvode.so.5.4.0
    ├── libsundials_nvecserial.so.5
    └── libsundials_nvecserial.so.5.4.0

and OMSimulator can simulate it.

I'll give it a final go with a clean Docker image and an installed OpenModelica.

AnHeuermann commented 3 months ago

Now I have build the same FMU from a Docker environment with Ubuntu Focal and could reproduce parts of the issue:

openmodelica-user@58b62e2c3bcf:/issue-oms-1282/temp/FMU/binaries/linux64$ ldd SimpleAirCycle.so 
        linux-vdso.so.1 (0x00007ffcd0b44000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f3f3e5ce000)
        libsundials_nvecserial.so.5 => /usr/lib/x86_64-linux-gnu/omc/libsundials_nvecserial.so.5 (0x00007f3f3e5c3000)
        libsundials_cvode.so.5 => /usr/lib/x86_64-linux-gnu/omc/libsundials_cvode.so.5 (0x00007f3f3e58f000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3f3e56c000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3f3e37a000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f3f3e8f8000)

The issue is, that the CMake version (v3.16) is too low, so the functionality to install runtime dependencies isn't available. I'll create a PR to fix this on OpenModelica. If CMake is too old we'll link statically, so there are no CVODE runtime dependencies to install and the FMU will just work.


So let's rebuild the Docker container based on ubuntu:jammy and I can finally reproduce the error:

$ ldd SimpleAirCycle.so 
        linux-vdso.so.1 (0x00007ffcb53a4000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f13dcbe4000)
        libsundials_nvecserial.so.5 => not found
        libsundials_cvode.so.5 => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f13dc9bb000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f13dcea6000)
Loading shared object failed: /issue-oms-1282/model-yb93rc61/temp/0001_SimpleAirCycle/binaries/linux64/SimpleAirCycle.so (libsundials_nvecserial.so.5: cannot open shared object file: No such file or directory)
Failed to load functions for FMI 2.info:    fmi2Instantiate() failed
error:   [terminate] Model "model" is in wrong model state

Now the issue is, that when you install OpenModelica the libsundials_*.so are also installed into /urs/lib/x86_64-linux-gnu/omc, but we explicitly exclude libraries form system path /urs/lib from installing into binaries/linux64.

You can change this behavior with command line flag [--fmuRuntimeDepends=all](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/omchelptext.html#omcflag-fmuruntimedepends) or install OpenModelica / Sundials v1.5.0 on the target machine.

I'll see if I'm able to specify the exclude to not exclude the omc directory.

AnHeuermann commented 3 months ago

O the joy of writing regular expressions. Of course CMake's regex engine doesn't support negative lookahead or other nice features. But I found a solution using POST_INCLUDE_FILES.

50:50 that I'll crash all of the FMU export over the weekend or this issue is resolved with the next nightly build. We'll see on Monday.

You can place your bets with an reaction:

Soren72 commented 3 months ago

It seems to me that you found the bug and its solution Thanks. Now, take a well-deserved weekend. Let me know when the solution is included in the nightly build.

AnHeuermann commented 3 months ago

I checked again with the Docker container and omc 1.23.0~dev-378-g063c0d3 and it seems to be working 🎉

Soren72 commented 3 months ago

Yes. It works for me also in 1.23.0~dev-378-g063c0d3. Thanks.