Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Stochastic events example from Dr. Modelica does not run

Stochastic events example from Dr. Modelica does not run

Hi,
The OpenModelica version is 1.6.0.
I was copying code from Dr. Modelica, section 13.2 "Models for Event Based Stochastic Processes ". The codes are:

Code:


package Random
import Modelica.Math;
constant Real NV_MAGICCONST=4*exp(-0.5)/sqrt(2.0);
type Seed = Integer[3];

function random "input random number generator with external storage of the seed"
  input Seed si "input random seed";
  output Real x "uniform random variate between 0 and 1";
  output Seed so "output random seed";
algorithm
  so[1] := rem((171 * si[1]),30269);
  so[2] := rem((172 * si[2]),30307);
  so[3] := rem((170 * si[3]),30323);
  // zero is a poor Seed, therfore substitute 1;
  if so[1] == 0 then
    so[1] := 1;
  end if;
  if so[2] == 0 then
    so[2] := 1;
  end if;
  if so[3] == 0 then
    so[3] := 1;
  end if;
  x := rem((so[1]/30269.0 +so[2]/30307.0 + so[3]/30323.0),1.0);
end random;

function normalvariate "normally distributed random variable"
  input Real mu "mean value";
  input Real sigma "standard deviation";
  input Seed si "input random seed";
  output Real x "gaussian random variate";
  output Seed so "output random seed";
protected
  Seed s1, s2;
  Real z, zz, u1, u2;
algorithm
  s1 := si;
  u2 := 1;
  while true loop
    (u1,s2) := random(s1);
    (u2,s1) := random(s2);
    z := NV_MAGICCONST*(u1-0.5)/u2;
    zz := z*z/4.0;
    if zz <= (- Math.log(u2)) then
      break;
    end if;
  end while;
  x := mu + z*sigma;
  so := s1;
end normalvariate;

connector discreteConnector
   discrete Boolean dcon;
end discreteConnector;

end Random;

and

Code:

model CustomerGeneration

  Random.discreteConnector dOutput;
  parameter Real mean = 0;
  parameter Real stDeviation = 1;

  discrete Real normalDelta;
  discrete Real nextCustomerArrivalTime(start=0);
  discrete Random.Seed randomSeed(start={23,87,187});

equation
  when pre(nextCustomerArrivalTime)<=time then
    (normalDelta,randomSeed)=Random.normalvariate(mean,stDeviation,pre(randomSeed));
     nextCustomerArrivalTime = pre(nextCustomerArrivalTime) + abs(normalDelta);
   end when;

   dOutput.dcon=if nextCustomerArrivalTime<>pre(nextCustomerArrivalTime) then true
   else  false;
end CustomerGeneration;

Then I use the simulate command:

Code:


simulate( CustomerGeneration, stopTime=10 )

And there is an error:

Code:

record SimulationResult

    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'CustomerGeneration', storeInTemp = false, noClean = false, options = '', outputFormat = 'plt'",
    messages = "Simulation failed for model: CustomerGeneration
[<interactive>:44:5-46:11:writable] Error: Class Math.log not found in scope Random.normalvariate (looking for a function or record).
Error: Error occured while flattening model CustomerGeneration
",
    timeFrontend = 0.0,
    timeBackend = 0.0,
    timeSimCode = 0.0,
    timeTemplates = 0.0,
    timeCompile = 0.0,
    timeSimulation = 0.0,
    timeTotal = 0.0
end SimulationResult;

Re: Stochastic events example from Dr. Modelica does not run

You forgot to use the command

Code:

loadModel(Modelica)

Re: Stochastic events example from Dr. Modelica does not run

I have used the loadModel(Modelica) command, but got a new error:

Code:

simulate( CustomerGeneration, stopTime=10 )

record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'CustomerGeneration', storeInTemp = false, noClean = false, options = '', outputFormat = 'plt'",
    messages = "Simulation failed for model: CustomerGeneration
Warning: In component <NO COMPONENT>, in relation nextCustomerArrivalTime <> pre(nextCustomerArrivalTime),  <>  on Reals is only allowed inside functions.
",
    timeFrontend = 0.0,
    timeBackend = 0.0,
    timeSimCode = 0.0,
    timeTemplates = 0.0,
    timeCompile = 0.0,
    timeSimulation = 0.0,
    timeTotal = 0.0
end SimulationResult;

There are 0 guests and 0 other users also viewing this topic
You are here: