Skip to content

Events ​

An event is a zero of a function of the solution: an impact, a threshold crossed, a time defined by the state. event_locator finds the zeros inside a step, at the scheme's accuracy, on the step's dense output: the step is not shortened, and the step-size control does not see it.

fortran
do while (impacts < 3)
   call st%begin_step(bx, u)
   do k=1, st%stages()
      call st%begin_stage(k, bx, DT, u)
      if (st%stage_state_is_q(k)) then ; us => u ; else ; call st%stage_state_view(us) ; endif
      call st%rhs_register_view(r)
      r = [us(2), -G]                                        ! the caller's residual
      call st%end_stage(k, bx, DT, u)
   enddo
   call st%finish_step(bx, DT, u)
   call ev%step(t, DT, g_of(u))                              ! the events' values at the step's end
   do while (ev%locating())                                  ! a zero in this step: the locator asks for trial points
      call st%dense_output(ev%next_theta(), bx, DT, u, out)
      call ev%update(g_of(out))
      if (ev%event() == 0) cycle
      call st%dense_output(ev%event_theta(), bx, DT, u, out)
      print '(I3,F12.6,2F11.5)', ev%event(), ev%event_time(), out
      if (ev%is_terminal()) exit
      call ev%resume                                         ! not terminal: the rest of the step
   enddo
   if (ev%event() > 0 .and. ev%is_terminal()) then           ! the impact: restart from it, bounced
      impacts = impacts + 1
      u = [0._R_P, -E * out(2)] ; t = ev%event_time()
      call ev%start(g_of(u))
   else
      t = t + DT
   endif
enddo
text
$ cookbook_events
event   t           height     velocity
  2    0.319275    0.50000   -3.13209
  1    0.451524   -0.00000   -4.42945
  2    0.643798    0.50000    1.65735
  2    0.981687    0.50000   -1.65735
  1    1.173961   -0.00000   -3.54356
  1    1.751912   -0.00000   -2.83485
  • Your functions. The locator holds no field. You evaluate each event function, a reduction over your boxes and ranks if it needs one, at the end of each step (step(t, dt, g)) and at each trial point it hands out. Inside a step it asks for next_theta(); you evaluate the functions on dense_output(theta, ...) or hermite_output and pass them to update(g), until locating() is false.
  • Directions and terminal events. initialize(n, direction, terminal, tol): direction +1 for a rising zero only, -1 for a falling one, 0 for both; a terminal event ends the step there (is_terminal()): restart from dense_output(event_theta(), ...) at event_time() and call start(g). After a non-terminal event, resume() looks for the next zero in the rest of the step: two zeros in one step are both found, in order.
  • The iteration. It is the Illinois variant of the false position (Dowell & Jarratt 1971) on the event whose secant crosses zero earliest, as Shampine, Gladwell and Brankin (1991) locate events. A trial point within tol of an end goes tol inside it, so the zero ends bracketed within tol (a fraction of the step). At tol = 1e-10, selah_test_events locates each zero of cos⁡t in at most 5 trial points.
  • Its accuracy. A located time is a zero of the dense output: its error is the interpolant's, divided by the function's slope. The 20 zeros of cos⁡t at dopri-54 with a step of 0.1 are within 3⋅10−8, and a bouncing ball's impacts within 10−10 (the extension reproduces the parabola).