# How can I identify which solution triggered the WhenEvent?

I am using NDSolve for problems like this:

sols = NDSolve[{(1 + 25 Exp[- u[s]^2] u[s]^2) u'[s]^2 == u[s]^2, u == 0.5,
WhenEvent[u[s] == 4, "StopIntegration"]}, u, {s, 0, 20}]

Plot[u[s] /. sols, {s, 0, 5}, Evaluated -> True] There are multiple solution branches, I am only interested in the one which was stopped by the WhenEvent, i.e. the one which reaches $u=4$ for some value of $s$.

Is there a convenient way to restrict the solution set to only those ones for which the WhenEvent was triggered?

• This doesn't seem like a very scalable answer, but what about something like Select[u /. sols, #[#["Domain"][[1, 2]]] == 4 &] ? – Chip Hurst Apr 26 '16 at 18:12
• @ChipHurst, it might be the best way nevertheless. Thanks for the suggestion. – Simon Woods Apr 26 '16 at 18:30

This might be useful if your actual event is more complicated, use a discrete variable as a flag:

sols = NDSolve[{(1 + 25 Exp[-u[s]^2] u[s]^2) u'[s]^2 == u[s]^2,
u == 0.5, a == 1,
WhenEvent[u[s] == 4, a[s] -> 0],
WhenEvent[a[s] == 0, "StopIntegration"]
}, {u, a}, {s, 0, 20},
DiscreteVariables -> {a[s]}]

u /. Select[sols, Last@((a /. #)["ValuesOnGrid"]) == 0 &]


I think the method suggested by @ChipHurst in comment is better for this example though.