通过 OMShell 和 OMPython 调用模型时出错 / 运行 - OpenModelica

Error calling / running Model via OMShell & OMPython - OpenModelica

我使用 Dimensions model 来放置我在许多不同模型中使用的系统参数,并使用 extend 调用它们,而不是为每个模型再次声明它们。这是一个简单的例子,但实际上,我有更多的方法。

我拥有的结构的简单模型:

package Main

  model Dimensions
    final parameter Modelica.SIunits.Length x = 10;
    final parameter Modelica.SIunits.Length y = 5;
  end Dimensions;

  package Test_env
    extends Main.Dimensions;

    model Test_model
      Real z;
    equation
      z = x + y;
    end Test_model;

  end Test_env;
end Main;

如果我在 OMEdit 中 运行 这个例子,它可以正常工作。但是,如果我在 OMShellOMPython / OMCSessionZMQ 中 运行 它不起作用。

Q - 也许我错误地使用了 extends 子句?如果是这样,除了声明一次参数并在其他模型中重用它们之外,还有什么替代方法?

这是我在 OMShell:

中得到的
>> loadFile("D:/1.Modelica/Simulations/Main.mo")
true

>> getClassNames()
{Main}

>> getClassNames(Main)
{Dimensions,Test_env}

>> getClassNames(Main.Test_env)
{Test_model}

>> simulate(Main.Test_env.Test_model, startTime=0, stopTime=1, numberOfIntervals=500, tolerance=1e-4, method="dassl", outputFormat="mat"); getErrorString()
record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 0.0001, method = 'dassl', fileNamePrefix = 'Main.Test_env.Test_model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "Failed to build model: Main.Test_env.Test_model",
    timeFrontend = 0.0110966,
    timeBackend = 0.0,
    timeSimCode = 0.0,
    timeTemplates = 0.0,
    timeCompile = 0.0,
    timeSimulation = 0.0,
    timeTotal = 0.0111225
end SimulationResult;
"[D:/1.Modelica/Simulations/Main.mo:3:5-3:45:writable] Error: Class Modelica.SIunits.Length not found in scope Main.Dimensions.
[D:/1.Modelica/Simulations/Main.mo:1:1-18:9:writable] Error: Class Test_env.Test_model not found in scope Main.
Error: Class Main.Test_env.Test_model not found in scope .
Error: Error occurred while flattening model Main.Test_env.Test_model
"

这是来自 OMPython / OMCSessionZMQ:

omc.sendExpression('simulate(Main.Test_env.Test_model, stopTime=1.0)')
---------------------------------------------------------------------------
{'resultFile': '',
 'simulationOptions': "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'Main.Test_env.Test_model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
 'messages': 'Failed to build model: Main.Test_env.Test_model',
 'timeFrontend': 0.0018766,
 'timeBackend': 0.0,
 'timeSimCode': 0.0,
 'timeTemplates': 0.0,
 'timeCompile': 0.0,
 'timeSimulation': 0.0,
 'timeTotal': 0.0018919}

总结通过评论给出的答案:

在您的模型中使用 extends 将被扩展。

 package Test_env
    model Test_model
      Real z;
      extends Main.Dimensions;
    equation
      z = x + y;
    end Test_model;
  end Test_env;

如果您的所有模型都需要相同的固定参数,那么将扩展添加到每个模型仍然是一个好习惯,这样每个人都知道变量的来源。

同时与 Modelica.Constants 进行比较以了解 Modelica 标准库如何定义常量。我用它来创建完整的示例:

package Main

  model Dimensions
    final constant Modelica.SIunits.Length x = 10;
    final constant Modelica.SIunits.Length y = 5;
  end Dimensions;

  package Test_env
    import Dim = Main.Dimensions;

    model Test_model
      Real z;
    equation
      z = Dim.x + Dim.y;
    end Test_model;

  end Test_env;
end Main;

如果您使用不同包中的东西(此处 Modelica.SIunits.Length),您需要加载该包。这就是你的错误

Error: Class Modelica.SIunits.Length not found in scope Main.Dimensions.

在 OpenModelica 中,Modelica 已在启动时加载,因此在 OMShell 中使用 loadModel(Modelica)loadFile(...)

>> loadModel(Modelica)
true

>> loadFile("Path/To/Main.mo")
true

>> simulate(Main.Test_env.Test_model, startTime=0, stopTime=1, numberOfIntervals=500, tolerance=1e-4, method="dassl", outputFormat="mat")
record SimulationResult
    resultFile = "C:/Users/USERNAME/AppData/Local/Temp/OpenModelica/Main.Test_env.Test_model_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 0.0001, method = 'dassl', fileNamePrefix = 'Main.Test_env.Test_model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
LOG_SUCCESS       | info    | The simulation finished successfully.
",
    timeFrontend = 0.3193980510936645,
    timeBackend = 0.00467019998960375,
    timeSimCode = 0.001078686094233897,
    timeTemplates = 0.02625684206983937,
    timeCompile = 9.15578961474681,
    timeSimulation = 0.2440117147112652,
    timeTotal = 9.751522705140404
end SimulationResult;
>>