Tuesday, October 26, 2010

Bouncing ball example in your favorite language

This is a request for audience participation!

The code below is a minimalist model defining the behavior of a ball subject to gravity, air resistance, and idealized bouncing.  I'd like to use this example to collect in one place code written in different hybrid modeling languages.  It would be great if you can send me (or add as comment) equivalent code written in your favorite hybrid modeling language, or for that matter any hybrid modeling language that you are familiar with.  As much as possible, please follow the same style and avoid making stylistic changes to the extent possible.  Please free to provide multiple versions if necessary, and to offer explanation of your stylistic choices as needed.

If you happen to know Verilog-AMS, PSPICE, Modelica, Sol, MapleSim, or Impromptu, I would really appreciate if you can express this model in those languages as well.  All other languages are very welcome as well!

Here's the code:


// Minimalist model for ball with airodynamic
//  resistance (2010/10/26, Scottsdale)


class Main (simulator)
 private
  a = create BouncingBall()
 end
end


class BouncingBall ()
 private
  g = -9.8; cr = 0.95; cd = 0.075;
  x = 10; x' = 2; x'' = 0;
 end


 if x>=0
  if x'<0
   x'' [=] g + cd * (x')^2
  else 
   x'' [=] g - cd * (x')^2
  end
 else
  x' = - cr * x';
  x  = 0
 end
end


Updates:


From Andre Platzer:


Here's the bouncing ball as a hybrid program:

(if (x=0) then v := -r*v fi;
 ({x'=v,v'=g+d*v^2,v<=0,x>=0}
++{x'=v,v'=g-d*v^2,v>=0,x>=0}))*


Here's the bouncing ball as a differential-algebraic program:

(if (x=0) then 
v := -r*v fi;
 {x'=v,((v'=g+d*v^2&v<=0)
       |(v'=g-d*v^2&v>=0)), x>=0})*