【问题标题】:OMPython using custom model with external packageOMPython 使用带有外部包的自定义模型
【发布时间】:2021-09-22 08:04:20
【问题描述】:

我正在尝试使用外部包 ModPowerSystems 模拟自定义模型,并使用 OMPython 进行一些自动化。

我的代码是:

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()

但是当我运行它时,我得到了:

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>.

我是 openmodelica 的超级新手,所以我不确定这里缺少什么。 有什么建议吗?


更新 1:

我正在关注@adrian-pop 的建议,但遇到了这个新错误:

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

不确定这里发生了什么,但“无法实例化 PACKAGE”错误消失了。扁平化模型时发生的错误是什么意思?


更新 2:

最后一个错误是由于使用了@adrian-pop指出的modelica中不允许的“时间”命名组件

【问题讨论】:

  • 我猜你的模型中有一个名为 time 的组件(或类)。这是不允许的。
  • 谢谢指出,错误消失了,mod.buildModel()没问题!

标签: python-3.x modelica openmodelica


【解决方案1】:

在第 12 行的 MainModel.mo 中似乎有一个名为“time”的组件?


下面的旧答案(问题已更改)。

有几个问题。如果您加载 package.mo 整个库将被加载,因此您无需在库列表中再次提供它。此外,如果您在库列表中提供库 ModPowerSystems,则不应在列表中提供任何其他内部包 ModPowerSystems.X.Y。

代码应如下所示:

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

另一个问题是"Error: Cannot instantiate ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel due to class specialization PACKAGE"。这意味着您要模拟的类:ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel 不是模型,是无法模拟的包。也许您需要选择该包中的模型。

还有其他问题,例如库中的 package.order 文件中缺少名称,但这些只是警告,让我们暂时忽略它们。

【讨论】:

  • 我听从了您的建议并弹出了新的错误(问题已更新)。还有关于 "Error: Cannot instantiate ModPowerSystems.CustomModule.Examples.BasicGrids.MainModel due to class specialization PACKAGE" 错误,我很确定在 OMEdit 上打开它时它是一个模型,而不是一个包。但是,当我遵循您的建议时,此错误消失了
  • 现在 OMPython 在我的模型上运行良好。谢谢你的回答:)
猜你喜欢
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 2010-10-30
  • 2019-12-06
  • 2020-04-24
  • 1970-01-01
相关资源
最近更新 更多