Home > Software design >  OMPython using custom model with external package
OMPython using custom model with external package

Time:09-22

I am trying to simulate custom model using external package ModPowerSystems, and use OMPython to do some automation.

my code is:

from OMPython import OMCSessionZMQ
from OMPython import ModelicaSystem
from pathlib import Path

omc = OMCSessionZMQ()

source_path = Path(__file__).resolve()
source_dir = source_path.parent
mod=ModelicaSystem(str(source_dir) "/simulator/ModPowerSystems/package.mo",
                    "ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel",
                    ["ModPowerSystems","Modelica","ModPowerSystems.CustomModule.Loads.DependentModel1","ModPowerSystems.CustomModule.Generation.DependentModel2"])
mod.buildModel()

but when I run it, I got:

2021-09-22 09:39:04,743 - OMPython - INFO - OMC Server is up and running at 
2021-09-22 09:39:05,017 - OMPython - INFO - OMC Server is up and running at 
[<interactive>:1:9-1:13:writable] Warning: Lexer treating \ as \\, since \U is not a valid Modelica escape sequence.
[<interactive>:1:9-1:19:writable] Warning: Lexer treating \ as \\, since \A is not a valid Modelica escape sequence.
[<interactive>:1:9-1:33:writable] Warning: Lexer treating \ as \\, since \P is not a valid Modelica escape sequence.
[<interactive>:1:9-1:42:writable] Warning: Lexer treating \ as \\, since \T is not a valid Modelica escape sequence.
Notification: Automatically loaded package Modelica 3.2.3 due to uses annotation.
Notification: Automatically loaded package Complex 3.2.3 due to uses annotation.
Notification: Automatically loaded package ModelicaServices 3.2.3 due to uses annotation.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.

[C:/Program Files/OpenModelica1.16.0-64bit/lib/omlibrary/ModPowerSystems/EmtThreePhase/Examples/BasicGrids/package.order:0:0-0:0:readonly] Warning: The package.order file does not list all .mo files and directories (containing package.mo) present in its directory.
Missing names are:
        SynGen_Park_UnitTest_FullModel
Notification: ModPowerSystems requested package Modelica of version 3.2.2. Modelica 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.

[C:/Program Files/OpenModelica1.16.0-64bit/lib/omlibrary/ModPowerSystems/EmtThreePhase/Examples/BasicGrids/package.order:0:0-0:0:readonly] Warning: The package.order file does not list all .mo files and directories (containing package.mo) present in its directory.
Missing names are:
        SynGen_Park_UnitTest_FullModel
Notification: ModPowerSystems requested package Modelica of version 3.2.2. Modelica 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.

[C:/Program Files/OpenModelica1.16.0-64bit/lib/omlibrary/ModPowerSystems/EmtThreePhase/Examples/BasicGrids/package.order:0:0-0:0:readonly] Warning: The package.order file does not list all .mo files and directories (containing package.mo) present in its directory.
Missing names are:
        SynGen_Park_UnitTest_FullModel
Notification: ModPowerSystems requested package Modelica of version 3.2.2. Modelica 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.

Notification: ModPowerSystems requested package Modelica of version 3.2.2. Modelica 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Error: Cannot instantiate ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel due to class specialization PACKAGE.
Error: Class ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel not found in scope <TOP>.

Notification: ModPowerSystems requested package Modelica of version 3.2.2. Modelica 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Error: Cannot instantiate ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel due to class specialization PACKAGE.
Error: Class ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel not found in scope <TOP>.

I am super new to openmodelica so I am not sure whats missing here. Any suggestions?


Update 1:

I am following @adrian-pop suggestion, and got this new error:

2021-09-22 10:46:32,128 - OMPython - INFO - OMC Server is up and running at
[<interactive>:1:9-1:13:writable] Warning: Lexer treating \ as \\, since \U is not a valid Modelica escape sequence.
[<interactive>:1:9-1:19:writable] Warning: Lexer treating \ as \\, since \A is not a valid Modelica escape sequence.
[<interactive>:1:9-1:33:writable] Warning: Lexer treating \ as \\, since \P is not a valid Modelica escape sequence.
[<interactive>:1:9-1:42:writable] Warning: Lexer treating \ as \\, since \T is not a valid Modelica escape sequence.
Notification: Automatically loaded package Modelica 3.2.3 due to uses annotation.
Notification: Automatically loaded package Complex 3.2.3 due to uses annotation.
Notification: Automatically loaded package ModelicaServices 3.2.3 due to uses annotation.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.

Notification: ModPowerSystems requested package Modelica of version 3.2.2. Modelica 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
[C:/Users/Administrator/Projects/Proj/simulator/ModPowerSystems/Base/Interfaces/ComplexPhasor/ThreePhase/OnePortGrounded.mo:7:5-7:45:writable] Warning: No corresponding 'inner' declaration found for component .ModPowerSystems.Base.System DependentModel2.system declared as 'outer '.
  The existing 'inner' components are:
    There are no 'inner' components defined in the model in any of the parent scopes of 'outer' component's scope: ModPowerSystems.CustomModel.Generation.DependentModel2.
  Check if you have not misspelled the 'outer' component name.
  Please declare an 'inner' component with the same name in the top scope.
  Continuing flattening by only considering the 'outer' component declaration.
[C:/Users/Administrator/Projects/Proj/simulator/ModPowerSystems/CustomModel/Generation/DependentModel2.mo:11:3-11:43:writable] Warning: No corresponding 'inner' declaration found for component .ModPowerSystems.Base.System DependentModel2.system declared as 'outer '.
  The existing 'inner' components are:
    There are no 'inner' components defined in the model in any of the parent scopes of 'outer' component's scope: ModPowerSystems.CustomModel.Generation.DependentModel2.
  Check if you have not misspelled the 'outer' component name.
  Please declare an 'inner' component with the same name in the top scope.
  Continuing flattening by only considering the 'outer' component declaration.
[C:/Users/Administrator/Projects/Proj/simulator/ModPowerSystems/Base/Interfaces/ComplexPhasor/ThreePhase/OnePortGrounded.mo:7:5-7:45:writable] Warning: No corresponding 'inner' declaration found for component .ModPowerSystems.Base.System DependentModel1.system declared as 'outer '.
  The existing 'inner' components are:
    There are no 'inner' components defined in the model in any of the parent scopes of 'outer' component's scope: ModPowerSystems.CustomModel.Loads.DependentModel1.
  Check if you have not misspelled the 'outer' component name.
  Please declare an 'inner' component with the same name in the top scope.
  Continuing flattening by only considering the 'outer' component declaration.
[C:/Users/Administrator/Projects/Proj/simulator/ModPowerSystems/CustomModel/Examples/BasicGrids/MainModel.mo:12:3-12:32:writable] Error: Identifier time is reserved for the built-in element with the same name.
Error: Error occurred while flattening model ModPowerSystems.CustomModel.Examples.BasicGrids.MainModel

Notification: ModPowerSystems requested package Modelica of version 3.2.2. Modelica 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package Complex of version 3.2.2. Complex 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
Notification: ModPowerSystems requested package ModelicaServices of version 3.2.2. ModelicaServices 3.2.3 is used instead which states that it is fully compatible without conversion script needed.
[C:/Users/Administrator/Projects/Proj/simulator/ModPowerSystems/Base/Interfaces/ComplexPhasor/ThreePhase/OnePortGrounded.mo:7:5-7:45:writable] Warning: No corresponding 'inner' declaration found for component .ModPowerSystems.Base.System DependentModel2.system declared as 'outer '.
  The existing 'inner' components are:
    There are no 'inner' components defined in the model in any of the parent scopes of 'outer' component's scope: ModPowerSystems.CustomModel.Generation.DependentModel2.
  Check if you have not misspelled the 'outer' component name.
  Please declare an 'inner' component with the same name in the top scope.
  Continuing flattening by only considering the 'outer' component declaration.
[C:/Users/Administrator/Projects/Proj/simulator/ModPowerSystems/CustomModel/Generation/DependentModel2.mo:11:3-11:43:writable] Warning: No corresponding 'inner' declaration found for component .ModPowerSystems.Base.System DependentModel2.system declared as 'outer '.
  The existing 'inner' components are:
    There are no 'inner' components defined in the model in any of the parent scopes of 'outer' component's scope: ModPowerSystems.CustomModel.Generation.DependentModel2.
  Check if you have not misspelled the 'outer' component name.
  Please declare an 'inner' component with the same name in the top scope.
  Continuing flattening by only considering the 'outer' component declaration.
[C:/Users/Administrator/Projects/Proj/simulator/ModPowerSystems/Base/Interfaces/ComplexPhasor/ThreePhase/OnePortGrounded.mo:7:5-7:45:writable] Warning: No corresponding 'inner' declaration found for component .ModPowerSystems.Base.System DependentModel1.system declared as 'outer '.
  The existing 'inner' components are:
    There are no 'inner' components defined in the model in any of the parent scopes of 'outer' component's scope: ModPowerSystems.CustomModel.Loads.DependentModel1.
  Check if you have not misspelled the 'outer' component name.
  Please declare an 'inner' component with the same name in the top scope.
  Continuing flattening by only considering the 'outer' component declaration.
[C:/Users/Administrator/Projects/Proj/simulator/ModPowerSystems/CustomModel/Examples/BasicGrids/MainModel.mo:12:3-12:32:writable] Error: Identifier time is reserved for the built-in element with the same name.
Error: Error occurred while flattening model ModPowerSystems.CustomModel.Examples.BasicGrids.MainModel

not sure what happened here but the "cannot instantiate PACKAGE" error is gone. and what does error occurred while flattening model means?


Update 2:

The last error was due to usage of "time" named component which is not allowed in modelica as pointed out by @adrian-pop

CodePudding user response:

It seems you have a component named "time" in the MainModel.mo at line 12?


Older answer below (the question changed).

There are several issues. If you load package.mo the entire library will be loaded so you don't need to give it once again in the list of libraries. Also, if you give library ModPowerSystems in the list of libraries you should not give any other internal packages ModPowerSystems.X.Y for example in the list.

The code should look like:

mod=ModelicaSystem(str(source_dir) "/simulator/ModPowerSystems/package.mo",
                    "ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel",
                    ["Modelica"])

Another issue is "Error: Cannot instantiate ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel due to class specialization PACKAGE". Which means that the class you are trying to simulate: ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel is not a model, is a package which cannot be simulated. Maybe there are models inside that package that you need to select.

There are other issues like missing names in the package.order files in the library but those are just warnings and let's ignore them for now.

  • Related