Help - Search - Members - Calendar
Full Version: provide event signal
Modelica Forum > Modelica > Modelica
Carsten
I want to provide a signal for models which is true, when one component generates an event and false during the rest of the time. It is comparable with the component Modelica.Blocks.Sources.SampleTrigger. But the difference is, that the output is not generated by a time function but by a condition. The output should only be peaks.

The idea was something as:

QUELLTEXT
Real x=sin(time); // only to have an input signal as example
Boolean signx = if x>0 then true else false;
output Boolean event1; // this variable should have peak outputs carrying events;

equation

if edge(signx) then
  event1 = true;
else
  event1 = false;
end if;


I tried a lot of ways with when equations but I did not succeed. Can anyone help me?

Thanks a lot, kind regards
Carsten
wagner
Hello Carsten,

I guess you already had the solution in previous attempts but you didn't see the correct results. It's because of the display-routine of the Modelica Tools (I'm using Dymola). Signals that only change their value for one simulation step are drawn as if they were constant. That's why you need another component that makes the event visible. I'm using a counter which increases its value when an event occurs. You'll find my solution attached to this post.

Regards

Florian
Carsten
Hello Florian,

thanks a lot for your answer. With the counter it works! I tried it to detect it with introducing a integer variable, setting it to 2 in case of the event and to 1 in the contrary case and then applying the max operator. But I already see my mistake, that the max operator only outputs the maximum value in that moment and not during whole simulation time.

Thanks
Carsten
Jawhar
QUOTE(wagner @ Nov 14 2006, 03:56 PM) *

Hello Carsten,

I guess you already had the solution in previous attempts but you didn't see the correct results. It's because of the display-routine of the Modelica Tools (I'm using Dymola). Signals that only change their value for one simulation step are drawn as if they were constant. That's why you need another component that makes the event visible. I'm using a counter which increases its value when an event occurs. You'll find my solution attached to this post.

Regards

Florian


Hello Florian

So, if i have an event (using edge), i can't detect it for one simulation step because Dymola drawn it as if it is a constant? what is the display-routine of Dymola? I find that odd .. Any more explication?
I am simulating a model within Dymola.. i have a boolean variable state wich changes from true to false only one time. But the problem that edge(state) is always equal to 0 (false). If i use OpenModelica and compile the same model.. the variable edge(state) can change to 1 (true)?

My last question is: when we had to use edge() or change()...?!
Best rgards,
Jawhar
wagner
Hello Jawhar,

in the first moment it's a little bit confusing, but what Dymola does is correct. An event appears only at a single moment of time. How do you want to draw this correctly in a chart that shows an interval of time. The event is infinitely small. So you can't see it in the chart. But be relieved, it also took me quite a while to find this "error" in one of my models.

To visualize the difference between edge and change simulate the following model and draw signx, edgeCounter and changeCounter in one chart.

CODE

model EdgeChange
  
  Real x; // only to have an input signal as example
  
  Boolean signx; //event we want to detect
  
  output Integer edgeCounter(start=0); //visualise rising edges
  output Integer changeCounter(start=0); //visualise rising and falling edges
  
equation
  
  x=sin(time*100);
  signx = if x>0 then true else false;
    
algorithm
  when
      (edge(signx)) then
      edgeCounter:=edgeCounter + 1;
  end when;

  when(change(signx)) then
      changeCounter=changeCounter+1;
  end when;
  
end EdgeChange;


As you can see, edge only detects signal changes in positive direction (false to true). Change also detects signal changes in negative direction (false to true and true to false).


Regards

Florian
Jawhar
Thank you for your answer. I am a little confused.

Let's take my model as an exemple:

CODE


model state1
  Real x;
  connector1 dout1;
  connector1 din1;
  Boolean resolve(start=true);
  Boolean test; // variable to test (edge(din1.state)
  
equation
test = edge(din1.state); // testing (edge(din1.state)
  if (initial()) then
    if (din1.state) then
    reinit(x,din1.s);
    else
    reinit(x,0.2);
    end if;
    end if;
when edge( din1.state) then // Here i had to detect an event (the passage of din1.state from false to true)
     reinit(x,din1.s);
     end when;

// some other code


Now this is the chart for din1.state and the variable test:
IPB Image


When din1.state changes from false to true.. edge(din1.state) remain equal to false! In an interval of time is not possible to use edge?

Best regards,
Jawhar
wagner
Hello Jawhar,

I think you misunderstood my explanation of the event stuff. Of course Dymola acts correctly with edge and change. It's just that you can't see an event in the resulting chart. You simply can't see a signal change which is only active for one simulation step, because it's infinitively short in time.

Can you reduce your overall package/model to the most relevant parts which is still possible to simulate? Then it's much easier for us to get an idea of your problem.

Regards

Florian
Jawhar
QUOTE(wagner @ Feb 8 2007, 03:06 PM) *

Hello Jawhar,

I think you misunderstood my explanation of the event stuff. Of course Dymola acts correctly with edge and change. It's just that you can't see an event in the resulting chart. You simply can't see a signal change which is only active for one simulation step, because it's infinitively short in time.

Can you reduce your overall package/model to the most relevant parts which is still possible to simulate? Then it's much easier for us to get an idea of your problem.

Regards

Florian


okey, thank you Florian for your answers.

I have two models. One model send an event to the other model to activate it. A variable in model1 changes from false to true, when this changes happens the second model2 had to detect this event (read it from connector). This is the model1 wich represent the state1 of a real system.

CODE

model state1
  Real x;
  connector1 dout1; // two attributes (s: real and state: boolean)
  connector1 din1;// two attributes (s: real and state: boolean)
  Boolean resolve(start=true);

  
equation

  if (initial()) then
    if (din1.state) then
    reinit(x,din1.s);
    else
    reinit(x,0.2);
    end if;
    end if;
  
     when edge( din1.state) then // if din1.state changes i had to reinitialise the x variable because i will use it in the differential equation later.
     reinit(x,din1.s); // The problem here is that edge(din1.state) have the same result (false) so i can't detect the event and reinitialise the variabe x.
     end when;
  
  if (resolve) then
    der(x)=2*x;
  else
    der(x)=0;
  end if;
  
  when (x>0.6) then
    resolve=false;
    dout1.state = true;
  end when;
  dout1.s = x;
end state1;



I hope that is more clear.. i think i had to detect this event without using edge function. sad.gif

Best regards,
Jawhar.
wagner
Hello Jawhar,

I built a small example. I think it works. I tested it using Dymola 6.0d.

CODE

connector connector1
  Real s;
  Boolean state;
end connector1;


CODE

model ChangeStateModel
  connector1 out;
  
  Real x;
  
equation
  if (initial()) then
    out.state=false;
  end if;
  
  x=sin(time*100);
  
  when
      (time>0.5) then
    out.state=true;
  end when;
  
  out.s=x;
end ChangeStateModel;


CODE

model Test
  state1 m1;
  ChangeStateModel m2;
equation
  connect(m1.din1,m2.out);
  
end Test;


If you simulate for one second you will see a jump in x at time 0.5 s from 0.54 to -0.26 (cf. Picture).


Regards

Florian
Jawhar
Hello Florian

I succeeded to run my example within the edge function. I have done a simple error.
Your last example is simple and clear. It helped me much.

Thank you for your assistance Florian!

Best regards,

Jawhar
Jawhar
I hope that I do not disturb you by my questions.

I want to create an event when a variable exceed a value. Here, when z exceed 2.12 i want to create a jump.. I want that my variable passes from false to true when z>2.12 but don't remain equal to true..Just a jump! Is there any Modelica function to use it?

CODE

  when z > 2.12 then
  dout2.state = true; // here i create an event, dout2.state passes from false to true but remain equal to true.
  dout2.s =z;
  end when;


Thank you..
wagner
Hello Jawhar,

the easiest way to do this is using an algorithm part in your model.


CODE

model eventGeneration
  Real z (start=0);

  output Boolean event;
  
  Integer eventVisualizer (start=0);


equation
  der(z)=1;

algorithm
  event=false;

  when z>0.5 then
    event=true;
  end when;
    
  if (event) then
    eventVisualizer=eventVisualizer+1;
  end if;
end eventGeneration;



Regards

Florian
Jawhar
Hello Florian,

I think that what you had present is a counter of events (correct me if i am wrong) or i did not understand your example. What i want to do is to reinitialize the variable dout2.state to false. I want to do something like this:

CODE

// dout2.state = false at the beginning of the simulation

  when z > 2.12 then
  dout2.state = true;// jump from false to true
  dout2.s =z;
  end when;
  
when edge(dout2.state) then // when we detect the jump
dout2.state = false; // we reinitialize dout2.state to false (initial value) <-- problem of compilation here!
end when;



So, if this code works.. in another model i can detect an event by using the edge function.

This code don't work. Error message: singularity..

Regards,
Jawhar
wagner
Hello Jawhar,

the variable event is your event. It is true for one simulation step. But as you can't see the event in a chart, I implemented the eventVisualizer. Just replace the variable event with dout.state and you have your model.

Regards

Florian
Jawhar
QUOTE(wagner @ Feb 12 2007, 03:13 PM) *

Hello Jawhar,

the variable event is your event. It is true for one simulation step. But as you can't see the event in a chart, I implemented the eventVisualizer. Just replace the variable event with dout.state and you have your model.

Regards

Florian


Hello Florian,

I had used eventVisualizer.. and i had used also change(eventVisualizer) to create events..all is working.
Thank you very much.

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.