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})*

5 comments:

  1. This should probably go on a wiki of sorts. I've long wanted a "Create Your Own 99 Bottles of Beer On the Wall" cookie cutter website, so that language designers have a place to go to in order to find the most common programming challenge problems for languages, such as same-fringe.

    ReplyDelete
  2. Hi John!

    A wiki would have been cool, indeed. Right now I don't have the cycles to maintain a wiki, but it's definitely something in the back of my mind.

    ReplyDelete
  3. I might whip something up at some point. Awhile back I was interested in what classical benchmarks for parallelism and concurrency there were. For example, I found a book on "The Salishan Problems" (ISBN-13 978-0444881359) that compared I think 6 different languages implementing the same 4 problems. It would be nice if that knowledge was freely available at a site designed similarly to http://99-bottles-of-beer.net/ but targeted toward more higher-level discussion, including whether the benchmarks are good or not and how they might degrade or be biased towards certain languages. The idea would be that it would work like Facebook originally worked, where to sign up you had to have a *.edu or equivalent e-mail address and if you were to post a solution everyone would know who you are and to what degree you were trying to do micro-optimizations and to what degree you were just showing off how good your language was. And maybe people could "Like" your implementation and their name would be next to your code as publicly liking it.

    I wasn't really suggesting you do this, just floating the idea out there so it can be discussed.

    ReplyDelete
  4. Hi John!

    I think it would definitely be nice to have something like facebook for technical things (but also separate to so that fb stays for non-work things). Not quite sure where the best place to start might be. A related thing that I have on my wish list is some programming framework that would let me easily create interactive programs for students to study/play around with. Initially I thought wikis could be useful for these kinds of things. But they kind of turn out to be good mostly for documentation, and one really needs to work to encourage/enforce a work flow on top...

    Walid.

    ReplyDelete
  5. Some people have tried wikis before.

    I think a sub-project of the Maude research project at UIUC had a wiki where you could submit code to and play with an interpreter written in Maude, but can't be sure...

    Then of course there is Haskell's wiki.

    Then there is that math project where the goal is to type-set math formulas in a wiki and also possibly use math environments to evaluate them... can't remember the name.

    ReplyDelete

Found this post useful? Please leave a comment here!