尝试生成随机变量时未找到翻译错误 Class

getting the Translation Error Class not found when tryin gto generate random variable

我正在尝试按照 this example 生成时间的随机函数:

model testData

  extends Modelica.Icons.Example;

  parameter Real k = 1.0;
  Real theta1;
  Real theta2;
  parameter Real tau = 1.0;
  
  parameter Modelica.SIunits.Period samplePeriod = 0.05;
  parameter Integer globalSeed = 30020;
  output Real omega1;
  
algorithm
  when initial() then
    state1024 := Generators.Xorshift1024star.initialState(localSeed, globalSeed);
    omega1     := 0;
  elsewhen sample(0,samplePeriod) then
    (omega1,state1024) := Generators.Xorshift1024star.random(pre(state1024));
  end when;
  
public
  parameter Integer id = Utilities.initializeImpureRandom(globalSeed);
  discrete Real rImpure;
  Integer iImpure;
algorithm
  when initial() then
    rImpure := 0;
    iImpure := 0;
  elsewhen sample(0,samplePeriod) then
    rImpure := Utilities.impureRandom(id=id);
    iImpure := Utilities.impureRandomInteger(
          id=id,
          imin=-1234,
          imax=2345);
  end when;

initial equation
  theta1 = 0;
  theta2 = 0;
  der(theta2) = 0;

equation
  der(theta1) = omega1;
  der(der(theta2)) = tau + k * (theta1 - theta2);

annotation(experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 0.02));

end testData;

但是,我收到错误消息:

Translation Error

Class Utilities.initializeImpureRandom not found in scope testData (looking for a function or record).

Translation Error

Error occurred while flattening model testData

如果您能帮助我了解问题所在以及如何解决,我将不胜感激。

您缺少一些导入,见下文,一些变量声明,并且您使用的 der(der(...)) 不起作用,您需要将内部 der 绑定到一个变量。下面这个模型编译模拟(不知道结果好不好)

model testData

  extends Modelica.Icons.Example;
  import Modelica.Math.Random.Generators;
  import Modelica.Math.Random.Utilities;

  parameter Real k = 1.0;
  Real theta1;
  Real theta2;
  Real der_theta2;
  parameter Real tau = 1.0;

  parameter Modelica.SIunits.Period samplePeriod = 0.05;
  parameter Integer globalSeed = 30020;
  parameter Integer localSeed = 614657;
  output Real omega1;
  discrete Integer state1024[33](each start=0, each fixed = true);

algorithm
  when initial() then
    state1024 := Generators.Xorshift1024star.initialState(localSeed, globalSeed);
    omega1     := 0;
  elsewhen sample(0,samplePeriod) then
    (omega1,state1024) := Generators.Xorshift1024star.random(pre(state1024));
  end when;

public
  parameter Integer id = Utilities.initializeImpureRandom(globalSeed);
  discrete Real rImpure;
  Integer iImpure;
algorithm
  when initial() then
    rImpure := 0;
    iImpure := 0;
  elsewhen sample(0,samplePeriod) then
    rImpure := Utilities.impureRandom(id=id);
    iImpure := Utilities.impureRandomInteger(
          id=id,
          imin=-1234,
          imax=2345);
  end when;

initial equation
  theta1 = 0;
  theta2 = 0;
  der(theta2) = 0;
  der_theta2 = 0;

equation
  der(theta1) = omega1;
  der(theta2) = der_theta2;
  der(der_theta2) = tau + k * (theta1 - theta2);

annotation(experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 0.02));

end testData;

示例 Modelica.Math.Random.Examples.GenerateRandomNumbers 使用相对 class 路径。

例如

Utilities.initializeImpureRandom 指向 Modelica.Math.Random.Utilities.initializeImpureRandom,由于包层次结构

Modelica
|- Math
   |- Random
      |- Examples
      |- Utilities

如果将示例代码复制到其他位置,相对路径将不再有效。

Dymola 在复制模型时更新相对路径(通过新建 > 复制 Class)。 Openmodelica 显然不是。

只需将以下两个导入添加到您的代码顶部,class 路径就会起作用:

import Modelica.Math.Random.Generators;
import Modelica.Math.Random.Utilities;

但是您的模型包含其他错误:

  • 缺少变量 localSeedstate1024 的声明。只需从原始示例中复制它们
  • 不支持
  • der(der(theta2))。创建一个中间变量der_theta2 = der(theta2)