Help - Search - Members - Calendar
Full Version: algebraic loops
Modelica Forum > Modelica > Modelica
Jawhar
Hi all,

I have a problem when simulating a model. I hope that you can help me as usual..

Let's take a look to this part of my code:

CODE


...
Boolean test(start=true);

equation
// test = true <-- i can't do this, error of singularity.
when change(transdin1.numbeventT) then
reinit( x, dout1.s);
test= false;
end when;
  
  if x <= 0.6 and [b]test[/b] then
    der(x)= 2*x;
  elseif (change(din1.numbevent) and x<= 0.6 and test) then
    der(x)= 2*x;
  else
    der(x)= 0;
  end if;



I would to initialize the variable test.. but i can do it in the equation part. I had done this in the algorithm part of my model but in simulating a have a message error like this:

QUOTE
The current version of Dymola cannot generate code for algebraic loops involving when equations or algorithms with when parts. You may be able to cut the loop by putting 'pre' around some of the references to unknown continuous time variables in when parts or when conditions.


So my problem is that i want to initialise my variable test by true.. For one cycle of simulation, the simulation is ok, but for the second cycle the simulation is wrong because the variable test is false since it changes from true to false..


I am using Dymola version 6.0d.

Thank you

Regards,
Jawhar
Carsten
Hello Jawhar,

for initialisation you have different options.

You can do it in the declaration part by using a modifier:

QUELLTEXT
Boolean test(start=false);


(In Modelica the variables are represented as an record. start is one entry as well as e.g. unit.)

Another possibility is the inital equation block. The equations following that keyword are only valid during initialisation:

QUELLTEXT
initial equation
  test = false;

equation
  test = ...; // something else

This both are the common ways for initialisation. There still other possibilities.

Hope I met your problem.

regards
Carsten

Oh sorry, I see your problem,

you already have a modifier in the declareation part. (start=true). Change the one to false and remove the modification in the equation part, then it should work.

regards
Carsten
Jawhar
Hello Carsten,

I think that you misunderstood my question..

I start with start=true in the declaration, after the when condition i turn the variable test to false.. so when test=false i will resolve the equation der(x)=0.. but after a while my model will be activated again, and the value of the variable test is false but it would be true so i can resolve the other parts of the equation.

Normally if i do this in the algorithm part, it will work but i have only one problem.. the problem is that dymola give me a message error: Algebriac loop cannot be resolved in the current version.. In the algorithm part the prob is here: change(transdin1.numbeventT) So i had tried to do this in the equation part, but i can't initialize my variable test to true.. i hope that my question is clear.

CODE


algorithm

test = true <-- this is good in algorithm part, not for the equation part..
when change(transdin1.numbeventT) then

test= false;
end when;



I had used initial equation, but it doesn't solve my problem..

Thank you,
Best regards,
Jawhar
Carsten
Hello Jawhar,

well, I missunderstood your problem. Maybe we go more in detail and look for the problem. An algebraic loop for the type boolean will be present if a event changes the value of a boolean variable (e.g. test to true) and as a result of the new value of the boolean variable (test==true) another equation changes the value back (test to false). Short example:

QUELLTEXT
model Unnamed
  Boolean test(start=true);
  Boolean p;
equation
  if test then
    p=false;
  else
    p=true;
  end if;
  
  when p==false then
    test = true;
  end when;
end Unnamed;



As there is no order in the equation part, the system has no solution for one moment. Algebraic loops can broken by including delays in different ways. But usually they happen caused by a small model error. Maybe you attach more parts of the model, which show the problem (are there more equations, where x and test are involved?).

best regards
Carsten
Jawhar
Thank you carsten

I will explain more my problem and i will paste some code.

I have 2 models wich interacts together.. these models are connected via connectors. when an event happens in model2, the model1 had to stop resolving the equation, so he resolves only der(x)=0 (x will be constant). So i had used this:

CODE

when change(transdin1.numbeventT) then //here i detect the event from connector..
reinit( x, dout1.s);// i reinitialize x..
test= false;// i put test = false so after i stop resolving der(x)=2*x and i resolve only der(x)=0..(x constant)
end when;


Here is the use of the variable test..

CODE

if x <= 0.6 and test then // if test true u can resolve equation
    der(x)= 2*x;
  elseif (change(din1.numbevent) and x<= 0.6 and test) then //if test true mean no event from model2 u can do what u want
    der(x)= 2*x;
  else // test is false, means event detected, x must be constant..
    der(x)= 0;
  end if;


Until here, no problem.. but after a while when model2 is activated.. he does some work and when the model1 is activated again, the variable test is false, so he can't resolve der(x)=2*x.

Hope that is more clear, and excuse my bad english.

Thank you.

Regards,
Jawhar.
Carsten
ok Jawhar,

well I hope I understand now better, what you want to do, but one question:

Is your problem the initialisation of test with false?

If yes, then try anther modifier in the declaration of test:

QUELLTEXT
Boolean test(start=false, fixed=true)


If fixed is false (default case) the start value is used only as guess value. If fixed is true, then the variable should definitely contain that value a start time. And you should check the value in the simulation. Go the Advanced Menu in the simulation tab (under the variable browser) and set time to zero. Then you can look whether test was false or true at start time.

If that was not your question, then maybe tell if I am right that you want your variable test to change at one event to true and after the next event to false?

Hope I come closer to your problem..

best regards
Carsten
Jawhar
Hello carsten,

Thank you

The solution isn't fixed=true..
Let's take a look in this example:

CODE


...

algorithm

  dout1.state :=false;
  
  when x>0.6 then
  dout1.state :=true;
  end when;

...


This is a part of my code which works withn't problem but another part of my code didn't work, it's almost the same:


CODE

algorithm

test = true;

when change(transdin1.numbeventT) then

test= false;
end when;


The error message is:
QUOTE
The current version of Dymola cannot generate code for algebraic loops involving when equations or algorithms with when parts. You may be able to cut the loop by putting 'pre' around some of the references to unknown continuous time variables in when parts or when conditions.


Hope that you can give me an explication to this error message, or an idea to transform the second part of my code.

Best regards,
Jawhar
wagner
Hello Jawhar,

can you provide a test ready package for your example? It would be much easier for us to understand and reproduce your error. Just remove all the unnecessary parts of your models and provide the essentials as an attachment to your post.

Regards

Florian
Jawhar
QUOTE(wagner @ Feb 26 2007, 01:40 PM) *

Hello Jawhar,

can you provide a test ready package for your example? It would be much easier for us to understand and reproduce your error. Just remove all the unnecessary parts of your models and provide the essentials as an attachment to your post.

Regards

Florian


Hello Florian.. This is my code in one package.. it is a little long and complicated. Take a look to the model "state1".. my problem is in the algorithm part.. Hope that u can help me as usual. If u have any other question about my code, u can contact me.

Thank u a lot Florian and Carsten.

Best regards,
Jawhar
wagner
Hello Jawhar,

I tried to understand your model, but it is to complicated for me without having more information of the hybrid automaton you want to implement. Can you provide a scan or a drawing of the automaton? Maybe you need to modify your model structure to get a running model.

As far as I understood you have:

two states state1 and state2
two transitions T2 and T3
(in the example T1 one has not post state, you should remove it).

State differential equations
state1: der(x)=2*x
state2: der(x)=0.3*x

State transitions
state1 -> T2 -> state2
state2 -> T3 -> state1

I could not reconstruct the transition conditions. Can you provide them in text form?

Regards

Florian



Regards

Florian
Jawhar
Hello Florian

This is my work research and it's embarrassing for me.. because i didn't publish it yet sad.gif (i have a paper about the theorical part without an example accepted recently in a IEEE congres)

I have done the theorical part and it's ok..so i haven't to redo it again..
Transition1 is just for test only..

So when i am in Transistion2 and i have a condition verified, i count an event and i connect it to state1.. when change(state1) is true so the state1 had only to resolve the equation der(x)=0 . so in state1 i have done this:

CODE

algorithm

test = true;

when change(transdin1.numbeventT) then // here i detect the event coming from Transition2, but it didn't work i have an error message about algebraic loops with dymola.

test= false;
end when;


So i had tried to put this part of code in the equation part, it works in one cyle (from state1 --> Transition2-->state2---> Transition3---> state1) when simulating the state1 for the second time, the variable test is already false. Normally if the algorithm part works, when simulating the model state1 for the second cycle the variable test will be equal to true.

I hope that is more clear, if not you can contact me. When i finish the whole example i will publich it here.

Thank you very much.

Best regards,
Hassen
Jawhar
I have found the problem.. I have done a mistake in the dynamic part.. so in another way, it's more simple and i haven't to use the part of code which makes problem for me..

Thank you Florian and Carsten..

Best regards,
jawhar
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.