1

我正在使用Dimensions modelfor 放置我在许多不同模型中使用的系统参数并通过 using 调用它们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中运行它,它就不起作用。

- 也许我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}
4

1 回答 1

1

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

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;
>> 
于 2020-01-15T14:00:46.267 回答