# WhenEvent error in NDSolve

I’m trying to solve a discontinuous differential equation where there are lost of energy when the trajectory cross the discontinuous manifold. The code I used was

sol1 = NDSolve[{x'[t] == y[t], y'[t] == x[t] - Sign[x[t]], x[0] == 0, y[0] == 0.5,
WhenEvent[
x[t] == 0, {y[t] -> 0.5*y[t]}]}, {x,
y}, {t, 0, 4}];
ParametricPlot[Evaluate[{x[t], x'[t]} /. sol1], {t, 0, 4}, PlotRange -> {{-0.8, 0.8}, {-1, 1}}]


The function WhenEvent it should realize the lost of energy when trajectory cross discontinuous. But when we use the Sign[x] this not happen. For instance, let take the code

sol2 = NDSolve[{x'[t] == y[t], y'[t] == -x[t], x[0] == 0, y[0] == 0.5,
WhenEvent[
x[t] == 0, {y[t] -> 0.5*y[t]}]}, {x,
y}, {t, 0, 4}];
ParametricPlot[Evaluate[{x[t], x'[t]} /. sol2], {t, 0, 4}, PlotRange -> {{-0.8, 0.8}, {-1, 1}}]


In this example the WhenEvent work normally. Anyone know what happen?

The problem is that the "DiscontinuityProcessing" stage of NSolve seems to supersede the WhenEvent. One way is too use use Method -> {"DiscontinuityProcessing" -> False}, but that is highly inefficient. A better way is to prevent Sign from being analyzed by wrapping it in a numeric black box and include a "CrossDiscontinuity" event at x[t] == 0. Both events will be processed. There were some issues (bugs?) I will discuss at the end.

sgn[u_?NumericQ] := Sign[u];
mstep = 0;
nstep = 0;
sol1 = NDSolve[{x'[t] == y[t], y'[t] == x[t] - sgn[x[t]], x[0] == 0,
y[0] == 1/2,
WhenEvent[x[t] == 0, mstep++; "CrossDiscontinuity"],
WhenEvent[x[t] == 0, nstep++; y[t] -> 1/2*y[t]],
WhenEvent[Norm[{x[t], y[t]}] < 1*^-6, "StopIntegration"]
}, {x, y}, {t, 0, 4}(*,WorkingPrecision\[Rule]20*)];
{mstep, nstep}
Evaluate@Flatten[{t, x["Domain"] /. sol1}]
ParametricPlot[Evaluate[{x[t], x'[t]} /. sol1],
Evaluate@Flatten[{t, x["Domain"] /. sol1}],
PlotRange -> {{-0.55, 0.55}, {-0.55, 0.55}}, PlotStyle -> Thick,
ColorFunction -> Function[{x, y, t}, ColorData["Rainbow", t]]]
(*
{18, 18}
{t, 0., 2.11063}
*)


The integration finishes in a finite amount of time (the solutions approaches the origin in finite time). The number of crossings seems to approach infinity, so I included a stopping criterion as an event. The number of crossings detected depends on WorkingPrecision, perhaps because as the solution approaches the origin, more precision is needed. The worst or most mystifying behavior is that the event actions

"CrossSlidingDiscontinuity"          (* simple String *)
Null; "CrossSlidingDiscontinuity"    (* CompoundExpression *)


and

y[t] -> 1/2*y[t]                     (* plain Rule *)
Null; y[t] -> 1/2*y[t]               (* CompoundExpression *)


produced different results. Indeed, the simpler, first actions, missed many events. The compound expressions seem to catch them all (although issues with precision seem to arise as the solution becomes very small). In the code above, I left some diagnostic step-counters. It seems any CompoundExpression causes all the events to be detected.