Sunday, February 19, 2012

Breaking ball in Yampa

After last week's initial attempt at implementing a breaking ball simulation in Yampa, Adam set out to familiarize himself with Yampa's switching combinators, starting with the basics. As mentioned in Henrik Nilsson's presentation accompanying the paper Functional Reactive Programming, continued, the simplest way to form a collection of signal functions (SFs) in Yampa is to use parB. This combinator uses the same input for all SFs in the collection, and also does not provide any way to modify the collection during execution. The pSwitchB combinator provides a way to update the collection (through its third parameter), but still uses the same input for each SF in the collection – the suffix B of the names of both of these combinators stands for "broadcast" and indicates the latter. The pSwitch combinator extends pSwitchB by providing a way to match (potentially unique) inputs with each signal function, through its first parameter – the routing function.

pSwitch :: Functor col =>
  -- Routing function, matches inputs with SFs in collection
  (forall sf . (a -> col sf -> col (b, sf)))
  -- Collection of SFs, used until next update
  -> SF (a, col c) (Event d)
  -- Produces an event if SF collection needs updating
  -> col (SF b c)
  -- Function to create updated collection of SFs  
  -> (col (SF b c) -> d -> SF a (col c))
  -- Resulting SF corresponding to pSwitch
  -> SF a (col c)

The system Adam set out to model was one where a ball is dropped from a height of 10 m (with a gravity constant of -10 m/s/s). Upon hitting the ground, the ball breaks into two new balls (bouncing with COR 0.75 and 0.5, respectively), and the process repeats.

Using pSwitch, it was possible to implement the simulation but, as can be seen in the plot below, a puzzling behavior appears in the output beginning at the Zeno point (~ 4.3) of the subsystem consisting of the "bottom" set of balls (corresponding to COR 0.5). Analytically, this is where the number of bounces approaches infinity. Adam will continue to investigate this behavior, he currently suspects that it has to do with the way Yampa samples the collection of SFs.