A couple of years ago I asked a question about solving n-body systems with NDSolve and detecting collisions with WhenEvent. I received an excellent answer, but unfortunately that code does not work anymore in Mathematica 10.2.0.
In particular, in v9 the integration continued after the collision (which occurs at t=2.39343) and properly updated the modified masses and velocities of particles, as can be seen in the resulting trajectories plot:
ParametricPlot3D[Evaluate[#@t & /@ pos], {t, 2.25, 2.43},
PlotRange -> All, ImageSize -> 500, PlotPoints -> 500,
MaxRecursion -> 15, Boxed -> False, Axes -> False]
In Mathematica 10, however, after the collision, the system falls apart and NDSolve can't produce sensible trajectories anymore - the parametric plot now looks like this:
How to fix the code in the linked answer for Mathematica 10 so that it will properly detect the collisions and update the masses and velocities of particles?


pids[[1]]is being evaluated beforepidsis defined. If I run the code a second time, it goes through without an error. The plot I get is the same as you V9 plot (V10.2, Mac OS): http://i.stack.imgur.com/J0oIL.png – Michael E2 Nov 24 '15 at 14:44pids = {1, 2};, beforewhenevnt, it works fine. If I wrap the second argument ofWhenEventinModule[{pids},...], the error refers topids$[[1]], which shows thatNDSolveis inspecting the held code, sincepids$does not have a module number at the end. The error then gives a stiffness/singularity warning and integration stops at the collision. In that case, I get your V10 graph. Try the initialization dodge. See if it works. – Michael E2 Nov 24 '15 at 15:05pids[[1]]happens,NDSolvedecides to ignore the event. Seems like a bug to me. At the least, there ought to be a message warning the user that there was an error in the event and that the event is being ignored (assuming it is ignored and not merely misfiring). – Michael E2 Nov 24 '15 at 15:11NDSolve`ProcessEquationsstage, which is not surprising. – Michael E2 Nov 24 '15 at 15:31pidinitialization trick like @MichaelE2 suggested. If I dont, I get the same (wrong) result as shown in the question. – shrx Nov 24 '15 at 17:12NDSolve::ndsz: At t == 2.3934322946201605, step size is effectively zero; singularity or stiff system suspected. >>and integration stops there. Collision is still detected, but I get garbage trajectories as shown in OP. – shrx Nov 24 '15 at 18:08t == 2.3934322946201605-- use the plotting domain{t, 2.25, 2.3934322946201605`}and you'll see something reasonable. – Michael E2 Nov 24 '15 at 19:24WhenEvent). – shrx Nov 24 '15 at 19:26pidsdoes not always work, then? (It sounded that way, but I wasn't sure.) – Michael E2 Nov 24 '15 at 19:37