Use of case switch

Moderators: Frank, paul, Hiwot, Chris, peter

bendesarts
Posts: 49
Joined: Thu Mar 07, 2013 10:10 am

Use of case switch

Postby bendesarts » Sat Jul 19, 2014 3:17 pm

Hello,

In my code, i would like to use "case switch" because i find that the lisibility is better than several loops with if then else. However, I have some issues with the use of "case swith". My code with use case doesn't work. Do you have a idea why ? To help troubleshooting, i put my code with "use case" which doesn't work and my code with "If then else" which works.

Moreover, i suppose that it is due to the fact that i don't use the conditions well. How a condition with 2 parts can be written ? For example, how can i write the condition "x<3 and x>2" ?

Code with use case

Code: Select all

parameters
   real F=1000;
   real start_time = 5.0 {s};
   real stop_time = 25.0 {s};
   real f_start = 5.0 {frequency, Hz};
   real f_stop = 25.0 {frequency, Hz};
   real omega_start;
   real omega_stop;
variables
   real omega {frequency, rad/s};     // current frequency {rad/s}
   real freq {frequency, Hz};      
   real hidden t;
   real hidden dw;
   real hidden dt;
   real hidden dwdt;
   real hidden dwdt2;
   boolean hidden change;
initialequations
   omega_start = (2*pi)*f_start;
   omega_stop = (2*pi)*f_stop;
   dw = omega_stop - omega_start;
   dt = stop_time - start_time;
   dwdt = dw/dt;
   dwdt2 = dwdt/2;
equations
   "calculate at least at the start and stop time"
   change = timeevent (start_time) or timeevent (stop_time);
   
   omega = omega_start + t * dwdt;
   freq=omega/(2*pi);

   switch time
   case time < start_time do
         FF = F*sin( time * omega_start );
   case time > stop_time do
         FF = F*sin( time * omega_stop );
   default do
         FF = F*sin( (time-start_time) *( omega_start + (time-start_time) * dwdt2  )  );
   end;



Code with If then else loops

Code: Select all

parameters
   real F=1000;
   real start_time = 5.0 {s};
   real stop_time = 25.0 {s};
   real f_start = 5.0 {frequency, Hz};
   real f_stop = 25.0 {frequency, Hz};
   real omega_start;
   real omega_stop;
variables
   real omega {frequency, rad/s};     // current frequency {rad/s}
   real freq {frequency, Hz};      
   real hidden t;
   real hidden dw;
   real hidden dt;
   real hidden dwdt;
   real hidden dwdt2;
   boolean hidden change;
initialequations
   omega_start = (2*pi)*f_start;
   omega_stop = (2*pi)*f_stop;
   dw = omega_stop - omega_start;
   dt = stop_time - start_time;
   dwdt = dw/dt;
   dwdt2 = dwdt/2;
equations
   "calculate at least at the start and stop time"
   change = timeevent (start_time) or timeevent (stop_time);
   
   omega = omega_start + (time - start_time) * dwdt;
   freq=omega/(2*pi);

   FF = if time < start_time then
      F*(time/start_time)*sin( time * omega_start )  //(time/start_time)
   else
      if time > stop_time then
         F*sin( time * omega_stop )
      else
         F*sin( (time - start_time)*( omega_start + (time - start_time) * dwdt2  )  )
      end   
   end;

Frank
Posts: 23
Joined: Wed Sep 28, 2005 9:32 am
Location: Controllab Products B.V.

Re: Use of case switch

Postby Frank » Tue Sep 16, 2014 10:30 am

Switch-case only works in the way the C-Style switch case works:
The argument in the switch is compared to the cases.

So in your example it would result in the following false checks:

Code: Select all

time == time < start_time

which is false

Code: Select all

time == time > stop_time

which is also false

So your situation can only be made with the nested if-then-else statements.
We are thinking of implementing an elsif statement in order to have conditional expression more easily
member 20-sim development team


Return to “Languages”

Who is online

Users browsing this forum: No registered users and 1 guest