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
  • Index
  • » Users
  • » milesma
  • » Profile

Posts

Posts

sjoelund.se wrote:

The ball will typically fall through the ground if the velocity does not bounce it high enough, so you need a condition to just let the ball remain to 0 velocity when the numbers get too small.

Thanks for your reply, from the book "Principles of Object-oriented modeling and simulation with modelica 3.3" second edition by Peter Fritzson, I understood the case you mentioned (Zero Effect). However, my problem is at the first bounce, it will falls into the ground (as if there is no ground). You can run the script and code to have a look.

I found "pre" is the cause of my problem. Remove it will just solve it. However, I don't know the side effect of removing "pre", there must be reason to have "pre" there?

Thanks
Miles

if I remove "pre" function in my failed "abstract" version, so it looks like:

Code:



when height <= radius then reinit(velocity, -c*velocity); end when;

then the problem solved, I can get the exact plot as the the following code:

Code:


model BouncingBall
    constant Real g = 9.81;
    parameter Real c = 0.9;
    parameter Real radius = 0.1;
    Real height(start=1);
    Real velocity(start=0);
equation
    der(height) = velocity;
    der(velocity) = -g;
    when height <= radius then reinit(velocity, -c*pre(velocity)); end when;
end BouncingBall;

Now the questions:
1. what exactly role is the "pre" function playing in my example? Is it necessary?
2. why the version with "pre" on the "abstract" version does not work?

Hi Guys,

I encountered an unpredictable behavior when running the simulation.

Let's start with the simple and successful example:

Code:


model BouncingBall
    constant Real g = 9.81;
    parameter Real c = 0.9;
    parameter Real radius = 0.1;
    Real height(start=1);
    Real velocity(start=0);
equation
    der(height) = velocity;
    der(velocity) = -g;
    when height <= radius then reinit(velocity, -c*pre(velocity)); end when;
end BouncingBall;

and following script:

Code:


loadFiles({"BouncingBall.mo"});
getErrorString();
simulate( BouncingBall,startTime=0,stopTime=5,outputFormat="plt"); getErrorString();
plot({height});
quit();

I can successfully get the plot I wanted.

However, if I use the same script but change the code to followings (all I did is to "abstract" the equation for re-use purpose), then the ball falls straightly into the ground, why is the code failed?

Code:


block derHeight
     Real height;
     Real velocity;
equation
    der(height)=velocity;
end derHeight;

block derVelocity
     Real velocity;
     Real g;
equation
    der(velocity)=-g;
end derVelocity;

block reinitVelocity
     Real height;
     Real radius;
     Real velocity;
     Real c;
equation
    when height <= radius then reinit(velocity, -c*pre(velocity)); end when;
end reinitVelocity;

model BouncingBall
    constant Real g = 9.81;
    parameter Real c = 0.9;
    parameter Real radius = 0.1;
    Real height(start=1);
    Real velocity(start=0);

    derHeight e1;
    derVelocity e2;
    reinitVelocity e3;   
equation
    velocity=e2.velocity;
    velocity=e1.velocity;
    velocity=e3.velocity;

    height = e1.height;
    height = e3.height;

    g = e2.g;
    radius=e3.radius;
    c = e3.c;
end BouncingBall;

Strange though, following code only abstract "e1" and it simulates successfully!

Code:


block derHeight
    Real height;
    Real velocity;
equation
    der(height)=velocity;
end derHeight;

model BouncingBall
    constant Real g = 9.81;
    parameter Real c = 0.9;
    parameter Real radius = 0.1;
    Real height(start=1);
    Real velocity(start=0);

    derHeight e1;
equation
    velocity=e1.velocity;
    height = e1.height;

    der(velocity)=-g;
    when height <= radius then reinit(velocity, -c*pre(velocity)); end when;
end BouncingBall;

Even more strange, following code only abstract "e2" but the simulation failed!

Code:


block derVelocity
    Real velocity;
    Real g;
equation
    der(velocity)=-g;
end derVelocity;

model BouncingBall
    constant Real g = 9.81;
    parameter Real c = 0.9;
    parameter Real radius = 0.1;
    Real height(start=1);
    Real velocity(start=0);

    derVelocity e2;
equation
    velocity=e2.velocity;
    g = e2.g;
    der(height)=velocity;
    when height <= radius then reinit(velocity, -c*pre(velocity)); end when;   
end BouncingBall;

Any helps? Thank you very much!
Miles

Can Modelica and OpenModelica solve Operational Research Problems like linearing programming (optimization) ?

Any documents and library describing these?

Thank you very much!

Miles

I spotted following line in OpenModelicaSystem.pdf

addEquation(A1<cref>,A2<expr>, A3<expr>)(??NotYetImplemented)
Adds the equation A2=A3 to the model named by A1.

I tried following script, but no luck. Any help will be appreciated.


class MyClass
    Real a;
end MyClass;

addEquation(MyClass, a, 10);

then I call list(); the equation was not added in.

I also tried

addEquation(MyClass, Code(a), Code(10));

still fail.

--------------------------------------

My question:
Is addEquation supported?
How can I add equation dynamically?

That's exactly what I wanted!
Now I can define Gamma as:

class Gamma
  Real c;
  Alpha alpha;
  Beta beta;
equation
  c = alpha.a + beta.b;
end Gamma;

Then use this script, it just works!!
loadFiles({"Alpha.mo", "Beta.mo", "Gamma.mo"});   

setComponentProperties(Gamma, alpha, {false,false,false,false,false},{"parameter"},{false,false},{""}); getErrorString();
setComponentProperties(Gamma, c, {false,false,false,false,false},{"parameter"},{false,false},{""}); getErrorString();

buildModel(Gamma);
instantiateModel(Gamma);
str_cmd := "Gamma.exe -override alpha.a=10,c=30,outputFormat=csv";
system(str_cmd); getErrorString();

-------------------------------------------------------

Bear with me, I have another question: how can I get a single value from the csv/plt file?
For example: In the above example, I've got following result in the .csv file (I believe it is for beta.b)

0,20
0.002,20
0.004,20
...
0.998,20
1,20
1,20

I know it is quite useful to plot with these information. But we all know the result is special: it is 30-10 = 20, it is always 20.

What kind of parameter can I set in the script to give me a single "20" in the file ?

Thanks Adrpo,

I never knew Modelica could set all to be parameter, good to know.

That way, if I understand correctly, is putting the equation as the default value. I could still get 30 with this, great.

However, If I use following command:
str_cmd := "Gamma.exe -override alpha.a=10,c=30,outputFormat=csv";

I will not be able to get beta.b=20 (by searching the .csv file, there is no "20").

----------------------------------

Actually, I wanted to define Gamma like this: (without any hint about input or output directions for variables)

class Gamma
  Real c;
  Alpha alpha;
  Beta beta;
equation
  c = alpha.a + beta.b;
end Gamma;

Then set it at script if possible at all:

loadFiles({"Alpha.mo", "Beta.mo", "Gamma.mo"});   
buildModel(Gamma, "alpha=parameter, beta=parameter, c=output");
....

----------------------------------
What's your suggestion?

Miles


Hi, I have a class defined as:

class Gamma
  Real c;
  parameter Alpha alpha;
  parameter Beta beta;
equation
  c = alpha.a + beta.b;
end Gamma;

it works with following command:

str_cmd := "Gamma.exe -override alpha.a=10,beta.b=20,outputFormat=csv";

However, if I want to get the value of beta by using c and alpha as parameters, I have to define another class?

For example, following is the command I wanted but it is not working (because c is not defined as parameter.)
str_cmd := "Gamma.exe -override c=30,alpha=10,outputFormat=csv";   // I wanted beta.b be computed as 20 (30 - 10).

-----------------------------
My question:

is it possible to define a class without specifying fixed "parameter" and "output" constraint keywords, but run some mos script by specifying the parameter or variable on the fly (before build the model)?




Thank you so much Adrian.

I used .plt file just because it is easy to read current/smile

I don't know how to open .mat file (open by Notepad++ showed only binaries). Any tool can open this file?

I found that you like to call "--locale=C", by reading the help, it simply said "Override the locale from the environment."

Then what are the list of valid locale values? Why is "C" special for you (or for us)?

Thanks again
Miles

Hi Adrian,

Definitely five start post current/smile

I have a question about this line:

results := results + str_a + "," + String(res[size(res, 1), size(res, 2)]) + ",\n";

I know "res" is a matrix, then I guess this line returning the "bottom right" number of the matrix, is that correct?

Code:


res[size(res, 1), size(res, 2)]

If so, is this a common way to get value of a variable regardless of time?
Or (ask in another way), in what scenario do we use this code?

Thanks again.

Thank you so much Adrian!

I just learned from this post that I could divide the "simulate" function into following commands:

Code:


loadFile("HelloWorld.mo");    getErrorString();
buildModel(K);     getErrorString();
str_cmd := "K.exe -override a=4,b=5,c=6,d=7";
system(str_cmd); getErrorString();

Then I can get "K_res.mat", that's great!

Could you help me a little bit more? What is the complete list of option settings on the built K.exe in this example?

For example, I want to specify generated file type be "plt", not "mat", how could I achieve this?
if I want to setup the "startTime" and "stopTime", how could? (I tried following command, it is not working)

Code:


str_cmd := "K.exe -override a=4,b=5,c=6,d=7 -startTime 0.0 -stopTime 10.0 -outputFormat plt";

I would assume all the parameters supported by "simulate" function should be supported by this way (calling .exe file with options), is that correct?

I solved it by defining

Code:


class K
  parameter Real a;
  parameter Real b;
  parameter Real c;
  parameter Real d;
  Real vK;
  Real vK1;
  Real vK2;
equation
  vK = vK1 * vK2;
  vK1 = a * b;
  vK2 = c * d;
end K;

class KClient
    K k(a=1,b=2,c=3,d=4);
end KClient;

then call by omc HelloWorld.mos
HelloWorld.mos file

Code:


loadFile("HelloWorld.mo");
simulate(KClient, numberOfIntervals=1, outputFormat="plt");
plot(k.vK)

in the result file, I have

Code:


DataSet: time
0, 0
1, 1
1, 1

DataSet: vK
0, 24
1, 24
1, 24

DataSet: vK1
0, 2
1, 2
1, 2

DataSet: vK2
0, 12
1, 12
1, 12

This is OK, but actually, all I wanted is the result "24". How can I configure the "simulate" command options to get a single result of "24".
may I use "val()" ?

Any help is highly appreciated.

Code:


//HelloWorld.mo

class K
  parameter Real a = 1;
  parameter Real b = 2;
  parameter Real c = 3;
  parameter Real d = 4; 
  Real vK;
  Real vK1;
  Real vK2;
equation
  vK = vK1 * vK2;
  vK1 = a * b;
  vK2 = c * d;
end K;

If I have following mos file :

Code:


HelloWorld.mos
loadFile("HelloWorld.mo");
simulate(K, numberOfIntervals=1, outputFormat="plt");
plot(vK)

and use following command to run,

Code:


omc HelloWorld.mos

I got 24, which is correct.

How do I change parameter to the model?
For example, I want my class be defined as:

Code:


//HelloWorld.mo

class K
  parameter Real;
  parameter Real;
  parameter Real;
  parameter Real; 
  Real vK;
  Real vK1;
  Real vK2;
equation
  vK = vK1 * vK2;
  vK1 = a * b;
  vK2 = c * d;
end K;

and using something like

Code:


HelloWorld.mos
loadFile("HelloWorld.mo");
simulate(K, a=1, b=2, c=3, d=4, numberOfIntervals=1, outputFormat="plt");
plot(vK)

to run, then I can change the argument without changing my model.

I know the "simulate" function is not accepting "a=1" way of setting, that's why I'm seeking help here.

How can I pass argument like "a=10,b=20,c=30,d=40" on the fly?

Or do I need to use "input variable" rather than parameter? If that's the case, how to do that?

Thanks
Miles

Aug-12-15 03:05:14
The First Basic Class (HelloWorld) in OMNotebook

Thank you very much!

Instead, it gave me result like

Code:


>> plot(x)
{"_omc_PlotResult", "C:/Users/xxx/AppData/Local/Temp/OpenModelica/Add_res.mat", "", "detailed", "plot", "false", "time", "", .....
}

I could sometimes be able to launch the Plot Window (when it simply gave "true" as dump message), but most of the time I can not launch the plot window (with the above dump message), why?

Thank you in advance.

Aug-09-15 23:32:12
The First Basic Class (HelloWorld) in OMNotebook

Hi Guys,

I'm learning OpenModelica and I've already got stuck in the first example.

In the First Basic Class (HelloWorld) in OMNoteBook,
A class is defined like this:

Code:


class HelloWorld
  Real x(start = 1,fixed=true);
  parameter Real a = 1;
equation
  der(x) = - a * x;
end HelloWorld;

Then plot it

Code:


plot( x )

I was surprised about the plotted graph (BTW: how to insert local pictures to this post?)
-------------------------------
Firstly, I was expecting a linear graph like "y = -x"; Obviously I was wrong, but I don't know why.
Then I was trying to understand what is "time derivative". But still can not understand why the plotted graph started from "0, 1" and decrease as time increases.
Could you explain these basic concept for me? I searched for a while, but can not get it.

  • Index
  • » Users
  • » milesma
  • » Profile
You are here: