0

I want to stop the simulation when my output reaches a certain limit or value irrespective of the simulation time. Let's take an example my output is 'prod' a variable which has the product of two numbers stored in it. If the product of the two numbers is greater than 12 then the simulation must stop irrespective of the other values left in the array.

is there any keyword which would do this in open modelica? I tried using the break and terminate command, but the output of prod after 3 seconds reached 12 and then the plot continued for 5 seconds(which was the simulation time) and the value stored in prod was zero. I want my simulation to stop and the product is 12 and it should not calculate the product after that and shouldn't display those values in the plot as well.

model ForLoopExample;
Real A[5] = {1,2,3,4,5};
Real B[5] = {2,3,4,5,6};
Real prod[5];
Boolean test_case(start = false);
algorithm 
for i in 1:size(A,1) loop 
    prod[i]:=A[i]*B[i];
    if prod[i]>=12 then 
    test_case = true;
    break;
    end if;
end for;
when test_case then 
    terminate("product above 12");
end when;
end ForLoopExample;
0

1 Answer 1

1

It is a bit tricky example you have since it involves understanding of Modelica and time and the difference between algorithm and equations. The code works as expected for me (if you just adjust code text_case := true). The algorithm does all computations at the very beginning of the simulation, and sort of does not "take time". You need to make the system time discrete and for each sample interval (say 1 second) you make one round of calculation.

I have made a crude translation of your code to a time discrete system. And I let index of A an B represent the time 1, 2,..5 seconds so we get the value of A and B for each samplePeriod.

This algorithm works and stops at time=3 when prod = 12 i.e greater or equal than 12. In general I would not like to have code that test on equality of two real numbers, but I let it stay as is.

Does this capture what you want?

model ForLoopExample
   parameter Real samplePeriod = 1;
   parameter Real A[5] = {1,2,3,4,5};
   parameter Real B[5] = {2,3,4,5,6};
   Real prod (start=0, fixed=true);
   Boolean test_case(start = false, fixed=true);
equation
   when sample(0, samplePeriod) then
      prod = A[integer(time)]*B[integer(time)];
      test_case = prod >= 12;
   end when;
   when test_case then
      terminate("product above 12");
   end when;
end ForLoopExample; 
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you it worked. I have another doubt, let's say I don't include time in my code. My product of two numbers do not have to be at 1 second, 2nd and so on. The simulation interval is left as the standard start and stop time in the simulation set up. Then how would I get my simulation to stop when my product is above 12? I do not want to see the graph of the parameter prod showing values after 12. Do I have to stick to doing it the same way as you have suggested?
Good. I guess I need to understand the "underlying problem" sort of to be relevant. The key is what simulation time means for the problem, for you? The A and B are they given in their totality at start as in in your example, or are they a time series so that here comes a pair (A, B) at regular intervals, or perhaps not that regular but still in pairs?
Well they are not a time series. They are just two arrays containing 5 numbers each and I wanted to find the product of the two arrays like A[1]*B[1],A[2]*B[2] and so on, is this what you mean by pairs? To learn modelica further I decided to see if the simulation can be stopped after my product of the two reaches a certain value.
Yes you understand what I mean with pairs right. And you want to use Modelica to just do the algorithmic part initially, and not really simulate any time, right?
Thank you, I will look into these suggestions.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.