- Index
- » Users
- » calnastic
- » Profile
Posts
Posts
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;
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;
Thank you, I'll try 1.6.0. Really appreciate your information!
Thanks! I was careless to include Rocket class.
I am using OpenModelica 1.5.0.
I'm copying the moon landing class from the ModelicaTutorialFritzson.pdf which comes with the program, page 25-26.
However, when I use the simulate function (also copied from the slides), an error occurred.
Please help.
This is the original code, I didn't modify anything:
Code:
>>class CelestialBody
constant Real g = 6.672e-11;
parameter Real radius;
parameter String name;
parameter Real mass;
end CelestialBody;
{CelestialBody}
>>class MoonLanding
parameter Real force1 = 36350;
parameter Real force2 = 1308;
protected
parameter Real thrustEndTime = 210;
parameter Real thrustDecreaseTime = 43.2;
public
Rocket apollo(name="apollo13");
CelestialBody moon(name="moon",mass=7.382e22,radius=1.738e6);
equation
apollo.thrust = if (time < thrustDecreaseTime) then force1
else if (time < thrustEndTime) then force2
else 0;
apollo.gravity=moon.g*moon.mass/(apollo.altitude+moon.radius)^2;
end MoonLanding;
{MoonLanding}
>>simulate(MoonLanding, stopTime=230)
record SimulationResult
resultFile = "Simulation failed.
[b]Error: Error occured while flattening model MoonLanding[/b]
"
end SimulationResult;
I'm having trouble with the plot window, it used to hold when I plot multiple graphs, but now it doesn't hold, only displaying the most recent graph. I have made sure the "Hold" button is active, as the screenshot shows.:
I'm using windows version 1.5.0, OS is windows 7.
I'm using the same variable to make different plots, by modifying the code slightly to show difference.
Please tell me what is going wrong.
Thanks for your reply, I'm confused by some general things.
after I have loaded Modelica and ModelicaAdditions, I tried to use this model:
Code:
model Place22 "Place with two input and two output transitions"
parameter Boolean initialState = false "Initial value of state";
Boolean state(final start = initialState) "State of place";
protected
Boolean newState(final start = initialState);
annotation(Coordsys(extent = [ -100, -100;100,100], grid = [2,2], component = [20,20]), Window(x = 0.21, y = 0.03, width = 0.52, height = 0.7), Icon(Ellipse(extent = [ -100, -100;100,100], style(color = 41, fillColor = 7)), Line(points = [ -100,60; -80,60], style(color = 41)), Line(points = [ -100, -60; -80, -60], style(color = 41)), Line(points = [80, -60;100, -60], style(color = 41)), Line(points = [82,60;102,60], style(color = 41)), Text(extent = [0,99;0,159], string = "%name")), Diagram(Ellipse(extent = [ -100, -100;100,100], style(color = 41)), Line(points = [ -100,60; -80,60], style(color = 41)), Line(points = [ -100, -60; -80, -60], style(color = 41)), Line(points = [80,60;100,60], style(color = 41)), Line(points = [80, -60;100, -60], style(color = 41))));
public
Interfaces.FirePortOut outTransition1 annotation(extent = [100, -70;120, -50]);
Interfaces.FirePortOut outTransition2 annotation(extent = [100,70;120,50]);
Interfaces.SetPortIn inTransition1 annotation(extent = [ -140, -80; -100, -40]);
Interfaces.SetPortIn inTransition2 annotation(extent = [ -140,80; -100,40]);
equation
state = pre(newState);
newState = inTransition1.set or inTransition2.set or state and not (outTransition1.fire or outTransition2.fire);
inTransition1.state = state;
inTransition2.state = inTransition1.state or inTransition1.set;
outTransition1.state = state;
outTransition2.state = outTransition1.state and not outTransition1.fire;
end Place22;
I simulated it using
Code:
simulate(Place22, startTime=0,stopTime=6.0);
Then when I tried to plot it, I don't see an "x" to plot. I was going to use something like
Code:
plot(x)
But after I have tried the variables declared in the model, none of them works.
Am I simulating a wrong model? If yes, could you offer a simple working model?
Thanks.
Hello,
To study ModelicaAdditions.PetriNets, I copied and used a segment of code in PetriNets.mo, the code is:
Code:
package Examples
extends Modelica.Icons.Library;
encapsulated model Parallel "Example to demonstrate parallel activities described by a petri net "
import Modelica.Icons;
import ModelicaAdditions.PetriNets;
extends Icons.Example;
output Boolean S1state;
output Boolean P1state;
output Boolean P2state;
output Boolean P3state;
output Boolean P4state;
output Boolean P5state;
output Boolean P6state;
PetriNets.Place01 S1(initialState = true) annotation(extent = [ -60,80; -40,100], rotation = -90);
PetriNets.Place11 P1 annotation(extent = [ -60, -10; -40,10], rotation = -90);
PetriNets.Parallel Par1(condLabel = "time>1") annotation(extent = [ -60,50; -40,70], rotation = -90);
PetriNets.Place12 P2 annotation(extent = [20,50;40,70], rotation = -90);
PetriNets.Transition T1(condLabel = "time>2") annotation(extent = [ -20,20;0,40], rotation = -90);
PetriNets.Transition T2(condLabel = "time>3") annotation(extent = [60,20;80,40], rotation = -90);
PetriNets.Place11 P3 annotation(extent = [ -20, -10;0,10], rotation = -90);
PetriNets.Transition T3(condLabel = "time>4") annotation(extent = [ -20, -36;0, -16], rotation = -90);
PetriNets.Place11 P4 annotation(extent = [60, -10;80,10], rotation = -90);
PetriNets.Transition T4(condLabel = "time>5") annotation(extent = [60, -36;80, -16], rotation = -90);
PetriNets.Place21 P5 annotation(extent = [20, -70;40, -50], rotation = -90);
PetriNets.Synchronize Sync1(condLabel = "time>6") annotation(extent = [ -60, -76; -40, -56], rotation = -90);
PetriNets.Place10 P6 annotation(extent = [ -60, -100; -40, -80], rotation = 270);
equation
connect(Par1.outTransition1,P1.inTransition) annotation(points = [ -50,55; -50,12], style(color = 41));
connect(T1.outTransition,P3.inTransition) annotation(points = [ -10,25; -10,12], style(color = 41));
connect(P3.outTransition,T3.inTransition) annotation(points = [ -10, -11; -10.05, -19.95], style(color = 41));
connect(P1.outTransition,Sync1.inTransition1) annotation(points = [ -50, -11; -50, -60], style(color = 41));
connect(Sync1.outTransition,P6.inTransition) annotation(points = [ -50, -71; -50, -78], style(color = 41));
connect(T2.outTransition,P4.inTransition) annotation(points = [70,25;70,12], style(color = 41));
connect(P4.outTransition,T4.inTransition) annotation(points = [70, -11;69.95, -19.95], style(color = 41));
connect(S1.outTransition,Par1.inTransition) annotation(points = [ -50.2,79; -50.05,66.05], style(color = 41));
connect(T3.outTransition,P5.inTransition1) annotation(points = [ -10, -31; -10, -37;24, -37;24, -48], style(color = 41));
connect(T4.outTransition,P5.inTransition2) annotation(points = [70, -31;70, -37;36, -37;36, -48], style(color = 41));
connect(P2.outTransition2,T2.inTransition) annotation(points = [36,48.9;36,45;69.95,45;69.95,36.05], style(color = 41));
connect(Par1.outTransition2,P2.inTransition) annotation(points = [ -44,55; -44,50; -25,50; -25,90;30,90;30,72], style(color = 41));
connect(P5.outTransition,Sync1.inTransition2) annotation(points = [30, -71;30, -90;0, -90;0, -51; -44, -51; -44, -60], style(color = 41));
connect(P2.outTransition1,T1.inTransition) annotation(points = [24,49;24,44; -10.05,44; -10.05,36.05], style(color = 41));
Par1.condition = time > 1;
T1.condition = time > 2;
T2.condition = time > 3;
T3.condition = time > 4;
T4.condition = time > 5;
Sync1.condition = time > 6;
S1state = S1.state;
P1state = P1.state;
P2state = P2.state;
P3state = P3.state;
P4state = P4.state;
P5state = P5.state;
P6state = P6.state;
end Parallel;
end Examples;
When I tried to simulate this Examples package, I got an error message:
simulate(Examples)
record SimulationResult
resultFile = "Simulation failed.
Error: Base class Modelica.Icons.Library not found in scope Examples
Error: Error occured while flattening model Examples
"
end SimulationResult;
Please tell me how to fix it. Your help is appreciated!
Thanks.
sjoelund.se wrote:
If you are using OMShell/OMEdit, do note that it will only display stdout from functions that are called during simulation.
Is there a way I can output all results in the loop to screen?
Hi,
I need a random number generator in OM, I found one online, the code is:
Code:
function random "Pseudo random number generator"
input Integer seedIn[3] "Seed from last call";
output Real x "Random number between 0 and 1";
output Integer seedOut[3] "Modified seed for next call";
algorithm
seedOut[1] := rem((171*seedIn[1]), 30269);
seedOut[2] := rem((172*seedIn[2]), 30307);
seedOut[3] := rem((170*seedIn[3]), 30323);
// Zero is a poor seed, therefore substitute 1;
for i in 1:3 loop
if seedOut[i] == 0 then
seedOut[i] := 1;
end if;
end for;
x := rem((seedOut[1]/30269.0 + seedOut[2]/30307.0 + seedOut[3]/30323.0), 1.0);
end random;
Then when I try to call the random function by using another function:
Code:
function testRandom1
input Integer seed[3];
output Real x;
algorithm
for i in 1:10 loop
(x, seed) := random(seed);
Modelica.Utilities.Streams.print("x = " + String(x,significantDigits=16) + "\n");
end for;
end testRandom1;
When I call testRandom1 function with parameters, like this:
testRandom1({23,35,75})
There is no output to the screen. But if I remove that "Modelica.Utilities.Streams.print" statement, I get one return value, but there is no loop then.
Please help. Thanks.
Thank you very much for the information. I'll try something else.
Hi,
I'm a new comer to OpenModelica, trying a few simple things with it.
I'm now thinking of importing a file containing a series of x-y data, plot in OM (scatter plot), and use best fit to find the linear equation. Microsoft Excel can do it but it doesn't have much scientific computing capabilities.
Please give some (specific) instructions about how to do the import, and best fit.
You help is really appreciated.
- Index
- » Users
- » calnastic
- » Profile