OpenModelica / OMSimulator

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

replaceSubmodel6.py throws errors #1194

Closed sashkapl closed 1 year ago

sashkapl commented 1 year ago

Description

I am working with a few fmu models and I am searching for possibilities to replace sub Models in ssp file. I am not able to contain connections between differently named interfaces (x connected to y)

Steps to reproduce the behavior

You have the same errors in the example python code replaceSubmodel6.py

Expected behavior

Ability to replace the model with keeping the existing connection

Screenshots


Result:
error:   [getVariable] Unknown signal "model.root.A.dummy"
warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../resources/replaceA_extended.fmu"
warning: deleting start value "A.t" in "inline" resources, because the identifier couldn't be resolved to any system signal in the replacing model

Version and OS

arun3688 commented 1 year ago

@sashkapl that is the expected output for replaceSubmodel6.py because in the replacing fmu there is no variable dummy if you look into the example there are two connection before replacing

A.dummy -> B.u1
A.y ->B.u

then we are replacing the A.fmu with replaceA_extended and replaceA_extended does not contain dummy variable and it will be deleted , but retaining the connectionA.y ->B.u

sashkapl commented 1 year ago

@arun3688 I post you my code, and I get the same error


oms.addSubModel("model.root.sin", "Sinus.fmu")

oms.addSubModel("model.root.Gain", "Gain_1.fmu")

oms.addConnection("model.root.Gain.u","model.root.sin.y")

src, status = oms.exportSnapshot("model")

oms.export(cref,"testSSP.ssp")
status = oms.terminate(cref)
status = oms.delete(cref)   

oms.importFile("testSSP.ssp")
oms.replaceSubModel("model.root.Gain", "Gain_2.fmu",False)

where Gain_1 and Gain_2 has the same interfaces

error:   [getVariable] Unknown signal "model.root.Gain.y"
error:   [getVariable] Unknown signal "model.root.Gain.y"
warning: deleting connection "sin.y ==> Gain.u", as signal "y" couldn't be resolved to any signal in the replaced submodel "Gain_2.fmu"
arun3688 commented 1 year ago

@sashkapl i see that oms.addConnection("model.root.Gain.u","model.root.sin.y") is ACausal (i.e)input -> output, Which should not be a problem here, But can you please change the connection to something like below

oms.addConnection("model.root.sin.y", "model.root.Gain.u")

and test the code, I think this should retain the connection, Could you please test it

sashkapl commented 1 year ago

Code:


from OMSimulator import OMSimulator
oms = OMSimulator()

oms.setCommandLineOption("--suppressPath=true")
oms.setTempDirectory("./temp/")

oms.newModel("model")

oms.addSystem("model.root", oms.system_wc)

oms.addSubModel("model.root.A", "Sinus.fmu")
oms.addSubModel("model.root.B",  "Gain_1_wrongInterface.fmu")

## add connections
oms.addConnection("model.root.A.y", "model.root.B.u")

oms.setResultFile("model", "replaceSubmodel6.mat")

src, status = oms.exportSnapshot("model")

oms.export("model", "replaceSubmodel6.ssp")
oms.terminate("model")
oms.delete("model")

oms.importFile("replaceSubmodel6.ssp")

oms.replaceSubModel("model.root.A", "Gain_2_wrongInterface.fmu", False)

src, status = oms.exportSnapshot("model")

oms.instantiate("model")
oms.initialize("model")

oms.simulate("model")

oms.terminate("model")
oms.delete("model")

Error:


error:   [getVariable] Unknown signal "model.root.A.y"
warning: deleting connection "A.y ==> B.u", as signal "y" couldn't be resolved to any signal in the replaced submodel "Gain_2_wrongInterface.fmu"
info:    Result file: replaceSubmodel6.mat (bufferSize=1)
info:    1 warnings
info:    1 errors

Interfaces in openModelica Gui:

image

arun3688 commented 1 year ago

@sashkapl That is the expected output, Because you are replacing Sine.fmu with Gain2_wronginterface.fmu and Gain_2_wrongInterface.fmu does not contain signal y and it has signal y_output and when you are replacing the connection looks like this A.y_output -> B.u which does not exist and hence you can see the error message

warning: deleting connection "A.y ==> B.u", as signal "y" couldn't be resolved to any signal in the replaced submodel "Gain_2_wrongInterface.fmu"

it will work only if the replacing fmu has same name , same type

sashkapl commented 1 year ago

My mistake while applying my model to your example code, should be oms.replaceSubModel("model.root.B", "Gain_2_wrongInterface.fmu", False) where B is gain

Same code with updated line: error: [getVariable] Unknown signal "model.root.B.y" error: [getVariable] Unknown signal "model.root.B.y" warning: deleting connection "A.y ==> B.u", as signal "y" couldn't be resolved to any signal in the replaced submodel "Gain_2_wrongInterface.fmu" info: Result file: replaceSubmodel6.mat (bufferSize=1) info: 1 warnings info: 2 errors

arun3688 commented 1 year ago

@sashkapl I made a test and it seems to work for me

from OMSimulator import OMSimulator
oms = OMSimulator()

oms.setCommandLineOption("--suppressPath=true")
oms.setTempDirectory("./replacesubmodel_11_py/")

oms.newModel("model")

oms.addSystem("model.root", oms.system_wc)
oms.addSubModel("model.root.A", "../resources/Modelica.Blocks.Sources.Sine.fmu")
oms.addSubModel("model.root.B", "../resources/Modelica.Blocks.Math.Gain.fmu")

## add connections
oms.addConnection("model.root.A.y", "model.root.B.u")
oms.setResultFile("model", "replaceSubmodel11.mat")
src, status = oms.exportSnapshot("model")
print(src)
oms.export("model", "replaceSubmodel11.ssp")
oms.terminate("model")
oms.delete("model")
oms.importFile("replaceSubmodel11.ssp")
oms.replaceSubModel("model.root.B", "../resources/Modelica.Blocks.Math.Gain.fmu", False)
src, status = oms.exportSnapshot("model")
print(src)

oms.terminate("model")
oms.delete("model")

Could you make a ssp for your test case and put it here so that i can see whether i can reproduce the issue, Note: you should add all the submodels in the ssp

arun3688 commented 1 year ago

@sashkapl see the example here where the connections are retained similar to your example https://github.com/OpenModelica/OMSimulator/blob/master/testsuite/simulation/replaceSubmodel11.py

sashkapl commented 1 year ago

@arun3688 thank you for your fast responses, I am not able to upload anything from my work PC, I will create same files via my private PC (as they are only for test purpose) and upload here

sashkapl commented 1 year ago

I tested your code with following files:


error:   [getVariable] Unknown signal "model.root.B.y"
error:   [getVariable] Unknown signal "model.root.B.y"
warning: deleting connection "A.y ==> B.u", as signal "y" couldn't be resolved to any signal in the replaced submodel "Gain_2.fmu"
info:    1 warnings
info:    2 errors

Code:

from OMSimulator import OMSimulator
oms = OMSimulator()

oms.setCommandLineOption("--suppressPath=true")
oms.setTempDirectory("./temp/")

oms.newModel("model")

oms.addSystem("model.root", oms.system_wc)
oms.addSubModel("model.root.A", "Sinus.fmu")
oms.addSubModel("model.root.B", "Gain_1.fmu")

## add connections
oms.addConnection("model.root.A.y", "model.root.B.u")
oms.setResultFile("model", "result.mat")
src, status = oms.exportSnapshot("model")
#print(src)
oms.export("model", "replaceSubmodel.ssp")
oms.terminate("model")
oms.delete("model")
oms.importFile("replaceSubmodel.ssp")
oms.replaceSubModel("model.root.B", "Gain_2.fmu", False)
src, status = oms.exportSnapshot("model")
#print(src)

oms.terminate("model")
oms.delete("model")

FmusSSP.zip

image

arun3688 commented 1 year ago

@sashkapl I ran the ssp file provided by you and it works for me without any issues and the connections are retained

from OMSimulator import OMSimulator
oms = OMSimulator()

oms.setCommandLineOption("--suppressPath=true")
oms.setTempDirectory("./replacesubmodel_11_py/")

oms.newModel("model")

oms.addSystem("model.root", oms.system_wc)

oms.addSubModel("model.root.A", "../resources/FmusSSP/Sinus.fmu")
oms.addSubModel("model.root.B", "../resources/FmusSSP/Gain_1.fmu")

## add connections
oms.addConnection("model.root.A.y", "model.root.B.u")

oms.setResultFile("model", "replaceSubmodel11.mat")

src, status = oms.exportSnapshot("model")
##print(src)

oms.export("model", "replaceSubmodel11.ssp")
oms.terminate("model")
oms.delete("model")

oms.importFile("replaceSubmodel11.ssp")

oms.replaceSubModel("model.root.B", "../resources/FmusSSP/Gain_2.fmu", False)

src, status = oms.exportSnapshot("model")
print(src)

oms.terminate("model")
oms.delete("model")

Output , connections are retained

##         <ssd:Connections>
##           <ssd:Connection
##             startElement="A"
##             startConnector="y"
##             endElement="B"
##             endConnector="u" />
##         </ssd:Connections>

I guess there is some other issue, My OMSimulator version is

# OMSimulator --version
OMSimulator v2.1.1.post190-g4390f41-mingw
sashkapl commented 1 year ago

Version: OMSimulator v2.1.1.post159-g606161d-mingw

I can reinstall it, is there any other option to test this function?

arun3688 commented 1 year ago

@sashkapl I guess it is old for the functionality, you should update the OMSimulator to current master and test it and then it should work

sashkapl commented 1 year ago

OMSimulator v2.1.1.post159-g606161d-mingw

Same code, same files, same error I will ask my colleagues to repeat it

arun3688 commented 1 year ago

@sashkapl Your version is old, Please do a git pull on the OMSimulator and run the tests, the version should be OMSimulator v2.1.1.post190-g4390f41-mingw

sashkapl commented 1 year ago

I can only use this version, as it is latest with pip installation (I retrieve it via artifactory of my organization) maybe this is the issue

arun3688 commented 1 year ago

@sashkapl it is true that pip package is not updated, but you can clone from the repository and build it as OMSimulator is a stand alone package

arun3688 commented 1 year ago

@sashkapl , You can download the latest artifacts fromhttps://build.openmodelica.org/omsimulator/nightly/according to your platform, And also we have updated our pip package to latest master.

sashkapl commented 1 year ago

@arun3688, thank you for updating pip, I see it but I am not able to update my own package as it is not passing our companies artifactory (need to be updated I assume), once I will be able to update my pip version I will come back to you

sashkapl commented 1 year ago

@arun3688 issue resolved with latest version, thank you for the support