- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Error using while
Error using while
Error using while
Hello everyone,
I am learning how to use loops in Modelica. My understanding is that I cannot apply loops directly in a function but I need an algorithm section. In particular, I am trying to implement a while loop in the following Pendulum Model. By the way, I am not able to understand how to remove this error:
Parse error: Modelica assignment statements are either on the form 'component_reference := expression' or '( output_expression_list ) := function_call'
Thank you so much in advance! Gabriele
model Pendulum
parameter Real m = 1, g = 9.81, L = 0.5;
Real F;
Real x(start = 0.5, fixed = true), y(start = 0, fixed = true);
Real vx, vy;
equation
m * der(vx) = -x / L * F;
m * der(vy) = (-y / L * F) - m * g;
der(x) = vx;
der(y) = vy;
algorithm
while F<20 loop
x ^ 2 + y ^ 2 := L ^ 2;
end while;
end Pendulum;
Re: Error using while
You cannot have an expression on the left hand side in algorithm sections! Only:
Code:
// component reference on the left hand side is fine
a := some_expression;
// this is not allowed by the grammar:
a + b := c:
In you case in while you have:
Code:
x ^ 2 + y ^ 2 := L ^ 2;
which is not allowed, you can write that as:
Code:
x := sqrt(L ^ 2 - y ^ 2);
- adrpo
- 885 Posts
Re: Error using while
You can use any algorithm section in a function with no issues, including loops.
- adrpo
- 885 Posts
Re: Error using while
Hello Adrpo,
Thank you so much for your help.
Now, the model looks like the one attached below.
By the way, I still get the following error message:
[1] 11:11:43 Translation Error
Internal error BackendDAE.adjacencyRowEnhanced failed for eqn:
algorithm
while F < 20.0 loop
x := sqrt(L ^ 2.0 - y ^ 2.0);
vx := (-y) * vy / sqrt(L ^ 2.0 - y ^ 2.0);
end while;
[2] 11:11:43 Translation Error
Internal error BackendDAEUtil.getAdjacencyMatrixEnhancedScalar failed
[3] 11:11:43 Translation Error
Internal error - IndexReduction.dynamicStateSelectionWork failed!
I think my problem is due to the fact that the model is formed by DAEs.
If I include this in a function, am I still able to simulate it? Can I insert DAEs inside a function?
I am new in OpenModelica and I am sure my questions may seem silly so thank you so much for your help!
Gabry
model Pendulum
parameter Real m = 1, g = 9.81, L = 0.5;
Real F;
Real x(start = 0.5, fixed = true), y(start = 0, fixed = true);
Real vx, vy;
equation
m * der(vx) = -x / L * F;
m * der(vy) = (-y / L * F) - m * g;
der(x) = vx;
der(y) = vy;
algorithm
while F<20 loop
x := sqrt(L ^ 2 - y ^ 2);
end while;
end Pendulum;
Re: Error using while
Hi Ardpo,
basically what I want to implement in my code is the following logic:
when a variable reaches a determined value, then stop the simulation.
In the case in the example is when F=20 then stop the simulation.
So, I thought to model this concept using a while loop. While the F is below 20, then calculate x.
This is the way I model the problem in Matlab.
By the way, maybe in Modelica there is another way to do this without using a loop. Do you know if there is another way?
Thank you so much!
Gabry
Re: Error using while
Hi Adrpo,
First of all, sorry for all the messages I sent!
In the meantime, I made some progress and I managed to solve my issue using as follows:
equation
m * der(vx) = -x / L * F;
m * der(vy) = (-y / L * F) - m * g;
der(x) = vx;
der(y) = vy;
x ^ 2 + y ^ 2 = L ^ 2;
algorithm
when F>20 then
terminate("end");
end when;
Using this algorithm inside my model I am able to stop the simulation when the condition is reached.
I just have one last question: Do you know if there is a way to calculate the time (wall-clock time) of the simulation? I need something similar to the function tic/toc in Matlab. By the way, something more general could be useful too. In fact, I need to know the tot time of the simulation starting from the first line of the model to the last one (I don't need the time of a particular part of the code).
Thank you so much again, Gabry
- adrpo
- 885 Posts
Re: Error using while
Gabri wrote:
basically what I want to implement in my code is the following logic:
when a variable reaches a determined value, then stop the simulation.
In the case in the example is when F=20 then stop the simulation.
Code:
model M
Real F = ...;
equation
when F < 20 then
terminate("F successfully reached <20");
end when;
end M;
https://build.openmodelica.org/Documentation/ModelicaReference.Operators.%27terminate()%27.html
- sjoelund.se
- 1700 Posts
Re: Error using while
Hi Adrpo,
Thanks for the link. With this, now I am able to measure the simulation time but I am not able to measure the time required for the initialization of the variables in my DAE system.
Do you know if there is a way to do this?
Thank you so much!
Gabry
Re: Error using while
There is a simulation flag (https://openmodelica.org/doc/OpenModeli … lags.html) for that:
Code:
$ ./M -lv LOG_STATS
LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
LOG_STATS | info | ### STATISTICS ###
| | | | | timer
| | | | | | 0.000294868s reading init.xml
| | | | | | 4.1369e-05s reading info.xml
| | | | | | 0.000147484s [ 3.6%] pre-initialization
| | | | | | 7.5327e-05s [ 1.8%] initialization
| | | | | | 5.107e-06s [ 0.1%] steps
| | | | | | 2.627e-05s [ 0.6%] solver (excl. callbacks)
| | | | | | 0.000536842s [ 13.1%] creating output-file
| | | | | | 0s [ 0.0%] event-handling
| | | | | | 0.000217657s [ 5.3%] overhead
| | | | | | 0.00303205s [ 73.8%] simulation
| | | | | | 0.00417501s [100.0%] total
| | | | | events
| | | | | | 0 state events
| | | | | | 0 time events
| | | | | solver: euler
| | | | | | 500 steps taken
| | | | | | 500 calls of functionODE
| | | | | | 0 evaluations of jacobian
| | | | | | 0 error test failures
| | | | | | 0 convergence test failures
| | | | | | 0s time of jacobian evaluation
LOG_SUCCESS | info | The simulation finished successfully.
- sjoelund.se
- 1700 Posts
Re: Error using while
Thank you so much sjoelund.se,
I have already tried to use this flag but the time I get back is a way smaller than the real one (clock-wall).
In fact, using an external timer I found that my total time (starting when I press the "simulate" button to the end of the simulation) is, more or less, 4 mins and the time I obtain bt the function is 5.86 seconds.
Do you know why I have this huge difference?
Thank you again, Gabry
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Error using while