It reminds me of this behavior:
sol = NDSolveValue[{y'[x] == 0, y[0.5] == 0.,
WhenEvent[x == 0, y[x] -> y[x] + 1]}, y, {x, -1, 1}]
Plot[sol[x], {x, -1, 1}]
The problem, to which in the case of WhenEvent[]
I have grown jadedly accustomed, is that the time integration direction is not accounted for in the internal event structure. So whether the integration passes the event with time moving forward or moving backward, +1 is added to the solution. NDSolve
starts the integration at the initial condition. It integrates backward to the beginning of the interval and forward to the end of the interval. This means one might have to rewrite the event actions if one changes the initial condition to start on the other side of an event.
The simplest fix would be to put a minus sign in front DiracDelta[x]
. Another would be to multiply by the integration signature Sign[x - x0]
:
NDSolveValue[{y'[x] == Sign[x - 0.5] DiracDelta[-x],
y[0.5] == 0.}, y, {x, -1, 1}]