问题
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.
回答1:
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;
回答2:
The example Modelica.Math.Random.Examples.GenerateRandomNumbers
uses relative class paths.
Utilities.initializeImpureRandom
for example points to Modelica.Math.Random.Utilities.initializeImpureRandom
, which works due to the package hierarchy
Modelica
|- Math
|- Random
|- Examples
|- Utilities
If you copy the code of the example to a different location, the relative paths will not work anymore.
Dymola updates relative paths when models are duplicated (via New > Duplicate Class). Openmodelica apparently not.
Just add the following two imports to the top of your code and the class paths will work:
import Modelica.Math.Random.Generators;
import Modelica.Math.Random.Utilities;
But your model contains additonal errors:
- The declaration of the variables
localSeed
andstate1024
is missing. Just copy them from the original example der(der(theta2))
is not supported. Create an intermediate variableder_theta2 = der(theta2)
来源:https://stackoverflow.com/questions/57878422/getting-the-translation-error-class-not-found-when-tryin-gto-generate-random-var