Search code examples
modelicaopenmodelica

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


I'm trying to follow this example to generate a random function of time:

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;

however, I get the error message:

Translation Error

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

Translation Error

Error occurred while flattening model testData

I would appreciate if you could help me understand what is the problem and how I can solve it.


Solution

  • You were missing some imports, see below, some variable declarations and you were using der(der(...)) which doesn't work, you need to bind the internal der to a variable. This model below compiles and simulates (I don't know if the results are fine or not).

    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;