I'm new on Dymola and I can't manage to get this code running. I'm trying to create a random generation to simulate the number of people in a room.
Here is the code I have so far. Do you have any idea what could be the problem ?
model Trial
Real p(start=rand()/32767);
Real E(start=1);
equation
when (sample(1,3600)) then//sample(start+interval) ce qui donne "start + i*interval"
p = rand()/32767;
if E == 1 then
if p < 0.2 then
E = 2;
elseif p < 0.6 then
E = 3;
else
E = 4;
end if;
elseif E == 2 then
if p < 0.2 then
E = 1;
elseif p < 0.6 then
E = 3;
else
E = 4;
end if;
elseif E == 3 then
if p < 0.2 then
E = 1;
elseif p < 0.6 then
E = 2;
else
E = 4;
end if;
elseif E == 4 then
if p < 0.2 then
E = 1;
elseif p < 0.6 then
E = 2;
else
E = 3;
end if;
end if;
end when;
annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
coordinateSystem(preserveAspectRatio=false)),
experiment(StopTime=86400, __Dymola_Algorithm="Dassl"));
end Trial;
I would suggest you have a look at Modelica.Math.Random.Examples.GenerateRandomNumbers
. Based on this example, I ended up with:
model Trial
parameter Integer id=Modelica.Math.Random.Utilities.initializeImpureRandom(globalSeed) "A unique number used to sort equations correctly";
parameter Integer globalSeed=30020 "Global seed to initialize random number generator";
Real p(start=Modelica.Math.Random.Utilities.impureRandom(id=id));
Integer E(start=1, fixed=true);
equation
when (sample(1, 3600)) then
//sample(start+interval) ce qui donne "start + i*interval"
p = Modelica.Math.Random.Utilities.impureRandom(id=id);
if pre(E) == 1 then
if p < 0.2 then
E = 2;
elseif p < 0.6 then
E = 3;
else
E = 4;
end if;
elseif pre(E) == 2 then
if p < 0.2 then
E = 1;
elseif p < 0.6 then
E = 3;
else
E = 4;
end if;
elseif pre(E) == 3 then
if p < 0.2 then
E = 1;
elseif p < 0.6 then
E = 2;
else
E = 4;
end if;
else
// if E == 4
if p < 0.2 then
E = 1;
elseif p < 0.6 then
E = 2;
else
E = 3;
end if;
end if;
end when;
annotation (
Icon(coordinateSystem(preserveAspectRatio=false)),
Diagram(coordinateSystem(preserveAspectRatio=false)),
experiment(StopTime=86400, __Dymola_Algorithm="Dassl"));
end Trial;
I also corrected:
E
: Real to Integer + fixed=true
,E
: pre(E) avoids an algebraic loop,else
.